Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/asn1/asn1_gen.c
Line
Count
Source
1
/*
2
 * Copyright 2002-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 <openssl/asn1.h>
11
#include <openssl/x509v3.h>
12
#include "internal/cryptlib.h"
13
#include "crypto/asn1.h"
14
15
48.4k
#define ASN1_GEN_FLAG 0x10000
16
5.71k
#define ASN1_GEN_FLAG_IMP (ASN1_GEN_FLAG | 1)
17
3.66k
#define ASN1_GEN_FLAG_EXP (ASN1_GEN_FLAG | 2)
18
#define ASN1_GEN_FLAG_TAG (ASN1_GEN_FLAG | 3)
19
972
#define ASN1_GEN_FLAG_BITWRAP (ASN1_GEN_FLAG | 4)
20
1.01k
#define ASN1_GEN_FLAG_OCTWRAP (ASN1_GEN_FLAG | 5)
21
1.02k
#define ASN1_GEN_FLAG_SEQWRAP (ASN1_GEN_FLAG | 6)
22
658
#define ASN1_GEN_FLAG_SETWRAP (ASN1_GEN_FLAG | 7)
23
3.63k
#define ASN1_GEN_FLAG_FORMAT (ASN1_GEN_FLAG | 8)
24
25
1.59M
#define ASN1_GEN_STR(str, val) { str, sizeof(str) - 1, val }
26
27
7.00k
#define ASN1_FLAG_EXP_MAX 20
28
/* Maximum number of nested sequences */
29
3.87k
#define ASN1_GEN_SEQ_MAX_DEPTH 50
30
31
/* Input formats */
32
33
/* ASCII: default */
34
28.5k
#define ASN1_GEN_FORMAT_ASCII 1
35
/* UTF8 */
36
1.80k
#define ASN1_GEN_FORMAT_UTF8 2
37
/* Hex */
38
3.11k
#define ASN1_GEN_FORMAT_HEX 3
39
/* List of bits */
40
2.38k
#define ASN1_GEN_FORMAT_BITLIST 4
41
42
struct tag_name_st {
43
    const char *strnam;
44
    int len;
45
    int tag;
46
};
47
48
typedef struct {
49
    int exp_tag;
50
    int exp_class;
51
    int exp_constructed;
52
    int exp_pad;
53
    long exp_len;
54
} tag_exp_type;
55
56
typedef struct {
57
    int imp_tag;
58
    int imp_class;
59
    int utype;
60
    int format;
61
    const char *str;
62
    tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
63
    int exp_count;
64
} tag_exp_arg;
65
66
static ASN1_TYPE *generate_v3(const char *str, X509V3_CTX *cnf, int depth,
67
    int *perr);
68
static int bitstr_cb(const char *elem, int len, void *bitstr);
69
static int asn1_cb(const char *elem, int len, void *bitstr);
70
static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
71
    int exp_constructed, int exp_pad, int imp_ok);
72
static int parse_tagging(const char *vstart, int vlen, int *ptag,
73
    int *pclass);
74
static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
75
    int depth, int *perr);
76
static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype);
77
static int asn1_str2tag(const char *tagstr, int len);
78
79
ASN1_TYPE *ASN1_generate_nconf(const char *str, CONF *nconf)
80
0
{
81
0
    X509V3_CTX cnf;
82
83
0
    if (!nconf)
84
0
        return ASN1_generate_v3(str, NULL);
85
86
0
    X509V3_set_nconf(&cnf, nconf);
87
0
    return ASN1_generate_v3(str, &cnf);
88
0
}
89
90
ASN1_TYPE *ASN1_generate_v3(const char *str, X509V3_CTX *cnf)
91
13.3k
{
92
13.3k
    int err = 0;
93
13.3k
    ASN1_TYPE *ret = generate_v3(str, cnf, 0, &err);
94
13.3k
    if (err)
95
13.3k
        ERR_raise(ERR_LIB_ASN1, err);
96
13.3k
    return ret;
97
13.3k
}
98
99
static ASN1_TYPE *generate_v3(const char *str, X509V3_CTX *cnf, int depth,
100
    int *perr)
