Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/asn1/a_mbstr.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2026 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
10.2M
{
39
10.2M
    return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
40
10.2M
}
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
8.65M
{
46
8.65M
    int str_type;
47
8.65M
    int ret;
48
8.65M
    char free_out;
49
8.65M
    int outform, outlen = 0;
50
8.65M
    ASN1_STRING *dest;
51
8.65M
    unsigned char *p;
52
8.65M
    int nchar;
53
8.65M
    int (*cpyfunc)(unsigned long, void *) = NULL;
54
8.65M
    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
8.65M
    if (!mask)
64
0
        mask = DIRSTRING_TYPE;
65
8.65M
    if (len < 0) {
66
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
67
0
        return -1;
68
8.65M
    } else if (len >= INT_MAX) {
69
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
70
0
        return -1;
71
0
    }
72
73
    /* First do a string check and work out the number of characters */
74
8.65M
    switch (inform) {
75
76
1.21M
    case MBSTRING_BMP:
77
1.21M
        if (len & 1) {
78
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
79
0
            return -1;
80
0
        }
81
1.21M
        nchar = len >> 1;
82
1.21M
        break;
83
84
1.14M
    case MBSTRING_UNIV:
85
1.14M
        if (len & 3) {
86
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
87
0
            return -1;
88
0
        }
89
1.14M
        nchar = len >> 2;
90
1.14M
        break;
91
92
1.48M
    case MBSTRING_UTF8:
93
1.48M
        nchar = 0;
94
        /* This counts the characters and does utf8 syntax checking */
95
1.48M
        ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
96
1.48M
        if (ret < 0) {
97
23.2k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
98
23.2k
            return -1;
99
23.2k
        }
100
1.46M
        break;
101
102
4.80M
    case MBSTRING_ASC:
103
4.80M
        nchar = len;
104
4.80M
        break;
105
106
0
    default:
107
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
108
0
        return -1;
109
8.65M
    }
110
111
8.63M
    if ((minsize > 0) && (nchar < minsize)) {
112
0
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_SHORT,
113
0
            "minsize=%ld", minsize);
114
0
        return -1;
115
0
    }
116
117
8.63M
    if ((maxsize > 0) && (nchar > maxsize)) {
118
0
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG,
119
0
            "maxsize=%ld", maxsize);
120
0
        return -1;
121
0
    }
122
123
    /* Now work out minimal type (if any) */
124
8.63M
    if (traverse_string(in, len, inform, type_str, &mask) < 0) {
125
3.33k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_CHARACTERS);
126
3.33k
        return -1;
127
3.33k
    }
128
129
    /*
130
     * Now work out output format and string type.
131
     * These checks should be in sync with the checks in type_str.
132
     */
133
8.63M
    outform = MBSTRING_ASC;
134
8.63M
    if (mask & B_ASN1_NUMERICSTRING)
135
0
        str_type = V_ASN1_NUMERICSTRING;
136
8.63M
    else if (mask & B_ASN1_PRINTABLESTRING)
137
0
        str_type = V_ASN1_PRINTABLESTRING;
138
8.63M
    else if (mask & B_ASN1_IA5STRING)
139
0
        str_type = V_ASN1_IA5STRING;
140
8.63M
    else if (mask & B_ASN1_T61STRING)
141
0
        str_type = V_ASN1_T61STRING;
142
8.63M
    else if (mask & B_ASN1_BMPSTRING) {
143
0
        str_type = V_ASN1_BMPSTRING;
144
0
        outform = MBSTRING_BMP;
145
8.63M
    } else if (mask & B_ASN1_UNIVERSALSTRING) {
146
0
        str_type = V_ASN1_UNIVERSALSTRING;
147
0
        outform = MBSTRING_UNIV;
148
8.63M
    } else {
149
8.63M
        str_type = V_ASN1_UTF8STRING;
150
8.63M
        outform = MBSTRING_UTF8;
151
8.63M
    }
152
8.63M
    if (!out)
153
0
        return str_type;
154
8.63M
    if (*out) {
155
8.63M
        free_out = 0;
156
8.63M
        dest = *out;
157
8.63M
        ASN1_STRING_set0(dest, NULL, 0);
158
8.63M
        dest->type = str_type;
159
8.63M
    } else {
160
0
        free_out = 1;
161
0
        dest = ASN1_STRING_type_new(str_type);
162
0
        if (dest == NULL) {
163
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
164
0
            return -1;
165
0
        }
166
0
        *out = dest;
167
0
    }
