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