Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
22.4k
#define ASN1_GEN_FLAG 0x10000
16
2.75k
#define ASN1_GEN_FLAG_IMP (ASN1_GEN_FLAG | 1)
17
1.83k
#define ASN1_GEN_FLAG_EXP (ASN1_GEN_FLAG | 2)
18
#define ASN1_GEN_FLAG_TAG (ASN1_GEN_FLAG | 3)
19
426
#define ASN1_GEN_FLAG_BITWRAP (ASN1_GEN_FLAG | 4)
20
471
#define ASN1_GEN_FLAG_OCTWRAP (ASN1_GEN_FLAG | 5)
21
619
#define ASN1_GEN_FLAG_SEQWRAP (ASN1_GEN_FLAG | 6)
22
451
#define ASN1_GEN_FLAG_SETWRAP (ASN1_GEN_FLAG | 7)
23
2.16k
#define ASN1_GEN_FLAG_FORMAT (ASN1_GEN_FLAG | 8)
24
25
691k
#define ASN1_GEN_STR(str, val) { str, sizeof(str) - 1, val }
26
27
3.64k
#define ASN1_FLAG_EXP_MAX 20
28
/* Maximum number of nested sequences */
29
284
#define ASN1_GEN_SEQ_MAX_DEPTH 50
30
31
/* Input formats */
32
33
/* ASCII: default */
34
10.6k
#define ASN1_GEN_FORMAT_ASCII 1
35
/* UTF8 */
36
912
#define ASN1_GEN_FORMAT_UTF8 2
37
/* Hex */
38
968
#define ASN1_GEN_FORMAT_HEX 3
39
/* List of bits */
40
1.27k
#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
6.08k
{
92
6.08k
    int err = 0;
93
6.08k
    ASN1_TYPE *ret = generate_v3(str, cnf, 0, &err);
94
6.08k
    if (err)
95
6.08k
        ERR_raise(ERR_LIB_ASN1, err);
96
6.08k
    return ret;
97
6.08k
}
98
99
static ASN1_TYPE *generate_v3(const char *str, X509V3_CTX *cnf, int depth,
100
    int *perr)
101
6.08k
{
102
6.08k
    ASN1_TYPE *ret;
103
6.08k
    tag_exp_arg asn1_tags;
104
6.08k
    tag_exp_type *etmp;
105
106
6.08k
    int i, len;
107
108
6.08k
    unsigned char *orig_der = NULL, *new_der = NULL;
109
6.08k
    const unsigned char *cpy_start;
110
6.08k
    unsigned char *p;
111
6.08k
    const unsigned char *cp;
112
6.08k
    int cpy_len;
113
6.08k
    long hdr_len = 0;
114
6.08k
    int hdr_constructed = 0, hdr_tag, hdr_class;
115
6.08k
    int r;
116
117
6.08k
    asn1_tags.imp_tag = -1;
118
6.08k
    asn1_tags.imp_class = -1;
119
6.08k
    asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
120
6.08k
    asn1_tags.exp_count = 0;
121
6.08k
    if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0) {
122
1.10k
        *perr = ASN1_R_UNKNOWN_TAG;
123
1.10k
        return NULL;
124
1.10k
    }
125
126
4.97k
    if ((asn1_tags.utype == V_ASN1_SEQUENCE)
127
4.80k
        || (asn1_tags.utype == V_ASN1_SET)) {
128
284
        if (!cnf) {
129
0
            *perr = ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG;
130
0
            return NULL;
131
0
        }
132
284
        if (depth >= ASN1_GEN_SEQ_MAX_DEPTH) {
133
0
            *perr = ASN1_R_ILLEGAL_NESTED_TAGGING;
134
0
            return NULL;
135
0
        }
136
284
        ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf, depth, perr);
137
4.69k
    } else {
138
4.69k
        ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
139
4.69k
    }
140
141
4.97k
    if (!ret)
142
1.00k
        return NULL;
143
144
    /* If no tagging return base type */
145
3.97k
    if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
146
1.93k
        return ret;
147
148
    /* Generate the encoding */
149
2.03k
    cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
150
2.03k
    ASN1_TYPE_free(ret);
151
2.03k
    ret = NULL;
152
2.03k
    if (orig_der == NULL)
153
3
        return NULL;