101
17.6k
{
102
17.6k
    ASN1_TYPE *ret;
103
17.6k
    tag_exp_arg asn1_tags;
104
17.6k
    tag_exp_type *etmp;
105
106
17.6k
    int i, len;
107
108
17.6k
    unsigned char *orig_der = NULL, *new_der = NULL;
109
17.6k
    const unsigned char *cpy_start;
110
17.6k
    unsigned char *p;
111
17.6k
    const unsigned char *cp;
112
17.6k
    int cpy_len;
113
17.6k
    long hdr_len = 0;
114
17.6k
    int hdr_constructed = 0, hdr_tag, hdr_class;
115
17.6k
    int r;
116
117
17.6k
    asn1_tags.imp_tag = -1;
118
17.6k
    asn1_tags.imp_class = -1;
119
17.6k
    asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
120
17.6k
    asn1_tags.exp_count = 0;
121
17.6k
    if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0) {
122
2.61k
        *perr = ASN1_R_UNKNOWN_TAG;
123
2.61k
        return NULL;
124
2.61k
    }
125
126
15.0k
    if ((asn1_tags.utype == V_ASN1_SEQUENCE)
127
12.3k
        || (asn1_tags.utype == V_ASN1_SET)) {
128
3.87k
        if (!cnf) {
129
0
            *perr = ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG;
130
0
            return NULL;
131
0
        }
132
3.87k
        if (depth >= ASN1_GEN_SEQ_MAX_DEPTH) {
133
38
            *perr = ASN1_R_ILLEGAL_NESTED_TAGGING;
134
38
            return NULL;
135
38
        }
136
3.83k
        ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf, depth, perr);
137
11.2k
    } else {
138
11.2k
        ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
139
11.2k
    }
140
141
15.0k
    if (!ret)
142
3.94k
        return NULL;
143
144
    /* If no tagging return base type */
145
11.0k
    if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
146
6.72k
        return ret;
147
148
    /* Generate the encoding */
149
4.37k
    cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
150
4.37k
    ASN1_TYPE_free(ret);
151
4.37k
    ret = NULL;
152
4.37k
    if (orig_der == NULL)
153
6
        return NULL;
154
    /* Set point to start copying for modified encoding */
155
4.36k
    cpy_start = orig_der;
156
157
    /* Do we need IMPLICIT tagging? */
158
4.36k
    if (asn1_tags.imp_tag != -1) {
159
        /* If IMPLICIT we will replace the underlying tag */
160
        /* Skip existing tag+len */
161
2.66k
        r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class,
162
2.66k
            cpy_len);
163
2.66k
        if (r & 0x80)
164
0
            goto err;
165
        /* Update copy length */
166
2.66k
        cpy_len -= cpy_start - orig_der;
167
        /*
168
         * For IMPLICIT tagging the length should match the original length
169
         * and constructed flag should be consistent.
170
         */
171
2.66k
        if (r & 0x1) {
172
            /* Indefinite length constructed */
173
0
            hdr_constructed = 2;
174
0
            hdr_len = 0;
175
2.66k
        } else {
176
            /* Just retain constructed flag */
177
2.66k
            hdr_constructed = r & V_ASN1_CONSTRUCTED;
178
2.66k
        }
179
        /*
180
         * Work out new length with IMPLICIT tag: ignore constructed because
181
         * it will mess up if indefinite length
182
         */
183
2.66k
        len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
184
2.66k
        if (len == -1)
185
0
            goto err;
186
2.66k
    } else {
187
1.70k
        len = cpy_len;
188
1.70k
    }
189
190
    /* Work out length in any EXPLICIT, starting from end */
191
192
4.36k
    for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
193
7.34k
        i < asn1_tags.exp_count; i++, etmp--) {
194
        /* Content length: number of content octets + any padding */
195
2.98k
        len += etmp->exp_pad;
196
2.98k
        etmp->exp_len = len;
197
        /* Total object length: length including new header */
198
2.98k
        len = ASN1_object_size(0, len, etmp->exp_tag);
199
2.98k
        if (len == -1)
200
0
            goto err;
201
2.98k
    }
202
203
    /* Allocate buffer for new encoding */
204
205
4.36k
    new_der = OPENSSL_malloc(len);
206
4.36k
    if (new_der == NULL)
207
0
        goto err;
208
209
    /* Generate tagged encoding */
210
211
4.36k
    p = new_der;
212
213
    /* Output explicit tags first */
214
215
7.34k
    for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
