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
, long len
, const ASN1_ITEM
*it
)
132 ASN1_VALUE
*ptmpval
= NULL
;
135 asn1_tlc_clear_nc(&c
);
136 if (ASN1_item_ex_d2i(pval
, in
, len
, it
, -1, 0, 0, &c
) > 0)
141 int ASN1_template_d2i(ASN1_VALUE
**pval
,
142 const unsigned char **in
, long len
, const ASN1_TEMPLATE
*tt
)
145 asn1_tlc_clear_nc(&c
);
146 return asn1_template_ex_d2i(pval
, in
, len
, tt
, 0, &c
);
150 /* Decode an item, taking care of IMPLICIT tagging, if any.
151 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
154 int ASN1_item_ex_d2i(ASN1_VALUE
**pval
, const unsigned char **in
, long len
,
156 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
158 const ASN1_TEMPLATE
*tt
, *errtt
= NULL
;
159 const ASN1_COMPAT_FUNCS
*cf
;
160 const ASN1_EXTERN_FUNCS
*ef
;
161 const ASN1_AUX
*aux
= it
->funcs
;
162 ASN1_aux_cb
*asn1_cb
;
163 const unsigned char *p
= NULL
, *q
;
164 unsigned char *wp
=NULL
; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
165 unsigned char imphack
= 0, oclass
;
166 char seq_eoc
, seq_nolen
, cst
, isopt
;
171 ASN1_VALUE
*pchval
, **pchptr
, *ptmpval
;
174 if (aux
&& aux
->asn1_cb
)
175 asn1_cb
= aux
->asn1_cb
;
180 case ASN1_ITYPE_PRIMITIVE
:
183 /* tagging or OPTIONAL is currently illegal on an item
184 * template because the flags can't get passed down.
185 * In practice this isn't a problem: we include the
186 * relevant flags from the item template in the
189 if ((tag
!= -1) || opt
)
191 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
192 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE
);
195 return asn1_template_ex_d2i(pval
, in
, len
,
196 it
->templates
, opt
, ctx
);
198 return asn1_d2i_ex_primitive(pval
, in
, len
, it
,
199 tag
, aclass
, opt
, ctx
);
202 case ASN1_ITYPE_MSTRING
:
204 /* Just read in tag and class */
205 ret
= asn1_check_tlen(NULL
, &otag
, &oclass
, NULL
, NULL
,
206 &p
, len
, -1, 0, 1, ctx
);
209 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
210 ERR_R_NESTED_ASN1_ERROR
);
214 /* Must be UNIVERSAL class */
215 if (oclass
!= V_ASN1_UNIVERSAL
)
217 /* If OPTIONAL, assume this is OK */
219 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
220 ASN1_R_MSTRING_NOT_UNIVERSAL
);
223 /* Check tag matches bit map */
224 if (!(ASN1_tag2bit(otag
) & it
->utype
))
226 /* If OPTIONAL, assume this is OK */
229 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
230 ASN1_R_MSTRING_WRONG_TAG
);
233 return asn1_d2i_ex_primitive(pval
, in
, len
,
234 it
, otag
, 0, 0, ctx
);
236 case ASN1_ITYPE_EXTERN
:
237 /* Use new style d2i */
239 return ef
->asn1_ex_d2i(pval
, in
, len
,
240 it
, tag
, aclass
, opt
, ctx
);
242 case ASN1_ITYPE_COMPAT
:
243 /* we must resort to old style evil hackery */
246 /* If OPTIONAL see if it is there */
254 /* Don't care about anything other than presence
257 ret
= asn1_check_tlen(NULL
, NULL
, NULL
, NULL
, NULL
,
258 &p
, len
, exptag
, aclass
, 1, ctx
);
261 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
262 ERR_R_NESTED_ASN1_ERROR
);
269 /* This is the old style evil hack IMPLICIT handling:
270 * since the underlying code is expecting a tag and
271 * class other than the one present we change the
272 * buffer temporarily then change it back afterwards.
273 * This doesn't and never did work for tags > 30.
275 * Yes this is *horrible* but it is only needed for
276 * old style d2i which will hopefully not be around
278 * FIXME: should copy the buffer then modify it so
279 * the input buffer can be const: we should *always*
280 * copy because the old style d2i might modify the
286 wp
= *(unsigned char **)in
;
290 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
291 ERR_R_NESTED_ASN1_ERROR
);
294 *wp
= (unsigned char)((*p
& V_ASN1_CONSTRUCTED
)
298 ptmpval
= cf
->asn1_d2i(pval
, in
, len
);
306 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
310 case ASN1_ITYPE_CHOICE
:
311 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
314 /* Allocate structure */
315 if (!*pval
&& !ASN1_item_ex_new(pval
, it
))
317 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
318 ERR_R_NESTED_ASN1_ERROR
);
321 /* CHOICE type, try each possibility in turn */
324 for (i
= 0, tt
=it
->templates
; i
< it
->tcount
; i
++, tt
++)
326 pchptr
= asn1_get_field_ptr(pval
, tt
);
327 /* We mark field as OPTIONAL so its absence
330 ret
= asn1_template_ex_d2i(pchptr
, &p
, len
, tt
, 1, ctx
);
331 /* If field not present, try the next one */
334 /* If positive return, read OK, break loop */
337 /* Otherwise must be an ASN1 parsing error */
339 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
340 ERR_R_NESTED_ASN1_ERROR
);
344 /* Did we fall off the end without reading anything? */
347 /* If OPTIONAL, this is OK */
350 /* Free and zero it */
351 ASN1_item_ex_free(pval
, it
);
354 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
355 ASN1_R_NO_MATCHING_CHOICE_TYPE
);
359 asn1_set_choice_selector(pval
, i
, it
);
361 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
365 case ASN1_ITYPE_NDEF_SEQUENCE
:
366 case ASN1_ITYPE_SEQUENCE
:
370 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
373 tag
= V_ASN1_SEQUENCE
;
374 aclass
= V_ASN1_UNIVERSAL
;
376 /* Get SEQUENCE length and update len, p */
377 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &seq_eoc
, &cst
,
378 &p
, len
, tag
, aclass
, opt
, ctx
);
381 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
382 ERR_R_NESTED_ASN1_ERROR
);
387 if (aux
&& (aux
->flags
& ASN1_AFLG_BROKEN
))
389 len
= tmplen
- (p
- *in
);
392 /* If indefinite we don't do a length check */
393 else seq_nolen
= seq_eoc
;
396 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
397 ASN1_R_SEQUENCE_NOT_CONSTRUCTED
);
401 if (!*pval
&& !ASN1_item_ex_new(pval
, it
))
403 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
404 ERR_R_NESTED_ASN1_ERROR
);
408 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
411 /* Get each field entry */
412 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++)
414 const ASN1_TEMPLATE
*seqtt
;
415 ASN1_VALUE
**pseqval
;
416 seqtt
= asn1_do_adb(pval
, tt
, 1);
419 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
420 /* Have we ran out of data? */
424 if (asn1_check_eoc(&p
, len
))
428 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
429 ASN1_R_UNEXPECTED_EOC
);
437 /* This determines the OPTIONAL flag value. The field
438 * cannot be omitted if it is the last of a SEQUENCE
439 * and there is still data to be read. This isn't
440 * strictly necessary but it increases efficiency in
443 if (i
== (it
->tcount
- 1))
445 else isopt
= (char)(seqtt
->flags
& ASN1_TFLG_OPTIONAL
);
446 /* attempt to read in field, allowing each to be
449 ret
= asn1_template_ex_d2i(pseqval
, &p
, len
,
458 /* OPTIONAL component absent.
459 * Free and zero the field.
461 ASN1_template_free(pseqval
, seqtt
);
468 /* Check for EOC if expecting one */
469 if (seq_eoc
&& !asn1_check_eoc(&p
, len
))
471 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_MISSING_EOC
);
474 /* Check all data read */
475 if (!seq_nolen
&& len
)
477 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
478 ASN1_R_SEQUENCE_LENGTH_MISMATCH
);
482 /* If we get here we've got no more data in the SEQUENCE,
483 * however we may not have read all fields so check all
484 * remaining are OPTIONAL and clear any that are.
486 for (; i
< it
->tcount
; tt
++, i
++)
488 const ASN1_TEMPLATE
*seqtt
;
489 seqtt
= asn1_do_adb(pval
, tt
, 1);
492 if (seqtt
->flags
& ASN1_TFLG_OPTIONAL
)
494 ASN1_VALUE
**pseqval
;
495 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
496 ASN1_template_free(pseqval
, seqtt
);
501 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
502 ASN1_R_FIELD_MISSING
);
507 if (!asn1_enc_save(pval
, *in
, p
- *in
, it
))
510 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
518 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_AUX_ERROR
);
520 ASN1_item_ex_free(pval
, it
);
522 ERR_add_error_data(4, "Field=", errtt
->field_name
,
523 ", Type=", it
->sname
);
525 ERR_add_error_data(2, "Type=", it
->sname
);
529 /* Templates are handled with two separate functions.
530 * One handles any EXPLICIT tag and the other handles the rest.
533 static int asn1_template_ex_d2i(ASN1_VALUE
**val
,
534 const unsigned char **in
, long inlen
,
535 const ASN1_TEMPLATE
*tt
, char opt
,
541 const unsigned char *p
, *q
;
546 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
550 /* Check if EXPLICIT tag expected */
551 if (flags
& ASN1_TFLG_EXPTAG
)
554 /* Need to work out amount of data available to the inner
555 * content and where it starts: so read in EXPLICIT header to
558 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &exp_eoc
, &cst
,
559 &p
, inlen
, tt
->tag
, aclass
, opt
, ctx
);
563 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
564 ERR_R_NESTED_ASN1_ERROR
);
571 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
572 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED
);
575 /* We've found the field so it can't be OPTIONAL now */
576 ret
= asn1_template_noexp_d2i(val
, &p
, len
, tt
, 0, ctx
);
579 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
580 ERR_R_NESTED_ASN1_ERROR
);
583 /* We read the field in OK so update length */
587 /* If NDEF we must have an EOC here */
588 if (!asn1_check_eoc(&p
, len
))
590 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
597 /* Otherwise we must hit the EXPLICIT tag end or its
601 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
602 ASN1_R_EXPLICIT_LENGTH_MISMATCH
);
608 return asn1_template_noexp_d2i(val
, in
, inlen
,
615 ASN1_template_free(val
, tt
);
620 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
621 const unsigned char **in
, long len
,
622 const ASN1_TEMPLATE
*tt
, char opt
,
627 const unsigned char *p
, *q
;
631 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
636 if (flags
& ASN1_TFLG_SK_MASK
)
638 /* SET OF, SEQUENCE OF */
641 /* First work out expected inner tag value */
642 if (flags
& ASN1_TFLG_IMPTAG
)
649 skaclass
= V_ASN1_UNIVERSAL
;
650 if (flags
& ASN1_TFLG_SET_OF
)
653 sktag
= V_ASN1_SEQUENCE
;
656 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &sk_eoc
, NULL
,
657 &p
, len
, sktag
, skaclass
, opt
, ctx
);
660 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
661 ERR_R_NESTED_ASN1_ERROR
);
667 *val
= (ASN1_VALUE
*)sk_new_null();
670 /* We've got a valid STACK: free up any items present */
671 STACK_OF(ASN1_VALUE
) *sktmp
672 = (STACK_OF(ASN1_VALUE
) *)*val
;
674 while(sk_ASN1_VALUE_num(sktmp
) > 0)
676 vtmp
= sk_ASN1_VALUE_pop(sktmp
);
677 ASN1_item_ex_free(&vtmp
,
678 ASN1_ITEM_ptr(tt
->item
));
684 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
685 ERR_R_MALLOC_FAILURE
);
689 /* Read as many items as we can */
694 /* See if EOC found */
695 if (asn1_check_eoc(&p
, len
))
699 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
700 ASN1_R_UNEXPECTED_EOC
);
708 if (!ASN1_item_ex_d2i(&skfield
, &p
, len
,
709 ASN1_ITEM_ptr(tt
->item
),
712 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
713 ERR_R_NESTED_ASN1_ERROR
);
717 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE
) *)*val
,
720 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
721 ERR_R_MALLOC_FAILURE
);
727 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ASN1_R_MISSING_EOC
);
731 else if (flags
& ASN1_TFLG_IMPTAG
)
733 /* IMPLICIT tagging */
734 ret
= ASN1_item_ex_d2i(val
, &p
, len
,
735 ASN1_ITEM_ptr(tt
->item
), tt
->tag
, aclass
, opt
, ctx
);
738 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
739 ERR_R_NESTED_ASN1_ERROR
);
747 /* Nothing special */
748 ret
= ASN1_item_ex_d2i(val
, &p
, len
, ASN1_ITEM_ptr(tt
->item
),
752 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
753 ERR_R_NESTED_ASN1_ERROR
);
764 ASN1_template_free(val
, tt
);
769 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
770 const unsigned char **in
, long inlen
,
772 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
776 char cst
, inf
, free_cont
= 0;
777 const unsigned char *p
;
779 const unsigned char *cont
= NULL
;
783 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_ILLEGAL_NULL
);
784 return 0; /* Should never happen */
787 if (it
->itype
== ASN1_ITYPE_MSTRING
)
795 if (utype
== V_ASN1_ANY
)
797 /* If type is ANY need to figure out type from tag */
798 unsigned char oclass
;
801 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
802 ASN1_R_ILLEGAL_TAGGED_ANY
);
807 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
808 ASN1_R_ILLEGAL_OPTIONAL_ANY
);
812 ret
= asn1_check_tlen(NULL
, &utype
, &oclass
, NULL
, NULL
,
813 &p
, inlen
, -1, 0, 0, ctx
);
816 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
817 ERR_R_NESTED_ASN1_ERROR
);
820 if (oclass
!= V_ASN1_UNIVERSAL
)
821 utype
= V_ASN1_OTHER
;
826 aclass
= V_ASN1_UNIVERSAL
;
830 ret
= asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, &cst
,
831 &p
, inlen
, tag
, aclass
, opt
, ctx
);
834 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_NESTED_ASN1_ERROR
);
840 /* SEQUENCE, SET and "OTHER" are left in encoded form */
841 if ((utype
== V_ASN1_SEQUENCE
)
842 || (utype
== V_ASN1_SET
) || (utype
== V_ASN1_OTHER
))
844 /* Clear context cache for type OTHER because the auto clear
845 * when we have a exact match wont work
847 if (utype
== V_ASN1_OTHER
)
851 /* SEQUENCE and SET must be constructed */
854 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
855 ASN1_R_TYPE_NOT_CONSTRUCTED
);
860 /* If indefinite length constructed find the real end */
863 if (!asn1_find_end(&p
, plen
, inf
))
869 len
= p
- cont
+ plen
;
879 /* Should really check the internal tags are correct but
880 * some things may get this wrong. The relevant specs
881 * say that constructed string types should be OCTET STRINGs
882 * internally irrespective of the type. So instead just check
883 * for UNIVERSAL class and ignore the tag.
885 if (!asn1_collect(&buf
, &p
, plen
, inf
, -1, V_ASN1_UNIVERSAL
))
891 /* Append a final null to string */
892 if (!BUF_MEM_grow_clean(&buf
, len
+ 1))
894 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
895 ERR_R_MALLOC_FAILURE
);
899 cont
= (const unsigned char *)buf
.data
;
909 /* We now have content length and type: translate into a structure */
910 if (!asn1_ex_c2i(pval
, cont
, len
, utype
, &free_cont
, it
))
916 if (free_cont
&& buf
.data
) OPENSSL_free(buf
.data
);
920 /* Translate ASN1 content octets into a structure */
922 int asn1_ex_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
923 int utype
, char *free_cont
, const ASN1_ITEM
*it
)
925 ASN1_VALUE
**opval
= NULL
;
927 ASN1_TYPE
*typ
= NULL
;
929 const ASN1_PRIMITIVE_FUNCS
*pf
;
933 if (pf
&& pf
->prim_c2i
)
934 return pf
->prim_c2i(pval
, cont
, len
, utype
, free_cont
, it
);
935 /* If ANY type clear type and set pointer to internal value */
936 if (it
->utype
== V_ASN1_ANY
)
940 typ
= ASN1_TYPE_new();
943 *pval
= (ASN1_VALUE
*)typ
;
946 typ
= (ASN1_TYPE
*)*pval
;
948 if (utype
!= typ
->type
)
949 ASN1_TYPE_set(typ
, utype
, NULL
);
951 pval
= &typ
->value
.asn1_value
;
956 if (!c2i_ASN1_OBJECT((ASN1_OBJECT
**)pval
, &cont
, len
))
963 ASN1err(ASN1_F_ASN1_EX_C2I
,
964 ASN1_R_NULL_IS_WRONG_LENGTH
);
967 *pval
= (ASN1_VALUE
*)1;
973 ASN1err(ASN1_F_ASN1_EX_C2I
,
974 ASN1_R_BOOLEAN_IS_WRONG_LENGTH
);
980 tbool
= (ASN1_BOOLEAN
*)pval
;
985 case V_ASN1_BIT_STRING
:
986 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING
**)pval
, &cont
, len
))
991 case V_ASN1_NEG_INTEGER
:
992 case V_ASN1_ENUMERATED
:
993 case V_ASN1_NEG_ENUMERATED
:
994 tint
= (ASN1_INTEGER
**)pval
;
995 if (!c2i_ASN1_INTEGER(tint
, &cont
, len
))
997 /* Fixup type to match the expected form */
998 (*tint
)->type
= utype
| ((*tint
)->type
& V_ASN1_NEG
);
1001 case V_ASN1_OCTET_STRING
:
1002 case V_ASN1_NUMERICSTRING
:
1003 case V_ASN1_PRINTABLESTRING
:
1004 case V_ASN1_T61STRING
:
1005 case V_ASN1_VIDEOTEXSTRING
:
1006 case V_ASN1_IA5STRING
:
1007 case V_ASN1_UTCTIME
:
1008 case V_ASN1_GENERALIZEDTIME
:
1009 case V_ASN1_GRAPHICSTRING
:
1010 case V_ASN1_VISIBLESTRING
:
1011 case V_ASN1_GENERALSTRING
:
1012 case V_ASN1_UNIVERSALSTRING
:
1013 case V_ASN1_BMPSTRING
:
1014 case V_ASN1_UTF8STRING
:
1017 case V_ASN1_SEQUENCE
:
1019 /* All based on ASN1_STRING and handled the same */
1022 stmp
= ASN1_STRING_type_new(utype
);
1025 ASN1err(ASN1_F_ASN1_EX_C2I
,
1026 ERR_R_MALLOC_FAILURE
);
1029 *pval
= (ASN1_VALUE
*)stmp
;
1033 stmp
= (ASN1_STRING
*)*pval
;
1036 /* If we've already allocated a buffer use it */
1040 OPENSSL_free(stmp
->data
);
1041 stmp
->data
= (unsigned char *)cont
; /* UGLY CAST! RL */
1047 if (!ASN1_STRING_set(stmp
, cont
, len
))
1049 ASN1err(ASN1_F_ASN1_EX_C2I
,
1050 ERR_R_MALLOC_FAILURE
);
1051 ASN1_STRING_free(stmp
);
1058 /* If ASN1_ANY and NULL type fix up value */
1059 if (typ
&& (utype
== V_ASN1_NULL
))
1060 typ
->value
.ptr
= NULL
;
1066 ASN1_TYPE_free(typ
);
1074 /* This function finds the end of an ASN1 structure when passed its maximum
1075 * length, whether it is indefinite length and a pointer to the content.
1076 * This is more efficient than calling asn1_collect because it does not
1077 * recurse on each indefinite length header.
1080 static int asn1_find_end(const unsigned char **in
, long len
, char inf
)
1084 const unsigned char *p
= *in
, *q
;
1085 /* If not indefinite length constructed just add length */
1092 /* Indefinite length constructed form. Find the end when enough EOCs
1093 * are found. If more indefinite length constructed headers
1094 * are encountered increment the expected eoc count otherwise just
1095 * skip to the end of the data.
1099 if(asn1_check_eoc(&p
, len
))
1102 if (expected_eoc
== 0)
1108 /* Just read in a header: only care about the length */
1109 if(!asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, NULL
, &p
, len
,
1112 ASN1err(ASN1_F_ASN1_FIND_END
, ERR_R_NESTED_ASN1_ERROR
);
1123 ASN1err(ASN1_F_ASN1_FIND_END
, ASN1_R_MISSING_EOC
);
1129 /* This function collects the asn1 data from a constructred string
1130 * type into a buffer. The values of 'in' and 'len' should refer
1131 * to the contents of the constructed type and 'inf' should be set
1132 * if it is indefinite length.
1135 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
1136 char inf
, int tag
, int aclass
)
1138 const unsigned char *p
, *q
;
1143 /* If no buffer and not indefinite length constructed just pass over
1144 * the encoded data */
1154 if (asn1_check_eoc(&p
, len
))
1156 /* EOC is illegal outside indefinite length
1157 * constructed form */
1160 ASN1err(ASN1_F_ASN1_COLLECT
,
1161 ASN1_R_UNEXPECTED_EOC
);
1168 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &ininf
, &cst
, &p
,
1169 len
, tag
, aclass
, 0, NULL
))
1171 ASN1err(ASN1_F_ASN1_COLLECT
, ERR_R_NESTED_ASN1_ERROR
);
1175 /* If indefinite length constructed update max length */
1178 #ifdef OPENSSL_ALLOW_NESTED_ASN1_STRINGS
1179 if (!asn1_collect(buf
, &p
, plen
, ininf
, tag
, aclass
))
1182 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_NESTED_ASN1_STRING
);
1186 else if (plen
&& !collect_data(buf
, &p
, plen
))
1192 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_MISSING_EOC
);
1199 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
)
1205 if (!BUF_MEM_grow_clean(buf
, len
+ plen
))
1207 ASN1err(ASN1_F_COLLECT_DATA
, ERR_R_MALLOC_FAILURE
);
1210 memcpy(buf
->data
+ len
, *p
, plen
);
1216 /* Check for ASN1 EOC and swallow it if found */
1218 static int asn1_check_eoc(const unsigned char **in
, long len
)
1220 const unsigned char *p
;
1221 if (len
< 2) return 0;
1231 /* Check an ASN1 tag and length: a bit like ASN1_get_object
1232 * but it sets the length for indefinite length constructed
1233 * form, we don't know the exact length but we can set an
1234 * upper bound to the amount of data available minus the
1235 * header length just read.
1238 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
1239 char *inf
, char *cst
,
1240 const unsigned char **in
, long len
,
1241 int exptag
, int expclass
, char opt
,
1247 const unsigned char *p
, *q
;
1251 if (ctx
&& ctx
->valid
)
1255 pclass
= ctx
->pclass
;
1261 i
= ASN1_get_object(&p
, &plen
, &ptag
, &pclass
, len
);
1266 ctx
->pclass
= pclass
;
1268 ctx
->hdrlen
= p
- q
;
1270 /* If definite length, and no error, length +
1271 * header can't exceed total amount of data available.
1273 if (!(i
& 0x81) && ((plen
+ ctx
->hdrlen
) > len
))
1275 ASN1err(ASN1_F_ASN1_CHECK_TLEN
,
1277 asn1_tlc_clear(ctx
);
1285 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_BAD_OBJECT_HEADER
);
1286 asn1_tlc_clear(ctx
);
1291 if ((exptag
!= ptag
) || (expclass
!= pclass
))
1293 /* If type is OPTIONAL, not an error:
1294 * indicate missing type.
1297 asn1_tlc_clear(ctx
);
1298 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_WRONG_TAG
);
1301 /* We have a tag and class match:
1302 * assume we are going to do something with it */
1303 asn1_tlc_clear(ctx
);
1307 plen
= len
- (p
- q
);
1313 *cst
= i
& V_ASN1_CONSTRUCTED
;