]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/asn1_decode_test.c
2 * Copyright 2017-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
13 #include <openssl/rand.h>
14 #include <openssl/asn1.h>
15 #include <openssl/asn1t.h>
16 #include <openssl/obj_mac.h>
17 #include "internal/numbers.h"
21 # pragma GCC diagnostic ignored "-Wunused-function"
24 # pragma clang diagnostic ignored "-Wunused-function"
27 /* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
28 static unsigned char t_invalid_zero
[] = {
29 0x30, 0x02, /* SEQUENCE tag + length */
30 0x02, 0x00 /* INTEGER tag + length */
33 #ifndef OPENSSL_NO_DEPRECATED_3_0
34 /* LONG case ************************************************************* */
40 ASN1_SEQUENCE(ASN1_LONG_DATA
) = {
41 ASN1_EMBED(ASN1_LONG_DATA
, test_long
, LONG
),
42 } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA
)
44 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA
)
45 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA
)
47 static int test_long(void)
49 const unsigned char *p
= t_invalid_zero
;
50 ASN1_LONG_DATA
*dectst
=
51 d2i_ASN1_LONG_DATA(NULL
, &p
, sizeof(t_invalid_zero
));
56 ASN1_LONG_DATA_free(dectst
);
61 /* INT32 case ************************************************************* */
67 ASN1_SEQUENCE(ASN1_INT32_DATA
) = {
68 ASN1_EMBED(ASN1_INT32_DATA
, test_int32
, INT32
),
69 } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA
)
71 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA
)
72 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA
)
74 static int test_int32(void)
76 const unsigned char *p
= t_invalid_zero
;
77 ASN1_INT32_DATA
*dectst
=
78 d2i_ASN1_INT32_DATA(NULL
, &p
, sizeof(t_invalid_zero
));
83 ASN1_INT32_DATA_free(dectst
);
87 /* UINT32 case ************************************************************* */
93 ASN1_SEQUENCE(ASN1_UINT32_DATA
) = {
94 ASN1_EMBED(ASN1_UINT32_DATA
, test_uint32
, UINT32
),
95 } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA
)
97 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA
)
98 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA
)
100 static int test_uint32(void)
102 const unsigned char *p
= t_invalid_zero
;
103 ASN1_UINT32_DATA
*dectst
=
104 d2i_ASN1_UINT32_DATA(NULL
, &p
, sizeof(t_invalid_zero
));
109 ASN1_UINT32_DATA_free(dectst
);
113 /* INT64 case ************************************************************* */
119 ASN1_SEQUENCE(ASN1_INT64_DATA
) = {
120 ASN1_EMBED(ASN1_INT64_DATA
, test_int64
, INT64
),
121 } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA
)
123 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA
)
124 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA
)
126 static int test_int64(void)
128 const unsigned char *p
= t_invalid_zero
;
129 ASN1_INT64_DATA
*dectst
=
130 d2i_ASN1_INT64_DATA(NULL
, &p
, sizeof(t_invalid_zero
));
135 ASN1_INT64_DATA_free(dectst
);
139 /* UINT64 case ************************************************************* */
142 uint64_t test_uint64
;
145 ASN1_SEQUENCE(ASN1_UINT64_DATA
) = {
146 ASN1_EMBED(ASN1_UINT64_DATA
, test_uint64
, UINT64
),
147 } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA
)
149 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA
)
150 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA
)
152 static int test_uint64(void)
154 const unsigned char *p
= t_invalid_zero
;
155 ASN1_UINT64_DATA
*dectst
=
156 d2i_ASN1_UINT64_DATA(NULL
, &p
, sizeof(t_invalid_zero
));
161 ASN1_UINT64_DATA_free(dectst
);
165 /* GeneralizedTime underflow *********************************************** */
167 static int test_gentime(void)
169 /* Underflowing GeneralizedTime 161208193400Z (YYMMDDHHMMSSZ) */
170 const unsigned char der
[] = {
171 0x18, 0x0d, 0x31, 0x36, 0x31, 0x32, 0x30, 0x38, 0x31,
172 0x39, 0x33, 0x34, 0x30, 0x30, 0x5a,
174 const unsigned char *p
;
176 ASN1_GENERALIZEDTIME
*gentime
;
179 der_len
= sizeof(der
);
180 gentime
= d2i_ASN1_GENERALIZEDTIME(NULL
, &p
, der_len
);
182 if (!TEST_ptr_null(gentime
))
185 ASN1_GENERALIZEDTIME_free(gentime
);
189 /* UTCTime underflow ******************************************************* */
191 static int test_utctime(void)
193 /* Underflowing UTCTime 0205104700Z (MMDDHHMMSSZ) */
194 const unsigned char der
[] = {
195 0x17, 0x0b, 0x30, 0x32, 0x30, 0x35, 0x31, 0x30,
196 0x34, 0x37, 0x30, 0x30, 0x5a,
198 const unsigned char *p
;
200 ASN1_UTCTIME
*utctime
;
203 der_len
= sizeof(der
);
204 utctime
= d2i_ASN1_UTCTIME(NULL
, &p
, der_len
);
206 if (!TEST_ptr_null(utctime
))
209 ASN1_UTCTIME_free(utctime
);
213 /* Invalid template ******************************************************** */
216 ASN1_STRING
*invalidDirString
;
219 ASN1_SEQUENCE(INVALIDTEMPLATE
) = {
221 * DirectoryString is a CHOICE type so it must use explicit tagging -
222 * but we deliberately use implicit here, which makes this template invalid.
224 ASN1_IMP(INVALIDTEMPLATE
, invalidDirString
, DIRECTORYSTRING
, 12)
225 } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE
)
227 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE
)
228 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE
)
230 /* Empty sequence for invalid template test */
231 static unsigned char t_invalid_template
[] = {
232 0x30, 0x03, /* SEQUENCE tag + length */
233 0x0c, 0x01, 0x41 /* UTF8String, length 1, "A" */
236 static int test_invalid_template(void)
238 const unsigned char *p
= t_invalid_template
;
239 INVALIDTEMPLATE
*tmp
= d2i_INVALIDTEMPLATE(NULL
, &p
,
240 sizeof(t_invalid_template
));
242 /* We expect a NULL pointer return */
243 if (TEST_ptr_null(tmp
))
246 INVALIDTEMPLATE_free(tmp
);
250 static int test_reuse_asn1_object(void)
252 static unsigned char cn_der
[] = { 0x06, 0x03, 0x55, 0x04, 0x06 };
253 static unsigned char oid_der
[] = {
254 0x06, 0x06, 0x2a, 0x03, 0x04, 0x05, 0x06, 0x07
258 unsigned char const *p
= oid_der
;
260 /* Create an object that owns dynamically allocated 'sn' and 'ln' fields */
262 if (!TEST_ptr(obj
= ASN1_OBJECT_create(NID_undef
, cn_der
, sizeof(cn_der
),
263 "C", "countryName")))
265 /* reuse obj - this should not leak sn and ln */
266 if (!TEST_ptr(d2i_ASN1_OBJECT(&obj
, &p
, sizeof(oid_der
))))
270 ASN1_OBJECT_free(obj
);
274 int setup_tests(void)
276 #ifndef OPENSSL_NO_DEPRECATED_3_0
279 ADD_TEST(test_int32
);
280 ADD_TEST(test_uint32
);
281 ADD_TEST(test_int64
);
282 ADD_TEST(test_uint64
);
283 ADD_TEST(test_gentime
);
284 ADD_TEST(test_utctime
);
285 ADD_TEST(test_invalid_template
);
286 ADD_TEST(test_reuse_asn1_object
);