216
4.36k
        i++, etmp++) {
217
2.98k
        ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
218
2.98k
            etmp->exp_tag, etmp->exp_class);
219
2.98k
        if (etmp->exp_pad)
220
250
            *p++ = 0;
221
2.98k
    }
222
223
    /* If IMPLICIT, output tag */
224
225
4.36k
    if (asn1_tags.imp_tag != -1) {
226
2.66k
        if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
227
2.25k
            && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
228
2.19k
                || asn1_tags.imp_tag == V_ASN1_SET))
229
101
            hdr_constructed = V_ASN1_CONSTRUCTED;
230
2.66k
        ASN1_put_object(&p, hdr_constructed, hdr_len,
231
2.66k
            asn1_tags.imp_tag, asn1_tags.imp_class);
232
2.66k
    }
233
234
    /* Copy across original encoding */
235
4.36k
    memcpy(p, cpy_start, cpy_len);
236
237
4.36k
    cp = new_der;
238
239
    /* Obtain new ASN1_TYPE structure */
240
4.36k
    ret = d2i_ASN1_TYPE(NULL, &cp, len);
241
242
4.36k
err:
243
4.36k
    OPENSSL_free(orig_der);
244
4.36k
    OPENSSL_free(new_der);
245
246
4.36k
    return ret;
247
4.36k
}
248
249
static int asn1_cb(const char *elem, int len, void *bitstr)
250
32.8k
{
251
32.8k
    tag_exp_arg *arg = bitstr;
252
32.8k
    int i;
253
32.8k
    int utype;
254
32.8k
    int vlen = 0;
255
32.8k
    const char *p, *vstart = NULL;
256
257
32.8k
    int tmp_tag, tmp_class;
258
259
32.8k
    if (elem == NULL)
260
320
        return -1;
261
262
985k
    for (i = 0, p = elem; i < len; p++, i++) {
263
        /* Look for the ':' in name value pairs */
264
978k
        if (*p == ':') {
265
25.6k
            vstart = p + 1;
266
25.6k
            vlen = len - (vstart - elem);
267
25.6k
            len = p - elem;
268
25.6k
            break;
269
25.6k
        }
270
978k
    }
271
272
32.5k
    utype = asn1_str2tag(elem, len);
273
274
32.5k
    if (utype == -1) {
275
785
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_UNKNOWN_TAG, "tag=%s", elem);
276
785
        return -1;
277
785
    }
278
279
    /* If this is not a modifier mark end of string and exit */
280
31.7k
    if (!(utype & ASN1_GEN_FLAG)) {
281
15.0k
        arg->utype = utype;
282
15.0k
        arg->str = vstart;
283
        /* If no value and not end of string, error */
284
15.0k
        if (!vstart && elem[len]) {
285
12
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_VALUE);
286
12
            return -1;
287
12
        }
288
15.0k
        return 0;
289
15.0k
    }
290
291
16.6k
    switch (utype) {
292
293
5.71k
    case ASN1_GEN_FLAG_IMP:
294
        /* Check for illegal multiple IMPLICIT tagging */
295
5.71k
        if (arg->imp_tag != -1) {
296
125
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING);
297
125
            return -1;
298
125
        }
299
5.58k
        if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
300
130
            return -1;
301
5.45k
        break;
302
303
5.45k
    case ASN1_GEN_FLAG_EXP:
304
305
3.66k
        if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
306
314
            return -1;
307
3.35k
        if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
308
28
            return -1;
309
3.32k
        break;
310
311
3.32k
    case ASN1_GEN_FLAG_SEQWRAP:
312
1.02k
        if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
313
6
            return -1;
314
1.01k
        break;
315
316
1.01k
    case ASN1_GEN_FLAG_SETWRAP:
317
658
        if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
318
6
            return -1;
319
652
        break;
320
321
972
    case ASN1_GEN_FLAG_BITWRAP:
322
972
        if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
323
8
            return -1;
324
964
        break;
325
326
1.01k
    case ASN1_GEN_FLAG_OCTWRAP:
327
1.01k
        if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
328
10
            return -1;
329
1.00k
        break;
330
331
3.63k
    case ASN1_GEN_FLAG_FORMAT:
332
3.63k
        if (!vstart) {
333
10
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
334
10
            return -1;
335
10
        }
336
3.62k
        if (HAS_PREFIX(vstart, "ASCII"))
