3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000-2018 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * 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>
69 * Constructed types with a recursive definition (such as can be found in PKCS7)
70 * could eventually exceed the stack given malicious input with excessive
71 * recursion. Therefore we limit the stack depth. This is the maximum number of
72 * recursive invocations of asn1_item_embed_d2i().
74 #define ASN1_MAX_CONSTRUCTED_NEST 30
76 static int asn1_check_eoc(const unsigned char **in
, long len
);
77 static int asn1_find_end(const unsigned char **in
, long len
, char inf
);
79 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
80 char inf
, int tag
, int aclass
, int depth
);
82 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
);
84 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
86 const unsigned char **in
, long len
,
87 int exptag
, int expclass
, char opt
, ASN1_TLC
*ctx
);
89 static int asn1_template_ex_d2i(ASN1_VALUE
**pval
,
90 const unsigned char **in
, long len
,
91 const ASN1_TEMPLATE
*tt
, char opt
,
92 ASN1_TLC
*ctx
, int depth
);
93 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
94 const unsigned char **in
, long len
,
95 const ASN1_TEMPLATE
*tt
, char opt
,
96 ASN1_TLC
*ctx
, int depth
);
97 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
98 const unsigned char **in
, long len
,
100 int tag
, int aclass
, char opt
,
103 /* Table to convert tags to bit values, used for MSTRING type */
104 static const unsigned long tag2bit
[32] = {
106 0, 0, 0, B_ASN1_BIT_STRING
,
108 B_ASN1_OCTET_STRING
, 0, 0, B_ASN1_UNKNOWN
,
110 B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
,
112 B_ASN1_UTF8STRING
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
,
114 B_ASN1_SEQUENCE
, 0, B_ASN1_NUMERICSTRING
, B_ASN1_PRINTABLESTRING
,
116 B_ASN1_T61STRING
, B_ASN1_VIDEOTEXSTRING
, B_ASN1_IA5STRING
,
118 B_ASN1_UTCTIME
, B_ASN1_GENERALIZEDTIME
,
120 B_ASN1_GRAPHICSTRING
, B_ASN1_ISO64STRING
, B_ASN1_GENERALSTRING
,
122 B_ASN1_UNIVERSALSTRING
, B_ASN1_UNKNOWN
, B_ASN1_BMPSTRING
, B_ASN1_UNKNOWN
,
125 unsigned long ASN1_tag2bit(int tag
)
127 if ((tag
< 0) || (tag
> 30))
132 /* Macro to initialize and invalidate the cache */
134 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
135 /* Version to avoid compiler warning about 'c' always non-NULL */
136 #define asn1_tlc_clear_nc(c) (c)->valid = 0
139 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
140 * function. 'in' points to a buffer to read the data from, in future we
141 * will have more advanced versions that can input data a piece at a time and
142 * this will simply be a special case.
145 ASN1_VALUE
*ASN1_item_d2i(ASN1_VALUE
**pval
,
146 const unsigned char **in
, long len
,
150 ASN1_VALUE
*ptmpval
= NULL
;
153 asn1_tlc_clear_nc(&c
);
154 if (ASN1_item_ex_d2i(pval
, in
, len
, it
, -1, 0, 0, &c
) > 0)
159 int ASN1_template_d2i(ASN1_VALUE
**pval
,
160 const unsigned char **in
, long len
,
161 const ASN1_TEMPLATE
*tt
)
164 asn1_tlc_clear_nc(&c
);
165 return asn1_template_ex_d2i(pval
, in
, len
, tt
, 0, &c
, 0);
169 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
170 * tag mismatch return -1 to handle OPTIONAL
172 static int asn1_item_ex_d2i(ASN1_VALUE
**pval
, const unsigned char **in
,
173 long len
, const ASN1_ITEM
*it
, int tag
, int aclass
,
174 char opt
, ASN1_TLC
*ctx
, int depth
)
176 const ASN1_TEMPLATE
*tt
, *errtt
= NULL
;
177 const ASN1_COMPAT_FUNCS
*cf
;
178 const ASN1_EXTERN_FUNCS
*ef
;
179 const ASN1_AUX
*aux
= it
->funcs
;
180 ASN1_aux_cb
*asn1_cb
;
181 const unsigned char *p
= NULL
, *q
;
182 unsigned char *wp
= NULL
; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
183 unsigned char imphack
= 0, oclass
;
184 char seq_eoc
, seq_nolen
, cst
, isopt
;
189 ASN1_VALUE
**pchptr
, *ptmpval
;
190 int combine
= aclass
& ASN1_TFLG_COMBINE
;
191 aclass
&= ~ASN1_TFLG_COMBINE
;
194 if (aux
&& aux
->asn1_cb
)
195 asn1_cb
= aux
->asn1_cb
;
199 if (++depth
> ASN1_MAX_CONSTRUCTED_NEST
) {
200 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_NESTED_TOO_DEEP
);
205 case ASN1_ITYPE_PRIMITIVE
:
208 * tagging or OPTIONAL is currently illegal on an item template
209 * because the flags can't get passed down. In practice this
210 * isn't a problem: we include the relevant flags from the item
211 * template in the template itself.
213 if ((tag
!= -1) || opt
) {
214 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
,
215 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE
);
218 return asn1_template_ex_d2i(pval
, in
, len
,
219 it
->templates
, opt
, ctx
, depth
);
221 return asn1_d2i_ex_primitive(pval
, in
, len
, it
,
222 tag
, aclass
, opt
, ctx
);
225 case ASN1_ITYPE_MSTRING
:
227 /* Just read in tag and class */
228 ret
= asn1_check_tlen(NULL
, &otag
, &oclass
, NULL
, NULL
,
229 &p
, len
, -1, 0, 1, ctx
);
231 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
235 /* Must be UNIVERSAL class */
236 if (oclass
!= V_ASN1_UNIVERSAL
) {
237 /* If OPTIONAL, assume this is OK */
240 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_MSTRING_NOT_UNIVERSAL
);
243 /* Check tag matches bit map */
244 if (!(ASN1_tag2bit(otag
) & it
->utype
)) {
245 /* If OPTIONAL, assume this is OK */
248 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_MSTRING_WRONG_TAG
);
251 return asn1_d2i_ex_primitive(pval
, in
, len
, it
, otag
, 0, 0, ctx
);
253 case ASN1_ITYPE_EXTERN
:
254 /* Use new style d2i */
256 return ef
->asn1_ex_d2i(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
);
258 case ASN1_ITYPE_COMPAT
:
259 /* we must resort to old style evil hackery */
262 /* If OPTIONAL see if it is there */
271 * Don't care about anything other than presence of expected tag
274 ret
= asn1_check_tlen(NULL
, NULL
, NULL
, NULL
, NULL
,
275 &p
, len
, exptag
, aclass
, 1, ctx
);
277 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
285 * This is the old style evil hack IMPLICIT handling: since the
286 * underlying code is expecting a tag and class other than the one
287 * present we change the buffer temporarily then change it back
288 * afterwards. This doesn't and never did work for tags > 30. Yes
289 * this is *horrible* but it is only needed for old style d2i which
290 * will hopefully not be around for much longer. FIXME: should copy
291 * the buffer then modify it so the input buffer can be const: we
292 * should *always* copy because the old style d2i might modify the
297 wp
= *(unsigned char **)in
;
300 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
303 *wp
= (unsigned char)((*p
& V_ASN1_CONSTRUCTED
)
307 ptmpval
= cf
->asn1_d2i(pval
, in
, len
);
315 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
318 case ASN1_ITYPE_CHOICE
:
319 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
322 /* Free up and zero CHOICE value if initialised */
323 i
= asn1_get_choice_selector(pval
, it
);
324 if ((i
>= 0) && (i
< it
->tcount
)) {
325 tt
= it
->templates
+ i
;
326 pchptr
= asn1_get_field_ptr(pval
, tt
);
327 ASN1_template_free(pchptr
, tt
);
328 asn1_set_choice_selector(pval
, -1, it
);
330 } else if (!ASN1_item_ex_new(pval
, it
)) {
331 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
334 /* CHOICE type, try each possibility in turn */
336 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
337 pchptr
= asn1_get_field_ptr(pval
, tt
);
339 * We mark field as OPTIONAL so its absence can be recognised.
341 ret
= asn1_template_ex_d2i(pchptr
, &p
, len
, tt
, 1, ctx
, depth
);
342 /* If field not present, try the next one */
345 /* If positive return, read OK, break loop */
348 /* Otherwise must be an ASN1 parsing error */
350 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
354 /* Did we fall off the end without reading anything? */
355 if (i
== it
->tcount
) {
356 /* If OPTIONAL, this is OK */
358 /* Free and zero it */
359 ASN1_item_ex_free(pval
, it
);
362 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_NO_MATCHING_CHOICE_TYPE
);
366 asn1_set_choice_selector(pval
, i
, it
);
367 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
372 case ASN1_ITYPE_NDEF_SEQUENCE
:
373 case ASN1_ITYPE_SEQUENCE
:
377 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
379 tag
= V_ASN1_SEQUENCE
;
380 aclass
= V_ASN1_UNIVERSAL
;
382 /* Get SEQUENCE length and update len, p */
383 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &seq_eoc
, &cst
,
384 &p
, len
, tag
, aclass
, opt
, ctx
);
386 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
388 } else if (ret
== -1)
390 if (aux
&& (aux
->flags
& ASN1_AFLG_BROKEN
)) {
391 len
= tmplen
- (p
- *in
);
394 /* If indefinite we don't do a length check */
398 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_SEQUENCE_NOT_CONSTRUCTED
);
402 if (!*pval
&& !ASN1_item_ex_new(pval
, it
)) {
403 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
407 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
410 /* Free up and zero any ADB found */
411 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
412 if (tt
->flags
& ASN1_TFLG_ADB_MASK
) {
413 const ASN1_TEMPLATE
*seqtt
;
414 ASN1_VALUE
**pseqval
;
415 seqtt
= asn1_do_adb(pval
, tt
, 0);
418 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
419 ASN1_template_free(pseqval
, seqtt
);
423 /* Get each field entry */
424 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
425 const ASN1_TEMPLATE
*seqtt
;
426 ASN1_VALUE
**pseqval
;
427 seqtt
= asn1_do_adb(pval
, tt
, 1);
430 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
431 /* Have we ran out of data? */
435 if (asn1_check_eoc(&p
, len
)) {
437 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_UNEXPECTED_EOC
);
446 * This determines the OPTIONAL flag value. The field cannot be
447 * omitted if it is the last of a SEQUENCE and there is still
448 * data to be read. This isn't strictly necessary but it
449 * increases efficiency in some cases.
451 if (i
== (it
->tcount
- 1))
454 isopt
= (char)(seqtt
->flags
& ASN1_TFLG_OPTIONAL
);
456 * attempt to read in field, allowing each to be OPTIONAL
459 ret
= asn1_template_ex_d2i(pseqval
, &p
, len
, seqtt
, isopt
, ctx
,
464 } else if (ret
== -1) {
466 * OPTIONAL component absent. Free and zero the field.
468 ASN1_template_free(pseqval
, seqtt
);
475 /* Check for EOC if expecting one */
476 if (seq_eoc
&& !asn1_check_eoc(&p
, len
)) {
477 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_MISSING_EOC
);
480 /* Check all data read */
481 if (!seq_nolen
&& len
) {
482 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_SEQUENCE_LENGTH_MISMATCH
);
487 * If we get here we've got no more data in the SEQUENCE, however we
488 * may not have read all fields so check all remaining are OPTIONAL
489 * and clear any that are.
491 for (; i
< it
->tcount
; tt
++, i
++) {
492 const ASN1_TEMPLATE
*seqtt
;
493 seqtt
= asn1_do_adb(pval
, tt
, 1);
496 if (seqtt
->flags
& ASN1_TFLG_OPTIONAL
) {
497 ASN1_VALUE
**pseqval
;
498 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
499 ASN1_template_free(pseqval
, seqtt
);
502 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I
, ASN1_R_FIELD_MISSING
);
507 if (!asn1_enc_save(pval
, *in
, p
- *in
, it
))
509 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
518 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 int ASN1_item_ex_d2i(ASN1_VALUE
**pval
, const unsigned char **in
, long len
,
532 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
534 return asn1_item_ex_d2i(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
, 0);
538 * Templates are handled with two separate functions. One handles any
539 * EXPLICIT tag and the other handles the rest.
542 static int asn1_template_ex_d2i(ASN1_VALUE
**val
,
543 const unsigned char **in
, long inlen
,
544 const ASN1_TEMPLATE
*tt
, char opt
,
545 ASN1_TLC
*ctx
, int depth
)
550 const unsigned char *p
, *q
;
555 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
559 /* Check if EXPLICIT tag expected */
560 if (flags
& ASN1_TFLG_EXPTAG
) {
563 * Need to work out amount of data available to the inner content and
564 * where it starts: so read in EXPLICIT header to get the info.
566 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &exp_eoc
, &cst
,
567 &p
, inlen
, tt
->tag
, aclass
, opt
, ctx
);
570 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
572 } else if (ret
== -1)
575 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
576 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED
);
579 /* We've found the field so it can't be OPTIONAL now */
580 ret
= asn1_template_noexp_d2i(val
, &p
, len
, tt
, 0, ctx
, depth
);
582 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
585 /* 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
)) {
590 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
, ASN1_R_MISSING_EOC
);
595 * Otherwise we must hit the EXPLICIT tag end or its an error
598 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
599 ASN1_R_EXPLICIT_LENGTH_MISMATCH
);
604 return asn1_template_noexp_d2i(val
, in
, inlen
, tt
, opt
, ctx
, depth
);
610 ASN1_template_free(val
, tt
);
614 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
615 const unsigned char **in
, long len
,
616 const ASN1_TEMPLATE
*tt
, char opt
,
617 ASN1_TLC
*ctx
, int depth
)
621 const unsigned char *p
, *q
;
625 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
630 if (flags
& ASN1_TFLG_SK_MASK
) {
631 /* SET OF, SEQUENCE OF */
634 /* First work out expected inner tag value */
635 if (flags
& ASN1_TFLG_IMPTAG
) {
639 skaclass
= V_ASN1_UNIVERSAL
;
640 if (flags
& ASN1_TFLG_SET_OF
)
643 sktag
= V_ASN1_SEQUENCE
;
646 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &sk_eoc
, NULL
,
647 &p
, len
, sktag
, skaclass
, opt
, ctx
);
649 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_NESTED_ASN1_ERROR
);
651 } else if (ret
== -1)
654 *val
= (ASN1_VALUE
*)sk_new_null();
657 * We've got a valid STACK: free up any items present
659 STACK_OF(ASN1_VALUE
) *sktmp
= (STACK_OF(ASN1_VALUE
) *)*val
;
661 while (sk_ASN1_VALUE_num(sktmp
) > 0) {
662 vtmp
= sk_ASN1_VALUE_pop(sktmp
);
663 ASN1_item_ex_free(&vtmp
, ASN1_ITEM_ptr(tt
->item
));
668 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_MALLOC_FAILURE
);
672 /* Read as many items as we can */
676 /* See if EOC found */
677 if (asn1_check_eoc(&p
, len
)) {
679 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
680 ASN1_R_UNEXPECTED_EOC
);
688 if (!asn1_item_ex_d2i(&skfield
, &p
, len
, ASN1_ITEM_ptr(tt
->item
),
689 -1, 0, 0, ctx
, depth
)) {
690 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
691 ERR_R_NESTED_ASN1_ERROR
);
695 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE
) *)*val
, skfield
)) {
696 ASN1_item_ex_free(&skfield
, ASN1_ITEM_ptr(tt
->item
));
697 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_MALLOC_FAILURE
);
702 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ASN1_R_MISSING_EOC
);
705 } else if (flags
& ASN1_TFLG_IMPTAG
) {
706 /* IMPLICIT tagging */
707 ret
= asn1_item_ex_d2i(val
, &p
, len
, ASN1_ITEM_ptr(tt
->item
), tt
->tag
,
708 aclass
, opt
, ctx
, depth
);
710 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_NESTED_ASN1_ERROR
);
712 } else if (ret
== -1)
715 /* Nothing special */
716 ret
= asn1_item_ex_d2i(val
, &p
, len
, ASN1_ITEM_ptr(tt
->item
),
717 -1, tt
->flags
& ASN1_TFLG_COMBINE
, opt
, ctx
,
720 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_NESTED_ASN1_ERROR
);
722 } else if (ret
== -1)
730 ASN1_template_free(val
, tt
);
734 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
735 const unsigned char **in
, long inlen
,
737 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
741 char cst
, inf
, free_cont
= 0;
742 const unsigned char *p
;
743 BUF_MEM buf
= { 0, NULL
, 0 };
744 const unsigned char *cont
= NULL
;
747 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_ILLEGAL_NULL
);
748 return 0; /* Should never happen */
751 if (it
->itype
== ASN1_ITYPE_MSTRING
) {
757 if (utype
== V_ASN1_ANY
) {
758 /* If type is ANY need to figure out type from tag */
759 unsigned char oclass
;
761 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_ILLEGAL_TAGGED_ANY
);
765 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
766 ASN1_R_ILLEGAL_OPTIONAL_ANY
);
770 ret
= asn1_check_tlen(NULL
, &utype
, &oclass
, NULL
, NULL
,
771 &p
, inlen
, -1, 0, 0, ctx
);
773 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_NESTED_ASN1_ERROR
);
776 if (oclass
!= V_ASN1_UNIVERSAL
)
777 utype
= V_ASN1_OTHER
;
781 aclass
= V_ASN1_UNIVERSAL
;
785 ret
= asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, &cst
,
786 &p
, inlen
, tag
, aclass
, opt
, ctx
);
788 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_NESTED_ASN1_ERROR
);
790 } else if (ret
== -1)
793 /* SEQUENCE, SET and "OTHER" are left in encoded form */
794 if ((utype
== V_ASN1_SEQUENCE
)
795 || (utype
== V_ASN1_SET
) || (utype
== V_ASN1_OTHER
)) {
797 * Clear context cache for type OTHER because the auto clear when we
798 * have a exact match wont work
800 if (utype
== V_ASN1_OTHER
) {
803 /* SEQUENCE and SET must be constructed */
805 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
806 ASN1_R_TYPE_NOT_CONSTRUCTED
);
811 /* If indefinite length constructed find the real end */
813 if (!asn1_find_end(&p
, plen
, inf
))
817 len
= p
- cont
+ plen
;
821 if (utype
== V_ASN1_NULL
|| utype
== V_ASN1_BOOLEAN
822 || utype
== V_ASN1_OBJECT
|| utype
== V_ASN1_INTEGER
823 || utype
== V_ASN1_ENUMERATED
) {
824 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_TYPE_NOT_PRIMITIVE
);
828 /* Free any returned 'buf' content */
831 * Should really check the internal tags are correct but some things
832 * may get this wrong. The relevant specs say that constructed string
833 * types should be OCTET STRINGs internally irrespective of the type.
834 * So instead just check for UNIVERSAL class and ignore the tag.
836 if (!asn1_collect(&buf
, &p
, plen
, inf
, -1, V_ASN1_UNIVERSAL
, 0)) {
840 /* Append a final null to string */
841 if (!BUF_MEM_grow_clean(&buf
, len
+ 1)) {
842 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_MALLOC_FAILURE
);
846 cont
= (const unsigned char *)buf
.data
;
853 /* We now have content length and type: translate into a structure */
854 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
855 if (!asn1_ex_c2i(pval
, cont
, len
, utype
, &free_cont
, it
))
861 if (free_cont
&& buf
.data
)
862 OPENSSL_free(buf
.data
);
866 /* Translate ASN1 content octets into a structure */
868 int asn1_ex_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
869 int utype
, char *free_cont
, const ASN1_ITEM
*it
)
871 ASN1_VALUE
**opval
= NULL
;
873 ASN1_TYPE
*typ
= NULL
;
875 const ASN1_PRIMITIVE_FUNCS
*pf
;
879 if (pf
&& pf
->prim_c2i
)
880 return pf
->prim_c2i(pval
, cont
, len
, utype
, free_cont
, it
);
881 /* If ANY type clear type and set pointer to internal value */
882 if (it
->utype
== V_ASN1_ANY
) {
884 typ
= ASN1_TYPE_new();
887 *pval
= (ASN1_VALUE
*)typ
;
889 typ
= (ASN1_TYPE
*)*pval
;
891 if (utype
!= typ
->type
)
892 ASN1_TYPE_set(typ
, utype
, NULL
);
894 pval
= &typ
->value
.asn1_value
;
898 if (!c2i_ASN1_OBJECT((ASN1_OBJECT
**)pval
, &cont
, len
))
904 ASN1err(ASN1_F_ASN1_EX_C2I
, ASN1_R_NULL_IS_WRONG_LENGTH
);
907 *pval
= (ASN1_VALUE
*)1;
912 ASN1err(ASN1_F_ASN1_EX_C2I
, ASN1_R_BOOLEAN_IS_WRONG_LENGTH
);
916 tbool
= (ASN1_BOOLEAN
*)pval
;
921 case V_ASN1_BIT_STRING
:
922 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING
**)pval
, &cont
, len
))
927 case V_ASN1_ENUMERATED
:
928 tint
= (ASN1_INTEGER
**)pval
;
929 if (!c2i_ASN1_INTEGER(tint
, &cont
, len
))
931 /* Fixup type to match the expected form */
932 (*tint
)->type
= utype
| ((*tint
)->type
& V_ASN1_NEG
);
935 case V_ASN1_OCTET_STRING
:
936 case V_ASN1_NUMERICSTRING
:
937 case V_ASN1_PRINTABLESTRING
:
938 case V_ASN1_T61STRING
:
939 case V_ASN1_VIDEOTEXSTRING
:
940 case V_ASN1_IA5STRING
:
942 case V_ASN1_GENERALIZEDTIME
:
943 case V_ASN1_GRAPHICSTRING
:
944 case V_ASN1_VISIBLESTRING
:
945 case V_ASN1_GENERALSTRING
:
946 case V_ASN1_UNIVERSALSTRING
:
947 case V_ASN1_BMPSTRING
:
948 case V_ASN1_UTF8STRING
:
951 case V_ASN1_SEQUENCE
:
953 if (utype
== V_ASN1_BMPSTRING
&& (len
& 1)) {
954 ASN1err(ASN1_F_ASN1_EX_C2I
, ASN1_R_BMPSTRING_IS_WRONG_LENGTH
);
957 if (utype
== V_ASN1_UNIVERSALSTRING
&& (len
& 3)) {
958 ASN1err(ASN1_F_ASN1_EX_C2I
,
959 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH
);
962 /* All based on ASN1_STRING and handled the same */
964 stmp
= ASN1_STRING_type_new(utype
);
966 ASN1err(ASN1_F_ASN1_EX_C2I
, ERR_R_MALLOC_FAILURE
);
969 *pval
= (ASN1_VALUE
*)stmp
;
971 stmp
= (ASN1_STRING
*)*pval
;
974 /* If we've already allocated a buffer use it */
977 OPENSSL_free(stmp
->data
);
978 stmp
->data
= (unsigned char *)cont
; /* UGLY CAST! RL */
982 if (!ASN1_STRING_set(stmp
, cont
, len
)) {
983 ASN1err(ASN1_F_ASN1_EX_C2I
, ERR_R_MALLOC_FAILURE
);
984 ASN1_STRING_free(stmp
);
991 /* If ASN1_ANY and NULL type fix up value */
992 if (typ
&& (utype
== V_ASN1_NULL
))
993 typ
->value
.ptr
= NULL
;
1006 * This function finds the end of an ASN1 structure when passed its maximum
1007 * length, whether it is indefinite length and a pointer to the content. This
1008 * is more efficient than calling asn1_collect because it does not recurse on
1009 * each indefinite length header.
1012 static int asn1_find_end(const unsigned char **in
, long len
, char inf
)
1016 const unsigned char *p
= *in
, *q
;
1017 /* If not indefinite length constructed just add length */
1024 * Indefinite length constructed form. Find the end when enough EOCs are
1025 * found. If more indefinite length constructed headers are encountered
1026 * increment the expected eoc count otherwise just skip to the end of the
1030 if (asn1_check_eoc(&p
, len
)) {
1032 if (expected_eoc
== 0)
1038 /* Just read in a header: only care about the length */
1039 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, NULL
, &p
, len
,
1041 ASN1err(ASN1_F_ASN1_FIND_END
, ERR_R_NESTED_ASN1_ERROR
);
1051 ASN1err(ASN1_F_ASN1_FIND_END
, ASN1_R_MISSING_EOC
);
1059 * This function collects the asn1 data from a constructred string type into
1060 * a buffer. The values of 'in' and 'len' should refer to the contents of the
1061 * constructed type and 'inf' should be set if it is indefinite length.
1064 #ifndef ASN1_MAX_STRING_NEST
1066 * This determines how many levels of recursion are permitted in ASN1 string
1067 * types. If it is not limited stack overflows can occur. If set to zero no
1068 * recursion is allowed at all. Although zero should be adequate examples
1069 * exist that require a value of 1. So 5 should be more than enough.
1071 # define ASN1_MAX_STRING_NEST 5
1074 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
1075 char inf
, int tag
, int aclass
, int depth
)
1077 const unsigned char *p
, *q
;
1083 * If no buffer and not indefinite length constructed just pass over the
1093 if (asn1_check_eoc(&p
, len
)) {
1095 * EOC is illegal outside indefinite length constructed form
1098 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_UNEXPECTED_EOC
);
1105 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &ininf
, &cst
, &p
,
1106 len
, tag
, aclass
, 0, NULL
)) {
1107 ASN1err(ASN1_F_ASN1_COLLECT
, ERR_R_NESTED_ASN1_ERROR
);
1111 /* If indefinite length constructed update max length */
1113 if (depth
>= ASN1_MAX_STRING_NEST
) {
1114 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_NESTED_ASN1_STRING
);
1117 if (!asn1_collect(buf
, &p
, plen
, ininf
, tag
, aclass
, depth
+ 1))
1119 } else if (plen
&& !collect_data(buf
, &p
, plen
))
1124 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_MISSING_EOC
);
1131 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
)
1136 if (!BUF_MEM_grow_clean(buf
, len
+ plen
)) {
1137 ASN1err(ASN1_F_COLLECT_DATA
, ERR_R_MALLOC_FAILURE
);
1140 memcpy(buf
->data
+ len
, *p
, plen
);
1146 /* Check for ASN1 EOC and swallow it if found */
1148 static int asn1_check_eoc(const unsigned char **in
, long len
)
1150 const unsigned char *p
;
1154 if (!p
[0] && !p
[1]) {
1162 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1163 * length for indefinite length constructed form, we don't know the exact
1164 * length but we can set an upper bound to the amount of data available minus
1165 * the header length just read.
1168 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
1169 char *inf
, char *cst
,
1170 const unsigned char **in
, long len
,
1171 int exptag
, int expclass
, char opt
, ASN1_TLC
*ctx
)
1176 const unsigned char *p
, *q
;
1180 if (ctx
&& ctx
->valid
) {
1183 pclass
= ctx
->pclass
;
1187 i
= ASN1_get_object(&p
, &plen
, &ptag
, &pclass
, len
);
1191 ctx
->pclass
= pclass
;
1193 ctx
->hdrlen
= p
- q
;
1196 * If definite length, and no error, length + header can't exceed
1197 * total amount of data available.
1199 if (!(i
& 0x81) && ((plen
+ ctx
->hdrlen
) > len
)) {
1200 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_TOO_LONG
);
1201 asn1_tlc_clear(ctx
);
1208 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_BAD_OBJECT_HEADER
);
1209 asn1_tlc_clear(ctx
);
1213 if ((exptag
!= ptag
) || (expclass
!= pclass
)) {
1215 * If type is OPTIONAL, not an error: indicate missing type.
1219 asn1_tlc_clear(ctx
);
1220 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_WRONG_TAG
);
1224 * We have a tag and class match: assume we are going to do something
1227 asn1_tlc_clear(ctx
);
1231 plen
= len
- (p
- q
);
1237 *cst
= i
& V_ASN1_CONSTRUCTED
;