168
    /* If both the same type just copy across */
169
8.63M
    if (inform == outform) {
170
1.46M
        if (!ASN1_STRING_set(dest, in, len)) {
171
0
            if (free_out) {
172
0
                ASN1_STRING_free(dest);
173
0
                *out = NULL;
174
0
            }
175
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
176
0
            return -1;
177
0
        }
178
1.46M
        return str_type;
179
1.46M
    }
180
181
    /* Work out how much space the destination will need */
182
7.16M
    switch (outform) {
183
0
    case MBSTRING_ASC:
184
0
        outlen = nchar;
185
0
        cpyfunc = cpy_asc;
186
0
        break;
187
188
0
    case MBSTRING_BMP:
189
0
        if (nchar > INT_MAX / 2) {
190
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
191
0
            if (free_out) {
192
0
                ASN1_STRING_free(dest);
193
0
                *out = NULL;
194
0
            }
195
0
            return -1;
196
0
        }
197
0
        outlen = nchar << 1;
198
0
        cpyfunc = cpy_bmp;
199
0
        break;
200
201
0
    case MBSTRING_UNIV:
202
0
        if (nchar > INT_MAX / 4) {
203
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
204
0
            if (free_out) {
205
0
                ASN1_STRING_free(dest);
206
0
                *out = NULL;
207
0
            }
208
0
            return -1;
209
0
        }
210
0
        outlen = nchar << 2;
211
0
        cpyfunc = cpy_univ;
212
0
        break;
213
214
7.16M
    case MBSTRING_UTF8:
215
7.16M
        outlen = 0;
216
7.16M
        ret = traverse_string(in, len, inform, out_utf8, &outlen);
217
7.16M
        if (ret < 0) { /* error already raised in out_utf8() */
218
0
            if (free_out) {
219
0
                ASN1_STRING_free(dest);
220
0
                *out = NULL;
221
0
            }
222
0
            return -1;
223
0
        }
224
7.16M
        cpyfunc = cpy_utf8;
225
7.16M
        break;
226
7.16M
    }
227
7.16M
    if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
228
0
        if (free_out) {
229
0
            ASN1_STRING_free(dest);
230
0
            *out = NULL;
231
0
        }
232
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
233
0
        return -1;
234
0
    }
235
7.16M
    dest->length = outlen;
236
7.16M
    dest->data = p;
237
7.16M
    p[outlen] = 0;
238
7.16M
    traverse_string(in, len, inform, cpyfunc, &p);
239
7.16M
    return str_type;
240
7.16M
}
241
242
/*
243
 * This function traverses a string and passes the value of each character to
244
 * an optional function along with a void * argument.
245
 */
246
247
static int traverse_string(const unsigned char *p, int len, int inform,
248
    int (*rfunc)(unsigned long value, void *in),
249
    void *arg)
250
28.8M
{
251
28.8M
    unsigned long value;
252
28.8M
    int ret;
253
1.72G
    while (len) {
254
1.69G
        if (inform == MBSTRING_ASC) {
255
1.60G
            value = *p++;
256
1.60G
            len--;
257
1.60G
        } else if (inform == MBSTRING_BMP) {
258
70.8M
            value = *p++ << 8;
259
70.8M
            value |= *p++;
260
70.8M
            len -= 2;
261
70.8M
        } else if (inform == MBSTRING_UNIV) {
262
174k
            value = ((unsigned long)*p++) << 24;
263
174k
            value |= ((unsigned long)*p++) << 16;
264
174k
            value |= *p++ << 8;
265
174k
            value |= *p++;
266
174k
            len -= 4;
267
20.3M
        } else {
268
20.3M
            ret = UTF8_getc(p, len, &value);
269
20.3M
            if (ret < 0)
270
26.6k
                return -1;
271
20.2M
            len -= ret;
272
20.2M
            p += ret;
273
20.2M
        }
274
1.69G
        if (rfunc) {
275
1.69G
            ret = rfunc(value, arg);
276
1.69G
            if (ret <= 0)
277
4.07k
                return ret;
278
1.69G
        }
279
1.69G
    }
280
28.8M
    return 1;
281
28.8M
}
282
283
/* Various utility functions for traverse_string */
284
285
/* Just count number of characters */
286
287
static int in_utf8(unsigned long value, void *arg)
288
10.8M
{
289
10.8M
    int *nchar;
290
291
10.8M
    if (!is_unicode_valid(value))
292
0
        return -2;
293
10.8M
    nchar = arg;
294
10.8M
    (*nchar)++;
295
10.8M
    return 1;
296
10.8M
}
297
298
/* Determine size of output as a UTF8 String */
299
300
static int out_utf8(unsigned long value, void *arg)
301
456M
{
302
456M
    int *outlen, len;
303
304
456M
    len = UTF8_putc(NULL, -1, value);
305
456M
    if (len <= 0) {
306
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
307
0
        return len;
308
0
    }
309
456M
    outlen = arg;
310
456M
    if (*outlen >= INT_MAX - len) {
311
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
312
0
        return -1;
313
0
    }
314
456M
    *outlen += len;
315
456M
    return 1;
316
456M
}
317
318
/*
319
 * Determine the "type" of a string: check each character against a supplied
320
 * "mask".
321
 */
