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"
20 #include "test_main_custom.h"
23 static char *cert = NULL;
24 static char *privkey = NULL;
26 #define LOG_BUFFER_SIZE 1024
27 static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
28 static int server_log_buffer_index = 0;
29 static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
30 static int client_log_buffer_index = 0;
31 static int error_writing_log = 0;
33 #ifndef OPENSSL_NO_OCSP
34 static const unsigned char orespder[] = "Dummy OCSP Response";
35 static int ocsp_server_called = 0;
36 static int ocsp_client_called = 0;
38 static int cdummyarg = 1;
39 static X509 *ocspcert = NULL;
42 #define NUM_EXTRA_CERTS 40
44 static void client_keylog_callback(const SSL *ssl, const char *line) {
45 int line_length = strlen(line);
47 /* If the log doesn't fit, error out. */
48 if ((client_log_buffer_index + line_length) > LOG_BUFFER_SIZE) {
49 printf("No room in client log\n");
50 error_writing_log = 1;
54 strcat(client_log_buffer, line);
55 client_log_buffer_index += line_length;
56 client_log_buffer[client_log_buffer_index] = '\n';
57 client_log_buffer_index += 1;
62 static void server_keylog_callback(const SSL *ssl, const char *line) {
63 int line_length = strlen(line);
65 /* If the log doesn't fit, error out. */
66 if ((server_log_buffer_index + line_length) > LOG_BUFFER_SIZE) {
67 printf("No room in server log\n");
68 error_writing_log = 1;
72 strcat(server_log_buffer, line);
73 server_log_buffer_index += line_length;
74 server_log_buffer[server_log_buffer_index] = '\n';
75 server_log_buffer_index += 1;
80 static int compare_hex_encoded_buffer(const char *hex_encoded,
87 /* One byte too big, just to be safe. */
90 if ((raw_length * 2) != hex_length) {
91 printf("Inconsistent hex encoded lengths.\n");
95 for (i = j = 0; (i < raw_length) && ((j + 1) < hex_length); i++) {
96 sprintf(hexed, "%02x", raw[i]);
97 if ((hexed[0] != hex_encoded[j]) || (hexed[1] != hex_encoded[j + 1])) {
98 printf("Hex output does not match.\n");
107 static int test_keylog_output(char *buffer, const SSL *ssl,
108 const SSL_SESSION *session) {
109 int saw_client_random = 0;
111 unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
112 size_t client_random_size = SSL3_RANDOM_SIZE;
113 unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
114 size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
116 token = strtok(buffer, " \n");
118 if (strcmp(token, "RSA") == 0) {
119 /* Premaster secret. Tokens should be: 16 ASCII bytes of
120 * hex-encoded encrypted secret, then the hex-encoded pre-master
123 token = strtok(NULL, " \n");
125 printf("Unexpectedly short premaster secret log.\n");
128 if (strlen(token) != 16) {
129 printf("Bad value for encrypted secret: %s\n", token);
132 token = strtok(NULL, " \n");
134 printf("Unexpectedly short premaster secret log.\n");
137 /* TODO: Can I check this sensibly? */
138 } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
139 /* Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
140 * client random, then the hex-encoded master secret.
142 client_random_size = SSL_get_client_random(ssl,
143 actual_client_random,
145 if (client_random_size != SSL3_RANDOM_SIZE) {
146 printf("Unexpected short client random.\n");
150 token = strtok(NULL, " \n");
152 printf("Unexpected short master secret log.\n");
155 if (strlen(token) != 64) {
156 printf("Bad value for client random: %s\n", token);
159 if (compare_hex_encoded_buffer(token, 64, actual_client_random,
160 client_random_size)) {
161 printf("Bad value for client random: %s\n", token);
165 token = strtok(NULL, " \n");
167 printf("Unexpectedly short master secret log.\n");
171 master_key_size = SSL_SESSION_get_master_key(session,
174 if (!master_key_size) {
175 printf("Error getting master key to compare.\n");
178 if (compare_hex_encoded_buffer(token, strlen(token),
181 printf("Bad value for master key: %s\n", token);
185 saw_client_random = 1;
187 printf("Unexpected token in buffer: %s\n", token);
191 token = strtok(NULL, " \n");
194 return saw_client_random;
197 static int test_keylog(void) {
198 SSL_CTX *cctx = NULL, *sctx = NULL;
199 SSL *clientssl = NULL, *serverssl = NULL;
202 /* Clean up logging space */
203 memset(client_log_buffer, 0, LOG_BUFFER_SIZE + 1);
204 memset(server_log_buffer, 0, LOG_BUFFER_SIZE + 1);
205 client_log_buffer_index = 0;
206 server_log_buffer_index = 0;
207 error_writing_log = 0;
209 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
210 &cctx, cert, privkey)) {
211 printf("Unable to create SSL_CTX pair\n");
215 /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
216 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
217 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
219 if (SSL_CTX_get_keylog_callback(cctx)) {
220 printf("Unexpected initial value for client "
221 "SSL_CTX_get_keylog_callback()\n");
224 if (SSL_CTX_get_keylog_callback(sctx)) {
225 printf("Unexpected initial value for server "
226 "SSL_CTX_get_keylog_callback()\n");
230 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
231 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
233 if (SSL_CTX_get_keylog_callback(cctx) != client_keylog_callback) {
234 printf("Unexpected set value for client "
235 "SSL_CTX_get_keylog_callback()\n");
238 if (SSL_CTX_get_keylog_callback(sctx) != server_keylog_callback) {
239 printf("Unexpected set value for server "
240 "SSL_CTX_get_keylog_callback()\n");
243 /* Now do a handshake and check that the logs have been written to. */
244 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
245 printf("Unable to create SSL objects\n");
249 if (!create_ssl_connection(serverssl, clientssl)) {
250 printf("Unable to create SSL connection\n");
254 if (error_writing_log) {
255 printf("Error encountered while logging\n");
259 if ((client_log_buffer_index == 0) || (server_log_buffer_index == 0)) {
260 printf("No logs written\n");
264 /* Now we want to test that our output data was vaguely sensible. We
265 * do that by using strtok and confirming that we have more or less the
268 if (test_keylog_output(client_log_buffer, clientssl,
269 SSL_get_session(clientssl)) != 1) {
270 printf("Error encountered in client log buffer\n");
273 if (test_keylog_output(server_log_buffer, serverssl,
274 SSL_get_session(serverssl)) != 1) {
275 printf("Error encountered in server log buffer\n");
290 #ifndef OPENSSL_NO_TLS1_3
291 static int test_keylog_no_master_key(void) {
292 SSL_CTX *cctx = NULL, *sctx = NULL;
293 SSL *clientssl = NULL, *serverssl = NULL;
296 /* Clean up logging space */
297 memset(client_log_buffer, 0, LOG_BUFFER_SIZE + 1);
298 memset(server_log_buffer, 0, LOG_BUFFER_SIZE + 1);
299 client_log_buffer_index = 0;
300 server_log_buffer_index = 0;
301 error_writing_log = 0;
303 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
304 &cctx, cert, privkey)) {
305 printf("Unable to create SSL_CTX pair\n");
309 if (SSL_CTX_get_keylog_callback(cctx)) {
310 printf("Unexpected initial value for client "
311 "SSL_CTX_get_keylog_callback()\n");
314 if (SSL_CTX_get_keylog_callback(sctx)) {
315 printf("Unexpected initial value for server "
316 "SSL_CTX_get_keylog_callback()\n");
320 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
321 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
323 if (SSL_CTX_get_keylog_callback(cctx) != client_keylog_callback) {
324 printf("Unexpected set value for client "
325 "SSL_CTX_get_keylog_callback()\n");
328 if (SSL_CTX_get_keylog_callback(sctx) != server_keylog_callback) {
329 printf("Unexpected set value for server "
330 "SSL_CTX_get_keylog_callback()\n");
333 /* Now do a handshake and check that the logs have been written to. */
334 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
335 printf("Unable to create SSL objects\n");
339 if (!create_ssl_connection(serverssl, clientssl)) {
340 printf("Unable to create SSL connection\n");
344 if (error_writing_log) {
345 printf("Error encountered while logging\n");
349 /* Now we want to test that our output data was vaguely sensible. For this
350 * test, we expect no CLIENT_RANDOM entry.
352 if (test_keylog_output(client_log_buffer, clientssl,
353 SSL_get_session(clientssl)) != 0) {
354 printf("Error encountered in client log buffer\n");
357 if (test_keylog_output(server_log_buffer, serverssl,
358 SSL_get_session(serverssl)) != 0) {
359 printf("Error encountered in server log buffer\n");
375 static int execute_test_large_message(const SSL_METHOD *smeth,
376 const SSL_METHOD *cmeth, int read_ahead)
378 SSL_CTX *cctx = NULL, *sctx = NULL;
379 SSL *clientssl = NULL, *serverssl = NULL;
382 BIO *certbio = BIO_new_file(cert, "r");
383 X509 *chaincert = NULL;
386 if (certbio == NULL) {
387 printf("Can't load the certficate file\n");
390 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
393 if (chaincert == NULL) {
394 printf("Unable to load certificate for chain\n");
398 if (!create_ssl_ctx_pair(smeth, cmeth, &sctx,
399 &cctx, cert, privkey)) {
400 printf("Unable to create SSL_CTX pair\n");
406 * Test that read_ahead works correctly when dealing with large
409 SSL_CTX_set_read_ahead(cctx, 1);
413 * We assume the supplied certificate is big enough so that if we add
414 * NUM_EXTRA_CERTS it will make the overall message large enough. The
415 * default buffer size is requested to be 16k, but due to the way BUF_MEM
416 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
417 * we need to have a message larger than that.
419 certlen = i2d_X509(chaincert, NULL);
420 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
421 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
422 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
423 if (!X509_up_ref(chaincert)) {
424 printf("Unable to up ref cert\n");
427 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
428 printf("Unable to add extra chain cert %d\n", i);
429 X509_free(chaincert);
434 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
435 printf("Unable to create SSL objects\n");
439 if (!create_ssl_connection(serverssl, clientssl)) {
440 printf("Unable to create SSL connection\n");
445 * Calling SSL_clear() first is not required but this tests that SSL_clear()
446 * doesn't leak (when using enable-crypto-mdebug).
448 if (!SSL_clear(serverssl)) {
449 printf("Unexpected failure from SSL_clear()\n");
455 X509_free(chaincert);
464 static int test_large_message_tls(void)
466 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
470 static int test_large_message_tls_read_ahead(void)
472 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
476 #ifndef OPENSSL_NO_DTLS
477 static int test_large_message_dtls(void)
480 * read_ahead is not relevant to DTLS because DTLS always acts as if
483 return execute_test_large_message(DTLS_server_method(),
484 DTLS_client_method(), 0);
488 #ifndef OPENSSL_NO_OCSP
489 static int ocsp_server_cb(SSL *s, void *arg)
491 int *argi = (int *)arg;
492 unsigned char *orespdercopy = NULL;
493 STACK_OF(OCSP_RESPID) *ids = NULL;
494 OCSP_RESPID *id = NULL;
497 /* In this test we are expecting exactly 1 OCSP_RESPID */
498 SSL_get_tlsext_status_ids(s, &ids);
499 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
500 return SSL_TLSEXT_ERR_ALERT_FATAL;
502 id = sk_OCSP_RESPID_value(ids, 0);
503 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
504 return SSL_TLSEXT_ERR_ALERT_FATAL;
505 } else if (*argi != 1) {
506 return SSL_TLSEXT_ERR_ALERT_FATAL;
510 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
511 if (orespdercopy == NULL)
512 return SSL_TLSEXT_ERR_ALERT_FATAL;
514 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
516 ocsp_server_called = 1;
518 return SSL_TLSEXT_ERR_OK;
521 static int ocsp_client_cb(SSL *s, void *arg)
523 int *argi = (int *)arg;
524 const unsigned char *respderin;
527 if (*argi != 1 && *argi != 2)
530 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
532 if (memcmp(orespder, respderin, len) != 0)
535 ocsp_client_called = 1;
540 static int test_tlsext_status_type(void)
542 SSL_CTX *cctx = NULL, *sctx = NULL;
543 SSL *clientssl = NULL, *serverssl = NULL;
545 STACK_OF(OCSP_RESPID) *ids = NULL;
546 OCSP_RESPID *id = NULL;
549 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
550 &cctx, cert, privkey)) {
551 printf("Unable to create SSL_CTX pair\n");
555 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
556 printf("Unexpected initial value for "
557 "SSL_CTX_get_tlsext_status_type()\n");
561 /* First just do various checks getting and setting tlsext_status_type */
563 clientssl = SSL_new(cctx);
564 if (SSL_get_tlsext_status_type(clientssl) != -1) {
565 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
569 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
570 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
574 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
575 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
582 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
583 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
587 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
588 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
592 clientssl = SSL_new(cctx);
594 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
595 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
603 * Now actually do a handshake and check OCSP information is exchanged and
604 * the callbacks get called
607 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
608 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
609 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
610 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
612 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
613 printf("Unable to create SSL objects\n");
617 if (!create_ssl_connection(serverssl, clientssl)) {
618 printf("Unable to create SSL connection\n");
622 if (!ocsp_client_called || !ocsp_server_called) {
623 printf("OCSP callbacks not called\n");
632 /* Try again but this time force the server side callback to fail */
633 ocsp_client_called = 0;
634 ocsp_server_called = 0;
637 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
638 printf("Unable to create SSL objects\n");
642 /* This should fail because the callback will fail */
643 if (create_ssl_connection(serverssl, clientssl)) {
644 printf("Unexpected success creating the connection\n");
648 if (ocsp_client_called || ocsp_server_called) {
649 printf("OCSP callbacks successfully called unexpectedly\n");
659 * This time we'll get the client to send an OCSP_RESPID that it will
662 ocsp_client_called = 0;
663 ocsp_server_called = 0;
666 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
667 printf("Unable to create SSL objects\n");
672 * We'll just use any old cert for this test - it doesn't have to be an OCSP
673 * specifc one. We'll use the server cert.
675 certbio = BIO_new_file(cert, "r");
676 if (certbio == NULL) {
677 printf("Can't load the certficate file\n");
680 id = OCSP_RESPID_new();
681 ids = sk_OCSP_RESPID_new_null();
682 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
683 if (id == NULL || ids == NULL || ocspcert == NULL
684 || !OCSP_RESPID_set_by_key(id, ocspcert)
685 || !sk_OCSP_RESPID_push(ids, id)) {
686 printf("Unable to set OCSP_RESPIDs\n");
690 SSL_set_tlsext_status_ids(clientssl, ids);
691 /* Control has been transferred */
697 if (!create_ssl_connection(serverssl, clientssl)) {
698 printf("Unable to create SSL connection\n");
702 if (!ocsp_client_called || !ocsp_server_called) {
703 printf("OCSP callbacks not called\n");
714 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
715 OCSP_RESPID_free(id);
724 typedef struct ssl_session_test_fixture {
725 const char *test_case_name;
728 } SSL_SESSION_TEST_FIXTURE;
730 static int new_called = 0, remove_called = 0;
732 static SSL_SESSION_TEST_FIXTURE
733 ssl_session_set_up(const char *const test_case_name)
735 SSL_SESSION_TEST_FIXTURE fixture;
737 fixture.test_case_name = test_case_name;
738 fixture.use_ext_cache = 1;
739 fixture.use_int_cache = 1;
741 new_called = remove_called = 0;
746 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
750 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
757 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
762 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
764 SSL_CTX *sctx = NULL, *cctx = NULL;
765 SSL *serverssl1 = NULL, *clientssl1 = NULL;
766 SSL *serverssl2 = NULL, *clientssl2 = NULL;
767 #ifndef OPENSSL_NO_TLS1_1
768 SSL *serverssl3 = NULL, *clientssl3 = NULL;
770 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
773 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
774 &cctx, cert, privkey)) {
775 printf("Unable to create SSL_CTX pair\n");
779 #ifndef OPENSSL_NO_TLS1_2
780 /* Only allow TLS1.2 so we can force a connection failure later */
781 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
785 * TODO(TLS1.3): Test temporarily disabled for TLS1.3 until we've
786 * implemented session resumption.
788 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
790 /* Set up session cache */
791 if (fix.use_ext_cache) {
792 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
793 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
795 if (fix.use_int_cache) {
796 /* Also covers instance where both are set */
797 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
799 SSL_CTX_set_session_cache_mode(cctx,
800 SSL_SESS_CACHE_CLIENT
801 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
804 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
806 printf("Unable to create SSL objects\n");
810 if (!create_ssl_connection(serverssl1, clientssl1)) {
811 printf("Unable to create SSL connection\n");
814 sess1 = SSL_get1_session(clientssl1);
816 printf("Unexpected NULL session\n");
820 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
821 /* Should have failed because it should already be in the cache */
822 printf("Unexpected success adding session to cache\n");
826 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
827 printf("Session not added to cache\n");
831 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
832 printf("Unable to create second SSL objects\n");
836 if (!create_ssl_connection(serverssl2, clientssl2)) {
837 printf("Unable to create second SSL connection\n");
841 sess2 = SSL_get1_session(clientssl2);
843 printf("Unexpected NULL session from clientssl2\n");
847 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
848 printf("Remove session callback unexpectedly called\n");
853 * This should clear sess2 from the cache because it is a "bad" session. See
854 * SSL_set_session() documentation.
856 if (!SSL_set_session(clientssl2, sess1)) {
857 printf("Unexpected failure setting session\n");
861 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
862 printf("Failed to call callback to remove session\n");
867 if (SSL_get_session(clientssl2) != sess1) {
868 printf("Unexpected session found\n");
872 if (fix.use_int_cache) {
873 if (!SSL_CTX_add_session(cctx, sess2)) {
875 * Should have succeeded because it should not already be in the cache
877 printf("Unexpected failure adding session to cache\n");
881 if (!SSL_CTX_remove_session(cctx, sess2)) {
882 printf("Unexpected failure removing session from cache\n");
886 /* This is for the purposes of internal cache testing...ignore the
887 * counter for external cache
889 if (fix.use_ext_cache)
893 /* This shouldn't be in the cache so should fail */
894 if (SSL_CTX_remove_session(cctx, sess2)) {
895 printf("Unexpected success removing session from cache\n");
899 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
900 printf("Failed to call callback to remove session #2\n");
904 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
905 /* Force a connection failure */
906 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
908 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
909 printf("Unable to create third SSL objects\n");
913 if (!SSL_set_session(clientssl3, sess1)) {
914 printf("Unable to set session for third connection\n");
918 /* This should fail because of the mismatched protocol versions */
919 if (create_ssl_connection(serverssl3, clientssl3)) {
920 printf("Unable to create third SSL connection\n");
925 /* We should have automatically removed the session from the cache */
926 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
927 printf("Failed to call callback to remove session #2\n");
931 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
933 * Should have succeeded because it should not already be in the cache
935 printf("Unexpected failure adding session to cache #2\n");
943 SSL_free(serverssl1);
944 SSL_free(clientssl1);
945 SSL_free(serverssl2);
946 SSL_free(clientssl2);
947 #ifndef OPENSSL_NO_TLS1_1
948 SSL_free(serverssl3);
949 SSL_free(clientssl3);
951 SSL_SESSION_free(sess1);
952 SSL_SESSION_free(sess2);
954 * Check if we need to remove any sessions up-refed for the external cache
957 SSL_SESSION_free(sess1);
959 SSL_SESSION_free(sess2);
966 static int test_session_with_only_int_cache(void)
968 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
970 fixture.use_ext_cache = 0;
972 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
975 static int test_session_with_only_ext_cache(void)
977 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
979 fixture.use_int_cache = 0;
981 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
984 static int test_session_with_both_cache(void)
986 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
988 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
995 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
997 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
1012 static int test_ssl_set_bio(int idx)
1014 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1017 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
1019 int initrbio, initwbio, newrbio, newwbio;
1023 printf("Failed to allocate SSL_CTX\n");
1029 printf("Failed to allocate SSL object\n");
1040 OPENSSL_assert(newwbio <= 2);
1042 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
1043 || newwbio == USE_BIO_1) {
1044 bio1 = BIO_new(BIO_s_mem());
1046 printf("Failed to allocate bio1\n");
1051 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
1052 || newwbio == USE_BIO_2) {
1053 bio2 = BIO_new(BIO_s_mem());
1055 printf("Failed to allocate bio2\n");
1060 setupbio(&irbio, bio1, bio2, initrbio);
1061 setupbio(&iwbio, bio1, bio2, initwbio);
1064 * We want to maintain our own refs to these BIO, so do an up ref for each
1065 * BIO that will have ownersip transferred in the SSL_set_bio() call
1069 if (iwbio != NULL && iwbio != irbio)
1072 SSL_set_bio(ssl, irbio, iwbio);
1074 setupbio(&nrbio, bio1, bio2, newrbio);
1075 setupbio(&nwbio, bio1, bio2, newwbio);
1078 * We will (maybe) transfer ownership again so do more up refs.
1079 * SSL_set_bio() has some really complicated ownership rules where BIOs have
1082 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
1084 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
1087 SSL_set_bio(ssl, nrbio, nwbio);
1096 * This test is checking that the ref counting for SSL_set_bio is correct.
1097 * If we get here and we did too many frees then we will fail in the above
1098 * functions. If we haven't done enough then this will only be detected in
1099 * a crypto-mdebug build
1106 typedef struct ssl_bio_test_fixture {
1107 const char *test_case_name;
1109 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
1110 } SSL_BIO_TEST_FIXTURE;
1112 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
1114 SSL_BIO_TEST_FIXTURE fixture;
1116 fixture.test_case_name = test_case_name;
1117 fixture.pop_ssl = 0;
1118 fixture.change_bio = NO_BIO_CHANGE;
1123 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
1127 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
1129 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
1130 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1135 printf("Failed to allocate SSL_CTX\n");
1141 printf("Failed to allocate SSL object\n");
1145 sslbio = BIO_new(BIO_f_ssl());
1146 membio1 = BIO_new(BIO_s_mem());
1148 if (sslbio == NULL || membio1 == NULL) {
1149 printf("Malloc failure creating BIOs\n");
1153 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
1156 * If anything goes wrong here then we could leak memory, so this will
1157 * be caught in a crypto-mdebug build
1159 BIO_push(sslbio, membio1);
1161 /* Verify chaning the rbio/wbio directly does not cause leaks */
1162 if (fix.change_bio != NO_BIO_CHANGE) {
1163 membio2 = BIO_new(BIO_s_mem());
1164 if (membio2 == NULL) {
1165 printf("Malloc failure creating membio2\n");
1168 if (fix.change_bio == CHANGE_RBIO)
1169 SSL_set0_rbio(ssl, membio2);
1171 SSL_set0_wbio(ssl, membio2);
1190 static int test_ssl_bio_pop_next_bio(void)
1192 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1194 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1197 static int test_ssl_bio_pop_ssl_bio(void)
1199 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1201 fixture.pop_ssl = 1;
1203 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1206 static int test_ssl_bio_change_rbio(void)
1208 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1210 fixture.change_bio = CHANGE_RBIO;
1212 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1215 static int test_ssl_bio_change_wbio(void)
1217 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1219 fixture.change_bio = CHANGE_WBIO;
1221 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1225 /* The list of sig algs */
1227 /* The length of the list */
1229 /* A sigalgs list in string format */
1230 const char *liststr;
1231 /* Whether setting the list should succeed */
1233 /* Whether creating a connection with the list should succeed */
1237 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
1238 #ifndef OPENSSL_NO_EC
1239 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
1240 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
1242 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
1243 static const int invalidlist2[] = {NID_sha256, NID_undef};
1244 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
1245 static const int invalidlist4[] = {NID_sha256};
1246 static const sigalgs_list testsigalgs[] = {
1247 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
1248 #ifndef OPENSSL_NO_EC
1249 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
1250 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
1252 {NULL, 0, "RSA+SHA256", 1, 1},
1253 #ifndef OPENSSL_NO_EC
1254 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
1255 {NULL, 0, "ECDSA+SHA512", 1, 0},
1257 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
1258 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
1259 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
1260 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
1261 {NULL, 0, "RSA", 0, 0},
1262 {NULL, 0, "SHA256", 0, 0},
1263 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
1264 {NULL, 0, "Invalid", 0, 0}};
1266 static int test_set_sigalgs(int idx)
1268 SSL_CTX *cctx = NULL, *sctx = NULL;
1269 SSL *clientssl = NULL, *serverssl = NULL;
1271 const sigalgs_list *curr;
1274 /* Should never happen */
1275 if ((size_t)idx >= OSSL_NELEM(testsigalgs) * 2)
1278 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
1279 curr = testctx ? &testsigalgs[idx]
1280 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
1282 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
1283 &cctx, cert, privkey)) {
1284 printf("Unable to create SSL_CTX pair\n");
1289 * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
1290 * for TLSv1.2 for now until we add a new API.
1292 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
1296 if (curr->list != NULL)
1297 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
1299 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
1303 printf("Unexpected failure setting sigalgs in SSL_CTX (%d)\n",
1310 printf("Unexpected success setting sigalgs in SSL_CTX (%d)\n", idx);
1315 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
1316 printf("Unable to create SSL objects\n");
1323 if (curr->list != NULL)
1324 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
1326 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
1329 printf("Unexpected failure setting sigalgs in SSL (%d)\n", idx);
1335 printf("Unexpected success setting sigalgs in SSL (%d)\n", idx);
1340 if (curr->connsuccess != create_ssl_connection(serverssl, clientssl)) {
1341 printf("Unexpected return value creating SSL connection (%d)\n", idx);
1348 SSL_free(serverssl);
1349 SSL_free(clientssl);
1356 int test_main(int argc, char *argv[])
1361 printf("Invalid argument count\n");
1368 ADD_TEST(test_large_message_tls);
1369 ADD_TEST(test_large_message_tls_read_ahead);
1370 #ifndef OPENSSL_NO_DTLS
1371 ADD_TEST(test_large_message_dtls);
1373 #ifndef OPENSSL_NO_OCSP
1374 ADD_TEST(test_tlsext_status_type);
1376 ADD_TEST(test_session_with_only_int_cache);
1377 ADD_TEST(test_session_with_only_ext_cache);
1378 ADD_TEST(test_session_with_both_cache);
1379 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
1380 ADD_TEST(test_ssl_bio_pop_next_bio);
1381 ADD_TEST(test_ssl_bio_pop_ssl_bio);
1382 ADD_TEST(test_ssl_bio_change_rbio);
1383 ADD_TEST(test_ssl_bio_change_wbio);
1384 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
1385 ADD_TEST(test_keylog);
1386 #ifndef OPENSSL_NO_TLS1_3
1387 ADD_TEST(test_keylog_no_master_key);
1390 testresult = run_tests(argv[0]);
1392 bio_s_mempacket_test_free();