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
117 /* Version to avoid compiler warning about 'c' always non-NULL */
118 #define asn1_tlc_clear_nc(c) (c)->valid = 0
120 /* Decode an ASN1 item, this currently behaves just
121 * like a standard 'd2i' function. 'in' points to
122 * a buffer to read the data from, in future we will
123 * have more advanced versions that can input data
124 * a piece at a time and this will simply be a special
128 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
129 const unsigned char **in, long len, const ASN1_ITEM *it)
132 ASN1_VALUE *ptmpval = NULL;
133 asn1_tlc_clear_nc(&c);
134 if (pval && *pval && it->itype == ASN1_ITYPE_PRIMITIVE)
136 if (ASN1_item_ex_d2i(&ptmpval, in, len, it, -1, 0, 0, &c) > 0) {
137 if (pval && it->itype != ASN1_ITYPE_PRIMITIVE) {
139 ASN1_item_free(*pval, it);
147 int ASN1_template_d2i(ASN1_VALUE **pval,
148 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
151 asn1_tlc_clear_nc(&c);
152 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
156 /* Decode an item, taking care of IMPLICIT tagging, if any.
157 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
160 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
162 int tag, int aclass, char opt, ASN1_TLC *ctx)
164 const ASN1_TEMPLATE *tt, *errtt = NULL;
165 const ASN1_COMPAT_FUNCS *cf;
166 const ASN1_EXTERN_FUNCS *ef;
167 const ASN1_AUX *aux = it->funcs;
168 ASN1_aux_cb *asn1_cb;
169 const unsigned char *p = NULL, *q;
170 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
171 unsigned char imphack = 0, oclass;
172 char seq_eoc, seq_nolen, cst, isopt;
177 ASN1_VALUE **pchptr, *ptmpval;
180 if (aux && aux->asn1_cb)
181 asn1_cb = aux->asn1_cb;
186 case ASN1_ITYPE_PRIMITIVE:
189 /* tagging or OPTIONAL is currently illegal on an item
190 * template because the flags can't get passed down.
191 * In practice this isn't a problem: we include the
192 * relevant flags from the item template in the
195 if ((tag != -1) || opt)
197 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
198 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
201 return asn1_template_ex_d2i(pval, in, len,
202 it->templates, opt, ctx);
204 return asn1_d2i_ex_primitive(pval, in, len, it,
205 tag, aclass, opt, ctx);
208 case ASN1_ITYPE_MSTRING:
210 /* Just read in tag and class */
211 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
212 &p, len, -1, 0, 1, ctx);
215 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
216 ERR_R_NESTED_ASN1_ERROR);
220 /* Must be UNIVERSAL class */
221 if (oclass != V_ASN1_UNIVERSAL)
223 /* If OPTIONAL, assume this is OK */
225 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
226 ASN1_R_MSTRING_NOT_UNIVERSAL);
229 /* Check tag matches bit map */
230 if (!(ASN1_tag2bit(otag) & it->utype))
232 /* If OPTIONAL, assume this is OK */
235 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
236 ASN1_R_MSTRING_WRONG_TAG);
239 return asn1_d2i_ex_primitive(pval, in, len,
240 it, otag, 0, 0, ctx);
242 case ASN1_ITYPE_EXTERN:
243 /* Use new style d2i */
245 return ef->asn1_ex_d2i(pval, in, len,
246 it, tag, aclass, opt, ctx);
248 case ASN1_ITYPE_COMPAT:
249 /* we must resort to old style evil hackery */
252 /* If OPTIONAL see if it is there */
260 /* Don't care about anything other than presence
263 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
264 &p, len, exptag, aclass, 1, ctx);
267 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
268 ERR_R_NESTED_ASN1_ERROR);
275 /* This is the old style evil hack IMPLICIT handling:
276 * since the underlying code is expecting a tag and
277 * class other than the one present we change the
278 * buffer temporarily then change it back afterwards.
279 * This doesn't and never did work for tags > 30.
281 * Yes this is *horrible* but it is only needed for
282 * old style d2i which will hopefully not be around
284 * FIXME: should copy the buffer then modify it so
285 * the input buffer can be const: we should *always*
286 * copy because the old style d2i might modify the
292 wp = *(unsigned char **)in;
296 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
297 ERR_R_NESTED_ASN1_ERROR);
300 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
304 ptmpval = cf->asn1_d2i(pval, in, len);
312 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
316 case ASN1_ITYPE_CHOICE:
317 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
321 /* Free up and zero CHOICE value if initialised */
322 i = asn1_get_choice_selector(pval, it);
323 if ((i >= 0) && (i < it->tcount)) {
324 tt = it->templates + i;
325 pchptr = asn1_get_field_ptr(pval, tt);
326 ASN1_template_free(pchptr, tt);
327 asn1_set_choice_selector(pval, -1, it);
329 } else if (!ASN1_item_ex_new(pval, it)) {
330 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
331 ERR_R_NESTED_ASN1_ERROR);
334 /* CHOICE type, try each possibility in turn */
336 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
338 pchptr = asn1_get_field_ptr(pval, tt);
339 /* We mark field as OPTIONAL so its absence
342 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
343 /* If field not present, try the next one */
346 /* If positive return, read OK, break loop */
349 /* Otherwise must be an ASN1 parsing error */
351 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
352 ERR_R_NESTED_ASN1_ERROR);
356 /* Did we fall off the end without reading anything? */
359 /* If OPTIONAL, this is OK */
362 /* Free and zero it */
363 ASN1_item_ex_free(pval, it);
366 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
367 ASN1_R_NO_MATCHING_CHOICE_TYPE);
371 asn1_set_choice_selector(pval, i, it);
373 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
377 case ASN1_ITYPE_NDEF_SEQUENCE:
378 case ASN1_ITYPE_SEQUENCE:
382 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
385 tag = V_ASN1_SEQUENCE;
386 aclass = V_ASN1_UNIVERSAL;
388 /* Get SEQUENCE length and update len, p */
389 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
390 &p, len, tag, aclass, opt, ctx);
393 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
394 ERR_R_NESTED_ASN1_ERROR);
399 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
401 len = tmplen - (p - *in);
404 /* If indefinite we don't do a length check */
405 else seq_nolen = seq_eoc;
408 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
409 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
413 if (!*pval && !ASN1_item_ex_new(pval, it))
415 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
416 ERR_R_NESTED_ASN1_ERROR);
420 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
423 /* Free up and zero any ADB found */
424 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
425 if (tt->flags & ASN1_TFLG_ADB_MASK) {
426 const ASN1_TEMPLATE *seqtt;
427 ASN1_VALUE **pseqval;
428 seqtt = asn1_do_adb(pval, tt, 1);
429 pseqval = asn1_get_field_ptr(pval, seqtt);
430 ASN1_template_free(pseqval, seqtt);
434 /* Get each field entry */
435 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
437 const ASN1_TEMPLATE *seqtt;
438 ASN1_VALUE **pseqval;
439 seqtt = asn1_do_adb(pval, tt, 1);
442 pseqval = asn1_get_field_ptr(pval, seqtt);
443 /* Have we ran out of data? */
447 if (asn1_check_eoc(&p, len))
451 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
452 ASN1_R_UNEXPECTED_EOC);
460 /* This determines the OPTIONAL flag value. The field
461 * cannot be omitted if it is the last of a SEQUENCE
462 * and there is still data to be read. This isn't
463 * strictly necessary but it increases efficiency in
466 if (i == (it->tcount - 1))
468 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
469 /* attempt to read in field, allowing each to be
472 ret = asn1_template_ex_d2i(pseqval, &p, len,
481 /* OPTIONAL component absent.
482 * Free and zero the field.
484 ASN1_template_free(pseqval, seqtt);
491 /* Check for EOC if expecting one */
492 if (seq_eoc && !asn1_check_eoc(&p, len))
494 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
497 /* Check all data read */
498 if (!seq_nolen && len)
500 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
501 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
505 /* If we get here we've got no more data in the SEQUENCE,
506 * however we may not have read all fields so check all
507 * remaining are OPTIONAL and clear any that are.
509 for (; i < it->tcount; tt++, i++)
511 const ASN1_TEMPLATE *seqtt;
512 seqtt = asn1_do_adb(pval, tt, 1);
515 if (seqtt->flags & ASN1_TFLG_OPTIONAL)
517 ASN1_VALUE **pseqval;
518 pseqval = asn1_get_field_ptr(pval, seqtt);
519 ASN1_template_free(pseqval, seqtt);
524 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
525 ASN1_R_FIELD_MISSING);
530 if (!asn1_enc_save(pval, *in, p - *in, it))
533 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
541 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
543 ASN1_item_ex_free(pval, it);
545 ERR_add_error_data(4, "Field=", errtt->field_name,
546 ", Type=", it->sname);
548 ERR_add_error_data(2, "Type=", it->sname);
552 /* Templates are handled with two separate functions.
553 * One handles any EXPLICIT tag and the other handles the rest.
556 static int asn1_template_ex_d2i(ASN1_VALUE **val,
557 const unsigned char **in, long inlen,
558 const ASN1_TEMPLATE *tt, char opt,
564 const unsigned char *p, *q;
569 aclass = flags & ASN1_TFLG_TAG_CLASS;
573 /* Check if EXPLICIT tag expected */
574 if (flags & ASN1_TFLG_EXPTAG)
577 /* Need to work out amount of data available to the inner
578 * content and where it starts: so read in EXPLICIT header to
581 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
582 &p, inlen, tt->tag, aclass, opt, ctx);
586 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
587 ERR_R_NESTED_ASN1_ERROR);
594 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
595 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
598 /* We've found the field so it can't be OPTIONAL now */
599 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
602 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
603 ERR_R_NESTED_ASN1_ERROR);
606 /* We read the field in OK so update length */
610 /* If NDEF we must have an EOC here */
611 if (!asn1_check_eoc(&p, len))
613 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
620 /* Otherwise we must hit the EXPLICIT tag end or its
624 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
625 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
631 return asn1_template_noexp_d2i(val, in, inlen,
638 ASN1_template_free(val, tt);
642 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
643 const unsigned char **in, long len,
644 const ASN1_TEMPLATE *tt, char opt,
649 const unsigned char *p, *q;
653 aclass = flags & ASN1_TFLG_TAG_CLASS;
658 if (flags & ASN1_TFLG_SK_MASK)
660 /* SET OF, SEQUENCE OF */
663 /* First work out expected inner tag value */
664 if (flags & ASN1_TFLG_IMPTAG)
671 skaclass = V_ASN1_UNIVERSAL;
672 if (flags & ASN1_TFLG_SET_OF)
675 sktag = V_ASN1_SEQUENCE;
678 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
679 &p, len, sktag, skaclass, opt, ctx);
682 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
683 ERR_R_NESTED_ASN1_ERROR);
689 *val = (ASN1_VALUE *)sk_new_null();
692 /* We've got a valid STACK: free up any items present */
693 STACK_OF(ASN1_VALUE) *sktmp
694 = (STACK_OF(ASN1_VALUE) *)*val;
696 while(sk_ASN1_VALUE_num(sktmp) > 0)
698 vtmp = sk_ASN1_VALUE_pop(sktmp);
699 ASN1_item_ex_free(&vtmp,
700 ASN1_ITEM_ptr(tt->item));
706 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
707 ERR_R_MALLOC_FAILURE);
711 /* Read as many items as we can */
716 /* See if EOC found */
717 if (asn1_check_eoc(&p, len))
721 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
722 ASN1_R_UNEXPECTED_EOC);
730 if (!ASN1_item_ex_d2i(&skfield, &p, len,
731 ASN1_ITEM_ptr(tt->item),
734 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
735 ERR_R_NESTED_ASN1_ERROR);
739 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
742 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
743 ERR_R_MALLOC_FAILURE);
749 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
753 else if (flags & ASN1_TFLG_IMPTAG)
755 /* IMPLICIT tagging */
756 ret = ASN1_item_ex_d2i(val, &p, len,
757 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
760 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
761 ERR_R_NESTED_ASN1_ERROR);
769 /* Nothing special */
770 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
774 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
775 ERR_R_NESTED_ASN1_ERROR);
786 ASN1_template_free(val, tt);
790 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
791 const unsigned char **in, long inlen,
793 int tag, int aclass, char opt, ASN1_TLC *ctx)
797 char cst, inf, free_cont = 0;
798 const unsigned char *p;
800 const unsigned char *cont = NULL;
804 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
805 return 0; /* Should never happen */
808 if (it->itype == ASN1_ITYPE_MSTRING)
816 if (utype == V_ASN1_ANY)
818 /* If type is ANY need to figure out type from tag */
819 unsigned char oclass;
822 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
823 ASN1_R_ILLEGAL_TAGGED_ANY);
828 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
829 ASN1_R_ILLEGAL_OPTIONAL_ANY);
833 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
834 &p, inlen, -1, 0, 0, ctx);
837 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
838 ERR_R_NESTED_ASN1_ERROR);
841 if (oclass != V_ASN1_UNIVERSAL)
842 utype = V_ASN1_OTHER;
847 aclass = V_ASN1_UNIVERSAL;
851 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
852 &p, inlen, tag, aclass, opt, ctx);
855 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
861 /* SEQUENCE, SET and "OTHER" are left in encoded form */
862 if ((utype == V_ASN1_SEQUENCE)
863 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
865 /* Clear context cache for type OTHER because the auto clear
866 * when we have a exact match wont work
868 if (utype == V_ASN1_OTHER)
872 /* SEQUENCE and SET must be constructed */
875 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
876 ASN1_R_TYPE_NOT_CONSTRUCTED);
881 /* If indefinite length constructed find the real end */
884 if (!asn1_find_end(&p, plen, inf))
890 len = p - cont + plen;
897 if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
898 || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
899 || utype == V_ASN1_ENUMERATED)
901 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
902 ASN1_R_TYPE_NOT_PRIMITIVE);
908 /* Should really check the internal tags are correct but
909 * some things may get this wrong. The relevant specs
910 * say that constructed string types should be OCTET STRINGs
911 * internally irrespective of the type. So instead just check
912 * for UNIVERSAL class and ignore the tag.
914 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0))
920 /* Append a final null to string */
921 if (!BUF_MEM_grow_clean(&buf, len + 1))
923 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
924 ERR_R_MALLOC_FAILURE);
928 cont = (const unsigned char *)buf.data;
938 /* We now have content length and type: translate into a structure */
939 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
945 if (free_cont && buf.data) OPENSSL_free(buf.data);
949 /* Translate ASN1 content octets into a structure */
951 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
952 int utype, char *free_cont, const ASN1_ITEM *it)
954 ASN1_VALUE **opval = NULL;
956 ASN1_TYPE *typ = NULL;
958 const ASN1_PRIMITIVE_FUNCS *pf;
962 if (pf && pf->prim_c2i)
963 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
964 /* If ANY type clear type and set pointer to internal value */
965 if (it->utype == V_ASN1_ANY)
969 typ = ASN1_TYPE_new();
972 *pval = (ASN1_VALUE *)typ;
975 typ = (ASN1_TYPE *)*pval;
977 if (utype != typ->type)
978 ASN1_TYPE_set(typ, utype, NULL);
980 pval = &typ->value.asn1_value;
985 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
992 ASN1err(ASN1_F_ASN1_EX_C2I,
993 ASN1_R_NULL_IS_WRONG_LENGTH);
996 *pval = (ASN1_VALUE *)1;
1002 ASN1err(ASN1_F_ASN1_EX_C2I,
1003 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
1008 ASN1_BOOLEAN *tbool;
1009 tbool = (ASN1_BOOLEAN *)pval;
1014 case V_ASN1_BIT_STRING:
1015 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
1019 case V_ASN1_INTEGER:
1020 case V_ASN1_NEG_INTEGER:
1021 case V_ASN1_ENUMERATED:
1022 case V_ASN1_NEG_ENUMERATED:
1023 tint = (ASN1_INTEGER **)pval;
1024 if (!c2i_ASN1_INTEGER(tint, &cont, len))
1026 /* Fixup type to match the expected form */
1027 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
1030 case V_ASN1_OCTET_STRING:
1031 case V_ASN1_NUMERICSTRING:
1032 case V_ASN1_PRINTABLESTRING:
1033 case V_ASN1_T61STRING:
1034 case V_ASN1_VIDEOTEXSTRING:
1035 case V_ASN1_IA5STRING:
1036 case V_ASN1_UTCTIME:
1037 case V_ASN1_GENERALIZEDTIME:
1038 case V_ASN1_GRAPHICSTRING:
1039 case V_ASN1_VISIBLESTRING:
1040 case V_ASN1_GENERALSTRING:
1041 case V_ASN1_UNIVERSALSTRING:
1042 case V_ASN1_BMPSTRING:
1043 case V_ASN1_UTF8STRING:
1046 case V_ASN1_SEQUENCE:
1048 if (utype == V_ASN1_BMPSTRING && (len & 1))
1050 ASN1err(ASN1_F_ASN1_EX_C2I,
1051 ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
1054 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3))
1056 ASN1err(ASN1_F_ASN1_EX_C2I,
1057 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
1060 /* All based on ASN1_STRING and handled the same */
1063 stmp = ASN1_STRING_type_new(utype);
1066 ASN1err(ASN1_F_ASN1_EX_C2I,
1067 ERR_R_MALLOC_FAILURE);
1070 *pval = (ASN1_VALUE *)stmp;
1074 stmp = (ASN1_STRING *)*pval;
1077 /* If we've already allocated a buffer use it */
1081 OPENSSL_free(stmp->data);
1082 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
1088 if (!ASN1_STRING_set(stmp, cont, len))
1090 ASN1err(ASN1_F_ASN1_EX_C2I,
1091 ERR_R_MALLOC_FAILURE);
1092 ASN1_STRING_free(stmp);
1099 /* If ASN1_ANY and NULL type fix up value */
1100 if (typ && (utype == V_ASN1_NULL))
1101 typ->value.ptr = NULL;
1107 ASN1_TYPE_free(typ);
1115 /* This function finds the end of an ASN1 structure when passed its maximum
1116 * length, whether it is indefinite length and a pointer to the content.
1117 * This is more efficient than calling asn1_collect because it does not
1118 * recurse on each indefinite length header.
1121 static int asn1_find_end(const unsigned char **in, long len, char inf)
1125 const unsigned char *p = *in, *q;
1126 /* If not indefinite length constructed just add length */
1133 /* Indefinite length constructed form. Find the end when enough EOCs
1134 * are found. If more indefinite length constructed headers
1135 * are encountered increment the expected eoc count otherwise just
1136 * skip to the end of the data.
1140 if(asn1_check_eoc(&p, len))
1143 if (expected_eoc == 0)
1149 /* Just read in a header: only care about the length */
1150 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1153 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1164 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
1170 /* This function collects the asn1 data from a constructred string
1171 * type into a buffer. The values of 'in' and 'len' should refer
1172 * to the contents of the constructed type and 'inf' should be set
1173 * if it is indefinite length.
1176 #ifndef ASN1_MAX_STRING_NEST
1177 /* This determines how many levels of recursion are permitted in ASN1
1178 * string types. If it is not limited stack overflows can occur. If set
1179 * to zero no recursion is allowed at all. Although zero should be adequate
1180 * examples exist that require a value of 1. So 5 should be more than enough.
1182 #define ASN1_MAX_STRING_NEST 5
1186 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1187 char inf, int tag, int aclass, int depth)
1189 const unsigned char *p, *q;
1194 /* If no buffer and not indefinite length constructed just pass over
1195 * the encoded data */
1205 if (asn1_check_eoc(&p, len))
1207 /* EOC is illegal outside indefinite length
1208 * constructed form */
1211 ASN1err(ASN1_F_ASN1_COLLECT,
1212 ASN1_R_UNEXPECTED_EOC);
1219 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1220 len, tag, aclass, 0, NULL))
1222 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1226 /* If indefinite length constructed update max length */
1229 if (depth >= ASN1_MAX_STRING_NEST)
1231 ASN1err(ASN1_F_ASN1_COLLECT,
1232 ASN1_R_NESTED_ASN1_STRING);
1235 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1239 else if (plen && !collect_data(buf, &p, plen))
1245 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1252 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1258 if (!BUF_MEM_grow_clean(buf, len + plen))
1260 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1263 memcpy(buf->data + len, *p, plen);
1269 /* Check for ASN1 EOC and swallow it if found */
1271 static int asn1_check_eoc(const unsigned char **in, long len)
1273 const unsigned char *p;
1274 if (len < 2) return 0;
1284 /* Check an ASN1 tag and length: a bit like ASN1_get_object
1285 * but it sets the length for indefinite length constructed
1286 * form, we don't know the exact length but we can set an
1287 * upper bound to the amount of data available minus the
1288 * header length just read.
1291 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1292 char *inf, char *cst,
1293 const unsigned char **in, long len,
1294 int exptag, int expclass, char opt,
1300 const unsigned char *p, *q;
1304 if (ctx && ctx->valid)
1308 pclass = ctx->pclass;
1314 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1319 ctx->pclass = pclass;
1321 ctx->hdrlen = p - q;
1323 /* If definite length, and no error, length +
1324 * header can't exceed total amount of data available.
1326 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
1328 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1330 asn1_tlc_clear(ctx);
1338 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1339 asn1_tlc_clear(ctx);
1344 if ((exptag != ptag) || (expclass != pclass))
1346 /* If type is OPTIONAL, not an error:
1347 * indicate missing type.
1350 asn1_tlc_clear(ctx);
1351 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1354 /* We have a tag and class match:
1355 * assume we are going to do something with it */
1356 asn1_tlc_clear(ctx);
1360 plen = len - (p - q);
1366 *cst = i & V_ASN1_CONSTRUCTED;