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 SSL
*serverssl3
= NULL
, *clientssl3
= NULL
;
126 SSL_SESSION
*sess1
= NULL
, *sess2
= NULL
;
129 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx
,
130 &cctx
, cert
, privkey
)) {
131 printf("Unable to create SSL_CTX pair\n");
135 #ifndef OPENSSL_NO_TLS1_2
136 /* Only allow TLS1.2 so we can force a connection failure later */
137 SSL_CTX_set_min_proto_version(cctx
, TLS1_2_VERSION
);
140 /* Set up session cache */
141 if (fix
.use_ext_cache
) {
142 SSL_CTX_sess_set_new_cb(cctx
, new_session_cb
);
143 SSL_CTX_sess_set_remove_cb(cctx
, remove_session_cb
);
145 if (fix
.use_int_cache
) {
146 /* Also covers instance where both are set */
147 SSL_CTX_set_session_cache_mode(cctx
, SSL_SESS_CACHE_CLIENT
);
149 SSL_CTX_set_session_cache_mode(cctx
,
150 SSL_SESS_CACHE_CLIENT
151 | SSL_SESS_CACHE_NO_INTERNAL_STORE
);
154 if (!create_ssl_connection(sctx
, cctx
, &serverssl1
, &clientssl1
, NULL
,
156 printf("Unable to create SSL connection\n");
159 sess1
= SSL_get1_session(clientssl1
);
161 printf("Unexpected NULL session\n");
165 if (fix
.use_int_cache
&& SSL_CTX_add_session(cctx
, sess1
)) {
166 /* Should have failed because it should already be in the cache */
167 printf("Unexpected success adding session to cache\n");
171 if (fix
.use_ext_cache
&& (new_called
!= 1 || remove_called
!= 0)) {
172 printf("Session not added to cache\n");
176 if (!create_ssl_connection(sctx
, cctx
, &serverssl2
, &clientssl2
, NULL
,
178 printf("Unable to create second SSL connection\n");
182 sess2
= SSL_get1_session(clientssl2
);
184 printf("Unexpected NULL session from clientssl2\n");
188 if (fix
.use_ext_cache
&& (new_called
!= 2 || remove_called
!= 0)) {
189 printf("Remove session callback unexpectedly called\n");
194 * This should clear sess2 from the cache because it is a "bad" session. See
195 * SSL_set_session() documentation.
197 if (!SSL_set_session(clientssl2
, sess1
)) {
198 printf("Unexpected failure setting session\n");
202 if (fix
.use_ext_cache
&& (new_called
!= 2 || remove_called
!= 1)) {
203 printf("Failed to call callback to remove session\n");
208 if (SSL_get_session(clientssl2
) != sess1
) {
209 printf("Unexpected session found\n");
213 if (fix
.use_int_cache
) {
214 if (!SSL_CTX_add_session(cctx
, sess2
)) {
216 * Should have succeeded because it should not already be in the cache
218 printf("Unexpected failure adding session to cache\n");
222 if (!SSL_CTX_remove_session(cctx
, sess2
)) {
223 printf("Unexpected failure removing session from cache\n");
227 /* This is for the purposes of internal cache testing...ignore the
228 * counter for external cache
230 if (fix
.use_ext_cache
)
234 /* This shouldn't be in the cache so should fail */
235 if (SSL_CTX_remove_session(cctx
, sess2
)) {
236 printf("Unexpected success removing session from cache\n");
240 if (fix
.use_ext_cache
&& (new_called
!= 2 || remove_called
!= 2)) {
241 printf("Failed to call callback to remove session #2\n");
245 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
246 /* Force a connection failure */
247 SSL_CTX_set_max_proto_version(sctx
, TLS1_1_VERSION
);
248 clientssl3
= SSL_new(cctx
);
249 if (clientssl3
== NULL
) {
250 printf("Malloc failure\n");
253 if (!SSL_set_session(clientssl3
, sess1
)) {
254 printf("Unable to set session for third connection\n");
258 /* This should fail because of the mismatched protocol versions */
259 if (create_ssl_connection(sctx
, cctx
, &serverssl3
, &clientssl3
, NULL
,
261 printf("Unexpected success creating SSL connection\n");
265 /* We should have automatically removed the session from the cache */
266 if (fix
.use_ext_cache
&& (new_called
!= 2 || remove_called
!= 3)) {
267 printf("Failed to call callback to remove session #2\n");
271 if (fix
.use_int_cache
&& !SSL_CTX_add_session(cctx
, sess2
)) {
273 * Should have succeeded because it should not already be in the cache
275 printf("Unexpected failure adding session to cache #2\n");
283 SSL_free(serverssl1
);
284 SSL_free(clientssl1
);
285 SSL_free(serverssl2
);
286 SSL_free(clientssl2
);
287 SSL_free(serverssl3
);
288 SSL_free(clientssl3
);
289 SSL_SESSION_free(sess1
);
290 SSL_SESSION_free(sess2
);
292 * Check if we need to remove any sessions up-refed for the external cache
295 SSL_SESSION_free(sess1
);
297 SSL_SESSION_free(sess2
);
304 static int test_session_with_only_int_cache(void)
306 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE
, ssl_session_set_up
);
308 fixture
.use_ext_cache
= 0;
310 EXECUTE_TEST(execute_test_session
, ssl_session_tear_down
);
313 static int test_session_with_only_ext_cache(void)
315 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE
, ssl_session_set_up
);
317 fixture
.use_int_cache
= 0;
319 EXECUTE_TEST(execute_test_session
, ssl_session_tear_down
);
322 static int test_session_with_both_cache(void)
324 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE
, ssl_session_set_up
);
326 EXECUTE_TEST(execute_test_session
, ssl_session_tear_down
);
333 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
335 static void setupbio(BIO
**res
, BIO
*bio1
, BIO
*bio2
, int type
)
350 static int test_ssl_set_bio(int idx
)
352 SSL_CTX
*ctx
= SSL_CTX_new(TLS_method());
355 BIO
*irbio
, *iwbio
, *nrbio
, *nwbio
;
357 int initrbio
, initwbio
, newrbio
, newwbio
;
361 printf("Failed to allocate SSL_CTX\n");
367 printf("Failed to allocate SSL object\n");
378 OPENSSL_assert(newwbio
<= 2);
380 if (initrbio
== USE_BIO_1
|| initwbio
== USE_BIO_1
|| newrbio
== USE_BIO_1
381 || newwbio
== USE_BIO_1
) {
382 bio1
= BIO_new(BIO_s_mem());
384 printf("Failed to allocate bio1\n");
389 if (initrbio
== USE_BIO_2
|| initwbio
== USE_BIO_2
|| newrbio
== USE_BIO_2
390 || newwbio
== USE_BIO_2
) {
391 bio2
= BIO_new(BIO_s_mem());
393 printf("Failed to allocate bio2\n");
398 setupbio(&irbio
, bio1
, bio2
, initrbio
);
399 setupbio(&iwbio
, bio1
, bio2
, initwbio
);
402 * We want to maintain our own refs to these BIO, so do an up ref for each
403 * BIO that will have ownersip transferred in the SSL_set_bio() call
407 if (iwbio
!= NULL
&& iwbio
!= irbio
)
410 SSL_set_bio(ssl
, irbio
, iwbio
);
412 setupbio(&nrbio
, bio1
, bio2
, newrbio
);
413 setupbio(&nwbio
, bio1
, bio2
, newwbio
);
416 * We will (maybe) transfer ownership again so do more up refs.
417 * SSL_set_bio() has some really complicated ownership rules where BIOs have
420 if (nrbio
!= NULL
&& nrbio
!= irbio
&& (nwbio
!= iwbio
|| nrbio
!= nwbio
))
422 if (nwbio
!= NULL
&& nwbio
!= nrbio
&& (nwbio
!= iwbio
|| (nwbio
== iwbio
&& irbio
== iwbio
)))
425 SSL_set_bio(ssl
, nrbio
, nwbio
);
434 * This test is checking that the ref counting for SSL_set_bio is correct.
435 * If we get here and we did too many frees then we will fail in the above
436 * functions. If we haven't done enough then this will only be detected in
437 * a crypto-mdebug build
444 typedef struct ssl_bio_test_fixture
{
445 const char *test_case_name
;
447 enum { NO_BIO_CHANGE
, CHANGE_RBIO
, CHANGE_WBIO
} change_bio
;
448 } SSL_BIO_TEST_FIXTURE
;
450 static SSL_BIO_TEST_FIXTURE
ssl_bio_set_up(const char *const test_case_name
)
452 SSL_BIO_TEST_FIXTURE fixture
;
454 fixture
.test_case_name
= test_case_name
;
460 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture
)
464 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix
)
466 BIO
*sslbio
= NULL
, *membio1
= NULL
, *membio2
= NULL
;
467 SSL_CTX
*ctx
= SSL_CTX_new(TLS_method());
472 printf("Failed to allocate SSL_CTX\n");
478 printf("Failed to allocate SSL object\n");
482 sslbio
= BIO_new(BIO_f_ssl());
483 membio1
= BIO_new(BIO_s_mem());
485 if (sslbio
== NULL
|| membio1
== NULL
) {
486 printf("Malloc failure creating BIOs\n");
490 BIO_set_ssl(sslbio
, ssl
, BIO_CLOSE
);
493 * If anything goes wrong here then we could leak memory, so this will
494 * be caught in a crypto-mdebug build
496 BIO_push(sslbio
, membio1
);
498 /* Verify chaning the rbio/wbio directly does not cause leaks */
499 if (fix
.change_bio
!= NO_BIO_CHANGE
) {
500 membio2
= BIO_new(BIO_s_mem());
501 if (membio2
== NULL
) {
502 printf("Malloc failure creating membio2\n");
505 if (fix
.change_bio
== CHANGE_RBIO
)
506 SSL_set_rbio(ssl
, membio2
);
508 SSL_set_wbio(ssl
, membio2
);
527 static int test_ssl_bio_pop_next_bio(void)
529 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE
, ssl_bio_set_up
);
531 EXECUTE_TEST(execute_test_ssl_bio
, ssl_bio_tear_down
);
534 static int test_ssl_bio_pop_ssl_bio(void)
536 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE
, ssl_bio_set_up
);
540 EXECUTE_TEST(execute_test_ssl_bio
, ssl_bio_tear_down
);
543 static int test_ssl_bio_change_rbio(void)
545 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE
, ssl_bio_set_up
);
547 fixture
.change_bio
= CHANGE_RBIO
;
549 EXECUTE_TEST(execute_test_ssl_bio
, ssl_bio_tear_down
);
552 static int test_ssl_bio_change_wbio(void)
554 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE
, ssl_bio_set_up
);
556 fixture
.change_bio
= CHANGE_WBIO
;
558 EXECUTE_TEST(execute_test_ssl_bio
, ssl_bio_tear_down
);
561 int main(int argc
, char *argv
[])
567 printf("Invalid argument count\n");
574 err
= BIO_new_fp(stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
576 CRYPTO_set_mem_debug(1);
577 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
579 ADD_TEST(test_tlsext_status_type
);
580 ADD_TEST(test_session_with_only_int_cache
);
581 ADD_TEST(test_session_with_only_ext_cache
);
582 ADD_TEST(test_session_with_both_cache
);
583 ADD_ALL_TESTS(test_ssl_set_bio
, TOTAL_SSL_SET_BIO_TESTS
);
584 ADD_TEST(test_ssl_bio_pop_next_bio
);
585 ADD_TEST(test_ssl_bio_pop_ssl_bio
);
586 ADD_TEST(test_ssl_bio_change_rbio
);
587 ADD_TEST(test_ssl_bio_change_wbio
);
589 testresult
= run_tests(argv
[0]);
591 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
592 if (CRYPTO_mem_leaks(err
) <= 0)