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