GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/asn1/tasn_dec.c Lines: 344 553 62.2 %
Date: 2017-11-13 Branches: 224 386 58.0 %

Line Branch Exec Source
1
/* $OpenBSD: tasn_dec.c,v 1.34 2017/01/29 17:49:22 beck 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
466640
	if ((tag < 0) || (tag > 30))
105
		return 0;
106
233320
	return tag2bit[tag];
107
233320
}
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
48118
	ASN1_TLC c;
128
24059
	ASN1_VALUE *ptmpval = NULL;
129
130
24059
	if (!pval)
131
12302
		pval = &ptmpval;
132
24059
	asn1_tlc_clear_nc(&c);
133
24059
	if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
134
23827
		return *pval;
135
232
	return NULL;
136
24059
}
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
922250
	const ASN1_AUX *aux = it->funcs;
160
	ASN1_aux_cb *asn1_cb;
161
922250
	const unsigned char *p = NULL, *q;
162
922250
	unsigned char oclass;
163
922250
	char seq_eoc, seq_nolen, cst, isopt;
164
	long tmplen;
165
	int i;
166
922250
	int otag;
167
	int ret = 0;
168
	ASN1_VALUE **pchptr;
169
	int combine;
170
171
922250
	combine = aclass & ASN1_TFLG_COMBINE;
172
922250
	aclass &= ~ASN1_TFLG_COMBINE;
173
174
922250
	if (!pval)
175
		return 0;
176
177

1006762
	if (aux && aux->asn1_cb)
178
45053
		asn1_cb = aux->asn1_cb;
179
	else
180
		asn1_cb = 0;
181
182

922250
	switch (it->itype) {
183
	case ASN1_ITYPE_PRIMITIVE:
184
519544
		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

259638
			if ((tag != -1) || opt) {
192
				ASN1error(ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
193
				goto err;
194
			}
195
129819
			return asn1_template_ex_d2i(pval, in, len,
196
			    it->templates, opt, ctx);
197
		}
198
389725
		return asn1_d2i_ex_primitive(pval, in, len, it,
199
		    tag, aclass, opt, ctx);
200
		break;
201
202
	case ASN1_ITYPE_MSTRING:
203
129756
		p = *in;
204
		/* Just read in tag and class */
205
129756
		ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
206
129756
		    &p, len, -1, 0, 1, ctx);
207
129756
		if (!ret) {
208
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
209
			goto err;
210
		}
211
212
		/* Must be UNIVERSAL class */
213
129756
		if (oclass != V_ASN1_UNIVERSAL) {
214
			/* If OPTIONAL, assume this is OK */
215
			if (opt)
216
				return -1;
217
			ASN1error(ASN1_R_MSTRING_NOT_UNIVERSAL);
218
			goto err;
219
		}
220
		/* Check tag matches bit map */
221
129756
		if (!(ASN1_tag2bit(otag) & it->utype)) {
222
			/* If OPTIONAL, assume this is OK */
223
			if (opt)
224
				return -1;
225
			ASN1error(ASN1_R_MSTRING_WRONG_TAG);
226
			goto err;
227
		}
228
259512
		return asn1_d2i_ex_primitive(pval, in, len,
229
129756
		    it, otag, 0, 0, ctx);
230
231
	case ASN1_ITYPE_EXTERN:
232
		/* Use new style d2i */
233
26363
		ef = it->funcs;
234
26363
		return ef->asn1_ex_d2i(pval, in, len,
235
		    it, tag, aclass, opt, ctx);
236
237
	case ASN1_ITYPE_CHOICE:
238

340
		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
239
			goto auxerr;
240
241
340
		if (*pval) {
242
			/* Free up and zero CHOICE value if initialised */
243
82
			i = asn1_get_choice_selector(pval, it);
244

82
			if ((i >= 0) && (i < it->tcount)) {
245
				tt = it->templates + i;
246
				pchptr = asn1_get_field_ptr(pval, tt);
247
				ASN1_template_free(pchptr, tt);
248
				asn1_set_choice_selector(pval, -1, it);
249
			}
250
258
		} else if (!ASN1_item_ex_new(pval, it)) {
251
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
252
			goto err;
253
		}
254
		/* CHOICE type, try each possibility in turn */
255
340
		p = *in;
256
1416
		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
257
708
			pchptr = asn1_get_field_ptr(pval, tt);
