2 * Copyright 2016-2023 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 "helpers/handshake.h"
19 #include "helpers/ssl_test_ctx.h"
22 static CONF
*conf
= NULL
;
23 static OSSL_PROVIDER
*defctxnull
= NULL
, *thisprov
= NULL
;
24 static OSSL_LIB_CTX
*libctx
= NULL
;
26 /* Currently the section names are of the form test-<number>, e.g. test-15. */
27 #define MAX_TESTCASE_NAME_LENGTH 100
29 static const char *print_alert(int alert
)
31 return alert
? SSL_alert_desc_string_long(alert
) : "no alert";
34 static int check_result(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
36 if (!TEST_int_eq(result
->result
, test_ctx
->expected_result
)) {
37 TEST_info("ExpectedResult mismatch: expected %s, got %s.",
38 ssl_test_result_name(test_ctx
->expected_result
),
39 ssl_test_result_name(result
->result
));
45 static int check_alerts(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
47 if (!TEST_int_eq(result
->client_alert_sent
,
48 result
->client_alert_received
)) {
49 TEST_info("Client sent alert %s but server received %s.",
50 print_alert(result
->client_alert_sent
),
51 print_alert(result
->client_alert_received
));
53 * We can't bail here because the peer doesn't always get far enough
54 * to process a received alert. Specifically, in protocol version
55 * negotiation tests, we have the following scenario.
56 * Client supports TLS v1.2 only; Server supports TLS v1.1.
57 * Client proposes TLS v1.2; server responds with 1.1;
58 * Client now sends a protocol alert, using TLS v1.2 in the header.
59 * The server, however, rejects the alert because of version mismatch
60 * in the record layer; therefore, the server appears to never
66 if (!TEST_int_eq(result
->server_alert_sent
,
67 result
->server_alert_received
)) {
68 TEST_info("Server sent alert %s but client received %s.",
69 print_alert(result
->server_alert_sent
),
70 print_alert(result
->server_alert_received
));
74 /* Tolerate an alert if one wasn't explicitly specified in the test. */
75 if (test_ctx
->expected_client_alert
77 * The info callback alert value is computed as
78 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
79 * where the low byte is the alert code and the high byte is other stuff.
81 && (result
->client_alert_sent
& 0xff) != test_ctx
->expected_client_alert
) {
82 TEST_error("ClientAlert mismatch: expected %s, got %s.",
83 print_alert(test_ctx
->expected_client_alert
),
84 print_alert(result
->client_alert_sent
));
88 if (test_ctx
->expected_server_alert
89 && (result
->server_alert_sent
& 0xff) != test_ctx
->expected_server_alert
) {
90 TEST_error("ServerAlert mismatch: expected %s, got %s.",
91 print_alert(test_ctx
->expected_server_alert
),
92 print_alert(result
->server_alert_sent
));
96 if (!TEST_int_le(result
->client_num_fatal_alerts_sent
, 1))
98 if (!TEST_int_le(result
->server_num_fatal_alerts_sent
, 1))
103 static int check_protocol(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
105 if (!TEST_int_eq(result
->client_protocol
, result
->server_protocol
)) {
106 TEST_info("Client has protocol %s but server has %s.",
107 ssl_protocol_name(result
->client_protocol
),
108 ssl_protocol_name(result
->server_protocol
));
112 if (test_ctx
->expected_protocol
) {
113 if (!TEST_int_eq(result
->client_protocol
,
114 test_ctx
->expected_protocol
)) {
115 TEST_info("Protocol mismatch: expected %s, got %s.\n",
116 ssl_protocol_name(test_ctx
->expected_protocol
),
117 ssl_protocol_name(result
->client_protocol
));
124 static int check_servername(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
126 if (!TEST_int_eq(result
->servername
, test_ctx
->expected_servername
)) {
127 TEST_info("Client ServerName mismatch, expected %s, got %s.",
128 ssl_servername_name(test_ctx
->expected_servername
),
129 ssl_servername_name(result
->servername
));
135 static int check_session_ticket(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
137 if (test_ctx
->session_ticket_expected
== SSL_TEST_SESSION_TICKET_IGNORE
)
139 if (!TEST_int_eq(result
->session_ticket
,
140 test_ctx
->session_ticket_expected
)) {
141 TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.",
142 ssl_session_ticket_name(test_ctx
->session_ticket_expected
),
143 ssl_session_ticket_name(result
->session_ticket
));
149 static int check_session_id(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
151 if (test_ctx
->session_id_expected
== SSL_TEST_SESSION_ID_IGNORE
)
153 if (!TEST_int_eq(result
->session_id
, test_ctx
->session_id_expected
)) {
154 TEST_info("Client SessionIdExpected mismatch, expected %s, got %s\n.",
155 ssl_session_id_name(test_ctx
->session_id_expected
),
156 ssl_session_id_name(result
->session_id
));
162 static int check_compression(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
164 if (!TEST_int_eq(result
->compression
, test_ctx
->compression_expected
))
168 #ifndef OPENSSL_NO_NEXTPROTONEG
169 static int check_npn(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
172 if (!TEST_str_eq(result
->client_npn_negotiated
,
173 result
->server_npn_negotiated
))
175 if (!TEST_str_eq(test_ctx
->expected_npn_protocol
,
176 result
->client_npn_negotiated
))
182 static int check_alpn(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
185 if (!TEST_str_eq(result
->client_alpn_negotiated
,
186 result
->server_alpn_negotiated
))
188 if (!TEST_str_eq(test_ctx
->expected_alpn_protocol
,
189 result
->client_alpn_negotiated
))
194 static int check_session_ticket_app_data(HANDSHAKE_RESULT
*result
,
195 SSL_TEST_CTX
*test_ctx
)
197 size_t result_len
= 0;
198 size_t expected_len
= 0;
200 /* consider empty and NULL strings to be the same */
201 if (result
->result_session_ticket_app_data
!= NULL
)
202 result_len
= strlen(result
->result_session_ticket_app_data
);
203 if (test_ctx
->expected_session_ticket_app_data
!= NULL
)
204 expected_len
= strlen(test_ctx
->expected_session_ticket_app_data
);
205 if (result_len
== 0 && expected_len
== 0)
208 if (!TEST_str_eq(result
->result_session_ticket_app_data
,
209 test_ctx
->expected_session_ticket_app_data
))
215 static int check_resumption(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
217 if (!TEST_int_eq(result
->client_resumed
, result
->server_resumed
))
219 if (!TEST_int_eq(result
->client_resumed
, test_ctx
->resumption_expected
))
224 static int check_nid(const char *name
, int expected_nid
, int nid
)
226 if (expected_nid
== 0 || expected_nid
== nid
)
228 TEST_error("%s type mismatch, %s vs %s\n",
229 name
, OBJ_nid2ln(expected_nid
),
230 nid
== NID_undef
? "absent" : OBJ_nid2ln(nid
));
234 static void print_ca_names(STACK_OF(X509_NAME
) *names
)
238 if (names
== NULL
|| sk_X509_NAME_num(names
) == 0) {
239 TEST_note(" <empty>");
242 for (i
= 0; i
< sk_X509_NAME_num(names
); i
++) {
243 X509_NAME_print_ex(bio_err
, sk_X509_NAME_value(names
, i
), 4,
245 BIO_puts(bio_err
, "\n");
249 static int check_ca_names(const char *name
,
250 STACK_OF(X509_NAME
) *expected_names
,
251 STACK_OF(X509_NAME
) *names
)
255 if (expected_names
== NULL
)
257 if (names
== NULL
|| sk_X509_NAME_num(names
) == 0) {
258 if (TEST_int_eq(sk_X509_NAME_num(expected_names
), 0))
262 if (sk_X509_NAME_num(names
) != sk_X509_NAME_num(expected_names
))
264 for (i
= 0; i
< sk_X509_NAME_num(names
); i
++) {
265 if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names
, i
),
266 sk_X509_NAME_value(expected_names
, i
)),
273 TEST_info("%s: list mismatch", name
);
274 TEST_note("Expected Names:");
275 print_ca_names(expected_names
);
276 TEST_note("Received Names:");
277 print_ca_names(names
);
281 static int check_tmp_key(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
283 return check_nid("Tmp key", test_ctx
->expected_tmp_key_type
,
284 result
->tmp_key_type
);
287 static int check_server_cert_type(HANDSHAKE_RESULT
*result
,
288 SSL_TEST_CTX
*test_ctx
)
290 return check_nid("Server certificate", test_ctx
->expected_server_cert_type
,
291 result
->server_cert_type
);
294 static int check_server_sign_hash(HANDSHAKE_RESULT
*result
,
295 SSL_TEST_CTX
*test_ctx
)
297 return check_nid("Server signing hash", test_ctx
->expected_server_sign_hash
,
298 result
->server_sign_hash
);
301 static int check_server_sign_type(HANDSHAKE_RESULT
*result
,
302 SSL_TEST_CTX
*test_ctx
)
304 return check_nid("Server signing", test_ctx
->expected_server_sign_type
,
305 result
->server_sign_type
);
308 static int check_server_ca_names(HANDSHAKE_RESULT
*result
,
309 SSL_TEST_CTX
*test_ctx
)
311 return check_ca_names("Server CA names",
312 test_ctx
->expected_server_ca_names
,
313 result
->server_ca_names
);
316 static int check_client_cert_type(HANDSHAKE_RESULT
*result
,
317 SSL_TEST_CTX
*test_ctx
)
319 return check_nid("Client certificate", test_ctx
->expected_client_cert_type
,
320 result
->client_cert_type
);
323 static int check_client_sign_hash(HANDSHAKE_RESULT
*result
,
324 SSL_TEST_CTX
*test_ctx
)
326 return check_nid("Client signing hash", test_ctx
->expected_client_sign_hash
,
327 result
->client_sign_hash
);
330 static int check_client_sign_type(HANDSHAKE_RESULT
*result
,
331 SSL_TEST_CTX
*test_ctx
)
333 return check_nid("Client signing", test_ctx
->expected_client_sign_type
,
334 result
->client_sign_type
);
337 static int check_client_ca_names(HANDSHAKE_RESULT
*result
,
338 SSL_TEST_CTX
*test_ctx
)
340 return check_ca_names("Client CA names",
341 test_ctx
->expected_client_ca_names
,
342 result
->client_ca_names
);
345 static int check_cipher(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
347 if (test_ctx
->expected_cipher
== NULL
)
349 if (!TEST_ptr(result
->cipher
))
351 if (!TEST_str_eq(test_ctx
->expected_cipher
,
358 * This could be further simplified by constructing an expected
359 * HANDSHAKE_RESULT, and implementing comparison methods for
362 static int check_test(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
365 ret
&= check_result(result
, test_ctx
);
366 ret
&= check_alerts(result
, test_ctx
);
367 if (result
->result
== SSL_TEST_SUCCESS
) {
368 ret
&= check_protocol(result
, test_ctx
);
369 ret
&= check_servername(result
, test_ctx
);
370 ret
&= check_session_ticket(result
, test_ctx
);
371 ret
&= check_compression(result
, test_ctx
);
372 ret
&= check_session_id(result
, test_ctx
);
373 ret
&= (result
->session_ticket_do_not_call
== 0);
374 #ifndef OPENSSL_NO_NEXTPROTONEG
375 ret
&= check_npn(result
, test_ctx
);
377 ret
&= check_cipher(result
, test_ctx
);
378 ret
&= check_alpn(result
, test_ctx
);
379 ret
&= check_session_ticket_app_data(result
, test_ctx
);
380 ret
&= check_resumption(result
, test_ctx
);
381 ret
&= check_tmp_key(result
, test_ctx
);
382 ret
&= check_server_cert_type(result
, test_ctx
);
383 ret
&= check_server_sign_hash(result
, test_ctx
);
384 ret
&= check_server_sign_type(result
, test_ctx
);
385 ret
&= check_server_ca_names(result
, test_ctx
);
386 ret
&= check_client_cert_type(result
, test_ctx
);
387 ret
&= check_client_sign_hash(result
, test_ctx
);
388 ret
&= check_client_sign_type(result
, test_ctx
);
389 ret
&= check_client_ca_names(result
, test_ctx
);
394 static int test_handshake(int idx
)
397 SSL_CTX
*server_ctx
= NULL
, *server2_ctx
= NULL
, *client_ctx
= NULL
,
398 *resume_server_ctx
= NULL
, *resume_client_ctx
= NULL
;
399 SSL_TEST_CTX
*test_ctx
= NULL
;
400 HANDSHAKE_RESULT
*result
= NULL
;
401 char test_app
[MAX_TESTCASE_NAME_LENGTH
];
403 BIO_snprintf(test_app
, sizeof(test_app
), "test-%d", idx
);
405 test_ctx
= SSL_TEST_CTX_create(conf
, test_app
, libctx
);
406 if (!TEST_ptr(test_ctx
))
409 /* Verify that the FIPS provider supports this test */
410 if (test_ctx
->fips_version
!= NULL
411 && !fips_provider_version_match(libctx
, test_ctx
->fips_version
)) {
412 ret
= TEST_skip("FIPS provider unable to run this test");
416 #ifndef OPENSSL_NO_DTLS
417 if (test_ctx
->method
== SSL_TEST_METHOD_DTLS
) {
418 server_ctx
= SSL_CTX_new_ex(libctx
, NULL
, DTLS_server_method());
419 if (!TEST_true(SSL_CTX_set_options(server_ctx
,
420 SSL_OP_ALLOW_CLIENT_RENEGOTIATION
))
421 || !TEST_true(SSL_CTX_set_max_proto_version(server_ctx
, 0)))
423 if (test_ctx
->extra
.server
.servername_callback
!=
424 SSL_TEST_SERVERNAME_CB_NONE
) {
425 if (!TEST_ptr(server2_ctx
=
426 SSL_CTX_new_ex(libctx
, NULL
, DTLS_server_method()))
427 || !TEST_true(SSL_CTX_set_options(server2_ctx
,
428 SSL_OP_ALLOW_CLIENT_RENEGOTIATION
)))
431 client_ctx
= SSL_CTX_new_ex(libctx
, NULL
, DTLS_client_method());
432 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx
, 0)))
434 if (test_ctx
->handshake_mode
== SSL_TEST_HANDSHAKE_RESUME
) {
435 resume_server_ctx
= SSL_CTX_new_ex(libctx
, NULL
,
436 DTLS_server_method());
437 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx
, 0))
438 || !TEST_true(SSL_CTX_set_options(resume_server_ctx
,
439 SSL_OP_ALLOW_CLIENT_RENEGOTIATION
)))
441 resume_client_ctx
= SSL_CTX_new_ex(libctx
, NULL
,
442 DTLS_client_method());
443 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx
, 0)))
445 if (!TEST_ptr(resume_server_ctx
)
446 || !TEST_ptr(resume_client_ctx
))
451 if (test_ctx
->method
== SSL_TEST_METHOD_TLS
) {
452 #if !defined(OPENSSL_NO_TLS1_3) \
453 && defined(OPENSSL_NO_EC) \
454 && defined(OPENSSL_NO_DH)
455 /* Without ec or dh there are no built-in groups for TLSv1.3 */
456 int maxversion
= TLS1_2_VERSION
;
461 server_ctx
= SSL_CTX_new_ex(libctx
, NULL
, TLS_server_method());
462 if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx
, maxversion
))
463 || !TEST_true(SSL_CTX_set_options(server_ctx
,
464 SSL_OP_ALLOW_CLIENT_RENEGOTIATION
)))
466 /* SNI on resumption isn't supported/tested yet. */
467 if (test_ctx
->extra
.server
.servername_callback
!=
468 SSL_TEST_SERVERNAME_CB_NONE
) {
469 if (!TEST_ptr(server2_ctx
=
470 SSL_CTX_new_ex(libctx
, NULL
, TLS_server_method()))
471 || !TEST_true(SSL_CTX_set_options(server2_ctx
,
472 SSL_OP_ALLOW_CLIENT_RENEGOTIATION
)))
474 if (!TEST_true(SSL_CTX_set_max_proto_version(server2_ctx
,
478 client_ctx
= SSL_CTX_new_ex(libctx
, NULL
, TLS_client_method());
479 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx
, maxversion
)))
482 if (test_ctx
->handshake_mode
== SSL_TEST_HANDSHAKE_RESUME
) {
483 resume_server_ctx
= SSL_CTX_new_ex(libctx
, NULL
,
484 TLS_server_method());
485 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx
,
487 || !TEST_true(SSL_CTX_set_options(resume_server_ctx
,
488 SSL_OP_ALLOW_CLIENT_RENEGOTIATION
)))
490 resume_client_ctx
= SSL_CTX_new_ex(libctx
, NULL
,
491 TLS_client_method());
492 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx
,
495 if (!TEST_ptr(resume_server_ctx
)
496 || !TEST_ptr(resume_client_ctx
))
501 #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
502 if (!TEST_true(OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG
, NULL
)))
506 if (!TEST_ptr(server_ctx
)
507 || !TEST_ptr(client_ctx
)
508 || !TEST_int_gt(CONF_modules_load(conf
, test_app
, 0), 0))
511 if (!SSL_CTX_config(server_ctx
, "server")
512 || !SSL_CTX_config(client_ctx
, "client")) {
516 if (server2_ctx
!= NULL
&& !SSL_CTX_config(server2_ctx
, "server2"))
518 if (resume_server_ctx
!= NULL
519 && !SSL_CTX_config(resume_server_ctx
, "resume-server"))
521 if (resume_client_ctx
!= NULL
522 && !SSL_CTX_config(resume_client_ctx
, "resume-client"))
525 result
= do_handshake(server_ctx
, server2_ctx
, client_ctx
,
526 resume_server_ctx
, resume_client_ctx
, test_ctx
);
529 ret
= check_test(result
, test_ctx
);
532 CONF_modules_unload(0);
533 SSL_CTX_free(server_ctx
);
534 SSL_CTX_free(server2_ctx
);
535 SSL_CTX_free(client_ctx
);
536 SSL_CTX_free(resume_server_ctx
);
537 SSL_CTX_free(resume_client_ctx
);
538 SSL_TEST_CTX_free(test_ctx
);
539 HANDSHAKE_RESULT_free(result
);
543 #define USAGE "conf_file module_name [module_conf_file]\n"
544 OPT_TEST_DECLARE_USAGE(USAGE
)
546 int setup_tests(void)
550 if (!test_skip_common_options()) {
551 TEST_error("Error parsing test options\n");
555 if (!TEST_ptr(conf
= NCONF_new(NULL
))
556 /* argv[1] should point to the test conf file */
557 || !TEST_int_gt(NCONF_load(conf
, test_get_argument(0), NULL
), 0)
558 || !TEST_int_ne(NCONF_get_number_e(conf
, NULL
, "num_tests",
560 TEST_error("usage: ssl_test %s", USAGE
);
564 if (!test_arg_libctx(&libctx
, &defctxnull
, &thisprov
, 1, USAGE
))
567 ADD_ALL_TESTS(test_handshake
, (int)num_tests
);
571 void cleanup_tests(void)
574 OSSL_PROVIDER_unload(defctxnull
);
575 OSSL_PROVIDER_unload(thisprov
);
576 OSSL_LIB_CTX_free(libctx
);