2 * Copyright 2017-2018 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 "internal/numbers.h"
19 # pragma GCC diagnostic ignored "-Wunused-function"
22 # pragma clang diagnostic ignored "-Wunused-function"
25 /* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
26 static unsigned char t_invalid_zero[] = {
27 0x30, 0x02, /* SEQUENCE tag + length */
28 0x02, 0x00 /* INTEGER tag + length */
31 #ifndef OPENSSL_NO_DEPRECATED_3_0
32 /* LONG case ************************************************************* */
38 ASN1_SEQUENCE(ASN1_LONG_DATA) = {
39 ASN1_EMBED(ASN1_LONG_DATA, test_long, LONG),
40 } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
42 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
43 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
45 static int test_long(void)
47 const unsigned char *p = t_invalid_zero;
48 ASN1_LONG_DATA *dectst =
49 d2i_ASN1_LONG_DATA(NULL, &p, sizeof(t_invalid_zero));
54 ASN1_LONG_DATA_free(dectst);
59 /* INT32 case ************************************************************* */
65 ASN1_SEQUENCE(ASN1_INT32_DATA) = {
66 ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
67 } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
69 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
70 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
72 static int test_int32(void)
74 const unsigned char *p = t_invalid_zero;
75 ASN1_INT32_DATA *dectst =
76 d2i_ASN1_INT32_DATA(NULL, &p, sizeof(t_invalid_zero));
81 ASN1_INT32_DATA_free(dectst);
85 /* UINT32 case ************************************************************* */
91 ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
92 ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
93 } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
95 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
96 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
98 static int test_uint32(void)
100 const unsigned char *p = t_invalid_zero;
101 ASN1_UINT32_DATA *dectst =
102 d2i_ASN1_UINT32_DATA(NULL, &p, sizeof(t_invalid_zero));
107 ASN1_UINT32_DATA_free(dectst);
111 /* INT64 case ************************************************************* */
117 ASN1_SEQUENCE(ASN1_INT64_DATA) = {
118 ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
119 } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
121 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
122 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
124 static int test_int64(void)
126 const unsigned char *p = t_invalid_zero;
127 ASN1_INT64_DATA *dectst =
128 d2i_ASN1_INT64_DATA(NULL, &p, sizeof(t_invalid_zero));
133 ASN1_INT64_DATA_free(dectst);
137 /* UINT64 case ************************************************************* */
140 uint64_t test_uint64;
143 ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
144 ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
145 } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
147 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
148 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
150 static int test_uint64(void)
152 const unsigned char *p = t_invalid_zero;
153 ASN1_UINT64_DATA *dectst =
154 d2i_ASN1_UINT64_DATA(NULL, &p, sizeof(t_invalid_zero));
159 ASN1_UINT64_DATA_free(dectst);
164 ASN1_STRING *invalidDirString;
167 ASN1_SEQUENCE(INVALIDTEMPLATE) = {
169 * DirectoryString is a CHOICE type so it must use explicit tagging -
170 * but we deliberately use implicit here, which makes this template invalid.
172 ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
173 } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
175 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
176 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
178 /* Empty sequence for invalid template test */
179 static unsigned char t_invalid_template[] = {
180 0x30, 0x03, /* SEQUENCE tag + length */
181 0x0c, 0x01, 0x41 /* UTF8String, length 1, "A" */
184 static int test_invalid_template(void)
186 const unsigned char *p = t_invalid_template;
187 INVALIDTEMPLATE *tmp = d2i_INVALIDTEMPLATE(NULL, &p,
188 sizeof(t_invalid_template));
190 /* We expect a NULL pointer return */
191 if (TEST_ptr_null(tmp))
194 INVALIDTEMPLATE_free(tmp);
198 int setup_tests(void)
200 #ifndef OPENSSL_NO_DEPRECATED_3_0
203 ADD_TEST(test_int32);
204 ADD_TEST(test_uint32);
205 ADD_TEST(test_int64);
206 ADD_TEST(test_uint64);
207 ADD_TEST(test_invalid_template);