2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
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
7 * https://www.openssl.org/source/license.html
12 #include <openssl/opensslconf.h>
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/ssl.h>
16 #include <openssl/ocsp.h>
17 #include <openssl/srp.h>
18 #include <openssl/txt_db.h>
19 #include <openssl/aes.h>
21 #include "ssltestlib.h"
23 #include "testutil/output.h"
24 #include "internal/nelem.h"
25 #include "../ssl/ssl_locl.h"
27 static char *cert = NULL;
28 static char *privkey = NULL;
29 static char *srpvfile = NULL;
30 static char *tmpfilename = NULL;
32 #define LOG_BUFFER_SIZE 2048
33 static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
34 static size_t server_log_buffer_index = 0;
35 static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
36 static size_t client_log_buffer_index = 0;
37 static int error_writing_log = 0;
39 #ifndef OPENSSL_NO_OCSP
40 static const unsigned char orespder[] = "Dummy OCSP Response";
41 static int ocsp_server_called = 0;
42 static int ocsp_client_called = 0;
44 static int cdummyarg = 1;
45 static X509 *ocspcert = NULL;
48 #define NUM_EXTRA_CERTS 40
49 #define CLIENT_VERSION_LEN 2
52 * This structure is used to validate that the correct number of log messages
53 * of various types are emitted when emitting secret logs.
55 struct sslapitest_log_counts {
56 unsigned int rsa_key_exchange_count;
57 unsigned int master_secret_count;
58 unsigned int client_early_secret_count;
59 unsigned int client_handshake_secret_count;
60 unsigned int server_handshake_secret_count;
61 unsigned int client_application_secret_count;
62 unsigned int server_application_secret_count;
63 unsigned int early_exporter_secret_count;
64 unsigned int exporter_secret_count;
68 static unsigned char serverinfov1[] = {
69 0xff, 0xff, /* Dummy extension type */
70 0x00, 0x01, /* Extension length is 1 byte */
71 0xff /* Dummy extension data */
74 static unsigned char serverinfov2[] = {
76 (unsigned char)(SSL_EXT_CLIENT_HELLO & 0xff), /* Dummy context - 4 bytes */
77 0xff, 0xff, /* Dummy extension type */
78 0x00, 0x01, /* Extension length is 1 byte */
79 0xff /* Dummy extension data */
82 static void client_keylog_callback(const SSL *ssl, const char *line)
84 int line_length = strlen(line);
86 /* If the log doesn't fit, error out. */
87 if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
88 TEST_info("Client log too full");
89 error_writing_log = 1;
93 strcat(client_log_buffer, line);
94 client_log_buffer_index += line_length;
95 client_log_buffer[client_log_buffer_index++] = '\n';
98 static void server_keylog_callback(const SSL *ssl, const char *line)
100 int line_length = strlen(line);
102 /* If the log doesn't fit, error out. */
103 if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
104 TEST_info("Server log too full");
105 error_writing_log = 1;
109 strcat(server_log_buffer, line);
110 server_log_buffer_index += line_length;
111 server_log_buffer[server_log_buffer_index++] = '\n';
114 static int compare_hex_encoded_buffer(const char *hex_encoded,
122 if (!TEST_size_t_eq(raw_length * 2, hex_length))
125 for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
126 sprintf(hexed, "%02x", raw[i]);
127 if (!TEST_int_eq(hexed[0], hex_encoded[j])
128 || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
135 static int test_keylog_output(char *buffer, const SSL *ssl,
136 const SSL_SESSION *session,
137 struct sslapitest_log_counts *expected)
140 unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
141 size_t client_random_size = SSL3_RANDOM_SIZE;
142 unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
143 size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
144 unsigned int rsa_key_exchange_count = 0;
145 unsigned int master_secret_count = 0;
146 unsigned int client_early_secret_count = 0;
147 unsigned int client_handshake_secret_count = 0;
148 unsigned int server_handshake_secret_count = 0;
149 unsigned int client_application_secret_count = 0;
150 unsigned int server_application_secret_count = 0;
151 unsigned int early_exporter_secret_count = 0;
152 unsigned int exporter_secret_count = 0;
154 for (token = strtok(buffer, " \n"); token != NULL;
155 token = strtok(NULL, " \n")) {
156 if (strcmp(token, "RSA") == 0) {
158 * Premaster secret. Tokens should be: 16 ASCII bytes of
159 * hex-encoded encrypted secret, then the hex-encoded pre-master
162 if (!TEST_ptr(token = strtok(NULL, " \n")))
164 if (!TEST_size_t_eq(strlen(token), 16))
166 if (!TEST_ptr(token = strtok(NULL, " \n")))
169 * We can't sensibly check the log because the premaster secret is
170 * transient, and OpenSSL doesn't keep hold of it once the master
171 * secret is generated.
173 rsa_key_exchange_count++;
174 } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
176 * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
177 * client random, then the hex-encoded master secret.
179 client_random_size = SSL_get_client_random(ssl,
180 actual_client_random,
182 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
185 if (!TEST_ptr(token = strtok(NULL, " \n")))
187 if (!TEST_size_t_eq(strlen(token), 64))
189 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
190 actual_client_random,
191 client_random_size)))
194 if (!TEST_ptr(token = strtok(NULL, " \n")))
196 master_key_size = SSL_SESSION_get_master_key(session,
199 if (!TEST_size_t_ne(master_key_size, 0))
201 if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
205 master_secret_count++;
206 } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
207 || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
208 || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
209 || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
210 || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
211 || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
212 || strcmp(token, "EXPORTER_SECRET") == 0) {
214 * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
215 * client random, and then the hex-encoded secret. In this case,
216 * we treat all of these secrets identically and then just
217 * distinguish between them when counting what we saw.
219 if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
220 client_early_secret_count++;
221 else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
222 client_handshake_secret_count++;
223 else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
224 server_handshake_secret_count++;
225 else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
226 client_application_secret_count++;
227 else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
228 server_application_secret_count++;
229 else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
230 early_exporter_secret_count++;
231 else if (strcmp(token, "EXPORTER_SECRET") == 0)
232 exporter_secret_count++;
234 client_random_size = SSL_get_client_random(ssl,
235 actual_client_random,
237 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
240 if (!TEST_ptr(token = strtok(NULL, " \n")))
242 if (!TEST_size_t_eq(strlen(token), 64))
244 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
245 actual_client_random,
246 client_random_size)))
249 if (!TEST_ptr(token = strtok(NULL, " \n")))
253 * TODO(TLS1.3): test that application traffic secrets are what
256 TEST_info("Unexpected token %s\n", token);
261 /* Got what we expected? */
262 if (!TEST_size_t_eq(rsa_key_exchange_count,
263 expected->rsa_key_exchange_count)
264 || !TEST_size_t_eq(master_secret_count,
265 expected->master_secret_count)
266 || !TEST_size_t_eq(client_early_secret_count,
267 expected->client_early_secret_count)
268 || !TEST_size_t_eq(client_handshake_secret_count,
269 expected->client_handshake_secret_count)
270 || !TEST_size_t_eq(server_handshake_secret_count,
271 expected->server_handshake_secret_count)
272 || !TEST_size_t_eq(client_application_secret_count,
273 expected->client_application_secret_count)
274 || !TEST_size_t_eq(server_application_secret_count,
275 expected->server_application_secret_count)
276 || !TEST_size_t_eq(early_exporter_secret_count,
277 expected->early_exporter_secret_count)
278 || !TEST_size_t_eq(exporter_secret_count,
279 expected->exporter_secret_count))
284 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
285 static int test_keylog(void)
287 SSL_CTX *cctx = NULL, *sctx = NULL;
288 SSL *clientssl = NULL, *serverssl = NULL;
290 struct sslapitest_log_counts expected = {0};
292 /* Clean up logging space */
293 memset(client_log_buffer, 0, sizeof(client_log_buffer));
294 memset(server_log_buffer, 0, sizeof(server_log_buffer));
295 client_log_buffer_index = 0;
296 server_log_buffer_index = 0;
297 error_writing_log = 0;
299 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
301 TLS1_VERSION, TLS_MAX_VERSION,
302 &sctx, &cctx, cert, privkey)))
305 /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
306 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
307 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
309 /* We also want to ensure that we use RSA-based key exchange. */
310 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
313 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
314 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
316 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
317 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
318 == client_keylog_callback))
320 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
321 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
322 == server_keylog_callback))
325 /* Now do a handshake and check that the logs have been written to. */
326 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
327 &clientssl, NULL, NULL))
328 || !TEST_true(create_ssl_connection(serverssl, clientssl,
330 || !TEST_false(error_writing_log)
331 || !TEST_int_gt(client_log_buffer_index, 0)
332 || !TEST_int_gt(server_log_buffer_index, 0))
336 * Now we want to test that our output data was vaguely sensible. We
337 * do that by using strtok and confirming that we have more or less the
338 * data we expect. For both client and server, we expect to see one master
339 * secret. The client should also see a RSA key exchange.
341 expected.rsa_key_exchange_count = 1;
342 expected.master_secret_count = 1;
343 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
344 SSL_get_session(clientssl), &expected)))
347 expected.rsa_key_exchange_count = 0;
348 if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
349 SSL_get_session(serverssl), &expected)))
364 #ifndef OPENSSL_NO_TLS1_3
365 static int test_keylog_no_master_key(void)
367 SSL_CTX *cctx = NULL, *sctx = NULL;
368 SSL *clientssl = NULL, *serverssl = NULL;
369 SSL_SESSION *sess = NULL;
371 struct sslapitest_log_counts expected = {0};
372 unsigned char buf[1];
373 size_t readbytes, written;
375 /* Clean up logging space */
376 memset(client_log_buffer, 0, sizeof(client_log_buffer));
377 memset(server_log_buffer, 0, sizeof(server_log_buffer));
378 client_log_buffer_index = 0;
379 server_log_buffer_index = 0;
380 error_writing_log = 0;
382 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
383 TLS1_VERSION, TLS_MAX_VERSION,
384 &sctx, &cctx, cert, privkey))
385 || !TEST_true(SSL_CTX_set_max_early_data(sctx,
386 SSL3_RT_MAX_PLAIN_LENGTH)))
389 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
390 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
393 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
394 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
395 == client_keylog_callback))
398 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
399 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
400 == server_keylog_callback))
403 /* Now do a handshake and check that the logs have been written to. */
404 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
405 &clientssl, NULL, NULL))
406 || !TEST_true(create_ssl_connection(serverssl, clientssl,
408 || !TEST_false(error_writing_log))
412 * Now we want to test that our output data was vaguely sensible. For this
413 * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
414 * TLSv1.3, but we do expect both client and server to emit keys.
416 expected.client_handshake_secret_count = 1;
417 expected.server_handshake_secret_count = 1;
418 expected.client_application_secret_count = 1;
419 expected.server_application_secret_count = 1;
420 expected.exporter_secret_count = 1;
421 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
422 SSL_get_session(clientssl), &expected))
423 || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
424 SSL_get_session(serverssl),
428 /* Terminate old session and resume with early data. */
429 sess = SSL_get1_session(clientssl);
430 SSL_shutdown(clientssl);
431 SSL_shutdown(serverssl);
434 serverssl = clientssl = NULL;
437 memset(client_log_buffer, 0, sizeof(client_log_buffer));
438 memset(server_log_buffer, 0, sizeof(server_log_buffer));
439 client_log_buffer_index = 0;
440 server_log_buffer_index = 0;
442 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
443 &clientssl, NULL, NULL))
444 || !TEST_true(SSL_set_session(clientssl, sess))
445 /* Here writing 0 length early data is enough. */
446 || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
447 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
449 SSL_READ_EARLY_DATA_ERROR)
450 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
451 SSL_EARLY_DATA_ACCEPTED)
452 || !TEST_true(create_ssl_connection(serverssl, clientssl,
454 || !TEST_true(SSL_session_reused(clientssl)))
457 /* In addition to the previous entries, expect early secrets. */
458 expected.client_early_secret_count = 1;
459 expected.early_exporter_secret_count = 1;
460 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
461 SSL_get_session(clientssl), &expected))
462 || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
463 SSL_get_session(serverssl),
470 SSL_SESSION_free(sess);
480 #ifndef OPENSSL_NO_TLS1_2
481 static int full_client_hello_callback(SSL *s, int *al, void *arg)
484 const unsigned char *p;
486 /* We only configure two ciphers, but the SCSV is added automatically. */
488 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
490 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
493 const int expected_extensions[] = {
494 #ifndef OPENSSL_NO_EC
500 /* Make sure we can defer processing and get called back. */
502 return SSL_CLIENT_HELLO_RETRY;
504 len = SSL_client_hello_get0_ciphers(s, &p);
505 if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
507 SSL_client_hello_get0_compression_methods(s, &p), 1)
508 || !TEST_int_eq(*p, 0))
509 return SSL_CLIENT_HELLO_ERROR;
510 if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
511 return SSL_CLIENT_HELLO_ERROR;
512 if (len != OSSL_NELEM(expected_extensions) ||
513 memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
514 printf("ClientHello callback expected extensions mismatch\n");
516 return SSL_CLIENT_HELLO_ERROR;
519 return SSL_CLIENT_HELLO_SUCCESS;
522 static int test_client_hello_cb(void)
524 SSL_CTX *cctx = NULL, *sctx = NULL;
525 SSL *clientssl = NULL, *serverssl = NULL;
526 int testctr = 0, testresult = 0;
528 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
529 TLS1_VERSION, TLS_MAX_VERSION,
530 &sctx, &cctx, cert, privkey)))
532 SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
534 /* The gimpy cipher list we configure can't do TLS 1.3. */
535 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
537 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
538 "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
539 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
540 &clientssl, NULL, NULL))
541 || !TEST_false(create_ssl_connection(serverssl, clientssl,
542 SSL_ERROR_WANT_CLIENT_HELLO_CB))
544 * Passing a -1 literal is a hack since
545 * the real value was lost.
547 || !TEST_int_eq(SSL_get_error(serverssl, -1),
548 SSL_ERROR_WANT_CLIENT_HELLO_CB)
549 || !TEST_true(create_ssl_connection(serverssl, clientssl,
565 static int execute_test_large_message(const SSL_METHOD *smeth,
566 const SSL_METHOD *cmeth,
567 int min_version, int max_version,
570 SSL_CTX *cctx = NULL, *sctx = NULL;
571 SSL *clientssl = NULL, *serverssl = NULL;
575 X509 *chaincert = NULL;
578 if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
580 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
583 if (!TEST_ptr(chaincert))
586 if (!TEST_true(create_ssl_ctx_pair(smeth, cmeth, min_version, max_version,
587 &sctx, &cctx, cert, privkey)))
592 * Test that read_ahead works correctly when dealing with large
595 SSL_CTX_set_read_ahead(cctx, 1);
599 * We assume the supplied certificate is big enough so that if we add
600 * NUM_EXTRA_CERTS it will make the overall message large enough. The
601 * default buffer size is requested to be 16k, but due to the way BUF_MEM
602 * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
603 * test we need to have a message larger than that.
605 certlen = i2d_X509(chaincert, NULL);
606 OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
607 (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
608 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
609 if (!X509_up_ref(chaincert))
611 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
612 X509_free(chaincert);
617 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
619 || !TEST_true(create_ssl_connection(serverssl, clientssl,
624 * Calling SSL_clear() first is not required but this tests that SSL_clear()
625 * doesn't leak (when using enable-crypto-mdebug).
627 if (!TEST_true(SSL_clear(serverssl)))
632 X509_free(chaincert);
641 static int test_large_message_tls(void)
643 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
644 TLS1_VERSION, TLS_MAX_VERSION,
648 static int test_large_message_tls_read_ahead(void)
650 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
651 TLS1_VERSION, TLS_MAX_VERSION,
655 #ifndef OPENSSL_NO_DTLS
656 static int test_large_message_dtls(void)
659 * read_ahead is not relevant to DTLS because DTLS always acts as if
662 return execute_test_large_message(DTLS_server_method(),
663 DTLS_client_method(),
664 DTLS1_VERSION, DTLS_MAX_VERSION,
669 #ifndef OPENSSL_NO_OCSP
670 static int ocsp_server_cb(SSL *s, void *arg)
672 int *argi = (int *)arg;
673 unsigned char *copy = NULL;
674 STACK_OF(OCSP_RESPID) *ids = NULL;
675 OCSP_RESPID *id = NULL;
678 /* In this test we are expecting exactly 1 OCSP_RESPID */
679 SSL_get_tlsext_status_ids(s, &ids);
680 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
681 return SSL_TLSEXT_ERR_ALERT_FATAL;
683 id = sk_OCSP_RESPID_value(ids, 0);
684 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
685 return SSL_TLSEXT_ERR_ALERT_FATAL;
686 } else if (*argi != 1) {
687 return SSL_TLSEXT_ERR_ALERT_FATAL;
690 if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
691 return SSL_TLSEXT_ERR_ALERT_FATAL;
693 SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder));
694 ocsp_server_called = 1;
695 return SSL_TLSEXT_ERR_OK;
698 static int ocsp_client_cb(SSL *s, void *arg)
700 int *argi = (int *)arg;
701 const unsigned char *respderin;
704 if (*argi != 1 && *argi != 2)
707 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
708 if (!TEST_mem_eq(orespder, len, respderin, len))
711 ocsp_client_called = 1;
715 static int test_tlsext_status_type(void)
717 SSL_CTX *cctx = NULL, *sctx = NULL;
718 SSL *clientssl = NULL, *serverssl = NULL;
720 STACK_OF(OCSP_RESPID) *ids = NULL;
721 OCSP_RESPID *id = NULL;
724 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
725 TLS1_VERSION, TLS_MAX_VERSION,
726 &sctx, &cctx, cert, privkey))
729 if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
732 /* First just do various checks getting and setting tlsext_status_type */
734 clientssl = SSL_new(cctx);
735 if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
736 || !TEST_true(SSL_set_tlsext_status_type(clientssl,
737 TLSEXT_STATUSTYPE_ocsp))
738 || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
739 TLSEXT_STATUSTYPE_ocsp))
745 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
746 || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
749 clientssl = SSL_new(cctx);
750 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
756 * Now actually do a handshake and check OCSP information is exchanged and
757 * the callbacks get called
759 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
760 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
761 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
762 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
763 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
764 &clientssl, NULL, NULL))
765 || !TEST_true(create_ssl_connection(serverssl, clientssl,
767 || !TEST_true(ocsp_client_called)
768 || !TEST_true(ocsp_server_called))
775 /* Try again but this time force the server side callback to fail */
776 ocsp_client_called = 0;
777 ocsp_server_called = 0;
779 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
780 &clientssl, NULL, NULL))
781 /* This should fail because the callback will fail */
782 || !TEST_false(create_ssl_connection(serverssl, clientssl,
784 || !TEST_false(ocsp_client_called)
785 || !TEST_false(ocsp_server_called))
793 * This time we'll get the client to send an OCSP_RESPID that it will
796 ocsp_client_called = 0;
797 ocsp_server_called = 0;
799 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
800 &clientssl, NULL, NULL)))
804 * We'll just use any old cert for this test - it doesn't have to be an OCSP
805 * specific one. We'll use the server cert.
807 if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
808 || !TEST_ptr(id = OCSP_RESPID_new())
809 || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
810 || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio,
812 || !TEST_true(OCSP_RESPID_set_by_key(id, ocspcert))
813 || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
816 SSL_set_tlsext_status_ids(clientssl, ids);
817 /* Control has been transferred */
823 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
825 || !TEST_true(ocsp_client_called)
826 || !TEST_true(ocsp_server_called))
836 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
837 OCSP_RESPID_free(id);
846 #if !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
847 static int new_called, remove_called, get_called;
849 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
853 * sess has been up-refed for us, but we don't actually need it so free it
856 SSL_SESSION_free(sess);
860 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
865 static SSL_SESSION *get_sess_val = NULL;
867 static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
875 static int execute_test_session(int maxprot, int use_int_cache,
878 SSL_CTX *sctx = NULL, *cctx = NULL;
879 SSL *serverssl1 = NULL, *clientssl1 = NULL;
880 SSL *serverssl2 = NULL, *clientssl2 = NULL;
881 # ifndef OPENSSL_NO_TLS1_1
882 SSL *serverssl3 = NULL, *clientssl3 = NULL;
884 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
885 int testresult = 0, numnewsesstick = 1;
887 new_called = remove_called = 0;
889 /* TLSv1.3 sends 2 NewSessionTickets */
890 if (maxprot == TLS1_3_VERSION)
893 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
894 TLS1_VERSION, TLS_MAX_VERSION,
895 &sctx, &cctx, cert, privkey)))
899 * Only allow the max protocol version so we can force a connection failure
902 SSL_CTX_set_min_proto_version(cctx, maxprot);
903 SSL_CTX_set_max_proto_version(cctx, maxprot);
905 /* Set up session cache */
907 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
908 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
911 /* Also covers instance where both are set */
912 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
914 SSL_CTX_set_session_cache_mode(cctx,
915 SSL_SESS_CACHE_CLIENT
916 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
919 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
921 || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
923 || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
926 /* Should fail because it should already be in the cache */
927 if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
930 && (!TEST_int_eq(new_called, numnewsesstick)
932 || !TEST_int_eq(remove_called, 0)))
935 new_called = remove_called = 0;
936 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
937 &clientssl2, NULL, NULL))
938 || !TEST_true(SSL_set_session(clientssl2, sess1))
939 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
941 || !TEST_true(SSL_session_reused(clientssl2)))
944 if (maxprot == TLS1_3_VERSION) {
946 * In TLSv1.3 we should have created a new session even though we have
947 * resumed. Since we attempted a resume we should also have removed the
948 * old ticket from the cache so that we try to only use tickets once.
951 && (!TEST_int_eq(new_called, 1)
952 || !TEST_int_eq(remove_called, 1)))
956 * In TLSv1.2 we expect to have resumed so no sessions added or
960 && (!TEST_int_eq(new_called, 0)
961 || !TEST_int_eq(remove_called, 0)))
965 SSL_SESSION_free(sess1);
966 if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
968 shutdown_ssl_connection(serverssl2, clientssl2);
969 serverssl2 = clientssl2 = NULL;
971 new_called = remove_called = 0;
972 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
973 &clientssl2, NULL, NULL))
974 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
978 if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
982 && (!TEST_int_eq(new_called, numnewsesstick)
983 || !TEST_int_eq(remove_called, 0)))
986 new_called = remove_called = 0;
988 * This should clear sess2 from the cache because it is a "bad" session.
989 * See SSL_set_session() documentation.
991 if (!TEST_true(SSL_set_session(clientssl2, sess1)))
994 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
996 if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
1000 /* Should succeeded because it should not already be in the cache */
1001 if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
1002 || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
1006 new_called = remove_called = 0;
1007 /* This shouldn't be in the cache so should fail */
1008 if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
1012 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
1015 # if !defined(OPENSSL_NO_TLS1_1)
1016 new_called = remove_called = 0;
1017 /* Force a connection failure */
1018 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
1019 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
1020 &clientssl3, NULL, NULL))
1021 || !TEST_true(SSL_set_session(clientssl3, sess1))
1022 /* This should fail because of the mismatched protocol versions */
1023 || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
1027 /* We should have automatically removed the session from the cache */
1029 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
1032 /* Should succeed because it should not already be in the cache */
1033 if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
1037 /* Now do some tests for server side caching */
1038 if (use_ext_cache) {
1039 SSL_CTX_sess_set_new_cb(cctx, NULL);
1040 SSL_CTX_sess_set_remove_cb(cctx, NULL);
1041 SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
1042 SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
1043 SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
1044 get_sess_val = NULL;
1047 SSL_CTX_set_session_cache_mode(cctx, 0);
1048 /* Internal caching is the default on the server side */
1050 SSL_CTX_set_session_cache_mode(sctx,
1051 SSL_SESS_CACHE_SERVER
1052 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
1054 SSL_free(serverssl1);
1055 SSL_free(clientssl1);
1056 serverssl1 = clientssl1 = NULL;
1057 SSL_free(serverssl2);
1058 SSL_free(clientssl2);
1059 serverssl2 = clientssl2 = NULL;
1060 SSL_SESSION_free(sess1);
1062 SSL_SESSION_free(sess2);
1065 SSL_CTX_set_max_proto_version(sctx, maxprot);
1066 if (maxprot == TLS1_2_VERSION)
1067 SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
1068 new_called = remove_called = get_called = 0;
1069 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
1071 || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
1073 || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
1074 || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
1077 if (use_int_cache) {
1078 if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
1080 * In TLSv1.3 it should not have been added to the internal cache,
1081 * except in the case where we also have an external cache (in that
1082 * case it gets added to the cache in order to generate remove
1083 * events after timeout).
1085 if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
1088 /* Should fail because it should already be in the cache */
1089 if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
1094 if (use_ext_cache) {
1095 SSL_SESSION *tmp = sess2;
1097 if (!TEST_int_eq(new_called, numnewsesstick)
1098 || !TEST_int_eq(remove_called, 0)
1099 || !TEST_int_eq(get_called, 0))
1102 * Delete the session from the internal cache to force a lookup from
1103 * the external cache. We take a copy first because
1104 * SSL_CTX_remove_session() also marks the session as non-resumable.
1106 if (use_int_cache && maxprot != TLS1_3_VERSION) {
1107 if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
1108 || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
1110 SSL_SESSION_free(sess2);
1115 new_called = remove_called = get_called = 0;
1116 get_sess_val = sess2;
1117 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
1118 &clientssl2, NULL, NULL))
1119 || !TEST_true(SSL_set_session(clientssl2, sess1))
1120 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
1122 || !TEST_true(SSL_session_reused(clientssl2)))
1125 if (use_ext_cache) {
1126 if (!TEST_int_eq(remove_called, 0))
1129 if (maxprot == TLS1_3_VERSION) {
1130 if (!TEST_int_eq(new_called, 1)
1131 || !TEST_int_eq(get_called, 0))
1134 if (!TEST_int_eq(new_called, 0)
1135 || !TEST_int_eq(get_called, 1))
1143 SSL_free(serverssl1);
1144 SSL_free(clientssl1);
1145 SSL_free(serverssl2);
1146 SSL_free(clientssl2);
1147 # ifndef OPENSSL_NO_TLS1_1
1148 SSL_free(serverssl3);
1149 SSL_free(clientssl3);
1151 SSL_SESSION_free(sess1);
1152 SSL_SESSION_free(sess2);
1158 #endif /* !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
1160 static int test_session_with_only_int_cache(void)
1162 #ifndef OPENSSL_NO_TLS1_3
1163 if (!execute_test_session(TLS1_3_VERSION, 1, 0))
1167 #ifndef OPENSSL_NO_TLS1_2
1168 return execute_test_session(TLS1_2_VERSION, 1, 0);
1174 static int test_session_with_only_ext_cache(void)
1176 #ifndef OPENSSL_NO_TLS1_3
1177 if (!execute_test_session(TLS1_3_VERSION, 0, 1))
1181 #ifndef OPENSSL_NO_TLS1_2
1182 return execute_test_session(TLS1_2_VERSION, 0, 1);
1188 static int test_session_with_both_cache(void)
1190 #ifndef OPENSSL_NO_TLS1_3
1191 if (!execute_test_session(TLS1_3_VERSION, 1, 1))
1195 #ifndef OPENSSL_NO_TLS1_2
1196 return execute_test_session(TLS1_2_VERSION, 1, 1);
1202 #ifndef OPENSSL_NO_TLS1_3
1203 static SSL_SESSION *sesscache[6];
1204 static int do_cache;
1206 static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
1209 sesscache[new_called] = sess;
1211 /* We don't need the reference to the session, so free it */
1212 SSL_SESSION_free(sess);
1219 static int post_handshake_verify(SSL *sssl, SSL *cssl)
1221 SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
1222 if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
1225 /* Start handshake on the server and client */
1226 if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
1227 || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
1228 || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
1229 || !TEST_true(create_ssl_connection(sssl, cssl,
1236 static int test_tickets(int idx)
1238 SSL_CTX *sctx = NULL, *cctx = NULL;
1239 SSL *serverssl = NULL, *clientssl = NULL;
1240 int testresult = 0, i;
1243 /* idx is the test number, but also the number of tickets we want */
1248 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
1249 TLS1_VERSION, TLS_MAX_VERSION, &sctx,
1250 &cctx, cert, privkey))
1251 || !TEST_true(SSL_CTX_set_num_tickets(sctx, idx)))
1254 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
1255 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
1256 SSL_CTX_sess_set_new_cb(cctx, new_cachesession_cb);
1258 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1259 &clientssl, NULL, NULL)))
1262 SSL_force_post_handshake_auth(clientssl);
1264 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1266 /* Check we got the number of tickets we were expecting */
1267 || !TEST_int_eq(idx, new_called))
1270 /* After a post-handshake authentication we should get new tickets issued */
1271 if (!post_handshake_verify(serverssl, clientssl)
1272 || !TEST_int_eq(idx * 2, new_called))
1275 SSL_shutdown(clientssl);
1276 SSL_shutdown(serverssl);
1277 SSL_free(serverssl);
1278 SSL_free(clientssl);
1279 serverssl = clientssl = NULL;
1281 /* Stop caching sessions - just count them */
1284 /* Test that we can resume with all the tickets we got given */
1285 for (i = 0; i < idx * 2; i++) {
1287 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1288 &clientssl, NULL, NULL))
1289 || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
1292 SSL_force_post_handshake_auth(clientssl);
1294 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1296 || !TEST_true(SSL_session_reused(clientssl))
1297 /* Following a resumption we only get 1 ticket */
1298 || !TEST_int_eq(new_called, 1))
1302 /* After a post-handshake authentication we should get 1 new ticket */
1303 if (!post_handshake_verify(serverssl, clientssl)
1304 || !TEST_int_eq(new_called, 1))
1307 SSL_shutdown(clientssl);
1308 SSL_shutdown(serverssl);
1309 SSL_free(serverssl);
1310 SSL_free(clientssl);
1311 serverssl = clientssl = NULL;
1312 SSL_SESSION_free(sesscache[i]);
1313 sesscache[i] = NULL;
1319 SSL_free(serverssl);
1320 SSL_free(clientssl);
1321 for (j = 0; j < OSSL_NELEM(sesscache); j++) {
1322 SSL_SESSION_free(sesscache[j]);
1323 sesscache[j] = NULL;
1335 #define USE_DEFAULT 3
1337 #define CONNTYPE_CONNECTION_SUCCESS 0
1338 #define CONNTYPE_CONNECTION_FAIL 1
1339 #define CONNTYPE_NO_CONNECTION 2
1341 #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
1342 #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2)
1343 #if !defined(OPENSSL_NO_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
1344 # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2)
1346 # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0
1349 #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
1350 + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
1351 + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
1353 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
1370 * Tests calls to SSL_set_bio() under various conditions.
1372 * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
1373 * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
1374 * then do more tests where we create a successful connection first using our
1375 * standard connection setup functions, and then call SSL_set_bio() with
1376 * various combinations of valid BIOs or NULL. We then repeat these tests
1377 * following a failed connection. In this last case we are looking to check that
1378 * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
1380 static int test_ssl_set_bio(int idx)
1382 SSL_CTX *sctx = NULL, *cctx = NULL;
1385 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
1386 SSL *serverssl = NULL, *clientssl = NULL;
1387 int initrbio, initwbio, newrbio, newwbio, conntype;
1390 if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
1398 conntype = CONNTYPE_NO_CONNECTION;
1400 idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
1401 initrbio = initwbio = USE_DEFAULT;
1409 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
1410 TLS1_VERSION, TLS_MAX_VERSION,
1411 &sctx, &cctx, cert, privkey)))
1414 if (conntype == CONNTYPE_CONNECTION_FAIL) {
1416 * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
1417 * because we reduced the number of tests in the definition of
1418 * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
1419 * mismatched protocol versions we will force a connection failure.
1421 SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
1422 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
1425 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1429 if (initrbio == USE_BIO_1
1430 || initwbio == USE_BIO_1
1431 || newrbio == USE_BIO_1
1432 || newwbio == USE_BIO_1) {
1433 if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
1437 if (initrbio == USE_BIO_2
1438 || initwbio == USE_BIO_2
1439 || newrbio == USE_BIO_2
1440 || newwbio == USE_BIO_2) {
1441 if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
1445 if (initrbio != USE_DEFAULT) {
1446 setupbio(&irbio, bio1, bio2, initrbio);
1447 setupbio(&iwbio, bio1, bio2, initwbio);
1448 SSL_set_bio(clientssl, irbio, iwbio);
1451 * We want to maintain our own refs to these BIO, so do an up ref for
1452 * each BIO that will have ownership transferred in the SSL_set_bio()
1457 if (iwbio != NULL && iwbio != irbio)
1461 if (conntype != CONNTYPE_NO_CONNECTION
1462 && !TEST_true(create_ssl_connection(serverssl, clientssl,
1464 == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
1467 setupbio(&nrbio, bio1, bio2, newrbio);
1468 setupbio(&nwbio, bio1, bio2, newwbio);
1471 * We will (maybe) transfer ownership again so do more up refs.
1472 * SSL_set_bio() has some really complicated ownership rules where BIOs have
1477 && (nwbio != iwbio || nrbio != nwbio))
1481 && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
1484 SSL_set_bio(clientssl, nrbio, nwbio);
1493 * This test is checking that the ref counting for SSL_set_bio is correct.
1494 * If we get here and we did too many frees then we will fail in the above
1495 * functions. If we haven't done enough then this will only be detected in
1496 * a crypto-mdebug build
1498 SSL_free(serverssl);
1499 SSL_free(clientssl);
1505 typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
1507 static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
1509 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
1514 if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))
1515 || !TEST_ptr(ssl = SSL_new(ctx))
1516 || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
1517 || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
1520 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
1523 * If anything goes wrong here then we could leak memory, so this will
1524 * be caught in a crypto-mdebug build
1526 BIO_push(sslbio, membio1);
1528 /* Verify changing the rbio/wbio directly does not cause leaks */
1529 if (change_bio != NO_BIO_CHANGE) {
1530 if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem())))
1532 if (change_bio == CHANGE_RBIO)
1533 SSL_set0_rbio(ssl, membio2);
1535 SSL_set0_wbio(ssl, membio2);
1554 static int test_ssl_bio_pop_next_bio(void)
1556 return execute_test_ssl_bio(0, NO_BIO_CHANGE);
1559 static int test_ssl_bio_pop_ssl_bio(void)
1561 return execute_test_ssl_bio(1, NO_BIO_CHANGE);
1564 static int test_ssl_bio_change_rbio(void)
1566 return execute_test_ssl_bio(0, CHANGE_RBIO);
1569 static int test_ssl_bio_change_wbio(void)
1571 return execute_test_ssl_bio(0, CHANGE_WBIO);
1574 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
1576 /* The list of sig algs */
1578 /* The length of the list */
1580 /* A sigalgs list in string format */
1581 const char *liststr;
1582 /* Whether setting the list should succeed */
1584 /* Whether creating a connection with the list should succeed */
1588 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
1589 # ifndef OPENSSL_NO_EC
1590 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
1591 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
1593 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
1594 static const int invalidlist2[] = {NID_sha256, NID_undef};
1595 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
1596 static const int invalidlist4[] = {NID_sha256};
1597 static const sigalgs_list testsigalgs[] = {
1598 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
1599 # ifndef OPENSSL_NO_EC
1600 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
1601 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
1603 {NULL, 0, "RSA+SHA256", 1, 1},
1604 # ifndef OPENSSL_NO_EC
1605 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
1606 {NULL, 0, "ECDSA+SHA512", 1, 0},
1608 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
1609 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
1610 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
1611 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
1612 {NULL, 0, "RSA", 0, 0},
1613 {NULL, 0, "SHA256", 0, 0},
1614 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
1615 {NULL, 0, "Invalid", 0, 0}
1618 static int test_set_sigalgs(int idx)
1620 SSL_CTX *cctx = NULL, *sctx = NULL;
1621 SSL *clientssl = NULL, *serverssl = NULL;
1623 const sigalgs_list *curr;
1626 /* Should never happen */
1627 if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
1630 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
1631 curr = testctx ? &testsigalgs[idx]
1632 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
1634 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
1635 TLS1_VERSION, TLS_MAX_VERSION,
1636 &sctx, &cctx, cert, privkey)))
1640 * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
1641 * for TLSv1.2 for now until we add a new API.
1643 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
1648 if (curr->list != NULL)
1649 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
1651 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
1655 TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
1661 TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
1666 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1667 &clientssl, NULL, NULL)))
1673 if (curr->list != NULL)
1674 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
1676 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
1679 TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
1688 if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
1696 SSL_free(serverssl);
1697 SSL_free(clientssl);
1705 #ifndef OPENSSL_NO_TLS1_3
1707 static SSL_SESSION *clientpsk = NULL;
1708 static SSL_SESSION *serverpsk = NULL;
1709 static const char *pskid = "Identity";
1710 static const char *srvid;
1712 static int use_session_cb_cnt = 0;
1713 static int find_session_cb_cnt = 0;
1714 static int psk_client_cb_cnt = 0;
1715 static int psk_server_cb_cnt = 0;
1717 static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
1718 size_t *idlen, SSL_SESSION **sess)
1720 switch (++use_session_cb_cnt) {
1722 /* The first call should always have a NULL md */
1728 /* The second call should always have an md */
1734 /* We should only be called a maximum of twice */
1738 if (clientpsk != NULL)
1739 SSL_SESSION_up_ref(clientpsk);
1742 *id = (const unsigned char *)pskid;
1743 *idlen = strlen(pskid);
1748 #ifndef OPENSSL_NO_PSK
1749 static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
1750 unsigned int max_id_len,
1752 unsigned int max_psk_len)
1754 unsigned int psklen = 0;
1756 psk_client_cb_cnt++;
1758 if (strlen(pskid) + 1 > max_id_len)
1761 /* We should only ever be called a maximum of twice per connection */
1762 if (psk_client_cb_cnt > 2)
1765 if (clientpsk == NULL)
1768 /* We'll reuse the PSK we set up for TLSv1.3 */
1769 if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
1771 psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
1772 strncpy(id, pskid, max_id_len);
1776 #endif /* OPENSSL_NO_PSK */
1778 static int find_session_cb(SSL *ssl, const unsigned char *identity,
1779 size_t identity_len, SSL_SESSION **sess)
1781 find_session_cb_cnt++;
1783 /* We should only ever be called a maximum of twice per connection */
1784 if (find_session_cb_cnt > 2)
1787 if (serverpsk == NULL)
1790 /* Identity should match that set by the client */
1791 if (strlen(srvid) != identity_len
1792 || strncmp(srvid, (const char *)identity, identity_len) != 0) {
1793 /* No PSK found, continue but without a PSK */
1798 SSL_SESSION_up_ref(serverpsk);
1804 #ifndef OPENSSL_NO_PSK
1805 static unsigned int psk_server_cb(SSL *ssl, const char *identity,
1806 unsigned char *psk, unsigned int max_psk_len)
1808 unsigned int psklen = 0;
1810 psk_server_cb_cnt++;
1812 /* We should only ever be called a maximum of twice per connection */
1813 if (find_session_cb_cnt > 2)
1816 if (serverpsk == NULL)
1819 /* Identity should match that set by the client */
1820 if (strcmp(srvid, identity) != 0) {
1824 /* We'll reuse the PSK we set up for TLSv1.3 */
1825 if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
1827 psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
1831 #endif /* OPENSSL_NO_PSK */
1833 #define MSG1 "Hello"
1834 #define MSG2 "World."
1839 #define MSG7 "message."
1841 #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02")
1842 #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
1845 * Helper method to setup objects for early data test. Caller frees objects on
1848 static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
1849 SSL **serverssl, SSL_SESSION **sess, int idx)
1852 && !TEST_true(create_ssl_ctx_pair(TLS_server_method(),
1853 TLS_client_method(),
1854 TLS1_VERSION, TLS_MAX_VERSION,
1855 sctx, cctx, cert, privkey)))
1858 if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
1862 /* When idx == 1 we repeat the tests with read_ahead set */
1863 SSL_CTX_set_read_ahead(*cctx, 1);
1864 SSL_CTX_set_read_ahead(*sctx, 1);
1865 } else if (idx == 2) {
1866 /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
1867 SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
1868 SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
1869 use_session_cb_cnt = 0;
1870 find_session_cb_cnt = 0;
1874 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
1879 * For one of the run throughs (doesn't matter which one), we'll try sending
1880 * some SNI data in the initial ClientHello. This will be ignored (because
1881 * there is no SNI cb set up by the server), so it should not impact
1885 && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
1889 /* Create the PSK */
1890 const SSL_CIPHER *cipher = NULL;
1891 const unsigned char key[] = {
1892 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
1893 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
1894 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
1895 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
1896 0x2c, 0x2d, 0x2e, 0x2f
1899 cipher = SSL_CIPHER_find(*clientssl, TLS13_AES_256_GCM_SHA384_BYTES);
1900 clientpsk = SSL_SESSION_new();
1901 if (!TEST_ptr(clientpsk)
1902 || !TEST_ptr(cipher)
1903 || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
1905 || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
1907 SSL_SESSION_set_protocol_version(clientpsk,
1910 * We just choose an arbitrary value for max_early_data which
1911 * should be big enough for testing purposes.
1913 || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
1915 || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
1916 SSL_SESSION_free(clientpsk);
1920 serverpsk = clientpsk;
1923 if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
1924 SSL_SESSION_free(clientpsk);
1925 SSL_SESSION_free(serverpsk);
1926 clientpsk = serverpsk = NULL;
1937 if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
1941 *sess = SSL_get1_session(*clientssl);
1942 SSL_shutdown(*clientssl);
1943 SSL_shutdown(*serverssl);
1944 SSL_free(*serverssl);
1945 SSL_free(*clientssl);
1946 *serverssl = *clientssl = NULL;
1948 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
1949 clientssl, NULL, NULL))
1950 || !TEST_true(SSL_set_session(*clientssl, *sess)))
1956 static int test_early_data_read_write(int idx)
1958 SSL_CTX *cctx = NULL, *sctx = NULL;
1959 SSL *clientssl = NULL, *serverssl = NULL;
1961 SSL_SESSION *sess = NULL;
1962 unsigned char buf[20], data[1024];
1963 size_t readbytes, written, eoedlen, rawread, rawwritten;
1966 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
1967 &serverssl, &sess, idx)))
1970 /* Write and read some early data */
1971 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
1973 || !TEST_size_t_eq(written, strlen(MSG1))
1974 || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
1975 sizeof(buf), &readbytes),
1976 SSL_READ_EARLY_DATA_SUCCESS)
1977 || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
1978 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
1979 SSL_EARLY_DATA_ACCEPTED))
1983 * Server should be able to write data, and client should be able to
1986 if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
1988 || !TEST_size_t_eq(written, strlen(MSG2))
1989 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1990 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
1993 /* Even after reading normal data, client should be able write early data */
1994 if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
1996 || !TEST_size_t_eq(written, strlen(MSG3)))
1999 /* Server should still be able read early data after writing data */
2000 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2002 SSL_READ_EARLY_DATA_SUCCESS)
2003 || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
2006 /* Write more data from server and read it from client */
2007 if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
2009 || !TEST_size_t_eq(written, strlen(MSG4))
2010 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2011 || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
2015 * If client writes normal data it should mean writing early data is no
2018 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
2019 || !TEST_size_t_eq(written, strlen(MSG5))
2020 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
2021 SSL_EARLY_DATA_ACCEPTED))
2025 * At this point the client has written EndOfEarlyData, ClientFinished and
2026 * normal (fully protected) data. We are going to cause a delay between the
2027 * arrival of EndOfEarlyData and ClientFinished. We read out all the data
2028 * in the read BIO, and then just put back the EndOfEarlyData message.
2030 rbio = SSL_get_rbio(serverssl);
2031 if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
2032 || !TEST_size_t_lt(rawread, sizeof(data))
2033 || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
2036 /* Record length is in the 4th and 5th bytes of the record header */
2037 eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
2038 if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
2039 || !TEST_size_t_eq(rawwritten, eoedlen))
2042 /* Server should be told that there is no more early data */
2043 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2045 SSL_READ_EARLY_DATA_FINISH)
2046 || !TEST_size_t_eq(readbytes, 0))
2050 * Server has not finished init yet, so should still be able to write early
2053 if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
2055 || !TEST_size_t_eq(written, strlen(MSG6)))
2058 /* Push the ClientFinished and the normal data back into the server rbio */
2059 if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
2061 || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
2064 /* Server should be able to read normal data */
2065 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2066 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
2069 /* Client and server should not be able to write/read early data now */
2070 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
2074 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2076 SSL_READ_EARLY_DATA_ERROR))
2080 /* Client should be able to read the data sent by the server */
2081 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2082 || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
2086 * Make sure we process the two NewSessionTickets. These arrive
2087 * post-handshake. We attempt reads which we do not expect to return any
2090 if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2091 || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
2095 /* Server should be able to write normal data */
2096 if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
2097 || !TEST_size_t_eq(written, strlen(MSG7))
2098 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2099 || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
2102 SSL_SESSION_free(sess);
2103 sess = SSL_get1_session(clientssl);
2104 use_session_cb_cnt = 0;
2105 find_session_cb_cnt = 0;
2107 SSL_shutdown(clientssl);
2108 SSL_shutdown(serverssl);
2109 SSL_free(serverssl);
2110 SSL_free(clientssl);
2111 serverssl = clientssl = NULL;
2112 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2113 &clientssl, NULL, NULL))
2114 || !TEST_true(SSL_set_session(clientssl, sess)))
2117 /* Write and read some early data */
2118 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2120 || !TEST_size_t_eq(written, strlen(MSG1))
2121 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2123 SSL_READ_EARLY_DATA_SUCCESS)
2124 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
2127 if (!TEST_int_gt(SSL_connect(clientssl), 0)
2128 || !TEST_int_gt(SSL_accept(serverssl), 0))
2131 /* Client and server should not be able to write/read early data now */
2132 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
2136 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2138 SSL_READ_EARLY_DATA_ERROR))
2142 /* Client and server should be able to write/read normal data */
2143 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
2144 || !TEST_size_t_eq(written, strlen(MSG5))
2145 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2146 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
2152 SSL_SESSION_free(sess);
2153 SSL_SESSION_free(clientpsk);
2154 SSL_SESSION_free(serverpsk);
2155 clientpsk = serverpsk = NULL;
2156 SSL_free(serverssl);
2157 SSL_free(clientssl);
2163 static int allow_ed_cb_called = 0;
2165 static int allow_early_data_cb(SSL *s, void *arg)
2167 int *usecb = (int *)arg;
2169 allow_ed_cb_called++;
2178 * idx == 0: Standard early_data setup
2179 * idx == 1: early_data setup using read_ahead
2180 * usecb == 0: Don't use a custom early data callback
2181 * usecb == 1: Use a custom early data callback and reject the early data
2182 * usecb == 2: Use a custom early data callback and accept the early data
2183 * confopt == 0: Configure anti-replay directly
2184 * confopt == 1: Configure anti-replay using SSL_CONF
2186 static int test_early_data_replay_int(int idx, int usecb, int confopt)
2188 SSL_CTX *cctx = NULL, *sctx = NULL;
2189 SSL *clientssl = NULL, *serverssl = NULL;
2191 SSL_SESSION *sess = NULL;
2192 size_t readbytes, written;
2193 unsigned char buf[20];
2195 allow_ed_cb_called = 0;
2197 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
2198 TLS1_VERSION, TLS_MAX_VERSION, &sctx,
2199 &cctx, cert, privkey)))
2204 SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
2206 SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
2208 if (!TEST_ptr(confctx))
2210 SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
2211 | SSL_CONF_FLAG_SERVER);
2212 SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
2213 if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
2215 SSL_CONF_CTX_free(confctx);
2218 SSL_CONF_CTX_free(confctx);
2220 SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
2223 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2224 &serverssl, &sess, idx)))
2228 * The server is configured to accept early data. Create a connection to
2229 * "use up" the ticket
2231 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
2232 || !TEST_true(SSL_session_reused(clientssl)))
2235 SSL_shutdown(clientssl);
2236 SSL_shutdown(serverssl);
2237 SSL_free(serverssl);
2238 SSL_free(clientssl);
2239 serverssl = clientssl = NULL;
2241 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2242 &clientssl, NULL, NULL))
2243 || !TEST_true(SSL_set_session(clientssl, sess)))
2246 /* Write and read some early data */
2247 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2249 || !TEST_size_t_eq(written, strlen(MSG1)))
2253 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2255 SSL_READ_EARLY_DATA_FINISH)
2257 * The ticket was reused, so the we should have rejected the
2260 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2261 SSL_EARLY_DATA_REJECTED))
2264 /* In this case the callback decides to accept the early data */
2265 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2267 SSL_READ_EARLY_DATA_SUCCESS)
2268 || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
2270 * Server will have sent its flight so client can now send
2271 * end of early data and complete its half of the handshake
2273 || !TEST_int_gt(SSL_connect(clientssl), 0)
2274 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2276 SSL_READ_EARLY_DATA_FINISH)
2277 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2278 SSL_EARLY_DATA_ACCEPTED))
2282 /* Complete the connection */
2283 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
2284 || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
2285 || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
2291 SSL_SESSION_free(sess);
2292 SSL_SESSION_free(clientpsk);
2293 SSL_SESSION_free(serverpsk);
2294 clientpsk = serverpsk = NULL;
2295 SSL_free(serverssl);
2296 SSL_free(clientssl);
2302 static int test_early_data_replay(int idx)
2304 int ret = 1, usecb, confopt;
2306 for (usecb = 0; usecb < 3; usecb++) {
2307 for (confopt = 0; confopt < 2; confopt++)
2308 ret &= test_early_data_replay_int(idx, usecb, confopt);
2315 * Helper function to test that a server attempting to read early data can
2316 * handle a connection from a client where the early data should be skipped.
2318 static int early_data_skip_helper(int hrr, int idx)
2320 SSL_CTX *cctx = NULL, *sctx = NULL;
2321 SSL *clientssl = NULL, *serverssl = NULL;
2323 SSL_SESSION *sess = NULL;
2324 unsigned char buf[20];
2325 size_t readbytes, written;
2327 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2328 &serverssl, &sess, idx)))
2332 /* Force an HRR to occur */
2333 if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
2335 } else if (idx == 2) {
2337 * We force early_data rejection by ensuring the PSK identity is
2340 srvid = "Dummy Identity";
2343 * Deliberately corrupt the creation time. We take 20 seconds off the
2344 * time. It could be any value as long as it is not within tolerance.
2345 * This should mean the ticket is rejected.
2347 if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
2351 /* Write some early data */
2352 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2354 || !TEST_size_t_eq(written, strlen(MSG1)))
2357 /* Server should reject the early data and skip over it */
2358 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2360 SSL_READ_EARLY_DATA_FINISH)
2361 || !TEST_size_t_eq(readbytes, 0)
2362 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2363 SSL_EARLY_DATA_REJECTED))
2368 * Finish off the handshake. We perform the same writes and reads as
2369 * further down but we expect them to fail due to the incomplete
2372 if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
2373 || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
2378 /* Should be able to send normal data despite rejection of early data */
2379 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
2380 || !TEST_size_t_eq(written, strlen(MSG2))
2381 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
2382 SSL_EARLY_DATA_REJECTED)
2383 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2384 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
2390 SSL_SESSION_free(clientpsk);
2391 SSL_SESSION_free(serverpsk);
2392 clientpsk = serverpsk = NULL;
2393 SSL_SESSION_free(sess);
2394 SSL_free(serverssl);
2395 SSL_free(clientssl);
2402 * Test that a server attempting to read early data can handle a connection
2403 * from a client where the early data is not acceptable.
2405 static int test_early_data_skip(int idx)
2407 return early_data_skip_helper(0, idx);
2411 * Test that a server attempting to read early data can handle a connection
2412 * from a client where an HRR occurs.
2414 static int test_early_data_skip_hrr(int idx)
2416 return early_data_skip_helper(1, idx);
2420 * Test that a server attempting to read early data can handle a connection
2421 * from a client that doesn't send any.
2423 static int test_early_data_not_sent(int idx)
2425 SSL_CTX *cctx = NULL, *sctx = NULL;
2426 SSL *clientssl = NULL, *serverssl = NULL;
2428 SSL_SESSION *sess = NULL;
2429 unsigned char buf[20];
2430 size_t readbytes, written;
2432 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2433 &serverssl, &sess, idx)))
2436 /* Write some data - should block due to handshake with server */
2437 SSL_set_connect_state(clientssl);
2438 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
2441 /* Server should detect that early data has not been sent */
2442 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2444 SSL_READ_EARLY_DATA_FINISH)
2445 || !TEST_size_t_eq(readbytes, 0)
2446 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2447 SSL_EARLY_DATA_NOT_SENT)
2448 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
2449 SSL_EARLY_DATA_NOT_SENT))
2452 /* Continue writing the message we started earlier */
2453 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
2454 || !TEST_size_t_eq(written, strlen(MSG1))
2455 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2456 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
2457 || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
2458 || !TEST_size_t_eq(written, strlen(MSG2)))
2461 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2462 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
2468 SSL_SESSION_free(sess);
2469 SSL_SESSION_free(clientpsk);
2470 SSL_SESSION_free(serverpsk);
2471 clientpsk = serverpsk = NULL;
2472 SSL_free(serverssl);
2473 SSL_free(clientssl);
2479 static int hostname_cb(SSL *s, int *al, void *arg)
2481 const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
2483 if (hostname != NULL && strcmp(hostname, "goodhost") == 0)
2484 return SSL_TLSEXT_ERR_OK;
2486 return SSL_TLSEXT_ERR_NOACK;
2489 static const char *servalpn;
2491 static int alpn_select_cb(SSL *ssl, const unsigned char **out,
2492 unsigned char *outlen, const unsigned char *in,
2493 unsigned int inlen, void *arg)
2495 unsigned int protlen = 0;
2496 const unsigned char *prot;
2498 for (prot = in; prot < in + inlen; prot += protlen) {
2500 if (in + inlen < prot + protlen)
2501 return SSL_TLSEXT_ERR_NOACK;
2503 if (protlen == strlen(servalpn)
2504 && memcmp(prot, servalpn, protlen) == 0) {
2507 return SSL_TLSEXT_ERR_OK;
2511 return SSL_TLSEXT_ERR_NOACK;
2514 /* Test that a PSK can be used to send early_data */
2515 static int test_early_data_psk(int idx)
2517 SSL_CTX *cctx = NULL, *sctx = NULL;
2518 SSL *clientssl = NULL, *serverssl = NULL;
2520 SSL_SESSION *sess = NULL;
2521 unsigned char alpnlist[] = {
2522 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
2525 #define GOODALPNLEN 9
2526 #define BADALPNLEN 8
2527 #define GOODALPN (alpnlist)
2528 #define BADALPN (alpnlist + GOODALPNLEN)
2530 unsigned char buf[20];
2531 size_t readbytes, written;
2532 int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
2533 int edstatus = SSL_EARLY_DATA_ACCEPTED;
2535 /* We always set this up with a final parameter of "2" for PSK */
2536 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2537 &serverssl, &sess, 2)))
2540 servalpn = "goodalpn";
2543 * Note: There is no test for inconsistent SNI with late client detection.
2544 * This is because servers do not acknowledge SNI even if they are using
2545 * it in a resumption handshake - so it is not actually possible for a
2546 * client to detect a problem.
2550 /* Set inconsistent SNI (early client detection) */
2551 err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
2552 if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
2553 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
2558 /* Set inconsistent ALPN (early client detection) */
2559 err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
2560 /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
2561 if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
2563 || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
2570 * Set invalid protocol version. Technically this affects PSKs without
2571 * early_data too, but we test it here because it is similar to the
2572 * SNI/ALPN consistency tests.
2574 err = SSL_R_BAD_PSK;
2575 if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
2581 * Set inconsistent SNI (server detected). In this case the connection
2582 * will succeed but reject early_data.
2584 SSL_SESSION_free(serverpsk);
2585 serverpsk = SSL_SESSION_dup(clientpsk);
2586 if (!TEST_ptr(serverpsk)
2587 || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
2589 edstatus = SSL_EARLY_DATA_REJECTED;
2590 readearlyres = SSL_READ_EARLY_DATA_FINISH;
2593 /* Set consistent SNI */
2594 if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
2595 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
2596 || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
2603 * Set inconsistent ALPN (server detected). In this case the connection
2604 * will succeed but reject early_data.
2606 servalpn = "badalpn";
2607 edstatus = SSL_EARLY_DATA_REJECTED;
2608 readearlyres = SSL_READ_EARLY_DATA_FINISH;
2612 * Set consistent ALPN.
2613 * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
2614 * accepts a list of protos (each one length prefixed).
2615 * SSL_set1_alpn_selected accepts a single protocol (not length
2618 if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
2620 || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
2624 SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
2628 /* Set inconsistent ALPN (late client detection) */
2629 SSL_SESSION_free(serverpsk);
2630 serverpsk = SSL_SESSION_dup(clientpsk);
2631 if (!TEST_ptr(serverpsk)
2632 || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
2635 || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
2638 || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
2641 SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
2642 edstatus = SSL_EARLY_DATA_ACCEPTED;
2643 readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
2644 /* SSL_connect() call should fail */
2649 TEST_error("Bad test index");
2653 SSL_set_connect_state(clientssl);
2655 if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2657 || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
2658 || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
2661 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2665 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2666 &readbytes), readearlyres)
2667 || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS
2668 && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
2669 || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
2670 || !TEST_int_eq(SSL_connect(clientssl), connectres))
2677 SSL_SESSION_free(sess);
2678 SSL_SESSION_free(clientpsk);
2679 SSL_SESSION_free(serverpsk);
2680 clientpsk = serverpsk = NULL;
2681 SSL_free(serverssl);
2682 SSL_free(clientssl);
2689 * Test that a server that doesn't try to read early data can handle a
2690 * client sending some.
2692 static int test_early_data_not_expected(int idx)
2694 SSL_CTX *cctx = NULL, *sctx = NULL;
2695 SSL *clientssl = NULL, *serverssl = NULL;
2697 SSL_SESSION *sess = NULL;
2698 unsigned char buf[20];
2699 size_t readbytes, written;
2701 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2702 &serverssl, &sess, idx)))
2705 /* Write some early data */
2706 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2711 * Server should skip over early data and then block waiting for client to
2712 * continue handshake
2714 if (!TEST_int_le(SSL_accept(serverssl), 0)
2715 || !TEST_int_gt(SSL_connect(clientssl), 0)
2716 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2717 SSL_EARLY_DATA_REJECTED)
2718 || !TEST_int_gt(SSL_accept(serverssl), 0)
2719 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
2720 SSL_EARLY_DATA_REJECTED))
2723 /* Send some normal data from client to server */
2724 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
2725 || !TEST_size_t_eq(written, strlen(MSG2)))
2728 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2729 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
2735 SSL_SESSION_free(sess);
2736 SSL_SESSION_free(clientpsk);
2737 SSL_SESSION_free(serverpsk);
2738 clientpsk = serverpsk = NULL;
2739 SSL_free(serverssl);
2740 SSL_free(clientssl);
2747 # ifndef OPENSSL_NO_TLS1_2
2749 * Test that a server attempting to read early data can handle a connection
2750 * from a TLSv1.2 client.
2752 static int test_early_data_tls1_2(int idx)
2754 SSL_CTX *cctx = NULL, *sctx = NULL;
2755 SSL *clientssl = NULL, *serverssl = NULL;
2757 unsigned char buf[20];
2758 size_t readbytes, written;
2760 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2761 &serverssl, NULL, idx)))
2764 /* Write some data - should block due to handshake with server */
2765 SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
2766 SSL_set_connect_state(clientssl);
2767 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
2771 * Server should do TLSv1.2 handshake. First it will block waiting for more
2772 * messages from client after ServerDone. Then SSL_read_early_data should
2773 * finish and detect that early data has not been sent
2775 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2777 SSL_READ_EARLY_DATA_ERROR))
2781 * Continue writing the message we started earlier. Will still block waiting
2782 * for the CCS/Finished from server
2784 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
2785 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2787 SSL_READ_EARLY_DATA_FINISH)
2788 || !TEST_size_t_eq(readbytes, 0)
2789 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2790 SSL_EARLY_DATA_NOT_SENT))
2793 /* Continue writing the message we started earlier */
2794 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
2795 || !TEST_size_t_eq(written, strlen(MSG1))
2796 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
2797 SSL_EARLY_DATA_NOT_SENT)
2798 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2799 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
2800 || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
2801 || !TEST_size_t_eq(written, strlen(MSG2))
2802 || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
2803 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
2809 SSL_SESSION_free(clientpsk);
2810 SSL_SESSION_free(serverpsk);
2811 clientpsk = serverpsk = NULL;
2812 SSL_free(serverssl);
2813 SSL_free(clientssl);
2819 # endif /* OPENSSL_NO_TLS1_2 */
2822 * Test configuring the TLSv1.3 ciphersuites
2824 * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
2825 * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
2826 * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
2827 * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
2828 * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
2829 * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
2830 * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
2831 * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
2832 * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
2833 * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
2835 static int test_set_ciphersuite(int idx)
2837 SSL_CTX *cctx = NULL, *sctx = NULL;
2838 SSL *clientssl = NULL, *serverssl = NULL;
2841 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
2842 TLS1_VERSION, TLS_MAX_VERSION,
2843 &sctx, &cctx, cert, privkey))
2844 || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
2845 "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
2848 if (idx >=4 && idx <= 7) {
2849 /* SSL_CTX explicit cipher list */
2850 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
2854 if (idx == 0 || idx == 4) {
2855 /* Default ciphersuite */
2856 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
2857 "TLS_AES_128_GCM_SHA256")))
2859 } else if (idx == 1 || idx == 5) {
2860 /* Non default ciphersuite */
2861 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
2862 "TLS_AES_128_CCM_SHA256")))
2866 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2867 &clientssl, NULL, NULL)))
2870 if (idx == 8 || idx == 9) {
2871 /* SSL explicit cipher list */
2872 if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
2876 if (idx == 2 || idx == 6 || idx == 8) {
2877 /* Default ciphersuite */
2878 if (!TEST_true(SSL_set_ciphersuites(clientssl,
2879 "TLS_AES_128_GCM_SHA256")))
2881 } else if (idx == 3 || idx == 7 || idx == 9) {
2882 /* Non default ciphersuite */
2883 if (!TEST_true(SSL_set_ciphersuites(clientssl,
2884 "TLS_AES_128_CCM_SHA256")))
2888 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
2894 SSL_free(serverssl);
2895 SSL_free(clientssl);
2902 static int test_ciphersuite_change(void)
2904 SSL_CTX *cctx = NULL, *sctx = NULL;
2905 SSL *clientssl = NULL, *serverssl = NULL;
2906 SSL_SESSION *clntsess = NULL;
2908 const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
2910 /* Create a session based on SHA-256 */
2911 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
2912 TLS1_VERSION, TLS_MAX_VERSION,
2913 &sctx, &cctx, cert, privkey))
2914 || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
2915 "TLS_AES_128_GCM_SHA256"))
2916 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2917 &clientssl, NULL, NULL))
2918 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2922 clntsess = SSL_get1_session(clientssl);
2923 /* Save for later */
2924 aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
2925 SSL_shutdown(clientssl);
2926 SSL_shutdown(serverssl);
2927 SSL_free(serverssl);
2928 SSL_free(clientssl);
2929 serverssl = clientssl = NULL;
2931 # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
2932 /* Check we can resume a session with a different SHA-256 ciphersuite */
2933 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
2934 "TLS_CHACHA20_POLY1305_SHA256"))
2935 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2937 || !TEST_true(SSL_set_session(clientssl, clntsess))
2938 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2940 || !TEST_true(SSL_session_reused(clientssl)))
2943 SSL_SESSION_free(clntsess);
2944 clntsess = SSL_get1_session(clientssl);
2945 SSL_shutdown(clientssl);
2946 SSL_shutdown(serverssl);
2947 SSL_free(serverssl);
2948 SSL_free(clientssl);
2949 serverssl = clientssl = NULL;
2953 * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
2954 * succeeds but does not resume.
2956 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
2957 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2959 || !TEST_true(SSL_set_session(clientssl, clntsess))
2960 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2962 || !TEST_false(SSL_session_reused(clientssl)))
2965 SSL_SESSION_free(clntsess);
2967 SSL_shutdown(clientssl);
2968 SSL_shutdown(serverssl);
2969 SSL_free(serverssl);
2970 SSL_free(clientssl);
2971 serverssl = clientssl = NULL;
2973 /* Create a session based on SHA384 */
2974 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
2975 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2976 &clientssl, NULL, NULL))
2977 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2981 clntsess = SSL_get1_session(clientssl);
2982 SSL_shutdown(clientssl);
2983 SSL_shutdown(serverssl);
2984 SSL_free(serverssl);
2985 SSL_free(clientssl);
2986 serverssl = clientssl = NULL;
2988 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
2989 "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
2990 || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
2991 "TLS_AES_256_GCM_SHA384"))
2992 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2994 || !TEST_true(SSL_set_session(clientssl, clntsess))
2996 * We use SSL_ERROR_WANT_READ below so that we can pause the
2997 * connection after the initial ClientHello has been sent to
2998 * enable us to make some session changes.
3000 || !TEST_false(create_ssl_connection(serverssl, clientssl,
3001 SSL_ERROR_WANT_READ)))
3004 /* Trick the client into thinking this session is for a different digest */
3005 clntsess->cipher = aes_128_gcm_sha256;
3006 clntsess->cipher_id = clntsess->cipher->id;
3009 * Continue the previously started connection. Server has selected a SHA-384
3010 * ciphersuite, but client thinks the session is for SHA-256, so it should
3013 if (!TEST_false(create_ssl_connection(serverssl, clientssl,
3015 || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
3016 SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
3022 SSL_SESSION_free(clntsess);
3023 SSL_free(serverssl);
3024 SSL_free(clientssl);
3033 * Test 0 = Test new style callbacks
3034 * Test 1 = Test both new and old style callbacks
3035 * Test 2 = Test old style callbacks
3036 * Test 3 = Test old style callbacks with no certificate
3038 static int test_tls13_psk(int idx)
3040 SSL_CTX *sctx = NULL, *cctx = NULL;
3041 SSL *serverssl = NULL, *clientssl = NULL;
3042 const SSL_CIPHER *cipher = NULL;
3043 const unsigned char key[] = {
3044 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
3045 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
3046 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
3047 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
3051 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
3052 TLS1_VERSION, TLS_MAX_VERSION,
3053 &sctx, &cctx, idx == 3 ? NULL : cert,
3054 idx == 3 ? NULL : privkey)))
3059 * We use a ciphersuite with SHA256 to ease testing old style PSK
3060 * callbacks which will always default to SHA256. This should not be
3061 * necessary if we have no cert/priv key. In that case the server should
3062 * prefer SHA256 automatically.
3064 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
3065 "TLS_AES_128_GCM_SHA256")))
3070 * Test 0: New style callbacks only
3071 * Test 1: New and old style callbacks (only the new ones should be used)
3072 * Test 2: Old style callbacks only
3074 if (idx == 0 || idx == 1) {
3075 SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
3076 SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
3078 #ifndef OPENSSL_NO_PSK
3080 SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
3081 SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
3085 use_session_cb_cnt = 0;
3086 find_session_cb_cnt = 0;
3087 psk_client_cb_cnt = 0;
3088 psk_server_cb_cnt = 0;
3092 * Check we can create a connection if callback decides not to send a
3095 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3097 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3099 || !TEST_false(SSL_session_reused(clientssl))
3100 || !TEST_false(SSL_session_reused(serverssl)))
3103 if (idx == 0 || idx == 1) {
3104 if (!TEST_true(use_session_cb_cnt == 1)
3105 || !TEST_true(find_session_cb_cnt == 0)
3107 * If no old style callback then below should be 0
3110 || !TEST_true(psk_client_cb_cnt == idx)
3111 || !TEST_true(psk_server_cb_cnt == 0))
3114 if (!TEST_true(use_session_cb_cnt == 0)
3115 || !TEST_true(find_session_cb_cnt == 0)
3116 || !TEST_true(psk_client_cb_cnt == 1)
3117 || !TEST_true(psk_server_cb_cnt == 0))
3121 shutdown_ssl_connection(serverssl, clientssl);
3122 serverssl = clientssl = NULL;
3123 use_session_cb_cnt = psk_client_cb_cnt = 0;
3126 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3130 /* Create the PSK */
3131 cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
3132 clientpsk = SSL_SESSION_new();
3133 if (!TEST_ptr(clientpsk)
3134 || !TEST_ptr(cipher)
3135 || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
3137 || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
3138 || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
3140 || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
3142 serverpsk = clientpsk;
3144 /* Check we can create a connection and the PSK is used */
3145 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3146 || !TEST_true(SSL_session_reused(clientssl))
3147 || !TEST_true(SSL_session_reused(serverssl)))
3150 if (idx == 0 || idx == 1) {
3151 if (!TEST_true(use_session_cb_cnt == 1)
3152 || !TEST_true(find_session_cb_cnt == 1)
3153 || !TEST_true(psk_client_cb_cnt == 0)
3154 || !TEST_true(psk_server_cb_cnt == 0))
3157 if (!TEST_true(use_session_cb_cnt == 0)
3158 || !TEST_true(find_session_cb_cnt == 0)
3159 || !TEST_true(psk_client_cb_cnt == 1)
3160 || !TEST_true(psk_server_cb_cnt == 1))
3164 shutdown_ssl_connection(serverssl, clientssl);
3165 serverssl = clientssl = NULL;
3166 use_session_cb_cnt = find_session_cb_cnt = 0;
3167 psk_client_cb_cnt = psk_server_cb_cnt = 0;
3169 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3174 if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
3178 * Check we can create a connection, the PSK is used and the callbacks are
3181 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3182 || !TEST_true(SSL_session_reused(clientssl))
3183 || !TEST_true(SSL_session_reused(serverssl)))
3186 if (idx == 0 || idx == 1) {
3187 if (!TEST_true(use_session_cb_cnt == 2)
3188 || !TEST_true(find_session_cb_cnt == 2)
3189 || !TEST_true(psk_client_cb_cnt == 0)
3190 || !TEST_true(psk_server_cb_cnt == 0))
3193 if (!TEST_true(use_session_cb_cnt == 0)
3194 || !TEST_true(find_session_cb_cnt == 0)
3195 || !TEST_true(psk_client_cb_cnt == 2)
3196 || !TEST_true(psk_server_cb_cnt == 2))
3200 shutdown_ssl_connection(serverssl, clientssl);
3201 serverssl = clientssl = NULL;
3202 use_session_cb_cnt = find_session_cb_cnt = 0;
3203 psk_client_cb_cnt = psk_server_cb_cnt = 0;
3207 * Check that if the server rejects the PSK we can still connect, but with
3210 srvid = "Dummy Identity";
3211 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3213 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3215 || !TEST_false(SSL_session_reused(clientssl))
3216 || !TEST_false(SSL_session_reused(serverssl)))
3219 if (idx == 0 || idx == 1) {
3220 if (!TEST_true(use_session_cb_cnt == 1)
3221 || !TEST_true(find_session_cb_cnt == 1)
3222 || !TEST_true(psk_client_cb_cnt == 0)
3224 * If no old style callback then below should be 0
3227 || !TEST_true(psk_server_cb_cnt == idx))
3230 if (!TEST_true(use_session_cb_cnt == 0)
3231 || !TEST_true(find_session_cb_cnt == 0)
3232 || !TEST_true(psk_client_cb_cnt == 1)
3233 || !TEST_true(psk_server_cb_cnt == 1))
3237 shutdown_ssl_connection(serverssl, clientssl);
3238 serverssl = clientssl = NULL;
3243 SSL_SESSION_free(clientpsk);
3244 SSL_SESSION_free(serverpsk);
3245 clientpsk = serverpsk = NULL;
3246 SSL_free(serverssl);
3247 SSL_free(clientssl);
3253 static unsigned char cookie_magic_value[] = "cookie magic";
3255 static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
3256 unsigned int *cookie_len)
3259 * Not suitable as a real cookie generation function but good enough for
3262 memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
3263 *cookie_len = sizeof(cookie_magic_value) - 1;
3268 static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
3269 unsigned int cookie_len)
3271 if (cookie_len == sizeof(cookie_magic_value) - 1
3272 && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
3278 static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
3282 int res = generate_cookie_callback(ssl, cookie, &temp);
3287 static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
3290 return verify_cookie_callback(ssl, cookie, cookie_len);
3293 static int test_stateless(void)
3295 SSL_CTX *sctx = NULL, *cctx = NULL;
3296 SSL *serverssl = NULL, *clientssl = NULL;
3299 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
3300 TLS1_VERSION, TLS_MAX_VERSION,
3301 &sctx, &cctx, cert, privkey)))
3304 /* The arrival of CCS messages can confuse the test */
3305 SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
3307 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3309 /* Send the first ClientHello */
3310 || !TEST_false(create_ssl_connection(serverssl, clientssl,
3311 SSL_ERROR_WANT_READ))
3313 * This should fail with a -1 return because we have no callbacks
3316 || !TEST_int_eq(SSL_stateless(serverssl), -1))
3319 /* Fatal error so abandon the connection from this client */
3320 SSL_free(clientssl);
3323 /* Set up the cookie generation and verification callbacks */
3324 SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
3325 SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
3328 * Create a new connection from the client (we can reuse the server SSL
3331 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3333 /* Send the first ClientHello */
3334 || !TEST_false(create_ssl_connection(serverssl, clientssl,
3335 SSL_ERROR_WANT_READ))
3336 /* This should fail because there is no cookie */
3337 || !TEST_int_eq(SSL_stateless(serverssl), 0))
3340 /* Abandon the connection from this client */
3341 SSL_free(clientssl);
3345 * Now create a connection from a new client but with the same server SSL
3348 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3350 /* Send the first ClientHello */
3351 || !TEST_false(create_ssl_connection(serverssl, clientssl,
3352 SSL_ERROR_WANT_READ))
3353 /* This should fail because there is no cookie */
3354 || !TEST_int_eq(SSL_stateless(serverssl), 0)
3355 /* Send the second ClientHello */
3356 || !TEST_false(create_ssl_connection(serverssl, clientssl,
3357 SSL_ERROR_WANT_READ))
3358 /* This should succeed because a cookie is now present */
3359 || !TEST_int_eq(SSL_stateless(serverssl), 1)
3360 /* Complete the connection */
3361 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3365 shutdown_ssl_connection(serverssl, clientssl);
3366 serverssl = clientssl = NULL;
3370 SSL_free(serverssl);
3371 SSL_free(clientssl);
3377 #endif /* OPENSSL_NO_TLS1_3 */
3379 static int clntaddoldcb = 0;
3380 static int clntparseoldcb = 0;
3381 static int srvaddoldcb = 0;
3382 static int srvparseoldcb = 0;
3383 static int clntaddnewcb = 0;
3384 static int clntparsenewcb = 0;
3385 static int srvaddnewcb = 0;
3386 static int srvparsenewcb = 0;
3387 static int snicb = 0;
3389 #define TEST_EXT_TYPE1 0xff00
3391 static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
3392 size_t *outlen, int *al, void *add_arg)
3394 int *server = (int *)add_arg;
3395 unsigned char *data;
3397 if (SSL_is_server(s))
3402 if (*server != SSL_is_server(s)
3403 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
3408 *outlen = sizeof(char);
3412 static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
3415 OPENSSL_free((unsigned char *)out);
3418 static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
3419 size_t inlen, int *al, void *parse_arg)
3421 int *server = (int *)parse_arg;
3423 if (SSL_is_server(s))
3428 if (*server != SSL_is_server(s)
3429 || inlen != sizeof(char)
3436 static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
3437 const unsigned char **out, size_t *outlen, X509 *x,
3438 size_t chainidx, int *al, void *add_arg)
3440 int *server = (int *)add_arg;
3441 unsigned char *data;
3443 if (SSL_is_server(s))
3448 if (*server != SSL_is_server(s)
3449 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
3454 *outlen = sizeof(*data);
3458 static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
3459 const unsigned char *out, void *add_arg)
3461 OPENSSL_free((unsigned char *)out);
3464 static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
3465 const unsigned char *in, size_t inlen, X509 *x,
3466 size_t chainidx, int *al, void *parse_arg)
3468 int *server = (int *)parse_arg;
3470 if (SSL_is_server(s))
3475 if (*server != SSL_is_server(s)
3476 || inlen != sizeof(char) || *in != 1)
3482 static int sni_cb(SSL *s, int *al, void *arg)
3484 SSL_CTX *ctx = (SSL_CTX *)arg;
3486 if (SSL_set_SSL_CTX(s, ctx) == NULL) {
3487 *al = SSL_AD_INTERNAL_ERROR;
3488 return SSL_TLSEXT_ERR_ALERT_FATAL;
3491 return SSL_TLSEXT_ERR_OK;
3495 * Custom call back tests.
3496 * Test 0: Old style callbacks in TLSv1.2
3497 * Test 1: New style callbacks in TLSv1.2
3498 * Test 2: New style callbacks in TLSv1.2 with SNI
3499 * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
3500 * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
3502 static int test_custom_exts(int tst)
3504 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
3505 SSL *clientssl = NULL, *serverssl = NULL;
3507 static int server = 1;
3508 static int client = 0;
3509 SSL_SESSION *sess = NULL;
3510 unsigned int context;
3512 #if defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_TLS1_3)
3513 /* Skip tests for TLSv1.2 and below in this case */
3518 /* Reset callback counters */
3519 clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
3520 clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
3523 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
3524 TLS1_VERSION, TLS_MAX_VERSION,
3525 &sctx, &cctx, cert, privkey)))
3529 && !TEST_true(create_ssl_ctx_pair(TLS_server_method(), NULL,
3530 TLS1_VERSION, TLS_MAX_VERSION,
3531 &sctx2, NULL, cert, privkey)))
3536 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
3537 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
3539 SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
3543 context = SSL_EXT_CLIENT_HELLO
3544 | SSL_EXT_TLS1_2_SERVER_HELLO
3545 | SSL_EXT_TLS1_3_SERVER_HELLO
3546 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
3547 | SSL_EXT_TLS1_3_CERTIFICATE
3548 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
3550 context = SSL_EXT_CLIENT_HELLO
3551 | SSL_EXT_TLS1_2_SERVER_HELLO
3552 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
3555 /* Create a client side custom extension */
3557 if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
3558 old_add_cb, old_free_cb,
3559 &client, old_parse_cb,
3563 if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
3564 new_add_cb, new_free_cb,
3565 &client, new_parse_cb, &client)))
3569 /* Should not be able to add duplicates */
3570 if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
3571 old_add_cb, old_free_cb,
3572 &client, old_parse_cb,
3574 || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
3575 context, new_add_cb,
3576 new_free_cb, &client,
3577 new_parse_cb, &client)))
3580 /* Create a server side custom extension */
3582 if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
3583 old_add_cb, old_free_cb,
3584 &server, old_parse_cb,
3588 if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
3589 new_add_cb, new_free_cb,
3590 &server, new_parse_cb, &server)))
3593 && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
3594 context, new_add_cb,
3595 new_free_cb, &server,
3596 new_parse_cb, &server)))
3600 /* Should not be able to add duplicates */
3601 if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
3602 old_add_cb, old_free_cb,
3603 &server, old_parse_cb,
3605 || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
3606 context, new_add_cb,
3607 new_free_cb, &server,
3608 new_parse_cb, &server)))
3613 if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
3614 || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
3618 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3619 &clientssl, NULL, NULL))
3620 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3625 if (clntaddoldcb != 1
3626 || clntparseoldcb != 1
3628 || srvparseoldcb != 1)
3630 } else if (tst == 1 || tst == 2 || tst == 3) {
3631 if (clntaddnewcb != 1
3632 || clntparsenewcb != 1
3634 || srvparsenewcb != 1
3635 || (tst != 2 && snicb != 0)
3636 || (tst == 2 && snicb != 1))
3639 /* In this case there 2 NewSessionTicket messages created */
3640 if (clntaddnewcb != 1
3641 || clntparsenewcb != 5
3643 || srvparsenewcb != 1)
3647 sess = SSL_get1_session(clientssl);
3648 SSL_shutdown(clientssl);
3649 SSL_shutdown(serverssl);
3650 SSL_free(serverssl);
3651 SSL_free(clientssl);
3652 serverssl = clientssl = NULL;
3655 /* We don't bother with the resumption aspects for this test */
3660 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3662 || !TEST_true(SSL_set_session(clientssl, sess))
3663 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3668 * For a resumed session we expect to add the ClientHello extension. For the
3669 * old style callbacks we ignore it on the server side because they set
3670 * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
3674 if (clntaddoldcb != 2
3675 || clntparseoldcb != 1
3677 || srvparseoldcb != 1)
3679 } else if (tst == 1 || tst == 2 || tst == 3) {
3680 if (clntaddnewcb != 2
3681 || clntparsenewcb != 2
3683 || srvparsenewcb != 2)
3687 * No Certificate message extensions in the resumption handshake,
3688 * 2 NewSessionTickets in the initial handshake, 1 in the resumption
3690 if (clntaddnewcb != 2
3691 || clntparsenewcb != 8
3693 || srvparsenewcb != 2)
3700 SSL_SESSION_free(sess);
3701 SSL_free(serverssl);
3702 SSL_free(clientssl);
3703 SSL_CTX_free(sctx2);
3710 * Test loading of serverinfo data in various formats. test_sslmessages actually
3711 * tests to make sure the extensions appear in the handshake
3713 static int test_serverinfo(int tst)
3715 unsigned int version;
3716 unsigned char *sibuf;
3718 int ret, expected, testresult = 0;
3721 ctx = SSL_CTX_new(TLS_method());
3725 if ((tst & 0x01) == 0x01)
3726 version = SSL_SERVERINFOV2;
3728 version = SSL_SERVERINFOV1;
3730 if ((tst & 0x02) == 0x02) {
3731 sibuf = serverinfov2;
3732 sibuflen = sizeof(serverinfov2);
3733 expected = (version == SSL_SERVERINFOV2);
3735 sibuf = serverinfov1;
3736 sibuflen = sizeof(serverinfov1);
3737 expected = (version == SSL_SERVERINFOV1);
3740 if ((tst & 0x04) == 0x04) {
3741 ret = SSL_CTX_use_serverinfo_ex(ctx, version, sibuf, sibuflen);
3743 ret = SSL_CTX_use_serverinfo(ctx, sibuf, sibuflen);
3746 * The version variable is irrelevant in this case - it's what is in the
3747 * buffer that matters
3749 if ((tst & 0x02) == 0x02)
3755 if (!TEST_true(ret == expected))
3767 * Test that SSL_export_keying_material() produces expected results. There are
3768 * no test vectors so all we do is test that both sides of the communication
3769 * produce the same results for different protocol versions.
3771 static int test_export_key_mat(int tst)
3774 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
3775 SSL *clientssl = NULL, *serverssl = NULL;
3776 const char label[] = "test label";
3777 const unsigned char context[] = "context";
3778 const unsigned char *emptycontext = NULL;
3779 unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
3780 unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
3781 const int protocols[] = {
3788 #ifdef OPENSSL_NO_TLS1
3792 #ifdef OPENSSL_NO_TLS1_1
3796 #ifdef OPENSSL_NO_TLS1_2
3800 #ifdef OPENSSL_NO_TLS1_3
3804 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
3805 TLS1_VERSION, TLS_MAX_VERSION,
3806 &sctx, &cctx, cert, privkey)))
3809 OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
3810 SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
3811 SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
3813 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
3815 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3819 if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
3820 sizeof(ckeymat1), label,
3821 sizeof(label) - 1, context,
3822 sizeof(context) - 1, 1), 1)
3823 || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
3824 sizeof(ckeymat2), label,
3828 || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
3829 sizeof(ckeymat3), label,
3832 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
3833 sizeof(skeymat1), label,
3836 sizeof(context) -1, 1),
3838 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
3839 sizeof(skeymat2), label,
3843 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
3844 sizeof(skeymat3), label,
3848 * Check that both sides created the same key material with the
3851 || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
3854 * Check that both sides created the same key material with an
3857 || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
3860 * Check that both sides created the same key material without a
3863 || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
3865 /* Different contexts should produce different results */
3866 || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
3871 * Check that an empty context and no context produce different results in
3872 * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
3874 if ((tst != 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
3876 || (tst ==3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
3883 SSL_free(serverssl);
3884 SSL_free(clientssl);
3885 SSL_CTX_free(sctx2);
3892 #ifndef OPENSSL_NO_TLS1_3
3894 * Test that SSL_export_keying_material_early() produces expected
3895 * results. There are no test vectors so all we do is test that both
3896 * sides of the communication produce the same results for different
3897 * protocol versions.
3899 static int test_export_key_mat_early(int idx)
3901 static const char label[] = "test label";
3902 static const unsigned char context[] = "context";
3904 SSL_CTX *cctx = NULL, *sctx = NULL;
3905 SSL *clientssl = NULL, *serverssl = NULL;
3906 SSL_SESSION *sess = NULL;
3907 const unsigned char *emptycontext = NULL;
3908 unsigned char ckeymat1[80], ckeymat2[80];
3909 unsigned char skeymat1[80], skeymat2[80];
3910 unsigned char buf[1];
3911 size_t readbytes, written;
3913 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
3917 /* Here writing 0 length early data is enough. */
3918 if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
3919 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3921 SSL_READ_EARLY_DATA_ERROR)
3922 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3923 SSL_EARLY_DATA_ACCEPTED))
3926 if (!TEST_int_eq(SSL_export_keying_material_early(
3927 clientssl, ckeymat1, sizeof(ckeymat1), label,
3928 sizeof(label) - 1, context, sizeof(context) - 1), 1)
3929 || !TEST_int_eq(SSL_export_keying_material_early(
3930 clientssl, ckeymat2, sizeof(ckeymat2), label,
3931 sizeof(label) - 1, emptycontext, 0), 1)
3932 || !TEST_int_eq(SSL_export_keying_material_early(
3933 serverssl, skeymat1, sizeof(skeymat1), label,
3934 sizeof(label) - 1, context, sizeof(context) - 1), 1)
3935 || !TEST_int_eq(SSL_export_keying_material_early(
3936 serverssl, skeymat2, sizeof(skeymat2), label,
3937 sizeof(label) - 1, emptycontext, 0), 1)
3939 * Check that both sides created the same key material with the
3942 || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
3945 * Check that both sides created the same key material with an
3948 || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
3950 /* Different contexts should produce different results */
3951 || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
3958 SSL_SESSION_free(sess);
3959 SSL_SESSION_free(clientpsk);
3960 SSL_SESSION_free(serverpsk);
3961 clientpsk = serverpsk = NULL;
3962 SSL_free(serverssl);
3963 SSL_free(clientssl);
3969 #endif /* OPENSSL_NO_TLS1_3 */
3971 static int test_ssl_clear(int idx)
3973 SSL_CTX *cctx = NULL, *sctx = NULL;
3974 SSL *clientssl = NULL, *serverssl = NULL;
3977 #ifdef OPENSSL_NO_TLS1_2
3982 /* Create an initial connection */
3983 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
3984 TLS1_VERSION, TLS_MAX_VERSION,
3985 &sctx, &cctx, cert, privkey))
3987 && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
3989 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3990 &clientssl, NULL, NULL))
3991 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3995 SSL_shutdown(clientssl);
3996 SSL_shutdown(serverssl);
3997 SSL_free(serverssl);
4000 /* Clear clientssl - we're going to reuse the object */
4001 if (!TEST_true(SSL_clear(clientssl)))
4004 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4006 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4008 || !TEST_true(SSL_session_reused(clientssl)))
4011 SSL_shutdown(clientssl);
4012 SSL_shutdown(serverssl);
4017 SSL_free(serverssl);
4018 SSL_free(clientssl);
4025 /* Parse CH and retrieve any MFL extension value if present */
4026 static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
4029 unsigned char *data;
4030 PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
4031 unsigned int MFL_code = 0, type = 0;
4033 if (!TEST_uint_gt( len = BIO_get_mem_data( bio, (char **) &data ), 0 ) )
4036 if (!TEST_true( PACKET_buf_init( &pkt, data, len ) )
4037 /* Skip the record header */
4038 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
4039 /* Skip the handshake message header */
4040 || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
4041 /* Skip client version and random */
4042 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
4043 + SSL3_RANDOM_SIZE))
4044 /* Skip session id */
4045 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
4047 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
4048 /* Skip compression */
4049 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
4050 /* Extensions len */
4051 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
4054 /* Loop through all extensions */
4055 while (PACKET_remaining(&pkt2)) {
4056 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
4057 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
4060 if (type == TLSEXT_TYPE_max_fragment_length) {
4061 if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
4062 || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
4065 *mfl_codemfl_code = MFL_code;
4074 /* Maximum-Fragment-Length TLS extension mode to test */
4075 static const unsigned char max_fragment_len_test[] = {
4076 TLSEXT_max_fragment_length_512,
4077 TLSEXT_max_fragment_length_1024,
4078 TLSEXT_max_fragment_length_2048,
4079 TLSEXT_max_fragment_length_4096
4082 static int test_max_fragment_len_ext(int idx_tst)
4086 int testresult = 0, MFL_mode = 0;
4089 ctx = SSL_CTX_new(TLS_method());
4093 if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
4094 ctx, max_fragment_len_test[idx_tst])))
4101 rbio = BIO_new(BIO_s_mem());
4102 wbio = BIO_new(BIO_s_mem());
4103 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
4109 SSL_set_bio(con, rbio, wbio);
4110 SSL_set_connect_state(con);
4112 if (!TEST_int_le(SSL_connect(con), 0)) {
4113 /* This shouldn't succeed because we don't have a server! */
4117 if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
4118 /* no MFL in client hello */
4120 if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
4132 #ifndef OPENSSL_NO_TLS1_3
4133 static int test_pha_key_update(void)
4135 SSL_CTX *cctx = NULL, *sctx = NULL;
4136 SSL *clientssl = NULL, *serverssl = NULL;
4139 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
4140 TLS1_VERSION, TLS_MAX_VERSION,
4141 &sctx, &cctx, cert, privkey)))
4144 if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
4145 || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
4146 || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
4147 || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
4151 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4155 SSL_force_post_handshake_auth(clientssl);
4157 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
4161 SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
4162 if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
4165 if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
4168 /* Start handshake on the server */
4169 if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
4172 /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
4173 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
4177 SSL_shutdown(clientssl);
4178 SSL_shutdown(serverssl);
4183 SSL_free(serverssl);
4184 SSL_free(clientssl);
4191 #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
4193 static SRP_VBASE *vbase = NULL;
4195 static int ssl_srp_cb(SSL *s, int *ad, void *arg)
4197 int ret = SSL3_AL_FATAL;
4199 SRP_user_pwd *user = NULL;
4201 username = SSL_get_srp_username(s);
4202 if (username == NULL) {
4203 *ad = SSL_AD_INTERNAL_ERROR;
4207 user = SRP_VBASE_get1_by_user(vbase, username);
4209 *ad = SSL_AD_INTERNAL_ERROR;
4213 if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
4215 *ad = SSL_AD_INTERNAL_ERROR;
4222 SRP_user_pwd_free(user);
4226 static int create_new_vfile(char *userid, char *password, const char *filename)
4229 OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
4232 BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
4235 if (!TEST_ptr(dummy) || !TEST_ptr(row))
4238 gNid = SRP_create_verifier(userid, password, &row[DB_srpsalt],
4239 &row[DB_srpverifier], NULL, NULL);
4240 if (!TEST_ptr(gNid))
4244 * The only way to create an empty TXT_DB is to provide a BIO with no data
4247 db = TXT_DB_read(dummy, DB_NUMBER);
4251 out = BIO_new_file(filename, "w");
4255 row[DB_srpid] = OPENSSL_strdup(userid);
4256 row[DB_srptype] = OPENSSL_strdup("V");
4257 row[DB_srpgN] = OPENSSL_strdup(gNid);
4259 if (!TEST_ptr(row[DB_srpid])
4260 || !TEST_ptr(row[DB_srptype])
4261 || !TEST_ptr(row[DB_srpgN])
4262 || !TEST_true(TXT_DB_insert(db, row)))
4267 if (!TXT_DB_write(out, db))
4273 for (i = 0; i < DB_NUMBER; i++)
4274 OPENSSL_free(row[i]);
4284 static int create_new_vbase(char *userid, char *password)
4286 BIGNUM *verifier = NULL, *salt = NULL;
4287 const SRP_gN *lgN = NULL;
4288 SRP_user_pwd *user_pwd = NULL;
4291 lgN = SRP_get_default_gN(NULL);
4295 if (!TEST_true(SRP_create_verifier_BN(userid, password, &salt, &verifier,
4299 user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
4300 if (!TEST_ptr(user_pwd))
4303 user_pwd->N = lgN->N;
4304 user_pwd->g = lgN->g;
4305 user_pwd->id = OPENSSL_strdup(userid);
4306 if (!TEST_ptr(user_pwd->id))
4309 user_pwd->v = verifier;
4311 verifier = salt = NULL;
4313 if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
4319 SRP_user_pwd_free(user_pwd);
4329 * Test 0: Simple successful SRP connection, new vbase
4330 * Test 1: Connection failure due to bad password, new vbase
4331 * Test 2: Simple successful SRP connection, vbase loaded from existing file
4332 * Test 3: Connection failure due to bad password, vbase loaded from existing
4334 * Test 4: Simple successful SRP connection, vbase loaded from new file
4335 * Test 5: Connection failure due to bad password, vbase loaded from new file
4337 static int test_srp(int tst)
4339 char *userid = "test", *password = "password", *tstsrpfile;
4340 SSL_CTX *cctx = NULL, *sctx = NULL;
4341 SSL *clientssl = NULL, *serverssl = NULL;
4342 int ret, testresult = 0;
4344 vbase = SRP_VBASE_new(NULL);
4345 if (!TEST_ptr(vbase))
4348 if (tst == 0 || tst == 1) {
4349 if (!TEST_true(create_new_vbase(userid, password)))
4352 if (tst == 4 || tst == 5) {
4353 if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
4355 tstsrpfile = tmpfilename;
4357 tstsrpfile = srpvfile;
4359 if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
4363 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
4364 TLS1_VERSION, TLS_MAX_VERSION,
4365 &sctx, &cctx, cert, privkey)))
4368 if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
4369 || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
4370 || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
4371 || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
4372 || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
4376 if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
4379 if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
4383 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4387 ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
4389 if (!TEST_true(tst % 2 == 0))
4392 if (!TEST_true(tst % 2 == 1))
4399 SRP_VBASE_free(vbase);
4401 SSL_free(serverssl);
4402 SSL_free(clientssl);
4410 static int info_cb_failed = 0;
4411 static int info_cb_offset = 0;
4412 static int info_cb_this_state = -1;
4414 static struct info_cb_states_st {
4416 const char *statestr;
4417 } info_cb_states[][60] = {
4419 /* TLSv1.2 server followed by resumption */
4420 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
4421 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
4422 {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
4423 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
4424 {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
4425 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
4426 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
4427 {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
4428 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"},
4429 {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
4430 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
4431 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
4432 {SSL_CB_EXIT, NULL}, {0, NULL},
4434 /* TLSv1.2 client followed by resumption */
4435 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
4436 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
4437 {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
4438 {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
4439 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
4440 {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
4441 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
4442 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
4443 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
4444 {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
4445 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
4446 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
4448 /* TLSv1.3 server followed by resumption */
4449 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
4450 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
4451 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
4452 {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
4453 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
4454 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
4455 {SSL_CB_LOOP, "TWST"}, {SSL_CB_HANDSHAKE_DONE, NULL},
4456 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TWST"},
4457 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
4458 {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
4459 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"},
4460 {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"},
4461 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL},
4462 {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
4463 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
4464 {SSL_CB_LOOP, "TWST"}, {SSL_CB_HANDSHAKE_DONE, NULL},
4465 {SSL_CB_EXIT, NULL}, {0, NULL},
4467 /* TLSv1.3 client followed by resumption */
4468 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
4469 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
4470 {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
4471 {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
4472 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
4473 {SSL_CB_EXIT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
4474 {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"},
4475 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
4476 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "SSLOK "},
4477 {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"},
4478 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
4479 {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
4480 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
4481 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
4482 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
4483 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
4484 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "SSLOK "},
4485 {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"},
4486 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
4488 /* TLSv1.3 server, early_data */
4489 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
4490 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
4491 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
4492 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
4493 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
4494 {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
4495 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
4496 {SSL_CB_LOOP, "TWST"}, {SSL_CB_HANDSHAKE_DONE, NULL},
4497 {SSL_CB_EXIT, NULL}, {0, NULL},
4499 /* TLSv1.3 client, early_data */
4500 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
4501 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
4502 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
4503 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
4504 {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
4505 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
4506 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
4507 {SSL_CB_EXIT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
4508 {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"},
4509 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
4515 static void sslapi_info_callback(const SSL *s, int where, int ret)
4517 struct info_cb_states_st *state = info_cb_states[info_cb_offset];
4519 /* We do not ever expect a connection to fail in this test */
4520 if (!TEST_false(ret == 0)) {
4526 * Do some sanity checks. We never expect these things to happen in this
4529 if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
4530 || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
4531 || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
4536 /* Now check we're in the right state */
4537 if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
4541 if ((where & SSL_CB_LOOP) != 0
4542 && !TEST_int_eq(strcmp(SSL_state_string(s),
4543 state[info_cb_this_state].statestr), 0)) {
4548 /* Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init */
4549 if ((where & SSL_CB_HANDSHAKE_DONE) && SSL_in_init((SSL *)s) != 0) {
4556 * Test the info callback gets called when we expect it to.
4558 * Test 0: TLSv1.2, server
4559 * Test 1: TLSv1.2, client
4560 * Test 2: TLSv1.3, server
4561 * Test 3: TLSv1.3, client
4562 * Test 4: TLSv1.3, server, early_data
4563 * Test 5: TLSv1.3, client, early_data
4565 static int test_info_callback(int tst)
4567 SSL_CTX *cctx = NULL, *sctx = NULL;
4568 SSL *clientssl = NULL, *serverssl = NULL;
4569 SSL_SESSION *clntsess = NULL;
4574 /* We need either ECDHE or DHE for the TLSv1.2 test to work */
4575 #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
4576 || !defined(OPENSSL_NO_DH))
4577 tlsvers = TLS1_2_VERSION;
4582 #ifndef OPENSSL_NO_TLS1_3
4583 tlsvers = TLS1_3_VERSION;
4591 info_cb_this_state = -1;
4592 info_cb_offset = tst;
4594 #ifndef OPENSSL_NO_TLS1_3
4596 SSL_SESSION *sess = NULL;
4597 size_t written, readbytes;
4598 unsigned char buf[80];
4600 /* early_data tests */
4601 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4602 &serverssl, &sess, 0)))
4605 /* We don't actually need this reference */
4606 SSL_SESSION_free(sess);
4608 SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
4609 sslapi_info_callback);
4611 /* Write and read some early data and then complete the connection */
4612 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4614 || !TEST_size_t_eq(written, strlen(MSG1))
4615 || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
4616 sizeof(buf), &readbytes),
4617 SSL_READ_EARLY_DATA_SUCCESS)
4618 || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
4619 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4620 SSL_EARLY_DATA_ACCEPTED)
4621 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4623 || !TEST_false(info_cb_failed))
4631 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
4632 TLS_client_method(),
4633 tlsvers, tlsvers, &sctx, &cctx, cert,
4638 * For even numbered tests we check the server callbacks. For odd numbers we
4641 SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
4642 sslapi_info_callback);
4644 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4645 &clientssl, NULL, NULL))
4646 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4648 || !TEST_false(info_cb_failed))
4653 clntsess = SSL_get1_session(clientssl);
4654 SSL_shutdown(clientssl);
4655 SSL_shutdown(serverssl);
4656 SSL_free(serverssl);
4657 SSL_free(clientssl);
4658 serverssl = clientssl = NULL;
4660 /* Now do a resumption */
4661 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
4663 || !TEST_true(SSL_set_session(clientssl, clntsess))
4664 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4666 || !TEST_true(SSL_session_reused(clientssl))
4667 || !TEST_false(info_cb_failed))
4673 SSL_free(serverssl);
4674 SSL_free(clientssl);
4675 SSL_SESSION_free(clntsess);
4681 static int test_ssl_pending(int tst)
4683 SSL_CTX *cctx = NULL, *sctx = NULL;
4684 SSL *clientssl = NULL, *serverssl = NULL;
4686 char msg[] = "A test message";
4688 size_t written, readbytes;
4691 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
4692 TLS_client_method(),
4693 TLS1_VERSION, TLS_MAX_VERSION,
4694 &sctx, &cctx, cert, privkey)))
4697 #ifndef OPENSSL_NO_DTLS
4698 if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
4699 DTLS_client_method(),
4700 DTLS1_VERSION, DTLS_MAX_VERSION,
4701 &sctx, &cctx, cert, privkey)))
4708 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4710 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4714 if (!TEST_int_eq(SSL_pending(clientssl), 0)
4715 || !TEST_false(SSL_has_pending(clientssl))
4716 || !TEST_int_eq(SSL_pending(serverssl), 0)
4717 || !TEST_false(SSL_has_pending(serverssl))
4718 || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
4719 || !TEST_size_t_eq(written, sizeof(msg))
4720 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
4721 || !TEST_size_t_eq(readbytes, sizeof(buf))
4722 || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
4723 || !TEST_true(SSL_has_pending(clientssl)))
4729 SSL_free(serverssl);
4730 SSL_free(clientssl);
4738 unsigned int maxprot;
4739 const char *clntciphers;
4740 const char *clnttls13ciphers;
4741 const char *srvrciphers;
4742 const char *srvrtls13ciphers;
4744 } shared_ciphers_data[] = {
4746 * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
4747 * TLSv1.3 is enabled but TLSv1.2 is disabled.
4749 #if defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
4752 "AES128-SHA:AES256-SHA",
4754 "AES256-SHA:DHE-RSA-AES128-SHA",
4760 "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
4762 "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
4764 "AES128-SHA:AES256-SHA"
4768 "AES128-SHA:AES256-SHA",
4770 "AES128-SHA:DHE-RSA-AES128-SHA",
4776 * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
4779 #if !defined(OPENSSL_NO_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
4780 && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4783 "AES128-SHA:AES256-SHA",
4785 "AES256-SHA:AES128-SHA256",
4787 "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
4788 "TLS_AES_128_GCM_SHA256:AES256-SHA"
4791 #ifndef OPENSSL_NO_TLS1_3
4795 "TLS_AES_256_GCM_SHA384",
4797 "TLS_AES_256_GCM_SHA384",
4798 "TLS_AES_256_GCM_SHA384"
4803 static int test_ssl_get_shared_ciphers(int tst)
4805 SSL_CTX *cctx = NULL, *sctx = NULL;
4806 SSL *clientssl = NULL, *serverssl = NULL;
4810 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
4811 TLS_client_method(),
4813 shared_ciphers_data[tst].maxprot,
4814 &sctx, &cctx, cert, privkey)))
4817 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
4818 shared_ciphers_data[tst].clntciphers))
4819 || (shared_ciphers_data[tst].clnttls13ciphers != NULL
4820 && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
4821 shared_ciphers_data[tst].clnttls13ciphers)))
4822 || !TEST_true(SSL_CTX_set_cipher_list(sctx,
4823 shared_ciphers_data[tst].srvrciphers))
4824 || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
4825 && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4826 shared_ciphers_data[tst].srvrtls13ciphers))))
4830 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4832 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4836 if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
4837 || !TEST_int_eq(strcmp(buf, shared_ciphers_data[tst].shared), 0)) {
4838 TEST_info("Shared ciphers are: %s\n", buf);
4845 SSL_free(serverssl);
4846 SSL_free(clientssl);
4853 static const char *appdata = "Hello World";
4854 static int gen_tick_called, dec_tick_called, tick_key_cb_called;
4855 static int tick_key_renew = 0;
4856 static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
4858 static int gen_tick_cb(SSL *s, void *arg)
4860 gen_tick_called = 1;
4862 return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
4866 static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
4867 const unsigned char *keyname,
4868 size_t keyname_length,
4869 SSL_TICKET_STATUS status,
4875 dec_tick_called = 1;
4877 if (status == SSL_TICKET_EMPTY)
4878 return SSL_TICKET_RETURN_IGNORE_RENEW;
4880 if (!TEST_true(status == SSL_TICKET_SUCCESS
4881 || status == SSL_TICKET_SUCCESS_RENEW))
4882 return SSL_TICKET_RETURN_ABORT;
4884 if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
4886 || !TEST_size_t_eq(tickdlen, strlen(appdata))
4887 || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
4888 return SSL_TICKET_RETURN_ABORT;
4890 if (tick_key_cb_called) {
4891 /* Don't change what the ticket key callback wanted to do */
4893 case SSL_TICKET_NO_DECRYPT:
4894 return SSL_TICKET_RETURN_IGNORE_RENEW;
4896 case SSL_TICKET_SUCCESS:
4897 return SSL_TICKET_RETURN_USE;
4899 case SSL_TICKET_SUCCESS_RENEW:
4900 return SSL_TICKET_RETURN_USE_RENEW;
4903 return SSL_TICKET_RETURN_ABORT;
4906 return tick_dec_ret;
4910 static int tick_key_cb(SSL *s, unsigned char key_name[16],
4911 unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
4912 HMAC_CTX *hctx, int enc)
4914 const unsigned char tick_aes_key[16] = "0123456789abcdef";
4915 const unsigned char tick_hmac_key[16] = "0123456789abcdef";
4917 tick_key_cb_called = 1;
4918 memset(iv, 0, AES_BLOCK_SIZE);
4919 memset(key_name, 0, 16);
4920 if (!EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, tick_aes_key, iv, enc)
4921 || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key),
4922 EVP_sha256(), NULL))
4925 return tick_key_renew ? 2 : 1;
4929 * Test the various ticket callbacks
4930 * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
4931 * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
4932 * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
4933 * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
4934 * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
4935 * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
4936 * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
4937 * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
4938 * Test 8: TLSv1.2, ticket key callback, ticket, no renewal
4939 * Test 9: TLSv1.3, ticket key callback, ticket, no renewal
4940 * Test 10: TLSv1.2, ticket key callback, ticket, renewal
4941 * Test 11: TLSv1.3, ticket key callback, ticket, renewal
4943 static int test_ticket_callbacks(int tst)
4945 SSL_CTX *cctx = NULL, *sctx = NULL;
4946 SSL *clientssl = NULL, *serverssl = NULL;
4947 SSL_SESSION *clntsess = NULL;
4950 #ifdef OPENSSL_NO_TLS1_2
4954 #ifdef OPENSSL_NO_TLS1_3
4959 gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
4961 /* Which tests the ticket key callback should request renewal for */
4962 if (tst == 10 || tst == 11)
4967 /* Which tests the decrypt ticket callback should request renewal for */
4971 tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
4976 tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
4981 tick_dec_ret = SSL_TICKET_RETURN_USE;
4986 tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
4990 tick_dec_ret = SSL_TICKET_RETURN_ABORT;
4993 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
4994 TLS_client_method(),
4996 ((tst % 2) == 0) ? TLS1_2_VERSION
4998 &sctx, &cctx, cert, privkey)))
5002 * We only want sessions to resume from tickets - not the session cache. So
5003 * switch the cache off.
5005 if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
5008 if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
5013 && !TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
5016 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5018 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5023 * The decrypt ticket key callback in TLSv1.2 should be called even though
5024 * we have no ticket yet, because it gets called with a status of
5025 * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
5026 * actually send any ticket data). This does not happen in TLSv1.3 because
5027 * it is not valid to send empty ticket data in TLSv1.3.
5029 if (!TEST_int_eq(gen_tick_called, 1)
5030 || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
5033 gen_tick_called = dec_tick_called = 0;
5035 clntsess = SSL_get1_session(clientssl);
5036 SSL_shutdown(clientssl);
5037 SSL_shutdown(serverssl);
5038 SSL_free(serverssl);
5039 SSL_free(clientssl);
5040 serverssl = clientssl = NULL;
5042 /* Now do a resumption */
5043 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
5045 || !TEST_true(SSL_set_session(clientssl, clntsess))
5046 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5050 if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
5051 || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW) {
5052 if (!TEST_false(SSL_session_reused(clientssl)))
5055 if (!TEST_true(SSL_session_reused(clientssl)))
5059 if (!TEST_int_eq(gen_tick_called,
5061 || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
5062 || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
5064 || !TEST_int_eq(dec_tick_called, 1))
5070 SSL_SESSION_free(clntsess);
5071 SSL_free(serverssl);
5072 SSL_free(clientssl);
5080 * Test bi-directional shutdown.
5082 * Test 1: TLSv1.2, server continues to read/write after client shutdown
5083 * Test 2: TLSv1.3, no pending NewSessionTicket messages
5084 * Test 3: TLSv1.3, pending NewSessionTicket messages
5085 * Test 4: TLSv1.3, server continues to read/write after client shutdown, client
5087 * Test 5: TLSv1.3, server continues to read/write after client shutdown, client
5090 static int test_shutdown(int tst)
5092 SSL_CTX *cctx = NULL, *sctx = NULL;
5093 SSL *clientssl = NULL, *serverssl = NULL;
5095 char msg[] = "A test message";
5097 size_t written, readbytes;
5099 #ifdef OPENSSL_NO_TLS1_2
5103 #ifdef OPENSSL_NO_TLS1_3
5108 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
5109 TLS_client_method(),
5111 (tst <= 1) ? TLS1_2_VERSION
5113 &sctx, &cctx, cert, privkey))
5114 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5119 if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
5122 } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
5127 if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
5132 * Reading on the server after the client has sent close_notify should
5133 * fail and provide SSL_ERROR_ZERO_RETURN
5135 if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
5136 || !TEST_int_eq(SSL_get_error(serverssl, 0),
5137 SSL_ERROR_ZERO_RETURN)
5138 || !TEST_int_eq(SSL_get_shutdown(serverssl),
5139 SSL_RECEIVED_SHUTDOWN)
5141 * Even though we're shutdown on receive we should still be
5144 || !TEST_true(SSL_write(serverssl, msg, sizeof(msg)))
5145 || !TEST_int_eq(SSL_shutdown(serverssl), 1))
5148 /* Should still be able to read data from server */
5149 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
5151 || !TEST_size_t_eq(readbytes, sizeof(msg))
5152 || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
5157 /* Writing on the client after sending close_notify shouldn't be possible */
5158 if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
5163 * For these tests the client has sent close_notify but it has not yet
5164 * been received by the server. The server has not sent close_notify
5167 if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
5169 * Writing on the server after sending close_notify shouldn't
5172 || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
5173 || !TEST_int_eq(SSL_shutdown(clientssl), 1)
5174 || !TEST_int_eq(SSL_shutdown(serverssl), 1))
5176 } else if (tst == 4) {
5178 * In this test the client has sent close_notify and it has been
5179 * received by the server which has responded with a close_notify. The
5180 * client needs to read the close_notify sent by the server.
5182 if (!TEST_int_eq(SSL_shutdown(clientssl), 1))
5188 * The client has sent close_notify and is expecting a close_notify
5189 * back, but instead there is application data first. The shutdown
5190 * should fail with a fatal error.
5192 if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
5193 || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
5200 SSL_free(serverssl);
5201 SSL_free(clientssl);
5208 int setup_tests(void)
5210 if (!TEST_ptr(cert = test_get_argument(0))
5211 || !TEST_ptr(privkey = test_get_argument(1))
5212 || !TEST_ptr(srpvfile = test_get_argument(2))
5213 || !TEST_ptr(tmpfilename = test_get_argument(3)))
5216 if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
5217 #ifdef OPENSSL_NO_CRYPTO_MDEBUG
5218 TEST_error("not supported in this build");
5221 int i, mcount, rcount, fcount;
5223 for (i = 0; i < 4; i++)
5224 test_export_key_mat(i);
5225 CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
5226 test_printf_stdout("malloc %d realloc %d free %d\n",
5227 mcount, rcount, fcount);
5232 ADD_TEST(test_large_message_tls);
5233 ADD_TEST(test_large_message_tls_read_ahead);
5234 #ifndef OPENSSL_NO_DTLS
5235 ADD_TEST(test_large_message_dtls);
5237 #ifndef OPENSSL_NO_OCSP
5238 ADD_TEST(test_tlsext_status_type);
5240 ADD_TEST(test_session_with_only_int_cache);
5241 ADD_TEST(test_session_with_only_ext_cache);
5242 ADD_TEST(test_session_with_both_cache);
5243 #ifndef OPENSSL_NO_TLS1_3
5244 ADD_ALL_TESTS(test_tickets, 3);
5246 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
5247 ADD_TEST(test_ssl_bio_pop_next_bio);
5248 ADD_TEST(test_ssl_bio_pop_ssl_bio);
5249 ADD_TEST(test_ssl_bio_change_rbio);
5250 ADD_TEST(test_ssl_bio_change_wbio);
5251 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
5252 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
5253 ADD_TEST(test_keylog);
5255 #ifndef OPENSSL_NO_TLS1_3
5256 ADD_TEST(test_keylog_no_master_key);
5258 #ifndef OPENSSL_NO_TLS1_2
5259 ADD_TEST(test_client_hello_cb);
5261 #ifndef OPENSSL_NO_TLS1_3
5262 ADD_ALL_TESTS(test_early_data_read_write, 3);
5264 * We don't do replay tests for external PSK. Replay protection isn't used
5267 ADD_ALL_TESTS(test_early_data_replay, 2);
5268 ADD_ALL_TESTS(test_early_data_skip, 3);
5269 ADD_ALL_TESTS(test_early_data_skip_hrr, 3);
5270 ADD_ALL_TESTS(test_early_data_not_sent, 3);
5271 ADD_ALL_TESTS(test_early_data_psk, 8);
5272 ADD_ALL_TESTS(test_early_data_not_expected, 3);
5273 # ifndef OPENSSL_NO_TLS1_2
5274 ADD_ALL_TESTS(test_early_data_tls1_2, 3);
5277 #ifndef OPENSSL_NO_TLS1_3
5278 ADD_ALL_TESTS(test_set_ciphersuite, 10);
5279 ADD_TEST(test_ciphersuite_change);
5280 #ifdef OPENSSL_NO_PSK
5281 ADD_ALL_TESTS(test_tls13_psk, 1);
5283 ADD_ALL_TESTS(test_tls13_psk, 4);
5284 #endif /* OPENSSL_NO_PSK */
5285 ADD_ALL_TESTS(test_custom_exts, 5);
5286 ADD_TEST(test_stateless);
5287 ADD_TEST(test_pha_key_update);
5289 ADD_ALL_TESTS(test_custom_exts, 3);
5291 ADD_ALL_TESTS(test_serverinfo, 8);
5292 ADD_ALL_TESTS(test_export_key_mat, 4);
5293 #ifndef OPENSSL_NO_TLS1_3
5294 ADD_ALL_TESTS(test_export_key_mat_early, 3);
5296 ADD_ALL_TESTS(test_ssl_clear, 2);
5297 ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
5298 #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
5299 ADD_ALL_TESTS(test_srp, 6);
5301 ADD_ALL_TESTS(test_info_callback, 6);
5302 ADD_ALL_TESTS(test_ssl_pending, 2);
5303 ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
5304 ADD_ALL_TESTS(test_ticket_callbacks, 12);
5305 ADD_ALL_TESTS(test_shutdown, 6);
5309 void cleanup_tests(void)
5311 bio_s_mempacket_test_free();