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, int read_ahead)
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);
51 if (chaincert == NULL) {
52 printf("Unable to load certificate for chain\n");
56 if (!create_ssl_ctx_pair(smeth, cmeth, &sctx,
57 &cctx, cert, privkey)) {
58 printf("Unable to create SSL_CTX pair\n");
64 * Test that read_ahead works correctly when dealing with large
67 SSL_CTX_set_read_ahead(cctx, 1);
71 * We assume the supplied certificate is big enough so that if we add
72 * NUM_EXTRA_CERTS it will make the overall message large enough. The
73 * default buffer size is requested to be 16k, but due to the way BUF_MEM
74 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
75 * we need to have a message larger than that.
77 certlen = i2d_X509(chaincert, NULL);
78 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
79 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
80 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
81 if (!X509_up_ref(chaincert)) {
82 printf("Unable to up ref cert\n");
85 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
86 printf("Unable to add extra chain cert %d\n", i);
92 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
93 printf("Unable to create SSL objects\n");
97 if (!create_ssl_connection(serverssl, clientssl)) {
98 printf("Unable to create SSL connection\n");
105 X509_free(chaincert);
114 static int test_large_message_tls(void)
116 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
120 static int test_large_message_tls_read_ahead(void)
122 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
126 #ifndef OPENSSL_NO_DTLS
127 static int test_large_message_dtls(void)
130 * read_ahead is not relevant to DTLS because DTLS always acts as if
133 return execute_test_large_message(DTLS_server_method(),
134 DTLS_client_method(), 0);
138 static int ocsp_server_cb(SSL *s, void *arg)
140 int *argi = (int *)arg;
141 unsigned char *orespdercopy = NULL;
142 STACK_OF(OCSP_RESPID) *ids = NULL;
143 OCSP_RESPID *id = NULL;
146 /* In this test we are expecting exactly 1 OCSP_RESPID */
147 SSL_get_tlsext_status_ids(s, &ids);
148 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
149 return SSL_TLSEXT_ERR_ALERT_FATAL;
151 id = sk_OCSP_RESPID_value(ids, 0);
152 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
153 return SSL_TLSEXT_ERR_ALERT_FATAL;
154 } else if (*argi != 1) {
155 return SSL_TLSEXT_ERR_ALERT_FATAL;
159 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
160 if (orespdercopy == NULL)
161 return SSL_TLSEXT_ERR_ALERT_FATAL;
163 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
165 ocsp_server_called = 1;
167 return SSL_TLSEXT_ERR_OK;
170 static int ocsp_client_cb(SSL *s, void *arg)
172 int *argi = (int *)arg;
173 const unsigned char *respderin;
176 if (*argi != 1 && *argi != 2)
179 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
181 if (memcmp(orespder, respderin, len) != 0)
184 ocsp_client_called = 1;
189 static int test_tlsext_status_type(void)
191 SSL_CTX *cctx = NULL, *sctx = NULL;
192 SSL *clientssl = NULL, *serverssl = NULL;
194 STACK_OF(OCSP_RESPID) *ids = NULL;
195 OCSP_RESPID *id = NULL;
198 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
199 &cctx, cert, privkey)) {
200 printf("Unable to create SSL_CTX pair\n");
204 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
205 printf("Unexpected initial value for "
206 "SSL_CTX_get_tlsext_status_type()\n");
210 /* First just do various checks getting and setting tlsext_status_type */
212 clientssl = SSL_new(cctx);
213 if (SSL_get_tlsext_status_type(clientssl) != -1) {
214 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
218 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
219 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
223 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
224 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
231 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
232 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
236 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
237 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
241 clientssl = SSL_new(cctx);
243 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
244 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
252 * Now actually do a handshake and check OCSP information is exchanged and
253 * the callbacks get called
256 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
257 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
258 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
259 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
261 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
262 printf("Unable to create SSL objects\n");
266 if (!create_ssl_connection(serverssl, clientssl)) {
267 printf("Unable to create SSL connection\n");
271 if (!ocsp_client_called || !ocsp_server_called) {
272 printf("OCSP callbacks not called\n");
281 /* Try again but this time force the server side callback to fail */
282 ocsp_client_called = 0;
283 ocsp_server_called = 0;
286 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
287 printf("Unable to create SSL objects\n");
291 /* This should fail because the callback will fail */
292 if (create_ssl_connection(serverssl, clientssl)) {
293 printf("Unexpected success creating the connection\n");
297 if (ocsp_client_called || ocsp_server_called) {
298 printf("OCSP callbacks successfully called unexpectedly\n");
308 * This time we'll get the client to send an OCSP_RESPID that it will
311 ocsp_client_called = 0;
312 ocsp_server_called = 0;
315 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
316 printf("Unable to create SSL objects\n");
321 * We'll just use any old cert for this test - it doesn't have to be an OCSP
322 * specifc one. We'll use the server cert.
324 certbio = BIO_new_file(cert, "r");
325 if (certbio == NULL) {
326 printf("Can't load the certficate file\n");
329 id = OCSP_RESPID_new();
330 ids = sk_OCSP_RESPID_new_null();
331 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
332 if (id == NULL || ids == NULL || ocspcert == NULL
333 || !OCSP_RESPID_set_by_key(id, ocspcert)
334 || !sk_OCSP_RESPID_push(ids, id)) {
335 printf("Unable to set OCSP_RESPIDs\n");
339 SSL_set_tlsext_status_ids(clientssl, ids);
340 /* Control has been transferred */
346 if (!create_ssl_connection(serverssl, clientssl)) {
347 printf("Unable to create SSL connection\n");
351 if (!ocsp_client_called || !ocsp_server_called) {
352 printf("OCSP callbacks not called\n");
363 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
364 OCSP_RESPID_free(id);
372 typedef struct ssl_session_test_fixture {
373 const char *test_case_name;
376 } SSL_SESSION_TEST_FIXTURE;
378 static int new_called = 0, remove_called = 0;
380 static SSL_SESSION_TEST_FIXTURE
381 ssl_session_set_up(const char *const test_case_name)
383 SSL_SESSION_TEST_FIXTURE fixture;
385 fixture.test_case_name = test_case_name;
386 fixture.use_ext_cache = 1;
387 fixture.use_int_cache = 1;
389 new_called = remove_called = 0;
394 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
398 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
405 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
410 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
412 SSL_CTX *sctx = NULL, *cctx = NULL;
413 SSL *serverssl1 = NULL, *clientssl1 = NULL;
414 SSL *serverssl2 = NULL, *clientssl2 = NULL;
415 #ifndef OPENSSL_NO_TLS1_1
416 SSL *serverssl3 = NULL, *clientssl3 = NULL;
418 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
421 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
422 &cctx, cert, privkey)) {
423 printf("Unable to create SSL_CTX pair\n");
427 #ifndef OPENSSL_NO_TLS1_2
428 /* Only allow TLS1.2 so we can force a connection failure later */
429 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
432 /* Set up session cache */
433 if (fix.use_ext_cache) {
434 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
435 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
437 if (fix.use_int_cache) {
438 /* Also covers instance where both are set */
439 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
441 SSL_CTX_set_session_cache_mode(cctx,
442 SSL_SESS_CACHE_CLIENT
443 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
446 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
448 printf("Unable to create SSL objects\n");
452 if (!create_ssl_connection(serverssl1, clientssl1)) {
453 printf("Unable to create SSL connection\n");
456 sess1 = SSL_get1_session(clientssl1);
458 printf("Unexpected NULL session\n");
462 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
463 /* Should have failed because it should already be in the cache */
464 printf("Unexpected success adding session to cache\n");
468 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
469 printf("Session not added to cache\n");
473 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
474 printf("Unable to create second SSL objects\n");
478 if (!create_ssl_connection(serverssl2, clientssl2)) {
479 printf("Unable to create second SSL connection\n");
483 sess2 = SSL_get1_session(clientssl2);
485 printf("Unexpected NULL session from clientssl2\n");
489 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
490 printf("Remove session callback unexpectedly called\n");
495 * This should clear sess2 from the cache because it is a "bad" session. See
496 * SSL_set_session() documentation.
498 if (!SSL_set_session(clientssl2, sess1)) {
499 printf("Unexpected failure setting session\n");
503 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
504 printf("Failed to call callback to remove session\n");
509 if (SSL_get_session(clientssl2) != sess1) {
510 printf("Unexpected session found\n");
514 if (fix.use_int_cache) {
515 if (!SSL_CTX_add_session(cctx, sess2)) {
517 * Should have succeeded because it should not already be in the cache
519 printf("Unexpected failure adding session to cache\n");
523 if (!SSL_CTX_remove_session(cctx, sess2)) {
524 printf("Unexpected failure removing session from cache\n");
528 /* This is for the purposes of internal cache testing...ignore the
529 * counter for external cache
531 if (fix.use_ext_cache)
535 /* This shouldn't be in the cache so should fail */
536 if (SSL_CTX_remove_session(cctx, sess2)) {
537 printf("Unexpected success removing session from cache\n");
541 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
542 printf("Failed to call callback to remove session #2\n");
546 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
547 /* Force a connection failure */
548 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
550 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
551 printf("Unable to create third SSL objects\n");
555 if (!SSL_set_session(clientssl3, sess1)) {
556 printf("Unable to set session for third connection\n");
560 /* This should fail because of the mismatched protocol versions */
561 if (create_ssl_connection(serverssl3, clientssl3)) {
562 printf("Unable to create third SSL connection\n");
567 /* We should have automatically removed the session from the cache */
568 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
569 printf("Failed to call callback to remove session #2\n");
573 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
575 * Should have succeeded because it should not already be in the cache
577 printf("Unexpected failure adding session to cache #2\n");
585 SSL_free(serverssl1);
586 SSL_free(clientssl1);
587 SSL_free(serverssl2);
588 SSL_free(clientssl2);
589 #ifndef OPENSSL_NO_TLS1_1
590 SSL_free(serverssl3);
591 SSL_free(clientssl3);
593 SSL_SESSION_free(sess1);
594 SSL_SESSION_free(sess2);
596 * Check if we need to remove any sessions up-refed for the external cache
599 SSL_SESSION_free(sess1);
601 SSL_SESSION_free(sess2);
608 static int test_session_with_only_int_cache(void)
610 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
612 fixture.use_ext_cache = 0;
614 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
617 static int test_session_with_only_ext_cache(void)
619 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
621 fixture.use_int_cache = 0;
623 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
626 static int test_session_with_both_cache(void)
628 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
630 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
637 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
639 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
654 static int test_ssl_set_bio(int idx)
656 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
659 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
661 int initrbio, initwbio, newrbio, newwbio;
665 printf("Failed to allocate SSL_CTX\n");
671 printf("Failed to allocate SSL object\n");
682 OPENSSL_assert(newwbio <= 2);
684 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
685 || newwbio == USE_BIO_1) {
686 bio1 = BIO_new(BIO_s_mem());
688 printf("Failed to allocate bio1\n");
693 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
694 || newwbio == USE_BIO_2) {
695 bio2 = BIO_new(BIO_s_mem());
697 printf("Failed to allocate bio2\n");
702 setupbio(&irbio, bio1, bio2, initrbio);
703 setupbio(&iwbio, bio1, bio2, initwbio);
706 * We want to maintain our own refs to these BIO, so do an up ref for each
707 * BIO that will have ownersip transferred in the SSL_set_bio() call
711 if (iwbio != NULL && iwbio != irbio)
714 SSL_set_bio(ssl, irbio, iwbio);
716 setupbio(&nrbio, bio1, bio2, newrbio);
717 setupbio(&nwbio, bio1, bio2, newwbio);
720 * We will (maybe) transfer ownership again so do more up refs.
721 * SSL_set_bio() has some really complicated ownership rules where BIOs have
724 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
726 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
729 SSL_set_bio(ssl, nrbio, nwbio);
738 * This test is checking that the ref counting for SSL_set_bio is correct.
739 * If we get here and we did too many frees then we will fail in the above
740 * functions. If we haven't done enough then this will only be detected in
741 * a crypto-mdebug build
748 typedef struct ssl_bio_test_fixture {
749 const char *test_case_name;
751 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
752 } SSL_BIO_TEST_FIXTURE;
754 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
756 SSL_BIO_TEST_FIXTURE fixture;
758 fixture.test_case_name = test_case_name;
760 fixture.change_bio = NO_BIO_CHANGE;
765 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
769 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
771 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
772 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
777 printf("Failed to allocate SSL_CTX\n");
783 printf("Failed to allocate SSL object\n");
787 sslbio = BIO_new(BIO_f_ssl());
788 membio1 = BIO_new(BIO_s_mem());
790 if (sslbio == NULL || membio1 == NULL) {
791 printf("Malloc failure creating BIOs\n");
795 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
798 * If anything goes wrong here then we could leak memory, so this will
799 * be caught in a crypto-mdebug build
801 BIO_push(sslbio, membio1);
803 /* Verify chaning the rbio/wbio directly does not cause leaks */
804 if (fix.change_bio != NO_BIO_CHANGE) {
805 membio2 = BIO_new(BIO_s_mem());
806 if (membio2 == NULL) {
807 printf("Malloc failure creating membio2\n");
810 if (fix.change_bio == CHANGE_RBIO)
811 SSL_set0_rbio(ssl, membio2);
813 SSL_set0_wbio(ssl, membio2);
832 static int test_ssl_bio_pop_next_bio(void)
834 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
836 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
839 static int test_ssl_bio_pop_ssl_bio(void)
841 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
845 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
848 static int test_ssl_bio_change_rbio(void)
850 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
852 fixture.change_bio = CHANGE_RBIO;
854 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
857 static int test_ssl_bio_change_wbio(void)
859 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
861 fixture.change_bio = CHANGE_WBIO;
863 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
866 int main(int argc, char *argv[])
872 printf("Invalid argument count\n");
879 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
881 CRYPTO_set_mem_debug(1);
882 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
884 ADD_TEST(test_large_message_tls);
885 ADD_TEST(test_large_message_tls_read_ahead);
886 #ifndef OPENSSL_NO_DTLS
887 ADD_TEST(test_large_message_dtls);
889 ADD_TEST(test_tlsext_status_type);
890 ADD_TEST(test_session_with_only_int_cache);
891 ADD_TEST(test_session_with_only_ext_cache);
892 ADD_TEST(test_session_with_both_cache);
893 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
894 ADD_TEST(test_ssl_bio_pop_next_bio);
895 ADD_TEST(test_ssl_bio_pop_ssl_bio);
896 ADD_TEST(test_ssl_bio_change_rbio);
897 ADD_TEST(test_ssl_bio_change_wbio);
899 testresult = run_tests(argv[0]);
901 bio_s_mempacket_test_free();
903 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
904 if (CRYPTO_mem_leaks(err) <= 0)