2 * Copyright 2000-2021 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, 0, 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) != NULL) (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)
125 if (pval == NULL || it == NULL) {
126 ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
129 rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
131 ASN1_item_ex_free(pval, it);
136 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
137 * tag mismatch return -1 to handle OPTIONAL
140 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
141 long len, const ASN1_ITEM *it,
142 int tag, int aclass, char opt, ASN1_TLC *ctx,
145 const ASN1_TEMPLATE *tt, *errtt = NULL;
146 const ASN1_EXTERN_FUNCS *ef;
148 ASN1_aux_cb *asn1_cb;
149 const unsigned char *p = NULL, *q;
150 unsigned char oclass;
151 char seq_eoc, seq_nolen, cst, isopt;
158 if (pval == NULL || it == NULL) {
159 ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
163 if (aux && aux->asn1_cb)
164 asn1_cb = aux->asn1_cb;
168 if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
169 ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
174 case ASN1_ITYPE_PRIMITIVE:
177 * tagging or OPTIONAL is currently illegal on an item template
178 * because the flags can't get passed down. In practice this
179 * isn't a problem: we include the relevant flags from the item
180 * template in the template itself.
182 if ((tag != -1) || opt) {
183 ERR_raise(ERR_LIB_ASN1,
184 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
187 return asn1_template_ex_d2i(pval, in, len,
188 it->templates, opt, ctx, depth);
190 return asn1_d2i_ex_primitive(pval, in, len, it,
191 tag, aclass, opt, ctx);
193 case ASN1_ITYPE_MSTRING:
195 * It never makes sense for multi-strings to have implicit tagging, so
196 * if tag != -1, then this looks like an error in the template.
199 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
204 /* Just read in tag and class */
205 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
206 &p, len, -1, 0, 1, ctx);
208 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
212 /* Must be UNIVERSAL class */
213 if (oclass != V_ASN1_UNIVERSAL) {
214 /* If OPTIONAL, assume this is OK */
217 ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
221 /* Check tag matches bit map */
222 if (!(ASN1_tag2bit(otag) & it->utype)) {
223 /* If OPTIONAL, assume this is OK */
226 ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
229 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
231 case ASN1_ITYPE_EXTERN:
232 /* Use new style d2i */
234 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
236 case ASN1_ITYPE_CHOICE:
238 * It never makes sense for CHOICE types to have implicit tagging, so
239 * if tag != -1, then this looks like an error in the template.
242 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
246 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
249 /* Free up and zero CHOICE value if initialised */
250 i = ossl_asn1_get_choice_selector(pval, it);
251 if ((i >= 0) && (i < it->tcount)) {
252 tt = it->templates + i;
253 pchptr = ossl_asn1_get_field_ptr(pval, tt);
254 ossl_asn1_template_free(pchptr, tt);
255 ossl_asn1_set_choice_selector(pval, -1, it);
257 } else if (!ASN1_item_ex_new(pval, it)) {
258 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
261 /* CHOICE type, try each possibility in turn */
263 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
264 pchptr = ossl_asn1_get_field_ptr(pval, tt);
266 * We mark field as OPTIONAL so its absence can be recognised.
268 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
269 /* If field not present, try the next one */
272 /* If positive return, read OK, break loop */
276 * Must be an ASN1 parsing error.
277 * Free up any partial choice value
279 ossl_asn1_template_free(pchptr, tt);
281 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
285 /* Did we fall off the end without reading anything? */
286 if (i == it->tcount) {
287 /* If OPTIONAL, this is OK */
289 /* Free and zero it */
290 ASN1_item_ex_free(pval, it);
293 ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
297 ossl_asn1_set_choice_selector(pval, i, it);
299 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
304 case ASN1_ITYPE_NDEF_SEQUENCE:
305 case ASN1_ITYPE_SEQUENCE:
309 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
311 tag = V_ASN1_SEQUENCE;
312 aclass = V_ASN1_UNIVERSAL;
314 /* Get SEQUENCE length and update len, p */
315 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
316 &p, len, tag, aclass, opt, ctx);
318 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
320 } else if (ret == -1)
322 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
323 len = tmplen - (p - *in);
326 /* If indefinite we don't do a length check */
330 ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
334 if (*pval == NULL && !ASN1_item_ex_new(pval, it)) {
335 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
339 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
342 /* Free up and zero any ADB found */
343 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
344 if (tt->flags & ASN1_TFLG_ADB_MASK) {
345 const ASN1_TEMPLATE *seqtt;
346 ASN1_VALUE **pseqval;
347 seqtt = ossl_asn1_do_adb(*pval, tt, 0);
350 pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
351 ossl_asn1_template_free(pseqval, seqtt);
355 /* Get each field entry */
356 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
357 const ASN1_TEMPLATE *seqtt;
358 ASN1_VALUE **pseqval;
359 seqtt = ossl_asn1_do_adb(*pval, tt, 1);
362 pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
363 /* Have we ran out of data? */
367 if (asn1_check_eoc(&p, len)) {
369 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
377 * This determines the OPTIONAL flag value. The field cannot be
378 * omitted if it is the last of a SEQUENCE and there is still
379 * data to be read. This isn't strictly necessary but it
380 * increases efficiency in some cases.
382 if (i == (it->tcount - 1))
385 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
387 * attempt to read in field, allowing each to be OPTIONAL
390 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
395 } else if (ret == -1) {
397 * OPTIONAL component absent. Free and zero the field.
399 ossl_asn1_template_free(pseqval, seqtt);
406 /* Check for EOC if expecting one */
407 if (seq_eoc && !asn1_check_eoc(&p, len)) {
408 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
411 /* Check all data read */
412 if (!seq_nolen && len) {
413 ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
418 * If we get here we've got no more data in the SEQUENCE, however we
419 * may not have read all fields so check all remaining are OPTIONAL
420 * and clear any that are.
422 for (; i < it->tcount; tt++, i++) {
423 const ASN1_TEMPLATE *seqtt;
424 seqtt = ossl_asn1_do_adb(*pval, tt, 1);
427 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
428 ASN1_VALUE **pseqval;
429 pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
430 ossl_asn1_template_free(pseqval, seqtt);
433 ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
438 if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
440 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
449 ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
452 ERR_add_error_data(4, "Field=", errtt->field_name,
453 ", Type=", it->sname);
455 ERR_add_error_data(2, "Type=", it->sname);
460 * Templates are handled with two separate functions. One handles any
461 * EXPLICIT tag and the other handles the rest.
464 static int asn1_template_ex_d2i(ASN1_VALUE **val,
465 const unsigned char **in, long inlen,
466 const ASN1_TEMPLATE *tt, char opt,
467 ASN1_TLC *ctx, int depth)
472 const unsigned char *p, *q;
477 aclass = flags & ASN1_TFLG_TAG_CLASS;
481 /* Check if EXPLICIT tag expected */
482 if (flags & ASN1_TFLG_EXPTAG) {
485 * Need to work out amount of data available to the inner content and
486 * where it starts: so read in EXPLICIT header to get the info.
488 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
489 &p, inlen, tt->tag, aclass, opt, ctx);
492 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
494 } else if (ret == -1)
497 ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
500 /* We've found the field so it can't be OPTIONAL now */
501 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
503 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
506 /* We read the field in OK so update length */
509 /* If NDEF we must have an EOC here */
510 if (!asn1_check_eoc(&p, len)) {
511 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
516 * Otherwise we must hit the EXPLICIT tag end or its an error
519 ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
524 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
533 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
534 const unsigned char **in, long len,
535 const ASN1_TEMPLATE *tt, char opt,
536 ASN1_TLC *ctx, int depth)
541 const unsigned char *p, *q;
545 aclass = flags & ASN1_TFLG_TAG_CLASS;
550 * If field is embedded then val needs fixing so it is a pointer to
551 * a pointer to a field.
553 if (tt->flags & ASN1_TFLG_EMBED) {
554 tval = (ASN1_VALUE *)val;
558 if (flags & ASN1_TFLG_SK_MASK) {
559 /* SET OF, SEQUENCE OF */
562 /* First work out expected inner tag value */
563 if (flags & ASN1_TFLG_IMPTAG) {
567 skaclass = V_ASN1_UNIVERSAL;
568 if (flags & ASN1_TFLG_SET_OF)
571 sktag = V_ASN1_SEQUENCE;
574 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
575 &p, len, sktag, skaclass, opt, ctx);
577 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
579 } else if (ret == -1)
582 *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
585 * We've got a valid STACK: free up any items present
587 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
589 while (sk_ASN1_VALUE_num(sktmp) > 0) {
590 vtmp = sk_ASN1_VALUE_pop(sktmp);
591 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
596 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
600 /* Read as many items as we can */
604 /* See if EOC found */
605 if (asn1_check_eoc(&p, len)) {
607 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
615 if (!asn1_item_embed_d2i(&skfield, &p, len,
616 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
618 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
619 /* |skfield| may be partially allocated despite failure. */
620 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
624 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
625 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
626 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
631 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
634 } else if (flags & ASN1_TFLG_IMPTAG) {
635 /* IMPLICIT tagging */
636 ret = asn1_item_embed_d2i(val, &p, len,
637 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
640 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
642 } else if (ret == -1)
645 /* Nothing special */
646 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
647 -1, 0, opt, ctx, depth);
649 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
651 } else if (ret == -1)
662 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
663 const unsigned char **in, long inlen,
665 int tag, int aclass, char opt, ASN1_TLC *ctx)
669 char cst, inf, free_cont = 0;
670 const unsigned char *p;
671 BUF_MEM buf = { 0, NULL, 0, 0 };
672 const unsigned char *cont = NULL;
676 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL);
677 return 0; /* Should never happen */
680 if (it->itype == ASN1_ITYPE_MSTRING) {
686 if (utype == V_ASN1_ANY) {
687 /* If type is ANY need to figure out type from tag */
688 unsigned char oclass;
690 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
694 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
698 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
699 &p, inlen, -1, 0, 0, ctx);
701 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
704 if (oclass != V_ASN1_UNIVERSAL)
705 utype = V_ASN1_OTHER;
709 aclass = V_ASN1_UNIVERSAL;
713 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
714 &p, inlen, tag, aclass, opt, ctx);
716 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
718 } else if (ret == -1)
721 /* SEQUENCE, SET and "OTHER" are left in encoded form */
722 if ((utype == V_ASN1_SEQUENCE)
723 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
725 * Clear context cache for type OTHER because the auto clear when we
726 * have a exact match won't work
728 if (utype == V_ASN1_OTHER) {
731 /* SEQUENCE and SET must be constructed */
733 ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
738 /* If indefinite length constructed find the real end */
740 if (!asn1_find_end(&p, plen, inf))
744 len = p - cont + plen;
748 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
749 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
750 || utype == V_ASN1_ENUMERATED) {
751 ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
755 /* Free any returned 'buf' content */
758 * Should really check the internal tags are correct but some things
759 * may get this wrong. The relevant specs say that constructed string
760 * types should be OCTET STRINGs internally irrespective of the type.
761 * So instead just check for UNIVERSAL class and ignore the tag.
763 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
767 /* Append a final null to string */
768 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
769 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
773 cont = (const unsigned char *)buf.data;
780 /* We now have content length and type: translate into a structure */
781 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
782 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
789 OPENSSL_free(buf.data);
793 /* Translate ASN1 content octets into a structure */
795 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
796 int utype, char *free_cont, const ASN1_ITEM *it)
798 ASN1_VALUE **opval = NULL;
800 ASN1_TYPE *typ = NULL;
802 const ASN1_PRIMITIVE_FUNCS *pf;
806 if (pf && pf->prim_c2i)
807 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
808 /* If ANY type clear type and set pointer to internal value */
809 if (it->utype == V_ASN1_ANY) {
811 typ = ASN1_TYPE_new();
814 *pval = (ASN1_VALUE *)typ;
816 typ = (ASN1_TYPE *)*pval;
818 if (utype != typ->type)
819 ASN1_TYPE_set(typ, utype, NULL);
821 pval = &typ->value.asn1_value;
825 if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
831 ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
834 *pval = (ASN1_VALUE *)1;
839 ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
843 tbool = (ASN1_BOOLEAN *)pval;
848 case V_ASN1_BIT_STRING:
849 if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
854 case V_ASN1_ENUMERATED:
855 tint = (ASN1_INTEGER **)pval;
856 if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
858 /* Fixup type to match the expected form */
859 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
862 case V_ASN1_OCTET_STRING:
863 case V_ASN1_NUMERICSTRING:
864 case V_ASN1_PRINTABLESTRING:
865 case V_ASN1_T61STRING:
866 case V_ASN1_VIDEOTEXSTRING:
867 case V_ASN1_IA5STRING:
869 case V_ASN1_GENERALIZEDTIME:
870 case V_ASN1_GRAPHICSTRING:
871 case V_ASN1_VISIBLESTRING:
872 case V_ASN1_GENERALSTRING:
873 case V_ASN1_UNIVERSALSTRING:
874 case V_ASN1_BMPSTRING:
875 case V_ASN1_UTF8STRING:
878 case V_ASN1_SEQUENCE:
880 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
881 ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
884 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
885 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
888 /* All based on ASN1_STRING and handled the same */
890 stmp = ASN1_STRING_type_new(utype);
892 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
895 *pval = (ASN1_VALUE *)stmp;
897 stmp = (ASN1_STRING *)*pval;
900 /* If we've already allocated a buffer use it */
902 OPENSSL_free(stmp->data);
903 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
907 if (!ASN1_STRING_set(stmp, cont, len)) {
908 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
909 ASN1_STRING_free(stmp);
916 /* If ASN1_ANY and NULL type fix up value */
917 if (typ && (utype == V_ASN1_NULL))
918 typ->value.ptr = NULL;
931 * This function finds the end of an ASN1 structure when passed its maximum
932 * length, whether it is indefinite length and a pointer to the content. This
933 * is more efficient than calling asn1_collect because it does not recurse on
934 * each indefinite length header.
937 static int asn1_find_end(const unsigned char **in, long len, char inf)
939 uint32_t expected_eoc;
941 const unsigned char *p = *in, *q;
942 /* If not indefinite length constructed just add length */
949 * Indefinite length constructed form. Find the end when enough EOCs are
950 * found. If more indefinite length constructed headers are encountered
951 * increment the expected eoc count otherwise just skip to the end of the
955 if (asn1_check_eoc(&p, len)) {
957 if (expected_eoc == 0)
963 /* Just read in a header: only care about the length */
964 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
966 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
970 if (expected_eoc == UINT32_MAX) {
971 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
981 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
989 * This function collects the asn1 data from a constructed string type into
990 * a buffer. The values of 'in' and 'len' should refer to the contents of the
991 * constructed type and 'inf' should be set if it is indefinite length.
994 #ifndef ASN1_MAX_STRING_NEST
996 * This determines how many levels of recursion are permitted in ASN1 string
997 * types. If it is not limited stack overflows can occur. If set to zero no
998 * recursion is allowed at all. Although zero should be adequate examples
999 * exist that require a value of 1. So 5 should be more than enough.
1001 # define ASN1_MAX_STRING_NEST 5
1004 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1005 char inf, int tag, int aclass, int depth)
1007 const unsigned char *p, *q;
1013 * If no buffer and not indefinite length constructed just pass over the
1023 if (asn1_check_eoc(&p, len)) {
1025 * EOC is illegal outside indefinite length constructed form
1028 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1035 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1036 len, tag, aclass, 0, NULL)) {
1037 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1041 /* If indefinite length constructed update max length */
1043 if (depth >= ASN1_MAX_STRING_NEST) {
1044 ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1047 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1049 } else if (plen && !collect_data(buf, &p, plen))
1054 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1061 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1066 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1067 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
1070 memcpy(buf->data + len, *p, plen);
1076 /* Check for ASN1 EOC and swallow it if found */
1078 static int asn1_check_eoc(const unsigned char **in, long len)
1080 const unsigned char *p;
1085 if (p[0] == '\0' && p[1] == '\0') {
1093 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1094 * length for indefinite length constructed form, we don't know the exact
1095 * length but we can set an upper bound to the amount of data available minus
1096 * the header length just read.
1099 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1100 char *inf, char *cst,
1101 const unsigned char **in, long len,
1102 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1107 const unsigned char *p, *q;
1111 if (ctx != NULL && ctx->valid) {
1114 pclass = ctx->pclass;
1118 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1122 ctx->pclass = pclass;
1124 ctx->hdrlen = p - q;
1127 * If definite length, and no error, length + header can't exceed
1128 * total amount of data available.
1130 if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
1131 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1132 asn1_tlc_clear(ctx);
1138 if ((i & 0x80) != 0)
1141 if (exptag != ptag || expclass != pclass) {
1143 * If type is OPTIONAL, not an error: indicate missing type.
1147 asn1_tlc_clear(ctx);
1148 ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1152 * We have a tag and class match: assume we are going to do something
1155 asn1_tlc_clear(ctx);
1159 plen = len - (p - q);
1165 *cst = i & V_ASN1_CONSTRUCTED;
1180 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1181 asn1_tlc_clear(ctx);