Coverage Report

Created: 2025-08-28 07:07

/src/openssl30/crypto/asn1/a_mbstr.c
Line
Count
Source (jump to first uncovered line)
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.02M
{
39
9.02M
    return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
40
9.02M
}
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.87M
{
46
6.87M
    int str_type;
47
6.87M
    int ret;
48
6.87M
    char free_out;
49
6.87M
    int outform, outlen = 0;
50
6.87M
    ASN1_STRING *dest;
51
6.87M
    unsigned char *p;
52
6.87M
    int nchar;
53
6.87M
    int (*cpyfunc) (unsigned long, void *) = NULL;
54
6.87M
    if (len == -1)
55
0
        len = strlen((const char *)in);
56
6.87M
    if (!mask)
57
0
        mask = DIRSTRING_TYPE;
58
6.87M
    if (len < 0)
59
0
        return -1;
60
61
    /* First do a string check and work out the number of characters */
62
6.87M
    switch (inform) {
63
64
662k
    case MBSTRING_BMP:
65
662k
        if (len & 1) {
66
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
67
0
            return -1;
68
0
        }
69
662k
        nchar = len >> 1;
70
662k
        break;
71
72
378k
    case MBSTRING_UNIV:
73
378k
        if (len & 3) {
74
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
75
0
            return -1;
76
0
        }
77
378k
        nchar = len >> 2;
78
378k
        break;
79
80
1.00M
    case MBSTRING_UTF8:
81
1.00M
        nchar = 0;
82
        /* This counts the characters and does utf8 syntax checking */
83
1.00M
        ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
84
1.00M
        if (ret < 0) {
85
24.4k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
86
24.4k
            return -1;
87
24.4k
        }
88
984k
        break;
89
90
4.82M
    case MBSTRING_ASC:
91
4.82M
        nchar = len;
92
4.82M
        break;
93
94
0
    default:
95
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
96
0
        return -1;
97
6.87M
    }
98
99
6.85M
    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.85M
    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.85M
    if (traverse_string(in, len, inform, type_str, &mask) < 0) {
113
2.87k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_CHARACTERS);
114
2.87k
        return -1;
115
2.87k
    }
116
117
    /* Now work out output format and string type */
118
6.85M
    outform = MBSTRING_ASC;
119
6.85M
    if (mask & B_ASN1_NUMERICSTRING)
120
0
        str_type = V_ASN1_NUMERICSTRING;
121
6.85M
    else if (mask & B_ASN1_PRINTABLESTRING)
122
0
        str_type = V_ASN1_PRINTABLESTRING;
123
6.85M
    else if (mask & B_ASN1_IA5STRING)
124
0
        str_type = V_ASN1_IA5STRING;
125
6.85M
    else if (mask & B_ASN1_T61STRING)
126
0
        str_type = V_ASN1_T61STRING;
127
6.85M
    else if (mask & B_ASN1_BMPSTRING) {
128
0
        str_type = V_ASN1_BMPSTRING;
129
0
        outform = MBSTRING_BMP;
130
6.85M
    } else if (mask & B_ASN1_UNIVERSALSTRING) {
131
0
        str_type = V_ASN1_UNIVERSALSTRING;
132
0
        outform = MBSTRING_UNIV;
133
6.85M
    } else {
134
6.85M
        str_type = V_ASN1_UTF8STRING;
135
6.85M
        outform = MBSTRING_UTF8;
136
6.85M
    }
137
6.85M
    if (!out)
138
0
        return str_type;
139
6.85M
    if (*out) {
140
6.85M
        free_out = 0;
141
6.85M
        dest = *out;
142
6.85M
        ASN1_STRING_set0(dest, NULL, 0);
143
6.85M
        dest->type = str_type;
144
6.85M
    } else {
145
0
        free_out = 1;
146
0
        dest = ASN1_STRING_type_new(str_type);
147
0
        if (dest == NULL) {
148
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
149
0
            return -1;
150
0
        }
151
0
        *out = dest;
152
0
    }
153
    /* If both the same type just copy across */
154
6.85M
    if (inform == outform) {
155
984k
        if (!ASN1_STRING_set(dest, in, len)) {
156
0
            if (free_out) {
157
0
                ASN1_STRING_free(dest);
158
0
                *out = NULL;
159
0
            }
160
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
161
0
            return -1;
162
0
        }
163
984k
        return str_type;
164
984k
    }
165
166
    /* Work out how much space the destination will need */
167
5.86M
    switch (outform) {
168
0
    case MBSTRING_ASC:
169
0
        outlen = nchar;
170
0
        cpyfunc = cpy_asc;
171
0
        break;
172
173
0
    case MBSTRING_BMP:
174
0
        outlen = nchar << 1;
175
0
        cpyfunc = cpy_bmp;
176
0
        break;
177
178
0
    case MBSTRING_UNIV:
179
0
        outlen = nchar << 2;
180
0
        cpyfunc = cpy_univ;
181
0
        break;
182
183
5.86M
    case MBSTRING_UTF8:
184
5.86M
        outlen = 0;
185
5.86M
        traverse_string(in, len, inform, out_utf8, &outlen);
186
5.86M
        cpyfunc = cpy_utf8;
187
5.86M
        break;
188
5.86M
    }
189
5.86M
    if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
190
0
        if (free_out) {
191
0
            ASN1_STRING_free(dest);
192
0
            *out = NULL;
193
0
        }
