1 /* $OpenBSD: asn1_gen.c,v 1.14 2015/07/18 14:40:59 miod Exp $ */
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.
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@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@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <string.h>
60 
61 #include <openssl/asn1.h>
62 #include <openssl/err.h>
63 #include <openssl/x509v3.h>
64 
65 #define ASN1_GEN_FLAG		0x10000
66 #define ASN1_GEN_FLAG_IMP	(ASN1_GEN_FLAG|1)
67 #define ASN1_GEN_FLAG_EXP	(ASN1_GEN_FLAG|2)
68 #define ASN1_GEN_FLAG_TAG	(ASN1_GEN_FLAG|3)
69 #define ASN1_GEN_FLAG_BITWRAP	(ASN1_GEN_FLAG|4)
70 #define ASN1_GEN_FLAG_OCTWRAP	(ASN1_GEN_FLAG|5)
71 #define ASN1_GEN_FLAG_SEQWRAP	(ASN1_GEN_FLAG|6)
72 #define ASN1_GEN_FLAG_SETWRAP	(ASN1_GEN_FLAG|7)
73 #define ASN1_GEN_FLAG_FORMAT	(ASN1_GEN_FLAG|8)
74 
75 #define ASN1_GEN_STR(str,val){str, sizeof(str) - 1, val}
76 
77 #define ASN1_FLAG_EXP_MAX	20
78 
79 /* Input formats */
80 
81 /* ASCII: default */
82 #define ASN1_GEN_FORMAT_ASCII	1
83 /* UTF8 */
84 #define ASN1_GEN_FORMAT_UTF8	2
85 /* Hex */
86 #define ASN1_GEN_FORMAT_HEX	3
87 /* List of bits */
88 #define ASN1_GEN_FORMAT_BITLIST	4
89 
90 struct tag_name_st {
91 	const char *strnam;
92 	int len;
93 	int tag;
94 };
95 
96 typedef struct {
97 	int exp_tag;
98 	int exp_class;
99 	int exp_constructed;
100 	int exp_pad;
101 	long exp_len;
102 } tag_exp_type;
103 
104 typedef struct {
105 	int imp_tag;
106 	int imp_class;
107 	int utype;
108 	int format;
109 	const char *str;
110 	tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
111 	int exp_count;
112 } tag_exp_arg;
113 
114 static int bitstr_cb(const char *elem, int len, void *bitstr);
115 static int asn1_cb(const char *elem, int len, void *bitstr);
116 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
117     int exp_constructed, int exp_pad, int imp_ok);
118 static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass);
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);
122 
123 ASN1_TYPE *
124 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 *
136 ASN1_generate_v3(char *str, X509V3_CTX *cnf)
137 {
138 	ASN1_TYPE *ret;
139 	tag_exp_arg asn1_tags;
140 	tag_exp_type *etmp;
141 
142 	int i, len;
143 
144 	unsigned char *orig_der = NULL, *new_der = NULL;
145 	const unsigned char *cpy_start;
146 	unsigned char *p;
147 	const unsigned char *cp;
148 	int cpy_len;
149 	long hdr_len = 0;
150 	int hdr_constructed = 0, hdr_tag, hdr_class;
151 	int r;
152 
153 	asn1_tags.imp_tag = -1;
154 	asn1_tags.imp_class = -1;
155 	asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
156 	asn1_tags.exp_count = 0;
157 	if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0)
158 		return NULL;
159 
160 	if ((asn1_tags.utype == V_ASN1_SEQUENCE) ||
161 	    (asn1_tags.utype == V_ASN1_SET)) {
162 		if (!cnf) {
163 			ASN1err(ASN1_F_ASN1_GENERATE_V3,
164 			    ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG);
165 			return NULL;
166 		}
167 		ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf);
168 	} else
169 		ret = asn1_str2type(asn1_tags.str, asn1_tags.format,
170 		    asn1_tags.utype);
171 
172 	if (!ret)
173 		return NULL;
174 
175 	/* If no tagging return base type */
176 	if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
177 		return ret;
178 
179 	/* Generate the encoding */
180 	cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
181 	ASN1_TYPE_free(ret);
182 	ret = NULL;
183 	/* Set point to start copying for modified encoding */
184 	cpy_start = orig_der;
185 
186 	/* Do we need IMPLICIT tagging? */
187 	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,
191 		    &hdr_class, cpy_len);
192 		if (r & 0x80)
193 			goto err;
194 		/* Update copy length */
195 		cpy_len -= cpy_start - orig_der;
196 		/* For IMPLICIT tagging the length should match the
197 		 * original length and constructed flag should be
198 		 * consistent.
199 		 */
200 		if (r & 0x1) {
201 			/* Indefinite length constructed */
202 			hdr_constructed = 2;
203 			hdr_len = 0;
204 		} else
205 			/* Just retain constructed flag */
206 			hdr_constructed = r & V_ASN1_CONSTRUCTED;
207 		/* Work out new length with IMPLICIT tag: ignore constructed
208 		 * because it will mess up if indefinite length
209 		 */
210 		len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
211 	} else
212 		len = cpy_len;
213 
214 	/* Work out length in any EXPLICIT, starting from end */
215 
216 	for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
217 	    i < asn1_tags.exp_count; i++, etmp--) {
218 		/* Content length: number of content octets + any padding */
219 		len += etmp->exp_pad;
220 		etmp->exp_len = len;
221 		/* Total object length: length including new header */
222 		len = ASN1_object_size(0, len, etmp->exp_tag);
223 	}
224 
225 	/* Allocate buffer for new encoding */
226 
227 	new_der = malloc(len);
228 	if (!new_der)
229 		goto err;
230 
231 	/* Generate tagged encoding */
232 	p = new_der;
233 
234 	/* Output explicit tags first */
235 	for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
236 	    i++, etmp++) {
237 		ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
238 		    etmp->exp_tag, etmp->exp_class);
239 		if (etmp->exp_pad)
240 			*p++ = 0;
241 	}
242 
243 	/* If IMPLICIT, output tag */
244 
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);
252 	}
253 
254 	/* Copy across original encoding */
255 	memcpy(p, cpy_start, cpy_len);
256 
257 	cp = new_der;
258 
259 	/* Obtain new ASN1_TYPE structure */
260 	ret = d2i_ASN1_TYPE(NULL, &cp, len);
261 
262 err:
263 	free(orig_der);
264 	free(new_der);
265 
266 	return ret;
267 }
268 
269 static int
270 asn1_cb(const char *elem, int len, void *bitstr)
271 {
272 	tag_exp_arg *arg = bitstr;
273 	int i;
274 	int utype;
275 	int vlen = 0;
276 	const char *p, *vstart = NULL;
277 
278 	int tmp_tag, tmp_class;
279 
280 	for (i = 0, p = elem; i < len; p++, i++) {
281 		/* Look for the ':' in name value pairs */
282 		if (*p == ':') {
283 			vstart = p + 1;
284 			vlen = len - (vstart - elem);
285 			len = p - elem;
286 			break;
287 		}
288 	}
289 
290 	utype = asn1_str2tag(elem, len);
291 
292 	if (utype == -1) {
293 		ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_TAG);
294 		ERR_asprintf_error_data("tag=%s", elem);
295 		return -1;
296 	}
297 
298 	/* If this is not a modifier mark end of string and exit */
299 	if (!(utype & ASN1_GEN_FLAG)) {
300 		arg->utype = utype;
301 		arg->str = vstart;
302 		/* If no value and not end of string, error */
303 		if (!vstart && elem[len]) {
304 			ASN1err(ASN1_F_ASN1_CB, ASN1_R_MISSING_VALUE);
305 			return -1;
306 		}
307 		return 0;
308 	}
309 
310 	switch (utype) {
311 
312 	case ASN1_GEN_FLAG_IMP:
313 		/* Check for illegal multiple IMPLICIT tagging */
314 		if (arg->imp_tag != -1) {
315 			ASN1err(ASN1_F_ASN1_CB, ASN1_R_ILLEGAL_NESTED_TAGGING);
316 			return -1;
317 		}
318 		if (!parse_tagging(vstart, vlen, &arg->imp_tag,
319 		    &arg->imp_class))
320 			return -1;
321 		break;
322 
323 	case ASN1_GEN_FLAG_EXP:
324 		if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
325 			return -1;
326 		if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
327 			return -1;
328 		break;
329 
330 	case ASN1_GEN_FLAG_SEQWRAP:
331 		if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
332 			return -1;
333 		break;
334 
335 	case ASN1_GEN_FLAG_SETWRAP:
336 		if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
337 			return -1;
338 		break;
339 
340 	case ASN1_GEN_FLAG_BITWRAP:
341 		if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
342 			return -1;
343 		break;
344 
345 	case ASN1_GEN_FLAG_OCTWRAP:
346 		if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
347 			return -1;
348 		break;
349 
350 	case ASN1_GEN_FLAG_FORMAT:
351 		if (vstart == NULL) {
352 			ASN1err(ASN1_F_ASN1_CB, ASN1_R_ILLEGAL_FORMAT);
353 			return -1;
354 		}
355 		if (!strncmp(vstart, "ASCII", 5))
356 			arg->format = ASN1_GEN_FORMAT_ASCII;
357 		else if (!strncmp(vstart, "UTF8", 4))
358 			arg->format = ASN1_GEN_FORMAT_UTF8;
359 		else if (!strncmp(vstart, "HEX", 3))
360 			arg->format = ASN1_GEN_FORMAT_HEX;
361 		else if (!strncmp(vstart, "BITLIST", 7))
362 			arg->format = ASN1_GEN_FORMAT_BITLIST;
363 		else {
364 			ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKOWN_FORMAT);
365 			return -1;
366 		}
367 		break;
368 
369 	}
370 
371 	return 1;
372 }
373 
374 static int
375 parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
376 {
377 	long tag_num;
378 	char *eptr;
379 
380 	if (!vstart)
381 		return 0;
382 	tag_num = strtoul(vstart, &eptr, 10);
383 	/* Check we haven't gone past max length: should be impossible */
384 	if (eptr && *eptr && (eptr > vstart + vlen))
385 		return 0;
386 	if (tag_num < 0) {
387 		ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_NUMBER);
388 		return 0;
389 	}
390 	*ptag = tag_num;
391 	/* If we have non numeric characters, parse them */
392 	if (eptr)
393 		vlen -= eptr - vstart;
394 	else
395 		vlen = 0;
396 	if (vlen) {
397 		switch (*eptr) {
398 
399 		case 'U':
400 			*pclass = V_ASN1_UNIVERSAL;
401 			break;
402 
403 		case 'A':
404 			*pclass = V_ASN1_APPLICATION;
405 			break;
406 
407 		case 'P':
408 			*pclass = V_ASN1_PRIVATE;
409 			break;
410 
411 		case 'C':
412 			*pclass = V_ASN1_CONTEXT_SPECIFIC;
413 			break;
414 
415 		default:
416 			ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_MODIFIER);
417 			ERR_asprintf_error_data("Char=%c", *eptr);
418 			return 0;
419 			break;
420 
421 		}
422 	} else
423 		*pclass = V_ASN1_CONTEXT_SPECIFIC;
424 
425 	return 1;
426 
427 }
428 
429 /* Handle multiple types: SET and SEQUENCE */
430 
431 static ASN1_TYPE *
432 asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
433 {
434 	ASN1_TYPE *ret = NULL;
435 	STACK_OF(ASN1_TYPE) *sk = NULL;
436 	STACK_OF(CONF_VALUE) *sect = NULL;
437 	unsigned char *der = NULL;
438 	int derlen;
439 	int i;
440 	sk = sk_ASN1_TYPE_new_null();
441 	if (!sk)
442 		goto bad;
443 	if (section) {
444 		if (!cnf)
445 			goto bad;
446 		sect = X509V3_get_section(cnf, (char *)section);
447 		if (!sect)
448 			goto bad;
449 		for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
450 			ASN1_TYPE *typ = ASN1_generate_v3(
451 			    sk_CONF_VALUE_value(sect, i)->value, cnf);
452 			if (!typ)
453 				goto bad;
454 			if (!sk_ASN1_TYPE_push(sk, typ))
455 				goto bad;
456 		}
457 	}
458 
459 	/* Now we has a STACK of the components, convert to the correct form */
460 
461 	if (utype == V_ASN1_SET)
462 		derlen = i2d_ASN1_SET_ANY(sk, &der);
463 	else
464 		derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
465 
466 	if (derlen < 0)
467 		goto bad;
468 
469 	if (!(ret = ASN1_TYPE_new()))
470 		goto bad;
471 
472 	if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
473 		goto bad;
474 
475 	ret->type = utype;
476 
477 	ret->value.asn1_string->data = der;
478 	ret->value.asn1_string->length = derlen;
479 
480 	der = NULL;
481 
482 bad:
483 	free(der);
484 	if (sk)
485 		sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
486 	if (sect)
487 		X509V3_section_free(cnf, sect);
488 
489 	return ret;
490 }
491 
492 static int
493 append_exp(tag_exp_arg *arg, int exp_tag, int exp_class, int exp_constructed,
494     int exp_pad, int imp_ok)
495 {
496 	tag_exp_type *exp_tmp;
497 
498 	/* Can only have IMPLICIT if permitted */
499 	if ((arg->imp_tag != -1) && !imp_ok) {
500 		ASN1err(ASN1_F_APPEND_EXP, ASN1_R_ILLEGAL_IMPLICIT_TAG);
501 		return 0;
502 	}
503 
504 	if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
505 		ASN1err(ASN1_F_APPEND_EXP, ASN1_R_DEPTH_EXCEEDED);
506 		return 0;
507 	}
508 
509 	exp_tmp = &arg->exp_list[arg->exp_count++];
510 
511 	/* If IMPLICIT set tag to implicit value then
512 	 * reset implicit tag since it has been used.
513 	 */
514 	if (arg->imp_tag != -1) {
515 		exp_tmp->exp_tag = arg->imp_tag;
516 		exp_tmp->exp_class = arg->imp_class;
517 		arg->imp_tag = -1;
518 		arg->imp_class = -1;
519 	} else {
520 		exp_tmp->exp_tag = exp_tag;
521 		exp_tmp->exp_class = exp_class;
522 	}
523 	exp_tmp->exp_constructed = exp_constructed;
524 	exp_tmp->exp_pad = exp_pad;
525 
526 	return 1;
527 }
528 
529 static int
530 asn1_str2tag(const char *tagstr, int len)
531 {
532 	unsigned int i;
533 	static const struct tag_name_st *tntmp, tnst [] = {
534 		ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
535 		ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
536 		ASN1_GEN_STR("NULL", V_ASN1_NULL),
537 		ASN1_GEN_STR("INT", V_ASN1_INTEGER),
538 		ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
539 		ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
540 		ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
541 		ASN1_GEN_STR("OID", V_ASN1_OBJECT),
542 		ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
543 		ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
544 		ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
545 		ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
546 		ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
547 		ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
548 		ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
549 		ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
550 		ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
551 		ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
552 		ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
553 		ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
554 		ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
555 		ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
556 		ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
557 		ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
558 		ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
559 		ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
560 		ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
561 		ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
562 		ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
563 		ASN1_GEN_STR("T61", V_ASN1_T61STRING),
564 		ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
565 		ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
566 		ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
567 		ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
568 		ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
569 		ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
570 
571 		/* Special cases */
572 		ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
573 		ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
574 		ASN1_GEN_STR("SET", V_ASN1_SET),
575 		/* type modifiers */
576 		/* Explicit tag */
577 		ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
578 		ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
579 		/* Implicit tag */
580 		ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
581 		ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
582 		/* OCTET STRING wrapper */
583 		ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
584 		/* SEQUENCE wrapper */
585 		ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
586 		/* SET wrapper */
587 		ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
588 		/* BIT STRING wrapper */
589 		ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
590 		ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
591 		ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
592 	};
593 
594 	if (len == -1)
595 		len = strlen(tagstr);
596 
597 	tntmp = tnst;
598 	for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st);
599 	    i++, tntmp++) {
600 		if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
601 			return tntmp->tag;
602 	}
603 
604 	return -1;
605 }
606 
607 static ASN1_TYPE *
608 asn1_str2type(const char *str, int format, int utype)
609 {
610 	ASN1_TYPE *atmp = NULL;
611 	CONF_VALUE vtmp;
612 	unsigned char *rdata;
613 	long rdlen;
614 	int no_unused = 1;
615 
616 	if (!(atmp = ASN1_TYPE_new())) {
617 		ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
618 		return NULL;
619 	}
620 
621 	if (!str)
622 		str = "";
623 
624 	switch (utype) {
625 
626 	case V_ASN1_NULL:
627 		if (str && *str) {
628 			ASN1err(ASN1_F_ASN1_STR2TYPE,
629 			    ASN1_R_ILLEGAL_NULL_VALUE);
630 			goto bad_form;
631 		}
632 		break;
633 
634 	case V_ASN1_BOOLEAN:
635 		if (format != ASN1_GEN_FORMAT_ASCII) {
636 			ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
637 			goto bad_form;
638 		}
639 		vtmp.name = NULL;
640 		vtmp.section = NULL;
641 		vtmp.value = (char *)str;
642 		if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
643 			ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BOOLEAN);
644 			goto bad_str;
645 		}
646 		break;
647 
648 	case V_ASN1_INTEGER:
649 	case V_ASN1_ENUMERATED:
650 		if (format != ASN1_GEN_FORMAT_ASCII) {
651 			ASN1err(ASN1_F_ASN1_STR2TYPE,
652 			    ASN1_R_INTEGER_NOT_ASCII_FORMAT);
653 			goto bad_form;
654 		}
655 		if (!(atmp->value.integer =
656 		    s2i_ASN1_INTEGER(NULL, (char *)str))) {
657 			ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_INTEGER);
658 			goto bad_str;
659 		}
660 		break;
661 
662 	case V_ASN1_OBJECT:
663 		if (format != ASN1_GEN_FORMAT_ASCII) {
664 			ASN1err(ASN1_F_ASN1_STR2TYPE,
665 			    ASN1_R_OBJECT_NOT_ASCII_FORMAT);
666 			goto bad_form;
667 		}
668 		if (!(atmp->value.object = OBJ_txt2obj(str, 0))) {
669 			ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_OBJECT);
670 			goto bad_str;
671 		}
672 		break;
673 
674 	case V_ASN1_UTCTIME:
675 	case V_ASN1_GENERALIZEDTIME:
676 		if (format != ASN1_GEN_FORMAT_ASCII) {
677 			ASN1err(ASN1_F_ASN1_STR2TYPE,
678 			    ASN1_R_TIME_NOT_ASCII_FORMAT);
679 			goto bad_form;
680 		}
681 		if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
682 			ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
683 			goto bad_str;
684 		}
685 		if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
686 			ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
687 			goto bad_str;
688 		}
689 		atmp->value.asn1_string->type = utype;
690 		if (!ASN1_TIME_check(atmp->value.asn1_string)) {
691 			ASN1err(ASN1_F_ASN1_STR2TYPE,
692 			    ASN1_R_ILLEGAL_TIME_VALUE);
693 			goto bad_str;
694 		}
695 		break;
696 
697 	case V_ASN1_BMPSTRING:
698 	case V_ASN1_PRINTABLESTRING:
699 	case V_ASN1_IA5STRING:
700 	case V_ASN1_T61STRING:
701 	case V_ASN1_UTF8STRING:
702 	case V_ASN1_VISIBLESTRING:
703 	case V_ASN1_UNIVERSALSTRING:
704 	case V_ASN1_GENERALSTRING:
705 	case V_ASN1_NUMERICSTRING:
706 
707 		if (format == ASN1_GEN_FORMAT_ASCII)
708 			format = MBSTRING_ASC;
709 		else if (format == ASN1_GEN_FORMAT_UTF8)
710 			format = MBSTRING_UTF8;
711 		else {
712 			ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_FORMAT);
713 			goto bad_form;
714 		}
715 
716 		if (ASN1_mbstring_copy(&atmp->value.asn1_string,
717 		    (unsigned char *)str, -1, format,
718 		    ASN1_tag2bit(utype)) <= 0) {
719 			ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
720 			goto bad_str;
721 		}
722 		break;
723 
724 	case V_ASN1_BIT_STRING:
725 	case V_ASN1_OCTET_STRING:
726 		if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
727 			ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
728 			goto bad_form;
729 		}
730 
731 		if (format == ASN1_GEN_FORMAT_HEX) {
732 
733 			if (!(rdata = string_to_hex((char *)str, &rdlen))) {
734 				ASN1err(ASN1_F_ASN1_STR2TYPE,
735 				    ASN1_R_ILLEGAL_HEX);
736 				goto bad_str;
737 			}
738 
739 			atmp->value.asn1_string->data = rdata;
740 			atmp->value.asn1_string->length = rdlen;
741 			atmp->value.asn1_string->type = utype;
742 
743 		} else if (format == ASN1_GEN_FORMAT_ASCII) {
744 			if (ASN1_STRING_set(atmp->value.asn1_string, str,
745 			    -1) == 0) {
746 				ASN1err(ASN1_F_ASN1_STR2TYPE,
747 				    ERR_R_MALLOC_FAILURE);
748 				goto bad_str;
749 			}
750 		} else if ((format == ASN1_GEN_FORMAT_BITLIST) &&
751 		    (utype == V_ASN1_BIT_STRING)) {
752 			if (!CONF_parse_list(str, ',', 1, bitstr_cb,
753 			    atmp->value.bit_string)) {
754 				ASN1err(ASN1_F_ASN1_STR2TYPE,
755 				    ASN1_R_LIST_ERROR);
756 				goto bad_str;
757 			}
758 			no_unused = 0;
759 
760 		} else {
761 			ASN1err(ASN1_F_ASN1_STR2TYPE,
762 			    ASN1_R_ILLEGAL_BITSTRING_FORMAT);
763 			goto bad_form;
764 		}
765 
766 		if ((utype == V_ASN1_BIT_STRING) && no_unused) {
767 			atmp->value.asn1_string->flags &=
768 			    ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
769 			atmp->value.asn1_string->flags |=
770 			    ASN1_STRING_FLAG_BITS_LEFT;
771 		}
772 
773 		break;
774 
775 	default:
776 		ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_UNSUPPORTED_TYPE);
777 		goto bad_str;
778 		break;
779 	}
780 
781 	atmp->type = utype;
782 	return atmp;
783 
784 bad_str:
785 	ERR_asprintf_error_data("string=%s", str);
786 bad_form:
787 	ASN1_TYPE_free(atmp);
788 	return NULL;
789 }
790 
791 static int
792 bitstr_cb(const char *elem, int len, void *bitstr)
793 {
794 	long bitnum;
795 	char *eptr;
796 
797 	if (!elem)
798 		return 0;
799 	bitnum = strtoul(elem, &eptr, 10);
800 	if (eptr && *eptr && (eptr != elem + len))
801 		return 0;
802 	if (bitnum < 0) {
803 		ASN1err(ASN1_F_BITSTR_CB, ASN1_R_INVALID_NUMBER);
804 		return 0;
805 	}
806 	if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
807 		ASN1err(ASN1_F_BITSTR_CB, ERR_R_MALLOC_FAILURE);
808 		return 0;
809 	}
810 	return 1;
811 }
812