337
390
            arg->format = ASN1_GEN_FORMAT_ASCII;
338
3.23k
        else if (HAS_PREFIX(vstart, "UTF8"))
339
1.10k
            arg->format = ASN1_GEN_FORMAT_UTF8;
340
2.13k
        else if (HAS_PREFIX(vstart, "HEX"))
341
429
            arg->format = ASN1_GEN_FORMAT_HEX;
342
1.70k
        else if (HAS_PREFIX(vstart, "BITLIST"))
343
1.32k
            arg->format = ASN1_GEN_FORMAT_BITLIST;
344
384
        else {
345
384
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
346
384
            return -1;
347
384
        }
348
3.24k
        break;
349
16.6k
    }
350
351
15.6k
    return 1;
352
16.6k
}
353
354
static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
355
9.25k
{
356
9.25k
    long tag_num;
357
9.25k
    char *eptr;
358
9.25k
    if (!vstart)
359
18
        return 0;
360
9.23k
    tag_num = strtoul(vstart, &eptr, 10);
361
    /* Check we haven't gone past max length: should be impossible */
362
9.23k
    if (eptr && *eptr && (eptr > vstart + vlen))
363
0
        return 0;
364
9.23k
    if (tag_num < 0) {
365
286
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_NUMBER);
366
286
        return 0;
367
286
    }
368
8.95k
    *ptag = tag_num;
369
    /* If we have non numeric characters, parse them */
370
8.95k
    if (eptr)
371
8.95k
        vlen -= eptr - vstart;
372
0
    else
373
0
        vlen = 0;
374
8.95k
    if (vlen) {
375
6.63k
        switch (*eptr) {
376
377
4.43k
        case 'U':
378
4.43k
            *pclass = V_ASN1_UNIVERSAL;
379
4.43k
            break;
380
381
1.20k
        case 'A':
382
1.20k
            *pclass = V_ASN1_APPLICATION;
383
1.20k
            break;
384
385
533
        case 'P':
386
533
            *pclass = V_ASN1_PRIVATE;
387
533
            break;
388
389
328
        case 'C':
390
328
            *pclass = V_ASN1_CONTEXT_SPECIFIC;
391
328
            break;
392
393
140
        default:
394
140
            ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MODIFIER,
395
140
                "Char=%c", *eptr);
396
140
            return 0;
397
6.63k
        }
398
6.63k
    } else
399
2.31k
        *pclass = V_ASN1_CONTEXT_SPECIFIC;
400
401
8.81k
    return 1;
402
8.95k
}
403
404
/* Handle multiple types: SET and SEQUENCE */
405
406
static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
407
    int depth, int *perr)
408
3.83k
{
409
3.83k
    ASN1_TYPE *ret = NULL;
410
3.83k
    STACK_OF(ASN1_TYPE) *sk = NULL;
411
3.83k
    STACK_OF(CONF_VALUE) *sect = NULL;
412
3.83k
    unsigned char *der = NULL;
413
3.83k
    int derlen;
414
3.83k
    int i;
415
3.83k
    sk = sk_ASN1_TYPE_new_null();
416
3.83k
    if (!sk)
417
0
        goto bad;
418
3.83k
    if (section) {
419
2.11k
        if (!cnf)
420
0
            goto bad;
421
2.11k
        sect = X509V3_get_section(cnf, (char *)section);
422
2.11k
        if (!sect)
423
12
            goto bad;
424
4.52k
        for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
425
4.34k
            ASN1_TYPE *typ = generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf,
426
4.34k
                depth + 1, perr);
427
4.34k
            if (!typ)
428
1.92k
                goto bad;
429
430
2.42k
            if (!sk_ASN1_TYPE_push(sk, typ)) {
431
0
                ASN1_TYPE_free(typ);
432
0
                goto bad;
433
0
            }
434
2.42k
        }
435
2.10k
    }
436
437
    /*
438
     * Now we has a STACK of the components, convert to the correct form
439
     */
440
441
1.90k
    if (utype == V_ASN1_SET)
442
754
        derlen = i2d_ASN1_SET_ANY(sk, &der);
443
1.14k
    else
444
1.14k
        derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
445
446
1.90k
    if (derlen < 0)
447
0
        goto bad;
448
1.90k
    if ((ret = ASN1_TYPE_new()) == NULL)
