Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/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
#include <openssl/byteorder.h>
16
17
#include <crypto/asn1.h>
18
19
static int traverse_string(const unsigned char *p, int len, int inform,
20
    int (*rfunc)(uint32_t value, void *in),
21
    void *arg);
22
static int in_utf8(uint32_t value, void *arg);
23
static int out_utf8(uint32_t value, void *arg);
24
static int type_str(uint32_t value, void *arg);
25
static int cpy_asc(uint32_t value, void *arg);
26
static int cpy_bmp(uint32_t value, void *arg);
27
static int cpy_univ(uint32_t value, void *arg);
28
static int cpy_utf8(uint32_t value, void *arg);
29
30
/*
31
 * These functions take a string in UTF8, ASCII or multibyte form and a mask
32
 * of permissible ASN1 string types. It then works out the minimal type
33
 * (using the order Numeric < Printable < IA5 < T61 < BMP < Universal < UTF8)
34
 * and creates a string of the correct type with the supplied data. Yes this is
35
 * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
36
 * size limits too.
37
 */
38
39
int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
40
    int inform, unsigned long mask)
41
9.01M
{
42
9.01M
    return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
43
9.01M
}
44
45
int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
46
    int inform, unsigned long mask_in,
47
    long minsize, long maxsize)
48
3.26M
{
49
3.26M
    int str_type;
50
3.26M
    int ret;
51
3.26M
    char free_out;
52
3.26M
    int outform, outlen = 0;
53
3.26M
    ASN1_STRING *dest;
54
3.26M
    unsigned char *p;
55
3.26M
    int nchar;
56
3.26M
    uint32_t mask = (uint32_t)mask_in;
57
3.26M
    int (*cpyfunc)(uint32_t, void *) = NULL;
58
3.26M
    if (len == -1) {
59
32.4k
        size_t len_s = strlen((const char *)in);
60
61
32.4k
        if (len_s >= INT_MAX) {
62
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
63
0
            return -1;
64
0
        }
65
32.4k
        len = (int)len_s;
66
32.4k
    }
67
3.26M
    if (!mask)
68
0
        mask = DIRSTRING_TYPE;
69
3.26M
    if (len < 0) {
70
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
71
0
        return -1;
72
3.26M
    } else if (len >= INT_MAX) {
73
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
74
0
        return -1;
75
0
    }
76
77
    /* First do a string check and work out the number of characters */
78
3.26M
    switch (inform) {
79
80
672k
    case MBSTRING_BMP:
81
672k
        if (len & 1) {
82
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
83
0
            return -1;
84
0
        }
85
672k
        nchar = len >> 1;
86
672k
        break;
87
88
468k
    case MBSTRING_UNIV:
89
468k
        if (len & 3) {
90
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
91
0
            return -1;
92
0
        }
93
468k
        nchar = len >> 2;
94
468k
        break;
95
96
714k
    case MBSTRING_UTF8:
97
714k
        nchar = 0;
98
        /* This counts the characters and does utf8 syntax checking */
99
714k
        ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
100
714k
        if (ret < 0) {
101
9.09k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
102
9.09k
            return -1;
103
9.09k
        }
104
705k
        break;
105
106
1.40M
    case MBSTRING_ASC:
107
1.40M
        nchar = len;
108
1.40M
        break;
109
110
0
    default:
111
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
112
0
        return -1;
113
3.26M
    }
114
115
3.25M
    if ((minsize > 0) && (nchar < minsize)) {
116
9
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_SHORT,
117
9
            "minsize=%ld", minsize);
118
9
        return -1;
119
9
    }
120
121
3.25M
    if ((maxsize > 0) && (nchar > maxsize)) {
122
13
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG,
123
13
            "maxsize=%ld", maxsize);
124
13
        return -1;
125
13
    }
126
127
    /* Now work out minimal type (if any) */
128
3.25M
    if (traverse_string(in, len, inform, type_str, &mask) < 0) {
129
1.91k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_CHARACTERS);
130
1.91k
        return -1;
131
1.91k
    }
132
133
    /*
134
     * Now work out output format and string type.
135
     * These checks should be in sync with the checks in type_str.
136
     */
137
3.25M
    outform = MBSTRING_ASC;
138
3.25M
    if (mask & B_ASN1_NUMERICSTRING)
139
492
        str_type = V_ASN1_NUMERICSTRING;
140
3.25M
    else if (mask & B_ASN1_PRINTABLESTRING)
141
403
        str_type = V_ASN1_PRINTABLESTRING;
142
3.24M
    else if (mask & B_ASN1_IA5STRING)
143
329
        str_type = V_ASN1_IA5STRING;
144
3.24M
    else if (mask & B_ASN1_T61STRING)
145
1.01k
        str_type = V_ASN1_T61STRING;
