Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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.07M
{
39
9.07M
    return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
40
9.07M
}
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.02M
{
46
6.02M
    int str_type;
47
6.02M
    int ret;
48
6.02M
    char free_out;
49
6.02M
    int outform, outlen = 0;
50
6.02M
    ASN1_STRING *dest;
51
6.02M
    unsigned char *p;
52
6.02M
    int nchar;
53
6.02M
    int (*cpyfunc)(unsigned long, void *) = NULL;
54
6.02M
    if (len == -1)
55
0
        len = strlen((const char *)in);
56
6.02M
    if (!mask)
57
0
        mask = DIRSTRING_TYPE;
58
6.02M
    if (len < 0)
59
0
        return -1;
60
61
    /* First do a string check and work out the number of characters */
62
6.02M
    switch (inform) {
63
64
617k
    case MBSTRING_BMP:
65
617k
        if (len & 1) {
66
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
67
0
            return -1;
68
0
        }
69
617k
        nchar = len >> 1;
70
617k
        break;
71
72
495k
    case MBSTRING_UNIV:
73
495k
        if (len & 3) {
74
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
75
0
            return -1;
76
0
        }
77
495k
        nchar = len >> 2;
78
495k
        break;
79
80
973k
    case MBSTRING_UTF8:
81
973k
        nchar = 0;
82
        /* This counts the characters and does utf8 syntax checking */
83
973k
        ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
84
973k
        if (ret < 0) {
85
16.7k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
86
16.7k
            return -1;
87
16.7k
        }
88
957k
        break;
89
90
3.93M
    case MBSTRING_ASC:
91
3.93M
        nchar = len;
92
3.93M
        break;
93
94
0
    default:
95
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
96
0
        return -1;
97
6.02M
    }
98
99
6.00M
    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.00M
    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.00M
    if (traverse_string(in, len, inform, type_str, &mask) < 0) {
113
2.43k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_CHARACTERS);
114
2.43k
        return -1;
115
2.43k
    }
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.00M
    outform = MBSTRING_ASC;
122
6.00M
    if (mask & B_ASN1_NUMERICSTRING)
123
0
        str_type = V_ASN1_NUMERICSTRING;
124
6.00M
    else if (mask & B_ASN1_PRINTABLESTRING)
125
0
        str_type = V_ASN1_PRINTABLESTRING;
126
6.00M
    else if (mask & B_ASN1_IA5STRING)
127
0
        str_type = V_ASN1_IA5STRING;
128
6.00M
    else if (mask & B_ASN1_T61STRING)
129
0
        str_type = V_ASN1_T61STRING;
130
6.00M
    else if (mask & B_ASN1_BMPSTRING) {
131
0
        str_type = V_ASN1_BMPSTRING;
132
0
        outform = MBSTRING_BMP;
133
6.00M
    } else if (mask & B_ASN1_UNIVERSALSTRING) {
134
0
        str_type = V_ASN1_UNIVERSALSTRING;
135
0
        outform = MBSTRING_UNIV;
136
6.00M
    } else {
137
6.00M
        str_type = V_ASN1_UTF8STRING;
138
6.00M
        outform = MBSTRING_UTF8;
139
6.00M
    }
140
6.00M
    if (!out)
141
0
        return str_type;
142
6.00M
    if (*out) {
143
6.00M
        free_out = 0;
144
6.00M
        dest = *out;
145
6.00M
        ASN1_STRING_set0(dest, NULL, 0);
146
6.00M
        dest->type = str_type;
147
6.00M
    } 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.00M
    if (inform == outform) {
158
957k
        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
957k
        return str_type;
167
957k
    }
168
169
    /* Work out how much space the destination will need */
170
5.04M
    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.04M
    case MBSTRING_UTF8:
187
5.04M
        outlen = 0;
188
5.04M
        ret = traverse_string(in, len, inform, out_utf8, &outlen);
189
5.04M
        if (ret < 0) {
190
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
191
0
            return -1;
192
0
        }
