tasn_dec.c 28.9 KB
Newer Older
D
 
Dr. Stephen Henson 已提交
1 2 3 4 5
/* tasn_dec.c */
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
 * project 2000.
 */
/* ====================================================================
6
 * Copyright (c) 2000-2004 The OpenSSL Project.  All rights reserved.
D
 
Dr. Stephen Henson 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */


#include <stddef.h>
61
#include <string.h>
D
 
Dr. Stephen Henson 已提交
62 63 64 65 66 67
#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#include <openssl/objects.h>
#include <openssl/buffer.h>
#include <openssl/err.h>

68
static int asn1_check_eoc(const unsigned char **in, long len);
69 70 71 72

static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
				char inf, int tag, int aclass);

73
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
				char *inf, char *cst,
				const unsigned char **in, long len,
				int exptag, int expclass, char opt,
				ASN1_TLC *ctx);

static int asn1_template_ex_d2i(ASN1_VALUE **pval,
				const unsigned char **in, long len,
				const ASN1_TEMPLATE *tt, char opt,
				ASN1_TLC *ctx);
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
				const unsigned char **in, long len,
				const ASN1_TEMPLATE *tt, char opt,
				ASN1_TLC *ctx);
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
				const unsigned char **in, long len,
				const ASN1_ITEM *it,
				int tag, int aclass, char opt, ASN1_TLC *ctx);
D
 
Dr. Stephen Henson 已提交
93

D
 
Dr. Stephen Henson 已提交
94
/* Table to convert tags to bit values, used for MSTRING type */
95
static unsigned long tag2bit[32] = {
D
 
Dr. Stephen Henson 已提交
96 97 98 99 100 101 102 103 104 105 106 107
0,	0,	0,	B_ASN1_BIT_STRING,	/* tags  0 -  3 */
B_ASN1_OCTET_STRING,	0,	0,		B_ASN1_UNKNOWN,/* tags  4- 7 */
B_ASN1_UNKNOWN,	B_ASN1_UNKNOWN,	B_ASN1_UNKNOWN,	B_ASN1_UNKNOWN,/* tags  8-11 */
B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
0,	0,	B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING,   /* tags 16-19 */
B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING,       /* tags 20-22 */
B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,			       /* tags 23-24 */	
B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING,  /* tags 25-27 */
B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
	};

unsigned long ASN1_tag2bit(int tag)
108 109
	{
	if ((tag < 0) || (tag > 30)) return 0;
D
 
Dr. Stephen Henson 已提交
110
	return tag2bit[tag];
111
	}
D
 
Dr. Stephen Henson 已提交
112

D
 
Dr. Stephen Henson 已提交
113 114
/* Macro to initialize and invalidate the cache */

115
#define asn1_tlc_clear(c)	if (c) (c)->valid = 0
D
 
Dr. Stephen Henson 已提交
116 117 118 119 120 121 122 123 124

/* Decode an ASN1 item, this currently behaves just 
 * like a standard 'd2i' function. 'in' points to 
 * a buffer to read the data from, in future we will
 * have more advanced versions that can input data
 * a piece at a time and this will simply be a special
 * case.
 */

125 126 127
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
		const unsigned char **in, long len, const ASN1_ITEM *it)
	{
D
 
Dr. Stephen Henson 已提交
128 129
	ASN1_TLC c;
	ASN1_VALUE *ptmpval = NULL;
130 131
	if (!pval)
		pval = &ptmpval;
D
 
Dr. Stephen Henson 已提交
132
	asn1_tlc_clear(&c);
133
	if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) 
D
 
Dr. Stephen Henson 已提交
134 135
		return *pval;
	return NULL;
136
	}
D
 
Dr. Stephen Henson 已提交
137

138 139 140
int ASN1_template_d2i(ASN1_VALUE **pval,
		const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
	{
D
 
Dr. Stephen Henson 已提交
141 142 143
	ASN1_TLC c;
	asn1_tlc_clear(&c);
	return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
144
	}
D
 
Dr. Stephen Henson 已提交
145 146 147 148 149 150


/* Decode an item, taking care of IMPLICIT tagging, if any.
 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
 */

151 152 153 154
int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
			const ASN1_ITEM *it,
			int tag, int aclass, char opt, ASN1_TLC *ctx)
	{
D
 
Dr. Stephen Henson 已提交
155 156 157 158 159
	const ASN1_TEMPLATE *tt, *errtt = NULL;
	const ASN1_COMPAT_FUNCS *cf;
	const ASN1_EXTERN_FUNCS *ef;
	const ASN1_AUX *aux = it->funcs;
	ASN1_aux_cb *asn1_cb;
160 161 162
	const unsigned char *p, *q;
	unsigned char *wp=NULL;	/* BIG FAT WARNING!  BREAKS CONST WHERE USED */
	unsigned char imphack = 0, oclass;
D
 
Dr. Stephen Henson 已提交
163 164 165 166 167 168
	char seq_eoc, seq_nolen, cst, isopt;
	long tmplen;
	int i;
	int otag;
	int ret = 0;
	ASN1_VALUE *pchval, **pchptr, *ptmpval;
169 170 171 172
	if (!pval)
		return 0;
	if (aux && aux->asn1_cb)
		asn1_cb = aux->asn1_cb;
D
 
Dr. Stephen Henson 已提交
173 174
	else asn1_cb = 0;

175 176
	switch(it->itype)
		{
D
 
Dr. Stephen Henson 已提交
177
		case ASN1_ITYPE_PRIMITIVE:
178 179 180 181 182 183 184
		if (it->templates)
			{
			/* tagging or OPTIONAL is currently illegal on an item
			 * template because the flags can't get passed down.
			 * In practice this isn't a problem: we include the
			 * relevant flags from the item template in the
			 * template itself.
185
			 */
186 187 188 189
			if ((tag != -1) || opt)
				{
				ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
				ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
190
				goto err;
191 192 193
				}
			return asn1_template_ex_d2i(pval, in, len,
					it->templates, opt, ctx);
194
		}
195 196
		return asn1_d2i_ex_primitive(pval, in, len, it,
						tag, aclass, opt, ctx);
D
 
Dr. Stephen Henson 已提交
197 198 199 200 201
		break;

		case ASN1_ITYPE_MSTRING:
		p = *in;
		/* Just read in tag and class */
202 203 204 205 206 207
		ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
						&p, len, -1, 0, 1, ctx);
		if (!ret)
			{
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
					ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
208
			goto err;
209 210
			}

D
 
Dr. Stephen Henson 已提交
211
		/* Must be UNIVERSAL class */
212 213
		if (oclass != V_ASN1_UNIVERSAL)
			{
D
 
Dr. Stephen Henson 已提交
214
			/* If OPTIONAL, assume this is OK */
215 216 217
			if (opt) return -1;
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
					ASN1_R_MSTRING_NOT_UNIVERSAL);
D
 
Dr. Stephen Henson 已提交
218
			goto err;
219
			}
