|
| 1 | +/* |
| 2 | + * Copyright 2023-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License 2.0 (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 |
| 8 | + */ |
| 9 | +#include <openssl/ssl.h> |
| 10 | + |
| 11 | +#include "helpers/ssltestlib.h" |
| 12 | +#include "internal/dane.h" |
| 13 | +#include "testutil.h" |
| 14 | + |
| 15 | +static char *certsdir = NULL; |
| 16 | +static char *cert = NULL; |
| 17 | +static char *privkey = NULL; |
| 18 | +static OSSL_LIB_CTX *libctx = NULL; |
| 19 | + |
| 20 | +static const unsigned char sid_ctx[] = "sid"; |
| 21 | +static const unsigned char cache_id[] = "this is a test"; |
| 22 | +static const unsigned char cache_id2[] = "different"; |
| 23 | + |
| 24 | +static int test_client_cache(void) |
| 25 | +{ |
| 26 | + int ret = 0; |
| 27 | + SSL_CTX *cctx = NULL, *sctx = NULL; |
| 28 | + SSL *cssl = NULL, *sssl = NULL; |
| 29 | + SSL_SESSION *sess1 = NULL, *sess2 = NULL, *sess3 = NULL; |
| 30 | + |
| 31 | + if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
| 32 | + TLS_client_method(), TLS1_VERSION, TLS1_2_VERSION, |
| 33 | + &sctx, &cctx, cert, privkey))) |
| 34 | + return 0; |
| 35 | + |
| 36 | + SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT); |
| 37 | + if (!TEST_true(SSL_CTX_set_session_id_context(sctx, sid_ctx, sizeof(sid_ctx))) |
| 38 | + || !TEST_true(SSL_CTX_set_session_id_context(cctx, sid_ctx, sizeof(sid_ctx)))) |
| 39 | + goto end; |
| 40 | + |
| 41 | + /* Initial connection */ |
| 42 | + if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL)) |
| 43 | + || !TEST_true(SSL_set1_cache_id(cssl, cache_id, sizeof(cache_id))) |
| 44 | + || !TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)) |
| 45 | + || !TEST_ptr(sess1 = SSL_get1_session(cssl))) |
| 46 | + goto end; |
| 47 | + |
| 48 | + shutdown_ssl_connection(sssl, cssl); |
| 49 | + sssl = cssl = NULL; |
| 50 | + |
| 51 | + /* Test automatic assignment of session when client has cache_id set */ |
| 52 | + if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL)) |
| 53 | + || !TEST_true(SSL_set1_cache_id(cssl, cache_id, sizeof(cache_id))) |
| 54 | + || !TEST_ptr(sess2 = SSL_get1_previous_client_session(cssl)) |
| 55 | + || !TEST_ptr_eq(sess1, sess2) |
| 56 | + || !TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)) |
| 57 | + || !TEST_true(SSL_session_reused(cssl)) |
| 58 | + || !TEST_ptr(sess3 = SSL_get1_session(cssl)) |
| 59 | + || !TEST_ptr_eq(sess2, sess3)) |
| 60 | + goto end; |
| 61 | + |
| 62 | + shutdown_ssl_connection(sssl, cssl); |
| 63 | + sssl = cssl = NULL; |
| 64 | + |
| 65 | + /* Ensure client is not resumed when no cache_id is set */ |
| 66 | + if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL)) |
| 67 | + || !TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)) |
| 68 | + || !TEST_false(SSL_session_reused(cssl))) |
| 69 | + goto end; |
| 70 | + |
| 71 | + shutdown_ssl_connection(sssl, cssl); |
| 72 | + sssl = cssl = NULL; |
| 73 | + |
| 74 | + /* Ensure client is not resumed when a different cache_id is set */ |
| 75 | + if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL)) |
| 76 | + || !TEST_true(SSL_set1_cache_id(cssl, cache_id2, sizeof(cache_id2))) |
| 77 | + || !TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)) |
| 78 | + || !TEST_false(SSL_session_reused(cssl))) |
| 79 | + goto end; |
| 80 | + |
| 81 | + shutdown_ssl_connection(sssl, cssl); |
| 82 | + sssl = cssl = NULL; |
| 83 | + |
| 84 | + ret = 1; |
| 85 | + end: |
| 86 | + SSL_free(sssl); |
| 87 | + SSL_free(cssl); |
| 88 | + SSL_SESSION_free(sess1); |
| 89 | + SSL_SESSION_free(sess2); |
| 90 | + SSL_SESSION_free(sess3); |
| 91 | + SSL_CTX_free(sctx); |
| 92 | + SSL_CTX_free(cctx); |
| 93 | + return ret; |
| 94 | +} |
| 95 | +OPT_TEST_DECLARE_USAGE("certdir\n") |
| 96 | + |
| 97 | +int setup_tests(void) |
| 98 | +{ |
| 99 | + if (!test_skip_common_options()) { |
| 100 | + TEST_error("Error parsing test options\n"); |
| 101 | + return 0; |
| 102 | + } |
| 103 | + |
| 104 | + if (!TEST_ptr(certsdir = test_get_argument(0))) |
| 105 | + return 0; |
| 106 | + |
| 107 | + cert = test_mk_file_path(certsdir, "servercert.pem"); |
| 108 | + if (cert == NULL) |
| 109 | + goto err; |
| 110 | + |
| 111 | + privkey = test_mk_file_path(certsdir, "serverkey.pem"); |
| 112 | + if (privkey == NULL) |
| 113 | + goto err; |
| 114 | + |
| 115 | + libctx = OSSL_LIB_CTX_new(); |
| 116 | + if (libctx == NULL) |
| 117 | + goto err; |
| 118 | + |
| 119 | + ADD_TEST(test_client_cache); |
| 120 | + return 1; |
| 121 | + |
| 122 | + err: |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +void cleanup_tests(void) |
| 127 | +{ |
| 128 | + OPENSSL_free(cert); |
| 129 | + OPENSSL_free(privkey); |
| 130 | + OSSL_LIB_CTX_free(libctx); |
| 131 | + } |
0 commit comments