2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/asn1.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/objects.h>
15 #include <openssl/buffer.h>
16 #include <openssl/err.h>
17 #include "internal/numbers.h"
18 #include "asn1_locl.h"
20 static int asn1_item_embed_d2i(ASN1_VALUE
**pval
, const unsigned char **in
,
21 long len
, const ASN1_ITEM
*it
,
22 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
);
24 static int asn1_check_eoc(const unsigned char **in
, long len
);
25 static int asn1_find_end(const unsigned char **in
, long len
, char inf
);
27 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
28 char inf
, int tag
, int aclass
, int depth
);
30 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
);
32 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
34 const unsigned char **in
, long len
,
35 int exptag
, int expclass
, char opt
, ASN1_TLC
*ctx
);
37 static int asn1_template_ex_d2i(ASN1_VALUE
**pval
,
38 const unsigned char **in
, long len
,
39 const ASN1_TEMPLATE
*tt
, char opt
,
41 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
42 const unsigned char **in
, long len
,
43 const ASN1_TEMPLATE
*tt
, char opt
,
45 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
46 const unsigned char **in
, long len
,
48 int tag
, int aclass
, char opt
,
50 static int asn1_ex_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
51 int utype
, char *free_cont
, const ASN1_ITEM
*it
);
53 /* Table to convert tags to bit values, used for MSTRING type */
54 static const unsigned long tag2bit
[32] = {
56 0, 0, 0, B_ASN1_BIT_STRING
,
58 B_ASN1_OCTET_STRING
, 0, 0, B_ASN1_UNKNOWN
,
60 B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
,
62 B_ASN1_UTF8STRING
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
,
64 B_ASN1_SEQUENCE
, 0, B_ASN1_NUMERICSTRING
, B_ASN1_PRINTABLESTRING
,
66 B_ASN1_T61STRING
, B_ASN1_VIDEOTEXSTRING
, B_ASN1_IA5STRING
,
68 B_ASN1_UTCTIME
, B_ASN1_GENERALIZEDTIME
,
70 B_ASN1_GRAPHICSTRING
, B_ASN1_ISO64STRING
, B_ASN1_GENERALSTRING
,
72 B_ASN1_UNIVERSALSTRING
, B_ASN1_UNKNOWN
, B_ASN1_BMPSTRING
, B_ASN1_UNKNOWN
,
75 unsigned long ASN1_tag2bit(int tag
)
77 if ((tag
< 0) || (tag
> 30))
82 /* Macro to initialize and invalidate the cache */
84 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
85 /* Version to avoid compiler warning about 'c' always non-NULL */
86 #define asn1_tlc_clear_nc(c) (c)->valid = 0
89 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
90 * function. 'in' points to a buffer to read the data from, in future we
91 * will have more advanced versions that can input data a piece at a time and
92 * this will simply be a special case.
95 ASN1_VALUE
*ASN1_item_d2i(ASN1_VALUE
**pval
,
96 const unsigned char **in
, long len
,
100 ASN1_VALUE
*ptmpval
= NULL
;
103 asn1_tlc_clear_nc(&c
);
104 if (ASN1_item_ex_d2i(pval
, in
, len
, it
, -1, 0, 0, &c
) > 0)
109 int ASN1_item_ex_d2i(ASN1_VALUE
**pval
, const unsigned char **in
, long len
,
111 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
114 rv
= asn1_item_embed_d2i(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
);
116 ASN1_item_ex_free(pval
, it
);
121 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
122 * tag mismatch return -1 to handle OPTIONAL
125 static int asn1_item_embed_d2i(ASN1_VALUE
**pval
, const unsigned char **in
,
126 long len
, const ASN1_ITEM
*it
,
127 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
129 const ASN1_TEMPLATE
*tt
, *errtt
= NULL
;
130 const ASN1_EXTERN_FUNCS
*ef
;
131 const ASN1_AUX
*aux
= it
->funcs
;
132 ASN1_aux_cb
*asn1_cb
;
133 const unsigned char *p
= NULL
, *q
;
134 unsigned char oclass
;
135 char seq_eoc
, seq_nolen
, cst
, isopt
;
143 if (aux
&& aux
->asn1_cb
)
144 asn1_cb
= aux
->asn1_cb
;
149 case ASN1_ITYPE_PRIMITIVE
:
152 * tagging or OPTIONAL is currently illegal on an item template
153 * because the flags can't get passed down. In practice this
154 * isn't a problem: we include the relevant flags from the item
155 * template in the template itself.
157 if ((tag
!= -1) || opt
) {
158 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
,
159 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE
);
162 return asn1_template_ex_d2i(pval
, in
, len
,
163 it
->templates
, opt
, ctx
);
165 return asn1_d2i_ex_primitive(pval
, in
, len
, it
,
166 tag
, aclass
, opt
, ctx
);
168 case ASN1_ITYPE_MSTRING
:
170 /* Just read in tag and class */
171 ret
= asn1_check_tlen(NULL
, &otag
, &oclass
, NULL
, NULL
,
172 &p
, len
, -1, 0, 1, ctx
);
174 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
178 /* Must be UNIVERSAL class */
179 if (oclass
!= V_ASN1_UNIVERSAL
) {
180 /* If OPTIONAL, assume this is OK */
183 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_MSTRING_NOT_UNIVERSAL
);
186 /* Check tag matches bit map */
187 if (!(ASN1_tag2bit(otag
) & it
->utype
)) {
188 /* If OPTIONAL, assume this is OK */
191 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_MSTRING_WRONG_TAG
);
194 return asn1_d2i_ex_primitive(pval
, in
, len
, it
, otag
, 0, 0, ctx
);
196 case ASN1_ITYPE_EXTERN
:
197 /* Use new style d2i */
199 return ef
->asn1_ex_d2i(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
);
201 case ASN1_ITYPE_CHOICE
:
202 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
205 /* Free up and zero CHOICE value if initialised */
206 i
= asn1_get_choice_selector(pval
, it
);
207 if ((i
>= 0) && (i
< it
->tcount
)) {
208 tt
= it
->templates
+ i
;
209 pchptr
= asn1_get_field_ptr(pval
, tt
);
210 asn1_template_free(pchptr
, tt
);
211 asn1_set_choice_selector(pval
, -1, it
);
213 } else if (!ASN1_item_ex_new(pval
, it
)) {
214 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
217 /* CHOICE type, try each possibility in turn */
219 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
220 pchptr
= asn1_get_field_ptr(pval
, tt
);
222 * We mark field as OPTIONAL so its absence can be recognised.
224 ret
= asn1_template_ex_d2i(pchptr
, &p
, len
, tt
, 1, ctx
);
225 /* If field not present, try the next one */
229 * Set the choice selector here to ensure that the value is
230 * correctly freed upon error. It may be partially initialized
231 * even if parsing failed.
233 asn1_set_choice_selector(pval
, i
, it
);
234 /* If positive return, read OK, break loop */
237 /* Otherwise must be an ASN1 parsing error */
239 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
243 /* Did we fall off the end without reading anything? */
244 if (i
== it
->tcount
) {
245 /* If OPTIONAL, this is OK */
247 /* Free and zero it */
248 ASN1_item_ex_free(pval
, it
);
251 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_NO_MATCHING_CHOICE_TYPE
);
255 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
260 case ASN1_ITYPE_NDEF_SEQUENCE
:
261 case ASN1_ITYPE_SEQUENCE
:
265 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
267 tag
= V_ASN1_SEQUENCE
;
268 aclass
= V_ASN1_UNIVERSAL
;
270 /* Get SEQUENCE length and update len, p */
271 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &seq_eoc
, &cst
,
272 &p
, len
, tag
, aclass
, opt
, ctx
);
274 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
276 } else if (ret
== -1)
278 if (aux
&& (aux
->flags
& ASN1_AFLG_BROKEN
)) {
279 len
= tmplen
- (p
- *in
);
282 /* If indefinite we don't do a length check */
286 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_SEQUENCE_NOT_CONSTRUCTED
);
290 if (!*pval
&& !ASN1_item_ex_new(pval
, it
)) {
291 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ERR_R_NESTED_ASN1_ERROR
);
295 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
298 /* Free up and zero any ADB found */
299 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
300 if (tt
->flags
& ASN1_TFLG_ADB_MASK
) {
301 const ASN1_TEMPLATE
*seqtt
;
302 ASN1_VALUE
**pseqval
;
303 seqtt
= asn1_do_adb(pval
, tt
, 1);
304 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
305 asn1_template_free(pseqval
, seqtt
);
309 /* Get each field entry */
310 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
311 const ASN1_TEMPLATE
*seqtt
;
312 ASN1_VALUE
**pseqval
;
313 seqtt
= asn1_do_adb(pval
, tt
, 1);
316 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
317 /* Have we ran out of data? */
321 if (asn1_check_eoc(&p
, len
)) {
323 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_UNEXPECTED_EOC
);
332 * This determines the OPTIONAL flag value. The field cannot be
333 * omitted if it is the last of a SEQUENCE and there is still
334 * data to be read. This isn't strictly necessary but it
335 * increases efficiency in some cases.
337 if (i
== (it
->tcount
- 1))
340 isopt
= (char)(seqtt
->flags
& ASN1_TFLG_OPTIONAL
);
342 * attempt to read in field, allowing each to be OPTIONAL
345 ret
= asn1_template_ex_d2i(pseqval
, &p
, len
, seqtt
, isopt
, ctx
);
349 } else if (ret
== -1) {
351 * OPTIONAL component absent. Free and zero the field.
353 asn1_template_free(pseqval
, seqtt
);
360 /* Check for EOC if expecting one */
361 if (seq_eoc
&& !asn1_check_eoc(&p
, len
)) {
362 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_MISSING_EOC
);
365 /* Check all data read */
366 if (!seq_nolen
&& len
) {
367 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_SEQUENCE_LENGTH_MISMATCH
);
372 * If we get here we've got no more data in the SEQUENCE, however we
373 * may not have read all fields so check all remaining are OPTIONAL
374 * and clear any that are.
376 for (; i
< it
->tcount
; tt
++, i
++) {
377 const ASN1_TEMPLATE
*seqtt
;
378 seqtt
= asn1_do_adb(pval
, tt
, 1);
381 if (seqtt
->flags
& ASN1_TFLG_OPTIONAL
) {
382 ASN1_VALUE
**pseqval
;
383 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
384 asn1_template_free(pseqval
, seqtt
);
387 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_FIELD_MISSING
);
392 if (!asn1_enc_save(pval
, *in
, p
- *in
, it
))
394 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
403 ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I
, ASN1_R_AUX_ERROR
);
406 ERR_add_error_data(4, "Field=", errtt
->field_name
,
407 ", Type=", it
->sname
);
409 ERR_add_error_data(2, "Type=", it
->sname
);
414 * Templates are handled with two separate functions. One handles any
415 * EXPLICIT tag and the other handles the rest.
418 static int asn1_template_ex_d2i(ASN1_VALUE
**val
,
419 const unsigned char **in
, long inlen
,
420 const ASN1_TEMPLATE
*tt
, char opt
,
426 const unsigned char *p
, *q
;
431 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
435 /* Check if EXPLICIT tag expected */
436 if (flags
& ASN1_TFLG_EXPTAG
) {
439 * Need to work out amount of data available to the inner content and
440 * where it starts: so read in EXPLICIT header to get the info.
442 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &exp_eoc
, &cst
,
443 &p
, inlen
, tt
->tag
, aclass
, opt
, ctx
);
446 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
448 } else if (ret
== -1)
451 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
452 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED
);
455 /* We've found the field so it can't be OPTIONAL now */
456 ret
= asn1_template_noexp_d2i(val
, &p
, len
, tt
, 0, ctx
);
458 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
461 /* We read the field in OK so update length */
464 /* If NDEF we must have an EOC here */
465 if (!asn1_check_eoc(&p
, len
)) {
466 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
, ASN1_R_MISSING_EOC
);
471 * Otherwise we must hit the EXPLICIT tag end or its an error
474 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I
,
475 ASN1_R_EXPLICIT_LENGTH_MISMATCH
);
480 return asn1_template_noexp_d2i(val
, in
, inlen
, tt
, opt
, ctx
);
489 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
490 const unsigned char **in
, long len
,
491 const ASN1_TEMPLATE
*tt
, char opt
,
497 const unsigned char *p
, *q
;
501 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
507 * If field is embedded then val needs fixing so it is a pointer to
508 * a pointer to a field.
510 if (tt
->flags
& ASN1_TFLG_EMBED
) {
511 tval
= (ASN1_VALUE
*)val
;
515 if (flags
& ASN1_TFLG_SK_MASK
) {
516 /* SET OF, SEQUENCE OF */
519 /* First work out expected inner tag value */
520 if (flags
& ASN1_TFLG_IMPTAG
) {
524 skaclass
= V_ASN1_UNIVERSAL
;
525 if (flags
& ASN1_TFLG_SET_OF
)
528 sktag
= V_ASN1_SEQUENCE
;
531 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &sk_eoc
, NULL
,
532 &p
, len
, sktag
, skaclass
, opt
, ctx
);
534 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_NESTED_ASN1_ERROR
);
536 } else if (ret
== -1)
539 *val
= (ASN1_VALUE
*)OPENSSL_sk_new_null();
542 * We've got a valid STACK: free up any items present
544 STACK_OF(ASN1_VALUE
) *sktmp
= (STACK_OF(ASN1_VALUE
) *)*val
;
546 while (sk_ASN1_VALUE_num(sktmp
) > 0) {
547 vtmp
= sk_ASN1_VALUE_pop(sktmp
);
548 ASN1_item_ex_free(&vtmp
, ASN1_ITEM_ptr(tt
->item
));
553 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_MALLOC_FAILURE
);
557 /* Read as many items as we can */
561 /* See if EOC found */
562 if (asn1_check_eoc(&p
, len
)) {
564 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
565 ASN1_R_UNEXPECTED_EOC
);
573 if (!asn1_item_embed_d2i(&skfield
, &p
, len
,
574 ASN1_ITEM_ptr(tt
->item
), -1, 0, 0, ctx
)) {
575 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
,
576 ERR_R_NESTED_ASN1_ERROR
);
577 /* |skfield| may be partially allocated despite failure. */
578 ASN1_item_free(skfield
, ASN1_ITEM_ptr(tt
->item
));
582 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE
) *)*val
, skfield
)) {
583 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_MALLOC_FAILURE
);
584 ASN1_item_free(skfield
, ASN1_ITEM_ptr(tt
->item
));
589 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ASN1_R_MISSING_EOC
);
592 } else if (flags
& ASN1_TFLG_IMPTAG
) {
593 /* IMPLICIT tagging */
594 ret
= asn1_item_embed_d2i(val
, &p
, len
,
595 ASN1_ITEM_ptr(tt
->item
), tt
->tag
, aclass
, opt
,
598 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_NESTED_ASN1_ERROR
);
600 } else if (ret
== -1)
603 /* Nothing special */
604 ret
= asn1_item_embed_d2i(val
, &p
, len
, ASN1_ITEM_ptr(tt
->item
),
607 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I
, ERR_R_NESTED_ASN1_ERROR
);
609 } else if (ret
== -1)
620 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
621 const unsigned char **in
, long inlen
,
623 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
627 char cst
, inf
, free_cont
= 0;
628 const unsigned char *p
;
629 BUF_MEM buf
= { 0, NULL
, 0, 0 };
630 const unsigned char *cont
= NULL
;
633 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_ILLEGAL_NULL
);
634 return 0; /* Should never happen */
637 if (it
->itype
== ASN1_ITYPE_MSTRING
) {
643 if (utype
== V_ASN1_ANY
) {
644 /* If type is ANY need to figure out type from tag */
645 unsigned char oclass
;
647 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_ILLEGAL_TAGGED_ANY
);
651 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
652 ASN1_R_ILLEGAL_OPTIONAL_ANY
);
656 ret
= asn1_check_tlen(NULL
, &utype
, &oclass
, NULL
, NULL
,
657 &p
, inlen
, -1, 0, 0, ctx
);
659 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_NESTED_ASN1_ERROR
);
662 if (oclass
!= V_ASN1_UNIVERSAL
)
663 utype
= V_ASN1_OTHER
;
667 aclass
= V_ASN1_UNIVERSAL
;
671 ret
= asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, &cst
,
672 &p
, inlen
, tag
, aclass
, opt
, ctx
);
674 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_NESTED_ASN1_ERROR
);
676 } else if (ret
== -1)
679 /* SEQUENCE, SET and "OTHER" are left in encoded form */
680 if ((utype
== V_ASN1_SEQUENCE
)
681 || (utype
== V_ASN1_SET
) || (utype
== V_ASN1_OTHER
)) {
683 * Clear context cache for type OTHER because the auto clear when we
684 * have a exact match wont work
686 if (utype
== V_ASN1_OTHER
) {
689 /* SEQUENCE and SET must be constructed */
691 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
,
692 ASN1_R_TYPE_NOT_CONSTRUCTED
);
697 /* If indefinite length constructed find the real end */
699 if (!asn1_find_end(&p
, plen
, inf
))
703 len
= p
- cont
+ plen
;
707 if (utype
== V_ASN1_NULL
|| utype
== V_ASN1_BOOLEAN
708 || utype
== V_ASN1_OBJECT
|| utype
== V_ASN1_INTEGER
709 || utype
== V_ASN1_ENUMERATED
) {
710 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ASN1_R_TYPE_NOT_PRIMITIVE
);
714 /* Free any returned 'buf' content */
717 * Should really check the internal tags are correct but some things
718 * may get this wrong. The relevant specs say that constructed string
719 * types should be OCTET STRINGs internally irrespective of the type.
720 * So instead just check for UNIVERSAL class and ignore the tag.
722 if (!asn1_collect(&buf
, &p
, plen
, inf
, -1, V_ASN1_UNIVERSAL
, 0)) {
726 /* Append a final null to string */
727 if (!BUF_MEM_grow_clean(&buf
, len
+ 1)) {
728 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE
, ERR_R_MALLOC_FAILURE
);
732 cont
= (const unsigned char *)buf
.data
;
739 /* We now have content length and type: translate into a structure */
740 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
741 if (!asn1_ex_c2i(pval
, cont
, len
, utype
, &free_cont
, it
))
748 OPENSSL_free(buf
.data
);
752 /* Translate ASN1 content octets into a structure */
754 static int asn1_ex_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
755 int utype
, char *free_cont
, const ASN1_ITEM
*it
)
757 ASN1_VALUE
**opval
= NULL
;
759 ASN1_TYPE
*typ
= NULL
;
761 const ASN1_PRIMITIVE_FUNCS
*pf
;
765 if (pf
&& pf
->prim_c2i
)
766 return pf
->prim_c2i(pval
, cont
, len
, utype
, free_cont
, it
);
767 /* If ANY type clear type and set pointer to internal value */
768 if (it
->utype
== V_ASN1_ANY
) {
770 typ
= ASN1_TYPE_new();
773 *pval
= (ASN1_VALUE
*)typ
;
775 typ
= (ASN1_TYPE
*)*pval
;
777 if (utype
!= typ
->type
)
778 ASN1_TYPE_set(typ
, utype
, NULL
);
780 pval
= &typ
->value
.asn1_value
;
784 if (!c2i_ASN1_OBJECT((ASN1_OBJECT
**)pval
, &cont
, len
))
790 ASN1err(ASN1_F_ASN1_EX_C2I
, ASN1_R_NULL_IS_WRONG_LENGTH
);
793 *pval
= (ASN1_VALUE
*)1;
798 ASN1err(ASN1_F_ASN1_EX_C2I
, ASN1_R_BOOLEAN_IS_WRONG_LENGTH
);
802 tbool
= (ASN1_BOOLEAN
*)pval
;
807 case V_ASN1_BIT_STRING
:
808 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING
**)pval
, &cont
, len
))
813 case V_ASN1_ENUMERATED
:
814 tint
= (ASN1_INTEGER
**)pval
;
815 if (!c2i_ASN1_INTEGER(tint
, &cont
, len
))
817 /* Fixup type to match the expected form */
818 (*tint
)->type
= utype
| ((*tint
)->type
& V_ASN1_NEG
);
821 case V_ASN1_OCTET_STRING
:
822 case V_ASN1_NUMERICSTRING
:
823 case V_ASN1_PRINTABLESTRING
:
824 case V_ASN1_T61STRING
:
825 case V_ASN1_VIDEOTEXSTRING
:
826 case V_ASN1_IA5STRING
:
828 case V_ASN1_GENERALIZEDTIME
:
829 case V_ASN1_GRAPHICSTRING
:
830 case V_ASN1_VISIBLESTRING
:
831 case V_ASN1_GENERALSTRING
:
832 case V_ASN1_UNIVERSALSTRING
:
833 case V_ASN1_BMPSTRING
:
834 case V_ASN1_UTF8STRING
:
837 case V_ASN1_SEQUENCE
:
839 if (utype
== V_ASN1_BMPSTRING
&& (len
& 1)) {
840 ASN1err(ASN1_F_ASN1_EX_C2I
, ASN1_R_BMPSTRING_IS_WRONG_LENGTH
);
843 if (utype
== V_ASN1_UNIVERSALSTRING
&& (len
& 3)) {
844 ASN1err(ASN1_F_ASN1_EX_C2I
,
845 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH
);
848 /* All based on ASN1_STRING and handled the same */
850 stmp
= ASN1_STRING_type_new(utype
);
852 ASN1err(ASN1_F_ASN1_EX_C2I
, ERR_R_MALLOC_FAILURE
);
855 *pval
= (ASN1_VALUE
*)stmp
;
857 stmp
= (ASN1_STRING
*)*pval
;
860 /* If we've already allocated a buffer use it */
862 OPENSSL_free(stmp
->data
);
863 stmp
->data
= (unsigned char *)cont
; /* UGLY CAST! RL */
867 if (!ASN1_STRING_set(stmp
, cont
, len
)) {
868 ASN1err(ASN1_F_ASN1_EX_C2I
, ERR_R_MALLOC_FAILURE
);
869 ASN1_STRING_free(stmp
);
876 /* If ASN1_ANY and NULL type fix up value */
877 if (typ
&& (utype
== V_ASN1_NULL
))
878 typ
->value
.ptr
= NULL
;
891 * This function finds the end of an ASN1 structure when passed its maximum
892 * length, whether it is indefinite length and a pointer to the content. This
893 * is more efficient than calling asn1_collect because it does not recurse on
894 * each indefinite length header.
897 static int asn1_find_end(const unsigned char **in
, long len
, char inf
)
899 uint32_t expected_eoc
;
901 const unsigned char *p
= *in
, *q
;
902 /* If not indefinite length constructed just add length */
909 * Indefinite length constructed form. Find the end when enough EOCs are
910 * found. If more indefinite length constructed headers are encountered
911 * increment the expected eoc count otherwise just skip to the end of the
915 if (asn1_check_eoc(&p
, len
)) {
917 if (expected_eoc
== 0)
923 /* Just read in a header: only care about the length */
924 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, NULL
, &p
, len
,
926 ASN1err(ASN1_F_ASN1_FIND_END
, ERR_R_NESTED_ASN1_ERROR
);
930 if (expected_eoc
== UINT32_MAX
) {
931 ASN1err(ASN1_F_ASN1_FIND_END
, ERR_R_NESTED_ASN1_ERROR
);
941 ASN1err(ASN1_F_ASN1_FIND_END
, ASN1_R_MISSING_EOC
);
949 * This function collects the asn1 data from a constructed string type into
950 * a buffer. The values of 'in' and 'len' should refer to the contents of the
951 * constructed type and 'inf' should be set if it is indefinite length.
954 #ifndef ASN1_MAX_STRING_NEST
956 * This determines how many levels of recursion are permitted in ASN1 string
957 * types. If it is not limited stack overflows can occur. If set to zero no
958 * recursion is allowed at all. Although zero should be adequate examples
959 * exist that require a value of 1. So 5 should be more than enough.
961 # define ASN1_MAX_STRING_NEST 5
964 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
965 char inf
, int tag
, int aclass
, int depth
)
967 const unsigned char *p
, *q
;
973 * If no buffer and not indefinite length constructed just pass over the
983 if (asn1_check_eoc(&p
, len
)) {
985 * EOC is illegal outside indefinite length constructed form
988 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_UNEXPECTED_EOC
);
995 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &ininf
, &cst
, &p
,
996 len
, tag
, aclass
, 0, NULL
)) {
997 ASN1err(ASN1_F_ASN1_COLLECT
, ERR_R_NESTED_ASN1_ERROR
);
1001 /* If indefinite length constructed update max length */
1003 if (depth
>= ASN1_MAX_STRING_NEST
) {
1004 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_NESTED_ASN1_STRING
);
1007 if (!asn1_collect(buf
, &p
, plen
, ininf
, tag
, aclass
, depth
+ 1))
1009 } else if (plen
&& !collect_data(buf
, &p
, plen
))
1014 ASN1err(ASN1_F_ASN1_COLLECT
, ASN1_R_MISSING_EOC
);
1021 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
)
1026 if (!BUF_MEM_grow_clean(buf
, len
+ plen
)) {
1027 ASN1err(ASN1_F_COLLECT_DATA
, ERR_R_MALLOC_FAILURE
);
1030 memcpy(buf
->data
+ len
, *p
, plen
);
1036 /* Check for ASN1 EOC and swallow it if found */
1038 static int asn1_check_eoc(const unsigned char **in
, long len
)
1040 const unsigned char *p
;
1044 if (!p
[0] && !p
[1]) {
1052 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1053 * length for indefinite length constructed form, we don't know the exact
1054 * length but we can set an upper bound to the amount of data available minus
1055 * the header length just read.
1058 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
1059 char *inf
, char *cst
,
1060 const unsigned char **in
, long len
,
1061 int exptag
, int expclass
, char opt
, ASN1_TLC
*ctx
)
1066 const unsigned char *p
, *q
;
1070 if (ctx
&& ctx
->valid
) {
1073 pclass
= ctx
->pclass
;
1077 i
= ASN1_get_object(&p
, &plen
, &ptag
, &pclass
, len
);
1081 ctx
->pclass
= pclass
;
1083 ctx
->hdrlen
= p
- q
;
1086 * If definite length, and no error, length + header can't exceed
1087 * total amount of data available.
1089 if (!(i
& 0x81) && ((plen
+ ctx
->hdrlen
) > len
)) {
1090 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_TOO_LONG
);
1091 asn1_tlc_clear(ctx
);
1098 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_BAD_OBJECT_HEADER
);
1099 asn1_tlc_clear(ctx
);
1103 if ((exptag
!= ptag
) || (expclass
!= pclass
)) {
1105 * If type is OPTIONAL, not an error: indicate missing type.
1109 asn1_tlc_clear(ctx
);
1110 ASN1err(ASN1_F_ASN1_CHECK_TLEN
, ASN1_R_WRONG_TAG
);
1114 * We have a tag and class match: assume we are going to do something
1117 asn1_tlc_clear(ctx
);
1121 plen
= len
- (p
- q
);
1127 *cst
= i
& V_ASN1_CONSTRUCTED
;