Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/asn1/a_mbstr.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2025 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
#include <crypto/asn1.h>
17
18
static int traverse_string(const unsigned char *p, int len, int inform,
19
    int (*rfunc)(unsigned long value, void *in),
20
    void *arg);
21
static int in_utf8(unsigned long value, void *arg);
22
static int out_utf8(unsigned long value, void *arg);
23
static int type_str(unsigned long value, void *arg);
24
static int cpy_asc(unsigned long value, void *arg);
25
static int cpy_bmp(unsigned long value, void *arg);
26
static int cpy_univ(unsigned long value, void *arg);
27
static int cpy_utf8(unsigned long value, void *arg);
28
29
/*
30
 * These functions take a string in UTF8, ASCII or multibyte form and a mask
31
 * of permissible ASN1 string types. It then works out the minimal type
32
 * (using the order Numeric < Printable < IA5 < T61 < BMP < Universal < UTF8)
33
 * and creates a string of the correct type with the supplied data. Yes this is
34
 * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
35
 * size limits too.
36
 */
37
38
int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
39
    int inform, unsigned long mask)
40
0
{
41
0
    return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
42
0
}
43
44
int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
45
    int inform, unsigned long mask,
46
    long minsize, long maxsize)
47
0
{
48
0
    int str_type;
49
0
    int ret;
50
0
    char free_out;
51
0
    int outform, outlen = 0;
52
0
    ASN1_STRING *dest;
53
0
    unsigned char *p;
54
0
    int nchar;
55
0
    int (*cpyfunc)(unsigned long, void *) = NULL;
56
0
    if (len == -1) {
57
0
        size_t len_s = strlen((const char *)in);
58
59
0
        if (len_s >= INT_MAX) {
60
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG);
61
0
            return -1;
62
0
        }
63
0
        len = (int)len_s;
64
0
    }
65
0
    if (!mask)
66
0
        mask = DIRSTRING_TYPE;
67
0
    if (len < 0) {
68
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
69
0
        return -1;
70
0
    }
71
72
    /* First do a string check and work out the number of characters */
73
0
    switch (inform) {
74
75
0
    case MBSTRING_BMP:
76
0
        if (len & 1) {
77
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
78
0
            return -1;
79
0
        }
80
0
        nchar = len >> 1;
81
0
        break;
82
83
0
    case MBSTRING_UNIV:
84
0
        if (len & 3) {
85
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
86
0
            return -1;
87
0
        }
88
0
        nchar = len >> 2;
89
0
        break;
90
91
0
    case MBSTRING_UTF8:
92
0
        nchar = 0;
93
        /* This counts the characters and does utf8 syntax checking */
94
0
        ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
95
0
        if (ret < 0) {
96
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
97
0
            return -1;
98
0
        }
99
0
        break;
100
101
0
    case MBSTRING_ASC:
102
0
        nchar = len;
103
0
        break;
104
105
0
    default:
106
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
107
0
        return -1;
108
0
    }
109
110
0
    if ((minsize > 0) && (nchar < minsize)) {
111
0
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_SHORT,
112
0
            "minsize=%ld", minsize);
113
0
        return -1;
114
0
    }
115
116
0
    if ((maxsize > 0) && (nchar > maxsize)) {
117
0
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG,
118
0
            "maxsize=%ld", maxsize);
119
0
        return -1;
120
0
    }
121
122
    /* Now work out minimal type (if any) */
123
0
    if (traverse_string(in, len, inform, type_str, &mask) < 0) {
124
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_CHARACTERS);
125
0
        return -1;
126
0
    }
127
128
    /*
129
     * Now work out output format and string type.
130
     * These checks should be in sync with the checks in type_str.
131
     */
132
0
    outform = MBSTRING_ASC;
133
0
    if (mask & B_ASN1_NUMERICSTRING)
134
0
        str_type = V_ASN1_NUMERICSTRING;
135
0
    else if (mask & B_ASN1_PRINTABLESTRING)
136
0
        str_type = V_ASN1_PRINTABLESTRING;
137
0
    else if (mask & B_ASN1_IA5STRING)
138
0
        str_type = V_ASN1_IA5STRING;
139
0
    else if (mask & B_ASN1_T61STRING)
140
0
        str_type = V_ASN1_T61STRING;
