2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
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).
61 #include <openssl/asn1.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/objects.h>
64 #include <openssl/buffer.h>
65 #include <openssl/err.h>
66 #include "asn1_locl.h"
68 static int asn1_item_embed_d2i(ASN1_VALUE
**pval
, const unsigned char **in
,
69 long len
, const ASN1_ITEM
*it
,
70 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
);
72 static int asn1_check_eoc(const unsigned char **in
, long len
);
73 static int asn1_find_end(const unsigned char **in
, long len
, char inf
);
75 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
76 char inf
, int tag
, int aclass
, int depth
);
78 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
);
80 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
82 const unsigned char **in
, long len
,
83 int exptag
, int expclass
, char opt
, ASN1_TLC
*ctx
);
85 static int asn1_template_ex_d2i(ASN1_VALUE
**pval
,
86 const unsigned char **in
, long len
,
87 const ASN1_TEMPLATE
*tt
, char opt
,
89 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
90 const unsigned char **in
, long len
,
91 const ASN1_TEMPLATE
*tt
, char opt
,
93 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
94 const unsigned char **in
, long len
,
96 int tag
, int aclass
, char opt
,
98 static int asn1_ex_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
99 int utype
, char *free_cont
, const ASN1_ITEM
*it
);
101 /* Table to convert tags to bit values, used for MSTRING type */
102 static const unsigned long tag2bit
[32] = {
104 0, 0, 0, B_ASN1_BIT_STRING
,
106 B_ASN1_OCTET_STRING
, 0, 0, B_ASN1_UNKNOWN
,
108 B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
,
110 B_ASN1_UTF8STRING
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
,
112 B_ASN1_SEQUENCE
, 0, B_ASN1_NUMERICSTRING
, B_ASN1_PRINTABLESTRING
,
114 B_ASN1_T61STRING
, B_ASN1_VIDEOTEXSTRING
, B_ASN1_IA5STRING
,
116 B_ASN1_UTCTIME
, B_ASN1_GENERALIZEDTIME
,
118 B_ASN1_GRAPHICSTRING
, B_ASN1_ISO64STRING
, B_ASN1_GENERALSTRING
,
120 B_ASN1_UNIVERSALSTRING
, B_ASN1_UNKNOWN
, B_ASN1_BMPSTRING
, B_ASN1_UNKNOWN
,
123 unsigned long ASN1_tag2bit(int tag
)
125 if ((tag
< 0) || (tag
> 30))
130 /* Macro to initialize and invalidate the cache */
132 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
133 /* Version to avoid compiler warning about 'c' always non-NULL */
134 #define asn1_tlc_clear_nc(c) (c)->valid = 0
137 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
138 * function. 'in' points to a buffer to read the data from, in future we
139 * will have more advanced versions that can input data a piece at a time and
140 * this will simply be a special case.
143 ASN1_VALUE
*ASN1_item_d2i(ASN1_VALUE
**pval
,
144 const unsigned char **in
, long len
,
148 ASN1_VALUE
*ptmpval
= NULL
;
151 asn1_tlc_clear_nc(&c
);
152 if (ASN1_item_ex_d2i(pval
, in
, len
, it
, -1, 0, 0, &c
) > 0)
157 int ASN1_item_ex_d2i(ASN1_VALUE
**pval
, const unsigned char **in
, long len
,
159 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
162 rv
= asn1_item_embed_d2i(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
);
164 ASN1_item_ex_free(pval
, it
);
169 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
170 * tag mismatch return -1 to handle OPTIONAL
173 static int asn1_item_embed_d2i(ASN1_VALUE
**pval
, const unsigned char **in
,
174 long len
, const ASN1_ITEM
*it
,
175 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
177 const ASN1_TEMPLATE
*tt
, *errtt
= NULL
;
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 oclass
;
183 char seq_eoc
, seq_nolen
, cst
, isopt
;
191 if (aux
&& aux
->asn1_cb
)
192 asn1_cb
= aux
->asn1_cb
;
197 case ASN1_ITYPE_PRIMITIVE
:
200 * tagging or OPTIONAL is currently illegal on an item template
201 * because the flags can't get passed down. In practice this
202 * isn't a problem: we include the relevant flags from the item
203 * template in the template itself.
205 if ((tag
!= -1) || opt
) {
206 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
,
207 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE
);
210 return asn1_template_ex_d2i(pval
, in
, len
,
211 it
->templates
, opt
, ctx
);
213 return asn1_d2i_ex_primitive(pval
, in
, len
, it
,
214 tag
, aclass
, opt
, ctx
);
216 case ASN1_ITYPE_MSTRING
:
218 /* Just read in tag and class */
219 ret
= asn1_check_tlen(NULL
, &otag
, &oclass
, NULL
, NULL
,
220 &p
, len
, -1, 0, 1, ctx
);
222 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
226 /* Must be UNIVERSAL class */
227 if (oclass
!= V_ASN1_UNIVERSAL
) {
228 /* If OPTIONAL, assume this is OK */
231 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_MSTRING_NOT_UNIVERSAL
);
234 /* Check tag matches bit map */
235 if (!(ASN1_tag2bit(otag
) & it
->utype
)) {
236 /* If OPTIONAL, assume this is OK */
239 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_MSTRING_WRONG_TAG
);
242 return asn1_d2i_ex_primitive(pval
, in
, len
, it
, otag
, 0, 0, ctx
);
244 case ASN1_ITYPE_EXTERN
:
245 /* Use new style d2i */
247 return ef
->asn1_ex_d2i(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
);
249 case ASN1_ITYPE_CHOICE
:
250 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
253 /* Free up and zero CHOICE value if initialised */
254 i
= asn1_get_choice_selector(pval
, it
);
255 if ((i
>= 0) && (i
< it
->tcount
)) {
256 tt
= it
->templates
+ i
;
257 pchptr
= asn1_get_field_ptr(pval
, tt
);
258 asn1_template_free(pchptr
, tt
);
259 asn1_set_choice_selector(pval
, -1, it
);
261 } else if (!ASN1_item_ex_new(pval
, it
)) {
262 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
265 /* CHOICE type, try each possibility in turn */
267 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
268 pchptr
= asn1_get_field_ptr(pval
, tt
);
270 * We mark field as OPTIONAL so its absence can be recognised.
272 ret
= asn1_template_ex_d2i(pchptr
, &p
, len
, tt
, 1, ctx
);
273 /* If field not present, try the next one */
277 * Set the choice selector here to ensure that the value is
278 * correctly freed upon error. It may be partially initialized
279 * even if parsing failed.
281 asn1_set_choice_selector(pval
, i
, it
);
282 /* If positive return, read OK, break loop */
285 /* Otherwise must be an ASN1 parsing error */
287 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
291 /* Did we fall off the end without reading anything? */
292 if (i
== it
->tcount
) {
293 /* If OPTIONAL, this is OK */
295 /* Free and zero it */
296 ASN1_item_ex_free(pval
, it
);
299 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_NO_MATCHING_CHOICE_TYPE
);
303 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
308 case ASN1_ITYPE_NDEF_SEQUENCE
:
309 case ASN1_ITYPE_SEQUENCE
:
313 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
315 tag
= V_ASN1_SEQUENCE
;
316 aclass
= V_ASN1_UNIVERSAL
;
318 /* Get SEQUENCE length and update len, p */
319 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &seq_eoc
, &cst
,
320 &p
, len
, tag
, aclass
, opt
, ctx
);
322 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
324 } else if (ret
== -1)
326 if (aux
&& (aux
->flags
& ASN1_AFLG_BROKEN
)) {
327 len
= tmplen
- (p
- *in
);
330 /* If indefinite we don't do a length check */
334 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_SEQUENCE_NOT_CONSTRUCTED
);
338 if (!*pval
&& !ASN1_item_ex_new(pval
, it
)) {
339 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
343 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
346 /* Free up and zero any ADB found */
347 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
348 if (tt
->flags
& ASN1_TFLG_ADB_MASK
) {
349 const ASN1_TEMPLATE
*seqtt
;
350 ASN1_VALUE
**pseqval
;
351 seqtt
= asn1_do_adb(pval
, tt
, 1);
352 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
353 asn1_template_free(pseqval
, seqtt
);
357 /* Get each field entry */
358 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
359 const ASN1_TEMPLATE
*seqtt
;
360 ASN1_VALUE
**pseqval
;
361 seqtt
= asn1_do_adb(pval
, tt
, 1);
364 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
365 /* Have we ran out of data? */
369 if (asn1_check_eoc(&p
, len
)) {
371 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_UNEXPECTED_EOC
);
380 * This determines the OPTIONAL flag value. The field cannot be
381 * omitted if it is the last of a SEQUENCE and there is still
382 * data to be read. This isn't strictly necessary but it
383 * increases efficiency in some cases.
385 if (i
== (it
->tcount
- 1))
388 isopt
= (char)(seqtt
->flags
& ASN1_TFLG_OPTIONAL
);
390 * attempt to read in field, allowing each to be OPTIONAL
393 ret
= asn1_template_ex_d2i(pseqval
, &p
, len
, seqtt
, isopt
, ctx
);
397 } else if (ret
== -1) {
399 * OPTIONAL component absent. Free and zero the field.
401 asn1_template_free(pseqval
, seqtt
);
408 /* Check for EOC if expecting one */
409 if (seq_eoc
&& !asn1_check_eoc(&p
, len
)) {
410 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_MISSING_EOC
);
413 /* Check all data read */
414 if (!seq_nolen
&& len
) {
415 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_SEQUENCE_LENGTH_MISMATCH
);
420 * If we get here we've got no more data in the SEQUENCE, however we
421 * may not have read all fields so check all remaining are OPTIONAL
422 * and clear any that are.
424 for (; i
< it
->tcount
; tt
++, i
++) {
425 const ASN1_TEMPLATE
*seqtt
;
426 seqtt
= asn1_do_adb(pval
, tt
, 1);
429 if (seqtt
->flags
& ASN1_TFLG_OPTIONAL
) {
430 ASN1_VALUE
**pseqval
;
431 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
432 asn1_template_free(pseqval
, seqtt
);
435 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_FIELD_MISSING
);
440 if (!asn1_enc_save(pval
, *in
, p
- *in
, it
))
442 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
451 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_AUX_ERROR
);
454 ERR_add_error_data(4, "Field=", errtt
->field_name
,
455 ", Type=", it
->sname
);
457 ERR_add_error_data(2, "Type=", it
->sname
);
462 * Templates are handled with two separate functions. One handles any
463 * EXPLICIT tag and the other handles the rest.
466 static int asn1_template_ex_d2i(ASN1_VALUE
**val
,
467 const unsigned char **in
, long inlen
,
468 const ASN1_TEMPLATE
*tt
, char opt
,
474 const unsigned char *p
, *q
;
479 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
483 /* Check if EXPLICIT tag expected */
484 if (flags
& ASN1_TFLG_EXPTAG
) {
487 * Need to work out amount of data available to the inner content and
488 * where it starts: so read in EXPLICIT header to get the info.
490 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &exp_eoc
, &cst
,
491 &p
, inlen
, tt
->tag
, aclass
, opt
, ctx
);
494 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
496 } else if (ret
== -1)
499 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
500 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED
);
503 /* We've found the field so it can't be OPTIONAL now */
504 ret
= asn1_template_noexp_d2i(val
, &p
, len
, tt
, 0, ctx
);
506 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
509 /* We read the field in OK so update length */
512 /* If NDEF we must have an EOC here */
513 if (!asn1_check_eoc(&p
, len
)) {
514 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
, ASN1_R_MISSING_EOC
);
519 * Otherwise we must hit the EXPLICIT tag end or its an error
522 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
523 ASN1_R_EXPLICIT_LENGTH_MISMATCH
);
528 return asn1_template_noexp_d2i(val
, in
, inlen
, tt
, opt
, ctx
);
537 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
538 const unsigned char **in
, long len
,
539 const ASN1_TEMPLATE
*tt
, char opt
,
545 const unsigned char *p
, *q
;
549 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
555 * If field is embedded then val needs fixing so it is a pointer to
556 * a pointer to a field.
558 if (tt
->flags
& ASN1_TFLG_EMBED
) {
559 tval
= (ASN1_VALUE
*)val
;
563 if (flags
& ASN1_TFLG_SK_MASK
) {
564 /* SET OF, SEQUENCE OF */
567 /* First work out expected inner tag value */
568 if (flags
& ASN1_TFLG_IMPTAG
) {
572 skaclass
= V_ASN1_UNIVERSAL
;
573 if (flags
& ASN1_TFLG_SET_OF
)
576 sktag
= V_ASN1_SEQUENCE
;
579 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &sk_eoc
, NULL
,
580 &p
, len
, sktag
, skaclass
, opt
, ctx
);
582 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_NESTED_ASN1_ERROR
);
584 } else if (ret
== -1)
587 *val
= (ASN1_VALUE
*)sk_new_null();
590 * We've got a valid STACK: free up any items present
592 STACK_OF(ASN1_VALUE
) *sktmp
= (STACK_OF(ASN1_VALUE
) *)*val
;
594 while (sk_ASN1_VALUE_num(sktmp
) > 0) {
595 vtmp
= sk_ASN1_VALUE_pop(sktmp
);
596 ASN1_item_ex_free(&vtmp
, ASN1_ITEM_ptr(tt
->item
));
601 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_MALLOC_FAILURE
);
605 /* Read as many items as we can */
609 /* See if EOC found */
610 if (asn1_check_eoc(&p
, len
)) {
612 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
613 ASN1_R_UNEXPECTED_EOC
);
621 if (!asn1_item_embed_d2i(&skfield
, &p
, len
,
622 ASN1_ITEM_ptr(tt
->item
), -1, 0, 0, ctx
)) {
623 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
624 ERR_R_NESTED_ASN1_ERROR
);
625 /* |skfield| may be partially allocated despite failure. */
626 ASN1_item_free(skfield
, ASN1_ITEM_ptr(tt
->item
));
630 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE
) *)*val
, skfield
)) {
631 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_MALLOC_FAILURE
);
632 ASN1_item_free(skfield
, ASN1_ITEM_ptr(tt
->item
));
637 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ASN1_R_MISSING_EOC
);
640 } else if (flags
& ASN1_TFLG_IMPTAG
) {
641 /* IMPLICIT tagging */
642 ret
= asn1_item_embed_d2i(val
, &p
, len
,
643 ASN1_ITEM_ptr(tt
->item
), tt
->tag
, aclass
, opt
,
646 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_NESTED_ASN1_ERROR
);
648 } else if (ret
== -1)
651 /* Nothing special */
652 ret
= asn1_item_embed_d2i(val
, &p
, len
, ASN1_ITEM_ptr(tt
->item
),
655 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_NESTED_ASN1_ERROR
);
657 } else if (ret
== -1)
668 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
669 const unsigned char **in
, long inlen
,
671 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
675 char cst
, inf
, free_cont
= 0;
676 const unsigned char *p
;
677 BUF_MEM buf
= { 0, NULL
, 0, 0 };
678 const unsigned char *cont
= NULL
;
681 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_ILLEGAL_NULL
);
682 return 0; /* Should never happen */
685 if (it
->itype
== ASN1_ITYPE_MSTRING
) {
691 if (utype
== V_ASN1_ANY
) {
692 /* If type is ANY need to figure out type from tag */
693 unsigned char oclass
;
695 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_ILLEGAL_TAGGED_ANY
);
699 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
700 ASN1_R_ILLEGAL_OPTIONAL_ANY
);
704 ret
= asn1_check_tlen(NULL
, &utype
, &oclass
, NULL
, NULL
,
705 &p
, inlen
, -1, 0, 0, ctx
);
707 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_NESTED_ASN1_ERROR
);
710 if (oclass
!= V_ASN1_UNIVERSAL
)
711 utype
= V_ASN1_OTHER
;
715 aclass
= V_ASN1_UNIVERSAL
;
719 ret
= asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, &cst
,
720 &p
, inlen
, tag
, aclass
, opt
, ctx
);
722 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_NESTED_ASN1_ERROR
);
724 } else if (ret
== -1)
727 /* SEQUENCE, SET and "OTHER" are left in encoded form */
728 if ((utype
== V_ASN1_SEQUENCE
)
729 || (utype
== V_ASN1_SET
) || (utype
== V_ASN1_OTHER
)) {
731 * Clear context cache for type OTHER because the auto clear when we
732 * have a exact match wont work
734 if (utype
== V_ASN1_OTHER
) {
737 /* SEQUENCE and SET must be constructed */
739 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
740 ASN1_R_TYPE_NOT_CONSTRUCTED
);
745 /* If indefinite length constructed find the real end */
747 if (!asn1_find_end(&p
, plen
, inf
))
751 len
= p
- cont
+ plen
;
755 if (utype
== V_ASN1_NULL
|| utype
== V_ASN1_BOOLEAN
756 || utype
== V_ASN1_OBJECT
|| utype
== V_ASN1_INTEGER
757 || utype
== V_ASN1_ENUMERATED
) {
758 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_TYPE_NOT_PRIMITIVE
);
762 /* Free any returned 'buf' content */
765 * Should really check the internal tags are correct but some things
766 * may get this wrong. The relevant specs say that constructed string
767 * types should be OCTET STRINGs internally irrespective of the type.
768 * So instead just check for UNIVERSAL class and ignore the tag.
770 if (!asn1_collect(&buf
, &p
, plen
, inf
, -1, V_ASN1_UNIVERSAL
, 0)) {
774 /* Append a final null to string */
775 if (!BUF_MEM_grow_clean(&buf
, len
+ 1)) {
776 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_MALLOC_FAILURE
);
780 cont
= (const unsigned char *)buf
.data
;
787 /* We now have content length and type: translate into a structure */
788 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
789 if (!asn1_ex_c2i(pval
, cont
, len
, utype
, &free_cont
, it
))
796 OPENSSL_free(buf
.data
);
800 /* Translate ASN1 content octets into a structure */
802 static int asn1_ex_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
803 int utype
, char *free_cont
, const ASN1_ITEM
*it
)
805 ASN1_VALUE
**opval
= NULL
;
807 ASN1_TYPE
*typ
= NULL
;
809 const ASN1_PRIMITIVE_FUNCS
*pf
;
813 if (pf
&& pf
->prim_c2i
)
814 return pf
->prim_c2i(pval
, cont
, len
, utype
, free_cont
, it
);
815 /* If ANY type clear type and set pointer to internal value */
816 if (it
->utype
== V_ASN1_ANY
) {
818 typ
= ASN1_TYPE_new();
821 *pval
= (ASN1_VALUE
*)typ
;
823 typ
= (ASN1_TYPE
*)*pval
;
825 if (utype
!= typ
->type
)
826 ASN1_TYPE_set(typ
, utype
, NULL
);
828 pval
= &typ
->value
.asn1_value
;
832 if (!c2i_ASN1_OBJECT((ASN1_OBJECT
**)pval
, &cont
, len
))
838 ASN1err(ASN1_F_ASN1_EX_C2I
, ASN1_R_NULL_IS_WRONG_LENGTH
);
841 *pval
= (ASN1_VALUE
*)1;
846 ASN1err(ASN1_F_ASN1_EX_C2I
, ASN1_R_BOOLEAN_IS_WRONG_LENGTH
);
850 tbool
= (ASN1_BOOLEAN
*)pval
;
855 case V_ASN1_BIT_STRING
:
856 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING
**)pval
, &cont
, len
))
861 case V_ASN1_NEG_INTEGER
:
862 case V_ASN1_ENUMERATED
:
863 case V_ASN1_NEG_ENUMERATED
:
864 tint
= (ASN1_INTEGER
**)pval
;
865 if (!c2i_ASN1_INTEGER(tint
, &cont
, len
))
867 /* Fixup type to match the expected form */
868 (*tint
)->type
= utype
| ((*tint
)->type
& V_ASN1_NEG
);
871 case V_ASN1_OCTET_STRING
:
872 case V_ASN1_NUMERICSTRING
:
873 case V_ASN1_PRINTABLESTRING
:
874 case V_ASN1_T61STRING
:
875 case V_ASN1_VIDEOTEXSTRING
:
876 case V_ASN1_IA5STRING
:
878 case V_ASN1_GENERALIZEDTIME
:
879 case V_ASN1_GRAPHICSTRING
:
880 case V_ASN1_VISIBLESTRING
:
881 case V_ASN1_GENERALSTRING
:
882 case V_ASN1_UNIVERSALSTRING
:
883 case V_ASN1_BMPSTRING
:
884 case V_ASN1_UTF8STRING
:
887 case V_ASN1_SEQUENCE
:
889 if (utype
== V_ASN1_BMPSTRING
&& (len
& 1)) {
890 ASN1err(ASN1_F_ASN1_EX_C2I
, ASN1_R_BMPSTRING_IS_WRONG_LENGTH
);
893 if (utype
== V_ASN1_UNIVERSALSTRING
&& (len
& 3)) {
894 ASN1err(ASN1_F_ASN1_EX_C2I
,
895 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH
);
898 /* All based on ASN1_STRING and handled the same */
900 stmp
= ASN1_STRING_type_new(utype
);
902 ASN1err(ASN1_F_ASN1_EX_C2I
, ERR_R_MALLOC_FAILURE
);
905 *pval
= (ASN1_VALUE
*)stmp
;
907 stmp
= (ASN1_STRING
*)*pval
;
910 /* If we've already allocated a buffer use it */
912 OPENSSL_free(stmp
->data
);
913 stmp
->data
= (unsigned char *)cont
; /* UGLY CAST! RL */
917 if (!ASN1_STRING_set(stmp
, cont
, len
)) {
918 ASN1err(ASN1_F_ASN1_EX_C2I
, ERR_R_MALLOC_FAILURE
);
919 ASN1_STRING_free(stmp
);
926 /* If ASN1_ANY and NULL type fix up value */
927 if (typ
&& (utype
== V_ASN1_NULL
))
928 typ
->value
.ptr
= NULL
;
941 * This function finds the end of an ASN1 structure when passed its maximum
942 * length, whether it is indefinite length and a pointer to the content. This
943 * is more efficient than calling asn1_collect because it does not recurse on
944 * each indefinite length header.
947 static int asn1_find_end(const unsigned char **in
, long len
, char inf
)
951 const unsigned char *p
= *in
, *q
;
952 /* If not indefinite length constructed just add length */
959 * Indefinite length constructed form. Find the end when enough EOCs are
960 * found. If more indefinite length constructed headers are encountered
961 * increment the expected eoc count otherwise just skip to the end of the
965 if (asn1_check_eoc(&p
, len
)) {
967 if (expected_eoc
== 0)
973 /* Just read in a header: only care about the length */
974 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, NULL
, &p
, len
,
976 ASN1err(ASN1_F_ASN1_FIND_END
, ERR_R_NESTED_ASN1_ERROR
);
986 ASN1err(ASN1_F_ASN1_FIND_END
, ASN1_R_MISSING_EOC
);
994 * This function collects the asn1 data from a constructed string type into
995 * a buffer. The values of 'in' and 'len' should refer to the contents of the
996 * constructed type and 'inf' should be set if it is indefinite length.
999 #ifndef ASN1_MAX_STRING_NEST
1001 * This determines how many levels of recursion are permitted in ASN1 string
1002 * types. If it is not limited stack overflows can occur. If set to zero no
1003 * recursion is allowed at all. Although zero should be adequate examples
1004 * exist that require a value of 1. So 5 should be more than enough.
1006 # define ASN1_MAX_STRING_NEST 5
1009 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
1010 char inf
, int tag
, int aclass
, int depth
)
1012 const unsigned char *p
, *q
;
1018 * If no buffer and not indefinite length constructed just pass over the
1028 if (asn1_check_eoc(&p
, len
)) {
1030 * EOC is illegal outside indefinite length constructed form
1033 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_UNEXPECTED_EOC
);
1040 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &ininf
, &cst
, &p
,
1041 len
, tag
, aclass
, 0, NULL
)) {
1042 ASN1err(ASN1_F_ASN1_COLLECT
, ERR_R_NESTED_ASN1_ERROR
);
1046 /* If indefinite length constructed update max length */
1048 if (depth
>= ASN1_MAX_STRING_NEST
) {
1049 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_NESTED_ASN1_STRING
);
1052 if (!asn1_collect(buf
, &p
, plen
, ininf
, tag
, aclass
, depth
+ 1))
1054 } else if (plen
&& !collect_data(buf
, &p
, plen
))
1059 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_MISSING_EOC
);
1066 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
)
1071 if (!BUF_MEM_grow_clean(buf
, len
+ plen
)) {
1072 ASN1err(ASN1_F_COLLECT_DATA
, ERR_R_MALLOC_FAILURE
);
1075 memcpy(buf
->data
+ len
, *p
, plen
);
1081 /* Check for ASN1 EOC and swallow it if found */
1083 static int asn1_check_eoc(const unsigned char **in
, long len
)
1085 const unsigned char *p
;
1089 if (!p
[0] && !p
[1]) {
1097 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1098 * length for indefinite length constructed form, we don't know the exact
1099 * length but we can set an upper bound to the amount of data available minus
1100 * the header length just read.
1103 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
1104 char *inf
, char *cst
,
1105 const unsigned char **in
, long len
,
1106 int exptag
, int expclass
, char opt
, ASN1_TLC
*ctx
)
1111 const unsigned char *p
, *q
;
1115 if (ctx
&& ctx
->valid
) {
1118 pclass
= ctx
->pclass
;
1122 i
= ASN1_get_object(&p
, &plen
, &ptag
, &pclass
, len
);
1126 ctx
->pclass
= pclass
;
1128 ctx
->hdrlen
= p
- q
;
1131 * If definite length, and no error, length + header can't exceed
1132 * total amount of data available.
1134 if (!(i
& 0x81) && ((plen
+ ctx
->hdrlen
) > len
)) {
1135 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_TOO_LONG
);
1136 asn1_tlc_clear(ctx
);
1143 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_BAD_OBJECT_HEADER
);
1144 asn1_tlc_clear(ctx
);
1148 if ((exptag
!= ptag
) || (expclass
!= pclass
)) {
1150 * If type is OPTIONAL, not an error: indicate missing type.
1154 asn1_tlc_clear(ctx
);
1155 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_WRONG_TAG
);
1159 * We have a tag and class match: assume we are going to do something
1162 asn1_tlc_clear(ctx
);
1166 plen
= len
- (p
- q
);
1172 *cst
= i
& V_ASN1_CONSTRUCTED
;