2 * Copyright 2000-2020 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, B_ASN1_UNKNOWN, 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) (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)
124 rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
126 ASN1_item_ex_free(pval, it);
131 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
132 * tag mismatch return -1 to handle OPTIONAL
135 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
136 long len, const ASN1_ITEM *it,
137 int tag, int aclass, char opt, ASN1_TLC *ctx,
140 const ASN1_TEMPLATE *tt, *errtt = NULL;
141 const ASN1_EXTERN_FUNCS *ef;
142 const ASN1_AUX *aux = it->funcs;
143 ASN1_aux_cb *asn1_cb;
144 const unsigned char *p = NULL, *q;
145 unsigned char oclass;
146 char seq_eoc, seq_nolen, cst, isopt;
155 if (aux && aux->asn1_cb)
156 asn1_cb = aux->asn1_cb;
160 if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
161 ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
166 case ASN1_ITYPE_PRIMITIVE:
169 * tagging or OPTIONAL is currently illegal on an item template
170 * because the flags can't get passed down. In practice this
171 * isn't a problem: we include the relevant flags from the item
172 * template in the template itself.
174 if ((tag != -1) || opt) {
175 ERR_raise(ERR_LIB_ASN1,
176 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
179 return asn1_template_ex_d2i(pval, in, len,
180 it->templates, opt, ctx, depth);
182 return asn1_d2i_ex_primitive(pval, in, len, it,
183 tag, aclass, opt, ctx);
185 case ASN1_ITYPE_MSTRING:
187 /* Just read in tag and class */
188 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
189 &p, len, -1, 0, 1, ctx);
191 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
195 /* Must be UNIVERSAL class */
196 if (oclass != V_ASN1_UNIVERSAL) {
197 /* If OPTIONAL, assume this is OK */
200 ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
203 /* Check tag matches bit map */
204 if (!(ASN1_tag2bit(otag) & it->utype)) {
205 /* If OPTIONAL, assume this is OK */
208 ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
211 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
213 case ASN1_ITYPE_EXTERN:
214 /* Use new style d2i */
216 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
218 case ASN1_ITYPE_CHOICE:
219 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
222 /* Free up and zero CHOICE value if initialised */
223 i = asn1_get_choice_selector(pval, it);
224 if ((i >= 0) && (i < it->tcount)) {
225 tt = it->templates + i;
226 pchptr = asn1_get_field_ptr(pval, tt);
227 asn1_template_free(pchptr, tt);
228 asn1_set_choice_selector(pval, -1, it);
230 } else if (!ASN1_item_ex_new(pval, it)) {
231 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
234 /* CHOICE type, try each possibility in turn */
236 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
237 pchptr = asn1_get_field_ptr(pval, tt);
239 * We mark field as OPTIONAL so its absence can be recognised.
241 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
242 /* If field not present, try the next one */
245 /* If positive return, read OK, break loop */
249 * Must be an ASN1 parsing error.
250 * Free up any partial choice value
252 asn1_template_free(pchptr, tt);
254 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
258 /* Did we fall off the end without reading anything? */
259 if (i == it->tcount) {
260 /* If OPTIONAL, this is OK */
262 /* Free and zero it */
263 ASN1_item_ex_free(pval, it);
266 ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
270 asn1_set_choice_selector(pval, i, it);
272 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
277 case ASN1_ITYPE_NDEF_SEQUENCE:
278 case ASN1_ITYPE_SEQUENCE:
282 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
284 tag = V_ASN1_SEQUENCE;
285 aclass = V_ASN1_UNIVERSAL;
287 /* Get SEQUENCE length and update len, p */
288 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
289 &p, len, tag, aclass, opt, ctx);
291 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
293 } else if (ret == -1)
295 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
296 len = tmplen - (p - *in);
299 /* If indefinite we don't do a length check */
303 ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
307 if (*pval == NULL && !ASN1_item_ex_new(pval, it)) {
308 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
312 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
315 /* Free up and zero any ADB found */
316 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
317 if (tt->flags & ASN1_TFLG_ADB_MASK) {
318 const ASN1_TEMPLATE *seqtt;
319 ASN1_VALUE **pseqval;
320 seqtt = asn1_do_adb(*pval, tt, 0);
323 pseqval = asn1_get_field_ptr(pval, seqtt);
324 asn1_template_free(pseqval, seqtt);
328 /* Get each field entry */
329 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
330 const ASN1_TEMPLATE *seqtt;
331 ASN1_VALUE **pseqval;
332 seqtt = asn1_do_adb(*pval, tt, 1);
335 pseqval = asn1_get_field_ptr(pval, seqtt);
336 /* Have we ran out of data? */
340 if (asn1_check_eoc(&p, len)) {
342 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
350 * This determines the OPTIONAL flag value. The field cannot be
351 * omitted if it is the last of a SEQUENCE and there is still
352 * data to be read. This isn't strictly necessary but it
353 * increases efficiency in some cases.
355 if (i == (it->tcount - 1))
358 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
360 * attempt to read in field, allowing each to be OPTIONAL
363 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
368 } else if (ret == -1) {
370 * OPTIONAL component absent. Free and zero the field.
372 asn1_template_free(pseqval, seqtt);
379 /* Check for EOC if expecting one */
380 if (seq_eoc && !asn1_check_eoc(&p, len)) {
381 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
384 /* Check all data read */
385 if (!seq_nolen && len) {
386 ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
391 * If we get here we've got no more data in the SEQUENCE, however we
392 * may not have read all fields so check all remaining are OPTIONAL
393 * and clear any that are.
395 for (; i < it->tcount; tt++, i++) {
396 const ASN1_TEMPLATE *seqtt;
397 seqtt = asn1_do_adb(*pval, tt, 1);
400 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
401 ASN1_VALUE **pseqval;
402 pseqval = asn1_get_field_ptr(pval, seqtt);
403 asn1_template_free(pseqval, seqtt);
406 ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
411 if (!asn1_enc_save(pval, *in, p - *in, it))
413 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
422 ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
425 ERR_add_error_data(4, "Field=", errtt->field_name,
426 ", Type=", it->sname);
428 ERR_add_error_data(2, "Type=", it->sname);
433 * Templates are handled with two separate functions. One handles any
434 * EXPLICIT tag and the other handles the rest.
437 static int asn1_template_ex_d2i(ASN1_VALUE **val,
438 const unsigned char **in, long inlen,
439 const ASN1_TEMPLATE *tt, char opt,
440 ASN1_TLC *ctx, int depth)
445 const unsigned char *p, *q;
450 aclass = flags & ASN1_TFLG_TAG_CLASS;
454 /* Check if EXPLICIT tag expected */
455 if (flags & ASN1_TFLG_EXPTAG) {
458 * Need to work out amount of data available to the inner content and
459 * where it starts: so read in EXPLICIT header to get the info.
461 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
462 &p, inlen, tt->tag, aclass, opt, ctx);
465 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
467 } else if (ret == -1)
470 ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
473 /* We've found the field so it can't be OPTIONAL now */
474 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
476 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
479 /* We read the field in OK so update length */
482 /* If NDEF we must have an EOC here */
483 if (!asn1_check_eoc(&p, len)) {
484 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
489 * Otherwise we must hit the EXPLICIT tag end or its an error
492 ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
497 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
506 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
507 const unsigned char **in, long len,
508 const ASN1_TEMPLATE *tt, char opt,
509 ASN1_TLC *ctx, int depth)
514 const unsigned char *p, *q;
518 aclass = flags & ASN1_TFLG_TAG_CLASS;
523 * If field is embedded then val needs fixing so it is a pointer to
524 * a pointer to a field.
526 if (tt->flags & ASN1_TFLG_EMBED) {
527 tval = (ASN1_VALUE *)val;
531 if (flags & ASN1_TFLG_SK_MASK) {
532 /* SET OF, SEQUENCE OF */
535 /* First work out expected inner tag value */
536 if (flags & ASN1_TFLG_IMPTAG) {
540 skaclass = V_ASN1_UNIVERSAL;
541 if (flags & ASN1_TFLG_SET_OF)
544 sktag = V_ASN1_SEQUENCE;
547 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
548 &p, len, sktag, skaclass, opt, ctx);
550 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
552 } else if (ret == -1)
555 *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
558 * We've got a valid STACK: free up any items present
560 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
562 while (sk_ASN1_VALUE_num(sktmp) > 0) {
563 vtmp = sk_ASN1_VALUE_pop(sktmp);
564 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
569 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
573 /* Read as many items as we can */
577 /* See if EOC found */
578 if (asn1_check_eoc(&p, len)) {
580 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
588 if (!asn1_item_embed_d2i(&skfield, &p, len,
589 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
591 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
592 /* |skfield| may be partially allocated despite failure. */
593 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
597 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
598 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
599 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
604 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
607 } else if (flags & ASN1_TFLG_IMPTAG) {
608 /* IMPLICIT tagging */
609 ret = asn1_item_embed_d2i(val, &p, len,
610 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
613 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
615 } else if (ret == -1)
618 /* Nothing special */
619 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
620 -1, 0, opt, ctx, depth);
622 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
624 } else if (ret == -1)
635 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
636 const unsigned char **in, long inlen,
638 int tag, int aclass, char opt, ASN1_TLC *ctx)
642 char cst, inf, free_cont = 0;
643 const unsigned char *p;
644 BUF_MEM buf = { 0, NULL, 0, 0 };
645 const unsigned char *cont = NULL;
649 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL);
650 return 0; /* Should never happen */
653 if (it->itype == ASN1_ITYPE_MSTRING) {
659 if (utype == V_ASN1_ANY) {
660 /* If type is ANY need to figure out type from tag */
661 unsigned char oclass;
663 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
667 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
671 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
672 &p, inlen, -1, 0, 0, ctx);
674 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
677 if (oclass != V_ASN1_UNIVERSAL)
678 utype = V_ASN1_OTHER;
682 aclass = V_ASN1_UNIVERSAL;
686 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
687 &p, inlen, tag, aclass, opt, ctx);
689 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
691 } else if (ret == -1)
694 /* SEQUENCE, SET and "OTHER" are left in encoded form */
695 if ((utype == V_ASN1_SEQUENCE)
696 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
698 * Clear context cache for type OTHER because the auto clear when we
699 * have a exact match won't work
701 if (utype == V_ASN1_OTHER) {
704 /* SEQUENCE and SET must be constructed */
706 ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
711 /* If indefinite length constructed find the real end */
713 if (!asn1_find_end(&p, plen, inf))
717 len = p - cont + plen;
721 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
722 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
723 || utype == V_ASN1_ENUMERATED) {
724 ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
728 /* Free any returned 'buf' content */
731 * Should really check the internal tags are correct but some things
732 * may get this wrong. The relevant specs say that constructed string
733 * types should be OCTET STRINGs internally irrespective of the type.
734 * So instead just check for UNIVERSAL class and ignore the tag.
736 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
740 /* Append a final null to string */
741 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
742 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
746 cont = (const unsigned char *)buf.data;
753 /* We now have content length and type: translate into a structure */
754 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
755 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
762 OPENSSL_free(buf.data);
766 /* Translate ASN1 content octets into a structure */
768 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
769 int utype, char *free_cont, const ASN1_ITEM *it)
771 ASN1_VALUE **opval = NULL;
773 ASN1_TYPE *typ = NULL;
775 const ASN1_PRIMITIVE_FUNCS *pf;
779 if (pf && pf->prim_c2i)
780 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
781 /* If ANY type clear type and set pointer to internal value */
782 if (it->utype == V_ASN1_ANY) {
784 typ = ASN1_TYPE_new();
787 *pval = (ASN1_VALUE *)typ;
789 typ = (ASN1_TYPE *)*pval;
791 if (utype != typ->type)
792 ASN1_TYPE_set(typ, utype, NULL);
794 pval = &typ->value.asn1_value;
798 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
804 ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
807 *pval = (ASN1_VALUE *)1;
812 ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
816 tbool = (ASN1_BOOLEAN *)pval;
821 case V_ASN1_BIT_STRING:
822 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
827 case V_ASN1_ENUMERATED:
828 tint = (ASN1_INTEGER **)pval;
829 if (!c2i_ASN1_INTEGER(tint, &cont, len))
831 /* Fixup type to match the expected form */
832 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
835 case V_ASN1_OCTET_STRING:
836 case V_ASN1_NUMERICSTRING:
837 case V_ASN1_PRINTABLESTRING:
838 case V_ASN1_T61STRING:
839 case V_ASN1_VIDEOTEXSTRING:
840 case V_ASN1_IA5STRING:
842 case V_ASN1_GENERALIZEDTIME:
843 case V_ASN1_GRAPHICSTRING:
844 case V_ASN1_VISIBLESTRING:
845 case V_ASN1_GENERALSTRING:
846 case V_ASN1_UNIVERSALSTRING:
847 case V_ASN1_BMPSTRING:
848 case V_ASN1_UTF8STRING:
851 case V_ASN1_SEQUENCE:
853 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
854 ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
857 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
858 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
861 /* All based on ASN1_STRING and handled the same */
863 stmp = ASN1_STRING_type_new(utype);
865 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
868 *pval = (ASN1_VALUE *)stmp;
870 stmp = (ASN1_STRING *)*pval;
873 /* If we've already allocated a buffer use it */
875 OPENSSL_free(stmp->data);
876 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
880 if (!ASN1_STRING_set(stmp, cont, len)) {
881 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
882 ASN1_STRING_free(stmp);
889 /* If ASN1_ANY and NULL type fix up value */
890 if (typ && (utype == V_ASN1_NULL))
891 typ->value.ptr = NULL;
904 * This function finds the end of an ASN1 structure when passed its maximum
905 * length, whether it is indefinite length and a pointer to the content. This
906 * is more efficient than calling asn1_collect because it does not recurse on
907 * each indefinite length header.
910 static int asn1_find_end(const unsigned char **in, long len, char inf)
912 uint32_t expected_eoc;
914 const unsigned char *p = *in, *q;
915 /* If not indefinite length constructed just add length */
922 * Indefinite length constructed form. Find the end when enough EOCs are
923 * found. If more indefinite length constructed headers are encountered
924 * increment the expected eoc count otherwise just skip to the end of the
928 if (asn1_check_eoc(&p, len)) {
930 if (expected_eoc == 0)
936 /* Just read in a header: only care about the length */
937 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
939 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
943 if (expected_eoc == UINT32_MAX) {
944 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
954 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
962 * This function collects the asn1 data from a constructed string type into
963 * a buffer. The values of 'in' and 'len' should refer to the contents of the
964 * constructed type and 'inf' should be set if it is indefinite length.
967 #ifndef ASN1_MAX_STRING_NEST
969 * This determines how many levels of recursion are permitted in ASN1 string
970 * types. If it is not limited stack overflows can occur. If set to zero no
971 * recursion is allowed at all. Although zero should be adequate examples
972 * exist that require a value of 1. So 5 should be more than enough.
974 # define ASN1_MAX_STRING_NEST 5
977 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
978 char inf, int tag, int aclass, int depth)
980 const unsigned char *p, *q;
986 * If no buffer and not indefinite length constructed just pass over the
996 if (asn1_check_eoc(&p, len)) {
998 * EOC is illegal outside indefinite length constructed form
1001 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1008 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1009 len, tag, aclass, 0, NULL)) {
1010 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1014 /* If indefinite length constructed update max length */
1016 if (depth >= ASN1_MAX_STRING_NEST) {
1017 ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1020 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1022 } else if (plen && !collect_data(buf, &p, plen))
1027 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1034 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1039 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1040 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
1043 memcpy(buf->data + len, *p, plen);
1049 /* Check for ASN1 EOC and swallow it if found */
1051 static int asn1_check_eoc(const unsigned char **in, long len)
1053 const unsigned char *p;
1058 if (p[0] == '\0' && p[1] == '\0') {
1066 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1067 * length for indefinite length constructed form, we don't know the exact
1068 * length but we can set an upper bound to the amount of data available minus
1069 * the header length just read.
1072 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1073 char *inf, char *cst,
1074 const unsigned char **in, long len,
1075 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1080 const unsigned char *p, *q;
1084 if (ctx && ctx->valid) {
1087 pclass = ctx->pclass;
1091 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1095 ctx->pclass = pclass;
1097 ctx->hdrlen = p - q;
1100 * If definite length, and no error, length + header can't exceed
1101 * total amount of data available.
1103 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1104 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1105 asn1_tlc_clear(ctx);
1112 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1113 asn1_tlc_clear(ctx);
1117 if ((exptag != ptag) || (expclass != pclass)) {
1119 * If type is OPTIONAL, not an error: indicate missing type.
1123 asn1_tlc_clear(ctx);
1124 ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1128 * We have a tag and class match: assume we are going to do something
1131 asn1_tlc_clear(ctx);
1135 plen = len - (p - q);
1141 *cst = i & V_ASN1_CONSTRUCTED;