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