258
			/* We mark field as OPTIONAL so its absence
259
			 * can be recognised.
260
			 */
261
708
			ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
262
			/* If field not present, try the next one */
263
708
			if (ret == -1)
264
				continue;
265
			/* If positive return, read OK, break loop */
266
340
			if (ret > 0)
267
				break;
268
			/* Otherwise must be an ASN1 parsing error */
269
			errtt = tt;
270
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
271
			goto err;
272
		}
273
274
		/* Did we fall off the end without reading anything? */
275
340
		if (i == it->tcount) {
276
			/* If OPTIONAL, this is OK */
277
			if (opt) {
278
				/* Free and zero it */
279
				ASN1_item_ex_free(pval, it);
280
				return -1;
281
			}
282
			ASN1error(ASN1_R_NO_MATCHING_CHOICE_TYPE);
283
			goto err;
284
		}
285
286
340
		asn1_set_choice_selector(pval, i, it);
287
340
		*in = p;
288

340
		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
289
			goto auxerr;
290
340
		return 1;
291
292
	case ASN1_ITYPE_NDEF_SEQUENCE:
293
	case ASN1_ITYPE_SEQUENCE:
294
246247
		p = *in;
295
246247
		tmplen = len;
296
297
		/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
298
246247
		if (tag == -1) {
299
			tag = V_ASN1_SEQUENCE;
300
			aclass = V_ASN1_UNIVERSAL;
301
246161
		}
302
		/* Get SEQUENCE length and update len, p */
303
246247
		ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
304
246247
		    &p, len, tag, aclass, opt, ctx);
305
246247
		if (!ret) {
306
228
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
307
228
			goto err;
308
246019
		} else if (ret == -1)
309
78
			return -1;
310

289573
		if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
311
			len = tmplen - (p - *in);
312
			seq_nolen = 1;
313
		}
314
		/* If indefinite we don't do a length check */
315
		else
316
245941
			seq_nolen = seq_eoc;
317
245941
		if (!cst) {
318
			ASN1error(ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
319
			goto err;
320
		}
321
322

408510
		if (!*pval && !ASN1_item_ex_new(pval, it)) {
323
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
324
			goto err;
325
		}
326
327

276477
		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
328
			goto auxerr;
329
330
		/* Free up and zero any ADB found */
331
1800598
		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
332
654358
			if (tt->flags & ASN1_TFLG_ADB_MASK) {
333
				const ASN1_TEMPLATE *seqtt;
334
				ASN1_VALUE **pseqval;
335
337
				seqtt = asn1_do_adb(pval, tt, 1);
336
337
				if (!seqtt)
337
					goto err;
338
337
				pseqval = asn1_get_field_ptr(pval, seqtt);
339
337
				ASN1_template_free(pseqval, seqtt);
340
337
			}
341
		}
342
343
		/* Get each field entry */
344
1771988
		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
345
			const ASN1_TEMPLATE *seqtt;
346
			ASN1_VALUE **pseqval;
347
648465
			seqtt = asn1_do_adb(pval, tt, 1);
348
648465
			if (!seqtt)
349
				goto err;
350
648465
			pseqval = asn1_get_field_ptr(pval, seqtt);
351
			/* Have we ran out of data? */
352
648465
			if (!len)
353
8408
				break;
354
640057
			q = p;
355
640057
			if (asn1_check_eoc(&p, len)) {
356
				if (!seq_eoc) {
357
					ASN1error(ASN1_R_UNEXPECTED_EOC);
358
					goto err;
359
				}
360
				len -= p - q;
361
				seq_eoc = 0;
362
				q = p;
363
				break;
364
			}
365
			/* This determines the OPTIONAL flag value. The field
366
			 * cannot be omitted if it is the last of a SEQUENCE
367
			 * and there is still data to be read. This isn't
368
			 * strictly necessary but it increases efficiency in
369
			 * some cases.
370
			 */
371
640057
			if (i == (it->tcount - 1))
372
237529
				isopt = 0;
373
			else
374
402528
				isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
375
			/* attempt to read in field, allowing each to be
376
			 * OPTIONAL */
377
378
640057
			ret = asn1_template_ex_d2i(pseqval, &p, len,
379
			    seqtt, isopt, ctx);
380
640057
			if (!ret) {
381
				errtt = seqtt;
382
4
				goto err;
383
640053
			} else if (ret == -1) {
384
				/* OPTIONAL component absent.
385
				 * Free and zero the field.
386
				 */
387
42158
				ASN1_template_free(pseqval, seqtt);
388
42158
				continue;
389
			}
