Coverage Report

Created: 2025-07-11 06:57

/src/openssl/crypto/asn1/a_mbstr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include "crypto/ctype.h"
12
#include "internal/cryptlib.h"
13
#include "internal/unicode.h"
14
#include <openssl/asn1.h>
15
16
static int traverse_string(const unsigned char *p, int len, int inform,
17
                           int (*rfunc) (unsigned long value, void *in),
18
                           void *arg);
19
static int in_utf8(unsigned long value, void *arg);
20
static int out_utf8(unsigned long value, void *arg);
21
static int type_str(unsigned long value, void *arg);
22
static int cpy_asc(unsigned long value, void *arg);
23
static int cpy_bmp(unsigned long value, void *arg);
24
static int cpy_univ(unsigned long value, void *arg);
25
static int cpy_utf8(unsigned long value, void *arg);
26
27
/*
28
 * These functions take a string in UTF8, ASCII or multibyte form and a mask
29
 * of permissible ASN1 string types. It then works out the minimal type
30
 * (using the order Numeric < Printable < IA5 < T61 < BMP < Universal < UTF8)
31
 * and creates a string of the correct type with the supplied data. Yes this is
32
 * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
33
 * size limits too.
34
 */
35
36
int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
37
                       int inform, unsigned long mask)
38
0
{
39
0
    return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
40
0
}
41
42
int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
43
                        int inform, unsigned long mask,
44
                        long minsize, long maxsize)
45
0
{
46
0
    int str_type;
47
0
    int ret;
48
0
    char free_out;
49
0
    int outform, outlen = 0;
50
0
    ASN1_STRING *dest;
51
0
    unsigned char *p;
52
0
    int nchar;
53
0
    int (*cpyfunc) (unsigned long, void *) = NULL;
54
0
    if (len == -1) {
55
0
        size_t len_s = strlen((const char *)in);
56
57
0
        if (len_s >= INT_MAX) {
58
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
59
0
            return -1;
60
0
        }
61
0
        len = (int)len_s;
62
0
    }
63
0
    if (!mask)
64
0
        mask = DIRSTRING_TYPE;
65
0
    if (len < 0) {
66
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
67
0
        return -1;
68
0
    }
69
70
    /* First do a string check and work out the number of characters */
71
0
    switch (inform) {
72
73
0
    case MBSTRING_BMP:
74
0
        if (len & 1) {
75
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
76
0
            return -1;
77
0
        }
78
0
        nchar = len >> 1;
79
0
        break;
80
81
0
    case MBSTRING_UNIV:
82
0
        if (len & 3) {
83
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
84
0
            return -1;
85
0
        }
86
0
        nchar = len >> 2;
87
0
        break;
88
89
0
    case MBSTRING_UTF8:
90
0
        nchar = 0;
91
        /* This counts the characters and does utf8 syntax checking */
92
0
        ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
93
0
        if (ret < 0) {
94
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
95
0
            return -1;
96
0
        }
97
0
        break;
98
99
0
    case MBSTRING_ASC:
100
0
        nchar = len;
101
0
        break;
102
103
0
    default:
104
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
105
0
        return -1;
106
0
    }
107
108
0
    if ((minsize > 0) && (nchar < minsize)) {
109
0
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_SHORT,
110
0
                       "minsize=%ld", minsize);
111
0
        return -1;
112
0
    }
113
114
0
    if ((maxsize > 0) && (nchar > maxsize)) {
115
0
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG,
116
0
                       "maxsize=%ld", maxsize);
117
0
        return -1;
118
0
    }
119
120
    /* Now work out minimal type (if any) */
121
0
    if (traverse_string(in, len, inform, type_str, &mask) < 0) {
122
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_CHARACTERS);
123
0
        return -1;
124
0
    }
125
126
    /* Now work out output format and string type */
127
0
    outform = MBSTRING_ASC;
128
0
    if (mask & B_ASN1_NUMERICSTRING)
129
0
        str_type = V_ASN1_NUMERICSTRING;
