3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/objects.h>
65 #include <openssl/buffer.h>
66 #include <openssl/err.h>
67 #include "asn1_locl.h"
69 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
70 long len, const ASN1_ITEM *it,
71 int tag, int aclass, char opt, ASN1_TLC *ctx);
73 static int asn1_check_eoc(const unsigned char **in, long len);
74 static int asn1_find_end(const unsigned char **in, long len, char inf);
76 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
77 char inf, int tag, int aclass, int depth);
79 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
81 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
83 const unsigned char **in, long len,
84 int exptag, int expclass, char opt, ASN1_TLC *ctx);
86 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
87 const unsigned char **in, long len,
88 const ASN1_TEMPLATE *tt, char opt,
90 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
91 const unsigned char **in, long len,
92 const ASN1_TEMPLATE *tt, char opt,
94 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
95 const unsigned char **in, long len,
97 int tag, int aclass, char opt,
99 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
100 int utype, char *free_cont, const ASN1_ITEM *it);
102 /* Table to convert tags to bit values, used for MSTRING type */
103 static const unsigned long tag2bit[32] = {
105 0, 0, 0, B_ASN1_BIT_STRING,
107 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
109 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
111 B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
113 B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
115 B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
117 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
119 B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
121 B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
124 unsigned long ASN1_tag2bit(int tag)
126 if ((tag < 0) || (tag > 30))
131 /* Macro to initialize and invalidate the cache */
133 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
134 /* Version to avoid compiler warning about 'c' always non-NULL */
135 #define asn1_tlc_clear_nc(c) (c)->valid = 0
138 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
139 * function. 'in' points to a buffer to read the data from, in future we
140 * will have more advanced versions that can input data a piece at a time and
141 * this will simply be a special case.
144 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
145 const unsigned char **in, long len,
149 ASN1_VALUE *ptmpval = NULL;
152 asn1_tlc_clear_nc(&c);
153 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
158 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
160 int tag, int aclass, char opt, ASN1_TLC *ctx)
163 rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx);
165 ASN1_item_ex_free(pval, it);
170 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
171 * tag mismatch return -1 to handle OPTIONAL
174 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
175 long len, const ASN1_ITEM *it,
176 int tag, int aclass, char opt, ASN1_TLC *ctx)
178 const ASN1_TEMPLATE *tt, *errtt = NULL;
179 const ASN1_EXTERN_FUNCS *ef;
180 const ASN1_AUX *aux = it->funcs;
181 ASN1_aux_cb *asn1_cb;
182 const unsigned char *p = NULL, *q;
183 unsigned char oclass;
184 char seq_eoc, seq_nolen, cst, isopt;
192 if (aux && aux->asn1_cb)
193 asn1_cb = aux->asn1_cb;
198 case ASN1_ITYPE_PRIMITIVE:
201 * tagging or OPTIONAL is currently illegal on an item template
202 * because the flags can't get passed down. In practice this
203 * isn't a problem: we include the relevant flags from the item
204 * template in the template itself.
206 if ((tag != -1) || opt) {
207 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
208 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
211 return asn1_template_ex_d2i(pval, in, len,
212 it->templates, opt, ctx);
214 return asn1_d2i_ex_primitive(pval, in, len, it,
215 tag, aclass, opt, ctx);
217 case ASN1_ITYPE_MSTRING:
219 /* Just read in tag and class */
220 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
221 &p, len, -1, 0, 1, ctx);
223 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
227 /* Must be UNIVERSAL class */
228 if (oclass != V_ASN1_UNIVERSAL) {
229 /* If OPTIONAL, assume this is OK */
232 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
235 /* Check tag matches bit map */
236 if (!(ASN1_tag2bit(otag) & it->utype)) {
237 /* If OPTIONAL, assume this is OK */
240 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
243 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
245 case ASN1_ITYPE_EXTERN:
246 /* Use new style d2i */
248 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
250 case ASN1_ITYPE_CHOICE:
251 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
254 /* Free up and zero CHOICE value if initialised */
255 i = asn1_get_choice_selector(pval, it);
256 if ((i >= 0) && (i < it->tcount)) {
257 tt = it->templates + i;
258 pchptr = asn1_get_field_ptr(pval, tt);
259 asn1_template_free(pchptr, tt);
260 asn1_set_choice_selector(pval, -1, it);
262 } else if (!ASN1_item_ex_new(pval, it)) {
263 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
266 /* CHOICE type, try each possibility in turn */
268 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
269 pchptr = asn1_get_field_ptr(pval, tt);
271 * We mark field as OPTIONAL so its absence can be recognised.
273 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
274 /* If field not present, try the next one */
277 /* If positive return, read OK, break loop */
280 /* Otherwise must be an ASN1 parsing error */
282 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
286 /* Did we fall off the end without reading anything? */
287 if (i == it->tcount) {
288 /* If OPTIONAL, this is OK */
290 /* Free and zero it */
291 ASN1_item_ex_free(pval, it);
294 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
298 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 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, 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 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
334 if (!*pval && !ASN1_item_ex_new(pval, it)) {
335 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, 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 = asn1_do_adb(pval, tt, 1);
348 pseqval = asn1_get_field_ptr(pval, seqtt);
349 asn1_template_free(pseqval, seqtt);
353 /* Get each field entry */
354 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
355 const ASN1_TEMPLATE *seqtt;
356 ASN1_VALUE **pseqval;
357 seqtt = asn1_do_adb(pval, tt, 1);
360 pseqval = asn1_get_field_ptr(pval, seqtt);
361 /* Have we ran out of data? */
365 if (asn1_check_eoc(&p, len)) {
367 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
376 * This determines the OPTIONAL flag value. The field cannot be
377 * omitted if it is the last of a SEQUENCE and there is still
378 * data to be read. This isn't strictly necessary but it
379 * increases efficiency in some cases.
381 if (i == (it->tcount - 1))
384 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
386 * attempt to read in field, allowing each to be OPTIONAL
389 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
393 } else if (ret == -1) {
395 * OPTIONAL component absent. Free and zero the field.
397 asn1_template_free(pseqval, seqtt);
404 /* Check for EOC if expecting one */
405 if (seq_eoc && !asn1_check_eoc(&p, len)) {
406 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
409 /* Check all data read */
410 if (!seq_nolen && len) {
411 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
416 * If we get here we've got no more data in the SEQUENCE, however we
417 * may not have read all fields so check all remaining are OPTIONAL
418 * and clear any that are.
420 for (; i < it->tcount; tt++, i++) {
421 const ASN1_TEMPLATE *seqtt;
422 seqtt = asn1_do_adb(pval, tt, 1);
425 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
426 ASN1_VALUE **pseqval;
427 pseqval = asn1_get_field_ptr(pval, seqtt);
428 asn1_template_free(pseqval, seqtt);
431 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
436 if (!asn1_enc_save(pval, *in, p - *in, it))
438 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
447 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
450 ERR_add_error_data(4, "Field=", errtt->field_name,
451 ", Type=", it->sname);
453 ERR_add_error_data(2, "Type=", it->sname);
458 * Templates are handled with two separate functions. One handles any
459 * EXPLICIT tag and the other handles the rest.
462 static int asn1_template_ex_d2i(ASN1_VALUE **val,
463 const unsigned char **in, long inlen,
464 const ASN1_TEMPLATE *tt, char opt,
470 const unsigned char *p, *q;
475 aclass = flags & ASN1_TFLG_TAG_CLASS;
479 /* Check if EXPLICIT tag expected */
480 if (flags & ASN1_TFLG_EXPTAG) {
483 * Need to work out amount of data available to the inner content and
484 * where it starts: so read in EXPLICIT header to get the info.
486 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
487 &p, inlen, tt->tag, aclass, opt, ctx);
490 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
492 } else if (ret == -1)
495 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
496 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
499 /* We've found the field so it can't be OPTIONAL now */
500 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
502 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
505 /* We read the field in OK so update length */
508 /* If NDEF we must have an EOC here */
509 if (!asn1_check_eoc(&p, len)) {
510 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
515 * Otherwise we must hit the EXPLICIT tag end or its an error
518 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
519 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
524 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
533 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
534 const unsigned char **in, long len,
535 const ASN1_TEMPLATE *tt, char opt,
541 const unsigned char *p, *q;
545 aclass = flags & ASN1_TFLG_TAG_CLASS;
551 * If field is embedded then val needs fixing so it is a pointer to
552 * a pointer to a field.
554 if (tt->flags & ASN1_TFLG_EMBED) {
555 tval = (ASN1_VALUE *)val;
559 if (flags & ASN1_TFLG_SK_MASK) {
560 /* SET OF, SEQUENCE OF */
563 /* First work out expected inner tag value */
564 if (flags & ASN1_TFLG_IMPTAG) {
568 skaclass = V_ASN1_UNIVERSAL;
569 if (flags & ASN1_TFLG_SET_OF)
572 sktag = V_ASN1_SEQUENCE;
575 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
576 &p, len, sktag, skaclass, opt, ctx);
578 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
580 } else if (ret == -1)
583 *val = (ASN1_VALUE *)sk_new_null();
586 * We've got a valid STACK: free up any items present
588 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
590 while (sk_ASN1_VALUE_num(sktmp) > 0) {
591 vtmp = sk_ASN1_VALUE_pop(sktmp);
592 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
597 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
601 /* Read as many items as we can */
605 /* See if EOC found */
606 if (asn1_check_eoc(&p, len)) {
608 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
609 ASN1_R_UNEXPECTED_EOC);
617 if (!asn1_item_embed_d2i(&skfield, &p, len,
618 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
619 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
620 ERR_R_NESTED_ASN1_ERROR);
624 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
625 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
626 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
631 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 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 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 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),
649 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 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;
675 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
676 return 0; /* Should never happen */
679 if (it->itype == ASN1_ITYPE_MSTRING) {
685 if (utype == V_ASN1_ANY) {
686 /* If type is ANY need to figure out type from tag */
687 unsigned char oclass;
689 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
693 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
694 ASN1_R_ILLEGAL_OPTIONAL_ANY);
698 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
699 &p, inlen, -1, 0, 0, ctx);
701 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 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 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 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 wont work
728 if (utype == V_ASN1_OTHER) {
731 /* SEQUENCE and SET must be constructed */
733 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
734 ASN1_R_TYPE_NOT_CONSTRUCTED);
739 /* If indefinite length constructed find the real end */
741 if (!asn1_find_end(&p, plen, inf))
745 len = p - cont + plen;
749 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
750 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
751 || utype == V_ASN1_ENUMERATED) {
752 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
756 /* Free any returned 'buf' content */
759 * Should really check the internal tags are correct but some things
760 * may get this wrong. The relevant specs say that constructed string
761 * types should be OCTET STRINGs internally irrespective of the type.
762 * So instead just check for UNIVERSAL class and ignore the tag.
764 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
768 /* Append a final null to string */
769 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
770 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
774 cont = (const unsigned char *)buf.data;
781 /* We now have content length and type: translate into a structure */
782 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
783 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
790 OPENSSL_free(buf.data);
794 /* Translate ASN1 content octets into a structure */
796 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
797 int utype, char *free_cont, const ASN1_ITEM *it)
799 ASN1_VALUE **opval = NULL;
801 ASN1_TYPE *typ = NULL;
803 const ASN1_PRIMITIVE_FUNCS *pf;
807 if (pf && pf->prim_c2i)
808 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
809 /* If ANY type clear type and set pointer to internal value */
810 if (it->utype == V_ASN1_ANY) {
812 typ = ASN1_TYPE_new();
815 *pval = (ASN1_VALUE *)typ;
817 typ = (ASN1_TYPE *)*pval;
819 if (utype != typ->type)
820 ASN1_TYPE_set(typ, utype, NULL);
822 pval = &typ->value.asn1_value;
826 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
832 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
835 *pval = (ASN1_VALUE *)1;
840 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
844 tbool = (ASN1_BOOLEAN *)pval;
849 case V_ASN1_BIT_STRING:
850 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
855 case V_ASN1_NEG_INTEGER:
856 case V_ASN1_ENUMERATED:
857 case V_ASN1_NEG_ENUMERATED:
858 tint = (ASN1_INTEGER **)pval;
859 if (!c2i_ASN1_INTEGER(tint, &cont, len))
861 /* Fixup type to match the expected form */
862 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
865 case V_ASN1_OCTET_STRING:
866 case V_ASN1_NUMERICSTRING:
867 case V_ASN1_PRINTABLESTRING:
868 case V_ASN1_T61STRING:
869 case V_ASN1_VIDEOTEXSTRING:
870 case V_ASN1_IA5STRING:
872 case V_ASN1_GENERALIZEDTIME:
873 case V_ASN1_GRAPHICSTRING:
874 case V_ASN1_VISIBLESTRING:
875 case V_ASN1_GENERALSTRING:
876 case V_ASN1_UNIVERSALSTRING:
877 case V_ASN1_BMPSTRING:
878 case V_ASN1_UTF8STRING:
881 case V_ASN1_SEQUENCE:
883 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
884 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
887 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
888 ASN1err(ASN1_F_ASN1_EX_C2I,
889 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
892 /* All based on ASN1_STRING and handled the same */
894 stmp = ASN1_STRING_type_new(utype);
896 ASN1err(ASN1_F_ASN1_EX_C2I, 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 ASN1err(ASN1_F_ASN1_EX_C2I, 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)
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 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
980 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
988 * This function collects the asn1 data from a constructred string type into
989 * a buffer. The values of 'in' and 'len' should refer to the contents of the
990 * constructed type and 'inf' should be set if it is indefinite length.
993 #ifndef ASN1_MAX_STRING_NEST
995 * This determines how many levels of recursion are permitted in ASN1 string
996 * types. If it is not limited stack overflows can occur. If set to zero no
997 * recursion is allowed at all. Although zero should be adequate examples
998 * exist that require a value of 1. So 5 should be more than enough.
1000 # define ASN1_MAX_STRING_NEST 5
1003 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1004 char inf, int tag, int aclass, int depth)
1006 const unsigned char *p, *q;
1012 * If no buffer and not indefinite length constructed just pass over the
1022 if (asn1_check_eoc(&p, len)) {
1024 * EOC is illegal outside indefinite length constructed form
1027 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1034 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1035 len, tag, aclass, 0, NULL)) {
1036 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1040 /* If indefinite length constructed update max length */
1042 if (depth >= ASN1_MAX_STRING_NEST) {
1043 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1046 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1048 } else if (plen && !collect_data(buf, &p, plen))
1053 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1060 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1065 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1066 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1069 memcpy(buf->data + len, *p, plen);
1075 /* Check for ASN1 EOC and swallow it if found */
1077 static int asn1_check_eoc(const unsigned char **in, long len)
1079 const unsigned char *p;
1083 if (!p[0] && !p[1]) {
1091 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1092 * length for indefinite length constructed form, we don't know the exact
1093 * length but we can set an upper bound to the amount of data available minus
1094 * the header length just read.
1097 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1098 char *inf, char *cst,
1099 const unsigned char **in, long len,
1100 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1105 const unsigned char *p, *q;
1109 if (ctx && ctx->valid) {
1112 pclass = ctx->pclass;
1116 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1120 ctx->pclass = pclass;
1122 ctx->hdrlen = p - q;
1125 * If definite length, and no error, length + header can't exceed
1126 * total amount of data available.
1128 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1129 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1130 asn1_tlc_clear(ctx);
1137 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1138 asn1_tlc_clear(ctx);
1142 if ((exptag != ptag) || (expclass != pclass)) {
1144 * If type is OPTIONAL, not an error: indicate missing type.
1148 asn1_tlc_clear(ctx);
1149 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1153 * We have a tag and class match: assume we are going to do something
1156 asn1_tlc_clear(ctx);
1160 plen = len - (p - q);
1166 *cst = i & V_ASN1_CONSTRUCTED;