2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
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).
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>
68 static int asn1_check_eoc(const unsigned char **in, long len);
69 static int asn1_find_end(const unsigned char **in, long len, char inf);
71 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72 char inf, int tag, int aclass, int depth);
74 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
76 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
78 const unsigned char **in, long len,
79 int exptag, int expclass, char opt,
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, ASN1_TLC *ctx);
95 /* Table to convert tags to bit values, used for MSTRING type */
96 static const unsigned long tag2bit[32] = {
97 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
98 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
99 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
100 B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
101 B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
102 B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
103 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
104 B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
105 B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
108 unsigned long ASN1_tag2bit(int tag)
110 if ((tag < 0) || (tag > 30)) return 0;
114 /* Macro to initialize and invalidate the cache */
116 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
118 /* Decode an ASN1 item, this currently behaves just
119 * like a standard 'd2i' function. 'in' points to
120 * a buffer to read the data from, in future we will
121 * have more advanced versions that can input data
122 * a piece at a time and this will simply be a special
126 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
127 const unsigned char **in, long len, const ASN1_ITEM *it)
130 ASN1_VALUE *ptmpval = NULL;
132 if (pval && *pval && it->itype == ASN1_ITYPE_PRIMITIVE)
135 if (ASN1_item_ex_d2i(&ptmpval, in, len, it, -1, 0, 0, &c) > 0) {
136 if (pval && it->itype != ASN1_ITYPE_PRIMITIVE) {
138 ASN1_item_free(*pval, it);
146 int ASN1_template_d2i(ASN1_VALUE **pval,
147 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
151 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
155 /* Decode an item, taking care of IMPLICIT tagging, if any.
156 * If 'opt' set and 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_COMPAT_FUNCS *cf;
165 const ASN1_EXTERN_FUNCS *ef;
166 const ASN1_AUX *aux = it->funcs;
167 ASN1_aux_cb *asn1_cb;
168 const unsigned char *p = NULL, *q;
169 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
170 unsigned char imphack = 0, oclass;
171 char seq_eoc, seq_nolen, cst, isopt;
176 ASN1_VALUE **pchptr, *ptmpval;
179 if (aux && aux->asn1_cb)
180 asn1_cb = aux->asn1_cb;
185 case ASN1_ITYPE_PRIMITIVE:
188 /* tagging or OPTIONAL is currently illegal on an item
189 * template because the flags can't get passed down.
190 * In practice this isn't a problem: we include the
191 * relevant flags from the item template in the
194 if ((tag != -1) || opt)
196 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
197 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
200 return asn1_template_ex_d2i(pval, in, len,
201 it->templates, opt, ctx);
203 return asn1_d2i_ex_primitive(pval, in, len, it,
204 tag, aclass, opt, ctx);
207 case ASN1_ITYPE_MSTRING:
209 /* Just read in tag and class */
210 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
211 &p, len, -1, 0, 1, ctx);
214 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
215 ERR_R_NESTED_ASN1_ERROR);
219 /* Must be UNIVERSAL class */
220 if (oclass != V_ASN1_UNIVERSAL)
222 /* If OPTIONAL, assume this is OK */
224 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
225 ASN1_R_MSTRING_NOT_UNIVERSAL);
228 /* Check tag matches bit map */
229 if (!(ASN1_tag2bit(otag) & it->utype))
231 /* If OPTIONAL, assume this is OK */
234 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
235 ASN1_R_MSTRING_WRONG_TAG);
238 return asn1_d2i_ex_primitive(pval, in, len,
239 it, otag, 0, 0, ctx);
241 case ASN1_ITYPE_EXTERN:
242 /* Use new style d2i */
244 return ef->asn1_ex_d2i(pval, in, len,
245 it, tag, aclass, opt, ctx);
247 case ASN1_ITYPE_COMPAT:
248 /* we must resort to old style evil hackery */
251 /* If OPTIONAL see if it is there */
259 /* Don't care about anything other than presence
262 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
263 &p, len, exptag, aclass, 1, ctx);
266 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
267 ERR_R_NESTED_ASN1_ERROR);
274 /* This is the old style evil hack IMPLICIT handling:
275 * since the underlying code is expecting a tag and
276 * class other than the one present we change the
277 * buffer temporarily then change it back afterwards.
278 * This doesn't and never did work for tags > 30.
280 * Yes this is *horrible* but it is only needed for
281 * old style d2i which will hopefully not be around
283 * FIXME: should copy the buffer then modify it so
284 * the input buffer can be const: we should *always*
285 * copy because the old style d2i might modify the
291 wp = *(unsigned char **)in;
295 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
296 ERR_R_NESTED_ASN1_ERROR);
299 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
303 ptmpval = cf->asn1_d2i(pval, in, len);
311 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
315 case ASN1_ITYPE_CHOICE:
316 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
320 /* Free up and zero CHOICE value if initialised */
321 i = asn1_get_choice_selector(pval, it);
322 if ((i >= 0) && (i < it->tcount)) {
323 tt = it->templates + i;
324 pchptr = asn1_get_field_ptr(pval, tt);
325 ASN1_template_free(pchptr, tt);
326 asn1_set_choice_selector(pval, -1, it);
328 } else if (!ASN1_item_ex_new(pval, it)) {
329 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
330 ERR_R_NESTED_ASN1_ERROR);
333 /* CHOICE type, try each possibility in turn */
335 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
337 pchptr = asn1_get_field_ptr(pval, tt);
338 /* We mark field as OPTIONAL so its absence
341 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
342 /* If field not present, try the next one */
345 /* If positive return, read OK, break loop */
348 /* Otherwise must be an ASN1 parsing error */
350 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
351 ERR_R_NESTED_ASN1_ERROR);
355 /* Did we fall off the end without reading anything? */
358 /* If OPTIONAL, this is OK */
361 /* Free and zero it */
362 ASN1_item_ex_free(pval, it);
365 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
366 ASN1_R_NO_MATCHING_CHOICE_TYPE);
370 asn1_set_choice_selector(pval, i, it);
372 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
376 case ASN1_ITYPE_NDEF_SEQUENCE:
377 case ASN1_ITYPE_SEQUENCE:
381 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
384 tag = V_ASN1_SEQUENCE;
385 aclass = V_ASN1_UNIVERSAL;
387 /* Get SEQUENCE length and update len, p */
388 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
389 &p, len, tag, aclass, opt, ctx);
392 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
393 ERR_R_NESTED_ASN1_ERROR);
398 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
400 len = tmplen - (p - *in);
403 /* If indefinite we don't do a length check */
404 else seq_nolen = seq_eoc;
407 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
408 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
412 if (!*pval && !ASN1_item_ex_new(pval, it))
414 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
415 ERR_R_NESTED_ASN1_ERROR);
419 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
422 /* Free up and zero any ADB found */
423 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
424 if (tt->flags & ASN1_TFLG_ADB_MASK) {
425 const ASN1_TEMPLATE *seqtt;
426 ASN1_VALUE **pseqval;
427 seqtt = asn1_do_adb(pval, tt, 1);
428 pseqval = asn1_get_field_ptr(pval, seqtt);
429 ASN1_template_free(pseqval, seqtt);
433 /* Get each field entry */
434 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
436 const ASN1_TEMPLATE *seqtt;
437 ASN1_VALUE **pseqval;
438 seqtt = asn1_do_adb(pval, tt, 1);
441 pseqval = asn1_get_field_ptr(pval, seqtt);
442 /* Have we ran out of data? */
446 if (asn1_check_eoc(&p, len))
450 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
451 ASN1_R_UNEXPECTED_EOC);
459 /* This determines the OPTIONAL flag value. The field
460 * cannot be omitted if it is the last of a SEQUENCE
461 * and there is still data to be read. This isn't
462 * strictly necessary but it increases efficiency in
465 if (i == (it->tcount - 1))
467 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
468 /* attempt to read in field, allowing each to be
471 ret = asn1_template_ex_d2i(pseqval, &p, len,
480 /* OPTIONAL component absent.
481 * Free and zero the field.
483 ASN1_template_free(pseqval, seqtt);
490 /* Check for EOC if expecting one */
491 if (seq_eoc && !asn1_check_eoc(&p, len))
493 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
496 /* Check all data read */
497 if (!seq_nolen && len)
499 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
500 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
504 /* If we get here we've got no more data in the SEQUENCE,
505 * however we may not have read all fields so check all
506 * remaining are OPTIONAL and clear any that are.
508 for (; i < it->tcount; tt++, i++)
510 const ASN1_TEMPLATE *seqtt;
511 seqtt = asn1_do_adb(pval, tt, 1);
514 if (seqtt->flags & ASN1_TFLG_OPTIONAL)
516 ASN1_VALUE **pseqval;
517 pseqval = asn1_get_field_ptr(pval, seqtt);
518 ASN1_template_free(pseqval, seqtt);
523 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
524 ASN1_R_FIELD_MISSING);
529 if (!asn1_enc_save(pval, *in, p - *in, it))
532 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
540 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
542 ASN1_item_ex_free(pval, it);
544 ERR_add_error_data(4, "Field=", errtt->field_name,
545 ", Type=", it->sname);
547 ERR_add_error_data(2, "Type=", it->sname);
551 /* Templates are handled with two separate functions.
552 * One handles any EXPLICIT tag and the other handles the rest.
555 static int asn1_template_ex_d2i(ASN1_VALUE **val,
556 const unsigned char **in, long inlen,
557 const ASN1_TEMPLATE *tt, char opt,
563 const unsigned char *p, *q;
568 aclass = flags & ASN1_TFLG_TAG_CLASS;
572 /* Check if EXPLICIT tag expected */
573 if (flags & ASN1_TFLG_EXPTAG)
576 /* Need to work out amount of data available to the inner
577 * content and where it starts: so read in EXPLICIT header to
580 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
581 &p, inlen, tt->tag, aclass, opt, ctx);
585 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
586 ERR_R_NESTED_ASN1_ERROR);
593 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
594 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
597 /* We've found the field so it can't be OPTIONAL now */
598 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
601 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
602 ERR_R_NESTED_ASN1_ERROR);
605 /* We read the field in OK so update length */
609 /* If NDEF we must have an EOC here */
610 if (!asn1_check_eoc(&p, len))
612 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
619 /* Otherwise we must hit the EXPLICIT tag end or its
623 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
624 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
630 return asn1_template_noexp_d2i(val, in, inlen,
637 ASN1_template_free(val, tt);
641 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
642 const unsigned char **in, long len,
643 const ASN1_TEMPLATE *tt, char opt,
648 const unsigned char *p, *q;
652 aclass = flags & ASN1_TFLG_TAG_CLASS;
657 if (flags & ASN1_TFLG_SK_MASK)
659 /* SET OF, SEQUENCE OF */
662 /* First work out expected inner tag value */
663 if (flags & ASN1_TFLG_IMPTAG)
670 skaclass = V_ASN1_UNIVERSAL;
671 if (flags & ASN1_TFLG_SET_OF)
674 sktag = V_ASN1_SEQUENCE;
677 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
678 &p, len, sktag, skaclass, opt, ctx);
681 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
682 ERR_R_NESTED_ASN1_ERROR);
688 *val = (ASN1_VALUE *)sk_new_null();
691 /* We've got a valid STACK: free up any items present */
692 STACK *sktmp = (STACK *)*val;
694 while(sk_num(sktmp) > 0)
696 vtmp = (ASN1_VALUE *)sk_pop(sktmp);
697 ASN1_item_ex_free(&vtmp,
698 ASN1_ITEM_ptr(tt->item));
704 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
705 ERR_R_MALLOC_FAILURE);
709 /* Read as many items as we can */
714 /* See if EOC found */
715 if (asn1_check_eoc(&p, len))
719 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
720 ASN1_R_UNEXPECTED_EOC);
728 if (!ASN1_item_ex_d2i(&skfield, &p, len,
729 ASN1_ITEM_ptr(tt->item),
732 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
733 ERR_R_NESTED_ASN1_ERROR);
737 if (!sk_push((STACK *)*val, (char *)skfield))
739 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
740 ERR_R_MALLOC_FAILURE);
746 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
750 else if (flags & ASN1_TFLG_IMPTAG)
752 /* IMPLICIT tagging */
753 ret = ASN1_item_ex_d2i(val, &p, len,
754 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
757 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
758 ERR_R_NESTED_ASN1_ERROR);
766 /* Nothing special */
767 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
771 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
772 ERR_R_NESTED_ASN1_ERROR);
783 ASN1_template_free(val, tt);
787 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
788 const unsigned char **in, long inlen,
790 int tag, int aclass, char opt, ASN1_TLC *ctx)
794 char cst, inf, free_cont = 0;
795 const unsigned char *p;
797 const unsigned char *cont = NULL;
801 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
802 return 0; /* Should never happen */
805 if (it->itype == ASN1_ITYPE_MSTRING)
813 if (utype == V_ASN1_ANY)
815 /* If type is ANY need to figure out type from tag */
816 unsigned char oclass;
819 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
820 ASN1_R_ILLEGAL_TAGGED_ANY);
825 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
826 ASN1_R_ILLEGAL_OPTIONAL_ANY);
830 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
831 &p, inlen, -1, 0, 0, ctx);
834 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
835 ERR_R_NESTED_ASN1_ERROR);
838 if (oclass != V_ASN1_UNIVERSAL)
839 utype = V_ASN1_OTHER;
844 aclass = V_ASN1_UNIVERSAL;
848 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
849 &p, inlen, tag, aclass, opt, ctx);
852 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
858 /* SEQUENCE, SET and "OTHER" are left in encoded form */
859 if ((utype == V_ASN1_SEQUENCE)
860 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
862 /* Clear context cache for type OTHER because the auto clear
863 * when we have a exact match wont work
865 if (utype == V_ASN1_OTHER)
869 /* SEQUENCE and SET must be constructed */
872 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
873 ASN1_R_TYPE_NOT_CONSTRUCTED);
878 /* If indefinite length constructed find the real end */
881 if (!asn1_find_end(&p, plen, inf))
887 len = p - cont + plen;
894 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
895 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
896 || utype == V_ASN1_ENUMERATED)
898 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
899 ASN1_R_TYPE_NOT_PRIMITIVE);
905 /* Should really check the internal tags are correct but
906 * some things may get this wrong. The relevant specs
907 * say that constructed string types should be OCTET STRINGs
908 * internally irrespective of the type. So instead just check
909 * for UNIVERSAL class and ignore the tag.
911 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0))
917 /* Append a final null to string */
918 if (!BUF_MEM_grow_clean(&buf, len + 1))
920 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
921 ERR_R_MALLOC_FAILURE);
925 cont = (const unsigned char *)buf.data;
935 /* We now have content length and type: translate into a structure */
936 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
942 if (free_cont && buf.data) OPENSSL_free(buf.data);
946 /* Translate ASN1 content octets into a structure */
948 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
949 int utype, char *free_cont, const ASN1_ITEM *it)
951 ASN1_VALUE **opval = NULL;
953 ASN1_TYPE *typ = NULL;
955 const ASN1_PRIMITIVE_FUNCS *pf;
959 if (pf && pf->prim_c2i)
960 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
961 /* If ANY type clear type and set pointer to internal value */
962 if (it->utype == V_ASN1_ANY)
966 typ = ASN1_TYPE_new();
969 *pval = (ASN1_VALUE *)typ;
972 typ = (ASN1_TYPE *)*pval;
974 if (utype != typ->type)
975 ASN1_TYPE_set(typ, utype, NULL);
977 pval = &typ->value.asn1_value;
982 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
989 ASN1err(ASN1_F_ASN1_EX_C2I,
990 ASN1_R_NULL_IS_WRONG_LENGTH);
993 *pval = (ASN1_VALUE *)1;
999 ASN1err(ASN1_F_ASN1_EX_C2I,
1000 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
1005 ASN1_BOOLEAN *tbool;
1006 tbool = (ASN1_BOOLEAN *)pval;
1011 case V_ASN1_BIT_STRING:
1012 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
1016 case V_ASN1_INTEGER:
1017 case V_ASN1_NEG_INTEGER:
1018 case V_ASN1_ENUMERATED:
1019 case V_ASN1_NEG_ENUMERATED:
1020 tint = (ASN1_INTEGER **)pval;
1021 if (!c2i_ASN1_INTEGER(tint, &cont, len))
1023 /* Fixup type to match the expected form */
1024 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
1027 case V_ASN1_OCTET_STRING:
1028 case V_ASN1_NUMERICSTRING:
1029 case V_ASN1_PRINTABLESTRING:
1030 case V_ASN1_T61STRING:
1031 case V_ASN1_VIDEOTEXSTRING:
1032 case V_ASN1_IA5STRING:
1033 case V_ASN1_UTCTIME:
1034 case V_ASN1_GENERALIZEDTIME:
1035 case V_ASN1_GRAPHICSTRING:
1036 case V_ASN1_VISIBLESTRING:
1037 case V_ASN1_GENERALSTRING:
1038 case V_ASN1_UNIVERSALSTRING:
1039 case V_ASN1_BMPSTRING:
1040 case V_ASN1_UTF8STRING:
1043 case V_ASN1_SEQUENCE:
1045 if (utype == V_ASN1_BMPSTRING && (len & 1))
1047 ASN1err(ASN1_F_ASN1_EX_C2I,
1048 ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
1051 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3))
1053 ASN1err(ASN1_F_ASN1_EX_C2I,
1054 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
1057 /* All based on ASN1_STRING and handled the same */
1060 stmp = ASN1_STRING_type_new(utype);
1063 ASN1err(ASN1_F_ASN1_EX_C2I,
1064 ERR_R_MALLOC_FAILURE);
1067 *pval = (ASN1_VALUE *)stmp;
1071 stmp = (ASN1_STRING *)*pval;
1074 /* If we've already allocated a buffer use it */
1078 OPENSSL_free(stmp->data);
1079 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
1085 if (!ASN1_STRING_set(stmp, cont, len))
1087 ASN1err(ASN1_F_ASN1_EX_C2I,
1088 ERR_R_MALLOC_FAILURE);
1089 ASN1_STRING_free(stmp);
1096 /* If ASN1_ANY and NULL type fix up value */
1097 if (typ && (utype == V_ASN1_NULL))
1098 typ->value.ptr = NULL;
1104 ASN1_TYPE_free(typ);
1112 /* This function finds the end of an ASN1 structure when passed its maximum
1113 * length, whether it is indefinite length and a pointer to the content.
1114 * This is more efficient than calling asn1_collect because it does not
1115 * recurse on each indefinite length header.
1118 static int asn1_find_end(const unsigned char **in, long len, char inf)
1122 const unsigned char *p = *in, *q;
1123 /* If not indefinite length constructed just add length */
1130 /* Indefinite length constructed form. Find the end when enough EOCs
1131 * are found. If more indefinite length constructed headers
1132 * are encountered increment the expected eoc count otherwise just
1133 * skip to the end of the data.
1137 if(asn1_check_eoc(&p, len))
1140 if (expected_eoc == 0)
1146 /* Just read in a header: only care about the length */
1147 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1150 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1161 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
1167 /* This function collects the asn1 data from a constructred string
1168 * type into a buffer. The values of 'in' and 'len' should refer
1169 * to the contents of the constructed type and 'inf' should be set
1170 * if it is indefinite length.
1173 #ifndef ASN1_MAX_STRING_NEST
1174 /* This determines how many levels of recursion are permitted in ASN1
1175 * string types. If it is not limited stack overflows can occur. If set
1176 * to zero no recursion is allowed at all. Although zero should be adequate
1177 * examples exist that require a value of 1. So 5 should be more than enough.
1179 #define ASN1_MAX_STRING_NEST 5
1183 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1184 char inf, int tag, int aclass, int depth)
1186 const unsigned char *p, *q;
1191 /* If no buffer and not indefinite length constructed just pass over
1192 * the encoded data */
1202 if (asn1_check_eoc(&p, len))
1204 /* EOC is illegal outside indefinite length
1205 * constructed form */
1208 ASN1err(ASN1_F_ASN1_COLLECT,
1209 ASN1_R_UNEXPECTED_EOC);
1216 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1217 len, tag, aclass, 0, NULL))
1219 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1223 /* If indefinite length constructed update max length */
1226 if (depth >= ASN1_MAX_STRING_NEST)
1228 ASN1err(ASN1_F_ASN1_COLLECT,
1229 ASN1_R_NESTED_ASN1_STRING);
1232 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1236 else if (plen && !collect_data(buf, &p, plen))
1242 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1249 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1255 if (!BUF_MEM_grow_clean(buf, len + plen))
1257 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1260 memcpy(buf->data + len, *p, plen);
1266 /* Check for ASN1 EOC and swallow it if found */
1268 static int asn1_check_eoc(const unsigned char **in, long len)
1270 const unsigned char *p;
1271 if (len < 2) return 0;
1281 /* Check an ASN1 tag and length: a bit like ASN1_get_object
1282 * but it sets the length for indefinite length constructed
1283 * form, we don't know the exact length but we can set an
1284 * upper bound to the amount of data available minus the
1285 * header length just read.
1288 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1289 char *inf, char *cst,
1290 const unsigned char **in, long len,
1291 int exptag, int expclass, char opt,
1297 const unsigned char *p, *q;
1301 if (ctx && ctx->valid)
1305 pclass = ctx->pclass;
1311 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1316 ctx->pclass = pclass;
1318 ctx->hdrlen = p - q;
1320 /* If definite length, and no error, length +
1321 * header can't exceed total amount of data available.
1323 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
1325 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1327 asn1_tlc_clear(ctx);
1335 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1336 asn1_tlc_clear(ctx);
1341 if ((exptag != ptag) || (expclass != pclass))
1343 /* If type is OPTIONAL, not an error:
1344 * indicate missing type.
1347 asn1_tlc_clear(ctx);
1348 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1351 /* We have a tag and class match:
1352 * assume we are going to do something with it */
1353 asn1_tlc_clear(ctx);
1357 plen = len - (p - q);
1363 *cst = i & V_ASN1_CONSTRUCTED;