130
0
    else if (mask & B_ASN1_PRINTABLESTRING)
131
0
        str_type = V_ASN1_PRINTABLESTRING;
132
0
    else if (mask & B_ASN1_IA5STRING)
133
0
        str_type = V_ASN1_IA5STRING;
134
0
    else if (mask & B_ASN1_T61STRING)
135
0
        str_type = V_ASN1_T61STRING;
136
0
    else if (mask & B_ASN1_BMPSTRING) {
137
0
        str_type = V_ASN1_BMPSTRING;
138
0
        outform = MBSTRING_BMP;
139
0
    } else if (mask & B_ASN1_UNIVERSALSTRING) {
140
0
        str_type = V_ASN1_UNIVERSALSTRING;
141
0
        outform = MBSTRING_UNIV;
142
0
    } else {
143
0
        str_type = V_ASN1_UTF8STRING;
144
0
        outform = MBSTRING_UTF8;
145
0
    }
146
0
    if (!out)
147
0
        return str_type;
148
0
    if (*out) {
149
0
        free_out = 0;
150
0
        dest = *out;
151
0
        ASN1_STRING_set0(dest, NULL, 0);
152
0
        dest->type = str_type;
153
0
    } else {
154
0
        free_out = 1;
155
0
        dest = ASN1_STRING_type_new(str_type);
156
0
        if (dest == NULL) {
157
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
158
0
            return -1;
159
0
        }
160
0
        *out = dest;
161
0
    }
162
    /* If both the same type just copy across */
163
0
    if (inform == outform) {
164
0
        if (!ASN1_STRING_set(dest, in, len)) {
165
0
            if (free_out) {
166
0
                ASN1_STRING_free(dest);
167
0
                *out = NULL;
168
0
            }
169
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
170
0
            return -1;
171
0
        }
172
0
        return str_type;
173
0
    }
174
175
    /* Work out how much space the destination will need */
176
0
    switch (outform) {
177
0
    case MBSTRING_ASC:
178
0
        outlen = nchar;
179
0
        cpyfunc = cpy_asc;
180
0
        break;
181
182
0
    case MBSTRING_BMP:
183
0
        outlen = nchar << 1;
184
0
        cpyfunc = cpy_bmp;
185
0
        break;
186
187
0
    case MBSTRING_UNIV:
188
0
        outlen = nchar << 2;
189
0
        cpyfunc = cpy_univ;
190
0
        break;
191
192
0
    case MBSTRING_UTF8:
193
0
        outlen = 0;
194
0
        traverse_string(in, len, inform, out_utf8, &outlen);
195
0
        cpyfunc = cpy_utf8;
196
0
        break;
197
0
    }
198
0
    if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
199
0
        if (free_out) {
200
0
            ASN1_STRING_free(dest);
201
0
            *out = NULL;
202
0
        }
203
0
        return -1;
204
0
    }
205
0
    dest->length = outlen;
206
0
    dest->data = p;
207
0
    p[outlen] = 0;
208
0
    traverse_string(in, len, inform, cpyfunc, &p);
209
0
    return str_type;
210
0
}
211
212
/*
213
 * This function traverses a string and passes the value of each character to
214
 * an optional function along with a void * argument.
215
 */
216
217
static int traverse_string(const unsigned char *p, int len, int inform,
218
                           int (*rfunc) (unsigned long value, void *in),
219
                           void *arg)