154
    /* Set point to start copying for modified encoding */
155
2.03k
    cpy_start = orig_der;
156
157
    /* Do we need IMPLICIT tagging? */
158
2.03k
    if (asn1_tags.imp_tag != -1) {
159
        /* If IMPLICIT we will replace the underlying tag */
160
        /* Skip existing tag+len */
161
1.06k
        r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class,
162
1.06k
            cpy_len);
163
1.06k
        if (r & 0x80)
164
0
            goto err;
165
        /* Update copy length */
166
1.06k
        cpy_len -= (int)(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
1.06k
        if (r & 0x1) {
172
            /* Indefinite length constructed */
173
0
            hdr_constructed = 2;
174
0
            hdr_len = 0;
175
1.06k
        } else {
176
            /* Just retain constructed flag */
177
1.06k
            hdr_constructed = r & V_ASN1_CONSTRUCTED;
178
1.06k
        }
179
        /*
180
         * Work out new length with IMPLICIT tag: ignore constructed because
181
         * it will mess up if indefinite length
182
         */
183
1.06k
        len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
184
1.06k
        if (len == -1)
185
0
            goto err;
186
1.06k
    } else {
187
967
        len = cpy_len;
188
967
    }
189
190
    /* Work out length in any EXPLICIT, starting from end */
191
192
2.03k
    for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
193
3.79k
        i < asn1_tags.exp_count; i++, etmp--) {
194
        /* Content length: number of content octets + any padding */
195
1.75k
        len += etmp->exp_pad;
196
1.75k
        etmp->exp_len = len;
197
        /* Total object length: length including new header */
198
1.75k
        len = ASN1_object_size(0, len, etmp->exp_tag);
199
1.75k
        if (len == -1)
200
0
            goto err;
201
1.75k
    }
202
203
    /* Allocate buffer for new encoding */
204
205
2.03k
    new_der = OPENSSL_malloc(len);
206
2.03k
    if (new_der == NULL)
207
0
        goto err;
208
209
    /* Generate tagged encoding */
210
211
2.03k
    p = new_der;
212
213
    /* Output explicit tags first */
214
215
3.79k
    for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
216
2.03k
        i++, etmp++) {
217
1.75k
        ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
218
1.75k
            etmp->exp_tag, etmp->exp_class);
219
1.75k
        if (etmp->exp_pad)
220
163
            *p++ = 0;
221
1.75k
    }
222
223
    /* If IMPLICIT, output tag */
224
225
2.03k
    if (asn1_tags.imp_tag != -1) {
226
1.06k
        if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
227
944
            && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
228
934
                || asn1_tags.imp_tag == V_ASN1_SET))
229
28
            hdr_constructed = V_ASN1_CONSTRUCTED;
230
1.06k
        ASN1_put_object(&p, hdr_constructed, hdr_len,
231
1.06k
            asn1_tags.imp_tag, asn1_tags.imp_class);
232
1.06k
    }
233
234
    /* Copy across original encoding */
235
2.03k
    memcpy(p, cpy_start, cpy_len);
236
237
2.03k
    cp = new_der;
238
239
    /* Obtain new ASN1_TYPE structure */
240
2.03k
    ret = d2i_ASN1_TYPE(NULL, &cp, len);
241
242
2.03k
err:
243
2.03k
    OPENSSL_free(orig_der);
244
2.03k
    OPENSSL_free(new_der);
245
246
2.03k
    return ret;
247
2.03k
}
248
249
static int asn1_cb(const char *elem, int len, void *bitstr)
250
14.1k
{
251
14.1k
    tag_exp_arg *arg = bitstr;
252
14.1k
    int i;
253
14.1k
    int utype;
254
14.1k
    int vlen = 0;
255
14.1k
    const char *p, *vstart = NULL;
256
257
14.1k
    int tmp_tag, tmp_class;
258
259
14.1k
    if (elem == NULL)
260
56
        return -1;
261
262
248k
    for (i = 0, p = elem; i < len; p++, i++) {
263
        /* Look for the ':' in name value pairs */
264
245k
        if (*p == ':') {
265
11.2k
            vstart = p + 1;
266
11.2k
            vlen = len - (int)(vstart - elem);
267
11.2k
            len = (int)(p - elem);
268
11.2k
            break;
269
11.2k
        }
270
245k
    }
271
272
14.1k
    utype = asn1_str2tag(elem, len);
273
274
14.1k
    if (utype == -1) {
275
406
        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_UNKNOWN_TAG, "tag=%s", elem);
276
406
        return -1;
277
406
    }
