Coverage Report

Created: 2025-12-10 06:24

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