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"
21 static char *cert = NULL;
22 static char *privkey = NULL;
24 static const unsigned char orespder[] = "Dummy OCSP Response";
25 static int ocsp_server_called = 0;
26 static int ocsp_client_called = 0;
28 static int cdummyarg = 1;
29 static X509 *ocspcert = NULL;
31 static int ocsp_server_cb(SSL *s, void *arg)
33 int *argi = (int *)arg;
34 unsigned char *orespdercopy = NULL;
35 STACK_OF(OCSP_RESPID) *ids = NULL;
36 OCSP_RESPID *id = NULL;
39 /* In this test we are expecting exactly 1 OCSP_RESPID */
40 SSL_get_tlsext_status_ids(s, &ids);
41 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
42 return SSL_TLSEXT_ERR_ALERT_FATAL;
44 id = sk_OCSP_RESPID_value(ids, 0);
45 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
46 return SSL_TLSEXT_ERR_ALERT_FATAL;
47 } else if (*argi != 1) {
48 return SSL_TLSEXT_ERR_ALERT_FATAL;
52 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
53 if (orespdercopy == NULL)
54 return SSL_TLSEXT_ERR_ALERT_FATAL;
56 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
58 ocsp_server_called = 1;
60 return SSL_TLSEXT_ERR_OK;
63 static int ocsp_client_cb(SSL *s, void *arg)
65 int *argi = (int *)arg;
66 const unsigned char *respderin;
69 if (*argi != 1 && *argi != 2)
72 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
74 if (memcmp(orespder, respderin, len) != 0)
77 ocsp_client_called = 1;
82 static int test_tlsext_status_type(void)
84 SSL_CTX *cctx = NULL, *sctx = NULL;
85 SSL *clientssl = NULL, *serverssl = NULL;
87 STACK_OF(OCSP_RESPID) *ids = NULL;
88 OCSP_RESPID *id = NULL;
91 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
92 &cctx, cert, privkey)) {
93 printf("Unable to create SSL_CTX pair\n");
97 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
98 printf("Unexpected initial value for "
99 "SSL_CTX_get_tlsext_status_type()\n");
103 /* First just do various checks getting and setting tlsext_status_type */
105 clientssl = SSL_new(cctx);
106 if (SSL_get_tlsext_status_type(clientssl) != -1) {
107 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
111 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
112 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
116 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
117 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
124 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
125 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
129 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
130 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
134 clientssl = SSL_new(cctx);
136 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
137 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
145 * Now actually do a handshake and check OCSP information is exchanged and
146 * the callbacks get called
149 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
150 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
151 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
152 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
154 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
155 printf("Unable to create SSL objects\n");
159 if (!create_ssl_connection(serverssl, clientssl)) {
160 printf("Unable to create SSL connection\n");
164 if (!ocsp_client_called || !ocsp_server_called) {
165 printf("OCSP callbacks not called\n");
174 /* Try again but this time force the server side callback to fail */
175 ocsp_client_called = 0;
176 ocsp_server_called = 0;
179 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
180 printf("Unable to create SSL objects\n");
184 /* This should fail because the callback will fail */
185 if (create_ssl_connection(serverssl, clientssl)) {
186 printf("Unexpected success creating the connection\n");
190 if (ocsp_client_called || ocsp_server_called) {
191 printf("OCSP callbacks successfully called unexpectedly\n");
201 * This time we'll get the client to send an OCSP_RESPID that it will
204 ocsp_client_called = 0;
205 ocsp_server_called = 0;
208 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
209 printf("Unable to create SSL objects\n");
214 * We'll just use any old cert for this test - it doesn't have to be an OCSP
215 * specifc one. We'll use the server cert.
217 certbio = BIO_new_file(cert, "r");
218 if (certbio == NULL) {
219 printf("Can't load the certficate file\n");
222 id = OCSP_RESPID_new();
223 ids = sk_OCSP_RESPID_new_null();
224 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
225 if (id == NULL || ids == NULL || ocspcert == NULL
226 || !OCSP_RESPID_set_by_key(id, ocspcert)
227 || !sk_OCSP_RESPID_push(ids, id)) {
228 printf("Unable to set OCSP_RESPIDs\n");
232 SSL_set_tlsext_status_ids(clientssl, ids);
233 /* Control has been transferred */
239 if (!create_ssl_connection(serverssl, clientssl)) {
240 printf("Unable to create SSL connection\n");
244 if (!ocsp_client_called || !ocsp_server_called) {
245 printf("OCSP callbacks not called\n");
256 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
257 OCSP_RESPID_free(id);
265 typedef struct ssl_session_test_fixture {
266 const char *test_case_name;
269 } SSL_SESSION_TEST_FIXTURE;
271 static int new_called = 0, remove_called = 0;
273 static SSL_SESSION_TEST_FIXTURE
274 ssl_session_set_up(const char *const test_case_name)
276 SSL_SESSION_TEST_FIXTURE fixture;
278 fixture.test_case_name = test_case_name;
279 fixture.use_ext_cache = 1;
280 fixture.use_int_cache = 1;
282 new_called = remove_called = 0;
287 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
291 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
298 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
303 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
305 SSL_CTX *sctx = NULL, *cctx = NULL;
306 SSL *serverssl1 = NULL, *clientssl1 = NULL;
307 SSL *serverssl2 = NULL, *clientssl2 = NULL;
308 #ifndef OPENSSL_NO_TLS1_1
309 SSL *serverssl3 = NULL, *clientssl3 = NULL;
311 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
314 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
315 &cctx, cert, privkey)) {
316 printf("Unable to create SSL_CTX pair\n");
320 #ifndef OPENSSL_NO_TLS1_2
321 /* Only allow TLS1.2 so we can force a connection failure later */
322 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
325 /* Set up session cache */
326 if (fix.use_ext_cache) {
327 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
328 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
330 if (fix.use_int_cache) {
331 /* Also covers instance where both are set */
332 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
334 SSL_CTX_set_session_cache_mode(cctx,
335 SSL_SESS_CACHE_CLIENT
336 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
339 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
341 printf("Unable to create SSL objects\n");
345 if (!create_ssl_connection(serverssl1, clientssl1)) {
346 printf("Unable to create SSL connection\n");
349 sess1 = SSL_get1_session(clientssl1);
351 printf("Unexpected NULL session\n");
355 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
356 /* Should have failed because it should already be in the cache */
357 printf("Unexpected success adding session to cache\n");
361 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
362 printf("Session not added to cache\n");
366 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
367 printf("Unable to create second SSL objects\n");
371 if (!create_ssl_connection(serverssl2, clientssl2)) {
372 printf("Unable to create second SSL connection\n");
376 sess2 = SSL_get1_session(clientssl2);
378 printf("Unexpected NULL session from clientssl2\n");
382 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
383 printf("Remove session callback unexpectedly called\n");
388 * This should clear sess2 from the cache because it is a "bad" session. See
389 * SSL_set_session() documentation.
391 if (!SSL_set_session(clientssl2, sess1)) {
392 printf("Unexpected failure setting session\n");
396 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
397 printf("Failed to call callback to remove session\n");
402 if (SSL_get_session(clientssl2) != sess1) {
403 printf("Unexpected session found\n");
407 if (fix.use_int_cache) {
408 if (!SSL_CTX_add_session(cctx, sess2)) {
410 * Should have succeeded because it should not already be in the cache
412 printf("Unexpected failure adding session to cache\n");
416 if (!SSL_CTX_remove_session(cctx, sess2)) {
417 printf("Unexpected failure removing session from cache\n");
421 /* This is for the purposes of internal cache testing...ignore the
422 * counter for external cache
424 if (fix.use_ext_cache)
428 /* This shouldn't be in the cache so should fail */
429 if (SSL_CTX_remove_session(cctx, sess2)) {
430 printf("Unexpected success removing session from cache\n");
434 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
435 printf("Failed to call callback to remove session #2\n");
439 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
440 /* Force a connection failure */
441 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
443 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
444 printf("Unable to create third SSL objects\n");
448 if (!SSL_set_session(clientssl3, sess1)) {
449 printf("Unable to set session for third connection\n");
453 /* This should fail because of the mismatched protocol versions */
454 if (create_ssl_connection(serverssl3, clientssl3)) {
455 printf("Unable to create third SSL connection\n");
460 /* We should have automatically removed the session from the cache */
461 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
462 printf("Failed to call callback to remove session #2\n");
466 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
468 * Should have succeeded because it should not already be in the cache
470 printf("Unexpected failure adding session to cache #2\n");
478 SSL_free(serverssl1);
479 SSL_free(clientssl1);
480 SSL_free(serverssl2);
481 SSL_free(clientssl2);
482 #ifndef OPENSSL_NO_TLS1_1
483 SSL_free(serverssl3);
484 SSL_free(clientssl3);
486 SSL_SESSION_free(sess1);
487 SSL_SESSION_free(sess2);
489 * Check if we need to remove any sessions up-refed for the external cache
492 SSL_SESSION_free(sess1);
494 SSL_SESSION_free(sess2);
501 static int test_session_with_only_int_cache(void)
503 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
505 fixture.use_ext_cache = 0;
507 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
510 static int test_session_with_only_ext_cache(void)
512 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
514 fixture.use_int_cache = 0;
516 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
519 static int test_session_with_both_cache(void)
521 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
523 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
530 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
532 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
547 static int test_ssl_set_bio(int idx)
549 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
552 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
554 int initrbio, initwbio, newrbio, newwbio;
558 printf("Failed to allocate SSL_CTX\n");
564 printf("Failed to allocate SSL object\n");
575 OPENSSL_assert(newwbio <= 2);
577 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
578 || newwbio == USE_BIO_1) {
579 bio1 = BIO_new(BIO_s_mem());
581 printf("Failed to allocate bio1\n");
586 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
587 || newwbio == USE_BIO_2) {
588 bio2 = BIO_new(BIO_s_mem());
590 printf("Failed to allocate bio2\n");
595 setupbio(&irbio, bio1, bio2, initrbio);
596 setupbio(&iwbio, bio1, bio2, initwbio);
599 * We want to maintain our own refs to these BIO, so do an up ref for each
600 * BIO that will have ownersip transferred in the SSL_set_bio() call
604 if (iwbio != NULL && iwbio != irbio)
607 SSL_set_bio(ssl, irbio, iwbio);
609 setupbio(&nrbio, bio1, bio2, newrbio);
610 setupbio(&nwbio, bio1, bio2, newwbio);
613 * We will (maybe) transfer ownership again so do more up refs.
614 * SSL_set_bio() has some really complicated ownership rules where BIOs have
617 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
619 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
622 SSL_set_bio(ssl, nrbio, nwbio);
631 * This test is checking that the ref counting for SSL_set_bio is correct.
632 * If we get here and we did too many frees then we will fail in the above
633 * functions. If we haven't done enough then this will only be detected in
634 * a crypto-mdebug build
641 typedef struct ssl_bio_test_fixture {
642 const char *test_case_name;
644 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
645 } SSL_BIO_TEST_FIXTURE;
647 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
649 SSL_BIO_TEST_FIXTURE fixture;
651 fixture.test_case_name = test_case_name;
653 fixture.change_bio = NO_BIO_CHANGE;
658 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
662 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
664 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
665 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
670 printf("Failed to allocate SSL_CTX\n");
676 printf("Failed to allocate SSL object\n");
680 sslbio = BIO_new(BIO_f_ssl());
681 membio1 = BIO_new(BIO_s_mem());
683 if (sslbio == NULL || membio1 == NULL) {
684 printf("Malloc failure creating BIOs\n");
688 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
691 * If anything goes wrong here then we could leak memory, so this will
692 * be caught in a crypto-mdebug build
694 BIO_push(sslbio, membio1);
696 /* Verify chaning the rbio/wbio directly does not cause leaks */
697 if (fix.change_bio != NO_BIO_CHANGE) {
698 membio2 = BIO_new(BIO_s_mem());
699 if (membio2 == NULL) {
700 printf("Malloc failure creating membio2\n");
703 if (fix.change_bio == CHANGE_RBIO)
704 SSL_set0_rbio(ssl, membio2);
706 SSL_set0_wbio(ssl, membio2);
725 static int test_ssl_bio_pop_next_bio(void)
727 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
729 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
732 static int test_ssl_bio_pop_ssl_bio(void)
734 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
738 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
741 static int test_ssl_bio_change_rbio(void)
743 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
745 fixture.change_bio = CHANGE_RBIO;
747 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
750 static int test_ssl_bio_change_wbio(void)
752 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
754 fixture.change_bio = CHANGE_WBIO;
756 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
759 int main(int argc, char *argv[])
765 printf("Invalid argument count\n");
772 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
774 CRYPTO_set_mem_debug(1);
775 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
777 ADD_TEST(test_tlsext_status_type);
778 ADD_TEST(test_session_with_only_int_cache);
779 ADD_TEST(test_session_with_only_ext_cache);
780 ADD_TEST(test_session_with_both_cache);
781 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
782 ADD_TEST(test_ssl_bio_pop_next_bio);
783 ADD_TEST(test_ssl_bio_pop_ssl_bio);
784 ADD_TEST(test_ssl_bio_change_rbio);
785 ADD_TEST(test_ssl_bio_change_wbio);
787 testresult = run_tests(argv[0]);
789 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
790 if (CRYPTO_mem_leaks(err) <= 0)