Coverage Report

Created: 2026-09-12 06:55

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
7.79M
{
42
7.79M
    return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
43
7.79M
}
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
2.66M
{
49
2.66M
    int str_type;
50
2.66M
    int ret;
51
2.66M
    char free_out;
52
2.66M
    int outform, outlen = 0;
53
2.66M
    ASN1_STRING *dest;
54
2.66M
    unsigned char *p;
55
2.66M
    int nchar;
56
2.66M
    uint32_t mask = (uint32_t)mask_in;
57
2.66M
    int (*cpyfunc)(uint32_t, void *) = NULL;
58
2.66M
    if (len == -1) {
59
7.42k
        size_t len_s = strlen((const char *)in);
60
61
7.42k
        if (len_s >= INT_MAX) {
62
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
63
0
            return -1;
64
0
        }
65
7.42k
        len = (int)len_s;
66
7.42k
    }
67
2.66M
    if (!mask)
68
0
        mask = DIRSTRING_TYPE;
69
2.66M
    if (len < 0) {
70
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
71
0
        return -1;
72
2.66M
    } 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
2.66M
    switch (inform) {
79
80
782k
    case MBSTRING_BMP:
81
782k
        if (len & 1) {
82
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
83
0
            return -1;
84
0
        }
85
782k
        nchar = len >> 1;
86
782k
        break;
87
88
416k
    case MBSTRING_UNIV:
89
416k
        if (len & 3) {
90
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
91
0
            return -1;
92
0
        }
93
416k
        nchar = len >> 2;
94
416k
        break;
95
96
417k
    case MBSTRING_UTF8:
97
417k
        nchar = 0;
98
        /* This counts the characters and does utf8 syntax checking */
99
417k
        ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
100
417k
        if (ret < 0) {
101
6.55k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
102
6.55k
            return -1;
103
6.55k
        }
104
410k
        break;
105
106
1.04M
    case MBSTRING_ASC:
107
1.04M
        nchar = len;
108
1.04M
        break;
109
110
0
    default:
111
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
112
0
        return -1;
113
2.66M
    }
114
115
2.65M
    if ((minsize > 0) && (nchar < minsize)) {
116
3
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_SHORT,
117
3
            "minsize=%ld", minsize);
118
3
        return -1;
119
3
    }
120
121
2.65M
    if ((maxsize > 0) && (nchar > maxsize)) {
122
12
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG,
123
12
            "maxsize=%ld", maxsize);
124
12
        return -1;
125
12
    }
126
127
    /* Now work out minimal type (if any) */
128
2.65M
    if (traverse_string(in, len, inform, type_str, &mask) < 0) {
129
1.02k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_CHARACTERS);
130
1.02k
        return -1;
131
1.02k
    }
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
2.65M
    outform = MBSTRING_ASC;
138
2.65M
    if (mask & B_ASN1_NUMERICSTRING)
139
0
        str_type = V_ASN1_NUMERICSTRING;
140
2.65M
    else if (mask & B_ASN1_PRINTABLESTRING)
141
85
        str_type = V_ASN1_PRINTABLESTRING;
142
2.65M
    else if (mask & B_ASN1_IA5STRING)
143
34
        str_type = V_ASN1_IA5STRING;
144
2.65M
    else if (mask & B_ASN1_T61STRING)
145
582
        str_type = V_ASN1_T61STRING;
146
2.65M
    else if (mask & B_ASN1_BMPSTRING) {
147
295
        str_type = V_ASN1_BMPSTRING;
148
295
        outform = MBSTRING_BMP;
149
2.65M
    } else if (mask & B_ASN1_UNIVERSALSTRING) {
150
325
        str_type = V_ASN1_UNIVERSALSTRING;
151
325
        outform = MBSTRING_UNIV;
152
2.65M
    } else {
153
2.65M
        str_type = V_ASN1_UTF8STRING;
154
2.65M
        outform = MBSTRING_UTF8;
155
2.65M
    }
156
2.65M
    if (!out)
157
0
        return str_type;
158
2.65M
    if (*out) {
159
2.65M
        free_out = 0;
160
2.65M
        dest = *out;
161
2.65M
        ASN1_STRING_set0(dest, NULL, 0);
162
2.65M
        dest->type = str_type;
163
2.65M
    } else {
164
2.24k
        free_out = 1;
165
2.24k
        dest = ASN1_STRING_type_new(str_type);
166
2.24k
        if (dest == NULL) {
167
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
168
0
            return -1;
169
0
        }
170
2.24k
        *out = dest;
171
2.24k
    }
172
    /* If both the same type just copy across */
173
2.65M
    if (inform == outform) {
174
411k
        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
411k
        if (len > 0)
182
232k
            memcpy(p, in, (size_t)len);
183
411k
        p[len] = '\0';
184
411k
        dest->data = p;
185
411k
        dest->length = len;
186
411k
        return str_type;
187
411k
    }
188
189
    /* Work out how much space the destination will need */
190
2.24M
    switch (outform) {
191
61
    case MBSTRING_ASC:
192
61
        outlen = nchar;
193
61
        cpyfunc = cpy_asc;
194
61
        break;
195
196
295
    case MBSTRING_BMP:
197
295
        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
295
        outlen = nchar << 1;
206
295
        cpyfunc = cpy_bmp;
207
295
        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.24M
    case MBSTRING_UTF8:
223
2.24M
        outlen = 0;
224
2.24M
        ret = traverse_string(in, len, inform, out_utf8, &outlen);
225
2.24M
        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.24M
        cpyfunc = cpy_utf8;
233
2.24M
        break;
234
2.24M
    }
235
2.24M
    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.24M
    dest->length = outlen;