D
 
Dr. Stephen Henson 已提交
220
		/* Check tag matches bit map */
221 222
		if (!(ASN1_tag2bit(otag) & it->utype))
			{
D
 
Dr. Stephen Henson 已提交
223
			/* If OPTIONAL, assume this is OK */
224 225 226 227
			if (opt)
				return -1;
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
					ASN1_R_MSTRING_WRONG_TAG);
D
 
Dr. Stephen Henson 已提交
228
			goto err;
229 230 231
			}
		return asn1_d2i_ex_primitive(pval, in, len,
						it, otag, 0, 0, ctx);
D
 
Dr. Stephen Henson 已提交
232 233 234 235

		case ASN1_ITYPE_EXTERN:
		/* Use new style d2i */
		ef = it->funcs;
236 237
		return ef->asn1_ex_d2i(pval, in, len,
						it, tag, aclass, opt, ctx);
D
 
Dr. Stephen Henson 已提交
238 239 240 241 242 243

		case ASN1_ITYPE_COMPAT:
		/* we must resort to old style evil hackery */
		cf = it->funcs;

		/* If OPTIONAL see if it is there */
244 245
		if (opt)
			{
D
 
Dr. Stephen Henson 已提交
246 247
			int exptag;
			p = *in;
248 249
			if (tag == -1)
				exptag = it->utype;
D
 
Dr. Stephen Henson 已提交
250
			else exptag = tag;
251 252 253 254 255 256 257 258 259
			/* Don't care about anything other than presence
			 * of expected tag */

			ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
					&p, len, exptag, aclass, 1, ctx);
			if (!ret)
				{
				ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
					ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
260
				goto err;
261 262 263
				}
			if (ret == -1)
				return -1;
D
 
Dr. Stephen Henson 已提交
264
			}
265

D
 
Dr. Stephen Henson 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		/* This is the old style evil hack IMPLICIT handling:
		 * since the underlying code is expecting a tag and
		 * class other than the one present we change the
		 * buffer temporarily then change it back afterwards.
		 * This doesn't and never did work for tags > 30.
		 *
		 * Yes this is *horrible* but it is only needed for
		 * old style d2i which will hopefully not be around
		 * for much longer.
		 * FIXME: should copy the buffer then modify it so
		 * the input buffer can be const: we should *always*
		 * copy because the old style d2i might modify the
		 * buffer.
		 */

281 282
		if (tag != -1)
			{
283 284
			wp = *(unsigned char **)in;
			imphack = *wp;
285 286 287
			*wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
								| it->utype);
			}
D
 
Dr. Stephen Henson 已提交
288 289 290

		ptmpval = cf->asn1_d2i(pval, in, len);

291 292 293 294 295
		if (tag != -1)
			*wp = imphack;

		if (ptmpval)
			return 1;
D
 
Dr. Stephen Henson 已提交
296 297 298 299 300 301

		ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
		goto err;


		case ASN1_ITYPE_CHOICE:
302
		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
D
 
Dr. Stephen Henson 已提交
303
				goto auxerr;
304 305

		/* Allocate structure */
306 307 308 309 310
		if (!*pval && !ASN1_item_ex_new(pval, it))
			{
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
						ERR_R_NESTED_ASN1_ERROR);
			goto err;
311
			}
D
 
Dr. Stephen Henson 已提交
312 313 314
		/* CHOICE type, try each possibility in turn */
		pchval = NULL;
		p = *in;
315 316
		for(i = 0, tt=it->templates; i < it->tcount; i++, tt++)
			{
317
			pchptr = asn1_get_field_ptr(pval, tt);
D
 
Dr. Stephen Henson 已提交
318 319 320
			/* We mark field as OPTIONAL so its absence
			 * can be recognised.
			 */
321
			ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
D
 
Dr. Stephen Henson 已提交
322
			/* If field not present, try the next one */
323 324
			if (ret == -1)
				continue;
D
 
Dr. Stephen Henson 已提交
325
			/* If positive return, read OK, break loop */
326 327
			if (ret > 0)
				break;
D
 
Dr. Stephen Henson 已提交
328 329
			/* Otherwise must be an ASN1 parsing error */
			errtt = tt;
330 331
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
						ERR_R_NESTED_ASN1_ERROR);
332
			goto err;
333 334
			}

D
 
Dr. Stephen Henson 已提交
335
		/* Did we fall off the end without reading anything? */
336 337
		if (i == it->tcount)
			{
D
 
Dr. Stephen Henson 已提交
338
			/* If OPTIONAL, this is OK */
339 340
			if (opt)
				{
341 342 343
				/* Free and zero it */
				ASN1_item_ex_free(pval, it);
				return -1;
344 345 346
				}
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
					ASN1_R_NO_MATCHING_CHOICE_TYPE);
