Coverage Report

Created: 2025-12-04 06:33

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-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 "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
    /* Set point to start copying for modified encoding */
152
0
    cpy_start = orig_der;
153
154
    /* Do we need IMPLICIT tagging? */
155
0
    if (asn1_tags.imp_tag != -1) {
156
        /* If IMPLICIT we will replace the underlying tag */
157
        /* Skip existing tag+len */
158
0
        r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class,
159
0
                            cpy_len);
160
0
        if (r & 0x80)
161
0
            goto err;
162
        /* Update copy length */
163
0
        cpy_len -= cpy_start - orig_der;
164
        /*
165
         * For IMPLICIT tagging the length should match the original length
166
         * and constructed flag should be consistent.
167
         */
168
0
        if (r & 0x1) {
169
            /* Indefinite length constructed */
170
0
            hdr_constructed = 2;
171
0
            hdr_len = 0;
172
0
        } else {
173
            /* Just retain constructed flag */
174
0
            hdr_constructed = r & V_ASN1_CONSTRUCTED;
175
0
        }
176
        /*
177
         * Work out new length with IMPLICIT tag: ignore constructed because
178
         * it will mess up if indefinite length
179
         */
180
0
        len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
181
0
        if (len == -1)
182
0
            goto err;
183
0
    } else {
184
0
        len = cpy_len;
185
0
    }
186
187
    /* Work out length in any EXPLICIT, starting from end */
188
189
0
    for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
190
0
         i < asn1_tags.exp_count; i++, etmp--) {
191
        /* Content length: number of content octets + any padding */
192
0
        len += etmp->exp_pad;
193
0
        etmp->exp_len = len;
194
        /* Total object length: length including new header */
195
0
        len = ASN1_object_size(0, len, etmp->exp_tag);
196
0
        if (len == -1)
197
0
            goto err;
198
0
    }
199
200
    /* Allocate buffer for new encoding */
201
202
0
    new_der = OPENSSL_malloc(len);
203
0
    if (new_der == NULL)
204
0
        goto err;
205
206
    /* Generate tagged encoding */
207
208
0
    p = new_der;
209
210
    /* Output explicit tags first */
211
212
0
    for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
213
0
         i++, etmp++) {
214
0
        ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
215
0
                        etmp->exp_tag, etmp->exp_class);
216
0
        if (etmp->exp_pad)
217
0
            *p++ = 0;
218
0
    }
219
220
    /* If IMPLICIT, output tag */
221
222
0
    if (asn1_tags.imp_tag != -1) {
223
0
        if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
224
0
            && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
225
0
                || asn1_tags.imp_tag == V_ASN1_SET))
226
0
            hdr_constructed = V_ASN1_CONSTRUCTED;
227
0
        ASN1_put_object(&p, hdr_constructed, hdr_len,
228
0
                        asn1_tags.imp_tag, asn1_tags.imp_class);
229
0
    }
230
231
    /* Copy across original encoding */
232
0
    memcpy(p, cpy_start, cpy_len);
233
234
0
    cp = new_der;
235
236
    /* Obtain new ASN1_TYPE structure */
237
0
    ret = d2i_ASN1_TYPE(NULL, &cp, len);
238
239
0
 err:
240
0
    OPENSSL_free(orig_der);
241
0
    OPENSSL_free(new_der);
242
243
0
    return ret;
244
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 - (vstart - elem);
265
0
            len = 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 (strncmp(vstart, "ASCII", 5) == 0)
335
0
            arg->format = ASN1_GEN_FORMAT_ASCII;
336
0
        else if (strncmp(vstart, "UTF8", 4) == 0)
337
0
            arg->format = ASN1_GEN_FORMAT_UTF8;
338
0
        else if (strncmp(vstart, "HEX", 3) == 0)
339
0
            arg->format = ASN1_GEN_FORMAT_HEX;
340
0
        else if (strncmp(vstart, "BITLIST", 7) == 0)
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
348
0
    }
349
350
0
    return 1;
351
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 -= 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
398
0
        }
399
0
    } else
400
0
        *pclass = V_ASN1_CONTEXT_SPECIFIC;
401
402
0
    return 1;
403
404
0
}
405
406
/* Handle multiple types: SET and SEQUENCE */
407
408
static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
409
                             int depth, int *perr)
