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;
203 /* Clean up logging space */
204 memset(client_log_buffer, 0, LOG_BUFFER_SIZE + 1);
205 memset(server_log_buffer, 0, LOG_BUFFER_SIZE + 1);
206 client_log_buffer_index = 0;
207 server_log_buffer_index = 0;
208 error_writing_log = 0;
210 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
211 &cctx, cert, privkey)) {
212 printf("Unable to create SSL_CTX pair\n");
216 /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
217 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
218 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
220 /* We also want to ensure that we use RSA-based key exchange. */
221 rc = SSL_CTX_set_cipher_list(cctx, "RSA");
223 printf("Unable to restrict to RSA key exchange.\n");
227 if (SSL_CTX_get_keylog_callback(cctx)) {
228 printf("Unexpected initial value for client "
229 "SSL_CTX_get_keylog_callback()\n");
232 if (SSL_CTX_get_keylog_callback(sctx)) {
233 printf("Unexpected initial value for server "
234 "SSL_CTX_get_keylog_callback()\n");
238 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
239 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
241 if (SSL_CTX_get_keylog_callback(cctx) != client_keylog_callback) {
242 printf("Unexpected set value for client "
243 "SSL_CTX_get_keylog_callback()\n");
246 if (SSL_CTX_get_keylog_callback(sctx) != server_keylog_callback) {
247 printf("Unexpected set value for server "
248 "SSL_CTX_get_keylog_callback()\n");
251 /* Now do a handshake and check that the logs have been written to. */
252 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
253 printf("Unable to create SSL objects\n");
257 if (!create_ssl_connection(serverssl, clientssl)) {
258 printf("Unable to create SSL connection\n");
262 if (error_writing_log) {
263 printf("Error encountered while logging\n");
267 if ((client_log_buffer_index == 0) || (server_log_buffer_index == 0)) {
268 printf("No logs written\n");
272 /* Now we want to test that our output data was vaguely sensible. We
273 * do that by using strtok and confirming that we have more or less the
276 if (test_keylog_output(client_log_buffer, clientssl,
277 SSL_get_session(clientssl)) != 1) {
278 printf("Error encountered in client log buffer\n");
281 if (test_keylog_output(server_log_buffer, serverssl,
282 SSL_get_session(serverssl)) != 1) {
283 printf("Error encountered in server log buffer\n");
298 #ifndef OPENSSL_NO_TLS1_3
299 static int test_keylog_no_master_key(void) {
300 SSL_CTX *cctx = NULL, *sctx = NULL;
301 SSL *clientssl = NULL, *serverssl = NULL;
304 /* Clean up logging space */
305 memset(client_log_buffer, 0, LOG_BUFFER_SIZE + 1);
306 memset(server_log_buffer, 0, LOG_BUFFER_SIZE + 1);
307 client_log_buffer_index = 0;
308 server_log_buffer_index = 0;
309 error_writing_log = 0;
311 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
312 &cctx, cert, privkey)) {
313 printf("Unable to create SSL_CTX pair\n");
317 if (SSL_CTX_get_keylog_callback(cctx)) {
318 printf("Unexpected initial value for client "
319 "SSL_CTX_get_keylog_callback()\n");
322 if (SSL_CTX_get_keylog_callback(sctx)) {
323 printf("Unexpected initial value for server "
324 "SSL_CTX_get_keylog_callback()\n");
328 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
329 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
331 if (SSL_CTX_get_keylog_callback(cctx) != client_keylog_callback) {
332 printf("Unexpected set value for client "
333 "SSL_CTX_get_keylog_callback()\n");
336 if (SSL_CTX_get_keylog_callback(sctx) != server_keylog_callback) {
337 printf("Unexpected set value for server "
338 "SSL_CTX_get_keylog_callback()\n");
341 /* Now do a handshake and check that the logs have been written to. */
342 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
343 printf("Unable to create SSL objects\n");
347 if (!create_ssl_connection(serverssl, clientssl)) {
348 printf("Unable to create SSL connection\n");
352 if (error_writing_log) {
353 printf("Error encountered while logging\n");
357 /* Now we want to test that our output data was vaguely sensible. For this
358 * test, we expect no CLIENT_RANDOM entry.
360 if (test_keylog_output(client_log_buffer, clientssl,
361 SSL_get_session(clientssl)) != 0) {
362 printf("Error encountered in client log buffer\n");
365 if (test_keylog_output(server_log_buffer, serverssl,
366 SSL_get_session(serverssl)) != 0) {
367 printf("Error encountered in server log buffer\n");
383 static int execute_test_large_message(const SSL_METHOD *smeth,
384 const SSL_METHOD *cmeth, int read_ahead)
386 SSL_CTX *cctx = NULL, *sctx = NULL;
387 SSL *clientssl = NULL, *serverssl = NULL;
390 BIO *certbio = BIO_new_file(cert, "r");
391 X509 *chaincert = NULL;
394 if (certbio == NULL) {
395 printf("Can't load the certficate file\n");
398 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
401 if (chaincert == NULL) {
402 printf("Unable to load certificate for chain\n");
406 if (!create_ssl_ctx_pair(smeth, cmeth, &sctx,
407 &cctx, cert, privkey)) {
408 printf("Unable to create SSL_CTX pair\n");
414 * Test that read_ahead works correctly when dealing with large
417 SSL_CTX_set_read_ahead(cctx, 1);
421 * We assume the supplied certificate is big enough so that if we add
422 * NUM_EXTRA_CERTS it will make the overall message large enough. The
423 * default buffer size is requested to be 16k, but due to the way BUF_MEM
424 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
425 * we need to have a message larger than that.
427 certlen = i2d_X509(chaincert, NULL);
428 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
429 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
430 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
431 if (!X509_up_ref(chaincert)) {
432 printf("Unable to up ref cert\n");
435 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
436 printf("Unable to add extra chain cert %d\n", i);
437 X509_free(chaincert);
442 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
443 printf("Unable to create SSL objects\n");
447 if (!create_ssl_connection(serverssl, clientssl)) {
448 printf("Unable to create SSL connection\n");
453 * Calling SSL_clear() first is not required but this tests that SSL_clear()
454 * doesn't leak (when using enable-crypto-mdebug).
456 if (!SSL_clear(serverssl)) {
457 printf("Unexpected failure from SSL_clear()\n");
463 X509_free(chaincert);
472 static int test_large_message_tls(void)
474 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
478 static int test_large_message_tls_read_ahead(void)
480 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
484 #ifndef OPENSSL_NO_DTLS
485 static int test_large_message_dtls(void)
488 * read_ahead is not relevant to DTLS because DTLS always acts as if
491 return execute_test_large_message(DTLS_server_method(),
492 DTLS_client_method(), 0);
496 #ifndef OPENSSL_NO_OCSP
497 static int ocsp_server_cb(SSL *s, void *arg)
499 int *argi = (int *)arg;
500 unsigned char *orespdercopy = NULL;
501 STACK_OF(OCSP_RESPID) *ids = NULL;
502 OCSP_RESPID *id = NULL;
505 /* In this test we are expecting exactly 1 OCSP_RESPID */
506 SSL_get_tlsext_status_ids(s, &ids);
507 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
508 return SSL_TLSEXT_ERR_ALERT_FATAL;
510 id = sk_OCSP_RESPID_value(ids, 0);
511 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
512 return SSL_TLSEXT_ERR_ALERT_FATAL;
513 } else if (*argi != 1) {
514 return SSL_TLSEXT_ERR_ALERT_FATAL;
518 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
519 if (orespdercopy == NULL)
520 return SSL_TLSEXT_ERR_ALERT_FATAL;
522 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
524 ocsp_server_called = 1;
526 return SSL_TLSEXT_ERR_OK;
529 static int ocsp_client_cb(SSL *s, void *arg)
531 int *argi = (int *)arg;
532 const unsigned char *respderin;
535 if (*argi != 1 && *argi != 2)
538 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
540 if (memcmp(orespder, respderin, len) != 0)
543 ocsp_client_called = 1;
548 static int test_tlsext_status_type(void)
550 SSL_CTX *cctx = NULL, *sctx = NULL;
551 SSL *clientssl = NULL, *serverssl = NULL;
553 STACK_OF(OCSP_RESPID) *ids = NULL;
554 OCSP_RESPID *id = NULL;
557 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
558 &cctx, cert, privkey)) {
559 printf("Unable to create SSL_CTX pair\n");
563 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
564 printf("Unexpected initial value for "
565 "SSL_CTX_get_tlsext_status_type()\n");
569 /* First just do various checks getting and setting tlsext_status_type */
571 clientssl = SSL_new(cctx);
572 if (SSL_get_tlsext_status_type(clientssl) != -1) {
573 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
577 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
578 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
582 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
583 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
590 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
591 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
595 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
596 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
600 clientssl = SSL_new(cctx);
602 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
603 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
611 * Now actually do a handshake and check OCSP information is exchanged and
612 * the callbacks get called
615 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
616 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
617 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
618 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
620 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
621 printf("Unable to create SSL objects\n");
625 if (!create_ssl_connection(serverssl, clientssl)) {
626 printf("Unable to create SSL connection\n");
630 if (!ocsp_client_called || !ocsp_server_called) {
631 printf("OCSP callbacks not called\n");
640 /* Try again but this time force the server side callback to fail */
641 ocsp_client_called = 0;
642 ocsp_server_called = 0;
645 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
646 printf("Unable to create SSL objects\n");
650 /* This should fail because the callback will fail */
651 if (create_ssl_connection(serverssl, clientssl)) {
652 printf("Unexpected success creating the connection\n");
656 if (ocsp_client_called || ocsp_server_called) {
657 printf("OCSP callbacks successfully called unexpectedly\n");
667 * This time we'll get the client to send an OCSP_RESPID that it will
670 ocsp_client_called = 0;
671 ocsp_server_called = 0;
674 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
675 printf("Unable to create SSL objects\n");
680 * We'll just use any old cert for this test - it doesn't have to be an OCSP
681 * specifc one. We'll use the server cert.
683 certbio = BIO_new_file(cert, "r");
684 if (certbio == NULL) {
685 printf("Can't load the certficate file\n");
688 id = OCSP_RESPID_new();
689 ids = sk_OCSP_RESPID_new_null();
690 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
691 if (id == NULL || ids == NULL || ocspcert == NULL
692 || !OCSP_RESPID_set_by_key(id, ocspcert)
693 || !sk_OCSP_RESPID_push(ids, id)) {
694 printf("Unable to set OCSP_RESPIDs\n");
698 SSL_set_tlsext_status_ids(clientssl, ids);
699 /* Control has been transferred */
705 if (!create_ssl_connection(serverssl, clientssl)) {
706 printf("Unable to create SSL connection\n");
710 if (!ocsp_client_called || !ocsp_server_called) {
711 printf("OCSP callbacks not called\n");
722 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
723 OCSP_RESPID_free(id);
732 typedef struct ssl_session_test_fixture {
733 const char *test_case_name;
736 } SSL_SESSION_TEST_FIXTURE;
738 static int new_called = 0, remove_called = 0;
740 static SSL_SESSION_TEST_FIXTURE
741 ssl_session_set_up(const char *const test_case_name)
743 SSL_SESSION_TEST_FIXTURE fixture;
745 fixture.test_case_name = test_case_name;
746 fixture.use_ext_cache = 1;
747 fixture.use_int_cache = 1;
749 new_called = remove_called = 0;
754 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
758 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
765 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
770 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
772 SSL_CTX *sctx = NULL, *cctx = NULL;
773 SSL *serverssl1 = NULL, *clientssl1 = NULL;
774 SSL *serverssl2 = NULL, *clientssl2 = NULL;
775 #ifndef OPENSSL_NO_TLS1_1
776 SSL *serverssl3 = NULL, *clientssl3 = NULL;
778 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
781 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
782 &cctx, cert, privkey)) {
783 printf("Unable to create SSL_CTX pair\n");
787 #ifndef OPENSSL_NO_TLS1_2
788 /* Only allow TLS1.2 so we can force a connection failure later */
789 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
792 /* Set up session cache */
793 if (fix.use_ext_cache) {
794 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
795 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
797 if (fix.use_int_cache) {
798 /* Also covers instance where both are set */
799 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
801 SSL_CTX_set_session_cache_mode(cctx,
802 SSL_SESS_CACHE_CLIENT
803 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
806 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
808 printf("Unable to create SSL objects\n");
812 if (!create_ssl_connection(serverssl1, clientssl1)) {
813 printf("Unable to create SSL connection\n");
816 sess1 = SSL_get1_session(clientssl1);
818 printf("Unexpected NULL session\n");
822 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
823 /* Should have failed because it should already be in the cache */
824 printf("Unexpected success adding session to cache\n");
828 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
829 printf("Session not added to cache\n");
833 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
834 printf("Unable to create second SSL objects\n");
838 if (!create_ssl_connection(serverssl2, clientssl2)) {
839 printf("Unable to create second SSL connection\n");
843 sess2 = SSL_get1_session(clientssl2);
845 printf("Unexpected NULL session from clientssl2\n");
849 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
850 printf("Remove session callback unexpectedly called\n");
855 * This should clear sess2 from the cache because it is a "bad" session. See
856 * SSL_set_session() documentation.
858 if (!SSL_set_session(clientssl2, sess1)) {
859 printf("Unexpected failure setting session\n");
863 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
864 printf("Failed to call callback to remove session\n");
869 if (SSL_get_session(clientssl2) != sess1) {
870 printf("Unexpected session found\n");
874 if (fix.use_int_cache) {
875 if (!SSL_CTX_add_session(cctx, sess2)) {
877 * Should have succeeded because it should not already be in the cache
879 printf("Unexpected failure adding session to cache\n");
883 if (!SSL_CTX_remove_session(cctx, sess2)) {
884 printf("Unexpected failure removing session from cache\n");
888 /* This is for the purposes of internal cache testing...ignore the
889 * counter for external cache
891 if (fix.use_ext_cache)
895 /* This shouldn't be in the cache so should fail */
896 if (SSL_CTX_remove_session(cctx, sess2)) {
897 printf("Unexpected success removing session from cache\n");
901 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
902 printf("Failed to call callback to remove session #2\n");
906 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
907 /* Force a connection failure */
908 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
910 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
911 printf("Unable to create third SSL objects\n");
915 if (!SSL_set_session(clientssl3, sess1)) {
916 printf("Unable to set session for third connection\n");
920 /* This should fail because of the mismatched protocol versions */
921 if (create_ssl_connection(serverssl3, clientssl3)) {
922 printf("Unable to create third SSL connection\n");
927 /* We should have automatically removed the session from the cache */
928 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
929 printf("Failed to call callback to remove session #2\n");
933 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
935 * Should have succeeded because it should not already be in the cache
937 printf("Unexpected failure adding session to cache #2\n");
945 SSL_free(serverssl1);
946 SSL_free(clientssl1);
947 SSL_free(serverssl2);
948 SSL_free(clientssl2);
949 #ifndef OPENSSL_NO_TLS1_1
950 SSL_free(serverssl3);
951 SSL_free(clientssl3);
953 SSL_SESSION_free(sess1);
954 SSL_SESSION_free(sess2);
956 * Check if we need to remove any sessions up-refed for the external cache
959 SSL_SESSION_free(sess1);
961 SSL_SESSION_free(sess2);
968 static int test_session_with_only_int_cache(void)
970 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
972 fixture.use_ext_cache = 0;
974 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
977 static int test_session_with_only_ext_cache(void)
979 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
981 fixture.use_int_cache = 0;
983 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
986 static int test_session_with_both_cache(void)
988 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
990 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
997 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
999 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
1014 static int test_ssl_set_bio(int idx)
1016 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1019 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
1021 int initrbio, initwbio, newrbio, newwbio;
1025 printf("Failed to allocate SSL_CTX\n");
1031 printf("Failed to allocate SSL object\n");
1042 OPENSSL_assert(newwbio <= 2);
1044 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
1045 || newwbio == USE_BIO_1) {
1046 bio1 = BIO_new(BIO_s_mem());
1048 printf("Failed to allocate bio1\n");
1053 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
1054 || newwbio == USE_BIO_2) {
1055 bio2 = BIO_new(BIO_s_mem());
1057 printf("Failed to allocate bio2\n");
1062 setupbio(&irbio, bio1, bio2, initrbio);
1063 setupbio(&iwbio, bio1, bio2, initwbio);
1066 * We want to maintain our own refs to these BIO, so do an up ref for each
1067 * BIO that will have ownersip transferred in the SSL_set_bio() call
1071 if (iwbio != NULL && iwbio != irbio)
1074 SSL_set_bio(ssl, irbio, iwbio);
1076 setupbio(&nrbio, bio1, bio2, newrbio);
1077 setupbio(&nwbio, bio1, bio2, newwbio);
1080 * We will (maybe) transfer ownership again so do more up refs.
1081 * SSL_set_bio() has some really complicated ownership rules where BIOs have
1084 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
1086 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
1089 SSL_set_bio(ssl, nrbio, nwbio);
1098 * This test is checking that the ref counting for SSL_set_bio is correct.
1099 * If we get here and we did too many frees then we will fail in the above
1100 * functions. If we haven't done enough then this will only be detected in
1101 * a crypto-mdebug build
1108 typedef struct ssl_bio_test_fixture {
1109 const char *test_case_name;
1111 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
1112 } SSL_BIO_TEST_FIXTURE;
1114 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
1116 SSL_BIO_TEST_FIXTURE fixture;
1118 fixture.test_case_name = test_case_name;
1119 fixture.pop_ssl = 0;
1120 fixture.change_bio = NO_BIO_CHANGE;
1125 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
1129 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
1131 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
1132 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1137 printf("Failed to allocate SSL_CTX\n");
1143 printf("Failed to allocate SSL object\n");
1147 sslbio = BIO_new(BIO_f_ssl());
1148 membio1 = BIO_new(BIO_s_mem());
1150 if (sslbio == NULL || membio1 == NULL) {
1151 printf("Malloc failure creating BIOs\n");
1155 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
1158 * If anything goes wrong here then we could leak memory, so this will
1159 * be caught in a crypto-mdebug build
1161 BIO_push(sslbio, membio1);
1163 /* Verify chaning the rbio/wbio directly does not cause leaks */
1164 if (fix.change_bio != NO_BIO_CHANGE) {
1165 membio2 = BIO_new(BIO_s_mem());
1166 if (membio2 == NULL) {
1167 printf("Malloc failure creating membio2\n");
1170 if (fix.change_bio == CHANGE_RBIO)
1171 SSL_set0_rbio(ssl, membio2);
1173 SSL_set0_wbio(ssl, membio2);
1192 static int test_ssl_bio_pop_next_bio(void)
1194 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1196 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1199 static int test_ssl_bio_pop_ssl_bio(void)
1201 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1203 fixture.pop_ssl = 1;
1205 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1208 static int test_ssl_bio_change_rbio(void)
1210 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1212 fixture.change_bio = CHANGE_RBIO;
1214 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1217 static int test_ssl_bio_change_wbio(void)
1219 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1221 fixture.change_bio = CHANGE_WBIO;
1223 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1227 /* The list of sig algs */
1229 /* The length of the list */
1231 /* A sigalgs list in string format */
1232 const char *liststr;
1233 /* Whether setting the list should succeed */
1235 /* Whether creating a connection with the list should succeed */
1239 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
1240 #ifndef OPENSSL_NO_EC
1241 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
1242 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
1244 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
1245 static const int invalidlist2[] = {NID_sha256, NID_undef};
1246 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
1247 static const int invalidlist4[] = {NID_sha256};
1248 static const sigalgs_list testsigalgs[] = {
1249 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
1250 #ifndef OPENSSL_NO_EC
1251 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
1252 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
1254 {NULL, 0, "RSA+SHA256", 1, 1},
1255 #ifndef OPENSSL_NO_EC
1256 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
1257 {NULL, 0, "ECDSA+SHA512", 1, 0},
1259 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
1260 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
1261 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
1262 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
1263 {NULL, 0, "RSA", 0, 0},
1264 {NULL, 0, "SHA256", 0, 0},
1265 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
1266 {NULL, 0, "Invalid", 0, 0}};
1268 static int test_set_sigalgs(int idx)
1270 SSL_CTX *cctx = NULL, *sctx = NULL;
1271 SSL *clientssl = NULL, *serverssl = NULL;
1273 const sigalgs_list *curr;
1276 /* Should never happen */
1277 if ((size_t)idx >= OSSL_NELEM(testsigalgs) * 2)
1280 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
1281 curr = testctx ? &testsigalgs[idx]
1282 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
1284 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
1285 &cctx, cert, privkey)) {
1286 printf("Unable to create SSL_CTX pair\n");
1291 * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
1292 * for TLSv1.2 for now until we add a new API.
1294 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
1298 if (curr->list != NULL)
1299 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
1301 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
1305 printf("Unexpected failure setting sigalgs in SSL_CTX (%d)\n",
1312 printf("Unexpected success setting sigalgs in SSL_CTX (%d)\n", idx);
1317 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
1318 printf("Unable to create SSL objects\n");
1325 if (curr->list != NULL)
1326 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
1328 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
1331 printf("Unexpected failure setting sigalgs in SSL (%d)\n", idx);
1337 printf("Unexpected success setting sigalgs in SSL (%d)\n", idx);
1342 if (curr->connsuccess != create_ssl_connection(serverssl, clientssl)) {
1343 printf("Unexpected return value creating SSL connection (%d)\n", idx);
1350 SSL_free(serverssl);
1351 SSL_free(clientssl);
1358 int test_main(int argc, char *argv[])
1363 printf("Invalid argument count\n");
1370 ADD_TEST(test_large_message_tls);
1371 ADD_TEST(test_large_message_tls_read_ahead);
1372 #ifndef OPENSSL_NO_DTLS
1373 ADD_TEST(test_large_message_dtls);
1375 #ifndef OPENSSL_NO_OCSP
1376 ADD_TEST(test_tlsext_status_type);
1378 ADD_TEST(test_session_with_only_int_cache);
1379 ADD_TEST(test_session_with_only_ext_cache);
1380 ADD_TEST(test_session_with_both_cache);
1381 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
1382 ADD_TEST(test_ssl_bio_pop_next_bio);
1383 ADD_TEST(test_ssl_bio_pop_ssl_bio);
1384 ADD_TEST(test_ssl_bio_change_rbio);
1385 ADD_TEST(test_ssl_bio_change_wbio);
1386 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
1387 ADD_TEST(test_keylog);
1388 #ifndef OPENSSL_NO_TLS1_3
1389 ADD_TEST(test_keylog_no_master_key);
1392 testresult = run_tests(argv[0]);
1394 bio_s_mempacket_test_free();