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