2 * Copyright 2016-2020 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>
16 #include <openssl/provider.h>
18 #include "handshake_helper.h"
19 #include "ssl_test_ctx.h"
22 DEFINE_STACK_OF(X509_NAME)
24 static CONF *conf = NULL;
25 static OSSL_PROVIDER *defctxnull = NULL, *thisprov = NULL;
26 static OPENSSL_CTX *libctx = NULL;
28 /* Currently the section names are of the form test-<number>, e.g. test-15. */
29 #define MAX_TESTCASE_NAME_LENGTH 100
31 static const char *print_alert(int alert)
33 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
36 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
38 if (!TEST_int_eq(result->result, test_ctx->expected_result)) {
39 TEST_info("ExpectedResult mismatch: expected %s, got %s.",
40 ssl_test_result_name(test_ctx->expected_result),
41 ssl_test_result_name(result->result));
47 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
49 if (!TEST_int_eq(result->client_alert_sent,
50 result->client_alert_received)) {
51 TEST_info("Client sent alert %s but server received %s.",
52 print_alert(result->client_alert_sent),
53 print_alert(result->client_alert_received));
55 * We can't bail here because the peer doesn't always get far enough
56 * to process a received alert. Specifically, in protocol version
57 * negotiation tests, we have the following scenario.
58 * Client supports TLS v1.2 only; Server supports TLS v1.1.
59 * Client proposes TLS v1.2; server responds with 1.1;
60 * Client now sends a protocol alert, using TLS v1.2 in the header.
61 * The server, however, rejects the alert because of version mismatch
62 * in the record layer; therefore, the server appears to never
68 if (!TEST_int_eq(result->server_alert_sent,
69 result->server_alert_received)) {
70 TEST_info("Server sent alert %s but client received %s.",
71 print_alert(result->server_alert_sent),
72 print_alert(result->server_alert_received));
76 /* Tolerate an alert if one wasn't explicitly specified in the test. */
77 if (test_ctx->expected_client_alert
79 * The info callback alert value is computed as
80 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
81 * where the low byte is the alert code and the high byte is other stuff.
83 && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
84 TEST_error("ClientAlert mismatch: expected %s, got %s.",
85 print_alert(test_ctx->expected_client_alert),
86 print_alert(result->client_alert_sent));
90 if (test_ctx->expected_server_alert
91 && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
92 TEST_error("ServerAlert mismatch: expected %s, got %s.",
93 print_alert(test_ctx->expected_server_alert),
94 print_alert(result->server_alert_sent));
98 if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1))
100 if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1))
105 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
107 if (!TEST_int_eq(result->client_protocol, result->server_protocol)) {
108 TEST_info("Client has protocol %s but server has %s.",
109 ssl_protocol_name(result->client_protocol),
110 ssl_protocol_name(result->server_protocol));
114 if (test_ctx->expected_protocol) {
115 if (!TEST_int_eq(result->client_protocol,
116 test_ctx->expected_protocol)) {
117 TEST_info("Protocol mismatch: expected %s, got %s.\n",
118 ssl_protocol_name(test_ctx->expected_protocol),
119 ssl_protocol_name(result->client_protocol));
126 static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
128 if (!TEST_int_eq(result->servername, test_ctx->expected_servername)) {
129 TEST_info("Client ServerName mismatch, expected %s, got %s.",
130 ssl_servername_name(test_ctx->expected_servername),
131 ssl_servername_name(result->servername));
137 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
139 if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
141 if (!TEST_int_eq(result->session_ticket,
142 test_ctx->session_ticket_expected)) {
143 TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.",
144 ssl_session_ticket_name(test_ctx->session_ticket_expected),
145 ssl_session_ticket_name(result->session_ticket));
151 static int check_session_id(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
153 if (test_ctx->session_id_expected == SSL_TEST_SESSION_ID_IGNORE)
155 if (!TEST_int_eq(result->session_id, test_ctx->session_id_expected)) {
156 TEST_info("Client SessionIdExpected mismatch, expected %s, got %s\n.",
157 ssl_session_id_name(test_ctx->session_id_expected),
158 ssl_session_id_name(result->session_id));
164 static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
166 if (!TEST_int_eq(result->compression, test_ctx->compression_expected))
170 #ifndef OPENSSL_NO_NEXTPROTONEG
171 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
174 if (!TEST_str_eq(result->client_npn_negotiated,
175 result->server_npn_negotiated))
177 if (!TEST_str_eq(test_ctx->expected_npn_protocol,
178 result->client_npn_negotiated))
184 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
187 if (!TEST_str_eq(result->client_alpn_negotiated,
188 result->server_alpn_negotiated))
190 if (!TEST_str_eq(test_ctx->expected_alpn_protocol,
191 result->client_alpn_negotiated))
196 static int check_session_ticket_app_data(HANDSHAKE_RESULT *result,
197 SSL_TEST_CTX *test_ctx)
199 size_t result_len = 0;
200 size_t expected_len = 0;
202 /* consider empty and NULL strings to be the same */
203 if (result->result_session_ticket_app_data != NULL)
204 result_len = strlen(result->result_session_ticket_app_data);
205 if (test_ctx->expected_session_ticket_app_data != NULL)
206 expected_len = strlen(test_ctx->expected_session_ticket_app_data);
207 if (result_len == 0 && expected_len == 0)
210 if (!TEST_str_eq(result->result_session_ticket_app_data,
211 test_ctx->expected_session_ticket_app_data))
217 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
219 if (!TEST_int_eq(result->client_resumed, result->server_resumed))
221 if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected))
226 static int check_nid(const char *name, int expected_nid, int nid)
228 if (expected_nid == 0 || expected_nid == nid)
230 TEST_error("%s type mismatch, %s vs %s\n",
231 name, OBJ_nid2ln(expected_nid),
232 nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
236 static void print_ca_names(STACK_OF(X509_NAME) *names)
240 if (names == NULL || sk_X509_NAME_num(names) == 0) {
241 TEST_note(" <empty>");
244 for (i = 0; i < sk_X509_NAME_num(names); i++) {
245 X509_NAME_print_ex(bio_err, sk_X509_NAME_value(names, i), 4,
247 BIO_puts(bio_err, "\n");
251 static int check_ca_names(const char *name,
252 STACK_OF(X509_NAME) *expected_names,
253 STACK_OF(X509_NAME) *names)
257 if (expected_names == NULL)
259 if (names == NULL || sk_X509_NAME_num(names) == 0) {
260 if (TEST_int_eq(sk_X509_NAME_num(expected_names), 0))
264 if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
266 for (i = 0; i < sk_X509_NAME_num(names); i++) {
267 if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names, i),
268 sk_X509_NAME_value(expected_names, i)),
275 TEST_info("%s: list mismatch", name);
276 TEST_note("Expected Names:");
277 print_ca_names(expected_names);
278 TEST_note("Received Names:");
279 print_ca_names(names);
283 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
285 return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
286 result->tmp_key_type);
289 static int check_server_cert_type(HANDSHAKE_RESULT *result,
290 SSL_TEST_CTX *test_ctx)
292 return check_nid("Server certificate", test_ctx->expected_server_cert_type,
293 result->server_cert_type);
296 static int check_server_sign_hash(HANDSHAKE_RESULT *result,
297 SSL_TEST_CTX *test_ctx)
299 return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
300 result->server_sign_hash);
303 static int check_server_sign_type(HANDSHAKE_RESULT *result,
304 SSL_TEST_CTX *test_ctx)
306 return check_nid("Server signing", test_ctx->expected_server_sign_type,
307 result->server_sign_type);
310 static int check_server_ca_names(HANDSHAKE_RESULT *result,
311 SSL_TEST_CTX *test_ctx)
313 return check_ca_names("Server CA names",
314 test_ctx->expected_server_ca_names,
315 result->server_ca_names);
318 static int check_client_cert_type(HANDSHAKE_RESULT *result,
319 SSL_TEST_CTX *test_ctx)
321 return check_nid("Client certificate", test_ctx->expected_client_cert_type,
322 result->client_cert_type);
325 static int check_client_sign_hash(HANDSHAKE_RESULT *result,
326 SSL_TEST_CTX *test_ctx)
328 return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
329 result->client_sign_hash);
332 static int check_client_sign_type(HANDSHAKE_RESULT *result,
333 SSL_TEST_CTX *test_ctx)
335 return check_nid("Client signing", test_ctx->expected_client_sign_type,
336 result->client_sign_type);
339 static int check_client_ca_names(HANDSHAKE_RESULT *result,
340 SSL_TEST_CTX *test_ctx)
342 return check_ca_names("Client CA names",
343 test_ctx->expected_client_ca_names,
344 result->client_ca_names);
347 static int check_cipher(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
349 if (test_ctx->expected_cipher == NULL)
351 if (!TEST_ptr(result->cipher))
353 if (!TEST_str_eq(test_ctx->expected_cipher,
360 * This could be further simplified by constructing an expected
361 * HANDSHAKE_RESULT, and implementing comparison methods for
364 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
367 ret &= check_result(result, test_ctx);
368 ret &= check_alerts(result, test_ctx);
369 if (result->result == SSL_TEST_SUCCESS) {
370 ret &= check_protocol(result, test_ctx);
371 ret &= check_servername(result, test_ctx);
372 ret &= check_session_ticket(result, test_ctx);
373 ret &= check_compression(result, test_ctx);
374 ret &= check_session_id(result, test_ctx);
375 ret &= (result->session_ticket_do_not_call == 0);
376 #ifndef OPENSSL_NO_NEXTPROTONEG
377 ret &= check_npn(result, test_ctx);
379 ret &= check_cipher(result, test_ctx);
380 ret &= check_alpn(result, test_ctx);
381 ret &= check_session_ticket_app_data(result, test_ctx);
382 ret &= check_resumption(result, test_ctx);
383 ret &= check_tmp_key(result, test_ctx);
384 ret &= check_server_cert_type(result, test_ctx);
385 ret &= check_server_sign_hash(result, test_ctx);
386 ret &= check_server_sign_type(result, test_ctx);
387 ret &= check_server_ca_names(result, test_ctx);
388 ret &= check_client_cert_type(result, test_ctx);
389 ret &= check_client_sign_hash(result, test_ctx);
390 ret &= check_client_sign_type(result, test_ctx);
391 ret &= check_client_ca_names(result, test_ctx);
396 static int test_handshake(int idx)
399 SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
400 *resume_server_ctx = NULL, *resume_client_ctx = NULL;
401 SSL_TEST_CTX *test_ctx = NULL;
402 HANDSHAKE_RESULT *result = NULL;
403 char test_app[MAX_TESTCASE_NAME_LENGTH];
405 BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
407 test_ctx = SSL_TEST_CTX_create(conf, test_app);
408 if (!TEST_ptr(test_ctx))
411 #ifndef OPENSSL_NO_DTLS
412 if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
413 server_ctx = SSL_CTX_new_with_libctx(libctx, NULL, DTLS_server_method());
414 if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
416 if (test_ctx->extra.server.servername_callback !=
417 SSL_TEST_SERVERNAME_CB_NONE) {
418 if (!TEST_ptr(server2_ctx =
419 SSL_CTX_new_with_libctx(libctx, NULL,
420 DTLS_server_method())))
423 client_ctx = SSL_CTX_new_with_libctx(libctx, NULL, DTLS_client_method());
424 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
426 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
427 resume_server_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
428 DTLS_server_method());
429 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0)))
431 resume_client_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
432 DTLS_client_method());
433 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
435 if (!TEST_ptr(resume_server_ctx)
436 || !TEST_ptr(resume_client_ctx))
441 if (test_ctx->method == SSL_TEST_METHOD_TLS) {
442 server_ctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_server_method());
443 if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
445 /* SNI on resumption isn't supported/tested yet. */
446 if (test_ctx->extra.server.servername_callback !=
447 SSL_TEST_SERVERNAME_CB_NONE) {
448 if (!TEST_ptr(server2_ctx =
449 SSL_CTX_new_with_libctx(libctx, NULL,
450 TLS_server_method())))
452 if (!TEST_true(SSL_CTX_set_max_proto_version(server2_ctx, 0)))
455 client_ctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_client_method());
456 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
459 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
460 resume_server_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
461 TLS_server_method());
462 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0)))
464 resume_client_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
465 TLS_client_method());
466 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
468 if (!TEST_ptr(resume_server_ctx)
469 || !TEST_ptr(resume_client_ctx))
474 #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
475 if (!TEST_true(OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL)))
479 if (!TEST_ptr(server_ctx)
480 || !TEST_ptr(client_ctx)
481 || !TEST_int_gt(CONF_modules_load(conf, test_app, 0), 0))
484 if (!SSL_CTX_config(server_ctx, "server")
485 || !SSL_CTX_config(client_ctx, "client")) {
489 if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
491 if (resume_server_ctx != NULL
492 && !SSL_CTX_config(resume_server_ctx, "resume-server"))
494 if (resume_client_ctx != NULL
495 && !SSL_CTX_config(resume_client_ctx, "resume-client"))
498 result = do_handshake(server_ctx, server2_ctx, client_ctx,
499 resume_server_ctx, resume_client_ctx, test_ctx);
502 ret = check_test(result, test_ctx);
505 CONF_modules_unload(0);
506 SSL_CTX_free(server_ctx);
507 SSL_CTX_free(server2_ctx);
508 SSL_CTX_free(client_ctx);
509 SSL_CTX_free(resume_server_ctx);
510 SSL_CTX_free(resume_client_ctx);
511 SSL_TEST_CTX_free(test_ctx);
512 HANDSHAKE_RESULT_free(result);
516 OPT_TEST_DECLARE_USAGE("conf_file modulename [fips_conf_file]\n")
518 int setup_tests(void)
521 const char *modulename;
523 if (!test_skip_common_options()) {
524 TEST_error("Error parsing test options\n");
528 if (!TEST_ptr(conf = NCONF_new(NULL))
529 /* argv[1] should point to the test conf file */
530 || !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)
531 || !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests",
535 if (!TEST_ptr(modulename = test_get_argument(1)))
538 if (strcmp(modulename, "none") != 0) {
539 const char *configfile = test_get_argument(2);
541 defctxnull = OSSL_PROVIDER_load(NULL, "null");
542 libctx = OPENSSL_CTX_new();
543 if (!TEST_ptr(libctx))
546 if (configfile != NULL
547 && !TEST_true(OPENSSL_CTX_load_config(libctx, configfile)))
550 thisprov = OSSL_PROVIDER_load(libctx, modulename);
551 if (!TEST_ptr(thisprov))
555 ADD_ALL_TESTS(test_handshake, (int)num_tests);
559 void cleanup_tests(void)
562 OSSL_PROVIDER_unload(defctxnull);
563 OSSL_PROVIDER_unload(thisprov);
564 OPENSSL_CTX_free(libctx);