Coverage Report

Created: 2025-12-31 06:58

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