141
0
    else if (mask & B_ASN1_BMPSTRING) {
142
0
        str_type = V_ASN1_BMPSTRING;
143
0
        outform = MBSTRING_BMP;
144
0
    } else if (mask & B_ASN1_UNIVERSALSTRING) {
145
0
        str_type = V_ASN1_UNIVERSALSTRING;
146
0
        outform = MBSTRING_UNIV;
147
0
    } else {
148
0
        str_type = V_ASN1_UTF8STRING;
149
0
        outform = MBSTRING_UTF8;
150
0
    }
151
0
    if (!out)
152
0
        return str_type;
153
0
    if (*out) {
154
0
        free_out = 0;
155
0
        dest = *out;
156
0
        ASN1_STRING_set0(dest, NULL, 0);
157
0
        dest->type = str_type;
158
0
    } else {
159
0
        free_out = 1;
160
0
        dest = ASN1_STRING_type_new(str_type);
161
0
        if (dest == NULL) {
162
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
163
0
            return -1;
164
0
        }
165
0
        *out = dest;
166
0
    }
167
    /* If both the same type just copy across */
168
0
    if (inform == outform) {
169
0
        if (!ASN1_STRING_set(dest, in, len)) {
170
0
            if (free_out) {
171
0
                ASN1_STRING_free(dest);
172
0
                *out = NULL;
173
0
            }
174
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
175
0
            return -1;
176
0
        }
177
0
        return str_type;
178
0
    }
179
180
    /* Work out how much space the destination will need */
181
0
    switch (outform) {
182
0
    case MBSTRING_ASC:
183
0
        outlen = nchar;
184
0
        cpyfunc = cpy_asc;
185
0
        break;
186
187
0
    case MBSTRING_BMP:
188
0
        outlen = nchar << 1;
189
0
        cpyfunc = cpy_bmp;
190
0
        break;
191
192
0
    case MBSTRING_UNIV:
193
0
        outlen = nchar << 2;
194
0
        cpyfunc = cpy_univ;
195
0
        break;
196
197
0
    case MBSTRING_UTF8:
198
0
        outlen = 0;
199
0
        ret = traverse_string(in, len, inform, out_utf8, &outlen);
200
0
        if (ret < 0) {
201
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
202
0
            return -1;
203
0
        }
204
0
        cpyfunc = cpy_utf8;
205
0
        break;
206
0
    }
207
0
    if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
208
0
        if (free_out) {
209
0
            ASN1_STRING_free(dest);
210
0
            *out = NULL;
211
0
        }
212
0
        return -1;
213
0
    }
214
0
    dest->length = outlen;
215
0
    dest->data = p;
216
0
    p[outlen] = 0;
217
0
    traverse_string(in, len, inform, cpyfunc, &p);
218
0
    return str_type;
219
0
}
220
221
/*
222
 * This function traverses a string and passes the value of each character to
223
 * an optional function along with a void * argument.
224
 */
225
226
static int traverse_string(const unsigned char *p, int len, int inform,
227
    int (*rfunc)(unsigned long value, void *in),
228
    void *arg)
229
0
{
230
0
    unsigned long value;
231
0
    int ret;
232
0
    while (len) {
233
0
        if (inform == MBSTRING_ASC) {
234
0
            value = *p++;
235
0
            len--;
236
0
        } else if (inform == MBSTRING_BMP) {
237
0
            value = *p++ << 8;
238
0
            value |= *p++;
239
0
            len -= 2;
240
0
        } else if (inform == MBSTRING_UNIV) {
241
0
            value = ((unsigned long)*p++) << 24;
242
0
            value |= ((unsigned long)*p++) << 16;
243
0
            value |= *p++ << 8;
244
0
            value |= *p++;
245
0
            len -= 4;
246
0
        } else {
247
0
            ret = UTF8_getc(p, len, &value);
248
0
            if (ret < 0)
249
0
                return -1;
250
0
            len -= ret;
251
0
            p += ret;
252
0
        }
253
0
        if (rfunc) {
254
0
            ret = rfunc(value, arg);
255
0
            if (ret <= 0)
256
0
                return ret;
257
0
        }
258
0
    }
259
0
    return 1;
260
0
}
261
262
/* Various utility functions for traverse_string */
263
264
/* Just count number of characters */
265
266
static int in_utf8(unsigned long value, void *arg)
267
0
{
268
0
    int *nchar;
269
270
0
    if (!is_unicode_valid(value))
271
0
        return -2;
272
0
    nchar = arg;
273
0
    (*nchar)++;
274
0
    return 1;
275
0
}
276
277
/* Determine size of output as a UTF8 String */
278
279
static int out_utf8(unsigned long value, void *arg)
280
0
{
281
0
    int *outlen, len;
282
283
0
    len = UTF8_putc(NULL, -1, value);
284
0
    if (len <= 0)
285
0
        return len;
286
0
    outlen = arg;
287
0
    *outlen += len;
288
0
    return 1;
289
0
}
290
291
/*
292
 * Determine the "type" of a string: check each character against a supplied
293
 * "mask".
294
 */
