3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2002 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/asn1.h>
62 #include <openssl/x509v3.h>
64 #define ASN1_GEN_FLAG 0x10000
65 #define ASN1_GEN_FLAG_IMP (ASN1_GEN_FLAG|1)
66 #define ASN1_GEN_FLAG_EXP (ASN1_GEN_FLAG|2)
67 #define ASN1_GEN_FLAG_TAG (ASN1_GEN_FLAG|3)
68 #define ASN1_GEN_FLAG_BITWRAP (ASN1_GEN_FLAG|4)
69 #define ASN1_GEN_FLAG_OCTWRAP (ASN1_GEN_FLAG|5)
70 #define ASN1_GEN_FLAG_SEQWRAP (ASN1_GEN_FLAG|6)
71 #define ASN1_GEN_FLAG_SETWRAP (ASN1_GEN_FLAG|7)
72 #define ASN1_GEN_FLAG_FORMAT (ASN1_GEN_FLAG|8)
74 #define ASN1_GEN_STR(str,val) {str, sizeof(str) - 1, val}
76 #define ASN1_FLAG_EXP_MAX 20
77 /* Maximum number of nested sequences */
78 #define ASN1_GEN_SEQ_MAX_DEPTH 50
83 #define ASN1_GEN_FORMAT_ASCII 1
85 #define ASN1_GEN_FORMAT_UTF8 2
87 #define ASN1_GEN_FORMAT_HEX 3
89 #define ASN1_GEN_FORMAT_BITLIST 4
111 tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
115 static ASN1_TYPE *generate_v3(char *str, X509V3_CTX *cnf, int depth,
117 static int bitstr_cb(const char *elem, int len, void *bitstr);
118 static int asn1_cb(const char *elem, int len, void *bitstr);
119 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
120 int exp_constructed, int exp_pad, int imp_ok);
121 static int parse_tagging(const char *vstart, int vlen, int *ptag,
123 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
124 int depth, int *perr);
125 static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype);
126 static int asn1_str2tag(const char *tagstr, int len);
128 ASN1_TYPE *ASN1_generate_nconf(char *str, CONF *nconf)
133 return ASN1_generate_v3(str, NULL);
135 X509V3_set_nconf(&cnf, nconf);
136 return ASN1_generate_v3(str, &cnf);
139 ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf)
142 ASN1_TYPE *ret = generate_v3(str, cnf, 0, &err);
144 ASN1err(ASN1_F_ASN1_GENERATE_V3, err);
148 static ASN1_TYPE *generate_v3(char *str, X509V3_CTX *cnf, int depth,
152 tag_exp_arg asn1_tags;
157 unsigned char *orig_der = NULL, *new_der = NULL;
158 const unsigned char *cpy_start;
160 const unsigned char *cp;
163 int hdr_constructed = 0, hdr_tag, hdr_class;
166 asn1_tags.imp_tag = -1;
167 asn1_tags.imp_class = -1;
168 asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
169 asn1_tags.exp_count = 0;
170 if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0) {
171 *perr = ASN1_R_UNKNOWN_TAG;
175 if ((asn1_tags.utype == V_ASN1_SEQUENCE)
176 || (asn1_tags.utype == V_ASN1_SET)) {
178 *perr = ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG;
181 if (depth >= ASN1_GEN_SEQ_MAX_DEPTH) {
182 *perr = ASN1_R_ILLEGAL_NESTED_TAGGING;
185 ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf, depth, perr);
187 ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
192 /* If no tagging return base type */
193 if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
196 /* Generate the encoding */
197 cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
200 /* Set point to start copying for modified encoding */
201 cpy_start = orig_der;
203 /* Do we need IMPLICIT tagging? */
204 if (asn1_tags.imp_tag != -1) {
205 /* If IMPLICIT we will replace the underlying tag */
206 /* Skip existing tag+len */
207 r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class,
211 /* Update copy length */
212 cpy_len -= cpy_start - orig_der;
214 * For IMPLICIT tagging the length should match the original length
215 * and constructed flag should be consistent.
218 /* Indefinite length constructed */
222 /* Just retain constructed flag */
223 hdr_constructed = r & V_ASN1_CONSTRUCTED;
225 * Work out new length with IMPLICIT tag: ignore constructed because
226 * it will mess up if indefinite length
228 len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
232 /* Work out length in any EXPLICIT, starting from end */
234 for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
235 i < asn1_tags.exp_count; i++, etmp--) {
236 /* Content length: number of content octets + any padding */
237 len += etmp->exp_pad;
239 /* Total object length: length including new header */
240 len = ASN1_object_size(0, len, etmp->exp_tag);
243 /* Allocate buffer for new encoding */
245 new_der = OPENSSL_malloc(len);
249 /* Generate tagged encoding */
253 /* Output explicit tags first */
255 for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
257 ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
258 etmp->exp_tag, etmp->exp_class);
263 /* If IMPLICIT, output tag */
265 if (asn1_tags.imp_tag != -1) {
266 if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
267 && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
268 || asn1_tags.imp_tag == V_ASN1_SET))
269 hdr_constructed = V_ASN1_CONSTRUCTED;
270 ASN1_put_object(&p, hdr_constructed, hdr_len,
271 asn1_tags.imp_tag, asn1_tags.imp_class);
274 /* Copy across original encoding */
275 memcpy(p, cpy_start, cpy_len);
279 /* Obtain new ASN1_TYPE structure */
280 ret = d2i_ASN1_TYPE(NULL, &cp, len);
283 OPENSSL_free(orig_der);
284 OPENSSL_free(new_der);
290 static int asn1_cb(const char *elem, int len, void *bitstr)
292 tag_exp_arg *arg = bitstr;
296 const char *p, *vstart = NULL;
298 int tmp_tag, tmp_class;
303 for (i = 0, p = elem; i < len; p++, i++) {
304 /* Look for the ':' in name value pairs */
307 vlen = len - (vstart - elem);
313 utype = asn1_str2tag(elem, len);
316 ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_TAG);
317 ERR_add_error_data(2, "tag=", elem);
321 /* If this is not a modifier mark end of string and exit */
322 if (!(utype & ASN1_GEN_FLAG)) {
325 /* If no value and not end of string, error */
326 if (!vstart && elem[len]) {
327 ASN1err(ASN1_F_ASN1_CB, ASN1_R_MISSING_VALUE);
335 case ASN1_GEN_FLAG_IMP:
336 /* Check for illegal multiple IMPLICIT tagging */
337 if (arg->imp_tag != -1) {
338 ASN1err(ASN1_F_ASN1_CB, ASN1_R_ILLEGAL_NESTED_TAGGING);
341 if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
345 case ASN1_GEN_FLAG_EXP:
347 if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
349 if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
353 case ASN1_GEN_FLAG_SEQWRAP:
354 if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
358 case ASN1_GEN_FLAG_SETWRAP:
359 if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
363 case ASN1_GEN_FLAG_BITWRAP:
364 if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
368 case ASN1_GEN_FLAG_OCTWRAP:
369 if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
373 case ASN1_GEN_FLAG_FORMAT:
375 ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_FORMAT);
378 if (!strncmp(vstart, "ASCII", 5))
379 arg->format = ASN1_GEN_FORMAT_ASCII;
380 else if (!strncmp(vstart, "UTF8", 4))
381 arg->format = ASN1_GEN_FORMAT_UTF8;
382 else if (!strncmp(vstart, "HEX", 3))
383 arg->format = ASN1_GEN_FORMAT_HEX;
384 else if (!strncmp(vstart, "BITLIST", 7))
385 arg->format = ASN1_GEN_FORMAT_BITLIST;
387 ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_FORMAT);
398 static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
405 tag_num = strtoul(vstart, &eptr, 10);
406 /* Check we haven't gone past max length: should be impossible */
407 if (eptr && *eptr && (eptr > vstart + vlen))
410 ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_NUMBER);
414 /* If we have non numeric characters, parse them */
416 vlen -= eptr - vstart;
423 *pclass = V_ASN1_UNIVERSAL;
427 *pclass = V_ASN1_APPLICATION;
431 *pclass = V_ASN1_PRIVATE;
435 *pclass = V_ASN1_CONTEXT_SPECIFIC;
441 ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_MODIFIER);
442 ERR_add_error_data(2, "Char=", erch);
447 *pclass = V_ASN1_CONTEXT_SPECIFIC;
453 /* Handle multiple types: SET and SEQUENCE */
455 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
456 int depth, int *perr)
458 ASN1_TYPE *ret = NULL;
459 STACK_OF(ASN1_TYPE) *sk = NULL;
460 STACK_OF(CONF_VALUE) *sect = NULL;
461 unsigned char *der = NULL;
464 sk = sk_ASN1_TYPE_new_null();
470 sect = X509V3_get_section(cnf, (char *)section);
473 for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
475 generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf,
479 if (!sk_ASN1_TYPE_push(sk, typ))
485 * Now we has a STACK of the components, convert to the correct form
488 if (utype == V_ASN1_SET)
489 derlen = i2d_ASN1_SET_ANY(sk, &der);
491 derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
496 if (!(ret = ASN1_TYPE_new()))
499 if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
504 ret->value.asn1_string->data = der;
505 ret->value.asn1_string->length = derlen;
513 sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
515 X509V3_section_free(cnf, sect);
520 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
521 int exp_constructed, int exp_pad, int imp_ok)
523 tag_exp_type *exp_tmp;
524 /* Can only have IMPLICIT if permitted */
525 if ((arg->imp_tag != -1) && !imp_ok) {
526 ASN1err(ASN1_F_APPEND_EXP, ASN1_R_ILLEGAL_IMPLICIT_TAG);
530 if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
531 ASN1err(ASN1_F_APPEND_EXP, ASN1_R_DEPTH_EXCEEDED);
535 exp_tmp = &arg->exp_list[arg->exp_count++];
538 * If IMPLICIT set tag to implicit value then reset implicit tag since it
541 if (arg->imp_tag != -1) {
542 exp_tmp->exp_tag = arg->imp_tag;
543 exp_tmp->exp_class = arg->imp_class;
547 exp_tmp->exp_tag = exp_tag;
548 exp_tmp->exp_class = exp_class;
550 exp_tmp->exp_constructed = exp_constructed;
551 exp_tmp->exp_pad = exp_pad;
556 static int asn1_str2tag(const char *tagstr, int len)
559 static const struct tag_name_st *tntmp, tnst[] = {
560 ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
561 ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
562 ASN1_GEN_STR("NULL", V_ASN1_NULL),
563 ASN1_GEN_STR("INT", V_ASN1_INTEGER),
564 ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
565 ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
566 ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
567 ASN1_GEN_STR("OID", V_ASN1_OBJECT),
568 ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
569 ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
570 ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
571 ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
572 ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
573 ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
574 ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
575 ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
576 ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
577 ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
578 ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
579 ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
580 ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
581 ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
582 ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
583 ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
584 ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
585 ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
586 ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
587 ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
588 ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
589 ASN1_GEN_STR("T61", V_ASN1_T61STRING),
590 ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
591 ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
592 ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
593 ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
594 ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
595 ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
598 ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
599 ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
600 ASN1_GEN_STR("SET", V_ASN1_SET),
603 ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
604 ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
606 ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
607 ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
608 /* OCTET STRING wrapper */
609 ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
610 /* SEQUENCE wrapper */
611 ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
613 ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
614 /* BIT STRING wrapper */
615 ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
616 ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
617 ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
621 len = strlen(tagstr);
624 for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st); i++, tntmp++) {
625 if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
632 static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
634 ASN1_TYPE *atmp = NULL;
638 unsigned char *rdata;
643 if (!(atmp = ASN1_TYPE_new())) {
644 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
655 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_NULL_VALUE);
661 if (format != ASN1_GEN_FORMAT_ASCII) {
662 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
667 vtmp.value = (char *)str;
668 if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
669 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BOOLEAN);
675 case V_ASN1_ENUMERATED:
676 if (format != ASN1_GEN_FORMAT_ASCII) {
677 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
680 if (!(atmp->value.integer = s2i_ASN1_INTEGER(NULL, (char *)str))) {
681 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_INTEGER);
687 if (format != ASN1_GEN_FORMAT_ASCII) {
688 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
691 if (!(atmp->value.object = OBJ_txt2obj(str, 0))) {
692 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_OBJECT);
698 case V_ASN1_GENERALIZEDTIME:
699 if (format != ASN1_GEN_FORMAT_ASCII) {
700 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_TIME_NOT_ASCII_FORMAT);
703 if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
704 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
707 if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
708 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
711 atmp->value.asn1_string->type = utype;
712 if (!ASN1_TIME_check(atmp->value.asn1_string)) {
713 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_TIME_VALUE);
719 case V_ASN1_BMPSTRING:
720 case V_ASN1_PRINTABLESTRING:
721 case V_ASN1_IA5STRING:
722 case V_ASN1_T61STRING:
723 case V_ASN1_UTF8STRING:
724 case V_ASN1_VISIBLESTRING:
725 case V_ASN1_UNIVERSALSTRING:
726 case V_ASN1_GENERALSTRING:
727 case V_ASN1_NUMERICSTRING:
729 if (format == ASN1_GEN_FORMAT_ASCII)
730 format = MBSTRING_ASC;
731 else if (format == ASN1_GEN_FORMAT_UTF8)
732 format = MBSTRING_UTF8;
734 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_FORMAT);
738 if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
739 -1, format, ASN1_tag2bit(utype)) <= 0) {
740 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
746 case V_ASN1_BIT_STRING:
748 case V_ASN1_OCTET_STRING:
750 if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
751 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
755 if (format == ASN1_GEN_FORMAT_HEX) {
757 if (!(rdata = string_to_hex((char *)str, &rdlen))) {
758 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_HEX);
762 atmp->value.asn1_string->data = rdata;
763 atmp->value.asn1_string->length = rdlen;
764 atmp->value.asn1_string->type = utype;
766 } else if (format == ASN1_GEN_FORMAT_ASCII)
767 ASN1_STRING_set(atmp->value.asn1_string, str, -1);
768 else if ((format == ASN1_GEN_FORMAT_BITLIST)
769 && (utype == V_ASN1_BIT_STRING)) {
771 (str, ',', 1, bitstr_cb, atmp->value.bit_string)) {
772 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_LIST_ERROR);
778 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
782 if ((utype == V_ASN1_BIT_STRING) && no_unused) {
783 atmp->value.asn1_string->flags
784 &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
785 atmp->value.asn1_string->flags |= ASN1_STRING_FLAG_BITS_LEFT;
791 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_UNSUPPORTED_TYPE);
799 ERR_add_error_data(2, "string=", str);
802 ASN1_TYPE_free(atmp);
807 static int bitstr_cb(const char *elem, int len, void *bitstr)
813 bitnum = strtoul(elem, &eptr, 10);
814 if (eptr && *eptr && (eptr != elem + len))
817 ASN1err(ASN1_F_BITSTR_CB, ASN1_R_INVALID_NUMBER);
820 if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
821 ASN1err(ASN1_F_BITSTR_CB, ERR_R_MALLOC_FAILURE);
827 static int mask_cb(const char *elem, int len, void *arg)
829 unsigned long *pmask = arg, tmpmask;
833 if (len == 3 && !strncmp(elem, "DIR", 3)) {
834 *pmask |= B_ASN1_DIRECTORYSTRING;
837 tag = asn1_str2tag(elem, len);
838 if (!tag || (tag & ASN1_GEN_FLAG))
840 tmpmask = ASN1_tag2bit(tag);
847 int ASN1_str2mask(const char *str, unsigned long *pmask)
850 return CONF_parse_list(str, '|', 1, mask_cb, pmask);