]>
Commit | Line | Data |
---|---|---|
453dfd8d EK |
1 | /* |
2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
440e5d80 RS |
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 | |
453dfd8d | 7 | * https://www.openssl.org/source/license.html |
453dfd8d EK |
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> | |
ce2cdac2 | 16 | #include <string.h> |
453dfd8d EK |
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 | ||
9f48bbac EK |
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 | return 1; | |
58 | } | |
59 | ||
60 | static int SSL_TEST_SERVER_CONF_equal(SSL_TEST_SERVER_CONF *server, | |
61 | SSL_TEST_SERVER_CONF *server2) | |
62 | { | |
63 | if (server->servername_callback != server2->servername_callback) { | |
64 | fprintf(stderr, "ServerNameCallback mismatch: %s vs %s.\n", | |
65 | ssl_servername_callback_name(server->servername_callback), | |
66 | ssl_servername_callback_name(server2->servername_callback)); | |
67 | return 0; | |
68 | } | |
69 | if (!strings_equal("Server NPNProtocols", server->npn_protocols, | |
70 | server2->npn_protocols)) | |
71 | return 0; | |
72 | if (!strings_equal("Server ALPNProtocols", server->alpn_protocols, | |
73 | server2->alpn_protocols)) | |
74 | return 0; | |
75 | if (server->broken_session_ticket != server2->broken_session_ticket) { | |
76 | fprintf(stderr, "Broken session ticket mismatch: %d vs %d.\n", | |
77 | server->broken_session_ticket, server2->broken_session_ticket); | |
78 | return 0; | |
79 | } | |
80 | return 1; | |
81 | } | |
82 | ||
83 | static int SSL_TEST_EXTRA_CONF_equal(SSL_TEST_EXTRA_CONF *extra, | |
84 | SSL_TEST_EXTRA_CONF *extra2) | |
85 | { | |
86 | return SSL_TEST_CLIENT_CONF_equal(&extra->client, &extra2->client) | |
87 | && SSL_TEST_SERVER_CONF_equal(&extra->server, &extra2->server) | |
88 | && SSL_TEST_SERVER_CONF_equal(&extra->server2, &extra2->server2); | |
89 | } | |
90 | ||
453dfd8d EK |
91 | /* Returns 1 if the contexts are equal, 0 otherwise. */ |
92 | static int SSL_TEST_CTX_equal(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2) | |
93 | { | |
9f48bbac EK |
94 | if (ctx->method != ctx2->method) { |
95 | fprintf(stderr, "Method mismatch: %s vs %s.\n", | |
96 | ssl_test_method_name(ctx->method), | |
97 | ssl_test_method_name(ctx2->method)); | |
98 | return 0; | |
99 | } | |
100 | if (ctx->handshake_mode != ctx2->handshake_mode) { | |
101 | fprintf(stderr, "HandshakeMode mismatch: %s vs %s.\n", | |
102 | ssl_handshake_mode_name(ctx->handshake_mode), | |
103 | ssl_handshake_mode_name(ctx2->handshake_mode)); | |
104 | return 0; | |
105 | } | |
106 | ||
107 | if (!SSL_TEST_EXTRA_CONF_equal(&ctx->extra, &ctx2->extra)) { | |
108 | fprintf(stderr, "Extra conf mismatch.\n"); | |
109 | return 0; | |
110 | } | |
111 | if (!SSL_TEST_EXTRA_CONF_equal(&ctx->resume_extra, &ctx2->resume_extra)) { | |
112 | fprintf(stderr, "Resume extra conf mismatch.\n"); | |
113 | return 0; | |
114 | } | |
115 | ||
453dfd8d EK |
116 | if (ctx->expected_result != ctx2->expected_result) { |
117 | fprintf(stderr, "ExpectedResult mismatch: %s vs %s.\n", | |
a263f320 EK |
118 | ssl_test_result_name(ctx->expected_result), |
119 | ssl_test_result_name(ctx2->expected_result)); | |
453dfd8d EK |
120 | return 0; |
121 | } | |
9f48bbac | 122 | if (ctx->expected_client_alert != ctx2->expected_client_alert) { |
453dfd8d | 123 | fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n", |
9f48bbac EK |
124 | ssl_alert_name(ctx->expected_client_alert), |
125 | ssl_alert_name(ctx2->expected_client_alert)); | |
453dfd8d EK |
126 | return 0; |
127 | } | |
9f48bbac | 128 | if (ctx->expected_server_alert != ctx2->expected_server_alert) { |
453dfd8d | 129 | fprintf(stderr, "ServerAlert mismatch: %s vs %s.\n", |
9f48bbac EK |
130 | ssl_alert_name(ctx->expected_server_alert), |
131 | ssl_alert_name(ctx2->expected_server_alert)); | |
453dfd8d EK |
132 | return 0; |
133 | } | |
9f48bbac | 134 | if (ctx->expected_protocol != ctx2->expected_protocol) { |
453dfd8d | 135 | fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n", |
9f48bbac EK |
136 | ssl_protocol_name(ctx->expected_protocol), |
137 | ssl_protocol_name(ctx2->expected_protocol)); | |
5c753de6 TS |
138 | return 0; |
139 | } | |
d2b23cd2 EK |
140 | if (ctx->expected_servername != ctx2->expected_servername) { |
141 | fprintf(stderr, "ExpectedServerName mismatch: %s vs %s.\n", | |
142 | ssl_servername_name(ctx->expected_servername), | |
143 | ssl_servername_name(ctx2->expected_servername)); | |
144 | return 0; | |
145 | } | |
5c753de6 TS |
146 | if (ctx->session_ticket_expected != ctx2->session_ticket_expected) { |
147 | fprintf(stderr, "SessionTicketExpected mismatch: %s vs %s.\n", | |
81fc33c9 EK |
148 | ssl_session_ticket_name(ctx->session_ticket_expected), |
149 | ssl_session_ticket_name(ctx2->session_ticket_expected)); | |
5c753de6 TS |
150 | return 0; |
151 | } | |
ce2cdac2 EK |
152 | if (!strings_equal("ExpectedNPNProtocol", ctx->expected_npn_protocol, |
153 | ctx2->expected_npn_protocol)) | |
154 | return 0; | |
ce2cdac2 EK |
155 | if (!strings_equal("ExpectedALPNProtocol", ctx->expected_alpn_protocol, |
156 | ctx2->expected_alpn_protocol)) | |
157 | return 0; | |
590ed3d7 EK |
158 | if (ctx->resumption_expected != ctx2->resumption_expected) { |
159 | fprintf(stderr, "ResumptionExpected mismatch: %d vs %d.\n", | |
160 | ctx->resumption_expected, ctx2->resumption_expected); | |
161 | return 0; | |
162 | } | |
453dfd8d EK |
163 | return 1; |
164 | } | |
165 | ||
166 | static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name) | |
167 | { | |
168 | SSL_TEST_CTX_TEST_FIXTURE fixture; | |
169 | fixture.test_case_name = test_case_name; | |
170 | fixture.expected_ctx = SSL_TEST_CTX_new(); | |
171 | OPENSSL_assert(fixture.expected_ctx != NULL); | |
172 | return fixture; | |
173 | } | |
174 | ||
175 | static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture) | |
176 | { | |
ababe86b | 177 | int success = 0; |
453dfd8d EK |
178 | |
179 | SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section); | |
180 | ||
181 | if (ctx == NULL) { | |
182 | fprintf(stderr, "Failed to parse good configuration %s.\n", | |
183 | fixture.test_section); | |
184 | goto err; | |
185 | } | |
186 | ||
187 | if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx)) | |
188 | goto err; | |
189 | ||
ababe86b | 190 | success = 1; |
453dfd8d EK |
191 | err: |
192 | SSL_TEST_CTX_free(ctx); | |
ababe86b | 193 | return success; |
453dfd8d EK |
194 | } |
195 | ||
196 | static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture) | |
197 | { | |
198 | SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section); | |
199 | ||
200 | if (ctx != NULL) { | |
201 | fprintf(stderr, "Parsing bad configuration %s succeeded.\n", | |
202 | fixture.test_section); | |
203 | SSL_TEST_CTX_free(ctx); | |
ababe86b | 204 | return 0; |
453dfd8d EK |
205 | } |
206 | ||
ababe86b | 207 | return 1; |
453dfd8d EK |
208 | } |
209 | ||
210 | static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture) | |
211 | { | |
212 | SSL_TEST_CTX_free(fixture.expected_ctx); | |
213 | ERR_print_errors_fp(stderr); | |
214 | } | |
215 | ||
216 | #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \ | |
217 | SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up) | |
218 | #define EXECUTE_SSL_TEST_CTX_TEST() \ | |
219 | EXECUTE_TEST(execute_test, tear_down) | |
220 | #define EXECUTE_SSL_TEST_CTX_FAILURE_TEST() \ | |
221 | EXECUTE_TEST(execute_failure_test, tear_down) | |
222 | ||
223 | static int test_empty_configuration() | |
224 | { | |
225 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
226 | fixture.test_section = "ssltest_default"; | |
227 | fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS; | |
228 | EXECUTE_SSL_TEST_CTX_TEST(); | |
229 | } | |
230 | ||
231 | static int test_good_configuration() | |
232 | { | |
233 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
234 | fixture.test_section = "ssltest_good"; | |
9f48bbac EK |
235 | fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS; |
236 | fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME; | |
237 | ||
453dfd8d | 238 | fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL; |
9f48bbac EK |
239 | fixture.expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA; |
240 | fixture.expected_ctx->expected_server_alert = 0; /* No alert. */ | |
241 | fixture.expected_ctx->expected_protocol = TLS1_1_VERSION; | |
d2b23cd2 | 242 | fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2; |
5c753de6 | 243 | fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES; |
590ed3d7 | 244 | fixture.expected_ctx->resumption_expected = 1; |
9f48bbac EK |
245 | |
246 | fixture.expected_ctx->extra.client.verify_callback = | |
247 | SSL_TEST_VERIFY_REJECT_ALL; | |
248 | fixture.expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2; | |
249 | fixture.expected_ctx->extra.client.npn_protocols = | |
250 | OPENSSL_strdup("foo,bar"); | |
251 | OPENSSL_assert(fixture.expected_ctx->extra.client.npn_protocols != NULL); | |
252 | ||
253 | fixture.expected_ctx->extra.server.servername_callback = | |
254 | SSL_TEST_SERVERNAME_IGNORE_MISMATCH; | |
255 | fixture.expected_ctx->extra.server.broken_session_ticket = 1; | |
256 | ||
257 | fixture.expected_ctx->resume_extra.server2.alpn_protocols = | |
258 | OPENSSL_strdup("baz"); | |
259 | OPENSSL_assert( | |
260 | fixture.expected_ctx->resume_extra.server2.alpn_protocols != NULL); | |
261 | ||
453dfd8d EK |
262 | EXECUTE_SSL_TEST_CTX_TEST(); |
263 | } | |
264 | ||
265 | static const char *bad_configurations[] = { | |
266 | "ssltest_unknown_option", | |
6bd3379a | 267 | "ssltest_wrong_section", |
453dfd8d EK |
268 | "ssltest_unknown_expected_result", |
269 | "ssltest_unknown_alert", | |
270 | "ssltest_unknown_protocol", | |
a263f320 | 271 | "ssltest_unknown_verify_callback", |
5c753de6 | 272 | "ssltest_unknown_servername", |
d2b23cd2 | 273 | "ssltest_unknown_servername_callback", |
5c753de6 | 274 | "ssltest_unknown_session_ticket_expected", |
74726750 | 275 | "ssltest_unknown_method", |
590ed3d7 EK |
276 | "ssltest_unknown_handshake_mode", |
277 | "ssltest_unknown_resumption_expected", | |
453dfd8d EK |
278 | }; |
279 | ||
280 | static int test_bad_configuration(int idx) | |
281 | { | |
282 | SETUP_SSL_TEST_CTX_TEST_FIXTURE(); | |
283 | fixture.test_section = bad_configurations[idx]; | |
284 | EXECUTE_SSL_TEST_CTX_FAILURE_TEST(); | |
285 | } | |
286 | ||
287 | int main(int argc, char **argv) | |
288 | { | |
289 | int result = 0; | |
290 | ||
291 | if (argc != 2) | |
292 | return 1; | |
293 | ||
294 | conf = NCONF_new(NULL); | |
295 | OPENSSL_assert(conf != NULL); | |
296 | ||
297 | /* argv[1] should point to test/ssl_test_ctx_test.conf */ | |
298 | OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0); | |
299 | ||
300 | ||
301 | ADD_TEST(test_empty_configuration); | |
302 | ADD_TEST(test_good_configuration); | |
303 | ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations)); | |
304 | ||
305 | result = run_tests(argv[0]); | |
306 | ||
307 | NCONF_free(conf); | |
308 | ||
309 | return result; | |
310 | } |