295
296
static int type_str(unsigned long value, void *arg)
297
0
{
298
0
    unsigned long usable_types = *((unsigned long *)arg);
299
0
    unsigned long types = usable_types;
300
0
    const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value);
301
302
    /*
303
     * Clear out all the types which are not checked later. If any of those
304
     * is present in the mask, then the UTF8 type will be added and checked
305
     * below.
306
     */
307
0
    types &= B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING
308
0
        | B_ASN1_IA5STRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING
309
0
        | B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING;
310
311
    /*
312
     * If any other types were in the input mask, they're effectively treated
313
     * as UTF8
314
     */
315
0
    if (types != usable_types)
316
0
        types |= B_ASN1_UTF8STRING;
317
318
    /*
319
     * These checks should be in sync with ASN1_mbstring_ncopy.
320
     */
321
0
    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native) || native == ' '))
322
0
        types &= ~B_ASN1_NUMERICSTRING;
323
0
    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
324
0
        types &= ~B_ASN1_PRINTABLESTRING;
325
0
    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
326
0
        types &= ~B_ASN1_IA5STRING;
327
0
    if ((types & B_ASN1_T61STRING) && (value > 0xff))
328
0
        types &= ~B_ASN1_T61STRING;
329
0
    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
330
0
        types &= ~B_ASN1_BMPSTRING;
331
0
    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
332
0
        types &= ~B_ASN1_UTF8STRING;
333
0
    if (!types)
334
0
        return -1;
335
0
    *((unsigned long *)arg) = types;
336
0
    return 1;
337
0
}
338
339
/* Copy one byte per character ASCII like strings */
340
341
static int cpy_asc(unsigned long value, void *arg)
342
0
{
343
0
    unsigned char **p, *q;
344
0
    p = arg;
345
0
    q = *p;
346
0
    *q = (unsigned char)value;
347
0
    (*p)++;
348
0
    return 1;
349
0
}
350
351
/* Copy two byte per character BMPStrings */
352
353
static int cpy_bmp(unsigned long value, void *arg)
354
0
{
355
0
    unsigned char **p, *q;
356
0
    p = arg;
357
0
    q = *p;
358
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
359
0
    *q = (unsigned char)(value & 0xff);
360
0
    *p += 2;
361
0
    return 1;
362
0
}
363
364
/* Copy four byte per character UniversalStrings */
365
366
static int cpy_univ(unsigned long value, void *arg)
367
0
{
368
0
    unsigned char **p, *q;
369
0
    p = arg;
370
0
    q = *p;
371
0
    *q++ = (unsigned char)((value >> 24) & 0xff);
372
0
    *q++ = (unsigned char)((value >> 16) & 0xff);
373
0
    *q++ = (unsigned char)((value >> 8) & 0xff);
374
0
    *q = (unsigned char)(value & 0xff);
375
0
    *p += 4;
376
0
    return 1;
377
0
}
378
379
/* Copy to a UTF8String */
380
381
static int cpy_utf8(unsigned long value, void *arg)
382
0
{
383
0
    unsigned char **p;
384
0
    int ret;
385
0
    p = arg;
386
    /* We already know there is enough room so pass 0xff as the length */
387
0
    ret = UTF8_putc(*p, 0xff, value);
388
0
    if (ret < 0)
389
0
        return ret;
390
0
    *p += ret;
391
0
    return 1;
392
0
}