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
10 #include <openssl/opensslconf.h>
11 #include <openssl/bio.h>
12 #include <openssl/crypto.h>
13 #include <openssl/ssl.h>
15 #include "ssltestlib.h"
18 static char *cert = NULL;
19 static char *privkey = NULL;
21 static int test_tlsext_status_type(void)
27 /* Test tlsext_status_type */
28 ctx = SSL_CTX_new(TLS_method());
30 if (SSL_CTX_get_tlsext_status_type(ctx) != -1) {
31 printf("Unexpected initial value for "
32 "SSL_CTX_get_tlsext_status_type()\n");
38 if (SSL_get_tlsext_status_type(con) != -1) {
39 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
43 if (!SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp)) {
44 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
48 if (SSL_get_tlsext_status_type(con) != TLSEXT_STATUSTYPE_ocsp) {
49 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
56 if (!SSL_CTX_set_tlsext_status_type(ctx, TLSEXT_STATUSTYPE_ocsp)) {
57 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
61 if (SSL_CTX_get_tlsext_status_type(ctx) != TLSEXT_STATUSTYPE_ocsp) {
62 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
68 if (SSL_get_tlsext_status_type(con) != TLSEXT_STATUSTYPE_ocsp) {
69 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
82 typedef struct ssl_session_test_fixture {
83 const char *test_case_name;
86 } SSL_SESSION_TEST_FIXTURE;
88 static int new_called = 0, remove_called = 0;
90 static SSL_SESSION_TEST_FIXTURE
91 ssl_session_set_up(const char *const test_case_name)
93 SSL_SESSION_TEST_FIXTURE fixture;
95 fixture.test_case_name = test_case_name;
96 fixture.use_ext_cache = 1;
97 fixture.use_int_cache = 1;
99 new_called = remove_called = 0;
104 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
108 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
115 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
120 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
122 SSL_CTX *sctx = NULL, *cctx = NULL;
123 SSL *serverssl1 = NULL, *clientssl1 = NULL;
124 SSL *serverssl2 = NULL, *clientssl2 = NULL;
125 #ifndef OPENSSL_NO_TLS1_1
126 SSL *serverssl3 = NULL, *clientssl3 = NULL;
128 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
131 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
132 &cctx, cert, privkey)) {
133 printf("Unable to create SSL_CTX pair\n");
137 #ifndef OPENSSL_NO_TLS1_2
138 /* Only allow TLS1.2 so we can force a connection failure later */
139 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
142 /* Set up session cache */
143 if (fix.use_ext_cache) {
144 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
145 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
147 if (fix.use_int_cache) {
148 /* Also covers instance where both are set */
149 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
151 SSL_CTX_set_session_cache_mode(cctx,
152 SSL_SESS_CACHE_CLIENT
153 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
156 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
158 printf("Unable to create SSL objects\n");
162 if (!create_ssl_connection(serverssl1, clientssl1)) {
163 printf("Unable to create SSL connection\n");
166 sess1 = SSL_get1_session(clientssl1);
168 printf("Unexpected NULL session\n");
172 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
173 /* Should have failed because it should already be in the cache */
174 printf("Unexpected success adding session to cache\n");
178 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
179 printf("Session not added to cache\n");
183 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
184 printf("Unable to create second SSL objects\n");
188 if (!create_ssl_connection(serverssl2, clientssl2)) {
189 printf("Unable to create second SSL connection\n");
193 sess2 = SSL_get1_session(clientssl2);
195 printf("Unexpected NULL session from clientssl2\n");
199 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
200 printf("Remove session callback unexpectedly called\n");
205 * This should clear sess2 from the cache because it is a "bad" session. See
206 * SSL_set_session() documentation.
208 if (!SSL_set_session(clientssl2, sess1)) {
209 printf("Unexpected failure setting session\n");
213 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
214 printf("Failed to call callback to remove session\n");
219 if (SSL_get_session(clientssl2) != sess1) {
220 printf("Unexpected session found\n");
224 if (fix.use_int_cache) {
225 if (!SSL_CTX_add_session(cctx, sess2)) {
227 * Should have succeeded because it should not already be in the cache
229 printf("Unexpected failure adding session to cache\n");
233 if (!SSL_CTX_remove_session(cctx, sess2)) {
234 printf("Unexpected failure removing session from cache\n");
238 /* This is for the purposes of internal cache testing...ignore the
239 * counter for external cache
241 if (fix.use_ext_cache)
245 /* This shouldn't be in the cache so should fail */
246 if (SSL_CTX_remove_session(cctx, sess2)) {
247 printf("Unexpected success removing session from cache\n");
251 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
252 printf("Failed to call callback to remove session #2\n");
256 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
257 /* Force a connection failure */
258 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
260 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
261 printf("Unable to create third SSL objects\n");
265 if (!SSL_set_session(clientssl3, sess1)) {
266 printf("Unable to set session for third connection\n");
270 /* This should fail because of the mismatched protocol versions */
271 if (create_ssl_connection(serverssl3, clientssl3)) {
272 printf("Unable to create third SSL connection\n");
277 /* We should have automatically removed the session from the cache */
278 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
279 printf("Failed to call callback to remove session #2\n");
283 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
285 * Should have succeeded because it should not already be in the cache
287 printf("Unexpected failure adding session to cache #2\n");
295 SSL_free(serverssl1);
296 SSL_free(clientssl1);
297 SSL_free(serverssl2);
298 SSL_free(clientssl2);
299 #ifndef OPENSSL_NO_TLS1_1
300 SSL_free(serverssl3);
301 SSL_free(clientssl3);
303 SSL_SESSION_free(sess1);
304 SSL_SESSION_free(sess2);
306 * Check if we need to remove any sessions up-refed for the external cache
309 SSL_SESSION_free(sess1);
311 SSL_SESSION_free(sess2);
318 static int test_session_with_only_int_cache(void)
320 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
322 fixture.use_ext_cache = 0;
324 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
327 static int test_session_with_only_ext_cache(void)
329 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
331 fixture.use_int_cache = 0;
333 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
336 static int test_session_with_both_cache(void)
338 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
340 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
347 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
349 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
364 static int test_ssl_set_bio(int idx)
366 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
369 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
371 int initrbio, initwbio, newrbio, newwbio;
375 printf("Failed to allocate SSL_CTX\n");
381 printf("Failed to allocate SSL object\n");
392 OPENSSL_assert(newwbio <= 2);
394 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
395 || newwbio == USE_BIO_1) {
396 bio1 = BIO_new(BIO_s_mem());
398 printf("Failed to allocate bio1\n");
403 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
404 || newwbio == USE_BIO_2) {
405 bio2 = BIO_new(BIO_s_mem());
407 printf("Failed to allocate bio2\n");
412 setupbio(&irbio, bio1, bio2, initrbio);
413 setupbio(&iwbio, bio1, bio2, initwbio);
416 * We want to maintain our own refs to these BIO, so do an up ref for each
417 * BIO that will have ownersip transferred in the SSL_set_bio() call
421 if (iwbio != NULL && iwbio != irbio)
424 SSL_set_bio(ssl, irbio, iwbio);
426 setupbio(&nrbio, bio1, bio2, newrbio);
427 setupbio(&nwbio, bio1, bio2, newwbio);
430 * We will (maybe) transfer ownership again so do more up refs.
431 * SSL_set_bio() has some really complicated ownership rules where BIOs have
434 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
436 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
439 SSL_set_bio(ssl, nrbio, nwbio);
448 * This test is checking that the ref counting for SSL_set_bio is correct.
449 * If we get here and we did too many frees then we will fail in the above
450 * functions. If we haven't done enough then this will only be detected in
451 * a crypto-mdebug build
458 typedef struct ssl_bio_test_fixture {
459 const char *test_case_name;
461 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
462 } SSL_BIO_TEST_FIXTURE;
464 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
466 SSL_BIO_TEST_FIXTURE fixture;
468 fixture.test_case_name = test_case_name;
470 fixture.change_bio = NO_BIO_CHANGE;
475 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
479 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
481 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
482 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
487 printf("Failed to allocate SSL_CTX\n");
493 printf("Failed to allocate SSL object\n");
497 sslbio = BIO_new(BIO_f_ssl());
498 membio1 = BIO_new(BIO_s_mem());
500 if (sslbio == NULL || membio1 == NULL) {
501 printf("Malloc failure creating BIOs\n");
505 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
508 * If anything goes wrong here then we could leak memory, so this will
509 * be caught in a crypto-mdebug build
511 BIO_push(sslbio, membio1);
513 /* Verify chaning the rbio/wbio directly does not cause leaks */
514 if (fix.change_bio != NO_BIO_CHANGE) {
515 membio2 = BIO_new(BIO_s_mem());
516 if (membio2 == NULL) {
517 printf("Malloc failure creating membio2\n");
520 if (fix.change_bio == CHANGE_RBIO)
521 SSL_set0_rbio(ssl, membio2);
523 SSL_set0_wbio(ssl, membio2);
542 static int test_ssl_bio_pop_next_bio(void)
544 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
546 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
549 static int test_ssl_bio_pop_ssl_bio(void)
551 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
555 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
558 static int test_ssl_bio_change_rbio(void)
560 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
562 fixture.change_bio = CHANGE_RBIO;
564 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
567 static int test_ssl_bio_change_wbio(void)
569 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
571 fixture.change_bio = CHANGE_WBIO;
573 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
576 int main(int argc, char *argv[])
582 printf("Invalid argument count\n");
589 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
591 CRYPTO_set_mem_debug(1);
592 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
594 ADD_TEST(test_tlsext_status_type);
595 ADD_TEST(test_session_with_only_int_cache);
596 ADD_TEST(test_session_with_only_ext_cache);
597 ADD_TEST(test_session_with_both_cache);
598 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
599 ADD_TEST(test_ssl_bio_pop_next_bio);
600 ADD_TEST(test_ssl_bio_pop_ssl_bio);
601 ADD_TEST(test_ssl_bio_change_rbio);
602 ADD_TEST(test_ssl_bio_change_wbio);
604 testresult = run_tests(argv[0]);
606 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
607 if (CRYPTO_mem_leaks(err) <= 0)