449
0
        goto bad;
450
1.90k
    if ((ret->value.asn1_string = ASN1_STRING_type_new(utype)) == NULL)
451
0
        goto bad;
452
453
1.90k
    ret->type = utype;
454
1.90k
    ret->value.asn1_string->data = der;
455
1.90k
    ret->value.asn1_string->length = derlen;
456
457
1.90k
    der = NULL;
458
459
3.83k
bad:
460
461
3.83k
    OPENSSL_free(der);
462
463
3.83k
    sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
464
3.83k
    X509V3_section_free(cnf, sect);
465
466
3.83k
    return ret;
467
1.90k
}
468
469
static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
470
    int exp_constructed, int exp_pad, int imp_ok)
471
7.02k
{
472
7.02k
    tag_exp_type *exp_tmp;
473
    /* Can only have IMPLICIT if permitted */
474
7.02k
    if ((arg->imp_tag != -1) && !imp_ok) {
475
20
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_IMPLICIT_TAG);
476
20
        return 0;
477
20
    }
478
479
7.00k
    if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
480
38
        ERR_raise(ERR_LIB_ASN1, ASN1_R_DEPTH_EXCEEDED);
481
38
        return 0;
482
38
    }
483
484
6.96k
    exp_tmp = &arg->exp_list[arg->exp_count++];
485
486
    /*
487
     * If IMPLICIT set tag to implicit value then reset implicit tag since it
488
     * has been used.
489
     */
490
6.96k
    if (arg->imp_tag != -1) {
491
2.35k
        exp_tmp->exp_tag = arg->imp_tag;
492
2.35k
        exp_tmp->exp_class = arg->imp_class;
493
2.35k
        arg->imp_tag = -1;
494
2.35k
        arg->imp_class = -1;
495
4.61k
    } else {
496
4.61k
        exp_tmp->exp_tag = exp_tag;
497
4.61k
        exp_tmp->exp_class = exp_class;
498
4.61k
    }
499
6.96k
    exp_tmp->exp_constructed = exp_constructed;
500
6.96k
    exp_tmp->exp_pad = exp_pad;
501
502
6.96k
    return 1;
503
7.00k
}
504
505
static int asn1_str2tag(const char *tagstr, int len)
506
32.5k
{
507
32.5k
    unsigned int i;
508
32.5k
    const struct tag_name_st *tntmp;
509
32.5k
    static const struct tag_name_st tnst[] = {
510
32.5k
        ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
511
32.5k
        ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
512
32.5k
        ASN1_GEN_STR("NULL", V_ASN1_NULL),
513
32.5k
        ASN1_GEN_STR("INT", V_ASN1_INTEGER),
514
32.5k
        ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
515
32.5k
        ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
516
32.5k
        ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
517
32.5k
        ASN1_GEN_STR("OID", V_ASN1_OBJECT),
518
32.5k
        ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
519
32.5k
        ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
520
32.5k
        ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
521
32.5k
        ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
522
32.5k
        ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
523
32.5k
        ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
524
32.5k
        ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
525
32.5k
        ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
526
32.5k
        ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
527
32.5k
        ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
528
32.5k
        ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
529
32.5k
        ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
530
32.5k
        ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
531
32.5k
        ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
532
32.5k
        ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
533
32.5k
        ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
534
32.5k
        ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
535
32.5k
        ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
536
32.5k
        ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
537
32.5k
        ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
538
32.5k
        ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
539
32.5k
        ASN1_GEN_STR("T61", V_ASN1_T61STRING),
540
32.5k
        ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
541
32.5k
        ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
542
32.5k
        ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
543
32.5k
        ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
544
32.5k
        ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
545
32.5k
        ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
546
547
        /* Special cases */
548
32.5k
        ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
549
32.5k
        ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
550
32.5k
        ASN1_GEN_STR("SET", V_ASN1_SET),
551
        /* type modifiers */
552
        /* Explicit tag */
553
32.5k
        ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
554
32.5k
        ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
555
        /* Implicit tag */
556
32.5k
        ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
557
32.5k
        ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
558
        /* OCTET STRING wrapper */
559
32.5k
        ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
560
        /* SEQUENCE wrapper */
561
32.5k
        ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
562
        /* SET wrapper */
563
32.5k
        ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
564
        /* BIT STRING wrapper */
565
32.5k
        ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
566
32.5k
        ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
567
32.5k
        ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
568
32.5k
    };
569
570
32.5k
    if (len == -1)
571
0
        len = strlen(tagstr);
572
573
32.5k
    tntmp = tnst;
574
1.09M
    for (i = 0; i < OSSL_NELEM(tnst); i++, tntmp++) {
575
1.09M
        if ((len == tntmp->len)
576
221k
            && (OPENSSL_strncasecmp(tntmp->strnam, tagstr, len) == 0))
577
31.7k
            return tntmp->tag;
578
1.09M
    }
579
580
785
    return -1;
581
32.5k
}
582
583
static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
584
11.2k
{
585
11.2k
    ASN1_TYPE *atmp = NULL;
586
11.2k
    CONF_VALUE vtmp;
587
11.2k
    unsigned char *rdata;
588
11.2k
    long rdlen;
589
11.2k
    int no_unused = 1;
590
591
11.2k
    if ((atmp = ASN1_TYPE_new()) == NULL) {
592
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
593
0
        return NULL;
594
0
    }
595
596
11.2k
    if (!str)
597
1.70k
        str = "";
598
599
11.2k
    switch (utype) {
600
601
686
    case V_ASN1_NULL:
602
686
        if (str && *str) {
603
9
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL_VALUE);
604
9
            goto bad_form;
605
9
        }
606
677
        break;
607
608
680
    case V_ASN1_BOOLEAN:
609
680
        if (format != ASN1_GEN_FORMAT_ASCII) {
610
6
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ASCII_FORMAT);
611
6
            goto bad_form;
612
6
        }