243
2.24M
    dest->data = p;
244
2.24M
    p[outlen] = 0;
245
2.24M
    traverse_string(in, len, inform, cpyfunc, &p);
246
2.24M
    return str_type;
247
2.24M
}
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
22.0M
{
258
22.0M
    uint32_t value;
259
22.0M
    int ret;
260
1.51G
    while (len) {
261
1.49G
        if (inform == MBSTRING_ASC) {
262
1.38G
            value = *p++;
263
1.38G
            len--;
264
1.38G
        } else if (inform == MBSTRING_BMP) {
265
84.7M
            uint16_t tmp;
266
84.7M
            p = OPENSSL_load_u16_be(&tmp, p);
267
84.7M
            value = tmp;
268
84.7M
            len -= 2;
269
84.7M
        } else if (inform == MBSTRING_UNIV) {
270
163k
            p = OPENSSL_load_u32_be(&value, p);
271
163k
            len -= 4;
272
22.4M
        } else {
273
22.4M
            ret = ossl_utf8_getc_internal(p, len, &value);
274
22.4M
            if (ret < 0)
275
17.3k
                return -1;
276
22.3M
            len -= ret;
277
22.3M
            p += ret;
278
22.3M
        }
279
1.49G
        if (rfunc) {
280
1.49G
            ret = rfunc(value, arg);
281
1.49G
            if (ret <= 0)
282
3.56k
                return ret;
283
1.49G
        }
284
1.49G
    }
285
22.0M
    return 1;
286
22.0M
}
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
11.7M
{
294
11.7M
    int *nchar;
295
296
11.7M
    if (!is_unicode_valid(value))
297
0
        return -2;
298
11.7M
    nchar = arg;
299
11.7M
    (*nchar)++;
300
11.7M
    return 1;
301
11.7M
}
302
303
/* Determine size of output as a UTF8 String */
304
305
static int out_utf8(uint32_t value, void *arg)
306
488M
{
307
488M
    int *outlen, len;
308
309
488M
    len = ossl_utf8_putc_internal(NULL, -1, value);
310
488M
    if (len <= 0) {
311
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
312
0
        return len;
313
0
    }
314
488M
    outlen = arg;
315
488M
    if (*outlen >= INT_MAX - len) {
316
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
317
0
        return -1;
318
0
    }
319
488M
    *outlen += len;
320
488M
    return 1;
321
488M
}
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
505M
{
330
505M
    uint32_t usable_types = *((uint32_t *)arg);
331
505M
    uint32_t types = usable_types;
332
505M
    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
505M
    types &= B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING
340
505M
        | B_ASN1_IA5STRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING
341
505M
        | 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
505M
    if (types != usable_types)
348
10
        types |= B_ASN1_UTF8STRING;
349
350
    /*
351
     * These checks should be in sync with ASN1_mbstring_ncopy.
352
     */
353
505M
    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native) || native == ' '))
354
0
        types &= ~B_ASN1_NUMERICSTRING;
355
505M
    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
356
11
        types &= ~B_ASN1_PRINTABLESTRING;
357
505M
    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
358
15
        types &= ~B_ASN1_IA5STRING;
359
505M
    if ((types & B_ASN1_T61STRING) && (value > 0xff))
360
44
        types &= ~B_ASN1_T61STRING;
361
505M
    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
362
0
        types &= ~B_ASN1_BMPSTRING;
363
505M
    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
364
3.49k
        types &= ~B_ASN1_UTF8STRING;
365
505M
    if (!types)
366
3.56k
        return -1;
367
505M
    *((uint32_t *)arg) = types;
368
505M
    return 1;
369
505M
}
370
371
/* Copy one byte per character ASCII like strings */
372
373
static int cpy_asc(uint32_t value, void *arg)
374
631
{
375
631
    unsigned char **p, *q;
376
631
    p = arg;
377
631
    q = *p;
378
631
    *q = (unsigned char)value;
379
631
    (*p)++;
380
631
    return 1;
381
631
}
382
383
/* Copy two byte per character BMPStrings */
384
385
static int cpy_bmp(uint32_t value, void *arg)
386
1.85M
{
387
1.85M
    unsigned char **p, *q;
388
1.85M
    p = arg;
389
1.85M
    q = *p;
390
1.85M
    *q++ = (unsigned char)((value >> 8) & 0xff);
391
1.85M
    *q = (unsigned char)(value & 0xff);
392
1.85M
    *p += 2;
393
1.85M
    return 1;
394
1.85M
}
395
396
/* Copy four byte per character UniversalStrings */
397
398
static int cpy_univ(uint32_t value, void *arg)
399
402k
{
400
402k
    unsigned char **p, *q;
401
402k
    p = arg;
402
402k
    q = *p;
403
402k
    *q++ = (unsigned char)((value >> 24) & 0xff);
404
402k
    *q++ = (unsigned char)((value >> 16) & 0xff);
405
402k
    *q++ = (unsigned char)((value >> 8) & 0xff);
406
402k
    *q = (unsigned char)(value & 0xff);
407
402k
    *p += 4;
408
402k
    return 1;
409
402k
}
410
411
/* Copy to a UTF8String */
412
413
static int cpy_utf8(uint32_t value, void *arg)
414
488M
{
415
488M
    unsigned char **p;
416
488M
    int ret;
417
488M
    p = arg;
418
    /* We already know there is enough room so pass 0xff as the length */
419
488M
    ret = ossl_utf8_putc_internal(*p, 0xff, value);
420
488M
    if (ret < 0)
421
0
        return ret;
422
488M
    *p += ret;
423
488M
    return 1;
424
488M
}