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;
174 static int test_tlsext_status_type(void)
176 SSL_CTX *cctx = NULL, *sctx = NULL;
177 SSL *clientssl = NULL, *serverssl = NULL;
179 STACK_OF(OCSP_RESPID) *ids = NULL;
180 OCSP_RESPID *id = NULL;
183 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
184 &cctx, cert, privkey)) {
185 printf("Unable to create SSL_CTX pair\n");
189 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
190 printf("Unexpected initial value for "
191 "SSL_CTX_get_tlsext_status_type()\n");
195 /* First just do various checks getting and setting tlsext_status_type */
197 clientssl = SSL_new(cctx);
198 if (SSL_get_tlsext_status_type(clientssl) != -1) {
199 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
203 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
204 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
208 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
209 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
216 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
217 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
221 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
222 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
226 clientssl = SSL_new(cctx);
228 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
229 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
237 * Now actually do a handshake and check OCSP information is exchanged and
238 * the callbacks get called
241 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
242 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
243 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
244 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
246 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
247 printf("Unable to create SSL objects\n");
251 if (!create_ssl_connection(serverssl, clientssl)) {
252 printf("Unable to create SSL connection\n");
256 if (!ocsp_client_called || !ocsp_server_called) {
257 printf("OCSP callbacks not called\n");
266 /* Try again but this time force the server side callback to fail */
267 ocsp_client_called = 0;
268 ocsp_server_called = 0;
271 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
272 printf("Unable to create SSL objects\n");
276 /* This should fail because the callback will fail */
277 if (create_ssl_connection(serverssl, clientssl)) {
278 printf("Unexpected success creating the connection\n");
282 if (ocsp_client_called || ocsp_server_called) {
283 printf("OCSP callbacks successfully called unexpectedly\n");
293 * This time we'll get the client to send an OCSP_RESPID that it will
296 ocsp_client_called = 0;
297 ocsp_server_called = 0;
300 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
301 printf("Unable to create SSL objects\n");
306 * We'll just use any old cert for this test - it doesn't have to be an OCSP
307 * specifc one. We'll use the server cert.
309 certbio = BIO_new_file(cert, "r");
310 if (certbio == NULL) {
311 printf("Can't load the certficate file\n");
314 id = OCSP_RESPID_new();
315 ids = sk_OCSP_RESPID_new_null();
316 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
317 if (id == NULL || ids == NULL || ocspcert == NULL
318 || !OCSP_RESPID_set_by_key(id, ocspcert)
319 || !sk_OCSP_RESPID_push(ids, id)) {
320 printf("Unable to set OCSP_RESPIDs\n");
324 SSL_set_tlsext_status_ids(clientssl, ids);
325 /* Control has been transferred */
331 if (!create_ssl_connection(serverssl, clientssl)) {
332 printf("Unable to create SSL connection\n");
336 if (!ocsp_client_called || !ocsp_server_called) {
337 printf("OCSP callbacks not called\n");
348 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
349 OCSP_RESPID_free(id);
356 #endif /* ndef OPENSSL_NO_OCSP */
358 typedef struct ssl_session_test_fixture {
359 const char *test_case_name;
362 } SSL_SESSION_TEST_FIXTURE;
364 static int new_called = 0, remove_called = 0;
366 static SSL_SESSION_TEST_FIXTURE
367 ssl_session_set_up(const char *const test_case_name)
369 SSL_SESSION_TEST_FIXTURE fixture;
371 fixture.test_case_name = test_case_name;
372 fixture.use_ext_cache = 1;
373 fixture.use_int_cache = 1;
375 new_called = remove_called = 0;
380 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
384 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
391 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
396 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
398 SSL_CTX *sctx = NULL, *cctx = NULL;
399 SSL *serverssl1 = NULL, *clientssl1 = NULL;
400 SSL *serverssl2 = NULL, *clientssl2 = NULL;
401 #ifndef OPENSSL_NO_TLS1_1
402 SSL *serverssl3 = NULL, *clientssl3 = NULL;
404 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
407 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
408 &cctx, cert, privkey)) {
409 printf("Unable to create SSL_CTX pair\n");
413 #ifndef OPENSSL_NO_TLS1_2
414 /* Only allow TLS1.2 so we can force a connection failure later */
415 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
418 /* Set up session cache */
419 if (fix.use_ext_cache) {
420 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
421 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
423 if (fix.use_int_cache) {
424 /* Also covers instance where both are set */
425 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
427 SSL_CTX_set_session_cache_mode(cctx,
428 SSL_SESS_CACHE_CLIENT
429 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
432 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
434 printf("Unable to create SSL objects\n");
438 if (!create_ssl_connection(serverssl1, clientssl1)) {
439 printf("Unable to create SSL connection\n");
442 sess1 = SSL_get1_session(clientssl1);
444 printf("Unexpected NULL session\n");
448 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
449 /* Should have failed because it should already be in the cache */
450 printf("Unexpected success adding session to cache\n");
454 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
455 printf("Session not added to cache\n");
459 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
460 printf("Unable to create second SSL objects\n");
464 if (!create_ssl_connection(serverssl2, clientssl2)) {
465 printf("Unable to create second SSL connection\n");
469 sess2 = SSL_get1_session(clientssl2);
471 printf("Unexpected NULL session from clientssl2\n");
475 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
476 printf("Remove session callback unexpectedly called\n");
481 * This should clear sess2 from the cache because it is a "bad" session. See
482 * SSL_set_session() documentation.
484 if (!SSL_set_session(clientssl2, sess1)) {
485 printf("Unexpected failure setting session\n");
489 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
490 printf("Failed to call callback to remove session\n");
495 if (SSL_get_session(clientssl2) != sess1) {
496 printf("Unexpected session found\n");
500 if (fix.use_int_cache) {
501 if (!SSL_CTX_add_session(cctx, sess2)) {
503 * Should have succeeded because it should not already be in the cache
505 printf("Unexpected failure adding session to cache\n");
509 if (!SSL_CTX_remove_session(cctx, sess2)) {
510 printf("Unexpected failure removing session from cache\n");
514 /* This is for the purposes of internal cache testing...ignore the
515 * counter for external cache
517 if (fix.use_ext_cache)
521 /* This shouldn't be in the cache so should fail */
522 if (SSL_CTX_remove_session(cctx, sess2)) {
523 printf("Unexpected success removing session from cache\n");
527 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
528 printf("Failed to call callback to remove session #2\n");
532 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
533 /* Force a connection failure */
534 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
536 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
537 printf("Unable to create third SSL objects\n");
541 if (!SSL_set_session(clientssl3, sess1)) {
542 printf("Unable to set session for third connection\n");
546 /* This should fail because of the mismatched protocol versions */
547 if (create_ssl_connection(serverssl3, clientssl3)) {
548 printf("Unable to create third SSL connection\n");
553 /* We should have automatically removed the session from the cache */
554 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
555 printf("Failed to call callback to remove session #2\n");
559 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
561 * Should have succeeded because it should not already be in the cache
563 printf("Unexpected failure adding session to cache #2\n");
571 SSL_free(serverssl1);
572 SSL_free(clientssl1);
573 SSL_free(serverssl2);
574 SSL_free(clientssl2);
575 #ifndef OPENSSL_NO_TLS1_1
576 SSL_free(serverssl3);
577 SSL_free(clientssl3);
579 SSL_SESSION_free(sess1);
580 SSL_SESSION_free(sess2);
582 * Check if we need to remove any sessions up-refed for the external cache
585 SSL_SESSION_free(sess1);
587 SSL_SESSION_free(sess2);
594 static int test_session_with_only_int_cache(void)
596 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
598 fixture.use_ext_cache = 0;
600 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
603 static int test_session_with_only_ext_cache(void)
605 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
607 fixture.use_int_cache = 0;
609 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
612 static int test_session_with_both_cache(void)
614 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
616 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
623 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
625 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
640 static int test_ssl_set_bio(int idx)
642 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
645 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
647 int initrbio, initwbio, newrbio, newwbio;
651 printf("Failed to allocate SSL_CTX\n");
657 printf("Failed to allocate SSL object\n");
668 OPENSSL_assert(newwbio <= 2);
670 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
671 || newwbio == USE_BIO_1) {
672 bio1 = BIO_new(BIO_s_mem());
674 printf("Failed to allocate bio1\n");
679 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
680 || newwbio == USE_BIO_2) {
681 bio2 = BIO_new(BIO_s_mem());
683 printf("Failed to allocate bio2\n");
688 setupbio(&irbio, bio1, bio2, initrbio);
689 setupbio(&iwbio, bio1, bio2, initwbio);
692 * We want to maintain our own refs to these BIO, so do an up ref for each
693 * BIO that will have ownersip transferred in the SSL_set_bio() call
697 if (iwbio != NULL && iwbio != irbio)
700 SSL_set_bio(ssl, irbio, iwbio);
702 setupbio(&nrbio, bio1, bio2, newrbio);
703 setupbio(&nwbio, bio1, bio2, newwbio);
706 * We will (maybe) transfer ownership again so do more up refs.
707 * SSL_set_bio() has some really complicated ownership rules where BIOs have
710 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
712 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
715 SSL_set_bio(ssl, nrbio, nwbio);
724 * This test is checking that the ref counting for SSL_set_bio is correct.
725 * If we get here and we did too many frees then we will fail in the above
726 * functions. If we haven't done enough then this will only be detected in
727 * a crypto-mdebug build
734 typedef struct ssl_bio_test_fixture {
735 const char *test_case_name;
737 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
738 } SSL_BIO_TEST_FIXTURE;
740 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
742 SSL_BIO_TEST_FIXTURE fixture;
744 fixture.test_case_name = test_case_name;
746 fixture.change_bio = NO_BIO_CHANGE;
751 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
755 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
757 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
758 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
763 printf("Failed to allocate SSL_CTX\n");
769 printf("Failed to allocate SSL object\n");
773 sslbio = BIO_new(BIO_f_ssl());
774 membio1 = BIO_new(BIO_s_mem());
776 if (sslbio == NULL || membio1 == NULL) {
777 printf("Malloc failure creating BIOs\n");
781 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
784 * If anything goes wrong here then we could leak memory, so this will
785 * be caught in a crypto-mdebug build
787 BIO_push(sslbio, membio1);
789 /* Verify chaning the rbio/wbio directly does not cause leaks */
790 if (fix.change_bio != NO_BIO_CHANGE) {
791 membio2 = BIO_new(BIO_s_mem());
792 if (membio2 == NULL) {
793 printf("Malloc failure creating membio2\n");
796 if (fix.change_bio == CHANGE_RBIO)
797 SSL_set0_rbio(ssl, membio2);
799 SSL_set0_wbio(ssl, membio2);
818 static int test_ssl_bio_pop_next_bio(void)
820 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
822 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
825 static int test_ssl_bio_pop_ssl_bio(void)
827 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
831 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
834 static int test_ssl_bio_change_rbio(void)
836 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
838 fixture.change_bio = CHANGE_RBIO;
840 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
843 static int test_ssl_bio_change_wbio(void)
845 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
847 fixture.change_bio = CHANGE_WBIO;
849 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
852 int main(int argc, char *argv[])
858 printf("Invalid argument count\n");
865 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
867 CRYPTO_set_mem_debug(1);
868 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
870 ADD_TEST(test_large_message_tls);
871 #ifndef OPENSSL_NO_DTLS
872 ADD_TEST(test_large_message_dtls);
874 #ifndef OPENSSL_NO_OCSP
875 ADD_TEST(test_tlsext_status_type);
877 ADD_TEST(test_session_with_only_int_cache);
878 ADD_TEST(test_session_with_only_ext_cache);
879 ADD_TEST(test_session_with_both_cache);
880 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
881 ADD_TEST(test_ssl_bio_pop_next_bio);
882 ADD_TEST(test_ssl_bio_pop_ssl_bio);
883 ADD_TEST(test_ssl_bio_change_rbio);
884 ADD_TEST(test_ssl_bio_change_wbio);
886 testresult = run_tests(argv[0]);
888 bio_s_mempacket_test_free();
890 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
891 if (CRYPTO_mem_leaks(err) <= 0)