Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/tasn_dec.c

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