410
0
{
411
0
    ASN1_TYPE *ret = NULL;
412
0
    STACK_OF(ASN1_TYPE) *sk = NULL;
413
0
    STACK_OF(CONF_VALUE) *sect = NULL;
414
0
    unsigned char *der = NULL;
415
0
    int derlen;
416
0
    int i;
417
0
    sk = sk_ASN1_TYPE_new_null();
418
0
    if (!sk)
419
0
        goto bad;
420
0
    if (section) {
421
0
        if (!cnf)
422
0
            goto bad;
423
0
        sect = X509V3_get_section(cnf, (char *)section);
424
0
        if (!sect)
425
0
            goto bad;
426
0
        for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
427
0
            ASN1_TYPE *typ =
428
0
                generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf,
429
0
                            depth + 1, perr);
430
0
            if (!typ)
431
0
                goto bad;
432
0
            if (!sk_ASN1_TYPE_push(sk, typ))
433
0
                goto bad;
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
        goto bad;
452
453
0
    ret->type = utype;
454
0
    ret->value.asn1_string->data = der;
455
0
    ret->value.asn1_string->length = derlen;
456
457
0
    der = NULL;
458
459
0
 bad:
460
461
0
    OPENSSL_free(der);
462
463
0
    sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
464
0
    X509V3_section_free(cnf, sect);
465
466
0
    return ret;
467
0
}
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
0
{
472
0
    tag_exp_type *exp_tmp;
473
    /* Can only have IMPLICIT if permitted */
474
0
    if ((arg->imp_tag != -1) && !imp_ok) {
475
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_IMPLICIT_TAG);
476
0
        return 0;
477
0
    }
478
479
0
    if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
480
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_DEPTH_EXCEEDED);
481
0
        return 0;
482
0
    }
483
484
0
    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
0
    if (arg->imp_tag != -1) {
491
0
        exp_tmp->exp_tag = arg->imp_tag;
492
0
        exp_tmp->exp_class = arg->imp_class;
493
0
        arg->imp_tag = -1;
494
0
        arg->imp_class = -1;
495
0
    } else {
496
0
        exp_tmp->exp_tag = exp_tag;
497
0
        exp_tmp->exp_class = exp_class;
498
0
    }
499
0
    exp_tmp->exp_constructed = exp_constructed;
500
0
    exp_tmp->exp_pad = exp_pad;
501
502
0
    return 1;
503
0
}
504
505
static int asn1_str2tag(const char *tagstr, int len)
506
0
{
507
0
    unsigned int i;
508
0
    const struct tag_name_st *tntmp;
509
0
    static const struct tag_name_st tnst[] = {
510
0
        ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
511
0
        ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
512
0
        ASN1_GEN_STR("NULL", V_ASN1_NULL),
513
0
        ASN1_GEN_STR("INT", V_ASN1_INTEGER),
514
0
        ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
515
0
        ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
516
0
        ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
517
0
        ASN1_GEN_STR("OID", V_ASN1_OBJECT),
518
0
        ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
519
0
        ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
520
0
        ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
521
0
        ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
522
0
        ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
523
0
        ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
524
0
        ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
525
0
        ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
526
0
        ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
527
0
        ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
528
0
        ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
529
0
        ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
530
0
        ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
531
0
        ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
532
0
        ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
533
0
        ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
534
0
        ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
535
0
        ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
536
0
        ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
537
0
        ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
538
0
        ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
539
0
        ASN1_GEN_STR("T61", V_ASN1_T61STRING),
540
0
        ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
541
0
        ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
542
0
        ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
543
0
        ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
544
0
        ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
545
0
        ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
546
547
        /* Special cases */
548
0
        ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
549
0
        ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
550
0
        ASN1_GEN_STR("SET", V_ASN1_SET),
551
        /* type modifiers */
552
        /* Explicit tag */
553
0
        ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
554
0
        ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
555
        /* Implicit tag */
556
0
        ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
557
0
        ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
558
        /* OCTET STRING wrapper */
559
0
        ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
560
        /* SEQUENCE wrapper */
561
0
        ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
562
        /* SET wrapper */
563
0
        ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
564
        /* BIT STRING wrapper */
565
0
        ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
566
0
        ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
567
0
        ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
568
0
    };
569
570
0
    if (len == -1)
571
0
        len = strlen(tagstr);
572
573
0
    tntmp = tnst;
574
0
    for (i = 0; i < OSSL_NELEM(tnst); i++, tntmp++) {
575
0
        if ((len == tntmp->len)
576
0
            && (OPENSSL_strncasecmp(tntmp->strnam, tagstr, len) == 0))
577
0
            return tntmp->tag;
578
0
    }
579
580
0
    return -1;
581
0
}
582
583
static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
584
0
{
585
0
    ASN1_TYPE *atmp = NULL;
586
0
    CONF_VALUE vtmp;
587
0
    unsigned char *rdata;
588
0
    long rdlen;
589
0
    int no_unused = 1;
590
591
0
    if ((atmp = ASN1_TYPE_new()) == NULL) {
592
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
593
0
        return NULL;
594
0
    }
595
596
0
    if (!str)
597
0
        str = "";
598
599
0
    switch (utype) {
600
601
0
    case V_ASN1_NULL:
602
0
        if (str && *str) {
603
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL_VALUE);
604
0
            goto bad_form;
605
0
        }
606
0
        break;
607
608
0
    case V_ASN1_BOOLEAN:
609
0
        if (format != ASN1_GEN_FORMAT_ASCII) {
610
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ASCII_FORMAT);
611
0
            goto bad_form;
612
0
        }
613
0
        vtmp.name = NULL;
614
0
        vtmp.section = NULL;