347
			goto err;
348 349
			}

D
 
Dr. Stephen Henson 已提交
350 351
		asn1_set_choice_selector(pval, i, it);
		*in = p;
352
		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
D
 
Dr. Stephen Henson 已提交
353 354 355
				goto auxerr;
		return 1;

356
		case ASN1_ITYPE_NDEF_SEQUENCE:
D
 
Dr. Stephen Henson 已提交
357 358 359 360 361
		case ASN1_ITYPE_SEQUENCE:
		p = *in;
		tmplen = len;

		/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
362 363
		if (tag == -1)
			{
D
 
Dr. Stephen Henson 已提交
364 365
			tag = V_ASN1_SEQUENCE;
			aclass = V_ASN1_UNIVERSAL;
366
			}
D
 
Dr. Stephen Henson 已提交
367
		/* Get SEQUENCE length and update len, p */
368 369 370 371 372 373
		ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
					&p, len, tag, aclass, opt, ctx);
		if (!ret)
			{
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
					ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
374
			goto err;
375 376 377 378 379
			}
		else if (ret == -1)
			return -1;
		if (aux && (aux->flags & ASN1_AFLG_BROKEN))
			{
D
 
Dr. Stephen Henson 已提交
380 381
			len = tmplen - (p - *in);
			seq_nolen = 1;
382 383 384 385 386 387 388
			}
		/* If indefinite we don't do a length check */
		else seq_nolen = seq_eoc;
		if (!cst)
			{
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
				ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
D
 
Dr. Stephen Henson 已提交
389
			goto err;
390
			}
D
 
Dr. Stephen Henson 已提交
391

392 393 394 395 396
		if (!*pval && !!ASN1_item_ex_new(pval, it))
			{
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
				ERR_R_NESTED_ASN1_ERROR);
			goto err;
D
 
Dr. Stephen Henson 已提交
397
			}
398 399

		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
D
 
Dr. Stephen Henson 已提交
400 401 402
				goto auxerr;

		/* Get each field entry */
403 404
		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
			{
D
 
Dr. Stephen Henson 已提交
405 406 407
			const ASN1_TEMPLATE *seqtt;
			ASN1_VALUE **pseqval;
			seqtt = asn1_do_adb(pval, tt, 1);
408 409
			if (!seqtt)
				goto err;
D
 
Dr. Stephen Henson 已提交
410 411
			pseqval = asn1_get_field_ptr(pval, seqtt);
			/* Have we ran out of data? */
412 413
			if (!len)
				break;
D
 
Dr. Stephen Henson 已提交
414
			q = p;
415 416 417 418 419 420
			if (asn1_check_eoc(&p, len))
				{
				if (!seq_eoc)
					{
					ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
							ASN1_R_UNEXPECTED_EOC);
D
 
Dr. Stephen Henson 已提交
421
					goto err;
422
					}
D
 
Dr. Stephen Henson 已提交
423 424 425 426
				len -= p - q;
				seq_eoc = 0;
				q = p;
				break;
427 428 429 430 431 432
				}
			/* This determines the OPTIONAL flag value. The field
			 * cannot be omitted if it is the last of a SEQUENCE
			 * and there is still data to be read. This isn't
			 * strictly necessary but it increases efficiency in
			 * some cases.
D
 
Dr. Stephen Henson 已提交
433
			 */
434 435
			if (i == (it->tcount - 1))
				isopt = 0;
436
			else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
437 438 439 440 441 442 443
			/* attempt to read in field, allowing each to be
			 * OPTIONAL */

			ret = asn1_template_ex_d2i(pseqval, &p, len,
							seqtt, isopt, ctx);
			if (!ret)
				{
D
 
Dr. Stephen Henson 已提交
444 445
				errtt = seqtt;
				goto err;
446 447 448 449 450
				}
			else if (ret == -1)
				{
				/* OPTIONAL component absent.
				 * Free and zero the field.
D
 
Dr. Stephen Henson 已提交
451 452 453
				 */
				ASN1_template_free(pseqval, seqtt);
				continue;
454
				}
D
 
Dr. Stephen Henson 已提交
455 456
			/* Update length */
			len -= p - q;
457 458
			}

D
 
Dr. Stephen Henson 已提交
459
		/* Check for EOC if expecting one */
460 461
		if (seq_eoc && !asn1_check_eoc(&p, len))
			{
D
 
Dr. Stephen Henson 已提交
462 463
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
			goto err;
464
			}
D
 
Dr. Stephen Henson 已提交
465
		/* Check all data read */
466 467 468 469
		if (!seq_nolen && len)
			{
			ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
					ASN1_R_SEQUENCE_LENGTH_MISMATCH);
D
 
Dr. Stephen Henson 已提交
470
			goto err;
471
			}
D
 
Dr. Stephen Henson 已提交
472 473 474 475 476

		/* If we get here we've got no more data in the SEQUENCE,
		 * however we may not have read all fields so check all
		 * remaining are OPTIONAL and clear any that are.
		 */
477 478
		for (; i < it->tcount; tt++, i++)
			{
D
 
Dr. Stephen Henson 已提交
479 480
			const ASN1_TEMPLATE *seqtt;
			seqtt = asn1_do_adb(pval, tt, 1);
481 482 483 484
			if (!seqtt)
				goto err;
			if (seqtt->flags & ASN1_TFLG_OPTIONAL)
				{
D
 
Dr. Stephen Henson 已提交
485 486 487
				ASN1_VALUE **pseqval;
				pseqval = asn1_get_field_ptr(pval, seqtt);
				ASN1_template_free(pseqval, seqtt);
488 489 490
				}
			else
				{
D
 
Dr. Stephen Henson 已提交
491
				errtt = seqtt;
492 493
				ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
							ASN1_R_FIELD_MISSING);
D
 
Dr. Stephen Henson 已提交
494
				goto err;
495
				}
