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