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 #define NUM_EXTRA_CERTS 40
33 static int execute_test_large_message(const SSL_METHOD *smeth,
34 const SSL_METHOD *cmeth)
36 SSL_CTX *cctx = NULL, *sctx = NULL;
37 SSL *clientssl = NULL, *serverssl = NULL;
40 BIO *certbio = BIO_new_file(cert, "r");
41 X509 *chaincert = NULL;
44 if (certbio == NULL) {
45 printf("Can't load the certficate file\n");
48 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
50 if (!create_ssl_ctx_pair(smeth, cmeth, &sctx,
51 &cctx, cert, privkey)) {
52 printf("Unable to create SSL_CTX pair\n");
59 * We assume the supplied certificate is big enough so that if we add
60 * NUM_EXTRA_CERTS it will make the overall message large enough. The
61 * default buffer size is requested to be 16k, but due to the way BUF_MEM
62 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
63 * we need to have a message larger than that.
65 certlen = i2d_X509(chaincert, NULL);
66 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
67 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
68 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
69 if (!X509_up_ref(chaincert)) {
70 printf("Unable to up ref cert\n");
73 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
74 printf("Unable to add extra chain cert %d\n", i);
80 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
81 printf("Unable to create SSL objects\n");
85 if (!create_ssl_connection(serverssl, clientssl)) {
86 printf("Unable to create SSL connection\n");
102 static int test_large_message_tls(void)
104 return execute_test_large_message(TLS_server_method(), TLS_client_method());
107 static int test_large_message_dtls(void)
109 return execute_test_large_message(DTLS_server_method(),
110 DTLS_client_method());
113 static int ocsp_server_cb(SSL *s, void *arg)
115 int *argi = (int *)arg;
116 unsigned char *orespdercopy = NULL;
117 STACK_OF(OCSP_RESPID) *ids = NULL;
118 OCSP_RESPID *id = NULL;
121 /* In this test we are expecting exactly 1 OCSP_RESPID */
122 SSL_get_tlsext_status_ids(s, &ids);
123 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
124 return SSL_TLSEXT_ERR_ALERT_FATAL;
126 id = sk_OCSP_RESPID_value(ids, 0);
127 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
128 return SSL_TLSEXT_ERR_ALERT_FATAL;
129 } else if (*argi != 1) {
130 return SSL_TLSEXT_ERR_ALERT_FATAL;
134 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
135 if (orespdercopy == NULL)
136 return SSL_TLSEXT_ERR_ALERT_FATAL;
138 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
140 ocsp_server_called = 1;
142 return SSL_TLSEXT_ERR_OK;
145 static int ocsp_client_cb(SSL *s, void *arg)
147 int *argi = (int *)arg;
148 const unsigned char *respderin;
151 if (*argi != 1 && *argi != 2)
154 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
156 if (memcmp(orespder, respderin, len) != 0)
159 ocsp_client_called = 1;
164 static int test_tlsext_status_type(void)
166 SSL_CTX *cctx = NULL, *sctx = NULL;
167 SSL *clientssl = NULL, *serverssl = NULL;
169 STACK_OF(OCSP_RESPID) *ids = NULL;
170 OCSP_RESPID *id = NULL;
173 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
174 &cctx, cert, privkey)) {
175 printf("Unable to create SSL_CTX pair\n");
179 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
180 printf("Unexpected initial value for "
181 "SSL_CTX_get_tlsext_status_type()\n");
185 /* First just do various checks getting and setting tlsext_status_type */
187 clientssl = SSL_new(cctx);
188 if (SSL_get_tlsext_status_type(clientssl) != -1) {
189 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
193 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
194 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
198 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
199 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
206 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
207 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
211 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
212 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
216 clientssl = SSL_new(cctx);
218 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
219 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
227 * Now actually do a handshake and check OCSP information is exchanged and
228 * the callbacks get called
231 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
232 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
233 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
234 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
236 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
237 printf("Unable to create SSL objects\n");
241 if (!create_ssl_connection(serverssl, clientssl)) {
242 printf("Unable to create SSL connection\n");
246 if (!ocsp_client_called || !ocsp_server_called) {
247 printf("OCSP callbacks not called\n");
256 /* Try again but this time force the server side callback to fail */
257 ocsp_client_called = 0;
258 ocsp_server_called = 0;
261 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
262 printf("Unable to create SSL objects\n");
266 /* This should fail because the callback will fail */
267 if (create_ssl_connection(serverssl, clientssl)) {
268 printf("Unexpected success creating the connection\n");
272 if (ocsp_client_called || ocsp_server_called) {
273 printf("OCSP callbacks successfully called unexpectedly\n");
283 * This time we'll get the client to send an OCSP_RESPID that it will
286 ocsp_client_called = 0;
287 ocsp_server_called = 0;
290 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
291 printf("Unable to create SSL objects\n");
296 * We'll just use any old cert for this test - it doesn't have to be an OCSP
297 * specifc one. We'll use the server cert.
299 certbio = BIO_new_file(cert, "r");
300 if (certbio == NULL) {
301 printf("Can't load the certficate file\n");
304 id = OCSP_RESPID_new();
305 ids = sk_OCSP_RESPID_new_null();
306 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
307 if (id == NULL || ids == NULL || ocspcert == NULL
308 || !OCSP_RESPID_set_by_key(id, ocspcert)
309 || !sk_OCSP_RESPID_push(ids, id)) {
310 printf("Unable to set OCSP_RESPIDs\n");
314 SSL_set_tlsext_status_ids(clientssl, ids);
315 /* Control has been transferred */
321 if (!create_ssl_connection(serverssl, clientssl)) {
322 printf("Unable to create SSL connection\n");
326 if (!ocsp_client_called || !ocsp_server_called) {
327 printf("OCSP callbacks not called\n");
338 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
339 OCSP_RESPID_free(id);
347 typedef struct ssl_session_test_fixture {
348 const char *test_case_name;
351 } SSL_SESSION_TEST_FIXTURE;
353 static int new_called = 0, remove_called = 0;
355 static SSL_SESSION_TEST_FIXTURE
356 ssl_session_set_up(const char *const test_case_name)
358 SSL_SESSION_TEST_FIXTURE fixture;
360 fixture.test_case_name = test_case_name;
361 fixture.use_ext_cache = 1;
362 fixture.use_int_cache = 1;
364 new_called = remove_called = 0;
369 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
373 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
380 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
385 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
387 SSL_CTX *sctx = NULL, *cctx = NULL;
388 SSL *serverssl1 = NULL, *clientssl1 = NULL;
389 SSL *serverssl2 = NULL, *clientssl2 = NULL;
390 #ifndef OPENSSL_NO_TLS1_1
391 SSL *serverssl3 = NULL, *clientssl3 = NULL;
393 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
396 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
397 &cctx, cert, privkey)) {
398 printf("Unable to create SSL_CTX pair\n");
402 #ifndef OPENSSL_NO_TLS1_2
403 /* Only allow TLS1.2 so we can force a connection failure later */
404 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
407 /* Set up session cache */
408 if (fix.use_ext_cache) {
409 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
410 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
412 if (fix.use_int_cache) {
413 /* Also covers instance where both are set */
414 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
416 SSL_CTX_set_session_cache_mode(cctx,
417 SSL_SESS_CACHE_CLIENT
418 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
421 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
423 printf("Unable to create SSL objects\n");
427 if (!create_ssl_connection(serverssl1, clientssl1)) {
428 printf("Unable to create SSL connection\n");
431 sess1 = SSL_get1_session(clientssl1);
433 printf("Unexpected NULL session\n");
437 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
438 /* Should have failed because it should already be in the cache */
439 printf("Unexpected success adding session to cache\n");
443 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
444 printf("Session not added to cache\n");
448 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
449 printf("Unable to create second SSL objects\n");
453 if (!create_ssl_connection(serverssl2, clientssl2)) {
454 printf("Unable to create second SSL connection\n");
458 sess2 = SSL_get1_session(clientssl2);
460 printf("Unexpected NULL session from clientssl2\n");
464 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
465 printf("Remove session callback unexpectedly called\n");
470 * This should clear sess2 from the cache because it is a "bad" session. See
471 * SSL_set_session() documentation.
473 if (!SSL_set_session(clientssl2, sess1)) {
474 printf("Unexpected failure setting session\n");
478 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
479 printf("Failed to call callback to remove session\n");
484 if (SSL_get_session(clientssl2) != sess1) {
485 printf("Unexpected session found\n");
489 if (fix.use_int_cache) {
490 if (!SSL_CTX_add_session(cctx, sess2)) {
492 * Should have succeeded because it should not already be in the cache
494 printf("Unexpected failure adding session to cache\n");
498 if (!SSL_CTX_remove_session(cctx, sess2)) {
499 printf("Unexpected failure removing session from cache\n");
503 /* This is for the purposes of internal cache testing...ignore the
504 * counter for external cache
506 if (fix.use_ext_cache)
510 /* This shouldn't be in the cache so should fail */
511 if (SSL_CTX_remove_session(cctx, sess2)) {
512 printf("Unexpected success removing session from cache\n");
516 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
517 printf("Failed to call callback to remove session #2\n");
521 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
522 /* Force a connection failure */
523 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
525 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
526 printf("Unable to create third SSL objects\n");
530 if (!SSL_set_session(clientssl3, sess1)) {
531 printf("Unable to set session for third connection\n");
535 /* This should fail because of the mismatched protocol versions */
536 if (create_ssl_connection(serverssl3, clientssl3)) {
537 printf("Unable to create third SSL connection\n");
542 /* We should have automatically removed the session from the cache */
543 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
544 printf("Failed to call callback to remove session #2\n");
548 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
550 * Should have succeeded because it should not already be in the cache
552 printf("Unexpected failure adding session to cache #2\n");
560 SSL_free(serverssl1);
561 SSL_free(clientssl1);
562 SSL_free(serverssl2);
563 SSL_free(clientssl2);
564 #ifndef OPENSSL_NO_TLS1_1
565 SSL_free(serverssl3);
566 SSL_free(clientssl3);
568 SSL_SESSION_free(sess1);
569 SSL_SESSION_free(sess2);
571 * Check if we need to remove any sessions up-refed for the external cache
574 SSL_SESSION_free(sess1);
576 SSL_SESSION_free(sess2);
583 static int test_session_with_only_int_cache(void)
585 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
587 fixture.use_ext_cache = 0;
589 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
592 static int test_session_with_only_ext_cache(void)
594 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
596 fixture.use_int_cache = 0;
598 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
601 static int test_session_with_both_cache(void)
603 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
605 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
612 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
614 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
629 static int test_ssl_set_bio(int idx)
631 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
634 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
636 int initrbio, initwbio, newrbio, newwbio;
640 printf("Failed to allocate SSL_CTX\n");
646 printf("Failed to allocate SSL object\n");
657 OPENSSL_assert(newwbio <= 2);
659 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
660 || newwbio == USE_BIO_1) {
661 bio1 = BIO_new(BIO_s_mem());
663 printf("Failed to allocate bio1\n");
668 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
669 || newwbio == USE_BIO_2) {
670 bio2 = BIO_new(BIO_s_mem());
672 printf("Failed to allocate bio2\n");
677 setupbio(&irbio, bio1, bio2, initrbio);
678 setupbio(&iwbio, bio1, bio2, initwbio);
681 * We want to maintain our own refs to these BIO, so do an up ref for each
682 * BIO that will have ownersip transferred in the SSL_set_bio() call
686 if (iwbio != NULL && iwbio != irbio)
689 SSL_set_bio(ssl, irbio, iwbio);
691 setupbio(&nrbio, bio1, bio2, newrbio);
692 setupbio(&nwbio, bio1, bio2, newwbio);
695 * We will (maybe) transfer ownership again so do more up refs.
696 * SSL_set_bio() has some really complicated ownership rules where BIOs have
699 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
701 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
704 SSL_set_bio(ssl, nrbio, nwbio);
713 * This test is checking that the ref counting for SSL_set_bio is correct.
714 * If we get here and we did too many frees then we will fail in the above
715 * functions. If we haven't done enough then this will only be detected in
716 * a crypto-mdebug build
723 typedef struct ssl_bio_test_fixture {
724 const char *test_case_name;
726 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
727 } SSL_BIO_TEST_FIXTURE;
729 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
731 SSL_BIO_TEST_FIXTURE fixture;
733 fixture.test_case_name = test_case_name;
735 fixture.change_bio = NO_BIO_CHANGE;
740 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
744 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
746 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
747 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
752 printf("Failed to allocate SSL_CTX\n");
758 printf("Failed to allocate SSL object\n");
762 sslbio = BIO_new(BIO_f_ssl());
763 membio1 = BIO_new(BIO_s_mem());
765 if (sslbio == NULL || membio1 == NULL) {
766 printf("Malloc failure creating BIOs\n");
770 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
773 * If anything goes wrong here then we could leak memory, so this will
774 * be caught in a crypto-mdebug build
776 BIO_push(sslbio, membio1);
778 /* Verify chaning the rbio/wbio directly does not cause leaks */
779 if (fix.change_bio != NO_BIO_CHANGE) {
780 membio2 = BIO_new(BIO_s_mem());
781 if (membio2 == NULL) {
782 printf("Malloc failure creating membio2\n");
785 if (fix.change_bio == CHANGE_RBIO)
786 SSL_set0_rbio(ssl, membio2);
788 SSL_set0_wbio(ssl, membio2);
807 static int test_ssl_bio_pop_next_bio(void)
809 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
811 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
814 static int test_ssl_bio_pop_ssl_bio(void)
816 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
820 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
823 static int test_ssl_bio_change_rbio(void)
825 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
827 fixture.change_bio = CHANGE_RBIO;
829 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
832 static int test_ssl_bio_change_wbio(void)
834 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
836 fixture.change_bio = CHANGE_WBIO;
838 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
841 int main(int argc, char *argv[])
847 printf("Invalid argument count\n");
854 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
856 CRYPTO_set_mem_debug(1);
857 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
859 ADD_TEST(test_large_message_tls);
860 ADD_TEST(test_large_message_dtls);
861 ADD_TEST(test_tlsext_status_type);
862 ADD_TEST(test_session_with_only_int_cache);
863 ADD_TEST(test_session_with_only_ext_cache);
864 ADD_TEST(test_session_with_both_cache);
865 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
866 ADD_TEST(test_ssl_bio_pop_next_bio);
867 ADD_TEST(test_ssl_bio_pop_ssl_bio);
868 ADD_TEST(test_ssl_bio_change_rbio);
869 ADD_TEST(test_ssl_bio_change_wbio);
871 testresult = run_tests(argv[0]);
873 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
874 if (CRYPTO_mem_leaks(err) <= 0)