D
 
Dr. Stephen Henson 已提交
496 497
			}
		/* Save encoding */
498 499
		if (!asn1_enc_save(pval, *in, p - *in, it))
			goto auxerr;
D
 
Dr. Stephen Henson 已提交
500
		*in = p;
501
		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
D
 
Dr. Stephen Henson 已提交
502 503 504 505 506
				goto auxerr;
		return 1;

		default:
		return 0;
507
		}
D
 
Dr. Stephen Henson 已提交
508 509 510 511
	auxerr:
	ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
	err:
	ASN1_item_ex_free(pval, it);
512 513 514 515 516
	if (errtt)
		ERR_add_error_data(4, "Field=", errtt->field_name,
					", Type=", it->sname);
	else
		ERR_add_error_data(2, "Type=", it->sname);
D
 
Dr. Stephen Henson 已提交
517
	return 0;
518
	}
D
 
Dr. Stephen Henson 已提交
519

520 521
/* Templates are handled with two separate functions.
 * One handles any EXPLICIT tag and the other handles the rest.
D
 
Dr. Stephen Henson 已提交
522 523
 */

524 525 526 527 528
static int asn1_template_ex_d2i(ASN1_VALUE **val,
				const unsigned char **in, long inlen,
				const ASN1_TEMPLATE *tt, char opt,
							ASN1_TLC *ctx)
	{
D
 
Dr. Stephen Henson 已提交
529 530 531
	int flags, aclass;
	int ret;
	long len;
532
	const unsigned char *p, *q;
D
 
Dr. Stephen Henson 已提交
533
	char exp_eoc;
534 535
	if (!val)
		return 0;
D
 
Dr. Stephen Henson 已提交
536 537 538 539 540 541
	flags = tt->flags;
	aclass = flags & ASN1_TFLG_TAG_CLASS;

	p = *in;

	/* Check if EXPLICIT tag expected */
542 543
	if (flags & ASN1_TFLG_EXPTAG)
		{
D
 
Dr. Stephen Henson 已提交
544
		char cst;
545 546 547
		/* Need to work out amount of data available to the inner
		 * content and where it starts: so read in EXPLICIT header to
		 * get the info.
D
 
Dr. Stephen Henson 已提交
548
		 */
549 550
		ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
					&p, inlen, tt->tag, aclass, opt, ctx);
D
 
Dr. Stephen Henson 已提交
551
		q = p;
552 553 554 555
		if (!ret)
			{
			ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
					ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
556
			return 0;
557 558 559 560 561 562 563
			}
		else if (ret == -1)
			return -1;
		if (!cst)
			{
			ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
					ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
D
 
Dr. Stephen Henson 已提交
564
			return 0;
565
			}
D
 
Dr. Stephen Henson 已提交
566 567
		/* We've found the field so it can't be OPTIONAL now */
		ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
568 569 570 571
		if (!ret)
			{
			ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
					ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
572
			return 0;
573
			}
D
 
Dr. Stephen Henson 已提交
574 575
		/* We read the field in OK so update length */
		len -= p - q;
576 577
		if (exp_eoc)
			{
D
 
Dr. Stephen Henson 已提交
578
			/* If NDEF we must have an EOC here */
579 580 581 582
			if (!asn1_check_eoc(&p, len))
				{
				ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
						ASN1_R_MISSING_EOC);
D
 
Dr. Stephen Henson 已提交
583
				goto err;
584
				}
D
 
Dr. Stephen Henson 已提交
585
			}
586 587 588 589 590 591 592 593
		else
			{
			/* Otherwise we must hit the EXPLICIT tag end or its
			 * an error */
			if (len)
				{
				ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
					ASN1_R_EXPLICIT_LENGTH_MISMATCH);
D
 
Dr. Stephen Henson 已提交
594
				goto err;
595
				}
D
 
Dr. Stephen Henson 已提交
596 597
			}
		}
598 599 600
		else 
			return asn1_template_noexp_d2i(val, in, inlen,
								tt, opt, ctx);
D
 
Dr. Stephen Henson 已提交
601 602 603 604 605 606 607 608

	*in = p;
	return 1;

	err:
	ASN1_template_free(val, tt);
	*val = NULL;
	return 0;
609
	}
D
 
Dr. Stephen Henson 已提交
610

611 612 613 614 615
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
				const unsigned char **in, long len,
				const ASN1_TEMPLATE *tt, char opt,
				ASN1_TLC *ctx)
	{
D
 
Dr. Stephen Henson 已提交
616 617
	int flags, aclass;
	int ret;
618
	const unsigned char *p, *q;
619 620
	if (!val)
		return 0;
D
 
Dr. Stephen Henson 已提交
621 622 623 624 625 626
	flags = tt->flags;
	aclass = flags & ASN1_TFLG_TAG_CLASS;

	p = *in;
	q = p;

627 628
	if (flags & ASN1_TFLG_SK_MASK)
		{
D
 
Dr. Stephen Henson 已提交
629 630 631 632
		/* SET OF, SEQUENCE OF */
		int sktag, skaclass;
		char sk_eoc;
		/* First work out expected inner tag value */
633 634
		if (flags & ASN1_TFLG_IMPTAG)
			{
D
 
Dr. Stephen Henson 已提交
635 636
			sktag = tt->tag;
			skaclass = aclass;
637 638 639
			}
		else
			{
D
 
Dr. Stephen Henson 已提交
640
			skaclass = V_ASN1_UNIVERSAL;
641 642 643 644 645
			if (flags & ASN1_TFLG_SET_OF)
				sktag = V_ASN1_SET;
			else
				sktag = V_ASN1_SEQUENCE;
			}
D
 
Dr. Stephen Henson 已提交
646
		/* Get the tag */
647 648 649 650 651 652
		ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
					&p, len, sktag, skaclass, opt, ctx);
		if (!ret)
			{
			ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
						ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
653
			return 0;
654 655 656 657 658 659 660
			}
		else if (ret == -1)
			return -1;
		if (!*val)
			*val = (ASN1_VALUE *)sk_new_null();
		else
			{
D
 
Dr. Stephen Henson 已提交
661 662 663
			/* We've got a valid STACK: free up any items present */
			STACK *sktmp = (STACK *)*val;
			ASN1_VALUE *vtmp;
664 665
			while(sk_num(sktmp) > 0)
				{
D
 
Dr. Stephen Henson 已提交
666
				vtmp = (ASN1_VALUE *)sk_pop(sktmp);
667 668 669
				ASN1_item_ex_free(&vtmp,
						ASN1_ITEM_ptr(tt->item));
				}
D
 
Dr. Stephen Henson 已提交
670 671
			}
				
672 673 674 675
		if (!*val)
			{
			ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
						ERR_R_MALLOC_FAILURE);
D
 
Dr. Stephen Henson 已提交
676
			goto err;
677 678
			}

