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) do { if ((c) != NULL) (c)->valid = 0; } while (0)
94 /* Version to avoid compiler warning about 'c' always non-NULL */
95 #define asn1_tlc_clear_nc(c) do {(c)->valid = 0; } while (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 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
167 if (aux && aux->asn1_cb)
168 asn1_cb = aux->asn1_cb;
172 if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
173 ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
178 case ASN1_ITYPE_PRIMITIVE:
181 * tagging or OPTIONAL is currently illegal on an item template
182 * because the flags can't get passed down. In practice this
183 * isn't a problem: we include the relevant flags from the item
184 * template in the template itself.
186 if ((tag != -1) || opt) {
187 ERR_raise(ERR_LIB_ASN1,
188 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
191 return asn1_template_ex_d2i(pval, in, len,
192 it->templates, opt, ctx, depth);
194 return asn1_d2i_ex_primitive(pval, in, len, it,
195 tag, aclass, opt, ctx);
197 case ASN1_ITYPE_MSTRING:
199 * It never makes sense for multi-strings to have implicit tagging, so
200 * if tag != -1, then this looks like an error in the template.
203 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
208 /* Just read in tag and class */
209 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
210 &p, len, -1, 0, 1, ctx);
212 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
216 /* Must be UNIVERSAL class */
217 if (oclass != V_ASN1_UNIVERSAL) {
218 /* If OPTIONAL, assume this is OK */
221 ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
225 /* Check tag matches bit map */
226 if (!(ASN1_tag2bit(otag) & it->utype)) {
227 /* If OPTIONAL, assume this is OK */
230 ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
233 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
235 case ASN1_ITYPE_EXTERN:
236 /* Use new style d2i */
238 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
240 case ASN1_ITYPE_CHOICE:
242 * It never makes sense for CHOICE types to have implicit tagging, so
243 * if tag != -1, then this looks like an error in the template.
246 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
250 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
253 /* Free up and zero CHOICE value if initialised */
254 i = ossl_asn1_get_choice_selector(pval, it);
255 if ((i >= 0) && (i < it->tcount)) {
256 tt = it->templates + i;
257 pchptr = ossl_asn1_get_field_ptr(pval, tt);
258 ossl_asn1_template_free(pchptr, tt);
259 ossl_asn1_set_choice_selector(pval, -1, it);
261 } else if (!ASN1_item_ex_new(pval, it)) {
262 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
265 /* CHOICE type, try each possibility in turn */
267 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
268 pchptr = ossl_asn1_get_field_ptr(pval, tt);
270 * We mark field as OPTIONAL so its absence can be recognised.
272 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
273 /* If field not present, try the next one */
276 /* If positive return, read OK, break loop */
280 * Must be an ASN1 parsing error.
281 * Free up any partial choice value
283 ossl_asn1_template_free(pchptr, tt);
285 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
289 /* Did we fall off the end without reading anything? */
290 if (i == it->tcount) {
291 /* If OPTIONAL, this is OK */
293 /* Free and zero it */
294 ASN1_item_ex_free(pval, it);
297 ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
301 ossl_asn1_set_choice_selector(pval, i, it);
303 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
308 case ASN1_ITYPE_NDEF_SEQUENCE:
309 case ASN1_ITYPE_SEQUENCE:
313 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
315 tag = V_ASN1_SEQUENCE;
316 aclass = V_ASN1_UNIVERSAL;
318 /* Get SEQUENCE length and update len, p */
319 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
320 &p, len, tag, aclass, opt, ctx);
322 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
324 } else if (ret == -1)
326 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
327 len = tmplen - (p - *in);
330 /* If indefinite we don't do a length check */
334 ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
338 if (*pval == NULL && !ASN1_item_ex_new(pval, it)) {
339 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
343 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
346 /* Free up and zero any ADB found */
347 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
348 if (tt->flags & ASN1_TFLG_ADB_MASK) {
349 const ASN1_TEMPLATE *seqtt;
350 ASN1_VALUE **pseqval;
351 seqtt = ossl_asn1_do_adb(*pval, tt, 0);
354 pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
355 ossl_asn1_template_free(pseqval, seqtt);
359 /* Get each field entry */
360 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
361 const ASN1_TEMPLATE *seqtt;
362 ASN1_VALUE **pseqval;
363 seqtt = ossl_asn1_do_adb(*pval, tt, 1);
366 pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
367 /* Have we ran out of data? */
371 if (asn1_check_eoc(&p, len)) {
373 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
381 * This determines the OPTIONAL flag value. The field cannot be
382 * omitted if it is the last of a SEQUENCE and there is still
383 * data to be read. This isn't strictly necessary but it
384 * increases efficiency in some cases.
386 if (i == (it->tcount - 1))
389 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
391 * attempt to read in field, allowing each to be OPTIONAL
394 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
399 } else if (ret == -1) {
401 * OPTIONAL component absent. Free and zero the field.
403 ossl_asn1_template_free(pseqval, seqtt);
410 /* Check for EOC if expecting one */
411 if (seq_eoc && !asn1_check_eoc(&p, len)) {
412 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
415 /* Check all data read */
416 if (!seq_nolen && len) {
417 ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
422 * If we get here we've got no more data in the SEQUENCE, however we
423 * may not have read all fields so check all remaining are OPTIONAL
424 * and clear any that are.
426 for (; i < it->tcount; tt++, i++) {
427 const ASN1_TEMPLATE *seqtt;
428 seqtt = ossl_asn1_do_adb(*pval, tt, 1);
431 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
432 ASN1_VALUE **pseqval;
433 pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
434 ossl_asn1_template_free(pseqval, seqtt);
437 ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
442 if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
444 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
453 ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
456 ERR_add_error_data(4, "Field=", errtt->field_name,
457 ", Type=", it->sname);
459 ERR_add_error_data(2, "Type=", it->sname);
464 * Templates are handled with two separate functions. One handles any
465 * EXPLICIT tag and the other handles the rest.
468 static int asn1_template_ex_d2i(ASN1_VALUE **val,
469 const unsigned char **in, long inlen,
470 const ASN1_TEMPLATE *tt, char opt,
471 ASN1_TLC *ctx, int depth)
476 const unsigned char *p, *q;
481 aclass = flags & ASN1_TFLG_TAG_CLASS;
485 /* Check if EXPLICIT tag expected */
486 if (flags & ASN1_TFLG_EXPTAG) {
489 * Need to work out amount of data available to the inner content and
490 * where it starts: so read in EXPLICIT header to get the info.
492 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
493 &p, inlen, tt->tag, aclass, opt, ctx);
496 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
498 } else if (ret == -1)
501 ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
504 /* We've found the field so it can't be OPTIONAL now */
505 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
507 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
510 /* We read the field in OK so update length */
513 /* If NDEF we must have an EOC here */
514 if (!asn1_check_eoc(&p, len)) {
515 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
520 * Otherwise we must hit the EXPLICIT tag end or its an error
523 ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
528 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
537 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
538 const unsigned char **in, long len,
539 const ASN1_TEMPLATE *tt, char opt,
540 ASN1_TLC *ctx, int depth)
545 const unsigned char *p, *q;
549 aclass = flags & ASN1_TFLG_TAG_CLASS;
554 * If field is embedded then val needs fixing so it is a pointer to
555 * a pointer to a field.
557 if (tt->flags & ASN1_TFLG_EMBED) {
558 tval = (ASN1_VALUE *)val;
562 if (flags & ASN1_TFLG_SK_MASK) {
563 /* SET OF, SEQUENCE OF */
566 /* First work out expected inner tag value */
567 if (flags & ASN1_TFLG_IMPTAG) {
571 skaclass = V_ASN1_UNIVERSAL;
572 if (flags & ASN1_TFLG_SET_OF)
575 sktag = V_ASN1_SEQUENCE;
578 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
579 &p, len, sktag, skaclass, opt, ctx);
581 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
583 } else if (ret == -1)
586 *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
589 * We've got a valid STACK: free up any items present
591 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
593 while (sk_ASN1_VALUE_num(sktmp) > 0) {
594 vtmp = sk_ASN1_VALUE_pop(sktmp);
595 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
600 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
604 /* Read as many items as we can */
608 /* See if EOC found */
609 if (asn1_check_eoc(&p, len)) {
611 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
619 if (!asn1_item_embed_d2i(&skfield, &p, len,
620 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
622 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
623 /* |skfield| may be partially allocated despite failure. */
624 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
628 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
629 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
630 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
635 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
638 } else if (flags & ASN1_TFLG_IMPTAG) {
639 /* IMPLICIT tagging */
640 ret = asn1_item_embed_d2i(val, &p, len,
641 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
644 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
646 } else if (ret == -1)
649 /* Nothing special */
650 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
651 -1, 0, opt, ctx, depth);
653 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
655 } else if (ret == -1)
666 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
667 const unsigned char **in, long inlen,
669 int tag, int aclass, char opt, ASN1_TLC *ctx)
673 char cst, inf, free_cont = 0;
674 const unsigned char *p;
675 BUF_MEM buf = { 0, NULL, 0, 0 };
676 const unsigned char *cont = NULL;
680 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL);
681 return 0; /* Should never happen */
684 if (it->itype == ASN1_ITYPE_MSTRING) {
690 if (utype == V_ASN1_ANY) {
691 /* If type is ANY need to figure out type from tag */
692 unsigned char oclass;
694 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
698 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
702 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
703 &p, inlen, -1, 0, 0, ctx);
705 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
708 if (oclass != V_ASN1_UNIVERSAL)
709 utype = V_ASN1_OTHER;
713 aclass = V_ASN1_UNIVERSAL;
717 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
718 &p, inlen, tag, aclass, opt, ctx);
720 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
722 } else if (ret == -1)
725 /* SEQUENCE, SET and "OTHER" are left in encoded form */
726 if ((utype == V_ASN1_SEQUENCE)
727 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
729 * Clear context cache for type OTHER because the auto clear when we
730 * have a exact match won't work
732 if (utype == V_ASN1_OTHER) {
735 /* SEQUENCE and SET must be constructed */
737 ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
742 /* If indefinite length constructed find the real end */
744 if (!asn1_find_end(&p, plen, inf))
748 len = p - cont + plen;
752 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
753 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
754 || utype == V_ASN1_ENUMERATED) {
755 ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
759 /* Free any returned 'buf' content */
762 * Should really check the internal tags are correct but some things
763 * may get this wrong. The relevant specs say that constructed string
764 * types should be OCTET STRINGs internally irrespective of the type.
765 * So instead just check for UNIVERSAL class and ignore the tag.
767 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
771 /* Append a final null to string */
772 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
773 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
777 cont = (const unsigned char *)buf.data;
784 /* We now have content length and type: translate into a structure */
785 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
786 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
793 OPENSSL_free(buf.data);
797 /* Translate ASN1 content octets into a structure */
799 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
800 int utype, char *free_cont, const ASN1_ITEM *it)
802 ASN1_VALUE **opval = NULL;
804 ASN1_TYPE *typ = NULL;
806 const ASN1_PRIMITIVE_FUNCS *pf;
810 if (pf && pf->prim_c2i)
811 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
812 /* If ANY type clear type and set pointer to internal value */
813 if (it->utype == V_ASN1_ANY) {
815 typ = ASN1_TYPE_new();
818 *pval = (ASN1_VALUE *)typ;
820 typ = (ASN1_TYPE *)*pval;
822 if (utype != typ->type)
823 ASN1_TYPE_set(typ, utype, NULL);
825 pval = &typ->value.asn1_value;
829 if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
835 ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
838 *pval = (ASN1_VALUE *)1;
843 ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
847 tbool = (ASN1_BOOLEAN *)pval;
852 case V_ASN1_BIT_STRING:
853 if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
858 case V_ASN1_ENUMERATED:
859 tint = (ASN1_INTEGER **)pval;
860 if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
862 /* Fixup type to match the expected form */
863 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
866 case V_ASN1_OCTET_STRING:
867 case V_ASN1_NUMERICSTRING:
868 case V_ASN1_PRINTABLESTRING:
869 case V_ASN1_T61STRING:
870 case V_ASN1_VIDEOTEXSTRING:
871 case V_ASN1_IA5STRING:
873 case V_ASN1_GENERALIZEDTIME:
874 case V_ASN1_GRAPHICSTRING:
875 case V_ASN1_VISIBLESTRING:
876 case V_ASN1_GENERALSTRING:
877 case V_ASN1_UNIVERSALSTRING:
878 case V_ASN1_BMPSTRING:
879 case V_ASN1_UTF8STRING:
882 case V_ASN1_SEQUENCE:
884 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
885 ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
888 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
889 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
892 /* All based on ASN1_STRING and handled the same */
894 stmp = ASN1_STRING_type_new(utype);
896 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
899 *pval = (ASN1_VALUE *)stmp;
901 stmp = (ASN1_STRING *)*pval;
904 /* If we've already allocated a buffer use it */
906 OPENSSL_free(stmp->data);
907 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
911 if (!ASN1_STRING_set(stmp, cont, len)) {
912 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
913 ASN1_STRING_free(stmp);
920 /* If ASN1_ANY and NULL type fix up value */
921 if (typ && (utype == V_ASN1_NULL))
922 typ->value.ptr = NULL;
935 * This function finds the end of an ASN1 structure when passed its maximum
936 * length, whether it is indefinite length and a pointer to the content. This
937 * is more efficient than calling asn1_collect because it does not recurse on
938 * each indefinite length header.
941 static int asn1_find_end(const unsigned char **in, long len, char inf)
943 uint32_t expected_eoc;
945 const unsigned char *p = *in, *q;
946 /* If not indefinite length constructed just add length */
953 * Indefinite length constructed form. Find the end when enough EOCs are
954 * found. If more indefinite length constructed headers are encountered
955 * increment the expected eoc count otherwise just skip to the end of the
959 if (asn1_check_eoc(&p, len)) {
961 if (expected_eoc == 0)
967 /* Just read in a header: only care about the length */
968 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
970 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
974 if (expected_eoc == UINT32_MAX) {
975 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
985 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
993 * This function collects the asn1 data from a constructed string type into
994 * a buffer. The values of 'in' and 'len' should refer to the contents of the
995 * constructed type and 'inf' should be set if it is indefinite length.
998 #ifndef ASN1_MAX_STRING_NEST
1000 * This determines how many levels of recursion are permitted in ASN1 string
1001 * types. If it is not limited stack overflows can occur. If set to zero no
1002 * recursion is allowed at all. Although zero should be adequate examples
1003 * exist that require a value of 1. So 5 should be more than enough.
1005 # define ASN1_MAX_STRING_NEST 5
1008 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1009 char inf, int tag, int aclass, int depth)
1011 const unsigned char *p, *q;
1017 * If no buffer and not indefinite length constructed just pass over the
1027 if (asn1_check_eoc(&p, len)) {
1029 * EOC is illegal outside indefinite length constructed form
1032 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1039 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1040 len, tag, aclass, 0, NULL)) {
1041 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1045 /* If indefinite length constructed update max length */
1047 if (depth >= ASN1_MAX_STRING_NEST) {
1048 ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1051 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1053 } else if (plen && !collect_data(buf, &p, plen))
1058 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1065 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1070 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1071 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
1074 memcpy(buf->data + len, *p, plen);
1080 /* Check for ASN1 EOC and swallow it if found */
1082 static int asn1_check_eoc(const unsigned char **in, long len)
1084 const unsigned char *p;
1089 if (p[0] == '\0' && p[1] == '\0') {
1097 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1098 * length for indefinite length constructed form, we don't know the exact
1099 * length but we can set an upper bound to the amount of data available minus
1100 * the header length just read.
1103 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1104 char *inf, char *cst,
1105 const unsigned char **in, long len,
1106 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1111 const unsigned char *p, *q;
1116 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1119 if (ctx != NULL && ctx->valid) {
1122 pclass = ctx->pclass;
1126 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1130 ctx->pclass = pclass;
1132 ctx->hdrlen = p - q;
1135 * If definite length, and no error, length + header can't exceed
1136 * total amount of data available.
1138 if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
1139 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1145 if ((i & 0x80) != 0) {
1146 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1150 if (exptag != ptag || expclass != pclass) {
1152 * If type is OPTIONAL, not an error: indicate missing type.
1156 ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1160 * We have a tag and class match: assume we are going to do something
1163 asn1_tlc_clear(ctx);
1167 plen = len - (p - q);
1173 *cst = i & V_ASN1_CONSTRUCTED;
1188 asn1_tlc_clear(ctx);