390
			/* Update length */
391
597895
			len -= p - q;
392

597895
		}
393
394
		/* Check for EOC if expecting one */
395

245937
		if (seq_eoc && !asn1_check_eoc(&p, len)) {
396
			ASN1error(ASN1_R_MISSING_EOC);
397
			goto err;
398
		}
399
		/* Check all data read */
400
245937
		if (!seq_nolen && len) {
401
			ASN1error(ASN1_R_SEQUENCE_LENGTH_MISMATCH);
402
			goto err;
403
		}
404
405
		/* If we get here we've got no more data in the SEQUENCE,
406
		 * however we may not have read all fields so check all
407
		 * remaining are OPTIONAL and clear any that are.
408
		 */
409
520468
		for (; i < it->tcount; tt++, i++) {
410
			const ASN1_TEMPLATE *seqtt;
411
14297
			seqtt = asn1_do_adb(pval, tt, 1);
412
14297
			if (!seqtt)
413
				goto err;
414
14297
			if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
415
				ASN1_VALUE **pseqval;
416
14297
				pseqval = asn1_get_field_ptr(pval, seqtt);
417
14297
				ASN1_template_free(pseqval, seqtt);
418
			} else {
419
				errtt = seqtt;
420
				ASN1error(ASN1_R_FIELD_MISSING);
421
				goto err;
422
			}
423
14297
		}
424
		/* Save encoding */
425
245937
		if (!asn1_enc_save(pval, *in, p - *in, it)) {
426
			ASN1error(ERR_R_MALLOC_FAILURE);
427
			goto auxerr;
428
		}
429
245937
		*in = p;
430

276473
		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
431
			goto auxerr;
432
245937
		return 1;
433
434
	default:
435
		return 0;
436
	}
437
438
auxerr:
439
	ASN1error(ASN1_R_AUX_ERROR);
440
err:
441
232
	if (combine == 0)
442
232
		ASN1_item_ex_free(pval, it);
443
232
	if (errtt)
444
8
		ERR_asprintf_error_data("Field=%s, Type=%s", errtt->field_name,
445
4
		    it->sname);
446
	else
447
228
		ERR_asprintf_error_data("Type=%s", it->sname);
448
232
	return 0;
449
922250
}
450
451
/* Templates are handled with two separate functions.
452
 * One handles any EXPLICIT tag and the other handles the rest.
453
 */
454
455
static int
456
asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long inlen,
457
    const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
458
{
459
	int flags, aclass;
460
	int ret;
461
1541168
	long len;
462
770584
	const unsigned char *p, *q;
463
770584
	char exp_eoc;
464
465
770584
	if (!val)
466
		return 0;
467
770584
	flags = tt->flags;
468
770584
	aclass = flags & ASN1_TFLG_TAG_CLASS;
469
470
770584
	p = *in;
471
472
	/* Check if EXPLICIT tag expected */
473
770584
	if (flags & ASN1_TFLG_EXPTAG) {
474
24063
		char cst;
475
		/* Need to work out amount of data available to the inner
476
		 * content and where it starts: so read in EXPLICIT header to
477
		 * get the info.
478
		 */
479
24063
		ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
480
24063
		    &p, inlen, tt->tag, aclass, opt, ctx);
481
24063
		q = p;
482
24063
		if (!ret) {
483
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
484
			return 0;
485
24063
		} else if (ret == -1)
486
2615
			return -1;
487
21448
		if (!cst) {
488
			ASN1error(ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
489
			return 0;
490
		}
491
		/* We've found the field so it can't be OPTIONAL now */
492
21448
		ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
493
21448
		if (!ret) {
494
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
495
			return 0;
496
		}
497
		/* We read the field in OK so update length */
498
21448
		len -= p - q;
499
21448
		if (exp_eoc) {
500
			/* If NDEF we must have an EOC here */
501
			if (!asn1_check_eoc(&p, len)) {
502
				ASN1error(ASN1_R_MISSING_EOC);
503
				goto err;
504
			}
505
		} else {
506
			/* Otherwise we must hit the EXPLICIT tag end or its
507
			 * an error */
508
21448
			if (len) {
509
				ASN1error(ASN1_R_EXPLICIT_LENGTH_MISMATCH);
510
				goto err;
511
			}
512
		}
513
801789
	} else
514
746521
		return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
515
516
21448
	*in = p;
517
21448
	return 1;
518
519
err:
520
	ASN1_template_free(val, tt);
521
	return 0;
522
770584
}
523
524
static int
525
asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, long len,
526
    const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