193
5.04M
        cpyfunc = cpy_utf8;
194
5.04M
        break;
195
5.04M
    }
196
5.04M
    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.04M
    dest->length = outlen;
204
5.04M
    dest->data = p;
205
5.04M
    p[outlen] = 0;
206
5.04M
    traverse_string(in, len, inform, cpyfunc, &p);
207
5.04M
    return str_type;
208
5.04M
}
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.4M
{
219
25.4M
    unsigned long value;
220
25.4M
    int ret;
221
1.51G
    while (len) {
222
1.49G
        if (inform == MBSTRING_ASC) {
223
1.42G
            value = *p++;
224
1.42G
            len--;
225
1.42G
        } else if (inform == MBSTRING_BMP) {
226
46.4M
            value = *p++ << 8;
227
46.4M
            value |= *p++;
228
46.4M
            len -= 2;
229
46.4M
        } else if (inform == MBSTRING_UNIV) {
230
182k
            value = ((unsigned long)*p++) << 24;
231
182k
            value |= ((unsigned long)*p++) << 16;
232
182k
            value |= *p++ << 8;
233
182k
            value |= *p++;
234
182k
            len -= 4;
235
18.0M
        } else {
236
18.0M
            ret = UTF8_getc(p, len, &value);
237
18.0M
            if (ret < 0)
238
22.3k
                return -1;
239
17.9M
            len -= ret;
240
17.9M
            p += ret;
241
17.9M
        }
242
1.49G
        if (rfunc) {
243
1.49G
            ret = rfunc(value, arg);
244
1.49G
            if (ret <= 0)
245
3.93k
                return ret;
246
1.49G
        }
247
1.49G
    }
248
25.4M
    return 1;
249
25.4M
}
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
9.78M
{
257
9.78M
    int *nchar;
258
259
9.78M
    if (!is_unicode_valid(value))
260
0
        return -2;
261
9.78M
    nchar = arg;
262
9.78M
    (*nchar)++;
263
9.78M
    return 1;
264
9.78M
}
265
266
/* Determine size of output as a UTF8 String */
267
268
static int out_utf8(unsigned long value, void *arg)
269
490M
{
270
490M
    int *outlen, len;
271
272
490M
    len = UTF8_putc(NULL, -1, value);
273
490M
    if (len <= 0)
274
0
        return len;
275
490M
    outlen = arg;
276
490M
    *outlen += len;
277
490M
    return 1;
278
490M
}
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
499M
{
287
499M
    unsigned long usable_types = *((unsigned long *)arg);
288
499M
    unsigned long types = usable_types;
289
499M
    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
499M
    types &= B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING
297
499M
        | B_ASN1_IA5STRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING
298
499M
        | 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
499M
    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
499M
    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native) || native == ' '))
311
0
        types &= ~B_ASN1_NUMERICSTRING;
312
499M
    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
313
0
        types &= ~B_ASN1_PRINTABLESTRING;
314
499M
    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
315
0
        types &= ~B_ASN1_IA5STRING;
316
499M
    if ((types & B_ASN1_T61STRING) && (value > 0xff))
317
0
        types &= ~B_ASN1_T61STRING;
318
499M
    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
319
0
        types &= ~B_ASN1_BMPSTRING;
320
499M
    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
321
3.93k
        types &= ~B_ASN1_UTF8STRING;
322
499M
    if (!types)
323
3.93k
        return -1;
324
499M
    *((unsigned long *)arg) = types;
325
499M
    return 1;
326
499M
}
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
490M
{
372
490M
    unsigned char **p;
373
490M
    int ret;
374
490M
    p = arg;
375
    /* We already know there is enough room so pass 0xff as the length */
376
490M
    ret = UTF8_putc(*p, 0xff, value);
377
490M
    if (ret < 0)
378
0
        return ret;
379
490M
    *p += ret;
380
490M
    return 1;
381
490M
}