2 * Copyright 2000-2018 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"
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;
114 asn1_tlc_clear_nc(&c);
115 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
120 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
122 int tag, int aclass, char opt, ASN1_TLC *ctx)
125 rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
127 ASN1_item_ex_free(pval, it);
132 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
133 * tag mismatch return -1 to handle OPTIONAL
136 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
137 long len, const ASN1_ITEM *it,
138 int tag, int aclass, char opt, ASN1_TLC *ctx,
141 const ASN1_TEMPLATE *tt, *errtt = NULL;
142 const ASN1_EXTERN_FUNCS *ef;
143 const ASN1_AUX *aux = it->funcs;
144 ASN1_aux_cb *asn1_cb;
145 const unsigned char *p = NULL, *q;
146 unsigned char oclass;
147 char seq_eoc, seq_nolen, cst, isopt;
156 if (aux && aux->asn1_cb)
157 asn1_cb = aux->asn1_cb;
161 if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
162 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NESTED_TOO_DEEP);
167 case ASN1_ITYPE_PRIMITIVE:
170 * tagging or OPTIONAL is currently illegal on an item template
171 * because the flags can't get passed down. In practice this
172 * isn't a problem: we include the relevant flags from the item
173 * template in the template itself.
175 if ((tag != -1) || opt) {
176 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
177 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
180 return asn1_template_ex_d2i(pval, in, len,
181 it->templates, opt, ctx, depth);
183 return asn1_d2i_ex_primitive(pval, in, len, it,
184 tag, aclass, opt, ctx);
186 case ASN1_ITYPE_MSTRING:
188 /* Just read in tag and class */
189 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
190 &p, len, -1, 0, 1, ctx);
192 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
196 /* Must be UNIVERSAL class */
197 if (oclass != V_ASN1_UNIVERSAL) {
198 /* If OPTIONAL, assume this is OK */
201 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
204 /* Check tag matches bit map */
205 if (!(ASN1_tag2bit(otag) & it->utype)) {
206 /* If OPTIONAL, assume this is OK */
209 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
212 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
214 case ASN1_ITYPE_EXTERN:
215 /* Use new style d2i */
217 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
219 case ASN1_ITYPE_CHOICE:
220 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
223 /* Free up and zero CHOICE value if initialised */
224 i = asn1_get_choice_selector(pval, it);
225 if ((i >= 0) && (i < it->tcount)) {
226 tt = it->templates + i;
227 pchptr = asn1_get_field_ptr(pval, tt);
228 asn1_template_free(pchptr, tt);
229 asn1_set_choice_selector(pval, -1, it);
231 } else if (!ASN1_item_ex_new(pval, it)) {
232 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
235 /* CHOICE type, try each possibility in turn */
237 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
238 pchptr = asn1_get_field_ptr(pval, tt);
240 * We mark field as OPTIONAL so its absence can be recognised.
242 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
243 /* If field not present, try the next one */
246 /* If positive return, read OK, break loop */
250 * Must be an ASN1 parsing error.
251 * Free up any partial choice value
253 asn1_template_free(pchptr, tt);
255 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
259 /* Did we fall off the end without reading anything? */
260 if (i == it->tcount) {
261 /* If OPTIONAL, this is OK */
263 /* Free and zero it */
264 ASN1_item_ex_free(pval, it);
267 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
271 asn1_set_choice_selector(pval, i, it);
273 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
278 case ASN1_ITYPE_NDEF_SEQUENCE:
279 case ASN1_ITYPE_SEQUENCE:
283 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
285 tag = V_ASN1_SEQUENCE;
286 aclass = V_ASN1_UNIVERSAL;
288 /* Get SEQUENCE length and update len, p */
289 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
290 &p, len, tag, aclass, opt, ctx);
292 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
294 } else if (ret == -1)
296 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
297 len = tmplen - (p - *in);
300 /* If indefinite we don't do a length check */
304 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
308 if (*pval == NULL && !ASN1_item_ex_new(pval, it)) {
309 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
313 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
316 /* Free up and zero any ADB found */
317 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
318 if (tt->flags & ASN1_TFLG_ADB_MASK) {
319 const ASN1_TEMPLATE *seqtt;
320 ASN1_VALUE **pseqval;
321 seqtt = asn1_do_adb(*pval, tt, 0);
324 pseqval = asn1_get_field_ptr(pval, seqtt);
325 asn1_template_free(pseqval, seqtt);
329 /* Get each field entry */
330 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
331 const ASN1_TEMPLATE *seqtt;
332 ASN1_VALUE **pseqval;
333 seqtt = asn1_do_adb(*pval, tt, 1);
336 pseqval = asn1_get_field_ptr(pval, seqtt);
337 /* Have we ran out of data? */
341 if (asn1_check_eoc(&p, len)) {
343 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
352 * This determines the OPTIONAL flag value. The field cannot be
353 * omitted if it is the last of a SEQUENCE and there is still
354 * data to be read. This isn't strictly necessary but it
355 * increases efficiency in some cases.
357 if (i == (it->tcount - 1))
360 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
362 * attempt to read in field, allowing each to be OPTIONAL
365 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
370 } else if (ret == -1) {
372 * OPTIONAL component absent. Free and zero the field.
374 asn1_template_free(pseqval, seqtt);
381 /* Check for EOC if expecting one */
382 if (seq_eoc && !asn1_check_eoc(&p, len)) {
383 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
386 /* Check all data read */
387 if (!seq_nolen && len) {
388 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
393 * If we get here we've got no more data in the SEQUENCE, however we
394 * may not have read all fields so check all remaining are OPTIONAL
395 * and clear any that are.
397 for (; i < it->tcount; tt++, i++) {
398 const ASN1_TEMPLATE *seqtt;
399 seqtt = asn1_do_adb(*pval, tt, 1);
402 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
403 ASN1_VALUE **pseqval;
404 pseqval = asn1_get_field_ptr(pval, seqtt);
405 asn1_template_free(pseqval, seqtt);
408 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
413 if (!asn1_enc_save(pval, *in, p - *in, it))
415 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
424 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
427 ERR_add_error_data(4, "Field=", errtt->field_name,
428 ", Type=", it->sname);
430 ERR_add_error_data(2, "Type=", it->sname);
435 * Templates are handled with two separate functions. One handles any
436 * EXPLICIT tag and the other handles the rest.
439 static int asn1_template_ex_d2i(ASN1_VALUE **val,
440 const unsigned char **in, long inlen,
441 const ASN1_TEMPLATE *tt, char opt,
442 ASN1_TLC *ctx, int depth)
447 const unsigned char *p, *q;
452 aclass = flags & ASN1_TFLG_TAG_CLASS;
456 /* Check if EXPLICIT tag expected */
457 if (flags & ASN1_TFLG_EXPTAG) {
460 * Need to work out amount of data available to the inner content and
461 * where it starts: so read in EXPLICIT header to get the info.
463 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
464 &p, inlen, tt->tag, aclass, opt, ctx);
467 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
469 } else if (ret == -1)
472 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
473 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
476 /* We've found the field so it can't be OPTIONAL now */
477 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
479 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
482 /* We read the field in OK so update length */
485 /* If NDEF we must have an EOC here */
486 if (!asn1_check_eoc(&p, len)) {
487 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
492 * Otherwise we must hit the EXPLICIT tag end or its an error
495 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
496 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
501 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
510 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
511 const unsigned char **in, long len,
512 const ASN1_TEMPLATE *tt, char opt,
513 ASN1_TLC *ctx, int depth)
518 const unsigned char *p, *q;
522 aclass = flags & ASN1_TFLG_TAG_CLASS;
528 * If field is embedded then val needs fixing so it is a pointer to
529 * a pointer to a field.
531 if (tt->flags & ASN1_TFLG_EMBED) {
532 tval = (ASN1_VALUE *)val;
536 if (flags & ASN1_TFLG_SK_MASK) {
537 /* SET OF, SEQUENCE OF */
540 /* First work out expected inner tag value */
541 if (flags & ASN1_TFLG_IMPTAG) {
545 skaclass = V_ASN1_UNIVERSAL;
546 if (flags & ASN1_TFLG_SET_OF)
549 sktag = V_ASN1_SEQUENCE;
552 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
553 &p, len, sktag, skaclass, opt, ctx);
555 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
557 } else if (ret == -1)
560 *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
563 * We've got a valid STACK: free up any items present
565 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
567 while (sk_ASN1_VALUE_num(sktmp) > 0) {
568 vtmp = sk_ASN1_VALUE_pop(sktmp);
569 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
574 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
578 /* Read as many items as we can */
582 /* See if EOC found */
583 if (asn1_check_eoc(&p, len)) {
585 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
586 ASN1_R_UNEXPECTED_EOC);
594 if (!asn1_item_embed_d2i(&skfield, &p, len,
595 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
597 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
598 ERR_R_NESTED_ASN1_ERROR);
599 /* |skfield| may be partially allocated despite failure. */
600 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
604 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
605 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
606 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
611 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
614 } else if (flags & ASN1_TFLG_IMPTAG) {
615 /* IMPLICIT tagging */
616 ret = asn1_item_embed_d2i(val, &p, len,
617 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
620 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
622 } else if (ret == -1)
625 /* Nothing special */
626 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
627 -1, 0, opt, ctx, depth);
629 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
631 } else if (ret == -1)
642 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
643 const unsigned char **in, long inlen,
645 int tag, int aclass, char opt, ASN1_TLC *ctx)
649 char cst, inf, free_cont = 0;
650 const unsigned char *p;
651 BUF_MEM buf = { 0, NULL, 0, 0 };
652 const unsigned char *cont = NULL;
656 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
657 return 0; /* Should never happen */
660 if (it->itype == ASN1_ITYPE_MSTRING) {
666 if (utype == V_ASN1_ANY) {
667 /* If type is ANY need to figure out type from tag */
668 unsigned char oclass;
670 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
674 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
675 ASN1_R_ILLEGAL_OPTIONAL_ANY);
679 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
680 &p, inlen, -1, 0, 0, ctx);
682 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
685 if (oclass != V_ASN1_UNIVERSAL)
686 utype = V_ASN1_OTHER;
690 aclass = V_ASN1_UNIVERSAL;
694 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
695 &p, inlen, tag, aclass, opt, ctx);
697 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
699 } else if (ret == -1)
702 /* SEQUENCE, SET and "OTHER" are left in encoded form */
703 if ((utype == V_ASN1_SEQUENCE)
704 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
706 * Clear context cache for type OTHER because the auto clear when we
707 * have a exact match won't work
709 if (utype == V_ASN1_OTHER) {
712 /* SEQUENCE and SET must be constructed */
714 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
715 ASN1_R_TYPE_NOT_CONSTRUCTED);
720 /* If indefinite length constructed find the real end */
722 if (!asn1_find_end(&p, plen, inf))
726 len = p - cont + plen;
730 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
731 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
732 || utype == V_ASN1_ENUMERATED) {
733 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
737 /* Free any returned 'buf' content */
740 * Should really check the internal tags are correct but some things
741 * may get this wrong. The relevant specs say that constructed string
742 * types should be OCTET STRINGs internally irrespective of the type.
743 * So instead just check for UNIVERSAL class and ignore the tag.
745 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
749 /* Append a final null to string */
750 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
751 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
755 cont = (const unsigned char *)buf.data;
762 /* We now have content length and type: translate into a structure */
763 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
764 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
771 OPENSSL_free(buf.data);
775 /* Translate ASN1 content octets into a structure */
777 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
778 int utype, char *free_cont, const ASN1_ITEM *it)
780 ASN1_VALUE **opval = NULL;
782 ASN1_TYPE *typ = NULL;
784 const ASN1_PRIMITIVE_FUNCS *pf;
788 if (pf && pf->prim_c2i)
789 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
790 /* If ANY type clear type and set pointer to internal value */
791 if (it->utype == V_ASN1_ANY) {
793 typ = ASN1_TYPE_new();
796 *pval = (ASN1_VALUE *)typ;
798 typ = (ASN1_TYPE *)*pval;
800 if (utype != typ->type)
801 ASN1_TYPE_set(typ, utype, NULL);
803 pval = &typ->value.asn1_value;
807 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
813 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
816 *pval = (ASN1_VALUE *)1;
821 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
825 tbool = (ASN1_BOOLEAN *)pval;
830 case V_ASN1_BIT_STRING:
831 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
836 case V_ASN1_ENUMERATED:
837 tint = (ASN1_INTEGER **)pval;
838 if (!c2i_ASN1_INTEGER(tint, &cont, len))
840 /* Fixup type to match the expected form */
841 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
844 case V_ASN1_OCTET_STRING:
845 case V_ASN1_NUMERICSTRING:
846 case V_ASN1_PRINTABLESTRING:
847 case V_ASN1_T61STRING:
848 case V_ASN1_VIDEOTEXSTRING:
849 case V_ASN1_IA5STRING:
851 case V_ASN1_GENERALIZEDTIME:
852 case V_ASN1_GRAPHICSTRING:
853 case V_ASN1_VISIBLESTRING:
854 case V_ASN1_GENERALSTRING:
855 case V_ASN1_UNIVERSALSTRING:
856 case V_ASN1_BMPSTRING:
857 case V_ASN1_UTF8STRING:
860 case V_ASN1_SEQUENCE:
862 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
863 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
866 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
867 ASN1err(ASN1_F_ASN1_EX_C2I,
868 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
871 /* All based on ASN1_STRING and handled the same */
873 stmp = ASN1_STRING_type_new(utype);
875 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
878 *pval = (ASN1_VALUE *)stmp;
880 stmp = (ASN1_STRING *)*pval;
883 /* If we've already allocated a buffer use it */
885 OPENSSL_free(stmp->data);
886 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
890 if (!ASN1_STRING_set(stmp, cont, len)) {
891 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
892 ASN1_STRING_free(stmp);
899 /* If ASN1_ANY and NULL type fix up value */
900 if (typ && (utype == V_ASN1_NULL))
901 typ->value.ptr = NULL;
914 * This function finds the end of an ASN1 structure when passed its maximum
915 * length, whether it is indefinite length and a pointer to the content. This
916 * is more efficient than calling asn1_collect because it does not recurse on
917 * each indefinite length header.
920 static int asn1_find_end(const unsigned char **in, long len, char inf)
922 uint32_t expected_eoc;
924 const unsigned char *p = *in, *q;
925 /* If not indefinite length constructed just add length */
932 * Indefinite length constructed form. Find the end when enough EOCs are
933 * found. If more indefinite length constructed headers are encountered
934 * increment the expected eoc count otherwise just skip to the end of the
938 if (asn1_check_eoc(&p, len)) {
940 if (expected_eoc == 0)
946 /* Just read in a header: only care about the length */
947 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
949 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
953 if (expected_eoc == UINT32_MAX) {
954 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
964 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
972 * This function collects the asn1 data from a constructed string type into
973 * a buffer. The values of 'in' and 'len' should refer to the contents of the
974 * constructed type and 'inf' should be set if it is indefinite length.
977 #ifndef ASN1_MAX_STRING_NEST
979 * This determines how many levels of recursion are permitted in ASN1 string
980 * types. If it is not limited stack overflows can occur. If set to zero no
981 * recursion is allowed at all. Although zero should be adequate examples
982 * exist that require a value of 1. So 5 should be more than enough.
984 # define ASN1_MAX_STRING_NEST 5
987 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
988 char inf, int tag, int aclass, int depth)
990 const unsigned char *p, *q;
996 * If no buffer and not indefinite length constructed just pass over the
1006 if (asn1_check_eoc(&p, len)) {
1008 * EOC is illegal outside indefinite length constructed form
1011 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1018 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1019 len, tag, aclass, 0, NULL)) {
1020 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1024 /* If indefinite length constructed update max length */
1026 if (depth >= ASN1_MAX_STRING_NEST) {
1027 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1030 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1032 } else if (plen && !collect_data(buf, &p, plen))
1037 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1044 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1049 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1050 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1053 memcpy(buf->data + len, *p, plen);
1059 /* Check for ASN1 EOC and swallow it if found */
1061 static int asn1_check_eoc(const unsigned char **in, long len)
1063 const unsigned char *p;
1068 if (p[0] == '\0' && p[1] == '\0') {
1076 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1077 * length for indefinite length constructed form, we don't know the exact
1078 * length but we can set an upper bound to the amount of data available minus
1079 * the header length just read.
1082 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1083 char *inf, char *cst,
1084 const unsigned char **in, long len,
1085 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1090 const unsigned char *p, *q;
1094 if (ctx && ctx->valid) {
1097 pclass = ctx->pclass;
1101 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1105 ctx->pclass = pclass;
1107 ctx->hdrlen = p - q;
1110 * If definite length, and no error, length + header can't exceed
1111 * total amount of data available.
1113 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1114 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1115 asn1_tlc_clear(ctx);
1122 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1123 asn1_tlc_clear(ctx);
1127 if ((exptag != ptag) || (expclass != pclass)) {
1129 * If type is OPTIONAL, not an error: indicate missing type.
1133 asn1_tlc_clear(ctx);
1134 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1138 * We have a tag and class match: assume we are going to do something
1141 asn1_tlc_clear(ctx);
1145 plen = len - (p - q);
1151 *cst = i & V_ASN1_CONSTRUCTED;