220
0
{
221
0
    unsigned long value;
222
0
    int ret;
223
0
    while (len) {
224
0
        if (inform == MBSTRING_ASC) {
225
0
            value = *p++;
226
0
            len--;
227
0
        } else if (inform == MBSTRING_BMP) {
228
0
            value = *p++ << 8;
229
0
            value |= *p++;
230
0
            len -= 2;
231
0
        } else if (inform == MBSTRING_UNIV) {
232
0
            value = ((unsigned long)*p++) << 24;
233
0
            value |= ((unsigned long)*p++) << 16;
234
0
            value |= *p++ << 8;
235
0
            value |= *p++;
236
0
            len -= 4;
237
0
        } else {
238
0
            ret = UTF8_getc(p, len, &value);
239
0
            if (ret < 0)
240
0
                return -1;
241
0
            len -= ret;
242
0
            p += ret;
243
0
        }
244
0
        if (rfunc) {
245
0
            ret = rfunc(value, arg);
246
0
            if (ret <= 0)
247
0
                return ret;
248
0
        }
249
0
    }
250
0
    return 1;
251
0
}
252
253
/* Various utility functions for traverse_string */
254
255
/* Just count number of characters */
256
257
static int in_utf8(unsigned long value, void *arg)
258
0
{
259
0
    int *nchar;
260
261
0
    if (!is_unicode_valid(value))
262
0
        return -2;
263
0
    nchar = arg;
264
0
    (*nchar)++;
265
0
    return 1;
266
0
}
267
268
/* Determine size of output as a UTF8 String */
269
270
static int out_utf8(unsigned long value, void *arg)
271
0
{
272
0
    int *outlen, len;
273
274
0
    len = UTF8_putc(NULL, -1, value);
275
0
    if (len <= 0)
276
0
        return len;
277
0
    outlen = arg;
278
0
    *outlen += len;
279
0
    return 1;
280
0
}
281
282
/*
283
 * Determine the "type" of a string: check each character against a supplied
284
 * "mask".
285
 */
286
287
static int type_str(unsigned long value, void *arg)
288
0
{
289
0
    unsigned long types = *((unsigned long *)arg);
290
0
    const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value);
291
292
0
    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native)
293
0
                                            || native == ' '))
294
0
        types &= ~B_ASN1_NUMERICSTRING;
295
0
    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
296
0
        types &= ~B_ASN1_PRINTABLESTRING;
297
0
    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
298
0
        types &= ~B_ASN1_IA5STRING;
299
0
    if ((types & B_ASN1_T61STRING) && (value > 0xff))
300
0
        types &= ~B_ASN1_T61STRING;
301
0
    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
302
0
        types &= ~B_ASN1_BMPSTRING;
303
0
    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
304
0
        types &= ~B_ASN1_UTF8STRING;
305
0
    if (!types)
306
0
        return -1;
307
0
    *((unsigned long *)arg) = types;
308
0
    return 1;
309
0
}
310
311
/* Copy one byte per character ASCII like strings */
312
313
static int cpy_asc(unsigned long value, void *arg)
314
0
{
315
0
    unsigned char **p, *q;
316
0
    p = arg;
317
0
    q = *p;
318
0
    *q = (unsigned char)value;
319
0
    (*p)++;
320
0
    return 1;
321
0
}
322
323
/* Copy two byte per character BMPStrings */
324
325
static int cpy_bmp(unsigned long value, void *arg)
326
0
{
327
0
    unsigned char **p, *q;
328
0
    p = arg;
329
0
    q = *p;
330
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
331
0
    *q = (unsigned char)(value & 0xff);
332
0
    *p += 2;
333
0
    return 1;
334
0
}
335
336
/* Copy four byte per character UniversalStrings */
337
338
static int cpy_univ(unsigned long value, void *arg)
339
0
{
340
0
    unsigned char **p, *q;
341
0
    p = arg;
342
0
    q = *p;
343
0
    *q++ = (unsigned char)((value >> 24) & 0xff);
344
0
    *q++ = (unsigned char)((value >> 16) & 0xff);
345
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
346
0
    *q = (unsigned char)(value & 0xff);
347
0
    *p += 4;
348
0
    return 1;
349
0
}
350
351
/* Copy to a UTF8String */
352
353
static int cpy_utf8(unsigned long value, void *arg)
354
0
{
355
0
    unsigned char **p;
356
0
    int ret;
357
0
    p = arg;
358
    /* We already know there is enough room so pass 0xff as the length */
359
0
    ret = UTF8_putc(*p, 0xff, value);
360
0
    *p += ret;
361
0
    return 1;
362
0
}