278
279
    /* If this is not a modifier mark end of string and exit */
280
13.7k
    if (!(utype & ASN1_GEN_FLAG)) {
281
4.98k
        arg->utype = utype;
282
4.98k
        arg->str = vstart;
283
        /* If no value and not end of string, error */
284
4.98k
        if (!vstart && elem[len]) {
285
5
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_VALUE);
286
5
            return -1;
287
5
        }
288
4.97k
        return 0;
289
4.98k
    }
290
291
8.72k
    switch (utype) {
292
293
2.75k
    case ASN1_GEN_FLAG_IMP:
294
        /* Check for illegal multiple IMPLICIT tagging */
295
2.75k
        if (arg->imp_tag != -1) {
296
46
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING);
297
46
            return -1;
298
46
        }
299
2.70k
        if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
300
45
            return -1;
301
2.66k
        break;
302
303
2.66k
    case ASN1_GEN_FLAG_EXP:
304
305
1.83k
        if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
306
143
            return -1;
307
1.69k
        if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
308
13
            return -1;
309
1.67k
        break;
310
311
1.67k
    case ASN1_GEN_FLAG_SEQWRAP:
312
619
        if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
313
5
            return -1;
314
614
        break;
315
316
614
    case ASN1_GEN_FLAG_SETWRAP:
317
451
        if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
318
3
            return -1;
319
448
        break;
320
321
448
    case ASN1_GEN_FLAG_BITWRAP:
322
426
        if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
323
3
            return -1;
324
423
        break;
325
326
471
    case ASN1_GEN_FLAG_OCTWRAP:
327
471
        if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
328
3
            return -1;
329
468
        break;
330
331
2.16k
    case ASN1_GEN_FLAG_FORMAT:
332
2.16k
        if (!vstart) {
333
5
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
334
5
            return -1;
335
5
        }
336
2.16k
        if (HAS_PREFIX(vstart, "ASCII"))
337
204
            arg->format = ASN1_GEN_FORMAT_ASCII;
338
1.95k
        else if (HAS_PREFIX(vstart, "UTF8"))
339
601
            arg->format = ASN1_GEN_FORMAT_UTF8;
340
1.35k
        else if (HAS_PREFIX(vstart, "HEX"))
341
251
            arg->format = ASN1_GEN_FORMAT_HEX;
342
1.10k
        else if (HAS_PREFIX(vstart, "BITLIST"))
343
897
            arg->format = ASN1_GEN_FORMAT_BITLIST;
344
208
        else {
345
208
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
346
208
            return -1;
347
208
        }
348
1.95k
        break;
349
8.72k
    }
350
351
8.24k
    return 1;
352
8.72k
}
353
354
static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
355
4.54k
{
356
4.54k
    long tag_num;
357
4.54k
    char *eptr;
358
4.54k
    if (!vstart)
359
9
        return 0;
360
4.53k
    tag_num = strtoul(vstart, &eptr, 10);
361
    /* Check we haven't gone past max length: should be impossible */
362
4.53k
    if (eptr && *eptr && (eptr > vstart + vlen))
363
0
        return 0;
364
4.53k
    if (tag_num < 0) {
365
132
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_NUMBER);
366
132
        return 0;
367
132
    }
368
4.40k
    *ptag = tag_num;
369
    /* If we have non numeric characters, parse them */
370
4.40k
    if (eptr)
371
4.40k
        vlen -= (int)(eptr - vstart);
372
0
    else
373
0
        vlen = 0;
374
4.40k
    if (vlen) {
375
3.48k
        switch (*eptr) {
376
377
2.27k
        case 'U':
378
2.27k
            *pclass = V_ASN1_UNIVERSAL;
379
2.27k
            break;
380
381
564
        case 'A':
382
564
            *pclass = V_ASN1_APPLICATION;
383
564
            break;
384
385
352
        case 'P':
386
352
            *pclass = V_ASN1_PRIVATE;
387
352
            break;
388
389
242
        case 'C':
390
242
            *pclass = V_ASN1_CONTEXT_SPECIFIC;
391
242
            break;
392
393
47
        default:
394
47
            ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MODIFIER,
395
47
                "Char=%c", *eptr);
396
47
            return 0;
397
3.48k
        }
