1 /* $OpenBSD: tasn_dec.c,v 1.31 2016/05/04 14:53:29 tedu Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000-2005 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 
60 #include <stddef.h>
61 #include <string.h>
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/objects.h>
65 #include <openssl/buffer.h>
66 #include <openssl/err.h>
67 
68 static int asn1_check_eoc(const unsigned char **in, long len);
69 static int asn1_find_end(const unsigned char **in, long len, char inf);
70 
71 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72     char inf, int tag, int aclass, int depth);
73 
74 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
75 
76 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
77     char *inf, char *cst, const unsigned char **in, long len, int exptag,
78     int expclass, char opt, ASN1_TLC *ctx);
79 
80 static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
81     long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
82 static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
83     long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
84 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
85     long len, const ASN1_ITEM *it, int tag, int aclass, char opt,
86     ASN1_TLC *ctx);
87 
88 /* Table to convert tags to bit values, used for MSTRING type */
89 static const unsigned long tag2bit[32] = {
90 	0,	0,	0,	B_ASN1_BIT_STRING,	/* tags  0 -  3 */
91 	B_ASN1_OCTET_STRING,	0,	0,		B_ASN1_UNKNOWN,/* tags  4- 7 */
92 	B_ASN1_UNKNOWN,	B_ASN1_UNKNOWN,	B_ASN1_UNKNOWN,	B_ASN1_UNKNOWN,/* tags  8-11 */
93 	B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
94 	B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
95 	B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING,       /* tags 20-22 */
96 	B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,			       /* tags 23-24 */
97 	B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING,  /* tags 25-27 */
98 	B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
99 };
100 
101 unsigned long
102 ASN1_tag2bit(int tag)
103 {
104 	if ((tag < 0) || (tag > 30))
105 		return 0;
106 	return tag2bit[tag];
107 }
108 
109 /* Macro to initialize and invalidate the cache */
110 
111 #define asn1_tlc_clear(c)	if (c) (c)->valid = 0
112 /* Version to avoid compiler warning about 'c' always non-NULL */
113 #define asn1_tlc_clear_nc(c)	(c)->valid = 0
114 
115 /* Decode an ASN1 item, this currently behaves just
116  * like a standard 'd2i' function. 'in' points to
117  * a buffer to read the data from, in future we will
118  * have more advanced versions that can input data
119  * a piece at a time and this will simply be a special
120  * case.
121  */
122 
123 ASN1_VALUE *
124 ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
125     const ASN1_ITEM *it)
126 {
127 	ASN1_TLC c;
128 	ASN1_VALUE *ptmpval = NULL;
129 
130 	if (!pval)
131 		pval = &ptmpval;
132 	asn1_tlc_clear_nc(&c);
133 	if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
134 		return *pval;
135 	return NULL;
136 }
137 
138 int
139 ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
140     const ASN1_TEMPLATE *tt)
141 {
142 	ASN1_TLC c;
143 
144 	asn1_tlc_clear_nc(&c);
145 	return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
146 }
147 
148 
149 /* Decode an item, taking care of IMPLICIT tagging, if any.
150  * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
151  */
152 
153 int
154 ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
155     const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
156 {
157 	const ASN1_TEMPLATE *tt, *errtt = NULL;
158 	const ASN1_EXTERN_FUNCS *ef;
159 	const ASN1_AUX *aux = it->funcs;
160 	ASN1_aux_cb *asn1_cb;
161 	const unsigned char *p = NULL, *q;
162 	unsigned char oclass;
163 	char seq_eoc, seq_nolen, cst, isopt;
164 	long tmplen;
165 	int i;
166 	int otag;
167 	int ret = 0;
168 	ASN1_VALUE **pchptr;
169 	int combine;
170 
171 	combine = aclass & ASN1_TFLG_COMBINE;
172 	aclass &= ~ASN1_TFLG_COMBINE;
173 
174 	if (!pval)
175 		return 0;
176 
177 	if (aux && aux->asn1_cb)
178 		asn1_cb = aux->asn1_cb;
179 	else
180 		asn1_cb = 0;
181 
182 	switch (it->itype) {
183 	case ASN1_ITYPE_PRIMITIVE:
184 		if (it->templates) {
185 			/* tagging or OPTIONAL is currently illegal on an item
186 			 * template because the flags can't get passed down.
187 			 * In practice this isn't a problem: we include the
188 			 * relevant flags from the item template in the
189 			 * template itself.
190 			 */
191 			if ((tag != -1) || opt) {
192 				ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
193 				    ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
194 				goto err;
195 			}
196 			return asn1_template_ex_d2i(pval, in, len,
197 			    it->templates, opt, ctx);
198 		}
199 		return asn1_d2i_ex_primitive(pval, in, len, it,
200 		    tag, aclass, opt, ctx);
201 		break;
202 
203 	case ASN1_ITYPE_MSTRING:
204 		p = *in;
205 		/* Just read in tag and class */
206 		ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
207 		    &p, len, -1, 0, 1, ctx);
208 		if (!ret) {
209 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
210 			    ERR_R_NESTED_ASN1_ERROR);
211 			goto err;
212 		}
213 
214 		/* Must be UNIVERSAL class */
215 		if (oclass != V_ASN1_UNIVERSAL) {
216 			/* If OPTIONAL, assume this is OK */
217 			if (opt)
218 				return -1;
219 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
220 			    ASN1_R_MSTRING_NOT_UNIVERSAL);
221 			goto err;
222 		}
223 		/* Check tag matches bit map */
224 		if (!(ASN1_tag2bit(otag) & it->utype)) {
225 			/* If OPTIONAL, assume this is OK */
226 			if (opt)
227 				return -1;
228 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
229 			    ASN1_R_MSTRING_WRONG_TAG);
230 			goto err;
231 		}
232 		return asn1_d2i_ex_primitive(pval, in, len,
233 		    it, otag, 0, 0, ctx);
234 
235 	case ASN1_ITYPE_EXTERN:
236 		/* Use new style d2i */
237 		ef = it->funcs;
238 		return ef->asn1_ex_d2i(pval, in, len,
239 		    it, tag, aclass, opt, ctx);
240 
241 	case ASN1_ITYPE_CHOICE:
242 		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
243 			goto auxerr;
244 
245 		if (*pval) {
246 			/* Free up and zero CHOICE value if initialised */
247 			i = asn1_get_choice_selector(pval, it);
248 			if ((i >= 0) && (i < it->tcount)) {
249 				tt = it->templates + i;
250 				pchptr = asn1_get_field_ptr(pval, tt);
251 				ASN1_template_free(pchptr, tt);
252 				asn1_set_choice_selector(pval, -1, it);
253 			}
254 		} else if (!ASN1_item_ex_new(pval, it)) {
255 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
256 			    ERR_R_NESTED_ASN1_ERROR);
257 			goto err;
258 		}
259 		/* CHOICE type, try each possibility in turn */
260 		p = *in;
261 		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
262 			pchptr = asn1_get_field_ptr(pval, tt);
263 			/* We mark field as OPTIONAL so its absence
264 			 * can be recognised.
265 			 */
266 			ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
267 			/* If field not present, try the next one */
268 			if (ret == -1)
269 				continue;
270 			/* If positive return, read OK, break loop */
271 			if (ret > 0)
272 				break;
273 			/* Otherwise must be an ASN1 parsing error */
274 			errtt = tt;
275 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
276 			    ERR_R_NESTED_ASN1_ERROR);
277 			goto err;
278 		}
279 
280 		/* Did we fall off the end without reading anything? */
281 		if (i == it->tcount) {
282 			/* If OPTIONAL, this is OK */
283 			if (opt) {
284 				/* Free and zero it */
285 				ASN1_item_ex_free(pval, it);
286 				return -1;
287 			}
288 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
289 			    ASN1_R_NO_MATCHING_CHOICE_TYPE);
290 			goto err;
291 		}
292 
293 		asn1_set_choice_selector(pval, i, it);
294 		*in = p;
295 		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
296 			goto auxerr;
297 		return 1;
298 
299 	case ASN1_ITYPE_NDEF_SEQUENCE:
300 	case ASN1_ITYPE_SEQUENCE:
301 		p = *in;
302 		tmplen = len;
303 
304 		/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
305 		if (tag == -1) {
306 			tag = V_ASN1_SEQUENCE;
307 			aclass = V_ASN1_UNIVERSAL;
308 		}
309 		/* Get SEQUENCE length and update len, p */
310 		ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
311 		    &p, len, tag, aclass, opt, ctx);
312 		if (!ret) {
313 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
314 			    ERR_R_NESTED_ASN1_ERROR);
315 			goto err;
316 		} else if (ret == -1)
317 			return -1;
318 		if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
319 			len = tmplen - (p - *in);
320 			seq_nolen = 1;
321 		}
322 		/* If indefinite we don't do a length check */
323 		else
324 			seq_nolen = seq_eoc;
325 		if (!cst) {
326 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
327 			    ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
328 			goto err;
329 		}
330 
331 		if (!*pval && !ASN1_item_ex_new(pval, it)) {
332 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
333 			    ERR_R_NESTED_ASN1_ERROR);
334 			goto err;
335 		}
336 
337 		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
338 			goto auxerr;
339 
340 		/* Free up and zero any ADB found */
341 		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
342 			if (tt->flags & ASN1_TFLG_ADB_MASK) {
343 				const ASN1_TEMPLATE *seqtt;
344 				ASN1_VALUE **pseqval;
345 				seqtt = asn1_do_adb(pval, tt, 1);
346 				if (!seqtt)
347 					goto err;
348 				pseqval = asn1_get_field_ptr(pval, seqtt);
349 				ASN1_template_free(pseqval, seqtt);
350 			}
351 		}
352 
353 		/* Get each field entry */
354 		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
355 			const ASN1_TEMPLATE *seqtt;
356 			ASN1_VALUE **pseqval;
357 			seqtt = asn1_do_adb(pval, tt, 1);
358 			if (!seqtt)
359 				goto err;
360 			pseqval = asn1_get_field_ptr(pval, seqtt);
361 			/* Have we ran out of data? */
362 			if (!len)
363 				break;
364 			q = p;
365 			if (asn1_check_eoc(&p, len)) {
366 				if (!seq_eoc) {
367 					ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
368 					    ASN1_R_UNEXPECTED_EOC);
369 					goto err;
370 				}
371 				len -= p - q;
372 				seq_eoc = 0;
373 				q = p;
374 				break;
375 			}
376 			/* This determines the OPTIONAL flag value. The field
377 			 * cannot be omitted if it is the last of a SEQUENCE
378 			 * and there is still data to be read. This isn't
379 			 * strictly necessary but it increases efficiency in
380 			 * some cases.
381 			 */
382 			if (i == (it->tcount - 1))
383 				isopt = 0;
384 			else
385 				isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
386 			/* attempt to read in field, allowing each to be
387 			 * OPTIONAL */
388 
389 			ret = asn1_template_ex_d2i(pseqval, &p, len,
390 			    seqtt, isopt, ctx);
391 			if (!ret) {
392 				errtt = seqtt;
393 				goto err;
394 			} else if (ret == -1) {
395 				/* OPTIONAL component absent.
396 				 * Free and zero the field.
397 				 */
398 				ASN1_template_free(pseqval, seqtt);
399 				continue;
400 			}
401 			/* Update length */
402 			len -= p - q;
403 		}
404 
405 		/* Check for EOC if expecting one */
406 		if (seq_eoc && !asn1_check_eoc(&p, len)) {
407 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
408 			goto err;
409 		}
410 		/* Check all data read */
411 		if (!seq_nolen && len) {
412 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
413 			    ASN1_R_SEQUENCE_LENGTH_MISMATCH);
414 			goto err;
415 		}
416 
417 		/* If we get here we've got no more data in the SEQUENCE,
418 		 * however we may not have read all fields so check all
419 		 * remaining are OPTIONAL and clear any that are.
420 		 */
421 		for (; i < it->tcount; tt++, i++) {
422 			const ASN1_TEMPLATE *seqtt;
423 			seqtt = asn1_do_adb(pval, tt, 1);
424 			if (!seqtt)
425 				goto err;
426 			if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
427 				ASN1_VALUE **pseqval;
428 				pseqval = asn1_get_field_ptr(pval, seqtt);
429 				ASN1_template_free(pseqval, seqtt);
430 			} else {
431 				errtt = seqtt;
432 				ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
433 				    ASN1_R_FIELD_MISSING);
434 				goto err;
435 			}
436 		}
437 		/* Save encoding */
438 		if (!asn1_enc_save(pval, *in, p - *in, it)) {
439 			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_MALLOC_FAILURE);
440 			goto auxerr;
441 		}
442 		*in = p;
443 		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
444 			goto auxerr;
445 		return 1;
446 
447 	default:
448 		return 0;
449 	}
450 
451 auxerr:
452 	ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
453 err:
454 	if (combine == 0)
455 		ASN1_item_ex_free(pval, it);
456 	if (errtt)
457 		ERR_asprintf_error_data("Field=%s, Type=%s", errtt->field_name,
458 		    it->sname);
459 	else
460 		ERR_asprintf_error_data("Type=%s", it->sname);
461 	return 0;
462 }
463 
464 /* Templates are handled with two separate functions.
465  * One handles any EXPLICIT tag and the other handles the rest.
466  */
467 
468 static int
469 asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long inlen,
470     const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
471 {
472 	int flags, aclass;
473 	int ret;
474 	long len;
475 	const unsigned char *p, *q;
476 	char exp_eoc;
477 
478 	if (!val)
479 		return 0;
480 	flags = tt->flags;
481 	aclass = flags & ASN1_TFLG_TAG_CLASS;
482 
483 	p = *in;
484 
485 	/* Check if EXPLICIT tag expected */
486 	if (flags & ASN1_TFLG_EXPTAG) {
487 		char cst;
488 		/* Need to work out amount of data available to the inner
489 		 * content and where it starts: so read in EXPLICIT header to
490 		 * get the info.
491 		 */
492 		ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
493 		    &p, inlen, tt->tag, aclass, opt, ctx);
494 		q = p;
495 		if (!ret) {
496 			ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
497 			    ERR_R_NESTED_ASN1_ERROR);
498 			return 0;
499 		} else if (ret == -1)
500 			return -1;
501 		if (!cst) {
502 			ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
503 			    ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
504 			return 0;
505 		}
506 		/* We've found the field so it can't be OPTIONAL now */
507 		ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
508 		if (!ret) {
509 			ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
510 			    ERR_R_NESTED_ASN1_ERROR);
511 			return 0;
512 		}
513 		/* We read the field in OK so update length */
514 		len -= p - q;
515 		if (exp_eoc) {
516 			/* If NDEF we must have an EOC here */
517 			if (!asn1_check_eoc(&p, len)) {
518 				ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
519 				    ASN1_R_MISSING_EOC);
520 				goto err;
521 			}
522 		} else {
523 			/* Otherwise we must hit the EXPLICIT tag end or its
524 			 * an error */
525 			if (len) {
526 				ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
527 				    ASN1_R_EXPLICIT_LENGTH_MISMATCH);
528 				goto err;
529 			}
530 		}
531 	} else
532 		return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
533 
534 	*in = p;
535 	return 1;
536 
537 err:
538 	ASN1_template_free(val, tt);
539 	return 0;
540 }
541 
542 static int
543 asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, long len,
544     const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
545 {
546 	int flags, aclass;
547 	int ret;
548 	const unsigned char *p, *q;
549 
550 	if (!val)
551 		return 0;
552 	flags = tt->flags;
553 	aclass = flags & ASN1_TFLG_TAG_CLASS;
554 
555 	p = *in;
556 	q = p;
557 
558 	if (flags & ASN1_TFLG_SK_MASK) {
559 		/* SET OF, SEQUENCE OF */
560 		int sktag, skaclass;
561 		char sk_eoc;
562 		/* First work out expected inner tag value */
563 		if (flags & ASN1_TFLG_IMPTAG) {
564 			sktag = tt->tag;
565 			skaclass = aclass;
566 		} else {
567 			skaclass = V_ASN1_UNIVERSAL;
568 			if (flags & ASN1_TFLG_SET_OF)
569 				sktag = V_ASN1_SET;
570 			else
571 				sktag = V_ASN1_SEQUENCE;
572 		}
573 		/* Get the tag */
574 		ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
575 		    &p, len, sktag, skaclass, opt, ctx);
576 		if (!ret) {
577 			ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
578 			    ERR_R_NESTED_ASN1_ERROR);
579 			return 0;
580 		} else if (ret == -1)
581 			return -1;
582 		if (!*val)
583 			*val = (ASN1_VALUE *)sk_new_null();
584 		else {
585 			/* We've got a valid STACK: free up any items present */
586 			STACK_OF(ASN1_VALUE) *sktmp =
587 			    (STACK_OF(ASN1_VALUE) *)*val;
588 			ASN1_VALUE *vtmp;
589 			while (sk_ASN1_VALUE_num(sktmp) > 0) {
590 				vtmp = sk_ASN1_VALUE_pop(sktmp);
591 				ASN1_item_ex_free(&vtmp,
592 				    ASN1_ITEM_ptr(tt->item));
593 			}
594 		}
595 
596 		if (!*val) {
597 			ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
598 			    ERR_R_MALLOC_FAILURE);
599 			goto err;
600 		}
601 
602 		/* Read as many items as we can */
603 		while (len > 0) {
604 			ASN1_VALUE *skfield;
605 			q = p;
606 			/* See if EOC found */
607 			if (asn1_check_eoc(&p, len)) {
608 				if (!sk_eoc) {
609 					ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
610 					    ASN1_R_UNEXPECTED_EOC);
611 					goto err;
612 				}
613 				len -= p - q;
614 				sk_eoc = 0;
615 				break;
616 			}
617 			skfield = NULL;
618 			if (!ASN1_item_ex_d2i(&skfield, &p, len,
619 			    ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
620 				ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
621 				    ERR_R_NESTED_ASN1_ERROR);
622 				goto err;
623 			}
624 			len -= p - q;
625 			if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
626 			    skfield)) {
627 				ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
628 				    ERR_R_MALLOC_FAILURE);
629 				goto err;
630 			}
631 		}
632 		if (sk_eoc) {
633 			ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
634 			    ASN1_R_MISSING_EOC);
635 			goto err;
636 		}
637 	} else if (flags & ASN1_TFLG_IMPTAG) {
638 		/* IMPLICIT tagging */
639 		ret = ASN1_item_ex_d2i(val, &p, len,
640 		    ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
641 		if (!ret) {
642 			ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
643 			    ERR_R_NESTED_ASN1_ERROR);
644 			goto err;
645 		} else if (ret == -1)
646 			return -1;
647 	} else {
648 		/* Nothing special */
649 		ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
650 		    -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx);
651 		if (!ret) {
652 			ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
653 			    ERR_R_NESTED_ASN1_ERROR);
654 			goto err;
655 		} else if (ret == -1)
656 			return -1;
657 	}
658 
659 	*in = p;
660 	return 1;
661 
662 err:
663 	ASN1_template_free(val, tt);
664 	return 0;
665 }
666 
667 static int
668 asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, long inlen,
669     const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
670 {
671 	int ret = 0, utype;
672 	long plen;
673 	char cst, inf, free_cont = 0;
674 	const unsigned char *p;
675 	BUF_MEM buf;
676 	const unsigned char *cont = NULL;
677 	long len;
678 
679 	buf.length = 0;
680 	buf.max = 0;
681 	buf.data = NULL;
682 
683 	if (!pval) {
684 		ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
685 		return 0; /* Should never happen */
686 	}
687 
688 	if (it->itype == ASN1_ITYPE_MSTRING) {
689 		utype = tag;
690 		tag = -1;
691 	} else
692 		utype = it->utype;
693 
694 	if (utype == V_ASN1_ANY) {
695 		/* If type is ANY need to figure out type from tag */
696 		unsigned char oclass;
697 		if (tag >= 0) {
698 			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
699 			    ASN1_R_ILLEGAL_TAGGED_ANY);
700 			return 0;
701 		}
702 		if (opt) {
703 			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
704 			    ASN1_R_ILLEGAL_OPTIONAL_ANY);
705 			return 0;
706 		}
707 		p = *in;
708 		ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
709 		    &p, inlen, -1, 0, 0, ctx);
710 		if (!ret) {
711 			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
712 			    ERR_R_NESTED_ASN1_ERROR);
713 			return 0;
714 		}
715 		if (oclass != V_ASN1_UNIVERSAL)
716 			utype = V_ASN1_OTHER;
717 	}
718 	if (tag == -1) {
719 		tag = utype;
720 		aclass = V_ASN1_UNIVERSAL;
721 	}
722 	p = *in;
723 	/* Check header */
724 	ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
725 	    &p, inlen, tag, aclass, opt, ctx);
726 	if (!ret) {
727 		ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
728 		return 0;
729 	} else if (ret == -1)
730 		return -1;
731 	ret = 0;
732 	/* SEQUENCE, SET and "OTHER" are left in encoded form */
733 	if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
734 	    (utype == V_ASN1_OTHER)) {
735 		/* Clear context cache for type OTHER because the auto clear
736 		 * when we have a exact match wont work
737 		 */
738 		if (utype == V_ASN1_OTHER) {
739 			asn1_tlc_clear(ctx);
740 		}
741 		/* SEQUENCE and SET must be constructed */
742 		else if (!cst) {
743 			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
744 			    ASN1_R_TYPE_NOT_CONSTRUCTED);
745 			return 0;
746 		}
747 
748 		cont = *in;
749 		/* If indefinite length constructed find the real end */
750 		if (inf) {
751 			if (!asn1_find_end(&p, plen, inf))
752 				goto err;
753 			len = p - cont;
754 		} else {
755 			len = p - cont + plen;
756 			p += plen;
757 			buf.data = NULL;
758 		}
759 	} else if (cst) {
760 		/* Should really check the internal tags are correct but
761 		 * some things may get this wrong. The relevant specs
762 		 * say that constructed string types should be OCTET STRINGs
763 		 * internally irrespective of the type. So instead just check
764 		 * for UNIVERSAL class and ignore the tag.
765 		 */
766 		if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
767 			free_cont = 1;
768 			goto err;
769 		}
770 		len = buf.length;
771 		/* Append a final null to string */
772 		if (!BUF_MEM_grow_clean(&buf, len + 1)) {
773 			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
774 			    ERR_R_MALLOC_FAILURE);
775 			return 0;
776 		}
777 		buf.data[len] = 0;
778 		cont = (const unsigned char *)buf.data;
779 		free_cont = 1;
780 	} else {
781 		cont = p;
782 		len = plen;
783 		p += plen;
784 	}
785 
786 	/* We now have content length and type: translate into a structure */
787 	if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
788 		goto err;
789 
790 	*in = p;
791 	ret = 1;
792 
793 err:
794 	if (free_cont && buf.data)
795 		free(buf.data);
796 	return ret;
797 }
798 
799 /* Translate ASN1 content octets into a structure */
800 
801 int
802 asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype,
803     char *free_cont, const ASN1_ITEM *it)
804 {
805 	ASN1_VALUE **opval = NULL;
806 	ASN1_STRING *stmp;
807 	ASN1_TYPE *typ = NULL;
808 	int ret = 0;
809 	const ASN1_PRIMITIVE_FUNCS *pf;
810 	ASN1_INTEGER **tint;
811 
812 	pf = it->funcs;
813 
814 	if (pf && pf->prim_c2i)
815 		return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
816 	/* If ANY type clear type and set pointer to internal value */
817 	if (it->utype == V_ASN1_ANY) {
818 		if (!*pval) {
819 			typ = ASN1_TYPE_new();
820 			if (typ == NULL)
821 				goto err;
822 			*pval = (ASN1_VALUE *)typ;
823 		} else
824 			typ = (ASN1_TYPE *)*pval;
825 
826 		if (utype != typ->type)
827 			ASN1_TYPE_set(typ, utype, NULL);
828 		opval = pval;
829 		pval = &typ->value.asn1_value;
830 	}
831 	switch (utype) {
832 	case V_ASN1_OBJECT:
833 		if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
834 			goto err;
835 		break;
836 
837 	case V_ASN1_NULL:
838 		if (len) {
839 			ASN1err(ASN1_F_ASN1_EX_C2I,
840 			    ASN1_R_NULL_IS_WRONG_LENGTH);
841 			goto err;
842 		}
843 		*pval = (ASN1_VALUE *)1;
844 		break;
845 
846 	case V_ASN1_BOOLEAN:
847 		if (len != 1) {
848 			ASN1err(ASN1_F_ASN1_EX_C2I,
849 			    ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
850 			goto err;
851 		} else {
852 			ASN1_BOOLEAN *tbool;
853 			tbool = (ASN1_BOOLEAN *)pval;
854 			*tbool = *cont;
855 		}
856 		break;
857 
858 	case V_ASN1_BIT_STRING:
859 		if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
860 			goto err;
861 		break;
862 
863 	case V_ASN1_INTEGER:
864 	case V_ASN1_ENUMERATED:
865 		tint = (ASN1_INTEGER **)pval;
866 		if (!c2i_ASN1_INTEGER(tint, &cont, len))
867 			goto err;
868 		/* Fixup type to match the expected form */
869 		(*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
870 		break;
871 
872 	case V_ASN1_OCTET_STRING:
873 	case V_ASN1_NUMERICSTRING:
874 	case V_ASN1_PRINTABLESTRING:
875 	case V_ASN1_T61STRING:
876 	case V_ASN1_VIDEOTEXSTRING:
877 	case V_ASN1_IA5STRING:
878 	case V_ASN1_UTCTIME:
879 	case V_ASN1_GENERALIZEDTIME:
880 	case V_ASN1_GRAPHICSTRING:
881 	case V_ASN1_VISIBLESTRING:
882 	case V_ASN1_GENERALSTRING:
883 	case V_ASN1_UNIVERSALSTRING:
884 	case V_ASN1_BMPSTRING:
885 	case V_ASN1_UTF8STRING:
886 	case V_ASN1_OTHER:
887 	case V_ASN1_SET:
888 	case V_ASN1_SEQUENCE:
889 	default:
890 		if (utype == V_ASN1_BMPSTRING && (len & 1)) {
891 			ASN1err(ASN1_F_ASN1_EX_C2I,
892 			    ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
893 			goto err;
894 		}
895 		if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
896 			ASN1err(ASN1_F_ASN1_EX_C2I,
897 			    ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
898 			goto err;
899 		}
900 		/* All based on ASN1_STRING and handled the same */
901 		if (!*pval) {
902 			stmp = ASN1_STRING_type_new(utype);
903 			if (!stmp) {
904 				ASN1err(ASN1_F_ASN1_EX_C2I,
905 				    ERR_R_MALLOC_FAILURE);
906 				goto err;
907 			}
908 			*pval = (ASN1_VALUE *)stmp;
909 		} else {
910 			stmp = (ASN1_STRING *)*pval;
911 			stmp->type = utype;
912 		}
913 		/* If we've already allocated a buffer use it */
914 		if (*free_cont) {
915 			free(stmp->data);
916 			stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
917 			stmp->length = len;
918 			*free_cont = 0;
919 		} else {
920 			if (!ASN1_STRING_set(stmp, cont, len)) {
921 				ASN1err(ASN1_F_ASN1_EX_C2I,
922 				    ERR_R_MALLOC_FAILURE);
923 				ASN1_STRING_free(stmp);
924 				*pval = NULL;
925 				goto err;
926 			}
927 		}
928 		break;
929 	}
930 	/* If ASN1_ANY and NULL type fix up value */
931 	if (typ && (utype == V_ASN1_NULL))
932 		typ->value.ptr = NULL;
933 
934 	ret = 1;
935 
936 err:
937 	if (!ret) {
938 		ASN1_TYPE_free(typ);
939 		if (opval)
940 			*opval = NULL;
941 	}
942 	return ret;
943 }
944 
945 
946 /* This function finds the end of an ASN1 structure when passed its maximum
947  * length, whether it is indefinite length and a pointer to the content.
948  * This is more efficient than calling asn1_collect because it does not
949  * recurse on each indefinite length header.
950  */
951 
952 static int
953 asn1_find_end(const unsigned char **in, long len, char inf)
954 {
955 	int expected_eoc;
956 	long plen;
957 	const unsigned char *p = *in, *q;
958 
959 	/* If not indefinite length constructed just add length */
960 	if (inf == 0) {
961 		*in += len;
962 		return 1;
963 	}
964 	expected_eoc = 1;
965 	/* Indefinite length constructed form. Find the end when enough EOCs
966 	 * are found. If more indefinite length constructed headers
967 	 * are encountered increment the expected eoc count otherwise just
968 	 * skip to the end of the data.
969 	 */
970 	while (len > 0) {
971 		if (asn1_check_eoc(&p, len)) {
972 			expected_eoc--;
973 			if (expected_eoc == 0)
974 				break;
975 			len -= 2;
976 			continue;
977 		}
978 		q = p;
979 		/* Just read in a header: only care about the length */
980 		if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
981 		    -1, 0, 0, NULL)) {
982 			ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
983 			return 0;
984 		}
985 		if (inf)
986 			expected_eoc++;
987 		else
988 			p += plen;
989 		len -= p - q;
990 	}
991 	if (expected_eoc) {
992 		ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
993 		return 0;
994 	}
995 	*in = p;
996 	return 1;
997 }
998 /* This function collects the asn1 data from a constructred string
999  * type into a buffer. The values of 'in' and 'len' should refer
1000  * to the contents of the constructed type and 'inf' should be set
1001  * if it is indefinite length.
1002  */
1003 
1004 #ifndef ASN1_MAX_STRING_NEST
1005 /* This determines how many levels of recursion are permitted in ASN1
1006  * string types. If it is not limited stack overflows can occur. If set
1007  * to zero no recursion is allowed at all. Although zero should be adequate
1008  * examples exist that require a value of 1. So 5 should be more than enough.
1009  */
1010 #define ASN1_MAX_STRING_NEST 5
1011 #endif
1012 
1013 static int
1014 asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, char inf,
1015     int tag, int aclass, int depth)
1016 {
1017 	const unsigned char *p, *q;
1018 	long plen;
1019 	char cst, ininf;
1020 
1021 	p = *in;
1022 	inf &= 1;
1023 	/* If no buffer and not indefinite length constructed just pass over
1024 	 * the encoded data */
1025 	if (!buf && !inf) {
1026 		*in += len;
1027 		return 1;
1028 	}
1029 	while (len > 0) {
1030 		q = p;
1031 		/* Check for EOC */
1032 		if (asn1_check_eoc(&p, len)) {
1033 			/* EOC is illegal outside indefinite length
1034 			 * constructed form */
1035 			if (!inf) {
1036 				ASN1err(ASN1_F_ASN1_COLLECT,
1037 				    ASN1_R_UNEXPECTED_EOC);
1038 				return 0;
1039 			}
1040 			inf = 0;
1041 			break;
1042 		}
1043 
1044 		if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1045 		    len, tag, aclass, 0, NULL)) {
1046 			ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1047 			return 0;
1048 		}
1049 
1050 		/* If indefinite length constructed update max length */
1051 		if (cst) {
1052 			if (depth >= ASN1_MAX_STRING_NEST) {
1053 				ASN1err(ASN1_F_ASN1_COLLECT,
1054 				    ASN1_R_NESTED_ASN1_STRING);
1055 				return 0;
1056 			}
1057 			if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1058 			    depth + 1))
1059 				return 0;
1060 		} else if (plen && !collect_data(buf, &p, plen))
1061 			return 0;
1062 		len -= p - q;
1063 	}
1064 	if (inf) {
1065 		ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1066 		return 0;
1067 	}
1068 	*in = p;
1069 	return 1;
1070 }
1071 
1072 static int
1073 collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1074 {
1075 	int len;
1076 	if (buf) {
1077 		len = buf->length;
1078 		if (!BUF_MEM_grow_clean(buf, len + plen)) {
1079 			ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1080 			return 0;
1081 		}
1082 		memcpy(buf->data + len, *p, plen);
1083 	}
1084 	*p += plen;
1085 	return 1;
1086 }
1087 
1088 /* Check for ASN1 EOC and swallow it if found */
1089 
1090 static int
1091 asn1_check_eoc(const unsigned char **in, long len)
1092 {
1093 	const unsigned char *p;
1094 
1095 	if (len < 2)
1096 		return 0;
1097 	p = *in;
1098 	if (!p[0] && !p[1]) {
1099 		*in += 2;
1100 		return 1;
1101 	}
1102 	return 0;
1103 }
1104 
1105 /* Check an ASN1 tag and length: a bit like ASN1_get_object
1106  * but it sets the length for indefinite length constructed
1107  * form, we don't know the exact length but we can set an
1108  * upper bound to the amount of data available minus the
1109  * header length just read.
1110  */
1111 
1112 static int
1113 asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf,
1114     char *cst, const unsigned char **in, long len, int exptag, int expclass,
1115     char opt, ASN1_TLC *ctx)
1116 {
1117 	int i;
1118 	int ptag, pclass;
1119 	long plen;
1120 	const unsigned char *p, *q;
1121 
1122 	p = *in;
1123 	q = p;
1124 
1125 	if (ctx && ctx->valid) {
1126 		i = ctx->ret;
1127 		plen = ctx->plen;
1128 		pclass = ctx->pclass;
1129 		ptag = ctx->ptag;
1130 		p += ctx->hdrlen;
1131 	} else {
1132 		i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1133 		if (ctx) {
1134 			ctx->ret = i;
1135 			ctx->plen = plen;
1136 			ctx->pclass = pclass;
1137 			ctx->ptag = ptag;
1138 			ctx->hdrlen = p - q;
1139 			ctx->valid = 1;
1140 			/* If definite length, and no error, length +
1141 			 * header can't exceed total amount of data available.
1142 			 */
1143 			if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1144 				ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1145 				    ASN1_R_TOO_LONG);
1146 				asn1_tlc_clear(ctx);
1147 				return 0;
1148 			}
1149 		}
1150 	}
1151 
1152 	if (i & 0x80) {
1153 		ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1154 		asn1_tlc_clear(ctx);
1155 		return 0;
1156 	}
1157 	if (exptag >= 0) {
1158 		if ((exptag != ptag) || (expclass != pclass)) {
1159 			/* If type is OPTIONAL, not an error:
1160 			 * indicate missing type.
1161 			 */
1162 			if (opt)
1163 				return -1;
1164 			asn1_tlc_clear(ctx);
1165 			ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1166 			return 0;
1167 		}
1168 		/* We have a tag and class match:
1169 		 * assume we are going to do something with it */
1170 		asn1_tlc_clear(ctx);
1171 	}
1172 
1173 	if (i & 1)
1174 		plen = len - (p - q);
1175 	if (inf)
1176 		*inf = i & 1;
1177 	if (cst)
1178 		*cst = i & V_ASN1_CONSTRUCTED;
1179 	if (olen)
1180 		*olen = plen;
1181 	if (oclass)
1182 		*oclass = pclass;
1183 	if (otag)
1184 		*otag = ptag;
1185 
1186 	*in = p;
1187 	return 1;
1188 }
1189