2 * Copyright 2017-2021 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/asn1t.h>
15 #include <openssl/obj_mac.h>
16 #include "internal/numbers.h"
20 # pragma GCC diagnostic ignored "-Wunused-function"
23 # pragma clang diagnostic ignored "-Wunused-function"
26 /* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
27 static unsigned char t_invalid_zero[] = {
28 0x30, 0x02, /* SEQUENCE tag + length */
29 0x02, 0x00 /* INTEGER tag + length */
32 #ifndef OPENSSL_NO_DEPRECATED_3_0
33 /* LONG case ************************************************************* */
39 ASN1_SEQUENCE(ASN1_LONG_DATA) = {
40 ASN1_EMBED(ASN1_LONG_DATA, test_long, LONG),
41 } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
43 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
44 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
46 static int test_long(void)
48 const unsigned char *p = t_invalid_zero;
49 ASN1_LONG_DATA *dectst =
50 d2i_ASN1_LONG_DATA(NULL, &p, sizeof(t_invalid_zero));
55 ASN1_LONG_DATA_free(dectst);
60 /* INT32 case ************************************************************* */
66 ASN1_SEQUENCE(ASN1_INT32_DATA) = {
67 ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
68 } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
70 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
71 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
73 static int test_int32(void)
75 const unsigned char *p = t_invalid_zero;
76 ASN1_INT32_DATA *dectst =
77 d2i_ASN1_INT32_DATA(NULL, &p, sizeof(t_invalid_zero));
82 ASN1_INT32_DATA_free(dectst);
86 /* UINT32 case ************************************************************* */
92 ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
93 ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
94 } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
96 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
97 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
99 static int test_uint32(void)
101 const unsigned char *p = t_invalid_zero;
102 ASN1_UINT32_DATA *dectst =
103 d2i_ASN1_UINT32_DATA(NULL, &p, sizeof(t_invalid_zero));
108 ASN1_UINT32_DATA_free(dectst);
112 /* INT64 case ************************************************************* */
118 ASN1_SEQUENCE(ASN1_INT64_DATA) = {
119 ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
120 } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
122 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
123 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
125 static int test_int64(void)
127 const unsigned char *p = t_invalid_zero;
128 ASN1_INT64_DATA *dectst =
129 d2i_ASN1_INT64_DATA(NULL, &p, sizeof(t_invalid_zero));
134 ASN1_INT64_DATA_free(dectst);
138 /* UINT64 case ************************************************************* */
141 uint64_t test_uint64;
144 ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
145 ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
146 } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
148 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
149 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
151 static int test_uint64(void)
153 const unsigned char *p = t_invalid_zero;
154 ASN1_UINT64_DATA *dectst =
155 d2i_ASN1_UINT64_DATA(NULL, &p, sizeof(t_invalid_zero));
160 ASN1_UINT64_DATA_free(dectst);
165 ASN1_STRING *invalidDirString;
168 ASN1_SEQUENCE(INVALIDTEMPLATE) = {
170 * DirectoryString is a CHOICE type so it must use explicit tagging -
171 * but we deliberately use implicit here, which makes this template invalid.
173 ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
174 } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
176 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
177 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
179 /* Empty sequence for invalid template test */
180 static unsigned char t_invalid_template[] = {
181 0x30, 0x03, /* SEQUENCE tag + length */
182 0x0c, 0x01, 0x41 /* UTF8String, length 1, "A" */
185 static int test_invalid_template(void)
187 const unsigned char *p = t_invalid_template;
188 INVALIDTEMPLATE *tmp = d2i_INVALIDTEMPLATE(NULL, &p,
189 sizeof(t_invalid_template));
191 /* We expect a NULL pointer return */
192 if (TEST_ptr_null(tmp))
195 INVALIDTEMPLATE_free(tmp);
199 static int test_reuse_asn1_object(void)
201 static unsigned char cn_der[] = { 0x06, 0x03, 0x55, 0x04, 0x06 };
202 static unsigned char oid_der[] = {
203 0x06, 0x06, 0x2a, 0x03, 0x04, 0x05, 0x06, 0x07
207 unsigned char const *p = oid_der;
209 /* Create an object that owns dynamically allocated 'sn' and 'ln' fields */
211 if (!TEST_ptr(obj = ASN1_OBJECT_create(NID_undef, cn_der, sizeof(cn_der),
212 "C", "countryName")))
214 /* reuse obj - this should not leak sn and ln */
215 if (!TEST_ptr(d2i_ASN1_OBJECT(&obj, &p, sizeof(oid_der))))
219 ASN1_OBJECT_free(obj);
223 int setup_tests(void)
225 #ifndef OPENSSL_NO_DEPRECATED_3_0
228 ADD_TEST(test_int32);
229 ADD_TEST(test_uint32);
230 ADD_TEST(test_int64);
231 ADD_TEST(test_uint64);
232 ADD_TEST(test_invalid_template);
233 ADD_TEST(test_reuse_asn1_object);