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/bio.h>
20 #include <openssl/err.h>
21 #include <openssl/x509.h>
22 #include <openssl/x509v3.h>
24 static const ASN1_ITEM *item_type;
25 static const char *test_file;
27 typedef struct d2i_test_fixture {
28 const char *test_case_name;
32 static D2I_TEST_FIXTURE set_up(const char *const test_case_name)
34 D2I_TEST_FIXTURE fixture;
35 fixture.test_case_name = test_case_name;
39 static int execute_test(D2I_TEST_FIXTURE fixture)
42 ASN1_VALUE *value = NULL;
44 unsigned char buf[2048];
45 const unsigned char *buf_ptr = buf;
48 if ((bio = BIO_new_file(test_file, "r")) == NULL)
52 * We don't use ASN1_item_d2i_bio because it, apparently,
53 * errors too early for some inputs.
55 len = BIO_read(bio, buf, sizeof buf);
59 value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type);
67 ASN1_item_free(value, item_type);
71 static void tear_down(D2I_TEST_FIXTURE fixture)
73 ERR_print_errors_fp(stderr);
76 #define SETUP_D2I_TEST_FIXTURE() \
77 SETUP_TEST_FIXTURE(D2I_TEST_FIXTURE, set_up)
79 #define EXECUTE_D2I_TEST() \
80 EXECUTE_TEST(execute_test, tear_down)
82 static int test_bad_asn1()
84 SETUP_D2I_TEST_FIXTURE();
89 * Usage: d2i_test <type> <file>, e.g.
90 * d2i_test generalname bad_generalname.der
92 int main(int argc, char **argv)
95 const char *test_type_name;
100 test_type_name = argv[1];
103 if (strcmp(test_type_name, "generalname") == 0) {
104 item_type = ASN1_ITEM_rptr(GENERAL_NAME);
105 } else if (strcmp(test_type_name, "x509") == 0) {
106 item_type = ASN1_ITEM_rptr(X509);
108 fprintf(stderr, "Bad type %s\n", test_type_name);
112 ADD_TEST(test_bad_asn1);
114 result = run_tests(argv[0]);