527
{
528
	int flags, aclass;
529
	int ret;
530
767969
	const unsigned char *p, *q;
531
532
767969
	if (!val)
533
		return 0;
534
767969
	flags = tt->flags;
535
767969
	aclass = flags & ASN1_TFLG_TAG_CLASS;
536
537
767969
	p = *in;
538
	q = p;
539
540
767969
	if (flags & ASN1_TFLG_SK_MASK) {
541
		/* SET OF, SEQUENCE OF */
542
		int sktag, skaclass;
543
140501
		char sk_eoc;
544
		/* First work out expected inner tag value */
545
140501
		if (flags & ASN1_TFLG_IMPTAG) {
546
44
			sktag = tt->tag;
547
			skaclass = aclass;
548
44
		} else {
549
			skaclass = V_ASN1_UNIVERSAL;
550
140457
			if (flags & ASN1_TFLG_SET_OF)
551
103482
				sktag = V_ASN1_SET;
552
			else
553
				sktag = V_ASN1_SEQUENCE;
554
		}
555
		/* Get the tag */
556
140501
		ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
557
140501
		    &p, len, sktag, skaclass, opt, ctx);
558
140501
		if (!ret) {
559
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
560
			return 0;
561
140501
		} else if (ret == -1)
562
18
			return -1;
563
140483
		if (!*val)
564
140432
			*val = (ASN1_VALUE *)sk_new_null();
565
		else {
566
			/* We've got a valid STACK: free up any items present */
567
			STACK_OF(ASN1_VALUE) *sktmp =
568
51
			    (STACK_OF(ASN1_VALUE) *)*val;
569
51
			ASN1_VALUE *vtmp;
570
102
			while (sk_ASN1_VALUE_num(sktmp) > 0) {
571
				vtmp = sk_ASN1_VALUE_pop(sktmp);
572
				ASN1_item_ex_free(&vtmp,
573
				    tt->item);
574
			}
575
51
		}
576
577
140483
		if (!*val) {
578
			ASN1error(ERR_R_MALLOC_FAILURE);
579
			goto err;
580
		}
581
582
		/* Read as many items as we can */
583
769686
		while (len > 0) {
584
244360
			ASN1_VALUE *skfield;
585
244360
			q = p;
586
			/* See if EOC found */
587
244360
			if (asn1_check_eoc(&p, len)) {
588
				if (!sk_eoc) {
589
					ASN1error(ASN1_R_UNEXPECTED_EOC);
590
					goto err;
591
				}
592
				len -= p - q;
593
				sk_eoc = 0;
594
				break;
595
			}
596
244360
			skfield = NULL;
597
488720
			if (!ASN1_item_ex_d2i(&skfield, &p, len,
598
244360
			    tt->item, -1, 0, 0, ctx)) {
599
				ASN1error(ERR_R_NESTED_ASN1_ERROR);
600
				goto err;
601
			}
602
244360
			len -= p - q;
603
244360
			if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
604
			    skfield)) {
605
				ASN1error(ERR_R_MALLOC_FAILURE);
606
				goto err;
607
			}
608
769658
		}
609
140483
		if (sk_eoc) {
610
			ASN1error(ASN1_R_MISSING_EOC);
611
			goto err;
612
		}
613

1783334
	} else if (flags & ASN1_TFLG_IMPTAG) {
614
		/* IMPLICIT tagging */
615
21492
		ret = ASN1_item_ex_d2i(val, &p, len,
616
21492
		    tt->item, tt->tag, aclass, opt, ctx);
617
21492
		if (!ret) {
618
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
619
			goto err;
620
21492
		} else if (ret == -1)
621
21166
			return -1;
622
	} else {
623
		/* Nothing special */
624
605976
		ret = ASN1_item_ex_d2i(val, &p, len, tt->item,
625
605976
		    -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx);
626
605976
		if (!ret) {
627
4
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
628
4
			goto err;
629
605972
		} else if (ret == -1)
630
18727
			return -1;
631
	}
632
633
728054
	*in = p;
634
728054
	return 1;
635
636
err:
637
4
	ASN1_template_free(val, tt);
