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