2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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);
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, size_t len,
133 ASN1_VALUE *ptmpval = NULL;
136 asn1_tlc_clear_nc(&c);
137 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
142 int ASN1_template_d2i(ASN1_VALUE **pval,
143 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
146 asn1_tlc_clear_nc(&c);
147 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
151 /* Decode an item, taking care of IMPLICIT tagging, if any.
152 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
155 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
157 int tag, int aclass, char opt, ASN1_TLC *ctx)
159 const ASN1_TEMPLATE *tt, *errtt = NULL;
160 const ASN1_COMPAT_FUNCS *cf;
161 const ASN1_EXTERN_FUNCS *ef;
162 const ASN1_AUX *aux = it->funcs;
163 ASN1_aux_cb *asn1_cb;
164 const unsigned char *p = NULL, *q;
165 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
166 unsigned char imphack = 0, oclass;
167 char seq_eoc, seq_nolen, cst, isopt;
172 ASN1_VALUE *pchval, **pchptr, *ptmpval;
175 if (aux && aux->asn1_cb)
176 asn1_cb = aux->asn1_cb;
181 case ASN1_ITYPE_PRIMITIVE:
184 /* tagging or OPTIONAL is currently illegal on an item
185 * template because the flags can't get passed down.
186 * In practice this isn't a problem: we include the
187 * relevant flags from the item template in the
190 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);
203 case ASN1_ITYPE_MSTRING:
205 /* Just read in tag and class */
206 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
207 &p, len, -1, 0, 1, ctx);
210 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
211 ERR_R_NESTED_ASN1_ERROR);
215 /* Must be UNIVERSAL class */
216 if (oclass != V_ASN1_UNIVERSAL)
218 /* If OPTIONAL, assume this is OK */
220 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
221 ASN1_R_MSTRING_NOT_UNIVERSAL);
224 /* Check tag matches bit map */
225 if (!(ASN1_tag2bit(otag) & it->utype))
227 /* If OPTIONAL, assume this is OK */
230 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
231 ASN1_R_MSTRING_WRONG_TAG);
234 return asn1_d2i_ex_primitive(pval, in, len,
235 it, otag, 0, 0, ctx);
237 case ASN1_ITYPE_EXTERN:
238 /* Use new style d2i */
240 return ef->asn1_ex_d2i(pval, in, len,
241 it, tag, aclass, opt, ctx);
243 case ASN1_ITYPE_COMPAT:
244 /* we must resort to old style evil hackery */
247 /* If OPTIONAL see if it is there */
255 /* Don't care about anything other than presence
258 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
259 &p, len, exptag, aclass, 1, ctx);
262 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
263 ERR_R_NESTED_ASN1_ERROR);
270 /* This is the old style evil hack IMPLICIT handling:
271 * since the underlying code is expecting a tag and
272 * class other than the one present we change the
273 * buffer temporarily then change it back afterwards.
274 * This doesn't and never did work for tags > 30.
276 * Yes this is *horrible* but it is only needed for
277 * old style d2i which will hopefully not be around
279 * FIXME: should copy the buffer then modify it so
280 * the input buffer can be const: we should *always*
281 * copy because the old style d2i might modify the
287 wp = *(unsigned char **)in;
291 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
292 ERR_R_NESTED_ASN1_ERROR);
295 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
299 ptmpval = cf->asn1_d2i(pval, in, len);
307 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
311 case ASN1_ITYPE_CHOICE:
312 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
315 /* Allocate structure */
316 if (!*pval && !ASN1_item_ex_new(pval, it))
318 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
319 ERR_R_NESTED_ASN1_ERROR);
322 /* CHOICE type, try each possibility in turn */
325 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
327 pchptr = asn1_get_field_ptr(pval, tt);
328 /* We mark field as OPTIONAL so its absence
331 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
332 /* If field not present, try the next one */
335 /* If positive return, read OK, break loop */
338 /* Otherwise must be an ASN1 parsing error */
340 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
341 ERR_R_NESTED_ASN1_ERROR);
345 /* Did we fall off the end without reading anything? */
348 /* If OPTIONAL, this is OK */
351 /* Free and zero it */
352 ASN1_item_ex_free(pval, it);
355 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
356 ASN1_R_NO_MATCHING_CHOICE_TYPE);
360 asn1_set_choice_selector(pval, i, it);
362 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
366 case ASN1_ITYPE_NDEF_SEQUENCE:
367 case ASN1_ITYPE_SEQUENCE:
371 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
374 tag = V_ASN1_SEQUENCE;
375 aclass = V_ASN1_UNIVERSAL;
377 /* Get SEQUENCE length and update len, p */
378 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
379 &p, len, tag, aclass, opt, ctx);
382 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
383 ERR_R_NESTED_ASN1_ERROR);
388 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
390 len = tmplen - (p - *in);
393 /* If indefinite we don't do a length check */
394 else seq_nolen = seq_eoc;
397 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
398 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
402 if (!*pval && !ASN1_item_ex_new(pval, it))
404 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
405 ERR_R_NESTED_ASN1_ERROR);
409 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
412 /* Get each field entry */
413 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
415 const ASN1_TEMPLATE *seqtt;
416 ASN1_VALUE **pseqval;
417 seqtt = asn1_do_adb(pval, tt, 1);
420 pseqval = asn1_get_field_ptr(pval, seqtt);
421 /* Have we ran out of data? */
425 if (asn1_check_eoc(&p, len))
429 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
430 ASN1_R_UNEXPECTED_EOC);
438 /* This determines the OPTIONAL flag value. The field
439 * cannot be omitted if it is the last of a SEQUENCE
440 * and there is still data to be read. This isn't
441 * strictly necessary but it increases efficiency in
444 if (i == (it->tcount - 1))
446 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
447 /* attempt to read in field, allowing each to be
450 ret = asn1_template_ex_d2i(pseqval, &p, len,
459 /* OPTIONAL component absent.
460 * Free and zero the field.
462 ASN1_template_free(pseqval, seqtt);
469 /* Check for EOC if expecting one */
470 if (seq_eoc && !asn1_check_eoc(&p, len))
472 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
475 /* Check all data read */
476 if (!seq_nolen && len)
478 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
479 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
483 /* If we get here we've got no more data in the SEQUENCE,
484 * however we may not have read all fields so check all
485 * remaining are OPTIONAL and clear any that are.
487 for (; i < it->tcount; tt++, i++)
489 const ASN1_TEMPLATE *seqtt;
490 seqtt = asn1_do_adb(pval, tt, 1);
493 if (seqtt->flags & ASN1_TFLG_OPTIONAL)
495 ASN1_VALUE **pseqval;
496 pseqval = asn1_get_field_ptr(pval, seqtt);
497 ASN1_template_free(pseqval, seqtt);
502 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
503 ASN1_R_FIELD_MISSING);
508 if (!asn1_enc_save(pval, *in, p - *in, it))
511 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
519 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
521 ASN1_item_ex_free(pval, it);
523 ERR_add_error_data(4, "Field=", errtt->field_name,
524 ", Type=", it->sname);
526 ERR_add_error_data(2, "Type=", it->sname);
530 /* Templates are handled with two separate functions.
531 * One handles any EXPLICIT tag and the other handles the rest.
534 static int asn1_template_ex_d2i(ASN1_VALUE **val,
535 const unsigned char **in, long inlen,
536 const ASN1_TEMPLATE *tt, char opt,
542 const unsigned char *p, *q;
547 aclass = flags & ASN1_TFLG_TAG_CLASS;
551 /* Check if EXPLICIT tag expected */
552 if (flags & ASN1_TFLG_EXPTAG)
555 /* Need to work out amount of data available to the inner
556 * content and where it starts: so read in EXPLICIT header to
559 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
560 &p, inlen, tt->tag, aclass, opt, ctx);
564 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
565 ERR_R_NESTED_ASN1_ERROR);
572 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
573 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
576 /* We've found the field so it can't be OPTIONAL now */
577 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
580 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
581 ERR_R_NESTED_ASN1_ERROR);
584 /* We read the field in OK so update length */
588 /* If NDEF we must have an EOC here */
589 if (!asn1_check_eoc(&p, len))
591 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
598 /* Otherwise we must hit the EXPLICIT tag end or its
602 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
603 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
609 return asn1_template_noexp_d2i(val, in, inlen,
616 ASN1_template_free(val, tt);
621 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
622 const unsigned char **in, long len,
623 const ASN1_TEMPLATE *tt, char opt,
628 const unsigned char *p, *q;
632 aclass = flags & ASN1_TFLG_TAG_CLASS;
637 if (flags & ASN1_TFLG_SK_MASK)
639 /* SET OF, SEQUENCE OF */
642 /* First work out expected inner tag value */
643 if (flags & ASN1_TFLG_IMPTAG)
650 skaclass = V_ASN1_UNIVERSAL;
651 if (flags & ASN1_TFLG_SET_OF)
654 sktag = V_ASN1_SEQUENCE;
657 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
658 &p, len, sktag, skaclass, opt, ctx);
661 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
662 ERR_R_NESTED_ASN1_ERROR);
668 *val = (ASN1_VALUE *)sk_new_null();
671 /* We've got a valid STACK: free up any items present */
672 STACK_OF(ASN1_VALUE) *sktmp
673 = (STACK_OF(ASN1_VALUE) *)*val;
675 while(sk_ASN1_VALUE_num(sktmp) > 0)
677 vtmp = sk_ASN1_VALUE_pop(sktmp);
678 ASN1_item_ex_free(&vtmp,
679 ASN1_ITEM_ptr(tt->item));
685 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
686 ERR_R_MALLOC_FAILURE);
690 /* Read as many items as we can */
695 /* See if EOC found */
696 if (asn1_check_eoc(&p, len))
700 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
701 ASN1_R_UNEXPECTED_EOC);
709 if (!ASN1_item_ex_d2i(&skfield, &p, len,
710 ASN1_ITEM_ptr(tt->item),
713 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
714 ERR_R_NESTED_ASN1_ERROR);
718 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
721 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
722 ERR_R_MALLOC_FAILURE);
728 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
732 else if (flags & ASN1_TFLG_IMPTAG)
734 /* IMPLICIT tagging */
735 ret = ASN1_item_ex_d2i(val, &p, len,
736 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
739 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
740 ERR_R_NESTED_ASN1_ERROR);
748 /* Nothing special */
749 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
753 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
754 ERR_R_NESTED_ASN1_ERROR);
765 ASN1_template_free(val, tt);
770 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
771 const unsigned char **in, long inlen,
773 int tag, int aclass, char opt, ASN1_TLC *ctx)
777 char cst, inf, free_cont = 0;
778 const unsigned char *p;
780 const unsigned char *cont = NULL;
784 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
785 return 0; /* Should never happen */
788 if (it->itype == ASN1_ITYPE_MSTRING)
796 if (utype == V_ASN1_ANY)
798 /* If type is ANY need to figure out type from tag */
799 unsigned char oclass;
802 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
803 ASN1_R_ILLEGAL_TAGGED_ANY);
808 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
809 ASN1_R_ILLEGAL_OPTIONAL_ANY);
813 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
814 &p, inlen, -1, 0, 0, ctx);
817 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
818 ERR_R_NESTED_ASN1_ERROR);
821 if (oclass != V_ASN1_UNIVERSAL)
822 utype = V_ASN1_OTHER;
827 aclass = V_ASN1_UNIVERSAL;
831 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
832 &p, inlen, tag, aclass, opt, ctx);
835 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
841 /* SEQUENCE, SET and "OTHER" are left in encoded form */
842 if ((utype == V_ASN1_SEQUENCE)
843 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
845 /* Clear context cache for type OTHER because the auto clear
846 * when we have a exact match wont work
848 if (utype == V_ASN1_OTHER)
852 /* SEQUENCE and SET must be constructed */
855 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
856 ASN1_R_TYPE_NOT_CONSTRUCTED);
861 /* If indefinite length constructed find the real end */
864 if (!asn1_find_end(&p, plen, inf))
870 len = p - cont + plen;
880 /* Should really check the internal tags are correct but
881 * some things may get this wrong. The relevant specs
882 * say that constructed string types should be OCTET STRINGs
883 * internally irrespective of the type. So instead just check
884 * for UNIVERSAL class and ignore the tag.
886 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL))
892 /* Append a final null to string */
893 if (!BUF_MEM_grow_clean(&buf, len + 1))
895 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
896 ERR_R_MALLOC_FAILURE);
900 cont = (const unsigned char *)buf.data;
910 /* We now have content length and type: translate into a structure */
911 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
917 if (free_cont && buf.data) OPENSSL_free(buf.data);
921 /* Translate ASN1 content octets into a structure */
923 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
924 int utype, char *free_cont, const ASN1_ITEM *it)
926 ASN1_VALUE **opval = NULL;
928 ASN1_TYPE *typ = NULL;
930 const ASN1_PRIMITIVE_FUNCS *pf;
934 if (pf && pf->prim_c2i)
935 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
936 /* If ANY type clear type and set pointer to internal value */
937 if (it->utype == V_ASN1_ANY)
941 typ = ASN1_TYPE_new();
944 *pval = (ASN1_VALUE *)typ;
947 typ = (ASN1_TYPE *)*pval;
949 if (utype != typ->type)
950 ASN1_TYPE_set(typ, utype, NULL);
952 pval = &typ->value.asn1_value;
957 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
964 ASN1err(ASN1_F_ASN1_EX_C2I,
965 ASN1_R_NULL_IS_WRONG_LENGTH);
968 *pval = (ASN1_VALUE *)1;
974 ASN1err(ASN1_F_ASN1_EX_C2I,
975 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
981 tbool = (ASN1_BOOLEAN *)pval;
986 case V_ASN1_BIT_STRING:
987 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
992 case V_ASN1_NEG_INTEGER:
993 case V_ASN1_ENUMERATED:
994 case V_ASN1_NEG_ENUMERATED:
995 tint = (ASN1_INTEGER **)pval;
996 if (!c2i_ASN1_INTEGER(tint, &cont, len))
998 /* Fixup type to match the expected form */
999 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
1002 case V_ASN1_OCTET_STRING:
1003 case V_ASN1_NUMERICSTRING:
1004 case V_ASN1_PRINTABLESTRING:
1005 case V_ASN1_T61STRING:
1006 case V_ASN1_VIDEOTEXSTRING:
1007 case V_ASN1_IA5STRING:
1008 case V_ASN1_UTCTIME:
1009 case V_ASN1_GENERALIZEDTIME:
1010 case V_ASN1_GRAPHICSTRING:
1011 case V_ASN1_VISIBLESTRING:
1012 case V_ASN1_GENERALSTRING:
1013 case V_ASN1_UNIVERSALSTRING:
1014 case V_ASN1_BMPSTRING:
1015 case V_ASN1_UTF8STRING:
1018 case V_ASN1_SEQUENCE:
1020 /* All based on ASN1_STRING and handled the same */
1023 stmp = ASN1_STRING_type_new(utype);
1026 ASN1err(ASN1_F_ASN1_EX_C2I,
1027 ERR_R_MALLOC_FAILURE);
1030 *pval = (ASN1_VALUE *)stmp;
1034 stmp = (ASN1_STRING *)*pval;
1037 /* If we've already allocated a buffer use it */
1041 OPENSSL_free(stmp->data);
1042 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
1048 if (!ASN1_STRING_set(stmp, cont, len))
1050 ASN1err(ASN1_F_ASN1_EX_C2I,
1051 ERR_R_MALLOC_FAILURE);
1052 ASN1_STRING_free(stmp);
1059 /* If ASN1_ANY and NULL type fix up value */
1060 if (typ && (utype == V_ASN1_NULL))
1061 typ->value.ptr = NULL;
1067 ASN1_TYPE_free(typ);
1075 /* This function finds the end of an ASN1 structure when passed its maximum
1076 * length, whether it is indefinite length and a pointer to the content.
1077 * This is more efficient than calling asn1_collect because it does not
1078 * recurse on each indefinite length header.
1081 static int asn1_find_end(const unsigned char **in, long len, char inf)
1085 const unsigned char *p = *in, *q;
1086 /* If not indefinite length constructed just add length */
1093 /* Indefinite length constructed form. Find the end when enough EOCs
1094 * are found. If more indefinite length constructed headers
1095 * are encountered increment the expected eoc count otherwise just
1096 * skip to the end of the data.
1100 if(asn1_check_eoc(&p, len))
1103 if (expected_eoc == 0)
1109 /* Just read in a header: only care about the length */
1110 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1113 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1124 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
1130 /* This function collects the asn1 data from a constructred string
1131 * type into a buffer. The values of 'in' and 'len' should refer
1132 * to the contents of the constructed type and 'inf' should be set
1133 * if it is indefinite length.
1136 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1137 char inf, int tag, int aclass)
1139 const unsigned char *p, *q;
1144 /* If no buffer and not indefinite length constructed just pass over
1145 * the encoded data */
1155 if (asn1_check_eoc(&p, len))
1157 /* EOC is illegal outside indefinite length
1158 * constructed form */
1161 ASN1err(ASN1_F_ASN1_COLLECT,
1162 ASN1_R_UNEXPECTED_EOC);
1169 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1170 len, tag, aclass, 0, NULL))
1172 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1176 /* If indefinite length constructed update max length */
1179 #ifdef OPENSSL_ALLOW_NESTED_ASN1_STRINGS
1180 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass))
1183 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1187 else if (plen && !collect_data(buf, &p, plen))
1193 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1200 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1206 if (!BUF_MEM_grow_clean(buf, len + plen))
1208 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1211 memcpy(buf->data + len, *p, plen);
1217 /* Check for ASN1 EOC and swallow it if found */
1219 static int asn1_check_eoc(const unsigned char **in, long len)
1221 const unsigned char *p;
1222 if (len < 2) return 0;
1232 /* Check an ASN1 tag and length: a bit like ASN1_get_object
1233 * but it sets the length for indefinite length constructed
1234 * form, we don't know the exact length but we can set an
1235 * upper bound to the amount of data available minus the
1236 * header length just read.
1239 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1240 char *inf, char *cst,
1241 const unsigned char **in, long len,
1242 int exptag, int expclass, char opt,
1248 const unsigned char *p, *q;
1252 if (ctx && ctx->valid)
1256 pclass = ctx->pclass;
1262 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1267 ctx->pclass = pclass;
1269 ctx->hdrlen = p - q;
1271 /* If definite length, and no error, length +
1272 * header can't exceed total amount of data available.
1274 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
1276 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1278 asn1_tlc_clear(ctx);
1286 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1287 asn1_tlc_clear(ctx);
1292 if ((exptag != ptag) || (expclass != pclass))
1294 /* If type is OPTIONAL, not an error:
1295 * indicate missing type.
1298 asn1_tlc_clear(ctx);
1299 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1302 /* We have a tag and class match:
1303 * assume we are going to do something with it */
1304 asn1_tlc_clear(ctx);
1308 plen = len - (p - q);
1314 *cst = i & V_ASN1_CONSTRUCTED;