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_check_eoc(const unsigned char **in, long len);
70 static int asn1_find_end(const unsigned char **in, long len, char inf);
72 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
73 char inf, int tag, int aclass, int depth);
75 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
77 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
79 const unsigned char **in, long len,
80 int exptag, int expclass, char opt, ASN1_TLC *ctx);
82 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
83 const unsigned char **in, long len,
84 const ASN1_TEMPLATE *tt, char opt,
86 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
87 const unsigned char **in, long len,
88 const ASN1_TEMPLATE *tt, char opt,
90 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
91 const unsigned char **in, long len,
93 int tag, int aclass, char opt,
95 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
96 int utype, char *free_cont, const ASN1_ITEM *it);
98 /* Table to convert tags to bit values, used for MSTRING type */
99 static const unsigned long tag2bit[32] = {
101 0, 0, 0, B_ASN1_BIT_STRING,
103 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
105 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
107 B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
109 B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
111 B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
113 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
115 B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
117 B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
120 unsigned long ASN1_tag2bit(int tag)
122 if ((tag < 0) || (tag > 30))
127 /* Macro to initialize and invalidate the cache */
129 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
130 /* Version to avoid compiler warning about 'c' always non-NULL */
131 #define asn1_tlc_clear_nc(c) (c)->valid = 0
134 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
135 * function. 'in' points to a buffer to read the data from, in future we
136 * will have more advanced versions that can input data a piece at a time and
137 * this will simply be a special case.
140 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
141 const unsigned char **in, long len,
145 ASN1_VALUE *ptmpval = NULL;
148 asn1_tlc_clear_nc(&c);
149 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
155 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
156 * tag mismatch return -1 to handle OPTIONAL
159 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
161 int tag, int aclass, char opt, ASN1_TLC *ctx)
163 const ASN1_TEMPLATE *tt, *errtt = NULL;
164 const ASN1_EXTERN_FUNCS *ef;
165 const ASN1_AUX *aux = it->funcs;
166 ASN1_aux_cb *asn1_cb;
167 const unsigned char *p = NULL, *q;
168 unsigned char oclass;
169 char seq_eoc, seq_nolen, cst, isopt;
177 if (aux && aux->asn1_cb)
178 asn1_cb = aux->asn1_cb;
183 case ASN1_ITYPE_PRIMITIVE:
186 * tagging or OPTIONAL is currently illegal on an item template
187 * because the flags can't get passed down. In practice this
188 * isn't a problem: we include the relevant flags from the item
189 * template in the template itself.
191 if ((tag != -1) || opt) {
192 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
193 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
196 return asn1_template_ex_d2i(pval, in, len,
197 it->templates, opt, ctx);
199 return asn1_d2i_ex_primitive(pval, in, len, it,
200 tag, aclass, opt, ctx);
202 case ASN1_ITYPE_MSTRING:
204 /* Just read in tag and class */
205 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
206 &p, len, -1, 0, 1, ctx);
208 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
212 /* Must be UNIVERSAL class */
213 if (oclass != V_ASN1_UNIVERSAL) {
214 /* If OPTIONAL, assume this is OK */
217 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
220 /* Check tag matches bit map */
221 if (!(ASN1_tag2bit(otag) & it->utype)) {
222 /* If OPTIONAL, assume this is OK */
225 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_WRONG_TAG);
228 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
230 case ASN1_ITYPE_EXTERN:
231 /* Use new style d2i */
233 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
235 case ASN1_ITYPE_CHOICE:
236 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
239 /* Free up and zero CHOICE value if initialised */
240 i = asn1_get_choice_selector(pval, it);
241 if ((i >= 0) && (i < it->tcount)) {
242 tt = it->templates + i;
243 pchptr = asn1_get_field_ptr(pval, tt);
244 asn1_template_free(pchptr, tt);
245 asn1_set_choice_selector(pval, -1, it);
247 } else if (!ASN1_item_ex_new(pval, it)) {
248 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
251 /* CHOICE type, try each possibility in turn */
253 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
254 pchptr = asn1_get_field_ptr(pval, tt);
256 * We mark field as OPTIONAL so its absence can be recognised.
258 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
259 /* If field not present, try the next one */
262 /* If positive return, read OK, break loop */
265 /* Otherwise must be an ASN1 parsing error */
267 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
271 /* Did we fall off the end without reading anything? */
272 if (i == it->tcount) {
273 /* If OPTIONAL, this is OK */
275 /* Free and zero it */
276 ASN1_item_ex_free(pval, it);
279 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
283 asn1_set_choice_selector(pval, i, it);
285 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
289 case ASN1_ITYPE_NDEF_SEQUENCE:
290 case ASN1_ITYPE_SEQUENCE:
294 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
296 tag = V_ASN1_SEQUENCE;
297 aclass = V_ASN1_UNIVERSAL;
299 /* Get SEQUENCE length and update len, p */
300 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
301 &p, len, tag, aclass, opt, ctx);
303 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
305 } else if (ret == -1)
307 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
308 len = tmplen - (p - *in);
311 /* If indefinite we don't do a length check */
315 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
319 if (!*pval && !ASN1_item_ex_new(pval, it)) {
320 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
324 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
327 /* Free up and zero any ADB found */
328 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
329 if (tt->flags & ASN1_TFLG_ADB_MASK) {
330 const ASN1_TEMPLATE *seqtt;
331 ASN1_VALUE **pseqval;
332 seqtt = asn1_do_adb(pval, tt, 1);
333 pseqval = asn1_get_field_ptr(pval, seqtt);
334 asn1_template_free(pseqval, seqtt);
338 /* Get each field entry */
339 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
340 const ASN1_TEMPLATE *seqtt;
341 ASN1_VALUE **pseqval;
342 seqtt = asn1_do_adb(pval, tt, 1);
345 pseqval = asn1_get_field_ptr(pval, seqtt);
346 /* Have we ran out of data? */
350 if (asn1_check_eoc(&p, len)) {
352 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_UNEXPECTED_EOC);
361 * This determines the OPTIONAL flag value. The field cannot be
362 * omitted if it is the last of a SEQUENCE and there is still
363 * data to be read. This isn't strictly necessary but it
364 * increases efficiency in some cases.
366 if (i == (it->tcount - 1))
369 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
371 * attempt to read in field, allowing each to be OPTIONAL
374 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
378 } else if (ret == -1) {
380 * OPTIONAL component absent. Free and zero the field.
382 asn1_template_free(pseqval, seqtt);
389 /* Check for EOC if expecting one */
390 if (seq_eoc && !asn1_check_eoc(&p, len)) {
391 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
394 /* Check all data read */
395 if (!seq_nolen && len) {
396 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
401 * If we get here we've got no more data in the SEQUENCE, however we
402 * may not have read all fields so check all remaining are OPTIONAL
403 * and clear any that are.
405 for (; i < it->tcount; tt++, i++) {
406 const ASN1_TEMPLATE *seqtt;
407 seqtt = asn1_do_adb(pval, tt, 1);
410 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
411 ASN1_VALUE **pseqval;
412 pseqval = asn1_get_field_ptr(pval, seqtt);
413 asn1_template_free(pseqval, seqtt);
416 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_FIELD_MISSING);
421 if (!asn1_enc_save(pval, *in, p - *in, it))
424 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
432 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
434 ASN1_item_ex_free(pval, it);
436 ERR_add_error_data(4, "Field=", errtt->field_name,
437 ", Type=", it->sname);
439 ERR_add_error_data(2, "Type=", it->sname);
444 * Templates are handled with two separate functions. One handles any
445 * EXPLICIT tag and the other handles the rest.
448 static int asn1_template_ex_d2i(ASN1_VALUE **val,
449 const unsigned char **in, long inlen,
450 const ASN1_TEMPLATE *tt, char opt,
456 const unsigned char *p, *q;
461 aclass = flags & ASN1_TFLG_TAG_CLASS;
465 /* Check if EXPLICIT tag expected */
466 if (flags & ASN1_TFLG_EXPTAG) {
469 * Need to work out amount of data available to the inner content and
470 * where it starts: so read in EXPLICIT header to get the info.
472 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
473 &p, inlen, tt->tag, aclass, opt, ctx);
476 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
478 } else if (ret == -1)
481 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
482 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
485 /* We've found the field so it can't be OPTIONAL now */
486 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
488 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
491 /* We read the field in OK so update length */
494 /* If NDEF we must have an EOC here */
495 if (!asn1_check_eoc(&p, len)) {
496 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
501 * Otherwise we must hit the EXPLICIT tag end or its an error
504 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
505 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
510 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
516 asn1_template_free(val, tt);
520 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
521 const unsigned char **in, long len,
522 const ASN1_TEMPLATE *tt, char opt,
527 const unsigned char *p, *q;
531 aclass = flags & ASN1_TFLG_TAG_CLASS;
536 if (flags & ASN1_TFLG_SK_MASK) {
537 /* SET OF, SEQUENCE OF */
540 /* First work out expected inner tag value */
541 if (flags & ASN1_TFLG_IMPTAG) {
545 skaclass = V_ASN1_UNIVERSAL;
546 if (flags & ASN1_TFLG_SET_OF)
549 sktag = V_ASN1_SEQUENCE;
552 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
553 &p, len, sktag, skaclass, opt, ctx);
555 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
557 } else if (ret == -1)
560 *val = (ASN1_VALUE *)sk_new_null();
563 * We've got a valid STACK: free up any items present
565 STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
567 while (sk_ASN1_VALUE_num(sktmp) > 0) {
568 vtmp = sk_ASN1_VALUE_pop(sktmp);
569 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
574 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
578 /* Read as many items as we can */
582 /* See if EOC found */
583 if (asn1_check_eoc(&p, len)) {
585 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
586 ASN1_R_UNEXPECTED_EOC);
594 if (!ASN1_item_ex_d2i(&skfield, &p, len,
595 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
596 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
597 ERR_R_NESTED_ASN1_ERROR);
601 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
602 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
607 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
610 } else if (flags & ASN1_TFLG_IMPTAG) {
611 /* IMPLICIT tagging */
612 ret = ASN1_item_ex_d2i(val, &p, len,
613 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
616 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
618 } else if (ret == -1)
621 /* Nothing special */
622 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
625 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
627 } else if (ret == -1)
635 asn1_template_free(val, tt);
639 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
640 const unsigned char **in, long inlen,
642 int tag, int aclass, char opt, ASN1_TLC *ctx)
646 char cst, inf, free_cont = 0;
647 const unsigned char *p;
649 const unsigned char *cont = NULL;
652 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
653 return 0; /* Should never happen */
656 if (it->itype == ASN1_ITYPE_MSTRING) {
662 if (utype == V_ASN1_ANY) {
663 /* If type is ANY need to figure out type from tag */
664 unsigned char oclass;
666 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
670 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
671 ASN1_R_ILLEGAL_OPTIONAL_ANY);
675 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
676 &p, inlen, -1, 0, 0, ctx);
678 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
681 if (oclass != V_ASN1_UNIVERSAL)
682 utype = V_ASN1_OTHER;
686 aclass = V_ASN1_UNIVERSAL;
690 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
691 &p, inlen, tag, aclass, opt, ctx);
693 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
695 } else if (ret == -1)
698 /* SEQUENCE, SET and "OTHER" are left in encoded form */
699 if ((utype == V_ASN1_SEQUENCE)
700 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
702 * Clear context cache for type OTHER because the auto clear when we
703 * have a exact match wont work
705 if (utype == V_ASN1_OTHER) {
708 /* SEQUENCE and SET must be constructed */
710 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
711 ASN1_R_TYPE_NOT_CONSTRUCTED);
716 /* If indefinite length constructed find the real end */
718 if (!asn1_find_end(&p, plen, inf))
722 len = p - cont + plen;
727 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
728 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
729 || utype == V_ASN1_ENUMERATED) {
730 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
737 * Should really check the internal tags are correct but some things
738 * may get this wrong. The relevant specs say that constructed string
739 * types should be OCTET STRINGs internally irrespective of the type.
740 * So instead just check for UNIVERSAL class and ignore the tag.
742 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
747 /* Append a final null to string */
748 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
749 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
753 cont = (const unsigned char *)buf.data;
761 /* We now have content length and type: translate into a structure */
762 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
768 if (free_cont && buf.data)
769 OPENSSL_free(buf.data);
773 /* Translate ASN1 content octets into a structure */
775 static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
776 int utype, char *free_cont, const ASN1_ITEM *it)
778 ASN1_VALUE **opval = NULL;
780 ASN1_TYPE *typ = NULL;
782 const ASN1_PRIMITIVE_FUNCS *pf;
786 if (pf && pf->prim_c2i)
787 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
788 /* If ANY type clear type and set pointer to internal value */
789 if (it->utype == V_ASN1_ANY) {
791 typ = ASN1_TYPE_new();
794 *pval = (ASN1_VALUE *)typ;
796 typ = (ASN1_TYPE *)*pval;
798 if (utype != typ->type)
799 ASN1_TYPE_set(typ, utype, NULL);
801 pval = &typ->value.asn1_value;
805 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
811 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
814 *pval = (ASN1_VALUE *)1;
819 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
823 tbool = (ASN1_BOOLEAN *)pval;
828 case V_ASN1_BIT_STRING:
829 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
834 case V_ASN1_NEG_INTEGER:
835 case V_ASN1_ENUMERATED:
836 case V_ASN1_NEG_ENUMERATED:
837 tint = (ASN1_INTEGER **)pval;
838 if (!c2i_ASN1_INTEGER(tint, &cont, len))
840 /* Fixup type to match the expected form */
841 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
844 case V_ASN1_OCTET_STRING:
845 case V_ASN1_NUMERICSTRING:
846 case V_ASN1_PRINTABLESTRING:
847 case V_ASN1_T61STRING:
848 case V_ASN1_VIDEOTEXSTRING:
849 case V_ASN1_IA5STRING:
851 case V_ASN1_GENERALIZEDTIME:
852 case V_ASN1_GRAPHICSTRING:
853 case V_ASN1_VISIBLESTRING:
854 case V_ASN1_GENERALSTRING:
855 case V_ASN1_UNIVERSALSTRING:
856 case V_ASN1_BMPSTRING:
857 case V_ASN1_UTF8STRING:
860 case V_ASN1_SEQUENCE:
862 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
863 ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
866 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
867 ASN1err(ASN1_F_ASN1_EX_C2I,
868 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
871 /* All based on ASN1_STRING and handled the same */
873 stmp = ASN1_STRING_type_new(utype);
875 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
878 *pval = (ASN1_VALUE *)stmp;
880 stmp = (ASN1_STRING *)*pval;
883 /* If we've already allocated a buffer use it */
886 OPENSSL_free(stmp->data);
887 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
891 if (!ASN1_STRING_set(stmp, cont, len)) {
892 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
893 ASN1_STRING_free(stmp);
900 /* If ASN1_ANY and NULL type fix up value */
901 if (typ && (utype == V_ASN1_NULL))
902 typ->value.ptr = NULL;
915 * This function finds the end of an ASN1 structure when passed its maximum
916 * length, whether it is indefinite length and a pointer to the content. This
917 * is more efficient than calling asn1_collect because it does not recurse on
918 * each indefinite length header.
921 static int asn1_find_end(const unsigned char **in, long len, char inf)
925 const unsigned char *p = *in, *q;
926 /* If not indefinite length constructed just add length */
933 * Indefinite length constructed form. Find the end when enough EOCs are
934 * found. If more indefinite length constructed headers are encountered
935 * increment the expected eoc count otherwise just skip to the end of the
939 if (asn1_check_eoc(&p, len)) {
941 if (expected_eoc == 0)
947 /* Just read in a header: only care about the length */
948 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
950 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
960 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
968 * This function collects the asn1 data from a constructred string type into
969 * a buffer. The values of 'in' and 'len' should refer to the contents of the
970 * constructed type and 'inf' should be set if it is indefinite length.
973 #ifndef ASN1_MAX_STRING_NEST
975 * This determines how many levels of recursion are permitted in ASN1 string
976 * types. If it is not limited stack overflows can occur. If set to zero no
977 * recursion is allowed at all. Although zero should be adequate examples
978 * exist that require a value of 1. So 5 should be more than enough.
980 # define ASN1_MAX_STRING_NEST 5
983 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
984 char inf, int tag, int aclass, int depth)
986 const unsigned char *p, *q;
992 * If no buffer and not indefinite length constructed just pass over the
1002 if (asn1_check_eoc(&p, len)) {
1004 * EOC is illegal outside indefinite length constructed form
1007 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1014 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1015 len, tag, aclass, 0, NULL)) {
1016 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1020 /* If indefinite length constructed update max length */
1022 if (depth >= ASN1_MAX_STRING_NEST) {
1023 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1026 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1028 } else if (plen && !collect_data(buf, &p, plen))
1033 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1040 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1045 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1046 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1049 memcpy(buf->data + len, *p, plen);
1055 /* Check for ASN1 EOC and swallow it if found */
1057 static int asn1_check_eoc(const unsigned char **in, long len)
1059 const unsigned char *p;
1063 if (!p[0] && !p[1]) {
1071 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1072 * length for indefinite length constructed form, we don't know the exact
1073 * length but we can set an upper bound to the amount of data available minus
1074 * the header length just read.
1077 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1078 char *inf, char *cst,
1079 const unsigned char **in, long len,
1080 int exptag, int expclass, char opt, ASN1_TLC *ctx)
1085 const unsigned char *p, *q;
1089 if (ctx && ctx->valid) {
1092 pclass = ctx->pclass;
1096 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1100 ctx->pclass = pclass;
1102 ctx->hdrlen = p - q;
1105 * If definite length, and no error, length + header can't exceed
1106 * total amount of data available.
1108 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1109 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1110 asn1_tlc_clear(ctx);
1117 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1118 asn1_tlc_clear(ctx);
1122 if ((exptag != ptag) || (expclass != pclass)) {
1124 * If type is OPTIONAL, not an error: indicate missing type.
1128 asn1_tlc_clear(ctx);
1129 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1133 * We have a tag and class match: assume we are going to do something
1136 asn1_tlc_clear(ctx);
1140 plen = len - (p - q);
1146 *cst = i & V_ASN1_CONSTRUCTED;