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