146
3.24M
    else if (mask & B_ASN1_BMPSTRING) {
147
3.06k
        str_type = V_ASN1_BMPSTRING;
148
3.06k
        outform = MBSTRING_BMP;
149
3.24M
    } else if (mask & B_ASN1_UNIVERSALSTRING) {
150
325
        str_type = V_ASN1_UNIVERSALSTRING;
151
325
        outform = MBSTRING_UNIV;
152
3.24M
    } else {
153
3.24M
        str_type = V_ASN1_UTF8STRING;
154
3.24M
        outform = MBSTRING_UTF8;
155
3.24M
    }
156
3.25M
    if (!out)
157
0
        return str_type;
158
3.25M
    if (*out) {
159
3.24M
        free_out = 0;
160
3.24M
        dest = *out;
161
3.24M
        ASN1_STRING_set0(dest, NULL, 0);
162
3.24M
        dest->type = str_type;
163
3.24M
    } else {
164
4.16k
        free_out = 1;
165
4.16k
        dest = ASN1_STRING_type_new(str_type);
166
4.16k
        if (dest == NULL) {
167
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
168
0
            return -1;
169
0
        }
170
4.16k
        *out = dest;
171
4.16k
    }
172
    /* If both the same type just copy across */
173
3.25M
    if (inform == outform) {
174
707k
        if ((p = OPENSSL_malloc((size_t)len + 1)) == NULL) {
175
0
            if (free_out) {
176
0
                ASN1_STRING_free(dest);
177
0
                *out = NULL;
178
0
            }
179
0
            return -1;
180
0
        }
181
707k
        if (len > 0)
182
467k
            memcpy(p, in, (size_t)len);
183
707k
        p[len] = '\0';
184
707k
        dest->data = p;
185
707k
        dest->length = len;
186
707k
        return str_type;
187
707k
    }
188
189
    /* Work out how much space the destination will need */
190
2.54M
    switch (outform) {
191
92
    case MBSTRING_ASC:
192
92
        outlen = nchar;
193
92
        cpyfunc = cpy_asc;
194
92
        break;
195
196
3.06k
    case MBSTRING_BMP:
197
3.06k
        if (nchar > INT_MAX / 2) {
198
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
199
0
            if (free_out) {
200
0
                ASN1_STRING_free(dest);
201
0
                *out = NULL;
202
0
            }
203
0
            return -1;
204
0
        }
205
3.06k
        outlen = nchar << 1;
206
3.06k
        cpyfunc = cpy_bmp;
207
3.06k
        break;
208
209
325
    case MBSTRING_UNIV:
210
325
        if (nchar > INT_MAX / 4) {
211
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
212
0
            if (free_out) {
213
0
                ASN1_STRING_free(dest);
214
0
                *out = NULL;
215
0
            }
216
0
            return -1;
217
0
        }
218
325
        outlen = nchar << 2;
219
325
        cpyfunc = cpy_univ;
220
325
        break;
221
222
2.54M
    case MBSTRING_UTF8:
223
2.54M
        outlen = 0;
224
2.54M
        ret = traverse_string(in, len, inform, out_utf8, &outlen);
225
2.54M
        if (ret < 0) { /* error already raised in out_utf8() */
226
0
            if (free_out) {
227
0
                ASN1_STRING_free(dest);
228
0
                *out = NULL;
229
0
            }
230
0
            return -1;
231
0
        }
232
2.54M
        cpyfunc = cpy_utf8;
233
2.54M
        break;
234
2.54M
    }
235
2.54M
    if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
236
0
        if (free_out) {
237
0
            ASN1_STRING_free(dest);
238
0
            *out = NULL;
239
0
        }
240
0
        return -1;
241
0
    }
242
2.54M
    dest->length = outlen;
243
2.54M
    dest->data = p;
244
2.54M
    p[outlen] = 0;
245
2.54M
    traverse_string(in, len, inform, cpyfunc, &p);
246
2.54M
    return str_type;
247
2.54M
}
248
249
/*
250
 * This function traverses a string and passes the value of each character to
251
 * an optional function along with a void * argument.
252
 */
253
254
static int traverse_string(const unsigned char *p, int len, int inform,
255
    int (*rfunc)(uint32_t value, void *in),
256
    void *arg)
