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