2 * Copyright 2016-2018 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"
22 static char *cert = NULL;
23 static char *privkey = NULL;
25 #ifndef OPENSSL_NO_OCSP
26 static const unsigned char orespder[] = "Dummy OCSP Response";
27 static int ocsp_server_called = 0;
28 static int ocsp_client_called = 0;
30 static int cdummyarg = 1;
31 static X509 *ocspcert = NULL;
34 #define NUM_EXTRA_CERTS 40
36 static int execute_test_large_message(const SSL_METHOD *smeth,
37 const SSL_METHOD *cmeth,
38 int min_version, int max_version,
41 SSL_CTX *cctx = NULL, *sctx = NULL;
42 SSL *clientssl = NULL, *serverssl = NULL;
45 BIO *certbio = BIO_new_file(cert, "r");
46 X509 *chaincert = NULL;
49 if (certbio == NULL) {
50 printf("Can't load the certificate file\n");
53 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
56 if (chaincert == NULL) {
57 printf("Unable to load certificate for chain\n");
61 if (!create_ssl_ctx_pair(smeth, cmeth, min_version, max_version, &sctx,
62 &cctx, cert, privkey)) {
63 printf("Unable to create SSL_CTX pair\n");
69 * Test that read_ahead works correctly when dealing with large
72 SSL_CTX_set_read_ahead(cctx, 1);
76 * We assume the supplied certificate is big enough so that if we add
77 * NUM_EXTRA_CERTS it will make the overall message large enough. The
78 * default buffer size is requested to be 16k, but due to the way BUF_MEM
79 * works, it ends up allocating a little over 21k (16 * 4/3). So, in this test
80 * we need to have a message larger than that.
82 certlen = i2d_X509(chaincert, NULL);
83 OPENSSL_assert((certlen * NUM_EXTRA_CERTS)
84 > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3));
85 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
86 if (!X509_up_ref(chaincert)) {
87 printf("Unable to up ref cert\n");
90 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
91 printf("Unable to add extra chain cert %d\n", i);
97 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
98 printf("Unable to create SSL objects\n");
102 if (!create_ssl_connection(serverssl, clientssl)) {
103 printf("Unable to create SSL connection\n");
108 * Calling SSL_clear() first is not required but this tests that SSL_clear()
109 * doesn't leak (when using enable-crypto-mdebug).
111 if (!SSL_clear(serverssl)) {
112 printf("Unexpected failure from SSL_clear()\n");
118 X509_free(chaincert);
127 static int test_large_message_tls(void)
129 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
130 TLS1_VERSION, TLS_MAX_VERSION,
134 static int test_large_message_tls_read_ahead(void)
136 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
137 TLS1_VERSION, TLS_MAX_VERSION,
141 #ifndef OPENSSL_NO_DTLS
142 static int test_large_message_dtls(void)
145 * read_ahead is not relevant to DTLS because DTLS always acts as if
148 return execute_test_large_message(DTLS_server_method(),
149 DTLS_client_method(),
150 DTLS1_VERSION, DTLS_MAX_VERSION,
155 #ifndef OPENSSL_NO_OCSP
156 static int ocsp_server_cb(SSL *s, void *arg)
158 int *argi = (int *)arg;
159 unsigned char *orespdercopy = NULL;
160 STACK_OF(OCSP_RESPID) *ids = NULL;
161 OCSP_RESPID *id = NULL;
164 /* In this test we are expecting exactly 1 OCSP_RESPID */
165 SSL_get_tlsext_status_ids(s, &ids);
166 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
167 return SSL_TLSEXT_ERR_ALERT_FATAL;
169 id = sk_OCSP_RESPID_value(ids, 0);
170 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
171 return SSL_TLSEXT_ERR_ALERT_FATAL;
172 } else if (*argi != 1) {
173 return SSL_TLSEXT_ERR_ALERT_FATAL;
177 orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
178 if (orespdercopy == NULL)
179 return SSL_TLSEXT_ERR_ALERT_FATAL;
181 SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
183 ocsp_server_called = 1;
185 return SSL_TLSEXT_ERR_OK;
188 static int ocsp_client_cb(SSL *s, void *arg)
190 int *argi = (int *)arg;
191 const unsigned char *respderin;
194 if (*argi != 1 && *argi != 2)
197 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
199 if (memcmp(orespder, respderin, len) != 0)
202 ocsp_client_called = 1;
207 static int test_tlsext_status_type(void)
209 SSL_CTX *cctx = NULL, *sctx = NULL;
210 SSL *clientssl = NULL, *serverssl = NULL;
212 STACK_OF(OCSP_RESPID) *ids = NULL;
213 OCSP_RESPID *id = NULL;
216 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
217 TLS1_VERSION, TLS_MAX_VERSION, &sctx, &cctx,
219 printf("Unable to create SSL_CTX pair\n");
223 if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
224 printf("Unexpected initial value for "
225 "SSL_CTX_get_tlsext_status_type()\n");
229 /* First just do various checks getting and setting tlsext_status_type */
231 clientssl = SSL_new(cctx);
232 if (SSL_get_tlsext_status_type(clientssl) != -1) {
233 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
237 if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
238 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
242 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
243 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
250 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
251 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
255 if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
256 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
260 clientssl = SSL_new(cctx);
262 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
263 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
271 * Now actually do a handshake and check OCSP information is exchanged and
272 * the callbacks get called
275 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
276 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
277 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
278 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
280 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
281 printf("Unable to create SSL objects\n");
285 if (!create_ssl_connection(serverssl, clientssl)) {
286 printf("Unable to create SSL connection\n");
290 if (!ocsp_client_called || !ocsp_server_called) {
291 printf("OCSP callbacks not called\n");
300 /* Try again but this time force the server side callback to fail */
301 ocsp_client_called = 0;
302 ocsp_server_called = 0;
305 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
306 printf("Unable to create SSL objects\n");
310 /* This should fail because the callback will fail */
311 if (create_ssl_connection(serverssl, clientssl)) {
312 printf("Unexpected success creating the connection\n");
316 if (ocsp_client_called || ocsp_server_called) {
317 printf("OCSP callbacks successfully called unexpectedly\n");
327 * This time we'll get the client to send an OCSP_RESPID that it will
330 ocsp_client_called = 0;
331 ocsp_server_called = 0;
334 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
335 printf("Unable to create SSL objects\n");
340 * We'll just use any old cert for this test - it doesn't have to be an OCSP
341 * specific one. We'll use the server cert.
343 certbio = BIO_new_file(cert, "r");
344 if (certbio == NULL) {
345 printf("Can't load the certificate file\n");
348 id = OCSP_RESPID_new();
349 ids = sk_OCSP_RESPID_new_null();
350 ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
351 if (id == NULL || ids == NULL || ocspcert == NULL
352 || !OCSP_RESPID_set_by_key(id, ocspcert)
353 || !sk_OCSP_RESPID_push(ids, id)) {
354 printf("Unable to set OCSP_RESPIDs\n");
358 SSL_set_tlsext_status_ids(clientssl, ids);
359 /* Control has been transferred */
365 if (!create_ssl_connection(serverssl, clientssl)) {
366 printf("Unable to create SSL connection\n");
370 if (!ocsp_client_called || !ocsp_server_called) {
371 printf("OCSP callbacks not called\n");
382 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
383 OCSP_RESPID_free(id);
390 #endif /* ndef OPENSSL_NO_OCSP */
392 typedef struct ssl_session_test_fixture {
393 const char *test_case_name;
396 } SSL_SESSION_TEST_FIXTURE;
398 static int new_called = 0, remove_called = 0;
400 static SSL_SESSION_TEST_FIXTURE
401 ssl_session_set_up(const char *const test_case_name)
403 SSL_SESSION_TEST_FIXTURE fixture;
405 fixture.test_case_name = test_case_name;
406 fixture.use_ext_cache = 1;
407 fixture.use_int_cache = 1;
409 new_called = remove_called = 0;
414 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
418 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
425 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
430 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
432 SSL_CTX *sctx = NULL, *cctx = NULL;
433 SSL *serverssl1 = NULL, *clientssl1 = NULL;
434 SSL *serverssl2 = NULL, *clientssl2 = NULL;
435 #ifndef OPENSSL_NO_TLS1_1
436 SSL *serverssl3 = NULL, *clientssl3 = NULL;
438 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
441 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
442 TLS1_VERSION, TLS_MAX_VERSION, &sctx, &cctx,
444 printf("Unable to create SSL_CTX pair\n");
448 #ifndef OPENSSL_NO_TLS1_2
449 /* Only allow TLS1.2 so we can force a connection failure later */
450 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
453 /* Set up session cache */
454 if (fix.use_ext_cache) {
455 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
456 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
458 if (fix.use_int_cache) {
459 /* Also covers instance where both are set */
460 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
462 SSL_CTX_set_session_cache_mode(cctx,
463 SSL_SESS_CACHE_CLIENT
464 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
467 if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
469 printf("Unable to create SSL objects\n");
473 if (!create_ssl_connection(serverssl1, clientssl1)) {
474 printf("Unable to create SSL connection\n");
477 sess1 = SSL_get1_session(clientssl1);
479 printf("Unexpected NULL session\n");
483 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
484 /* Should have failed because it should already be in the cache */
485 printf("Unexpected success adding session to cache\n");
489 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
490 printf("Session not added to cache\n");
494 if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
495 printf("Unable to create second SSL objects\n");
499 if (!create_ssl_connection(serverssl2, clientssl2)) {
500 printf("Unable to create second SSL connection\n");
504 sess2 = SSL_get1_session(clientssl2);
506 printf("Unexpected NULL session from clientssl2\n");
510 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
511 printf("Remove session callback unexpectedly called\n");
516 * This should clear sess2 from the cache because it is a "bad" session. See
517 * SSL_set_session() documentation.
519 if (!SSL_set_session(clientssl2, sess1)) {
520 printf("Unexpected failure setting session\n");
524 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
525 printf("Failed to call callback to remove session\n");
530 if (SSL_get_session(clientssl2) != sess1) {
531 printf("Unexpected session found\n");
535 if (fix.use_int_cache) {
536 if (!SSL_CTX_add_session(cctx, sess2)) {
538 * Should have succeeded because it should not already be in the cache
540 printf("Unexpected failure adding session to cache\n");
544 if (!SSL_CTX_remove_session(cctx, sess2)) {
545 printf("Unexpected failure removing session from cache\n");
549 /* This is for the purposes of internal cache testing...ignore the
550 * counter for external cache
552 if (fix.use_ext_cache)
556 /* This shouldn't be in the cache so should fail */
557 if (SSL_CTX_remove_session(cctx, sess2)) {
558 printf("Unexpected success removing session from cache\n");
562 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
563 printf("Failed to call callback to remove session #2\n");
567 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
568 /* Force a connection failure */
569 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
571 if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
572 printf("Unable to create third SSL objects\n");
576 if (!SSL_set_session(clientssl3, sess1)) {
577 printf("Unable to set session for third connection\n");
581 /* This should fail because of the mismatched protocol versions */
582 if (create_ssl_connection(serverssl3, clientssl3)) {
583 printf("Unable to create third SSL connection\n");
588 /* We should have automatically removed the session from the cache */
589 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
590 printf("Failed to call callback to remove session #2\n");
594 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
596 * Should have succeeded because it should not already be in the cache
598 printf("Unexpected failure adding session to cache #2\n");
606 SSL_free(serverssl1);
607 SSL_free(clientssl1);
608 SSL_free(serverssl2);
609 SSL_free(clientssl2);
610 #ifndef OPENSSL_NO_TLS1_1
611 SSL_free(serverssl3);
612 SSL_free(clientssl3);
614 SSL_SESSION_free(sess1);
615 SSL_SESSION_free(sess2);
617 * Check if we need to remove any sessions up-refed for the external cache
620 SSL_SESSION_free(sess1);
622 SSL_SESSION_free(sess2);
629 static int test_session_with_only_int_cache(void)
631 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
633 fixture.use_ext_cache = 0;
635 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
638 static int test_session_with_only_ext_cache(void)
640 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
642 fixture.use_int_cache = 0;
644 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
647 static int test_session_with_both_cache(void)
649 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
651 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
658 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
660 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
675 static int test_ssl_set_bio(int idx)
677 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
680 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
682 int initrbio, initwbio, newrbio, newwbio;
686 printf("Failed to allocate SSL_CTX\n");
692 printf("Failed to allocate SSL object\n");
703 OPENSSL_assert(newwbio <= 2);
705 if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
706 || newwbio == USE_BIO_1) {
707 bio1 = BIO_new(BIO_s_mem());
709 printf("Failed to allocate bio1\n");
714 if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
715 || newwbio == USE_BIO_2) {
716 bio2 = BIO_new(BIO_s_mem());
718 printf("Failed to allocate bio2\n");
723 setupbio(&irbio, bio1, bio2, initrbio);
724 setupbio(&iwbio, bio1, bio2, initwbio);
727 * We want to maintain our own refs to these BIO, so do an up ref for each
728 * BIO that will have ownership transferred in the SSL_set_bio() call
732 if (iwbio != NULL && iwbio != irbio)
735 SSL_set_bio(ssl, irbio, iwbio);
737 setupbio(&nrbio, bio1, bio2, newrbio);
738 setupbio(&nwbio, bio1, bio2, newwbio);
741 * We will (maybe) transfer ownership again so do more up refs.
742 * SSL_set_bio() has some really complicated ownership rules where BIOs have
745 if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
747 if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
750 SSL_set_bio(ssl, nrbio, nwbio);
759 * This test is checking that the ref counting for SSL_set_bio is correct.
760 * If we get here and we did too many frees then we will fail in the above
761 * functions. If we haven't done enough then this will only be detected in
762 * a crypto-mdebug build
769 typedef struct ssl_bio_test_fixture {
770 const char *test_case_name;
772 enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
773 } SSL_BIO_TEST_FIXTURE;
775 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
777 SSL_BIO_TEST_FIXTURE fixture;
779 fixture.test_case_name = test_case_name;
781 fixture.change_bio = NO_BIO_CHANGE;
786 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
790 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
792 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
793 SSL_CTX *ctx = SSL_CTX_new(TLS_method());
798 printf("Failed to allocate SSL_CTX\n");
804 printf("Failed to allocate SSL object\n");
808 sslbio = BIO_new(BIO_f_ssl());
809 membio1 = BIO_new(BIO_s_mem());
811 if (sslbio == NULL || membio1 == NULL) {
812 printf("Malloc failure creating BIOs\n");
816 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
819 * If anything goes wrong here then we could leak memory, so this will
820 * be caught in a crypto-mdebug build
822 BIO_push(sslbio, membio1);
824 /* Verify changing the rbio/wbio directly does not cause leaks */
825 if (fix.change_bio != NO_BIO_CHANGE) {
826 membio2 = BIO_new(BIO_s_mem());
827 if (membio2 == NULL) {
828 printf("Malloc failure creating membio2\n");
831 if (fix.change_bio == CHANGE_RBIO)
832 SSL_set0_rbio(ssl, membio2);
834 SSL_set0_wbio(ssl, membio2);
853 static int test_ssl_bio_pop_next_bio(void)
855 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
857 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
860 static int test_ssl_bio_pop_ssl_bio(void)
862 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
866 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
869 static int test_ssl_bio_change_rbio(void)
871 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
873 fixture.change_bio = CHANGE_RBIO;
875 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
878 static int test_ssl_bio_change_wbio(void)
880 SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
882 fixture.change_bio = CHANGE_WBIO;
884 EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
888 /* The list of sig algs */
890 /* The length of the list */
892 /* A sigalgs list in string format */
894 /* Whether setting the list should succeed */
896 /* Whether creating a connection with the list should succeed */
900 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
901 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
902 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
903 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
904 static const int invalidlist2[] = {NID_sha256, NID_undef};
905 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
906 static const int invalidlist4[] = {NID_sha256};
907 static const sigalgs_list testsigalgs[] = {
908 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
909 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
910 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
911 {NULL, 0, "RSA+SHA256", 1, 1},
912 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
913 {NULL, 0, "ECDSA+SHA512", 1, 0},
914 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
915 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
916 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
917 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
918 {NULL, 0, "RSA", 0, 0},
919 {NULL, 0, "SHA256", 0, 0},
920 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
921 {NULL, 0, "Invalid", 0, 0}};
923 static int test_set_sigalgs(int idx)
925 SSL_CTX *cctx = NULL, *sctx = NULL;
926 SSL *clientssl = NULL, *serverssl = NULL;
928 const sigalgs_list *curr;
931 /* Should never happen */
932 if ((size_t)idx >= OSSL_NELEM(testsigalgs) * 2)
935 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
936 curr = testctx ? &testsigalgs[idx]
937 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
939 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
940 TLS1_VERSION, TLS_MAX_VERSION, &sctx, &cctx,
942 printf("Unable to create SSL_CTX pair\n");
948 if (curr->list != NULL)
949 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
951 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
955 printf("Unexpected failure setting sigalgs in SSL_CTX (%d)\n",
962 printf("Unexpected success setting sigalgs in SSL_CTX (%d)\n", idx);
967 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
968 printf("Unable to create SSL objects\n");
975 if (curr->list != NULL)
976 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
978 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
981 printf("Unexpected failure setting sigalgs in SSL (%d)\n", idx);
987 printf("Unexpected success setting sigalgs in SSL (%d)\n", idx);
992 if (curr->connsuccess != create_ssl_connection(serverssl, clientssl)) {
993 printf("Unexpected return value creating SSL connection (%d)\n", idx);
1000 SSL_free(serverssl);
1001 SSL_free(clientssl);
1008 static int clntaddcb = 0;
1009 static int clntparsecb = 0;
1010 static int srvaddcb = 0;
1011 static int srvparsecb = 0;
1012 static int snicb = 0;
1014 #define TEST_EXT_TYPE1 0xff00
1016 static int add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
1017 size_t *outlen, int *al, void *add_arg)
1019 int *server = (int *)add_arg;
1020 unsigned char *data;
1022 if (SSL_is_server(s))
1027 if (*server != SSL_is_server(s)
1028 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
1033 *outlen = sizeof(char);
1037 static void free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
1040 OPENSSL_free((unsigned char *)out);
1043 static int parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
1044 size_t inlen, int *al, void *parse_arg)
1046 int *server = (int *)parse_arg;
1048 if (SSL_is_server(s))
1053 if (*server != SSL_is_server(s)
1054 || inlen != sizeof(char)
1061 static int sni_cb(SSL *s, int *al, void *arg)
1063 SSL_CTX *ctx = (SSL_CTX *)arg;
1065 if (SSL_set_SSL_CTX(s, ctx) == NULL) {
1066 *al = SSL_AD_INTERNAL_ERROR;
1067 return SSL_TLSEXT_ERR_ALERT_FATAL;
1070 return SSL_TLSEXT_ERR_OK;
1074 * Custom call back tests.
1075 * Test 0: callbacks in TLSv1.2
1076 * Test 1: callbacks in TLSv1.2 with SNI
1078 static int test_custom_exts(int tst)
1080 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
1081 SSL *clientssl = NULL, *serverssl = NULL;
1083 static int server = 1;
1084 static int client = 0;
1085 SSL_SESSION *sess = NULL;
1087 /* Reset callback counters */
1088 clntaddcb = clntparsecb = srvaddcb = srvparsecb = 0;
1091 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
1092 TLS1_VERSION, TLS_MAX_VERSION, &sctx, &cctx,
1094 printf("Unable to create SSL_CTX pair\n");
1099 && !create_ssl_ctx_pair(TLS_server_method(), NULL,
1100 TLS1_VERSION, TLS_MAX_VERSION, &sctx2, NULL,
1102 printf("Unable to create SSL_CTX pair (2)\n");
1106 /* Create a client side custom extension */
1107 if (!SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1, add_cb, free_cb,
1108 &client, parse_cb, &client)) {
1109 printf("Unable to add client custom extension\n");
1113 /* Should not be able to add duplicates */
1114 if (SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1, add_cb, free_cb,
1115 &client, parse_cb, &client)) {
1116 printf("Unexpected success adding duplicate extension\n");
1120 /* Create a server side custom extension */
1121 if (!SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1, add_cb, free_cb,
1122 &server, parse_cb, &server)) {
1123 printf("Unable to add server custom extension\n");
1127 && !SSL_CTX_add_server_custom_ext(sctx2, TEST_EXT_TYPE1,
1131 printf("Unable to add server custom extension for SNI\n");
1135 /* Should not be able to add duplicates */
1136 if (SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1, add_cb, free_cb,
1137 &server, parse_cb, &server)) {
1138 printf("Unexpected success adding duplicate extension (2)\n");
1144 if (!SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb)
1145 || !SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)) {
1146 printf("Cannot set SNI callbacks\n");
1151 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)
1152 || !create_ssl_connection(serverssl, clientssl)) {
1153 printf("Cannot create SSL connection\n");
1161 || (tst != 1 && snicb != 0)
1162 || (tst == 1 && snicb != 1)) {
1163 printf("Incorrect callback counts\n");
1167 sess = SSL_get1_session(clientssl);
1168 SSL_shutdown(clientssl);
1169 SSL_shutdown(serverssl);
1170 SSL_free(serverssl);
1171 SSL_free(clientssl);
1172 serverssl = clientssl = NULL;
1175 /* We don't bother with the resumption aspects for this test */
1180 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)
1181 || !SSL_set_session(clientssl, sess)
1182 || !create_ssl_connection(serverssl, clientssl)) {
1183 printf("Cannot create resumption connection\n");
1188 * For a resumed session we expect to add the ClientHello extension but we
1189 * should ignore it on the server side.
1194 || srvparsecb != 1) {
1195 printf("Incorrect resumption callback counts\n");
1202 SSL_SESSION_free(sess);
1203 SSL_free(serverssl);
1204 SSL_free(clientssl);
1205 SSL_CTX_free(sctx2);
1211 static int test_ssl_pending(int tst)
1213 SSL_CTX *cctx = NULL, *sctx = NULL;
1214 SSL *clientssl = NULL, *serverssl = NULL;
1216 char msg[] = "A test message";
1221 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
1222 TLS1_VERSION, TLS_MAX_VERSION,
1223 &sctx, &cctx, cert, privkey)) {
1224 printf("Failed creating SSL_CTX pair\n");
1228 #ifndef OPENSSL_NO_DTLS
1229 if (!create_ssl_ctx_pair(DTLS_server_method(), DTLS_client_method(),
1230 DTLS1_VERSION, DTLS_MAX_VERSION,
1231 &sctx, &cctx, cert, privkey)) {
1232 printf("Failed creating SSL_CTX pair\n");
1240 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)
1241 || !create_ssl_connection(serverssl, clientssl)) {
1242 printf("Failed creating connection\n");
1246 written = SSL_write(serverssl, msg, sizeof(msg));
1247 if (written != sizeof(msg)
1248 || SSL_read(clientssl, buf, sizeof(buf)) != sizeof(buf)
1249 || SSL_pending(clientssl) != (int)(written - sizeof(buf))) {
1250 printf("Failed checking SSL_pending\n");
1257 SSL_free(serverssl);
1258 SSL_free(clientssl);
1266 int main(int argc, char *argv[])
1272 printf("Invalid argument count\n");
1279 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
1281 CRYPTO_set_mem_debug(1);
1282 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1284 ADD_TEST(test_large_message_tls);
1285 ADD_TEST(test_large_message_tls_read_ahead);
1286 #ifndef OPENSSL_NO_DTLS
1287 ADD_TEST(test_large_message_dtls);
1289 #ifndef OPENSSL_NO_OCSP
1290 ADD_TEST(test_tlsext_status_type);
1292 ADD_TEST(test_session_with_only_int_cache);
1293 ADD_TEST(test_session_with_only_ext_cache);
1294 ADD_TEST(test_session_with_both_cache);
1295 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
1296 ADD_TEST(test_ssl_bio_pop_next_bio);
1297 ADD_TEST(test_ssl_bio_pop_ssl_bio);
1298 ADD_TEST(test_ssl_bio_change_rbio);
1299 ADD_TEST(test_ssl_bio_change_wbio);
1300 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
1301 ADD_ALL_TESTS(test_custom_exts, 2);
1302 ADD_ALL_TESTS(test_ssl_pending, 2);
1304 testresult = run_tests(argv[0]);
1306 bio_s_mempacket_test_free();
1308 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
1309 if (CRYPTO_mem_leaks(err) <= 0)