615
0
        vtmp.value = (char *)str;
616
0
        if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
617
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BOOLEAN);
618
0
            goto bad_str;
619
0
        }
620
0
        break;
621
622
0
    case V_ASN1_INTEGER:
623
0
    case V_ASN1_ENUMERATED:
624
0
        if (format != ASN1_GEN_FORMAT_ASCII) {
625
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
626
0
            goto bad_form;
627
0
        }
628
0
        if ((atmp->value.integer
629
0
                    = s2i_ASN1_INTEGER(NULL, str)) == NULL) {
630
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_INTEGER);
631
0
            goto bad_str;
632
0
        }
633
0
        break;
634
635
0
    case V_ASN1_OBJECT:
636
0
        if (format != ASN1_GEN_FORMAT_ASCII) {
637
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
638
0
            goto bad_form;
639
0
        }
640
0
        if ((atmp->value.object = OBJ_txt2obj(str, 0)) == NULL) {
641
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OBJECT);
642
0
            goto bad_str;
643
0
        }
644
0
        break;
645
646
0
    case V_ASN1_UTCTIME:
647
0
    case V_ASN1_GENERALIZEDTIME:
648
0
        if (format != ASN1_GEN_FORMAT_ASCII) {
649
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TIME_NOT_ASCII_FORMAT);
650
0
            goto bad_form;
651
0
        }
652
0
        if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
653
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
654
0
            goto bad_str;
655
0
        }
656
0
        if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
657
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
658
0
            goto bad_str;
659
0
        }
660
0
        atmp->value.asn1_string->type = utype;
661
0
        if (!ASN1_TIME_check(atmp->value.asn1_string)) {
662
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
663
0
            goto bad_str;
664
0
        }
665
666
0
        break;
667
668
0
    case V_ASN1_BMPSTRING:
669
0
    case V_ASN1_PRINTABLESTRING:
670
0
    case V_ASN1_IA5STRING:
671
0
    case V_ASN1_T61STRING:
672
0
    case V_ASN1_UTF8STRING:
673
0
    case V_ASN1_VISIBLESTRING:
674
0
    case V_ASN1_UNIVERSALSTRING:
675
0
    case V_ASN1_GENERALSTRING:
676
0
    case V_ASN1_NUMERICSTRING:
677
0
        if (format == ASN1_GEN_FORMAT_ASCII)
678
0
            format = MBSTRING_ASC;
679
0
        else if (format == ASN1_GEN_FORMAT_UTF8)
680
0
            format = MBSTRING_UTF8;
681
0
        else {
682
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_FORMAT);
683
0
            goto bad_form;
684
0
        }
685
686
0
        if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
687
0
                               -1, format, ASN1_tag2bit(utype)) <= 0) {
688
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
689
0
            goto bad_str;
690
0
        }
691
692
0
        break;
693
694
0
    case V_ASN1_BIT_STRING:
695
0
    case V_ASN1_OCTET_STRING:
696
0
        if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
697
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
698
0
            goto bad_form;
699
0
        }
700
701
0
        if (format == ASN1_GEN_FORMAT_HEX) {
702
0
            if ((rdata = OPENSSL_hexstr2buf(str, &rdlen)) == NULL) {
703
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_HEX);
704
0
                goto bad_str;
705
0
            }
706
0
            atmp->value.asn1_string->data = rdata;
707
0
            atmp->value.asn1_string->length = rdlen;
708
0
            atmp->value.asn1_string->type = utype;
709
0
        } else if (format == ASN1_GEN_FORMAT_ASCII) {
710
0
            if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
711
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
712
0
                goto bad_str;
713
0
            }
714
0
        } else if ((format == ASN1_GEN_FORMAT_BITLIST)
715
0
                 && (utype == V_ASN1_BIT_STRING)) {
716
0
            if (!CONF_parse_list
717
0
                (str, ',', 1, bitstr_cb, atmp->value.bit_string)) {
718
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_LIST_ERROR);
719
0
                goto bad_str;
720
0
            }
721
0
            no_unused = 0;
722
723
0
        } else {
724
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
725
0
            goto bad_form;
726
0
        }
727
728
0
        if ((utype == V_ASN1_BIT_STRING) && no_unused) {
729
0
            atmp->value.asn1_string->flags
730
0
                &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
731
0
            atmp->value.asn1_string->flags |= ASN1_STRING_FLAG_BITS_LEFT;
732
0
        }
733
734
0
        break;
735
736
0
    default:
737
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
738
0
        goto bad_str;
739
0
    }
740
741
0
    atmp->type = utype;
742
0
    return atmp;
743
744
0
 bad_str:
745
0
    ERR_add_error_data(2, "string=", str);
746
0
 bad_form:
747
748
0
    ASN1_TYPE_free(atmp);
749
0
    return NULL;
750
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_MALLOC_FAILURE);
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) && (strncmp(elem, "DIR", 3) == 0)) {
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
}