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