Coverage Report

Created: 2026-02-14 07:20

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