D
 
Dr. Stephen Henson 已提交
679
		/* Read as many items as we can */
680 681
		while(len > 0)
			{
D
 
Dr. Stephen Henson 已提交
682 683 684
			ASN1_VALUE *skfield;
			q = p;
			/* See if EOC found */
685 686 687 688 689 690
			if (asn1_check_eoc(&p, len))
				{
				if (!sk_eoc)
					{
					ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
							ASN1_R_UNEXPECTED_EOC);
D
 
Dr. Stephen Henson 已提交
691
					goto err;
692
					}
D
 
Dr. Stephen Henson 已提交
693 694 695
				len -= p - q;
				sk_eoc = 0;
				break;
696
				}
D
 
Dr. Stephen Henson 已提交
697
			skfield = NULL;
698 699 700 701 702 703
			if (!ASN1_item_ex_d2i(&skfield, &p, len,
						ASN1_ITEM_ptr(tt->item),
						-1, 0, 0, ctx))
				{
				ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
					ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
704
				goto err;
705
				}
D
 
Dr. Stephen Henson 已提交
706
			len -= p - q;
707 708 709 710
			if (!sk_push((STACK *)*val, (char *)skfield))
				{
				ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
						ERR_R_MALLOC_FAILURE);
D
 
Dr. Stephen Henson 已提交
711
				goto err;
712
				}
D
 
Dr. Stephen Henson 已提交
713
			}
714 715
		if (sk_eoc)
			{
D
 
Dr. Stephen Henson 已提交
716 717
			ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_MISSING_EOC);
			goto err;
718
			}
D
 
Dr. Stephen Henson 已提交
719
		}
720 721
	else if (flags & ASN1_TFLG_IMPTAG)
		{
D
 
Dr. Stephen Henson 已提交
722
		/* IMPLICIT tagging */
723 724 725 726 727 728
		ret = ASN1_item_ex_d2i(val, &p, len,
			ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
		if (!ret)
			{
			ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
						ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
729
			goto err;
730 731 732 733 734 735
			}
		else if (ret == -1)
			return -1;
		}
	else
		{
D
 
Dr. Stephen Henson 已提交
736
		/* Nothing special */
737 738 739 740 741 742
		ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
							-1, 0, opt, ctx);
		if (!ret)
			{
			ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
					ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
743
			goto err;
744 745 746 747
			}
		else if (ret == -1)
			return -1;
		}
D
 
Dr. Stephen Henson 已提交
748 749 750 751 752 753 754 755

	*in = p;
	return 1;

	err:
	ASN1_template_free(val, tt);
	*val = NULL;
	return 0;
756
	}
D
 
Dr. Stephen Henson 已提交
757

758 759 760 761 762
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
				const unsigned char **in, long inlen, 
				const ASN1_ITEM *it,
				int tag, int aclass, char opt, ASN1_TLC *ctx)
	{
D
 
Dr. Stephen Henson 已提交
763 764 765
	int ret = 0, utype;
	long plen;
	char cst, inf, free_cont = 0;
766
	const unsigned char *p;
D
 
Dr. Stephen Henson 已提交
767
	BUF_MEM buf;
768
	const unsigned char *cont = NULL;
D
 
Dr. Stephen Henson 已提交
769
	long len; 
770 771
	if (!pval)
		{
D
 
Dr. Stephen Henson 已提交
772 773
		ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
		return 0; /* Should never happen */
774
		}
D
 
Dr. Stephen Henson 已提交
775

776 777
	if (it->itype == ASN1_ITYPE_MSTRING)
		{
D
 
Dr. Stephen Henson 已提交
778 779
		utype = tag;
		tag = -1;
780 781 782
		}
	else
		utype = it->utype;
D
 
Dr. Stephen Henson 已提交
783

784 785
	if (utype == V_ASN1_ANY)
		{
D
 
Dr. Stephen Henson 已提交
786 787
		/* If type is ANY need to figure out type from tag */
		unsigned char oclass;
788 789 790 791
		if (tag >= 0)
			{
			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
					ASN1_R_ILLEGAL_TAGGED_ANY);
D
 
Dr. Stephen Henson 已提交
792
			return 0;
793 794 795 796 797
			}
		if (opt)
			{
			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
					ASN1_R_ILLEGAL_OPTIONAL_ANY);
D
 
Dr. Stephen Henson 已提交
798
			return 0;
799
			}
D
 
Dr. Stephen Henson 已提交
800
		p = *in;
801 802 803 804 805 806
		ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
					&p, inlen, -1, 0, 0, ctx);
		if (!ret)
			{
			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
					ERR_R_NESTED_ASN1_ERROR);
D
 
Dr. Stephen Henson 已提交
807
			return 0;
808 809 810
			}
		if (oclass != V_ASN1_UNIVERSAL)
			utype = V_ASN1_OTHER;
D
 
