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
10 #include <openssl/opensslconf.h>
11 #include <openssl/bio.h>
12 #include <openssl/crypto.h>
13 #include <openssl/ssl.h>
15 #include "ssltestlib.h"
18 static char *cert = NULL;
19 static char *privkey = NULL;
21 static int test_tlsext_status_type(void)
27 /* Test tlsext_status_type */
28 ctx = SSL_CTX_new(TLS_method());
30 if (SSL_CTX_get_tlsext_status_type(ctx) != -1) {
31 printf("Unexpected initial value for "
32 "SSL_CTX_get_tlsext_status_type()\n");
38 if (SSL_get_tlsext_status_type(con) != -1) {
39 printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
43 if (!SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp)) {
44 printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
48 if (SSL_get_tlsext_status_type(con) != TLSEXT_STATUSTYPE_ocsp) {
49 printf("Unexpected result for SSL_get_tlsext_status_type()\n");
56 if (!SSL_CTX_set_tlsext_status_type(ctx, TLSEXT_STATUSTYPE_ocsp)) {
57 printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
61 if (SSL_CTX_get_tlsext_status_type(ctx) != TLSEXT_STATUSTYPE_ocsp) {
62 printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
68 if (SSL_get_tlsext_status_type(con) != TLSEXT_STATUSTYPE_ocsp) {
69 printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
82 typedef struct ssl_session_test_fixture {
83 const char *test_case_name;
86 } SSL_SESSION_TEST_FIXTURE;
88 static int new_called = 0, remove_called = 0;
90 static SSL_SESSION_TEST_FIXTURE
91 ssl_session_set_up(const char *const test_case_name)
93 SSL_SESSION_TEST_FIXTURE fixture;
95 fixture.test_case_name = test_case_name;
96 fixture.use_ext_cache = 1;
97 fixture.use_int_cache = 1;
99 new_called = remove_called = 0;
104 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
108 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
115 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
120 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
122 SSL_CTX *sctx = NULL, *cctx = NULL;
123 SSL *serverssl1 = NULL, *clientssl1 = NULL;
124 SSL *serverssl2 = NULL, *clientssl2 = NULL;
125 SSL *serverssl3 = NULL, *clientssl3 = NULL;
126 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
129 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
130 &cctx, cert, privkey)) {
131 printf("Unable to create SSL_CTX pair\n");
135 #ifndef OPENSSL_NO_TLS1_2
136 /* Only allow TLS1.2 so we can force a connection failure later */
137 SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
140 /* Set up session cache */
141 if (fix.use_ext_cache) {
142 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
143 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
145 if (fix.use_int_cache) {
146 /* Also covers instance where both are set */
147 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
149 SSL_CTX_set_session_cache_mode(cctx,
150 SSL_SESS_CACHE_CLIENT
151 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
154 if (!create_ssl_connection(sctx, cctx, &serverssl1, &clientssl1, NULL,
156 printf("Unable to create SSL connection\n");
159 sess1 = SSL_get1_session(clientssl1);
161 printf("Unexpected NULL session\n");
165 if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
166 /* Should have failed because it should already be in the cache */
167 printf("Unexpected success adding session to cache\n");
171 if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
172 printf("Session not added to cache\n");
176 if (!create_ssl_connection(sctx, cctx, &serverssl2, &clientssl2, NULL,
178 printf("Unable to create second SSL connection\n");
182 sess2 = SSL_get1_session(clientssl2);
184 printf("Unexpected NULL session from clientssl2\n");
188 if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
189 printf("Remove session callback unexpectedly called\n");
194 * This should clear sess2 from the cache because it is a "bad" session. See
195 * SSL_set_session() documentation.
197 if (!SSL_set_session(clientssl2, sess1)) {
198 printf("Unexpected failure setting session\n");
202 if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
203 printf("Failed to call callback to remove session\n");
208 if (SSL_get_session(clientssl2) != sess1) {
209 printf("Unexpected session found\n");
213 if (fix.use_int_cache) {
214 if (!SSL_CTX_add_session(cctx, sess2)) {
216 * Should have succeeded because it should not already be in the cache
218 printf("Unexpected failure adding session to cache\n");
222 if (!SSL_CTX_remove_session(cctx, sess2)) {
223 printf("Unexpected failure removing session from cache\n");
227 /* This is for the purposes of internal cache testing...ignore the
228 * counter for external cache
230 if (fix.use_ext_cache)
234 /* This shouldn't be in the cache so should fail */
235 if (SSL_CTX_remove_session(cctx, sess2)) {
236 printf("Unexpected success removing session from cache\n");
240 if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
241 printf("Failed to call callback to remove session #2\n");
245 #ifndef OPENSSL_NO_TLS1_1
246 /* Force a connection failure */
247 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
248 clientssl3 = SSL_new(cctx);
249 if (clientssl3 == NULL) {
250 printf("Malloc failure\n");
253 if (!SSL_set_session(clientssl3, sess1)) {
254 printf("Unable to set session for third connection\n");
258 /* This should fail because of the mismatched protocol versions */
259 if (create_ssl_connection(sctx, cctx, &serverssl3, &clientssl3, NULL,
261 printf("Unexpected success creating SSL connection\n");
265 /* We should have automatically removed the session from the cache */
266 if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
267 printf("Failed to call callback to remove session #2\n");
271 if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
273 * Should have succeeded because it should not already be in the cache
275 printf("Unexpected failure adding session to cache #2\n");
283 SSL_free(serverssl1);
284 SSL_free(clientssl1);
285 SSL_free(serverssl2);
286 SSL_free(clientssl2);
287 SSL_free(serverssl3);
288 SSL_free(clientssl3);
289 SSL_SESSION_free(sess1);
290 SSL_SESSION_free(sess2);
292 * Check if we need to remove any sessions up-refed for the external cache
295 SSL_SESSION_free(sess1);
297 SSL_SESSION_free(sess2);
304 static int test_session_with_only_int_cache(void) {
305 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
307 fixture.use_ext_cache = 0;
309 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
312 static int test_session_with_only_ext_cache(void) {
313 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
315 fixture.use_int_cache = 0;
317 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
320 static int test_session_with_both_cache(void) {
321 SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
323 EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
326 int main(int argc, char *argv[])
332 printf("Invalid argument count\n");
339 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
341 CRYPTO_set_mem_debug(1);
342 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
344 ADD_TEST(test_tlsext_status_type);
345 ADD_TEST(test_session_with_only_int_cache);
346 ADD_TEST(test_session_with_only_ext_cache);
347 ADD_TEST(test_session_with_both_cache);
349 testresult = run_tests(argv[0]);
351 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
352 if (CRYPTO_mem_leaks(err) <= 0)