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