Dr. Stephen Henson 已提交
811
		}
812 813
	if (tag == -1)
		{
D
 
Dr. Stephen Henson 已提交
814 815
		tag = utype;
		aclass = V_ASN1_UNIVERSAL;
816
		}
D
 
Dr. Stephen Henson 已提交
817 818
	p = *in;
	/* Check header */
819 820 821 822
	ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
				&p, inlen, tag, aclass, opt, ctx);
	if (!ret)
		{
D
 
Dr. Stephen Henson 已提交
823 824
		ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
		return 0;
825 826 827
		}
	else if (ret == -1)
		return -1;
D
 
Dr. Stephen Henson 已提交
828
	/* SEQUENCE, SET and "OTHER" are left in encoded form */
829 830 831 832 833
	if ((utype == V_ASN1_SEQUENCE)
		|| (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
		{
		/* Clear context cache for type OTHER because the auto clear
		 * when we have a exact match wont work
D
 
Dr. Stephen Henson 已提交
834
		 */
835 836
		if (utype == V_ASN1_OTHER)
			{
D
 
Dr. Stephen Henson 已提交
837
			asn1_tlc_clear(ctx);
838
			}
D
 
Dr. Stephen Henson 已提交
839
		/* SEQUENCE and SET must be constructed */
840 841 842 843
		else if (!cst)
			{
			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
				ASN1_R_TYPE_NOT_CONSTRUCTED);
D
 
Dr. Stephen Henson 已提交
844
			return 0;
845
			}
D
 
Dr. Stephen Henson 已提交
846 847 848

		cont = *in;
		/* If indefinite length constructed find the real end */
849 850 851 852
		if (inf)
			{
			if (!asn1_collect(NULL, &p, plen, inf, -1, -1))
				 goto err;
D
 
Dr. Stephen Henson 已提交
853
			len = p - cont;
854 855 856
			}
		else
			{
D
 
Dr. Stephen Henson 已提交
857 858 859
			len = p - cont + plen;
			p += plen;
			buf.data = NULL;
860
			}
D
 
Dr. Stephen Henson 已提交
861
		}
862 863
	else if (cst)
		{
D
 
Dr. Stephen Henson 已提交
864 865 866 867 868 869 870 871 872
		buf.length = 0;
		buf.max = 0;
		buf.data = NULL;
		/* Should really check the internal tags are correct but
		 * some things may get this wrong. The relevant specs
		 * say that constructed string types should be OCTET STRINGs
		 * internally irrespective of the type. So instead just check
		 * for UNIVERSAL class and ignore the tag.
		 */
873 874
		if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL))
			goto err;
D
 
Dr. Stephen Henson 已提交
875
		len = buf.length;
D
Dr. Stephen Henson 已提交
876
		/* Append a final null to string */
877 878 879 880
		if (!BUF_MEM_grow_clean(&buf, len + 1))
			{
			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
						ERR_R_MALLOC_FAILURE);
D
Dr. Stephen Henson 已提交
881
			return 0;
882
			}
D
Dr. Stephen Henson 已提交
883
		buf.data[len] = 0;
884
		cont = (const unsigned char *)buf.data;
D
 
Dr. Stephen Henson 已提交
885
		free_cont = 1;
886 887 888
		}
	else
		{
D
 
Dr. Stephen Henson 已提交
889 890 891
		cont = p;
		len = plen;
		p += plen;
892
		}
D
 
Dr. Stephen Henson 已提交
893 894

	/* We now have content length and type: translate into a structure */
895 896
	if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
		goto err;
D
 
Dr. Stephen Henson 已提交
897 898 899 900

	*in = p;
	ret = 1;
	err:
901
	if (free_cont && buf.data) OPENSSL_free(buf.data);
D
 
Dr. Stephen Henson 已提交
902
	return ret;
903
	}
D
 
Dr. Stephen Henson 已提交
904 905 906

/* Translate ASN1 content octets into a structure */

