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