GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/asn1/tasn_dec.c Lines: 344 553 62.2 %
Date: 2017-11-07 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
550576
	if ((tag < 0) || (tag > 30))
105
		return 0;
106
275288
	return tag2bit[tag];
107
275288
}
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
57336
	ASN1_TLC c;
128
28668
	ASN1_VALUE *ptmpval = NULL;
129
130
28668
	if (!pval)
131
12952
		pval = &ptmpval;
132
28668
	asn1_tlc_clear_nc(&c);
133
28668
	if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
134
28208
		return *pval;
135
460
	return NULL;
136
28668
}
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
1099955
	const ASN1_AUX *aux = it->funcs;
160
	ASN1_aux_cb *asn1_cb;
161
1099955
	const unsigned char *p = NULL, *q;
162
1099955
	unsigned char oclass;
163
1099955
	char seq_eoc, seq_nolen, cst, isopt;
164
	long tmplen;
165
	int i;
166
1099955
	int otag;
167
	int ret = 0;
168
	ASN1_VALUE **pchptr;
169
	int combine;
170
171
1099955
	combine = aclass & ASN1_TFLG_COMBINE;
172
1099955
	aclass &= ~ASN1_TFLG_COMBINE;
173
174
1099955
	if (!pval)
175
		return 0;
176
177

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

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

306316
			if ((tag != -1) || opt) {
192
				ASN1error(ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
193
				goto err;
194
			}
195
153158
			return asn1_template_ex_d2i(pval, in, len,
196
			    it->templates, opt, ctx);
197
		}
198
469659
		return asn1_d2i_ex_primitive(pval, in, len, it,
199
		    tag, aclass, opt, ctx);
200
		break;
201
202
	case ASN1_ITYPE_MSTRING:
203
153068
		p = *in;
204
		/* Just read in tag and class */
205
153068
		ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
206
153068
		    &p, len, -1, 0, 1, ctx);
207
153068
		if (!ret) {
208
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
209
			goto err;
210
		}
211
212
		/* Must be UNIVERSAL class */
213
153068
		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
153068
		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
306136
		return asn1_d2i_ex_primitive(pval, in, len,
229
153068
		    it, otag, 0, 0, ctx);
230
231
	case ASN1_ITYPE_EXTERN:
232
		/* Use new style d2i */
233
31138
		ef = it->funcs;
234
31138
		return ef->asn1_ex_d2i(pval, in, len,
235
		    it, tag, aclass, opt, ctx);
236
237
	case ASN1_ITYPE_CHOICE:
238

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

106
			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
282
		} 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
388
		p = *in;
256
1628
		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
257
814
			pchptr = asn1_get_field_ptr(pval, tt);
258
			/* We mark field as OPTIONAL so its absence
259
			 * can be recognised.
260
			 */
261
814
			ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
262
			/* If field not present, try the next one */
263
814
			if (ret == -1)
264
				continue;
265
			/* If positive return, read OK, break loop */
266
388
			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
388
		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
388
		asn1_set_choice_selector(pval, i, it);
287
388
		*in = p;
288

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

342898
		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
291984
			seq_nolen = seq_eoc;
317
291984
		if (!cst) {
318
			ASN1error(ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
319
			goto err;
320
		}
321
322

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

327466
		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
2140290
		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
332
778161
			if (tt->flags & ASN1_TFLG_ADB_MASK) {
333
				const ASN1_TEMPLATE *seqtt;
334
				ASN1_VALUE **pseqval;
335
352
				seqtt = asn1_do_adb(pval, tt, 1);
336
352
				if (!seqtt)
337
					goto err;
338
352
				pseqval = asn1_get_field_ptr(pval, seqtt);
339
352
				ASN1_template_free(pseqval, seqtt);
340
352
			}
341
		}
342
343
		/* Get each field entry */
344
2111450
		for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
345
			const ASN1_TEMPLATE *seqtt;
346
			ASN1_VALUE **pseqval;
347
772891
			seqtt = asn1_do_adb(pval, tt, 1);
348
772891
			if (!seqtt)
349
				goto err;
350
772891
			pseqval = asn1_get_field_ptr(pval, seqtt);
351
			/* Have we ran out of data? */
352
772891
			if (!len)
353
9146
				break;
354
763745
			q = p;
355
763745
			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
763745
			if (i == (it->tcount - 1))
372
282834
				isopt = 0;
373
			else
374
480911
				isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
375
			/* attempt to read in field, allowing each to be
376
			 * OPTIONAL */
377
378
763745
			ret = asn1_template_ex_d2i(pseqval, &p, len,
379
			    seqtt, isopt, ctx);
