Coverage Report

Created: 2026-08-31 06:56

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