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