Add memory leak detection to d2i_test
[openssl.git] / test / d2i_test.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /* Regression tests for ASN.1 parsing bugs. */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "testutil.h"
16
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>
23 #include "e_os.h"
24
25 static const ASN1_ITEM *item_type;
26 static const char *test_file;
27
28 typedef enum {
29     ASN1_UNKNOWN,
30     ASN1_OK,
31     ASN1_BIO,
32     ASN1_DECODE,
33     ASN1_ENCODE,
34     ASN1_COMPARE
35 } expected_error_t;
36
37 typedef struct {
38     const char *str;
39     expected_error_t code;
40 } error_enum;
41
42 static expected_error_t expected_error = ASN1_UNKNOWN;
43
44 typedef struct d2i_test_fixture {
45     const char *test_case_name;
46 } D2I_TEST_FIXTURE;
47
48 static D2I_TEST_FIXTURE set_up(const char *const test_case_name)
49 {
50     D2I_TEST_FIXTURE fixture;
51     fixture.test_case_name = test_case_name;
52     return fixture;
53 }
54
55 static int execute_test(D2I_TEST_FIXTURE fixture)
56 {
57     BIO *bio = NULL;
58     ASN1_VALUE *value = NULL;
59     int ret = 0;
60     unsigned char buf[2048];
61     const unsigned char *buf_ptr = buf;
62     unsigned char *der = NULL;
63     int derlen;
64     int len;
65
66     if ((bio = BIO_new_file(test_file, "r")) == NULL)
67         return 0;
68
69     if (expected_error == ASN1_BIO) {
70         value = ASN1_item_d2i_bio(item_type, bio, NULL);
71         if (value == NULL)
72             ret = 1;
73         goto err;
74     }
75
76     /*
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
79      * decoder is called.
80      */
81     len = BIO_read(bio, buf, sizeof buf);
82     if (len < 0)
83         goto err;
84
85     value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type);
86     if (value == NULL) {
87         if (expected_error == ASN1_DECODE)
88             ret = 1;
89         goto err;
90     }
91
92     derlen = ASN1_item_i2d(value, &der, item_type);
93
94     if (der == NULL || derlen < 0) {
95         if (expected_error == ASN1_ENCODE)
96             ret = 1;
97         goto err;
98     }
99
100     if (derlen != len || memcmp(der, buf, derlen) != 0) {
101         if (expected_error == ASN1_COMPARE)
102             ret = 1;
103         goto err;
104     }
105
106     if (expected_error == ASN1_OK)
107         ret = 1;
108
109  err:
110     /* Don't indicate success for memory allocation errors */
111     if (ret == 1 && ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE)
112         ret = 0;
113     BIO_free(bio);
114     OPENSSL_free(der);
115     ASN1_item_free(value, item_type);
116     return ret;
117 }
118
119 static void tear_down(D2I_TEST_FIXTURE fixture)
120 {
121     ERR_print_errors_fp(stderr);
122 }
123
124 #define SETUP_D2I_TEST_FIXTURE() \
125     SETUP_TEST_FIXTURE(D2I_TEST_FIXTURE, set_up)
126
127 #define EXECUTE_D2I_TEST() \
128     EXECUTE_TEST(execute_test, tear_down)
129
130 static int test_bad_asn1()
131 {
132     SETUP_D2I_TEST_FIXTURE();
133     EXECUTE_D2I_TEST();
134 }
135
136 /*
137  * Usage: d2i_test <type> <file>, e.g.
138  * d2i_test generalname bad_generalname.der
139  */
140 int main(int argc, char **argv)
141 {
142     int result = 0;
143     const char *test_type_name;
144     const char *expected_error_string;
145     const char *p = getenv("OPENSSL_DEBUG_MEMORY");
146
147     size_t i;
148
149     static error_enum expected_errors[] = {
150         {"OK", ASN1_OK},
151         {"BIO", ASN1_BIO},
152         {"decode", ASN1_DECODE},
153         {"encode", ASN1_ENCODE},
154         {"compare", ASN1_COMPARE}
155     };
156
157     if (p != NULL && strcmp(p, "on") == 0)
158         CRYPTO_set_mem_debug(1);
159     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
160
161     if (argc != 4) {
162         fprintf(stderr,
163                 "Usage: d2i_test item_name expected_error file.der\n");
164         return 1;
165     }
166
167     test_type_name = argv[1];
168     expected_error_string = argv[2];
169     test_file = argv[3];
170
171     item_type = ASN1_ITEM_lookup(test_type_name);
172
173     if (item_type == NULL) {
174         fprintf(stderr, "Unknown type %s\n", test_type_name);
175         fprintf(stderr, "Supported types:\n");
176         for (i = 0;; i++) {
177             const ASN1_ITEM *it = ASN1_ITEM_get(i);
178
179             if (it == NULL)
180                 break;
181             fprintf(stderr, "\t%s\n", it->sname);
182         }
183         return 1;
184     }
185
186     for (i = 0; i < OSSL_NELEM(expected_errors); i++) {
187         if (strcmp(expected_errors[i].str, expected_error_string) == 0) {
188             expected_error = expected_errors[i].code;
189             break;
190         }
191     }
192
193     if (expected_error == ASN1_UNKNOWN) {
194         fprintf(stderr, "Unknown expected error %s\n", expected_error_string);
195         return 1;
196     }
197
198     ADD_TEST(test_bad_asn1);
199
200     result = run_tests(argv[0]);
201
202 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
203     if (CRYPTO_mem_leaks_fp(stderr) <= 0)
204         result = 1;
205 #endif
206
207     return result;
208 }