GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/asn1/asn1_gen.c Lines: 0 308 0.0 %
Date: 2017-11-13 Branches: 0 227 0.0 %

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