Coverage Report

Created: 2025-12-31 06:58

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-2024 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_MALLOC_FAILURE);
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_MALLOC_FAILURE);
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
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
202
0
        return -1;
203
0
    }
204
5.53M
    dest->length = outlen;
205
5.53M
    dest->data = p;
206
5.53M
    p[outlen] = 0;
207
5.53M
    traverse_string(in, len, inform, cpyfunc, &p);
208
5.53M
    return str_type;
209
5.53M
}
210
211
/*
212
 * This function traverses a string and passes the value of each character to
213
 * an optional function along with a void * argument.
214
 */
215
216
static int traverse_string(const unsigned char *p, int len, int inform,
217
    int (*rfunc)(unsigned long value, void *in),
218
    void *arg)
219
25.3M
{
220
25.3M
    unsigned long value;
221
25.3M
    int ret;
222
1.49G
    while (len) {
223
1.47G
        if (inform == MBSTRING_ASC) {
224
1.39G
            value = *p++;
225
1.39G
            len--;
226
1.39G
        } else if (inform == MBSTRING_BMP) {
227
55.5M
            value = *p++ << 8;
228
55.5M
            value |= *p++;
229
55.5M
            len -= 2;
230
55.5M
        } else if (inform == MBSTRING_UNIV) {
231
201k
            value = ((unsigned long)*p++) << 24;
232
201k
            value |= ((unsigned long)*p++) << 16;
233
201k
            value |= *p++ << 8;
234
201k
            value |= *p++;
235
201k
            len -= 4;
236
22.1M
        } else {
237
22.1M
            ret = UTF8_getc(p, len, &value);
238
22.1M
            if (ret < 0)
239
21.2k
                return -1;
240
22.1M
            len -= ret;
241
22.1M
            p += ret;
242
22.1M
        }
243
1.47G
        if (rfunc) {
244
1.47G
            ret = rfunc(value, arg);
245
1.47G
            if (ret <= 0)
246
3.89k
                return ret;
247
1.47G
        }
248
1.47G
    }
249
25.3M
    return 1;
250
25.3M
}
251
252
/* Various utility functions for traverse_string */
253
254
/* Just count number of characters */
255
256
static int in_utf8(unsigned long value, void *arg)
257
11.9M
{
258
11.9M
    int *nchar;
259
260
11.9M
    if (!is_unicode_valid(value))
261
0
        return -2;
262
11.9M
    nchar = arg;
263
11.9M
    (*nchar)++;
264
11.9M
    return 1;
265
11.9M
}
266
267
/* Determine size of output as a UTF8 String */
268
269
static int out_utf8(unsigned long value, void *arg)
270
483M
{
271
483M
    int *outlen, len;
272
273
483M
    len = UTF8_putc(NULL, -1, value);
274
483M
    if (len <= 0)
275
0
        return len;
276
483M
    outlen = arg;
277
483M
    *outlen += len;
278
483M
    return 1;
279
483M
}
280
281
/*
282
 * Determine the "type" of a string: check each character against a supplied
283
 * "mask".
284
 */
285
286
static int type_str(unsigned long value, void *arg)
287
494M
{
288
494M
    unsigned long usable_types = *((unsigned long *)arg);
289
494M
    unsigned long types = usable_types;
290
494M
    const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value);
291
292
    /*
293
     * Clear out all the types which are not checked later. If any of those
294
     * is present in the mask, then the UTF8 type will be added and checked
295
     * below.
296
     */
297
494M
    types &= B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING
298
494M
        | B_ASN1_IA5STRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING
299
494M
        | B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING;
300
301
    /*
302
     * If any other types were in the input mask, they're effectively treated
303
     * as UTF8
304
     */
305
494M
    if (types != usable_types)
306
0
        types |= B_ASN1_UTF8STRING;
307
308
    /*
309
     * These checks should be in sync with ASN1_mbstring_ncopy.
310
     */
311
494M
    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native) || native == ' '))
312
0
        types &= ~B_ASN1_NUMERICSTRING;
313
494M
    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
314
0
        types &= ~B_ASN1_PRINTABLESTRING;
315
494M
    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
316
0
        types &= ~B_ASN1_IA5STRING;
317
494M
    if ((types & B_ASN1_T61STRING) && (value > 0xff))
318
0
        types &= ~B_ASN1_T61STRING;
319
494M
    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
320
0
        types &= ~B_ASN1_BMPSTRING;
321
494M
    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
322
3.89k
        types &= ~B_ASN1_UTF8STRING;
323
494M
    if (!types)
324
3.89k
        return -1;
325
494M
    *((unsigned long *)arg) = types;
326
494M
    return 1;
327
494M
}
328
329
/* Copy one byte per character ASCII like strings */
330
331
static int cpy_asc(unsigned long value, void *arg)
332
0
{
333
0
    unsigned char **p, *q;
334
0
    p = arg;
335
0
    q = *p;
336
0
    *q = (unsigned char)value;
337
0
    (*p)++;
338
0
    return 1;
339
0
}
340
341
/* Copy two byte per character BMPStrings */
342
343
static int cpy_bmp(unsigned long value, void *arg)
344
0
{
345
0
    unsigned char **p, *q;
346
0
    p = arg;
347
0
    q = *p;
348
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
349
0
    *q = (unsigned char)(value & 0xff);
350
0
    *p += 2;
351
0
    return 1;
352
0
}
353
354
/* Copy four byte per character UniversalStrings */
355
356
static int cpy_univ(unsigned long value, void *arg)
357
0
{
358
0
    unsigned char **p, *q;
359
0
    p = arg;
360
0
    q = *p;
361
0
    *q++ = (unsigned char)((value >> 24) & 0xff);
362
0
    *q++ = (unsigned char)((value >> 16) & 0xff);
363
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
364
0
    *q = (unsigned char)(value & 0xff);
365
0
    *p += 4;
366
0
    return 1;
367
0
}
368
369
/* Copy to a UTF8String */
370
371
static int cpy_utf8(unsigned long value, void *arg)
372
483M
{
373
483M
    unsigned char **p;
374
483M
    int ret;
375
483M
    p = arg;
376
    /* We already know there is enough room so pass 0xff as the length */
377
483M
    ret = UTF8_putc(*p, 0xff, value);
378
483M
    if (ret < 0)
379
0
        return ret;
380
483M
    *p += ret;
381
483M
    return 1;
382
483M
}