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);
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");
63 * We assume the supplied certificate is big enough so that if we add
64 * NUM_EXTRA_CERTS it will make the overall message large enough. The
65 * default buffer size is requested to be 16k, but due to the way BUF_MEM
66 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
67 * we need to have a message larger than that.
69 certlen = i2d_X509(chaincert, NULL);
70 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
71 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
72 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
73 if (!X509_up_ref(chaincert)) {
74 printf("Unable to up ref cert\n");
77 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
78 printf("Unable to add extra chain cert %d\n", i);
84 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
85 printf("Unable to create SSL objects\n");
89 if (!create_ssl_connection(serverssl, clientssl)) {
90 printf("Unable to create SSL connection\n");
106 static int test_large_message_tls(void)
108 return execute_test_large_message(TLS_server_method(), TLS_client_method());
111 static int test_large_message_dtls(void)
113 return execute_test_large_message(DTLS_server_method(),
114 DTLS_client_method());
117 static int ocsp_server_cb(SSL *s, void *arg)
119 int *argi = (int *)arg;
120 unsigned char *orespdercopy = NULL;
121 STACK_OF(OCSP_RESPID) *ids = NULL;
122 OCSP_RESPID *id = NULL;
125 /* In this test we are expecting exactly 1 OCSP_RESPID */
126 SSL_get_tlsext_status_ids(s, &ids);
127 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
128 return SSL_TLSEXT_ERR_ALERT_FATAL;
130 id = sk_OCSP_RESPID_value(ids, 0);
131 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
132 return SSL_TLSEXT_ERR_ALERT_FATAL;
133 } else if (*argi != 1) {
134 return SSL_TLSEXT_ERR_ALERT_FATAL;
138 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
139 if (orespdercopy == NULL)
140 return SSL_TLSEXT_ERR_ALERT_FATAL;
142 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
144 ocsp_server_called = 1;
146 return SSL_TLSEXT_ERR_OK;
149 static int ocsp_client_cb(SSL *s, void *arg)
151 int *argi = (int *)arg;
152 const unsigned char *respderin;
155 if (*argi != 1 && *argi != 2)
158 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
160 if (memcmp(orespder, respderin, len) != 0)
163 ocsp_client_called = 1;
168 static int test_tlsext_status_type(void)
170 SSL_CTX *cctx = NULL, *sctx = NULL;
171 SSL *clientssl = NULL, *serverssl = NULL;
173 STACK_OF(OCSP_RESPID) *ids = NULL;
174 OCSP_RESPID *id = NULL;
177 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
178 &cctx, cert, privkey)) {
179 printf("Unable to create SSL_CTX pair\n");
183 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
184 printf("Unexpected initial value for "
185 "SSL_CTX_get_tlsext_status_type()\n");
189 /* First just do various checks getting and setting tlsext_status_type */
191 clientssl = SSL_new(cctx);
192 if (SSL_get_tlsext_status_type(clientssl) != -1) {
193 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
197 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
198 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
202 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
203 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
210 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
211 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
215 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
216 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
220 clientssl = SSL_new(cctx);
222 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
223 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
231 * Now actually do a handshake and check OCSP information is exchanged and
232 * the callbacks get called
235 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
236 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
237 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
238 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
240 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
241 printf("Unable to create SSL objects\n");
245 if (!create_ssl_connection(serverssl, clientssl)) {
246 printf("Unable to create SSL connection\n");
250 if (!ocsp_client_called || !ocsp_server_called) {
251 printf("OCSP callbacks not called\n");
260 /* Try again but this time force the server side callback to fail */
261 ocsp_client_called = 0;
262 ocsp_server_called = 0;
265 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
266 printf("Unable to create SSL objects\n");
270 /* This should fail because the callback will fail */
271 if (create_ssl_connection(serverssl, clientssl)) {
272 printf("Unexpected success creating the connection\n");
276 if (ocsp_client_called || ocsp_server_called) {
277 printf("OCSP callbacks successfully called unexpectedly\n");
287 * This time we'll get the client to send an OCSP_RESPID that it will
290 ocsp_client_called = 0;
291 ocsp_server_called = 0;
294 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
295 printf("Unable to create SSL objects\n");
300 * We'll just use any old cert for this test - it doesn't have to be an OCSP
301 * specifc one. We'll use the server cert.
303 certbio = BIO_new_file(cert, "r");
304 if (certbio == NULL) {
305 printf("Can't load the certficate file\n");
308 id = OCSP_RESPID_new();
309 ids = sk_OCSP_RESPID_new_null();
310 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
311 if (id == NULL || ids == NULL || ocspcert == NULL
312 || !OCSP_RESPID_set_by_key(id, ocspcert)
313 || !sk_OCSP_RESPID_push(ids, id)) {
314 printf("Unable to set OCSP_RESPIDs\n");
318 SSL_set_tlsext_status_ids(clientssl, ids);
319 /* Control has been transferred */
325 if (!create_ssl_connection(serverssl, clientssl)) {
326 printf("Unable to create SSL connection\n");
330 if (!ocsp_client_called || !ocsp_server_called) {
331 printf("OCSP callbacks not called\n");
342 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
343 OCSP_RESPID_free(id);
351 typedef struct ssl_session_test_fixture {
352 const char *test_case_name;
355 } SSL_SESSION_TEST_FIXTURE;
357 static int new_called = 0, remove_called = 0;
359 static SSL_SESSION_TEST_FIXTURE
360 ssl_session_set_up(const char *const test_case_name)
362 SSL_SESSION_TEST_FIXTURE fixture;
364 fixture.test_case_name = test_case_name;
365 fixture.use_ext_cache = 1;
366 fixture.use_int_cache = 1;
368 new_called = remove_called = 0;
373 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
377 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
384 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
389 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
391 SSL_CTX *sctx = NULL, *cctx = NULL;
392 SSL *serverssl1 = NULL, *clientssl1 = NULL;
393 SSL *serverssl2 = NULL, *clientssl2 = NULL;
394 #ifndef OPENSSL_NO_TLS1_1
395 SSL *serverssl3 = NULL, *clientssl3 = NULL;
397 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
400 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
401 &cctx, cert, privkey)) {
402 printf("Unable to create SSL_CTX pair\n");
406 #ifndef OPENSSL_NO_TLS1_2
407 /* Only allow TLS1.2 so we can force a connection failure later */
408 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
411 /* Set up session cache */
412 if (fix.use_ext_cache) {
413 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
414 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
416 if (fix.use_int_cache) {
417 /* Also covers instance where both are set */
418 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
420 SSL_CTX_set_session_cache_mode(cctx,
421 SSL_SESS_CACHE_CLIENT
422 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
425 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
427 printf("Unable to create SSL objects\n");
431 if (!create_ssl_connection(serverssl1, clientssl1)) {
432 printf("Unable to create SSL connection\n");
435 sess1 = SSL_get1_session(clientssl1);
437 printf("Unexpected NULL session\n");
441 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
442 /* Should have failed because it should already be in the cache */
443 printf("Unexpected success adding session to cache\n");
447 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
448 printf("Session not added to cache\n");
452 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
453 printf("Unable to create second SSL objects\n");
457 if (!create_ssl_connection(serverssl2, clientssl2)) {
458 printf("Unable to create second SSL connection\n");
462 sess2 = SSL_get1_session(clientssl2);
464 printf("Unexpected NULL session from clientssl2\n");
468 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
469 printf("Remove session callback unexpectedly called\n");
474 * This should clear sess2 from the cache because it is a "bad" session. See
475 * SSL_set_session() documentation.
477 if (!SSL_set_session(clientssl2, sess1)) {
478 printf("Unexpected failure setting session\n");
482 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
483 printf("Failed to call callback to remove session\n");
488 if (SSL_get_session(clientssl2) != sess1) {
489 printf("Unexpected session found\n");
493 if (fix.use_int_cache) {
494 if (!SSL_CTX_add_session(cctx, sess2)) {
496 * Should have succeeded because it should not already be in the cache
498 printf("Unexpected failure adding session to cache\n");
502 if (!SSL_CTX_remove_session(cctx, sess2)) {
503 printf("Unexpected failure removing session from cache\n");
507 /* This is for the purposes of internal cache testing...ignore the
508 * counter for external cache
510 if (fix.use_ext_cache)
514 /* This shouldn't be in the cache so should fail */
515 if (SSL_CTX_remove_session(cctx, sess2)) {
516 printf("Unexpected success removing session from cache\n");
520 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
521 printf("Failed to call callback to remove session #2\n");
525 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
526 /* Force a connection failure */
527 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
529 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
530 printf("Unable to create third SSL objects\n");
534 if (!SSL_set_session(clientssl3, sess1)) {
535 printf("Unable to set session for third connection\n");
539 /* This should fail because of the mismatched protocol versions */
540 if (create_ssl_connection(serverssl3, clientssl3)) {
541 printf("Unable to create third SSL connection\n");
546 /* We should have automatically removed the session from the cache */
547 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
548 printf("Failed to call callback to remove session #2\n");
552 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
554 * Should have succeeded because it should not already be in the cache
556 printf("Unexpected failure adding session to cache #2\n");
564 SSL_free(serverssl1);
565 SSL_free(clientssl1);
566 SSL_free(serverssl2);
567 SSL_free(clientssl2);
568 #ifndef OPENSSL_NO_TLS1_1
569 SSL_free(serverssl3);
570 SSL_free(clientssl3);
572 SSL_SESSION_free(sess1);
573 SSL_SESSION_free(sess2);
575 * Check if we need to remove any sessions up-refed for the external cache
578 SSL_SESSION_free(sess1);
580 SSL_SESSION_free(sess2);
587 static int test_session_with_only_int_cache(void)
589 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
591 fixture.use_ext_cache = 0;
593 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
596 static int test_session_with_only_ext_cache(void)
598 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
600 fixture.use_int_cache = 0;
602 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
605 static int test_session_with_both_cache(void)
607 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
609 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
616 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
618 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
633 static int test_ssl_set_bio(int idx)
635 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
638 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
640 int initrbio, initwbio, newrbio, newwbio;
644 printf("Failed to allocate SSL_CTX\n");
650 printf("Failed to allocate SSL object\n");
661 OPENSSL_assert(newwbio <= 2);
663 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
664 || newwbio == USE_BIO_1) {
665 bio1 = BIO_new(BIO_s_mem());
667 printf("Failed to allocate bio1\n");
672 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
673 || newwbio == USE_BIO_2) {
674 bio2 = BIO_new(BIO_s_mem());
676 printf("Failed to allocate bio2\n");
681 setupbio(&irbio, bio1, bio2, initrbio);
682 setupbio(&iwbio, bio1, bio2, initwbio);
685 * We want to maintain our own refs to these BIO, so do an up ref for each
686 * BIO that will have ownersip transferred in the SSL_set_bio() call
690 if (iwbio != NULL && iwbio != irbio)
693 SSL_set_bio(ssl, irbio, iwbio);
695 setupbio(&nrbio, bio1, bio2, newrbio);
696 setupbio(&nwbio, bio1, bio2, newwbio);
699 * We will (maybe) transfer ownership again so do more up refs.
700 * SSL_set_bio() has some really complicated ownership rules where BIOs have
703 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
705 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
708 SSL_set_bio(ssl, nrbio, nwbio);
717 * This test is checking that the ref counting for SSL_set_bio is correct.
718 * If we get here and we did too many frees then we will fail in the above
719 * functions. If we haven't done enough then this will only be detected in
720 * a crypto-mdebug build
727 typedef struct ssl_bio_test_fixture {
728 const char *test_case_name;
730 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
731 } SSL_BIO_TEST_FIXTURE;
733 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
735 SSL_BIO_TEST_FIXTURE fixture;
737 fixture.test_case_name = test_case_name;
739 fixture.change_bio = NO_BIO_CHANGE;
744 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
748 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
750 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
751 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
756 printf("Failed to allocate SSL_CTX\n");
762 printf("Failed to allocate SSL object\n");
766 sslbio = BIO_new(BIO_f_ssl());
767 membio1 = BIO_new(BIO_s_mem());
769 if (sslbio == NULL || membio1 == NULL) {
770 printf("Malloc failure creating BIOs\n");
774 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
777 * If anything goes wrong here then we could leak memory, so this will
778 * be caught in a crypto-mdebug build
780 BIO_push(sslbio, membio1);
782 /* Verify chaning the rbio/wbio directly does not cause leaks */
783 if (fix.change_bio != NO_BIO_CHANGE) {
784 membio2 = BIO_new(BIO_s_mem());
785 if (membio2 == NULL) {
786 printf("Malloc failure creating membio2\n");
789 if (fix.change_bio == CHANGE_RBIO)
790 SSL_set0_rbio(ssl, membio2);
792 SSL_set0_wbio(ssl, membio2);
811 static int test_ssl_bio_pop_next_bio(void)
813 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
815 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
818 static int test_ssl_bio_pop_ssl_bio(void)
820 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
824 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
827 static int test_ssl_bio_change_rbio(void)
829 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
831 fixture.change_bio = CHANGE_RBIO;
833 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
836 static int test_ssl_bio_change_wbio(void)
838 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
840 fixture.change_bio = CHANGE_WBIO;
842 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
845 int main(int argc, char *argv[])
851 printf("Invalid argument count\n");
858 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
860 CRYPTO_set_mem_debug(1);
861 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
863 ADD_TEST(test_large_message_tls);
864 ADD_TEST(test_large_message_dtls);
865 ADD_TEST(test_tlsext_status_type);
866 ADD_TEST(test_session_with_only_int_cache);
867 ADD_TEST(test_session_with_only_ext_cache);
868 ADD_TEST(test_session_with_both_cache);
869 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
870 ADD_TEST(test_ssl_bio_pop_next_bio);
871 ADD_TEST(test_ssl_bio_pop_ssl_bio);
872 ADD_TEST(test_ssl_bio_change_rbio);
873 ADD_TEST(test_ssl_bio_change_wbio);
875 testresult = run_tests(argv[0]);
877 bio_s_mempacket_test_free();
879 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
880 if (CRYPTO_mem_leaks(err) <= 0)