2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
13 #include <openssl/conf.h>
14 #include <openssl/err.h>
15 #include <openssl/ssl.h>
17 #include "handshake_helper.h"
18 #include "ssl_test_ctx.h"
21 static CONF *conf = NULL;
23 /* Currently the section names are of the form test-<number>, e.g. test-15. */
24 #define MAX_TESTCASE_NAME_LENGTH 100
26 static const char *print_alert(int alert)
28 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
31 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
33 if (!TEST_int_eq(result->result, test_ctx->expected_result)) {
34 TEST_info("ExpectedResult mismatch: expected %s, got %s.",
35 ssl_test_result_name(test_ctx->expected_result),
36 ssl_test_result_name(result->result));
42 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
44 if (!TEST_int_eq(result->client_alert_sent,
45 result->client_alert_received)) {
46 TEST_info("Client sent alert %s but server received %s.",
47 print_alert(result->client_alert_sent),
48 print_alert(result->client_alert_received));
50 * We can't bail here because the peer doesn't always get far enough
51 * to process a received alert. Specifically, in protocol version
52 * negotiation tests, we have the following scenario.
53 * Client supports TLS v1.2 only; Server supports TLS v1.1.
54 * Client proposes TLS v1.2; server responds with 1.1;
55 * Client now sends a protocol alert, using TLS v1.2 in the header.
56 * The server, however, rejects the alert because of version mismatch
57 * in the record layer; therefore, the server appears to never
63 if (!TEST_int_eq(result->server_alert_sent,
64 result->server_alert_received)) {
65 TEST_info("Server sent alert %s but client received %s.",
66 print_alert(result->server_alert_sent),
67 print_alert(result->server_alert_received));
71 /* Tolerate an alert if one wasn't explicitly specified in the test. */
72 if (test_ctx->expected_client_alert
74 * The info callback alert value is computed as
75 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
76 * where the low byte is the alert code and the high byte is other stuff.
78 && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
79 TEST_error("ClientAlert mismatch: expected %s, got %s.",
80 print_alert(test_ctx->expected_client_alert),
81 print_alert(result->client_alert_sent));
85 if (test_ctx->expected_server_alert
86 && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
87 TEST_error("ServerAlert mismatch: expected %s, got %s.",
88 print_alert(test_ctx->expected_server_alert),
89 print_alert(result->server_alert_sent));
93 if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1))
95 if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1))
100 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
102 if (!TEST_int_eq(result->client_protocol, result->server_protocol)) {
103 TEST_info("Client has protocol %s but server has %s.",
104 ssl_protocol_name(result->client_protocol),
105 ssl_protocol_name(result->server_protocol));
109 if (test_ctx->expected_protocol) {
110 if (!TEST_int_eq(result->client_protocol,
111 test_ctx->expected_protocol)) {
112 TEST_info("Protocol mismatch: expected %s, got %s.\n",
113 ssl_protocol_name(test_ctx->expected_protocol),
114 ssl_protocol_name(result->client_protocol));
121 static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
123 if (!TEST_int_eq(result->servername, test_ctx->expected_servername)) {
124 TEST_info("Client ServerName mismatch, expected %s, got %s.",
125 ssl_servername_name(test_ctx->expected_servername),
126 ssl_servername_name(result->servername));
132 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
134 if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
136 if (!TEST_int_eq(result->session_ticket,
137 test_ctx->session_ticket_expected)) {
138 TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.",
139 ssl_session_ticket_name(test_ctx->session_ticket_expected),
140 ssl_session_ticket_name(result->session_ticket));
146 static int check_session_id(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
148 if (test_ctx->session_id_expected == SSL_TEST_SESSION_ID_IGNORE)
150 if (!TEST_int_eq(result->session_id, test_ctx->session_id_expected)) {
151 TEST_info("Client SessionIdExpected mismatch, expected %s, got %s\n.",
152 ssl_session_id_name(test_ctx->session_id_expected),
153 ssl_session_id_name(result->session_id));
159 static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
161 if (!TEST_int_eq(result->compression, test_ctx->compression_expected))
165 #ifndef OPENSSL_NO_NEXTPROTONEG
166 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
169 if (!TEST_str_eq(result->client_npn_negotiated,
170 result->server_npn_negotiated))
172 if (!TEST_str_eq(test_ctx->expected_npn_protocol,
173 result->client_npn_negotiated))
179 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
182 if (!TEST_str_eq(result->client_alpn_negotiated,
183 result->server_alpn_negotiated))
185 if (!TEST_str_eq(test_ctx->expected_alpn_protocol,
186 result->client_alpn_negotiated))
191 static int check_session_ticket_app_data(HANDSHAKE_RESULT *result,
192 SSL_TEST_CTX *test_ctx)
194 size_t result_len = 0;
195 size_t expected_len = 0;
197 /* consider empty and NULL strings to be the same */
198 if (result->result_session_ticket_app_data != NULL)
199 result_len = strlen(result->result_session_ticket_app_data);
200 if (test_ctx->expected_session_ticket_app_data != NULL)
201 expected_len = strlen(test_ctx->expected_session_ticket_app_data);
202 if (result_len == 0 && expected_len == 0)
205 if (!TEST_str_eq(result->result_session_ticket_app_data,
206 test_ctx->expected_session_ticket_app_data))
212 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
214 if (!TEST_int_eq(result->client_resumed, result->server_resumed))
216 if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected))
221 static int check_nid(const char *name, int expected_nid, int nid)
223 if (expected_nid == 0 || expected_nid == nid)
225 TEST_error("%s type mismatch, %s vs %s\n",
226 name, OBJ_nid2ln(expected_nid),
227 nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
231 static void print_ca_names(STACK_OF(X509_NAME) *names)
235 if (names == NULL || sk_X509_NAME_num(names) == 0) {
236 TEST_note(" <empty>");
239 for (i = 0; i < sk_X509_NAME_num(names); i++) {
240 X509_NAME_print_ex(bio_err, sk_X509_NAME_value(names, i), 4,
242 BIO_puts(bio_err, "\n");
246 static int check_ca_names(const char *name,
247 STACK_OF(X509_NAME) *expected_names,
248 STACK_OF(X509_NAME) *names)
252 if (expected_names == NULL)
254 if (names == NULL || sk_X509_NAME_num(names) == 0) {
255 if (TEST_int_eq(sk_X509_NAME_num(expected_names), 0))
259 if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
261 for (i = 0; i < sk_X509_NAME_num(names); i++) {
262 if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names, i),
263 sk_X509_NAME_value(expected_names, i)),
270 TEST_info("%s: list mismatch", name);
271 TEST_note("Expected Names:");
272 print_ca_names(expected_names);
273 TEST_note("Received Names:");
274 print_ca_names(names);
278 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
280 return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
281 result->tmp_key_type);
284 static int check_server_cert_type(HANDSHAKE_RESULT *result,
285 SSL_TEST_CTX *test_ctx)
287 return check_nid("Server certificate", test_ctx->expected_server_cert_type,
288 result->server_cert_type);
291 static int check_server_sign_hash(HANDSHAKE_RESULT *result,
292 SSL_TEST_CTX *test_ctx)
294 return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
295 result->server_sign_hash);
298 static int check_server_sign_type(HANDSHAKE_RESULT *result,
299 SSL_TEST_CTX *test_ctx)
301 return check_nid("Server signing", test_ctx->expected_server_sign_type,
302 result->server_sign_type);
305 static int check_server_ca_names(HANDSHAKE_RESULT *result,
306 SSL_TEST_CTX *test_ctx)
308 return check_ca_names("Server CA names",
309 test_ctx->expected_server_ca_names,
310 result->server_ca_names);
313 static int check_client_cert_type(HANDSHAKE_RESULT *result,
314 SSL_TEST_CTX *test_ctx)
316 return check_nid("Client certificate", test_ctx->expected_client_cert_type,
317 result->client_cert_type);
320 static int check_client_sign_hash(HANDSHAKE_RESULT *result,
321 SSL_TEST_CTX *test_ctx)
323 return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
324 result->client_sign_hash);
327 static int check_client_sign_type(HANDSHAKE_RESULT *result,
328 SSL_TEST_CTX *test_ctx)
330 return check_nid("Client signing", test_ctx->expected_client_sign_type,
331 result->client_sign_type);
334 static int check_client_ca_names(HANDSHAKE_RESULT *result,
335 SSL_TEST_CTX *test_ctx)
337 return check_ca_names("Client CA names",
338 test_ctx->expected_client_ca_names,
339 result->client_ca_names);
342 static int check_cipher(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
344 if (test_ctx->expected_cipher == NULL)
346 if (!TEST_ptr(result->cipher))
348 if (!TEST_str_eq(test_ctx->expected_cipher,
355 * This could be further simplified by constructing an expected
356 * HANDSHAKE_RESULT, and implementing comparison methods for
359 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
362 ret &= check_result(result, test_ctx);
363 ret &= check_alerts(result, test_ctx);
364 if (result->result == SSL_TEST_SUCCESS) {
365 ret &= check_protocol(result, test_ctx);
366 ret &= check_servername(result, test_ctx);
367 ret &= check_session_ticket(result, test_ctx);
368 ret &= check_compression(result, test_ctx);
369 ret &= check_session_id(result, test_ctx);
370 ret &= (result->session_ticket_do_not_call == 0);
371 #ifndef OPENSSL_NO_NEXTPROTONEG
372 ret &= check_npn(result, test_ctx);
374 ret &= check_cipher(result, test_ctx);
375 ret &= check_alpn(result, test_ctx);
376 ret &= check_session_ticket_app_data(result, test_ctx);
377 ret &= check_resumption(result, test_ctx);
378 ret &= check_tmp_key(result, test_ctx);
379 ret &= check_server_cert_type(result, test_ctx);
380 ret &= check_server_sign_hash(result, test_ctx);
381 ret &= check_server_sign_type(result, test_ctx);
382 ret &= check_server_ca_names(result, test_ctx);
383 ret &= check_client_cert_type(result, test_ctx);
384 ret &= check_client_sign_hash(result, test_ctx);
385 ret &= check_client_sign_type(result, test_ctx);
386 ret &= check_client_ca_names(result, test_ctx);
391 static int test_handshake(int idx)
394 SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
395 *resume_server_ctx = NULL, *resume_client_ctx = NULL;
396 SSL_TEST_CTX *test_ctx = NULL;
397 HANDSHAKE_RESULT *result = NULL;
398 char test_app[MAX_TESTCASE_NAME_LENGTH];
400 BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
402 test_ctx = SSL_TEST_CTX_create(conf, test_app);
403 if (!TEST_ptr(test_ctx))
406 #ifndef OPENSSL_NO_DTLS
407 if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
408 server_ctx = SSL_CTX_new(DTLS_server_method());
409 if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
411 if (test_ctx->extra.server.servername_callback !=
412 SSL_TEST_SERVERNAME_CB_NONE) {
413 if (!TEST_ptr(server2_ctx = SSL_CTX_new(DTLS_server_method())))
416 client_ctx = SSL_CTX_new(DTLS_client_method());
417 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
419 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
420 resume_server_ctx = SSL_CTX_new(DTLS_server_method());
421 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0)))
423 resume_client_ctx = SSL_CTX_new(DTLS_client_method());
424 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
426 if (!TEST_ptr(resume_server_ctx)
427 || !TEST_ptr(resume_client_ctx))
432 if (test_ctx->method == SSL_TEST_METHOD_TLS) {
433 server_ctx = SSL_CTX_new(TLS_server_method());
434 if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
436 /* SNI on resumption isn't supported/tested yet. */
437 if (test_ctx->extra.server.servername_callback !=
438 SSL_TEST_SERVERNAME_CB_NONE) {
439 if (!TEST_ptr(server2_ctx = SSL_CTX_new(TLS_server_method())))
441 if (!TEST_true(SSL_CTX_set_max_proto_version(server2_ctx, 0)))
444 client_ctx = SSL_CTX_new(TLS_client_method());
445 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
448 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
449 resume_server_ctx = SSL_CTX_new(TLS_server_method());
450 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0)))
452 resume_client_ctx = SSL_CTX_new(TLS_client_method());
453 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
455 if (!TEST_ptr(resume_server_ctx)
456 || !TEST_ptr(resume_client_ctx))
461 #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
462 if (!TEST_true(OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL)))
466 if (!TEST_ptr(server_ctx)
467 || !TEST_ptr(client_ctx)
468 || !TEST_int_gt(CONF_modules_load(conf, test_app, 0), 0))
471 if (!SSL_CTX_config(server_ctx, "server")
472 || !SSL_CTX_config(client_ctx, "client")) {
476 if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
478 if (resume_server_ctx != NULL
479 && !SSL_CTX_config(resume_server_ctx, "resume-server"))
481 if (resume_client_ctx != NULL
482 && !SSL_CTX_config(resume_client_ctx, "resume-client"))
485 result = do_handshake(server_ctx, server2_ctx, client_ctx,
486 resume_server_ctx, resume_client_ctx, test_ctx);
489 ret = check_test(result, test_ctx);
492 CONF_modules_unload(0);
493 SSL_CTX_free(server_ctx);
494 SSL_CTX_free(server2_ctx);
495 SSL_CTX_free(client_ctx);
496 SSL_CTX_free(resume_server_ctx);
497 SSL_CTX_free(resume_client_ctx);
498 SSL_TEST_CTX_free(test_ctx);
499 HANDSHAKE_RESULT_free(result);
503 OPT_TEST_DECLARE_USAGE("conf_file\n")
505 int setup_tests(void)
509 if (!TEST_ptr(conf = NCONF_new(NULL))
510 /* argv[1] should point to the test conf file */
511 || !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)
512 || !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests",
516 ADD_ALL_TESTS(test_handshake, (int)num_tests);
520 void cleanup_tests(void)