194
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
195
0
        return -1;
196
0
    }
197
5.86M
    dest->length = outlen;
198
5.86M
    dest->data = p;
199
5.86M
    p[outlen] = 0;
200
5.86M
    traverse_string(in, len, inform, cpyfunc, &p);
201
5.86M
    return str_type;
202
5.86M
}
203
204
/*
205
 * This function traverses a string and passes the value of each character to
206
 * an optional function along with a void * argument.
207
 */
208
209
static int traverse_string(const unsigned char *p, int len, int inform,
210
                           int (*rfunc) (unsigned long value, void *in),
211
                           void *arg)
212
25.4M
{
213
25.4M
    unsigned long value;
214
25.4M
    int ret;
215
1.33G
    while (len) {
216
1.31G
        if (inform == MBSTRING_ASC) {
217
1.24G
            value = *p++;
218
1.24G
            len--;
219
1.24G
        } else if (inform == MBSTRING_BMP) {
220
40.5M
            value = *p++ << 8;
221
40.5M
            value |= *p++;
222
40.5M
            len -= 2;
223
40.5M
        } else if (inform == MBSTRING_UNIV) {
224
176k
            value = ((unsigned long)*p++) << 24;
225
176k
            value |= ((unsigned long)*p++) << 16;
226
176k
            value |= *p++ << 8;
227
176k
            value |= *p++;
228
176k
            len -= 4;
229
22.9M
        } else {
230
22.9M
            ret = UTF8_getc(p, len, &value);
231
22.9M
            if (ret < 0)
232
27.3k
                return -1;
233
22.9M
            len -= ret;
234
22.9M
            p += ret;
235
22.9M
        }
236
1.31G
        if (rfunc) {
237
1.31G
            ret = rfunc(value, arg);
238
1.31G
            if (ret <= 0)
239
4.16k
                return ret;
240
1.31G
        }
241
1.31G
    }
242
25.4M
    return 1;
243
25.4M
}
244
245
/* Various utility functions for traverse_string */
246
247
/* Just count number of characters */
248
249
static int in_utf8(unsigned long value, void *arg)
250
12.4M
{
251
12.4M
    int *nchar;
252
253
12.4M
    if (!is_unicode_valid(value))
254
637
        return -2;
255
12.4M
    nchar = arg;
256
12.4M
    (*nchar)++;
257
12.4M
    return 1;
258
12.4M
}
259
260
/* Determine size of output as a UTF8 String */
261
262
static int out_utf8(unsigned long value, void *arg)
263
429M
{
264
429M
    int *outlen, len;
265
266
429M
    len = UTF8_putc(NULL, -1, value);
267
429M
    if (len <= 0)
268
0
        return len;
269
429M
    outlen = arg;
270
429M
    *outlen += len;
271
429M
    return 1;
272
429M
}
273
274
/*
275
 * Determine the "type" of a string: check each character against a supplied
276
 * "mask".
277
 */
278
279
static int type_str(unsigned long value, void *arg)
280
441M
{
281
441M
    unsigned long types = *((unsigned long *)arg);
282
441M
    const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value);
283
284
441M
    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native)
285
0
                                            || native == ' '))
286
0
        types &= ~B_ASN1_NUMERICSTRING;
287
441M
    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
288
0
        types &= ~B_ASN1_PRINTABLESTRING;
289
441M
    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
290
0
        types &= ~B_ASN1_IA5STRING;
291
441M
    if ((types & B_ASN1_T61STRING) && (value > 0xff))
292
0
        types &= ~B_ASN1_T61STRING;
293
441M
    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
294
0
        types &= ~B_ASN1_BMPSTRING;
295
441M
    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
296
3.52k
        types &= ~B_ASN1_UTF8STRING;
297
441M
    if (!types)
298
3.52k
        return -1;
299
441M
    *((unsigned long *)arg) = types;
300
441M
    return 1;
301
441M
}
302
303
/* Copy one byte per character ASCII like strings */
304
305
static int cpy_asc(unsigned long value, void *arg)
306
0
{
307
0
    unsigned char **p, *q;
308
0
    p = arg;
309
0
    q = *p;
310
0
    *q = (unsigned char)value;
311
0
    (*p)++;
312
0
    return 1;
313
0
}
314
315
/* Copy two byte per character BMPStrings */
316
317
static int cpy_bmp(unsigned long value, void *arg)
318
0
{
319
0
    unsigned char **p, *q;
320
0
    p = arg;
321
0
    q = *p;
322
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
323
0
    *q = (unsigned char)(value & 0xff);
324
0
    *p += 2;
325
0
    return 1;
326
0
}
327
328
/* Copy four byte per character UniversalStrings */
329
330
static int cpy_univ(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 >> 24) & 0xff);
336
0
    *q++ = (unsigned char)((value >> 16) & 0xff);
337
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
338
0
    *q = (unsigned char)(value & 0xff);
339
0
    *p += 4;
340
0
    return 1;
341
0
}
342
343
/* Copy to a UTF8String */
344
345
static int cpy_utf8(unsigned long value, void *arg)
346
429M
{
347
429M
    unsigned char **p;
348
429M
    int ret;
349
429M
    p = arg;
350
    /* We already know there is enough room so pass 0xff as the length */
351
429M
    ret = UTF8_putc(*p, 0xff, value);
352
429M
    *p += ret;
353
429M
    return 1;
354
429M
}