257
25.2M
{
258
25.2M
    uint32_t value;
259
25.2M
    int ret;
260
2.01G
    while (len) {
261
1.98G
        if (inform == MBSTRING_ASC) {
262
1.77G
            value = *p++;
263
1.77G
            len--;
264
1.77G
        } else if (inform == MBSTRING_BMP) {
265
105M
            uint16_t tmp;
266
105M
            p = OPENSSL_load_u16_be(&tmp, p);
267
105M
            value = tmp;
268
105M
            len -= 2;
269
110M
        } else if (inform == MBSTRING_UNIV) {
270
210k
            p = OPENSSL_load_u32_be(&value, p);
271
210k
            len -= 4;
272
110M
        } else {
273
110M
            ret = ossl_utf8_getc_internal(p, len, &value);
274
110M
            if (ret < 0)
275
22.3k
                return -1;
276
110M
            len -= ret;
277
110M
            p += ret;
278
110M
        }
279
1.98G
        if (rfunc) {
280
1.98G
            ret = rfunc(value, arg);
281
1.98G
            if (ret <= 0)
282
4.55k
                return ret;
283
1.98G
        }
284
1.98G
    }
285
25.2M
    return 1;
286
25.2M
}
287
288
/* Various utility functions for traverse_string */
289
290
/* Just count number of characters */
291
292
static int in_utf8(uint32_t value, void *arg)
293
56.3M
{
294
56.3M
    int *nchar;
295
296
56.3M
    if (!is_unicode_valid(value))
297
0
        return -2;
298
56.3M
    nchar = arg;
299
56.3M
    (*nchar)++;
300
56.3M
    return 1;
301
56.3M
}
302
303
/* Determine size of output as a UTF8 String */
304
305
static int out_utf8(uint32_t value, void *arg)
306
622M
{
307
622M
    int *outlen, len;
308
309
622M
    len = ossl_utf8_putc_internal(NULL, -1, value);
310
622M
    if (len <= 0) {
311
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
312
0
        return len;
313
0
    }
314
622M
    outlen = arg;
315
622M
    if (*outlen >= INT_MAX - len) {
316
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
317
0
        return -1;
318
0
    }
319
622M
    *outlen += len;
320
622M
    return 1;
321
622M
}
322
323
/*
324
 * Determine the "type" of a string: check each character against a supplied
325
 * "mask".
326
 */
327
328
static int type_str(uint32_t value, void *arg)
329
681M
{
330
681M
    uint32_t usable_types = *((uint32_t *)arg);
331
681M
    uint32_t types = usable_types;
332
681M
    const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value);
333
334
    /*
335
     * Clear out all the types which are not checked later. If any of those
336
     * is present in the mask, then the UTF8 type will be added and checked
337
     * below.
338
     */
339
681M
    types &= B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING
340
681M
        | B_ASN1_IA5STRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING
341
681M
        | B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING;
342
343
    /*
344
     * If any other types were in the input mask, they're effectively treated
345
     * as UTF8
346
     */
347
681M
    if (types != usable_types)
348
30
        types |= B_ASN1_UTF8STRING;
349
350
    /*
351
     * These checks should be in sync with ASN1_mbstring_ncopy.
352
     */
353
681M
    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native) || native == ' '))
354
21
        types &= ~B_ASN1_NUMERICSTRING;
355
681M
    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
356
12
        types &= ~B_ASN1_PRINTABLESTRING;
357
681M
    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
358
26
        types &= ~B_ASN1_IA5STRING;
359
681M
    if ((types & B_ASN1_T61STRING) && (value > 0xff))
360
105
        types &= ~B_ASN1_T61STRING;
361
681M
    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
362
18
        types &= ~B_ASN1_BMPSTRING;
363
681M
    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
364
4.36k
        types &= ~B_ASN1_UTF8STRING;
365
681M
    if (!types)
366
4.55k
        return -1;
367
681M
    *((uint32_t *)arg) = types;
368
681M
    return 1;
369
681M
}
370
371
/* Copy one byte per character ASCII like strings */
372
373
static int cpy_asc(uint32_t value, void *arg)
374
555
{
375
555
    unsigned char **p, *q;
376
555
    p = arg;
377
555
    q = *p;
378
555
    *q = (unsigned char)value;
379
555
    (*p)++;
380
555
    return 1;
381
555
}
382
383
/* Copy two byte per character BMPStrings */
384
385
static int cpy_bmp(uint32_t value, void *arg)
386
2.54M
{
387
2.54M
    unsigned char **p, *q;
388
2.54M
    p = arg;
389
2.54M
    q = *p;
390
2.54M
    *q++ = (unsigned char)((value >> 8) & 0xff);
391
2.54M
    *q = (unsigned char)(value & 0xff);
392
2.54M
    *p += 2;
393
2.54M
    return 1;
394
2.54M
}
395
396
/* Copy four byte per character UniversalStrings */
397
398
static int cpy_univ(uint32_t value, void *arg)
399
507k
{
400
507k
    unsigned char **p, *q;
401
507k
    p = arg;
402
507k
    q = *p;
403
507k
    *q++ = (unsigned char)((value >> 24) & 0xff);
404
507k
    *q++ = (unsigned char)((value >> 16) & 0xff);
405
507k
    *q++ = (unsigned char)((value >> 8) & 0xff);
406
507k
    *q = (unsigned char)(value & 0xff);
407
507k
    *p += 4;
408
507k
    return 1;
409
507k
}
410
411
/* Copy to a UTF8String */
412
413
static int cpy_utf8(uint32_t value, void *arg)
414
622M
{
415
622M
    unsigned char **p;
416
622M
    int ret;
417
622M
    p = arg;
418
    /* We already know there is enough room so pass 0xff as the length */
419
622M
    ret = ossl_utf8_putc_internal(*p, 0xff, value);
420
622M
    if (ret < 0)
421
0
        return ret;
422
622M
    *p += ret;
423
622M
    return 1;
424
622M
}