Coverage Report

Created: 2026-09-13 06:13

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