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
81 #define ASN1_GEN_FORMAT_ASCII 1
83 #define ASN1_GEN_FORMAT_UTF8 2
85 #define ASN1_GEN_FORMAT_HEX 3
87 #define ASN1_GEN_FORMAT_BITLIST 4
109 tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
113 static int bitstr_cb(const char *elem, int len, void *bitstr);
114 static int asn1_cb(const char *elem, int len, void *bitstr);
115 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
116 int exp_constructed, int exp_pad, int imp_ok);
117 static int parse_tagging(const char *vstart, int vlen, int *ptag,
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);
123 ASN1_TYPE *ASN1_generate_nconf(char *str, CONF *nconf)
128 return ASN1_generate_v3(str, NULL);
130 X509V3_set_nconf(&cnf, nconf);
131 return ASN1_generate_v3(str, &cnf);
134 ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf)
137 tag_exp_arg asn1_tags;
142 unsigned char *orig_der = NULL, *new_der = NULL;
143 const unsigned char *cpy_start;
145 const unsigned char *cp;
148 int hdr_constructed = 0, hdr_tag, hdr_class;
151 asn1_tags.imp_tag = -1;
152 asn1_tags.imp_class = -1;
153 asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
154 asn1_tags.exp_count = 0;
155 if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0)
158 if ((asn1_tags.utype == V_ASN1_SEQUENCE)
159 || (asn1_tags.utype == V_ASN1_SET)) {
161 ASN1err(ASN1_F_ASN1_GENERATE_V3,
162 ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG);
165 ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf);
167 ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
172 /* If no tagging return base type */
173 if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
176 /* Generate the encoding */
177 cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
180 /* Set point to start copying for modified encoding */
181 cpy_start = orig_der;
183 /* Do we need IMPLICIT tagging? */
184 if (asn1_tags.imp_tag != -1) {
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,
191 /* Update copy length */
192 cpy_len -= cpy_start - orig_der;
194 * For IMPLICIT tagging the length should match the original length
195 * and constructed flag should be consistent.
198 /* Indefinite length constructed */
202 /* Just retain constructed flag */
203 hdr_constructed = r & V_ASN1_CONSTRUCTED;
205 * Work out new length with IMPLICIT tag: ignore constructed because
206 * it will mess up if indefinite length
208 len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
212 /* Work out length in any EXPLICIT, starting from end */
214 for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
215 i < asn1_tags.exp_count; i++, etmp--) {
216 /* Content length: number of content octets + any padding */
217 len += etmp->exp_pad;
219 /* Total object length: length including new header */
220 len = ASN1_object_size(0, len, etmp->exp_tag);
223 /* Allocate buffer for new encoding */
225 new_der = OPENSSL_malloc(len);
229 /* Generate tagged encoding */
233 /* Output explicit tags first */
235 for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
237 ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
238 etmp->exp_tag, etmp->exp_class);
243 /* If IMPLICIT, output tag */
245 if (asn1_tags.imp_tag != -1) {
246 if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
247 && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
248 || asn1_tags.imp_tag == V_ASN1_SET))
249 hdr_constructed = V_ASN1_CONSTRUCTED;
250 ASN1_put_object(&p, hdr_constructed, hdr_len,
251 asn1_tags.imp_tag, asn1_tags.imp_class);
254 /* Copy across original encoding */
255 memcpy(p, cpy_start, cpy_len);
259 /* Obtain new ASN1_TYPE structure */
260 ret = d2i_ASN1_TYPE(NULL, &cp, len);
264 OPENSSL_free(orig_der);
266 OPENSSL_free(new_der);
272 static int asn1_cb(const char *elem, int len, void *bitstr)
274 tag_exp_arg *arg = bitstr;
278 const char *p, *vstart = NULL;
280 int tmp_tag, tmp_class;
282 for (i = 0, p = elem; i < len; p++, i++) {
283 /* Look for the ':' in name value pairs */
286 vlen = len - (vstart - elem);
292 utype = asn1_str2tag(elem, len);
295 ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_TAG);
296 ERR_add_error_data(2, "tag=", elem);
300 /* If this is not a modifier mark end of string and exit */
301 if (!(utype & ASN1_GEN_FLAG)) {
304 /* If no value and not end of string, error */
305 if (!vstart && elem[len]) {
306 ASN1err(ASN1_F_ASN1_CB, ASN1_R_MISSING_VALUE);
314 case ASN1_GEN_FLAG_IMP:
315 /* Check for illegal multiple IMPLICIT tagging */
316 if (arg->imp_tag != -1) {
317 ASN1err(ASN1_F_ASN1_CB, ASN1_R_ILLEGAL_NESTED_TAGGING);
320 if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
324 case ASN1_GEN_FLAG_EXP:
326 if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
328 if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
332 case ASN1_GEN_FLAG_SEQWRAP:
333 if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
337 case ASN1_GEN_FLAG_SETWRAP:
338 if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
342 case ASN1_GEN_FLAG_BITWRAP:
343 if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
347 case ASN1_GEN_FLAG_OCTWRAP:
348 if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
352 case ASN1_GEN_FLAG_FORMAT:
353 if (!strncmp(vstart, "ASCII", 5))
354 arg->format = ASN1_GEN_FORMAT_ASCII;
355 else if (!strncmp(vstart, "UTF8", 4))
356 arg->format = ASN1_GEN_FORMAT_UTF8;
357 else if (!strncmp(vstart, "HEX", 3))
358 arg->format = ASN1_GEN_FORMAT_HEX;
359 else if (!strncmp(vstart, "BITLIST", 3))
360 arg->format = ASN1_GEN_FORMAT_BITLIST;
362 ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_FORMAT);
373 static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
380 tag_num = strtoul(vstart, &eptr, 10);
381 /* Check we haven't gone past max length: should be impossible */
382 if (eptr && *eptr && (eptr > vstart + vlen))
385 ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_NUMBER);
389 /* If we have non numeric characters, parse them */
391 vlen -= eptr - vstart;
398 *pclass = V_ASN1_UNIVERSAL;
402 *pclass = V_ASN1_APPLICATION;
406 *pclass = V_ASN1_PRIVATE;
410 *pclass = V_ASN1_CONTEXT_SPECIFIC;
416 ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_MODIFIER);
417 ERR_add_error_data(2, "Char=", erch);
423 *pclass = V_ASN1_CONTEXT_SPECIFIC;
429 /* Handle multiple types: SET and SEQUENCE */
431 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
433 ASN1_TYPE *ret = NULL;
434 STACK_OF(ASN1_TYPE) *sk = NULL;
435 STACK_OF(CONF_VALUE) *sect = NULL;
436 unsigned char *der = NULL;
439 sk = sk_ASN1_TYPE_new_null();
445 sect = X509V3_get_section(cnf, (char *)section);
448 for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
450 ASN1_generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf);
453 if (!sk_ASN1_TYPE_push(sk, typ))
459 * Now we has a STACK of the components, convert to the correct form
462 if (utype == V_ASN1_SET)
463 derlen = i2d_ASN1_SET_ANY(sk, &der);
465 derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
470 if (!(ret = ASN1_TYPE_new()))
473 if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
478 ret->value.asn1_string->data = der;
479 ret->value.asn1_string->length = derlen;
489 sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
491 X509V3_section_free(cnf, sect);
496 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
497 int exp_constructed, int exp_pad, int imp_ok)
499 tag_exp_type *exp_tmp;
500 /* Can only have IMPLICIT if permitted */
501 if ((arg->imp_tag != -1) && !imp_ok) {
502 ASN1err(ASN1_F_APPEND_EXP, ASN1_R_ILLEGAL_IMPLICIT_TAG);
506 if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
507 ASN1err(ASN1_F_APPEND_EXP, ASN1_R_DEPTH_EXCEEDED);
511 exp_tmp = &arg->exp_list[arg->exp_count++];
514 * If IMPLICIT set tag to implicit value then reset implicit tag since it
517 if (arg->imp_tag != -1) {
518 exp_tmp->exp_tag = arg->imp_tag;
519 exp_tmp->exp_class = arg->imp_class;
523 exp_tmp->exp_tag = exp_tag;
524 exp_tmp->exp_class = exp_class;
526 exp_tmp->exp_constructed = exp_constructed;
527 exp_tmp->exp_pad = exp_pad;
532 static int asn1_str2tag(const char *tagstr, int len)
535 static const struct tag_name_st *tntmp, tnst[] = {
536 ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
537 ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
538 ASN1_GEN_STR("NULL", V_ASN1_NULL),
539 ASN1_GEN_STR("INT", V_ASN1_INTEGER),
540 ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
541 ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
542 ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
543 ASN1_GEN_STR("OID", V_ASN1_OBJECT),
544 ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
545 ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
546 ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
547 ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
548 ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
549 ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
550 ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
551 ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
552 ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
553 ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
554 ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
555 ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
556 ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
557 ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
558 ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
559 ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
560 ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
561 ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
562 ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
563 ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
564 ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
565 ASN1_GEN_STR("T61", V_ASN1_T61STRING),
566 ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
567 ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
568 ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
569 ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
570 ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
571 ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
574 ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
575 ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
576 ASN1_GEN_STR("SET", V_ASN1_SET),
579 ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
580 ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
582 ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
583 ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
584 /* OCTET STRING wrapper */
585 ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
586 /* SEQUENCE wrapper */
587 ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
589 ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
590 /* BIT STRING wrapper */
591 ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
592 ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
593 ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
597 len = strlen(tagstr);
600 for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st); i++, tntmp++) {
601 if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
608 static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
610 ASN1_TYPE *atmp = NULL;
614 unsigned char *rdata;
619 if (!(atmp = ASN1_TYPE_new())) {
620 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
631 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_NULL_VALUE);
637 if (format != ASN1_GEN_FORMAT_ASCII) {
638 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
643 vtmp.value = (char *)str;
644 if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
645 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BOOLEAN);
651 case V_ASN1_ENUMERATED:
652 if (format != ASN1_GEN_FORMAT_ASCII) {
653 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
656 if (!(atmp->value.integer = s2i_ASN1_INTEGER(NULL, (char *)str))) {
657 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_INTEGER);
663 if (format != ASN1_GEN_FORMAT_ASCII) {
664 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
667 if (!(atmp->value.object = OBJ_txt2obj(str, 0))) {
668 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_OBJECT);
674 case V_ASN1_GENERALIZEDTIME:
675 if (format != ASN1_GEN_FORMAT_ASCII) {
676 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_TIME_NOT_ASCII_FORMAT);
679 if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
680 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
683 if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
684 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
687 atmp->value.asn1_string->type = utype;
688 if (!ASN1_TIME_check(atmp->value.asn1_string)) {
689 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_TIME_VALUE);
695 case V_ASN1_BMPSTRING:
696 case V_ASN1_PRINTABLESTRING:
697 case V_ASN1_IA5STRING:
698 case V_ASN1_T61STRING:
699 case V_ASN1_UTF8STRING:
700 case V_ASN1_VISIBLESTRING:
701 case V_ASN1_UNIVERSALSTRING:
702 case V_ASN1_GENERALSTRING:
703 case V_ASN1_NUMERICSTRING:
705 if (format == ASN1_GEN_FORMAT_ASCII)
706 format = MBSTRING_ASC;
707 else if (format == ASN1_GEN_FORMAT_UTF8)
708 format = MBSTRING_UTF8;
710 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_FORMAT);
714 if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
715 -1, format, ASN1_tag2bit(utype)) <= 0) {
716 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
722 case V_ASN1_BIT_STRING:
724 case V_ASN1_OCTET_STRING:
726 if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
727 ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
731 if (format == ASN1_GEN_FORMAT_HEX) {
733 if (!(rdata = string_to_hex((char *)str, &rdlen))) {
734 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_HEX);
738 atmp->value.asn1_string->data = rdata;
739 atmp->value.asn1_string->length = rdlen;
740 atmp->value.asn1_string->type = utype;
742 } else if (format == ASN1_GEN_FORMAT_ASCII)
743 ASN1_STRING_set(atmp->value.asn1_string, str, -1);
744 else if ((format == ASN1_GEN_FORMAT_BITLIST)
745 && (utype == V_ASN1_BIT_STRING)) {
747 (str, ',', 1, bitstr_cb, atmp->value.bit_string)) {
748 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_LIST_ERROR);
754 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
758 if ((utype == V_ASN1_BIT_STRING) && no_unused) {
759 atmp->value.asn1_string->flags
760 &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
761 atmp->value.asn1_string->flags |= ASN1_STRING_FLAG_BITS_LEFT;
767 ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_UNSUPPORTED_TYPE);
776 ERR_add_error_data(2, "string=", str);
779 ASN1_TYPE_free(atmp);
784 static int bitstr_cb(const char *elem, int len, void *bitstr)
790 bitnum = strtoul(elem, &eptr, 10);
791 if (eptr && *eptr && (eptr != elem + len))
794 ASN1err(ASN1_F_BITSTR_CB, ASN1_R_INVALID_NUMBER);
797 if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
798 ASN1err(ASN1_F_BITSTR_CB, ERR_R_MALLOC_FAILURE);
804 static int mask_cb(const char *elem, int len, void *arg)
806 unsigned long *pmask = arg, tmpmask;
808 if (len == 3 && !strncmp(elem, "DIR", 3)) {
809 *pmask |= B_ASN1_DIRECTORYSTRING;
812 tag = asn1_str2tag(elem, len);
813 if (!tag || (tag & ASN1_GEN_FLAG))
815 tmpmask = ASN1_tag2bit(tag);
822 int ASN1_str2mask(const char *str, unsigned long *pmask)
825 return CONF_parse_list(str, '|', 1, mask_cb, pmask);