613
674
        vtmp.name = NULL;
614
674
        vtmp.section = NULL;
615
674
        vtmp.value = (char *)str;
616
674
        if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
617
355
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BOOLEAN);
618
355
            goto bad_str;
619
355
        }
620
319
        break;
621
622
1.19k
    case V_ASN1_INTEGER:
623
1.22k
    case V_ASN1_ENUMERATED:
624
1.22k
        if (format != ASN1_GEN_FORMAT_ASCII) {
625
6
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
626
6
            goto bad_form;
627
6
        }
628
1.21k
        if ((atmp->value.integer
629
1.21k
                = s2i_ASN1_INTEGER(NULL, str))
630
1.21k
            == NULL) {
631
81
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_INTEGER);
632
81
            goto bad_str;
633
81
        }
634
1.13k
        break;
635
636
1.13k
    case V_ASN1_OBJECT:
637
114
        if (format != ASN1_GEN_FORMAT_ASCII) {
638
6
            ERR_raise(ERR_LIB_ASN1, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
639
6
            goto bad_form;
640
6
        }
641
108
        if ((atmp->value.object = OBJ_txt2obj(str, 0)) == NULL) {
642
21
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OBJECT);
643
21
            goto bad_str;
644
21
        }
645
87
        break;
646
647
874
    case V_ASN1_UTCTIME:
648
1.23k
    case V_ASN1_GENERALIZEDTIME:
649
1.23k
        if (format != ASN1_GEN_FORMAT_ASCII) {
650
6
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TIME_NOT_ASCII_FORMAT);
651
6
            goto bad_form;
652
6
        }
653
1.23k
        if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
654
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
655
0
            goto bad_str;
656
0
        }
657
1.23k
        if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
658
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
659
0
            goto bad_str;
660
0
        }
661
1.23k
        atmp->value.asn1_string->type = utype;
662
1.23k
        if (!ASN1_TIME_check(atmp->value.asn1_string)) {
663
658
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
664
658
            goto bad_str;
665
658
        }
666
667
572
        break;
668
669
1.25k
    case V_ASN1_BMPSTRING:
670
1.36k
    case V_ASN1_PRINTABLESTRING:
671
1.53k
    case V_ASN1_IA5STRING:
672
2.79k
    case V_ASN1_T61STRING:
673
4.20k
    case V_ASN1_UTF8STRING:
674
4.21k
    case V_ASN1_VISIBLESTRING:
675
4.54k
    case V_ASN1_UNIVERSALSTRING:
676
4.57k
    case V_ASN1_GENERALSTRING:
677
4.57k
    case V_ASN1_NUMERICSTRING:
678
4.57k
        if (format == ASN1_GEN_FORMAT_ASCII)
679
3.88k
            format = MBSTRING_ASC;