907 908 909
int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
			int utype, char *free_cont, const ASN1_ITEM *it)
	{
910
	ASN1_VALUE **opval = NULL;
D
 
Dr. Stephen Henson 已提交
911 912 913 914
	ASN1_STRING *stmp;
	ASN1_TYPE *typ = NULL;
	int ret = 0;
	const ASN1_PRIMITIVE_FUNCS *pf;
915
	ASN1_INTEGER **tint;
D
 
Dr. Stephen Henson 已提交
916
	pf = it->funcs;
917 918 919

	if (pf && pf->prim_c2i)
		return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
D
 
Dr. Stephen Henson 已提交
920
	/* If ANY type clear type and set pointer to internal value */
921 922 923 924
	if (it->utype == V_ASN1_ANY)
		{
		if (!*pval)
			{
D
 
Dr. Stephen Henson 已提交
925 926
			typ = ASN1_TYPE_new();
			*pval = (ASN1_VALUE *)typ;
927 928 929 930 931 932
			}
		else
			typ = (ASN1_TYPE *)*pval;

		if (utype != typ->type)
			ASN1_TYPE_set(typ, utype, NULL);
933
		opval = pval;
D
 
Dr. Stephen Henson 已提交
934
		pval = (ASN1_VALUE **)&typ->value.ptr;
935 936 937
		}
	switch(utype)
		{
D
 
Dr. Stephen Henson 已提交
938
		case V_ASN1_OBJECT:
939 940
		if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
			goto err;
D
 
Dr. Stephen Henson 已提交
941 942 943
		break;

		case V_ASN1_NULL:
944 945 946 947
		if (len)
			{
			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
						ASN1_R_NULL_IS_WRONG_LENGTH);
D
 
Dr. Stephen Henson 已提交
948
			goto err;
949
			}
D
 
Dr. Stephen Henson 已提交
950 951 952 953
		*pval = (ASN1_VALUE *)1;
		break;

		case V_ASN1_BOOLEAN:
954 955 956 957
		if (len != 1)
			{
			ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
						ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
D
 
Dr. Stephen Henson 已提交
958
			goto err;
959 960 961
			}
		else
			{
D
 
Dr. Stephen Henson 已提交
962 963 964
			ASN1_BOOLEAN *tbool;
			tbool = (ASN1_BOOLEAN *)pval;
			*tbool = *cont;
965
			}
D
 
Dr. Stephen Henson 已提交
966 967 968
		break;

		case V_ASN1_BIT_STRING:
969 970
		if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
			goto err;
D
 
Dr. Stephen Henson 已提交
971 972 973 974 975 976
		break;

		case V_ASN1_INTEGER:
		case V_ASN1_NEG_INTEGER:
		case V_ASN1_ENUMERATED:
		case V_ASN1_NEG_ENUMERATED:
977
		tint = (ASN1_INTEGER **)pval;
978 979
		if (!c2i_ASN1_INTEGER(tint, &cont, len))
			goto err;
980 981
		/* Fixup type to match the expected form */
		(*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
D
 
Dr. Stephen Henson 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
		break;

		case V_ASN1_OCTET_STRING:
		case V_ASN1_NUMERICSTRING:
		case V_ASN1_PRINTABLESTRING:
		case V_ASN1_T61STRING:
		case V_ASN1_VIDEOTEXSTRING:
		case V_ASN1_IA5STRING:
		case V_ASN1_UTCTIME:
		case V_ASN1_GENERALIZEDTIME:
		case V_ASN1_GRAPHICSTRING:
		case V_ASN1_VISIBLESTRING:
		case V_ASN1_GENERALSTRING:
		case V_ASN1_UNIVERSALSTRING:
		case V_ASN1_BMPSTRING:
		case V_ASN1_UTF8STRING:
		case V_ASN1_OTHER:
		case V_ASN1_SET:
		case V_ASN1_SEQUENCE:
		default:
		/* All based on ASN1_STRING and handled the same */
1003 1004
		if (!*pval)
			{
D
 
Dr. Stephen Henson 已提交
1005
			stmp = ASN1_STRING_type_new(utype);
1006 1007 1008 1009
			if (!stmp)
				{
				ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
							ERR_R_MALLOC_FAILURE);
D
 
Dr. Stephen Henson 已提交
1010
				goto err;
1011
				}
D
 
Dr. Stephen Henson 已提交
1012
			*pval = (ASN1_VALUE *)stmp;
1013 1014 1015
			}
		else
			{
D
 
Dr. Stephen Henson 已提交
1016 1017
			stmp = (ASN1_STRING *)*pval;
			stmp->type = utype;
1018
			}
D
 
Dr. Stephen Henson 已提交
1019
		/* If we've already allocated a buffer use it */
1020 1021 1022 1023
		if (*free_cont)
			{
			if (stmp->data)
				OPENSSL_free(stmp->data);
1024
			stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
D
 
Dr. Stephen Henson 已提交
1025 1026
			stmp->length = len;
			*free_cont = 0;
1027 1028 1029 1030 1031 1032 1033
			}
		else
			{
			if (!ASN1_STRING_set(stmp, cont, len))
				{
				ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
							ERR_R_MALLOC_FAILURE);
D
 
Dr. Stephen Henson 已提交
1034 1035 1036
				ASN1_STRING_free(stmp);	
				*pval = NULL;
				goto err;
1037
				}
D
 
Dr. Stephen Henson 已提交
1038 1039
			}
		break;
1040
		}
D
 
Dr. Stephen Henson 已提交
1041
	/* If ASN1_ANY and NULL type fix up value */
1042 1043
	if (typ && (utype == V_ASN1_NULL))
		 typ->value.ptr = NULL;
D
 
Dr. Stephen Henson 已提交
1044 1045 1046

	ret = 1;
	err:
1047
	if (!ret)
1048 1049 1050 1051 1052
		{
		ASN1_TYPE_free(typ);
		if (opval)
			*opval = NULL;
		}
D
 
Dr. Stephen Henson 已提交
1053
	return ret;
1054
	}
D
 
Dr. Stephen Henson 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063

/* This function collects the asn1 data from a constructred string
 * type into a buffer. The values of 'in' and 'len' should refer
 * to the contents of the constructed type and 'inf' should be set
 * if it is indefinite length. If 'buf' is NULL then we just want
 * to find the end of the current structure: useful for indefinite
 * length constructed stuff.
 */

1064 1065 1066
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
				char inf, int tag, int aclass)
	{
1067
	const unsigned char *p, *q;
D
 
Dr. Stephen Henson 已提交
1068 1069 1070 1071
	long plen;
	char cst, ininf;
	p = *in;
	inf &= 1;
1072 1073 1074 1075
	/* If no buffer and not indefinite length constructed just pass over
	 * the encoded data */
	if (!buf && !inf)
		{
D
 
Dr. Stephen Henson 已提交
1076 1077
		*in += len;
		return 1;
1078 1079 1080
		}
	while(len > 0)
		{
D
 
Dr. Stephen Henson 已提交
1081 1082
		q = p;
		/* Check for EOC */
1083 1084 1085 1086 1087 1088 1089 1090
		if (asn1_check_eoc(&p, len))
			{
			/* EOC is illegal outside indefinite length
			 * constructed form */
			if (!inf)
				{
				ASN1err(ASN1_F_ASN1_COLLECT,
					ASN1_R_UNEXPECTED_EOC);
D
 
Dr. Stephen Henson 已提交
1091
				return 0;
1092
				}
D
 
Dr. Stephen Henson 已提交
1093 1094
			inf = 0;
			break;
1095 1096 1097 1098 1099
			}

		if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
					len, tag, aclass, 0, NULL))
			{
D
 
Dr. Stephen Henson 已提交
1100 1101
			ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
			return 0;
1102 1103
			}