638
4
	return 0;
639
767969
}
640
641
static int
642
asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, long inlen,
643
    const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
644
{
645
1038962
	int ret = 0, utype;
646
519481
	long plen;
647
519481
	char cst, inf, free_cont = 0;
648
519481
	const unsigned char *p;
649
519481
	BUF_MEM buf;
650
	const unsigned char *cont = NULL;
651
	long len;
652
653
519481
	buf.length = 0;
654
519481
	buf.max = 0;
655
519481
	buf.data = NULL;
656
657
519481
	if (!pval) {
658
		ASN1error(ASN1_R_ILLEGAL_NULL);
659
		return 0; /* Should never happen */
660
	}
661
662
519481
	if (it->itype == ASN1_ITYPE_MSTRING) {
663
129756
		utype = tag;
664
		tag = -1;
665
129756
	} else
666
389725
		utype = it->utype;
667
668
519481
	if (utype == V_ASN1_ANY) {
669
		/* If type is ANY need to figure out type from tag */
670
39796
		unsigned char oclass;
671
39796
		if (tag >= 0) {
672
			ASN1error(ASN1_R_ILLEGAL_TAGGED_ANY);
673
			return 0;
674
		}
675
39796
		if (opt) {
676
			ASN1error(ASN1_R_ILLEGAL_OPTIONAL_ANY);
677
			return 0;
678
		}
679
39796
		p = *in;
680
39796
		ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
681
		    &p, inlen, -1, 0, 0, ctx);
682
39796
		if (!ret) {
683
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
684
			return 0;
685
		}
686
39796
		if (oclass != V_ASN1_UNIVERSAL)
687
2
			utype = V_ASN1_OTHER;
688
79592
	}
689
519481
	if (tag == -1) {
690
498075
		tag = utype;
691
		aclass = V_ASN1_UNIVERSAL;
692
498075
	}
693
519481
	p = *in;
694
	/* Check header */
695
519481
	ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
696
	    &p, inlen, tag, aclass, opt, ctx);
697
519481
	if (!ret) {
698
4
		ASN1error(ERR_R_NESTED_ASN1_ERROR);
699
4
		return 0;
700
519477
	} else if (ret == -1)
701
39815
		return -1;
702
	ret = 0;
703
	/* SEQUENCE, SET and "OTHER" are left in encoded form */
704
959324
	if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
705
479662
	    (utype == V_ASN1_OTHER)) {
706
		/* Clear context cache for type OTHER because the auto clear
707
		 * when we have a exact match wont work
708
		 */
709
100
		if (utype == V_ASN1_OTHER) {
710
4
			asn1_tlc_clear(ctx);
711
		}
712
		/* SEQUENCE and SET must be constructed */
713
98
		else if (!cst) {
714
			ASN1error(ASN1_R_TYPE_NOT_CONSTRUCTED);
715
			return 0;
716
		}
717
718
100
		cont = *in;
719
		/* If indefinite length constructed find the real end */
720
100
		if (inf) {
721
			if (!asn1_find_end(&p, plen, inf))
722
				goto err;
723
			len = p - cont;
724
		} else {
725
100
			len = p - cont + plen;
726
100
			p += plen;
727
100
			buf.data = NULL;
728
		}
729
479562
	} else if (cst) {
730
		/* Should really check the internal tags are correct but
731
		 * some things may get this wrong. The relevant specs
732
		 * say that constructed string types should be OCTET STRINGs
733
		 * internally irrespective of the type. So instead just check
734
		 * for UNIVERSAL class and ignore the tag.
735
		 */
736
		if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
737
			free_cont = 1;
738
			goto err;
739
		}
740
		len = buf.length;
741
		/* Append a final null to string */
742
		if (!BUF_MEM_grow_clean(&buf, len + 1)) {
743
			ASN1error(ERR_R_MALLOC_FAILURE);
744
			return 0;
745
		}
746
		buf.data[len] = 0;
747
		cont = (const unsigned char *)buf.data;
748
		free_cont = 1;
749
	} else {
750
479562
		cont = p;
751
479562
		len = plen;
752
479562
		p += plen;
753
	}
754
755
	/* We now have content length and type: translate into a structure */
756
479662
	if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
757
		goto err;
758
759
479662
	*in = p;
760
479662
	ret = 1;
761
762
err:
763

479662
	if (free_cont && buf.data)
764
		free(buf.data);
