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"
20 #include "test_main_custom.h"
22 static char *cert = NULL;
23 static char *privkey = NULL;
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;
32 #define NUM_EXTRA_CERTS 40
34 static int execute_test_large_message(const SSL_METHOD *smeth,
35 const SSL_METHOD *cmeth, int read_ahead)
37 SSL_CTX *cctx = NULL, *sctx = NULL;
38 SSL *clientssl = NULL, *serverssl = NULL;
41 BIO *certbio = BIO_new_file(cert, "r");
42 X509 *chaincert = NULL;
45 if (certbio == NULL) {
46 printf("Can't load the certficate file\n");
49 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
52 if (chaincert == NULL) {
53 printf("Unable to load certificate for chain\n");
57 if (!create_ssl_ctx_pair(smeth, cmeth, &sctx,
58 &cctx, cert, privkey)) {
59 printf("Unable to create SSL_CTX pair\n");
65 * Test that read_ahead works correctly when dealing with large
68 SSL_CTX_set_read_ahead(cctx, 1);
72 * We assume the supplied certificate is big enough so that if we add
73 * NUM_EXTRA_CERTS it will make the overall message large enough. The
74 * default buffer size is requested to be 16k, but due to the way BUF_MEM
75 * works, it ends up allocing a little over 21k (16 * 4/3). So, in this test
76 * we need to have a message larger than that.
78 certlen = i2d_X509(chaincert, NULL);
79 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
80 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
81 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
82 if (!X509_up_ref(chaincert)) {
83 printf("Unable to up ref cert\n");
86 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
87 printf("Unable to add extra chain cert %d\n", i);
93 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
94 printf("Unable to create SSL objects\n");
98 if (!create_ssl_connection(serverssl, clientssl)) {
99 printf("Unable to create SSL connection\n");
106 X509_free(chaincert);
115 static int test_large_message_tls(void)
117 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
121 static int test_large_message_tls_read_ahead(void)
123 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
127 #ifndef OPENSSL_NO_DTLS
128 static int test_large_message_dtls(void)
131 * read_ahead is not relevant to DTLS because DTLS always acts as if
134 return execute_test_large_message(DTLS_server_method(),
135 DTLS_client_method(), 0);
139 static int ocsp_server_cb(SSL *s, void *arg)
141 int *argi = (int *)arg;
142 unsigned char *orespdercopy = NULL;
143 STACK_OF(OCSP_RESPID) *ids = NULL;
144 OCSP_RESPID *id = NULL;
147 /* In this test we are expecting exactly 1 OCSP_RESPID */
148 SSL_get_tlsext_status_ids(s, &ids);
149 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
150 return SSL_TLSEXT_ERR_ALERT_FATAL;
152 id = sk_OCSP_RESPID_value(ids, 0);
153 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
154 return SSL_TLSEXT_ERR_ALERT_FATAL;
155 } else if (*argi != 1) {
156 return SSL_TLSEXT_ERR_ALERT_FATAL;
160 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
161 if (orespdercopy == NULL)
162 return SSL_TLSEXT_ERR_ALERT_FATAL;
164 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
166 ocsp_server_called = 1;
168 return SSL_TLSEXT_ERR_OK;
171 static int ocsp_client_cb(SSL *s, void *arg)
173 int *argi = (int *)arg;
174 const unsigned char *respderin;
177 if (*argi != 1 && *argi != 2)
180 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
182 if (memcmp(orespder, respderin, len) != 0)
185 ocsp_client_called = 1;
190 static int test_tlsext_status_type(void)
192 SSL_CTX *cctx = NULL, *sctx = NULL;
193 SSL *clientssl = NULL, *serverssl = NULL;
195 STACK_OF(OCSP_RESPID) *ids = NULL;
196 OCSP_RESPID *id = NULL;
199 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
200 &cctx, cert, privkey)) {
201 printf("Unable to create SSL_CTX pair\n");
205 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
206 printf("Unexpected initial value for "
207 "SSL_CTX_get_tlsext_status_type()\n");
211 /* First just do various checks getting and setting tlsext_status_type */
213 clientssl = SSL_new(cctx);
214 if (SSL_get_tlsext_status_type(clientssl) != -1) {
215 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
219 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
220 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
224 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
225 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
232 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
233 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
237 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
238 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
242 clientssl = SSL_new(cctx);
244 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
245 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
253 * Now actually do a handshake and check OCSP information is exchanged and
254 * the callbacks get called
257 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
258 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
259 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
260 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
262 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
263 printf("Unable to create SSL objects\n");
267 if (!create_ssl_connection(serverssl, clientssl)) {
268 printf("Unable to create SSL connection\n");
272 if (!ocsp_client_called || !ocsp_server_called) {
273 printf("OCSP callbacks not called\n");
282 /* Try again but this time force the server side callback to fail */
283 ocsp_client_called = 0;
284 ocsp_server_called = 0;
287 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
288 printf("Unable to create SSL objects\n");
292 /* This should fail because the callback will fail */
293 if (create_ssl_connection(serverssl, clientssl)) {
294 printf("Unexpected success creating the connection\n");
298 if (ocsp_client_called || ocsp_server_called) {
299 printf("OCSP callbacks successfully called unexpectedly\n");
309 * This time we'll get the client to send an OCSP_RESPID that it will
312 ocsp_client_called = 0;
313 ocsp_server_called = 0;
316 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
317 printf("Unable to create SSL objects\n");
322 * We'll just use any old cert for this test - it doesn't have to be an OCSP
323 * specifc one. We'll use the server cert.
325 certbio = BIO_new_file(cert, "r");
326 if (certbio == NULL) {
327 printf("Can't load the certficate file\n");
330 id = OCSP_RESPID_new();
331 ids = sk_OCSP_RESPID_new_null();
332 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
333 if (id == NULL || ids == NULL || ocspcert == NULL
334 || !OCSP_RESPID_set_by_key(id, ocspcert)
335 || !sk_OCSP_RESPID_push(ids, id)) {
336 printf("Unable to set OCSP_RESPIDs\n");
340 SSL_set_tlsext_status_ids(clientssl, ids);
341 /* Control has been transferred */
347 if (!create_ssl_connection(serverssl, clientssl)) {
348 printf("Unable to create SSL connection\n");
352 if (!ocsp_client_called || !ocsp_server_called) {
353 printf("OCSP callbacks not called\n");
364 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
365 OCSP_RESPID_free(id);
373 typedef struct ssl_session_test_fixture {
374 const char *test_case_name;
377 } SSL_SESSION_TEST_FIXTURE;
379 static int new_called = 0, remove_called = 0;
381 static SSL_SESSION_TEST_FIXTURE
382 ssl_session_set_up(const char *const test_case_name)
384 SSL_SESSION_TEST_FIXTURE fixture;
386 fixture.test_case_name = test_case_name;
387 fixture.use_ext_cache = 1;
388 fixture.use_int_cache = 1;
390 new_called = remove_called = 0;
395 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
399 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
406 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
411 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
413 SSL_CTX *sctx = NULL, *cctx = NULL;
414 SSL *serverssl1 = NULL, *clientssl1 = NULL;
415 SSL *serverssl2 = NULL, *clientssl2 = NULL;
416 #ifndef OPENSSL_NO_TLS1_1
417 SSL *serverssl3 = NULL, *clientssl3 = NULL;
419 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
422 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
423 &cctx, cert, privkey)) {
424 printf("Unable to create SSL_CTX pair\n");
428 #ifndef OPENSSL_NO_TLS1_2
429 /* Only allow TLS1.2 so we can force a connection failure later */
430 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
433 /* Set up session cache */
434 if (fix.use_ext_cache) {
435 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
436 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
438 if (fix.use_int_cache) {
439 /* Also covers instance where both are set */
440 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
442 SSL_CTX_set_session_cache_mode(cctx,
443 SSL_SESS_CACHE_CLIENT
444 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
447 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
449 printf("Unable to create SSL objects\n");
453 if (!create_ssl_connection(serverssl1, clientssl1)) {
454 printf("Unable to create SSL connection\n");
457 sess1 = SSL_get1_session(clientssl1);
459 printf("Unexpected NULL session\n");
463 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
464 /* Should have failed because it should already be in the cache */
465 printf("Unexpected success adding session to cache\n");
469 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
470 printf("Session not added to cache\n");
474 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
475 printf("Unable to create second SSL objects\n");
479 if (!create_ssl_connection(serverssl2, clientssl2)) {
480 printf("Unable to create second SSL connection\n");
484 sess2 = SSL_get1_session(clientssl2);
486 printf("Unexpected NULL session from clientssl2\n");
490 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
491 printf("Remove session callback unexpectedly called\n");
496 * This should clear sess2 from the cache because it is a "bad" session. See
497 * SSL_set_session() documentation.
499 if (!SSL_set_session(clientssl2, sess1)) {
500 printf("Unexpected failure setting session\n");
504 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
505 printf("Failed to call callback to remove session\n");
510 if (SSL_get_session(clientssl2) != sess1) {
511 printf("Unexpected session found\n");
515 if (fix.use_int_cache) {
516 if (!SSL_CTX_add_session(cctx, sess2)) {
518 * Should have succeeded because it should not already be in the cache
520 printf("Unexpected failure adding session to cache\n");
524 if (!SSL_CTX_remove_session(cctx, sess2)) {
525 printf("Unexpected failure removing session from cache\n");
529 /* This is for the purposes of internal cache testing...ignore the
530 * counter for external cache
532 if (fix.use_ext_cache)
536 /* This shouldn't be in the cache so should fail */
537 if (SSL_CTX_remove_session(cctx, sess2)) {
538 printf("Unexpected success removing session from cache\n");
542 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
543 printf("Failed to call callback to remove session #2\n");
547 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
548 /* Force a connection failure */
549 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
551 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
552 printf("Unable to create third SSL objects\n");
556 if (!SSL_set_session(clientssl3, sess1)) {
557 printf("Unable to set session for third connection\n");
561 /* This should fail because of the mismatched protocol versions */
562 if (create_ssl_connection(serverssl3, clientssl3)) {
563 printf("Unable to create third SSL connection\n");
568 /* We should have automatically removed the session from the cache */
569 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
570 printf("Failed to call callback to remove session #2\n");
574 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
576 * Should have succeeded because it should not already be in the cache
578 printf("Unexpected failure adding session to cache #2\n");
586 SSL_free(serverssl1);
587 SSL_free(clientssl1);
588 SSL_free(serverssl2);
589 SSL_free(clientssl2);
590 #ifndef OPENSSL_NO_TLS1_1
591 SSL_free(serverssl3);
592 SSL_free(clientssl3);
594 SSL_SESSION_free(sess1);
595 SSL_SESSION_free(sess2);
597 * Check if we need to remove any sessions up-refed for the external cache
600 SSL_SESSION_free(sess1);
602 SSL_SESSION_free(sess2);
609 static int test_session_with_only_int_cache(void)
611 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
613 fixture.use_ext_cache = 0;
615 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
618 static int test_session_with_only_ext_cache(void)
620 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
622 fixture.use_int_cache = 0;
624 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
627 static int test_session_with_both_cache(void)
629 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
631 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
638 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
640 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
655 static int test_ssl_set_bio(int idx)
657 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
660 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
662 int initrbio, initwbio, newrbio, newwbio;
666 printf("Failed to allocate SSL_CTX\n");
672 printf("Failed to allocate SSL object\n");
683 OPENSSL_assert(newwbio <= 2);
685 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
686 || newwbio == USE_BIO_1) {
687 bio1 = BIO_new(BIO_s_mem());
689 printf("Failed to allocate bio1\n");
694 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
695 || newwbio == USE_BIO_2) {
696 bio2 = BIO_new(BIO_s_mem());
698 printf("Failed to allocate bio2\n");
703 setupbio(&irbio, bio1, bio2, initrbio);
704 setupbio(&iwbio, bio1, bio2, initwbio);
707 * We want to maintain our own refs to these BIO, so do an up ref for each
708 * BIO that will have ownersip transferred in the SSL_set_bio() call
712 if (iwbio != NULL && iwbio != irbio)
715 SSL_set_bio(ssl, irbio, iwbio);
717 setupbio(&nrbio, bio1, bio2, newrbio);
718 setupbio(&nwbio, bio1, bio2, newwbio);
721 * We will (maybe) transfer ownership again so do more up refs.
722 * SSL_set_bio() has some really complicated ownership rules where BIOs have
725 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
727 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
730 SSL_set_bio(ssl, nrbio, nwbio);
739 * This test is checking that the ref counting for SSL_set_bio is correct.
740 * If we get here and we did too many frees then we will fail in the above
741 * functions. If we haven't done enough then this will only be detected in
742 * a crypto-mdebug build
749 typedef struct ssl_bio_test_fixture {
750 const char *test_case_name;
752 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
753 } SSL_BIO_TEST_FIXTURE;
755 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
757 SSL_BIO_TEST_FIXTURE fixture;
759 fixture.test_case_name = test_case_name;
761 fixture.change_bio = NO_BIO_CHANGE;
766 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
770 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
772 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
773 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
778 printf("Failed to allocate SSL_CTX\n");
784 printf("Failed to allocate SSL object\n");
788 sslbio = BIO_new(BIO_f_ssl());
789 membio1 = BIO_new(BIO_s_mem());
791 if (sslbio == NULL || membio1 == NULL) {
792 printf("Malloc failure creating BIOs\n");
796 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
799 * If anything goes wrong here then we could leak memory, so this will
800 * be caught in a crypto-mdebug build
802 BIO_push(sslbio, membio1);
804 /* Verify chaning the rbio/wbio directly does not cause leaks */
805 if (fix.change_bio != NO_BIO_CHANGE) {
806 membio2 = BIO_new(BIO_s_mem());
807 if (membio2 == NULL) {
808 printf("Malloc failure creating membio2\n");
811 if (fix.change_bio == CHANGE_RBIO)
812 SSL_set0_rbio(ssl, membio2);
814 SSL_set0_wbio(ssl, membio2);
833 static int test_ssl_bio_pop_next_bio(void)
835 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
837 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
840 static int test_ssl_bio_pop_ssl_bio(void)
842 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
846 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
849 static int test_ssl_bio_change_rbio(void)
851 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
853 fixture.change_bio = CHANGE_RBIO;
855 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
858 static int test_ssl_bio_change_wbio(void)
860 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
862 fixture.change_bio = CHANGE_WBIO;
864 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
867 int test_main(int argc, char *argv[])
872 printf("Invalid argument count\n");
879 ADD_TEST(test_large_message_tls);
880 ADD_TEST(test_large_message_tls_read_ahead);
881 #ifndef OPENSSL_NO_DTLS
882 ADD_TEST(test_large_message_dtls);
884 ADD_TEST(test_tlsext_status_type);
885 ADD_TEST(test_session_with_only_int_cache);
886 ADD_TEST(test_session_with_only_ext_cache);
887 ADD_TEST(test_session_with_both_cache);
888 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
889 ADD_TEST(test_ssl_bio_pop_next_bio);
890 ADD_TEST(test_ssl_bio_pop_ssl_bio);
891 ADD_TEST(test_ssl_bio_change_rbio);
892 ADD_TEST(test_ssl_bio_change_wbio);
894 testresult = run_tests(argv[0]);
896 bio_s_mempacket_test_free();