2 * Copyright 2016 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"
22 static char *cert = NULL;
23 static char *privkey = NULL;
25 #define LOG_BUFFER_SIZE 1024
26 static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
27 static size_t server_log_buffer_index = 0;
28 static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
29 static size_t client_log_buffer_index = 0;
30 static int error_writing_log = 0;
32 #ifndef OPENSSL_NO_OCSP
33 static const unsigned char orespder[] = "Dummy OCSP Response";
34 static int ocsp_server_called = 0;
35 static int ocsp_client_called = 0;
37 static int cdummyarg = 1;
38 static X509 *ocspcert = NULL;
41 #define NUM_EXTRA_CERTS 40
44 * This structure is used to validate that the correct number of log messages
45 * of various types are emitted when emitting secret logs.
47 struct sslapitest_log_counts {
48 unsigned int rsa_key_exchange_count;
49 unsigned int master_secret_count;
50 unsigned int client_handshake_secret_count;
51 unsigned int server_handshake_secret_count;
52 unsigned int client_application_secret_count;
53 unsigned int server_application_secret_count;
57 static unsigned char serverinfov1[] = {
58 0xff, 0xff, /* Dummy extension type */
59 0x00, 0x01, /* Extension length is 1 byte */
60 0xff /* Dummy extension data */
63 static unsigned char serverinfov2[] = {
65 (unsigned char)(SSL_EXT_CLIENT_HELLO & 0xff), /* Dummy context - 4 bytes */
66 0xff, 0xff, /* Dummy extension type */
67 0x00, 0x01, /* Extension length is 1 byte */
68 0xff /* Dummy extension data */
71 static void client_keylog_callback(const SSL *ssl, const char *line)
73 int line_length = strlen(line);
75 /* If the log doesn't fit, error out. */
76 if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
77 TEST_info("Client log too full");
78 error_writing_log = 1;
82 strcat(client_log_buffer, line);
83 client_log_buffer_index += line_length;
84 client_log_buffer[client_log_buffer_index++] = '\n';
87 static void server_keylog_callback(const SSL *ssl, const char *line)
89 int line_length = strlen(line);
91 /* If the log doesn't fit, error out. */
92 if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
93 TEST_info("Server og too full");
94 error_writing_log = 1;
98 strcat(server_log_buffer, line);
99 server_log_buffer_index += line_length;
100 server_log_buffer[server_log_buffer_index++] = '\n';
103 static int compare_hex_encoded_buffer(const char *hex_encoded,
111 if (!TEST_size_t_eq(raw_length * 2, hex_length))
114 for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
115 sprintf(hexed, "%02x", raw[i]);
116 if (!TEST_int_eq(hexed[0], hex_encoded[j])
117 || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
124 static int test_keylog_output(char *buffer, const SSL *ssl,
125 const SSL_SESSION *session,
126 struct sslapitest_log_counts *expected)
129 unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
130 size_t client_random_size = SSL3_RANDOM_SIZE;
131 unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
132 size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
133 unsigned int rsa_key_exchange_count = 0;
134 unsigned int master_secret_count = 0;
135 unsigned int client_handshake_secret_count = 0;
136 unsigned int server_handshake_secret_count = 0;
137 unsigned int client_application_secret_count = 0;
138 unsigned int server_application_secret_count = 0;
140 for (token = strtok(buffer, " \n"); token != NULL;
141 token = strtok(NULL, " \n")) {
142 if (strcmp(token, "RSA") == 0) {
144 * Premaster secret. Tokens should be: 16 ASCII bytes of
145 * hex-encoded encrypted secret, then the hex-encoded pre-master
148 if (!TEST_ptr(token = strtok(NULL, " \n")))
150 if (!TEST_size_t_eq(strlen(token), 16))
152 if (!TEST_ptr(token = strtok(NULL, " \n")))
155 * We can't sensibly check the log because the premaster secret is
156 * transient, and OpenSSL doesn't keep hold of it once the master
157 * secret is generated.
159 rsa_key_exchange_count++;
160 } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
162 * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
163 * client random, then the hex-encoded master secret.
165 client_random_size = SSL_get_client_random(ssl,
166 actual_client_random,
168 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
171 if (!TEST_ptr(token = strtok(NULL, " \n")))
173 if (!TEST_size_t_eq(strlen(token), 64))
175 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
176 actual_client_random,
177 client_random_size)))
180 if (!TEST_ptr(token = strtok(NULL, " \n")))
182 master_key_size = SSL_SESSION_get_master_key(session,
185 if (!TEST_size_t_ne(master_key_size, 0))
187 if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
191 master_secret_count++;
192 } else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
193 || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
194 || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
195 || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0) {
197 * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
198 * client random, and then the hex-encoded secret. In this case,
199 * we treat all of these secrets identically and then just
200 * distinguish between them when counting what we saw.
202 if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
203 client_handshake_secret_count++;
204 else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
205 server_handshake_secret_count++;
206 else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
207 client_application_secret_count++;
208 else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
209 server_application_secret_count++;
211 client_random_size = SSL_get_client_random(ssl,
212 actual_client_random,
214 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
217 if (!TEST_ptr(token = strtok(NULL, " \n")))
219 if (!TEST_size_t_eq(strlen(token), 64))
221 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
222 actual_client_random,
223 client_random_size)))
226 if (!TEST_ptr(token = strtok(NULL, " \n")))
230 * TODO(TLS1.3): test that application traffic secrets are what
233 TEST_info("Unexpected token %s\n", token);
238 /* Got what we expected? */
239 if (!TEST_size_t_eq(rsa_key_exchange_count,
240 expected->rsa_key_exchange_count)
241 || !TEST_size_t_eq(master_secret_count,
242 expected->master_secret_count)
243 || !TEST_size_t_eq(client_handshake_secret_count,
244 expected->client_handshake_secret_count)
245 || !TEST_size_t_eq(server_handshake_secret_count,
246 expected->server_handshake_secret_count)
247 || !TEST_size_t_eq(client_application_secret_count,
248 expected->client_application_secret_count)
249 || !TEST_size_t_eq(server_application_secret_count,
250 expected->server_application_secret_count))
255 static int test_keylog(void)
257 SSL_CTX *cctx = NULL, *sctx = NULL;
258 SSL *clientssl = NULL, *serverssl = NULL;
260 struct sslapitest_log_counts expected = {0};
262 /* Clean up logging space */
263 memset(client_log_buffer, 0, sizeof(client_log_buffer));
264 memset(server_log_buffer, 0, sizeof(server_log_buffer));
265 client_log_buffer_index = 0;
266 server_log_buffer_index = 0;
267 error_writing_log = 0;
269 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
271 &sctx, &cctx, cert, privkey)))
274 /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
275 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
276 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
278 /* We also want to ensure that we use RSA-based key exchange. */
279 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
282 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
283 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
285 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
286 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
287 == client_keylog_callback))
289 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
290 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
291 == server_keylog_callback))
294 /* Now do a handshake and check that the logs have been written to. */
295 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
296 &clientssl, NULL, NULL))
297 || !TEST_true(create_ssl_connection(serverssl, clientssl,
299 || !TEST_false(error_writing_log)
300 || !TEST_int_gt(client_log_buffer_index, 0)
301 || !TEST_int_gt(server_log_buffer_index, 0))
305 * Now we want to test that our output data was vaguely sensible. We
306 * do that by using strtok and confirming that we have more or less the
307 * data we expect. For both client and server, we expect to see one master
308 * secret. The client should also see a RSA key exchange.
310 expected.rsa_key_exchange_count = 1;
311 expected.master_secret_count = 1;
312 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
313 SSL_get_session(clientssl), &expected)))
316 expected.rsa_key_exchange_count = 0;
317 if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
318 SSL_get_session(serverssl), &expected)))
332 #ifndef OPENSSL_NO_TLS1_3
333 static int test_keylog_no_master_key(void)
335 SSL_CTX *cctx = NULL, *sctx = NULL;
336 SSL *clientssl = NULL, *serverssl = NULL;
338 struct sslapitest_log_counts expected = {0};
340 /* Clean up logging space */
341 memset(client_log_buffer, 0, sizeof(client_log_buffer));
342 memset(server_log_buffer, 0, sizeof(server_log_buffer));
343 client_log_buffer_index = 0;
344 server_log_buffer_index = 0;
345 error_writing_log = 0;
347 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
348 TLS_client_method(), &sctx,
349 &cctx, cert, privkey)))
352 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
353 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
356 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
357 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
358 == client_keylog_callback))
361 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
362 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
363 == server_keylog_callback))
366 /* Now do a handshake and check that the logs have been written to. */
367 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
368 &clientssl, NULL, NULL))
369 || !TEST_true(create_ssl_connection(serverssl, clientssl,
371 || !TEST_false(error_writing_log))
375 * Now we want to test that our output data was vaguely sensible. For this
376 * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
377 * TLSv1.3, but we do expect both client and server to emit keys.
379 expected.client_handshake_secret_count = 1;
380 expected.server_handshake_secret_count = 1;
381 expected.client_application_secret_count = 1;
382 expected.server_application_secret_count = 1;
383 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
384 SSL_get_session(clientssl), &expected))
385 || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
386 SSL_get_session(serverssl),
402 #ifndef OPENSSL_NO_TLS1_2
403 static int full_early_callback(SSL *s, int *al, void *arg)
406 const unsigned char *p;
408 /* We only configure two ciphers, but the SCSV is added automatically. */
410 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
412 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
415 const int expected_extensions[] = {
416 #ifndef OPENSSL_NO_EC
422 /* Make sure we can defer processing and get called back. */
426 len = SSL_early_get0_ciphers(s, &p);
427 if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
428 || !TEST_size_t_eq(SSL_early_get0_compression_methods(s, &p), 1)
429 || !TEST_int_eq(*p, 0))
431 if (!SSL_early_get1_extensions_present(s, &exts, &len))
433 if (len != OSSL_NELEM(expected_extensions) ||
434 memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
435 printf("Early callback expected ClientHello extensions mismatch\n");
443 static int test_early_cb(void)
445 SSL_CTX *cctx = NULL, *sctx = NULL;
446 SSL *clientssl = NULL, *serverssl = NULL;
447 int testctr = 0, testresult = 0;
449 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
450 TLS_client_method(), &sctx,
451 &cctx, cert, privkey)))
453 SSL_CTX_set_early_cb(sctx, full_early_callback, &testctr);
455 /* The gimpy cipher list we configure can't do TLS 1.3. */
456 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
458 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
459 "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
460 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
461 &clientssl, NULL, NULL))
462 || !TEST_false(create_ssl_connection(serverssl, clientssl,
463 SSL_ERROR_WANT_EARLY))
465 * Passing a -1 literal is a hack since
466 * the real value was lost.
468 || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_EARLY)
469 || !TEST_true(create_ssl_connection(serverssl, clientssl,
485 static int execute_test_large_message(const SSL_METHOD *smeth,
486 const SSL_METHOD *cmeth, int read_ahead)
488 SSL_CTX *cctx = NULL, *sctx = NULL;
489 SSL *clientssl = NULL, *serverssl = NULL;
493 X509 *chaincert = NULL;
496 if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
498 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
501 if (!TEST_ptr(chaincert))
504 if (!TEST_true(create_ssl_ctx_pair(smeth, cmeth, &sctx,
505 &cctx, cert, privkey)))
510 * Test that read_ahead works correctly when dealing with large
513 SSL_CTX_set_read_ahead(cctx, 1);
517 * We assume the supplied certificate is big enough so that if we add
518 * NUM_EXTRA_CERTS it will make the overall message large enough. The
519 * default buffer size is requested to be 16k, but due to the way BUF_MEM
520 * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
521 * test we need to have a message larger than that.
523 certlen = i2d_X509(chaincert, NULL);
524 OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
525 (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
526 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
527 if (!X509_up_ref(chaincert))
529 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
530 X509_free(chaincert);
535 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
537 || !TEST_true(create_ssl_connection(serverssl, clientssl,
542 * Calling SSL_clear() first is not required but this tests that SSL_clear()
543 * doesn't leak (when using enable-crypto-mdebug).
545 if (!TEST_true(SSL_clear(serverssl)))
550 X509_free(chaincert);
559 static int test_large_message_tls(void)
561 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
565 static int test_large_message_tls_read_ahead(void)
567 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
571 #ifndef OPENSSL_NO_DTLS
572 static int test_large_message_dtls(void)
575 * read_ahead is not relevant to DTLS because DTLS always acts as if
578 return execute_test_large_message(DTLS_server_method(),
579 DTLS_client_method(), 0);
583 #ifndef OPENSSL_NO_OCSP
584 static int ocsp_server_cb(SSL *s, void *arg)
586 int *argi = (int *)arg;
587 unsigned char *copy = NULL;
588 STACK_OF(OCSP_RESPID) *ids = NULL;
589 OCSP_RESPID *id = NULL;
592 /* In this test we are expecting exactly 1 OCSP_RESPID */
593 SSL_get_tlsext_status_ids(s, &ids);
594 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
595 return SSL_TLSEXT_ERR_ALERT_FATAL;
597 id = sk_OCSP_RESPID_value(ids, 0);
598 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
599 return SSL_TLSEXT_ERR_ALERT_FATAL;
600 } else if (*argi != 1) {
601 return SSL_TLSEXT_ERR_ALERT_FATAL;
604 if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
605 return SSL_TLSEXT_ERR_ALERT_FATAL;
607 SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder));
608 ocsp_server_called = 1;
609 return SSL_TLSEXT_ERR_OK;
612 static int ocsp_client_cb(SSL *s, void *arg)
614 int *argi = (int *)arg;
615 const unsigned char *respderin;
618 if (*argi != 1 && *argi != 2)
621 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
622 if (!TEST_mem_eq(orespder, len, respderin, len))
625 ocsp_client_called = 1;
629 static int test_tlsext_status_type(void)
631 SSL_CTX *cctx = NULL, *sctx = NULL;
632 SSL *clientssl = NULL, *serverssl = NULL;
634 STACK_OF(OCSP_RESPID) *ids = NULL;
635 OCSP_RESPID *id = NULL;
638 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
639 &cctx, cert, privkey))
642 if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
645 /* First just do various checks getting and setting tlsext_status_type */
647 clientssl = SSL_new(cctx);
648 if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
649 || !TEST_true(SSL_set_tlsext_status_type(clientssl,
650 TLSEXT_STATUSTYPE_ocsp))
651 || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
652 TLSEXT_STATUSTYPE_ocsp))
658 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
659 || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
662 clientssl = SSL_new(cctx);
663 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
669 * Now actually do a handshake and check OCSP information is exchanged and
670 * the callbacks get called
672 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
673 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
674 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
675 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
676 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
677 &clientssl, NULL, NULL))
678 || !TEST_true(create_ssl_connection(serverssl, clientssl,
680 || !TEST_true(ocsp_client_called)
681 || !TEST_true(ocsp_server_called))
688 /* Try again but this time force the server side callback to fail */
689 ocsp_client_called = 0;
690 ocsp_server_called = 0;
692 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
693 &clientssl, NULL, NULL))
694 /* This should fail because the callback will fail */
695 || !TEST_false(create_ssl_connection(serverssl, clientssl,
697 || !TEST_false(ocsp_client_called)
698 || !TEST_false(ocsp_server_called))
706 * This time we'll get the client to send an OCSP_RESPID that it will
709 ocsp_client_called = 0;
710 ocsp_server_called = 0;
712 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
713 &clientssl, NULL, NULL)))
717 * We'll just use any old cert for this test - it doesn't have to be an OCSP
718 * specific one. We'll use the server cert.
720 if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
721 || !TEST_ptr(id = OCSP_RESPID_new())
722 || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
723 || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio,
725 || !TEST_true(OCSP_RESPID_set_by_key(id, ocspcert))
726 || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
729 SSL_set_tlsext_status_ids(clientssl, ids);
730 /* Control has been transferred */
736 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
738 || !TEST_true(ocsp_client_called)
739 || !TEST_true(ocsp_server_called))
749 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
750 OCSP_RESPID_free(id);
759 typedef struct ssl_session_test_fixture {
760 const char *test_case_name;
763 } SSL_SESSION_TEST_FIXTURE;
765 static int new_called = 0, remove_called = 0;
767 static SSL_SESSION_TEST_FIXTURE
768 ssl_session_set_up(const char *const test_case_name)
770 SSL_SESSION_TEST_FIXTURE fixture;
772 fixture.test_case_name = test_case_name;
773 fixture.use_ext_cache = 1;
774 fixture.use_int_cache = 1;
776 new_called = remove_called = 0;
781 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
785 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
791 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
796 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
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 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
808 TLS_client_method(), &sctx,
809 &cctx, cert, privkey)))
812 #ifndef OPENSSL_NO_TLS1_2
813 /* Only allow TLS1.2 so we can force a connection failure later */
814 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
817 /* Set up session cache */
818 if (fix.use_ext_cache) {
819 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
820 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
822 if (fix.use_int_cache) {
823 /* Also covers instance where both are set */
824 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
826 SSL_CTX_set_session_cache_mode(cctx,
827 SSL_SESS_CACHE_CLIENT
828 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
831 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
833 || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
835 || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
838 /* Should fail because it should already be in the cache */
839 if (fix.use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
841 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0))
844 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
845 &clientssl2, NULL, NULL))
846 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
850 if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
853 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0))
857 * This should clear sess2 from the cache because it is a "bad" session.
858 * See SSL_set_session() documentation.
860 if (!TEST_true(SSL_set_session(clientssl2, sess1)))
862 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1))
864 if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
867 if (fix.use_int_cache) {
868 /* Should succeeded because it should not already be in the cache */
869 if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
870 || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
874 * This is for the purposes of internal cache testing...ignore the
875 * counter for external cache
877 if (fix.use_ext_cache)
881 /* This shouldn't be in the cache so should fail */
882 if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
885 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2))
888 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
889 /* Force a connection failure */
890 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
891 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
892 &clientssl3, NULL, NULL))
893 || !TEST_true(SSL_set_session(clientssl3, sess1))
894 /* This should fail because of the mismatched protocol versions */
895 || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
899 /* We should have automatically removed the session from the cache */
900 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3))
903 /* Should succeed because it should not already be in the cache */
904 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2))
911 SSL_free(serverssl1);
912 SSL_free(clientssl1);
913 SSL_free(serverssl2);
914 SSL_free(clientssl2);
915 #ifndef OPENSSL_NO_TLS1_1
916 SSL_free(serverssl3);
917 SSL_free(clientssl3);
919 SSL_SESSION_free(sess1);
920 SSL_SESSION_free(sess2);
923 * Check if we need to remove any sessions up-refed for the external cache
926 SSL_SESSION_free(sess1);
928 SSL_SESSION_free(sess2);
935 static int test_session_with_only_int_cache(void)
937 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
938 fixture.use_ext_cache = 0;
939 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
942 static int test_session_with_only_ext_cache(void)
944 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
945 fixture.use_int_cache = 0;
946 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
949 static int test_session_with_both_cache(void)
951 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
952 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
959 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
961 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
976 static int test_ssl_set_bio(int idx)
981 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
983 int initrbio, initwbio, newrbio, newwbio;
993 if (!TEST_int_le(newwbio, 2))
996 if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))
997 || !TEST_ptr(ssl = SSL_new(ctx)))
1000 if (initrbio == USE_BIO_1
1001 || initwbio == USE_BIO_1
1002 || newrbio == USE_BIO_1
1003 || newwbio == USE_BIO_1) {
1004 if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
1008 if (initrbio == USE_BIO_2
1009 || initwbio == USE_BIO_2
1010 || newrbio == USE_BIO_2
1011 || newwbio == USE_BIO_2) {
1012 if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
1016 setupbio(&irbio, bio1, bio2, initrbio);
1017 setupbio(&iwbio, bio1, bio2, initwbio);
1020 * We want to maintain our own refs to these BIO, so do an up ref for each
1021 * BIO that will have ownership transferred in the SSL_set_bio() call
1025 if (iwbio != NULL && iwbio != irbio)
1028 SSL_set_bio(ssl, irbio, iwbio);
1030 setupbio(&nrbio, bio1, bio2, newrbio);
1031 setupbio(&nwbio, bio1, bio2, newwbio);
1034 * We will (maybe) transfer ownership again so do more up refs.
1035 * SSL_set_bio() has some really complicated ownership rules where BIOs have
1040 && (nwbio != iwbio || nrbio != nwbio))
1044 && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
1047 SSL_set_bio(ssl, nrbio, nwbio);
1057 * This test is checking that the ref counting for SSL_set_bio is correct.
1058 * If we get here and we did too many frees then we will fail in the above
1059 * functions. If we haven't done enough then this will only be detected in
1060 * a crypto-mdebug build
1066 typedef struct ssl_bio_test_fixture {
1067 const char *test_case_name;
1069 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
1070 } SSL_BIO_TEST_FIXTURE;
1072 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
1074 SSL_BIO_TEST_FIXTURE fixture;
1076 fixture.test_case_name = test_case_name;
1077 fixture.pop_ssl = 0;
1078 fixture.change_bio = NO_BIO_CHANGE;
1082 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
1086 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
1088 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
1093 if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))
1094 || !TEST_ptr(ssl = SSL_new(ctx))
1095 || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
1096 || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
1099 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
1102 * If anything goes wrong here then we could leak memory, so this will
1103 * be caught in a crypto-mdebug build
1105 BIO_push(sslbio, membio1);
1107 /* Verify changing the rbio/wbio directly does not cause leaks */
1108 if (fix.change_bio != NO_BIO_CHANGE) {
1109 if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem())))
1111 if (fix.change_bio == CHANGE_RBIO)
1112 SSL_set0_rbio(ssl, membio2);
1114 SSL_set0_wbio(ssl, membio2);
1133 static int test_ssl_bio_pop_next_bio(void)
1135 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1136 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1139 static int test_ssl_bio_pop_ssl_bio(void)
1141 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1142 fixture.pop_ssl = 1;
1143 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1146 static int test_ssl_bio_change_rbio(void)
1148 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1149 fixture.change_bio = CHANGE_RBIO;
1150 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1153 static int test_ssl_bio_change_wbio(void)
1155 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1156 fixture.change_bio = CHANGE_WBIO;
1157 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1161 /* The list of sig algs */
1163 /* The length of the list */
1165 /* A sigalgs list in string format */
1166 const char *liststr;
1167 /* Whether setting the list should succeed */
1169 /* Whether creating a connection with the list should succeed */
1173 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
1174 #ifndef OPENSSL_NO_EC
1175 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
1176 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
1178 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
1179 static const int invalidlist2[] = {NID_sha256, NID_undef};
1180 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
1181 static const int invalidlist4[] = {NID_sha256};
1182 static const sigalgs_list testsigalgs[] = {
1183 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
1184 #ifndef OPENSSL_NO_EC
1185 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
1186 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
1188 {NULL, 0, "RSA+SHA256", 1, 1},
1189 #ifndef OPENSSL_NO_EC
1190 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
1191 {NULL, 0, "ECDSA+SHA512", 1, 0},
1193 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
1194 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
1195 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
1196 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
1197 {NULL, 0, "RSA", 0, 0},
1198 {NULL, 0, "SHA256", 0, 0},
1199 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
1200 {NULL, 0, "Invalid", 0, 0}
1203 static int test_set_sigalgs(int idx)
1205 SSL_CTX *cctx = NULL, *sctx = NULL;
1206 SSL *clientssl = NULL, *serverssl = NULL;
1208 const sigalgs_list *curr;
1211 /* Should never happen */
1212 if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
1215 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
1216 curr = testctx ? &testsigalgs[idx]
1217 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
1219 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
1220 TLS_client_method(), &sctx,
1221 &cctx, cert, privkey)))
1225 * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
1226 * for TLSv1.2 for now until we add a new API.
1228 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
1233 if (curr->list != NULL)
1234 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
1236 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
1240 TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
1246 TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
1251 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1252 &clientssl, NULL, NULL)))
1258 if (curr->list != NULL)
1259 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
1261 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
1264 TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
1273 if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
1281 SSL_free(serverssl);
1282 SSL_free(clientssl);
1289 #ifndef OPENSSL_NO_TLS1_3
1291 #define MSG1 "Hello"
1292 #define MSG2 "World."
1297 #define MSG7 "message."
1300 * Helper method to setup objects for early data test. Caller frees objects on
1303 static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
1304 SSL **serverssl, SSL_SESSION **sess, int idx)
1306 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
1307 TLS_client_method(), sctx,
1308 cctx, cert, privkey)))
1311 /* When idx == 1 we repeat the tests with read_ahead set */
1313 SSL_CTX_set_read_ahead(*cctx, 1);
1314 SSL_CTX_set_read_ahead(*sctx, 1);
1317 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
1319 || !TEST_true(create_ssl_connection(*serverssl, *clientssl,
1323 *sess = SSL_get1_session(*clientssl);
1324 SSL_shutdown(*clientssl);
1325 SSL_shutdown(*serverssl);
1326 SSL_free(*serverssl);
1327 SSL_free(*clientssl);
1328 *serverssl = *clientssl = NULL;
1330 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
1331 clientssl, NULL, NULL))
1332 || !TEST_true(SSL_set_session(*clientssl, *sess)))
1338 static int test_early_data_read_write(int idx)
1340 SSL_CTX *cctx = NULL, *sctx = NULL;
1341 SSL *clientssl = NULL, *serverssl = NULL;
1343 SSL_SESSION *sess = NULL;
1344 unsigned char buf[20], data[1024];
1345 size_t readbytes, written, eoedlen, rawread, rawwritten;
1348 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
1349 &serverssl, &sess, idx)))
1352 /* Write and read some early data */
1353 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
1355 || !TEST_size_t_eq(written, strlen(MSG1))
1356 || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
1357 sizeof(buf), &readbytes),
1358 SSL_READ_EARLY_DATA_SUCCESS)
1359 || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
1360 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
1361 SSL_EARLY_DATA_ACCEPTED))
1365 * Server should be able to write data, and client should be able to
1368 if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
1370 || !TEST_size_t_eq(written, strlen(MSG2))
1371 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1372 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
1375 /* Even after reading normal data, client should be able write early data */
1376 if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
1378 || !TEST_size_t_eq(written, strlen(MSG3)))
1381 /* Server should still be able read early data after writing data */
1382 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1384 SSL_READ_EARLY_DATA_SUCCESS)
1385 || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
1388 /* Write more data from server and read it from client */
1389 if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
1391 || !TEST_size_t_eq(written, strlen(MSG4))
1392 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1393 || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
1397 * If client writes normal data it should mean writing early data is no
1400 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
1401 || !TEST_size_t_eq(written, strlen(MSG5))
1402 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
1403 SSL_EARLY_DATA_ACCEPTED))
1407 * At this point the client has written EndOfEarlyData, ClientFinished and
1408 * normal (fully protected) data. We are going to cause a delay between the
1409 * arrival of EndOfEarlyData and ClientFinished. We read out all the data
1410 * in the read BIO, and then just put back the EndOfEarlyData message.
1412 rbio = SSL_get_rbio(serverssl);
1413 if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
1414 || !TEST_size_t_lt(rawread, sizeof(data))
1415 || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
1418 /* Record length is in the 4th and 5th bytes of the record header */
1419 eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
1420 if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
1421 || !TEST_size_t_eq(rawwritten, eoedlen))
1424 /* Server should be told that there is no more early data */
1425 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1427 SSL_READ_EARLY_DATA_FINISH)
1428 || !TEST_size_t_eq(readbytes, 0))
1432 * Server has not finished init yet, so should still be able to write early
1435 if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
1437 || !TEST_size_t_eq(written, strlen(MSG6)))
1440 /* Push the ClientFinished and the normal data back into the server rbio */
1441 if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
1443 || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
1446 /* Server should be able to read normal data */
1447 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
1448 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
1451 /* Client and server should not be able to write/read early data now */
1452 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
1456 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1458 SSL_READ_EARLY_DATA_ERROR))
1462 /* Client should be able to read the data sent by the server */
1463 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1464 || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
1468 * Make sure we process the NewSessionTicket. This arrives post-handshake.
1469 * We attempt a read which we do not expect to return any data.
1471 if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)))
1474 /* Server should be able to write normal data */
1475 if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
1476 || !TEST_size_t_eq(written, strlen(MSG7))
1477 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1478 || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
1481 SSL_SESSION_free(sess);
1482 sess = SSL_get1_session(clientssl);
1484 SSL_shutdown(clientssl);
1485 SSL_shutdown(serverssl);
1486 SSL_free(serverssl);
1487 SSL_free(clientssl);
1488 serverssl = clientssl = NULL;
1489 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1490 &clientssl, NULL, NULL))
1491 || !TEST_true(SSL_set_session(clientssl, sess)))
1494 /* Write and read some early data */
1495 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
1497 || !TEST_size_t_eq(written, strlen(MSG1))
1498 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1500 SSL_READ_EARLY_DATA_SUCCESS)
1501 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
1504 if (!TEST_int_gt(SSL_connect(clientssl), 0)
1505 || !TEST_int_gt(SSL_accept(serverssl), 0))
1508 /* Client and server should not be able to write/read early data now */
1509 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
1513 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1515 SSL_READ_EARLY_DATA_ERROR))
1519 /* Client and server should be able to write/read normal data */
1520 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
1521 || !TEST_size_t_eq(written, strlen(MSG5))
1522 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
1523 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
1529 SSL_SESSION_free(sess);
1530 SSL_free(serverssl);
1531 SSL_free(clientssl);
1538 * Test that a server attempting to read early data can handle a connection
1539 * from a client where the early data is not acceptable.
1541 static int test_early_data_skip(int idx)
1543 SSL_CTX *cctx = NULL, *sctx = NULL;
1544 SSL *clientssl = NULL, *serverssl = NULL;
1546 SSL_SESSION *sess = NULL;
1547 unsigned char buf[20];
1548 size_t readbytes, written;
1550 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
1551 &serverssl, &sess, idx)))
1555 * Deliberately corrupt the creation time. We take 20 seconds off the time.
1556 * It could be any value as long as it is not within tolerance. This should
1557 * mean the ticket is rejected.
1559 if (!TEST_true(SSL_SESSION_set_time(sess, time(NULL) - 20)))
1562 /* Write some early data */
1563 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
1565 || !TEST_size_t_eq(written, strlen(MSG1)))
1568 /* Server should reject the early data and skip over it */
1569 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1571 SSL_READ_EARLY_DATA_FINISH)
1572 || !TEST_size_t_eq(readbytes, 0)
1573 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
1574 SSL_EARLY_DATA_REJECTED))
1577 /* Should be able to send normal data despite rejection of early data */
1578 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
1579 || !TEST_size_t_eq(written, strlen(MSG2))
1580 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
1581 SSL_EARLY_DATA_REJECTED)
1582 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
1583 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
1589 SSL_SESSION_free(sess);
1590 SSL_free(serverssl);
1591 SSL_free(clientssl);
1598 * Test that a server attempting to read early data can handle a connection
1599 * from a client that doesn't send any.
1601 static int test_early_data_not_sent(int idx)
1603 SSL_CTX *cctx = NULL, *sctx = NULL;
1604 SSL *clientssl = NULL, *serverssl = NULL;
1606 SSL_SESSION *sess = NULL;
1607 unsigned char buf[20];
1608 size_t readbytes, written;
1610 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
1611 &serverssl, &sess, idx)))
1614 /* Write some data - should block due to handshake with server */
1615 SSL_set_connect_state(clientssl);
1616 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
1619 /* Server should detect that early data has not been sent */
1620 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1622 SSL_READ_EARLY_DATA_FINISH)
1623 || !TEST_size_t_eq(readbytes, 0)
1624 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
1625 SSL_EARLY_DATA_NOT_SENT)
1626 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
1627 SSL_EARLY_DATA_NOT_SENT))
1630 /* Continue writing the message we started earlier */
1631 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
1632 || !TEST_size_t_eq(written, strlen(MSG1))
1633 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
1634 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
1635 || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
1636 || !TEST_size_t_eq(written, strlen(MSG2)))
1640 * Should block due to the NewSessionTicket arrival unless we're using
1644 if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)))
1648 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
1649 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
1655 SSL_SESSION_free(sess);
1656 SSL_free(serverssl);
1657 SSL_free(clientssl);
1664 * Test that a server that doesn't try to read early data can handle a
1665 * client sending some.
1667 static int test_early_data_not_expected(int idx)
1669 SSL_CTX *cctx = NULL, *sctx = NULL;
1670 SSL *clientssl = NULL, *serverssl = NULL;
1672 SSL_SESSION *sess = NULL;
1673 unsigned char buf[20];
1674 size_t readbytes, written;
1677 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
1678 &serverssl, &sess, idx)))
1681 /* Write some early data */
1682 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
1687 * Server should skip over early data and then block waiting for client to
1688 * continue handshake
1690 if (!TEST_int_le(SSL_accept(serverssl), 0)
1691 || !TEST_int_gt(SSL_connect(clientssl), 0)
1692 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
1693 SSL_EARLY_DATA_REJECTED)
1694 || !TEST_int_gt(SSL_accept(serverssl), 0)
1695 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
1696 SSL_EARLY_DATA_REJECTED))
1699 /* Send some normal data from client to server */
1700 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
1701 || !TEST_size_t_eq(written, strlen(MSG2)))
1704 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
1705 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
1711 SSL_SESSION_free(sess);
1712 SSL_free(serverssl);
1713 SSL_free(clientssl);
1720 # ifndef OPENSSL_NO_TLS1_2
1722 * Test that a server attempting to read early data can handle a connection
1723 * from a TLSv1.2 client.
1725 static int test_early_data_tls1_2(int idx)
1727 SSL_CTX *cctx = NULL, *sctx = NULL;
1728 SSL *clientssl = NULL, *serverssl = NULL;
1730 unsigned char buf[20];
1731 size_t readbytes, written;
1733 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
1734 TLS_client_method(), &sctx,
1735 &cctx, cert, privkey)))
1738 /* When idx == 1 we repeat the tests with read_ahead set */
1740 SSL_CTX_set_read_ahead(cctx, 1);
1741 SSL_CTX_set_read_ahead(sctx, 1);
1744 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1745 &clientssl, NULL, NULL)))
1748 /* Write some data - should block due to handshake with server */
1749 SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
1750 SSL_set_connect_state(clientssl);
1751 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
1755 * Server should do TLSv1.2 handshake. First it will block waiting for more
1756 * messages from client after ServerDone. Then SSL_read_early_data should
1757 * finish and detect that early data has not been sent
1759 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1761 SSL_READ_EARLY_DATA_ERROR))
1765 * Continue writing the message we started earlier. Will still block waiting
1766 * for the CCS/Finished from server
1768 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
1769 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
1771 SSL_READ_EARLY_DATA_FINISH)
1772 || !TEST_size_t_eq(readbytes, 0)
1773 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
1774 SSL_EARLY_DATA_NOT_SENT))
1777 /* Continue writing the message we started earlier */
1778 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
1779 || !TEST_size_t_eq(written, strlen(MSG1))
1780 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
1781 SSL_EARLY_DATA_NOT_SENT)
1782 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
1783 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
1784 || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
1785 || !TEST_size_t_eq(written, strlen(MSG2))
1786 || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
1787 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
1793 SSL_free(serverssl);
1794 SSL_free(clientssl);
1803 static int clntaddoldcb = 0;
1804 static int clntparseoldcb = 0;
1805 static int srvaddoldcb = 0;
1806 static int srvparseoldcb = 0;
1807 static int clntaddnewcb = 0;
1808 static int clntparsenewcb = 0;
1809 static int srvaddnewcb = 0;
1810 static int srvparsenewcb = 0;
1811 static int snicb = 0;
1813 #define TEST_EXT_TYPE1 0xff00
1815 static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
1816 size_t *outlen, int *al, void *add_arg)
1818 int *server = (int *)add_arg;
1819 unsigned char *data;
1821 if (SSL_is_server(s))
1826 if (*server != SSL_is_server(s)
1827 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
1832 *outlen = sizeof(char);
1836 static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
1839 OPENSSL_free((unsigned char *)out);
1842 static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
1843 size_t inlen, int *al, void *parse_arg)
1845 int *server = (int *)parse_arg;
1847 if (SSL_is_server(s))
1852 if (*server != SSL_is_server(s)
1853 || inlen != sizeof(char)
1860 static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
1861 const unsigned char **out, size_t *outlen, X509 *x,
1862 size_t chainidx, int *al, void *add_arg)
1864 int *server = (int *)add_arg;
1865 unsigned char *data;
1867 if (SSL_is_server(s))
1872 if (*server != SSL_is_server(s)
1873 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
1878 *outlen = sizeof(*data);
1882 static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
1883 const unsigned char *out, void *add_arg)
1885 OPENSSL_free((unsigned char *)out);
1888 static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
1889 const unsigned char *in, size_t inlen, X509 *x,
1890 size_t chainidx, int *al, void *parse_arg)
1892 int *server = (int *)parse_arg;
1894 if (SSL_is_server(s))
1899 if (*server != SSL_is_server(s)
1900 || inlen != sizeof(char) || *in != 1)
1906 static int sni_cb(SSL *s, int *al, void *arg)
1908 SSL_CTX *ctx = (SSL_CTX *)arg;
1910 if (SSL_set_SSL_CTX(s, ctx) == NULL) {
1911 *al = SSL_AD_INTERNAL_ERROR;
1912 return SSL_TLSEXT_ERR_ALERT_FATAL;
1915 return SSL_TLSEXT_ERR_OK;
1919 * Custom call back tests.
1920 * Test 0: Old style callbacks in TLSv1.2
1921 * Test 1: New style callbacks in TLSv1.2
1922 * Test 2: New style callbacks in TLSv1.2 with SNI
1923 * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
1924 * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
1926 static int test_custom_exts(int tst)
1928 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
1929 SSL *clientssl = NULL, *serverssl = NULL;
1931 static int server = 1;
1932 static int client = 0;
1933 SSL_SESSION *sess = NULL;
1934 unsigned int context;
1936 /* Reset callback counters */
1937 clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
1938 clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
1941 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
1942 TLS_client_method(), &sctx,
1943 &cctx, cert, privkey)))
1947 && !TEST_true(create_ssl_ctx_pair(TLS_server_method(), NULL, &sctx2,
1948 NULL, cert, privkey)))
1953 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
1954 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
1956 SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
1960 context = SSL_EXT_CLIENT_HELLO
1961 | SSL_EXT_TLS1_2_SERVER_HELLO
1962 | SSL_EXT_TLS1_3_SERVER_HELLO
1963 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
1964 | SSL_EXT_TLS1_3_CERTIFICATE
1965 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
1967 context = SSL_EXT_CLIENT_HELLO
1968 | SSL_EXT_TLS1_2_SERVER_HELLO
1969 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
1972 /* Create a client side custom extension */
1974 if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
1975 old_add_cb, old_free_cb,
1976 &client, old_parse_cb,
1980 if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
1981 new_add_cb, new_free_cb,
1982 &client, new_parse_cb, &client)))
1986 /* Should not be able to add duplicates */
1987 if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
1988 old_add_cb, old_free_cb,
1989 &client, old_parse_cb,
1991 || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
1992 context, new_add_cb,
1993 new_free_cb, &client,
1994 new_parse_cb, &client)))
1997 /* Create a server side custom extension */
1999 if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
2000 old_add_cb, old_free_cb,
2001 &server, old_parse_cb,
2005 if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
2006 new_add_cb, new_free_cb,
2007 &server, new_parse_cb, &server)))
2010 && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
2011 context, new_add_cb,
2012 new_free_cb, &server,
2013 new_parse_cb, &server)))
2017 /* Should not be able to add duplicates */
2018 if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
2019 old_add_cb, old_free_cb,
2020 &server, old_parse_cb,
2022 || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
2023 context, new_add_cb,
2024 new_free_cb, &server,
2025 new_parse_cb, &server)))
2030 if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
2031 || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
2035 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2036 &clientssl, NULL, NULL))
2037 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2042 if (clntaddoldcb != 1
2043 || clntparseoldcb != 1
2045 || srvparseoldcb != 1)
2047 } else if (tst == 1 || tst == 2 || tst == 3) {
2048 if (clntaddnewcb != 1
2049 || clntparsenewcb != 1
2051 || srvparsenewcb != 1
2052 || (tst != 2 && snicb != 0)
2053 || (tst == 2 && snicb != 1))
2056 if (clntaddnewcb != 1
2057 || clntparsenewcb != 4
2059 || srvparsenewcb != 1)
2063 sess = SSL_get1_session(clientssl);
2064 SSL_shutdown(clientssl);
2065 SSL_shutdown(serverssl);
2066 SSL_free(serverssl);
2067 SSL_free(clientssl);
2068 serverssl = clientssl = NULL;
2071 /* We don't bother with the resumption aspects for this test */
2076 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2078 || !TEST_true(SSL_set_session(clientssl, sess))
2079 || !TEST_true(create_ssl_connection(serverssl, clientssl,
2084 * For a resumed session we expect to add the ClientHello extension. For the
2085 * old style callbacks we ignore it on the server side because they set
2086 * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
2090 if (clntaddoldcb != 2
2091 || clntparseoldcb != 1
2093 || srvparseoldcb != 1)
2095 } else if (tst == 1 || tst == 2 || tst == 3) {
2096 if (clntaddnewcb != 2
2097 || clntparsenewcb != 2
2099 || srvparsenewcb != 2)
2102 /* No Certificate message extensions in the resumption handshake */
2103 if (clntaddnewcb != 2
2104 || clntparsenewcb != 7
2106 || srvparsenewcb != 2)
2113 SSL_SESSION_free(sess);
2114 SSL_free(serverssl);
2115 SSL_free(clientssl);
2116 SSL_CTX_free(sctx2);
2123 * Test loading of serverinfo data in various formats. test_sslmessages actually
2124 * tests to make sure the extensions appear in the handshake
2126 static int test_serverinfo(int tst)
2128 unsigned int version;
2129 unsigned char *sibuf;
2131 int ret, expected, testresult = 0;
2134 ctx = SSL_CTX_new(TLS_method());
2138 if ((tst & 0x01) == 0x01)
2139 version = SSL_SERVERINFOV2;
2141 version = SSL_SERVERINFOV1;
2143 if ((tst & 0x02) == 0x02) {
2144 sibuf = serverinfov2;
2145 sibuflen = sizeof(serverinfov2);
2146 expected = (version == SSL_SERVERINFOV2);
2148 sibuf = serverinfov1;
2149 sibuflen = sizeof(serverinfov1);
2150 expected = (version == SSL_SERVERINFOV1);
2153 if ((tst & 0x04) == 0x04) {
2154 ret = SSL_CTX_use_serverinfo_ex(ctx, version, sibuf, sibuflen);
2156 ret = SSL_CTX_use_serverinfo(ctx, sibuf, sibuflen);
2159 * The version variable is irrelevant in this case - it's what is in the
2160 * buffer that matters
2162 if ((tst & 0x02) == 0x02)
2168 if (!TEST_true(ret == expected))
2179 int test_main(int argc, char *argv[])
2184 TEST_error("Wrong argument count");
2191 ADD_TEST(test_large_message_tls);
2192 ADD_TEST(test_large_message_tls_read_ahead);
2193 #ifndef OPENSSL_NO_DTLS
2194 ADD_TEST(test_large_message_dtls);
2196 #ifndef OPENSSL_NO_OCSP
2197 ADD_TEST(test_tlsext_status_type);
2199 ADD_TEST(test_session_with_only_int_cache);
2200 ADD_TEST(test_session_with_only_ext_cache);
2201 ADD_TEST(test_session_with_both_cache);
2202 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
2203 ADD_TEST(test_ssl_bio_pop_next_bio);
2204 ADD_TEST(test_ssl_bio_pop_ssl_bio);
2205 ADD_TEST(test_ssl_bio_change_rbio);
2206 ADD_TEST(test_ssl_bio_change_wbio);
2207 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
2208 ADD_TEST(test_keylog);
2209 #ifndef OPENSSL_NO_TLS1_3
2210 ADD_TEST(test_keylog_no_master_key);
2212 #ifndef OPENSSL_NO_TLS1_2
2213 ADD_TEST(test_early_cb);
2215 #ifndef OPENSSL_NO_TLS1_3
2216 ADD_ALL_TESTS(test_early_data_read_write, 2);
2217 ADD_ALL_TESTS(test_early_data_skip, 2);
2218 ADD_ALL_TESTS(test_early_data_not_sent, 2);
2219 ADD_ALL_TESTS(test_early_data_not_expected, 2);
2220 # ifndef OPENSSL_NO_TLS1_2
2221 ADD_ALL_TESTS(test_early_data_tls1_2, 2);
2224 #ifndef OPENSSL_NO_TLS1_3
2225 ADD_ALL_TESTS(test_custom_exts, 5);
2227 ADD_ALL_TESTS(test_custom_exts, 3);
2229 ADD_ALL_TESTS(test_serverinfo, 8);
2231 testresult = run_tests(argv[0]);
2233 bio_s_mempacket_test_free();