2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL licenses, (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
11 /* Regression tests for ASN.1 parsing bugs. */
18 #include <openssl/asn1.h>
19 #include <openssl/asn1t.h>
20 #include <openssl/bio.h>
21 #include <openssl/err.h>
22 #include <openssl/x509.h>
23 #include <openssl/x509v3.h>
26 static const ASN1_ITEM *item_type;
27 static const char *test_file;
40 expected_error_t code;
43 static expected_error_t expected_error = ASN1_UNKNOWN;
45 typedef struct d2i_test_fixture {
46 const char *test_case_name;
49 static D2I_TEST_FIXTURE set_up(const char *const test_case_name)
51 D2I_TEST_FIXTURE fixture;
52 fixture.test_case_name = test_case_name;
56 static int execute_test(D2I_TEST_FIXTURE fixture)
59 ASN1_VALUE *value = NULL;
61 unsigned char buf[2048];
62 const unsigned char *buf_ptr = buf;
63 unsigned char *der = NULL;
67 if ((bio = BIO_new_file(test_file, "r")) == NULL)
70 if (expected_error == ASN1_BIO) {
71 value = ASN1_item_d2i_bio(item_type, bio, NULL);
78 * Unless we are testing it we don't use ASN1_item_d2i_bio because it
79 * performs sanity checks on the input and can reject it before the
82 len = BIO_read(bio, buf, sizeof buf);
86 value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type);
88 if (expected_error == ASN1_DECODE)
93 derlen = ASN1_item_i2d(value, &der, item_type);
95 if (der == NULL || derlen < 0) {
96 if (expected_error == ASN1_ENCODE)
101 if (derlen != len || memcmp(der, buf, derlen) != 0) {
102 if (expected_error == ASN1_COMPARE)
107 if (expected_error == ASN1_OK)
111 /* Don't indicate success for memory allocation errors */
112 if (ret == 1 && ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE)
116 ASN1_item_free(value, item_type);
120 static void tear_down(D2I_TEST_FIXTURE fixture)
122 ERR_print_errors_fp(stderr);
125 #define SETUP_D2I_TEST_FIXTURE() \
126 SETUP_TEST_FIXTURE(D2I_TEST_FIXTURE, set_up)
128 #define EXECUTE_D2I_TEST() \
129 EXECUTE_TEST(execute_test, tear_down)
131 static int test_bad_asn1()
133 SETUP_D2I_TEST_FIXTURE();
138 * Usage: d2i_test <type> <file>, e.g.
139 * d2i_test generalname bad_generalname.der
141 int main(int argc, char **argv)
144 const char *test_type_name;
145 const char *expected_error_string;
148 static ASN1_ITEM_EXP *items[] = {
149 ASN1_ITEM_ref(ASN1_ANY),
151 ASN1_ITEM_ref(GENERAL_NAME),
152 ASN1_ITEM_ref(ASN1_INTEGER)
155 static error_enum expected_errors[] = {
158 {"decode", ASN1_DECODE},
159 {"encode", ASN1_ENCODE},
160 {"compare", ASN1_COMPARE}
165 "Usage: d2i_test item_name expected_error file.der\n");
169 test_type_name = argv[1];
170 expected_error_string = argv[2];
173 for (i = 0; i < OSSL_NELEM(items); i++) {
174 const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]);
175 if (strcmp(test_type_name, it->sname) == 0) {
180 if (item_type == NULL) {
181 fprintf(stderr, "Unknown type %s\n", test_type_name);
182 fprintf(stderr, "Supported types:\n");
183 for (i = 0; i < OSSL_NELEM(items); i++) {
184 const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]);
185 fprintf(stderr, "\t%s\n", it->sname);
190 for (i = 0; i < OSSL_NELEM(expected_errors); i++) {
191 if (strcmp(expected_errors[i].str, expected_error_string) == 0) {
192 expected_error = expected_errors[i].code;
197 if (expected_error == ASN1_UNKNOWN) {
198 fprintf(stderr, "Unknown expected error %s\n", expected_error_string);
202 ADD_TEST(test_bad_asn1);
204 result = run_tests(argv[0]);