765
479662
	return ret;
766
519481
}
767
768
/* Translate ASN1 content octets into a structure */
769
770
int
771
asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype,
772
    char *free_cont, const ASN1_ITEM *it)
773
{
774
	ASN1_VALUE **opval = NULL;
775
	ASN1_STRING *stmp;
776
	ASN1_TYPE *typ = NULL;
777
	int ret = 0;
778
	const ASN1_PRIMITIVE_FUNCS *pf;
779
	ASN1_INTEGER **tint;
780
781
479662
	pf = it->funcs;
782
783

494179
	if (pf && pf->prim_c2i)
784
14517
		return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
785
	/* If ANY type clear type and set pointer to internal value */
786
465145
	if (it->utype == V_ASN1_ANY) {
787
39796
		if (!*pval) {
788
39528
			typ = ASN1_TYPE_new();
789
39528
			if (typ == NULL)
790
				goto err;
791
39528
			*pval = (ASN1_VALUE *)typ;
792
39528
		} else
793
268
			typ = (ASN1_TYPE *)*pval;
794
795
39796
		if (utype != typ->type)
796
39796
			ASN1_TYPE_set(typ, utype, NULL);
797
		opval = pval;
798
39796
		pval = &typ->value.asn1_value;
799
39796
	}
800

465145
	switch (utype) {
801
	case V_ASN1_OBJECT:
802
185041
		if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
803
			goto err;
804
		break;
805
806
	case V_ASN1_NULL:
807
37918
		if (len) {
808
			ASN1error(ASN1_R_NULL_IS_WRONG_LENGTH);
809
			goto err;
810
		}
811
37918
		*pval = (ASN1_VALUE *)1;
812
37918
		break;
813
814
	case V_ASN1_BOOLEAN:
815
18996
		if (len != 1) {
816
			ASN1error(ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
817
			goto err;
818
		} else {
819
			ASN1_BOOLEAN *tbool;
820
18996
			tbool = (ASN1_BOOLEAN *)pval;
821
18996
			*tbool = *cont;
822
		}
823
18996
		break;
824
825
	case V_ASN1_BIT_STRING:
826
27101
		if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
827
			goto err;
828
		break;
829
830
	case V_ASN1_INTEGER:
831
	case V_ASN1_ENUMERATED:
832
24506
		tint = (ASN1_INTEGER **)pval;
833
24506
		if (!c2i_ASN1_INTEGER(tint, &cont, len))
834
			goto err;
835
		/* Fixup type to match the expected form */
836
24506
		(*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
837
24506
		break;
838
839
	case V_ASN1_OCTET_STRING:
840
	case V_ASN1_NUMERICSTRING:
841
	case V_ASN1_PRINTABLESTRING:
842
	case V_ASN1_T61STRING:
843
	case V_ASN1_VIDEOTEXSTRING:
844
	case V_ASN1_IA5STRING:
845
	case V_ASN1_UTCTIME:
846
	case V_ASN1_GENERALIZEDTIME:
847
	case V_ASN1_GRAPHICSTRING:
848
	case V_ASN1_VISIBLESTRING:
849
	case V_ASN1_GENERALSTRING:
850
	case V_ASN1_UNIVERSALSTRING:
851
	case V_ASN1_BMPSTRING:
852
	case V_ASN1_UTF8STRING:
853
	case V_ASN1_OTHER:
854
	case V_ASN1_SET:
855
	case V_ASN1_SEQUENCE:
856
	default:
857

171589
		if (utype == V_ASN1_BMPSTRING && (len & 1)) {
858
			ASN1error(ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
859
			goto err;
860
		}
861

171583
		if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
862
			ASN1error(ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
863
			goto err;
864
		}
865
		/* All based on ASN1_STRING and handled the same */
866
171583
		if (!*pval) {
867
1030
			stmp = ASN1_STRING_type_new(utype);
868
1030
			if (!stmp) {
869
				ASN1error(ERR_R_MALLOC_FAILURE);
870
				goto err;
871
			}
872
1030
			*pval = (ASN1_VALUE *)stmp;
873
1030
		} else {
874
170553
			stmp = (ASN1_STRING *)*pval;
875
170553
			stmp->type = utype;
876
		}
877
		/* If we've already allocated a buffer use it */
878
171583
		if (*free_cont) {
879
			free(stmp->data);
880
			stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
881
			stmp->length = len;
882
			*free_cont = 0;
883
		} else {
884
171583
			if (!ASN1_STRING_set(stmp, cont, len)) {
885
				ASN1error(ERR_R_MALLOC_FAILURE);
886
				ASN1_STRING_free(stmp);
887
				*pval = NULL;
888
				goto err;
889
			}
890
		}
891
		break;
892
	}
893
	/* If ASN1_ANY and NULL type fix up value */
894
465145
	if (typ && (utype == V_ASN1_NULL))
895
37910
		typ->value.ptr = NULL;
896
897
465145
	ret = 1;
898
899
err:
900
465145
	if (!ret) {
901
		ASN1_TYPE_free(typ);
902
		if (opval)
903
			*opval = NULL;
904
	}
905
465145
	return ret;
906
479662
}
907
908
909
/* This function finds the end of an ASN1 structure when passed its maximum
910
 * length, whether it is indefinite length and a pointer to the content.
911
 * This is more efficient than calling asn1_collect because it does not
912
 * recurse on each indefinite length header.
913
 */
914
915
static int
916
asn1_find_end(const unsigned char **in, long len, char inf)
917
{
918
	int expected_eoc;
919
	long plen;
920
	const unsigned char *p = *in, *q;
921
922
	/* If not indefinite length constructed just add length */
923
	if (inf == 0) {
924
		*in += len;
925
		return 1;
926
	}
927
	expected_eoc = 1;
928
	/* Indefinite length constructed form. Find the end when enough EOCs
929
	 * are found. If more indefinite length constructed headers
930
	 * are encountered increment the expected eoc count otherwise just
931
	 * skip to the end of the data.
932
	 */
933
	while (len > 0) {
934
		if (asn1_check_eoc(&p, len)) {
935
			expected_eoc--;
936
			if (expected_eoc == 0)
937
				break;
938
			len -= 2;
939
			continue;
940
		}
941
		q = p;
942
		/* Just read in a header: only care about the length */
943
		if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
944
		    -1, 0, 0, NULL)) {
945
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
946
			return 0;
947
		}
948
		if (inf)
949
			expected_eoc++;
950
		else
951
			p += plen;
952
		len -= p - q;
953
	}
954
	if (expected_eoc) {
955
		ASN1error(ASN1_R_MISSING_EOC);
956
		return 0;
957
	}
958
	*in = p;
959
	return 1;
960
}
961
/* This function collects the asn1 data from a constructred string
962
 * type into a buffer. The values of 'in' and 'len' should refer
963
 * to the contents of the constructed type and 'inf' should be set
964
 * if it is indefinite length.
965
 */
966
967
#ifndef ASN1_MAX_STRING_NEST
968
/* This determines how many levels of recursion are permitted in ASN1
969
 * string types. If it is not limited stack overflows can occur. If set
970
 * to zero no recursion is allowed at all. Although zero should be adequate
971
 * examples exist that require a value of 1. So 5 should be more than enough.
972
 */
973
#define ASN1_MAX_STRING_NEST 5
974
#endif
975
976
static int
977
asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, char inf,
978
    int tag, int aclass, int depth)
