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