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"
21 * Constructed types with a recursive definition (such as can be found in PKCS7)
22 * could eventually exceed the stack given malicious input with excessive
23 * recursion. Therefore we limit the stack depth. This is the maximum number of
24 * recursive invocations of asn1_item_embed_d2i().
26 #define ASN1_MAX_CONSTRUCTED_NEST 30
28 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
29 long len, const ASN1_ITEM *it,
30 int tag, int aclass, char opt, ASN1_TLC *ctx,
33 static int asn1_check_eoc(const unsigned char **in, long len);
34 static int asn1_find_end(const unsigned char **in, long len, char inf);
36 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
37 char inf, int tag, int aclass, int depth);
39 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
41 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
43 const unsigned char **in, long len,
44 int exptag, int expclass, char opt, ASN1_TLC *ctx);
46 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
47 const unsigned char **in, long len,
48 const ASN1_TEMPLATE *tt, char opt,
49 ASN1_TLC *ctx, int depth);
50 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
51 const unsigned char **in, long len,
52 const ASN1_TEMPLATE *tt, char opt,
53 ASN1_TLC *ctx, int depth);
54 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
55 const unsigned char **in, long len,
57 int tag, int aclass, char opt,
59 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
60 int utype, char *free_cont, const ASN1_ITEM *it);
62 /* Table to convert tags to bit values, used for MSTRING type */
63 static const unsigned long tag2bit[32] = {
65 0, 0, 0, B_ASN1_BIT_STRING,
67 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
69 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
71 B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
73 B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
75 B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
77 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
79 B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
81 B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
84 unsigned long ASN1_tag2bit(int tag)
86 if ((tag < 0) || (tag > 30))
91 /* Macro to initialize and invalidate the cache */
93 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
94 /* Version to avoid compiler warning about 'c' always non-NULL */
95 #define asn1_tlc_clear_nc(c) (c)->valid = 0
98 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
99 * function. 'in' points to a buffer to read the data from, in future we
100 * will have more advanced versions that can input data a piece at a time and
101 * this will simply be a special case.
104 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
105 const unsigned char **in, long len,
109 ASN1_VALUE *ptmpval = NULL;
113 asn1_tlc_clear_nc(&c);
114 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
119 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
121 int tag, int aclass, char opt, ASN1_TLC *ctx)
124 rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
126 ASN1_item_ex_free(pval, it);
131 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
132 * tag mismatch return -1 to handle OPTIONAL
135 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
136 long len, const ASN1_ITEM *it,
137 int tag, int aclass, char opt, ASN1_TLC *ctx,
140 const ASN1_TEMPLATE *tt, *errtt = NULL;
141 const ASN1_EXTERN_FUNCS *ef;
142 const ASN1_AUX *aux = it->funcs;
143 ASN1_aux_cb *asn1_cb;
144 const unsigned char *p = NULL, *q;
145 unsigned char oclass;
146 char seq_eoc, seq_nolen, cst, isopt;
155 if (aux && aux->asn1_cb)
156 asn1_cb = aux->asn1_cb;
160 if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
161 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NESTED_TOO_DEEP);
166 case ASN1_ITYPE_PRIMITIVE:
169 * tagging or OPTIONAL is currently illegal on an item template
170 * because the flags can't get passed down. In practice this
171 * isn't a problem: we include the relevant flags from the item
172 * template in the template itself.
174 if ((tag != -1) || opt) {
175 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
176 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
179 return asn1_template_ex_d2i(pval, in, len,
180 it->templates, opt, ctx, depth);
182 return asn1_d2i_ex_primitive(pval, in, len, it,
183 tag, aclass, opt, ctx);
185 case ASN1_ITYPE_MSTRING:
187 /* Just read in tag and class */
188 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
189 &p, len, -1, 0, 1, ctx);
191 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
195 /* Must be UNIVERSAL class */
196 if (oclass != V_ASN1_UNIVERSAL) {
197 /* If OPTIONAL, assume this is OK */
200 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
203 /* Check tag matches bit map */
204 if (!(ASN1_tag2bit(otag) & it->utype)) {
205 /* If OPTIONAL, assume this is OK */
208 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
211 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
213 case ASN1_ITYPE_EXTERN:
214 /* Use new style d2i */
216 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
218 case ASN1_ITYPE_CHOICE:
219 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
222 /* Free up and zero CHOICE value if initialised */
223 i = asn1_get_choice_selector(pval, it);
224 if ((i >= 0) && (i < it->tcount)) {
225 tt = it->templates + i;
226 pchptr = asn1_get_field_ptr(pval, tt);
227 asn1_template_free(pchptr, tt);
228 asn1_set_choice_selector(pval, -1, it);
230 } else if (!ASN1_item_ex_new(pval, it)) {
231 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
234 /* CHOICE type, try each possibility in turn */
236 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
237 pchptr = asn1_get_field_ptr(pval, tt);
239 * We mark field as OPTIONAL so its absence can be recognised.
241 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
242 /* If field not present, try the next one */
245 /* If positive return, read OK, break loop */
249 * Must be an ASN1 parsing error.
250 * Free up any partial choice value
252 asn1_template_free(pchptr, tt);
254 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
258 /* Did we fall off the end without reading anything? */
259 if (i == it->tcount) {
260 /* If OPTIONAL, this is OK */
262 /* Free and zero it */
263 ASN1_item_ex_free(pval, it);
266 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
270 asn1_set_choice_selector(pval, i, it);
272 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
277 case ASN1_ITYPE_NDEF_SEQUENCE:
278 case ASN1_ITYPE_SEQUENCE:
282 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
284 tag = V_ASN1_SEQUENCE;
285 aclass = V_ASN1_UNIVERSAL;
287 /* Get SEQUENCE length and update len, p */
288 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
289 &p, len, tag, aclass, opt, ctx);
291 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
293 } else if (ret == -1)
295 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
296 len = tmplen - (p - *in);
299 /* If indefinite we don't do a length check */
303 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
307 if (*pval == NULL && !ASN1_item_ex_new(pval, it)) {
308 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
312 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
315 /* Free up and zero any ADB found */
316 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
317 if (tt->flags & ASN1_TFLG_ADB_MASK) {
318 const ASN1_TEMPLATE *seqtt;
319 ASN1_VALUE **pseqval;
320 seqtt = asn1_do_adb(*pval, tt, 0);
323 pseqval = asn1_get_field_ptr(pval, seqtt);
324 asn1_template_free(pseqval, seqtt);
328 /* Get each field entry */
329 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
330 const ASN1_TEMPLATE *seqtt;
331 ASN1_VALUE **pseqval;
332 seqtt = asn1_do_adb(*pval, tt, 1);
335 pseqval = asn1_get_field_ptr(pval, seqtt);
336 /* Have we ran out of data? */
340 if (asn1_check_eoc(&p, len)) {
342 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
351 * This determines the OPTIONAL flag value. The field cannot be
352 * omitted if it is the last of a SEQUENCE and there is still
353 * data to be read. This isn't strictly necessary but it
354 * increases efficiency in some cases.
356 if (i == (it->tcount - 1))
359 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
361 * attempt to read in field, allowing each to be OPTIONAL
364 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
369 } else if (ret == -1) {
371 * OPTIONAL component absent. Free and zero the field.
373 asn1_template_free(pseqval, seqtt);
380 /* Check for EOC if expecting one */
381 if (seq_eoc && !asn1_check_eoc(&p, len)) {
382 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
385 /* Check all data read */
386 if (!seq_nolen && len) {
387 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
392 * If we get here we've got no more data in the SEQUENCE, however we
393 * may not have read all fields so check all remaining are OPTIONAL
394 * and clear any that are.
396 for (; i < it->tcount; tt++, i++) {
397 const ASN1_TEMPLATE *seqtt;
398 seqtt = asn1_do_adb(*pval, tt, 1);
401 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
402 ASN1_VALUE **pseqval;
403 pseqval = asn1_get_field_ptr(pval, seqtt);
404 asn1_template_free(pseqval, seqtt);
407 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
412 if (!asn1_enc_save(pval, *in, p - *in, it))
414 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
423 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
426 ERR_add_error_data(4, "Field=", errtt->field_name,
427 ", Type=", it->sname);
429 ERR_add_error_data(2, "Type=", it->sname);
434 * Templates are handled with two separate functions. One handles any
435 * EXPLICIT tag and the other handles the rest.
438 static int asn1_template_ex_d2i(ASN1_VALUE **val,
439 const unsigned char **in, long inlen,
440 const ASN1_TEMPLATE *tt, char opt,
441 ASN1_TLC *ctx, int depth)
446 const unsigned char *p, *q;
451 aclass = flags & ASN1_TFLG_TAG_CLASS;
455 /* Check if EXPLICIT tag expected */
456 if (flags & ASN1_TFLG_EXPTAG) {
459 * Need to work out amount of data available to the inner content and
460 * where it starts: so read in EXPLICIT header to get the info.
462 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
463 &p, inlen, tt->tag, aclass, opt, ctx);
466 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
468 } else if (ret == -1)
471 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
472 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
475 /* We've found the field so it can't be OPTIONAL now */
476 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
478 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
481 /* We read the field in OK so update length */
484 /* If NDEF we must have an EOC here */
485 if (!asn1_check_eoc(&p, len)) {
486 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
491 * Otherwise we must hit the EXPLICIT tag end or its an error
494 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
495 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
500 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
509 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
510 const unsigned char **in, long len,
511 const ASN1_TEMPLATE *tt, char opt,
512 ASN1_TLC *ctx, int depth)
517 const unsigned char *p, *q;
521 aclass = flags & ASN1_TFLG_TAG_CLASS;
527 * If field is embedded then val needs fixing so it is a pointer to
528 * a pointer to a field.
530 if (tt->flags & ASN1_TFLG_EMBED) {
531 tval = (ASN1_VALUE *)val;
535 if (flags & ASN1_TFLG_SK_MASK) {
536 /* SET OF, SEQUENCE OF */
539 /* First work out expected inner tag value */
540 if (flags & ASN1_TFLG_IMPTAG) {
544 skaclass = V_ASN1_UNIVERSAL;
545 if (flags & ASN1_TFLG_SET_OF)
548 sktag = V_ASN1_SEQUENCE;
551 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
552 &p, len, sktag, skaclass, opt, ctx);
554 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
556 } else if (ret == -1)
559 *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
562 * We've got a valid STACK: free up any items present
564 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
566 while (sk_ASN1_VALUE_num(sktmp) > 0) {
567 vtmp = sk_ASN1_VALUE_pop(sktmp);
568 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
573 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
577 /* Read as many items as we can */
581 /* See if EOC found */
582 if (asn1_check_eoc(&p, len)) {
584 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
585 ASN1_R_UNEXPECTED_EOC);
593 if (!asn1_item_embed_d2i(&skfield, &p, len,
594 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
596 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
597 ERR_R_NESTED_ASN1_ERROR);
598 /* |skfield| may be partially allocated despite failure. */
599 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
603 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
604 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
605 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
610 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
613 } else if (flags & ASN1_TFLG_IMPTAG) {
614 /* IMPLICIT tagging */
615 ret = asn1_item_embed_d2i(val, &p, len,
616 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
619 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
621 } else if (ret == -1)
624 /* Nothing special */
625 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
626 -1, 0, opt, ctx, depth);
628 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
630 } else if (ret == -1)
641 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
642 const unsigned char **in, long inlen,
644 int tag, int aclass, char opt, ASN1_TLC *ctx)
648 char cst, inf, free_cont = 0;
649 const unsigned char *p;
650 BUF_MEM buf = { 0, NULL, 0, 0 };
651 const unsigned char *cont = NULL;
655 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
656 return 0; /* Should never happen */
659 if (it->itype == ASN1_ITYPE_MSTRING) {
665 if (utype == V_ASN1_ANY) {
666 /* If type is ANY need to figure out type from tag */
667 unsigned char oclass;
669 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
673 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
674 ASN1_R_ILLEGAL_OPTIONAL_ANY);
678 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
679 &p, inlen, -1, 0, 0, ctx);
681 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
684 if (oclass != V_ASN1_UNIVERSAL)
685 utype = V_ASN1_OTHER;
689 aclass = V_ASN1_UNIVERSAL;
693 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
694 &p, inlen, tag, aclass, opt, ctx);
696 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
698 } else if (ret == -1)
701 /* SEQUENCE, SET and "OTHER" are left in encoded form */
702 if ((utype == V_ASN1_SEQUENCE)
703 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
705 * Clear context cache for type OTHER because the auto clear when we
706 * have a exact match won't work
708 if (utype == V_ASN1_OTHER) {
711 /* SEQUENCE and SET must be constructed */
713 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
714 ASN1_R_TYPE_NOT_CONSTRUCTED);
719 /* If indefinite length constructed find the real end */
721 if (!asn1_find_end(&p, plen, inf))
725 len = p - cont + plen;
729 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
730 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
731 || utype == V_ASN1_ENUMERATED) {
732 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
736 /* Free any returned 'buf' content */
739 * Should really check the internal tags are correct but some things
740 * may get this wrong. The relevant specs say that constructed string
741 * types should be OCTET STRINGs internally irrespective of the type.
742 * So instead just check for UNIVERSAL class and ignore the tag.
744 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
748 /* Append a final null to string */
749 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
750 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
754 cont = (const unsigned char *)buf.data;
761 /* We now have content length and type: translate into a structure */
762 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
763 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
770 OPENSSL_free(buf.data);
774 /* Translate ASN1 content octets into a structure */
776 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
777 int utype, char *free_cont, const ASN1_ITEM *it)
779 ASN1_VALUE **opval = NULL;
781 ASN1_TYPE *typ = NULL;
783 const ASN1_PRIMITIVE_FUNCS *pf;
787 if (pf && pf->prim_c2i)
788 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
789 /* If ANY type clear type and set pointer to internal value */
790 if (it->utype == V_ASN1_ANY) {
792 typ = ASN1_TYPE_new();
795 *pval = (ASN1_VALUE *)typ;
797 typ = (ASN1_TYPE *)*pval;
799 if (utype != typ->type)
800 ASN1_TYPE_set(typ, utype, NULL);
802 pval = &typ->value.asn1_value;
806 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
812 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
815 *pval = (ASN1_VALUE *)1;
820 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
824 tbool = (ASN1_BOOLEAN *)pval;
829 case V_ASN1_BIT_STRING:
830 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
835 case V_ASN1_ENUMERATED:
836 tint = (ASN1_INTEGER **)pval;
837 if (!c2i_ASN1_INTEGER(tint, &cont, len))
839 /* Fixup type to match the expected form */
840 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
843 case V_ASN1_OCTET_STRING:
844 case V_ASN1_NUMERICSTRING:
845 case V_ASN1_PRINTABLESTRING:
846 case V_ASN1_T61STRING:
847 case V_ASN1_VIDEOTEXSTRING:
848 case V_ASN1_IA5STRING:
850 case V_ASN1_GENERALIZEDTIME:
851 case V_ASN1_GRAPHICSTRING:
852 case V_ASN1_VISIBLESTRING:
853 case V_ASN1_GENERALSTRING:
854 case V_ASN1_UNIVERSALSTRING:
855 case V_ASN1_BMPSTRING:
856 case V_ASN1_UTF8STRING:
859 case V_ASN1_SEQUENCE:
861 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
862 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
865 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
866 ASN1err(ASN1_F_ASN1_EX_C2I,
867 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
870 /* All based on ASN1_STRING and handled the same */
872 stmp = ASN1_STRING_type_new(utype);
874 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
877 *pval = (ASN1_VALUE *)stmp;
879 stmp = (ASN1_STRING *)*pval;
882 /* If we've already allocated a buffer use it */
884 OPENSSL_free(stmp->data);
885 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
889 if (!ASN1_STRING_set(stmp, cont, len)) {
890 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
891 ASN1_STRING_free(stmp);
898 /* If ASN1_ANY and NULL type fix up value */
899 if (typ && (utype == V_ASN1_NULL))
900 typ->value.ptr = NULL;
913 * This function finds the end of an ASN1 structure when passed its maximum
914 * length, whether it is indefinite length and a pointer to the content. This
915 * is more efficient than calling asn1_collect because it does not recurse on
916 * each indefinite length header.
919 static int asn1_find_end(const unsigned char **in, long len, char inf)
921 uint32_t expected_eoc;
923 const unsigned char *p = *in, *q;
924 /* If not indefinite length constructed just add length */
931 * Indefinite length constructed form. Find the end when enough EOCs are
932 * found. If more indefinite length constructed headers are encountered
933 * increment the expected eoc count otherwise just skip to the end of the
937 if (asn1_check_eoc(&p, len)) {
939 if (expected_eoc == 0)
945 /* Just read in a header: only care about the length */
946 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
948 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
952 if (expected_eoc == UINT32_MAX) {
953 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
963 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
971 * This function collects the asn1 data from a constructed string type into
972 * a buffer. The values of 'in' and 'len' should refer to the contents of the
973 * constructed type and 'inf' should be set if it is indefinite length.
976 #ifndef ASN1_MAX_STRING_NEST
978 * This determines how many levels of recursion are permitted in ASN1 string
979 * types. If it is not limited stack overflows can occur. If set to zero no
980 * recursion is allowed at all. Although zero should be adequate examples
981 * exist that require a value of 1. So 5 should be more than enough.
983 # define ASN1_MAX_STRING_NEST 5
986 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
987 char inf, int tag, int aclass, int depth)
989 const unsigned char *p, *q;
995 * If no buffer and not indefinite length constructed just pass over the
1005 if (asn1_check_eoc(&p, len)) {
1007 * EOC is illegal outside indefinite length constructed form
1010 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1017 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1018 len, tag, aclass, 0, NULL)) {
1019 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1023 /* If indefinite length constructed update max length */
1025 if (depth >= ASN1_MAX_STRING_NEST) {
1026 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1029 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1031 } else if (plen && !collect_data(buf, &p, plen))
1036 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1043 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1048 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1049 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1052 memcpy(buf->data + len, *p, plen);
1058 /* Check for ASN1 EOC and swallow it if found */
1060 static int asn1_check_eoc(const unsigned char **in, long len)
1062 const unsigned char *p;
1067 if (p[0] == '\0' && p[1] == '\0') {
1075 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1076 * length for indefinite length constructed form, we don't know the exact
1077 * length but we can set an upper bound to the amount of data available minus
1078 * the header length just read.
1081 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1082 char *inf, char *cst,
1083 const unsigned char **in, long len,
1084 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1089 const unsigned char *p, *q;
1093 if (ctx && ctx->valid) {
1096 pclass = ctx->pclass;
1100 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1104 ctx->pclass = pclass;
1106 ctx->hdrlen = p - q;
1109 * If definite length, and no error, length + header can't exceed
1110 * total amount of data available.
1112 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1113 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1114 asn1_tlc_clear(ctx);
1121 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1122 asn1_tlc_clear(ctx);
1126 if ((exptag != ptag) || (expclass != pclass)) {
1128 * If type is OPTIONAL, not an error: indicate missing type.
1132 asn1_tlc_clear(ctx);
1133 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1137 * We have a tag and class match: assume we are going to do something
1140 asn1_tlc_clear(ctx);
1144 plen = len - (p - q);
1150 *cst = i & V_ASN1_CONSTRUCTED;