398
3.48k
    } else
399
918
        *pclass = V_ASN1_CONTEXT_SPECIFIC;
400
401
4.35k
    return 1;
402
4.40k
}
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
284
{
409
284
    ASN1_TYPE *ret = NULL;
410
284
    STACK_OF(ASN1_TYPE) *sk = NULL;
411
284
    STACK_OF(CONF_VALUE) *sect = NULL;
412
284
    unsigned char *der = NULL;
413
284
    int derlen;
414
284
    int i;
415
284
    sk = sk_ASN1_TYPE_new_null();
416
284
    if (!sk)
417
0
        goto bad;
418
284
    if (section) {
419
9
        if (!cnf)
420
0
            goto bad;
421
9
        sect = X509V3_get_section(cnf, (char *)section);
422
9
        if (!sect)
423
4
            goto bad;
424
5
        for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
425
3
            ASN1_TYPE *typ = generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf,
426
3
                depth + 1, perr);
427
3
            if (!typ)
428
3
                goto bad;
429
430
0
            if (!sk_ASN1_TYPE_push(sk, typ)) {
431
0
                ASN1_TYPE_free(typ);
432
0
                goto bad;
433
0
            }
434
0
        }
435
5
    }
436
437
    /*
438
     * Now we has a STACK of the components, convert to the correct form
439
     */
440
441
277
    if (utype == V_ASN1_SET)
442
107
        derlen = i2d_ASN1_SET_ANY(sk, &der);
443
170
    else
444
170
        derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
445
446
277
    if (derlen < 0)
447
0
        goto bad;
448
277
    if ((ret = ASN1_TYPE_new()) == NULL)
449
0
        goto bad;
450
277
    if ((ret->value.asn1_string = ASN1_STRING_type_new(utype)) == NULL)
451
0
        goto bad;
452
453
277
    ret->type = utype;
454
277
    ret->value.asn1_string->data = der;
455
277
    ret->value.asn1_string->length = derlen;
456
457
277
    der = NULL;
458
459
284
bad:
460
461
284
    OPENSSL_free(der);
462
463
284
    sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
464
284
    X509V3_section_free(cnf, sect);
465
466
284
    return ret;
467
277
}
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
3.65k
{
472
3.65k
    tag_exp_type *exp_tmp;
473
    /* Can only have IMPLICIT if permitted */
474
3.65k
    if ((arg->imp_tag != -1) && !imp_ok) {
475
10
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_IMPLICIT_TAG);
476
10
        return 0;
477
10
    }
478
479
3.64k
    if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
480
17
        ERR_raise(ERR_LIB_ASN1, ASN1_R_DEPTH_EXCEEDED);
481
17
        return 0;
482
17
    }
483
484
3.63k
    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
3.63k
    if (arg->imp_tag != -1) {
491
1.41k
        exp_tmp->exp_tag = arg->imp_tag;
492
1.41k
        exp_tmp->exp_class = arg->imp_class;
493
1.41k
        arg->imp_tag = -1;
494
1.41k
        arg->imp_class = -1;
495
2.21k
    } else {
496
2.21k
        exp_tmp->exp_tag = exp_tag;
497
2.21k
        exp_tmp->exp_class = exp_class;
498
2.21k
    }
499
3.63k
    exp_tmp->exp_constructed = exp_constructed;
500
3.63k
    exp_tmp->exp_pad = exp_pad;
501
502
3.63k
    return 1;