380
763745
			if (!ret) {
381
				errtt = seqtt;
382
4
				goto err;
383
763741
			} else if (ret == -1) {
384
				/* OPTIONAL component absent.
385
				 * Free and zero the field.
386
				 */
387
52040
				ASN1_template_free(pseqval, seqtt);
388
52040
				continue;
389
			}
390
			/* Update length */
391
711701
			len -= p - q;
392

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

291980
		if (seq_eoc && !asn1_check_eoc(&p, len)) {
396
			ASN1error(ASN1_R_MISSING_EOC);
397
			goto err;
398
		}
399
		/* Check all data read */
400
291980
		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
320804
		for (; i < it->tcount; tt++, i++) {
410
			const ASN1_TEMPLATE *seqtt;
411
14412
			seqtt = asn1_do_adb(pval, tt, 1);
412
14412
			if (!seqtt)
413
				goto err;
414
14412
			if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
415
				ASN1_VALUE **pseqval;
416
14412
				pseqval = asn1_get_field_ptr(pval, seqtt);
417
14412
				ASN1_template_free(pseqval, seqtt);
418
			} else {
419
				errtt = seqtt;
420
				ASN1error(ASN1_R_FIELD_MISSING);
421
				goto err;
422
			}
423
14412
		}
424
		/* Save encoding */
425
291980
		if (!asn1_enc_save(pval, *in, p - *in, it)) {
426
			ASN1error(ERR_R_MALLOC_FAILURE);
427
			goto auxerr;
428
		}
429
291980
		*in = p;
430

327462
		if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
431
			goto auxerr;
432
291980
		return 1;
433
434
	default:
435
		return 0;
436
	}
437
438
auxerr:
439
	ASN1error(ASN1_R_AUX_ERROR);
440
err:
441
460
	if (combine == 0)
442
460
		ASN1_item_ex_free(pval, it);
443
460
	if (errtt)
444
8
		ERR_asprintf_error_data("Field=%s, Type=%s", errtt->field_name,
445
4
		    it->sname);
446
	else
447
456
		ERR_asprintf_error_data("Type=%s", it->sname);
448
460
	return 0;
449
1099955
}
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
1835434
	long len;
462
917717
	const unsigned char *p, *q;
463
917717
	char exp_eoc;
464
465
917717
	if (!val)
466
		return 0;
467
917717
	flags = tt->flags;
468
917717
	aclass = flags & ASN1_TFLG_TAG_CLASS;
469
470
917717
	p = *in;
471
472
	/* Check if EXPLICIT tag expected */
473
917717
	if (flags & ASN1_TFLG_EXPTAG) {
474
29146
		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
29146
		ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
480
29146
		    &p, inlen, tt->tag, aclass, opt, ctx);
481
29146
		q = p;
482
29146
		if (!ret) {
483
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
484
			return 0;
485
29146
		} else if (ret == -1)
486
2204
			return -1;
487
26942
		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
26942
		ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
493
26942
		if (!ret) {
494
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
495
			return 0;
496
		}
497
		/* We read the field in OK so update length */
498
26942
		len -= p - q;
499
26942
		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
26942
			if (len) {
509
				ASN1error(ASN1_R_EXPLICIT_LENGTH_MISMATCH);
510
				goto err;
511
			}
512
		}
513
955918
	} else
514
888571
		return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
515
516
26942
	*in = p;
517
26942
	return 1;
518
519
err:
520
	ASN1_template_free(val, tt);
521
	return 0;
522
917717
}
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
915513
	const unsigned char *p, *q;
531
532
915513
	if (!val)
533
		return 0;
534
915513
	flags = tt->flags;
535
915513
	aclass = flags & ASN1_TFLG_TAG_CLASS;
536
537
915513
	p = *in;
538
	q = p;
539
540
915513
	if (flags & ASN1_TFLG_SK_MASK) {
541
		/* SET OF, SEQUENCE OF */
542
		int sktag, skaclass;
543
166646
		char sk_eoc;
544
		/* First work out expected inner tag value */
545
166646
		if (flags & ASN1_TFLG_IMPTAG) {
546
70
			sktag = tt->tag;
547
			skaclass = aclass;
548
70
		} else {
549
			skaclass = V_ASN1_UNIVERSAL;
550
166576
			if (flags & ASN1_TFLG_SET_OF)
551
122072
				sktag = V_ASN1_SET;
552
			else
553
				sktag = V_ASN1_SEQUENCE;
554
		}
555
		/* Get the tag */
556
166646
		ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
557
166646
		    &p, len, sktag, skaclass, opt, ctx);
558
166646
		if (!ret) {
559
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
560
			return 0;
561
166646
		} else if (ret == -1)
562
24
			return -1;
563
166622
		if (!*val)