D
 
Dr. Stephen Henson 已提交
1104
		/* If indefinite length constructed update max length */
1105 1106 1107 1108 1109 1110 1111
		if (cst)
			{
			if (!asn1_collect(buf, &p, plen, ininf, tag, aclass))
				return 0;
			}
		else if (!collect_data(buf, &p, plen))
			return 0;
D
 
Dr. Stephen Henson 已提交
1112
		len -= p - q;
1113 1114 1115
		}
	if (inf)
		{
D
 
Dr. Stephen Henson 已提交
1116 1117
		ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
		return 0;
1118
		}
D
 
Dr. Stephen Henson 已提交
1119 1120
	*in = p;
	return 1;
1121
	}
D
 
Dr. Stephen Henson 已提交
1122

1123
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1124 1125 1126 1127 1128 1129 1130 1131 1132
	{
	int len;
	if (buf)
		{
		len = buf->length;
		if (!BUF_MEM_grow_clean(buf, len + plen))
			{
			ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
			return 0;
D
 
Dr. Stephen Henson 已提交
1133
			}
1134
		memcpy(buf->data + len, *p, plen);
D
 
Dr. Stephen Henson 已提交
1135
		}
1136 1137 1138
	*p += plen;
	return 1;
	}
D
 
Dr. Stephen Henson 已提交
1139 1140 1141

/* Check for ASN1 EOC and swallow it if found */

1142
static int asn1_check_eoc(const unsigned char **in, long len)
1143
	{
1144
	const unsigned char *p;
1145
	if (len < 2) return 0;
D
 
Dr. Stephen Henson 已提交
1146
	p = *in;
1147 1148
	if (!p[0] && !p[1])
		{
D
 
Dr. Stephen Henson 已提交
1149 1150
		*in += 2;
		return 1;
1151
		}
D
 
Dr. Stephen Henson 已提交
1152
	return 0;
1153
	}
D
 
Dr. Stephen Henson 已提交
1154 1155 1156 1157 1158 1159 1160 1161

/* Check an ASN1 tag and length: a bit like ASN1_get_object
 * but it sets the length for indefinite length constructed
 * form, we don't know the exact length but we can set an
 * upper bound to the amount of data available minus the
 * header length just read.
 */

1162 1163 1164 1165 1166 1167
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
				char *inf, char *cst,
				const unsigned char **in, long len,
				int exptag, int expclass, char opt,
				ASN1_TLC *ctx)
	{
D
 
Dr. Stephen Henson 已提交
1168 1169 1170
	int i;
	int ptag, pclass;
	long plen;
1171
	const unsigned char *p, *q;
D
 
Dr. Stephen Henson 已提交
1172 1173 1174
	p = *in;
	q = p;

1175 1176
	if (ctx && ctx->valid)
		{
D
 
Dr. Stephen Henson 已提交
1177 1178 1179 1180 1181
		i = ctx->ret;
		plen = ctx->plen;
		pclass = ctx->pclass;
		ptag = ctx->ptag;
		p += ctx->hdrlen;
1182 1183 1184
		}
	else
		{
D
 
Dr. Stephen Henson 已提交
1185
		i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1186 1187
		if (ctx)
			{
D
 
Dr. Stephen Henson 已提交
1188 1189 1190 1191 1192 1193
			ctx->ret = i;
			ctx->plen = plen;
			ctx->pclass = pclass;
			ctx->ptag = ptag;
			ctx->hdrlen = p - q;
			ctx->valid = 1;
D
 
Dr. Stephen Henson 已提交
1194 1195
			/* If definite length, and no error, length +
			 * header can't exceed total amount of data available. 
D
 
Dr. Stephen Henson 已提交
1196
			 */
1197 1198 1199 1200
			if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
				{
				ASN1err(ASN1_F_ASN1_CHECK_TLEN,
							ASN1_R_TOO_LONG);
D
 
Dr. Stephen Henson 已提交
1201 1202
				asn1_tlc_clear(ctx);
				return 0;
1203
				}
D
 
Dr. Stephen Henson 已提交
1204
			}
D
 
Dr. Stephen Henson 已提交
1205
		}
D
 
Dr. Stephen Henson 已提交
1206

1207 1208
	if (i & 0x80)
		{
D
 
Dr. Stephen Henson 已提交
1209 1210 1211
		ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
		asn1_tlc_clear(ctx);
		return 0;
1212 1213 1214 1215 1216 1217 1218
		}
	if (exptag >= 0)
		{
		if ((exptag != ptag) || (expclass != pclass))
			{
			/* If type is OPTIONAL, not an error:
			 * indicate missing type.
D
 
Dr. Stephen Henson 已提交
1219
			 */
1220
			if (opt) return -1;
D
 
Dr. Stephen Henson 已提交
1221 1222 1223
			asn1_tlc_clear(ctx);
			ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
			return 0;
1224 1225 1226
			}
		/* We have a tag and class match:
		 * assume we are going to do something with it */
D
 
Dr. Stephen Henson 已提交
1227
		asn1_tlc_clear(ctx);
1228
		}
D
 
Dr. Stephen Henson 已提交
1229

1230 1231
	if (i & 1)
		plen = len - (p - q);
D
 
Dr. Stephen Henson 已提交
1232

1233 1234
	if (inf)
		*inf = i & 1;
D
 
Dr. Stephen Henson 已提交
1235

1236 1237
	if (cst)
		*cst = i & V_ASN1_CONSTRUCTED;
D
 
Dr. Stephen Henson 已提交
1238

1239 1240 1241 1242 1243 1244 1245 1246
	if (olen)
		*olen = plen;

	if (oclass)
		*oclass = pclass;

	if (otag)
		*otag = ptag;
D
 
Dr. Stephen Henson 已提交
1247 1248 1249

	*in = p;
	return 1;
1250
	}