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
);
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
;
134 if (ASN1_item_ex_d2i(pval
, in
, len
, it
, -1, 0, 0, &c
) > 0)
139 int ASN1_template_d2i(ASN1_VALUE
**pval
,
140 const unsigned char **in
, long len
, const ASN1_TEMPLATE
*tt
)
144 return asn1_template_ex_d2i(pval
, in
, len
, tt
, 0, &c
);
148 /* Decode an item, taking care of IMPLICIT tagging, if any.
149 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
152 int ASN1_item_ex_d2i(ASN1_VALUE
**pval
, const unsigned char **in
, long len
,
154 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
156 const ASN1_TEMPLATE
*tt
, *errtt
= NULL
;
157 const ASN1_COMPAT_FUNCS
*cf
;
158 const ASN1_EXTERN_FUNCS
*ef
;
159 const ASN1_AUX
*aux
= it
->funcs
;
160 ASN1_aux_cb
*asn1_cb
;
161 const unsigned char *p
= NULL
, *q
;
162 unsigned char *wp
=NULL
; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
163 unsigned char imphack
= 0, oclass
;
164 char seq_eoc
, seq_nolen
, cst
, isopt
;
169 ASN1_VALUE
*pchval
, **pchptr
, *ptmpval
;
172 if (aux
&& aux
->asn1_cb
)
173 asn1_cb
= aux
->asn1_cb
;
178 case ASN1_ITYPE_PRIMITIVE
:
181 /* tagging or OPTIONAL is currently illegal on an item
182 * template because the flags can't get passed down.
183 * In practice this isn't a problem: we include the
184 * relevant flags from the item template in the
187 if ((tag
!= -1) || opt
)
189 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
190 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE
);
193 return asn1_template_ex_d2i(pval
, in
, len
,
194 it
->templates
, opt
, ctx
);
196 return asn1_d2i_ex_primitive(pval
, in
, len
, it
,
197 tag
, aclass
, opt
, ctx
);
200 case ASN1_ITYPE_MSTRING
:
202 /* Just read in tag and class */
203 ret
= asn1_check_tlen(NULL
, &otag
, &oclass
, NULL
, NULL
,
204 &p
, len
, -1, 0, 1, ctx
);
207 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
208 ERR_R_NESTED_ASN1_ERROR
);
212 /* Must be UNIVERSAL class */
213 if (oclass
!= V_ASN1_UNIVERSAL
)
215 /* If OPTIONAL, assume this is OK */
217 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
218 ASN1_R_MSTRING_NOT_UNIVERSAL
);
221 /* Check tag matches bit map */
222 if (!(ASN1_tag2bit(otag
) & it
->utype
))
224 /* If OPTIONAL, assume this is OK */
227 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
228 ASN1_R_MSTRING_WRONG_TAG
);
231 return asn1_d2i_ex_primitive(pval
, in
, len
,
232 it
, otag
, 0, 0, ctx
);
234 case ASN1_ITYPE_EXTERN
:
235 /* Use new style d2i */
237 return ef
->asn1_ex_d2i(pval
, in
, len
,
238 it
, tag
, aclass
, opt
, ctx
);
240 case ASN1_ITYPE_COMPAT
:
241 /* we must resort to old style evil hackery */
244 /* If OPTIONAL see if it is there */
252 /* Don't care about anything other than presence
255 ret
= asn1_check_tlen(NULL
, NULL
, NULL
, NULL
, NULL
,
256 &p
, len
, exptag
, aclass
, 1, ctx
);
259 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
260 ERR_R_NESTED_ASN1_ERROR
);
267 /* This is the old style evil hack IMPLICIT handling:
268 * since the underlying code is expecting a tag and
269 * class other than the one present we change the
270 * buffer temporarily then change it back afterwards.
271 * This doesn't and never did work for tags > 30.
273 * Yes this is *horrible* but it is only needed for
274 * old style d2i which will hopefully not be around
276 * FIXME: should copy the buffer then modify it so
277 * the input buffer can be const: we should *always*
278 * copy because the old style d2i might modify the
284 wp
= *(unsigned char **)in
;
288 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
289 ERR_R_NESTED_ASN1_ERROR
);
292 *wp
= (unsigned char)((*p
& V_ASN1_CONSTRUCTED
)
296 ptmpval
= cf
->asn1_d2i(pval
, in
, len
);
304 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
308 case ASN1_ITYPE_CHOICE
:
309 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
))
312 /* Allocate structure */
313 if (!*pval
&& !ASN1_item_ex_new(pval
, it
))
315 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
316 ERR_R_NESTED_ASN1_ERROR
);
319 /* CHOICE type, try each possibility in turn */
322 for (i
= 0, tt
=it
->templates
; i
< it
->tcount
; i
++, tt
++)
324 pchptr
= asn1_get_field_ptr(pval
, tt
);
325 /* We mark field as OPTIONAL so its absence
328 ret
= asn1_template_ex_d2i(pchptr
, &p
, len
, tt
, 1, ctx
);
329 /* If field not present, try the next one */
332 /* If positive return, read OK, break loop */
335 /* Otherwise must be an ASN1 parsing error */
337 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
338 ERR_R_NESTED_ASN1_ERROR
);
342 /* Did we fall off the end without reading anything? */
345 /* If OPTIONAL, this is OK */
348 /* Free and zero it */
349 ASN1_item_ex_free(pval
, it
);
352 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
353 ASN1_R_NO_MATCHING_CHOICE_TYPE
);
357 asn1_set_choice_selector(pval
, i
, it
);
359 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
))
363 case ASN1_ITYPE_NDEF_SEQUENCE
:
364 case ASN1_ITYPE_SEQUENCE
:
368 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
371 tag
= V_ASN1_SEQUENCE
;
372 aclass
= V_ASN1_UNIVERSAL
;
374 /* Get SEQUENCE length and update len, p */
375 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &seq_eoc
, &cst
,
376 &p
, len
, tag
, aclass
, opt
, ctx
);
379 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
380 ERR_R_NESTED_ASN1_ERROR
);
385 if (aux
&& (aux
->flags
& ASN1_AFLG_BROKEN
))
387 len
= tmplen
- (p
- *in
);
390 /* If indefinite we don't do a length check */
391 else seq_nolen
= seq_eoc
;
394 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
395 ASN1_R_SEQUENCE_NOT_CONSTRUCTED
);
399 if (!*pval
&& !ASN1_item_ex_new(pval
, it
))
401 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
402 ERR_R_NESTED_ASN1_ERROR
);
406 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
))
409 /* Get each field entry */
410 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++)
412 const ASN1_TEMPLATE
*seqtt
;
413 ASN1_VALUE
**pseqval
;
414 seqtt
= asn1_do_adb(pval
, tt
, 1);
417 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
418 /* Have we ran out of data? */
422 if (asn1_check_eoc(&p
, len
))
426 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
427 ASN1_R_UNEXPECTED_EOC
);
435 /* This determines the OPTIONAL flag value. The field
436 * cannot be omitted if it is the last of a SEQUENCE
437 * and there is still data to be read. This isn't
438 * strictly necessary but it increases efficiency in
441 if (i
== (it
->tcount
- 1))
443 else isopt
= (char)(seqtt
->flags
& ASN1_TFLG_OPTIONAL
);
444 /* attempt to read in field, allowing each to be
447 ret
= asn1_template_ex_d2i(pseqval
, &p
, len
,
456 /* OPTIONAL component absent.
457 * Free and zero the field.
459 ASN1_template_free(pseqval
, seqtt
);
466 /* Check for EOC if expecting one */
467 if (seq_eoc
&& !asn1_check_eoc(&p
, len
))
469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_MISSING_EOC
);
472 /* Check all data read */
473 if (!seq_nolen
&& len
)
475 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
476 ASN1_R_SEQUENCE_LENGTH_MISMATCH
);
480 /* If we get here we've got no more data in the SEQUENCE,
481 * however we may not have read all fields so check all
482 * remaining are OPTIONAL and clear any that are.
484 for (; i
< it
->tcount
; tt
++, i
++)
486 const ASN1_TEMPLATE
*seqtt
;
487 seqtt
= asn1_do_adb(pval
, tt
, 1);
490 if (seqtt
->flags
& ASN1_TFLG_OPTIONAL
)
492 ASN1_VALUE
**pseqval
;
493 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
494 ASN1_template_free(pseqval
, seqtt
);
499 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
500 ASN1_R_FIELD_MISSING
);
505 if (!asn1_enc_save(pval
, *in
, p
- *in
, it
))
508 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
))
516 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_AUX_ERROR
);
518 ASN1_item_ex_free(pval
, it
);
520 ERR_add_error_data(4, "Field=", errtt
->field_name
,
521 ", Type=", it
->sname
);
523 ERR_add_error_data(2, "Type=", it
->sname
);
527 /* Templates are handled with two separate functions.
528 * One handles any EXPLICIT tag and the other handles the rest.
531 static int asn1_template_ex_d2i(ASN1_VALUE
**val
,
532 const unsigned char **in
, long inlen
,
533 const ASN1_TEMPLATE
*tt
, char opt
,
539 const unsigned char *p
, *q
;
544 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
548 /* Check if EXPLICIT tag expected */
549 if (flags
& ASN1_TFLG_EXPTAG
)
552 /* Need to work out amount of data available to the inner
553 * content and where it starts: so read in EXPLICIT header to
556 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &exp_eoc
, &cst
,
557 &p
, inlen
, tt
->tag
, aclass
, opt
, ctx
);
561 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
562 ERR_R_NESTED_ASN1_ERROR
);
569 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
570 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED
);
573 /* We've found the field so it can't be OPTIONAL now */
574 ret
= asn1_template_noexp_d2i(val
, &p
, len
, tt
, 0, ctx
);
577 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
578 ERR_R_NESTED_ASN1_ERROR
);
581 /* We read the field in OK so update length */
585 /* If NDEF we must have an EOC here */
586 if (!asn1_check_eoc(&p
, len
))
588 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
595 /* Otherwise we must hit the EXPLICIT tag end or its
599 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
600 ASN1_R_EXPLICIT_LENGTH_MISMATCH
);
606 return asn1_template_noexp_d2i(val
, in
, inlen
,
613 ASN1_template_free(val
, tt
);
618 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
619 const unsigned char **in
, long len
,
620 const ASN1_TEMPLATE
*tt
, char opt
,
625 const unsigned char *p
, *q
;
629 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
634 if (flags
& ASN1_TFLG_SK_MASK
)
636 /* SET OF, SEQUENCE OF */
639 /* First work out expected inner tag value */
640 if (flags
& ASN1_TFLG_IMPTAG
)
647 skaclass
= V_ASN1_UNIVERSAL
;
648 if (flags
& ASN1_TFLG_SET_OF
)
651 sktag
= V_ASN1_SEQUENCE
;
654 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &sk_eoc
, NULL
,
655 &p
, len
, sktag
, skaclass
, opt
, ctx
);
658 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
659 ERR_R_NESTED_ASN1_ERROR
);
665 *val
= (ASN1_VALUE
*)sk_new_null();
668 /* We've got a valid STACK: free up any items present */
669 STACK
*sktmp
= (STACK
*)*val
;
671 while(sk_num(sktmp
) > 0)
673 vtmp
= (ASN1_VALUE
*)sk_pop(sktmp
);
674 ASN1_item_ex_free(&vtmp
,
675 ASN1_ITEM_ptr(tt
->item
));
681 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
682 ERR_R_MALLOC_FAILURE
);
686 /* Read as many items as we can */
691 /* See if EOC found */
692 if (asn1_check_eoc(&p
, len
))
696 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
697 ASN1_R_UNEXPECTED_EOC
);
705 if (!ASN1_item_ex_d2i(&skfield
, &p
, len
,
706 ASN1_ITEM_ptr(tt
->item
),
709 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
710 ERR_R_NESTED_ASN1_ERROR
);
714 if (!sk_push((STACK
*)*val
, (char *)skfield
))
716 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
717 ERR_R_MALLOC_FAILURE
);
723 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ASN1_R_MISSING_EOC
);
727 else if (flags
& ASN1_TFLG_IMPTAG
)
729 /* IMPLICIT tagging */
730 ret
= ASN1_item_ex_d2i(val
, &p
, len
,
731 ASN1_ITEM_ptr(tt
->item
), tt
->tag
, aclass
, opt
, ctx
);
734 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
735 ERR_R_NESTED_ASN1_ERROR
);
743 /* Nothing special */
744 ret
= ASN1_item_ex_d2i(val
, &p
, len
, ASN1_ITEM_ptr(tt
->item
),
748 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
749 ERR_R_NESTED_ASN1_ERROR
);
760 ASN1_template_free(val
, tt
);
765 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
766 const unsigned char **in
, long inlen
,
768 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
772 char cst
, inf
, free_cont
= 0;
773 const unsigned char *p
;
775 const unsigned char *cont
= NULL
;
779 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_ILLEGAL_NULL
);
780 return 0; /* Should never happen */
783 if (it
->itype
== ASN1_ITYPE_MSTRING
)
791 if (utype
== V_ASN1_ANY
)
793 /* If type is ANY need to figure out type from tag */
794 unsigned char oclass
;
797 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
798 ASN1_R_ILLEGAL_TAGGED_ANY
);
803 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
804 ASN1_R_ILLEGAL_OPTIONAL_ANY
);
808 ret
= asn1_check_tlen(NULL
, &utype
, &oclass
, NULL
, NULL
,
809 &p
, inlen
, -1, 0, 0, ctx
);
812 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
813 ERR_R_NESTED_ASN1_ERROR
);
816 if (oclass
!= V_ASN1_UNIVERSAL
)
817 utype
= V_ASN1_OTHER
;
822 aclass
= V_ASN1_UNIVERSAL
;
826 ret
= asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, &cst
,
827 &p
, inlen
, tag
, aclass
, opt
, ctx
);
830 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_NESTED_ASN1_ERROR
);
836 /* SEQUENCE, SET and "OTHER" are left in encoded form */
837 if ((utype
== V_ASN1_SEQUENCE
)
838 || (utype
== V_ASN1_SET
) || (utype
== V_ASN1_OTHER
))
840 /* Clear context cache for type OTHER because the auto clear
841 * when we have a exact match wont work
843 if (utype
== V_ASN1_OTHER
)
847 /* SEQUENCE and SET must be constructed */
850 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
851 ASN1_R_TYPE_NOT_CONSTRUCTED
);
856 /* If indefinite length constructed find the real end */
859 if (!asn1_find_end(&p
, plen
, inf
))
865 len
= p
- cont
+ plen
;
875 /* Should really check the internal tags are correct but
876 * some things may get this wrong. The relevant specs
877 * say that constructed string types should be OCTET STRINGs
878 * internally irrespective of the type. So instead just check
879 * for UNIVERSAL class and ignore the tag.
881 if (!asn1_collect(&buf
, &p
, plen
, inf
, -1, V_ASN1_UNIVERSAL
))
887 /* Append a final null to string */
888 if (!BUF_MEM_grow_clean(&buf
, len
+ 1))
890 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
891 ERR_R_MALLOC_FAILURE
);
895 cont
= (const unsigned char *)buf
.data
;
905 /* We now have content length and type: translate into a structure */
906 if (!asn1_ex_c2i(pval
, cont
, len
, utype
, &free_cont
, it
))
912 if (free_cont
&& buf
.data
) OPENSSL_free(buf
.data
);
916 /* Translate ASN1 content octets into a structure */
918 int asn1_ex_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
919 int utype
, char *free_cont
, const ASN1_ITEM
*it
)
921 ASN1_VALUE
**opval
= NULL
;
923 ASN1_TYPE
*typ
= NULL
;
925 const ASN1_PRIMITIVE_FUNCS
*pf
;
929 if (pf
&& pf
->prim_c2i
)
930 return pf
->prim_c2i(pval
, cont
, len
, utype
, free_cont
, it
);
931 /* If ANY type clear type and set pointer to internal value */
932 if (it
->utype
== V_ASN1_ANY
)
936 typ
= ASN1_TYPE_new();
939 *pval
= (ASN1_VALUE
*)typ
;
942 typ
= (ASN1_TYPE
*)*pval
;
944 if (utype
!= typ
->type
)
945 ASN1_TYPE_set(typ
, utype
, NULL
);
947 pval
= &typ
->value
.asn1_value
;
952 if (!c2i_ASN1_OBJECT((ASN1_OBJECT
**)pval
, &cont
, len
))
959 ASN1err(ASN1_F_ASN1_EX_C2I
,
960 ASN1_R_NULL_IS_WRONG_LENGTH
);
963 *pval
= (ASN1_VALUE
*)1;
969 ASN1err(ASN1_F_ASN1_EX_C2I
,
970 ASN1_R_BOOLEAN_IS_WRONG_LENGTH
);
976 tbool
= (ASN1_BOOLEAN
*)pval
;
981 case V_ASN1_BIT_STRING
:
982 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING
**)pval
, &cont
, len
))
987 case V_ASN1_NEG_INTEGER
:
988 case V_ASN1_ENUMERATED
:
989 case V_ASN1_NEG_ENUMERATED
:
990 tint
= (ASN1_INTEGER
**)pval
;
991 if (!c2i_ASN1_INTEGER(tint
, &cont
, len
))
993 /* Fixup type to match the expected form */
994 (*tint
)->type
= utype
| ((*tint
)->type
& V_ASN1_NEG
);
997 case V_ASN1_OCTET_STRING
:
998 case V_ASN1_NUMERICSTRING
:
999 case V_ASN1_PRINTABLESTRING
:
1000 case V_ASN1_T61STRING
:
1001 case V_ASN1_VIDEOTEXSTRING
:
1002 case V_ASN1_IA5STRING
:
1003 case V_ASN1_UTCTIME
:
1004 case V_ASN1_GENERALIZEDTIME
:
1005 case V_ASN1_GRAPHICSTRING
:
1006 case V_ASN1_VISIBLESTRING
:
1007 case V_ASN1_GENERALSTRING
:
1008 case V_ASN1_UNIVERSALSTRING
:
1009 case V_ASN1_BMPSTRING
:
1010 case V_ASN1_UTF8STRING
:
1013 case V_ASN1_SEQUENCE
:
1015 /* All based on ASN1_STRING and handled the same */
1018 stmp
= ASN1_STRING_type_new(utype
);
1021 ASN1err(ASN1_F_ASN1_EX_C2I
,
1022 ERR_R_MALLOC_FAILURE
);
1025 *pval
= (ASN1_VALUE
*)stmp
;
1029 stmp
= (ASN1_STRING
*)*pval
;
1032 /* If we've already allocated a buffer use it */
1036 OPENSSL_free(stmp
->data
);
1037 stmp
->data
= (unsigned char *)cont
; /* UGLY CAST! RL */
1043 if (!ASN1_STRING_set(stmp
, cont
, len
))
1045 ASN1err(ASN1_F_ASN1_EX_C2I
,
1046 ERR_R_MALLOC_FAILURE
);
1047 ASN1_STRING_free(stmp
);
1054 /* If ASN1_ANY and NULL type fix up value */
1055 if (typ
&& (utype
== V_ASN1_NULL
))
1056 typ
->value
.ptr
= NULL
;
1062 ASN1_TYPE_free(typ
);
1070 /* This function finds the end of an ASN1 structure when passed its maximum
1071 * length, whether it is indefinite length and a pointer to the content.
1072 * This is more efficient than calling asn1_collect because it does not
1073 * recurse on each indefinite length header.
1076 static int asn1_find_end(const unsigned char **in
, long len
, char inf
)
1080 const unsigned char *p
= *in
, *q
;
1081 /* If not indefinite length constructed just add length */
1088 /* Indefinite length constructed form. Find the end when enough EOCs
1089 * are found. If more indefinite length constructed headers
1090 * are encountered increment the expected eoc count otherwise just
1091 * skip to the end of the data.
1095 if(asn1_check_eoc(&p
, len
))
1098 if (expected_eoc
== 0)
1104 /* Just read in a header: only care about the length */
1105 if(!asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, NULL
, &p
, len
,
1108 ASN1err(ASN1_F_ASN1_FIND_END
, ERR_R_NESTED_ASN1_ERROR
);
1119 ASN1err(ASN1_F_ASN1_FIND_END
, ASN1_R_MISSING_EOC
);
1125 /* This function collects the asn1 data from a constructred string
1126 * type into a buffer. The values of 'in' and 'len' should refer
1127 * to the contents of the constructed type and 'inf' should be set
1128 * if it is indefinite length.
1131 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
1132 char inf
, int tag
, int aclass
)
1134 const unsigned char *p
, *q
;
1139 /* If no buffer and not indefinite length constructed just pass over
1140 * the encoded data */
1150 if (asn1_check_eoc(&p
, len
))
1152 /* EOC is illegal outside indefinite length
1153 * constructed form */
1156 ASN1err(ASN1_F_ASN1_COLLECT
,
1157 ASN1_R_UNEXPECTED_EOC
);
1164 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &ininf
, &cst
, &p
,
1165 len
, tag
, aclass
, 0, NULL
))
1167 ASN1err(ASN1_F_ASN1_COLLECT
, ERR_R_NESTED_ASN1_ERROR
);
1171 /* If indefinite length constructed update max length */
1174 #ifdef OPENSSL_ALLOW_NESTED_ASN1_STRINGS
1175 if (!asn1_collect(buf
, &p
, plen
, ininf
, tag
, aclass
))
1178 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_NESTED_ASN1_STRING
);
1182 else if (plen
&& !collect_data(buf
, &p
, plen
))
1188 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_MISSING_EOC
);
1195 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
)
1201 if (!BUF_MEM_grow_clean(buf
, len
+ plen
))
1203 ASN1err(ASN1_F_COLLECT_DATA
, ERR_R_MALLOC_FAILURE
);
1206 memcpy(buf
->data
+ len
, *p
, plen
);
1212 /* Check for ASN1 EOC and swallow it if found */
1214 static int asn1_check_eoc(const unsigned char **in
, long len
)
1216 const unsigned char *p
;
1217 if (len
< 2) return 0;
1227 /* Check an ASN1 tag and length: a bit like ASN1_get_object
1228 * but it sets the length for indefinite length constructed
1229 * form, we don't know the exact length but we can set an
1230 * upper bound to the amount of data available minus the
1231 * header length just read.
1234 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
1235 char *inf
, char *cst
,
1236 const unsigned char **in
, long len
,
1237 int exptag
, int expclass
, char opt
,
1243 const unsigned char *p
, *q
;
1247 if (ctx
&& ctx
->valid
)
1251 pclass
= ctx
->pclass
;
1257 i
= ASN1_get_object(&p
, &plen
, &ptag
, &pclass
, len
);
1262 ctx
->pclass
= pclass
;
1264 ctx
->hdrlen
= p
- q
;
1266 /* If definite length, and no error, length +
1267 * header can't exceed total amount of data available.
1269 if (!(i
& 0x81) && ((plen
+ ctx
->hdrlen
) > len
))
1271 ASN1err(ASN1_F_ASN1_CHECK_TLEN
,
1273 asn1_tlc_clear(ctx
);
1281 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_BAD_OBJECT_HEADER
);
1282 asn1_tlc_clear(ctx
);
1287 if ((exptag
!= ptag
) || (expclass
!= pclass
))
1289 /* If type is OPTIONAL, not an error:
1290 * indicate missing type.
1293 asn1_tlc_clear(ctx
);
1294 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_WRONG_TAG
);
1297 /* We have a tag and class match:
1298 * assume we are going to do something with it */
1299 asn1_tlc_clear(ctx
);
1303 plen
= len
- (p
- q
);
1309 *cst
= i
& V_ASN1_CONSTRUCTED
;