564
166548
			*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
74
			    (STACK_OF(ASN1_VALUE) *)*val;
569
74
			ASN1_VALUE *vtmp;
570
148
			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
74
		}
576
577
166622
		if (!*val) {
578
			ASN1error(ERR_R_MALLOC_FAILURE);
579
			goto err;
580
		}
581
582
		/* Read as many items as we can */
583
457904
		while (len > 0) {
584
291282
			ASN1_VALUE *skfield;
585
291282
			q = p;
586
			/* See if EOC found */
587
291282
			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
291282
			skfield = NULL;
597
582564
			if (!ASN1_item_ex_d2i(&skfield, &p, len,
598
291282
			    tt->item, -1, 0, 0, ctx)) {
599
				ASN1error(ERR_R_NESTED_ASN1_ERROR);
600
				goto err;
601
			}
602
291282
			len -= p - q;
603
291282
			if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
604
			    skfield)) {
605
				ASN1error(ERR_R_MALLOC_FAILURE);
606
				goto err;
607
			}
608
1006630
		}
609
166622
		if (sk_eoc) {
610
			ASN1error(ASN1_R_MISSING_EOC);
611
			goto err;
612
		}
613

2114959
	} else if (flags & ASN1_TFLG_IMPTAG) {
614
		/* IMPLICIT tagging */
615
27134
		ret = ASN1_item_ex_d2i(val, &p, len,
616
27134
		    tt->item, tt->tag, aclass, opt, ctx);
617
27134
		if (!ret) {
618
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
619
			goto err;
620
27134
		} else if (ret == -1)
621
26704
			return -1;
622
	} else {
623
		/* Nothing special */
624
721733
		ret = ASN1_item_ex_d2i(val, &p, len, tt->item,
625
721733
		    -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx);
626
721733
		if (!ret) {
627
4
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
628
4
			goto err;
629
721729
		} else if (ret == -1)
630
23534
			return -1;
631
	}
632
633
865247
	*in = p;
634
865247
	return 1;
635
636
err:
637
4
	ASN1_template_free(val, tt);
638
4
	return 0;
639
915513
}
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
1245454
	int ret = 0, utype;
646
622727
	long plen;
647
622727
	char cst, inf, free_cont = 0;
648
622727
	const unsigned char *p;
649
622727
	BUF_MEM buf;
650
	const unsigned char *cont = NULL;
651
	long len;
652
653
622727
	buf.length = 0;
654
622727
	buf.max = 0;
655
622727
	buf.data = NULL;
656
657
622727
	if (!pval) {
658
		ASN1error(ASN1_R_ILLEGAL_NULL);
659
		return 0; /* Should never happen */
660
	}
661
662
622727
	if (it->itype == ASN1_ITYPE_MSTRING) {
663
153068
		utype = tag;
664
		tag = -1;
665
153068
	} else
666
469659
		utype = it->utype;
667
668
622727
	if (utype == V_ASN1_ANY) {
669
		/* If type is ANY need to figure out type from tag */
670
45810
		unsigned char oclass;
671
45810
		if (tag >= 0) {
672
			ASN1error(ASN1_R_ILLEGAL_TAGGED_ANY);
673
			return 0;
674
		}
675
45810
		if (opt) {
676
			ASN1error(ASN1_R_ILLEGAL_OPTIONAL_ANY);
677
			return 0;
678
		}
679
45810
		p = *in;
680
45810
		ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
681
		    &p, inlen, -1, 0, 0, ctx);
682
45810
		if (!ret) {
683
			ASN1error(ERR_R_NESTED_ASN1_ERROR);
684
			return 0;
685
		}
686
45810
		if (oclass != V_ASN1_UNIVERSAL)
687
2
			utype = V_ASN1_OTHER;
688
91620
	}
689
622727
	if (tag == -1) {
690
595705
		tag = utype;
691
		aclass = V_ASN1_UNIVERSAL;
692
595705
	}
693
622727
	p = *in;
694
	/* Check header */
695
622727
	ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
696
	    &p, inlen, tag, aclass, opt, ctx);
697
622727
	if (!ret) {
698
4
		ASN1error(ERR_R_NESTED_ASN1_ERROR);
699
4
		return 0;
700
622723
	} else if (ret == -1)
701
50134
		return -1;
702
	ret = 0;
703
	/* SEQUENCE, SET and "OTHER" are left in encoded form */
704
1145178
	if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
