1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
57 #include <openssl/x509.h>
61 #include <openssl/asn1.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 #include <openssl/x509v3.h>
67 #include "../internal.h"
70 * Although this file is in crypto/x509 for layering purposes, it emits
71 * errors from the ASN.1 module for OpenSSL compatibility.
74 #define ASN1_GEN_FLAG 0x10000
75 #define ASN1_GEN_FLAG_IMP (ASN1_GEN_FLAG|1)
76 #define ASN1_GEN_FLAG_EXP (ASN1_GEN_FLAG|2)
77 #define ASN1_GEN_FLAG_TAG (ASN1_GEN_FLAG|3)
78 #define ASN1_GEN_FLAG_BITWRAP (ASN1_GEN_FLAG|4)
79 #define ASN1_GEN_FLAG_OCTWRAP (ASN1_GEN_FLAG|5)
80 #define ASN1_GEN_FLAG_SEQWRAP (ASN1_GEN_FLAG|6)
81 #define ASN1_GEN_FLAG_SETWRAP (ASN1_GEN_FLAG|7)
82 #define ASN1_GEN_FLAG_FORMAT (ASN1_GEN_FLAG|8)
84 #define ASN1_GEN_STR(str,val) {str, sizeof(str) - 1, val}
86 #define ASN1_FLAG_EXP_MAX 20
87 /* Maximum number of nested sequences */
88 #define ASN1_GEN_SEQ_MAX_DEPTH 50
93 #define ASN1_GEN_FORMAT_ASCII 1
95 #define ASN1_GEN_FORMAT_UTF8 2
97 #define ASN1_GEN_FORMAT_HEX 3
99 #define ASN1_GEN_FORMAT_BITLIST 4
121 tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
125 static ASN1_TYPE *generate_v3(char *str, X509V3_CTX *cnf, int depth,
127 static int bitstr_cb(const char *elem, int len, void *bitstr);
128 static int asn1_cb(const char *elem, int len, void *bitstr);
129 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
130 int exp_constructed, int exp_pad, int imp_ok);
131 static int parse_tagging(const char *vstart, int vlen, int *ptag,
133 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
134 int depth, int *perr);
135 static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype);
136 static int asn1_str2tag(const char *tagstr, int len);
138 ASN1_TYPE *ASN1_generate_nconf(char *str, CONF *nconf)
143 return ASN1_generate_v3(str, NULL);
145 X509V3_set_nconf(&cnf, nconf);
146 return ASN1_generate_v3(str, &cnf);
149 ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf)
152 ASN1_TYPE *ret = generate_v3(str, cnf, 0, &err);
154 OPENSSL_PUT_ERROR(ASN1, err);
158 static ASN1_TYPE *generate_v3(char *str, X509V3_CTX *cnf, int depth,
162 tag_exp_arg asn1_tags;
167 unsigned char *orig_der = NULL, *new_der = NULL;
168 const unsigned char *cpy_start;
170 const unsigned char *cp;
173 int hdr_constructed = 0, hdr_tag, hdr_class;
176 asn1_tags.imp_tag = -1;
177 asn1_tags.imp_class = -1;
178 asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
179 asn1_tags.exp_count = 0;
180 if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0) {
181 *perr = ASN1_R_UNKNOWN_TAG;
185 if ((asn1_tags.utype == V_ASN1_SEQUENCE)
186 || (asn1_tags.utype == V_ASN1_SET)) {
188 *perr = ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG;
191 if (depth >= ASN1_GEN_SEQ_MAX_DEPTH) {
192 *perr = ASN1_R_ILLEGAL_NESTED_TAGGING;
195 ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf, depth, perr);
197 ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
202 /* If no tagging return base type */
203 if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
206 /* Generate the encoding */
207 cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
210 /* Set point to start copying for modified encoding */
211 cpy_start = orig_der;
213 /* Do we need IMPLICIT tagging? */
214 if (asn1_tags.imp_tag != -1) {
215 /* If IMPLICIT we will replace the underlying tag */
216 /* Skip existing tag+len */
217 r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class,
221 /* Update copy length */
222 cpy_len -= cpy_start - orig_der;
224 * For IMPLICIT tagging the length should match the original length
225 * and constructed flag should be consistent.
228 /* Indefinite length constructed */
232 /* Just retain constructed flag */
233 hdr_constructed = r & V_ASN1_CONSTRUCTED;
235 * Work out new length with IMPLICIT tag: ignore constructed because
236 * it will mess up if indefinite length
238 len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
242 /* Work out length in any EXPLICIT, starting from end */
244 for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
245 i < asn1_tags.exp_count; i++, etmp--) {
246 /* Content length: number of content octets + any padding */
247 len += etmp->exp_pad;
249 /* Total object length: length including new header */
250 len = ASN1_object_size(0, len, etmp->exp_tag);
253 /* Allocate buffer for new encoding */
255 new_der = OPENSSL_malloc(len);
259 /* Generate tagged encoding */
263 /* Output explicit tags first */
265 for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
267 ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
268 etmp->exp_tag, etmp->exp_class);
273 /* If IMPLICIT, output tag */
275 if (asn1_tags.imp_tag != -1) {
276 if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
277 && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
278 || asn1_tags.imp_tag == V_ASN1_SET))
279 hdr_constructed = V_ASN1_CONSTRUCTED;
280 ASN1_put_object(&p, hdr_constructed, hdr_len,
281 asn1_tags.imp_tag, asn1_tags.imp_class);
284 /* Copy across original encoding */
285 OPENSSL_memcpy(p, cpy_start, cpy_len);
289 /* Obtain new ASN1_TYPE structure */
290 ret = d2i_ASN1_TYPE(NULL, &cp, len);
294 OPENSSL_free(orig_der);
296 OPENSSL_free(new_der);
302 static int asn1_cb(const char *elem, int len, void *bitstr)
304 tag_exp_arg *arg = bitstr;
308 const char *p, *vstart = NULL;
310 int tmp_tag, tmp_class;
315 for (i = 0, p = elem; i < len; p++, i++) {
316 /* Look for the ':' in name value pairs */
319 vlen = len - (vstart - elem);
325 utype = asn1_str2tag(elem, len);
328 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_TAG);
329 ERR_add_error_data(2, "tag=", elem);
333 /* If this is not a modifier mark end of string and exit */
334 if (!(utype & ASN1_GEN_FLAG)) {
337 /* If no value and not end of string, error */
338 if (!vstart && elem[len]) {
339 OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE);
347 case ASN1_GEN_FLAG_IMP:
348 /* Check for illegal multiple IMPLICIT tagging */
349 if (arg->imp_tag != -1) {
350 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING);
353 if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
357 case ASN1_GEN_FLAG_EXP:
359 if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
361 if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
365 case ASN1_GEN_FLAG_SEQWRAP:
366 if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
370 case ASN1_GEN_FLAG_SETWRAP:
371 if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
375 case ASN1_GEN_FLAG_BITWRAP:
376 if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
380 case ASN1_GEN_FLAG_OCTWRAP:
381 if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
385 case ASN1_GEN_FLAG_FORMAT:
387 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
390 if (!strncmp(vstart, "ASCII", 5))
391 arg->format = ASN1_GEN_FORMAT_ASCII;
392 else if (!strncmp(vstart, "UTF8", 4))
393 arg->format = ASN1_GEN_FORMAT_UTF8;
394 else if (!strncmp(vstart, "HEX", 3))
395 arg->format = ASN1_GEN_FORMAT_HEX;
396 else if (!strncmp(vstart, "BITLIST", 7))
397 arg->format = ASN1_GEN_FORMAT_BITLIST;
399 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
410 static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
417 tag_num = strtoul(vstart, &eptr, 10);
418 /* Check we haven't gone past max length: should be impossible */
419 if (eptr && *eptr && (eptr > vstart + vlen))
422 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_NUMBER);
426 /* If we have non numeric characters, parse them */
428 vlen -= eptr - vstart;
435 *pclass = V_ASN1_UNIVERSAL;
439 *pclass = V_ASN1_APPLICATION;
443 *pclass = V_ASN1_PRIVATE;
447 *pclass = V_ASN1_CONTEXT_SPECIFIC;
453 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_MODIFIER);
454 ERR_add_error_data(2, "Char=", erch);
460 *pclass = V_ASN1_CONTEXT_SPECIFIC;
466 /* Handle multiple types: SET and SEQUENCE */
468 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
469 int depth, int *perr)
471 ASN1_TYPE *ret = NULL;
472 STACK_OF(ASN1_TYPE) *sk = NULL;
473 STACK_OF(CONF_VALUE) *sect = NULL;
474 unsigned char *der = NULL;
477 sk = sk_ASN1_TYPE_new_null();
483 sect = X509V3_get_section(cnf, (char *)section);
486 for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
488 generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf,
492 if (!sk_ASN1_TYPE_push(sk, typ))
498 * Now we has a STACK of the components, convert to the correct form
501 if (utype == V_ASN1_SET)
502 derlen = i2d_ASN1_SET_ANY(sk, &der);
504 derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
509 if (!(ret = ASN1_TYPE_new()))
512 if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
517 ret->value.asn1_string->data = der;
518 ret->value.asn1_string->length = derlen;
528 sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
530 X509V3_section_free(cnf, sect);
535 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
536 int exp_constructed, int exp_pad, int imp_ok)
538 tag_exp_type *exp_tmp;
539 /* Can only have IMPLICIT if permitted */
540 if ((arg->imp_tag != -1) && !imp_ok) {
541 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_IMPLICIT_TAG);
545 if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
546 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DEPTH_EXCEEDED);
550 exp_tmp = &arg->exp_list[arg->exp_count++];
553 * If IMPLICIT set tag to implicit value then reset implicit tag since it
556 if (arg->imp_tag != -1) {
557 exp_tmp->exp_tag = arg->imp_tag;
558 exp_tmp->exp_class = arg->imp_class;
562 exp_tmp->exp_tag = exp_tag;
563 exp_tmp->exp_class = exp_class;
565 exp_tmp->exp_constructed = exp_constructed;
566 exp_tmp->exp_pad = exp_pad;
571 static int asn1_str2tag(const char *tagstr, int len)
574 static const struct tag_name_st *tntmp, tnst[] = {
575 ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
576 ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
577 ASN1_GEN_STR("NULL", V_ASN1_NULL),
578 ASN1_GEN_STR("INT", V_ASN1_INTEGER),
579 ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
580 ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
581 ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
582 ASN1_GEN_STR("OID", V_ASN1_OBJECT),
583 ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
584 ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
585 ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
586 ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
587 ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
588 ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
589 ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
590 ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
591 ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
592 ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
593 ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
594 ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
595 ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
596 ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
597 ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
598 ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
599 ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
600 ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
601 ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
602 ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
603 ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
604 ASN1_GEN_STR("T61", V_ASN1_T61STRING),
605 ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
606 ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
607 ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
608 ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
609 ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
610 ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
613 ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
614 ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
615 ASN1_GEN_STR("SET", V_ASN1_SET),
618 ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
619 ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
621 ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
622 ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
623 /* OCTET STRING wrapper */
624 ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
625 /* SEQUENCE wrapper */
626 ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
628 ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
629 /* BIT STRING wrapper */
630 ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
631 ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
632 ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
636 len = strlen(tagstr);
639 for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st); i++, tntmp++) {
640 if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
647 static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
649 ASN1_TYPE *atmp = NULL;
653 unsigned char *rdata;
658 if (!(atmp = ASN1_TYPE_new())) {
659 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
670 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NULL_VALUE);
676 if (format != ASN1_GEN_FORMAT_ASCII) {
677 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ASCII_FORMAT);
682 vtmp.value = (char *)str;
683 if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
684 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_BOOLEAN);
690 case V_ASN1_ENUMERATED:
691 if (format != ASN1_GEN_FORMAT_ASCII) {
692 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
695 if (!(atmp->value.integer = s2i_ASN1_INTEGER(NULL, (char *)str))) {
696 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_INTEGER);
702 if (format != ASN1_GEN_FORMAT_ASCII) {
703 OPENSSL_PUT_ERROR(ASN1, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
706 if (!(atmp->value.object = OBJ_txt2obj(str, 0))) {
707 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
713 case V_ASN1_GENERALIZEDTIME:
714 if (format != ASN1_GEN_FORMAT_ASCII) {
715 OPENSSL_PUT_ERROR(ASN1, ASN1_R_TIME_NOT_ASCII_FORMAT);
718 if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
719 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
722 if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
723 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
726 atmp->value.asn1_string->type = utype;
727 if (!ASN1_TIME_check(atmp->value.asn1_string)) {
728 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
734 case V_ASN1_BMPSTRING:
735 case V_ASN1_PRINTABLESTRING:
736 case V_ASN1_IA5STRING:
737 case V_ASN1_T61STRING:
738 case V_ASN1_UTF8STRING:
739 case V_ASN1_VISIBLESTRING:
740 case V_ASN1_UNIVERSALSTRING:
741 case V_ASN1_GENERALSTRING:
742 case V_ASN1_NUMERICSTRING:
744 if (format == ASN1_GEN_FORMAT_ASCII)
745 format = MBSTRING_ASC;
746 else if (format == ASN1_GEN_FORMAT_UTF8)
747 format = MBSTRING_UTF8;
749 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_FORMAT);
753 if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
754 -1, format, ASN1_tag2bit(utype)) <= 0) {
755 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
761 case V_ASN1_BIT_STRING:
763 case V_ASN1_OCTET_STRING:
765 if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
766 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
770 if (format == ASN1_GEN_FORMAT_HEX) {
772 if (!(rdata = string_to_hex((char *)str, &rdlen))) {
773 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_HEX);
777 atmp->value.asn1_string->data = rdata;
778 atmp->value.asn1_string->length = rdlen;
779 atmp->value.asn1_string->type = utype;
781 } else if (format == ASN1_GEN_FORMAT_ASCII)
782 ASN1_STRING_set(atmp->value.asn1_string, str, -1);
783 else if ((format == ASN1_GEN_FORMAT_BITLIST)
784 && (utype == V_ASN1_BIT_STRING)) {
786 (str, ',', 1, bitstr_cb, atmp->value.bit_string)) {
787 OPENSSL_PUT_ERROR(ASN1, ASN1_R_LIST_ERROR);
793 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
797 if ((utype == V_ASN1_BIT_STRING) && no_unused) {
798 atmp->value.asn1_string->flags
799 &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
800 atmp->value.asn1_string->flags |= ASN1_STRING_FLAG_BITS_LEFT;
806 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_TYPE);
815 ERR_add_error_data(2, "string=", str);
818 ASN1_TYPE_free(atmp);
823 static int bitstr_cb(const char *elem, int len, void *bitstr)
829 bitnum = strtoul(elem, &eptr, 10);
830 if (eptr && *eptr && (eptr != elem + len))
833 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_NUMBER);
836 if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
837 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);