2a633f9a5355c69d711b7364595437229b76fb93
[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 (!TEST_int_eq(client->verify_callback, client2->verify_callback)) {
40         TEST_info("ClientVerifyCallback mismatch: %s vs %s.",
41                   ssl_verify_callback_name(client->verify_callback),
42                   ssl_verify_callback_name(client2->verify_callback));
43         return 0;
44     }
45     if (!TEST_int_eq(client->servername, client2->servername)) {
46         TEST_info("ServerName mismatch: %s vs %s.",
47                   ssl_servername_name(client->servername),
48                   ssl_servername_name(client2->servername));
49         return 0;
50     }
51     if (!TEST_str_eq(client->npn_protocols, client2->npn_protocols))
52         return 0;
53     if (!TEST_str_eq(client->alpn_protocols, client2->alpn_protocols))
54         return 0;
55     if (!TEST_int_eq(client->ct_validation, client2->ct_validation)) {
56         TEST_info("CTValidation mismatch: %s vs %s.",
57                   ssl_ct_validation_name(client->ct_validation),
58                   ssl_ct_validation_name(client2->ct_validation));
59         return 0;
60     }
61     return 1;
62 }
63
64 static int SSL_TEST_SERVER_CONF_equal(SSL_TEST_SERVER_CONF *server,
65                                       SSL_TEST_SERVER_CONF *server2)
66 {
67     if (!TEST_int_eq(server->servername_callback,
68                      server2->servername_callback)) {
69         TEST_info("ServerNameCallback mismatch: %s vs %s.",
70                   ssl_servername_callback_name(server->servername_callback),
71                   ssl_servername_callback_name(server2->servername_callback));
72         return 0;
73     }
74     if (!TEST_str_eq(server->npn_protocols, server2->npn_protocols))
75         return 0;
76     if (!TEST_str_eq(server->alpn_protocols, server2->alpn_protocols))
77         return 0;
78     if (!TEST_int_eq(server->broken_session_ticket,
79                      server2->broken_session_ticket))
80         return 0;
81     if (!TEST_int_eq(server->cert_status, server2->cert_status)) {
82         TEST_info("CertStatus mismatch: %s vs %s.",
83                   ssl_certstatus_name(server->cert_status),
84                   ssl_certstatus_name(server2->cert_status));
85         return 0;
86     }
87     return 1;
88 }
89
90 static int SSL_TEST_EXTRA_CONF_equal(SSL_TEST_EXTRA_CONF *extra,
91                                      SSL_TEST_EXTRA_CONF *extra2)
92 {
93     return SSL_TEST_CLIENT_CONF_equal(&extra->client, &extra2->client)
94         && SSL_TEST_SERVER_CONF_equal(&extra->server, &extra2->server)
95         && SSL_TEST_SERVER_CONF_equal(&extra->server2, &extra2->server2);
96 }
97
98 /* Returns 1 if the contexts are equal, 0 otherwise. */
99 static int SSL_TEST_CTX_equal(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
100 {
101     if (!TEST_int_eq(ctx->method, ctx2->method)) {
102         TEST_info("Method mismatch: %s vs %s.",
103                   ssl_test_method_name(ctx->method),
104                   ssl_test_method_name(ctx2->method));
105         return 0;
106     }
107     if (!TEST_int_eq(ctx->handshake_mode, ctx2->handshake_mode)) {
108         TEST_info("HandshakeMode mismatch: %s vs %s.",
109                   ssl_handshake_mode_name(ctx->handshake_mode),
110                   ssl_handshake_mode_name(ctx2->handshake_mode));
111         return 0;
112     }
113     if (!TEST_int_eq(ctx->app_data_size, ctx2->app_data_size))
114         return 0;
115
116     if (!TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size))
117         return 0;
118
119     if (!SSL_TEST_EXTRA_CONF_equal(&ctx->extra, &ctx2->extra))
120         return 0;
121     if (!SSL_TEST_EXTRA_CONF_equal(&ctx->resume_extra, &ctx2->resume_extra))
122         return 0;
123
124     if (!TEST_int_eq(ctx->expected_result, ctx2->expected_result)) {
125         TEST_info("ExpectedResult mismatch: %s vs %s.",
126                   ssl_test_result_name(ctx->expected_result),
127                   ssl_test_result_name(ctx2->expected_result));
128         return 0;
129     }
130     if (!TEST_int_eq(ctx->expected_client_alert, ctx2->expected_client_alert)) {
131         TEST_info("ClientAlert mismatch: %s vs %s.",
132                   ssl_alert_name(ctx->expected_client_alert),
133                   ssl_alert_name(ctx2->expected_client_alert));
134         return 0;
135     }
136     if (!TEST_int_eq(ctx->expected_server_alert, ctx2->expected_server_alert)) {
137         TEST_info("ServerAlert mismatch: %s vs %s.",
138                   ssl_alert_name(ctx->expected_server_alert),
139                   ssl_alert_name(ctx2->expected_server_alert));
140         return 0;
141     }
142     if (!TEST_int_eq(ctx->expected_protocol, ctx2->expected_protocol)) {
143         TEST_info("ClientAlert mismatch: %s vs %s.",
144                   ssl_protocol_name(ctx->expected_protocol),
145                   ssl_protocol_name(ctx2->expected_protocol));
146         return 0;
147     }
148     if (!TEST_int_eq(ctx->expected_servername, ctx2->expected_servername)) {
149         TEST_info("ExpectedServerName mismatch: %s vs %s.",
150                   ssl_servername_name(ctx->expected_servername),
151                   ssl_servername_name(ctx2->expected_servername));
152         return 0;
153     }
154     if (!TEST_int_eq(ctx->session_ticket_expected,
155                      ctx2->session_ticket_expected)) {
156         TEST_info("SessionTicketExpected mismatch: %s vs %s.",
157                 ssl_session_ticket_name(ctx->session_ticket_expected),
158                 ssl_session_ticket_name(ctx2->session_ticket_expected));
159         return 0;
160     }
161     if (!TEST_int_eq(ctx->compression_expected, ctx2->compression_expected)) {
162         TEST_info("ComrpessionExpected mismatch: %d vs %d.",
163                   ctx->compression_expected,
164                   ctx2->compression_expected);
165         return 0;
166     }
167     if (!TEST_str_eq(ctx->expected_npn_protocol, ctx2->expected_npn_protocol))
168         return 0;
169     if (!TEST_str_eq(ctx->expected_alpn_protocol, ctx2->expected_alpn_protocol))
170         return 0;
171     if (!TEST_int_eq(ctx->resumption_expected, ctx2->resumption_expected))
172         return 0;
173     return 1;
174 }
175
176 static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
177 {
178     SSL_TEST_CTX_TEST_FIXTURE fixture;
179     fixture.test_case_name = test_case_name;
180     fixture.expected_ctx = SSL_TEST_CTX_new();
181     TEST_check(fixture.expected_ctx != NULL);
182     return fixture;
183 }
184
185 static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
186 {
187     int success = 0;
188
189     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
190
191     if (!TEST_ptr(ctx)) {
192         TEST_info("Failed to parse good configuration %s.",
193                   fixture.test_section);
194         goto err;
195     }
196
197     if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
198         goto err;
199
200     success = 1;
201  err:
202     SSL_TEST_CTX_free(ctx);
203     return success;
204 }
205
206 static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
207 {
208     SSL_TEST_CTX_free(fixture.expected_ctx);
209 }
210
211 #define SETUP_SSL_TEST_CTX_TEST_FIXTURE()                       \
212     SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
213 #define EXECUTE_SSL_TEST_CTX_TEST()             \
214     EXECUTE_TEST(execute_test, tear_down)
215
216 static int test_empty_configuration()
217 {
218     SETUP_SSL_TEST_CTX_TEST_FIXTURE();
219     fixture.test_section = "ssltest_default";
220     fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
221     EXECUTE_SSL_TEST_CTX_TEST();
222 }
223
224 static int test_good_configuration()
225 {
226     SETUP_SSL_TEST_CTX_TEST_FIXTURE();
227     fixture.test_section = "ssltest_good";
228     fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS;
229     fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
230     fixture.expected_ctx->app_data_size = 1024;
231     fixture.expected_ctx->max_fragment_size = 2048;
232
233     fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
234     fixture.expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
235     fixture.expected_ctx->expected_server_alert = 0;  /* No alert. */
236     fixture.expected_ctx->expected_protocol = TLS1_1_VERSION;
237     fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
238     fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
239     fixture.expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
240     fixture.expected_ctx->resumption_expected = 1;
241
242     fixture.expected_ctx->extra.client.verify_callback =
243         SSL_TEST_VERIFY_REJECT_ALL;
244     fixture.expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
245     fixture.expected_ctx->extra.client.npn_protocols =
246         OPENSSL_strdup("foo,bar");
247     TEST_check(fixture.expected_ctx->extra.client.npn_protocols != NULL);
248
249     fixture.expected_ctx->extra.server.servername_callback =
250         SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
251     fixture.expected_ctx->extra.server.broken_session_ticket = 1;
252
253     fixture.expected_ctx->resume_extra.server2.alpn_protocols =
254         OPENSSL_strdup("baz");
255     TEST_check(
256         fixture.expected_ctx->resume_extra.server2.alpn_protocols != NULL);
257
258     fixture.expected_ctx->resume_extra.client.ct_validation =
259         SSL_TEST_CT_VALIDATION_STRICT;
260
261     EXECUTE_SSL_TEST_CTX_TEST();
262 }
263
264 static const char *bad_configurations[] = {
265     "ssltest_unknown_option",
266     "ssltest_wrong_section",
267     "ssltest_unknown_expected_result",
268     "ssltest_unknown_alert",
269     "ssltest_unknown_protocol",
270     "ssltest_unknown_verify_callback",
271     "ssltest_unknown_servername",
272     "ssltest_unknown_servername_callback",
273     "ssltest_unknown_session_ticket_expected",
274     "ssltest_unknown_compression_expected",
275     "ssltest_unknown_method",
276     "ssltest_unknown_handshake_mode",
277     "ssltest_unknown_resumption_expected",
278     "ssltest_unknown_ct_validation",
279 };
280
281 static int test_bad_configuration(int idx)
282 {
283     SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, bad_configurations[idx]);
284
285     if (!TEST_ptr_null(ctx)) {
286         TEST_info("Parsing bad configuration %s succeeded.",
287                   bad_configurations[idx]);
288         SSL_TEST_CTX_free(ctx);
289         return 0;
290     }
291
292     return 1;
293 }
294
295 int test_main(int argc, char **argv)
296 {
297     int result = 0;
298
299     if (argc != 2)
300         return 1;
301
302     conf = NCONF_new(NULL);
303     TEST_check(conf != NULL);
304
305     /* argv[1] should point to test/ssl_test_ctx_test.conf */
306     TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
307
308     ADD_TEST(test_empty_configuration);
309     ADD_TEST(test_good_configuration);
310     ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
311
312     result = run_tests(argv[0]);
313
314     NCONF_free(conf);
315
316     return result;
317 }