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 #ifndef OPENSSL_NO_OCSP
25 static const unsigned char orespder[] = "Dummy OCSP Response";
26 static int ocsp_server_called = 0;
27 static int ocsp_client_called = 0;
29 static int cdummyarg = 1;
30 static X509 *ocspcert = NULL;
33 #define NUM_EXTRA_CERTS 40
35 static int execute_test_large_message(const SSL_METHOD *smeth,
36 const SSL_METHOD *cmeth)
38 SSL_CTX *cctx = NULL, *sctx = NULL;
39 SSL *clientssl = NULL, *serverssl = NULL;
42 BIO *certbio = BIO_new_file(cert, "r");
43 X509 *chaincert = NULL;
46 if (certbio == NULL) {
47 printf("Can't load the certficate file\n");
50 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
53 if (chaincert == NULL) {
54 printf("Unable to load certificate for chain\n");
58 if (!create_ssl_ctx_pair(smeth, cmeth, &sctx,
59 &cctx, cert, privkey)) {
60 printf("Unable to create SSL_CTX pair\n");
65 * We assume the supplied certificate is big enough so that if we add
66 * NUM_EXTRA_CERTS it will make the overall message large enough. The
67 * default buffer size is requested to be 16k, but due to the way BUF_MEM
68 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
69 * we need to have a message larger than that.
71 certlen = i2d_X509(chaincert, NULL);
72 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
73 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
74 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
75 if (!X509_up_ref(chaincert)) {
76 printf("Unable to up ref cert\n");
79 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
80 printf("Unable to add extra chain cert %d\n", i);
86 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
87 printf("Unable to create SSL objects\n");
91 if (!create_ssl_connection(serverssl, clientssl)) {
92 printf("Unable to create SSL connection\n");
108 static int test_large_message_tls(void)
110 return execute_test_large_message(TLS_server_method(), TLS_client_method());
113 #ifndef OPENSSL_NO_DTLS
114 static int test_large_message_dtls(void)
116 return execute_test_large_message(DTLS_server_method(),
117 DTLS_client_method());
121 #ifndef OPENSSL_NO_OCSP
122 static int ocsp_server_cb(SSL *s, void *arg)
124 int *argi = (int *)arg;
125 unsigned char *orespdercopy = NULL;
126 STACK_OF(OCSP_RESPID) *ids = NULL;
127 OCSP_RESPID *id = NULL;
130 /* In this test we are expecting exactly 1 OCSP_RESPID */
131 SSL_get_tlsext_status_ids(s, &ids);
132 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
133 return SSL_TLSEXT_ERR_ALERT_FATAL;
135 id = sk_OCSP_RESPID_value(ids, 0);
136 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
137 return SSL_TLSEXT_ERR_ALERT_FATAL;
138 } else if (*argi != 1) {
139 return SSL_TLSEXT_ERR_ALERT_FATAL;
143 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
144 if (orespdercopy == NULL)
145 return SSL_TLSEXT_ERR_ALERT_FATAL;
147 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
149 ocsp_server_called = 1;
151 return SSL_TLSEXT_ERR_OK;
154 static int ocsp_client_cb(SSL *s, void *arg)
156 int *argi = (int *)arg;
157 const unsigned char *respderin;
160 if (*argi != 1 && *argi != 2)
163 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
165 if (memcmp(orespder, respderin, len) != 0)
168 ocsp_client_called = 1;
173 static int test_tlsext_status_type(void)
175 SSL_CTX *cctx = NULL, *sctx = NULL;
176 SSL *clientssl = NULL, *serverssl = NULL;
178 STACK_OF(OCSP_RESPID) *ids = NULL;
179 OCSP_RESPID *id = NULL;
182 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
183 &cctx, cert, privkey)) {
184 printf("Unable to create SSL_CTX pair\n");
188 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
189 printf("Unexpected initial value for "
190 "SSL_CTX_get_tlsext_status_type()\n");
194 /* First just do various checks getting and setting tlsext_status_type */
196 clientssl = SSL_new(cctx);
197 if (SSL_get_tlsext_status_type(clientssl) != -1) {
198 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
202 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
203 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
207 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
208 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
215 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
216 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
220 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
221 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
225 clientssl = SSL_new(cctx);
227 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
228 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
236 * Now actually do a handshake and check OCSP information is exchanged and
237 * the callbacks get called
240 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
241 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
242 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
243 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
245 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
246 printf("Unable to create SSL objects\n");
250 if (!create_ssl_connection(serverssl, clientssl)) {
251 printf("Unable to create SSL connection\n");
255 if (!ocsp_client_called || !ocsp_server_called) {
256 printf("OCSP callbacks not called\n");
265 /* Try again but this time force the server side callback to fail */
266 ocsp_client_called = 0;
267 ocsp_server_called = 0;
270 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
271 printf("Unable to create SSL objects\n");
275 /* This should fail because the callback will fail */
276 if (create_ssl_connection(serverssl, clientssl)) {
277 printf("Unexpected success creating the connection\n");
281 if (ocsp_client_called || ocsp_server_called) {
282 printf("OCSP callbacks successfully called unexpectedly\n");
292 * This time we'll get the client to send an OCSP_RESPID that it will
295 ocsp_client_called = 0;
296 ocsp_server_called = 0;
299 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
300 printf("Unable to create SSL objects\n");
305 * We'll just use any old cert for this test - it doesn't have to be an OCSP
306 * specifc one. We'll use the server cert.
308 certbio = BIO_new_file(cert, "r");
309 if (certbio == NULL) {
310 printf("Can't load the certficate file\n");
313 id = OCSP_RESPID_new();
314 ids = sk_OCSP_RESPID_new_null();
315 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
316 if (id == NULL || ids == NULL || ocspcert == NULL
317 || !OCSP_RESPID_set_by_key(id, ocspcert)
318 || !sk_OCSP_RESPID_push(ids, id)) {
319 printf("Unable to set OCSP_RESPIDs\n");
323 SSL_set_tlsext_status_ids(clientssl, ids);
324 /* Control has been transferred */
330 if (!create_ssl_connection(serverssl, clientssl)) {
331 printf("Unable to create SSL connection\n");
335 if (!ocsp_client_called || !ocsp_server_called) {
336 printf("OCSP callbacks not called\n");
347 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
348 OCSP_RESPID_free(id);
355 #endif /* ndef OPENSSL_NO_OCSP */
357 typedef struct ssl_session_test_fixture {
358 const char *test_case_name;
361 } SSL_SESSION_TEST_FIXTURE;
363 static int new_called = 0, remove_called = 0;
365 static SSL_SESSION_TEST_FIXTURE
366 ssl_session_set_up(const char *const test_case_name)
368 SSL_SESSION_TEST_FIXTURE fixture;
370 fixture.test_case_name = test_case_name;
371 fixture.use_ext_cache = 1;
372 fixture.use_int_cache = 1;
374 new_called = remove_called = 0;
379 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
383 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
390 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
395 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
397 SSL_CTX *sctx = NULL, *cctx = NULL;
398 SSL *serverssl1 = NULL, *clientssl1 = NULL;
399 SSL *serverssl2 = NULL, *clientssl2 = NULL;
400 #ifndef OPENSSL_NO_TLS1_1
401 SSL *serverssl3 = NULL, *clientssl3 = NULL;
403 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
406 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
407 &cctx, cert, privkey)) {
408 printf("Unable to create SSL_CTX pair\n");
412 #ifndef OPENSSL_NO_TLS1_2
413 /* Only allow TLS1.2 so we can force a connection failure later */
414 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
417 /* Set up session cache */
418 if (fix.use_ext_cache) {
419 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
420 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
422 if (fix.use_int_cache) {
423 /* Also covers instance where both are set */
424 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
426 SSL_CTX_set_session_cache_mode(cctx,
427 SSL_SESS_CACHE_CLIENT
428 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
431 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
433 printf("Unable to create SSL objects\n");
437 if (!create_ssl_connection(serverssl1, clientssl1)) {
438 printf("Unable to create SSL connection\n");
441 sess1 = SSL_get1_session(clientssl1);
443 printf("Unexpected NULL session\n");
447 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
448 /* Should have failed because it should already be in the cache */
449 printf("Unexpected success adding session to cache\n");
453 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
454 printf("Session not added to cache\n");
458 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
459 printf("Unable to create second SSL objects\n");
463 if (!create_ssl_connection(serverssl2, clientssl2)) {
464 printf("Unable to create second SSL connection\n");
468 sess2 = SSL_get1_session(clientssl2);
470 printf("Unexpected NULL session from clientssl2\n");
474 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
475 printf("Remove session callback unexpectedly called\n");
480 * This should clear sess2 from the cache because it is a "bad" session. See
481 * SSL_set_session() documentation.
483 if (!SSL_set_session(clientssl2, sess1)) {
484 printf("Unexpected failure setting session\n");
488 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
489 printf("Failed to call callback to remove session\n");
494 if (SSL_get_session(clientssl2) != sess1) {
495 printf("Unexpected session found\n");
499 if (fix.use_int_cache) {
500 if (!SSL_CTX_add_session(cctx, sess2)) {
502 * Should have succeeded because it should not already be in the cache
504 printf("Unexpected failure adding session to cache\n");
508 if (!SSL_CTX_remove_session(cctx, sess2)) {
509 printf("Unexpected failure removing session from cache\n");
513 /* This is for the purposes of internal cache testing...ignore the
514 * counter for external cache
516 if (fix.use_ext_cache)
520 /* This shouldn't be in the cache so should fail */
521 if (SSL_CTX_remove_session(cctx, sess2)) {
522 printf("Unexpected success removing session from cache\n");
526 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
527 printf("Failed to call callback to remove session #2\n");
531 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
532 /* Force a connection failure */
533 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
535 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
536 printf("Unable to create third SSL objects\n");
540 if (!SSL_set_session(clientssl3, sess1)) {
541 printf("Unable to set session for third connection\n");
545 /* This should fail because of the mismatched protocol versions */
546 if (create_ssl_connection(serverssl3, clientssl3)) {
547 printf("Unable to create third SSL connection\n");
552 /* We should have automatically removed the session from the cache */
553 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
554 printf("Failed to call callback to remove session #2\n");
558 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
560 * Should have succeeded because it should not already be in the cache
562 printf("Unexpected failure adding session to cache #2\n");
570 SSL_free(serverssl1);
571 SSL_free(clientssl1);
572 SSL_free(serverssl2);
573 SSL_free(clientssl2);
574 #ifndef OPENSSL_NO_TLS1_1
575 SSL_free(serverssl3);
576 SSL_free(clientssl3);
578 SSL_SESSION_free(sess1);
579 SSL_SESSION_free(sess2);
581 * Check if we need to remove any sessions up-refed for the external cache
584 SSL_SESSION_free(sess1);
586 SSL_SESSION_free(sess2);
593 static int test_session_with_only_int_cache(void)
595 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
597 fixture.use_ext_cache = 0;
599 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
602 static int test_session_with_only_ext_cache(void)
604 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
606 fixture.use_int_cache = 0;
608 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
611 static int test_session_with_both_cache(void)
613 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
615 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
622 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
624 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
639 static int test_ssl_set_bio(int idx)
641 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
644 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
646 int initrbio, initwbio, newrbio, newwbio;
650 printf("Failed to allocate SSL_CTX\n");
656 printf("Failed to allocate SSL object\n");
667 OPENSSL_assert(newwbio <= 2);
669 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
670 || newwbio == USE_BIO_1) {
671 bio1 = BIO_new(BIO_s_mem());
673 printf("Failed to allocate bio1\n");
678 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
679 || newwbio == USE_BIO_2) {
680 bio2 = BIO_new(BIO_s_mem());
682 printf("Failed to allocate bio2\n");
687 setupbio(&irbio, bio1, bio2, initrbio);
688 setupbio(&iwbio, bio1, bio2, initwbio);
691 * We want to maintain our own refs to these BIO, so do an up ref for each
692 * BIO that will have ownersip transferred in the SSL_set_bio() call
696 if (iwbio != NULL && iwbio != irbio)
699 SSL_set_bio(ssl, irbio, iwbio);
701 setupbio(&nrbio, bio1, bio2, newrbio);
702 setupbio(&nwbio, bio1, bio2, newwbio);
705 * We will (maybe) transfer ownership again so do more up refs.
706 * SSL_set_bio() has some really complicated ownership rules where BIOs have
709 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
711 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
714 SSL_set_bio(ssl, nrbio, nwbio);
723 * This test is checking that the ref counting for SSL_set_bio is correct.
724 * If we get here and we did too many frees then we will fail in the above
725 * functions. If we haven't done enough then this will only be detected in
726 * a crypto-mdebug build
733 typedef struct ssl_bio_test_fixture {
734 const char *test_case_name;
736 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
737 } SSL_BIO_TEST_FIXTURE;
739 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
741 SSL_BIO_TEST_FIXTURE fixture;
743 fixture.test_case_name = test_case_name;
745 fixture.change_bio = NO_BIO_CHANGE;
750 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
754 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
756 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
757 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
762 printf("Failed to allocate SSL_CTX\n");
768 printf("Failed to allocate SSL object\n");
772 sslbio = BIO_new(BIO_f_ssl());
773 membio1 = BIO_new(BIO_s_mem());
775 if (sslbio == NULL || membio1 == NULL) {
776 printf("Malloc failure creating BIOs\n");
780 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
783 * If anything goes wrong here then we could leak memory, so this will
784 * be caught in a crypto-mdebug build
786 BIO_push(sslbio, membio1);
788 /* Verify chaning the rbio/wbio directly does not cause leaks */
789 if (fix.change_bio != NO_BIO_CHANGE) {
790 membio2 = BIO_new(BIO_s_mem());
791 if (membio2 == NULL) {
792 printf("Malloc failure creating membio2\n");
795 if (fix.change_bio == CHANGE_RBIO)
796 SSL_set0_rbio(ssl, membio2);
798 SSL_set0_wbio(ssl, membio2);
817 static int test_ssl_bio_pop_next_bio(void)
819 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
821 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
824 static int test_ssl_bio_pop_ssl_bio(void)
826 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
830 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
833 static int test_ssl_bio_change_rbio(void)
835 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
837 fixture.change_bio = CHANGE_RBIO;
839 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
842 static int test_ssl_bio_change_wbio(void)
844 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
846 fixture.change_bio = CHANGE_WBIO;
848 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
851 int main(int argc, char *argv[])
857 printf("Invalid argument count\n");
864 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
866 CRYPTO_set_mem_debug(1);
867 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
869 ADD_TEST(test_large_message_tls);
870 #ifndef OPENSSL_NO_DTLS
871 ADD_TEST(test_large_message_dtls);
873 #ifndef OPENSSL_NO_OCSP
874 ADD_TEST(test_tlsext_status_type);
876 ADD_TEST(test_session_with_only_int_cache);
877 ADD_TEST(test_session_with_only_ext_cache);
878 ADD_TEST(test_session_with_both_cache);
879 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
880 ADD_TEST(test_ssl_bio_pop_next_bio);
881 ADD_TEST(test_ssl_bio_pop_ssl_bio);
882 ADD_TEST(test_ssl_bio_change_rbio);
883 ADD_TEST(test_ssl_bio_change_wbio);
885 testresult = run_tests(argv[0]);
887 bio_s_mempacket_test_free();
889 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
890 if (CRYPTO_mem_leaks(err) <= 0)