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