979
{
980
	const unsigned char *p, *q;
981
	long plen;
982
	char cst, ininf;
983
984
	p = *in;
985
	inf &= 1;
986
	/* If no buffer and not indefinite length constructed just pass over
987
	 * the encoded data */
988
	if (!buf && !inf) {
989
		*in += len;
990
		return 1;
991
	}
992
	while (len > 0) {
993
		q = p;
994
		/* Check for EOC */
995
		if (asn1_check_eoc(&p, len)) {
996
			/* EOC is illegal outside indefinite length
997
			 * constructed form */
998
			if (!inf) {
999
				ASN1error(ASN1_R_UNEXPECTED_EOC);
1000
				return 0;
1001
			}
1002
			inf = 0;
1003
			break;
1004
		}
1005
1006
		if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1007
		    len, tag, aclass, 0, NULL)) {
1008
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
1009
			return 0;
1010
		}
1011
1012
		/* If indefinite length constructed update max length */
1013
		if (cst) {
1014
			if (depth >= ASN1_MAX_STRING_NEST) {
1015
				ASN1error(ASN1_R_NESTED_ASN1_STRING);
1016
				return 0;
1017
			}
1018
			if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1019
			    depth + 1))
1020
				return 0;
1021
		} else if (plen && !collect_data(buf, &p, plen))
