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