705
572589
	    (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
106
		if (utype == V_ASN1_OTHER) {
710
4
			asn1_tlc_clear(ctx);
711
		}
712
		/* SEQUENCE and SET must be constructed */
713
104
		else if (!cst) {
714
			ASN1error(ASN1_R_TYPE_NOT_CONSTRUCTED);
715
			return 0;
716
		}
717
718
106
		cont = *in;
719
		/* If indefinite length constructed find the real end */
720
106
		if (inf) {
721
			if (!asn1_find_end(&p, plen, inf))
722
				goto err;
723
			len = p - cont;
724
		} else {
725
106
			len = p - cont + plen;
726
106
			p += plen;
727
106
			buf.data = NULL;
728
		}
729
572483
	} 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
572483
		cont = p;
751
572483
		len = plen;
752
572483
		p += plen;
753
	}
754
755
	/* We now have content length and type: translate into a structure */
756
572589
	if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
757
		goto err;
758
759
572589
	*in = p;
760
572589
	ret = 1;
761
762
err:
763

572589
	if (free_cont && buf.data)
764
		free(buf.data);
765
572589
	return ret;
766
622727
}
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
572589
	pf = it->funcs;
782
783

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

554146
	switch (utype) {
801
	case V_ASN1_OBJECT:
802
220532
		if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
803
			goto err;
804
		break;
805
806
	case V_ASN1_NULL:
807
43526
		if (len) {
808
			ASN1error(ASN1_R_NULL_IS_WRONG_LENGTH);
809
			goto err;
810
		}
811
43526
		*pval = (ASN1_VALUE *)1;
812
43526
		break;
813
814
	case V_ASN1_BOOLEAN:
815
23990
		if (len != 1) {
816
			ASN1error(ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
817
			goto err;
818
		} else {
819
			ASN1_BOOLEAN *tbool;
820
23990
			tbool = (ASN1_BOOLEAN *)pval;
821
23990
			*tbool = *cont;
822
		}
823
23990
		break;
824
825
	case V_ASN1_BIT_STRING:
826
31974
		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
29626
		tint = (ASN1_INTEGER **)pval;
833
29626
		if (!c2i_ASN1_INTEGER(tint, &cont, len))
834
			goto err;
835
		/* Fixup type to match the expected form */
836
29626
		(*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
837
29626
		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

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

204498
		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
204498
		if (!*pval) {
867
1252
			stmp = ASN1_STRING_type_new(utype);
868
1252
			if (!stmp) {
869
				ASN1error(ERR_R_MALLOC_FAILURE);
870
				goto err;
871
			}
872
1252
			*pval = (ASN1_VALUE *)stmp;
873
1252
		} else {
874
203246
			stmp = (ASN1_STRING *)*pval;
875
203246
			stmp->type = utype;
876
		}
877
		/* If we've already allocated a buffer use it */
878
204498
		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
204498
			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
554146
	if (typ && (utype == V_ASN1_NULL))
895
43518
		typ->value.ptr = NULL;
896
897
554146
	ret = 1;
898
899
err:
900
554146
	if (!ret) {
901
		ASN1_TYPE_free(typ);
902
		if (opval)
903
			*opval = NULL;
904
	}
905
554146
	return ret;
906
572589
}
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
2110054
	if (len < 2)
1057
		return 0;
1058
1055027
	p = *in;
1059

1055027
	if (!p[0] && !p[1]) {
1060
		*in += 2;
1061
		return 1;
1062
	}
1063
1055027
	return 0;
1064
1055027
}
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
2619882
	int ptag, pclass;
1080
1309941
	long plen;
1081
1309941
	const unsigned char *p, *q;
1082
1083
1309941
	p = *in;
1084
	q = p;
1085
1086

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

2116738
			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
1309941
	if (i & 0x80) {
1113
456
		ASN1error(ASN1_R_BAD_OBJECT_HEADER);
1114
912
		asn1_tlc_clear(ctx);
1115
456
		return 0;
1116
	}
1117
1309485
	if (exptag >= 0) {
1118

2168740
		if ((exptag != ptag) || (expclass != pclass)) {
1119
			/* If type is OPTIONAL, not an error:
1120
			 * indicate missing type.
1121
			 */
1122
52470
			if (opt)
1123
52466
				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
2116270
		asn1_tlc_clear(ctx);
1131
	}
1132
1133
1257015
	if (i & 1)
1134
		plen = len - (p - q);
1135
1257015
	if (inf)
1136
1058137
		*inf = i & 1;
1137
1257015
	if (cst)
1138
891515
		*cst = i & V_ASN1_CONSTRUCTED;
1139
1257015
	if (olen)
1140
1058137
		*olen = plen;
1141
1257015
	if (oclass)
1142
198878
		*oclass = pclass;
1143
1257015
	if (otag)
1144
198878
		*otag = ptag;
1145
1146
1257015
	*in = p;
1147
1257015
	return 1;
1148
1309941
}