503
3.64k
}
504
505
static int asn1_str2tag(const char *tagstr, int len)
506
14.1k
{
507
14.1k
    unsigned int i;
508
14.1k
    const struct tag_name_st *tntmp;
509
14.1k
    static const struct tag_name_st tnst[] = {
510
14.1k
        ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
511
14.1k
        ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
512
14.1k
        ASN1_GEN_STR("NULL", V_ASN1_NULL),
513
14.1k
        ASN1_GEN_STR("INT", V_ASN1_INTEGER),
514
14.1k
        ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
515
14.1k
        ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
516
14.1k
        ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
517
14.1k
        ASN1_GEN_STR("OID", V_ASN1_OBJECT),
518
14.1k
        ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
519
14.1k
        ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
520
14.1k
        ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
521
14.1k
        ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
522
14.1k
        ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
523
14.1k
        ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
524
14.1k
        ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
525
14.1k
        ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
526
14.1k
        ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
527
14.1k
        ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
528
14.1k
        ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
529
14.1k
        ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
530
14.1k
        ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
531
14.1k
        ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
532
14.1k
        ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
533
14.1k
        ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
534
14.1k
        ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
535
14.1k
        ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
536
14.1k
        ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
537
14.1k
        ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
538
14.1k
        ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
539
14.1k
        ASN1_GEN_STR("T61", V_ASN1_T61STRING),
540
14.1k
        ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
541
14.1k
        ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
542
14.1k
        ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
543
14.1k
        ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
544
14.1k
        ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
545
14.1k
        ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
546
547
        /* Special cases */
548
14.1k
        ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
549
14.1k
        ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
550
14.1k
        ASN1_GEN_STR("SET", V_ASN1_SET),
551
        /* type modifiers */
552
        /* Explicit tag */
553
14.1k
        ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
554
14.1k
        ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
555
        /* Implicit tag */
556
14.1k
        ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
557
14.1k
        ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
558
        /* OCTET STRING wrapper */
559
14.1k
        ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
560
        /* SEQUENCE wrapper */
561
14.1k
        ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
562
        /* SET wrapper */
563
14.1k
        ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
564
        /* BIT STRING wrapper */
565
14.1k
        ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
566
14.1k
        ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
567
14.1k
        ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
568
14.1k
    };
569
570
14.1k
    if (len == -1)
571
0
        len = (int)strlen(tagstr);
572
573
14.1k
    tntmp = tnst;
574
497k
    for (i = 0; i < OSSL_NELEM(tnst); i++, tntmp++) {
575
496k
        if ((len == tntmp->len)
576
93.0k
            && (OPENSSL_strncasecmp(tntmp->strnam, tagstr, len) == 0))
577
13.7k
            return tntmp->tag;
578
496k
    }
579
580
406
    return -1;
581
14.1k
}
582
583
static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
584
4.69k
{
585
4.69k
    ASN1_TYPE *atmp = NULL;
586
4.69k
    CONF_VALUE vtmp;
587
4.69k
    unsigned char *rdata;
588
4.69k
    long rdlen;
589
4.69k
    int no_unused = 1;
590
591
4.69k
    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
4.69k
    if (!str)
597
562
        str = "";
598
599
4.69k
    switch (utype) {
600
601
316
    case V_ASN1_NULL:
602
316
        if (str && *str) {
603
5
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL_VALUE);
604
5
            goto bad_form;
605
5
        }
606
311
        break;
607
608
311
    case V_ASN1_BOOLEAN:
609
184
        if (format != ASN1_GEN_FORMAT_ASCII) {
610
3
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ASCII_FORMAT);
611
3
            goto bad_form;
612
3
        }
613
181
        vtmp.name = NULL;
614
181
        vtmp.section = NULL;
615
181
        vtmp.value = (char *)str;
616
181
        if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
617
76
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BOOLEAN);
618
76
            goto bad_str;
619
76
        }
620
105
        break;
621
622
435
    case V_ASN1_INTEGER:
623
439
    case V_ASN1_ENUMERATED:
624
439
        if (format != ASN1_GEN_FORMAT_ASCII) {
625
3
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
626
3
            goto bad_form;
627
3
        }
628
436
        if ((atmp->value.integer
629
436
                = s2i_ASN1_INTEGER(NULL, str))
630
436
            == NULL) {
631
121
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_INTEGER);
632
121
            goto bad_str;
633
121
        }
634
315
        break;
635
636
315
    case V_ASN1_OBJECT:
637
97
        if (format != ASN1_GEN_FORMAT_ASCII) {
638
3
            ERR_raise(ERR_LIB_ASN1, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
639
3
            goto bad_form;
640
3
        }
641
94
        if ((atmp->value.object = OBJ_txt2obj(str, 0)) == NULL) {
642
17
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OBJECT);
643
17
            goto bad_str;
644
17
        }
645
77
        break;
646
647
358
    case V_ASN1_UTCTIME:
648
488
    case V_ASN1_GENERALIZEDTIME:
649
488
        if (format != ASN1_GEN_FORMAT_ASCII) {
650
3
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TIME_NOT_ASCII_FORMAT);
651
3
            goto bad_form;
652
3
        }
653
485
        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
485
        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
485
        atmp->value.asn1_string->type = utype;
662
485
        if (!ASN1_TIME_check(atmp->value.asn1_string)) {
663
307
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
664
307
            goto bad_str;
665
307
        }
666
667
178
        break;
668
669
295
    case V_ASN1_BMPSTRING:
670
381
    case V_ASN1_PRINTABLESTRING:
671
432
    case V_ASN1_IA5STRING:
672
1.18k
    case V_ASN1_T61STRING:
673
2.11k
    case V_ASN1_UTF8STRING:
674
2.11k
    case V_ASN1_VISIBLESTRING:
675
2.44k
    case V_ASN1_UNIVERSALSTRING:
676
2.45k
    case V_ASN1_GENERALSTRING:
677
2.45k
    case V_ASN1_NUMERICSTRING:
678
2.45k
        if (format == ASN1_GEN_FORMAT_ASCII)
679
2.14k
            format = MBSTRING_ASC;
680
311
        else if (format == ASN1_GEN_FORMAT_UTF8)
681
308
            format = MBSTRING_UTF8;
682
3
        else {
683
3
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_FORMAT);
684
3
            goto bad_form;
685
3
        }
686
687
2.45k
        if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
688
2.45k
                -1, format, ASN1_tag2bit(utype))
689
2.45k
            <= 0) {
690
207
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
691
207
            goto bad_str;
692
207
        }
693
694
2.24k
        break;
695
696
2.24k
    case V_ASN1_BIT_STRING:
697
717
    case V_ASN1_OCTET_STRING:
698
717
        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
717
        if (format == ASN1_GEN_FORMAT_HEX) {
704
8
            if ((rdata = OPENSSL_hexstr2buf(str, &rdlen)) == NULL) {
705
4
                ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_HEX);
706
4
                goto bad_str;
707
4
            }
708
4
            atmp->value.asn1_string->data = rdata;
709
4
            atmp->value.asn1_string->length = rdlen;
710
4
            atmp->value.asn1_string->type = utype;
711
709
        } else if (format == ASN1_GEN_FORMAT_ASCII) {
712
335
            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
374
        } else if ((format == ASN1_GEN_FORMAT_BITLIST)
717
371
            && (utype == V_ASN1_BIT_STRING)) {
718
368
            if (!CONF_parse_list(str, ',', 1, bitstr_cb, atmp->value.bit_string)) {
719
240
                ERR_raise(ERR_LIB_ASN1, ASN1_R_LIST_ERROR);
720
240
                goto bad_str;
721
240
            }
722
128
            no_unused = 0;
723
724
128
        } else {
725
6
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
726
6
            goto bad_form;
727
6
        }
728
729
467
        if ((utype == V_ASN1_BIT_STRING) && no_unused)
730
279
            ossl_asn1_string_set_bits_left(atmp->value.asn1_string, 0);
731
732
467
        break;
733
734
0
    default:
735
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
736
0
        goto bad_str;
737
4.69k
    }
738
739
3.69k
    atmp->type = utype;
740
3.69k
    return atmp;
741
742
972
bad_str:
743
972
    ERR_add_error_data(2, "string=", str);
744
998
bad_form:
745
746
998
    ASN1_TYPE_free(atmp);
747
998
    return NULL;
748
972
}
749
750
static int bitstr_cb(const char *elem, int len, void *bitstr)
751
1.97k
{
752
1.97k
    long bitnum;
753
1.97k
    char *eptr;
754
1.97k
    if (!elem)
755
24
        return 0;
756
1.95k
    bitnum = strtoul(elem, &eptr, 10);
757
1.95k
    if (eptr && *eptr && (eptr != elem + len))
758
48
        return 0;
759
1.90k
    if (bitnum < 0) {
760
114
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_NUMBER);
761
114
        return 0;
762
114
    }
763
1.78k
    if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
764
54
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
765
54
        return 0;
766
54
    }
767
1.73k
    return 1;
768
1.78k
}
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
}