2 * Copyright 2016-2021 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>
17 #ifndef OPENSSL_NO_QUIC
18 #include <openssl/quic.h>
21 #include "helpers/handshake.h"
22 #include "helpers/ssl_test_ctx.h"
25 static CONF *conf = NULL;
26 static OSSL_PROVIDER *defctxnull = NULL, *thisprov = NULL;
27 static OSSL_LIB_CTX *libctx = NULL;
29 /* Currently the section names are of the form test-<number>, e.g. test-15. */
30 #define MAX_TESTCASE_NAME_LENGTH 100
32 static const char *print_alert(int alert)
34 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
37 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
39 if (!TEST_int_eq(result->result, test_ctx->expected_result)) {
40 TEST_info("ExpectedResult mismatch: expected %s, got %s.",
41 ssl_test_result_name(test_ctx->expected_result),
42 ssl_test_result_name(result->result));
48 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
50 if (!TEST_int_eq(result->client_alert_sent,
51 result->client_alert_received)) {
52 TEST_info("Client sent alert %s but server received %s.",
53 print_alert(result->client_alert_sent),
54 print_alert(result->client_alert_received));
56 * We can't bail here because the peer doesn't always get far enough
57 * to process a received alert. Specifically, in protocol version
58 * negotiation tests, we have the following scenario.
59 * Client supports TLS v1.2 only; Server supports TLS v1.1.
60 * Client proposes TLS v1.2; server responds with 1.1;
61 * Client now sends a protocol alert, using TLS v1.2 in the header.
62 * The server, however, rejects the alert because of version mismatch
63 * in the record layer; therefore, the server appears to never
69 if (!TEST_int_eq(result->server_alert_sent,
70 result->server_alert_received)) {
71 TEST_info("Server sent alert %s but client received %s.",
72 print_alert(result->server_alert_sent),
73 print_alert(result->server_alert_received));
77 /* Tolerate an alert if one wasn't explicitly specified in the test. */
78 if (test_ctx->expected_client_alert
80 * The info callback alert value is computed as
81 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
82 * where the low byte is the alert code and the high byte is other stuff.
84 && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
85 TEST_error("ClientAlert mismatch: expected %s, got %s.",
86 print_alert(test_ctx->expected_client_alert),
87 print_alert(result->client_alert_sent));
91 if (test_ctx->expected_server_alert
92 && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
93 TEST_error("ServerAlert mismatch: expected %s, got %s.",
94 print_alert(test_ctx->expected_server_alert),
95 print_alert(result->server_alert_sent));
99 if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1))
101 if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1))
106 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
108 if (!TEST_int_eq(result->client_protocol, result->server_protocol)) {
109 TEST_info("Client has protocol %s but server has %s.",
110 ssl_protocol_name(result->client_protocol),
111 ssl_protocol_name(result->server_protocol));
115 if (test_ctx->expected_protocol) {
116 if (!TEST_int_eq(result->client_protocol,
117 test_ctx->expected_protocol)) {
118 TEST_info("Protocol mismatch: expected %s, got %s.\n",
119 ssl_protocol_name(test_ctx->expected_protocol),
120 ssl_protocol_name(result->client_protocol));
127 static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
129 if (!TEST_int_eq(result->servername, test_ctx->expected_servername)) {
130 TEST_info("Client ServerName mismatch, expected %s, got %s.",
131 ssl_servername_name(test_ctx->expected_servername),
132 ssl_servername_name(result->servername));
138 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
140 if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
142 if (!TEST_int_eq(result->session_ticket,
143 test_ctx->session_ticket_expected)) {
144 TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.",
145 ssl_session_ticket_name(test_ctx->session_ticket_expected),
146 ssl_session_ticket_name(result->session_ticket));
152 static int check_session_id(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
154 if (test_ctx->session_id_expected == SSL_TEST_SESSION_ID_IGNORE)
156 if (!TEST_int_eq(result->session_id, test_ctx->session_id_expected)) {
157 TEST_info("Client SessionIdExpected mismatch, expected %s, got %s\n.",
158 ssl_session_id_name(test_ctx->session_id_expected),
159 ssl_session_id_name(result->session_id));
165 static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
167 if (!TEST_int_eq(result->compression, test_ctx->compression_expected))
171 #ifndef OPENSSL_NO_NEXTPROTONEG
172 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
175 if (!TEST_str_eq(result->client_npn_negotiated,
176 result->server_npn_negotiated))
178 if (!TEST_str_eq(test_ctx->expected_npn_protocol,
179 result->client_npn_negotiated))
185 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
188 if (!TEST_str_eq(result->client_alpn_negotiated,
189 result->server_alpn_negotiated))
191 if (!TEST_str_eq(test_ctx->expected_alpn_protocol,
192 result->client_alpn_negotiated))
197 static int check_session_ticket_app_data(HANDSHAKE_RESULT *result,
198 SSL_TEST_CTX *test_ctx)
200 size_t result_len = 0;
201 size_t expected_len = 0;
203 /* consider empty and NULL strings to be the same */
204 if (result->result_session_ticket_app_data != NULL)
205 result_len = strlen(result->result_session_ticket_app_data);
206 if (test_ctx->expected_session_ticket_app_data != NULL)
207 expected_len = strlen(test_ctx->expected_session_ticket_app_data);
208 if (result_len == 0 && expected_len == 0)
211 if (!TEST_str_eq(result->result_session_ticket_app_data,
212 test_ctx->expected_session_ticket_app_data))
218 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
220 if (!TEST_int_eq(result->client_resumed, result->server_resumed))
222 if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected))
227 static int check_nid(const char *name, int expected_nid, int nid)
229 if (expected_nid == 0 || expected_nid == nid)
231 TEST_error("%s type mismatch, %s vs %s\n",
232 name, OBJ_nid2ln(expected_nid),
233 nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
237 static void print_ca_names(STACK_OF(X509_NAME) *names)
241 if (names == NULL || sk_X509_NAME_num(names) == 0) {
242 TEST_note(" <empty>");
245 for (i = 0; i < sk_X509_NAME_num(names); i++) {
246 X509_NAME_print_ex(bio_err, sk_X509_NAME_value(names, i), 4,
248 BIO_puts(bio_err, "\n");
252 static int check_ca_names(const char *name,
253 STACK_OF(X509_NAME) *expected_names,
254 STACK_OF(X509_NAME) *names)
258 if (expected_names == NULL)
260 if (names == NULL || sk_X509_NAME_num(names) == 0) {
261 if (TEST_int_eq(sk_X509_NAME_num(expected_names), 0))
265 if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
267 for (i = 0; i < sk_X509_NAME_num(names); i++) {
268 if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names, i),
269 sk_X509_NAME_value(expected_names, i)),
276 TEST_info("%s: list mismatch", name);
277 TEST_note("Expected Names:");
278 print_ca_names(expected_names);
279 TEST_note("Received Names:");
280 print_ca_names(names);
284 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
286 return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
287 result->tmp_key_type);
290 static int check_server_cert_type(HANDSHAKE_RESULT *result,
291 SSL_TEST_CTX *test_ctx)
293 return check_nid("Server certificate", test_ctx->expected_server_cert_type,
294 result->server_cert_type);
297 static int check_server_sign_hash(HANDSHAKE_RESULT *result,
298 SSL_TEST_CTX *test_ctx)
300 return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
301 result->server_sign_hash);
304 static int check_server_sign_type(HANDSHAKE_RESULT *result,
305 SSL_TEST_CTX *test_ctx)
307 return check_nid("Server signing", test_ctx->expected_server_sign_type,
308 result->server_sign_type);
311 static int check_server_ca_names(HANDSHAKE_RESULT *result,
312 SSL_TEST_CTX *test_ctx)
314 return check_ca_names("Server CA names",
315 test_ctx->expected_server_ca_names,
316 result->server_ca_names);
319 static int check_client_cert_type(HANDSHAKE_RESULT *result,
320 SSL_TEST_CTX *test_ctx)
322 return check_nid("Client certificate", test_ctx->expected_client_cert_type,
323 result->client_cert_type);
326 static int check_client_sign_hash(HANDSHAKE_RESULT *result,
327 SSL_TEST_CTX *test_ctx)
329 return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
330 result->client_sign_hash);
333 static int check_client_sign_type(HANDSHAKE_RESULT *result,
334 SSL_TEST_CTX *test_ctx)
336 return check_nid("Client signing", test_ctx->expected_client_sign_type,
337 result->client_sign_type);
340 static int check_client_ca_names(HANDSHAKE_RESULT *result,
341 SSL_TEST_CTX *test_ctx)
343 return check_ca_names("Client CA names",
344 test_ctx->expected_client_ca_names,
345 result->client_ca_names);
348 static int check_cipher(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
350 if (test_ctx->expected_cipher == NULL)
352 if (!TEST_ptr(result->cipher))
354 if (!TEST_str_eq(test_ctx->expected_cipher,
361 * This could be further simplified by constructing an expected
362 * HANDSHAKE_RESULT, and implementing comparison methods for
365 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
368 ret &= check_result(result, test_ctx);
369 ret &= check_alerts(result, test_ctx);
370 if (result->result == SSL_TEST_SUCCESS) {
371 ret &= check_protocol(result, test_ctx);
372 ret &= check_servername(result, test_ctx);
373 ret &= check_session_ticket(result, test_ctx);
374 ret &= check_compression(result, test_ctx);
375 ret &= check_session_id(result, test_ctx);
376 ret &= (result->session_ticket_do_not_call == 0);
377 #ifndef OPENSSL_NO_NEXTPROTONEG
378 ret &= check_npn(result, test_ctx);
380 ret &= check_cipher(result, test_ctx);
381 ret &= check_alpn(result, test_ctx);
382 ret &= check_session_ticket_app_data(result, test_ctx);
383 ret &= check_resumption(result, test_ctx);
384 ret &= check_tmp_key(result, test_ctx);
385 ret &= check_server_cert_type(result, test_ctx);
386 ret &= check_server_sign_hash(result, test_ctx);
387 ret &= check_server_sign_type(result, test_ctx);
388 ret &= check_server_ca_names(result, test_ctx);
389 ret &= check_client_cert_type(result, test_ctx);
390 ret &= check_client_sign_hash(result, test_ctx);
391 ret &= check_client_sign_type(result, test_ctx);
392 ret &= check_client_ca_names(result, test_ctx);
397 static int test_handshake(int idx)
400 SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
401 *resume_server_ctx = NULL, *resume_client_ctx = NULL;
402 SSL_TEST_CTX *test_ctx = NULL;
403 HANDSHAKE_RESULT *result = NULL;
404 char test_app[MAX_TESTCASE_NAME_LENGTH];
406 BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
408 test_ctx = SSL_TEST_CTX_create(conf, test_app, libctx);
409 if (!TEST_ptr(test_ctx))
412 #ifndef OPENSSL_NO_DTLS
413 if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
414 server_ctx = SSL_CTX_new_ex(libctx, NULL, DTLS_server_method());
415 if (!TEST_true(SSL_CTX_set_options(server_ctx,
416 SSL_OP_ALLOW_CLIENT_RENEGOTIATION))
417 || !TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
419 if (test_ctx->extra.server.servername_callback !=
420 SSL_TEST_SERVERNAME_CB_NONE) {
421 if (!TEST_ptr(server2_ctx =
422 SSL_CTX_new_ex(libctx, NULL, DTLS_server_method()))
423 || !TEST_true(SSL_CTX_set_options(server2_ctx,
424 SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
427 client_ctx = SSL_CTX_new_ex(libctx, NULL, DTLS_client_method());
428 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
430 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
431 resume_server_ctx = SSL_CTX_new_ex(libctx, NULL,
432 DTLS_server_method());
433 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0))
434 || !TEST_true(SSL_CTX_set_options(resume_server_ctx,
435 SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
437 resume_client_ctx = SSL_CTX_new_ex(libctx, NULL,
438 DTLS_client_method());
439 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
441 if (!TEST_ptr(resume_server_ctx)
442 || !TEST_ptr(resume_client_ctx))
447 if (test_ctx->method == SSL_TEST_METHOD_TLS) {
448 #if !defined(OPENSSL_NO_TLS1_3) \
449 && defined(OPENSSL_NO_EC) \
450 && defined(OPENSSL_NO_DH)
451 /* Without ec or dh there are no built-in groups for TLSv1.3 */
452 int maxversion = TLS1_2_VERSION;
457 server_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
458 if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, maxversion))
459 || !TEST_true(SSL_CTX_set_options(server_ctx,
460 SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
462 /* SNI on resumption isn't supported/tested yet. */
463 if (test_ctx->extra.server.servername_callback !=
464 SSL_TEST_SERVERNAME_CB_NONE) {
465 if (!TEST_ptr(server2_ctx =
466 SSL_CTX_new_ex(libctx, NULL, TLS_server_method()))
467 || !TEST_true(SSL_CTX_set_options(server2_ctx,
468 SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
470 if (!TEST_true(SSL_CTX_set_max_proto_version(server2_ctx,
474 client_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method());
475 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, maxversion)))
478 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
479 resume_server_ctx = SSL_CTX_new_ex(libctx, NULL,
480 TLS_server_method());
481 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx,
483 || !TEST_true(SSL_CTX_set_options(resume_server_ctx,
484 SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
486 resume_client_ctx = SSL_CTX_new_ex(libctx, NULL,
487 TLS_client_method());
488 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx,
491 if (!TEST_ptr(resume_server_ctx)
492 || !TEST_ptr(resume_client_ctx))
496 #ifndef OPENSSL_NO_QUIC
497 if (test_ctx->method == SSL_TEST_METHOD_QUIC) {
498 server_ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_server_method());
499 if (test_ctx->extra.server.servername_callback !=
500 SSL_TEST_SERVERNAME_CB_NONE) {
501 if (!TEST_ptr(server2_ctx =
502 SSL_CTX_new_ex(libctx, NULL,
503 OSSL_QUIC_server_method())))
506 client_ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
507 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
508 resume_server_ctx = SSL_CTX_new_ex(libctx, NULL,
509 OSSL_QUIC_server_method());
510 resume_client_ctx = SSL_CTX_new_ex(libctx, NULL,
511 OSSL_QUIC_client_method());
512 if (!TEST_ptr(resume_server_ctx)
513 || !TEST_ptr(resume_client_ctx))
519 #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
520 if (!TEST_true(OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL)))
524 if (!TEST_ptr(server_ctx)
525 || !TEST_ptr(client_ctx)
526 || !TEST_int_gt(CONF_modules_load(conf, test_app, 0), 0))
529 if (!SSL_CTX_config(server_ctx, "server")
530 || !SSL_CTX_config(client_ctx, "client")) {
534 if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
536 if (resume_server_ctx != NULL
537 && !SSL_CTX_config(resume_server_ctx, "resume-server"))
539 if (resume_client_ctx != NULL
540 && !SSL_CTX_config(resume_client_ctx, "resume-client"))
543 result = do_handshake(server_ctx, server2_ctx, client_ctx,
544 resume_server_ctx, resume_client_ctx, test_ctx);
547 ret = check_test(result, test_ctx);
550 CONF_modules_unload(0);
551 SSL_CTX_free(server_ctx);
552 SSL_CTX_free(server2_ctx);
553 SSL_CTX_free(client_ctx);
554 SSL_CTX_free(resume_server_ctx);
555 SSL_CTX_free(resume_client_ctx);
556 SSL_TEST_CTX_free(test_ctx);
557 HANDSHAKE_RESULT_free(result);
561 #define USAGE "conf_file module_name [module_conf_file]\n"
562 OPT_TEST_DECLARE_USAGE(USAGE)
564 int setup_tests(void)
568 if (!test_skip_common_options()) {
569 TEST_error("Error parsing test options\n");
573 if (!TEST_ptr(conf = NCONF_new(NULL))
574 /* argv[1] should point to the test conf file */
575 || !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)
576 || !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests",
578 TEST_error("usage: ssl_test %s", USAGE);
582 if (!test_arg_libctx(&libctx, &defctxnull, &thisprov, 1, USAGE))
585 ADD_ALL_TESTS(test_handshake, (int)num_tests);
589 void cleanup_tests(void)
592 OSSL_PROVIDER_unload(defctxnull);
593 OSSL_PROVIDER_unload(thisprov);
594 OSSL_LIB_CTX_free(libctx);