2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
20 DEFINE_STACK_OF(ASN1_VALUE)
23 * Constructed types with a recursive definition (such as can be found in PKCS7)
24 * could eventually exceed the stack given malicious input with excessive
25 * recursion. Therefore we limit the stack depth. This is the maximum number of
26 * recursive invocations of asn1_item_embed_d2i().
28 #define ASN1_MAX_CONSTRUCTED_NEST 30
30 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
31 long len, const ASN1_ITEM *it,
32 int tag, int aclass, char opt, ASN1_TLC *ctx,
35 static int asn1_check_eoc(const unsigned char **in, long len);
36 static int asn1_find_end(const unsigned char **in, long len, char inf);
38 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
39 char inf, int tag, int aclass, int depth);
41 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
43 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
45 const unsigned char **in, long len,
46 int exptag, int expclass, char opt, ASN1_TLC *ctx);
48 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
49 const unsigned char **in, long len,
50 const ASN1_TEMPLATE *tt, char opt,
51 ASN1_TLC *ctx, int depth);
52 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
53 const unsigned char **in, long len,
54 const ASN1_TEMPLATE *tt, char opt,
55 ASN1_TLC *ctx, int depth);
56 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
57 const unsigned char **in, long len,
59 int tag, int aclass, char opt,
61 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
62 int utype, char *free_cont, const ASN1_ITEM *it);
64 /* Table to convert tags to bit values, used for MSTRING type */
65 static const unsigned long tag2bit[32] = {
67 0, 0, 0, B_ASN1_BIT_STRING,
69 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
71 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
73 B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
75 B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
77 B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
79 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
81 B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
83 B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
86 unsigned long ASN1_tag2bit(int tag)
88 if ((tag < 0) || (tag > 30))
93 /* Macro to initialize and invalidate the cache */
95 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
96 /* Version to avoid compiler warning about 'c' always non-NULL */
97 #define asn1_tlc_clear_nc(c) (c)->valid = 0
100 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
101 * function. 'in' points to a buffer to read the data from, in future we
102 * will have more advanced versions that can input data a piece at a time and
103 * this will simply be a special case.
106 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
107 const unsigned char **in, long len,
111 ASN1_VALUE *ptmpval = NULL;
115 asn1_tlc_clear_nc(&c);
116 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
121 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
123 int tag, int aclass, char opt, ASN1_TLC *ctx)
126 rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
128 ASN1_item_ex_free(pval, it);
133 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
134 * tag mismatch return -1 to handle OPTIONAL
137 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
138 long len, const ASN1_ITEM *it,
139 int tag, int aclass, char opt, ASN1_TLC *ctx,
142 const ASN1_TEMPLATE *tt, *errtt = NULL;
143 const ASN1_EXTERN_FUNCS *ef;
144 const ASN1_AUX *aux = it->funcs;
145 ASN1_aux_cb *asn1_cb;
146 const unsigned char *p = NULL, *q;
147 unsigned char oclass;
148 char seq_eoc, seq_nolen, cst, isopt;
157 if (aux && aux->asn1_cb)
158 asn1_cb = aux->asn1_cb;
162 if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
163 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NESTED_TOO_DEEP);
168 case ASN1_ITYPE_PRIMITIVE:
171 * tagging or OPTIONAL is currently illegal on an item template
172 * because the flags can't get passed down. In practice this
173 * isn't a problem: we include the relevant flags from the item
174 * template in the template itself.
176 if ((tag != -1) || opt) {
177 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
178 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
181 return asn1_template_ex_d2i(pval, in, len,
182 it->templates, opt, ctx, depth);
184 return asn1_d2i_ex_primitive(pval, in, len, it,
185 tag, aclass, opt, ctx);
187 case ASN1_ITYPE_MSTRING:
189 /* Just read in tag and class */
190 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
191 &p, len, -1, 0, 1, ctx);
193 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
197 /* Must be UNIVERSAL class */
198 if (oclass != V_ASN1_UNIVERSAL) {
199 /* If OPTIONAL, assume this is OK */
202 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
205 /* Check tag matches bit map */
206 if (!(ASN1_tag2bit(otag) & it->utype)) {
207 /* If OPTIONAL, assume this is OK */
210 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
213 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
215 case ASN1_ITYPE_EXTERN:
216 /* Use new style d2i */
218 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
220 case ASN1_ITYPE_CHOICE:
221 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
224 /* Free up and zero CHOICE value if initialised */
225 i = asn1_get_choice_selector(pval, it);
226 if ((i >= 0) && (i < it->tcount)) {
227 tt = it->templates + i;
228 pchptr = asn1_get_field_ptr(pval, tt);
229 asn1_template_free(pchptr, tt);
230 asn1_set_choice_selector(pval, -1, it);
232 } else if (!ASN1_item_ex_new(pval, it)) {
233 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
236 /* CHOICE type, try each possibility in turn */
238 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
239 pchptr = asn1_get_field_ptr(pval, tt);
241 * We mark field as OPTIONAL so its absence can be recognised.
243 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
244 /* If field not present, try the next one */
247 /* If positive return, read OK, break loop */
251 * Must be an ASN1 parsing error.
252 * Free up any partial choice value
254 asn1_template_free(pchptr, tt);
256 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
260 /* Did we fall off the end without reading anything? */
261 if (i == it->tcount) {
262 /* If OPTIONAL, this is OK */
264 /* Free and zero it */
265 ASN1_item_ex_free(pval, it);
268 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
272 asn1_set_choice_selector(pval, i, it);
274 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
279 case ASN1_ITYPE_NDEF_SEQUENCE:
280 case ASN1_ITYPE_SEQUENCE:
284 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
286 tag = V_ASN1_SEQUENCE;
287 aclass = V_ASN1_UNIVERSAL;
289 /* Get SEQUENCE length and update len, p */
290 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
291 &p, len, tag, aclass, opt, ctx);
293 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
295 } else if (ret == -1)
297 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
298 len = tmplen - (p - *in);
301 /* If indefinite we don't do a length check */
305 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
309 if (*pval == NULL && !ASN1_item_ex_new(pval, it)) {
310 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
314 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
317 /* Free up and zero any ADB found */
318 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
319 if (tt->flags & ASN1_TFLG_ADB_MASK) {
320 const ASN1_TEMPLATE *seqtt;
321 ASN1_VALUE **pseqval;
322 seqtt = asn1_do_adb(*pval, tt, 0);
325 pseqval = asn1_get_field_ptr(pval, seqtt);
326 asn1_template_free(pseqval, seqtt);
330 /* Get each field entry */
331 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
332 const ASN1_TEMPLATE *seqtt;
333 ASN1_VALUE **pseqval;
334 seqtt = asn1_do_adb(*pval, tt, 1);
337 pseqval = asn1_get_field_ptr(pval, seqtt);
338 /* Have we ran out of data? */
342 if (asn1_check_eoc(&p, len)) {
344 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
353 * This determines the OPTIONAL flag value. The field cannot be
354 * omitted if it is the last of a SEQUENCE and there is still
355 * data to be read. This isn't strictly necessary but it
356 * increases efficiency in some cases.
358 if (i == (it->tcount - 1))
361 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
363 * attempt to read in field, allowing each to be OPTIONAL
366 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
371 } else if (ret == -1) {
373 * OPTIONAL component absent. Free and zero the field.
375 asn1_template_free(pseqval, seqtt);
382 /* Check for EOC if expecting one */
383 if (seq_eoc && !asn1_check_eoc(&p, len)) {
384 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
387 /* Check all data read */
388 if (!seq_nolen && len) {
389 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
394 * If we get here we've got no more data in the SEQUENCE, however we
395 * may not have read all fields so check all remaining are OPTIONAL
396 * and clear any that are.
398 for (; i < it->tcount; tt++, i++) {
399 const ASN1_TEMPLATE *seqtt;
400 seqtt = asn1_do_adb(*pval, tt, 1);
403 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
404 ASN1_VALUE **pseqval;
405 pseqval = asn1_get_field_ptr(pval, seqtt);
406 asn1_template_free(pseqval, seqtt);
409 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
414 if (!asn1_enc_save(pval, *in, p - *in, it))
416 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
425 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
428 ERR_add_error_data(4, "Field=", errtt->field_name,
429 ", Type=", it->sname);
431 ERR_add_error_data(2, "Type=", it->sname);
436 * Templates are handled with two separate functions. One handles any
437 * EXPLICIT tag and the other handles the rest.
440 static int asn1_template_ex_d2i(ASN1_VALUE **val,
441 const unsigned char **in, long inlen,
442 const ASN1_TEMPLATE *tt, char opt,
443 ASN1_TLC *ctx, int depth)
448 const unsigned char *p, *q;
453 aclass = flags & ASN1_TFLG_TAG_CLASS;
457 /* Check if EXPLICIT tag expected */
458 if (flags & ASN1_TFLG_EXPTAG) {
461 * Need to work out amount of data available to the inner content and
462 * where it starts: so read in EXPLICIT header to get the info.
464 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
465 &p, inlen, tt->tag, aclass, opt, ctx);
468 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
470 } else if (ret == -1)
473 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
474 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
477 /* We've found the field so it can't be OPTIONAL now */
478 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
480 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
483 /* We read the field in OK so update length */
486 /* If NDEF we must have an EOC here */
487 if (!asn1_check_eoc(&p, len)) {
488 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
493 * Otherwise we must hit the EXPLICIT tag end or its an error
496 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
497 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
502 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
511 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
512 const unsigned char **in, long len,
513 const ASN1_TEMPLATE *tt, char opt,
514 ASN1_TLC *ctx, int depth)
519 const unsigned char *p, *q;
523 aclass = flags & ASN1_TFLG_TAG_CLASS;
529 * If field is embedded then val needs fixing so it is a pointer to
530 * a pointer to a field.
532 if (tt->flags & ASN1_TFLG_EMBED) {
533 tval = (ASN1_VALUE *)val;
537 if (flags & ASN1_TFLG_SK_MASK) {
538 /* SET OF, SEQUENCE OF */
541 /* First work out expected inner tag value */
542 if (flags & ASN1_TFLG_IMPTAG) {
546 skaclass = V_ASN1_UNIVERSAL;
547 if (flags & ASN1_TFLG_SET_OF)
550 sktag = V_ASN1_SEQUENCE;
553 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
554 &p, len, sktag, skaclass, opt, ctx);
556 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
558 } else if (ret == -1)
561 *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
564 * We've got a valid STACK: free up any items present
566 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
568 while (sk_ASN1_VALUE_num(sktmp) > 0) {
569 vtmp = sk_ASN1_VALUE_pop(sktmp);
570 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
575 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
579 /* Read as many items as we can */
583 /* See if EOC found */
584 if (asn1_check_eoc(&p, len)) {
586 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
587 ASN1_R_UNEXPECTED_EOC);
595 if (!asn1_item_embed_d2i(&skfield, &p, len,
596 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
598 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
599 ERR_R_NESTED_ASN1_ERROR);
600 /* |skfield| may be partially allocated despite failure. */
601 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
605 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
606 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
607 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
612 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
615 } else if (flags & ASN1_TFLG_IMPTAG) {
616 /* IMPLICIT tagging */
617 ret = asn1_item_embed_d2i(val, &p, len,
618 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
621 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
623 } else if (ret == -1)
626 /* Nothing special */
627 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
628 -1, 0, opt, ctx, depth);
630 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
632 } else if (ret == -1)
643 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
644 const unsigned char **in, long inlen,
646 int tag, int aclass, char opt, ASN1_TLC *ctx)
650 char cst, inf, free_cont = 0;
651 const unsigned char *p;
652 BUF_MEM buf = { 0, NULL, 0, 0 };
653 const unsigned char *cont = NULL;
657 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
658 return 0; /* Should never happen */
661 if (it->itype == ASN1_ITYPE_MSTRING) {
667 if (utype == V_ASN1_ANY) {
668 /* If type is ANY need to figure out type from tag */
669 unsigned char oclass;
671 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
675 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
676 ASN1_R_ILLEGAL_OPTIONAL_ANY);
680 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
681 &p, inlen, -1, 0, 0, ctx);
683 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
686 if (oclass != V_ASN1_UNIVERSAL)
687 utype = V_ASN1_OTHER;
691 aclass = V_ASN1_UNIVERSAL;
695 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
696 &p, inlen, tag, aclass, opt, ctx);
698 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
700 } else if (ret == -1)
703 /* SEQUENCE, SET and "OTHER" are left in encoded form */
704 if ((utype == V_ASN1_SEQUENCE)
705 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
707 * Clear context cache for type OTHER because the auto clear when we
708 * have a exact match won't work
710 if (utype == V_ASN1_OTHER) {
713 /* SEQUENCE and SET must be constructed */
715 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
716 ASN1_R_TYPE_NOT_CONSTRUCTED);
721 /* If indefinite length constructed find the real end */
723 if (!asn1_find_end(&p, plen, inf))
727 len = p - cont + plen;
731 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
732 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
733 || utype == V_ASN1_ENUMERATED) {
734 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
738 /* Free any returned 'buf' content */
741 * Should really check the internal tags are correct but some things
742 * may get this wrong. The relevant specs say that constructed string
743 * types should be OCTET STRINGs internally irrespective of the type.
744 * So instead just check for UNIVERSAL class and ignore the tag.
746 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
750 /* Append a final null to string */
751 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
752 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
756 cont = (const unsigned char *)buf.data;
763 /* We now have content length and type: translate into a structure */
764 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
765 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
772 OPENSSL_free(buf.data);
776 /* Translate ASN1 content octets into a structure */
778 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
779 int utype, char *free_cont, const ASN1_ITEM *it)
781 ASN1_VALUE **opval = NULL;
783 ASN1_TYPE *typ = NULL;
785 const ASN1_PRIMITIVE_FUNCS *pf;
789 if (pf && pf->prim_c2i)
790 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
791 /* If ANY type clear type and set pointer to internal value */
792 if (it->utype == V_ASN1_ANY) {
794 typ = ASN1_TYPE_new();
797 *pval = (ASN1_VALUE *)typ;
799 typ = (ASN1_TYPE *)*pval;
801 if (utype != typ->type)
802 ASN1_TYPE_set(typ, utype, NULL);
804 pval = &typ->value.asn1_value;
808 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
814 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
817 *pval = (ASN1_VALUE *)1;
822 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
826 tbool = (ASN1_BOOLEAN *)pval;
831 case V_ASN1_BIT_STRING:
832 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
837 case V_ASN1_ENUMERATED:
838 tint = (ASN1_INTEGER **)pval;
839 if (!c2i_ASN1_INTEGER(tint, &cont, len))
841 /* Fixup type to match the expected form */
842 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
845 case V_ASN1_OCTET_STRING:
846 case V_ASN1_NUMERICSTRING:
847 case V_ASN1_PRINTABLESTRING:
848 case V_ASN1_T61STRING:
849 case V_ASN1_VIDEOTEXSTRING:
850 case V_ASN1_IA5STRING:
852 case V_ASN1_GENERALIZEDTIME:
853 case V_ASN1_GRAPHICSTRING:
854 case V_ASN1_VISIBLESTRING:
855 case V_ASN1_GENERALSTRING:
856 case V_ASN1_UNIVERSALSTRING:
857 case V_ASN1_BMPSTRING:
858 case V_ASN1_UTF8STRING:
861 case V_ASN1_SEQUENCE:
863 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
864 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
867 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
868 ASN1err(ASN1_F_ASN1_EX_C2I,
869 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
872 /* All based on ASN1_STRING and handled the same */
874 stmp = ASN1_STRING_type_new(utype);
876 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
879 *pval = (ASN1_VALUE *)stmp;
881 stmp = (ASN1_STRING *)*pval;
884 /* If we've already allocated a buffer use it */
886 OPENSSL_free(stmp->data);
887 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
891 if (!ASN1_STRING_set(stmp, cont, len)) {
892 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
893 ASN1_STRING_free(stmp);
900 /* If ASN1_ANY and NULL type fix up value */
901 if (typ && (utype == V_ASN1_NULL))
902 typ->value.ptr = NULL;
915 * This function finds the end of an ASN1 structure when passed its maximum
916 * length, whether it is indefinite length and a pointer to the content. This
917 * is more efficient than calling asn1_collect because it does not recurse on
918 * each indefinite length header.
921 static int asn1_find_end(const unsigned char **in, long len, char inf)
923 uint32_t expected_eoc;
925 const unsigned char *p = *in, *q;
926 /* If not indefinite length constructed just add length */
933 * Indefinite length constructed form. Find the end when enough EOCs are
934 * found. If more indefinite length constructed headers are encountered
935 * increment the expected eoc count otherwise just skip to the end of the
939 if (asn1_check_eoc(&p, len)) {
941 if (expected_eoc == 0)
947 /* Just read in a header: only care about the length */
948 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
950 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
954 if (expected_eoc == UINT32_MAX) {
955 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
965 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
973 * This function collects the asn1 data from a constructed string type into
974 * a buffer. The values of 'in' and 'len' should refer to the contents of the
975 * constructed type and 'inf' should be set if it is indefinite length.
978 #ifndef ASN1_MAX_STRING_NEST
980 * This determines how many levels of recursion are permitted in ASN1 string
981 * types. If it is not limited stack overflows can occur. If set to zero no
982 * recursion is allowed at all. Although zero should be adequate examples
983 * exist that require a value of 1. So 5 should be more than enough.
985 # define ASN1_MAX_STRING_NEST 5
988 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
989 char inf, int tag, int aclass, int depth)
991 const unsigned char *p, *q;
997 * If no buffer and not indefinite length constructed just pass over the
1007 if (asn1_check_eoc(&p, len)) {
1009 * EOC is illegal outside indefinite length constructed form
1012 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1019 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1020 len, tag, aclass, 0, NULL)) {
1021 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1025 /* If indefinite length constructed update max length */
1027 if (depth >= ASN1_MAX_STRING_NEST) {
1028 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1031 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1033 } else if (plen && !collect_data(buf, &p, plen))
1038 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1045 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1050 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1051 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1054 memcpy(buf->data + len, *p, plen);
1060 /* Check for ASN1 EOC and swallow it if found */
1062 static int asn1_check_eoc(const unsigned char **in, long len)
1064 const unsigned char *p;
1069 if (p[0] == '\0' && p[1] == '\0') {
1077 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1078 * length for indefinite length constructed form, we don't know the exact
1079 * length but we can set an upper bound to the amount of data available minus
1080 * the header length just read.
1083 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1084 char *inf, char *cst,
1085 const unsigned char **in, long len,
1086 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1091 const unsigned char *p, *q;
1095 if (ctx && ctx->valid) {
1098 pclass = ctx->pclass;
1102 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1106 ctx->pclass = pclass;
1108 ctx->hdrlen = p - q;
1111 * If definite length, and no error, length + header can't exceed
1112 * total amount of data available.
1114 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1115 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1116 asn1_tlc_clear(ctx);
1123 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1124 asn1_tlc_clear(ctx);
1128 if ((exptag != ptag) || (expclass != pclass)) {
1130 * If type is OPTIONAL, not an error: indicate missing type.
1134 asn1_tlc_clear(ctx);
1135 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1139 * We have a tag and class match: assume we are going to do something
1142 asn1_tlc_clear(ctx);
1146 plen = len - (p - q);
1152 *cst = i & V_ASN1_CONSTRUCTED;