| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
// |
| 5 |
// Created by Smith, Justin on 6/13/25. |
| 6 |
// |
| 7 |
|
| 8 |
#ifndef SSL_TEST_H |
| 9 |
#define SSL_TEST_H |
| 10 |
#include "internal.h" |
| 11 |
#include "openssl/base.h" |
| 12 |
|
| 13 |
#include <gtest/gtest.h> |
| 14 |
|
| 15 |
BSSL_NAMESPACE_BEGIN |
| 16 |
|
| 17 |
#define TRACED_CALL(code) \ |
| 18 |
do { \ |
| 19 |
SCOPED_TRACE("<- called from here"); \ |
| 20 |
code; \ |
| 21 |
if (::testing::Test::HasFatalFailure()) { \ |
| 22 |
return; \ |
| 23 |
} \ |
| 24 |
} while (false) |
| 25 |
|
| 26 |
extern UniquePtr<SSL_SESSION> g_last_session; |
| 27 |
|
| 28 |
bool GetClientHello(SSL *ssl, std::vector<uint8_t> *out); |
| 29 |
|
| 30 |
struct VersionParam { |
| 31 |
uint16_t version; |
| 32 |
enum { is_tls, is_dtls } ssl_method; |
| 33 |
// This field is used to generate custom test name suffixes |
| 34 |
// based on the test parameters. |
| 35 |
const char name[20]; |
| 36 |
// SSL transfer: the sever SSL is encoded into bytes, and then decoded to |
| 37 |
// another SSL. After transfer, the encoded SSL is freed. The decoded one is |
| 38 |
// used to exchange data. This flag is to replay existing tests with the |
| 39 |
// transferred SSL. If false, the tests use the original server SSL. If true, |
| 40 |
// the tests are replayed with the transferred server SSL. Note: SSL transfer |
| 41 |
// works only with either TLS 1.2 or TLS 1.3 after handshake finished and all |
| 42 |
// post-handshake messages have been flushed. |
| 43 |
bool transfer_ssl; |
| 44 |
}; |
| 45 |
|
| 46 |
struct SSLTestParam { |
| 47 |
// SSL transfer: the sever SSL is encoded into bytes, and then decoded to |
| 48 |
// another SSL. After transfer, the encoded SSL is freed. The decoded one is |
| 49 |
// used to exchange data. This flag is to replay existing tests with the |
| 50 |
// transferred SSL. If false, the tests use the original server SSL. If true, |
| 51 |
// the tests are replayed with the transferred server SSL. Note: SSL transfer |
| 52 |
// works only with either TLS 1.2 or TLS 1.3 after handshake finished and all |
| 53 |
// post-handshake messages have been flushed. |
| 54 |
bool transfer_ssl; |
| 55 |
}; |
| 56 |
|
| 57 |
// If true, after handshake finished, the test uses the transferred SSL. |
| 58 |
static const bool TRANSFER_SSL = true; |
| 59 |
|
| 60 |
static const SSLTestParam kSSLTestParams[] = { |
| 61 |
{!TRANSFER_SSL}, |
| 62 |
{TRANSFER_SSL}, |
| 63 |
}; |
| 64 |
|
| 65 |
static const VersionParam kAllVersions[] = { |
| 66 |
{TLS1_VERSION, VersionParam::is_tls, "TLS1", !TRANSFER_SSL}, |
| 67 |
{TLS1_1_VERSION, VersionParam::is_tls, "TLS1_1", !TRANSFER_SSL}, |
| 68 |
{TLS1_2_VERSION, VersionParam::is_tls, "TLS1_2", !TRANSFER_SSL}, |
| 69 |
{TLS1_3_VERSION, VersionParam::is_tls, "TLS1_3", !TRANSFER_SSL}, |
| 70 |
{DTLS1_VERSION, VersionParam::is_dtls, "DTLS1", !TRANSFER_SSL}, |
| 71 |
{DTLS1_2_VERSION, VersionParam::is_dtls, "DTLS1_2", !TRANSFER_SSL}, |
| 72 |
{TLS1_2_VERSION, VersionParam::is_tls, "TLS1_2_SSL_TRANSFER", TRANSFER_SSL}, |
| 73 |
{TLS1_3_VERSION, VersionParam::is_tls, "TLS1_3_SSL_TRANSFER", TRANSFER_SSL}, |
| 74 |
}; |
| 75 |
|
| 76 |
uint16_t EpochFromSequence(uint64_t seq); |
| 77 |
|
| 78 |
|
| 79 |
struct ClientConfig { |
| 80 |
SSL_SESSION *session = nullptr; |
| 81 |
std::string servername; |
| 82 |
std::string verify_hostname; |
| 83 |
unsigned hostflags = 0; |
| 84 |
bool early_data = false; |
| 85 |
}; |
| 86 |
|
| 87 |
void ExpectSessionReused(SSL_CTX *client_ctx, SSL_CTX *server_ctx, |
| 88 |
SSL_SESSION *session, bool want_reused); |
| 89 |
|
| 90 |
UniquePtr<SSL_SESSION> CreateClientSession( |
| 91 |
SSL_CTX *client_ctx, SSL_CTX *server_ctx, |
| 92 |
const ClientConfig &config = ClientConfig()); |
| 93 |
|
| 94 |
void TransferSSL(bssl::UniquePtr<SSL> *in, SSL_CTX *in_ctx, |
| 95 |
bssl::UniquePtr<SSL> *out, bool free_in=true); |
| 96 |
|
| 97 |
bool ConnectClientAndServer(UniquePtr<SSL> *out_client, |
| 98 |
UniquePtr<SSL> *out_server, SSL_CTX *client_ctx, |
| 99 |
SSL_CTX *server_ctx, |
| 100 |
const ClientConfig &config = ClientConfig(), |
| 101 |
bool shed_handshake_config = true); |
| 102 |
|
| 103 |
bool FlushNewSessionTickets(SSL *client, SSL *server); |
| 104 |
|
| 105 |
UniquePtr<CRYPTO_BUFFER> GetChainTestCertificateBuffer(); |
| 106 |
UniquePtr<CRYPTO_BUFFER> GetChainTestIntermediateBuffer(); |
| 107 |
UniquePtr<X509> GetECDSATestCertificate(); |
| 108 |
UniquePtr<EVP_PKEY> GetECDSATestKey(); |
| 109 |
UniquePtr<CRYPTO_BUFFER> GetChainTestCertificateBuffer(); |
| 110 |
UniquePtr<X509> GetChainTestCertificate(); |
| 111 |
UniquePtr<EVP_PKEY> GetChainTestKey(); |
| 112 |
UniquePtr<X509> GetChainTestIntermediate(); |
| 113 |
|
| 114 |
UniquePtr<X509> GetLeafSecret(); |
| 115 |
UniquePtr<X509> GetLeafPublic(); |
| 116 |
UniquePtr<EVP_PKEY> GetLeafKey(); |
| 117 |
UniquePtr<X509> GetLeafRoot(); |
| 118 |
|
| 119 |
UniquePtr<X509> GetED25519TestCertificate(); |
| 120 |
UniquePtr<EVP_PKEY> GetED25519TestKey(); |
| 121 |
|
| 122 |
bool ExpectSingleError(int lib, int reason); |
| 123 |
|
| 124 |
UniquePtr<X509> X509FromBuffer(UniquePtr<CRYPTO_BUFFER> buffer); |
| 125 |
|
| 126 |
const char *GetVersionName(uint16_t version); |
| 127 |
|
| 128 |
void FrozenTimeCallback(const SSL *ssl, timeval *out_clock); |
| 129 |
|
| 130 |
bool ChainsEqual(STACK_OF(X509) *chain, const std::vector<X509 *> &expected); |
| 131 |
|
| 132 |
int SaveLastSession(SSL *ssl, SSL_SESSION *session); |
| 133 |
|
| 134 |
class SSLTest : public testing::TestWithParam<SSLTestParam> {}; |
| 135 |
|
| 136 |
UniquePtr<SSL_CTX> CreateContextWithTestCertificate(const SSL_METHOD *method); |
| 137 |
|
| 138 |
void SetUpExpectedNewCodePoint(SSL_CTX *ctx); |
| 139 |
|
| 140 |
UniquePtr<X509> GetTestCertificate(); |
| 141 |
|
| 142 |
UniquePtr<EVP_PKEY> GetTestKey(); |
| 143 |
|
| 144 |
UniquePtr<EVP_PKEY> KeyFromPEM(const char *pem); |
| 145 |
|
| 146 |
bool CreateClientAndServer(bssl::UniquePtr<SSL> *out_client, |
| 147 |
bssl::UniquePtr<SSL> *out_server, |
| 148 |
SSL_CTX *client_ctx, SSL_CTX *server_ctx); |
| 149 |
|
| 150 |
bool CompleteHandshakes(SSL *client, SSL *server); |
| 151 |
|
| 152 |
void SetUpExpectedOldCodePoint(SSL_CTX *ctx); |
| 153 |
|
| 154 |
UniquePtr<EVP_PKEY> KeyFromPEM(const char *pem); |
| 155 |
|
| 156 |
BSSL_NAMESPACE_END |
| 157 |
|
| 158 |
#endif // SSL_TEST_H |