2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 /* Regression tests for ASN.1 parsing bugs. */
17 #include <openssl/asn1.h>
18 #include <openssl/asn1t.h>
19 #include <openssl/bio.h>
20 #include <openssl/err.h>
21 #include <openssl/x509.h>
22 #include <openssl/x509v3.h>
25 static const ASN1_ITEM *item_type;
26 static const char *test_file;
39 expected_error_t code;
42 static expected_error_t expected_error = ASN1_UNKNOWN;
44 typedef struct d2i_test_fixture {
45 const char *test_case_name;
48 static D2I_TEST_FIXTURE set_up(const char *const test_case_name)
50 D2I_TEST_FIXTURE fixture;
51 fixture.test_case_name = test_case_name;
55 static int execute_test(D2I_TEST_FIXTURE fixture)
58 ASN1_VALUE *value = NULL;
60 unsigned char buf[2048];
61 const unsigned char *buf_ptr = buf;
62 unsigned char *der = NULL;
66 if ((bio = BIO_new_file(test_file, "r")) == NULL)
69 if (expected_error == ASN1_BIO) {
70 value = ASN1_item_d2i_bio(item_type, bio, NULL);
77 * Unless we are testing it we don't use ASN1_item_d2i_bio because it
78 * performs sanity checks on the input and can reject it before the
81 len = BIO_read(bio, buf, sizeof buf);
85 value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type);
87 if (expected_error == ASN1_DECODE)
92 derlen = ASN1_item_i2d(value, &der, item_type);
94 if (der == NULL || derlen < 0) {
95 if (expected_error == ASN1_ENCODE)
100 if (derlen != len || memcmp(der, buf, derlen) != 0) {
101 if (expected_error == ASN1_COMPARE)
106 if (expected_error == ASN1_OK)
110 /* Don't indicate success for memory allocation errors */
111 if (ret == 1 && ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE)
115 ASN1_item_free(value, item_type);
119 static void tear_down(D2I_TEST_FIXTURE fixture)
121 ERR_print_errors_fp(stderr);
124 #define SETUP_D2I_TEST_FIXTURE() \
125 SETUP_TEST_FIXTURE(D2I_TEST_FIXTURE, set_up)
127 #define EXECUTE_D2I_TEST() \
128 EXECUTE_TEST(execute_test, tear_down)
130 static int test_bad_asn1()
132 SETUP_D2I_TEST_FIXTURE();
137 * Usage: d2i_test <type> <file>, e.g.
138 * d2i_test generalname bad_generalname.der
140 int main(int argc, char **argv)
143 const char *test_type_name;
144 const char *expected_error_string;
145 const char *p = getenv("OPENSSL_DEBUG_MEMORY");
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}
163 if (p != NULL && strcmp(p, "on") == 0)
164 CRYPTO_set_mem_debug(1);
165 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
169 "Usage: d2i_test item_name expected_error file.der\n");
173 test_type_name = argv[1];
174 expected_error_string = argv[2];
177 for (i = 0; i < OSSL_NELEM(items); i++) {
178 const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]);
179 if (strcmp(test_type_name, it->sname) == 0) {
184 if (item_type == NULL) {
185 fprintf(stderr, "Unknown type %s\n", test_type_name);
186 fprintf(stderr, "Supported types:\n");
187 for (i = 0; i < OSSL_NELEM(items); i++) {
188 const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]);
189 fprintf(stderr, "\t%s\n", it->sname);
194 for (i = 0; i < OSSL_NELEM(expected_errors); i++) {
195 if (strcmp(expected_errors[i].str, expected_error_string) == 0) {
196 expected_error = expected_errors[i].code;
201 if (expected_error == ASN1_UNKNOWN) {
202 fprintf(stderr, "Unknown expected error %s\n", expected_error_string);
206 ADD_TEST(test_bad_asn1);
208 result = run_tests(argv[0]);
210 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
211 if (CRYPTO_mem_leaks_fp(stderr) <= 0)