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);
793 * TODO(TLS1.3): Test temporarily disabled for TLS1.3 until we've
794 * implemented session resumption.
796 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
798 /* Set up session cache */
799 if (fix.use_ext_cache) {
800 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
801 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
803 if (fix.use_int_cache) {
804 /* Also covers instance where both are set */
805 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
807 SSL_CTX_set_session_cache_mode(cctx,
808 SSL_SESS_CACHE_CLIENT
809 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
812 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
814 printf("Unable to create SSL objects\n");
818 if (!create_ssl_connection(serverssl1, clientssl1)) {
819 printf("Unable to create SSL connection\n");
822 sess1 = SSL_get1_session(clientssl1);
824 printf("Unexpected NULL session\n");
828 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
829 /* Should have failed because it should already be in the cache */
830 printf("Unexpected success adding session to cache\n");
834 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
835 printf("Session not added to cache\n");
839 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
840 printf("Unable to create second SSL objects\n");
844 if (!create_ssl_connection(serverssl2, clientssl2)) {
845 printf("Unable to create second SSL connection\n");
849 sess2 = SSL_get1_session(clientssl2);
851 printf("Unexpected NULL session from clientssl2\n");
855 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
856 printf("Remove session callback unexpectedly called\n");
861 * This should clear sess2 from the cache because it is a "bad" session. See
862 * SSL_set_session() documentation.
864 if (!SSL_set_session(clientssl2, sess1)) {
865 printf("Unexpected failure setting session\n");
869 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
870 printf("Failed to call callback to remove session\n");
875 if (SSL_get_session(clientssl2) != sess1) {
876 printf("Unexpected session found\n");
880 if (fix.use_int_cache) {
881 if (!SSL_CTX_add_session(cctx, sess2)) {
883 * Should have succeeded because it should not already be in the cache
885 printf("Unexpected failure adding session to cache\n");
889 if (!SSL_CTX_remove_session(cctx, sess2)) {
890 printf("Unexpected failure removing session from cache\n");
894 /* This is for the purposes of internal cache testing...ignore the
895 * counter for external cache
897 if (fix.use_ext_cache)
901 /* This shouldn't be in the cache so should fail */
902 if (SSL_CTX_remove_session(cctx, sess2)) {
903 printf("Unexpected success removing session from cache\n");
907 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
908 printf("Failed to call callback to remove session #2\n");
912 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
913 /* Force a connection failure */
914 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
916 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
917 printf("Unable to create third SSL objects\n");
921 if (!SSL_set_session(clientssl3, sess1)) {
922 printf("Unable to set session for third connection\n");
926 /* This should fail because of the mismatched protocol versions */
927 if (create_ssl_connection(serverssl3, clientssl3)) {
928 printf("Unable to create third SSL connection\n");
933 /* We should have automatically removed the session from the cache */
934 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
935 printf("Failed to call callback to remove session #2\n");
939 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
941 * Should have succeeded because it should not already be in the cache
943 printf("Unexpected failure adding session to cache #2\n");
951 SSL_free(serverssl1);
952 SSL_free(clientssl1);
953 SSL_free(serverssl2);
954 SSL_free(clientssl2);
955 #ifndef OPENSSL_NO_TLS1_1
956 SSL_free(serverssl3);
957 SSL_free(clientssl3);
959 SSL_SESSION_free(sess1);
960 SSL_SESSION_free(sess2);
962 * Check if we need to remove any sessions up-refed for the external cache
965 SSL_SESSION_free(sess1);
967 SSL_SESSION_free(sess2);
974 static int test_session_with_only_int_cache(void)
976 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
978 fixture.use_ext_cache = 0;
980 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
983 static int test_session_with_only_ext_cache(void)
985 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
987 fixture.use_int_cache = 0;
989 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
992 static int test_session_with_both_cache(void)
994 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
996 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
1003 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
1005 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
1020 static int test_ssl_set_bio(int idx)
1022 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1025 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
1027 int initrbio, initwbio, newrbio, newwbio;
1031 printf("Failed to allocate SSL_CTX\n");
1037 printf("Failed to allocate SSL object\n");
1048 OPENSSL_assert(newwbio <= 2);
1050 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
1051 || newwbio == USE_BIO_1) {
1052 bio1 = BIO_new(BIO_s_mem());
1054 printf("Failed to allocate bio1\n");
1059 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
1060 || newwbio == USE_BIO_2) {
1061 bio2 = BIO_new(BIO_s_mem());
1063 printf("Failed to allocate bio2\n");
1068 setupbio(&irbio, bio1, bio2, initrbio);
1069 setupbio(&iwbio, bio1, bio2, initwbio);
1072 * We want to maintain our own refs to these BIO, so do an up ref for each
1073 * BIO that will have ownersip transferred in the SSL_set_bio() call
1077 if (iwbio != NULL && iwbio != irbio)
1080 SSL_set_bio(ssl, irbio, iwbio);
1082 setupbio(&nrbio, bio1, bio2, newrbio);
1083 setupbio(&nwbio, bio1, bio2, newwbio);
1086 * We will (maybe) transfer ownership again so do more up refs.
1087 * SSL_set_bio() has some really complicated ownership rules where BIOs have
1090 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
1092 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
1095 SSL_set_bio(ssl, nrbio, nwbio);
1104 * This test is checking that the ref counting for SSL_set_bio is correct.
1105 * If we get here and we did too many frees then we will fail in the above
1106 * functions. If we haven't done enough then this will only be detected in
1107 * a crypto-mdebug build
1114 typedef struct ssl_bio_test_fixture {
1115 const char *test_case_name;
1117 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
1118 } SSL_BIO_TEST_FIXTURE;
1120 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
1122 SSL_BIO_TEST_FIXTURE fixture;
1124 fixture.test_case_name = test_case_name;
1125 fixture.pop_ssl = 0;
1126 fixture.change_bio = NO_BIO_CHANGE;
1131 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
1135 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
1137 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
1138 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
1143 printf("Failed to allocate SSL_CTX\n");
1149 printf("Failed to allocate SSL object\n");
1153 sslbio = BIO_new(BIO_f_ssl());
1154 membio1 = BIO_new(BIO_s_mem());
1156 if (sslbio == NULL || membio1 == NULL) {
1157 printf("Malloc failure creating BIOs\n");
1161 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
1164 * If anything goes wrong here then we could leak memory, so this will
1165 * be caught in a crypto-mdebug build
1167 BIO_push(sslbio, membio1);
1169 /* Verify chaning the rbio/wbio directly does not cause leaks */
1170 if (fix.change_bio != NO_BIO_CHANGE) {
1171 membio2 = BIO_new(BIO_s_mem());
1172 if (membio2 == NULL) {
1173 printf("Malloc failure creating membio2\n");
1176 if (fix.change_bio == CHANGE_RBIO)
1177 SSL_set0_rbio(ssl, membio2);
1179 SSL_set0_wbio(ssl, membio2);
1198 static int test_ssl_bio_pop_next_bio(void)
1200 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1202 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1205 static int test_ssl_bio_pop_ssl_bio(void)
1207 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1209 fixture.pop_ssl = 1;
1211 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1214 static int test_ssl_bio_change_rbio(void)
1216 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1218 fixture.change_bio = CHANGE_RBIO;
1220 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1223 static int test_ssl_bio_change_wbio(void)
1225 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
1227 fixture.change_bio = CHANGE_WBIO;
1229 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
1233 /* The list of sig algs */
1235 /* The length of the list */
1237 /* A sigalgs list in string format */
1238 const char *liststr;
1239 /* Whether setting the list should succeed */
1241 /* Whether creating a connection with the list should succeed */
1245 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
1246 #ifndef OPENSSL_NO_EC
1247 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
1248 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
1250 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
1251 static const int invalidlist2[] = {NID_sha256, NID_undef};
1252 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
1253 static const int invalidlist4[] = {NID_sha256};
1254 static const sigalgs_list testsigalgs[] = {
1255 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
1256 #ifndef OPENSSL_NO_EC
1257 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
1258 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
1260 {NULL, 0, "RSA+SHA256", 1, 1},
1261 #ifndef OPENSSL_NO_EC
1262 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
1263 {NULL, 0, "ECDSA+SHA512", 1, 0},
1265 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
1266 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
1267 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
1268 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
1269 {NULL, 0, "RSA", 0, 0},
1270 {NULL, 0, "SHA256", 0, 0},
1271 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
1272 {NULL, 0, "Invalid", 0, 0}};
1274 static int test_set_sigalgs(int idx)
1276 SSL_CTX *cctx = NULL, *sctx = NULL;
1277 SSL *clientssl = NULL, *serverssl = NULL;
1279 const sigalgs_list *curr;
1282 /* Should never happen */
1283 if ((size_t)idx >= OSSL_NELEM(testsigalgs) * 2)
1286 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
1287 curr = testctx ? &testsigalgs[idx]
1288 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
1290 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
1291 &cctx, cert, privkey)) {
1292 printf("Unable to create SSL_CTX pair\n");
1297 * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
1298 * for TLSv1.2 for now until we add a new API.
1300 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
1304 if (curr->list != NULL)
1305 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
1307 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
1311 printf("Unexpected failure setting sigalgs in SSL_CTX (%d)\n",
1318 printf("Unexpected success setting sigalgs in SSL_CTX (%d)\n", idx);
1323 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
1324 printf("Unable to create SSL objects\n");
1331 if (curr->list != NULL)
1332 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
1334 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
1337 printf("Unexpected failure setting sigalgs in SSL (%d)\n", idx);
1343 printf("Unexpected success setting sigalgs in SSL (%d)\n", idx);
1348 if (curr->connsuccess != create_ssl_connection(serverssl, clientssl)) {
1349 printf("Unexpected return value creating SSL connection (%d)\n", idx);
1356 SSL_free(serverssl);
1357 SSL_free(clientssl);
1364 int test_main(int argc, char *argv[])
1369 printf("Invalid argument count\n");
1376 ADD_TEST(test_large_message_tls);
1377 ADD_TEST(test_large_message_tls_read_ahead);
1378 #ifndef OPENSSL_NO_DTLS
1379 ADD_TEST(test_large_message_dtls);
1381 #ifndef OPENSSL_NO_OCSP
1382 ADD_TEST(test_tlsext_status_type);
1384 ADD_TEST(test_session_with_only_int_cache);
1385 ADD_TEST(test_session_with_only_ext_cache);
1386 ADD_TEST(test_session_with_both_cache);
1387 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
1388 ADD_TEST(test_ssl_bio_pop_next_bio);
1389 ADD_TEST(test_ssl_bio_pop_ssl_bio);
1390 ADD_TEST(test_ssl_bio_change_rbio);
1391 ADD_TEST(test_ssl_bio_change_wbio);
1392 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
1393 ADD_TEST(test_keylog);
1394 #ifndef OPENSSL_NO_TLS1_3
1395 ADD_TEST(test_keylog_no_master_key);
1398 testresult = run_tests(argv[0]);
1400 bio_s_mempacket_test_free();