Coverage Report

Created: 2026-02-14 07:20

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.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_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.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_MALLOC_FAILURE);
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
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
202
0
        return -1;
203
0
    }
204
5.04M
    dest->length = outlen;
205
5.04M
    dest->data = p;
206
5.04M
    p[outlen] = 0;
207
5.04M
    traverse_string(in, len, inform, cpyfunc, &p);
208
5.04M
    return str_type;
209
5.04M
}
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.4M
{
220
25.4M
    unsigned long value;
221
25.4M
    int ret;
222
1.51G
    while (len) {
223
1.49G
        if (inform == MBSTRING_ASC) {
224
1.42G
            value = *p++;
225
1.42G
            len--;
226
1.42G
        } else if (inform == MBSTRING_BMP) {
227
46.4M
            value = *p++ << 8;
228
46.4M
            value |= *p++;
229
46.4M
            len -= 2;
230
46.4M
        } else if (inform == MBSTRING_UNIV) {
231
182k
            value = ((unsigned long)*p++) << 24;
232
182k
            value |= ((unsigned long)*p++) << 16;
233
182k
            value |= *p++ << 8;
234
182k
            value |= *p++;
235
182k
            len -= 4;
236
18.0M
        } else {
237
18.0M
            ret = UTF8_getc(p, len, &value);
238
18.0M
            if (ret < 0)
239
22.3k
                return -1;
240
17.9M
            len -= ret;
241
17.9M
            p += ret;
242
17.9M
        }
243
1.49G
        if (rfunc) {
244
1.49G
            ret = rfunc(value, arg);
245
1.49G
            if (ret <= 0)
246
3.93k
                return ret;
247
1.49G
        }
248
1.49G
    }
249
25.4M
    return 1;
250
25.4M
}
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
9.78M
{
258
9.78M
    int *nchar;
259
260
9.78M
    if (!is_unicode_valid(value))
261
0
        return -2;
262
9.78M
    nchar = arg;
263
9.78M
    (*nchar)++;
264
9.78M
    return 1;
265
9.78M
}
266
267
/* Determine size of output as a UTF8 String */
268
269
static int out_utf8(unsigned long value, void *arg)
270
490M
{
271
490M
    int *outlen, len;
272
273
490M
    len = UTF8_putc(NULL, -1, value);
274
490M
    if (len <= 0)
275
0
        return len;
276
490M
    outlen = arg;
277
490M
    *outlen += len;
278
490M
    return 1;
279
490M
}
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
499M
{
288
499M
    unsigned long usable_types = *((unsigned long *)arg);
289
499M
    unsigned long types = usable_types;
290
499M
    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
499M
    types &= B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING
298
499M
        | B_ASN1_IA5STRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING
299
499M
        | 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
499M
    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
499M
    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native) || native == ' '))
312
0
        types &= ~B_ASN1_NUMERICSTRING;
313
499M
    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
314
0
        types &= ~B_ASN1_PRINTABLESTRING;
315
499M
    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
316
0
        types &= ~B_ASN1_IA5STRING;
317
499M
    if ((types & B_ASN1_T61STRING) && (value > 0xff))
318
0
        types &= ~B_ASN1_T61STRING;
319
499M
    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
320
0
        types &= ~B_ASN1_BMPSTRING;
321
499M
    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
322
3.93k
        types &= ~B_ASN1_UTF8STRING;
323
499M
    if (!types)
324
3.93k
        return -1;
325
499M
    *((unsigned long *)arg) = types;
326
499M
    return 1;
327
499M
}
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
490M
{
373
490M
    unsigned char **p;
374
490M
    int ret;
375
490M
    p = arg;
376
    /* We already know there is enough room so pass 0xff as the length */
377
490M
    ret = UTF8_putc(*p, 0xff, value);
378
490M
    if (ret < 0)
379
0
        return ret;
380
490M
    *p += ret;
381
490M
    return 1;
382
490M
}