322
323
static int type_str(unsigned long value, void *arg)
324
569M
{
325
569M
    unsigned long usable_types = *((unsigned long *)arg);
326
569M
    unsigned long types = usable_types;
327
569M
    const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value);
328
329
    /*
330
     * Clear out all the types which are not checked later. If any of those
331
     * is present in the mask, then the UTF8 type will be added and checked
332
     * below.
333
     */
334
569M
    types &= B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING
335
569M
        | B_ASN1_IA5STRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING
336
569M
        | B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING;
337
338
    /*
339
     * If any other types were in the input mask, they're effectively treated
340
     * as UTF8
341
     */
342
569M
    if (types != usable_types)
343
0
        types |= B_ASN1_UTF8STRING;
344
345
    /*
346
     * These checks should be in sync with ASN1_mbstring_ncopy.
347
     */
348
569M
    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native) || native == ' '))
349
0
        types &= ~B_ASN1_NUMERICSTRING;
350
569M
    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
351
0
        types &= ~B_ASN1_PRINTABLESTRING;
352
569M
    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
353
0
        types &= ~B_ASN1_IA5STRING;
354
569M
    if ((types & B_ASN1_T61STRING) && (value > 0xff))
355
0
        types &= ~B_ASN1_T61STRING;
356
569M
    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
357
0
        types &= ~B_ASN1_BMPSTRING;
358
569M
    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
359
4.07k
        types &= ~B_ASN1_UTF8STRING;
360
569M
    if (!types)
361
4.07k
        return -1;
362
569M
    *((unsigned long *)arg) = types;
363
569M
    return 1;
364
569M
}
365
366
/* Copy one byte per character ASCII like strings */
367
368
static int cpy_asc(unsigned long value, void *arg)
369
0
{
370
0
    unsigned char **p, *q;
371
0
    p = arg;
372
0
    q = *p;
373
0
    *q = (unsigned char)value;
374
0
    (*p)++;
375
0
    return 1;
376
0
}
377
378
/* Copy two byte per character BMPStrings */
379
380
static int cpy_bmp(unsigned long value, void *arg)
381
0
{
382
0
    unsigned char **p, *q;
383
0
    p = arg;
384
0
    q = *p;
385
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
386
0
    *q = (unsigned char)(value & 0xff);
387
0
    *p += 2;
388
0
    return 1;
389
0
}
390
391
/* Copy four byte per character UniversalStrings */
392
393
static int cpy_univ(unsigned long value, void *arg)
394
0
{
395
0
    unsigned char **p, *q;
396
0
    p = arg;
397
0
    q = *p;
398
0
    *q++ = (unsigned char)((value >> 24) & 0xff);
399
0
    *q++ = (unsigned char)((value >> 16) & 0xff);
400
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
401
0
    *q = (unsigned char)(value & 0xff);
402
0
    *p += 4;
403
0
    return 1;
404
0
}
405
406
/* Copy to a UTF8String */
407
408
static int cpy_utf8(unsigned long value, void *arg)
409
559M
{
410
559M
    unsigned char **p;
411
559M
    int ret;
412
559M
    p = arg;
413
    /* We already know there is enough room so pass 0xff as the length */
414
559M
    ret = UTF8_putc(*p, 0xff, value);
415
559M
    if (ret < 0)
416
0
        return ret;
417
559M
    *p += ret;
418
559M
    return 1;
419
559M
}