680
697
        else if (format == ASN1_GEN_FORMAT_UTF8)
681
691
            format = MBSTRING_UTF8;
682
6
        else {
683
6
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_FORMAT);
684
6
            goto bad_form;
685
6
        }
686
687
4.57k
        if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
688
4.57k
                -1, format, ASN1_tag2bit(utype))
689
4.57k
            <= 0) {
690
410
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
691
410
            goto bad_str;
692
410
        }
693
694
4.16k
        break;
695
696
4.16k
    case V_ASN1_BIT_STRING:
697
2.68k
    case V_ASN1_OCTET_STRING:
698
2.68k
        if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
699
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
700
0
            goto bad_form;
701
0
        }
702
703
2.68k
        if (format == ASN1_GEN_FORMAT_HEX) {
704
19
            if ((rdata = OPENSSL_hexstr2buf(str, &rdlen)) == NULL) {
705
6
                ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_HEX);
706
6
                goto bad_str;
707
6
            }
708
13
            atmp->value.asn1_string->data = rdata;
709
13
            atmp->value.asn1_string->length = rdlen;
710
13
            atmp->value.asn1_string->type = utype;
711
2.66k
        } else if (format == ASN1_GEN_FORMAT_ASCII) {
712
1.60k
            if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
713
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
714
0
                goto bad_str;
715
0
            }
716
1.60k
        } else if ((format == ASN1_GEN_FORMAT_BITLIST)
717
1.05k
            && (utype == V_ASN1_BIT_STRING)) {
718
1.05k
            if (!CONF_parse_list(str, ',', 1, bitstr_cb, atmp->value.bit_string)) {
719
428
                ERR_raise(ERR_LIB_ASN1, ASN1_R_LIST_ERROR);
720
428
                goto bad_str;
721
428
            }
722
624
            no_unused = 0;
723
724
624
        } else {
725
12
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
726
12
            goto bad_form;
727
12
        }
728
729
2.23k
        if ((utype == V_ASN1_BIT_STRING) && no_unused)
730
618
            ossl_asn1_string_set_bits_left(atmp->value.asn1_string, 0);
731
732
2.23k
        break;
733
734
0
    default:
735
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
736
0
        goto bad_str;
737
11.2k
    }
738
739
9.19k
    atmp->type = utype;
740
9.19k
    return atmp;
741
742
1.95k
bad_str:
743
1.95k
    ERR_add_error_data(2, "string=", str);
744
2.01k
bad_form:
745
746
2.01k
    ASN1_TYPE_free(atmp);
747
2.01k
    return NULL;
748
1.95k
}
749
750
static int bitstr_cb(const char *elem, int len, void *bitstr)
751
2.48k
{
752
2.48k
    long bitnum;
753
2.48k
    char *eptr;
754
2.48k
    if (!elem)
755
40
        return 0;
756
2.44k
    bitnum = strtoul(elem, &eptr, 10);
757
2.44k
    if (eptr && *eptr && (eptr != elem + len))
758
36
        return 0;
759
2.40k
    if (bitnum < 0) {
760
240
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_NUMBER);
761
240
        return 0;
762
240
    }
763
2.16k
    if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
764
112
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
765
112
        return 0;
766
112
    }
767
2.05k
    return 1;
768
2.16k
}
769
770
static int mask_cb(const char *elem, int len, void *arg)
771
0
{
772
0
    unsigned long *pmask = arg, tmpmask;
773
0
    int tag;
774
0
    if (elem == NULL)
775
0
        return 0;
776
0
    if (len == 3 && HAS_PREFIX(elem, "DIR")) {
777
0
        *pmask |= B_ASN1_DIRECTORYSTRING;
778
0
        return 1;
779
0
    }
780
0
    tag = asn1_str2tag(elem, len);
781
0
    if (!tag || (tag & ASN1_GEN_FLAG))
782
0
        return 0;
783
0
    tmpmask = ASN1_tag2bit(tag);
784
0
    if (!tmpmask)
785
0
        return 0;
786
0
    *pmask |= tmpmask;
787
0
    return 1;
788
0
}
789
790
int ASN1_str2mask(const char *str, unsigned long *pmask)
791
0
{
792
0
    *pmask = 0;
793
0
    return CONF_parse_list(str, '|', 1, mask_cb, pmask);
794
0
}