2 * Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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_local.h"
21 * Constructed types with a recursive definition (such as can be found in PKCS7)
22 * could eventually exceed the stack given malicious input with excessive
23 * recursion. Therefore we limit the stack depth. This is the maximum number of
24 * recursive invocations of asn1_item_embed_d2i().
26 #define ASN1_MAX_CONSTRUCTED_NEST 30
28 static int asn1_item_embed_d2i(ASN1_VALUE
**pval
, const unsigned char **in
,
29 long len
, const ASN1_ITEM
*it
,
30 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
,
31 int depth
, OSSL_LIB_CTX
*libctx
,
34 static int asn1_check_eoc(const unsigned char **in
, long len
);
35 static int asn1_find_end(const unsigned char **in
, long len
, char inf
);
37 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
38 char inf
, int tag
, int aclass
, int depth
);
40 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
);
42 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
44 const unsigned char **in
, long len
,
45 int exptag
, int expclass
, char opt
, ASN1_TLC
*ctx
);
47 static int asn1_template_ex_d2i(ASN1_VALUE
**pval
,
48 const unsigned char **in
, long len
,
49 const ASN1_TEMPLATE
*tt
, char opt
,
50 ASN1_TLC
*ctx
, int depth
, OSSL_LIB_CTX
*libctx
,
52 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
53 const unsigned char **in
, long len
,
54 const ASN1_TEMPLATE
*tt
, char opt
,
55 ASN1_TLC
*ctx
, int depth
,
56 OSSL_LIB_CTX
*libctx
, const char *propq
);
57 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
58 const unsigned char **in
, long len
,
60 int tag
, int aclass
, char opt
,
62 static int asn1_ex_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
63 int utype
, char *free_cont
, const ASN1_ITEM
*it
);
65 /* Table to convert tags to bit values, used for MSTRING type */
66 static const unsigned long tag2bit
[32] = {
68 0, 0, 0, B_ASN1_BIT_STRING
,
70 B_ASN1_OCTET_STRING
, 0, 0, B_ASN1_UNKNOWN
,
72 B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, 0, B_ASN1_UNKNOWN
,
74 B_ASN1_UTF8STRING
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
, B_ASN1_UNKNOWN
,
76 B_ASN1_SEQUENCE
, 0, B_ASN1_NUMERICSTRING
, B_ASN1_PRINTABLESTRING
,
78 B_ASN1_T61STRING
, B_ASN1_VIDEOTEXSTRING
, B_ASN1_IA5STRING
,
80 B_ASN1_UTCTIME
, B_ASN1_GENERALIZEDTIME
,
82 B_ASN1_GRAPHICSTRING
, B_ASN1_ISO64STRING
, B_ASN1_GENERALSTRING
,
84 B_ASN1_UNIVERSALSTRING
, B_ASN1_UNKNOWN
, B_ASN1_BMPSTRING
, B_ASN1_UNKNOWN
,
87 unsigned long ASN1_tag2bit(int tag
)
89 if ((tag
< 0) || (tag
> 30))
94 /* Macro to initialize and invalidate the cache */
96 #define asn1_tlc_clear(c) do { if ((c) != NULL) (c)->valid = 0; } while (0)
97 /* Version to avoid compiler warning about 'c' always non-NULL */
98 #define asn1_tlc_clear_nc(c) do {(c)->valid = 0; } while (0)
101 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
102 * function. 'in' points to a buffer to read the data from, in future we
103 * will have more advanced versions that can input data a piece at a time and
104 * this will simply be a special case.
107 static int asn1_item_ex_d2i_intern(ASN1_VALUE
**pval
, const unsigned char **in
,
108 long len
, const ASN1_ITEM
*it
, int tag
,
109 int aclass
, char opt
, ASN1_TLC
*ctx
,
110 OSSL_LIB_CTX
*libctx
, const char *propq
)
114 if (pval
== NULL
|| it
== NULL
) {
115 ERR_raise(ERR_LIB_ASN1
, ERR_R_PASSED_NULL_PARAMETER
);
118 rv
= asn1_item_embed_d2i(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
, 0,
121 ASN1_item_ex_free(pval
, it
);
125 int ASN1_item_ex_d2i(ASN1_VALUE
**pval
, const unsigned char **in
, long len
,
127 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
129 return asn1_item_ex_d2i_intern(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
,
133 ASN1_VALUE
*ASN1_item_d2i_ex(ASN1_VALUE
**pval
,
134 const unsigned char **in
, long len
,
135 const ASN1_ITEM
*it
, OSSL_LIB_CTX
*libctx
,
139 ASN1_VALUE
*ptmpval
= NULL
;
143 asn1_tlc_clear_nc(&c
);
144 if (asn1_item_ex_d2i_intern(pval
, in
, len
, it
, -1, 0, 0, &c
, libctx
,
150 ASN1_VALUE
*ASN1_item_d2i(ASN1_VALUE
**pval
,
151 const unsigned char **in
, long len
,
154 return ASN1_item_d2i_ex(pval
, in
, len
, it
, NULL
, NULL
);
158 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
159 * tag mismatch return -1 to handle OPTIONAL
162 static int asn1_item_embed_d2i(ASN1_VALUE
**pval
, const unsigned char **in
,
163 long len
, const ASN1_ITEM
*it
,
164 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
,
165 int depth
, OSSL_LIB_CTX
*libctx
,
168 const ASN1_TEMPLATE
*tt
, *errtt
= NULL
;
169 const ASN1_EXTERN_FUNCS
*ef
;
171 ASN1_aux_cb
*asn1_cb
;
172 const unsigned char *p
= NULL
, *q
;
173 unsigned char oclass
;
174 char seq_eoc
, seq_nolen
, cst
, isopt
;
181 if (pval
== NULL
|| it
== NULL
) {
182 ERR_raise(ERR_LIB_ASN1
, ERR_R_PASSED_NULL_PARAMETER
);
186 ERR_raise(ERR_LIB_ASN1
, ASN1_R_TOO_SMALL
);
190 if (aux
&& aux
->asn1_cb
)
191 asn1_cb
= aux
->asn1_cb
;
195 if (++depth
> ASN1_MAX_CONSTRUCTED_NEST
) {
196 ERR_raise(ERR_LIB_ASN1
, ASN1_R_NESTED_TOO_DEEP
);
201 case ASN1_ITYPE_PRIMITIVE
:
204 * tagging or OPTIONAL is currently illegal on an item template
205 * because the flags can't get passed down. In practice this
206 * isn't a problem: we include the relevant flags from the item
207 * template in the template itself.
209 if ((tag
!= -1) || opt
) {
210 ERR_raise(ERR_LIB_ASN1
,
211 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE
);
214 return asn1_template_ex_d2i(pval
, in
, len
, it
->templates
, opt
, ctx
,
215 depth
, libctx
, propq
);
217 return asn1_d2i_ex_primitive(pval
, in
, len
, it
,
218 tag
, aclass
, opt
, ctx
);
220 case ASN1_ITYPE_MSTRING
:
222 * It never makes sense for multi-strings to have implicit tagging, so
223 * if tag != -1, then this looks like an error in the template.
226 ERR_raise(ERR_LIB_ASN1
, ASN1_R_BAD_TEMPLATE
);
231 /* Just read in tag and class */
232 ret
= asn1_check_tlen(NULL
, &otag
, &oclass
, NULL
, NULL
,
233 &p
, len
, -1, 0, 1, ctx
);
235 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
239 /* Must be UNIVERSAL class */
240 if (oclass
!= V_ASN1_UNIVERSAL
) {
241 /* If OPTIONAL, assume this is OK */
244 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MSTRING_NOT_UNIVERSAL
);
248 /* Check tag matches bit map */
249 if (!(ASN1_tag2bit(otag
) & it
->utype
)) {
250 /* If OPTIONAL, assume this is OK */
253 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MSTRING_WRONG_TAG
);
256 return asn1_d2i_ex_primitive(pval
, in
, len
, it
, otag
, 0, 0, ctx
);
258 case ASN1_ITYPE_EXTERN
:
259 /* Use new style d2i */
261 if (ef
->asn1_ex_d2i_ex
!= NULL
)
262 return ef
->asn1_ex_d2i_ex(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
,
264 return ef
->asn1_ex_d2i(pval
, in
, len
, it
, tag
, aclass
, opt
, ctx
);
266 case ASN1_ITYPE_CHOICE
:
268 * It never makes sense for CHOICE types to have implicit tagging, so
269 * if tag != -1, then this looks like an error in the template.
272 ERR_raise(ERR_LIB_ASN1
, ASN1_R_BAD_TEMPLATE
);
276 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
279 /* Free up and zero CHOICE value if initialised */
280 i
= ossl_asn1_get_choice_selector(pval
, it
);
281 if ((i
>= 0) && (i
< it
->tcount
)) {
282 tt
= it
->templates
+ i
;
283 pchptr
= ossl_asn1_get_field_ptr(pval
, tt
);
284 ossl_asn1_template_free(pchptr
, tt
);
285 ossl_asn1_set_choice_selector(pval
, -1, it
);
287 } else if (!ossl_asn1_item_ex_new_intern(pval
, it
, libctx
, propq
)) {
288 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
291 /* CHOICE type, try each possibility in turn */
293 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
294 pchptr
= ossl_asn1_get_field_ptr(pval
, tt
);
296 * We mark field as OPTIONAL so its absence can be recognised.
298 ret
= asn1_template_ex_d2i(pchptr
, &p
, len
, tt
, 1, ctx
, depth
,
300 /* If field not present, try the next one */
303 /* If positive return, read OK, break loop */
307 * Must be an ASN1 parsing error.
308 * Free up any partial choice value
310 ossl_asn1_template_free(pchptr
, tt
);
312 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
316 /* Did we fall off the end without reading anything? */
317 if (i
== it
->tcount
) {
318 /* If OPTIONAL, this is OK */
320 /* Free and zero it */
321 ASN1_item_ex_free(pval
, it
);
324 ERR_raise(ERR_LIB_ASN1
, ASN1_R_NO_MATCHING_CHOICE_TYPE
);
328 ossl_asn1_set_choice_selector(pval
, i
, it
);
330 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
335 case ASN1_ITYPE_NDEF_SEQUENCE
:
336 case ASN1_ITYPE_SEQUENCE
:
340 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
342 tag
= V_ASN1_SEQUENCE
;
343 aclass
= V_ASN1_UNIVERSAL
;
345 /* Get SEQUENCE length and update len, p */
346 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &seq_eoc
, &cst
,
347 &p
, len
, tag
, aclass
, opt
, ctx
);
349 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
351 } else if (ret
== -1)
353 if (aux
&& (aux
->flags
& ASN1_AFLG_BROKEN
)) {
354 len
= tmplen
- (p
- *in
);
357 /* If indefinite we don't do a length check */
361 ERR_raise(ERR_LIB_ASN1
, ASN1_R_SEQUENCE_NOT_CONSTRUCTED
);
366 && !ossl_asn1_item_ex_new_intern(pval
, it
, libctx
, propq
)) {
367 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
371 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_PRE
, pval
, it
, NULL
))
374 /* Free up and zero any ADB found */
375 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
376 if (tt
->flags
& ASN1_TFLG_ADB_MASK
) {
377 const ASN1_TEMPLATE
*seqtt
;
378 ASN1_VALUE
**pseqval
;
379 seqtt
= ossl_asn1_do_adb(*pval
, tt
, 0);
382 pseqval
= ossl_asn1_get_field_ptr(pval
, seqtt
);
383 ossl_asn1_template_free(pseqval
, seqtt
);
387 /* Get each field entry */
388 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; i
++, tt
++) {
389 const ASN1_TEMPLATE
*seqtt
;
390 ASN1_VALUE
**pseqval
;
391 seqtt
= ossl_asn1_do_adb(*pval
, tt
, 1);
394 pseqval
= ossl_asn1_get_field_ptr(pval
, seqtt
);
395 /* Have we ran out of data? */
399 if (asn1_check_eoc(&p
, len
)) {
401 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNEXPECTED_EOC
);
409 * This determines the OPTIONAL flag value. The field cannot be
410 * omitted if it is the last of a SEQUENCE and there is still
411 * data to be read. This isn't strictly necessary but it
412 * increases efficiency in some cases.
414 if (i
== (it
->tcount
- 1))
417 isopt
= (char)(seqtt
->flags
& ASN1_TFLG_OPTIONAL
);
419 * attempt to read in field, allowing each to be OPTIONAL
422 ret
= asn1_template_ex_d2i(pseqval
, &p
, len
, seqtt
, isopt
, ctx
,
423 depth
, libctx
, propq
);
427 } else if (ret
== -1) {
429 * OPTIONAL component absent. Free and zero the field.
431 ossl_asn1_template_free(pseqval
, seqtt
);
438 /* Check for EOC if expecting one */
439 if (seq_eoc
&& !asn1_check_eoc(&p
, len
)) {
440 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MISSING_EOC
);
443 /* Check all data read */
444 if (!seq_nolen
&& len
) {
445 ERR_raise(ERR_LIB_ASN1
, ASN1_R_SEQUENCE_LENGTH_MISMATCH
);
450 * If we get here we've got no more data in the SEQUENCE, however we
451 * may not have read all fields so check all remaining are OPTIONAL
452 * and clear any that are.
454 for (; i
< it
->tcount
; tt
++, i
++) {
455 const ASN1_TEMPLATE
*seqtt
;
456 seqtt
= ossl_asn1_do_adb(*pval
, tt
, 1);
459 if (seqtt
->flags
& ASN1_TFLG_OPTIONAL
) {
460 ASN1_VALUE
**pseqval
;
461 pseqval
= ossl_asn1_get_field_ptr(pval
, seqtt
);
462 ossl_asn1_template_free(pseqval
, seqtt
);
465 ERR_raise(ERR_LIB_ASN1
, ASN1_R_FIELD_MISSING
);
470 if (!ossl_asn1_enc_save(pval
, *in
, p
- *in
, it
))
472 if (asn1_cb
&& !asn1_cb(ASN1_OP_D2I_POST
, pval
, it
, NULL
))
481 ERR_raise(ERR_LIB_ASN1
, ASN1_R_AUX_ERROR
);
484 ERR_add_error_data(4, "Field=", errtt
->field_name
,
485 ", Type=", it
->sname
);
487 ERR_add_error_data(2, "Type=", it
->sname
);
492 * Templates are handled with two separate functions. One handles any
493 * EXPLICIT tag and the other handles the rest.
496 static int asn1_template_ex_d2i(ASN1_VALUE
**val
,
497 const unsigned char **in
, long inlen
,
498 const ASN1_TEMPLATE
*tt
, char opt
,
499 ASN1_TLC
*ctx
, int depth
,
500 OSSL_LIB_CTX
*libctx
, const char *propq
)
505 const unsigned char *p
, *q
;
510 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
514 /* Check if EXPLICIT tag expected */
515 if (flags
& ASN1_TFLG_EXPTAG
) {
518 * Need to work out amount of data available to the inner content and
519 * where it starts: so read in EXPLICIT header to get the info.
521 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &exp_eoc
, &cst
,
522 &p
, inlen
, tt
->tag
, aclass
, opt
, ctx
);
525 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
527 } else if (ret
== -1)
530 ERR_raise(ERR_LIB_ASN1
, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED
);
533 /* We've found the field so it can't be OPTIONAL now */
534 ret
= asn1_template_noexp_d2i(val
, &p
, len
, tt
, 0, ctx
, depth
, libctx
,
537 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
540 /* We read the field in OK so update length */
543 /* If NDEF we must have an EOC here */
544 if (!asn1_check_eoc(&p
, len
)) {
545 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MISSING_EOC
);
550 * Otherwise we must hit the EXPLICIT tag end or its an error
553 ERR_raise(ERR_LIB_ASN1
, ASN1_R_EXPLICIT_LENGTH_MISMATCH
);
558 return asn1_template_noexp_d2i(val
, in
, inlen
, tt
, opt
, ctx
, depth
,
568 static int asn1_template_noexp_d2i(ASN1_VALUE
**val
,
569 const unsigned char **in
, long len
,
570 const ASN1_TEMPLATE
*tt
, char opt
,
571 ASN1_TLC
*ctx
, int depth
,
572 OSSL_LIB_CTX
*libctx
, const char *propq
)
577 const unsigned char *p
, *q
;
581 aclass
= flags
& ASN1_TFLG_TAG_CLASS
;
586 * If field is embedded then val needs fixing so it is a pointer to
587 * a pointer to a field.
589 if (tt
->flags
& ASN1_TFLG_EMBED
) {
590 tval
= (ASN1_VALUE
*)val
;
594 if (flags
& ASN1_TFLG_SK_MASK
) {
595 /* SET OF, SEQUENCE OF */
598 /* First work out expected inner tag value */
599 if (flags
& ASN1_TFLG_IMPTAG
) {
603 skaclass
= V_ASN1_UNIVERSAL
;
604 if (flags
& ASN1_TFLG_SET_OF
)
607 sktag
= V_ASN1_SEQUENCE
;
610 ret
= asn1_check_tlen(&len
, NULL
, NULL
, &sk_eoc
, NULL
,
611 &p
, len
, sktag
, skaclass
, opt
, ctx
);
613 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
615 } else if (ret
== -1)
618 *val
= (ASN1_VALUE
*)sk_ASN1_VALUE_new_null();
621 * We've got a valid STACK: free up any items present
623 STACK_OF(ASN1_VALUE
) *sktmp
= (STACK_OF(ASN1_VALUE
) *)*val
;
625 while (sk_ASN1_VALUE_num(sktmp
) > 0) {
626 vtmp
= sk_ASN1_VALUE_pop(sktmp
);
627 ASN1_item_ex_free(&vtmp
, ASN1_ITEM_ptr(tt
->item
));
632 ERR_raise(ERR_LIB_ASN1
, ERR_R_CRYPTO_LIB
);
636 /* Read as many items as we can */
640 /* See if EOC found */
641 if (asn1_check_eoc(&p
, len
)) {
643 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNEXPECTED_EOC
);
651 if (asn1_item_embed_d2i(&skfield
, &p
, len
,
652 ASN1_ITEM_ptr(tt
->item
), -1, 0, 0, ctx
,
653 depth
, libctx
, propq
) <= 0) {
654 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
655 /* |skfield| may be partially allocated despite failure. */
656 ASN1_item_free(skfield
, ASN1_ITEM_ptr(tt
->item
));
660 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE
) *)*val
, skfield
)) {
661 ERR_raise(ERR_LIB_ASN1
, ERR_R_CRYPTO_LIB
);
662 ASN1_item_free(skfield
, ASN1_ITEM_ptr(tt
->item
));
667 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MISSING_EOC
);
670 } else if (flags
& ASN1_TFLG_IMPTAG
) {
671 /* IMPLICIT tagging */
672 ret
= asn1_item_embed_d2i(val
, &p
, len
,
673 ASN1_ITEM_ptr(tt
->item
), tt
->tag
, aclass
, opt
,
674 ctx
, depth
, libctx
, propq
);
676 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
678 } else if (ret
== -1)
681 /* Nothing special */
682 ret
= asn1_item_embed_d2i(val
, &p
, len
, ASN1_ITEM_ptr(tt
->item
),
683 -1, 0, opt
, ctx
, depth
, libctx
, propq
);
685 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
687 } else if (ret
== -1)
698 static int asn1_d2i_ex_primitive(ASN1_VALUE
**pval
,
699 const unsigned char **in
, long inlen
,
701 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
)
705 char cst
, inf
, free_cont
= 0;
706 const unsigned char *p
;
707 BUF_MEM buf
= { 0, NULL
, 0, 0 };
708 const unsigned char *cont
= NULL
;
712 ERR_raise(ERR_LIB_ASN1
, ASN1_R_ILLEGAL_NULL
);
713 return 0; /* Should never happen */
716 if (it
->itype
== ASN1_ITYPE_MSTRING
) {
722 if (utype
== V_ASN1_ANY
) {
723 /* If type is ANY need to figure out type from tag */
724 unsigned char oclass
;
726 ERR_raise(ERR_LIB_ASN1
, ASN1_R_ILLEGAL_TAGGED_ANY
);
730 ERR_raise(ERR_LIB_ASN1
, ASN1_R_ILLEGAL_OPTIONAL_ANY
);
734 ret
= asn1_check_tlen(NULL
, &utype
, &oclass
, NULL
, NULL
,
735 &p
, inlen
, -1, 0, 0, ctx
);
737 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
740 if (oclass
!= V_ASN1_UNIVERSAL
)
741 utype
= V_ASN1_OTHER
;
745 aclass
= V_ASN1_UNIVERSAL
;
749 ret
= asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, &cst
,
750 &p
, inlen
, tag
, aclass
, opt
, ctx
);
752 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
754 } else if (ret
== -1)
757 /* SEQUENCE, SET and "OTHER" are left in encoded form */
758 if ((utype
== V_ASN1_SEQUENCE
)
759 || (utype
== V_ASN1_SET
) || (utype
== V_ASN1_OTHER
)) {
761 * Clear context cache for type OTHER because the auto clear when we
762 * have a exact match won't work
764 if (utype
== V_ASN1_OTHER
) {
767 /* SEQUENCE and SET must be constructed */
769 ERR_raise(ERR_LIB_ASN1
, ASN1_R_TYPE_NOT_CONSTRUCTED
);
774 /* If indefinite length constructed find the real end */
776 if (!asn1_find_end(&p
, plen
, inf
))
780 len
= p
- cont
+ plen
;
784 if (utype
== V_ASN1_NULL
|| utype
== V_ASN1_BOOLEAN
785 || utype
== V_ASN1_OBJECT
|| utype
== V_ASN1_INTEGER
786 || utype
== V_ASN1_ENUMERATED
) {
787 ERR_raise(ERR_LIB_ASN1
, ASN1_R_TYPE_NOT_PRIMITIVE
);
791 /* Free any returned 'buf' content */
794 * Should really check the internal tags are correct but some things
795 * may get this wrong. The relevant specs say that constructed string
796 * types should be OCTET STRINGs internally irrespective of the type.
797 * So instead just check for UNIVERSAL class and ignore the tag.
799 if (!asn1_collect(&buf
, &p
, plen
, inf
, -1, V_ASN1_UNIVERSAL
, 0)) {
803 /* Append a final null to string */
804 if (!BUF_MEM_grow_clean(&buf
, len
+ 1)) {
805 ERR_raise(ERR_LIB_ASN1
, ERR_R_BUF_LIB
);
809 cont
= (const unsigned char *)buf
.data
;
816 /* We now have content length and type: translate into a structure */
817 /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
818 if (!asn1_ex_c2i(pval
, cont
, len
, utype
, &free_cont
, it
))
825 OPENSSL_free(buf
.data
);
829 /* Translate ASN1 content octets into a structure */
831 static int asn1_ex_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
832 int utype
, char *free_cont
, const ASN1_ITEM
*it
)
834 ASN1_VALUE
**opval
= NULL
;
836 ASN1_TYPE
*typ
= NULL
;
838 const ASN1_PRIMITIVE_FUNCS
*pf
;
842 if (pf
&& pf
->prim_c2i
)
843 return pf
->prim_c2i(pval
, cont
, len
, utype
, free_cont
, it
);
844 /* If ANY type clear type and set pointer to internal value */
845 if (it
->utype
== V_ASN1_ANY
) {
847 typ
= ASN1_TYPE_new();
850 *pval
= (ASN1_VALUE
*)typ
;
852 typ
= (ASN1_TYPE
*)*pval
;
854 if (utype
!= typ
->type
)
855 ASN1_TYPE_set(typ
, utype
, NULL
);
857 pval
= &typ
->value
.asn1_value
;
861 if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT
**)pval
, &cont
, len
))
867 ERR_raise(ERR_LIB_ASN1
, ASN1_R_NULL_IS_WRONG_LENGTH
);
870 *pval
= (ASN1_VALUE
*)1;
875 ERR_raise(ERR_LIB_ASN1
, ASN1_R_BOOLEAN_IS_WRONG_LENGTH
);
879 tbool
= (ASN1_BOOLEAN
*)pval
;
884 case V_ASN1_BIT_STRING
:
885 if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING
**)pval
, &cont
, len
))
890 case V_ASN1_ENUMERATED
:
891 tint
= (ASN1_INTEGER
**)pval
;
892 if (!ossl_c2i_ASN1_INTEGER(tint
, &cont
, len
))
894 /* Fixup type to match the expected form */
895 (*tint
)->type
= utype
| ((*tint
)->type
& V_ASN1_NEG
);
898 case V_ASN1_OCTET_STRING
:
899 case V_ASN1_NUMERICSTRING
:
900 case V_ASN1_PRINTABLESTRING
:
901 case V_ASN1_T61STRING
:
902 case V_ASN1_VIDEOTEXSTRING
:
903 case V_ASN1_IA5STRING
:
905 case V_ASN1_GENERALIZEDTIME
:
906 case V_ASN1_GRAPHICSTRING
:
907 case V_ASN1_VISIBLESTRING
:
908 case V_ASN1_GENERALSTRING
:
909 case V_ASN1_UNIVERSALSTRING
:
910 case V_ASN1_BMPSTRING
:
911 case V_ASN1_UTF8STRING
:
914 case V_ASN1_SEQUENCE
:
916 if (utype
== V_ASN1_BMPSTRING
&& (len
& 1)) {
917 ERR_raise(ERR_LIB_ASN1
, ASN1_R_BMPSTRING_IS_WRONG_LENGTH
);
920 if (utype
== V_ASN1_UNIVERSALSTRING
&& (len
& 3)) {
921 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH
);
924 if (utype
== V_ASN1_GENERALIZEDTIME
&& (len
< 15)) {
925 ERR_raise(ERR_LIB_ASN1
, ASN1_R_GENERALIZEDTIME_IS_TOO_SHORT
);
928 if (utype
== V_ASN1_UTCTIME
&& (len
< 13)) {
929 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UTCTIME_IS_TOO_SHORT
);
932 /* All based on ASN1_STRING and handled the same */
934 stmp
= ASN1_STRING_type_new(utype
);
936 ERR_raise(ERR_LIB_ASN1
, ERR_R_ASN1_LIB
);
939 *pval
= (ASN1_VALUE
*)stmp
;
941 stmp
= (ASN1_STRING
*)*pval
;
944 /* If we've already allocated a buffer use it */
946 ASN1_STRING_set0(stmp
, (unsigned char *)cont
/* UGLY CAST! */, len
);
949 if (!ASN1_STRING_set(stmp
, cont
, len
)) {
950 ERR_raise(ERR_LIB_ASN1
, ERR_R_ASN1_LIB
);
951 ASN1_STRING_free(stmp
);
958 /* If ASN1_ANY and NULL type fix up value */
959 if (typ
&& (utype
== V_ASN1_NULL
))
960 typ
->value
.ptr
= NULL
;
973 * This function finds the end of an ASN1 structure when passed its maximum
974 * length, whether it is indefinite length and a pointer to the content. This
975 * is more efficient than calling asn1_collect because it does not recurse on
976 * each indefinite length header.
979 static int asn1_find_end(const unsigned char **in
, long len
, char inf
)
981 uint32_t expected_eoc
;
983 const unsigned char *p
= *in
, *q
;
984 /* If not indefinite length constructed just add length */
991 * Indefinite length constructed form. Find the end when enough EOCs are
992 * found. If more indefinite length constructed headers are encountered
993 * increment the expected eoc count otherwise just skip to the end of the
997 if (asn1_check_eoc(&p
, len
)) {
999 if (expected_eoc
== 0)
1005 /* Just read in a header: only care about the length */
1006 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &inf
, NULL
, &p
, len
,
1008 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
1012 if (expected_eoc
== UINT32_MAX
) {
1013 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
1023 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MISSING_EOC
);
1031 * This function collects the asn1 data from a constructed string type into
1032 * a buffer. The values of 'in' and 'len' should refer to the contents of the
1033 * constructed type and 'inf' should be set if it is indefinite length.
1036 #ifndef ASN1_MAX_STRING_NEST
1038 * This determines how many levels of recursion are permitted in ASN1 string
1039 * types. If it is not limited stack overflows can occur. If set to zero no
1040 * recursion is allowed at all. Although zero should be adequate examples
1041 * exist that require a value of 1. So 5 should be more than enough.
1043 # define ASN1_MAX_STRING_NEST 5
1046 static int asn1_collect(BUF_MEM
*buf
, const unsigned char **in
, long len
,
1047 char inf
, int tag
, int aclass
, int depth
)
1049 const unsigned char *p
, *q
;
1055 * If no buffer and not indefinite length constructed just pass over the
1065 if (asn1_check_eoc(&p
, len
)) {
1067 * EOC is illegal outside indefinite length constructed form
1070 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNEXPECTED_EOC
);
1077 if (!asn1_check_tlen(&plen
, NULL
, NULL
, &ininf
, &cst
, &p
,
1078 len
, tag
, aclass
, 0, NULL
)) {
1079 ERR_raise(ERR_LIB_ASN1
, ERR_R_NESTED_ASN1_ERROR
);
1083 /* If indefinite length constructed update max length */
1085 if (depth
>= ASN1_MAX_STRING_NEST
) {
1086 ERR_raise(ERR_LIB_ASN1
, ASN1_R_NESTED_ASN1_STRING
);
1089 if (!asn1_collect(buf
, &p
, plen
, ininf
, tag
, aclass
, depth
+ 1))
1091 } else if (plen
&& !collect_data(buf
, &p
, plen
))
1096 ERR_raise(ERR_LIB_ASN1
, ASN1_R_MISSING_EOC
);
1103 static int collect_data(BUF_MEM
*buf
, const unsigned char **p
, long plen
)
1108 if (!BUF_MEM_grow_clean(buf
, len
+ plen
)) {
1109 ERR_raise(ERR_LIB_ASN1
, ERR_R_BUF_LIB
);
1112 memcpy(buf
->data
+ len
, *p
, plen
);
1118 /* Check for ASN1 EOC and swallow it if found */
1120 static int asn1_check_eoc(const unsigned char **in
, long len
)
1122 const unsigned char *p
;
1127 if (p
[0] == '\0' && p
[1] == '\0') {
1135 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1136 * length for indefinite length constructed form, we don't know the exact
1137 * length but we can set an upper bound to the amount of data available minus
1138 * the header length just read.
1141 static int asn1_check_tlen(long *olen
, int *otag
, unsigned char *oclass
,
1142 char *inf
, char *cst
,
1143 const unsigned char **in
, long len
,
1144 int exptag
, int expclass
, char opt
, ASN1_TLC
*ctx
)
1149 const unsigned char *p
, *q
;
1154 ERR_raise(ERR_LIB_ASN1
, ASN1_R_TOO_SMALL
);
1157 if (ctx
!= NULL
&& ctx
->valid
) {
1160 pclass
= ctx
->pclass
;
1164 i
= ASN1_get_object(&p
, &plen
, &ptag
, &pclass
, len
);
1168 ctx
->pclass
= pclass
;
1170 ctx
->hdrlen
= p
- q
;
1173 * If definite length, and no error, length + header can't exceed
1174 * total amount of data available.
1176 if ((i
& 0x81) == 0 && (plen
+ ctx
->hdrlen
) > len
) {
1177 ERR_raise(ERR_LIB_ASN1
, ASN1_R_TOO_LONG
);
1183 if ((i
& 0x80) != 0) {
1184 ERR_raise(ERR_LIB_ASN1
, ASN1_R_BAD_OBJECT_HEADER
);
1188 if (exptag
!= ptag
|| expclass
!= pclass
) {
1190 * If type is OPTIONAL, not an error: indicate missing type.
1194 ERR_raise(ERR_LIB_ASN1
, ASN1_R_WRONG_TAG
);
1198 * We have a tag and class match: assume we are going to do something
1201 asn1_tlc_clear(ctx
);
1205 plen
= len
- (p
- q
);
1211 *cst
= i
& V_ASN1_CONSTRUCTED
;
1226 asn1_tlc_clear(ctx
);