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