2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000-2004 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);
70 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
71 char inf, int tag, int aclass);
73 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
75 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
77 const unsigned char **in, long len,
78 int exptag, int expclass, char opt,
81 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
82 const unsigned char **in, long len,
83 const ASN1_TEMPLATE *tt, char opt,
85 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
86 const unsigned char **in, long len,
87 const ASN1_TEMPLATE *tt, char opt,
89 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
90 const unsigned char **in, long len,
92 int tag, int aclass, char opt, ASN1_TLC *ctx);
94 /* Table to convert tags to bit values, used for MSTRING type */
95 static unsigned long tag2bit[32] = {
96 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
97 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
98 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
99 B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
100 0, 0, B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
101 B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
102 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
103 B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
104 B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
107 unsigned long ASN1_tag2bit(int tag)
109 if ((tag < 0) || (tag > 30)) return 0;
113 /* Macro to initialize and invalidate the cache */
115 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
117 /* Decode an ASN1 item, this currently behaves just
118 * like a standard 'd2i' function. 'in' points to
119 * a buffer to read the data from, in future we will
120 * have more advanced versions that can input data
121 * a piece at a time and this will simply be a special
125 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
126 const unsigned char **in, long len, const ASN1_ITEM *it)
129 ASN1_VALUE *ptmpval = NULL;
133 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
138 int ASN1_template_d2i(ASN1_VALUE **pval,
139 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
143 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
147 /* Decode an item, taking care of IMPLICIT tagging, if any.
148 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
151 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
153 int tag, int aclass, char opt, ASN1_TLC *ctx)
155 const ASN1_TEMPLATE *tt, *errtt = NULL;
156 const ASN1_COMPAT_FUNCS *cf;
157 const ASN1_EXTERN_FUNCS *ef;
158 const ASN1_AUX *aux = it->funcs;
159 ASN1_aux_cb *asn1_cb;
160 const unsigned char *p, *q;
161 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
162 unsigned char imphack = 0, oclass;
163 char seq_eoc, seq_nolen, cst, isopt;
168 ASN1_VALUE *pchval, **pchptr, *ptmpval;
171 if (aux && aux->asn1_cb)
172 asn1_cb = aux->asn1_cb;
177 case ASN1_ITYPE_PRIMITIVE:
180 /* tagging or OPTIONAL is currently illegal on an item
181 * template because the flags can't get passed down.
182 * In practice this isn't a problem: we include the
183 * relevant flags from the item template in the
186 if ((tag != -1) || opt)
188 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
189 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
192 return asn1_template_ex_d2i(pval, in, len,
193 it->templates, opt, ctx);
195 return asn1_d2i_ex_primitive(pval, in, len, it,
196 tag, aclass, opt, ctx);
199 case ASN1_ITYPE_MSTRING:
201 /* Just read in tag and class */
202 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
203 &p, len, -1, 0, 1, ctx);
206 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
207 ERR_R_NESTED_ASN1_ERROR);
211 /* Must be UNIVERSAL class */
212 if (oclass != V_ASN1_UNIVERSAL)
214 /* If OPTIONAL, assume this is OK */
216 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
217 ASN1_R_MSTRING_NOT_UNIVERSAL);
220 /* Check tag matches bit map */
221 if (!(ASN1_tag2bit(otag) & it->utype))
223 /* If OPTIONAL, assume this is OK */
226 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
227 ASN1_R_MSTRING_WRONG_TAG);
230 return asn1_d2i_ex_primitive(pval, in, len,
231 it, otag, 0, 0, ctx);
233 case ASN1_ITYPE_EXTERN:
234 /* Use new style d2i */
236 return ef->asn1_ex_d2i(pval, in, len,
237 it, tag, aclass, opt, ctx);
239 case ASN1_ITYPE_COMPAT:
240 /* we must resort to old style evil hackery */
243 /* If OPTIONAL see if it is there */
251 /* Don't care about anything other than presence
254 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
255 &p, len, exptag, aclass, 1, ctx);
258 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
259 ERR_R_NESTED_ASN1_ERROR);
266 /* This is the old style evil hack IMPLICIT handling:
267 * since the underlying code is expecting a tag and
268 * class other than the one present we change the
269 * buffer temporarily then change it back afterwards.
270 * This doesn't and never did work for tags > 30.
272 * Yes this is *horrible* but it is only needed for
273 * old style d2i which will hopefully not be around
275 * FIXME: should copy the buffer then modify it so
276 * the input buffer can be const: we should *always*
277 * copy because the old style d2i might modify the
283 wp = *(unsigned char **)in;
285 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
289 ptmpval = cf->asn1_d2i(pval, in, len);
297 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
301 case ASN1_ITYPE_CHOICE:
302 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
305 /* Allocate structure */
306 if (!*pval && !ASN1_item_ex_new(pval, it))
308 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
309 ERR_R_NESTED_ASN1_ERROR);
312 /* CHOICE type, try each possibility in turn */
315 for(i = 0, tt=it->templates; i < it->tcount; i++, tt++)
317 pchptr = asn1_get_field_ptr(pval, tt);
318 /* We mark field as OPTIONAL so its absence
321 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
322 /* If field not present, try the next one */
325 /* If positive return, read OK, break loop */
328 /* Otherwise must be an ASN1 parsing error */
330 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
331 ERR_R_NESTED_ASN1_ERROR);
335 /* Did we fall off the end without reading anything? */
338 /* If OPTIONAL, this is OK */
341 /* Free and zero it */
342 ASN1_item_ex_free(pval, it);
345 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
346 ASN1_R_NO_MATCHING_CHOICE_TYPE);
350 asn1_set_choice_selector(pval, i, it);
352 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
356 case ASN1_ITYPE_NDEF_SEQUENCE:
357 case ASN1_ITYPE_SEQUENCE:
361 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
364 tag = V_ASN1_SEQUENCE;
365 aclass = V_ASN1_UNIVERSAL;
367 /* Get SEQUENCE length and update len, p */
368 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
369 &p, len, tag, aclass, opt, ctx);
372 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
373 ERR_R_NESTED_ASN1_ERROR);
378 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
380 len = tmplen - (p - *in);
383 /* If indefinite we don't do a length check */
384 else seq_nolen = seq_eoc;
387 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
388 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
392 if (!*pval && !!ASN1_item_ex_new(pval, it))
394 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
395 ERR_R_NESTED_ASN1_ERROR);
399 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
402 /* Get each field entry */
403 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
405 const ASN1_TEMPLATE *seqtt;
406 ASN1_VALUE **pseqval;
407 seqtt = asn1_do_adb(pval, tt, 1);
410 pseqval = asn1_get_field_ptr(pval, seqtt);
411 /* Have we ran out of data? */
415 if (asn1_check_eoc(&p, len))
419 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
420 ASN1_R_UNEXPECTED_EOC);
428 /* This determines the OPTIONAL flag value. The field
429 * cannot be omitted if it is the last of a SEQUENCE
430 * and there is still data to be read. This isn't
431 * strictly necessary but it increases efficiency in
434 if (i == (it->tcount - 1))
436 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
437 /* attempt to read in field, allowing each to be
440 ret = asn1_template_ex_d2i(pseqval, &p, len,
449 /* OPTIONAL component absent.
450 * Free and zero the field.
452 ASN1_template_free(pseqval, seqtt);
459 /* Check for EOC if expecting one */
460 if (seq_eoc && !asn1_check_eoc(&p, len))
462 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
465 /* Check all data read */
466 if (!seq_nolen && len)
468 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
469 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
473 /* If we get here we've got no more data in the SEQUENCE,
474 * however we may not have read all fields so check all
475 * remaining are OPTIONAL and clear any that are.
477 for (; i < it->tcount; tt++, i++)
479 const ASN1_TEMPLATE *seqtt;
480 seqtt = asn1_do_adb(pval, tt, 1);
483 if (seqtt->flags & ASN1_TFLG_OPTIONAL)
485 ASN1_VALUE **pseqval;
486 pseqval = asn1_get_field_ptr(pval, seqtt);
487 ASN1_template_free(pseqval, seqtt);
492 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
493 ASN1_R_FIELD_MISSING);
498 if (!asn1_enc_save(pval, *in, p - *in, it))
501 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
509 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
511 ASN1_item_ex_free(pval, it);
513 ERR_add_error_data(4, "Field=", errtt->field_name,
514 ", Type=", it->sname);
516 ERR_add_error_data(2, "Type=", it->sname);
520 /* Templates are handled with two separate functions.
521 * One handles any EXPLICIT tag and the other handles the rest.
524 static int asn1_template_ex_d2i(ASN1_VALUE **val,
525 const unsigned char **in, long inlen,
526 const ASN1_TEMPLATE *tt, char opt,
532 const unsigned char *p, *q;
537 aclass = flags & ASN1_TFLG_TAG_CLASS;
541 /* Check if EXPLICIT tag expected */
542 if (flags & ASN1_TFLG_EXPTAG)
545 /* Need to work out amount of data available to the inner
546 * content and where it starts: so read in EXPLICIT header to
549 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
550 &p, inlen, tt->tag, aclass, opt, ctx);
554 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
555 ERR_R_NESTED_ASN1_ERROR);
562 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
563 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
566 /* We've found the field so it can't be OPTIONAL now */
567 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
570 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
571 ERR_R_NESTED_ASN1_ERROR);
574 /* We read the field in OK so update length */
578 /* If NDEF we must have an EOC here */
579 if (!asn1_check_eoc(&p, len))
581 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
588 /* Otherwise we must hit the EXPLICIT tag end or its
592 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
593 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
599 return asn1_template_noexp_d2i(val, in, inlen,
606 ASN1_template_free(val, tt);
611 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
612 const unsigned char **in, long len,
613 const ASN1_TEMPLATE *tt, char opt,
618 const unsigned char *p, *q;
622 aclass = flags & ASN1_TFLG_TAG_CLASS;
627 if (flags & ASN1_TFLG_SK_MASK)
629 /* SET OF, SEQUENCE OF */
632 /* First work out expected inner tag value */
633 if (flags & ASN1_TFLG_IMPTAG)
640 skaclass = V_ASN1_UNIVERSAL;
641 if (flags & ASN1_TFLG_SET_OF)
644 sktag = V_ASN1_SEQUENCE;
647 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
648 &p, len, sktag, skaclass, opt, ctx);
651 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
652 ERR_R_NESTED_ASN1_ERROR);
658 *val = (ASN1_VALUE *)sk_new_null();
661 /* We've got a valid STACK: free up any items present */
662 STACK *sktmp = (STACK *)*val;
664 while(sk_num(sktmp) > 0)
666 vtmp = (ASN1_VALUE *)sk_pop(sktmp);
667 ASN1_item_ex_free(&vtmp,
668 ASN1_ITEM_ptr(tt->item));
674 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
675 ERR_R_MALLOC_FAILURE);
679 /* Read as many items as we can */
684 /* See if EOC found */
685 if (asn1_check_eoc(&p, len))
689 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
690 ASN1_R_UNEXPECTED_EOC);
698 if (!ASN1_item_ex_d2i(&skfield, &p, len,
699 ASN1_ITEM_ptr(tt->item),
702 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
703 ERR_R_NESTED_ASN1_ERROR);
707 if (!sk_push((STACK *)*val, (char *)skfield))
709 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
710 ERR_R_MALLOC_FAILURE);
716 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_MISSING_EOC);
720 else if (flags & ASN1_TFLG_IMPTAG)
722 /* IMPLICIT tagging */
723 ret = ASN1_item_ex_d2i(val, &p, len,
724 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
727 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
728 ERR_R_NESTED_ASN1_ERROR);
736 /* Nothing special */
737 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
741 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I,
742 ERR_R_NESTED_ASN1_ERROR);
753 ASN1_template_free(val, tt);
758 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
759 const unsigned char **in, long inlen,
761 int tag, int aclass, char opt, ASN1_TLC *ctx)
765 char cst, inf, free_cont = 0;
766 const unsigned char *p;
768 const unsigned char *cont = NULL;
772 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
773 return 0; /* Should never happen */
776 if (it->itype == ASN1_ITYPE_MSTRING)
784 if (utype == V_ASN1_ANY)
786 /* If type is ANY need to figure out type from tag */
787 unsigned char oclass;
790 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
791 ASN1_R_ILLEGAL_TAGGED_ANY);
796 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
797 ASN1_R_ILLEGAL_OPTIONAL_ANY);
801 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
802 &p, inlen, -1, 0, 0, ctx);
805 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
806 ERR_R_NESTED_ASN1_ERROR);
809 if (oclass != V_ASN1_UNIVERSAL)
810 utype = V_ASN1_OTHER;
815 aclass = V_ASN1_UNIVERSAL;
819 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
820 &p, inlen, tag, aclass, opt, ctx);
823 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
828 /* SEQUENCE, SET and "OTHER" are left in encoded form */
829 if ((utype == V_ASN1_SEQUENCE)
830 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
832 /* Clear context cache for type OTHER because the auto clear
833 * when we have a exact match wont work
835 if (utype == V_ASN1_OTHER)
839 /* SEQUENCE and SET must be constructed */
842 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
843 ASN1_R_TYPE_NOT_CONSTRUCTED);
848 /* If indefinite length constructed find the real end */
851 if (!asn1_collect(NULL, &p, plen, inf, -1, -1))
857 len = p - cont + plen;
867 /* Should really check the internal tags are correct but
868 * some things may get this wrong. The relevant specs
869 * say that constructed string types should be OCTET STRINGs
870 * internally irrespective of the type. So instead just check
871 * for UNIVERSAL class and ignore the tag.
873 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL))
876 /* Append a final null to string */
877 if (!BUF_MEM_grow_clean(&buf, len + 1))
879 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
880 ERR_R_MALLOC_FAILURE);
884 cont = (const unsigned char *)buf.data;
894 /* We now have content length and type: translate into a structure */
895 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
901 if (free_cont && buf.data) OPENSSL_free(buf.data);
905 /* Translate ASN1 content octets into a structure */
907 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
908 int utype, char *free_cont, const ASN1_ITEM *it)
910 ASN1_VALUE **opval = NULL;
912 ASN1_TYPE *typ = NULL;
914 const ASN1_PRIMITIVE_FUNCS *pf;
918 if (pf && pf->prim_c2i)
919 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
920 /* If ANY type clear type and set pointer to internal value */
921 if (it->utype == V_ASN1_ANY)
925 typ = ASN1_TYPE_new();
926 *pval = (ASN1_VALUE *)typ;
929 typ = (ASN1_TYPE *)*pval;
931 if (utype != typ->type)
932 ASN1_TYPE_set(typ, utype, NULL);
934 pval = (ASN1_VALUE **)&typ->value.ptr;
939 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
946 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
947 ASN1_R_NULL_IS_WRONG_LENGTH);
950 *pval = (ASN1_VALUE *)1;
956 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
957 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
963 tbool = (ASN1_BOOLEAN *)pval;
968 case V_ASN1_BIT_STRING:
969 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
974 case V_ASN1_NEG_INTEGER:
975 case V_ASN1_ENUMERATED:
976 case V_ASN1_NEG_ENUMERATED:
977 tint = (ASN1_INTEGER **)pval;
978 if (!c2i_ASN1_INTEGER(tint, &cont, len))
980 /* Fixup type to match the expected form */
981 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
984 case V_ASN1_OCTET_STRING:
985 case V_ASN1_NUMERICSTRING:
986 case V_ASN1_PRINTABLESTRING:
987 case V_ASN1_T61STRING:
988 case V_ASN1_VIDEOTEXSTRING:
989 case V_ASN1_IA5STRING:
991 case V_ASN1_GENERALIZEDTIME:
992 case V_ASN1_GRAPHICSTRING:
993 case V_ASN1_VISIBLESTRING:
994 case V_ASN1_GENERALSTRING:
995 case V_ASN1_UNIVERSALSTRING:
996 case V_ASN1_BMPSTRING:
997 case V_ASN1_UTF8STRING:
1000 case V_ASN1_SEQUENCE:
1002 /* All based on ASN1_STRING and handled the same */
1005 stmp = ASN1_STRING_type_new(utype);
1008 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
1009 ERR_R_MALLOC_FAILURE);
1012 *pval = (ASN1_VALUE *)stmp;
1016 stmp = (ASN1_STRING *)*pval;
1019 /* If we've already allocated a buffer use it */
1023 OPENSSL_free(stmp->data);
1024 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
1030 if (!ASN1_STRING_set(stmp, cont, len))
1032 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
1033 ERR_R_MALLOC_FAILURE);
1034 ASN1_STRING_free(stmp);
1041 /* If ASN1_ANY and NULL type fix up value */
1042 if (typ && (utype == V_ASN1_NULL))
1043 typ->value.ptr = NULL;
1049 ASN1_TYPE_free(typ);
1056 /* This function collects the asn1 data from a constructred string
1057 * type into a buffer. The values of 'in' and 'len' should refer
1058 * to the contents of the constructed type and 'inf' should be set
1059 * if it is indefinite length. If 'buf' is NULL then we just want
1060 * to find the end of the current structure: useful for indefinite
1061 * length constructed stuff.
1064 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1065 char inf, int tag, int aclass)
1067 const unsigned char *p, *q;
1072 /* If no buffer and not indefinite length constructed just pass over
1073 * the encoded data */
1083 if (asn1_check_eoc(&p, len))
1085 /* EOC is illegal outside indefinite length
1086 * constructed form */
1089 ASN1err(ASN1_F_ASN1_COLLECT,
1090 ASN1_R_UNEXPECTED_EOC);
1097 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1098 len, tag, aclass, 0, NULL))
1100 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1104 /* If indefinite length constructed update max length */
1107 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass))
1110 else if (!collect_data(buf, &p, plen))
1116 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1123 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1129 if (!BUF_MEM_grow_clean(buf, len + plen))
1131 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1134 memcpy(buf->data + len, *p, plen);
1140 /* Check for ASN1 EOC and swallow it if found */
1142 static int asn1_check_eoc(const unsigned char **in, long len)
1144 const unsigned char *p;
1145 if (len < 2) return 0;
1155 /* Check an ASN1 tag and length: a bit like ASN1_get_object
1156 * but it sets the length for indefinite length constructed
1157 * form, we don't know the exact length but we can set an
1158 * upper bound to the amount of data available minus the
1159 * header length just read.
1162 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1163 char *inf, char *cst,
1164 const unsigned char **in, long len,
1165 int exptag, int expclass, char opt,
1171 const unsigned char *p, *q;
1175 if (ctx && ctx->valid)
1179 pclass = ctx->pclass;
1185 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1190 ctx->pclass = pclass;
1192 ctx->hdrlen = p - q;
1194 /* If definite length, and no error, length +
1195 * header can't exceed total amount of data available.
1197 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
1199 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1201 asn1_tlc_clear(ctx);
1209 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1210 asn1_tlc_clear(ctx);
1215 if ((exptag != ptag) || (expclass != pclass))
1217 /* If type is OPTIONAL, not an error:
1218 * indicate missing type.
1221 asn1_tlc_clear(ctx);
1222 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1225 /* We have a tag and class match:
1226 * assume we are going to do something with it */
1227 asn1_tlc_clear(ctx);
1231 plen = len - (p - q);
1237 *cst = i & V_ASN1_CONSTRUCTED;