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