Coverage Report

Created: 2025-12-31 06:58

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