Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / openssl / crypto / asn1 / asn1_gen.c
blob4fc241908f6923b4f99e5e4e8d2ed0c1ba4a17bf
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 * ====================================================================
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).
59 #include "cryptlib.h"
60 #include <openssl/asn1.h>
61 #include <openssl/x509v3.h>
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)
73 #define ASN1_GEN_STR(str,val) {str, sizeof(str) - 1, val}
75 #define ASN1_FLAG_EXP_MAX 20
77 /* Input formats */
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
89 struct tag_name_st
91 const char *strnam;
92 int len;
93 int tag;
96 typedef struct
98 int exp_tag;
99 int exp_class;
100 int exp_constructed;
101 int exp_pad;
102 long exp_len;
103 } tag_exp_type;
105 typedef struct
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;
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);
124 ASN1_TYPE *ASN1_generate_nconf(char *str, CONF *nconf)
126 X509V3_CTX cnf;
128 if (!nconf)
129 return ASN1_generate_v3(str, NULL);
131 X509V3_set_nconf(&cnf, nconf);
132 return ASN1_generate_v3(str, &cnf);
135 ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf)
137 ASN1_TYPE *ret;
138 tag_exp_arg asn1_tags;
139 tag_exp_type *etmp;
141 int i, len;
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;
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;
159 if ((asn1_tags.utype == V_ASN1_SEQUENCE) || (asn1_tags.utype == V_ASN1_SET))
161 if (!cnf)
163 ASN1err(ASN1_F_ASN1_GENERATE_V3, ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG);
164 return NULL;
166 ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf);
168 else
169 ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
171 if (!ret)
172 return NULL;
174 /* If no tagging return base type */
175 if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
176 return ret;
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;
185 /* Do we need IMPLICIT tagging? */
186 if (asn1_tags.imp_tag != -1)
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.
199 if (r & 0x1)
201 /* Indefinite length constructed */
202 hdr_constructed = 2;
203 hdr_len = 0;
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
211 len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
213 else
214 len = cpy_len;
216 /* Work out length in any EXPLICIT, starting from end */
218 for(i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1; i < asn1_tags.exp_count; i++, etmp--)
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);
227 /* Allocate buffer for new encoding */
229 new_der = OPENSSL_malloc(len);
230 if (!new_der)
231 goto err;
233 /* Generate tagged encoding */
235 p = new_der;
237 /* Output explicit tags first */
239 for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count; i++, etmp++)
241 ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
242 etmp->exp_tag, etmp->exp_class);
243 if (etmp->exp_pad)
244 *p++ = 0;
247 /* If IMPLICIT, output tag */
249 if (asn1_tags.imp_tag != -1)
251 if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
252 && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
253 || asn1_tags.imp_tag == V_ASN1_SET) )
254 hdr_constructed = V_ASN1_CONSTRUCTED;
255 ASN1_put_object(&p, hdr_constructed, hdr_len,
256 asn1_tags.imp_tag, asn1_tags.imp_class);
259 /* Copy across original encoding */
260 memcpy(p, cpy_start, cpy_len);
262 cp = new_der;
264 /* Obtain new ASN1_TYPE structure */
265 ret = d2i_ASN1_TYPE(NULL, &cp, len);
267 err:
268 if (orig_der)
269 OPENSSL_free(orig_der);
270 if (new_der)
271 OPENSSL_free(new_der);
273 return ret;
277 static int asn1_cb(const char *elem, int len, void *bitstr)
279 tag_exp_arg *arg = bitstr;
280 int i;
281 int utype;
282 int vlen = 0;
283 const char *p, *vstart = NULL;
285 int tmp_tag, tmp_class;
287 for(i = 0, p = elem; i < len; p++, i++)
289 /* Look for the ':' in name value pairs */
290 if (*p == ':')
292 vstart = p + 1;
293 vlen = len - (vstart - elem);
294 len = p - elem;
295 break;
299 utype = asn1_str2tag(elem, len);
301 if (utype == -1)
303 ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_TAG);
304 ERR_add_error_data(2, "tag=", elem);
305 return -1;
308 /* If this is not a modifier mark end of string and exit */
309 if (!(utype & ASN1_GEN_FLAG))
311 arg->utype = utype;
312 arg->str = vstart;
313 /* If no value and not end of string, error */
314 if (!vstart && elem[len])
316 ASN1err(ASN1_F_ASN1_CB, ASN1_R_MISSING_VALUE);
317 return -1;
319 return 0;
322 switch(utype)
325 case ASN1_GEN_FLAG_IMP:
326 /* Check for illegal multiple IMPLICIT tagging */
327 if (arg->imp_tag != -1)
329 ASN1err(ASN1_F_ASN1_CB, ASN1_R_ILLEGAL_NESTED_TAGGING);
330 return -1;
332 if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
333 return -1;
334 break;
336 case ASN1_GEN_FLAG_EXP:
338 if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
339 return -1;
340 if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
341 return -1;
342 break;
344 case ASN1_GEN_FLAG_SEQWRAP:
345 if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
346 return -1;
347 break;
349 case ASN1_GEN_FLAG_SETWRAP:
350 if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
351 return -1;
352 break;
354 case ASN1_GEN_FLAG_BITWRAP:
355 if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
356 return -1;
357 break;
359 case ASN1_GEN_FLAG_OCTWRAP:
360 if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
361 return -1;
362 break;
364 case ASN1_GEN_FLAG_FORMAT:
365 if (!strncmp(vstart, "ASCII", 5))
366 arg->format = ASN1_GEN_FORMAT_ASCII;
367 else if (!strncmp(vstart, "UTF8", 4))
368 arg->format = ASN1_GEN_FORMAT_UTF8;
369 else if (!strncmp(vstart, "HEX", 3))
370 arg->format = ASN1_GEN_FORMAT_HEX;
371 else if (!strncmp(vstart, "BITLIST", 3))
372 arg->format = ASN1_GEN_FORMAT_BITLIST;
373 else
375 ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKOWN_FORMAT);
376 return -1;
378 break;
382 return 1;
386 static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
388 char erch[2];
389 long tag_num;
390 char *eptr;
391 if (!vstart)
392 return 0;
393 tag_num = strtoul(vstart, &eptr, 10);
394 /* Check we haven't gone past max length: should be impossible */
395 if (eptr && *eptr && (eptr > vstart + vlen))
396 return 0;
397 if (tag_num < 0)
399 ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_NUMBER);
400 return 0;
402 *ptag = tag_num;
403 /* If we have non numeric characters, parse them */
404 if (eptr)
405 vlen -= eptr - vstart;
406 else
407 vlen = 0;
408 if (vlen)
410 switch (*eptr)
413 case 'U':
414 *pclass = V_ASN1_UNIVERSAL;
415 break;
417 case 'A':
418 *pclass = V_ASN1_APPLICATION;
419 break;
421 case 'P':
422 *pclass = V_ASN1_PRIVATE;
423 break;
425 case 'C':
426 *pclass = V_ASN1_CONTEXT_SPECIFIC;
427 break;
429 default:
430 erch[0] = *eptr;
431 erch[1] = 0;
432 ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_MODIFIER);
433 ERR_add_error_data(2, "Char=", erch);
434 return 0;
435 break;
439 else
440 *pclass = V_ASN1_CONTEXT_SPECIFIC;
442 return 1;
446 /* Handle multiple types: SET and SEQUENCE */
448 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
450 ASN1_TYPE *ret = NULL;
451 STACK_OF(ASN1_TYPE) *sk = NULL;
452 STACK_OF(CONF_VALUE) *sect = NULL;
453 unsigned char *der = NULL;
454 int derlen;
455 int i;
456 sk = sk_ASN1_TYPE_new_null();
457 if (!sk)
458 goto bad;
459 if (section)
461 if (!cnf)
462 goto bad;
463 sect = X509V3_get_section(cnf, (char *)section);
464 if (!sect)
465 goto bad;
466 for (i = 0; i < sk_CONF_VALUE_num(sect); i++)
468 ASN1_TYPE *typ = ASN1_generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf);
469 if (!typ)
470 goto bad;
471 if (!sk_ASN1_TYPE_push(sk, typ))
472 goto bad;
476 /* Now we has a STACK of the components, convert to the correct form */
478 if (utype == V_ASN1_SET)
479 derlen = i2d_ASN1_SET_ANY(sk, &der);
480 else
481 derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
483 if (derlen < 0)
484 goto bad;
486 if (!(ret = ASN1_TYPE_new()))
487 goto bad;
489 if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
490 goto bad;
492 ret->type = utype;
494 ret->value.asn1_string->data = der;
495 ret->value.asn1_string->length = derlen;
497 der = NULL;
499 bad:
501 if (der)
502 OPENSSL_free(der);
504 if (sk)
505 sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
506 if (sect)
507 X509V3_section_free(cnf, sect);
509 return ret;
512 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class, int exp_constructed, int exp_pad, int imp_ok)
514 tag_exp_type *exp_tmp;
515 /* Can only have IMPLICIT if permitted */
516 if ((arg->imp_tag != -1) && !imp_ok)
518 ASN1err(ASN1_F_APPEND_EXP, ASN1_R_ILLEGAL_IMPLICIT_TAG);
519 return 0;
522 if (arg->exp_count == ASN1_FLAG_EXP_MAX)
524 ASN1err(ASN1_F_APPEND_EXP, ASN1_R_DEPTH_EXCEEDED);
525 return 0;
528 exp_tmp = &arg->exp_list[arg->exp_count++];
530 /* If IMPLICIT set tag to implicit value then
531 * reset implicit tag since it has been used.
533 if (arg->imp_tag != -1)
535 exp_tmp->exp_tag = arg->imp_tag;
536 exp_tmp->exp_class = arg->imp_class;
537 arg->imp_tag = -1;
538 arg->imp_class = -1;
540 else
542 exp_tmp->exp_tag = exp_tag;
543 exp_tmp->exp_class = exp_class;
545 exp_tmp->exp_constructed = exp_constructed;
546 exp_tmp->exp_pad = exp_pad;
548 return 1;
552 static int asn1_str2tag(const char *tagstr, int len)
554 unsigned int i;
555 static const struct tag_name_st *tntmp, tnst [] = {
556 ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
557 ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
558 ASN1_GEN_STR("NULL", V_ASN1_NULL),
559 ASN1_GEN_STR("INT", V_ASN1_INTEGER),
560 ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
561 ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
562 ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
563 ASN1_GEN_STR("OID", V_ASN1_OBJECT),
564 ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
565 ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
566 ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
567 ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
568 ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
569 ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
570 ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
571 ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
572 ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
573 ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
574 ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
575 ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
576 ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
577 ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
578 ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
579 ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
580 ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
581 ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
582 ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
583 ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
584 ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
585 ASN1_GEN_STR("T61", V_ASN1_T61STRING),
586 ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
587 ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
588 ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
589 ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
590 ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
591 ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
593 /* Special cases */
594 ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
595 ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
596 ASN1_GEN_STR("SET", V_ASN1_SET),
597 /* type modifiers */
598 /* Explicit tag */
599 ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
600 ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
601 /* Implicit tag */
602 ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
603 ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
604 /* OCTET STRING wrapper */
605 ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
606 /* SEQUENCE wrapper */
607 ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
608 /* SET wrapper */
609 ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
610 /* BIT STRING wrapper */
611 ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
612 ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
613 ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
616 if (len == -1)
617 len = strlen(tagstr);
619 tntmp = tnst;
620 for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st); i++, tntmp++)
622 if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
623 return tntmp->tag;
626 return -1;
629 static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
631 ASN1_TYPE *atmp = NULL;
633 CONF_VALUE vtmp;
635 unsigned char *rdata;
636 long rdlen;
638 int no_unused = 1;
640 if (!(atmp = ASN1_TYPE_new()))
642 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
643 return NULL;
646 if (!str)
647 str = "";
649 switch(utype)
652 case V_ASN1_NULL:
653 if (str && *str)
655 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_NULL_VALUE);
656 goto bad_form;
658 break;
660 case V_ASN1_BOOLEAN:
661 if (format != ASN1_GEN_FORMAT_ASCII)
663 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
664 goto bad_form;
666 vtmp.name = NULL;
667 vtmp.section = NULL;
668 vtmp.value = (char *)str;
669 if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean))
671 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BOOLEAN);
672 goto bad_str;
674 break;
676 case V_ASN1_INTEGER:
677 case V_ASN1_ENUMERATED:
678 if (format != ASN1_GEN_FORMAT_ASCII)
680 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
681 goto bad_form;
683 if (!(atmp->value.integer = s2i_ASN1_INTEGER(NULL, (char *)str)))
685 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_INTEGER);
686 goto bad_str;
688 break;
690 case V_ASN1_OBJECT:
691 if (format != ASN1_GEN_FORMAT_ASCII)
693 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
694 goto bad_form;
696 if (!(atmp->value.object = OBJ_txt2obj(str, 0)))
698 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_OBJECT);
699 goto bad_str;
701 break;
703 case V_ASN1_UTCTIME:
704 case V_ASN1_GENERALIZEDTIME:
705 if (format != ASN1_GEN_FORMAT_ASCII)
707 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_TIME_NOT_ASCII_FORMAT);
708 goto bad_form;
710 if (!(atmp->value.asn1_string = ASN1_STRING_new()))
712 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
713 goto bad_str;
715 if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1))
717 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
718 goto bad_str;
720 atmp->value.asn1_string->type = utype;
721 if (!ASN1_TIME_check(atmp->value.asn1_string))
723 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_TIME_VALUE);
724 goto bad_str;
727 break;
729 case V_ASN1_BMPSTRING:
730 case V_ASN1_PRINTABLESTRING:
731 case V_ASN1_IA5STRING:
732 case V_ASN1_T61STRING:
733 case V_ASN1_UTF8STRING:
734 case V_ASN1_VISIBLESTRING:
735 case V_ASN1_UNIVERSALSTRING:
736 case V_ASN1_GENERALSTRING:
737 case V_ASN1_NUMERICSTRING:
739 if (format == ASN1_GEN_FORMAT_ASCII)
740 format = MBSTRING_ASC;
741 else if (format == ASN1_GEN_FORMAT_UTF8)
742 format = MBSTRING_UTF8;
743 else
745 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_FORMAT);
746 goto bad_form;
750 if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
751 -1, format, ASN1_tag2bit(utype)) <= 0)
753 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
754 goto bad_str;
758 break;
760 case V_ASN1_BIT_STRING:
762 case V_ASN1_OCTET_STRING:
764 if (!(atmp->value.asn1_string = ASN1_STRING_new()))
766 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
767 goto bad_form;
770 if (format == ASN1_GEN_FORMAT_HEX)
773 if (!(rdata = string_to_hex((char *)str, &rdlen)))
775 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_HEX);
776 goto bad_str;
779 atmp->value.asn1_string->data = rdata;
780 atmp->value.asn1_string->length = rdlen;
781 atmp->value.asn1_string->type = utype;
784 else if (format == ASN1_GEN_FORMAT_ASCII)
785 ASN1_STRING_set(atmp->value.asn1_string, str, -1);
786 else if ((format == ASN1_GEN_FORMAT_BITLIST) && (utype == V_ASN1_BIT_STRING))
788 if (!CONF_parse_list(str, ',', 1, bitstr_cb, atmp->value.bit_string))
790 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_LIST_ERROR);
791 goto bad_str;
793 no_unused = 0;
796 else
798 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
799 goto bad_form;
802 if ((utype == V_ASN1_BIT_STRING) && no_unused)
804 atmp->value.asn1_string->flags
805 &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
806 atmp->value.asn1_string->flags
807 |= ASN1_STRING_FLAG_BITS_LEFT;
811 break;
813 default:
814 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_UNSUPPORTED_TYPE);
815 goto bad_str;
816 break;
820 atmp->type = utype;
821 return atmp;
824 bad_str:
825 ERR_add_error_data(2, "string=", str);
826 bad_form:
828 ASN1_TYPE_free(atmp);
829 return NULL;
833 static int bitstr_cb(const char *elem, int len, void *bitstr)
835 long bitnum;
836 char *eptr;
837 if (!elem)
838 return 0;
839 bitnum = strtoul(elem, &eptr, 10);
840 if (eptr && *eptr && (eptr != elem + len))
841 return 0;
842 if (bitnum < 0)
844 ASN1err(ASN1_F_BITSTR_CB, ASN1_R_INVALID_NUMBER);
845 return 0;
847 if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1))
849 ASN1err(ASN1_F_BITSTR_CB, ERR_R_MALLOC_FAILURE);
850 return 0;
852 return 1;