Coverage Report

Created: 2025-08-11 07:04

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