| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <limits.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
#include <time.h> |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <array> |
| 11 |
#include <limits> |
| 12 |
#include <string> |
| 13 |
#include <utility> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
#include <gtest/gtest.h> |
| 17 |
|
| 18 |
#include <openssl/aead.h> |
| 19 |
#include <openssl/base64.h> |
| 20 |
#include <openssl/bio.h> |
| 21 |
#include <openssl/bytestring.h> |
| 22 |
#include <openssl/cipher.h> |
| 23 |
#include <openssl/crypto.h> |
| 24 |
#include <openssl/curve25519.h> |
| 25 |
#include <openssl/err.h> |
| 26 |
#include <openssl/hmac.h> |
| 27 |
#include <openssl/hpke.h> |
| 28 |
#include <openssl/pem.h> |
| 29 |
#include <openssl/rand.h> |
| 30 |
#include <openssl/sha.h> |
| 31 |
#include <openssl/ssl.h> |
| 32 |
#include <openssl/x509.h> |
| 33 |
|
| 34 |
#include "../crypto/fipsmodule/ec/internal.h" |
| 35 |
#include "../crypto/fipsmodule/ml_kem/ml_kem.h" |
| 36 |
#include "../crypto/internal.h" |
| 37 |
#include "../crypto/test/file_util.h" |
| 38 |
#include "../crypto/test/test_util.h" |
| 39 |
#include "internal.h" |
| 40 |
#include "ssl_common_test.h" |
| 41 |
|
| 42 |
#if defined(OPENSSL_WINDOWS) |
| 43 |
// Windows defines struct timeval in winsock2.h. |
| 44 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 45 |
#include <winsock2.h> |
| 46 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 47 |
#else |
| 48 |
#include <sys/time.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
#if defined(OPENSSL_THREADS) |
| 52 |
#include <thread> |
| 53 |
#endif |
| 54 |
|
| 55 |
BSSL_NAMESPACE_BEGIN |
| 56 |
|
| 57 |
namespace { |
| 58 |
|
| 59 |
INSTANTIATE_TEST_SUITE_P(SSLTests, SSLTest, testing::ValuesIn(kSSLTestParams), |
| 60 |
[](const testing::TestParamInfo<SSLTestParam> &i) { |
| 61 |
if (i.param.transfer_ssl) { |
| 62 |
return "SSL_Transfer"; |
| 63 |
} else { |
| 64 |
return "NO_SSL_Transfer"; |
| 65 |
} |
| 66 |
}); |
| 67 |
|
| 68 |
static bssl::UniquePtr<SSL_CTX> CreateContextWithCertificate( |
| 69 |
const SSL_METHOD *method, bssl::UniquePtr<X509> cert, |
| 70 |
bssl::UniquePtr<EVP_PKEY> key) { |
| 71 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(method)); |
| 72 |
if (!ctx || !cert || !key || |
| 73 |
!SSL_CTX_use_certificate(ctx.get(), cert.get()) || |
| 74 |
!SSL_CTX_use_PrivateKey(ctx.get(), key.get())) { |
| 75 |
return nullptr; |
| 76 |
} |
| 77 |
return ctx; |
| 78 |
} |
| 79 |
|
| 80 |
// Test that |SSL_get_client_CA_list| echoes back the configured parameter even |
| 81 |
// before configuring as a server. |
| 82 |
TEST(SSLTest, ClientCAList) { |
| 83 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 84 |
ASSERT_TRUE(ctx); |
| 85 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 86 |
ASSERT_TRUE(ssl); |
| 87 |
|
| 88 |
bssl::UniquePtr<X509_NAME> name(X509_NAME_new()); |
| 89 |
ASSERT_TRUE(name); |
| 90 |
|
| 91 |
bssl::UniquePtr<X509_NAME> name_dup(X509_NAME_dup(name.get())); |
| 92 |
ASSERT_TRUE(name_dup); |
| 93 |
|
| 94 |
bssl::UniquePtr<STACK_OF(X509_NAME)> stack(sk_X509_NAME_new_null()); |
| 95 |
ASSERT_TRUE(stack); |
| 96 |
ASSERT_TRUE(PushToStack(stack.get(), std::move(name_dup))); |
| 97 |
|
| 98 |
// |SSL_set_client_CA_list| takes ownership. |
| 99 |
SSL_set_client_CA_list(ssl.get(), stack.release()); |
| 100 |
|
| 101 |
STACK_OF(X509_NAME) *result = SSL_get_client_CA_list(ssl.get()); |
| 102 |
ASSERT_TRUE(result); |
| 103 |
ASSERT_EQ(1u, sk_X509_NAME_num(result)); |
| 104 |
EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(result, 0), name.get())); |
| 105 |
} |
| 106 |
|
| 107 |
TEST(SSLTest, AddClientCA) { |
| 108 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 109 |
ASSERT_TRUE(ctx); |
| 110 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 111 |
ASSERT_TRUE(ssl); |
| 112 |
|
| 113 |
bssl::UniquePtr<X509> cert1 = GetTestCertificate(); |
| 114 |
bssl::UniquePtr<X509> cert2 = GetChainTestCertificate(); |
| 115 |
ASSERT_TRUE(cert1 && cert2); |
| 116 |
X509_NAME *name1 = X509_get_subject_name(cert1.get()); |
| 117 |
X509_NAME *name2 = X509_get_subject_name(cert2.get()); |
| 118 |
|
| 119 |
EXPECT_EQ(0u, sk_X509_NAME_num(SSL_get_client_CA_list(ssl.get()))); |
| 120 |
|
| 121 |
ASSERT_TRUE(SSL_add_client_CA(ssl.get(), cert1.get())); |
| 122 |
ASSERT_TRUE(SSL_add_client_CA(ssl.get(), cert2.get())); |
| 123 |
|
| 124 |
STACK_OF(X509_NAME) *list = SSL_get_client_CA_list(ssl.get()); |
| 125 |
ASSERT_EQ(2u, sk_X509_NAME_num(list)); |
| 126 |
EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 0), name1)); |
| 127 |
EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 1), name2)); |
| 128 |
|
| 129 |
ASSERT_TRUE(SSL_add_client_CA(ssl.get(), cert1.get())); |
| 130 |
|
| 131 |
list = SSL_get_client_CA_list(ssl.get()); |
| 132 |
ASSERT_EQ(3u, sk_X509_NAME_num(list)); |
| 133 |
EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 0), name1)); |
| 134 |
EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 1), name2)); |
| 135 |
EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 2), name1)); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
TEST(SSLTest, TLS13ExporterAvailability) { |
| 140 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 141 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 142 |
CreateContextWithTestCertificate(TLS_method()); |
| 143 |
ASSERT_TRUE(client_ctx); |
| 144 |
ASSERT_TRUE(server_ctx); |
| 145 |
// Configure only TLS 1.3. |
| 146 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 147 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 148 |
|
| 149 |
bssl::UniquePtr<SSL> client, server; |
| 150 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 151 |
server_ctx.get())); |
| 152 |
|
| 153 |
std::vector<uint8_t> buffer(32); |
| 154 |
const char *label = "EXPORTER-test-label"; |
| 155 |
|
| 156 |
// The exporters are not available before the handshake starts. |
| 157 |
EXPECT_FALSE(SSL_export_keying_material(client.get(), buffer.data(), |
| 158 |
buffer.size(), label, strlen(label), |
| 159 |
nullptr, 0, 0)); |
| 160 |
EXPECT_FALSE(SSL_export_keying_material(server.get(), buffer.data(), |
| 161 |
buffer.size(), label, strlen(label), |
| 162 |
nullptr, 0, 0)); |
| 163 |
|
| 164 |
// Send the client's first flight of handshake messages. |
| 165 |
int client_ret = SSL_do_handshake(client.get()); |
| 166 |
EXPECT_EQ(SSL_get_error(client.get(), client_ret), SSL_ERROR_WANT_READ); |
| 167 |
|
| 168 |
// The handshake isn't far enough for the exporters to work. |
| 169 |
EXPECT_FALSE(SSL_export_keying_material(client.get(), buffer.data(), |
| 170 |
buffer.size(), label, strlen(label), |
| 171 |
nullptr, 0, 0)); |
| 172 |
EXPECT_FALSE(SSL_export_keying_material(server.get(), buffer.data(), |
| 173 |
buffer.size(), label, strlen(label), |
| 174 |
nullptr, 0, 0)); |
| 175 |
|
| 176 |
// Send all the server's handshake messages. |
| 177 |
int server_ret = SSL_do_handshake(server.get()); |
| 178 |
EXPECT_EQ(SSL_get_error(server.get(), server_ret), SSL_ERROR_WANT_READ); |
| 179 |
|
| 180 |
// At this point in the handshake, the server should have the exporter key |
| 181 |
// derived since it's sent its Finished message. The client hasn't yet |
| 182 |
// processed the server's handshake messages, so the exporter shouldn't be |
| 183 |
// available to the client. |
| 184 |
EXPECT_FALSE(SSL_export_keying_material(client.get(), buffer.data(), |
| 185 |
buffer.size(), label, strlen(label), |
| 186 |
nullptr, 0, 0)); |
| 187 |
EXPECT_TRUE(SSL_export_keying_material(server.get(), buffer.data(), |
| 188 |
buffer.size(), label, strlen(label), |
| 189 |
nullptr, 0, 0)); |
| 190 |
|
| 191 |
// Finish the handshake on the client. |
| 192 |
EXPECT_EQ(SSL_do_handshake(client.get()), 1); |
| 193 |
|
| 194 |
// The exporter should be available on both endpoints. |
| 195 |
EXPECT_TRUE(SSL_export_keying_material(client.get(), buffer.data(), |
| 196 |
buffer.size(), label, strlen(label), |
| 197 |
nullptr, 0, 0)); |
| 198 |
EXPECT_TRUE(SSL_export_keying_material(server.get(), buffer.data(), |
| 199 |
buffer.size(), label, strlen(label), |
| 200 |
nullptr, 0, 0)); |
| 201 |
|
| 202 |
// Finish the handshake on the server. |
| 203 |
EXPECT_EQ(SSL_do_handshake(server.get()), 1); |
| 204 |
|
| 205 |
// The exporter should still be available on both endpoints. |
| 206 |
EXPECT_TRUE(SSL_export_keying_material(client.get(), buffer.data(), |
| 207 |
buffer.size(), label, strlen(label), |
| 208 |
nullptr, 0, 0)); |
| 209 |
EXPECT_TRUE(SSL_export_keying_material(server.get(), buffer.data(), |
| 210 |
buffer.size(), label, strlen(label), |
| 211 |
nullptr, 0, 0)); |
| 212 |
} |
| 213 |
|
| 214 |
static void AppendSession(SSL_SESSION *session, void *arg) { |
| 215 |
std::vector<SSL_SESSION *> *out = |
| 216 |
reinterpret_cast<std::vector<SSL_SESSION *> *>(arg); |
| 217 |
out->push_back(session); |
| 218 |
} |
| 219 |
|
| 220 |
// CacheEquals returns true if |ctx|'s session cache consists of |expected|, in |
| 221 |
// order. |
| 222 |
static bool CacheEquals(SSL_CTX *ctx, |
| 223 |
const std::vector<SSL_SESSION *> &expected) { |
| 224 |
// Check the linked list. |
| 225 |
SSL_SESSION *ptr = ctx->session_cache_head; |
| 226 |
for (SSL_SESSION *session : expected) { |
| 227 |
if (ptr != session) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
// Redundant w/ above, but avoids static analysis failure |
| 231 |
if (ptr == nullptr) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
// TODO(davidben): This is an absurd way to denote the end of the list. |
| 235 |
if (ptr->next == |
| 236 |
reinterpret_cast<SSL_SESSION *>(&ctx->session_cache_tail)) { |
| 237 |
ptr = nullptr; |
| 238 |
} else { |
| 239 |
ptr = ptr->next; |
| 240 |
} |
| 241 |
} |
| 242 |
if (ptr != nullptr) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
// Check the hash table. |
| 247 |
std::vector<SSL_SESSION *> actual, expected_copy; |
| 248 |
lh_SSL_SESSION_doall_arg(ctx->sessions, AppendSession, &actual); |
| 249 |
expected_copy = expected; |
| 250 |
|
| 251 |
std::sort(actual.begin(), actual.end()); |
| 252 |
std::sort(expected_copy.begin(), expected_copy.end()); |
| 253 |
|
| 254 |
return actual == expected_copy; |
| 255 |
} |
| 256 |
|
| 257 |
static bssl::UniquePtr<SSL_SESSION> CreateTestSession(uint32_t number) { |
| 258 |
bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(TLS_method())); |
| 259 |
if (!ssl_ctx) { |
| 260 |
return nullptr; |
| 261 |
} |
| 262 |
bssl::UniquePtr<SSL_SESSION> ret(SSL_SESSION_new(ssl_ctx.get())); |
| 263 |
if (!ret) { |
| 264 |
return nullptr; |
| 265 |
} |
| 266 |
|
| 267 |
uint8_t id[SSL3_SSL_SESSION_ID_LENGTH] = {0}; |
| 268 |
OPENSSL_memcpy(id, &number, sizeof(number)); |
| 269 |
if (!SSL_SESSION_set1_id(ret.get(), id, sizeof(id))) { |
| 270 |
return nullptr; |
| 271 |
} |
| 272 |
return ret; |
| 273 |
} |
| 274 |
|
| 275 |
// Test that the internal session cache behaves as expected. |
| 276 |
TEST(SSLTest, InternalSessionCache) { |
| 277 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 278 |
ASSERT_TRUE(ctx); |
| 279 |
|
| 280 |
// Prepare 10 test sessions. |
| 281 |
std::vector<bssl::UniquePtr<SSL_SESSION>> sessions; |
| 282 |
for (int i = 0; i < 10; i++) { |
| 283 |
bssl::UniquePtr<SSL_SESSION> session = CreateTestSession(i); |
| 284 |
ASSERT_TRUE(session); |
| 285 |
sessions.push_back(std::move(session)); |
| 286 |
} |
| 287 |
|
| 288 |
SSL_CTX_sess_set_cache_size(ctx.get(), 5); |
| 289 |
|
| 290 |
// Insert all the test sessions. |
| 291 |
for (const auto &session : sessions) { |
| 292 |
ASSERT_TRUE(SSL_CTX_add_session(ctx.get(), session.get())); |
| 293 |
} |
| 294 |
|
| 295 |
// Only the last five should be in the list. |
| 296 |
ASSERT_TRUE(CacheEquals( |
| 297 |
ctx.get(), {sessions[9].get(), sessions[8].get(), sessions[7].get(), |
| 298 |
sessions[6].get(), sessions[5].get()})); |
| 299 |
|
| 300 |
// Inserting an element already in the cache should fail and leave the cache |
| 301 |
// unchanged. |
| 302 |
ASSERT_FALSE(SSL_CTX_add_session(ctx.get(), sessions[7].get())); |
| 303 |
ASSERT_TRUE(CacheEquals( |
| 304 |
ctx.get(), {sessions[9].get(), sessions[8].get(), sessions[7].get(), |
| 305 |
sessions[6].get(), sessions[5].get()})); |
| 306 |
|
| 307 |
// Although collisions should be impossible (256-bit session IDs), the cache |
| 308 |
// must handle them gracefully. |
| 309 |
bssl::UniquePtr<SSL_SESSION> collision(CreateTestSession(7)); |
| 310 |
ASSERT_TRUE(collision); |
| 311 |
ASSERT_TRUE(SSL_CTX_add_session(ctx.get(), collision.get())); |
| 312 |
ASSERT_TRUE(CacheEquals( |
| 313 |
ctx.get(), {collision.get(), sessions[9].get(), sessions[8].get(), |
| 314 |
sessions[6].get(), sessions[5].get()})); |
| 315 |
|
| 316 |
// Removing sessions behaves correctly. |
| 317 |
ASSERT_TRUE(SSL_CTX_remove_session(ctx.get(), sessions[6].get())); |
| 318 |
ASSERT_TRUE(CacheEquals(ctx.get(), {collision.get(), sessions[9].get(), |
| 319 |
sessions[8].get(), sessions[5].get()})); |
| 320 |
|
| 321 |
// Removing sessions requires an exact match. |
| 322 |
ASSERT_FALSE(SSL_CTX_remove_session(ctx.get(), sessions[0].get())); |
| 323 |
ASSERT_FALSE(SSL_CTX_remove_session(ctx.get(), sessions[7].get())); |
| 324 |
|
| 325 |
// The cache remains unchanged. |
| 326 |
ASSERT_TRUE(CacheEquals(ctx.get(), {collision.get(), sessions[9].get(), |
| 327 |
sessions[8].get(), sessions[5].get()})); |
| 328 |
} |
| 329 |
|
| 330 |
static const uint8_t kTestName[] = { |
| 331 |
0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, |
| 332 |
0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, |
| 333 |
0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, |
| 334 |
0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, |
| 335 |
0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, |
| 336 |
0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, |
| 337 |
}; |
| 338 |
|
| 339 |
// Test that, after seeing TLS 1.2 in response to early data, |SSL_write| |
| 340 |
// continues to report |SSL_R_WRONG_VERSION_ON_EARLY_DATA|. See |
| 341 |
// https://crbug.com/1078515. |
| 342 |
TEST(SSLTest, WriteAfterWrongVersionOnEarlyData) { |
| 343 |
// Set up some 0-RTT-enabled contexts. |
| 344 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 345 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 346 |
CreateContextWithTestCertificate(TLS_method()); |
| 347 |
ASSERT_TRUE(client_ctx); |
| 348 |
ASSERT_TRUE(server_ctx); |
| 349 |
SSL_CTX_set_early_data_enabled(client_ctx.get(), 1); |
| 350 |
SSL_CTX_set_early_data_enabled(server_ctx.get(), 1); |
| 351 |
SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 352 |
SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 353 |
|
| 354 |
// Get an early-data-capable session. |
| 355 |
bssl::UniquePtr<SSL_SESSION> session = |
| 356 |
CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 357 |
ASSERT_TRUE(session); |
| 358 |
EXPECT_TRUE(SSL_SESSION_early_data_capable(session.get())); |
| 359 |
|
| 360 |
// Offer the session to the server, but now the server speaks TLS 1.2. |
| 361 |
bssl::UniquePtr<SSL> client, server; |
| 362 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 363 |
server_ctx.get())); |
| 364 |
SSL_set_session(client.get(), session.get()); |
| 365 |
EXPECT_TRUE(SSL_set_max_proto_version(server.get(), TLS1_2_VERSION)); |
| 366 |
|
| 367 |
// The client handshake initially succeeds in the early data state. |
| 368 |
EXPECT_EQ(1, SSL_do_handshake(client.get())); |
| 369 |
EXPECT_TRUE(SSL_in_early_data(client.get())); |
| 370 |
|
| 371 |
// The server processes the ClientHello and negotiates TLS 1.2. |
| 372 |
EXPECT_EQ(-1, SSL_do_handshake(server.get())); |
| 373 |
EXPECT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server.get(), -1)); |
| 374 |
EXPECT_EQ(TLS1_2_VERSION, SSL_version(server.get())); |
| 375 |
|
| 376 |
// Capture the client's output. |
| 377 |
bssl::UniquePtr<BIO> mem(BIO_new(BIO_s_mem())); |
| 378 |
ASSERT_TRUE(mem); |
| 379 |
SSL_set0_wbio(client.get(), bssl::UpRef(mem).release()); |
| 380 |
|
| 381 |
// The client processes the ServerHello and fails. |
| 382 |
EXPECT_EQ(-1, SSL_do_handshake(client.get())); |
| 383 |
EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(client.get(), -1)); |
| 384 |
EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 385 |
SSL_R_WRONG_VERSION_ON_EARLY_DATA)); |
| 386 |
|
| 387 |
// The client should have written an alert to the transport. |
| 388 |
const uint8_t *unused = nullptr; |
| 389 |
size_t len = 0; |
| 390 |
ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 391 |
EXPECT_NE(0u, len); |
| 392 |
EXPECT_TRUE(BIO_reset(mem.get())); |
| 393 |
|
| 394 |
// Writing should fail, with the same error as the handshake. |
| 395 |
EXPECT_EQ(-1, SSL_write(client.get(), "a", 1)); |
| 396 |
EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(client.get(), -1)); |
| 397 |
EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 398 |
SSL_R_WRONG_VERSION_ON_EARLY_DATA)); |
| 399 |
|
| 400 |
// Nothing should be written to the transport. |
| 401 |
ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 402 |
EXPECT_EQ(0u, len); |
| 403 |
} |
| 404 |
|
| 405 |
TEST(SSLTest, SessionDuplication) { |
| 406 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 407 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 408 |
CreateContextWithTestCertificate(TLS_method()); |
| 409 |
ASSERT_TRUE(client_ctx); |
| 410 |
ASSERT_TRUE(server_ctx); |
| 411 |
|
| 412 |
bssl::UniquePtr<SSL> client, server; |
| 413 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 414 |
server_ctx.get())); |
| 415 |
|
| 416 |
SSL_SESSION *session0 = SSL_get_session(client.get()); |
| 417 |
bssl::UniquePtr<SSL_SESSION> session1 = |
| 418 |
bssl::SSL_SESSION_dup(session0, SSL_SESSION_DUP_ALL); |
| 419 |
ASSERT_TRUE(session1); |
| 420 |
|
| 421 |
session1->not_resumable = false; |
| 422 |
|
| 423 |
uint8_t *s0_bytes = nullptr, *s1_bytes = nullptr; |
| 424 |
size_t s0_len = 0, s1_len = 0; |
| 425 |
|
| 426 |
ASSERT_TRUE(SSL_SESSION_to_bytes(session0, &s0_bytes, &s0_len)); |
| 427 |
bssl::UniquePtr<uint8_t> free_s0(s0_bytes); |
| 428 |
|
| 429 |
ASSERT_TRUE(SSL_SESSION_to_bytes(session1.get(), &s1_bytes, &s1_len)); |
| 430 |
bssl::UniquePtr<uint8_t> free_s1(s1_bytes); |
| 431 |
|
| 432 |
EXPECT_EQ(Bytes(s0_bytes, s0_len), Bytes(s1_bytes, s1_len)); |
| 433 |
} |
| 434 |
|
| 435 |
#if !defined(OPENSSL_NO_SOCK) |
| 436 |
static void ExpectFDs(const SSL *ssl, int rfd, int wfd) { |
| 437 |
EXPECT_EQ(rfd, SSL_get_fd(ssl)); |
| 438 |
EXPECT_EQ(rfd, SSL_get_rfd(ssl)); |
| 439 |
EXPECT_EQ(wfd, SSL_get_wfd(ssl)); |
| 440 |
|
| 441 |
// The wrapper BIOs are always equal when fds are equal, even if set |
| 442 |
// individually. |
| 443 |
if (rfd == wfd) { |
| 444 |
EXPECT_EQ(SSL_get_rbio(ssl), SSL_get_wbio(ssl)); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
TEST(SSLTest, SetFD) { |
| 449 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 450 |
ASSERT_TRUE(ctx); |
| 451 |
|
| 452 |
// Test setting different read and write FDs. |
| 453 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 454 |
ASSERT_TRUE(ssl); |
| 455 |
EXPECT_TRUE(SSL_set_rfd(ssl.get(), 1)); |
| 456 |
EXPECT_TRUE(SSL_set_wfd(ssl.get(), 2)); |
| 457 |
ExpectFDs(ssl.get(), 1, 2); |
| 458 |
|
| 459 |
// Test setting the same FD. |
| 460 |
ssl.reset(SSL_new(ctx.get())); |
| 461 |
ASSERT_TRUE(ssl); |
| 462 |
EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 463 |
ExpectFDs(ssl.get(), 1, 1); |
| 464 |
|
| 465 |
// Test setting the same FD one side at a time. |
| 466 |
ssl.reset(SSL_new(ctx.get())); |
| 467 |
ASSERT_TRUE(ssl); |
| 468 |
EXPECT_TRUE(SSL_set_rfd(ssl.get(), 1)); |
| 469 |
EXPECT_TRUE(SSL_set_wfd(ssl.get(), 1)); |
| 470 |
ExpectFDs(ssl.get(), 1, 1); |
| 471 |
|
| 472 |
// Test setting the same FD in the other order. |
| 473 |
ssl.reset(SSL_new(ctx.get())); |
| 474 |
ASSERT_TRUE(ssl); |
| 475 |
EXPECT_TRUE(SSL_set_wfd(ssl.get(), 1)); |
| 476 |
EXPECT_TRUE(SSL_set_rfd(ssl.get(), 1)); |
| 477 |
ExpectFDs(ssl.get(), 1, 1); |
| 478 |
|
| 479 |
// Test changing the read FD partway through. |
| 480 |
ssl.reset(SSL_new(ctx.get())); |
| 481 |
ASSERT_TRUE(ssl); |
| 482 |
EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 483 |
EXPECT_TRUE(SSL_set_rfd(ssl.get(), 2)); |
| 484 |
ExpectFDs(ssl.get(), 2, 1); |
| 485 |
|
| 486 |
// Test changing the write FD partway through. |
| 487 |
ssl.reset(SSL_new(ctx.get())); |
| 488 |
ASSERT_TRUE(ssl); |
| 489 |
EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 490 |
EXPECT_TRUE(SSL_set_wfd(ssl.get(), 2)); |
| 491 |
ExpectFDs(ssl.get(), 1, 2); |
| 492 |
|
| 493 |
// Test a no-op change to the read FD partway through. |
| 494 |
ssl.reset(SSL_new(ctx.get())); |
| 495 |
ASSERT_TRUE(ssl); |
| 496 |
EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 497 |
EXPECT_TRUE(SSL_set_rfd(ssl.get(), 1)); |
| 498 |
ExpectFDs(ssl.get(), 1, 1); |
| 499 |
|
| 500 |
// Test a no-op change to the write FD partway through. |
| 501 |
ssl.reset(SSL_new(ctx.get())); |
| 502 |
ASSERT_TRUE(ssl); |
| 503 |
EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 504 |
EXPECT_TRUE(SSL_set_wfd(ssl.get(), 1)); |
| 505 |
ExpectFDs(ssl.get(), 1, 1); |
| 506 |
|
| 507 |
// ASan builds will implicitly test that the internal |BIO| reference-counting |
| 508 |
// is correct. |
| 509 |
} |
| 510 |
#endif // !OPENSSL_NO_SOCK |
| 511 |
|
| 512 |
TEST(SSLTest, SetBIO) { |
| 513 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 514 |
ASSERT_TRUE(ctx); |
| 515 |
|
| 516 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 517 |
bssl::UniquePtr<BIO> bio1(BIO_new(BIO_s_mem())), bio2(BIO_new(BIO_s_mem())), |
| 518 |
bio3(BIO_new(BIO_s_mem())); |
| 519 |
ASSERT_TRUE(ssl); |
| 520 |
ASSERT_TRUE(bio1); |
| 521 |
ASSERT_TRUE(bio2); |
| 522 |
ASSERT_TRUE(bio3); |
| 523 |
|
| 524 |
// SSL_set_bio takes one reference when the parameters are the same. |
| 525 |
BIO_up_ref(bio1.get()); |
| 526 |
SSL_set_bio(ssl.get(), bio1.get(), bio1.get()); |
| 527 |
|
| 528 |
// Repeating the call does nothing. |
| 529 |
SSL_set_bio(ssl.get(), bio1.get(), bio1.get()); |
| 530 |
|
| 531 |
// It takes one reference each when the parameters are different. |
| 532 |
BIO_up_ref(bio2.get()); |
| 533 |
BIO_up_ref(bio3.get()); |
| 534 |
SSL_set_bio(ssl.get(), bio2.get(), bio3.get()); |
| 535 |
|
| 536 |
// Repeating the call does nothing. |
| 537 |
SSL_set_bio(ssl.get(), bio2.get(), bio3.get()); |
| 538 |
|
| 539 |
// It takes one reference when changing only wbio. |
| 540 |
BIO_up_ref(bio1.get()); |
| 541 |
SSL_set_bio(ssl.get(), bio2.get(), bio1.get()); |
| 542 |
|
| 543 |
// It takes one reference when changing only rbio and the two are different. |
| 544 |
BIO_up_ref(bio3.get()); |
| 545 |
SSL_set_bio(ssl.get(), bio3.get(), bio1.get()); |
| 546 |
|
| 547 |
// If setting wbio to rbio, it takes no additional references. |
| 548 |
SSL_set_bio(ssl.get(), bio3.get(), bio3.get()); |
| 549 |
|
| 550 |
// From there, wbio may be switched to something else. |
| 551 |
BIO_up_ref(bio1.get()); |
| 552 |
SSL_set_bio(ssl.get(), bio3.get(), bio1.get()); |
| 553 |
|
| 554 |
// If setting rbio to wbio, it takes no additional references. |
| 555 |
SSL_set_bio(ssl.get(), bio1.get(), bio1.get()); |
| 556 |
|
| 557 |
// From there, rbio may be switched to something else, but, for historical |
| 558 |
// reasons, it takes a reference to both parameters. |
| 559 |
BIO_up_ref(bio1.get()); |
| 560 |
BIO_up_ref(bio2.get()); |
| 561 |
SSL_set_bio(ssl.get(), bio2.get(), bio1.get()); |
| 562 |
|
| 563 |
// ASAN builds will implicitly test that the internal |BIO| reference-counting |
| 564 |
// is correct. |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
// Tests that our ClientHellos do not change unexpectedly. These are purely |
| 570 |
// change detection tests. If they fail as part of an intentional ClientHello |
| 571 |
// change, update the test vector. |
| 572 |
TEST(SSLTest, ClientHello) { |
| 573 |
struct { |
| 574 |
uint16_t max_version; |
| 575 |
std::vector<uint8_t> expected; |
| 576 |
} kTests[] = { |
| 577 |
{TLS1_VERSION, |
| 578 |
{0x16, 0x03, 0x01, 0x00, 0x58, 0x01, 0x00, 0x00, 0x54, 0x03, 0x01, 0x00, |
| 579 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 580 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 581 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xc0, 0x09, |
| 582 |
0xc0, 0x13, 0xc0, 0x0a, 0xc0, 0x14, 0x00, 0x2f, 0x00, 0x35, 0x01, 0x00, |
| 583 |
0x00, 0x1f, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, |
| 584 |
0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, |
| 585 |
0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00}}, |
| 586 |
{TLS1_1_VERSION, |
| 587 |
{0x16, 0x03, 0x01, 0x00, 0x58, 0x01, 0x00, 0x00, 0x54, 0x03, 0x02, 0x00, |
| 588 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 589 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 590 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xc0, 0x09, |
| 591 |
0xc0, 0x13, 0xc0, 0x0a, 0xc0, 0x14, 0x00, 0x2f, 0x00, 0x35, 0x01, 0x00, |
| 592 |
0x00, 0x1f, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, |
| 593 |
0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, |
| 594 |
0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00}}, |
| 595 |
{TLS1_2_VERSION, |
| 596 |
{0x16, 0x03, 0x01, 0x00, 0x88, 0x01, 0x00, 0x00, 0x84, 0x03, 0x03, 0x00, |
| 597 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 598 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 599 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0xcc, 0xa9, |
| 600 |
0xcc, 0xa8, 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30, 0xc0, 0x09, |
| 601 |
0xc0, 0x13, 0xc0, 0x27, 0xc0, 0x0a, 0xc0, 0x14, 0xc0, 0x28, 0x00, 0x9c, |
| 602 |
0x00, 0x9d, 0x00, 0x2f, 0x00, 0x3c, 0x00, 0x35, 0x01, 0x00, 0x00, 0x39, |
| 603 |
0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, |
| 604 |
0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, 0x0b, 0x00, |
| 605 |
0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x16, 0x00, |
| 606 |
0x14, 0x04, 0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03, 0x08, 0x05, 0x05, |
| 607 |
0x01, 0x06, 0x03, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01}}, |
| 608 |
{TLS1_3_VERSION, |
| 609 |
{0x16, 0x03, 0x01, 0x05, 0xb5, 0x01, 0x00, 0x05, 0xb1, 0x03, 0x03, 0x00, |
| 610 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 611 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 612 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 613 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 614 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 615 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, |
| 616 |
0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30, |
| 617 |
0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x27, 0xc0, 0x0a, 0xc0, 0x14, 0xc0, 0x28, |
| 618 |
0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f, 0x00, 0x3c, 0x00, 0x35, 0x01, 0x00, |
| 619 |
0x05, 0x40, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, |
| 620 |
0x0a, 0x00, 0x0e, 0x00, 0x0c, 0x11, 0xec, 0x11, 0xeb, 0x11, 0xed, 0x00, |
| 621 |
0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, |
| 622 |
0x23, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x16, 0x00, 0x14, 0x04, 0x03, 0x08, |
| 623 |
0x04, 0x04, 0x01, 0x05, 0x03, 0x08, 0x05, 0x05, 0x01, 0x06, 0x03, 0x08, |
| 624 |
0x06, 0x06, 0x01, 0x02, 0x01, 0x00, 0x33, 0x04, 0xea, 0x04, 0xe8, 0x11, |
| 625 |
0xec, 0x04, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 626 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 627 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 628 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 629 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 630 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 631 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 632 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 633 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 634 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 635 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 636 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 637 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 638 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 639 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 640 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 641 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 642 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 643 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 644 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 645 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 646 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 647 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 648 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 649 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 650 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 651 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 652 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 653 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 654 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 655 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 656 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 657 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 658 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 659 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 660 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 661 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 662 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 663 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 664 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 665 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 666 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 667 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 668 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 669 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 670 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 671 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 672 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 673 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 674 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 675 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 676 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 677 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 678 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 679 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 680 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 681 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 682 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 683 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 684 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 685 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 686 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 687 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 688 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 689 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 690 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 691 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 692 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 693 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 694 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 695 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 696 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 697 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 698 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 699 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 700 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 701 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 702 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 703 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 704 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 705 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 706 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 707 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 708 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 709 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 710 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 711 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 712 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 713 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 714 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 715 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 716 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 717 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 718 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 719 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 720 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 721 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 722 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 723 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 724 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 725 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 726 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x20, 0x00, |
| 727 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 728 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 729 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x02, 0x01, |
| 730 |
0x01, 0x00, 0x2b, 0x00, 0x09, 0x08, 0x03, 0x04, 0x03, 0x03, 0x03, 0x02, |
| 731 |
0x03, 0x01}}, |
| 732 |
}; |
| 733 |
|
| 734 |
for (const auto &t : kTests) { |
| 735 |
SCOPED_TRACE(t.max_version); |
| 736 |
|
| 737 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 738 |
ASSERT_TRUE(ctx); |
| 739 |
// Our default cipher list varies by CPU capabilities, so manually place the |
| 740 |
// ChaCha20 ciphers in front. |
| 741 |
const char *cipher_list = "CHACHA20:ALL"; |
| 742 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), t.max_version)); |
| 743 |
ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(ctx.get(), cipher_list)); |
| 744 |
|
| 745 |
// Explicitly set TLS 1.3 ciphersuites so CPU capabilities don't affect |
| 746 |
// order |
| 747 |
ASSERT_TRUE( |
| 748 |
SSL_CTX_set_ciphersuites(ctx.get(), TLS13_DEFAULT_CIPHER_LIST_AES_HW)); |
| 749 |
|
| 750 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 751 |
ASSERT_TRUE(ssl); |
| 752 |
std::vector<uint8_t> client_hello; |
| 753 |
ASSERT_TRUE(GetClientHello(ssl.get(), &client_hello)); |
| 754 |
|
| 755 |
// Zero the client_random. |
| 756 |
constexpr size_t kRandomOffset = 1 + 2 + 2 + // record header |
| 757 |
1 + 3 + // handshake message header |
| 758 |
2; // client_version |
| 759 |
|
| 760 |
constexpr size_t kKeyShare1Offset = 195; |
| 761 |
constexpr size_t kKeyShare1Size = 32 + MLKEM768_PUBLIC_KEY_BYTES; |
| 762 |
constexpr size_t kKeyShare2Offset = kKeyShare1Offset + kKeyShare1Size |
| 763 |
+ 2 // KeyShare 2 IANA ID |
| 764 |
+ 2; // KeyShare 2 Length |
| 765 |
constexpr size_t kKeyShare2Size = 32; |
| 766 |
|
| 767 |
int pre = client_hello.size(); |
| 768 |
if (t.max_version == TLS1_3_VERSION) { |
| 769 |
ASSERT_GE(client_hello.size(), |
| 770 |
kRandomOffset + SSL3_RANDOM_SIZE + 1 + SSL3_SESSION_ID_SIZE); |
| 771 |
OPENSSL_memset(client_hello.data() + kRandomOffset, 0, |
| 772 |
SSL3_RANDOM_SIZE + 1 + SSL3_SESSION_ID_SIZE); |
| 773 |
// Jump to key share extension and zero out the key |
| 774 |
OPENSSL_memset(client_hello.data() + kKeyShare1Offset, 0, kKeyShare1Size); |
| 775 |
OPENSSL_memset(client_hello.data() + kKeyShare2Offset, 0, kKeyShare2Size); |
| 776 |
} else { |
| 777 |
ASSERT_GE(client_hello.size(), kRandomOffset + SSL3_RANDOM_SIZE); |
| 778 |
OPENSSL_memset(client_hello.data() + kRandomOffset, 0, SSL3_RANDOM_SIZE); |
| 779 |
} |
| 780 |
int post = client_hello.size(); |
| 781 |
ASSERT_EQ(pre, post); |
| 782 |
|
| 783 |
if (client_hello != t.expected) { |
| 784 |
ADD_FAILURE() << "ClientHellos did not match."; |
| 785 |
// Print the value manually so it is easier to update the test vector. |
| 786 |
for (size_t i = 0; i < client_hello.size(); i += 12) { |
| 787 |
printf(" %c", i == 0 ? '{' : ' '); |
| 788 |
for (size_t j = i; j < client_hello.size() && j < i + 12; j++) { |
| 789 |
if (j > i) { |
| 790 |
printf(" "); |
| 791 |
} |
| 792 |
printf("0x%02x", client_hello[j]); |
| 793 |
if (j < client_hello.size() - 1) { |
| 794 |
printf(","); |
| 795 |
} |
| 796 |
} |
| 797 |
if (i + 12 >= client_hello.size()) { |
| 798 |
printf("}},"); |
| 799 |
} |
| 800 |
printf("\n"); |
| 801 |
} |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
// Test that the early callback can swap the maximum version. |
| 809 |
TEST(SSLTest, EarlyCallbackVersionSwitch) { |
| 810 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 811 |
CreateContextWithTestCertificate(TLS_method()); |
| 812 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 813 |
ASSERT_TRUE(server_ctx); |
| 814 |
ASSERT_TRUE(client_ctx); |
| 815 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 816 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 817 |
|
| 818 |
SSL_CTX_set_select_certificate_cb( |
| 819 |
server_ctx.get(), |
| 820 |
[](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 821 |
if (!SSL_set_max_proto_version(client_hello->ssl, TLS1_2_VERSION)) { |
| 822 |
return ssl_select_cert_error; |
| 823 |
} |
| 824 |
|
| 825 |
return ssl_select_cert_success; |
| 826 |
}); |
| 827 |
|
| 828 |
bssl::UniquePtr<SSL> client, server; |
| 829 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 830 |
server_ctx.get())); |
| 831 |
EXPECT_EQ(TLS1_2_VERSION, SSL_version(client.get())); |
| 832 |
} |
| 833 |
|
| 834 |
TEST(SSLTest, SetVersion) { |
| 835 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 836 |
ASSERT_TRUE(ctx); |
| 837 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 838 |
ASSERT_TRUE(ssl); |
| 839 |
|
| 840 |
// Set valid TLS versions. |
| 841 |
for (const auto &vers : kAllVersions) { |
| 842 |
SCOPED_TRACE(vers.name); |
| 843 |
if (vers.ssl_method == VersionParam::is_tls) { |
| 844 |
EXPECT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), vers.version)); |
| 845 |
EXPECT_EQ(SSL_CTX_get_max_proto_version(ctx.get()), vers.version); |
| 846 |
EXPECT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), vers.version)); |
| 847 |
EXPECT_EQ(SSL_CTX_get_min_proto_version(ctx.get()), vers.version); |
| 848 |
EXPECT_TRUE(SSL_set_max_proto_version(ssl.get(), vers.version)); |
| 849 |
EXPECT_EQ(SSL_get_max_proto_version(ssl.get()), vers.version); |
| 850 |
EXPECT_TRUE(SSL_set_min_proto_version(ssl.get(), vers.version)); |
| 851 |
EXPECT_EQ(SSL_get_min_proto_version(ssl.get()), vers.version); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
// Invalid TLS versions are rejected. |
| 856 |
EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), DTLS1_VERSION)); |
| 857 |
EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0x0200)); |
| 858 |
EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0x1234)); |
| 859 |
EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), DTLS1_VERSION)); |
| 860 |
EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0x0200)); |
| 861 |
EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0x1234)); |
| 862 |
EXPECT_FALSE(SSL_set_max_proto_version(ssl.get(), 0x0200)); |
| 863 |
EXPECT_FALSE(SSL_set_max_proto_version(ssl.get(), 0x1234)); |
| 864 |
EXPECT_FALSE(SSL_set_min_proto_version(ssl.get(), DTLS1_VERSION)); |
| 865 |
EXPECT_FALSE(SSL_set_min_proto_version(ssl.get(), 0x0200)); |
| 866 |
EXPECT_FALSE(SSL_set_min_proto_version(ssl.get(), 0x1234)); |
| 867 |
|
| 868 |
// Zero represents the default version. |
| 869 |
EXPECT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), 0)); |
| 870 |
EXPECT_EQ(0, SSL_CTX_get_max_proto_version(ctx.get())); |
| 871 |
EXPECT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), 0)); |
| 872 |
EXPECT_EQ(0, SSL_CTX_get_min_proto_version(ctx.get())); |
| 873 |
EXPECT_TRUE(SSL_set_max_proto_version(ssl.get(), 0)); |
| 874 |
EXPECT_EQ(0, SSL_get_max_proto_version(ssl.get())); |
| 875 |
EXPECT_TRUE(SSL_set_min_proto_version(ssl.get(), 0)); |
| 876 |
EXPECT_EQ(0, SSL_get_min_proto_version(ssl.get())); |
| 877 |
|
| 878 |
// SSL 3.0 is not available. |
| 879 |
EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), SSL3_VERSION)); |
| 880 |
EXPECT_FALSE(SSL_set_min_proto_version(ssl.get(), SSL3_VERSION)); |
| 881 |
|
| 882 |
ctx.reset(SSL_CTX_new(DTLS_method())); |
| 883 |
ASSERT_TRUE(ctx); |
| 884 |
ssl.reset(SSL_new(ctx.get())); |
| 885 |
ASSERT_TRUE(ssl); |
| 886 |
|
| 887 |
// Set valid DTLS versions. |
| 888 |
for (const auto &vers : kAllVersions) { |
| 889 |
SCOPED_TRACE(vers.name); |
| 890 |
if (vers.ssl_method == VersionParam::is_dtls) { |
| 891 |
EXPECT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), vers.version)); |
| 892 |
EXPECT_EQ(SSL_CTX_get_max_proto_version(ctx.get()), vers.version); |
| 893 |
EXPECT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), vers.version)); |
| 894 |
EXPECT_EQ(SSL_CTX_get_min_proto_version(ctx.get()), vers.version); |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
// Invalid DTLS versions are rejected. |
| 899 |
EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_VERSION)); |
| 900 |
EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0xfefe /* DTLS 1.1 */)); |
| 901 |
EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0xfffe /* DTLS 0.1 */)); |
| 902 |
EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0x1234)); |
| 903 |
EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_VERSION)); |
| 904 |
EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0xfefe /* DTLS 1.1 */)); |
| 905 |
EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0xfffe /* DTLS 0.1 */)); |
| 906 |
EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0x1234)); |
| 907 |
|
| 908 |
// Zero represents the default version. |
| 909 |
EXPECT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), 0)); |
| 910 |
EXPECT_EQ(0, SSL_CTX_get_max_proto_version(ctx.get())); |
| 911 |
EXPECT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), 0)); |
| 912 |
EXPECT_EQ(0, SSL_CTX_get_min_proto_version(ctx.get())); |
| 913 |
} |
| 914 |
|
| 915 |
TEST(SSLTest, SetVerifyResult) { |
| 916 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 917 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 918 |
CreateContextWithTestCertificate(TLS_method()); |
| 919 |
ASSERT_TRUE(client_ctx); |
| 920 |
ASSERT_TRUE(server_ctx); |
| 921 |
|
| 922 |
bssl::UniquePtr<SSL> client, server; |
| 923 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 924 |
server_ctx.get())); |
| 925 |
|
| 926 |
SSL_SESSION *client_session = SSL_get_session(client.get()); |
| 927 |
SSL_SESSION *server_session = SSL_get_session(server.get()); |
| 928 |
|
| 929 |
// SSL and SSL_SESSION should share the same verify_result after a handshake |
| 930 |
EXPECT_EQ(SSL_get_verify_result(client.get()), client_session->verify_result); |
| 931 |
EXPECT_EQ(SSL_get_verify_result(server.get()), server_session->verify_result); |
| 932 |
|
| 933 |
// Use custom verification result |
| 934 |
SSL_set_verify_result(client.get(), X509_V_ERR_CERT_REVOKED); |
| 935 |
EXPECT_EQ(X509_V_ERR_CERT_REVOKED, SSL_get_verify_result(client.get())); |
| 936 |
EXPECT_NE(SSL_get_verify_result(client.get()), client_session->verify_result); |
| 937 |
} |
| 938 |
|
| 939 |
TEST(SSLTest, BuildCertChain) { |
| 940 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 941 |
|
| 942 |
// No certificate set, so this should fail. |
| 943 |
EXPECT_FALSE(SSL_CTX_build_cert_chain(ctx.get(), 0)); |
| 944 |
EXPECT_TRUE(ExpectSingleError(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET)); |
| 945 |
|
| 946 |
ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), GetLeafPublic().get())); |
| 947 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), GetLeafKey().get())); |
| 948 |
|
| 949 |
// Verification will fail because there is no valid root cert available. |
| 950 |
EXPECT_FALSE(SSL_CTX_build_cert_chain(ctx.get(), 0)); |
| 951 |
ERR_clear_error(); |
| 952 |
|
| 953 |
// Should return 2 when |SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR| is set. |
| 954 |
EXPECT_EQ( |
| 955 |
SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR), |
| 956 |
2); |
| 957 |
EXPECT_TRUE(ExpectSingleError(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED)); |
| 958 |
ERR_clear_error(); |
| 959 |
|
| 960 |
// Should return 2, but with no error on the stack when |
| 961 |
// |SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR| and |SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR| |
| 962 |
// are set. |
| 963 |
EXPECT_EQ( |
| 964 |
SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR | |
| 965 |
SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR), |
| 966 |
2); |
| 967 |
EXPECT_FALSE(ERR_get_error()); |
| 968 |
|
| 969 |
// Pass in the trust store. |SSL_CTX_build_cert_chain| should succeed now. |
| 970 |
ASSERT_TRUE(X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx.get()), |
| 971 |
GetLeafRoot().get())); |
| 972 |
X509_VERIFY_PARAM_set_flags(SSL_CTX_get0_param(ctx.get()), |
| 973 |
X509_V_FLAG_NO_CHECK_TIME); |
| 974 |
EXPECT_EQ(SSL_CTX_build_cert_chain(ctx.get(), 0), 1); |
| 975 |
STACK_OF(X509) *chain = nullptr; |
| 976 |
ASSERT_TRUE(SSL_CTX_get0_chain_certs(ctx.get(), &chain)); |
| 977 |
EXPECT_TRUE(ChainsEqual(chain, {GetLeafRoot().get()})); |
| 978 |
|
| 979 |
// Root cert is self-signed, so |SSL_BUILD_CHAIN_FLAG_UNTRUSTED| will |
| 980 |
// still pass. |
| 981 |
ASSERT_TRUE(SSL_CTX_clear_chain_certs(ctx.get())); |
| 982 |
EXPECT_TRUE( |
| 983 |
SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_UNTRUSTED)); |
| 984 |
ASSERT_TRUE(SSL_CTX_get0_chain_certs(ctx.get(), &chain)); |
| 985 |
EXPECT_TRUE(ChainsEqual(chain, {GetLeafRoot().get()})); |
| 986 |
|
| 987 |
// |SSL_BUILD_CHAIN_FLAG_CHECK| uses the already built cert chain as the trust |
| 988 |
// store and verifies against it. If we clear the cert chain, there should be |
| 989 |
// no trust store to compare against if |SSL_BUILD_CHAIN_FLAG_CHECK| is still |
| 990 |
// set. |
| 991 |
EXPECT_EQ(SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_CHECK), 1); |
| 992 |
ASSERT_TRUE(SSL_CTX_clear_chain_certs(ctx.get())); |
| 993 |
EXPECT_FALSE(SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_CHECK)); |
| 994 |
EXPECT_TRUE(ExpectSingleError(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED)); |
| 995 |
|
| 996 |
// |SSL_BUILD_CHAIN_FLAG_CHECK| and |SSL_BUILD_CHAIN_FLAG_UNTRUSTED| are |
| 997 |
// mutually exclusive, with |SSL_BUILD_CHAIN_FLAG_CHECK| taking priority. |
| 998 |
// The result with both set should be the same as only |
| 999 |
// |SSL_BUILD_CHAIN_FLAG_CHECK| being set. |
| 1000 |
ASSERT_TRUE(SSL_CTX_clear_chain_certs(ctx.get())); |
| 1001 |
EXPECT_FALSE(SSL_CTX_build_cert_chain( |
| 1002 |
ctx.get(), SSL_BUILD_CHAIN_FLAG_CHECK | SSL_BUILD_CHAIN_FLAG_UNTRUSTED)); |
| 1003 |
EXPECT_FALSE(SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_CHECK)); |
| 1004 |
// First call with |SSL_BUILD_CHAIN_FLAG_CHECK| existing will fail, second |
| 1005 |
// call with |SSL_BUILD_CHAIN_FLAG_UNTRUSTED| will succeed. |
| 1006 |
EXPECT_FALSE(SSL_CTX_build_cert_chain( |
| 1007 |
ctx.get(), SSL_BUILD_CHAIN_FLAG_CHECK | SSL_BUILD_CHAIN_FLAG_UNTRUSTED)); |
| 1008 |
EXPECT_EQ(SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_UNTRUSTED), |
| 1009 |
1); |
| 1010 |
// |SSL_BUILD_CHAIN_FLAG_CHECK| will succeed since we have a built chain now. |
| 1011 |
EXPECT_EQ(SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_CHECK), 1); |
| 1012 |
|
| 1013 |
// Test that successful verification with |SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR| |
| 1014 |
// does not return 2. |
| 1015 |
ASSERT_TRUE(SSL_CTX_clear_chain_certs(ctx.get())); |
| 1016 |
EXPECT_EQ( |
| 1017 |
SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR), |
| 1018 |
1); |
| 1019 |
|
| 1020 |
// Test that successful verification with |SSL_BUILD_CHAIN_FLAG_NO_ROOT| |
| 1021 |
// does include the root cert. |
| 1022 |
ASSERT_TRUE(SSL_CTX_clear_chain_certs(ctx.get())); |
| 1023 |
EXPECT_EQ(SSL_CTX_build_cert_chain(ctx.get(), SSL_BUILD_CHAIN_FLAG_NO_ROOT), |
| 1024 |
1); |
| 1025 |
ASSERT_TRUE(SSL_CTX_get0_chain_certs(ctx.get(), &chain)); |
| 1026 |
EXPECT_TRUE(ChainsEqual(chain, {})); |
| 1027 |
} |
| 1028 |
|
| 1029 |
TEST(SSLTest, AddChainCertHack) { |
| 1030 |
// Ensure that we don't accidently break the hack that we have in place to |
| 1031 |
// keep curl and serf happy when they use an |X509| even after transfering |
| 1032 |
// ownership. |
| 1033 |
|
| 1034 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1035 |
ASSERT_TRUE(ctx); |
| 1036 |
X509 *cert = GetTestCertificate().release(); |
| 1037 |
ASSERT_TRUE(cert); |
| 1038 |
SSL_CTX_add0_chain_cert(ctx.get(), cert); |
| 1039 |
|
| 1040 |
// This should not trigger a use-after-free. |
| 1041 |
X509_cmp(cert, cert); |
| 1042 |
} |
| 1043 |
|
| 1044 |
TEST(SSLTest, GetCertificate) { |
| 1045 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1046 |
ASSERT_TRUE(ctx); |
| 1047 |
bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 1048 |
ASSERT_TRUE(cert); |
| 1049 |
ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 1050 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 1051 |
ASSERT_TRUE(ssl); |
| 1052 |
|
| 1053 |
X509 *cert2 = SSL_CTX_get0_certificate(ctx.get()); |
| 1054 |
ASSERT_TRUE(cert2); |
| 1055 |
|
| 1056 |
X509 *cert3 = SSL_get_certificate(ssl.get()); |
| 1057 |
ASSERT_TRUE(cert3); |
| 1058 |
|
| 1059 |
// The old and new certificates must be identical. |
| 1060 |
EXPECT_EQ(0, X509_cmp(cert.get(), cert2)); |
| 1061 |
EXPECT_EQ(0, X509_cmp(cert.get(), cert3)); |
| 1062 |
|
| 1063 |
uint8_t *der = nullptr; |
| 1064 |
long der_len = i2d_X509(cert.get(), &der); |
| 1065 |
ASSERT_LT(0, der_len); |
| 1066 |
bssl::UniquePtr<uint8_t> free_der(der); |
| 1067 |
|
| 1068 |
uint8_t *der2 = nullptr; |
| 1069 |
long der2_len = i2d_X509(cert2, &der2); |
| 1070 |
ASSERT_LT(0, der2_len); |
| 1071 |
bssl::UniquePtr<uint8_t> free_der2(der2); |
| 1072 |
|
| 1073 |
uint8_t *der3 = nullptr; |
| 1074 |
long der3_len = i2d_X509(cert3, &der3); |
| 1075 |
ASSERT_LT(0, der3_len); |
| 1076 |
bssl::UniquePtr<uint8_t> free_der3(der3); |
| 1077 |
|
| 1078 |
// They must also encode identically. |
| 1079 |
EXPECT_EQ(Bytes(der, der_len), Bytes(der2, der2_len)); |
| 1080 |
EXPECT_EQ(Bytes(der, der_len), Bytes(der3, der3_len)); |
| 1081 |
} |
| 1082 |
|
| 1083 |
TEST(SSLTest, GetCertificateExData) { |
| 1084 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1085 |
ASSERT_TRUE(ctx); |
| 1086 |
bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 1087 |
ASSERT_TRUE(cert); |
| 1088 |
|
| 1089 |
int ex_data_index = |
| 1090 |
X509_get_ex_new_index(0, nullptr, nullptr, nullptr, nullptr); |
| 1091 |
const char ex_data[] = "AWS-LC external data"; |
| 1092 |
ASSERT_TRUE(X509_set_ex_data(cert.get(), ex_data_index, (void *)ex_data)); |
| 1093 |
ASSERT_TRUE(X509_get_ex_data(cert.get(), ex_data_index)); |
| 1094 |
|
| 1095 |
ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 1096 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 1097 |
ASSERT_TRUE(ssl); |
| 1098 |
|
| 1099 |
X509 *cert2 = SSL_CTX_get0_certificate(ctx.get()); |
| 1100 |
ASSERT_TRUE(cert2); |
| 1101 |
const char *ex_data2 = (const char *)X509_get_ex_data(cert2, ex_data_index); |
| 1102 |
EXPECT_TRUE(ex_data2); |
| 1103 |
|
| 1104 |
X509 *cert3 = SSL_get_certificate(ssl.get()); |
| 1105 |
ASSERT_TRUE(cert3); |
| 1106 |
const char *ex_data3 = (const char *)X509_get_ex_data(cert3, ex_data_index); |
| 1107 |
EXPECT_TRUE(ex_data3); |
| 1108 |
|
| 1109 |
// The external data extracted must be identical. |
| 1110 |
EXPECT_EQ(ex_data2, ex_data); |
| 1111 |
EXPECT_EQ(ex_data3, ex_data); |
| 1112 |
} |
| 1113 |
|
| 1114 |
TEST(SSLTest, GetCertificateASN1) { |
| 1115 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1116 |
ASSERT_TRUE(ctx); |
| 1117 |
bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 1118 |
ASSERT_TRUE(cert); |
| 1119 |
|
| 1120 |
// Convert cert to ASN1 to pass in. |
| 1121 |
uint8_t *der = nullptr; |
| 1122 |
size_t der_len = i2d_X509(cert.get(), &der); |
| 1123 |
bssl::UniquePtr<uint8_t> free_der(der); |
| 1124 |
|
| 1125 |
ASSERT_TRUE(SSL_CTX_use_certificate_ASN1(ctx.get(), der_len, der)); |
| 1126 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 1127 |
ASSERT_TRUE(ssl); |
| 1128 |
|
| 1129 |
X509 *cert2 = SSL_CTX_get0_certificate(ctx.get()); |
| 1130 |
ASSERT_TRUE(cert2); |
| 1131 |
|
| 1132 |
X509 *cert3 = SSL_get_certificate(ssl.get()); |
| 1133 |
ASSERT_TRUE(cert3); |
| 1134 |
|
| 1135 |
// The old and new certificates must be identical. |
| 1136 |
EXPECT_EQ(0, X509_cmp(cert.get(), cert2)); |
| 1137 |
EXPECT_EQ(0, X509_cmp(cert.get(), cert3)); |
| 1138 |
|
| 1139 |
uint8_t *der2 = nullptr; |
| 1140 |
long der2_len = i2d_X509(cert2, &der2); |
| 1141 |
ASSERT_LT(0, der2_len); |
| 1142 |
bssl::UniquePtr<uint8_t> free_der2(der2); |
| 1143 |
|
| 1144 |
uint8_t *der3 = nullptr; |
| 1145 |
long der3_len = i2d_X509(cert3, &der3); |
| 1146 |
ASSERT_LT(0, der3_len); |
| 1147 |
bssl::UniquePtr<uint8_t> free_der3(der3); |
| 1148 |
|
| 1149 |
// They must also encode identically. |
| 1150 |
EXPECT_EQ(Bytes(der, der_len), Bytes(der2, der2_len)); |
| 1151 |
EXPECT_EQ(Bytes(der, der_len), Bytes(der3, der3_len)); |
| 1152 |
} |
| 1153 |
|
| 1154 |
TEST(SSLTest, SetChainAndKeyMismatch) { |
| 1155 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 1156 |
ASSERT_TRUE(ctx); |
| 1157 |
|
| 1158 |
bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 1159 |
ASSERT_TRUE(key); |
| 1160 |
bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 1161 |
ASSERT_TRUE(leaf); |
| 1162 |
std::vector<CRYPTO_BUFFER *> chain = { |
| 1163 |
leaf.get(), |
| 1164 |
}; |
| 1165 |
|
| 1166 |
// Should fail because |GetTestKey| doesn't match the chain-test certificate. |
| 1167 |
ASSERT_FALSE(SSL_CTX_set_chain_and_key(ctx.get(), &chain[0], chain.size(), |
| 1168 |
key.get(), nullptr)); |
| 1169 |
ERR_clear_error(); |
| 1170 |
|
| 1171 |
// Ensure |SSL_CTX_use_cert_and_key| also fails |
| 1172 |
bssl::UniquePtr<X509> x509_leaf = |
| 1173 |
X509FromBuffer(GetChainTestCertificateBuffer()); |
| 1174 |
ASSERT_FALSE( |
| 1175 |
SSL_CTX_use_cert_and_key(ctx.get(), x509_leaf.get(), key.get(), NULL, 1)); |
| 1176 |
ERR_clear_error(); |
| 1177 |
} |
| 1178 |
|
| 1179 |
TEST(SSLTest, SetChainAndKey) { |
| 1180 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 1181 |
ASSERT_TRUE(client_ctx); |
| 1182 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 1183 |
ASSERT_TRUE(server_ctx); |
| 1184 |
|
| 1185 |
ASSERT_EQ(nullptr, SSL_CTX_get0_chain(server_ctx.get())); |
| 1186 |
|
| 1187 |
bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 1188 |
ASSERT_TRUE(key); |
| 1189 |
bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 1190 |
ASSERT_TRUE(leaf); |
| 1191 |
bssl::UniquePtr<CRYPTO_BUFFER> intermediate = |
| 1192 |
GetChainTestIntermediateBuffer(); |
| 1193 |
ASSERT_TRUE(intermediate); |
| 1194 |
std::vector<CRYPTO_BUFFER *> chain = { |
| 1195 |
leaf.get(), |
| 1196 |
intermediate.get(), |
| 1197 |
}; |
| 1198 |
ASSERT_TRUE(SSL_CTX_set_chain_and_key(server_ctx.get(), &chain[0], |
| 1199 |
chain.size(), key.get(), nullptr)); |
| 1200 |
|
| 1201 |
ASSERT_EQ(chain.size(), |
| 1202 |
sk_CRYPTO_BUFFER_num(SSL_CTX_get0_chain(server_ctx.get()))); |
| 1203 |
|
| 1204 |
SSL_CTX_set_custom_verify( |
| 1205 |
client_ctx.get(), SSL_VERIFY_PEER, |
| 1206 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1207 |
return ssl_verify_ok; |
| 1208 |
}); |
| 1209 |
|
| 1210 |
bssl::UniquePtr<SSL> client, server; |
| 1211 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1212 |
server_ctx.get())); |
| 1213 |
} |
| 1214 |
|
| 1215 |
// Verify that SSL_CTX_set_chain_and_key invalidates the X509 leaf and chain |
| 1216 |
// caches so that SSL_CTX_get0_certificate and SSL_CTX_get0_chain_certs reflect |
| 1217 |
// the newly-configured certificate chain rather than stale cached values. |
| 1218 |
TEST(SSLTest, SetChainAndKeyCacheInvalidation) { |
| 1219 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1220 |
ASSERT_TRUE(ctx); |
| 1221 |
|
| 1222 |
// Configure an RSA leaf + intermediate and warm up both the x509_leaf and |
| 1223 |
// x509_chain caches. |
| 1224 |
bssl::UniquePtr<EVP_PKEY> key1 = GetChainTestKey(); |
| 1225 |
ASSERT_TRUE(key1); |
| 1226 |
bssl::UniquePtr<CRYPTO_BUFFER> leaf1 = GetChainTestCertificateBuffer(); |
| 1227 |
ASSERT_TRUE(leaf1); |
| 1228 |
bssl::UniquePtr<CRYPTO_BUFFER> intermediate1 = GetChainTestIntermediateBuffer(); |
| 1229 |
ASSERT_TRUE(intermediate1); |
| 1230 |
{ |
| 1231 |
std::vector<CRYPTO_BUFFER *> chain = {leaf1.get(), intermediate1.get()}; |
| 1232 |
ASSERT_TRUE(SSL_CTX_set_chain_and_key(ctx.get(), chain.data(), |
| 1233 |
chain.size(), key1.get(), nullptr)); |
| 1234 |
} |
| 1235 |
|
| 1236 |
bssl::UniquePtr<X509> cert1 = GetChainTestCertificate(); |
| 1237 |
ASSERT_TRUE(cert1); |
| 1238 |
bssl::UniquePtr<X509> intermediate_x509_1 = GetChainTestIntermediate(); |
| 1239 |
ASSERT_TRUE(intermediate_x509_1); |
| 1240 |
|
| 1241 |
// Calling SSL_CTX_get0_certificate populates the internal X509 leaf cache. |
| 1242 |
ASSERT_EQ(0, X509_cmp(SSL_CTX_get0_certificate(ctx.get()), cert1.get())); |
| 1243 |
|
| 1244 |
// Calling SSL_CTX_get0_chain_certs populates the internal X509 chain cache. |
| 1245 |
STACK_OF(X509) *chain_out = nullptr; |
| 1246 |
ASSERT_TRUE(SSL_CTX_get0_chain_certs(ctx.get(), &chain_out)); |
| 1247 |
ASSERT_EQ(1u, sk_X509_num(chain_out)); |
| 1248 |
ASSERT_EQ(0, X509_cmp(sk_X509_value(chain_out, 0), intermediate_x509_1.get())); |
| 1249 |
|
| 1250 |
// Replace with a different RSA certificate and key pair. The new chain has no |
| 1251 |
// intermediate, so the x509_chain cache (if not invalidated) would still hold |
| 1252 |
// the old intermediate. |
| 1253 |
bssl::UniquePtr<X509> cert2 = GetTestCertificate(); |
| 1254 |
ASSERT_TRUE(cert2); |
| 1255 |
uint8_t *der = nullptr; |
| 1256 |
size_t der_len = i2d_X509(cert2.get(), &der); |
| 1257 |
ASSERT_GT(der_len, 0u); |
| 1258 |
bssl::UniquePtr<uint8_t> free_der(der); |
| 1259 |
bssl::UniquePtr<CRYPTO_BUFFER> leaf2( |
| 1260 |
CRYPTO_BUFFER_new(der, der_len, nullptr)); |
| 1261 |
ASSERT_TRUE(leaf2); |
| 1262 |
bssl::UniquePtr<EVP_PKEY> key2 = GetTestKey(); |
| 1263 |
ASSERT_TRUE(key2); |
| 1264 |
{ |
| 1265 |
std::vector<CRYPTO_BUFFER *> chain = {leaf2.get()}; |
| 1266 |
ASSERT_TRUE(SSL_CTX_set_chain_and_key(ctx.get(), chain.data(), |
| 1267 |
chain.size(), key2.get(), nullptr)); |
| 1268 |
} |
| 1269 |
|
| 1270 |
// The x509_leaf cache must reflect the new leaf certificate. |
| 1271 |
X509 *got = SSL_CTX_get0_certificate(ctx.get()); |
| 1272 |
ASSERT_TRUE(got); |
| 1273 |
EXPECT_EQ(0, X509_cmp(got, cert2.get())); |
| 1274 |
EXPECT_NE(0, X509_cmp(got, cert1.get())); |
| 1275 |
|
| 1276 |
// The x509_chain cache must be invalidated. The new chain has no |
| 1277 |
// intermediates, so SSL_CTX_get0_chain_certs must return null rather than |
| 1278 |
// the previously-cached intermediate. |
| 1279 |
chain_out = nullptr; |
| 1280 |
ASSERT_TRUE(SSL_CTX_get0_chain_certs(ctx.get(), &chain_out)); |
| 1281 |
EXPECT_EQ(nullptr, chain_out); |
| 1282 |
} |
| 1283 |
|
| 1284 |
// Verify that SSL_CTX_use_certificate_ASN1 (which routes through ssl_set_cert) |
| 1285 |
// invalidates the X509 leaf cache so that SSL_CTX_get0_certificate reflects the |
| 1286 |
// newly-configured certificate rather than a stale cached value. |
| 1287 |
TEST(SSLTest, UseCertificateASN1CacheInvalidation) { |
| 1288 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1289 |
ASSERT_TRUE(ctx); |
| 1290 |
|
| 1291 |
// Configure an initial RSA certificate and warm up the X509 leaf cache. |
| 1292 |
bssl::UniquePtr<X509> cert1 = GetTestCertificate(); |
| 1293 |
ASSERT_TRUE(cert1); |
| 1294 |
uint8_t *der1 = nullptr; |
| 1295 |
size_t der1_len = i2d_X509(cert1.get(), &der1); |
| 1296 |
ASSERT_GT(der1_len, 0u); |
| 1297 |
bssl::UniquePtr<uint8_t> free_der1(der1); |
| 1298 |
ASSERT_TRUE(SSL_CTX_use_certificate_ASN1(ctx.get(), der1_len, der1)); |
| 1299 |
|
| 1300 |
// Calling SSL_CTX_get0_certificate populates the internal X509 leaf cache. |
| 1301 |
ASSERT_EQ(0, X509_cmp(SSL_CTX_get0_certificate(ctx.get()), cert1.get())); |
| 1302 |
|
| 1303 |
// Replace with a different RSA certificate via the same API path. |
| 1304 |
bssl::UniquePtr<X509> cert2 = GetChainTestCertificate(); |
| 1305 |
ASSERT_TRUE(cert2); |
| 1306 |
uint8_t *der2 = nullptr; |
| 1307 |
size_t der2_len = i2d_X509(cert2.get(), &der2); |
| 1308 |
ASSERT_GT(der2_len, 0u); |
| 1309 |
bssl::UniquePtr<uint8_t> free_der2(der2); |
| 1310 |
ASSERT_TRUE(SSL_CTX_use_certificate_ASN1(ctx.get(), der2_len, der2)); |
| 1311 |
|
| 1312 |
// The stale cache must have been invalidated. SSL_CTX_get0_certificate must |
| 1313 |
// return the newly-configured certificate, not the previously-cached one. |
| 1314 |
X509 *got = SSL_CTX_get0_certificate(ctx.get()); |
| 1315 |
ASSERT_TRUE(got); |
| 1316 |
EXPECT_EQ(0, X509_cmp(got, cert2.get())); |
| 1317 |
EXPECT_NE(0, X509_cmp(got, cert1.get())); |
| 1318 |
} |
| 1319 |
|
| 1320 |
TEST(SSLTest, SetLeafChainAndKey) { |
| 1321 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1322 |
ASSERT_TRUE(client_ctx); |
| 1323 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1324 |
ASSERT_TRUE(server_ctx); |
| 1325 |
|
| 1326 |
ASSERT_EQ(nullptr, SSL_CTX_get0_chain(server_ctx.get())); |
| 1327 |
|
| 1328 |
bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 1329 |
ASSERT_TRUE(key); |
| 1330 |
bssl::UniquePtr<X509> leaf = GetChainTestCertificate(); |
| 1331 |
ASSERT_TRUE(leaf); |
| 1332 |
bssl::UniquePtr<X509> intermediate = GetChainTestIntermediate(); |
| 1333 |
bssl::UniquePtr<STACK_OF(X509)> chain(sk_X509_new_null()); |
| 1334 |
ASSERT_TRUE(chain); |
| 1335 |
ASSERT_TRUE(PushToStack(chain.get(), std::move(intermediate))); |
| 1336 |
|
| 1337 |
ASSERT_TRUE(SSL_CTX_use_cert_and_key(server_ctx.get(), leaf.get(), key.get(), |
| 1338 |
chain.get(), 1)); |
| 1339 |
|
| 1340 |
SSL_CTX_set_custom_verify( |
| 1341 |
client_ctx.get(), SSL_VERIFY_PEER, |
| 1342 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1343 |
return ssl_verify_ok; |
| 1344 |
}); |
| 1345 |
|
| 1346 |
bssl::UniquePtr<SSL> client, server; |
| 1347 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1348 |
server_ctx.get())); |
| 1349 |
|
| 1350 |
// Try setting on previously populated fields without an override |
| 1351 |
ASSERT_FALSE(SSL_CTX_use_cert_and_key(server_ctx.get(), leaf.get(), key.get(), |
| 1352 |
chain.get(), 0)); |
| 1353 |
ERR_clear_error(); |
| 1354 |
} |
| 1355 |
|
| 1356 |
TEST(SSLTest, SSLUseCertAndKey) { |
| 1357 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1358 |
ASSERT_TRUE(client_ctx); |
| 1359 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1360 |
ASSERT_TRUE(server_ctx); |
| 1361 |
|
| 1362 |
bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 1363 |
ASSERT_TRUE(key); |
| 1364 |
bssl::UniquePtr<X509> leaf = GetChainTestCertificate(); |
| 1365 |
ASSERT_TRUE(leaf); |
| 1366 |
bssl::UniquePtr<X509> intermediate = GetChainTestIntermediate(); |
| 1367 |
bssl::UniquePtr<STACK_OF(X509)> chain(sk_X509_new_null()); |
| 1368 |
ASSERT_TRUE(chain); |
| 1369 |
ASSERT_TRUE(PushToStack(chain.get(), std::move(intermediate))); |
| 1370 |
|
| 1371 |
bssl::UniquePtr<SSL> server(SSL_new(server_ctx.get())); |
| 1372 |
ASSERT_TRUE(server); |
| 1373 |
|
| 1374 |
// Setting cert and key on the SSL object should succeed. |
| 1375 |
ASSERT_TRUE(SSL_use_cert_and_key(server.get(), leaf.get(), key.get(), |
| 1376 |
chain.get(), 1)); |
| 1377 |
|
| 1378 |
// Without override, setting again should fail. |
| 1379 |
ASSERT_FALSE(SSL_use_cert_and_key(server.get(), leaf.get(), key.get(), |
| 1380 |
chain.get(), 0)); |
| 1381 |
ERR_clear_error(); |
| 1382 |
|
| 1383 |
SSL_CTX_set_custom_verify( |
| 1384 |
client_ctx.get(), SSL_VERIFY_PEER, |
| 1385 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1386 |
return ssl_verify_ok; |
| 1387 |
}); |
| 1388 |
|
| 1389 |
bssl::UniquePtr<SSL> client; |
| 1390 |
// Reset server SSL for the connection test using CreateClientAndServer. |
| 1391 |
server.reset(); |
| 1392 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 1393 |
server_ctx.get())); |
| 1394 |
ASSERT_TRUE(SSL_use_cert_and_key(server.get(), leaf.get(), key.get(), |
| 1395 |
chain.get(), 1)); |
| 1396 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 1397 |
} |
| 1398 |
|
| 1399 |
TEST(SSLTest, BuffersFailWithoutCustomVerify) { |
| 1400 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 1401 |
ASSERT_TRUE(client_ctx); |
| 1402 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 1403 |
ASSERT_TRUE(server_ctx); |
| 1404 |
|
| 1405 |
bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 1406 |
ASSERT_TRUE(key); |
| 1407 |
bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 1408 |
ASSERT_TRUE(leaf); |
| 1409 |
std::vector<CRYPTO_BUFFER *> chain = {leaf.get()}; |
| 1410 |
ASSERT_TRUE(SSL_CTX_set_chain_and_key(server_ctx.get(), &chain[0], |
| 1411 |
chain.size(), key.get(), nullptr)); |
| 1412 |
|
| 1413 |
// Without SSL_CTX_set_custom_verify(), i.e. with everything in the default |
| 1414 |
// configuration, certificate verification should fail. |
| 1415 |
bssl::UniquePtr<SSL> client, server; |
| 1416 |
ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1417 |
server_ctx.get())); |
| 1418 |
|
| 1419 |
// Whereas with a verifier, the connection should succeed. |
| 1420 |
SSL_CTX_set_custom_verify( |
| 1421 |
client_ctx.get(), SSL_VERIFY_PEER, |
| 1422 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1423 |
return ssl_verify_ok; |
| 1424 |
}); |
| 1425 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1426 |
server_ctx.get())); |
| 1427 |
} |
| 1428 |
|
| 1429 |
TEST(SSLTest, CustomVerify) { |
| 1430 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 1431 |
ASSERT_TRUE(client_ctx); |
| 1432 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 1433 |
ASSERT_TRUE(server_ctx); |
| 1434 |
|
| 1435 |
bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 1436 |
ASSERT_TRUE(key); |
| 1437 |
bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 1438 |
ASSERT_TRUE(leaf); |
| 1439 |
std::vector<CRYPTO_BUFFER *> chain = {leaf.get()}; |
| 1440 |
ASSERT_TRUE(SSL_CTX_set_chain_and_key(server_ctx.get(), &chain[0], |
| 1441 |
chain.size(), key.get(), nullptr)); |
| 1442 |
|
| 1443 |
SSL_CTX_set_custom_verify( |
| 1444 |
client_ctx.get(), SSL_VERIFY_PEER, |
| 1445 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1446 |
return ssl_verify_ok; |
| 1447 |
}); |
| 1448 |
|
| 1449 |
bssl::UniquePtr<SSL> client, server; |
| 1450 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1451 |
server_ctx.get())); |
| 1452 |
|
| 1453 |
// With SSL_VERIFY_PEER, ssl_verify_invalid should result in a dropped |
| 1454 |
// connection. |
| 1455 |
SSL_CTX_set_custom_verify( |
| 1456 |
client_ctx.get(), SSL_VERIFY_PEER, |
| 1457 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1458 |
return ssl_verify_invalid; |
| 1459 |
}); |
| 1460 |
|
| 1461 |
ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1462 |
server_ctx.get())); |
| 1463 |
|
| 1464 |
// But with SSL_VERIFY_NONE, ssl_verify_invalid should not cause a dropped |
| 1465 |
// connection. |
| 1466 |
SSL_CTX_set_custom_verify( |
| 1467 |
client_ctx.get(), SSL_VERIFY_NONE, |
| 1468 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1469 |
return ssl_verify_invalid; |
| 1470 |
}); |
| 1471 |
|
| 1472 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1473 |
server_ctx.get())); |
| 1474 |
} |
| 1475 |
|
| 1476 |
TEST(SSLTest, ClientCABuffers) { |
| 1477 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 1478 |
ASSERT_TRUE(client_ctx); |
| 1479 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 1480 |
ASSERT_TRUE(server_ctx); |
| 1481 |
|
| 1482 |
bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 1483 |
ASSERT_TRUE(key); |
| 1484 |
bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 1485 |
ASSERT_TRUE(leaf); |
| 1486 |
bssl::UniquePtr<CRYPTO_BUFFER> intermediate = |
| 1487 |
GetChainTestIntermediateBuffer(); |
| 1488 |
ASSERT_TRUE(intermediate); |
| 1489 |
std::vector<CRYPTO_BUFFER *> chain = { |
| 1490 |
leaf.get(), |
| 1491 |
intermediate.get(), |
| 1492 |
}; |
| 1493 |
ASSERT_TRUE(SSL_CTX_set_chain_and_key(server_ctx.get(), &chain[0], |
| 1494 |
chain.size(), key.get(), nullptr)); |
| 1495 |
|
| 1496 |
bssl::UniquePtr<CRYPTO_BUFFER> ca_name( |
| 1497 |
CRYPTO_BUFFER_new(kTestName, sizeof(kTestName), nullptr)); |
| 1498 |
ASSERT_TRUE(ca_name); |
| 1499 |
bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names( |
| 1500 |
sk_CRYPTO_BUFFER_new_null()); |
| 1501 |
ASSERT_TRUE(ca_names); |
| 1502 |
ASSERT_TRUE(PushToStack(ca_names.get(), std::move(ca_name))); |
| 1503 |
SSL_CTX_set0_client_CAs(server_ctx.get(), ca_names.release()); |
| 1504 |
|
| 1505 |
// Configure client and server to accept all certificates. |
| 1506 |
SSL_CTX_set_custom_verify( |
| 1507 |
client_ctx.get(), SSL_VERIFY_PEER, |
| 1508 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1509 |
return ssl_verify_ok; |
| 1510 |
}); |
| 1511 |
SSL_CTX_set_custom_verify( |
| 1512 |
server_ctx.get(), SSL_VERIFY_PEER, |
| 1513 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1514 |
return ssl_verify_ok; |
| 1515 |
}); |
| 1516 |
|
| 1517 |
bool cert_cb_called = false; |
| 1518 |
SSL_CTX_set_cert_cb( |
| 1519 |
client_ctx.get(), |
| 1520 |
[](SSL *ssl, void *arg) -> int { |
| 1521 |
const STACK_OF(CRYPTO_BUFFER) *peer_names = |
| 1522 |
SSL_get0_server_requested_CAs(ssl); |
| 1523 |
EXPECT_EQ(1u, sk_CRYPTO_BUFFER_num(peer_names)); |
| 1524 |
CRYPTO_BUFFER *peer_name = sk_CRYPTO_BUFFER_value(peer_names, 0); |
| 1525 |
EXPECT_EQ(Bytes(kTestName), Bytes(CRYPTO_BUFFER_data(peer_name), |
| 1526 |
CRYPTO_BUFFER_len(peer_name))); |
| 1527 |
*reinterpret_cast<bool *>(arg) = true; |
| 1528 |
return 1; |
| 1529 |
}, |
| 1530 |
&cert_cb_called); |
| 1531 |
|
| 1532 |
bssl::UniquePtr<SSL> client, server; |
| 1533 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1534 |
server_ctx.get())); |
| 1535 |
EXPECT_TRUE(cert_cb_called); |
| 1536 |
} |
| 1537 |
|
| 1538 |
// Test that |SSL_get_client_CA_list| returns the server's CA list on the |
| 1539 |
// client both during the cert callback and after the handshake completes. |
| 1540 |
TEST(SSLTest, PeerCANamesX509DuringAndAfterHandshake) { |
| 1541 |
for (uint16_t version : {TLS1_2_VERSION, TLS1_3_VERSION}) { |
| 1542 |
SCOPED_TRACE(version); |
| 1543 |
|
| 1544 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1545 |
ASSERT_TRUE(server_ctx); |
| 1546 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), version)); |
| 1547 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), version)); |
| 1548 |
|
| 1549 |
bssl::UniquePtr<X509> cert = GetChainTestCertificate(); |
| 1550 |
bssl::UniquePtr<X509> intermediate = GetChainTestIntermediate(); |
| 1551 |
bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 1552 |
ASSERT_TRUE(cert && intermediate && key); |
| 1553 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), cert.get())); |
| 1554 |
ASSERT_TRUE( |
| 1555 |
SSL_CTX_add1_chain_cert(server_ctx.get(), intermediate.get())); |
| 1556 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx.get(), key.get())); |
| 1557 |
|
| 1558 |
// Configure the server's CA list using X509_NAMEs. |
| 1559 |
bssl::UniquePtr<X509_NAME> ca_name(X509_NAME_new()); |
| 1560 |
ASSERT_TRUE(ca_name); |
| 1561 |
ASSERT_TRUE(X509_NAME_add_entry_by_txt( |
| 1562 |
ca_name.get(), "CN", MBSTRING_ASC, |
| 1563 |
reinterpret_cast<const unsigned char *>("Test CA"), -1, -1, 0)); |
| 1564 |
|
| 1565 |
bssl::UniquePtr<X509_NAME> ca_name_copy(X509_NAME_dup(ca_name.get())); |
| 1566 |
ASSERT_TRUE(ca_name_copy); |
| 1567 |
bssl::UniquePtr<STACK_OF(X509_NAME)> ca_list(sk_X509_NAME_new_null()); |
| 1568 |
ASSERT_TRUE(ca_list); |
| 1569 |
ASSERT_TRUE(PushToStack(ca_list.get(), std::move(ca_name_copy))); |
| 1570 |
// SSL_CTX_set_client_CA_list takes ownership. |
| 1571 |
SSL_CTX_set_client_CA_list(server_ctx.get(), ca_list.release()); |
| 1572 |
|
| 1573 |
// The server must request client certificates. |
| 1574 |
SSL_CTX_set_verify(server_ctx.get(), SSL_VERIFY_PEER, nullptr); |
| 1575 |
|
| 1576 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1577 |
ASSERT_TRUE(client_ctx); |
| 1578 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), version)); |
| 1579 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), version)); |
| 1580 |
// Accept any server certificate. |
| 1581 |
SSL_CTX_set_custom_verify( |
| 1582 |
client_ctx.get(), SSL_VERIFY_PEER, |
| 1583 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 1584 |
return ssl_verify_ok; |
| 1585 |
}); |
| 1586 |
|
| 1587 |
// Use a cert callback to verify CA names are available during the |
| 1588 |
// handshake via the X509-based API. |
| 1589 |
bool cert_cb_called = false; |
| 1590 |
SSL_CTX_set_cert_cb( |
| 1591 |
client_ctx.get(), |
| 1592 |
[](SSL *ssl, void *arg) -> int { |
| 1593 |
STACK_OF(X509_NAME) *ca_list = SSL_get_client_CA_list(ssl); |
| 1594 |
EXPECT_TRUE(ca_list); |
| 1595 |
EXPECT_EQ(1u, sk_X509_NAME_num(ca_list)); |
| 1596 |
*reinterpret_cast<bool *>(arg) = true; |
| 1597 |
return 1; |
| 1598 |
}, |
| 1599 |
&cert_cb_called); |
| 1600 |
|
| 1601 |
bssl::UniquePtr<SSL> client, server; |
| 1602 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1603 |
server_ctx.get())); |
| 1604 |
EXPECT_TRUE(cert_cb_called); |
| 1605 |
|
| 1606 |
// After the handshake, verify the same CA list is still available. |
| 1607 |
// The handshake config has been shed by default, so ssl->config is NULL, |
| 1608 |
// but the client-side peer CA names are persisted in ssl->s3. |
| 1609 |
STACK_OF(X509_NAME) *client_ca_list = |
| 1610 |
SSL_get_client_CA_list(client.get()); |
| 1611 |
ASSERT_TRUE(client_ca_list); |
| 1612 |
ASSERT_EQ(1u, sk_X509_NAME_num(client_ca_list)); |
| 1613 |
EXPECT_EQ(0, |
| 1614 |
X509_NAME_cmp(sk_X509_NAME_value(client_ca_list, 0), |
| 1615 |
ca_name.get())); |
| 1616 |
|
| 1617 |
// Now do a second handshake with the same client context but a server |
| 1618 |
// that does NOT request client certificates (no CertificateRequest). |
| 1619 |
// The client should see no peer CA names after this handshake. |
| 1620 |
bssl::UniquePtr<SSL_CTX> server_ctx2(SSL_CTX_new(TLS_method())); |
| 1621 |
ASSERT_TRUE(server_ctx2); |
| 1622 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx2.get(), version)); |
| 1623 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx2.get(), version)); |
| 1624 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx2.get(), cert.get())); |
| 1625 |
ASSERT_TRUE( |
| 1626 |
SSL_CTX_add1_chain_cert(server_ctx2.get(), intermediate.get())); |
| 1627 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx2.get(), key.get())); |
| 1628 |
|
| 1629 |
bssl::UniquePtr<SSL> client2, server2; |
| 1630 |
ASSERT_TRUE(ConnectClientAndServer(&client2, &server2, client_ctx.get(), |
| 1631 |
server_ctx2.get())); |
| 1632 |
EXPECT_FALSE(SSL_get_client_CA_list(client2.get())); |
| 1633 |
} |
| 1634 |
} |
| 1635 |
|
| 1636 |
// Configuring the empty cipher list, though an error, should still modify the |
| 1637 |
// configuration. |
| 1638 |
TEST(SSLTest, EmptyCipherList) { |
| 1639 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1640 |
ASSERT_TRUE(ctx); |
| 1641 |
|
| 1642 |
// Initially, the cipher list is not empty. |
| 1643 |
EXPECT_NE(0u, sk_SSL_CIPHER_num(SSL_CTX_get_ciphers(ctx.get()))); |
| 1644 |
|
| 1645 |
// Configuring the empty cipher list with |SSL_CTX_set_cipher_list| |
| 1646 |
// succeeds. |
| 1647 |
EXPECT_TRUE(SSL_CTX_set_cipher_list(ctx.get(), "")); |
| 1648 |
// The cipher list should only be populated with default TLS 1.3 ciphersuites |
| 1649 |
EXPECT_EQ(3u, sk_SSL_CIPHER_num(SSL_CTX_get_ciphers(ctx.get()))); |
| 1650 |
|
| 1651 |
// Configuring the empty cipher list with |SSL_CTX_set_ciphersuites| works |
| 1652 |
EXPECT_TRUE(SSL_CTX_set_ciphersuites(ctx.get(), "")); |
| 1653 |
EXPECT_EQ(0u, sk_SSL_CIPHER_num(ctx->tls13_cipher_list->ciphers.get())); |
| 1654 |
EXPECT_EQ(0u, sk_SSL_CIPHER_num(SSL_CTX_get_ciphers(ctx.get()))); |
| 1655 |
|
| 1656 |
// Configuring the empty cipher list with |SSL_CTX_set_strict_cipher_list| |
| 1657 |
// fails. |
| 1658 |
EXPECT_FALSE(SSL_CTX_set_strict_cipher_list(ctx.get(), "")); |
| 1659 |
ERR_clear_error(); |
| 1660 |
|
| 1661 |
// But the cipher list is still updated to empty. |
| 1662 |
EXPECT_EQ(0u, sk_SSL_CIPHER_num(SSL_CTX_get_ciphers(ctx.get()))); |
| 1663 |
} |
| 1664 |
|
| 1665 |
struct TLSVersionTestParams { |
| 1666 |
uint16_t version; |
| 1667 |
}; |
| 1668 |
|
| 1669 |
const TLSVersionTestParams kTLSVersionTests[] = { |
| 1670 |
{TLS1_VERSION}, |
| 1671 |
{TLS1_1_VERSION}, |
| 1672 |
{TLS1_2_VERSION}, |
| 1673 |
{TLS1_3_VERSION}, |
| 1674 |
}; |
| 1675 |
|
| 1676 |
struct CertificateKeyTestParams { |
| 1677 |
bssl::UniquePtr<X509> (*certificate)(); |
| 1678 |
bssl::UniquePtr<EVP_PKEY> (*key)(); |
| 1679 |
int slot_index; |
| 1680 |
const char suite[50]; |
| 1681 |
uint16_t corresponding_sigalg; |
| 1682 |
}; |
| 1683 |
|
| 1684 |
const CertificateKeyTestParams kCertificateKeyTests[] = { |
| 1685 |
{GetTestCertificate, GetTestKey, SSL_PKEY_RSA, |
| 1686 |
"TLS_RSA_WITH_AES_256_CBC_SHA:", SSL_SIGN_RSA_PSS_RSAE_SHA256}, |
| 1687 |
{GetECDSATestCertificate, GetECDSATestKey, SSL_PKEY_ECC, |
| 1688 |
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:", SSL_SIGN_ECDSA_SECP256R1_SHA256}, |
| 1689 |
{GetED25519TestCertificate, GetED25519TestKey, SSL_PKEY_ED25519, |
| 1690 |
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:", SSL_SIGN_ED25519}, |
| 1691 |
}; |
| 1692 |
|
| 1693 |
class MultipleCertificateSlotTest |
| 1694 |
: public testing::TestWithParam< |
| 1695 |
std::tuple<TLSVersionTestParams, CertificateKeyTestParams>> { |
| 1696 |
public: |
| 1697 |
MultipleCertificateSlotTest() { |
| 1698 |
this->version = version_param().version; |
| 1699 |
this->slot_index = certificate_key_param().slot_index; |
| 1700 |
} |
| 1701 |
|
| 1702 |
uint16_t version = 0; |
| 1703 |
int slot_index = -1; |
| 1704 |
|
| 1705 |
static TLSVersionTestParams version_param() { |
| 1706 |
return std::get<0>(GetParam()); |
| 1707 |
} |
| 1708 |
static CertificateKeyTestParams certificate_key_param() { |
| 1709 |
return std::get<1>(GetParam()); |
| 1710 |
} |
| 1711 |
|
| 1712 |
const void StandardCertificateSlotIndexTests(SSL_CTX *client_ctx, |
| 1713 |
SSL_CTX *server_ctx, |
| 1714 |
std::vector<uint16_t> sigalgs, |
| 1715 |
int last_cert_type_set, |
| 1716 |
int should_connect) { |
| 1717 |
EXPECT_TRUE(SSL_CTX_set_signing_algorithm_prefs(client_ctx, sigalgs.data(), |
| 1718 |
sigalgs.size())); |
| 1719 |
EXPECT_TRUE(SSL_CTX_set_verify_algorithm_prefs(client_ctx, sigalgs.data(), |
| 1720 |
sigalgs.size())); |
| 1721 |
|
| 1722 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx, version)); |
| 1723 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx, version)); |
| 1724 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx, version)); |
| 1725 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx, version)); |
| 1726 |
|
| 1727 |
ClientConfig config; |
| 1728 |
bssl::UniquePtr<SSL> client, server; |
| 1729 |
|
| 1730 |
EXPECT_EQ(ConnectClientAndServer(&client, &server, client_ctx, server_ctx, |
| 1731 |
config, false), |
| 1732 |
should_connect); |
| 1733 |
if (!should_connect) { |
| 1734 |
return; |
| 1735 |
} |
| 1736 |
|
| 1737 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 1738 |
|
| 1739 |
// Check the internal slot index to verify that the correct slot was set. |
| 1740 |
// This should be the slot of the last certificate that was set in |
| 1741 |
// |server_ctx|. |
| 1742 |
EXPECT_EQ(server_ctx->cert->cert_private_key_idx, last_cert_type_set); |
| 1743 |
EXPECT_EQ(server->ctx->cert->cert_private_key_idx, last_cert_type_set); |
| 1744 |
|
| 1745 |
// Check the internal slot index to verify that the correct slot was used |
| 1746 |
// during the handshake. |
| 1747 |
EXPECT_EQ(server->config->cert->cert_private_key_idx, slot_index); |
| 1748 |
} |
| 1749 |
}; |
| 1750 |
|
| 1751 |
INSTANTIATE_TEST_SUITE_P( |
| 1752 |
MultipleCertificateSlotAllTest, MultipleCertificateSlotTest, |
| 1753 |
testing::Combine(testing::ValuesIn(kTLSVersionTests), |
| 1754 |
testing::ValuesIn(kCertificateKeyTests))); |
| 1755 |
|
| 1756 |
// Sets up the |SSL_CTX| with |SSL_CTX_use_certificate| & |SSL_use_PrivateKey|. |
| 1757 |
TEST_P(MultipleCertificateSlotTest, CertificateSlotIndex) { |
| 1758 |
if (version < TLS1_2_VERSION && slot_index == SSL_PKEY_ED25519) { |
| 1759 |
// ED25519 is not supported in versions prior to TLS1.2. |
| 1760 |
GTEST_SKIP(); |
| 1761 |
} |
| 1762 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1763 |
bssl::UniquePtr<SSL_CTX> server_ctx(CreateContextWithCertificate( |
| 1764 |
TLS_method(), certificate_key_param().certificate(), |
| 1765 |
certificate_key_param().key())); |
| 1766 |
|
| 1767 |
StandardCertificateSlotIndexTests( |
| 1768 |
client_ctx.get(), server_ctx.get(), |
| 1769 |
{SSL_SIGN_ED25519, SSL_SIGN_ECDSA_SECP256R1_SHA256, |
| 1770 |
SSL_SIGN_RSA_PSS_RSAE_SHA256}, |
| 1771 |
slot_index, true); |
| 1772 |
} |
| 1773 |
|
| 1774 |
// Sets up the |SSL_CTX| with |SSL_CTX_set_chain_and_key|. |
| 1775 |
TEST_P(MultipleCertificateSlotTest, SetChainAndKeyIndex) { |
| 1776 |
if (version < TLS1_2_VERSION && slot_index == SSL_PKEY_ED25519) { |
| 1777 |
// ED25519 is not supported in versions prior to TLS1.2. |
| 1778 |
GTEST_SKIP(); |
| 1779 |
} |
| 1780 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1781 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1782 |
|
| 1783 |
uint8_t *buf = nullptr; |
| 1784 |
int cert_len = i2d_X509(certificate_key_param().certificate().get(), &buf); |
| 1785 |
bssl::UniquePtr<uint8_t> free_buf(buf); |
| 1786 |
|
| 1787 |
bssl::UniquePtr<CRYPTO_BUFFER> leaf( |
| 1788 |
CRYPTO_BUFFER_new(buf, cert_len, nullptr)); |
| 1789 |
ASSERT_TRUE(leaf); |
| 1790 |
std::vector<CRYPTO_BUFFER *> chain = {leaf.get()}; |
| 1791 |
|
| 1792 |
ASSERT_TRUE( |
| 1793 |
SSL_CTX_set_chain_and_key(server_ctx.get(), &chain[0], chain.size(), |
| 1794 |
certificate_key_param().key().get(), nullptr)); |
| 1795 |
|
| 1796 |
StandardCertificateSlotIndexTests( |
| 1797 |
client_ctx.get(), server_ctx.get(), |
| 1798 |
{SSL_SIGN_ED25519, SSL_SIGN_ECDSA_SECP256R1_SHA256, |
| 1799 |
SSL_SIGN_RSA_PSS_RSAE_SHA256}, |
| 1800 |
slot_index, true); |
| 1801 |
} |
| 1802 |
|
| 1803 |
TEST_P(MultipleCertificateSlotTest, AutomaticSelectionSigAlgs) { |
| 1804 |
if (version < TLS1_2_VERSION && slot_index == SSL_PKEY_ED25519) { |
| 1805 |
// ED25519 is not supported in versions prior to TLS1.2. |
| 1806 |
GTEST_SKIP(); |
| 1807 |
} |
| 1808 |
|
| 1809 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1810 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1811 |
|
| 1812 |
ASSERT_TRUE( |
| 1813 |
SSL_CTX_use_certificate(server_ctx.get(), GetTestCertificate().get())); |
| 1814 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx.get(), GetTestKey().get())); |
| 1815 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), |
| 1816 |
GetECDSATestCertificate().get())); |
| 1817 |
ASSERT_TRUE( |
| 1818 |
SSL_CTX_use_PrivateKey(server_ctx.get(), GetECDSATestKey().get())); |
| 1819 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), |
| 1820 |
GetED25519TestCertificate().get())); |
| 1821 |
ASSERT_TRUE( |
| 1822 |
SSL_CTX_use_PrivateKey(server_ctx.get(), GetED25519TestKey().get())); |
| 1823 |
|
| 1824 |
|
| 1825 |
// Versions prior to TLS1.3 need a valid authentication cipher suite to pair |
| 1826 |
// with the certificate. |
| 1827 |
if (version < TLS1_3_VERSION) { |
| 1828 |
ASSERT_TRUE(SSL_CTX_set_cipher_list(client_ctx.get(), |
| 1829 |
certificate_key_param().suite)); |
| 1830 |
} |
| 1831 |
|
| 1832 |
StandardCertificateSlotIndexTests( |
| 1833 |
client_ctx.get(), server_ctx.get(), |
| 1834 |
{certificate_key_param().corresponding_sigalg}, SSL_PKEY_ED25519, true); |
| 1835 |
} |
| 1836 |
|
| 1837 |
TEST_P(MultipleCertificateSlotTest, AutomaticSelectionCipherAuth) { |
| 1838 |
if ((version < TLS1_2_VERSION && slot_index == SSL_PKEY_ED25519) || |
| 1839 |
version >= TLS1_3_VERSION) { |
| 1840 |
// ED25519 is not supported in versions prior to TLS1.2. |
| 1841 |
// TLS 1.3 not have cipher-based authentication configuration. |
| 1842 |
GTEST_SKIP(); |
| 1843 |
} |
| 1844 |
|
| 1845 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1846 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1847 |
|
| 1848 |
ASSERT_TRUE( |
| 1849 |
SSL_CTX_use_certificate(server_ctx.get(), GetTestCertificate().get())); |
| 1850 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx.get(), GetTestKey().get())); |
| 1851 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), |
| 1852 |
GetECDSATestCertificate().get())); |
| 1853 |
ASSERT_TRUE( |
| 1854 |
SSL_CTX_use_PrivateKey(server_ctx.get(), GetECDSATestKey().get())); |
| 1855 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), |
| 1856 |
GetED25519TestCertificate().get())); |
| 1857 |
ASSERT_TRUE( |
| 1858 |
SSL_CTX_use_PrivateKey(server_ctx.get(), GetED25519TestKey().get())); |
| 1859 |
|
| 1860 |
|
| 1861 |
// Versions prior to TLS1.3 need a valid authentication cipher suite to pair |
| 1862 |
// with the certificate. |
| 1863 |
if (version < TLS1_3_VERSION) { |
| 1864 |
ASSERT_TRUE(SSL_CTX_set_cipher_list(client_ctx.get(), |
| 1865 |
certificate_key_param().suite)); |
| 1866 |
} |
| 1867 |
|
| 1868 |
// We allow all possible sigalgs in this test, but either the ECDSA or ED25519 |
| 1869 |
// certificate could be chosen when using an |SSL_aECDSA| ciphersuite. |
| 1870 |
std::vector<uint16_t> sigalgs = {SSL_SIGN_RSA_PSS_RSAE_SHA256}; |
| 1871 |
if (slot_index == SSL_PKEY_ED25519) { |
| 1872 |
sigalgs.push_back(SSL_SIGN_ED25519); |
| 1873 |
} else { |
| 1874 |
sigalgs.push_back(SSL_SIGN_ECDSA_SECP256R1_SHA256); |
| 1875 |
} |
| 1876 |
|
| 1877 |
StandardCertificateSlotIndexTests(client_ctx.get(), server_ctx.get(), sigalgs, |
| 1878 |
SSL_PKEY_ED25519, true); |
| 1879 |
} |
| 1880 |
|
| 1881 |
TEST_P(MultipleCertificateSlotTest, MissingCertificate) { |
| 1882 |
if (version < TLS1_2_VERSION && slot_index == SSL_PKEY_ED25519) { |
| 1883 |
// ED25519 is not supported in versions prior to TLS1.2. |
| 1884 |
GTEST_SKIP(); |
| 1885 |
} |
| 1886 |
|
| 1887 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1888 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1889 |
|
| 1890 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx.get(), GetTestKey().get())); |
| 1891 |
ASSERT_TRUE( |
| 1892 |
SSL_CTX_use_PrivateKey(server_ctx.get(), GetECDSATestKey().get())); |
| 1893 |
ASSERT_TRUE( |
| 1894 |
SSL_CTX_use_PrivateKey(server_ctx.get(), GetED25519TestKey().get())); |
| 1895 |
|
| 1896 |
// Versions prior to TLS1.3 need a valid authentication cipher suite to pair |
| 1897 |
// with the certificate. |
| 1898 |
if (version < TLS1_3_VERSION) { |
| 1899 |
ASSERT_TRUE(SSL_CTX_set_cipher_list(client_ctx.get(), |
| 1900 |
certificate_key_param().suite)); |
| 1901 |
} |
| 1902 |
|
| 1903 |
StandardCertificateSlotIndexTests( |
| 1904 |
client_ctx.get(), server_ctx.get(), |
| 1905 |
{certificate_key_param().corresponding_sigalg}, -1, false); |
| 1906 |
} |
| 1907 |
|
| 1908 |
TEST_P(MultipleCertificateSlotTest, MissingPrivateKey) { |
| 1909 |
if (version < TLS1_2_VERSION && slot_index == SSL_PKEY_ED25519) { |
| 1910 |
// ED25519 is not supported in versions prior to TLS1.2. |
| 1911 |
GTEST_SKIP(); |
| 1912 |
} |
| 1913 |
|
| 1914 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1915 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1916 |
|
| 1917 |
ASSERT_TRUE( |
| 1918 |
SSL_CTX_use_certificate(server_ctx.get(), GetTestCertificate().get())); |
| 1919 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), |
| 1920 |
GetECDSATestCertificate().get())); |
| 1921 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), |
| 1922 |
GetED25519TestCertificate().get())); |
| 1923 |
|
| 1924 |
// Versions prior to TLS1.3 need a valid authentication cipher suite to pair |
| 1925 |
// with the certificate. |
| 1926 |
if (version < TLS1_3_VERSION) { |
| 1927 |
ASSERT_TRUE(SSL_CTX_set_cipher_list(client_ctx.get(), |
| 1928 |
certificate_key_param().suite)); |
| 1929 |
} |
| 1930 |
|
| 1931 |
StandardCertificateSlotIndexTests( |
| 1932 |
client_ctx.get(), server_ctx.get(), |
| 1933 |
{certificate_key_param().corresponding_sigalg}, -1, false); |
| 1934 |
} |
| 1935 |
|
| 1936 |
|
| 1937 |
struct MultiTransferReadWriteTestParams { |
| 1938 |
const char suite[50]; |
| 1939 |
bool tls13; |
| 1940 |
bssl::UniquePtr<X509> (*certificate)(); |
| 1941 |
bssl::UniquePtr<EVP_PKEY> (*key)(); |
| 1942 |
}; |
| 1943 |
|
| 1944 |
static const MultiTransferReadWriteTestParams kMultiTransferReadWriteTests[] = { |
| 1945 |
{"TLS_AES_128_GCM_SHA256:", true, GetECDSATestCertificate, GetECDSATestKey}, |
| 1946 |
{"TLS_AES_256_GCM_SHA384:", true, GetECDSATestCertificate, GetECDSATestKey}, |
| 1947 |
{"TLS_CHACHA20_POLY1305_SHA256:", true, GetECDSATestCertificate, |
| 1948 |
GetECDSATestKey}, |
| 1949 |
{"TLS_RSA_WITH_NULL_SHA:", false, GetTestCertificate, GetTestKey}, |
| 1950 |
{"TLS_RSA_WITH_3DES_EDE_CBC_SHA:", false, GetTestCertificate, GetTestKey}, |
| 1951 |
{"TLS_RSA_WITH_AES_128_CBC_SHA:", false, GetTestCertificate, GetTestKey}, |
| 1952 |
{"TLS_RSA_WITH_AES_256_CBC_SHA:", false, GetTestCertificate, GetTestKey}, |
| 1953 |
{"TLS_RSA_WITH_AES_128_CBC_SHA256:", false, GetTestCertificate, GetTestKey}, |
| 1954 |
{"TLS_RSA_WITH_AES_128_GCM_SHA256:", false, GetTestCertificate, GetTestKey}, |
| 1955 |
{"TLS_RSA_WITH_AES_256_GCM_SHA384:", false, GetTestCertificate, GetTestKey}, |
| 1956 |
{"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:", false, GetECDSATestCertificate, |
| 1957 |
GetECDSATestKey}, |
| 1958 |
{"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:", false, GetECDSATestCertificate, |
| 1959 |
GetECDSATestKey}, |
| 1960 |
{"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:", false, GetTestCertificate, |
| 1961 |
GetTestKey}, |
| 1962 |
{"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:", false, GetTestCertificate, |
| 1963 |
GetTestKey}, |
| 1964 |
{"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:", false, GetTestCertificate, |
| 1965 |
GetTestKey}, |
| 1966 |
{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:", false, GetECDSATestCertificate, |
| 1967 |
GetECDSATestKey}, |
| 1968 |
{"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:", false, GetECDSATestCertificate, |
| 1969 |
GetECDSATestKey}, |
| 1970 |
{"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:", false, GetTestCertificate, |
| 1971 |
GetTestKey}, |
| 1972 |
{"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:", false, GetTestCertificate, |
| 1973 |
GetTestKey}, |
| 1974 |
{"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:", false, GetTestCertificate, |
| 1975 |
GetTestKey}, |
| 1976 |
{"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:", false, |
| 1977 |
GetECDSATestCertificate, GetECDSATestKey}}; |
| 1978 |
|
| 1979 |
class MultiTransferReadWriteTest |
| 1980 |
: public testing::TestWithParam<MultiTransferReadWriteTestParams> {}; |
| 1981 |
|
| 1982 |
INSTANTIATE_TEST_SUITE_P(SuiteTests, MultiTransferReadWriteTest, |
| 1983 |
testing::ValuesIn(kMultiTransferReadWriteTests)); |
| 1984 |
|
| 1985 |
TEST_P(MultiTransferReadWriteTest, SuiteTransfers) { |
| 1986 |
auto params = GetParam(); |
| 1987 |
|
| 1988 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1989 |
bssl::UniquePtr<SSL_CTX> server_ctx(CreateContextWithCertificate( |
| 1990 |
TLS_method(), params.certificate(), params.key())); |
| 1991 |
|
| 1992 |
uint16_t version = TLS1_2_VERSION; |
| 1993 |
int (*set_cipher_suites)(SSL_CTX *, const char *) = SSL_CTX_set_cipher_list; |
| 1994 |
if (params.tls13) { |
| 1995 |
version = TLS1_3_VERSION; |
| 1996 |
set_cipher_suites = SSL_CTX_set_ciphersuites; |
| 1997 |
} |
| 1998 |
|
| 1999 |
ASSERT_TRUE(set_cipher_suites(client_ctx.get(), params.suite)); |
| 2000 |
ASSERT_TRUE(set_cipher_suites(server_ctx.get(), params.suite)); |
| 2001 |
|
| 2002 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), version)); |
| 2003 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), version)); |
| 2004 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), version)); |
| 2005 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), version)); |
| 2006 |
|
| 2007 |
ClientConfig config; |
| 2008 |
bssl::UniquePtr<SSL> client, server; |
| 2009 |
|
| 2010 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 2011 |
server_ctx.get(), config, true)); |
| 2012 |
|
| 2013 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 2014 |
|
| 2015 |
bssl::UniquePtr<SSL> transfer_server; |
| 2016 |
TransferSSL(&server, server_ctx.get(), &transfer_server, false); |
| 2017 |
|
| 2018 |
// All Read/Write connection operations should fail now for a transferred server |
| 2019 |
ASSERT_EQ(-1, SSL_connect(server.get())); |
| 2020 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2021 |
ERR_clear_error(); |
| 2022 |
|
| 2023 |
ASSERT_EQ(-1, SSL_accept(server.get())); |
| 2024 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2025 |
ERR_clear_error(); |
| 2026 |
|
| 2027 |
ASSERT_EQ(-1, SSL_shutdown(server.get())); |
| 2028 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2029 |
ERR_clear_error(); |
| 2030 |
|
| 2031 |
ASSERT_EQ(-1, SSL_do_handshake(server.get())); |
| 2032 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2033 |
ERR_clear_error(); |
| 2034 |
|
| 2035 |
ASSERT_EQ(-1, SSL_peek(server.get(), nullptr, 0)); |
| 2036 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2037 |
ERR_clear_error(); |
| 2038 |
|
| 2039 |
ASSERT_EQ(0, SSL_peek_ex(server.get(), nullptr, 0, nullptr)); |
| 2040 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2041 |
ERR_clear_error(); |
| 2042 |
|
| 2043 |
ASSERT_EQ(-1, SSL_read(server.get(), nullptr, 0)); |
| 2044 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2045 |
ERR_clear_error(); |
| 2046 |
|
| 2047 |
ASSERT_EQ(0, SSL_read_ex(server.get(), nullptr, 0, nullptr)); |
| 2048 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2049 |
ERR_clear_error(); |
| 2050 |
|
| 2051 |
ASSERT_EQ(-1, SSL_write(server.get(), nullptr, 0)); |
| 2052 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2053 |
ERR_clear_error(); |
| 2054 |
|
| 2055 |
ASSERT_EQ(0, SSL_write_ex(server.get(), nullptr, 0, nullptr)); |
| 2056 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2057 |
ERR_clear_error(); |
| 2058 |
|
| 2059 |
ASSERT_EQ(0, SSL_key_update(server.get(), SSL_KEY_UPDATE_REQUESTED)); |
| 2060 |
ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2061 |
ERR_clear_error(); |
| 2062 |
|
| 2063 |
server = std::move(transfer_server); |
| 2064 |
|
| 2065 |
char buf[3]; |
| 2066 |
size_t buf_cap = sizeof(buf); |
| 2067 |
|
| 2068 |
size_t server_transfers = 5; |
| 2069 |
size_t read_write_per_instance = 20; |
| 2070 |
|
| 2071 |
for (size_t t = 0; t < server_transfers; t++) { |
| 2072 |
for (size_t i = 0; i < read_write_per_instance; i++) { |
| 2073 |
std::string send_str = std::to_string(i); |
| 2074 |
|
| 2075 |
if(t > 0 && i == 0) { |
| 2076 |
// Actually read the previously peeked data from the last transfer |
| 2077 |
std::string peeked_str = std::to_string(read_write_per_instance-1); |
| 2078 |
int read = SSL_read(server.get(), buf, buf_cap); |
| 2079 |
ASSERT_TRUE(read); |
| 2080 |
ASSERT_TRUE((size_t)read == peeked_str.length()); |
| 2081 |
std::string read_str(buf, read); |
| 2082 |
ASSERT_EQ(peeked_str, read_str); |
| 2083 |
} |
| 2084 |
|
| 2085 |
// Assert server open |
| 2086 |
ASSERT_TRUE(SSL_write(client.get(), send_str.c_str(), send_str.length())); |
| 2087 |
int read = SSL_read(server.get(), buf, buf_cap); |
| 2088 |
ASSERT_TRUE(read); |
| 2089 |
ASSERT_TRUE((size_t)read == send_str.length()); |
| 2090 |
std::string read_str(buf, read); |
| 2091 |
ASSERT_EQ(send_str, read_str); |
| 2092 |
|
| 2093 |
// Assert server seal |
| 2094 |
ASSERT_TRUE(SSL_write(server.get(), send_str.c_str(), send_str.length())); |
| 2095 |
read = SSL_read(client.get(), buf, buf_cap); |
| 2096 |
ASSERT_TRUE(read); |
| 2097 |
ASSERT_TRUE((size_t)read == send_str.length()); |
| 2098 |
read_str = std::string(buf, read); |
| 2099 |
ASSERT_EQ(send_str, read_str); |
| 2100 |
|
| 2101 |
if(i == read_write_per_instance - 1) { |
| 2102 |
// Peek some client data before serialization |
| 2103 |
ASSERT_TRUE(SSL_write(client.get(), send_str.c_str(), send_str.length())); |
| 2104 |
read = SSL_peek(server.get(), buf, buf_cap); |
| 2105 |
ASSERT_TRUE(read); |
| 2106 |
ASSERT_TRUE((size_t)read == send_str.length()); |
| 2107 |
read_str = std::string(buf, read); |
| 2108 |
ASSERT_EQ(send_str, read_str); |
| 2109 |
} |
| 2110 |
} |
| 2111 |
if(version == TLS1_3_VERSION) { |
| 2112 |
// Queue a Key Update to validate pending handshake flight messages are serialized |
| 2113 |
SSL_key_update(server.get(), SSL_KEY_UPDATE_REQUESTED); |
| 2114 |
} |
| 2115 |
TransferSSL(&server, server_ctx.get(), &transfer_server); |
| 2116 |
server = std::move(transfer_server); |
| 2117 |
} |
| 2118 |
} |
| 2119 |
|
| 2120 |
TEST(SSLTest, BIO) { |
| 2121 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 2122 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 2123 |
CreateContextWithTestCertificate(TLS_method())); |
| 2124 |
ASSERT_TRUE(client_ctx); |
| 2125 |
ASSERT_TRUE(server_ctx); |
| 2126 |
|
| 2127 |
for (bool take_ownership : {true, false}) { |
| 2128 |
// For simplicity, get the handshake out of the way first. |
| 2129 |
bssl::UniquePtr<SSL> client, server; |
| 2130 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 2131 |
server_ctx.get())); |
| 2132 |
|
| 2133 |
// Wrap |client| in an SSL BIO. |
| 2134 |
bssl::UniquePtr<BIO> client_bio(BIO_new(BIO_f_ssl())); |
| 2135 |
ASSERT_TRUE(client_bio); |
| 2136 |
ASSERT_EQ(1, BIO_set_ssl(client_bio.get(), client.get(), take_ownership)); |
| 2137 |
if (take_ownership) { |
| 2138 |
client.release(); |
| 2139 |
} |
| 2140 |
|
| 2141 |
// Flushing the BIO should not crash. |
| 2142 |
EXPECT_EQ(1, BIO_flush(client_bio.get())); |
| 2143 |
|
| 2144 |
// Exchange some data. |
| 2145 |
EXPECT_EQ(5, BIO_write(client_bio.get(), "hello", 5)); |
| 2146 |
uint8_t buf[5]; |
| 2147 |
ASSERT_EQ(5, SSL_read(server.get(), buf, sizeof(buf))); |
| 2148 |
EXPECT_EQ(Bytes("hello"), Bytes(buf)); |
| 2149 |
|
| 2150 |
EXPECT_EQ(5, SSL_write(server.get(), "world", 5)); |
| 2151 |
ASSERT_EQ(5, BIO_read(client_bio.get(), buf, sizeof(buf))); |
| 2152 |
EXPECT_EQ(Bytes("world"), Bytes(buf)); |
| 2153 |
|
| 2154 |
// |BIO_should_read| should work. |
| 2155 |
EXPECT_EQ(-1, BIO_read(client_bio.get(), buf, sizeof(buf))); |
| 2156 |
EXPECT_TRUE(BIO_should_read(client_bio.get())); |
| 2157 |
|
| 2158 |
// Writing data should eventually exceed the buffer size and fail, reporting |
| 2159 |
// |BIO_should_write|. |
| 2160 |
int ret = 0; |
| 2161 |
for (int i = 0; i < 1024; i++) { |
| 2162 |
const uint8_t kZeros[1024] = {0}; |
| 2163 |
ret = BIO_write(client_bio.get(), kZeros, sizeof(kZeros)); |
| 2164 |
if (ret <= 0) { |
| 2165 |
break; |
| 2166 |
} |
| 2167 |
} |
| 2168 |
EXPECT_EQ(-1, ret); |
| 2169 |
EXPECT_TRUE(BIO_should_write(client_bio.get())); |
| 2170 |
} |
| 2171 |
} |
| 2172 |
|
| 2173 |
#if !defined(OPENSSL_NO_SOCK) |
| 2174 |
TEST(SSLTest, BIO_2) { |
| 2175 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 2176 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 2177 |
CreateContextWithTestCertificate(TLS_method())); |
| 2178 |
ASSERT_TRUE(client_ctx); |
| 2179 |
ASSERT_TRUE(server_ctx); |
| 2180 |
|
| 2181 |
bssl::UniquePtr<BIO> server_bio(BIO_new_ssl(server_ctx.get(), 0)); |
| 2182 |
bssl::UniquePtr<BIO> client_bio(BIO_new_ssl_connect(client_ctx.get())); |
| 2183 |
ASSERT_TRUE(server_bio); |
| 2184 |
ASSERT_TRUE(client_bio); |
| 2185 |
|
| 2186 |
SSL *server_ssl_ptr = nullptr, *client_ssl_ptr = nullptr; |
| 2187 |
ASSERT_TRUE(BIO_get_ssl(server_bio.get(), &server_ssl_ptr)); |
| 2188 |
ASSERT_TRUE(BIO_get_ssl(client_bio.get(), &client_ssl_ptr)); |
| 2189 |
ASSERT_TRUE(server_ssl_ptr); |
| 2190 |
ASSERT_TRUE(client_ssl_ptr); |
| 2191 |
|
| 2192 |
// Client SSL BIOs typically establish connections to a host using |
| 2193 |
// |BIO_do_connect|, which leverages the underlying connect |BIO| for socket |
| 2194 |
// management. While OpenSSL provides |BIO_new_accept| and |BIO_s_accept| for |
| 2195 |
// server-side socket setup, we haven't yet implemented this functionality. |
| 2196 |
// For these tests, we opt for traditional SSL connection methods instead |
| 2197 |
// until we have support for server-side socket management via |BIO|s. |
| 2198 |
// Adding full socket management on the server side would exceed the scope of |
| 2199 |
// testing |BIO_new_ssl(_connect)|, especially since we have dedicated tests |
| 2200 |
// elsewhere that verify |BIO_do_connect|'s correctness. |
| 2201 |
BIO *bio1 = nullptr, *bio2 = nullptr; |
| 2202 |
ASSERT_TRUE(BIO_new_bio_pair(&bio1, 0, &bio2, 0)); |
| 2203 |
SSL_set_bio(client_ssl_ptr, bio1, bio1); |
| 2204 |
SSL_set_bio(server_ssl_ptr, bio2, bio2); |
| 2205 |
ASSERT_TRUE(CompleteHandshakes(client_ssl_ptr, server_ssl_ptr)); |
| 2206 |
} |
| 2207 |
#endif // !OPENSSL_NO_SOCK |
| 2208 |
|
| 2209 |
TEST(SSLTest, ALPNConfig) { |
| 2210 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 2211 |
ASSERT_TRUE(ctx); |
| 2212 |
bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 2213 |
bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 2214 |
ASSERT_TRUE(cert); |
| 2215 |
ASSERT_TRUE(key); |
| 2216 |
ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 2217 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 2218 |
|
| 2219 |
// Set up some machinery to check the configured ALPN against what is actually |
| 2220 |
// sent over the wire. Note that the ALPN callback is only called when the |
| 2221 |
// client offers ALPN. |
| 2222 |
std::vector<uint8_t> observed_alpn; |
| 2223 |
SSL_CTX_set_alpn_select_cb( |
| 2224 |
ctx.get(), |
| 2225 |
[](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in, |
| 2226 |
unsigned in_len, void *arg) -> int { |
| 2227 |
std::vector<uint8_t> *observed_alpn_ptr = |
| 2228 |
static_cast<std::vector<uint8_t> *>(arg); |
| 2229 |
observed_alpn_ptr->assign(in, in + in_len); |
| 2230 |
return SSL_TLSEXT_ERR_NOACK; |
| 2231 |
}, |
| 2232 |
&observed_alpn); |
| 2233 |
auto check_alpn_proto = [&](Span<const uint8_t> expected) { |
| 2234 |
observed_alpn.clear(); |
| 2235 |
bssl::UniquePtr<SSL> client, server; |
| 2236 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 2237 |
EXPECT_EQ(Bytes(expected), Bytes(observed_alpn)); |
| 2238 |
}; |
| 2239 |
|
| 2240 |
// Note that |SSL_CTX_set_alpn_protos|'s return value is reversed. |
| 2241 |
static const uint8_t kValidList[] = {0x03, 'f', 'o', 'o', |
| 2242 |
0x03, 'b', 'a', 'r'}; |
| 2243 |
EXPECT_EQ(0, |
| 2244 |
SSL_CTX_set_alpn_protos(ctx.get(), kValidList, sizeof(kValidList))); |
| 2245 |
check_alpn_proto(kValidList); |
| 2246 |
|
| 2247 |
// Invalid lists are rejected. |
| 2248 |
static const uint8_t kInvalidList[] = {0x04, 'f', 'o', 'o'}; |
| 2249 |
EXPECT_EQ(1, SSL_CTX_set_alpn_protos(ctx.get(), kInvalidList, |
| 2250 |
sizeof(kInvalidList))); |
| 2251 |
|
| 2252 |
// Empty lists are valid and are interpreted as disabling ALPN. |
| 2253 |
EXPECT_EQ(0, SSL_CTX_set_alpn_protos(ctx.get(), nullptr, 0)); |
| 2254 |
check_alpn_proto({}); |
| 2255 |
} |
| 2256 |
|
| 2257 |
// Test that the key usage checker can correctly handle issuerUID and |
| 2258 |
// subjectUID. See https://crbug.com/1199744. |
| 2259 |
TEST(SSLTest, KeyUsageWithUIDs) { |
| 2260 |
static const char kGoodKeyUsage[] = R"( |
| 2261 |
-----BEGIN CERTIFICATE----- |
| 2262 |
MIIB7DCCAZOgAwIBAgIJANlMBNpJfb/rMAoGCCqGSM49BAMCMEUxCzAJBgNVBAYT |
| 2263 |
AkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRn |
| 2264 |
aXRzIFB0eSBMdGQwHhcNMTQwNDIzMjMyMTU3WhcNMTQwNTIzMjMyMTU3WjBFMQsw |
| 2265 |
CQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJu |
| 2266 |
ZXQgV2lkZ2l0cyBQdHkgTHRkMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE5itp |
| 2267 |
4r9ln5e+Lx4NlIpM1Zdrt6keDUb73ampHp3culoB59aXqAoY+cPEox5W4nyDSNsW |
| 2268 |
Ghz1HX7xlC1Lz3IiwYEEABI0VoIEABI0VqNgMF4wHQYDVR0OBBYEFKuE0qyrlfCC |
| 2269 |
ThZ4B1VXX+QmjYLRMB8GA1UdIwQYMBaAFKuE0qyrlfCCThZ4B1VXX+QmjYLRMA4G |
| 2270 |
A1UdDwEB/wQEAwIHgDAMBgNVHRMEBTADAQH/MAoGCCqGSM49BAMCA0cAMEQCIEWJ |
| 2271 |
34EcqW5MHwLIA1hZ2Tj/jV2QjN02KLxis9mFsqDKAiAMlMTkzsM51vVs9Ohqa+Rc |
| 2272 |
4Z7qDhjIhiF4dM0uEDYRVA== |
| 2273 |
-----END CERTIFICATE----- |
| 2274 |
)"; |
| 2275 |
static const char kBadKeyUsage[] = R"( |
| 2276 |
-----BEGIN CERTIFICATE----- |
| 2277 |
MIIB7jCCAZOgAwIBAgIJANlMBNpJfb/rMAoGCCqGSM49BAMCMEUxCzAJBgNVBAYT |
| 2278 |
AkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRn |
| 2279 |
aXRzIFB0eSBMdGQwHhcNMTQwNDIzMjMyMTU3WhcNMTQwNTIzMjMyMTU3WjBFMQsw |
| 2280 |
CQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJu |
| 2281 |
ZXQgV2lkZ2l0cyBQdHkgTHRkMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE5itp |
| 2282 |
4r9ln5e+Lx4NlIpM1Zdrt6keDUb73ampHp3culoB59aXqAoY+cPEox5W4nyDSNsW |
| 2283 |
Ghz1HX7xlC1Lz3IiwYEEABI0VoIEABI0VqNgMF4wHQYDVR0OBBYEFKuE0qyrlfCC |
| 2284 |
ThZ4B1VXX+QmjYLRMB8GA1UdIwQYMBaAFKuE0qyrlfCCThZ4B1VXX+QmjYLRMA4G |
| 2285 |
A1UdDwEB/wQEAwIDCDAMBgNVHRMEBTADAQH/MAoGCCqGSM49BAMCA0kAMEYCIQC6 |
| 2286 |
taYBUDu2gcZC6EMk79FBHArYI0ucF+kzvETegZCbBAIhANtObFec5gtso/47moPD |
| 2287 |
RHrQbWsFUakETXL9QMlegh5t |
| 2288 |
-----END CERTIFICATE----- |
| 2289 |
)"; |
| 2290 |
|
| 2291 |
bssl::UniquePtr<X509> good = CertFromPEM(kGoodKeyUsage); |
| 2292 |
ASSERT_TRUE(good); |
| 2293 |
bssl::UniquePtr<X509> bad = CertFromPEM(kBadKeyUsage); |
| 2294 |
ASSERT_TRUE(bad); |
| 2295 |
|
| 2296 |
// We check key usage when configuring EC certificates to distinguish ECDSA |
| 2297 |
// and ECDH. |
| 2298 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 2299 |
ASSERT_TRUE(ctx); |
| 2300 |
EXPECT_TRUE(SSL_CTX_use_certificate(ctx.get(), good.get())); |
| 2301 |
EXPECT_FALSE(SSL_CTX_use_certificate(ctx.get(), bad.get())); |
| 2302 |
} |
| 2303 |
|
| 2304 |
|
| 2305 |
// Test that |SSL_ERROR_SYSCALL| continues to work after a close_notify. |
| 2306 |
TEST(SSLTest, ErrorSyscallAfterCloseNotify) { |
| 2307 |
// Make a custom |BIO| where writes fail, but without pushing to the error |
| 2308 |
// queue. |
| 2309 |
bssl::UniquePtr<BIO_METHOD> method(BIO_meth_new(0, nullptr)); |
| 2310 |
ASSERT_TRUE(method); |
| 2311 |
BIO_meth_set_create(method.get(), [](BIO *b) -> int { |
| 2312 |
BIO_set_init(b, 1); |
| 2313 |
return 1; |
| 2314 |
}); |
| 2315 |
static bool write_failed = false; |
| 2316 |
BIO_meth_set_write(method.get(), [](BIO *, const char *, int) -> int { |
| 2317 |
// Fail the operation and don't add to the error queue. |
| 2318 |
write_failed = true; |
| 2319 |
return -1; |
| 2320 |
}); |
| 2321 |
bssl::UniquePtr<BIO> wbio_silent_error(BIO_new(method.get())); |
| 2322 |
ASSERT_TRUE(wbio_silent_error); |
| 2323 |
|
| 2324 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 2325 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 2326 |
CreateContextWithTestCertificate(TLS_method()); |
| 2327 |
ASSERT_TRUE(client_ctx); |
| 2328 |
ASSERT_TRUE(server_ctx); |
| 2329 |
bssl::UniquePtr<SSL> client, server; |
| 2330 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 2331 |
server_ctx.get())); |
| 2332 |
|
| 2333 |
// Replace the write |BIO| with |wbio_silent_error|. |
| 2334 |
SSL_set0_wbio(client.get(), wbio_silent_error.release()); |
| 2335 |
|
| 2336 |
// Writes should fail. There is nothing in the error queue, so |
| 2337 |
// |SSL_ERROR_SYSCALL| indicates the caller needs to check out-of-band. |
| 2338 |
const uint8_t data[1] = {0}; |
| 2339 |
int ret = SSL_write(client.get(), data, sizeof(data)); |
| 2340 |
EXPECT_EQ(ret, -1); |
| 2341 |
EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_SYSCALL); |
| 2342 |
EXPECT_TRUE(write_failed); |
| 2343 |
write_failed = false; |
| 2344 |
|
| 2345 |
// Send a close_notify from the server. It should return 0 because |
| 2346 |
// close_notify was sent, but not received. Confusingly, this is a success |
| 2347 |
// output for |SSL_shutdown|'s API. |
| 2348 |
EXPECT_EQ(SSL_shutdown(server.get()), 0); |
| 2349 |
|
| 2350 |
// Read the close_notify on the client. |
| 2351 |
uint8_t buf[1]; |
| 2352 |
ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 2353 |
EXPECT_EQ(ret, 0); |
| 2354 |
EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_ZERO_RETURN); |
| 2355 |
|
| 2356 |
// Further calls to |SSL_read| continue to report |SSL_ERROR_ZERO_RETURN|. |
| 2357 |
ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 2358 |
EXPECT_EQ(ret, 0); |
| 2359 |
EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_ZERO_RETURN); |
| 2360 |
|
| 2361 |
// Although the client has seen close_notify, it should continue to report |
| 2362 |
// |SSL_ERROR_SYSCALL| when its writes fail. |
| 2363 |
ret = SSL_write(client.get(), data, sizeof(data)); |
| 2364 |
EXPECT_EQ(ret, -1); |
| 2365 |
EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_SYSCALL); |
| 2366 |
EXPECT_TRUE(write_failed); |
| 2367 |
write_failed = false; |
| 2368 |
|
| 2369 |
// Cause |BIO_write| to fail with a return value of zero instead. |
| 2370 |
// |SSL_get_error| should not misinterpret this as a close_notify. |
| 2371 |
// |
| 2372 |
// This is not actually a correct implementation of |BIO_write|, but the rest |
| 2373 |
// of the code treats zero from |BIO_write| as an error, so ensure it does so |
| 2374 |
// correctly. Fixing https://crbug.com/boringssl/503 will make this case moot. |
| 2375 |
BIO_meth_set_write(method.get(), [](BIO *, const char *, int) -> int { |
| 2376 |
write_failed = true; |
| 2377 |
return 0; |
| 2378 |
}); |
| 2379 |
ret = SSL_write(client.get(), data, sizeof(data)); |
| 2380 |
EXPECT_EQ(ret, 0); |
| 2381 |
EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_SYSCALL); |
| 2382 |
EXPECT_TRUE(write_failed); |
| 2383 |
write_failed = false; |
| 2384 |
} |
| 2385 |
|
| 2386 |
} // namespace |
| 2387 |
BSSL_NAMESPACE_END |