1022
			return 0;
1023
		len -= p - q;
1024
	}
1025
	if (inf) {
1026
		ASN1error(ASN1_R_MISSING_EOC);
1027
		return 0;
1028
	}
1029
	*in = p;
1030
	return 1;
1031
}
1032
1033
static int
1034
collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1035
{
1036
	int len;
1037
	if (buf) {
1038
		len = buf->length;
1039
		if (!BUF_MEM_grow_clean(buf, len + plen)) {
1040
			ASN1error(ERR_R_MALLOC_FAILURE);
1041
			return 0;
1042
		}
1043
		memcpy(buf->data + len, *p, plen);
1044
	}
1045
	*p += plen;
1046
	return 1;
1047
}
1048
1049
/* Check for ASN1 EOC and swallow it if found */
1050
1051
static int
1052
asn1_check_eoc(const unsigned char **in, long len)
1053
{
1054
	const unsigned char *p;
1055
1056
1768834
	if (len < 2)
1057
		return 0;
1058
884417
	p = *in;
1059

884417
	if (!p[0] && !p[1]) {
1060
		*in += 2;
1061
		return 1;
1062
	}
1063
884417
	return 0;
1064
884417
}
1065
1066
/* Check an ASN1 tag and length: a bit like ASN1_get_object
1067
 * but it sets the length for indefinite length constructed
1068
 * form, we don't know the exact length but we can set an
1069
 * upper bound to the amount of data available minus the
1070
 * header length just read.
1071
 */
1072
1073
static int
1074
asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf,
1075
    char *cst, const unsigned char **in, long len, int exptag, int expclass,
1076
    char opt, ASN1_TLC *ctx)
1077
{
1078
	int i;
1079
2199688
	int ptag, pclass;
1080
1099844
	long plen;
1081
1099844
	const unsigned char *p, *q;
1082
1083
1099844
	p = *in;
1084
	q = p;
1085
1086

2199688
	if (ctx && ctx->valid) {
1087
212078
		i = ctx->ret;
1088
212078
		plen = ctx->plen;
1089
212078
		pclass = ctx->pclass;
1090
212078
		ptag = ctx->ptag;
1091
212078
		p += ctx->hdrlen;
1092
212078
	} else {
1093
887766
		i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1094
887766
		if (ctx) {
1095
887766
			ctx->ret = i;
1096
887766
			ctx->plen = plen;
1097
887766
			ctx->pclass = pclass;
1098
887766
			ctx->ptag = ptag;
1099
887766
			ctx->hdrlen = p - q;
1100
887766
			ctx->valid = 1;
1101
			/* If definite length, and no error, length +
1102
			 * header can't exceed total amount of data available.
1103
			 */
1104

1775304
			if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1105
				ASN1error(ASN1_R_TOO_LONG);
1106
				asn1_tlc_clear(ctx);
1107
				return 0;
1108
			}
1109
		}
1110
	}
1111
1112
1099844
	if (i & 0x80) {
1113
228
		ASN1error(ASN1_R_BAD_OBJECT_HEADER);
1114
456
		asn1_tlc_clear(ctx);
1115
228
		return 0;
1116
	}
1117
1099616
	if (exptag >= 0) {
1118

1817594
		if ((exptag != ptag) || (expclass != pclass)) {
1119
			/* If type is OPTIONAL, not an error:
1120
			 * indicate missing type.
1121
			 */
1122
42530
			if (opt)
1123
42526
				return -1;
1124
8
			asn1_tlc_clear(ctx);
1125
4
			ASN1error(ASN1_R_WRONG_TAG);
1126
4
			return 0;
1127
		}
1128
		/* We have a tag and class match:
1129
		 * assume we are going to do something with it */
1130
1775064
		asn1_tlc_clear(ctx);
1131
	}
1132
1133
1057086
	if (i & 1)
1134
		plen = len - (p - q);
1135
1057086
	if (inf)
1136
887534
		*inf = i & 1;
1137
1057086
	if (cst)
1138
747051
		*cst = i & V_ASN1_CONSTRUCTED;
1139
1057086
	if (olen)
1140
887534
		*olen = plen;
1141
1057086
	if (oclass)
1142
169552
		*oclass = pclass;
1143
1057086
	if (otag)
1144
169552
		*otag = ptag;
1145
1146
1057086
	*in = p;
1147
1057086
	return 1;
1148
1099844
}