SSL tests: port CT tests, add a few more
[openssl.git] / test / ssl_test_ctx_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 /*
11  * Ideally, CONF should offer standard parsing methods and cover them
12  * in tests. But since we have no CONF tests, we use a custom test for now.
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17
18 #include "e_os.h"
19 #include "ssl_test_ctx.h"
20 #include "testutil.h"
21 #include <openssl/e_os2.h>
22 #include <openssl/err.h>
23 #include <openssl/conf.h>
24 #include <openssl/ssl.h>
25
26 static CONF *conf = NULL;
27
28 typedef struct ssl_test_ctx_test_fixture {
29     const char *test_case_name;
30     const char *test_section;
31     /* Expected parsed configuration. */
32     SSL_TEST_CTX *expected_ctx;
33 } SSL_TEST_CTX_TEST_FIXTURE;
34
35
36 static int SSL_TEST_CLIENT_CONF_equal(SSL_TEST_CLIENT_CONF *client,
37                                       SSL_TEST_CLIENT_CONF *client2)
38 {
39     if (client->verify_callback != client2->verify_callback) {
40         fprintf(stderr, "ClientVerifyCallback mismatch: %s vs %s.\n",
41                 ssl_verify_callback_name(client->verify_callback),
42                 ssl_verify_callback_name(client2->verify_callback));
43         return 0;
44     }
45     if (client->servername != client2->servername) {
46         fprintf(stderr, "ServerName mismatch: %s vs %s.\n",
47                 ssl_servername_name(client->servername),
48                 ssl_servername_name(client2->servername));
49         return 0;
50     }
51     if (!strings_equal("Client NPNProtocols", client->npn_protocols,
52                        client2->npn_protocols))
53         return 0;
54     if (!strings_equal("Client ALPNProtocols", client->alpn_protocols,
55                        client2->alpn_protocols))
56         return 0;
57     if (client->ct_validation != client2->ct_validation) {
58         fprintf(stderr, "CTValidation mismatch: %s vs %s.\n",
59                 ssl_ct_validation_name(client->ct_validation),
60                 ssl_ct_validation_name(client2->ct_validation));
61         return 0;
62     }
63     return 1;
64 }
65
66 static int SSL_TEST_SERVER_CONF_equal(SSL_TEST_SERVER_CONF *server,
67                                       SSL_TEST_SERVER_CONF *server2)
68 {
69     if (server->servername_callback != server2->servername_callback) {
70         fprintf(stderr, "ServerNameCallback mismatch: %s vs %s.\n",
71                 ssl_servername_callback_name(server->servername_callback),
72                 ssl_servername_callback_name(server2->servername_callback));
73         return 0;
74     }
75     if (!strings_equal("Server NPNProtocols", server->npn_protocols,
76                        server2->npn_protocols))
77         return 0;
78     if (!strings_equal("Server ALPNProtocols", server->alpn_protocols,
79                        server2->alpn_protocols))
80         return 0;
81     if (server->broken_session_ticket != server2->broken_session_ticket) {
82         fprintf(stderr, "Broken session ticket mismatch: %d vs %d.\n",
83                 server->broken_session_ticket, server2->broken_session_ticket);
84         return 0;
85     }
86     return 1;
87 }
88
89 static int SSL_TEST_EXTRA_CONF_equal(SSL_TEST_EXTRA_CONF *extra,
90                                      SSL_TEST_EXTRA_CONF *extra2)
91 {
92     return SSL_TEST_CLIENT_CONF_equal(&extra->client, &extra2->client)
93         && SSL_TEST_SERVER_CONF_equal(&extra->server, &extra2->server)
94         && SSL_TEST_SERVER_CONF_equal(&extra->server2, &extra2->server2);
95 }
96
97 /* Returns 1 if the contexts are equal, 0 otherwise. */
98 static int SSL_TEST_CTX_equal(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
99 {
100     if (ctx->method != ctx2->method) {
101         fprintf(stderr, "Method mismatch: %s vs %s.\n",
102                 ssl_test_method_name(ctx->method),
103                 ssl_test_method_name(ctx2->method));
104         return 0;
105     }
106     if (ctx->handshake_mode != ctx2->handshake_mode) {
107         fprintf(stderr, "HandshakeMode mismatch: %s vs %s.\n",
108                 ssl_handshake_mode_name(ctx->handshake_mode),
109                 ssl_handshake_mode_name(ctx2->handshake_mode));
110         return 0;
111     }
112
113     if (!SSL_TEST_EXTRA_CONF_equal(&ctx->extra, &ctx2->extra)) {
114         fprintf(stderr, "Extra conf mismatch.\n");
115         return 0;
116     }
117     if (!SSL_TEST_EXTRA_CONF_equal(&ctx->resume_extra, &ctx2->resume_extra)) {
118         fprintf(stderr, "Resume extra conf mismatch.\n");
119         return 0;
120     }
121
122     if (ctx->expected_result != ctx2->expected_result) {
123         fprintf(stderr, "ExpectedResult mismatch: %s vs %s.\n",
124                 ssl_test_result_name(ctx->expected_result),
125                 ssl_test_result_name(ctx2->expected_result));
126         return 0;
127     }
128     if (ctx->expected_client_alert != ctx2->expected_client_alert) {
129         fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n",
130                 ssl_alert_name(ctx->expected_client_alert),
131                 ssl_alert_name(ctx2->expected_client_alert));
132         return 0;
133     }
134     if (ctx->expected_server_alert != ctx2->expected_server_alert) {
135         fprintf(stderr, "ServerAlert mismatch: %s vs %s.\n",
136                 ssl_alert_name(ctx->expected_server_alert),
137                 ssl_alert_name(ctx2->expected_server_alert));
138         return 0;
139     }
140     if (ctx->expected_protocol != ctx2->expected_protocol) {
141         fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n",
142                 ssl_protocol_name(ctx->expected_protocol),
143                 ssl_protocol_name(ctx2->expected_protocol));
144         return 0;
145     }
146     if (ctx->expected_servername != ctx2->expected_servername) {
147         fprintf(stderr, "ExpectedServerName mismatch: %s vs %s.\n",
148                 ssl_servername_name(ctx->expected_servername),
149                 ssl_servername_name(ctx2->expected_servername));
150         return 0;
151     }
152     if (ctx->session_ticket_expected != ctx2->session_ticket_expected) {
153         fprintf(stderr, "SessionTicketExpected mismatch: %s vs %s.\n",
154                 ssl_session_ticket_name(ctx->session_ticket_expected),
155                 ssl_session_ticket_name(ctx2->session_ticket_expected));
156         return 0;
157     }
158     if (!strings_equal("ExpectedNPNProtocol", ctx->expected_npn_protocol,
159                        ctx2->expected_npn_protocol))
160         return 0;
161     if (!strings_equal("ExpectedALPNProtocol", ctx->expected_alpn_protocol,
162                        ctx2->expected_alpn_protocol))
163         return 0;
164     if (ctx->resumption_expected != ctx2->resumption_expected) {
165         fprintf(stderr, "ResumptionExpected mismatch: %d vs %d.\n",
166                 ctx->resumption_expected, ctx2->resumption_expected);
167         return 0;
168     }
169     return 1;
170 }
171
172 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
173 {
174     SSL_TEST_CTX_TEST_FIXTURE fixture;
175     fixture.test_case_name = test_case_name;
176     fixture.expected_ctx = SSL_TEST_CTX_new();
177     OPENSSL_assert(fixture.expected_ctx != NULL);
178     return fixture;
179 }
180
181 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
182 {
183     int success = 0;
184
185     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
186
187     if (ctx == NULL) {
188         fprintf(stderr, "Failed to parse good configuration %s.\n",
189                 fixture.test_section);
190         goto err;
191     }
192
193     if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
194         goto err;
195
196     success = 1;
197  err:
198     SSL_TEST_CTX_free(ctx);
199     return success;
200 }
201
202 static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
203 {
204     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
205
206     if (ctx != NULL) {
207         fprintf(stderr, "Parsing bad configuration %s succeeded.\n",
208                 fixture.test_section);
209         SSL_TEST_CTX_free(ctx);
210         return 0;
211     }
212
213     return 1;
214 }
215
216 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
217 {
218     SSL_TEST_CTX_free(fixture.expected_ctx);
219     ERR_print_errors_fp(stderr);
220 }
221
222 #define SETUP_SSL_TEST_CTX_TEST_FIXTURE()                       \
223     SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
224 #define EXECUTE_SSL_TEST_CTX_TEST()             \
225     EXECUTE_TEST(execute_test, tear_down)
226 #define EXECUTE_SSL_TEST_CTX_FAILURE_TEST()             \
227     EXECUTE_TEST(execute_failure_test, tear_down)
228
229 static int test_empty_configuration()
230 {
231     SETUP_SSL_TEST_CTX_TEST_FIXTURE();
232     fixture.test_section = "ssltest_default";
233     fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
234     EXECUTE_SSL_TEST_CTX_TEST();
235 }
236
237 static int test_good_configuration()
238 {
239     SETUP_SSL_TEST_CTX_TEST_FIXTURE();
240     fixture.test_section = "ssltest_good";
241     fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS;
242     fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
243
244     fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
245     fixture.expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
246     fixture.expected_ctx->expected_server_alert = 0;  /* No alert. */
247     fixture.expected_ctx->expected_protocol = TLS1_1_VERSION;
248     fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
249     fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
250     fixture.expected_ctx->resumption_expected = 1;
251
252     fixture.expected_ctx->extra.client.verify_callback =
253         SSL_TEST_VERIFY_REJECT_ALL;
254     fixture.expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
255     fixture.expected_ctx->extra.client.npn_protocols =
256         OPENSSL_strdup("foo,bar");
257     OPENSSL_assert(fixture.expected_ctx->extra.client.npn_protocols != NULL);
258
259     fixture.expected_ctx->extra.server.servername_callback =
260         SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
261     fixture.expected_ctx->extra.server.broken_session_ticket = 1;
262
263     fixture.expected_ctx->resume_extra.server2.alpn_protocols =
264         OPENSSL_strdup("baz");
265     OPENSSL_assert(
266         fixture.expected_ctx->resume_extra.server2.alpn_protocols != NULL);
267
268     fixture.expected_ctx->resume_extra.client.ct_validation =
269         SSL_TEST_CT_VALIDATION_STRICT;
270
271     EXECUTE_SSL_TEST_CTX_TEST();
272 }
273
274 static const char *bad_configurations[] = {
275     "ssltest_unknown_option",
276     "ssltest_wrong_section",
277     "ssltest_unknown_expected_result",
278     "ssltest_unknown_alert",
279     "ssltest_unknown_protocol",
280     "ssltest_unknown_verify_callback",
281     "ssltest_unknown_servername",
282     "ssltest_unknown_servername_callback",
283     "ssltest_unknown_session_ticket_expected",
284     "ssltest_unknown_method",
285     "ssltest_unknown_handshake_mode",
286     "ssltest_unknown_resumption_expected",
287     "ssltest_unknown_ct_validation",
288 };
289
290 static int test_bad_configuration(int idx)
291 {
292         SETUP_SSL_TEST_CTX_TEST_FIXTURE();
293         fixture.test_section = bad_configurations[idx];
294         EXECUTE_SSL_TEST_CTX_FAILURE_TEST();
295 }
296
297 int main(int argc, char **argv)
298 {
299     int result = 0;
300
301     if (argc != 2)
302         return 1;
303
304     conf = NCONF_new(NULL);
305     OPENSSL_assert(conf != NULL);
306
307     /* argv[1] should point to test/ssl_test_ctx_test.conf */
308     OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
309
310
311     ADD_TEST(test_empty_configuration);
312     ADD_TEST(test_good_configuration);
313     ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
314
315     result = run_tests(argv[0]);
316
317     NCONF_free(conf);
318
319     return result;
320 }