2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/asn1.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/objects.h>
64 #include <openssl/buffer.h>
65 #include <openssl/err.h>
66 #include "asn1_locl.h"
68 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
69 long len, const ASN1_ITEM *it,
70 int tag, int aclass, char opt, ASN1_TLC *ctx);
72 static int asn1_check_eoc(const unsigned char **in, long len);
73 static int asn1_find_end(const unsigned char **in, long len, char inf);
75 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
76 char inf, int tag, int aclass, int depth);
78 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
80 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
82 const unsigned char **in, long len,
83 int exptag, int expclass, char opt, ASN1_TLC *ctx);
85 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
86 const unsigned char **in, long len,
87 const ASN1_TEMPLATE *tt, char opt,
89 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
90 const unsigned char **in, long len,
91 const ASN1_TEMPLATE *tt, char opt,
93 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
94 const unsigned char **in, long len,
96 int tag, int aclass, char opt,
98 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
99 int utype, char *free_cont, const ASN1_ITEM *it);
101 /* Table to convert tags to bit values, used for MSTRING type */
102 static const unsigned long tag2bit[32] = {
104 0, 0, 0, B_ASN1_BIT_STRING,
106 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
108 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
110 B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
112 B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
114 B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
116 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
118 B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
120 B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
123 unsigned long ASN1_tag2bit(int tag)
125 if ((tag < 0) || (tag > 30))
130 /* Macro to initialize and invalidate the cache */
132 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
133 /* Version to avoid compiler warning about 'c' always non-NULL */
134 #define asn1_tlc_clear_nc(c) (c)->valid = 0
137 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
138 * function. 'in' points to a buffer to read the data from, in future we
139 * will have more advanced versions that can input data a piece at a time and
140 * this will simply be a special case.
143 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
144 const unsigned char **in, long len,
148 ASN1_VALUE *ptmpval = NULL;
151 asn1_tlc_clear_nc(&c);
152 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
157 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
159 int tag, int aclass, char opt, ASN1_TLC *ctx)
162 rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx);
164 ASN1_item_ex_free(pval, it);
169 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
170 * tag mismatch return -1 to handle OPTIONAL
173 static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
174 long len, const ASN1_ITEM *it,
175 int tag, int aclass, char opt, ASN1_TLC *ctx)
177 const ASN1_TEMPLATE *tt, *errtt = NULL;
178 const ASN1_EXTERN_FUNCS *ef;
179 const ASN1_AUX *aux = it->funcs;
180 ASN1_aux_cb *asn1_cb;
181 const unsigned char *p = NULL, *q;
182 unsigned char oclass;
183 char seq_eoc, seq_nolen, cst, isopt;
191 if (aux && aux->asn1_cb)
192 asn1_cb = aux->asn1_cb;
197 case ASN1_ITYPE_PRIMITIVE:
200 * tagging or OPTIONAL is currently illegal on an item template
201 * because the flags can't get passed down. In practice this
202 * isn't a problem: we include the relevant flags from the item
203 * template in the template itself.
205 if ((tag != -1) || opt) {
206 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
207 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
210 return asn1_template_ex_d2i(pval, in, len,
211 it->templates, opt, ctx);
213 return asn1_d2i_ex_primitive(pval, in, len, it,
214 tag, aclass, opt, ctx);
216 case ASN1_ITYPE_MSTRING:
218 /* Just read in tag and class */
219 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
220 &p, len, -1, 0, 1, ctx);
222 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
226 /* Must be UNIVERSAL class */
227 if (oclass != V_ASN1_UNIVERSAL) {
228 /* If OPTIONAL, assume this is OK */
231 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
234 /* Check tag matches bit map */
235 if (!(ASN1_tag2bit(otag) & it->utype)) {
236 /* If OPTIONAL, assume this is OK */
239 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
242 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
244 case ASN1_ITYPE_EXTERN:
245 /* Use new style d2i */
247 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
249 case ASN1_ITYPE_CHOICE:
250 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
253 /* Free up and zero CHOICE value if initialised */
254 i = asn1_get_choice_selector(pval, it);
255 if ((i >= 0) && (i < it->tcount)) {
256 tt = it->templates + i;
257 pchptr = asn1_get_field_ptr(pval, tt);
258 asn1_template_free(pchptr, tt);
259 asn1_set_choice_selector(pval, -1, it);
261 } else if (!ASN1_item_ex_new(pval, it)) {
262 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, 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 = 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);
273 /* If field not present, try the next one */
276 /* If positive return, read OK, break loop */
279 /* Otherwise must be an ASN1 parsing error */
281 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, 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 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
297 asn1_set_choice_selector(pval, i, it);
298 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
303 case ASN1_ITYPE_NDEF_SEQUENCE:
304 case ASN1_ITYPE_SEQUENCE:
308 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
310 tag = V_ASN1_SEQUENCE;
311 aclass = V_ASN1_UNIVERSAL;
313 /* Get SEQUENCE length and update len, p */
314 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
315 &p, len, tag, aclass, opt, ctx);
317 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
319 } else if (ret == -1)
321 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
322 len = tmplen - (p - *in);
325 /* If indefinite we don't do a length check */
329 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
333 if (!*pval && !ASN1_item_ex_new(pval, it)) {
334 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
338 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
341 /* Free up and zero any ADB found */
342 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
343 if (tt->flags & ASN1_TFLG_ADB_MASK) {
344 const ASN1_TEMPLATE *seqtt;
345 ASN1_VALUE **pseqval;
346 seqtt = asn1_do_adb(pval, tt, 1);
347 pseqval = asn1_get_field_ptr(pval, seqtt);
348 asn1_template_free(pseqval, seqtt);
352 /* Get each field entry */
353 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
354 const ASN1_TEMPLATE *seqtt;
355 ASN1_VALUE **pseqval;
356 seqtt = asn1_do_adb(pval, tt, 1);
359 pseqval = asn1_get_field_ptr(pval, seqtt);
360 /* Have we ran out of data? */
364 if (asn1_check_eoc(&p, len)) {
366 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
375 * This determines the OPTIONAL flag value. The field cannot be
376 * omitted if it is the last of a SEQUENCE and there is still
377 * data to be read. This isn't strictly necessary but it
378 * increases efficiency in some cases.
380 if (i == (it->tcount - 1))
383 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
385 * attempt to read in field, allowing each to be OPTIONAL
388 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
392 } else if (ret == -1) {
394 * OPTIONAL component absent. Free and zero the field.
396 asn1_template_free(pseqval, seqtt);
403 /* Check for EOC if expecting one */
404 if (seq_eoc && !asn1_check_eoc(&p, len)) {
405 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
408 /* Check all data read */
409 if (!seq_nolen && len) {
410 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
415 * If we get here we've got no more data in the SEQUENCE, however we
416 * may not have read all fields so check all remaining are OPTIONAL
417 * and clear any that are.
419 for (; i < it->tcount; tt++, i++) {
420 const ASN1_TEMPLATE *seqtt;
421 seqtt = asn1_do_adb(pval, tt, 1);
424 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
425 ASN1_VALUE **pseqval;
426 pseqval = asn1_get_field_ptr(pval, seqtt);
427 asn1_template_free(pseqval, seqtt);
430 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
435 if (!asn1_enc_save(pval, *in, p - *in, it))
437 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
446 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
449 ERR_add_error_data(4, "Field=", errtt->field_name,
450 ", Type=", it->sname);
452 ERR_add_error_data(2, "Type=", it->sname);
457 * Templates are handled with two separate functions. One handles any
458 * EXPLICIT tag and the other handles the rest.
461 static int asn1_template_ex_d2i(ASN1_VALUE **val,
462 const unsigned char **in, long inlen,
463 const ASN1_TEMPLATE *tt, char opt,
469 const unsigned char *p, *q;
474 aclass = flags & ASN1_TFLG_TAG_CLASS;
478 /* Check if EXPLICIT tag expected */
479 if (flags & ASN1_TFLG_EXPTAG) {
482 * Need to work out amount of data available to the inner content and
483 * where it starts: so read in EXPLICIT header to get the info.
485 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
486 &p, inlen, tt->tag, aclass, opt, ctx);
489 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
491 } else if (ret == -1)
494 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
495 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
498 /* We've found the field so it can't be OPTIONAL now */
499 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
501 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
504 /* We read the field in OK so update length */
507 /* If NDEF we must have an EOC here */
508 if (!asn1_check_eoc(&p, len)) {
509 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
514 * Otherwise we must hit the EXPLICIT tag end or its an error
517 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
518 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
523 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
532 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
533 const unsigned char **in, long len,
534 const ASN1_TEMPLATE *tt, char opt,
540 const unsigned char *p, *q;
544 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 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
579 } else if (ret == -1)
582 *val = (ASN1_VALUE *)sk_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 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 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 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
608 ASN1_R_UNEXPECTED_EOC);
616 if (!asn1_item_embed_d2i(&skfield, &p, len,
617 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
618 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
619 ERR_R_NESTED_ASN1_ERROR);
623 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
624 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
625 ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
630 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
633 } else if (flags & ASN1_TFLG_IMPTAG) {
634 /* IMPLICIT tagging */
635 ret = asn1_item_embed_d2i(val, &p, len,
636 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
639 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
641 } else if (ret == -1)
644 /* Nothing special */
645 ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
648 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
650 } else if (ret == -1)
661 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
662 const unsigned char **in, long inlen,
664 int tag, int aclass, char opt, ASN1_TLC *ctx)
668 char cst, inf, free_cont = 0;
669 const unsigned char *p;
670 BUF_MEM buf = { 0, NULL, 0, 0 };
671 const unsigned char *cont = NULL;
674 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
675 return 0; /* Should never happen */
678 if (it->itype == ASN1_ITYPE_MSTRING) {
684 if (utype == V_ASN1_ANY) {
685 /* If type is ANY need to figure out type from tag */
686 unsigned char oclass;
688 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
692 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
693 ASN1_R_ILLEGAL_OPTIONAL_ANY);
697 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
698 &p, inlen, -1, 0, 0, ctx);
700 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
703 if (oclass != V_ASN1_UNIVERSAL)
704 utype = V_ASN1_OTHER;
708 aclass = V_ASN1_UNIVERSAL;
712 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
713 &p, inlen, tag, aclass, opt, ctx);
715 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
717 } else if (ret == -1)
720 /* SEQUENCE, SET and "OTHER" are left in encoded form */
721 if ((utype == V_ASN1_SEQUENCE)
722 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
724 * Clear context cache for type OTHER because the auto clear when we
725 * have a exact match wont work
727 if (utype == V_ASN1_OTHER) {
730 /* SEQUENCE and SET must be constructed */
732 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
733 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 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 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 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 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 (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
831 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
834 *pval = (ASN1_VALUE *)1;
839 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
843 tbool = (ASN1_BOOLEAN *)pval;
848 case V_ASN1_BIT_STRING:
849 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
854 case V_ASN1_NEG_INTEGER:
855 case V_ASN1_ENUMERATED:
856 case V_ASN1_NEG_ENUMERATED:
857 tint = (ASN1_INTEGER **)pval;
858 if (!c2i_ASN1_INTEGER(tint, &cont, len))
860 /* Fixup type to match the expected form */
861 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
864 case V_ASN1_OCTET_STRING:
865 case V_ASN1_NUMERICSTRING:
866 case V_ASN1_PRINTABLESTRING:
867 case V_ASN1_T61STRING:
868 case V_ASN1_VIDEOTEXSTRING:
869 case V_ASN1_IA5STRING:
871 case V_ASN1_GENERALIZEDTIME:
872 case V_ASN1_GRAPHICSTRING:
873 case V_ASN1_VISIBLESTRING:
874 case V_ASN1_GENERALSTRING:
875 case V_ASN1_UNIVERSALSTRING:
876 case V_ASN1_BMPSTRING:
877 case V_ASN1_UTF8STRING:
880 case V_ASN1_SEQUENCE:
882 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
883 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
886 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
887 ASN1err(ASN1_F_ASN1_EX_C2I,
888 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
891 /* All based on ASN1_STRING and handled the same */
893 stmp = ASN1_STRING_type_new(utype);
895 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
898 *pval = (ASN1_VALUE *)stmp;
900 stmp = (ASN1_STRING *)*pval;
903 /* If we've already allocated a buffer use it */
905 OPENSSL_free(stmp->data);
906 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
910 if (!ASN1_STRING_set(stmp, cont, len)) {
911 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
912 ASN1_STRING_free(stmp);
919 /* If ASN1_ANY and NULL type fix up value */
920 if (typ && (utype == V_ASN1_NULL))
921 typ->value.ptr = NULL;
934 * This function finds the end of an ASN1 structure when passed its maximum
935 * length, whether it is indefinite length and a pointer to the content. This
936 * is more efficient than calling asn1_collect because it does not recurse on
937 * each indefinite length header.
940 static int asn1_find_end(const unsigned char **in, long len, char inf)
944 const unsigned char *p = *in, *q;
945 /* If not indefinite length constructed just add length */
952 * Indefinite length constructed form. Find the end when enough EOCs are
953 * found. If more indefinite length constructed headers are encountered
954 * increment the expected eoc count otherwise just skip to the end of the
958 if (asn1_check_eoc(&p, len)) {
960 if (expected_eoc == 0)
966 /* Just read in a header: only care about the length */
967 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
969 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
979 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
987 * This function collects the asn1 data from a constructed string type into
988 * a buffer. The values of 'in' and 'len' should refer to the contents of the
989 * constructed type and 'inf' should be set if it is indefinite length.
992 #ifndef ASN1_MAX_STRING_NEST
994 * This determines how many levels of recursion are permitted in ASN1 string
995 * types. If it is not limited stack overflows can occur. If set to zero no
996 * recursion is allowed at all. Although zero should be adequate examples
997 * exist that require a value of 1. So 5 should be more than enough.
999 # define ASN1_MAX_STRING_NEST 5
1002 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1003 char inf, int tag, int aclass, int depth)
1005 const unsigned char *p, *q;
1011 * If no buffer and not indefinite length constructed just pass over the
1021 if (asn1_check_eoc(&p, len)) {
1023 * EOC is illegal outside indefinite length constructed form
1026 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1033 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1034 len, tag, aclass, 0, NULL)) {
1035 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1039 /* If indefinite length constructed update max length */
1041 if (depth >= ASN1_MAX_STRING_NEST) {
1042 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1045 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1047 } else if (plen && !collect_data(buf, &p, plen))
1052 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1059 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1064 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1065 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1068 memcpy(buf->data + len, *p, plen);
1074 /* Check for ASN1 EOC and swallow it if found */
1076 static int asn1_check_eoc(const unsigned char **in, long len)
1078 const unsigned char *p;
1082 if (!p[0] && !p[1]) {
1090 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1091 * length for indefinite length constructed form, we don't know the exact
1092 * length but we can set an upper bound to the amount of data available minus
1093 * the header length just read.
1096 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1097 char *inf, char *cst,
1098 const unsigned char **in, long len,
1099 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1104 const unsigned char *p, *q;
1108 if (ctx && ctx->valid) {
1111 pclass = ctx->pclass;
1115 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1119 ctx->pclass = pclass;
1121 ctx->hdrlen = p - q;
1124 * If definite length, and no error, length + header can't exceed
1125 * total amount of data available.
1127 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1128 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1129 asn1_tlc_clear(ctx);
1136 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1137 asn1_tlc_clear(ctx);
1141 if ((exptag != ptag) || (expclass != pclass)) {
1143 * If type is OPTIONAL, not an error: indicate missing type.
1147 asn1_tlc_clear(ctx);
1148 ASN1err(ASN1_F_ASN1_CHECK_TLEN, 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;