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