2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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_locl.h"
22 * Constructed types with a recursive definition (such as can be found in PKCS7)
23 * could eventually exceed the stack given malicious input with excessive
24 * recursion. Therefore we limit the stack depth. This is the maximum number of
25 * recursive invocations of asn1_item_embed_d2i().
27 #define ASN1_MAX_CONSTRUCTED_NEST 30
29 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
30 long len, const ASN1_ITEM *it,
31 int tag, int aclass, char opt, ASN1_TLC *ctx,
34 static int asn1_check_eoc(const unsigned char **in, long len);
35 static int asn1_find_end(const unsigned char **in, long len, char inf);
37 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
38 char inf, int tag, int aclass, int depth);
40 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
42 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
44 const unsigned char **in, long len,
45 int exptag, int expclass, char opt, ASN1_TLC *ctx);
47 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
48 const unsigned char **in, long len,
49 const ASN1_TEMPLATE *tt, char opt,
50 ASN1_TLC *ctx, int depth);
51 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
52 const unsigned char **in, long len,
53 const ASN1_TEMPLATE *tt, char opt,
54 ASN1_TLC *ctx, int depth);
55 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
56 const unsigned char **in, long len,
58 int tag, int aclass, char opt,
60 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
61 int utype, char *free_cont, const ASN1_ITEM *it);
63 /* Table to convert tags to bit values, used for MSTRING type */
64 static const unsigned long tag2bit[32] = {
66 0, 0, 0, B_ASN1_BIT_STRING,
68 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
70 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
72 B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
74 B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
76 B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
78 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
80 B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
82 B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
85 unsigned long ASN1_tag2bit(int tag)
87 if ((tag < 0) || (tag > 30))
92 /* Macro to initialize and invalidate the cache */
94 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
95 /* Version to avoid compiler warning about 'c' always non-NULL */
96 #define asn1_tlc_clear_nc(c) (c)->valid = 0
99 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
100 * function. 'in' points to a buffer to read the data from, in future we
101 * will have more advanced versions that can input data a piece at a time and
102 * this will simply be a special case.
105 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
106 const unsigned char **in, long len,
110 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;
154 if (aux && aux->asn1_cb)
155 asn1_cb = aux->asn1_cb;
159 if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
160 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NESTED_TOO_DEEP);
165 case ASN1_ITYPE_PRIMITIVE:
168 * tagging or OPTIONAL is currently illegal on an item template
169 * because the flags can't get passed down. In practice this
170 * isn't a problem: we include the relevant flags from the item
171 * template in the template itself.
173 if ((tag != -1) || opt) {
174 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
175 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
178 return asn1_template_ex_d2i(pval, in, len,
179 it->templates, opt, ctx, depth);
181 return asn1_d2i_ex_primitive(pval, in, len, it,
182 tag, aclass, opt, ctx);
184 case ASN1_ITYPE_MSTRING:
186 /* Just read in tag and class */
187 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
188 &p, len, -1, 0, 1, ctx);
190 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
194 /* Must be UNIVERSAL class */
195 if (oclass != V_ASN1_UNIVERSAL) {
196 /* If OPTIONAL, assume this is OK */
199 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
202 /* Check tag matches bit map */
203 if (!(ASN1_tag2bit(otag) & it->utype)) {
204 /* If OPTIONAL, assume this is OK */
207 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
210 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
212 case ASN1_ITYPE_EXTERN:
213 /* Use new style d2i */
215 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
217 case ASN1_ITYPE_CHOICE:
218 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
221 /* Free up and zero CHOICE value if initialised */
222 i = asn1_get_choice_selector(pval, it);
223 if ((i >= 0) && (i < it->tcount)) {
224 tt = it->templates + i;
225 pchptr = asn1_get_field_ptr(pval, tt);
226 asn1_template_free(pchptr, tt);
227 asn1_set_choice_selector(pval, -1, it);
229 } else if (!ASN1_item_ex_new(pval, it)) {
230 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
233 /* CHOICE type, try each possibility in turn */
235 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
236 pchptr = asn1_get_field_ptr(pval, tt);
238 * We mark field as OPTIONAL so its absence can be recognised.
240 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
241 /* If field not present, try the next one */
244 /* If positive return, read OK, break loop */
248 * Must be an ASN1 parsing error.
249 * Free up any partial choice value
251 asn1_template_free(pchptr, tt);
253 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
257 /* Did we fall off the end without reading anything? */
258 if (i == it->tcount) {
259 /* If OPTIONAL, this is OK */
261 /* Free and zero it */
262 ASN1_item_ex_free(pval, it);
265 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
269 asn1_set_choice_selector(pval, i, it);
271 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
276 case ASN1_ITYPE_NDEF_SEQUENCE:
277 case ASN1_ITYPE_SEQUENCE:
281 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
283 tag = V_ASN1_SEQUENCE;
284 aclass = V_ASN1_UNIVERSAL;
286 /* Get SEQUENCE length and update len, p */
287 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
288 &p, len, tag, aclass, opt, ctx);
290 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
292 } else if (ret == -1)
294 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
295 len = tmplen - (p - *in);
298 /* If indefinite we don't do a length check */
302 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
306 if (!*pval && !ASN1_item_ex_new(pval, it)) {
307 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
311 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
314 /* Free up and zero any ADB found */
315 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
316 if (tt->flags & ASN1_TFLG_ADB_MASK) {
317 const ASN1_TEMPLATE *seqtt;
318 ASN1_VALUE **pseqval;
319 seqtt = asn1_do_adb(pval, tt, 0);
322 pseqval = asn1_get_field_ptr(pval, seqtt);
323 asn1_template_free(pseqval, seqtt);
327 /* Get each field entry */
328 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
329 const ASN1_TEMPLATE *seqtt;
330 ASN1_VALUE **pseqval;
331 seqtt = asn1_do_adb(pval, tt, 1);
334 pseqval = asn1_get_field_ptr(pval, seqtt);
335 /* Have we ran out of data? */
339 if (asn1_check_eoc(&p, len)) {
341 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
350 * This determines the OPTIONAL flag value. The field cannot be
351 * omitted if it is the last of a SEQUENCE and there is still
352 * data to be read. This isn't strictly necessary but it
353 * increases efficiency in some cases.
355 if (i == (it->tcount - 1))
358 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
360 * attempt to read in field, allowing each to be OPTIONAL
363 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
368 } else if (ret == -1) {
370 * OPTIONAL component absent. Free and zero the field.
372 asn1_template_free(pseqval, seqtt);
379 /* Check for EOC if expecting one */
380 if (seq_eoc && !asn1_check_eoc(&p, len)) {
381 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
384 /* Check all data read */
385 if (!seq_nolen && len) {
386 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
391 * If we get here we've got no more data in the SEQUENCE, however we
392 * may not have read all fields so check all remaining are OPTIONAL
393 * and clear any that are.
395 for (; i < it->tcount; tt++, i++) {
396 const ASN1_TEMPLATE *seqtt;
397 seqtt = asn1_do_adb(pval, tt, 1);
400 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
401 ASN1_VALUE **pseqval;
402 pseqval = asn1_get_field_ptr(pval, seqtt);
403 asn1_template_free(pseqval, seqtt);
406 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
411 if (!asn1_enc_save(pval, *in, p - *in, it))
413 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
422 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
425 ERR_add_error_data(4, "Field=", errtt->field_name,
426 ", Type=", it->sname);
428 ERR_add_error_data(2, "Type=", it->sname);
433 * Templates are handled with two separate functions. One handles any
434 * EXPLICIT tag and the other handles the rest.
437 static int asn1_template_ex_d2i(ASN1_VALUE **val,
438 const unsigned char **in, long inlen,
439 const ASN1_TEMPLATE *tt, char opt,
440 ASN1_TLC *ctx, int depth)
445 const unsigned char *p, *q;
450 aclass = flags & ASN1_TFLG_TAG_CLASS;
454 /* Check if EXPLICIT tag expected */
455 if (flags & ASN1_TFLG_EXPTAG) {
458 * Need to work out amount of data available to the inner content and
459 * where it starts: so read in EXPLICIT header to get the info.
461 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
462 &p, inlen, tt->tag, aclass, opt, ctx);
465 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
467 } else if (ret == -1)
470 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
471 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
474 /* We've found the field so it can't be OPTIONAL now */
475 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
477 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
480 /* We read the field in OK so update length */
483 /* If NDEF we must have an EOC here */
484 if (!asn1_check_eoc(&p, len)) {
485 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
490 * Otherwise we must hit the EXPLICIT tag end or its an error
493 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
494 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
499 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
508 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
509 const unsigned char **in, long len,
510 const ASN1_TEMPLATE *tt, char opt,
511 ASN1_TLC *ctx, int depth)
516 const unsigned char *p, *q;
520 aclass = flags & ASN1_TFLG_TAG_CLASS;
526 * If field is embedded then val needs fixing so it is a pointer to
527 * a pointer to a field.
529 if (tt->flags & ASN1_TFLG_EMBED) {
530 tval = (ASN1_VALUE *)val;
534 if (flags & ASN1_TFLG_SK_MASK) {
535 /* SET OF, SEQUENCE OF */
538 /* First work out expected inner tag value */
539 if (flags & ASN1_TFLG_IMPTAG) {
543 skaclass = V_ASN1_UNIVERSAL;
544 if (flags & ASN1_TFLG_SET_OF)
547 sktag = V_ASN1_SEQUENCE;
550 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
551 &p, len, sktag, skaclass, opt, ctx);
553 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
555 } else if (ret == -1)
558 *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
561 * We've got a valid STACK: free up any items present
563 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
565 while (sk_ASN1_VALUE_num(sktmp) > 0) {
566 vtmp = sk_ASN1_VALUE_pop(sktmp);
567 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
572 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
576 /* Read as many items as we can */
580 /* See if EOC found */
581 if (asn1_check_eoc(&p, len)) {
583 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
584 ASN1_R_UNEXPECTED_EOC);
592 if (!asn1_item_embed_d2i(&skfield, &p, len,
593 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
595 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
596 ERR_R_NESTED_ASN1_ERROR);
597 /* |skfield| may be partially allocated despite failure. */
598 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
602 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
603 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
604 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
609 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
612 } else if (flags & ASN1_TFLG_IMPTAG) {
613 /* IMPLICIT tagging */
614 ret = asn1_item_embed_d2i(val, &p, len,
615 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
618 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
620 } else if (ret == -1)
623 /* Nothing special */
624 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
625 -1, 0, opt, ctx, depth);
627 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
629 } else if (ret == -1)
640 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
641 const unsigned char **in, long inlen,
643 int tag, int aclass, char opt, ASN1_TLC *ctx)
647 char cst, inf, free_cont = 0;
648 const unsigned char *p;
649 BUF_MEM buf = { 0, NULL, 0, 0 };
650 const unsigned char *cont = NULL;
653 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
654 return 0; /* Should never happen */
657 if (it->itype == ASN1_ITYPE_MSTRING) {
663 if (utype == V_ASN1_ANY) {
664 /* If type is ANY need to figure out type from tag */
665 unsigned char oclass;
667 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
671 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
672 ASN1_R_ILLEGAL_OPTIONAL_ANY);
676 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
677 &p, inlen, -1, 0, 0, ctx);
679 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
682 if (oclass != V_ASN1_UNIVERSAL)
683 utype = V_ASN1_OTHER;
687 aclass = V_ASN1_UNIVERSAL;
691 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
692 &p, inlen, tag, aclass, opt, ctx);
694 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
696 } else if (ret == -1)
699 /* SEQUENCE, SET and "OTHER" are left in encoded form */
700 if ((utype == V_ASN1_SEQUENCE)
701 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
703 * Clear context cache for type OTHER because the auto clear when we
704 * have a exact match won't work
706 if (utype == V_ASN1_OTHER) {
709 /* SEQUENCE and SET must be constructed */
711 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
712 ASN1_R_TYPE_NOT_CONSTRUCTED);
717 /* If indefinite length constructed find the real end */
719 if (!asn1_find_end(&p, plen, inf))
723 len = p - cont + plen;
727 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
728 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
729 || utype == V_ASN1_ENUMERATED) {
730 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
734 /* Free any returned 'buf' content */
737 * Should really check the internal tags are correct but some things
738 * may get this wrong. The relevant specs say that constructed string
739 * types should be OCTET STRINGs internally irrespective of the type.
740 * So instead just check for UNIVERSAL class and ignore the tag.
742 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
746 /* Append a final null to string */
747 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
748 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
752 cont = (const unsigned char *)buf.data;
759 /* We now have content length and type: translate into a structure */
760 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
761 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
768 OPENSSL_free(buf.data);
772 /* Translate ASN1 content octets into a structure */
774 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
775 int utype, char *free_cont, const ASN1_ITEM *it)
777 ASN1_VALUE **opval = NULL;
779 ASN1_TYPE *typ = NULL;
781 const ASN1_PRIMITIVE_FUNCS *pf;
785 if (pf && pf->prim_c2i)
786 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
787 /* If ANY type clear type and set pointer to internal value */
788 if (it->utype == V_ASN1_ANY) {
790 typ = ASN1_TYPE_new();
793 *pval = (ASN1_VALUE *)typ;
795 typ = (ASN1_TYPE *)*pval;
797 if (utype != typ->type)
798 ASN1_TYPE_set(typ, utype, NULL);
800 pval = &typ->value.asn1_value;
804 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
810 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
813 *pval = (ASN1_VALUE *)1;
818 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
822 tbool = (ASN1_BOOLEAN *)pval;
827 case V_ASN1_BIT_STRING:
828 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
833 case V_ASN1_ENUMERATED:
834 tint = (ASN1_INTEGER **)pval;
835 if (!c2i_ASN1_INTEGER(tint, &cont, len))
837 /* Fixup type to match the expected form */
838 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
841 case V_ASN1_OCTET_STRING:
842 case V_ASN1_NUMERICSTRING:
843 case V_ASN1_PRINTABLESTRING:
844 case V_ASN1_T61STRING:
845 case V_ASN1_VIDEOTEXSTRING:
846 case V_ASN1_IA5STRING:
848 case V_ASN1_GENERALIZEDTIME:
849 case V_ASN1_GRAPHICSTRING:
850 case V_ASN1_VISIBLESTRING:
851 case V_ASN1_GENERALSTRING:
852 case V_ASN1_UNIVERSALSTRING:
853 case V_ASN1_BMPSTRING:
854 case V_ASN1_UTF8STRING:
857 case V_ASN1_SEQUENCE:
859 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
860 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
863 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
864 ASN1err(ASN1_F_ASN1_EX_C2I,
865 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
868 /* All based on ASN1_STRING and handled the same */
870 stmp = ASN1_STRING_type_new(utype);
872 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
875 *pval = (ASN1_VALUE *)stmp;
877 stmp = (ASN1_STRING *)*pval;
880 /* If we've already allocated a buffer use it */
882 OPENSSL_free(stmp->data);
883 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
887 if (!ASN1_STRING_set(stmp, cont, len)) {
888 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
889 ASN1_STRING_free(stmp);
896 /* If ASN1_ANY and NULL type fix up value */
897 if (typ && (utype == V_ASN1_NULL))
898 typ->value.ptr = NULL;
911 * This function finds the end of an ASN1 structure when passed its maximum
912 * length, whether it is indefinite length and a pointer to the content. This
913 * is more efficient than calling asn1_collect because it does not recurse on
914 * each indefinite length header.
917 static int asn1_find_end(const unsigned char **in, long len, char inf)
919 uint32_t expected_eoc;
921 const unsigned char *p = *in, *q;
922 /* If not indefinite length constructed just add length */
929 * Indefinite length constructed form. Find the end when enough EOCs are
930 * found. If more indefinite length constructed headers are encountered
931 * increment the expected eoc count otherwise just skip to the end of the
935 if (asn1_check_eoc(&p, len)) {
937 if (expected_eoc == 0)
943 /* Just read in a header: only care about the length */
944 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
946 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
950 if (expected_eoc == UINT32_MAX) {
951 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
961 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
969 * This function collects the asn1 data from a constructed string type into
970 * a buffer. The values of 'in' and 'len' should refer to the contents of the
971 * constructed type and 'inf' should be set if it is indefinite length.
974 #ifndef ASN1_MAX_STRING_NEST
976 * This determines how many levels of recursion are permitted in ASN1 string
977 * types. If it is not limited stack overflows can occur. If set to zero no
978 * recursion is allowed at all. Although zero should be adequate examples
979 * exist that require a value of 1. So 5 should be more than enough.
981 # define ASN1_MAX_STRING_NEST 5
984 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
985 char inf, int tag, int aclass, int depth)
987 const unsigned char *p, *q;
993 * If no buffer and not indefinite length constructed just pass over the
1003 if (asn1_check_eoc(&p, len)) {
1005 * EOC is illegal outside indefinite length constructed form
1008 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1015 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1016 len, tag, aclass, 0, NULL)) {
1017 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1021 /* If indefinite length constructed update max length */
1023 if (depth >= ASN1_MAX_STRING_NEST) {
1024 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1027 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1029 } else if (plen && !collect_data(buf, &p, plen))
1034 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1041 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1046 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1047 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1050 memcpy(buf->data + len, *p, plen);
1056 /* Check for ASN1 EOC and swallow it if found */
1058 static int asn1_check_eoc(const unsigned char **in, long len)
1060 const unsigned char *p;
1064 if (!p[0] && !p[1]) {
1072 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1073 * length for indefinite length constructed form, we don't know the exact
1074 * length but we can set an upper bound to the amount of data available minus
1075 * the header length just read.
1078 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1079 char *inf, char *cst,
1080 const unsigned char **in, long len,
1081 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1086 const unsigned char *p, *q;
1090 if (ctx && ctx->valid) {
1093 pclass = ctx->pclass;
1097 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1101 ctx->pclass = pclass;
1103 ctx->hdrlen = p - q;
1106 * If definite length, and no error, length + header can't exceed
1107 * total amount of data available.
1109 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1110 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1111 asn1_tlc_clear(ctx);
1118 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1119 asn1_tlc_clear(ctx);
1123 if ((exptag != ptag) || (expclass != pclass)) {
1125 * If type is OPTIONAL, not an error: indicate missing type.
1129 asn1_tlc_clear(ctx);
1130 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1134 * We have a tag and class match: assume we are going to do something
1137 asn1_tlc_clear(ctx);
1141 plen = len - (p - q);
1147 *cst = i & V_ASN1_CONSTRUCTED;