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, int read_ahead)
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");
66 * Test that read_ahead works correctly when dealing with large
69 SSL_CTX_set_read_ahead(cctx, 1);
73 * We assume the supplied certificate is big enough so that if we add
74 * NUM_EXTRA_CERTS it will make the overall message large enough. The
75 * default buffer size is requested to be 16k, but due to the way BUF_MEM
76 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
77 * we need to have a message larger than that.
79 certlen = i2d_X509(chaincert, NULL);
80 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
81 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
82 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
83 if (!X509_up_ref(chaincert)) {
84 printf("Unable to up ref cert\n");
87 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
88 printf("Unable to add extra chain cert %d\n", i);
94 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
95 printf("Unable to create SSL objects\n");
99 if (!create_ssl_connection(serverssl, clientssl)) {
100 printf("Unable to create SSL connection\n");
107 X509_free(chaincert);
116 static int test_large_message_tls(void)
118 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
122 static int test_large_message_tls_read_ahead(void)
124 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
128 #ifndef OPENSSL_NO_DTLS
129 static int test_large_message_dtls(void)
132 * read_ahead is not relevant to DTLS because DTLS always acts as if
135 return execute_test_large_message(DTLS_server_method(),
136 DTLS_client_method(), 0);
140 #ifndef OPENSSL_NO_OCSP
141 static int ocsp_server_cb(SSL *s, void *arg)
143 int *argi = (int *)arg;
144 unsigned char *orespdercopy = NULL;
145 STACK_OF(OCSP_RESPID) *ids = NULL;
146 OCSP_RESPID *id = NULL;
149 /* In this test we are expecting exactly 1 OCSP_RESPID */
150 SSL_get_tlsext_status_ids(s, &ids);
151 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
152 return SSL_TLSEXT_ERR_ALERT_FATAL;
154 id = sk_OCSP_RESPID_value(ids, 0);
155 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
156 return SSL_TLSEXT_ERR_ALERT_FATAL;
157 } else if (*argi != 1) {
158 return SSL_TLSEXT_ERR_ALERT_FATAL;
162 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
163 if (orespdercopy == NULL)
164 return SSL_TLSEXT_ERR_ALERT_FATAL;
166 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
168 ocsp_server_called = 1;
170 return SSL_TLSEXT_ERR_OK;
173 static int ocsp_client_cb(SSL *s, void *arg)
175 int *argi = (int *)arg;
176 const unsigned char *respderin;
179 if (*argi != 1 && *argi != 2)
182 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
184 if (memcmp(orespder, respderin, len) != 0)
187 ocsp_client_called = 1;
192 static int test_tlsext_status_type(void)
194 SSL_CTX *cctx = NULL, *sctx = NULL;
195 SSL *clientssl = NULL, *serverssl = NULL;
197 STACK_OF(OCSP_RESPID) *ids = NULL;
198 OCSP_RESPID *id = NULL;
201 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
202 &cctx, cert, privkey)) {
203 printf("Unable to create SSL_CTX pair\n");
207 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
208 printf("Unexpected initial value for "
209 "SSL_CTX_get_tlsext_status_type()\n");
213 /* First just do various checks getting and setting tlsext_status_type */
215 clientssl = SSL_new(cctx);
216 if (SSL_get_tlsext_status_type(clientssl) != -1) {
217 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
221 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
222 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
226 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
227 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
234 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
235 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
239 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
240 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
244 clientssl = SSL_new(cctx);
246 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
247 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
255 * Now actually do a handshake and check OCSP information is exchanged and
256 * the callbacks get called
259 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
260 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
261 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
262 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
264 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
265 printf("Unable to create SSL objects\n");
269 if (!create_ssl_connection(serverssl, clientssl)) {
270 printf("Unable to create SSL connection\n");
274 if (!ocsp_client_called || !ocsp_server_called) {
275 printf("OCSP callbacks not called\n");
284 /* Try again but this time force the server side callback to fail */
285 ocsp_client_called = 0;
286 ocsp_server_called = 0;
289 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
290 printf("Unable to create SSL objects\n");
294 /* This should fail because the callback will fail */
295 if (create_ssl_connection(serverssl, clientssl)) {
296 printf("Unexpected success creating the connection\n");
300 if (ocsp_client_called || ocsp_server_called) {
301 printf("OCSP callbacks successfully called unexpectedly\n");
311 * This time we'll get the client to send an OCSP_RESPID that it will
314 ocsp_client_called = 0;
315 ocsp_server_called = 0;
318 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
319 printf("Unable to create SSL objects\n");
324 * We'll just use any old cert for this test - it doesn't have to be an OCSP
325 * specifc one. We'll use the server cert.
327 certbio = BIO_new_file(cert, "r");
328 if (certbio == NULL) {
329 printf("Can't load the certficate file\n");
332 id = OCSP_RESPID_new();
333 ids = sk_OCSP_RESPID_new_null();
334 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
335 if (id == NULL || ids == NULL || ocspcert == NULL
336 || !OCSP_RESPID_set_by_key(id, ocspcert)
337 || !sk_OCSP_RESPID_push(ids, id)) {
338 printf("Unable to set OCSP_RESPIDs\n");
342 SSL_set_tlsext_status_ids(clientssl, ids);
343 /* Control has been transferred */
349 if (!create_ssl_connection(serverssl, clientssl)) {
350 printf("Unable to create SSL connection\n");
354 if (!ocsp_client_called || !ocsp_server_called) {
355 printf("OCSP callbacks not called\n");
366 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
367 OCSP_RESPID_free(id);
374 #endif /* ndef OPENSSL_NO_OCSP */
376 typedef struct ssl_session_test_fixture {
377 const char *test_case_name;
380 } SSL_SESSION_TEST_FIXTURE;
382 static int new_called = 0, remove_called = 0;
384 static SSL_SESSION_TEST_FIXTURE
385 ssl_session_set_up(const char *const test_case_name)
387 SSL_SESSION_TEST_FIXTURE fixture;
389 fixture.test_case_name = test_case_name;
390 fixture.use_ext_cache = 1;
391 fixture.use_int_cache = 1;
393 new_called = remove_called = 0;
398 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
402 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
409 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
414 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
416 SSL_CTX *sctx = NULL, *cctx = NULL;
417 SSL *serverssl1 = NULL, *clientssl1 = NULL;
418 SSL *serverssl2 = NULL, *clientssl2 = NULL;
419 #ifndef OPENSSL_NO_TLS1_1
420 SSL *serverssl3 = NULL, *clientssl3 = NULL;
422 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
425 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
426 &cctx, cert, privkey)) {
427 printf("Unable to create SSL_CTX pair\n");
431 #ifndef OPENSSL_NO_TLS1_2
432 /* Only allow TLS1.2 so we can force a connection failure later */
433 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
436 /* Set up session cache */
437 if (fix.use_ext_cache) {
438 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
439 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
441 if (fix.use_int_cache) {
442 /* Also covers instance where both are set */
443 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
445 SSL_CTX_set_session_cache_mode(cctx,
446 SSL_SESS_CACHE_CLIENT
447 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
450 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
452 printf("Unable to create SSL objects\n");
456 if (!create_ssl_connection(serverssl1, clientssl1)) {
457 printf("Unable to create SSL connection\n");
460 sess1 = SSL_get1_session(clientssl1);
462 printf("Unexpected NULL session\n");
466 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
467 /* Should have failed because it should already be in the cache */
468 printf("Unexpected success adding session to cache\n");
472 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
473 printf("Session not added to cache\n");
477 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
478 printf("Unable to create second SSL objects\n");
482 if (!create_ssl_connection(serverssl2, clientssl2)) {
483 printf("Unable to create second SSL connection\n");
487 sess2 = SSL_get1_session(clientssl2);
489 printf("Unexpected NULL session from clientssl2\n");
493 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
494 printf("Remove session callback unexpectedly called\n");
499 * This should clear sess2 from the cache because it is a "bad" session. See
500 * SSL_set_session() documentation.
502 if (!SSL_set_session(clientssl2, sess1)) {
503 printf("Unexpected failure setting session\n");
507 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
508 printf("Failed to call callback to remove session\n");
513 if (SSL_get_session(clientssl2) != sess1) {
514 printf("Unexpected session found\n");
518 if (fix.use_int_cache) {
519 if (!SSL_CTX_add_session(cctx, sess2)) {
521 * Should have succeeded because it should not already be in the cache
523 printf("Unexpected failure adding session to cache\n");
527 if (!SSL_CTX_remove_session(cctx, sess2)) {
528 printf("Unexpected failure removing session from cache\n");
532 /* This is for the purposes of internal cache testing...ignore the
533 * counter for external cache
535 if (fix.use_ext_cache)
539 /* This shouldn't be in the cache so should fail */
540 if (SSL_CTX_remove_session(cctx, sess2)) {
541 printf("Unexpected success removing session from cache\n");
545 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
546 printf("Failed to call callback to remove session #2\n");
550 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
551 /* Force a connection failure */
552 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
554 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
555 printf("Unable to create third SSL objects\n");
559 if (!SSL_set_session(clientssl3, sess1)) {
560 printf("Unable to set session for third connection\n");
564 /* This should fail because of the mismatched protocol versions */
565 if (create_ssl_connection(serverssl3, clientssl3)) {
566 printf("Unable to create third SSL connection\n");
571 /* We should have automatically removed the session from the cache */
572 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
573 printf("Failed to call callback to remove session #2\n");
577 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
579 * Should have succeeded because it should not already be in the cache
581 printf("Unexpected failure adding session to cache #2\n");
589 SSL_free(serverssl1);
590 SSL_free(clientssl1);
591 SSL_free(serverssl2);
592 SSL_free(clientssl2);
593 #ifndef OPENSSL_NO_TLS1_1
594 SSL_free(serverssl3);
595 SSL_free(clientssl3);
597 SSL_SESSION_free(sess1);
598 SSL_SESSION_free(sess2);
600 * Check if we need to remove any sessions up-refed for the external cache
603 SSL_SESSION_free(sess1);
605 SSL_SESSION_free(sess2);
612 static int test_session_with_only_int_cache(void)
614 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
616 fixture.use_ext_cache = 0;
618 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
621 static int test_session_with_only_ext_cache(void)
623 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
625 fixture.use_int_cache = 0;
627 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
630 static int test_session_with_both_cache(void)
632 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
634 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
641 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
643 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
658 static int test_ssl_set_bio(int idx)
660 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
663 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
665 int initrbio, initwbio, newrbio, newwbio;
669 printf("Failed to allocate SSL_CTX\n");
675 printf("Failed to allocate SSL object\n");
686 OPENSSL_assert(newwbio <= 2);
688 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
689 || newwbio == USE_BIO_1) {
690 bio1 = BIO_new(BIO_s_mem());
692 printf("Failed to allocate bio1\n");
697 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
698 || newwbio == USE_BIO_2) {
699 bio2 = BIO_new(BIO_s_mem());
701 printf("Failed to allocate bio2\n");
706 setupbio(&irbio, bio1, bio2, initrbio);
707 setupbio(&iwbio, bio1, bio2, initwbio);
710 * We want to maintain our own refs to these BIO, so do an up ref for each
711 * BIO that will have ownersip transferred in the SSL_set_bio() call
715 if (iwbio != NULL && iwbio != irbio)
718 SSL_set_bio(ssl, irbio, iwbio);
720 setupbio(&nrbio, bio1, bio2, newrbio);
721 setupbio(&nwbio, bio1, bio2, newwbio);
724 * We will (maybe) transfer ownership again so do more up refs.
725 * SSL_set_bio() has some really complicated ownership rules where BIOs have
728 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
730 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
733 SSL_set_bio(ssl, nrbio, nwbio);
742 * This test is checking that the ref counting for SSL_set_bio is correct.
743 * If we get here and we did too many frees then we will fail in the above
744 * functions. If we haven't done enough then this will only be detected in
745 * a crypto-mdebug build
752 typedef struct ssl_bio_test_fixture {
753 const char *test_case_name;
755 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
756 } SSL_BIO_TEST_FIXTURE;
758 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
760 SSL_BIO_TEST_FIXTURE fixture;
762 fixture.test_case_name = test_case_name;
764 fixture.change_bio = NO_BIO_CHANGE;
769 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
773 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
775 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
776 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
781 printf("Failed to allocate SSL_CTX\n");
787 printf("Failed to allocate SSL object\n");
791 sslbio = BIO_new(BIO_f_ssl());
792 membio1 = BIO_new(BIO_s_mem());
794 if (sslbio == NULL || membio1 == NULL) {
795 printf("Malloc failure creating BIOs\n");
799 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
802 * If anything goes wrong here then we could leak memory, so this will
803 * be caught in a crypto-mdebug build
805 BIO_push(sslbio, membio1);
807 /* Verify chaning the rbio/wbio directly does not cause leaks */
808 if (fix.change_bio != NO_BIO_CHANGE) {
809 membio2 = BIO_new(BIO_s_mem());
810 if (membio2 == NULL) {
811 printf("Malloc failure creating membio2\n");
814 if (fix.change_bio == CHANGE_RBIO)
815 SSL_set0_rbio(ssl, membio2);
817 SSL_set0_wbio(ssl, membio2);
836 static int test_ssl_bio_pop_next_bio(void)
838 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
840 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
843 static int test_ssl_bio_pop_ssl_bio(void)
845 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
849 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
852 static int test_ssl_bio_change_rbio(void)
854 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
856 fixture.change_bio = CHANGE_RBIO;
858 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
861 static int test_ssl_bio_change_wbio(void)
863 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
865 fixture.change_bio = CHANGE_WBIO;
867 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
870 int main(int argc, char *argv[])
876 printf("Invalid argument count\n");
883 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
885 CRYPTO_set_mem_debug(1);
886 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
888 ADD_TEST(test_large_message_tls);
889 ADD_TEST(test_large_message_tls_read_ahead);
890 #ifndef OPENSSL_NO_DTLS
891 ADD_TEST(test_large_message_dtls);
893 #ifndef OPENSSL_NO_OCSP
894 ADD_TEST(test_tlsext_status_type);
896 ADD_TEST(test_session_with_only_int_cache);
897 ADD_TEST(test_session_with_only_ext_cache);
898 ADD_TEST(test_session_with_both_cache);
899 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
900 ADD_TEST(test_ssl_bio_pop_next_bio);
901 ADD_TEST(test_ssl_bio_pop_ssl_bio);
902 ADD_TEST(test_ssl_bio_change_rbio);
903 ADD_TEST(test_ssl_bio_change_wbio);
905 testresult = run_tests(argv[0]);
907 bio_s_mempacket_test_free();
909 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
910 if (CRYPTO_mem_leaks(err) <= 0)