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"
20 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
21 long len, const ASN1_ITEM *it,
22 int tag, int aclass, char opt, ASN1_TLC *ctx);
24 static int asn1_check_eoc(const unsigned char **in, long len);
25 static int asn1_find_end(const unsigned char **in, long len, char inf);
27 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
28 char inf, int tag, int aclass, int depth);
30 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
32 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
34 const unsigned char **in, long len,
35 int exptag, int expclass, char opt, ASN1_TLC *ctx);
37 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
38 const unsigned char **in, long len,
39 const ASN1_TEMPLATE *tt, char opt,
41 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
42 const unsigned char **in, long len,
43 const ASN1_TEMPLATE *tt, char opt,
45 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
46 const unsigned char **in, long len,
48 int tag, int aclass, char opt,
50 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
51 int utype, char *free_cont, const ASN1_ITEM *it);
53 /* Table to convert tags to bit values, used for MSTRING type */
54 static const unsigned long tag2bit[32] = {
56 0, 0, 0, B_ASN1_BIT_STRING,
58 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
60 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
62 B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
64 B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
66 B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
68 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
70 B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
72 B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
75 unsigned long ASN1_tag2bit(int tag)
77 if ((tag < 0) || (tag > 30))
82 /* Macro to initialize and invalidate the cache */
84 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
85 /* Version to avoid compiler warning about 'c' always non-NULL */
86 #define asn1_tlc_clear_nc(c) (c)->valid = 0
89 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
90 * function. 'in' points to a buffer to read the data from, in future we
91 * will have more advanced versions that can input data a piece at a time and
92 * this will simply be a special case.
95 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
96 const unsigned char **in, long len,
100 ASN1_VALUE *ptmpval = NULL;
103 asn1_tlc_clear_nc(&c);
104 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
109 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
111 int tag, int aclass, char opt, ASN1_TLC *ctx)
114 rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx);
116 ASN1_item_ex_free(pval, it);
121 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
122 * tag mismatch return -1 to handle OPTIONAL
125 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
126 long len, const ASN1_ITEM *it,
127 int tag, int aclass, char opt, ASN1_TLC *ctx)
129 const ASN1_TEMPLATE *tt, *errtt = NULL;
130 const ASN1_EXTERN_FUNCS *ef;
131 const ASN1_AUX *aux = it->funcs;
132 ASN1_aux_cb *asn1_cb;
133 const unsigned char *p = NULL, *q;
134 unsigned char oclass;
135 char seq_eoc, seq_nolen, cst, isopt;
143 if (aux && aux->asn1_cb)
144 asn1_cb = aux->asn1_cb;
149 case ASN1_ITYPE_PRIMITIVE:
152 * tagging or OPTIONAL is currently illegal on an item template
153 * because the flags can't get passed down. In practice this
154 * isn't a problem: we include the relevant flags from the item
155 * template in the template itself.
157 if ((tag != -1) || opt) {
158 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
159 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
162 return asn1_template_ex_d2i(pval, in, len,
163 it->templates, opt, ctx);
165 return asn1_d2i_ex_primitive(pval, in, len, it,
166 tag, aclass, opt, ctx);
168 case ASN1_ITYPE_MSTRING:
170 /* Just read in tag and class */
171 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
172 &p, len, -1, 0, 1, ctx);
174 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
178 /* Must be UNIVERSAL class */
179 if (oclass != V_ASN1_UNIVERSAL) {
180 /* If OPTIONAL, assume this is OK */
183 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
186 /* Check tag matches bit map */
187 if (!(ASN1_tag2bit(otag) & it->utype)) {
188 /* If OPTIONAL, assume this is OK */
191 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
194 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
196 case ASN1_ITYPE_EXTERN:
197 /* Use new style d2i */
199 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
201 case ASN1_ITYPE_CHOICE:
202 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
205 /* Free up and zero CHOICE value if initialised */
206 i = asn1_get_choice_selector(pval, it);
207 if ((i >= 0) && (i < it->tcount)) {
208 tt = it->templates + i;
209 pchptr = asn1_get_field_ptr(pval, tt);
210 asn1_template_free(pchptr, tt);
211 asn1_set_choice_selector(pval, -1, it);
213 } else if (!ASN1_item_ex_new(pval, it)) {
214 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
217 /* CHOICE type, try each possibility in turn */
219 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
220 pchptr = asn1_get_field_ptr(pval, tt);
222 * We mark field as OPTIONAL so its absence can be recognised.
224 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
225 /* If field not present, try the next one */
228 /* If positive return, read OK, break loop */
232 * Must be an ASN1 parsing error.
233 * Free up any partial choice value
235 asn1_template_free(pchptr, tt);
237 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
241 /* Did we fall off the end without reading anything? */
242 if (i == it->tcount) {
243 /* If OPTIONAL, this is OK */
245 /* Free and zero it */
246 ASN1_item_ex_free(pval, it);
249 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
253 asn1_set_choice_selector(pval, i, it);
255 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
260 case ASN1_ITYPE_NDEF_SEQUENCE:
261 case ASN1_ITYPE_SEQUENCE:
265 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
267 tag = V_ASN1_SEQUENCE;
268 aclass = V_ASN1_UNIVERSAL;
270 /* Get SEQUENCE length and update len, p */
271 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
272 &p, len, tag, aclass, opt, ctx);
274 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
276 } else if (ret == -1)
278 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
279 len = tmplen - (p - *in);
282 /* If indefinite we don't do a length check */
286 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
290 if (!*pval && !ASN1_item_ex_new(pval, it)) {
291 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
295 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
298 /* Free up and zero any ADB found */
299 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
300 if (tt->flags & ASN1_TFLG_ADB_MASK) {
301 const ASN1_TEMPLATE *seqtt;
302 ASN1_VALUE **pseqval;
303 seqtt = asn1_do_adb(pval, tt, 0);
306 pseqval = asn1_get_field_ptr(pval, seqtt);
307 asn1_template_free(pseqval, seqtt);
311 /* Get each field entry */
312 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
313 const ASN1_TEMPLATE *seqtt;
314 ASN1_VALUE **pseqval;
315 seqtt = asn1_do_adb(pval, tt, 1);
318 pseqval = asn1_get_field_ptr(pval, seqtt);
319 /* Have we ran out of data? */
323 if (asn1_check_eoc(&p, len)) {
325 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
334 * This determines the OPTIONAL flag value. The field cannot be
335 * omitted if it is the last of a SEQUENCE and there is still
336 * data to be read. This isn't strictly necessary but it
337 * increases efficiency in some cases.
339 if (i == (it->tcount - 1))
342 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
344 * attempt to read in field, allowing each to be OPTIONAL
347 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
351 } else if (ret == -1) {
353 * OPTIONAL component absent. Free and zero the field.
355 asn1_template_free(pseqval, seqtt);
362 /* Check for EOC if expecting one */
363 if (seq_eoc && !asn1_check_eoc(&p, len)) {
364 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
367 /* Check all data read */
368 if (!seq_nolen && len) {
369 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
374 * If we get here we've got no more data in the SEQUENCE, however we
375 * may not have read all fields so check all remaining are OPTIONAL
376 * and clear any that are.
378 for (; i < it->tcount; tt++, i++) {
379 const ASN1_TEMPLATE *seqtt;
380 seqtt = asn1_do_adb(pval, tt, 1);
383 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
384 ASN1_VALUE **pseqval;
385 pseqval = asn1_get_field_ptr(pval, seqtt);
386 asn1_template_free(pseqval, seqtt);
389 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
394 if (!asn1_enc_save(pval, *in, p - *in, it))
396 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
405 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
408 ERR_add_error_data(4, "Field=", errtt->field_name,
409 ", Type=", it->sname);
411 ERR_add_error_data(2, "Type=", it->sname);
416 * Templates are handled with two separate functions. One handles any
417 * EXPLICIT tag and the other handles the rest.
420 static int asn1_template_ex_d2i(ASN1_VALUE **val,
421 const unsigned char **in, long inlen,
422 const ASN1_TEMPLATE *tt, char opt,
428 const unsigned char *p, *q;
433 aclass = flags & ASN1_TFLG_TAG_CLASS;
437 /* Check if EXPLICIT tag expected */
438 if (flags & ASN1_TFLG_EXPTAG) {
441 * Need to work out amount of data available to the inner content and
442 * where it starts: so read in EXPLICIT header to get the info.
444 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
445 &p, inlen, tt->tag, aclass, opt, ctx);
448 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
450 } else if (ret == -1)
453 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
454 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
457 /* We've found the field so it can't be OPTIONAL now */
458 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
460 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
463 /* We read the field in OK so update length */
466 /* If NDEF we must have an EOC here */
467 if (!asn1_check_eoc(&p, len)) {
468 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
473 * Otherwise we must hit the EXPLICIT tag end or its an error
476 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
477 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
482 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
491 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
492 const unsigned char **in, long len,
493 const ASN1_TEMPLATE *tt, char opt,
499 const unsigned char *p, *q;
503 aclass = flags & ASN1_TFLG_TAG_CLASS;
509 * If field is embedded then val needs fixing so it is a pointer to
510 * a pointer to a field.
512 if (tt->flags & ASN1_TFLG_EMBED) {
513 tval = (ASN1_VALUE *)val;
517 if (flags & ASN1_TFLG_SK_MASK) {
518 /* SET OF, SEQUENCE OF */
521 /* First work out expected inner tag value */
522 if (flags & ASN1_TFLG_IMPTAG) {
526 skaclass = V_ASN1_UNIVERSAL;
527 if (flags & ASN1_TFLG_SET_OF)
530 sktag = V_ASN1_SEQUENCE;
533 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
534 &p, len, sktag, skaclass, opt, ctx);
536 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
538 } else if (ret == -1)
541 *val = (ASN1_VALUE *)OPENSSL_sk_new_null();
544 * We've got a valid STACK: free up any items present
546 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
548 while (sk_ASN1_VALUE_num(sktmp) > 0) {
549 vtmp = sk_ASN1_VALUE_pop(sktmp);
550 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
555 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
559 /* Read as many items as we can */
563 /* See if EOC found */
564 if (asn1_check_eoc(&p, len)) {
566 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
567 ASN1_R_UNEXPECTED_EOC);
575 if (!asn1_item_embed_d2i(&skfield, &p, len,
576 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
577 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
578 ERR_R_NESTED_ASN1_ERROR);
579 /* |skfield| may be partially allocated despite failure. */
580 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
584 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
585 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
586 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
591 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
594 } else if (flags & ASN1_TFLG_IMPTAG) {
595 /* IMPLICIT tagging */
596 ret = asn1_item_embed_d2i(val, &p, len,
597 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
600 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
602 } else if (ret == -1)
605 /* Nothing special */
606 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
609 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
611 } else if (ret == -1)
622 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
623 const unsigned char **in, long inlen,
625 int tag, int aclass, char opt, ASN1_TLC *ctx)
629 char cst, inf, free_cont = 0;
630 const unsigned char *p;
631 BUF_MEM buf = { 0, NULL, 0, 0 };
632 const unsigned char *cont = NULL;
635 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
636 return 0; /* Should never happen */
639 if (it->itype == ASN1_ITYPE_MSTRING) {
645 if (utype == V_ASN1_ANY) {
646 /* If type is ANY need to figure out type from tag */
647 unsigned char oclass;
649 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
653 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
654 ASN1_R_ILLEGAL_OPTIONAL_ANY);
658 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
659 &p, inlen, -1, 0, 0, ctx);
661 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
664 if (oclass != V_ASN1_UNIVERSAL)
665 utype = V_ASN1_OTHER;
669 aclass = V_ASN1_UNIVERSAL;
673 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
674 &p, inlen, tag, aclass, opt, ctx);
676 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
678 } else if (ret == -1)
681 /* SEQUENCE, SET and "OTHER" are left in encoded form */
682 if ((utype == V_ASN1_SEQUENCE)
683 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
685 * Clear context cache for type OTHER because the auto clear when we
686 * have a exact match won't work
688 if (utype == V_ASN1_OTHER) {
691 /* SEQUENCE and SET must be constructed */
693 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
694 ASN1_R_TYPE_NOT_CONSTRUCTED);
699 /* If indefinite length constructed find the real end */
701 if (!asn1_find_end(&p, plen, inf))
705 len = p - cont + plen;
709 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
710 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
711 || utype == V_ASN1_ENUMERATED) {
712 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
716 /* Free any returned 'buf' content */
719 * Should really check the internal tags are correct but some things
720 * may get this wrong. The relevant specs say that constructed string
721 * types should be OCTET STRINGs internally irrespective of the type.
722 * So instead just check for UNIVERSAL class and ignore the tag.
724 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
728 /* Append a final null to string */
729 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
730 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
734 cont = (const unsigned char *)buf.data;
741 /* We now have content length and type: translate into a structure */
742 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
743 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
750 OPENSSL_free(buf.data);
754 /* Translate ASN1 content octets into a structure */
756 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
757 int utype, char *free_cont, const ASN1_ITEM *it)
759 ASN1_VALUE **opval = NULL;
761 ASN1_TYPE *typ = NULL;
763 const ASN1_PRIMITIVE_FUNCS *pf;
767 if (pf && pf->prim_c2i)
768 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
769 /* If ANY type clear type and set pointer to internal value */
770 if (it->utype == V_ASN1_ANY) {
772 typ = ASN1_TYPE_new();
775 *pval = (ASN1_VALUE *)typ;
777 typ = (ASN1_TYPE *)*pval;
779 if (utype != typ->type)
780 ASN1_TYPE_set(typ, utype, NULL);
782 pval = &typ->value.asn1_value;
786 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
792 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
795 *pval = (ASN1_VALUE *)1;
800 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
804 tbool = (ASN1_BOOLEAN *)pval;
809 case V_ASN1_BIT_STRING:
810 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
815 case V_ASN1_ENUMERATED:
816 tint = (ASN1_INTEGER **)pval;
817 if (!c2i_ASN1_INTEGER(tint, &cont, len))
819 /* Fixup type to match the expected form */
820 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
823 case V_ASN1_OCTET_STRING:
824 case V_ASN1_NUMERICSTRING:
825 case V_ASN1_PRINTABLESTRING:
826 case V_ASN1_T61STRING:
827 case V_ASN1_VIDEOTEXSTRING:
828 case V_ASN1_IA5STRING:
830 case V_ASN1_GENERALIZEDTIME:
831 case V_ASN1_GRAPHICSTRING:
832 case V_ASN1_VISIBLESTRING:
833 case V_ASN1_GENERALSTRING:
834 case V_ASN1_UNIVERSALSTRING:
835 case V_ASN1_BMPSTRING:
836 case V_ASN1_UTF8STRING:
839 case V_ASN1_SEQUENCE:
841 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
842 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
845 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
846 ASN1err(ASN1_F_ASN1_EX_C2I,
847 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
850 /* All based on ASN1_STRING and handled the same */
852 stmp = ASN1_STRING_type_new(utype);
854 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
857 *pval = (ASN1_VALUE *)stmp;
859 stmp = (ASN1_STRING *)*pval;
862 /* If we've already allocated a buffer use it */
864 OPENSSL_free(stmp->data);
865 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
869 if (!ASN1_STRING_set(stmp, cont, len)) {
870 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
871 ASN1_STRING_free(stmp);
878 /* If ASN1_ANY and NULL type fix up value */
879 if (typ && (utype == V_ASN1_NULL))
880 typ->value.ptr = NULL;
893 * This function finds the end of an ASN1 structure when passed its maximum
894 * length, whether it is indefinite length and a pointer to the content. This
895 * is more efficient than calling asn1_collect because it does not recurse on
896 * each indefinite length header.
899 static int asn1_find_end(const unsigned char **in, long len, char inf)
901 uint32_t expected_eoc;
903 const unsigned char *p = *in, *q;
904 /* If not indefinite length constructed just add length */
911 * Indefinite length constructed form. Find the end when enough EOCs are
912 * found. If more indefinite length constructed headers are encountered
913 * increment the expected eoc count otherwise just skip to the end of the
917 if (asn1_check_eoc(&p, len)) {
919 if (expected_eoc == 0)
925 /* Just read in a header: only care about the length */
926 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
928 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
932 if (expected_eoc == UINT32_MAX) {
933 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
943 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
951 * This function collects the asn1 data from a constructed string type into
952 * a buffer. The values of 'in' and 'len' should refer to the contents of the
953 * constructed type and 'inf' should be set if it is indefinite length.
956 #ifndef ASN1_MAX_STRING_NEST
958 * This determines how many levels of recursion are permitted in ASN1 string
959 * types. If it is not limited stack overflows can occur. If set to zero no
960 * recursion is allowed at all. Although zero should be adequate examples
961 * exist that require a value of 1. So 5 should be more than enough.
963 # define ASN1_MAX_STRING_NEST 5
966 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
967 char inf, int tag, int aclass, int depth)
969 const unsigned char *p, *q;
975 * If no buffer and not indefinite length constructed just pass over the
985 if (asn1_check_eoc(&p, len)) {
987 * EOC is illegal outside indefinite length constructed form
990 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
997 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
998 len, tag, aclass, 0, NULL)) {
999 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1003 /* If indefinite length constructed update max length */
1005 if (depth >= ASN1_MAX_STRING_NEST) {
1006 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1009 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1011 } else if (plen && !collect_data(buf, &p, plen))
1016 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1023 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1028 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1029 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1032 memcpy(buf->data + len, *p, plen);
1038 /* Check for ASN1 EOC and swallow it if found */
1040 static int asn1_check_eoc(const unsigned char **in, long len)
1042 const unsigned char *p;
1046 if (!p[0] && !p[1]) {
1054 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1055 * length for indefinite length constructed form, we don't know the exact
1056 * length but we can set an upper bound to the amount of data available minus
1057 * the header length just read.
1060 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1061 char *inf, char *cst,
1062 const unsigned char **in, long len,
1063 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1068 const unsigned char *p, *q;
1072 if (ctx && ctx->valid) {
1075 pclass = ctx->pclass;
1079 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1083 ctx->pclass = pclass;
1085 ctx->hdrlen = p - q;
1088 * If definite length, and no error, length + header can't exceed
1089 * total amount of data available.
1091 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1092 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1093 asn1_tlc_clear(ctx);
1100 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1101 asn1_tlc_clear(ctx);
1105 if ((exptag != ptag) || (expclass != pclass)) {
1107 * If type is OPTIONAL, not an error: indicate missing type.
1111 asn1_tlc_clear(ctx);
1112 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1116 * We have a tag and class match: assume we are going to do something
1119 asn1_tlc_clear(ctx);
1123 plen = len - (p - q);
1129 *cst = i & V_ASN1_CONSTRUCTED;