]>
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 | #include <stdio.h> | |
5c753de6 | 11 | #include <string.h> |
453dfd8d EK |
12 | |
13 | #include <openssl/conf.h> | |
14 | #include <openssl/err.h> | |
15 | #include <openssl/ssl.h> | |
16 | ||
17 | #include "handshake_helper.h" | |
18 | #include "ssl_test_ctx.h" | |
19 | #include "testutil.h" | |
e364c3b2 | 20 | #include "test_main_custom.h" |
453dfd8d EK |
21 | |
22 | static CONF *conf = NULL; | |
23 | ||
24 | /* Currently the section names are of the form test-<number>, e.g. test-15. */ | |
25 | #define MAX_TESTCASE_NAME_LENGTH 100 | |
26 | ||
453dfd8d EK |
27 | static const char *print_alert(int alert) |
28 | { | |
29 | return alert ? SSL_alert_desc_string_long(alert) : "no alert"; | |
30 | } | |
31 | ||
ce2cdac2 | 32 | static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
453dfd8d | 33 | { |
2fae041d P |
34 | if (!TEST_int_eq(result->result, test_ctx->expected_result)) { |
35 | TEST_info("ExpectedResult mismatch: expected %s, got %s.", | |
36 | ssl_test_result_name(test_ctx->expected_result), | |
37 | ssl_test_result_name(result->result)); | |
453dfd8d EK |
38 | return 0; |
39 | } | |
40 | return 1; | |
41 | } | |
42 | ||
ce2cdac2 | 43 | static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
453dfd8d | 44 | { |
2fae041d P |
45 | if (!TEST_int_eq(result->client_alert_sent, |
46 | result->client_alert_received)) { | |
47 | TEST_info("Client sent alert %s but server received %s.", | |
48 | print_alert(result->client_alert_sent), | |
49 | print_alert(result->client_alert_received)); | |
453dfd8d EK |
50 | /* |
51 | * We can't bail here because the peer doesn't always get far enough | |
52 | * to process a received alert. Specifically, in protocol version | |
53 | * negotiation tests, we have the following scenario. | |
54 | * Client supports TLS v1.2 only; Server supports TLS v1.1. | |
55 | * Client proposes TLS v1.2; server responds with 1.1; | |
56 | * Client now sends a protocol alert, using TLS v1.2 in the header. | |
57 | * The server, however, rejects the alert because of version mismatch | |
58 | * in the record layer; therefore, the server appears to never | |
59 | * receive the alert. | |
60 | */ | |
61 | /* return 0; */ | |
62 | } | |
63 | ||
2fae041d P |
64 | if (!TEST_int_eq(result->server_alert_sent, |
65 | result->server_alert_received)) { | |
66 | TEST_info("Server sent alert %s but client received %s.", | |
67 | print_alert(result->server_alert_sent), | |
68 | print_alert(result->server_alert_received)); | |
453dfd8d EK |
69 | /* return 0; */ |
70 | } | |
71 | ||
72 | /* Tolerate an alert if one wasn't explicitly specified in the test. */ | |
9f48bbac | 73 | if (test_ctx->expected_client_alert |
453dfd8d EK |
74 | /* |
75 | * The info callback alert value is computed as | |
76 | * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1] | |
77 | * where the low byte is the alert code and the high byte is other stuff. | |
78 | */ | |
9f48bbac | 79 | && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) { |
2fae041d P |
80 | TEST_error("ClientAlert mismatch: expected %s, got %s.", |
81 | print_alert(test_ctx->expected_client_alert), | |
82 | print_alert(result->client_alert_sent)); | |
453dfd8d EK |
83 | return 0; |
84 | } | |
85 | ||
9f48bbac EK |
86 | if (test_ctx->expected_server_alert |
87 | && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) { | |
2fae041d P |
88 | TEST_error("ServerAlert mismatch: expected %s, got %s.", |
89 | print_alert(test_ctx->expected_server_alert), | |
90 | print_alert(result->server_alert_sent)); | |
453dfd8d EK |
91 | return 0; |
92 | } | |
93 | ||
2fae041d | 94 | if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1)) |
dd8e5a57 | 95 | return 0; |
2fae041d | 96 | if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1)) |
dd8e5a57 | 97 | return 0; |
453dfd8d EK |
98 | return 1; |
99 | } | |
100 | ||
ce2cdac2 | 101 | static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
453dfd8d | 102 | { |
2fae041d P |
103 | if (!TEST_int_eq(result->client_protocol, result->server_protocol)) { |
104 | TEST_info("Client has protocol %s but server has %s.", | |
105 | ssl_protocol_name(result->client_protocol), | |
106 | ssl_protocol_name(result->server_protocol)); | |
453dfd8d EK |
107 | return 0; |
108 | } | |
109 | ||
9f48bbac | 110 | if (test_ctx->expected_protocol) { |
2fae041d P |
111 | if (!TEST_int_eq(result->client_protocol, |
112 | test_ctx->expected_protocol)) { | |
113 | TEST_info("Protocol mismatch: expected %s, got %s.\n", | |
114 | ssl_protocol_name(test_ctx->expected_protocol), | |
115 | ssl_protocol_name(result->client_protocol)); | |
453dfd8d EK |
116 | return 0; |
117 | } | |
118 | } | |
119 | return 1; | |
120 | } | |
121 | ||
ce2cdac2 | 122 | static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
5c753de6 | 123 | { |
2fae041d P |
124 | if (!TEST_int_eq(result->servername, test_ctx->expected_servername)) { |
125 | TEST_info("Client ServerName mismatch, expected %s, got %s.", | |
126 | ssl_servername_name(test_ctx->expected_servername), | |
127 | ssl_servername_name(result->servername)); | |
d2b23cd2 | 128 | return 0; |
5c753de6 | 129 | } |
d2b23cd2 | 130 | return 1; |
5c753de6 TS |
131 | } |
132 | ||
ce2cdac2 | 133 | static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
5c753de6 TS |
134 | { |
135 | if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE) | |
136 | return 1; | |
2fae041d P |
137 | if (!TEST_int_eq(result->session_ticket, |
138 | test_ctx->session_ticket_expected)) { | |
139 | TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.", | |
140 | ssl_session_ticket_name(test_ctx->session_ticket_expected), | |
141 | ssl_session_ticket_name(result->session_ticket)); | |
5c753de6 TS |
142 | return 0; |
143 | } | |
144 | return 1; | |
145 | } | |
146 | ||
439db0c9 MC |
147 | static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
148 | { | |
2fae041d | 149 | if (!TEST_int_eq(result->compression, test_ctx->compression_expected)) |
439db0c9 | 150 | return 0; |
439db0c9 MC |
151 | return 1; |
152 | } | |
620c6ad3 | 153 | #ifndef OPENSSL_NO_NEXTPROTONEG |
ce2cdac2 EK |
154 | static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
155 | { | |
156 | int ret = 1; | |
2fae041d P |
157 | if (!TEST_str_eq(result->client_npn_negotiated, |
158 | result->server_npn_negotiated)) | |
159 | ret = 0; | |
160 | if (!TEST_str_eq(test_ctx->expected_npn_protocol, | |
161 | result->client_npn_negotiated)) | |
162 | ret = 0; | |
ce2cdac2 EK |
163 | return ret; |
164 | } | |
7b7cea6d | 165 | #endif |
ce2cdac2 EK |
166 | |
167 | static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) | |
168 | { | |
169 | int ret = 1; | |
2fae041d P |
170 | if (!TEST_str_eq(result->client_alpn_negotiated, |
171 | result->server_alpn_negotiated)) | |
172 | ret = 0; | |
173 | if (!TEST_str_eq(test_ctx->expected_alpn_protocol, | |
174 | result->client_alpn_negotiated)) | |
175 | ret = 0; | |
ce2cdac2 EK |
176 | return ret; |
177 | } | |
178 | ||
590ed3d7 EK |
179 | static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
180 | { | |
2fae041d | 181 | if (!TEST_int_eq(result->client_resumed, result->server_resumed)) |
590ed3d7 | 182 | return 0; |
2fae041d | 183 | if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected)) |
590ed3d7 | 184 | return 0; |
590ed3d7 EK |
185 | return 1; |
186 | } | |
187 | ||
ee5b6a42 | 188 | static int check_nid(const char *name, int expected_nid, int nid) |
b93ad05d | 189 | { |
ee5b6a42 | 190 | if (expected_nid == 0 || expected_nid == nid) |
b93ad05d | 191 | return 1; |
2fae041d P |
192 | TEST_error("%s type mismatch, %s vs %s\n", |
193 | name, OBJ_nid2ln(expected_nid), | |
194 | nid == NID_undef ? "absent" : OBJ_nid2ln(nid)); | |
b93ad05d DSH |
195 | return 0; |
196 | } | |
197 | ||
7f5f35af DSH |
198 | static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
199 | { | |
ee5b6a42 DSH |
200 | return check_nid("Tmp key", test_ctx->expected_tmp_key_type, |
201 | result->tmp_key_type); | |
7f5f35af DSH |
202 | } |
203 | ||
204 | static int check_server_cert_type(HANDSHAKE_RESULT *result, | |
205 | SSL_TEST_CTX *test_ctx) | |
206 | { | |
ee5b6a42 DSH |
207 | return check_nid("Server certificate", test_ctx->expected_server_cert_type, |
208 | result->server_cert_type); | |
209 | } | |
210 | ||
211 | static int check_server_sign_hash(HANDSHAKE_RESULT *result, | |
212 | SSL_TEST_CTX *test_ctx) | |
213 | { | |
214 | return check_nid("Server signing hash", test_ctx->expected_server_sign_hash, | |
215 | result->server_sign_hash); | |
7f5f35af DSH |
216 | } |
217 | ||
54b7f2a5 DSH |
218 | static int check_server_sign_type(HANDSHAKE_RESULT *result, |
219 | SSL_TEST_CTX *test_ctx) | |
220 | { | |
221 | return check_nid("Server signing", test_ctx->expected_server_sign_type, | |
222 | result->server_sign_type); | |
223 | } | |
224 | ||
7f5f35af DSH |
225 | static int check_client_cert_type(HANDSHAKE_RESULT *result, |
226 | SSL_TEST_CTX *test_ctx) | |
227 | { | |
ee5b6a42 DSH |
228 | return check_nid("Client certificate", test_ctx->expected_client_cert_type, |
229 | result->client_cert_type); | |
230 | } | |
231 | ||
232 | static int check_client_sign_hash(HANDSHAKE_RESULT *result, | |
233 | SSL_TEST_CTX *test_ctx) | |
234 | { | |
235 | return check_nid("Client signing hash", test_ctx->expected_client_sign_hash, | |
236 | result->client_sign_hash); | |
7f5f35af DSH |
237 | } |
238 | ||
54b7f2a5 DSH |
239 | static int check_client_sign_type(HANDSHAKE_RESULT *result, |
240 | SSL_TEST_CTX *test_ctx) | |
241 | { | |
242 | return check_nid("Client signing", test_ctx->expected_client_sign_type, | |
243 | result->client_sign_type); | |
244 | } | |
245 | ||
2e21539b DSH |
246 | static void print_ca_names(STACK_OF(X509_NAME) *names) |
247 | { | |
248 | BIO *err; | |
249 | int i; | |
250 | ||
251 | if (names == NULL || sk_X509_NAME_num(names) == 0) { | |
252 | fprintf(stderr, " <empty>\n"); | |
253 | return; | |
254 | } | |
255 | err = BIO_new_fp(stderr, BIO_NOCLOSE); | |
256 | for (i = 0; i < sk_X509_NAME_num(names); i++) { | |
257 | X509_NAME_print_ex(err, sk_X509_NAME_value(names, i), 4, | |
258 | XN_FLAG_ONELINE); | |
259 | BIO_puts(err, "\n"); | |
260 | } | |
261 | BIO_free(err); | |
262 | } | |
263 | ||
264 | static int check_ca_names(const char *name, | |
265 | STACK_OF(X509_NAME) *expected_names, | |
266 | STACK_OF(X509_NAME) *names) | |
267 | { | |
268 | int i; | |
269 | ||
270 | if (expected_names == NULL) | |
271 | return 1; | |
272 | if (names == NULL || sk_X509_NAME_num(names) == 0) { | |
273 | if (sk_X509_NAME_num(expected_names) == 0) | |
274 | return 1; | |
275 | goto err; | |
276 | } | |
277 | if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names)) | |
278 | goto err; | |
279 | for (i = 0; i < sk_X509_NAME_num(names); i++) { | |
280 | if (X509_NAME_cmp(sk_X509_NAME_value(names, i), | |
281 | sk_X509_NAME_value(expected_names, i)) != 0) { | |
282 | goto err; | |
283 | } | |
284 | } | |
285 | return 1; | |
286 | err: | |
287 | fprintf(stderr, "%s: list mismatch\nExpected Names:\n", name); | |
288 | print_ca_names(expected_names); | |
289 | fprintf(stderr, "Received Names:\n"); | |
290 | print_ca_names(names); | |
291 | return 0; | |
292 | } | |
293 | ||
294 | static int check_client_ca_names(HANDSHAKE_RESULT *result, | |
295 | SSL_TEST_CTX *test_ctx) | |
296 | { | |
297 | return check_ca_names("Client CA names", | |
298 | test_ctx->expected_client_ca_names, | |
299 | result->client_ca_names); | |
300 | } | |
301 | ||
453dfd8d EK |
302 | /* |
303 | * This could be further simplified by constructing an expected | |
304 | * HANDSHAKE_RESULT, and implementing comparison methods for | |
305 | * its fields. | |
306 | */ | |
ce2cdac2 | 307 | static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) |
453dfd8d EK |
308 | { |
309 | int ret = 1; | |
310 | ret &= check_result(result, test_ctx); | |
311 | ret &= check_alerts(result, test_ctx); | |
ce2cdac2 | 312 | if (result->result == SSL_TEST_SUCCESS) { |
453dfd8d | 313 | ret &= check_protocol(result, test_ctx); |
5c753de6 | 314 | ret &= check_servername(result, test_ctx); |
81fc33c9 | 315 | ret &= check_session_ticket(result, test_ctx); |
439db0c9 | 316 | ret &= check_compression(result, test_ctx); |
ce2cdac2 | 317 | ret &= (result->session_ticket_do_not_call == 0); |
620c6ad3 | 318 | #ifndef OPENSSL_NO_NEXTPROTONEG |
ce2cdac2 | 319 | ret &= check_npn(result, test_ctx); |
620c6ad3 | 320 | #endif |
7b7cea6d | 321 | ret &= check_alpn(result, test_ctx); |
590ed3d7 | 322 | ret &= check_resumption(result, test_ctx); |
b93ad05d | 323 | ret &= check_tmp_key(result, test_ctx); |
7f5f35af | 324 | ret &= check_server_cert_type(result, test_ctx); |
ee5b6a42 | 325 | ret &= check_server_sign_hash(result, test_ctx); |
54b7f2a5 | 326 | ret &= check_server_sign_type(result, test_ctx); |
7f5f35af | 327 | ret &= check_client_cert_type(result, test_ctx); |
ee5b6a42 | 328 | ret &= check_client_sign_hash(result, test_ctx); |
54b7f2a5 | 329 | ret &= check_client_sign_type(result, test_ctx); |
2e21539b | 330 | ret &= check_client_ca_names(result, test_ctx); |
5c753de6 | 331 | } |
453dfd8d EK |
332 | return ret; |
333 | } | |
334 | ||
d836d71b | 335 | static int test_handshake(int idx) |
453dfd8d | 336 | { |
ababe86b | 337 | int ret = 0; |
590ed3d7 | 338 | SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL, |
11279b13 | 339 | *resume_server_ctx = NULL, *resume_client_ctx = NULL; |
453dfd8d | 340 | SSL_TEST_CTX *test_ctx = NULL; |
ce2cdac2 | 341 | HANDSHAKE_RESULT *result = NULL; |
d836d71b EK |
342 | char test_app[MAX_TESTCASE_NAME_LENGTH]; |
343 | ||
344 | BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx); | |
453dfd8d | 345 | |
d836d71b | 346 | test_ctx = SSL_TEST_CTX_create(conf, test_app); |
2fae041d | 347 | if (!TEST_ptr(test_ctx)) |
74726750 EK |
348 | goto err; |
349 | ||
350 | #ifndef OPENSSL_NO_DTLS | |
351 | if (test_ctx->method == SSL_TEST_METHOD_DTLS) { | |
352 | server_ctx = SSL_CTX_new(DTLS_server_method()); | |
9f48bbac EK |
353 | if (test_ctx->extra.server.servername_callback != |
354 | SSL_TEST_SERVERNAME_CB_NONE) { | |
d2b23cd2 | 355 | server2_ctx = SSL_CTX_new(DTLS_server_method()); |
d61f0078 | 356 | TEST_check(server2_ctx != NULL); |
d2b23cd2 | 357 | } |
74726750 | 358 | client_ctx = SSL_CTX_new(DTLS_client_method()); |
590ed3d7 EK |
359 | if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) { |
360 | resume_server_ctx = SSL_CTX_new(DTLS_server_method()); | |
11279b13 | 361 | resume_client_ctx = SSL_CTX_new(DTLS_client_method()); |
d61f0078 EK |
362 | TEST_check(resume_server_ctx != NULL); |
363 | TEST_check(resume_client_ctx != NULL); | |
590ed3d7 | 364 | } |
74726750 EK |
365 | } |
366 | #endif | |
367 | if (test_ctx->method == SSL_TEST_METHOD_TLS) { | |
368 | server_ctx = SSL_CTX_new(TLS_server_method()); | |
9f48bbac EK |
369 | /* SNI on resumption isn't supported/tested yet. */ |
370 | if (test_ctx->extra.server.servername_callback != | |
371 | SSL_TEST_SERVERNAME_CB_NONE) { | |
d2b23cd2 | 372 | server2_ctx = SSL_CTX_new(TLS_server_method()); |
d61f0078 | 373 | TEST_check(server2_ctx != NULL); |
d2b23cd2 | 374 | } |
74726750 | 375 | client_ctx = SSL_CTX_new(TLS_client_method()); |
590ed3d7 EK |
376 | |
377 | if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) { | |
378 | resume_server_ctx = SSL_CTX_new(TLS_server_method()); | |
11279b13 | 379 | resume_client_ctx = SSL_CTX_new(TLS_client_method()); |
d61f0078 EK |
380 | TEST_check(resume_server_ctx != NULL); |
381 | TEST_check(resume_client_ctx != NULL); | |
590ed3d7 | 382 | } |
74726750 EK |
383 | } |
384 | ||
d61f0078 EK |
385 | TEST_check(server_ctx != NULL); |
386 | TEST_check(client_ctx != NULL); | |
453dfd8d | 387 | |
d836d71b | 388 | TEST_check(CONF_modules_load(conf, test_app, 0) > 0); |
453dfd8d EK |
389 | |
390 | if (!SSL_CTX_config(server_ctx, "server") | |
5c753de6 | 391 | || !SSL_CTX_config(client_ctx, "client")) { |
453dfd8d EK |
392 | goto err; |
393 | } | |
394 | ||
d2b23cd2 EK |
395 | if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2")) |
396 | goto err; | |
590ed3d7 EK |
397 | if (resume_server_ctx != NULL |
398 | && !SSL_CTX_config(resume_server_ctx, "resume-server")) | |
399 | goto err; | |
11279b13 EK |
400 | if (resume_client_ctx != NULL |
401 | && !SSL_CTX_config(resume_client_ctx, "resume-client")) | |
402 | goto err; | |
d2b23cd2 | 403 | |
590ed3d7 | 404 | result = do_handshake(server_ctx, server2_ctx, client_ctx, |
11279b13 | 405 | resume_server_ctx, resume_client_ctx, test_ctx); |
453dfd8d | 406 | |
ababe86b | 407 | ret = check_test(result, test_ctx); |
453dfd8d EK |
408 | |
409 | err: | |
410 | CONF_modules_unload(0); | |
411 | SSL_CTX_free(server_ctx); | |
5c753de6 | 412 | SSL_CTX_free(server2_ctx); |
453dfd8d | 413 | SSL_CTX_free(client_ctx); |
590ed3d7 | 414 | SSL_CTX_free(resume_server_ctx); |
11279b13 | 415 | SSL_CTX_free(resume_client_ctx); |
453dfd8d | 416 | SSL_TEST_CTX_free(test_ctx); |
ce2cdac2 | 417 | HANDSHAKE_RESULT_free(result); |
453dfd8d EK |
418 | return ret; |
419 | } | |
420 | ||
e364c3b2 | 421 | int test_main(int argc, char **argv) |
453dfd8d EK |
422 | { |
423 | int result = 0; | |
424 | long num_tests; | |
425 | ||
426 | if (argc != 2) | |
427 | return 1; | |
428 | ||
429 | conf = NCONF_new(NULL); | |
d61f0078 | 430 | TEST_check(conf != NULL); |
453dfd8d EK |
431 | |
432 | /* argv[1] should point to the test conf file */ | |
d61f0078 | 433 | TEST_check(NCONF_load(conf, argv[1], NULL) > 0); |
453dfd8d | 434 | |
d61f0078 | 435 | TEST_check(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests)); |
453dfd8d EK |
436 | |
437 | ADD_ALL_TESTS(test_handshake, (int)(num_tests)); | |
438 | result = run_tests(argv[0]); | |
439 | ||
e364c3b2 | 440 | NCONF_free(conf); |
453dfd8d EK |
441 | return result; |
442 | } |