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>
18 #include "ssltestlib.h"
20 #include "testutil/output.h"
21 #include "internal/nelem.h"
22 #include "../ssl/ssl_locl.h"
24 static char *cert = NULL;
25 static char *privkey = NULL;
27 #define LOG_BUFFER_SIZE 1024
28 static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
29 static size_t server_log_buffer_index = 0;
30 static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
31 static size_t client_log_buffer_index = 0;
32 static int error_writing_log = 0;
34 #ifndef OPENSSL_NO_OCSP
35 static const unsigned char orespder[] = "Dummy OCSP Response";
36 static int ocsp_server_called = 0;
37 static int ocsp_client_called = 0;
39 static int cdummyarg = 1;
40 static X509 *ocspcert = NULL;
43 #define NUM_EXTRA_CERTS 40
44 #define CLIENT_VERSION_LEN 2
47 * This structure is used to validate that the correct number of log messages
48 * of various types are emitted when emitting secret logs.
50 struct sslapitest_log_counts {
51 unsigned int rsa_key_exchange_count;
52 unsigned int master_secret_count;
53 unsigned int client_handshake_secret_count;
54 unsigned int server_handshake_secret_count;
55 unsigned int client_application_secret_count;
56 unsigned int server_application_secret_count;
60 static unsigned char serverinfov1[] = {
61 0xff, 0xff, /* Dummy extension type */
62 0x00, 0x01, /* Extension length is 1 byte */
63 0xff /* Dummy extension data */
66 static unsigned char serverinfov2[] = {
68 (unsigned char)(SSL_EXT_CLIENT_HELLO & 0xff), /* Dummy context - 4 bytes */
69 0xff, 0xff, /* Dummy extension type */
70 0x00, 0x01, /* Extension length is 1 byte */
71 0xff /* Dummy extension data */
74 static void client_keylog_callback(const SSL *ssl, const char *line)
76 int line_length = strlen(line);
78 /* If the log doesn't fit, error out. */
79 if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
80 TEST_info("Client log too full");
81 error_writing_log = 1;
85 strcat(client_log_buffer, line);
86 client_log_buffer_index += line_length;
87 client_log_buffer[client_log_buffer_index++] = '\n';
90 static void server_keylog_callback(const SSL *ssl, const char *line)
92 int line_length = strlen(line);
94 /* If the log doesn't fit, error out. */
95 if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
96 TEST_info("Server log too full");
97 error_writing_log = 1;
101 strcat(server_log_buffer, line);
102 server_log_buffer_index += line_length;
103 server_log_buffer[server_log_buffer_index++] = '\n';
106 static int compare_hex_encoded_buffer(const char *hex_encoded,
114 if (!TEST_size_t_eq(raw_length * 2, hex_length))
117 for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
118 sprintf(hexed, "%02x", raw[i]);
119 if (!TEST_int_eq(hexed[0], hex_encoded[j])
120 || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
127 static int test_keylog_output(char *buffer, const SSL *ssl,
128 const SSL_SESSION *session,
129 struct sslapitest_log_counts *expected)
132 unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
133 size_t client_random_size = SSL3_RANDOM_SIZE;
134 unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
135 size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
136 unsigned int rsa_key_exchange_count = 0;
137 unsigned int master_secret_count = 0;
138 unsigned int client_handshake_secret_count = 0;
139 unsigned int server_handshake_secret_count = 0;
140 unsigned int client_application_secret_count = 0;
141 unsigned int server_application_secret_count = 0;
143 for (token = strtok(buffer, " \n"); token != NULL;
144 token = strtok(NULL, " \n")) {
145 if (strcmp(token, "RSA") == 0) {
147 * Premaster secret. Tokens should be: 16 ASCII bytes of
148 * hex-encoded encrypted secret, then the hex-encoded pre-master
151 if (!TEST_ptr(token = strtok(NULL, " \n")))
153 if (!TEST_size_t_eq(strlen(token), 16))
155 if (!TEST_ptr(token = strtok(NULL, " \n")))
158 * We can't sensibly check the log because the premaster secret is
159 * transient, and OpenSSL doesn't keep hold of it once the master
160 * secret is generated.
162 rsa_key_exchange_count++;
163 } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
165 * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
166 * client random, then the hex-encoded master secret.
168 client_random_size = SSL_get_client_random(ssl,
169 actual_client_random,
171 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
174 if (!TEST_ptr(token = strtok(NULL, " \n")))
176 if (!TEST_size_t_eq(strlen(token), 64))
178 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
179 actual_client_random,
180 client_random_size)))
183 if (!TEST_ptr(token = strtok(NULL, " \n")))
185 master_key_size = SSL_SESSION_get_master_key(session,
188 if (!TEST_size_t_ne(master_key_size, 0))
190 if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
194 master_secret_count++;
195 } else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
196 || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
197 || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
198 || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0) {
200 * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
201 * client random, and then the hex-encoded secret. In this case,
202 * we treat all of these secrets identically and then just
203 * distinguish between them when counting what we saw.
205 if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
206 client_handshake_secret_count++;
207 else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
208 server_handshake_secret_count++;
209 else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
210 client_application_secret_count++;
211 else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
212 server_application_secret_count++;
214 client_random_size = SSL_get_client_random(ssl,
215 actual_client_random,
217 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
220 if (!TEST_ptr(token = strtok(NULL, " \n")))
222 if (!TEST_size_t_eq(strlen(token), 64))
224 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
225 actual_client_random,
226 client_random_size)))
229 if (!TEST_ptr(token = strtok(NULL, " \n")))
233 * TODO(TLS1.3): test that application traffic secrets are what
236 TEST_info("Unexpected token %s\n", token);
241 /* Got what we expected? */
242 if (!TEST_size_t_eq(rsa_key_exchange_count,
243 expected->rsa_key_exchange_count)
244 || !TEST_size_t_eq(master_secret_count,
245 expected->master_secret_count)
246 || !TEST_size_t_eq(client_handshake_secret_count,
247 expected->client_handshake_secret_count)
248 || !TEST_size_t_eq(server_handshake_secret_count,
249 expected->server_handshake_secret_count)
250 || !TEST_size_t_eq(client_application_secret_count,
251 expected->client_application_secret_count)
252 || !TEST_size_t_eq(server_application_secret_count,
253 expected->server_application_secret_count))
258 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
259 static int test_keylog(void)
261 SSL_CTX *cctx = NULL, *sctx = NULL;
262 SSL *clientssl = NULL, *serverssl = NULL;
264 struct sslapitest_log_counts expected = {0};
266 /* Clean up logging space */
267 memset(client_log_buffer, 0, sizeof(client_log_buffer));
268 memset(server_log_buffer, 0, sizeof(server_log_buffer));
269 client_log_buffer_index = 0;
270 server_log_buffer_index = 0;
271 error_writing_log = 0;
273 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
275 &sctx, &cctx, cert, privkey)))
278 /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
279 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
280 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
282 /* We also want to ensure that we use RSA-based key exchange. */
283 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
286 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
287 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
289 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
290 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
291 == client_keylog_callback))
293 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
294 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
295 == server_keylog_callback))
298 /* Now do a handshake and check that the logs have been written to. */
299 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
300 &clientssl, NULL, NULL))
301 || !TEST_true(create_ssl_connection(serverssl, clientssl,
303 || !TEST_false(error_writing_log)
304 || !TEST_int_gt(client_log_buffer_index, 0)
305 || !TEST_int_gt(server_log_buffer_index, 0))
309 * Now we want to test that our output data was vaguely sensible. We
310 * do that by using strtok and confirming that we have more or less the
311 * data we expect. For both client and server, we expect to see one master
312 * secret. The client should also see a RSA key exchange.
314 expected.rsa_key_exchange_count = 1;
315 expected.master_secret_count = 1;
316 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
317 SSL_get_session(clientssl), &expected)))
320 expected.rsa_key_exchange_count = 0;
321 if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
322 SSL_get_session(serverssl), &expected)))
337 #ifndef OPENSSL_NO_TLS1_3
338 static int test_keylog_no_master_key(void)
340 SSL_CTX *cctx = NULL, *sctx = NULL;
341 SSL *clientssl = NULL, *serverssl = NULL;
343 struct sslapitest_log_counts expected = {0};
345 /* Clean up logging space */
346 memset(client_log_buffer, 0, sizeof(client_log_buffer));
347 memset(server_log_buffer, 0, sizeof(server_log_buffer));
348 client_log_buffer_index = 0;
349 server_log_buffer_index = 0;
350 error_writing_log = 0;
352 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
353 TLS_client_method(), &sctx,
354 &cctx, cert, privkey)))
357 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
358 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
361 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
362 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
363 == client_keylog_callback))
366 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
367 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
368 == server_keylog_callback))
371 /* Now do a handshake and check that the logs have been written to. */
372 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
373 &clientssl, NULL, NULL))
374 || !TEST_true(create_ssl_connection(serverssl, clientssl,
376 || !TEST_false(error_writing_log))
380 * Now we want to test that our output data was vaguely sensible. For this
381 * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
382 * TLSv1.3, but we do expect both client and server to emit keys.
384 expected.client_handshake_secret_count = 1;
385 expected.server_handshake_secret_count = 1;
386 expected.client_application_secret_count = 1;
387 expected.server_application_secret_count = 1;
388 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
389 SSL_get_session(clientssl), &expected))
390 || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
391 SSL_get_session(serverssl),
407 #ifndef OPENSSL_NO_TLS1_2
408 static int full_client_hello_callback(SSL *s, int *al, void *arg)
411 const unsigned char *p;
413 /* We only configure two ciphers, but the SCSV is added automatically. */
415 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
417 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
420 const int expected_extensions[] = {
421 #ifndef OPENSSL_NO_EC
427 /* Make sure we can defer processing and get called back. */
429 return SSL_CLIENT_HELLO_RETRY;
431 len = SSL_client_hello_get0_ciphers(s, &p);
432 if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
434 SSL_client_hello_get0_compression_methods(s, &p), 1)
435 || !TEST_int_eq(*p, 0))
436 return SSL_CLIENT_HELLO_ERROR;
437 if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
438 return SSL_CLIENT_HELLO_ERROR;
439 if (len != OSSL_NELEM(expected_extensions) ||
440 memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
441 printf("ClientHello callback expected extensions mismatch\n");
443 return SSL_CLIENT_HELLO_ERROR;
446 return SSL_CLIENT_HELLO_SUCCESS;
449 static int test_client_hello_cb(void)
451 SSL_CTX *cctx = NULL, *sctx = NULL;
452 SSL *clientssl = NULL, *serverssl = NULL;
453 int testctr = 0, testresult = 0;
455 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
456 TLS_client_method(), &sctx,
457 &cctx, cert, privkey)))
459 SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
461 /* The gimpy cipher list we configure can't do TLS 1.3. */
462 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
464 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
465 "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
466 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
467 &clientssl, NULL, NULL))
468 || !TEST_false(create_ssl_connection(serverssl, clientssl,
469 SSL_ERROR_WANT_CLIENT_HELLO_CB))
471 * Passing a -1 literal is a hack since
472 * the real value was lost.
474 || !TEST_int_eq(SSL_get_error(serverssl, -1),
475 SSL_ERROR_WANT_CLIENT_HELLO_CB)
476 || !TEST_true(create_ssl_connection(serverssl, clientssl,
492 static int execute_test_large_message(const SSL_METHOD *smeth,
493 const SSL_METHOD *cmeth, int read_ahead)
495 SSL_CTX *cctx = NULL, *sctx = NULL;
496 SSL *clientssl = NULL, *serverssl = NULL;
500 X509 *chaincert = NULL;
503 if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
505 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
508 if (!TEST_ptr(chaincert))
511 if (!TEST_true(create_ssl_ctx_pair(smeth, cmeth, &sctx,
512 &cctx, cert, privkey)))
517 * Test that read_ahead works correctly when dealing with large
520 SSL_CTX_set_read_ahead(cctx, 1);
524 * We assume the supplied certificate is big enough so that if we add
525 * NUM_EXTRA_CERTS it will make the overall message large enough. The
526 * default buffer size is requested to be 16k, but due to the way BUF_MEM
527 * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
528 * test we need to have a message larger than that.
530 certlen = i2d_X509(chaincert, NULL);
531 OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
532 (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
533 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
534 if (!X509_up_ref(chaincert))
536 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
537 X509_free(chaincert);
542 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
544 || !TEST_true(create_ssl_connection(serverssl, clientssl,
549 * Calling SSL_clear() first is not required but this tests that SSL_clear()
550 * doesn't leak (when using enable-crypto-mdebug).
552 if (!TEST_true(SSL_clear(serverssl)))
557 X509_free(chaincert);
566 static int test_large_message_tls(void)
568 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
572 static int test_large_message_tls_read_ahead(void)
574 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
578 #ifndef OPENSSL_NO_DTLS
579 static int test_large_message_dtls(void)
582 * read_ahead is not relevant to DTLS because DTLS always acts as if
585 return execute_test_large_message(DTLS_server_method(),
586 DTLS_client_method(), 0);
590 #ifndef OPENSSL_NO_OCSP
591 static int ocsp_server_cb(SSL *s, void *arg)
593 int *argi = (int *)arg;
594 unsigned char *copy = NULL;
595 STACK_OF(OCSP_RESPID) *ids = NULL;
596 OCSP_RESPID *id = NULL;
599 /* In this test we are expecting exactly 1 OCSP_RESPID */
600 SSL_get_tlsext_status_ids(s, &ids);
601 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
602 return SSL_TLSEXT_ERR_ALERT_FATAL;
604 id = sk_OCSP_RESPID_value(ids, 0);
605 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
606 return SSL_TLSEXT_ERR_ALERT_FATAL;
607 } else if (*argi != 1) {
608 return SSL_TLSEXT_ERR_ALERT_FATAL;
611 if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
612 return SSL_TLSEXT_ERR_ALERT_FATAL;
614 SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder));
615 ocsp_server_called = 1;
616 return SSL_TLSEXT_ERR_OK;
619 static int ocsp_client_cb(SSL *s, void *arg)
621 int *argi = (int *)arg;
622 const unsigned char *respderin;
625 if (*argi != 1 && *argi != 2)
628 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
629 if (!TEST_mem_eq(orespder, len, respderin, len))
632 ocsp_client_called = 1;
636 static int test_tlsext_status_type(void)
638 SSL_CTX *cctx = NULL, *sctx = NULL;
639 SSL *clientssl = NULL, *serverssl = NULL;
641 STACK_OF(OCSP_RESPID) *ids = NULL;
642 OCSP_RESPID *id = NULL;
645 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
646 &cctx, cert, privkey))
649 if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
652 /* First just do various checks getting and setting tlsext_status_type */
654 clientssl = SSL_new(cctx);
655 if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
656 || !TEST_true(SSL_set_tlsext_status_type(clientssl,
657 TLSEXT_STATUSTYPE_ocsp))
658 || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
659 TLSEXT_STATUSTYPE_ocsp))
665 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
666 || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
669 clientssl = SSL_new(cctx);
670 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
676 * Now actually do a handshake and check OCSP information is exchanged and
677 * the callbacks get called
679 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
680 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
681 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
682 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
683 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
684 &clientssl, NULL, NULL))
685 || !TEST_true(create_ssl_connection(serverssl, clientssl,
687 || !TEST_true(ocsp_client_called)
688 || !TEST_true(ocsp_server_called))
695 /* Try again but this time force the server side callback to fail */
696 ocsp_client_called = 0;
697 ocsp_server_called = 0;
699 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
700 &clientssl, NULL, NULL))
701 /* This should fail because the callback will fail */
702 || !TEST_false(create_ssl_connection(serverssl, clientssl,
704 || !TEST_false(ocsp_client_called)
705 || !TEST_false(ocsp_server_called))
713 * This time we'll get the client to send an OCSP_RESPID that it will
716 ocsp_client_called = 0;
717 ocsp_server_called = 0;
719 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
720 &clientssl, NULL, NULL)))
724 * We'll just use any old cert for this test - it doesn't have to be an OCSP
725 * specific one. We'll use the server cert.
727 if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
728 || !TEST_ptr(id = OCSP_RESPID_new())
729 || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
730 || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio,
732 || !TEST_true(OCSP_RESPID_set_by_key(id, ocspcert))
733 || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
736 SSL_set_tlsext_status_ids(clientssl, ids);
737 /* Control has been transferred */
743 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
745 || !TEST_true(ocsp_client_called)
746 || !TEST_true(ocsp_server_called))
756 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
757 OCSP_RESPID_free(id);
766 #if !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
767 static int new_called, remove_called, get_called;
769 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
773 * sess has been up-refed for us, but we don't actually need it so free it
776 SSL_SESSION_free(sess);
780 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
785 static SSL_SESSION *get_sess_val = NULL;
787 static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
795 static int execute_test_session(int maxprot, int use_int_cache,
798 SSL_CTX *sctx = NULL, *cctx = NULL;
799 SSL *serverssl1 = NULL, *clientssl1 = NULL;
800 SSL *serverssl2 = NULL, *clientssl2 = NULL;
801 # ifndef OPENSSL_NO_TLS1_1
802 SSL *serverssl3 = NULL, *clientssl3 = NULL;
804 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
807 new_called = remove_called = 0;
809 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
810 TLS_client_method(), &sctx,
811 &cctx, cert, privkey)))
815 * Only allow the max protocol version so we can force a connection failure
818 SSL_CTX_set_min_proto_version(cctx, maxprot);
819 SSL_CTX_set_max_proto_version(cctx, maxprot);
821 /* Set up session cache */
823 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
824 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
827 /* Also covers instance where both are set */
828 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
830 SSL_CTX_set_session_cache_mode(cctx,
831 SSL_SESS_CACHE_CLIENT
832 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
835 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
837 || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
839 || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
842 /* Should fail because it should already be in the cache */
843 if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
846 && (!TEST_int_eq(new_called, 1) || !TEST_int_eq(remove_called, 0)))
849 new_called = remove_called = 0;
850 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
851 &clientssl2, NULL, NULL))
852 || !TEST_true(SSL_set_session(clientssl2, sess1))
853 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
855 || !TEST_true(SSL_session_reused(clientssl2)))
858 if (maxprot == TLS1_3_VERSION) {
860 * In TLSv1.3 we should have created a new session even though we have
861 * resumed. The original session should also have been removed.
864 && (!TEST_int_eq(new_called, 1)
865 || !TEST_int_eq(remove_called, 1)))
869 * In TLSv1.2 we expect to have resumed so no sessions added or
873 && (!TEST_int_eq(new_called, 0)
874 || !TEST_int_eq(remove_called, 0)))
878 SSL_SESSION_free(sess1);
879 if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
881 shutdown_ssl_connection(serverssl2, clientssl2);
882 serverssl2 = clientssl2 = NULL;
884 new_called = remove_called = 0;
885 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
886 &clientssl2, NULL, NULL))
887 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
891 if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
895 && (!TEST_int_eq(new_called, 1) || !TEST_int_eq(remove_called, 0)))
898 new_called = remove_called = 0;
900 * This should clear sess2 from the cache because it is a "bad" session.
901 * See SSL_set_session() documentation.
903 if (!TEST_true(SSL_set_session(clientssl2, sess1)))
906 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
908 if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
912 /* Should succeeded because it should not already be in the cache */
913 if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
914 || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
918 new_called = remove_called = 0;
919 /* This shouldn't be in the cache so should fail */
920 if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
924 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
927 # if !defined(OPENSSL_NO_TLS1_1)
928 new_called = remove_called = 0;
929 /* Force a connection failure */
930 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
931 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
932 &clientssl3, NULL, NULL))
933 || !TEST_true(SSL_set_session(clientssl3, sess1))
934 /* This should fail because of the mismatched protocol versions */
935 || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
939 /* We should have automatically removed the session from the cache */
941 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
944 /* Should succeed because it should not already be in the cache */
945 if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
949 /* Now do some tests for server side caching */
951 SSL_CTX_sess_set_new_cb(cctx, NULL);
952 SSL_CTX_sess_set_remove_cb(cctx, NULL);
953 SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
954 SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
955 SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
959 SSL_CTX_set_session_cache_mode(cctx, 0);
960 /* Internal caching is the default on the server side */
962 SSL_CTX_set_session_cache_mode(sctx,
963 SSL_SESS_CACHE_SERVER
964 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
966 SSL_free(serverssl1);
967 SSL_free(clientssl1);
968 serverssl1 = clientssl1 = NULL;
969 SSL_free(serverssl2);
970 SSL_free(clientssl2);
971 serverssl2 = clientssl2 = NULL;
972 SSL_SESSION_free(sess1);
974 SSL_SESSION_free(sess2);
977 SSL_CTX_set_max_proto_version(sctx, maxprot);
978 SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
979 new_called = remove_called = get_called = 0;
980 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
982 || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
984 || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
985 || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
988 /* Should fail because it should already be in the cache */
989 if (use_int_cache && !TEST_false(SSL_CTX_add_session(sctx, sess2)))
993 SSL_SESSION *tmp = sess2;
995 if (!TEST_int_eq(new_called, 1)
996 || !TEST_int_eq(remove_called, 0)
997 || !TEST_int_eq(get_called, 0))
1000 * Delete the session from the internal cache to force a lookup from
1001 * the external cache. We take a copy first because
1002 * SSL_CTX_remove_session() also marks the session as non-resumable.
1004 if (use_int_cache) {
1005 if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
1006 || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
1008 SSL_SESSION_free(sess2);
1013 new_called = remove_called = get_called = 0;
1014 get_sess_val = sess2;
1015 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
1016 &clientssl2, NULL, NULL))
1017 || !TEST_true(SSL_set_session(clientssl2, sess1))
1018 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
1020 || !TEST_true(SSL_session_reused(clientssl2)))
1023 if (use_ext_cache) {
1024 if (!TEST_int_eq(remove_called, 0))
1027 if (maxprot == TLS1_3_VERSION) {
1029 * Every time we issue a NewSessionTicket we are creating a new
1030 * session for next time in TLSv1.3
1032 if (!TEST_int_eq(new_called, 1)
1033 || !TEST_int_eq(get_called, 0))
1036 if (!TEST_int_eq(new_called, 0)
1037 || !TEST_int_eq(get_called, 1))
1045 SSL_free(serverssl1);
1046 SSL_free(clientssl1);
1047 SSL_free(serverssl2);
1048 SSL_free(clientssl2);
1049 # ifndef OPENSSL_NO_TLS1_1
1050 SSL_free(serverssl3);
1051 SSL_free(clientssl3);
1053 SSL_SESSION_free(sess1);
1054 SSL_SESSION_free(sess2);
1060 #endif /* !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
1062 static int test_session_with_only_int_cache(void)
1064 #ifndef OPENSSL_NO_TLS1_3
1065 if (!execute_test_session(TLS1_3_VERSION, 1, 0))
1069 #ifndef OPENSSL_NO_TLS1_2
1070 return execute_test_session(TLS1_2_VERSION, 1, 0);
1076 static int test_session_with_only_ext_cache(void)
1078 #ifndef OPENSSL_NO_TLS1_3
1079 if (!execute_test_session(TLS1_3_VERSION, 0, 1))
1083 #ifndef OPENSSL_NO_TLS1_2
1084 return execute_test_session(TLS1_2_VERSION, 0, 1);
1090 static int test_session_with_both_cache(void)
1092 #ifndef OPENSSL_NO_TLS1_3
1093 if (!execute_test_session(TLS1_3_VERSION, 1, 1))
1097 #ifndef OPENSSL_NO_TLS1_2
1098 return execute_test_session(TLS1_2_VERSION, 1, 1);
1108 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
1110 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
1125 static int test_ssl_set_bio(int idx)
1130 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
1132 int initrbio, initwbio, newrbio, newwbio;
1142 if (!TEST_int_le(newwbio, 2))
1145 if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))
1146 || !TEST_ptr(ssl = SSL_new(ctx)))
1149 if (initrbio == USE_BIO_1
1150 || initwbio == USE_BIO_1
1151 || newrbio == USE_BIO_1
1152 || newwbio == USE_BIO_1) {
1153 if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
1157 if (initrbio == USE_BIO_2
1158 || initwbio == USE_BIO_2
1159 || newrbio == USE_BIO_2
1160 || newwbio == USE_BIO_2) {
1161 if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
1165 setupbio(&irbio, bio1, bio2, initrbio);
1166 setupbio(&iwbio, bio1, bio2, initwbio);
1169 * We want to maintain our own refs to these BIO, so do an up ref for each
1170 * BIO that will have ownership transferred in the SSL_set_bio() call
1174 if (iwbio != NULL && iwbio != irbio)
1177 SSL_set_bio(ssl, irbio, iwbio);
1179 setupbio(&nrbio, bio1, bio2, newrbio);
1180 setupbio(&nwbio, bio1, bio2, newwbio);
1183 * We will (maybe) transfer ownership again so do more up refs.
1184 * SSL_set_bio() has some really complicated ownership rules where BIOs have
1189 && (nwbio != iwbio || nrbio != nwbio))
1193 && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
1196 SSL_set_bio(ssl, nrbio, nwbio);
1206 * This test is checking that the ref counting for SSL_set_bio is correct.
1207 * If we get here and we did too many frees then we will fail in the above
1208 * functions. If we haven't done enough then this will only be detected in
1209 * a crypto-mdebug build
1215 typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
1217 static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
1219 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
1224 if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))
1225 || !TEST_ptr(ssl = SSL_new(ctx))
1226 || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
1227 || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
1230 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
1233 * If anything goes wrong here then we could leak memory, so this will
1234 * be caught in a crypto-mdebug build
1236 BIO_push(sslbio, membio1);
1238 /* Verify changing the rbio/wbio directly does not cause leaks */
1239 if (change_bio != NO_BIO_CHANGE) {
1240 if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem())))
1242 if (change_bio == CHANGE_RBIO)
1243 SSL_set0_rbio(ssl, membio2);
1245 SSL_set0_wbio(ssl, membio2);
1264 static int test_ssl_bio_pop_next_bio(void)
1266 return execute_test_ssl_bio(0, NO_BIO_CHANGE);
1269 static int test_ssl_bio_pop_ssl_bio(void)
1271 return execute_test_ssl_bio(1, NO_BIO_CHANGE);
1274 static int test_ssl_bio_change_rbio(void)
1276 return execute_test_ssl_bio(0, CHANGE_RBIO);
1279 static int test_ssl_bio_change_wbio(void)
1281 return execute_test_ssl_bio(0, CHANGE_WBIO);
1284 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
1286 /* The list of sig algs */
1288 /* The length of the list */
1290 /* A sigalgs list in string format */
1291 const char *liststr;
1292 /* Whether setting the list should succeed */
1294 /* Whether creating a connection with the list should succeed */
1298 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
1299 # ifndef OPENSSL_NO_EC
1300 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
1301 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
1303 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
1304 static const int invalidlist2[] = {NID_sha256, NID_undef};
1305 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
1306 static const int invalidlist4[] = {NID_sha256};
1307 static const sigalgs_list testsigalgs[] = {
1308 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
1309 # ifndef OPENSSL_NO_EC
1310 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
1311 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
1313 {NULL, 0, "RSA+SHA256", 1, 1},
1314 # ifndef OPENSSL_NO_EC
1315 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
1316 {NULL, 0, "ECDSA+SHA512", 1, 0},
1318 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
1319 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
1320 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
1321 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
1322 {NULL, 0, "RSA", 0, 0},
1323 {NULL, 0, "SHA256", 0, 0},
1324 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
1325 {NULL, 0, "Invalid", 0, 0}
1328 static int test_set_sigalgs(int idx)
1330 SSL_CTX *cctx = NULL, *sctx = NULL;
1331 SSL *clientssl = NULL, *serverssl = NULL;
1333 const sigalgs_list *curr;
1336 /* Should never happen */
1337 if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
1340 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
1341 curr = testctx ? &testsigalgs[idx]
1342 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
1344 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
1345 TLS_client_method(), &sctx,
1346 &cctx, cert, privkey)))
1350 * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
1351 * for TLSv1.2 for now until we add a new API.
1353 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
1358 if (curr->list != NULL)
1359 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
1361 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
1365 TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
1371 TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
1376 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1377 &clientssl, NULL, NULL)))
1383 if (curr->list != NULL)
1384 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
1386 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
1389 TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
1398 if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
1406 SSL_free(serverssl);
1407 SSL_free(clientssl);
1415 #ifndef OPENSSL_NO_TLS1_3
1417 static SSL_SESSION *clientpsk = NULL;
1418 static SSL_SESSION *serverpsk = NULL;
1419 static const char *pskid = "Identity";
1420 static const char *srvid;
1422 static int use_session_cb_cnt = 0;
1423 static int find_session_cb_cnt = 0;
1424 static int psk_client_cb_cnt = 0;
1425 static int psk_server_cb_cnt = 0;
1427 static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
1428 size_t *idlen, SSL_SESSION **sess)
1430 switch (++use_session_cb_cnt) {
1432 /* The first call should always have a NULL md */
1438 /* The second call should always have an md */
1444 /* We should only be called a maximum of twice */
1448 if (clientpsk != NULL)
1449 SSL_SESSION_up_ref(clientpsk);
1452 *id = (const unsigned char *)pskid;
1453 *idlen = strlen(pskid);
1458 static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
1459 unsigned int max_id_len,
1461 unsigned int max_psk_len)
1463 unsigned int psklen = 0;
1465 psk_client_cb_cnt++;
1467 if (strlen(pskid) + 1 > max_id_len)
1470 /* We should only ever be called a maximum of twice per connection */
1471 if (psk_client_cb_cnt > 2)
1474 if (clientpsk == NULL)
1477 /* We'll reuse the PSK we set up for TLSv1.3 */
1478 if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
1480 psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
1481 strncpy(id, pskid, max_id_len);
1486 static int find_session_cb(SSL *ssl, const unsigned char *identity,
1487 size_t identity_len, SSL_SESSION **sess)
1489 find_session_cb_cnt++;
1491 /* We should only ever be called a maximum of twice per connection */
1492 if (find_session_cb_cnt > 2)
1495 if (serverpsk == NULL)
1498 /* Identity should match that set by the client */
1499 if (strlen(srvid) != identity_len
1500 || strncmp(srvid, (const char *)identity, identity_len) != 0) {
1501 /* No PSK found, continue but without a PSK */
1506 SSL_SESSION_up_ref(serverpsk);
1512 static unsigned int psk_server_cb(SSL *ssl, const char *identity,
1513 unsigned char *psk, unsigned int max_psk_len)
1515 unsigned int psklen = 0;
1517 psk_server_cb_cnt++;
1519 /* We should only ever be called a maximum of twice per connection */
1520 if (find_session_cb_cnt > 2)
1523 if (serverpsk == NULL)
1526 /* Identity should match that set by the client */
1527 if (strcmp(srvid, identity) != 0) {
1531 /* We'll reuse the PSK we set up for TLSv1.3 */
1532 if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
1534 psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
1539 #define MSG1 "Hello"
1540 #define MSG2 "World."
1545 #define MSG7 "message."
1547 #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02")
1548 #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
1551 * Helper method to setup objects for early data test. Caller frees objects on
1554 static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
1555 SSL **serverssl, SSL_SESSION **sess, int idx)
1557 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
1558 TLS_client_method(), sctx,
1559 cctx, cert, privkey))
1560 || !TEST_true(SSL_CTX_set_max_early_data(*sctx,
1561 SSL3_RT_MAX_PLAIN_LENGTH))
1562 || !TEST_true(SSL_CTX_set_max_early_data(*cctx,
1563 SSL3_RT_MAX_PLAIN_LENGTH)))
1567 /* When idx == 1 we repeat the tests with read_ahead set */
1568 SSL_CTX_set_read_ahead(*cctx, 1);
1569 SSL_CTX_set_read_ahead(*sctx, 1);
1570 } else if (idx == 2) {
1571 /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
1572 SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
1573 SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
1574 use_session_cb_cnt = 0;
1575 find_session_cb_cnt = 0;
1579 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
1584 * For one of the run throughs (doesn't matter which one), we'll try sending
1585 * some SNI data in the initial ClientHello. This will be ignored (because
1586 * there is no SNI cb set up by the server), so it should not impact
1590 && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
1594 /* Create the PSK */
1595 const SSL_CIPHER *cipher = NULL;
1596 const unsigned char key[] = {
1597 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
1598 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
1599 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
1600 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
1601 0x2c, 0x2d, 0x2e, 0x2f
1604 cipher = SSL_CIPHER_find(*clientssl, TLS13_AES_256_GCM_SHA384_BYTES);
1605 clientpsk = SSL_SESSION_new();
1606 if (!TEST_ptr(clientpsk)
1607 || !TEST_ptr(cipher)
1608 || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
1610 || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
1612 SSL_SESSION_set_protocol_version(clientpsk,
1615 * We just choose an arbitrary value for max_early_data which
1616 * should be big enough for testing purposes.
1618 || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
1620 || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
1621 SSL_SESSION_free(clientpsk);
1625 serverpsk = clientpsk;
1635 if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
1639 *sess = SSL_get1_session(*clientssl);
1640 SSL_shutdown(*clientssl);
1641 SSL_shutdown(*serverssl);
1642 SSL_free(*serverssl);
1643 SSL_free(*clientssl);
1644 *serverssl = *clientssl = NULL;
1646 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
1647 clientssl, NULL, NULL))
1648 || !TEST_true(SSL_set_session(*clientssl, *sess)))
1654 static int test_early_data_read_write(int idx)
1656 SSL_CTX *cctx = NULL, *sctx = NULL;
1657 SSL *clientssl = NULL, *serverssl = NULL;
1659 SSL_SESSION *sess = NULL;
1660 unsigned char buf[20], data[1024];
1661 size_t readbytes, written, eoedlen, rawread, rawwritten;
1664 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
1665 &serverssl, &sess, idx)))
1668 /* Write and read some early data */
1669 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
1671 || !TEST_size_t_eq(written, strlen(MSG1))
1672 || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
1673 sizeof(buf), &readbytes),
1674 SSL_READ_EARLY_DATA_SUCCESS)
1675 || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
1676 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
1677 SSL_EARLY_DATA_ACCEPTED))
1681 * Server should be able to write data, and client should be able to
1684 if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
1686 || !TEST_size_t_eq(written, strlen(MSG2))
1687 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1688 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
1691 /* Even after reading normal data, client should be able write early data */
1692 if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
1694 || !TEST_size_t_eq(written, strlen(MSG3)))
1697 /* Server should still be able read early data after writing data */
1698 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1700 SSL_READ_EARLY_DATA_SUCCESS)
1701 || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
1704 /* Write more data from server and read it from client */
1705 if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
1707 || !TEST_size_t_eq(written, strlen(MSG4))
1708 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1709 || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
1713 * If client writes normal data it should mean writing early data is no
1716 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
1717 || !TEST_size_t_eq(written, strlen(MSG5))
1718 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
1719 SSL_EARLY_DATA_ACCEPTED))
1723 * At this point the client has written EndOfEarlyData, ClientFinished and
1724 * normal (fully protected) data. We are going to cause a delay between the
1725 * arrival of EndOfEarlyData and ClientFinished. We read out all the data
1726 * in the read BIO, and then just put back the EndOfEarlyData message.
1728 rbio = SSL_get_rbio(serverssl);
1729 if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
1730 || !TEST_size_t_lt(rawread, sizeof(data))
1731 || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
1734 /* Record length is in the 4th and 5th bytes of the record header */
1735 eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
1736 if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
1737 || !TEST_size_t_eq(rawwritten, eoedlen))
1740 /* Server should be told that there is no more early data */
1741 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1743 SSL_READ_EARLY_DATA_FINISH)
1744 || !TEST_size_t_eq(readbytes, 0))
1748 * Server has not finished init yet, so should still be able to write early
1751 if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
1753 || !TEST_size_t_eq(written, strlen(MSG6)))
1756 /* Push the ClientFinished and the normal data back into the server rbio */
1757 if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
1759 || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
1762 /* Server should be able to read normal data */
1763 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
1764 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
1767 /* Client and server should not be able to write/read early data now */
1768 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
1772 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1774 SSL_READ_EARLY_DATA_ERROR))
1778 /* Client should be able to read the data sent by the server */
1779 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1780 || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
1784 * Make sure we process the NewSessionTicket. This arrives post-handshake.
1785 * We attempt a read which we do not expect to return any data.
1787 if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)))
1790 /* Server should be able to write normal data */
1791 if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
1792 || !TEST_size_t_eq(written, strlen(MSG7))
1793 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1794 || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
1797 /* We keep the PSK session around if using PSK */
1799 SSL_SESSION_free(sess);
1800 sess = SSL_get1_session(clientssl);
1801 use_session_cb_cnt = 0;
1802 find_session_cb_cnt = 0;
1804 SSL_shutdown(clientssl);
1805 SSL_shutdown(serverssl);
1806 SSL_free(serverssl);
1807 SSL_free(clientssl);
1808 serverssl = clientssl = NULL;
1809 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1810 &clientssl, NULL, NULL))
1811 || !TEST_true(SSL_set_session(clientssl, sess)))
1814 /* Write and read some early data */
1815 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
1817 || !TEST_size_t_eq(written, strlen(MSG1))
1818 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1820 SSL_READ_EARLY_DATA_SUCCESS)
1821 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
1824 if (!TEST_int_gt(SSL_connect(clientssl), 0)
1825 || !TEST_int_gt(SSL_accept(serverssl), 0))
1828 /* Client and server should not be able to write/read early data now */
1829 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
1833 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1835 SSL_READ_EARLY_DATA_ERROR))
1839 /* Client and server should be able to write/read normal data */
1840 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
1841 || !TEST_size_t_eq(written, strlen(MSG5))
1842 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
1843 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
1849 if (sess != clientpsk)
1850 SSL_SESSION_free(sess);
1851 SSL_SESSION_free(clientpsk);
1852 SSL_SESSION_free(serverpsk);
1853 clientpsk = serverpsk = NULL;
1854 SSL_free(serverssl);
1855 SSL_free(clientssl);
1861 static int test_early_data_replay(int idx)
1863 SSL_CTX *cctx = NULL, *sctx = NULL;
1864 SSL *clientssl = NULL, *serverssl = NULL;
1866 SSL_SESSION *sess = NULL;
1868 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
1869 &serverssl, &sess, idx)))
1873 * The server is configured to accept early data. Create a connection to
1874 * "use up" the ticket
1876 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
1877 || !TEST_true(SSL_session_reused(clientssl)))
1880 SSL_shutdown(clientssl);
1881 SSL_shutdown(serverssl);
1882 SSL_free(serverssl);
1883 SSL_free(clientssl);
1884 serverssl = clientssl = NULL;
1886 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1887 &clientssl, NULL, NULL))
1888 || !TEST_true(SSL_set_session(clientssl, sess))
1889 || !TEST_true(create_ssl_connection(serverssl, clientssl,
1892 * This time we should not have resumed the session because we
1893 * already used it once.
1895 || !TEST_false(SSL_session_reused(clientssl)))
1901 if (sess != clientpsk)
1902 SSL_SESSION_free(sess);
1903 SSL_SESSION_free(clientpsk);
1904 SSL_SESSION_free(serverpsk);
1905 clientpsk = serverpsk = NULL;
1906 SSL_free(serverssl);
1907 SSL_free(clientssl);
1914 * Helper function to test that a server attempting to read early data can
1915 * handle a connection from a client where the early data should be skipped.
1917 static int early_data_skip_helper(int hrr, int idx)
1919 SSL_CTX *cctx = NULL, *sctx = NULL;
1920 SSL *clientssl = NULL, *serverssl = NULL;
1922 SSL_SESSION *sess = NULL;
1923 unsigned char buf[20];
1924 size_t readbytes, written;
1926 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
1927 &serverssl, &sess, idx)))
1931 /* Force an HRR to occur */
1932 if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
1934 } else if (idx == 2) {
1936 * We force early_data rejection by ensuring the PSK identity is
1939 srvid = "Dummy Identity";
1942 * Deliberately corrupt the creation time. We take 20 seconds off the
1943 * time. It could be any value as long as it is not within tolerance.
1944 * This should mean the ticket is rejected.
1946 if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
1950 /* Write some early data */
1951 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
1953 || !TEST_size_t_eq(written, strlen(MSG1)))
1956 /* Server should reject the early data and skip over it */
1957 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1959 SSL_READ_EARLY_DATA_FINISH)
1960 || !TEST_size_t_eq(readbytes, 0)
1961 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
1962 SSL_EARLY_DATA_REJECTED))
1967 * Finish off the handshake. We perform the same writes and reads as
1968 * further down but we expect them to fail due to the incomplete
1971 if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
1972 || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
1977 /* Should be able to send normal data despite rejection of early data */
1978 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
1979 || !TEST_size_t_eq(written, strlen(MSG2))
1980 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
1981 SSL_EARLY_DATA_REJECTED)
1982 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
1983 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
1989 if (sess != clientpsk)
1990 SSL_SESSION_free(clientpsk);
1991 SSL_SESSION_free(serverpsk);
1992 clientpsk = serverpsk = NULL;
1993 SSL_SESSION_free(sess);
1994 SSL_free(serverssl);
1995 SSL_free(clientssl);
2002 * Test that a server attempting to read early data can handle a connection
2003 * from a client where the early data is not acceptable.
2005 static int test_early_data_skip(int idx)
2007 return early_data_skip_helper(0, idx);
2011 * Test that a server attempting to read early data can handle a connection
2012 * from a client where an HRR occurs.
2014 static int test_early_data_skip_hrr(int idx)
2016 return early_data_skip_helper(1, idx);
2020 * Test that a server attempting to read early data can handle a connection
2021 * from a client that doesn't send any.
2023 static int test_early_data_not_sent(int idx)
2025 SSL_CTX *cctx = NULL, *sctx = NULL;
2026 SSL *clientssl = NULL, *serverssl = NULL;
2028 SSL_SESSION *sess = NULL;
2029 unsigned char buf[20];
2030 size_t readbytes, written;
2032 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2033 &serverssl, &sess, idx)))
2036 /* Write some data - should block due to handshake with server */
2037 SSL_set_connect_state(clientssl);
2038 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
2041 /* Server should detect that early data has not been sent */
2042 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2044 SSL_READ_EARLY_DATA_FINISH)
2045 || !TEST_size_t_eq(readbytes, 0)
2046 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2047 SSL_EARLY_DATA_NOT_SENT)
2048 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
2049 SSL_EARLY_DATA_NOT_SENT))
2052 /* Continue writing the message we started earlier */
2053 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
2054 || !TEST_size_t_eq(written, strlen(MSG1))
2055 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2056 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
2057 || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
2058 || !TEST_size_t_eq(written, strlen(MSG2)))
2062 * Should block due to the NewSessionTicket arrival unless we're using
2066 if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)))
2070 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2071 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
2077 /* If using PSK then clientpsk and sess are the same */
2078 SSL_SESSION_free(sess);
2079 SSL_SESSION_free(serverpsk);
2080 clientpsk = serverpsk = NULL;
2081 SSL_free(serverssl);
2082 SSL_free(clientssl);
2088 static int hostname_cb(SSL *s, int *al, void *arg)
2090 const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
2092 if (hostname != NULL && strcmp(hostname, "goodhost") == 0)
2093 return SSL_TLSEXT_ERR_OK;
2095 return SSL_TLSEXT_ERR_NOACK;
2098 static const char *servalpn;
2100 static int alpn_select_cb(SSL *ssl, const unsigned char **out,
2101 unsigned char *outlen, const unsigned char *in,
2102 unsigned int inlen, void *arg)
2104 unsigned int protlen = 0;
2105 const unsigned char *prot;
2107 for (prot = in; prot < in + inlen; prot += protlen) {
2109 if (in + inlen < prot + protlen)
2110 return SSL_TLSEXT_ERR_NOACK;
2112 if (protlen == strlen(servalpn)
2113 && memcmp(prot, servalpn, protlen) == 0) {
2116 return SSL_TLSEXT_ERR_OK;
2120 return SSL_TLSEXT_ERR_NOACK;
2123 /* Test that a PSK can be used to send early_data */
2124 static int test_early_data_psk(int idx)
2126 SSL_CTX *cctx = NULL, *sctx = NULL;
2127 SSL *clientssl = NULL, *serverssl = NULL;
2129 SSL_SESSION *sess = NULL;
2130 unsigned char alpnlist[] = {
2131 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
2134 #define GOODALPNLEN 9
2135 #define BADALPNLEN 8
2136 #define GOODALPN (alpnlist)
2137 #define BADALPN (alpnlist + GOODALPNLEN)
2139 unsigned char buf[20];
2140 size_t readbytes, written;
2141 int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
2142 int edstatus = SSL_EARLY_DATA_ACCEPTED;
2144 /* We always set this up with a final parameter of "2" for PSK */
2145 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2146 &serverssl, &sess, 2)))
2149 servalpn = "goodalpn";
2152 * Note: There is no test for inconsistent SNI with late client detection.
2153 * This is because servers do not acknowledge SNI even if they are using
2154 * it in a resumption handshake - so it is not actually possible for a
2155 * client to detect a problem.
2159 /* Set inconsistent SNI (early client detection) */
2160 err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
2161 if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
2162 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
2167 /* Set inconsistent ALPN (early client detection) */
2168 err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
2169 /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
2170 if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
2172 || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
2179 * Set invalid protocol version. Technically this affects PSKs without
2180 * early_data too, but we test it here because it is similar to the
2181 * SNI/ALPN consistency tests.
2183 err = SSL_R_BAD_PSK;
2184 if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
2190 * Set inconsistent SNI (server detected). In this case the connection
2191 * will succeed but reject early_data.
2193 SSL_SESSION_free(serverpsk);
2194 serverpsk = SSL_SESSION_dup(clientpsk);
2195 if (!TEST_ptr(serverpsk)
2196 || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
2198 edstatus = SSL_EARLY_DATA_REJECTED;
2199 readearlyres = SSL_READ_EARLY_DATA_FINISH;
2202 /* Set consistent SNI */
2203 if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
2204 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
2205 || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
2212 * Set inconsistent ALPN (server detected). In this case the connection
2213 * will succeed but reject early_data.
2215 servalpn = "badalpn";
2216 edstatus = SSL_EARLY_DATA_REJECTED;
2217 readearlyres = SSL_READ_EARLY_DATA_FINISH;
2221 * Set consistent ALPN.
2222 * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
2223 * accepts a list of protos (each one length prefixed).
2224 * SSL_set1_alpn_selected accepts a single protocol (not length
2227 if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
2229 || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
2233 SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
2237 /* Set inconsistent ALPN (late client detection) */
2238 SSL_SESSION_free(serverpsk);
2239 serverpsk = SSL_SESSION_dup(clientpsk);
2240 if (!TEST_ptr(serverpsk)
2241 || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
2244 || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
2247 || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
2250 SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
2251 edstatus = SSL_EARLY_DATA_ACCEPTED;
2252 readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
2253 /* SSL_connect() call should fail */
2258 TEST_error("Bad test index");
2262 SSL_set_connect_state(clientssl);
2264 if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2266 || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
2267 || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
2270 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2274 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2275 &readbytes), readearlyres)
2276 || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS
2277 && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
2278 || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
2279 || !TEST_int_eq(SSL_connect(clientssl), connectres))
2286 SSL_SESSION_free(clientpsk);
2287 SSL_SESSION_free(serverpsk);
2288 clientpsk = serverpsk = NULL;
2289 SSL_free(serverssl);
2290 SSL_free(clientssl);
2297 * Test that a server that doesn't try to read early data can handle a
2298 * client sending some.
2300 static int test_early_data_not_expected(int idx)
2302 SSL_CTX *cctx = NULL, *sctx = NULL;
2303 SSL *clientssl = NULL, *serverssl = NULL;
2305 SSL_SESSION *sess = NULL;
2306 unsigned char buf[20];
2307 size_t readbytes, written;
2309 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2310 &serverssl, &sess, idx)))
2313 /* Write some early data */
2314 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2319 * Server should skip over early data and then block waiting for client to
2320 * continue handshake
2322 if (!TEST_int_le(SSL_accept(serverssl), 0)
2323 || !TEST_int_gt(SSL_connect(clientssl), 0)
2324 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2325 SSL_EARLY_DATA_REJECTED)
2326 || !TEST_int_gt(SSL_accept(serverssl), 0)
2327 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
2328 SSL_EARLY_DATA_REJECTED))
2331 /* Send some normal data from client to server */
2332 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
2333 || !TEST_size_t_eq(written, strlen(MSG2)))
2336 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2337 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
2343 /* If using PSK then clientpsk and sess are the same */
2344 SSL_SESSION_free(sess);
2345 SSL_SESSION_free(serverpsk);
2346 clientpsk = serverpsk = NULL;
2347 SSL_free(serverssl);
2348 SSL_free(clientssl);
2355 # ifndef OPENSSL_NO_TLS1_2
2357 * Test that a server attempting to read early data can handle a connection
2358 * from a TLSv1.2 client.
2360 static int test_early_data_tls1_2(int idx)
2362 SSL_CTX *cctx = NULL, *sctx = NULL;
2363 SSL *clientssl = NULL, *serverssl = NULL;
2365 unsigned char buf[20];
2366 size_t readbytes, written;
2368 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2369 &serverssl, NULL, idx)))
2372 /* Write some data - should block due to handshake with server */
2373 SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
2374 SSL_set_connect_state(clientssl);
2375 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
2379 * Server should do TLSv1.2 handshake. First it will block waiting for more
2380 * messages from client after ServerDone. Then SSL_read_early_data should
2381 * finish and detect that early data has not been sent
2383 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2385 SSL_READ_EARLY_DATA_ERROR))
2389 * Continue writing the message we started earlier. Will still block waiting
2390 * for the CCS/Finished from server
2392 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
2393 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2395 SSL_READ_EARLY_DATA_FINISH)
2396 || !TEST_size_t_eq(readbytes, 0)
2397 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2398 SSL_EARLY_DATA_NOT_SENT))
2401 /* Continue writing the message we started earlier */
2402 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
2403 || !TEST_size_t_eq(written, strlen(MSG1))
2404 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
2405 SSL_EARLY_DATA_NOT_SENT)
2406 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2407 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
2408 || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
2409 || !TEST_size_t_eq(written, strlen(MSG2))
2410 || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
2411 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
2417 /* If using PSK then clientpsk and sess are the same */
2418 SSL_SESSION_free(clientpsk);
2419 SSL_SESSION_free(serverpsk);
2420 clientpsk = serverpsk = NULL;
2421 SSL_free(serverssl);
2422 SSL_free(clientssl);
2428 # endif /* OPENSSL_NO_TLS1_2 */
2430 static int test_ciphersuite_change(void)
2432 SSL_CTX *cctx = NULL, *sctx = NULL;
2433 SSL *clientssl = NULL, *serverssl = NULL;
2434 SSL_SESSION *clntsess = NULL;
2436 const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
2438 /* Create a session based on SHA-256 */
2439 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
2440 TLS_client_method(), &sctx,
2441 &cctx, cert, privkey))
2442 || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
2443 "TLS_AES_128_GCM_SHA256"))
2444 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2445 &clientssl, NULL, NULL))
2446 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2450 clntsess = SSL_get1_session(clientssl);
2451 /* Save for later */
2452 aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
2453 SSL_shutdown(clientssl);
2454 SSL_shutdown(serverssl);
2455 SSL_free(serverssl);
2456 SSL_free(clientssl);
2457 serverssl = clientssl = NULL;
2459 # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
2460 /* Check we can resume a session with a different SHA-256 ciphersuite */
2461 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
2462 "TLS_CHACHA20_POLY1305_SHA256"))
2463 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2465 || !TEST_true(SSL_set_session(clientssl, clntsess))
2466 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2468 || !TEST_true(SSL_session_reused(clientssl)))
2471 SSL_SESSION_free(clntsess);
2472 clntsess = SSL_get1_session(clientssl);
2473 SSL_shutdown(clientssl);
2474 SSL_shutdown(serverssl);
2475 SSL_free(serverssl);
2476 SSL_free(clientssl);
2477 serverssl = clientssl = NULL;
2481 * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
2482 * succeeds but does not resume.
2484 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
2485 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2487 || !TEST_true(SSL_set_session(clientssl, clntsess))
2488 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2490 || !TEST_false(SSL_session_reused(clientssl)))
2493 SSL_SESSION_free(clntsess);
2495 SSL_shutdown(clientssl);
2496 SSL_shutdown(serverssl);
2497 SSL_free(serverssl);
2498 SSL_free(clientssl);
2499 serverssl = clientssl = NULL;
2501 /* Create a session based on SHA384 */
2502 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
2503 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2504 &clientssl, NULL, NULL))
2505 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2509 clntsess = SSL_get1_session(clientssl);
2510 SSL_shutdown(clientssl);
2511 SSL_shutdown(serverssl);
2512 SSL_free(serverssl);
2513 SSL_free(clientssl);
2514 serverssl = clientssl = NULL;
2516 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
2517 "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
2518 || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
2519 "TLS_AES_256_GCM_SHA384"))
2520 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2522 || !TEST_true(SSL_set_session(clientssl, clntsess))
2524 * We use SSL_ERROR_WANT_READ below so that we can pause the
2525 * connection after the initial ClientHello has been sent to
2526 * enable us to make some session changes.
2528 || !TEST_false(create_ssl_connection(serverssl, clientssl,
2529 SSL_ERROR_WANT_READ)))
2532 /* Trick the client into thinking this session is for a different digest */
2533 clntsess->cipher = aes_128_gcm_sha256;
2534 clntsess->cipher_id = clntsess->cipher->id;
2537 * Continue the previously started connection. Server has selected a SHA-384
2538 * ciphersuite, but client thinks the session is for SHA-256, so it should
2541 if (!TEST_false(create_ssl_connection(serverssl, clientssl,
2543 || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
2544 SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
2550 SSL_SESSION_free(clntsess);
2551 SSL_free(serverssl);
2552 SSL_free(clientssl);
2559 static int test_tls13_psk(int idx)
2561 SSL_CTX *sctx = NULL, *cctx = NULL;
2562 SSL *serverssl = NULL, *clientssl = NULL;
2563 const SSL_CIPHER *cipher = NULL;
2564 const unsigned char key[] = {
2565 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
2566 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
2567 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
2568 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
2572 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
2573 TLS_client_method(), &sctx,
2574 &cctx, cert, privkey)))
2578 * We use a ciphersuite with SHA256 to ease testing old style PSK callbacks
2579 * which will always default to SHA256
2581 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
2585 * Test 0: New style callbacks only
2586 * Test 1: New and old style callbacks (only the new ones should be used)
2587 * Test 2: Old style callbacks only
2589 if (idx == 0 || idx == 1) {
2590 SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
2591 SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
2593 if (idx == 1 || idx == 2) {
2594 SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
2595 SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
2598 use_session_cb_cnt = 0;
2599 find_session_cb_cnt = 0;
2600 psk_client_cb_cnt = 0;
2601 psk_server_cb_cnt = 0;
2603 /* Check we can create a connection if callback decides not to send a PSK */
2604 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2606 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2608 || !TEST_false(SSL_session_reused(clientssl))
2609 || !TEST_false(SSL_session_reused(serverssl)))
2612 if (idx == 0 || idx == 1) {
2613 if (!TEST_true(use_session_cb_cnt == 1)
2614 || !TEST_true(find_session_cb_cnt == 0)
2616 * If no old style callback then below should be 0
2619 || !TEST_true(psk_client_cb_cnt == idx)
2620 || !TEST_true(psk_server_cb_cnt == 0))
2623 if (!TEST_true(use_session_cb_cnt == 0)
2624 || !TEST_true(find_session_cb_cnt == 0)
2625 || !TEST_true(psk_client_cb_cnt == 1)
2626 || !TEST_true(psk_server_cb_cnt == 0))
2630 shutdown_ssl_connection(serverssl, clientssl);
2631 serverssl = clientssl = NULL;
2632 use_session_cb_cnt = psk_client_cb_cnt = 0;
2634 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2638 /* Create the PSK */
2639 cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
2640 clientpsk = SSL_SESSION_new();
2641 if (!TEST_ptr(clientpsk)
2642 || !TEST_ptr(cipher)
2643 || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
2645 || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
2646 || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
2648 || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
2650 serverpsk = clientpsk;
2652 /* Check we can create a connection and the PSK is used */
2653 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
2654 || !TEST_true(SSL_session_reused(clientssl))
2655 || !TEST_true(SSL_session_reused(serverssl)))
2658 if (idx == 0 || idx == 1) {
2659 if (!TEST_true(use_session_cb_cnt == 1)
2660 || !TEST_true(find_session_cb_cnt == 1)
2661 || !TEST_true(psk_client_cb_cnt == 0)
2662 || !TEST_true(psk_server_cb_cnt == 0))
2665 if (!TEST_true(use_session_cb_cnt == 0)
2666 || !TEST_true(find_session_cb_cnt == 0)
2667 || !TEST_true(psk_client_cb_cnt == 1)
2668 || !TEST_true(psk_server_cb_cnt == 1))
2672 shutdown_ssl_connection(serverssl, clientssl);
2673 serverssl = clientssl = NULL;
2674 use_session_cb_cnt = find_session_cb_cnt = 0;
2675 psk_client_cb_cnt = psk_server_cb_cnt = 0;
2677 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2682 if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
2686 * Check we can create a connection, the PSK is used and the callbacks are
2689 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
2690 || !TEST_true(SSL_session_reused(clientssl))
2691 || !TEST_true(SSL_session_reused(serverssl)))
2694 if (idx == 0 || idx == 1) {
2695 if (!TEST_true(use_session_cb_cnt == 2)
2696 || !TEST_true(find_session_cb_cnt == 2)
2697 || !TEST_true(psk_client_cb_cnt == 0)
2698 || !TEST_true(psk_server_cb_cnt == 0))
2701 if (!TEST_true(use_session_cb_cnt == 0)
2702 || !TEST_true(find_session_cb_cnt == 0)
2703 || !TEST_true(psk_client_cb_cnt == 2)
2704 || !TEST_true(psk_server_cb_cnt == 2))
2708 shutdown_ssl_connection(serverssl, clientssl);
2709 serverssl = clientssl = NULL;
2710 use_session_cb_cnt = find_session_cb_cnt = 0;
2711 psk_client_cb_cnt = psk_server_cb_cnt = 0;
2714 * Check that if the server rejects the PSK we can still connect, but with
2717 srvid = "Dummy Identity";
2718 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2720 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2722 || !TEST_false(SSL_session_reused(clientssl))
2723 || !TEST_false(SSL_session_reused(serverssl)))
2726 if (idx == 0 || idx == 1) {
2727 if (!TEST_true(use_session_cb_cnt == 1)
2728 || !TEST_true(find_session_cb_cnt == 1)
2729 || !TEST_true(psk_client_cb_cnt == 0)
2731 * If no old style callback then below should be 0
2734 || !TEST_true(psk_server_cb_cnt == idx))
2737 if (!TEST_true(use_session_cb_cnt == 0)
2738 || !TEST_true(find_session_cb_cnt == 0)
2739 || !TEST_true(psk_client_cb_cnt == 1)
2740 || !TEST_true(psk_server_cb_cnt == 1))
2744 shutdown_ssl_connection(serverssl, clientssl);
2745 serverssl = clientssl = NULL;
2749 SSL_SESSION_free(clientpsk);
2750 SSL_SESSION_free(serverpsk);
2751 clientpsk = serverpsk = NULL;
2752 SSL_free(serverssl);
2753 SSL_free(clientssl);
2759 static unsigned char cookie_magic_value[] = "cookie magic";
2761 static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
2762 unsigned int *cookie_len)
2765 * Not suitable as a real cookie generation function but good enough for
2768 memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
2769 *cookie_len = sizeof(cookie_magic_value) - 1;
2774 static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
2775 unsigned int cookie_len)
2777 if (cookie_len == sizeof(cookie_magic_value) - 1
2778 && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
2784 static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
2788 int res = generate_cookie_callback(ssl, cookie, &temp);
2793 static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
2796 return verify_cookie_callback(ssl, cookie, cookie_len);
2799 static int test_stateless(void)
2801 SSL_CTX *sctx = NULL, *cctx = NULL;
2802 SSL *serverssl = NULL, *clientssl = NULL;
2805 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
2806 TLS_client_method(), &sctx,
2807 &cctx, cert, privkey)))
2810 /* The arrival of CCS messages can confuse the test */
2811 SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
2813 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2815 /* Send the first ClientHello */
2816 || !TEST_false(create_ssl_connection(serverssl, clientssl,
2817 SSL_ERROR_WANT_READ))
2819 * This should fail with a -1 return because we have no callbacks
2822 || !TEST_int_eq(SSL_stateless(serverssl), -1))
2825 /* Fatal error so abandon the connection from this client */
2826 SSL_free(clientssl);
2829 /* Set up the cookie generation and verification callbacks */
2830 SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
2831 SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
2834 * Create a new connection from the client (we can reuse the server SSL
2837 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2839 /* Send the first ClientHello */
2840 || !TEST_false(create_ssl_connection(serverssl, clientssl,
2841 SSL_ERROR_WANT_READ))
2842 /* This should fail because there is no cookie */
2843 || !TEST_int_eq(SSL_stateless(serverssl), 0))
2846 /* Abandon the connection from this client */
2847 SSL_free(clientssl);
2851 * Now create a connection from a new client but with the same server SSL
2854 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2856 /* Send the first ClientHello */
2857 || !TEST_false(create_ssl_connection(serverssl, clientssl,
2858 SSL_ERROR_WANT_READ))
2859 /* This should fail because there is no cookie */
2860 || !TEST_int_eq(SSL_stateless(serverssl), 0)
2861 /* Send the second ClientHello */
2862 || !TEST_false(create_ssl_connection(serverssl, clientssl,
2863 SSL_ERROR_WANT_READ))
2864 /* This should succeed because a cookie is now present */
2865 || !TEST_int_eq(SSL_stateless(serverssl), 1)
2866 /* Complete the connection */
2867 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2871 shutdown_ssl_connection(serverssl, clientssl);
2872 serverssl = clientssl = NULL;
2876 SSL_free(serverssl);
2877 SSL_free(clientssl);
2883 #endif /* OPENSSL_NO_TLS1_3 */
2885 static int clntaddoldcb = 0;
2886 static int clntparseoldcb = 0;
2887 static int srvaddoldcb = 0;
2888 static int srvparseoldcb = 0;
2889 static int clntaddnewcb = 0;
2890 static int clntparsenewcb = 0;
2891 static int srvaddnewcb = 0;
2892 static int srvparsenewcb = 0;
2893 static int snicb = 0;
2895 #define TEST_EXT_TYPE1 0xff00
2897 static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
2898 size_t *outlen, int *al, void *add_arg)
2900 int *server = (int *)add_arg;
2901 unsigned char *data;
2903 if (SSL_is_server(s))
2908 if (*server != SSL_is_server(s)
2909 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
2914 *outlen = sizeof(char);
2918 static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
2921 OPENSSL_free((unsigned char *)out);
2924 static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
2925 size_t inlen, int *al, void *parse_arg)
2927 int *server = (int *)parse_arg;
2929 if (SSL_is_server(s))
2934 if (*server != SSL_is_server(s)
2935 || inlen != sizeof(char)
2942 static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
2943 const unsigned char **out, size_t *outlen, X509 *x,
2944 size_t chainidx, int *al, void *add_arg)
2946 int *server = (int *)add_arg;
2947 unsigned char *data;
2949 if (SSL_is_server(s))
2954 if (*server != SSL_is_server(s)
2955 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
2960 *outlen = sizeof(*data);
2964 static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
2965 const unsigned char *out, void *add_arg)
2967 OPENSSL_free((unsigned char *)out);
2970 static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
2971 const unsigned char *in, size_t inlen, X509 *x,
2972 size_t chainidx, int *al, void *parse_arg)
2974 int *server = (int *)parse_arg;
2976 if (SSL_is_server(s))
2981 if (*server != SSL_is_server(s)
2982 || inlen != sizeof(char) || *in != 1)
2988 static int sni_cb(SSL *s, int *al, void *arg)
2990 SSL_CTX *ctx = (SSL_CTX *)arg;
2992 if (SSL_set_SSL_CTX(s, ctx) == NULL) {
2993 *al = SSL_AD_INTERNAL_ERROR;
2994 return SSL_TLSEXT_ERR_ALERT_FATAL;
2997 return SSL_TLSEXT_ERR_OK;
3001 * Custom call back tests.
3002 * Test 0: Old style callbacks in TLSv1.2
3003 * Test 1: New style callbacks in TLSv1.2
3004 * Test 2: New style callbacks in TLSv1.2 with SNI
3005 * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
3006 * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
3008 static int test_custom_exts(int tst)
3010 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
3011 SSL *clientssl = NULL, *serverssl = NULL;
3013 static int server = 1;
3014 static int client = 0;
3015 SSL_SESSION *sess = NULL;
3016 unsigned int context;
3018 #if defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_TLS1_3)
3019 /* Skip tests for TLSv1.2 and below in this case */
3024 /* Reset callback counters */
3025 clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
3026 clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
3029 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
3030 TLS_client_method(), &sctx,
3031 &cctx, cert, privkey)))
3035 && !TEST_true(create_ssl_ctx_pair(TLS_server_method(), NULL, &sctx2,
3036 NULL, cert, privkey)))
3041 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
3042 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
3044 SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
3048 context = SSL_EXT_CLIENT_HELLO
3049 | SSL_EXT_TLS1_2_SERVER_HELLO
3050 | SSL_EXT_TLS1_3_SERVER_HELLO
3051 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
3052 | SSL_EXT_TLS1_3_CERTIFICATE
3053 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
3055 context = SSL_EXT_CLIENT_HELLO
3056 | SSL_EXT_TLS1_2_SERVER_HELLO
3057 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
3060 /* Create a client side custom extension */
3062 if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
3063 old_add_cb, old_free_cb,
3064 &client, old_parse_cb,
3068 if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
3069 new_add_cb, new_free_cb,
3070 &client, new_parse_cb, &client)))
3074 /* Should not be able to add duplicates */
3075 if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
3076 old_add_cb, old_free_cb,
3077 &client, old_parse_cb,
3079 || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
3080 context, new_add_cb,
3081 new_free_cb, &client,
3082 new_parse_cb, &client)))
3085 /* Create a server side custom extension */
3087 if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
3088 old_add_cb, old_free_cb,
3089 &server, old_parse_cb,
3093 if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
3094 new_add_cb, new_free_cb,
3095 &server, new_parse_cb, &server)))
3098 && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
3099 context, new_add_cb,
3100 new_free_cb, &server,
3101 new_parse_cb, &server)))
3105 /* Should not be able to add duplicates */
3106 if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
3107 old_add_cb, old_free_cb,
3108 &server, old_parse_cb,
3110 || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
3111 context, new_add_cb,
3112 new_free_cb, &server,
3113 new_parse_cb, &server)))
3118 if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
3119 || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
3123 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3124 &clientssl, NULL, NULL))
3125 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3130 if (clntaddoldcb != 1
3131 || clntparseoldcb != 1
3133 || srvparseoldcb != 1)
3135 } else if (tst == 1 || tst == 2 || tst == 3) {
3136 if (clntaddnewcb != 1
3137 || clntparsenewcb != 1
3139 || srvparsenewcb != 1
3140 || (tst != 2 && snicb != 0)
3141 || (tst == 2 && snicb != 1))
3144 if (clntaddnewcb != 1
3145 || clntparsenewcb != 4
3147 || srvparsenewcb != 1)
3151 sess = SSL_get1_session(clientssl);
3152 SSL_shutdown(clientssl);
3153 SSL_shutdown(serverssl);
3154 SSL_free(serverssl);
3155 SSL_free(clientssl);
3156 serverssl = clientssl = NULL;
3159 /* We don't bother with the resumption aspects for this test */
3164 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3166 || !TEST_true(SSL_set_session(clientssl, sess))
3167 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3172 * For a resumed session we expect to add the ClientHello extension. For the
3173 * old style callbacks we ignore it on the server side because they set
3174 * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
3178 if (clntaddoldcb != 2
3179 || clntparseoldcb != 1
3181 || srvparseoldcb != 1)
3183 } else if (tst == 1 || tst == 2 || tst == 3) {
3184 if (clntaddnewcb != 2
3185 || clntparsenewcb != 2
3187 || srvparsenewcb != 2)
3190 /* No Certificate message extensions in the resumption handshake */
3191 if (clntaddnewcb != 2
3192 || clntparsenewcb != 7
3194 || srvparsenewcb != 2)
3201 SSL_SESSION_free(sess);
3202 SSL_free(serverssl);
3203 SSL_free(clientssl);
3204 SSL_CTX_free(sctx2);
3211 * Test loading of serverinfo data in various formats. test_sslmessages actually
3212 * tests to make sure the extensions appear in the handshake
3214 static int test_serverinfo(int tst)
3216 unsigned int version;
3217 unsigned char *sibuf;
3219 int ret, expected, testresult = 0;
3222 ctx = SSL_CTX_new(TLS_method());
3226 if ((tst & 0x01) == 0x01)
3227 version = SSL_SERVERINFOV2;
3229 version = SSL_SERVERINFOV1;
3231 if ((tst & 0x02) == 0x02) {
3232 sibuf = serverinfov2;
3233 sibuflen = sizeof(serverinfov2);
3234 expected = (version == SSL_SERVERINFOV2);
3236 sibuf = serverinfov1;
3237 sibuflen = sizeof(serverinfov1);
3238 expected = (version == SSL_SERVERINFOV1);
3241 if ((tst & 0x04) == 0x04) {
3242 ret = SSL_CTX_use_serverinfo_ex(ctx, version, sibuf, sibuflen);
3244 ret = SSL_CTX_use_serverinfo(ctx, sibuf, sibuflen);
3247 * The version variable is irrelevant in this case - it's what is in the
3248 * buffer that matters
3250 if ((tst & 0x02) == 0x02)
3256 if (!TEST_true(ret == expected))
3268 * Test that SSL_export_keying_material() produces expected results. There are
3269 * no test vectors so all we do is test that both sides of the communication
3270 * produce the same results for different protocol versions.
3272 static int test_export_key_mat(int tst)
3275 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
3276 SSL *clientssl = NULL, *serverssl = NULL;
3277 const char label[] = "test label";
3278 const unsigned char context[] = "context";
3279 const unsigned char *emptycontext = NULL;
3280 unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
3281 unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
3282 const int protocols[] = {
3289 #ifdef OPENSSL_NO_TLS1
3293 #ifdef OPENSSL_NO_TLS1_1
3297 #ifdef OPENSSL_NO_TLS1_2
3301 #ifdef OPENSSL_NO_TLS1_3
3305 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
3306 TLS_client_method(), &sctx,
3307 &cctx, cert, privkey)))
3310 OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
3311 SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
3312 SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
3314 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
3316 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3320 if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
3321 sizeof(ckeymat1), label,
3322 sizeof(label) - 1, context,
3323 sizeof(context) - 1, 1), 1)
3324 || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
3325 sizeof(ckeymat2), label,
3329 || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
3330 sizeof(ckeymat3), label,
3333 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
3334 sizeof(skeymat1), label,
3337 sizeof(context) -1, 1),
3339 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
3340 sizeof(skeymat2), label,
3344 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
3345 sizeof(skeymat3), label,
3349 * Check that both sides created the same key material with the
3352 || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
3355 * Check that both sides created the same key material with an
3358 || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
3361 * Check that both sides created the same key material without a
3364 || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
3366 /* Different contexts should produce different results */
3367 || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
3372 * Check that an empty context and no context produce different results in
3373 * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
3375 if ((tst != 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
3377 || (tst ==3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
3384 SSL_free(serverssl);
3385 SSL_free(clientssl);
3386 SSL_CTX_free(sctx2);
3393 #ifndef OPENSSL_NO_TLS1_3
3395 * Test that SSL_export_keying_material_early() produces expected
3396 * results. There are no test vectors so all we do is test that both
3397 * sides of the communication produce the same results for different
3398 * protocol versions.
3400 static int test_export_key_mat_early(int idx)
3402 static const char label[] = "test label";
3403 static const unsigned char context[] = "context";
3405 SSL_CTX *cctx = NULL, *sctx = NULL;
3406 SSL *clientssl = NULL, *serverssl = NULL;
3407 SSL_SESSION *sess = NULL;
3408 const unsigned char *emptycontext = NULL;
3409 unsigned char ckeymat1[80], ckeymat2[80];
3410 unsigned char skeymat1[80], skeymat2[80];
3411 unsigned char buf[1];
3412 size_t readbytes, written;
3414 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
3418 /* Here writing 0 length early data is enough. */
3419 if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
3420 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3422 SSL_READ_EARLY_DATA_ERROR)
3423 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3424 SSL_EARLY_DATA_ACCEPTED))
3427 if (!TEST_int_eq(SSL_export_keying_material_early(
3428 clientssl, ckeymat1, sizeof(ckeymat1), label,
3429 sizeof(label) - 1, context, sizeof(context) - 1), 1)
3430 || !TEST_int_eq(SSL_export_keying_material_early(
3431 clientssl, ckeymat2, sizeof(ckeymat2), label,
3432 sizeof(label) - 1, emptycontext, 0), 1)
3433 || !TEST_int_eq(SSL_export_keying_material_early(
3434 serverssl, skeymat1, sizeof(skeymat1), label,
3435 sizeof(label) - 1, context, sizeof(context) - 1), 1)
3436 || !TEST_int_eq(SSL_export_keying_material_early(
3437 serverssl, skeymat2, sizeof(skeymat2), label,
3438 sizeof(label) - 1, emptycontext, 0), 1)
3440 * Check that both sides created the same key material with the
3443 || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
3446 * Check that both sides created the same key material with an
3449 || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
3451 /* Different contexts should produce different results */
3452 || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
3459 if (sess != clientpsk)
3460 SSL_SESSION_free(sess);
3461 SSL_SESSION_free(clientpsk);
3462 SSL_SESSION_free(serverpsk);
3463 clientpsk = serverpsk = NULL;
3464 SSL_free(serverssl);
3465 SSL_free(clientssl);
3471 #endif /* OPENSSL_NO_TLS1_3 */
3473 static int test_ssl_clear(int idx)
3475 SSL_CTX *cctx = NULL, *sctx = NULL;
3476 SSL *clientssl = NULL, *serverssl = NULL;
3479 #ifdef OPENSSL_NO_TLS1_2
3484 /* Create an initial connection */
3485 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
3486 TLS_client_method(), &sctx,
3487 &cctx, cert, privkey))
3489 && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
3491 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3492 &clientssl, NULL, NULL))
3493 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3497 SSL_shutdown(clientssl);
3498 SSL_shutdown(serverssl);
3499 SSL_free(serverssl);
3502 /* Clear clientssl - we're going to reuse the object */
3503 if (!TEST_true(SSL_clear(clientssl)))
3506 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3508 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3510 || !TEST_true(SSL_session_reused(clientssl)))
3513 SSL_shutdown(clientssl);
3514 SSL_shutdown(serverssl);
3519 SSL_free(serverssl);
3520 SSL_free(clientssl);
3527 /* Parse CH and retrieve any MFL extension value if present */
3528 static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
3531 unsigned char *data;
3532 PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
3533 unsigned int MFL_code = 0, type = 0;
3535 if (!TEST_uint_gt( len = BIO_get_mem_data( bio, (char **) &data ), 0 ) )
3538 if (!TEST_true( PACKET_buf_init( &pkt, data, len ) )
3539 /* Skip the record header */
3540 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
3541 /* Skip the handshake message header */
3542 || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
3543 /* Skip client version and random */
3544 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
3545 + SSL3_RANDOM_SIZE))
3546 /* Skip session id */
3547 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
3549 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
3550 /* Skip compression */
3551 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
3552 /* Extensions len */
3553 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
3556 /* Loop through all extensions */
3557 while (PACKET_remaining(&pkt2)) {
3558 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
3559 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
3562 if (type == TLSEXT_TYPE_max_fragment_length) {
3563 if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
3564 || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
3567 *mfl_codemfl_code = MFL_code;
3576 /* Maximum-Fragment-Length TLS extension mode to test */
3577 static const unsigned char max_fragment_len_test[] = {
3578 TLSEXT_max_fragment_length_512,
3579 TLSEXT_max_fragment_length_1024,
3580 TLSEXT_max_fragment_length_2048,
3581 TLSEXT_max_fragment_length_4096
3584 static int test_max_fragment_len_ext(int idx_tst)
3588 int testresult = 0, MFL_mode = 0;
3591 ctx = SSL_CTX_new(TLS_method());
3595 if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
3596 ctx, max_fragment_len_test[idx_tst])))
3603 rbio = BIO_new(BIO_s_mem());
3604 wbio = BIO_new(BIO_s_mem());
3605 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
3611 SSL_set_bio(con, rbio, wbio);
3612 SSL_set_connect_state(con);
3614 if (!TEST_int_le(SSL_connect(con), 0)) {
3615 /* This shouldn't succeed because we don't have a server! */
3619 if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
3620 /* no MFL in client hello */
3622 if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
3634 #ifndef OPENSSL_NO_TLS1_3
3635 static int test_pha_key_update(void)
3637 SSL_CTX *cctx = NULL, *sctx = NULL;
3638 SSL *clientssl = NULL, *serverssl = NULL;
3641 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
3642 TLS_client_method(),
3643 &sctx, &cctx, cert, privkey)))
3646 if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
3647 || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
3648 || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
3649 || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
3653 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3657 SSL_force_post_handshake_auth(clientssl);
3659 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
3663 SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
3664 if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
3667 if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
3670 /* Start handshake on the server */
3671 if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
3674 /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
3675 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
3679 SSL_shutdown(clientssl);
3680 SSL_shutdown(serverssl);
3685 SSL_free(serverssl);
3686 SSL_free(clientssl);
3693 int setup_tests(void)
3695 if (!TEST_ptr(cert = test_get_argument(0))
3696 || !TEST_ptr(privkey = test_get_argument(1)))
3699 if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
3700 #ifdef OPENSSL_NO_CRYPTO_MDEBUG
3701 TEST_error("not supported in this build");
3704 int i, mcount, rcount, fcount;
3706 for (i = 0; i < 4; i++)
3707 test_export_key_mat(i);
3708 CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
3709 test_printf_stdout("malloc %d realloc %d free %d\n",
3710 mcount, rcount, fcount);
3715 ADD_TEST(test_large_message_tls);
3716 ADD_TEST(test_large_message_tls_read_ahead);
3717 #ifndef OPENSSL_NO_DTLS
3718 ADD_TEST(test_large_message_dtls);
3720 #ifndef OPENSSL_NO_OCSP
3721 ADD_TEST(test_tlsext_status_type);
3723 ADD_TEST(test_session_with_only_int_cache);
3724 ADD_TEST(test_session_with_only_ext_cache);
3725 ADD_TEST(test_session_with_both_cache);
3726 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
3727 ADD_TEST(test_ssl_bio_pop_next_bio);
3728 ADD_TEST(test_ssl_bio_pop_ssl_bio);
3729 ADD_TEST(test_ssl_bio_change_rbio);
3730 ADD_TEST(test_ssl_bio_change_wbio);
3731 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
3732 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
3733 ADD_TEST(test_keylog);
3735 #ifndef OPENSSL_NO_TLS1_3
3736 ADD_TEST(test_keylog_no_master_key);
3738 #ifndef OPENSSL_NO_TLS1_2
3739 ADD_TEST(test_client_hello_cb);
3741 #ifndef OPENSSL_NO_TLS1_3
3742 ADD_ALL_TESTS(test_early_data_read_write, 3);
3744 * We don't do replay tests for external PSK. Replay protection isn't used
3747 ADD_ALL_TESTS(test_early_data_replay, 2);
3748 ADD_ALL_TESTS(test_early_data_skip, 3);
3749 ADD_ALL_TESTS(test_early_data_skip_hrr, 3);
3750 ADD_ALL_TESTS(test_early_data_not_sent, 3);
3751 ADD_ALL_TESTS(test_early_data_psk, 8);
3752 ADD_ALL_TESTS(test_early_data_not_expected, 3);
3753 # ifndef OPENSSL_NO_TLS1_2
3754 ADD_ALL_TESTS(test_early_data_tls1_2, 3);
3757 #ifndef OPENSSL_NO_TLS1_3
3758 ADD_TEST(test_ciphersuite_change);
3759 ADD_ALL_TESTS(test_tls13_psk, 3);
3760 ADD_ALL_TESTS(test_custom_exts, 5);
3761 ADD_TEST(test_stateless);
3762 ADD_TEST(test_pha_key_update);
3764 ADD_ALL_TESTS(test_custom_exts, 3);
3766 ADD_ALL_TESTS(test_serverinfo, 8);
3767 ADD_ALL_TESTS(test_export_key_mat, 4);
3768 #ifndef OPENSSL_NO_TLS1_3
3769 ADD_ALL_TESTS(test_export_key_mat_early, 3);
3771 ADD_ALL_TESTS(test_ssl_clear, 2);
3772 ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
3776 void cleanup_tests(void)
3778 bio_s_mempacket_test_free();