| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <openssl/ssl.h> |
| 5 |
#include "../crypto/test/test_util.h" |
| 6 |
#include "ssl_common_test.h" |
| 7 |
|
| 8 |
BSSL_NAMESPACE_BEGIN |
| 9 |
|
| 10 |
template <typename T> |
| 11 |
class UnownedSSLExData { |
| 12 |
public: |
| 13 |
UnownedSSLExData() { |
| 14 |
index_ = SSL_get_ex_new_index(0, nullptr, nullptr, nullptr, nullptr); |
| 15 |
} |
| 16 |
|
| 17 |
T *Get(const SSL *ssl) { |
| 18 |
return index_ < 0 ? nullptr |
| 19 |
: static_cast<T *>(SSL_get_ex_data(ssl, index_)); |
| 20 |
} |
| 21 |
|
| 22 |
bool Set(SSL *ssl, T *t) { |
| 23 |
return index_ >= 0 && SSL_set_ex_data(ssl, index_, t); |
| 24 |
} |
| 25 |
|
| 26 |
private: |
| 27 |
int index_; |
| 28 |
}; |
| 29 |
|
| 30 |
constexpr size_t kNumQUICLevels = 4; |
| 31 |
static_assert(ssl_encryption_initial < kNumQUICLevels, |
| 32 |
"kNumQUICLevels is wrong"); |
| 33 |
static_assert(ssl_encryption_early_data < kNumQUICLevels, |
| 34 |
"kNumQUICLevels is wrong"); |
| 35 |
static_assert(ssl_encryption_handshake < kNumQUICLevels, |
| 36 |
"kNumQUICLevels is wrong"); |
| 37 |
static_assert(ssl_encryption_application < kNumQUICLevels, |
| 38 |
"kNumQUICLevels is wrong"); |
| 39 |
|
| 40 |
static const char *LevelToString(ssl_encryption_level_t level) { |
| 41 |
switch (level) { |
| 42 |
case ssl_encryption_initial: |
| 43 |
return "initial"; |
| 44 |
case ssl_encryption_early_data: |
| 45 |
return "early data"; |
| 46 |
case ssl_encryption_handshake: |
| 47 |
return "handshake"; |
| 48 |
case ssl_encryption_application: |
| 49 |
return "application"; |
| 50 |
} |
| 51 |
return "<unknown>"; |
| 52 |
} |
| 53 |
|
| 54 |
class MockQUICTransport { |
| 55 |
public: |
| 56 |
enum class Role { kClient, kServer }; |
| 57 |
|
| 58 |
explicit MockQUICTransport(Role role) : role_(role) { |
| 59 |
// The caller is expected to configure initial secrets. |
| 60 |
levels_[ssl_encryption_initial].write_secret = {1}; |
| 61 |
levels_[ssl_encryption_initial].read_secret = {1}; |
| 62 |
} |
| 63 |
|
| 64 |
void set_peer(MockQUICTransport *peer) { peer_ = peer; } |
| 65 |
|
| 66 |
bool has_alert() const { return has_alert_; } |
| 67 |
ssl_encryption_level_t alert_level() const { return alert_level_; } |
| 68 |
uint8_t alert() const { return alert_; } |
| 69 |
|
| 70 |
bool PeerSecretsMatch(ssl_encryption_level_t level) const { |
| 71 |
return levels_[level].write_secret == peer_->levels_[level].read_secret && |
| 72 |
levels_[level].read_secret == peer_->levels_[level].write_secret && |
| 73 |
levels_[level].cipher == peer_->levels_[level].cipher; |
| 74 |
} |
| 75 |
|
| 76 |
bool HasReadSecret(ssl_encryption_level_t level) const { |
| 77 |
return !levels_[level].read_secret.empty(); |
| 78 |
} |
| 79 |
|
| 80 |
bool HasWriteSecret(ssl_encryption_level_t level) const { |
| 81 |
return !levels_[level].write_secret.empty(); |
| 82 |
} |
| 83 |
|
| 84 |
void AllowOutOfOrderWrites() { allow_out_of_order_writes_ = true; } |
| 85 |
|
| 86 |
bool SetReadSecret(ssl_encryption_level_t level, const SSL_CIPHER *cipher, |
| 87 |
Span<const uint8_t> secret) { |
| 88 |
if (HasReadSecret(level)) { |
| 89 |
ADD_FAILURE() << LevelToString(level) << " read secret configured twice"; |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
if (role_ == Role::kClient && level == ssl_encryption_early_data) { |
| 94 |
ADD_FAILURE() << "Unexpected early data read secret"; |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
ssl_encryption_level_t ack_level = |
| 99 |
level == ssl_encryption_early_data ? ssl_encryption_application : level; |
| 100 |
if (!HasWriteSecret(ack_level)) { |
| 101 |
ADD_FAILURE() << LevelToString(level) |
| 102 |
<< " read secret configured before ACK write secret"; |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
if (cipher == nullptr) { |
| 107 |
ADD_FAILURE() << "Unexpected null cipher"; |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
if (level != ssl_encryption_early_data && |
| 112 |
SSL_CIPHER_get_id(cipher) != levels_[level].cipher) { |
| 113 |
ADD_FAILURE() << "Cipher suite inconsistent"; |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
levels_[level].read_secret.assign(secret.begin(), secret.end()); |
| 118 |
levels_[level].cipher = SSL_CIPHER_get_id(cipher); |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
bool SetWriteSecret(ssl_encryption_level_t level, const SSL_CIPHER *cipher, |
| 123 |
Span<const uint8_t> secret) { |
| 124 |
if (HasWriteSecret(level)) { |
| 125 |
ADD_FAILURE() << LevelToString(level) << " write secret configured twice"; |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
if (role_ == Role::kServer && level == ssl_encryption_early_data) { |
| 130 |
ADD_FAILURE() << "Unexpected early data write secret"; |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
if (cipher == nullptr) { |
| 135 |
ADD_FAILURE() << "Unexpected null cipher"; |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
levels_[level].write_secret.assign(secret.begin(), secret.end()); |
| 140 |
levels_[level].cipher = SSL_CIPHER_get_id(cipher); |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
bool WriteHandshakeData(ssl_encryption_level_t level, |
| 145 |
Span<const uint8_t> data) { |
| 146 |
if (levels_[level].write_secret.empty()) { |
| 147 |
ADD_FAILURE() << LevelToString(level) |
| 148 |
<< " write secret not yet configured"; |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
// Although the levels are conceptually separate, BoringSSL finishes writing |
| 153 |
// data from a previous level before installing keys for the next level. |
| 154 |
if (!allow_out_of_order_writes_) { |
| 155 |
switch (level) { |
| 156 |
case ssl_encryption_early_data: |
| 157 |
ADD_FAILURE() << "unexpected handshake data at early data level"; |
| 158 |
return false; |
| 159 |
case ssl_encryption_initial: |
| 160 |
if (!levels_[ssl_encryption_handshake].write_secret.empty()) { |
| 161 |
ADD_FAILURE() |
| 162 |
<< LevelToString(level) |
| 163 |
<< " handshake data written after handshake keys installed"; |
| 164 |
return false; |
| 165 |
} |
| 166 |
OPENSSL_FALLTHROUGH; |
| 167 |
case ssl_encryption_handshake: |
| 168 |
if (!levels_[ssl_encryption_application].write_secret.empty()) { |
| 169 |
ADD_FAILURE() |
| 170 |
<< LevelToString(level) |
| 171 |
<< " handshake data written after application keys installed"; |
| 172 |
return false; |
| 173 |
} |
| 174 |
OPENSSL_FALLTHROUGH; |
| 175 |
case ssl_encryption_application: |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
levels_[level].write_data.insert(levels_[level].write_data.end(), |
| 181 |
data.begin(), data.end()); |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
bool SendAlert(ssl_encryption_level_t level, uint8_t alert_value) { |
| 186 |
if (has_alert_) { |
| 187 |
ADD_FAILURE() << "duplicate alert sent"; |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
if (levels_[level].write_secret.empty()) { |
| 192 |
ADD_FAILURE() << LevelToString(level) |
| 193 |
<< " write secret not yet configured"; |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
has_alert_ = true; |
| 198 |
alert_level_ = level; |
| 199 |
alert_ = alert_value; |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
bool ReadHandshakeData(std::vector<uint8_t> *out, |
| 204 |
ssl_encryption_level_t level, |
| 205 |
size_t num = std::numeric_limits<size_t>::max()) { |
| 206 |
if (levels_[level].read_secret.empty()) { |
| 207 |
ADD_FAILURE() << "data read before keys configured in level " << level; |
| 208 |
return false; |
| 209 |
} |
| 210 |
// The peer may not have configured any keys yet. |
| 211 |
if (peer_->levels_[level].write_secret.empty()) { |
| 212 |
out->clear(); |
| 213 |
return true; |
| 214 |
} |
| 215 |
// Check the peer computed the same key. |
| 216 |
if (peer_->levels_[level].write_secret != levels_[level].read_secret) { |
| 217 |
ADD_FAILURE() << "peer write key does not match read key in level " |
| 218 |
<< level; |
| 219 |
return false; |
| 220 |
} |
| 221 |
if (peer_->levels_[level].cipher != levels_[level].cipher) { |
| 222 |
ADD_FAILURE() << "peer cipher does not match in level " << level; |
| 223 |
return false; |
| 224 |
} |
| 225 |
std::vector<uint8_t> *peer_data = &peer_->levels_[level].write_data; |
| 226 |
num = std::min(num, peer_data->size()); |
| 227 |
out->assign(peer_data->begin(), peer_data->begin() + num); |
| 228 |
peer_data->erase(peer_data->begin(), peer_data->begin() + num); |
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
private: |
| 233 |
Role role_; |
| 234 |
MockQUICTransport *peer_ = nullptr; |
| 235 |
|
| 236 |
bool allow_out_of_order_writes_ = false; |
| 237 |
bool has_alert_ = false; |
| 238 |
ssl_encryption_level_t alert_level_ = ssl_encryption_initial; |
| 239 |
uint8_t alert_ = 0; |
| 240 |
|
| 241 |
struct Level { |
| 242 |
std::vector<uint8_t> write_data; |
| 243 |
std::vector<uint8_t> write_secret; |
| 244 |
std::vector<uint8_t> read_secret; |
| 245 |
uint32_t cipher = 0; |
| 246 |
}; |
| 247 |
Level levels_[kNumQUICLevels]; |
| 248 |
}; |
| 249 |
|
| 250 |
class MockQUICTransportPair { |
| 251 |
public: |
| 252 |
MockQUICTransportPair() |
| 253 |
: client_(MockQUICTransport::Role::kClient), |
| 254 |
server_(MockQUICTransport::Role::kServer) { |
| 255 |
client_.set_peer(&server_); |
| 256 |
server_.set_peer(&client_); |
| 257 |
} |
| 258 |
|
| 259 |
~MockQUICTransportPair() { |
| 260 |
client_.set_peer(nullptr); |
| 261 |
server_.set_peer(nullptr); |
| 262 |
} |
| 263 |
|
| 264 |
MockQUICTransport *client() { return &client_; } |
| 265 |
MockQUICTransport *server() { return &server_; } |
| 266 |
|
| 267 |
bool SecretsMatch(ssl_encryption_level_t level) const { |
| 268 |
// We only need to check |HasReadSecret| and |HasWriteSecret| on |client_|. |
| 269 |
// |PeerSecretsMatch| checks that |server_| is analogously configured. |
| 270 |
return client_.PeerSecretsMatch(level) && client_.HasWriteSecret(level) && |
| 271 |
(level == ssl_encryption_early_data || client_.HasReadSecret(level)); |
| 272 |
} |
| 273 |
|
| 274 |
private: |
| 275 |
MockQUICTransport client_; |
| 276 |
MockQUICTransport server_; |
| 277 |
}; |
| 278 |
|
| 279 |
class QUICMethodTest : public testing::Test { |
| 280 |
protected: |
| 281 |
void SetUp() override { |
| 282 |
client_ctx_.reset(SSL_CTX_new(TLS_method())); |
| 283 |
server_ctx_ = CreateContextWithTestCertificate(TLS_method()); |
| 284 |
ASSERT_TRUE(client_ctx_); |
| 285 |
ASSERT_TRUE(server_ctx_); |
| 286 |
|
| 287 |
SSL_CTX_set_min_proto_version(server_ctx_.get(), TLS1_3_VERSION); |
| 288 |
SSL_CTX_set_max_proto_version(server_ctx_.get(), TLS1_3_VERSION); |
| 289 |
SSL_CTX_set_min_proto_version(client_ctx_.get(), TLS1_3_VERSION); |
| 290 |
SSL_CTX_set_max_proto_version(client_ctx_.get(), TLS1_3_VERSION); |
| 291 |
|
| 292 |
static const uint8_t kALPNProtos[] = {0x03, 'f', 'o', 'o'}; |
| 293 |
ASSERT_EQ(SSL_CTX_set_alpn_protos(client_ctx_.get(), kALPNProtos, |
| 294 |
sizeof(kALPNProtos)), |
| 295 |
0); |
| 296 |
SSL_CTX_set_alpn_select_cb( |
| 297 |
server_ctx_.get(), |
| 298 |
[](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in, |
| 299 |
unsigned in_len, void *arg) -> int { |
| 300 |
return SSL_select_next_proto( |
| 301 |
const_cast<uint8_t **>(out), out_len, in, in_len, |
| 302 |
kALPNProtos, sizeof(kALPNProtos)) == OPENSSL_NPN_NEGOTIATED |
| 303 |
? SSL_TLSEXT_ERR_OK |
| 304 |
: SSL_TLSEXT_ERR_NOACK; |
| 305 |
}, |
| 306 |
nullptr); |
| 307 |
} |
| 308 |
|
| 309 |
static MockQUICTransport *TransportFromSSL(const SSL *ssl) { |
| 310 |
return ex_data_.Get(ssl); |
| 311 |
} |
| 312 |
|
| 313 |
static bool ProvideHandshakeData( |
| 314 |
SSL *ssl, size_t num = std::numeric_limits<size_t>::max()) { |
| 315 |
MockQUICTransport *transport = TransportFromSSL(ssl); |
| 316 |
ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 317 |
std::vector<uint8_t> data; |
| 318 |
return transport->ReadHandshakeData(&data, level, num) && |
| 319 |
SSL_provide_quic_data(ssl, level, data.data(), data.size()); |
| 320 |
} |
| 321 |
|
| 322 |
void AllowOutOfOrderWrites() { allow_out_of_order_writes_ = true; } |
| 323 |
|
| 324 |
bool CreateClientAndServer() { |
| 325 |
client_.reset(SSL_new(client_ctx_.get())); |
| 326 |
server_.reset(SSL_new(server_ctx_.get())); |
| 327 |
if (!client_ || !server_) { |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
SSL_set_connect_state(client_.get()); |
| 332 |
SSL_set_accept_state(server_.get()); |
| 333 |
|
| 334 |
transport_.reset(new MockQUICTransportPair); |
| 335 |
if (!ex_data_.Set(client_.get(), transport_->client()) || |
| 336 |
!ex_data_.Set(server_.get(), transport_->server())) { |
| 337 |
return false; |
| 338 |
} |
| 339 |
if (allow_out_of_order_writes_) { |
| 340 |
transport_->client()->AllowOutOfOrderWrites(); |
| 341 |
transport_->server()->AllowOutOfOrderWrites(); |
| 342 |
} |
| 343 |
static const uint8_t client_transport_params[] = {0}; |
| 344 |
if (!SSL_set_quic_transport_params(client_.get(), client_transport_params, |
| 345 |
sizeof(client_transport_params)) || |
| 346 |
!SSL_set_quic_transport_params(server_.get(), |
| 347 |
server_transport_params_.data(), |
| 348 |
server_transport_params_.size()) || |
| 349 |
!SSL_set_quic_early_data_context( |
| 350 |
server_.get(), server_quic_early_data_context_.data(), |
| 351 |
server_quic_early_data_context_.size())) { |
| 352 |
return false; |
| 353 |
} |
| 354 |
return true; |
| 355 |
} |
| 356 |
|
| 357 |
enum class ExpectedError { |
| 358 |
kNoError, |
| 359 |
kClientError, |
| 360 |
kServerError, |
| 361 |
}; |
| 362 |
|
| 363 |
// CompleteHandshakesForQUIC runs |SSL_do_handshake| on |client_| and |
| 364 |
// |server_| until each completes once. It returns true on success and false |
| 365 |
// on failure. |
| 366 |
bool CompleteHandshakesForQUIC() { |
| 367 |
return RunQUICHandshakesAndExpectError(ExpectedError::kNoError); |
| 368 |
} |
| 369 |
|
| 370 |
// Runs |SSL_do_handshake| on |client_| and |server_| until each completes |
| 371 |
// once. If |expect_client_error| is true, it will return true only if the |
| 372 |
// client handshake failed. Otherwise, it returns true if both handshakes |
| 373 |
// succeed and false otherwise. |
| 374 |
bool RunQUICHandshakesAndExpectError(ExpectedError expected_error) { |
| 375 |
bool client_done = false, server_done = false; |
| 376 |
while (!client_done || !server_done) { |
| 377 |
if (!client_done) { |
| 378 |
if (!ProvideHandshakeData(client_.get())) { |
| 379 |
ADD_FAILURE() << "ProvideHandshakeData(client_) failed"; |
| 380 |
return false; |
| 381 |
} |
| 382 |
int client_ret = SSL_do_handshake(client_.get()); |
| 383 |
int client_err = SSL_get_error(client_.get(), client_ret); |
| 384 |
if (client_ret == 1) { |
| 385 |
client_done = true; |
| 386 |
} else if (client_ret != -1 || client_err != SSL_ERROR_WANT_READ) { |
| 387 |
if (expected_error == ExpectedError::kClientError) { |
| 388 |
return true; |
| 389 |
} |
| 390 |
ADD_FAILURE() << "Unexpected client output: " << client_ret << " " |
| 391 |
<< client_err; |
| 392 |
return false; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
if (!server_done) { |
| 397 |
if (!ProvideHandshakeData(server_.get())) { |
| 398 |
ADD_FAILURE() << "ProvideHandshakeData(server_) failed"; |
| 399 |
return false; |
| 400 |
} |
| 401 |
int server_ret = SSL_do_handshake(server_.get()); |
| 402 |
int server_err = SSL_get_error(server_.get(), server_ret); |
| 403 |
if (server_ret == 1) { |
| 404 |
server_done = true; |
| 405 |
} else if (server_ret != -1 || server_err != SSL_ERROR_WANT_READ) { |
| 406 |
if (expected_error == ExpectedError::kServerError) { |
| 407 |
return true; |
| 408 |
} |
| 409 |
ADD_FAILURE() << "Unexpected server output: " << server_ret << " " |
| 410 |
<< server_err; |
| 411 |
return false; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
return expected_error == ExpectedError::kNoError; |
| 416 |
} |
| 417 |
|
| 418 |
bssl::UniquePtr<SSL_SESSION> CreateClientSessionForQUIC() { |
| 419 |
g_last_session = nullptr; |
| 420 |
SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 421 |
if (!CreateClientAndServer() || !CompleteHandshakesForQUIC()) { |
| 422 |
return nullptr; |
| 423 |
} |
| 424 |
|
| 425 |
// The server sent NewSessionTicket messages in the handshake. |
| 426 |
if (!ProvideHandshakeData(client_.get()) || |
| 427 |
!SSL_process_quic_post_handshake(client_.get())) { |
| 428 |
return nullptr; |
| 429 |
} |
| 430 |
|
| 431 |
return std::move(g_last_session); |
| 432 |
} |
| 433 |
|
| 434 |
void ExpectHandshakeSuccess() { |
| 435 |
EXPECT_TRUE(transport_->SecretsMatch(ssl_encryption_application)); |
| 436 |
EXPECT_EQ(ssl_encryption_application, SSL_quic_read_level(client_.get())); |
| 437 |
EXPECT_EQ(ssl_encryption_application, SSL_quic_write_level(client_.get())); |
| 438 |
EXPECT_EQ(ssl_encryption_application, SSL_quic_read_level(server_.get())); |
| 439 |
EXPECT_EQ(ssl_encryption_application, SSL_quic_write_level(server_.get())); |
| 440 |
EXPECT_FALSE(transport_->client()->has_alert()); |
| 441 |
EXPECT_FALSE(transport_->server()->has_alert()); |
| 442 |
|
| 443 |
// SSL_do_handshake is now idempotent. |
| 444 |
EXPECT_EQ(SSL_do_handshake(client_.get()), 1); |
| 445 |
EXPECT_EQ(SSL_do_handshake(server_.get()), 1); |
| 446 |
} |
| 447 |
|
| 448 |
// Returns a default SSL_QUIC_METHOD. Individual methods may be overwritten by |
| 449 |
// the test. |
| 450 |
SSL_QUIC_METHOD DefaultQUICMethod() { |
| 451 |
return SSL_QUIC_METHOD{ |
| 452 |
SetReadSecretCallback, SetWriteSecretCallback, AddHandshakeDataCallback, |
| 453 |
FlushFlightCallback, SendAlertCallback, |
| 454 |
}; |
| 455 |
} |
| 456 |
|
| 457 |
static int SetReadSecretCallback(SSL *ssl, ssl_encryption_level_t level, |
| 458 |
const SSL_CIPHER *cipher, |
| 459 |
const uint8_t *secret, size_t secret_len) { |
| 460 |
return TransportFromSSL(ssl)->SetReadSecret( |
| 461 |
level, cipher, MakeConstSpan(secret, secret_len)); |
| 462 |
} |
| 463 |
|
| 464 |
static int SetWriteSecretCallback(SSL *ssl, ssl_encryption_level_t level, |
| 465 |
const SSL_CIPHER *cipher, |
| 466 |
const uint8_t *secret, size_t secret_len) { |
| 467 |
return TransportFromSSL(ssl)->SetWriteSecret( |
| 468 |
level, cipher, MakeConstSpan(secret, secret_len)); |
| 469 |
} |
| 470 |
|
| 471 |
static int AddHandshakeDataCallback(SSL *ssl, |
| 472 |
enum ssl_encryption_level_t level, |
| 473 |
const uint8_t *data, size_t len) { |
| 474 |
EXPECT_EQ(level, SSL_quic_write_level(ssl)); |
| 475 |
return TransportFromSSL(ssl)->WriteHandshakeData(level, |
| 476 |
MakeConstSpan(data, len)); |
| 477 |
} |
| 478 |
|
| 479 |
static int FlushFlightCallback(SSL *ssl) { return 1; } |
| 480 |
|
| 481 |
static int SendAlertCallback(SSL *ssl, ssl_encryption_level_t level, |
| 482 |
uint8_t alert) { |
| 483 |
EXPECT_EQ(level, SSL_quic_write_level(ssl)); |
| 484 |
return TransportFromSSL(ssl)->SendAlert(level, alert); |
| 485 |
} |
| 486 |
|
| 487 |
bssl::UniquePtr<SSL_CTX> client_ctx_; |
| 488 |
bssl::UniquePtr<SSL_CTX> server_ctx_; |
| 489 |
|
| 490 |
static UnownedSSLExData<MockQUICTransport> ex_data_; |
| 491 |
std::unique_ptr<MockQUICTransportPair> transport_; |
| 492 |
|
| 493 |
bssl::UniquePtr<SSL> client_; |
| 494 |
bssl::UniquePtr<SSL> server_; |
| 495 |
|
| 496 |
std::vector<uint8_t> server_transport_params_ = {1}; |
| 497 |
std::vector<uint8_t> server_quic_early_data_context_ = {2}; |
| 498 |
|
| 499 |
bool allow_out_of_order_writes_ = false; |
| 500 |
}; |
| 501 |
|
| 502 |
UnownedSSLExData<MockQUICTransport> QUICMethodTest::ex_data_; |
| 503 |
|
| 504 |
// Test a full handshake and resumption work. |
| 505 |
TEST_F(QUICMethodTest, Basic) { |
| 506 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 507 |
|
| 508 |
g_last_session = nullptr; |
| 509 |
|
| 510 |
SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 511 |
SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 512 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 513 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 514 |
|
| 515 |
ASSERT_TRUE(CreateClientAndServer()); |
| 516 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 517 |
|
| 518 |
ExpectHandshakeSuccess(); |
| 519 |
EXPECT_FALSE(SSL_session_reused(client_.get())); |
| 520 |
EXPECT_FALSE(SSL_session_reused(server_.get())); |
| 521 |
|
| 522 |
// The server sent NewSessionTicket messages in the handshake. |
| 523 |
EXPECT_FALSE(g_last_session); |
| 524 |
ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 525 |
EXPECT_EQ(SSL_process_quic_post_handshake(client_.get()), 1); |
| 526 |
EXPECT_TRUE(g_last_session); |
| 527 |
|
| 528 |
// Create a second connection to verify resumption works. |
| 529 |
ASSERT_TRUE(CreateClientAndServer()); |
| 530 |
bssl::UniquePtr<SSL_SESSION> session = std::move(g_last_session); |
| 531 |
SSL_set_session(client_.get(), session.get()); |
| 532 |
|
| 533 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 534 |
|
| 535 |
ExpectHandshakeSuccess(); |
| 536 |
EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 537 |
EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 538 |
} |
| 539 |
|
| 540 |
// Test that HelloRetryRequest in QUIC works. |
| 541 |
TEST_F(QUICMethodTest, HelloRetryRequest) { |
| 542 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 543 |
|
| 544 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 545 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 546 |
|
| 547 |
// BoringSSL predicts the most preferred ECDH group, so using different |
| 548 |
// preferences will trigger HelloRetryRequest. |
| 549 |
static const int kClientPrefs[] = {NID_X25519, NID_X9_62_prime256v1}; |
| 550 |
ASSERT_TRUE(SSL_CTX_set1_groups(client_ctx_.get(), kClientPrefs, |
| 551 |
OPENSSL_ARRAY_SIZE(kClientPrefs))); |
| 552 |
static const int kServerPrefs[] = {NID_X9_62_prime256v1, NID_X25519}; |
| 553 |
ASSERT_TRUE(SSL_CTX_set1_groups(server_ctx_.get(), kServerPrefs, |
| 554 |
OPENSSL_ARRAY_SIZE(kServerPrefs))); |
| 555 |
|
| 556 |
ASSERT_TRUE(CreateClientAndServer()); |
| 557 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 558 |
ExpectHandshakeSuccess(); |
| 559 |
} |
| 560 |
|
| 561 |
// Test that the client does not send a legacy_session_id in the ClientHello. |
| 562 |
TEST_F(QUICMethodTest, NoLegacySessionId) { |
| 563 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 564 |
|
| 565 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 566 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 567 |
// Check that the session ID length is 0 in an early callback. |
| 568 |
SSL_CTX_set_select_certificate_cb( |
| 569 |
server_ctx_.get(), |
| 570 |
[](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 571 |
EXPECT_EQ(client_hello->session_id_len, 0u); |
| 572 |
return ssl_select_cert_success; |
| 573 |
}); |
| 574 |
|
| 575 |
ASSERT_TRUE(CreateClientAndServer()); |
| 576 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 577 |
|
| 578 |
ExpectHandshakeSuccess(); |
| 579 |
} |
| 580 |
|
| 581 |
// Test that, even in a 1-RTT handshake, the server installs keys at the right |
| 582 |
// time. Half-RTT keys are available early, but 1-RTT read keys are deferred. |
| 583 |
TEST_F(QUICMethodTest, HalfRTTKeys) { |
| 584 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 585 |
|
| 586 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 587 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 588 |
ASSERT_TRUE(CreateClientAndServer()); |
| 589 |
|
| 590 |
// The client sends ClientHello. |
| 591 |
ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 592 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client_.get(), -1)); |
| 593 |
|
| 594 |
// The server reads ClientHello and sends ServerHello..Finished. |
| 595 |
ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 596 |
ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 597 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server_.get(), -1)); |
| 598 |
|
| 599 |
// At this point, the server has half-RTT write keys, but it cannot access |
| 600 |
// 1-RTT read keys until client Finished. |
| 601 |
EXPECT_TRUE(transport_->server()->HasWriteSecret(ssl_encryption_application)); |
| 602 |
EXPECT_FALSE(transport_->server()->HasReadSecret(ssl_encryption_application)); |
| 603 |
|
| 604 |
// Finish up the client and server handshakes. |
| 605 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 606 |
|
| 607 |
// Both sides can now exchange 1-RTT data. |
| 608 |
ExpectHandshakeSuccess(); |
| 609 |
} |
| 610 |
|
| 611 |
TEST_F(QUICMethodTest, ZeroRTTAccept) { |
| 612 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 613 |
|
| 614 |
SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 615 |
SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 616 |
SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 617 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 618 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 619 |
|
| 620 |
bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 621 |
ASSERT_TRUE(session); |
| 622 |
|
| 623 |
ASSERT_TRUE(CreateClientAndServer()); |
| 624 |
SSL_set_session(client_.get(), session.get()); |
| 625 |
|
| 626 |
EXPECT_FALSE(SSL_get_client_ciphers(client_.get())); |
| 627 |
EXPECT_FALSE(SSL_get_client_ciphers(server_.get())); |
| 628 |
|
| 629 |
// The client handshake should return immediately into the early data state. |
| 630 |
ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 631 |
EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 632 |
// The transport should have keys for sending 0-RTT data. |
| 633 |
EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 634 |
|
| 635 |
// The server will consume the ClientHello and also enter the early data |
| 636 |
// state. |
| 637 |
ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 638 |
ASSERT_EQ(SSL_do_handshake(server_.get()), 1); |
| 639 |
EXPECT_TRUE(SSL_in_early_data(server_.get())); |
| 640 |
EXPECT_TRUE(transport_->SecretsMatch(ssl_encryption_early_data)); |
| 641 |
// At this point, the server has half-RTT write keys, but it cannot access |
| 642 |
// 1-RTT read keys until client Finished. |
| 643 |
EXPECT_TRUE(transport_->server()->HasWriteSecret(ssl_encryption_application)); |
| 644 |
EXPECT_FALSE(transport_->server()->HasReadSecret(ssl_encryption_application)); |
| 645 |
// The client should still have no view of the server's preferences, but the |
| 646 |
// server should have seen at least one cipher from the client. |
| 647 |
EXPECT_FALSE(SSL_get_client_ciphers(client_.get())); |
| 648 |
EXPECT_GT(sk_SSL_CIPHER_num(SSL_get_client_ciphers(server_.get())), |
| 649 |
(size_t)0); |
| 650 |
|
| 651 |
// Finish up the client and server handshakes. |
| 652 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 653 |
|
| 654 |
// Both sides can now exchange 1-RTT data. |
| 655 |
ExpectHandshakeSuccess(); |
| 656 |
EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 657 |
EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 658 |
EXPECT_FALSE(SSL_in_early_data(client_.get())); |
| 659 |
EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 660 |
EXPECT_TRUE(SSL_early_data_accepted(client_.get())); |
| 661 |
EXPECT_TRUE(SSL_early_data_accepted(server_.get())); |
| 662 |
|
| 663 |
// Finish handling post-handshake messages after the first 0-RTT resumption. |
| 664 |
EXPECT_TRUE(ProvideHandshakeData(client_.get())); |
| 665 |
EXPECT_TRUE(SSL_process_quic_post_handshake(client_.get())); |
| 666 |
|
| 667 |
// Perform a second 0-RTT resumption attempt, and confirm that 0-RTT is |
| 668 |
// accepted again. |
| 669 |
ASSERT_TRUE(CreateClientAndServer()); |
| 670 |
SSL_set_session(client_.get(), g_last_session.get()); |
| 671 |
|
| 672 |
// The client handshake should return immediately into the early data state. |
| 673 |
ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 674 |
EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 675 |
// The transport should have keys for sending 0-RTT data. |
| 676 |
EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 677 |
|
| 678 |
// The server will consume the ClientHello and also enter the early data |
| 679 |
// state. |
| 680 |
ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 681 |
ASSERT_EQ(SSL_do_handshake(server_.get()), 1); |
| 682 |
EXPECT_TRUE(SSL_in_early_data(server_.get())); |
| 683 |
EXPECT_TRUE(transport_->SecretsMatch(ssl_encryption_early_data)); |
| 684 |
// At this point, the server has half-RTT write keys, but it cannot access |
| 685 |
// 1-RTT read keys until client Finished. |
| 686 |
EXPECT_TRUE(transport_->server()->HasWriteSecret(ssl_encryption_application)); |
| 687 |
EXPECT_FALSE(transport_->server()->HasReadSecret(ssl_encryption_application)); |
| 688 |
|
| 689 |
// Finish up the client and server handshakes. |
| 690 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 691 |
|
| 692 |
// Both sides can now exchange 1-RTT data. |
| 693 |
ExpectHandshakeSuccess(); |
| 694 |
EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 695 |
EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 696 |
EXPECT_FALSE(SSL_in_early_data(client_.get())); |
| 697 |
EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 698 |
EXPECT_TRUE(SSL_early_data_accepted(client_.get())); |
| 699 |
EXPECT_TRUE(SSL_early_data_accepted(server_.get())); |
| 700 |
EXPECT_EQ(SSL_get_early_data_reason(client_.get()), ssl_early_data_accepted); |
| 701 |
EXPECT_EQ(SSL_get_early_data_reason(server_.get()), ssl_early_data_accepted); |
| 702 |
} |
| 703 |
|
| 704 |
TEST_F(QUICMethodTest, ZeroRTTRejectMismatchedParameters) { |
| 705 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 706 |
|
| 707 |
SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 708 |
SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 709 |
SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 710 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 711 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 712 |
|
| 713 |
|
| 714 |
bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 715 |
ASSERT_TRUE(session); |
| 716 |
|
| 717 |
ASSERT_TRUE(CreateClientAndServer()); |
| 718 |
static const uint8_t new_context[] = {4}; |
| 719 |
ASSERT_TRUE(SSL_set_quic_early_data_context(server_.get(), new_context, |
| 720 |
sizeof(new_context))); |
| 721 |
SSL_set_session(client_.get(), session.get()); |
| 722 |
|
| 723 |
// The client handshake should return immediately into the early data |
| 724 |
// state. |
| 725 |
ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 726 |
EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 727 |
// The transport should have keys for sending 0-RTT data. |
| 728 |
EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 729 |
|
| 730 |
// The server will consume the ClientHello, but it will not accept 0-RTT. |
| 731 |
ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 732 |
ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 733 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server_.get(), -1)); |
| 734 |
EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 735 |
EXPECT_FALSE(transport_->server()->HasReadSecret(ssl_encryption_early_data)); |
| 736 |
|
| 737 |
// The client consumes the server response and signals 0-RTT rejection. |
| 738 |
for (;;) { |
| 739 |
ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 740 |
ASSERT_EQ(-1, SSL_do_handshake(client_.get())); |
| 741 |
int err = SSL_get_error(client_.get(), -1); |
| 742 |
if (err == SSL_ERROR_EARLY_DATA_REJECTED) { |
| 743 |
break; |
| 744 |
} |
| 745 |
ASSERT_EQ(SSL_ERROR_WANT_READ, err); |
| 746 |
} |
| 747 |
|
| 748 |
// As in TLS over TCP, 0-RTT rejection is sticky. |
| 749 |
ASSERT_EQ(-1, SSL_do_handshake(client_.get())); |
| 750 |
ASSERT_EQ(SSL_ERROR_EARLY_DATA_REJECTED, SSL_get_error(client_.get(), -1)); |
| 751 |
|
| 752 |
// Finish up the client and server handshakes. |
| 753 |
SSL_reset_early_data_reject(client_.get()); |
| 754 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 755 |
|
| 756 |
// Both sides can now exchange 1-RTT data. |
| 757 |
ExpectHandshakeSuccess(); |
| 758 |
EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 759 |
EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 760 |
EXPECT_FALSE(SSL_in_early_data(client_.get())); |
| 761 |
EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 762 |
EXPECT_FALSE(SSL_early_data_accepted(client_.get())); |
| 763 |
EXPECT_FALSE(SSL_early_data_accepted(server_.get())); |
| 764 |
} |
| 765 |
|
| 766 |
TEST_F(QUICMethodTest, NoZeroRTTTicketWithoutEarlyDataContext) { |
| 767 |
server_quic_early_data_context_ = {}; |
| 768 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 769 |
|
| 770 |
SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 771 |
SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 772 |
SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 773 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 774 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 775 |
|
| 776 |
bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 777 |
ASSERT_TRUE(session); |
| 778 |
EXPECT_FALSE(SSL_SESSION_early_data_capable(session.get())); |
| 779 |
} |
| 780 |
|
| 781 |
TEST_F(QUICMethodTest, ZeroRTTReject) { |
| 782 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 783 |
|
| 784 |
SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 785 |
SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 786 |
SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 787 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 788 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 789 |
|
| 790 |
bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 791 |
ASSERT_TRUE(session); |
| 792 |
|
| 793 |
for (bool reject_hrr : {false, true}) { |
| 794 |
SCOPED_TRACE(reject_hrr); |
| 795 |
|
| 796 |
ASSERT_TRUE(CreateClientAndServer()); |
| 797 |
if (reject_hrr) { |
| 798 |
// Configure the server to prefer P-256, which will reject 0-RTT via |
| 799 |
// HelloRetryRequest. |
| 800 |
int p256 = NID_X9_62_prime256v1; |
| 801 |
ASSERT_TRUE(SSL_set1_groups(server_.get(), &p256, 1)); |
| 802 |
} else { |
| 803 |
// Disable 0-RTT on the server, so it will reject it. |
| 804 |
SSL_set_early_data_enabled(server_.get(), 0); |
| 805 |
} |
| 806 |
SSL_set_session(client_.get(), session.get()); |
| 807 |
|
| 808 |
// The client handshake should return immediately into the early data state. |
| 809 |
ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 810 |
EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 811 |
// The transport should have keys for sending 0-RTT data. |
| 812 |
EXPECT_TRUE( |
| 813 |
transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 814 |
|
| 815 |
// The server will consume the ClientHello, but it will not accept 0-RTT. |
| 816 |
ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 817 |
ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 818 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server_.get(), -1)); |
| 819 |
EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 820 |
EXPECT_FALSE( |
| 821 |
transport_->server()->HasReadSecret(ssl_encryption_early_data)); |
| 822 |
|
| 823 |
// The client consumes the server response and signals 0-RTT rejection. |
| 824 |
for (;;) { |
| 825 |
ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 826 |
ASSERT_EQ(-1, SSL_do_handshake(client_.get())); |
| 827 |
int err = SSL_get_error(client_.get(), -1); |
| 828 |
if (err == SSL_ERROR_EARLY_DATA_REJECTED) { |
| 829 |
break; |
| 830 |
} |
| 831 |
ASSERT_EQ(SSL_ERROR_WANT_READ, err); |
| 832 |
} |
| 833 |
|
| 834 |
// As in TLS over TCP, 0-RTT rejection is sticky. |
| 835 |
ASSERT_EQ(-1, SSL_do_handshake(client_.get())); |
| 836 |
ASSERT_EQ(SSL_ERROR_EARLY_DATA_REJECTED, SSL_get_error(client_.get(), -1)); |
| 837 |
|
| 838 |
// Finish up the client and server handshakes. |
| 839 |
SSL_reset_early_data_reject(client_.get()); |
| 840 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 841 |
|
| 842 |
// Both sides can now exchange 1-RTT data. |
| 843 |
ExpectHandshakeSuccess(); |
| 844 |
EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 845 |
EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 846 |
EXPECT_FALSE(SSL_in_early_data(client_.get())); |
| 847 |
EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 848 |
EXPECT_FALSE(SSL_early_data_accepted(client_.get())); |
| 849 |
EXPECT_FALSE(SSL_early_data_accepted(server_.get())); |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
TEST_F(QUICMethodTest, NoZeroRTTKeysBeforeReverify) { |
| 854 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 855 |
|
| 856 |
SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 857 |
SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 858 |
SSL_CTX_set_reverify_on_resume(client_ctx_.get(), 1); |
| 859 |
SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 860 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 861 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 862 |
|
| 863 |
bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 864 |
ASSERT_TRUE(session); |
| 865 |
|
| 866 |
ASSERT_TRUE(CreateClientAndServer()); |
| 867 |
SSL_set_session(client_.get(), session.get()); |
| 868 |
|
| 869 |
// Configure the certificate (re)verification to never complete. The client |
| 870 |
// handshake should pause. |
| 871 |
SSL_set_custom_verify( |
| 872 |
client_.get(), SSL_VERIFY_PEER, |
| 873 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 874 |
return ssl_verify_retry; |
| 875 |
}); |
| 876 |
ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 877 |
ASSERT_EQ(SSL_get_error(client_.get(), -1), |
| 878 |
SSL_ERROR_WANT_CERTIFICATE_VERIFY); |
| 879 |
|
| 880 |
// The early data keys have not yet been released. |
| 881 |
EXPECT_FALSE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 882 |
|
| 883 |
// After the verification completes, the handshake progresses to the 0-RTT |
| 884 |
// point and releases keys. |
| 885 |
SSL_set_custom_verify( |
| 886 |
client_.get(), SSL_VERIFY_PEER, |
| 887 |
[](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 888 |
return ssl_verify_ok; |
| 889 |
}); |
| 890 |
ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 891 |
EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 892 |
EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 893 |
} |
| 894 |
|
| 895 |
// Test only releasing data to QUIC one byte at a time on request, to maximize |
| 896 |
// state machine pauses. Additionally, test that existing asynchronous callbacks |
| 897 |
// still work. |
| 898 |
TEST_F(QUICMethodTest, Async) { |
| 899 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 900 |
|
| 901 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 902 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 903 |
ASSERT_TRUE(CreateClientAndServer()); |
| 904 |
|
| 905 |
// Install an asynchronous certificate callback. |
| 906 |
bool cert_cb_ok = false; |
| 907 |
SSL_set_cert_cb( |
| 908 |
server_.get(), |
| 909 |
[](SSL *, void *arg) -> int { |
| 910 |
return *static_cast<bool *>(arg) ? 1 : -1; |
| 911 |
}, |
| 912 |
&cert_cb_ok); |
| 913 |
|
| 914 |
for (;;) { |
| 915 |
int client_ret = SSL_do_handshake(client_.get()); |
| 916 |
if (client_ret != 1) { |
| 917 |
ASSERT_EQ(client_ret, -1); |
| 918 |
ASSERT_EQ(SSL_get_error(client_.get(), client_ret), SSL_ERROR_WANT_READ); |
| 919 |
ASSERT_TRUE(ProvideHandshakeData(client_.get(), 1)); |
| 920 |
} |
| 921 |
|
| 922 |
int server_ret = SSL_do_handshake(server_.get()); |
| 923 |
if (server_ret != 1) { |
| 924 |
ASSERT_EQ(server_ret, -1); |
| 925 |
int ssl_err = SSL_get_error(server_.get(), server_ret); |
| 926 |
switch (ssl_err) { |
| 927 |
case SSL_ERROR_WANT_READ: |
| 928 |
ASSERT_TRUE(ProvideHandshakeData(server_.get(), 1)); |
| 929 |
break; |
| 930 |
case SSL_ERROR_WANT_X509_LOOKUP: |
| 931 |
ASSERT_FALSE(cert_cb_ok); |
| 932 |
cert_cb_ok = true; |
| 933 |
break; |
| 934 |
default: |
| 935 |
FAIL() << "Unexpected SSL_get_error result: " << ssl_err; |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
if (client_ret == 1 && server_ret == 1) { |
| 940 |
break; |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
ExpectHandshakeSuccess(); |
| 945 |
} |
| 946 |
|
| 947 |
// Test buffering write data until explicit flushes. |
| 948 |
TEST_F(QUICMethodTest, Buffered) { |
| 949 |
AllowOutOfOrderWrites(); |
| 950 |
|
| 951 |
struct BufferedFlight { |
| 952 |
std::vector<uint8_t> data[kNumQUICLevels]; |
| 953 |
}; |
| 954 |
static UnownedSSLExData<BufferedFlight> buffered_flights; |
| 955 |
|
| 956 |
auto add_handshake_data = [](SSL *ssl, enum ssl_encryption_level_t level, |
| 957 |
const uint8_t *data, size_t len) -> int { |
| 958 |
BufferedFlight *flight = buffered_flights.Get(ssl); |
| 959 |
flight->data[level].insert(flight->data[level].end(), data, data + len); |
| 960 |
return 1; |
| 961 |
}; |
| 962 |
|
| 963 |
auto flush_flight = [](SSL *ssl) -> int { |
| 964 |
BufferedFlight *flight = buffered_flights.Get(ssl); |
| 965 |
for (size_t level = 0; level < kNumQUICLevels; level++) { |
| 966 |
if (!flight->data[level].empty()) { |
| 967 |
if (!TransportFromSSL(ssl)->WriteHandshakeData( |
| 968 |
static_cast<ssl_encryption_level_t>(level), |
| 969 |
flight->data[level])) { |
| 970 |
return 0; |
| 971 |
} |
| 972 |
flight->data[level].clear(); |
| 973 |
} |
| 974 |
} |
| 975 |
return 1; |
| 976 |
}; |
| 977 |
|
| 978 |
SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 979 |
quic_method.add_handshake_data = add_handshake_data; |
| 980 |
quic_method.flush_flight = flush_flight; |
| 981 |
|
| 982 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 983 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 984 |
ASSERT_TRUE(CreateClientAndServer()); |
| 985 |
|
| 986 |
BufferedFlight client_flight, server_flight; |
| 987 |
ASSERT_TRUE(buffered_flights.Set(client_.get(), &client_flight)); |
| 988 |
ASSERT_TRUE(buffered_flights.Set(server_.get(), &server_flight)); |
| 989 |
|
| 990 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 991 |
|
| 992 |
ExpectHandshakeSuccess(); |
| 993 |
} |
| 994 |
|
| 995 |
// Test that excess data at one level is rejected. That is, if a single |
| 996 |
// |SSL_provide_quic_data| call included both ServerHello and |
| 997 |
// EncryptedExtensions in a single chunk, BoringSSL notices and rejects this on |
| 998 |
// key change. |
| 999 |
TEST_F(QUICMethodTest, ExcessProvidedData) { |
| 1000 |
AllowOutOfOrderWrites(); |
| 1001 |
|
| 1002 |
auto add_handshake_data = [](SSL *ssl, enum ssl_encryption_level_t level, |
| 1003 |
const uint8_t *data, size_t len) -> int { |
| 1004 |
// Switch everything to the initial level. |
| 1005 |
return TransportFromSSL(ssl)->WriteHandshakeData(ssl_encryption_initial, |
| 1006 |
MakeConstSpan(data, len)); |
| 1007 |
}; |
| 1008 |
|
| 1009 |
SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1010 |
quic_method.add_handshake_data = add_handshake_data; |
| 1011 |
|
| 1012 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1013 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1014 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1015 |
|
| 1016 |
// Send the ClientHello and ServerHello through Finished. |
| 1017 |
ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 1018 |
ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_WANT_READ); |
| 1019 |
ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 1020 |
ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 1021 |
ASSERT_EQ(SSL_get_error(server_.get(), -1), SSL_ERROR_WANT_READ); |
| 1022 |
|
| 1023 |
// The client is still waiting for the ServerHello at initial |
| 1024 |
// encryption. |
| 1025 |
ASSERT_EQ(ssl_encryption_initial, SSL_quic_read_level(client_.get())); |
| 1026 |
|
| 1027 |
// |add_handshake_data| incorrectly wrote everything at the initial level, so |
| 1028 |
// this queues up ServerHello through Finished in one chunk. |
| 1029 |
ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 1030 |
|
| 1031 |
// The client reads ServerHello successfully, but then rejects the buffered |
| 1032 |
// EncryptedExtensions on key change. |
| 1033 |
ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 1034 |
ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_SSL); |
| 1035 |
EXPECT_TRUE( |
| 1036 |
ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_EXCESS_HANDSHAKE_DATA)); |
| 1037 |
|
| 1038 |
// The client sends an alert in response to this. The alert is sent at |
| 1039 |
// handshake level because we install write secrets before read secrets and |
| 1040 |
// the error is discovered when installing the read secret. (How to send |
| 1041 |
// alerts on protocol syntax errors near key changes is ambiguous in general.) |
| 1042 |
ASSERT_TRUE(transport_->client()->has_alert()); |
| 1043 |
EXPECT_EQ(transport_->client()->alert_level(), ssl_encryption_handshake); |
| 1044 |
EXPECT_EQ(transport_->client()->alert(), SSL_AD_UNEXPECTED_MESSAGE); |
| 1045 |
|
| 1046 |
// Sanity-check handshake secrets. The error is discovered while setting the |
| 1047 |
// read secret, so only the write secret has been installed. |
| 1048 |
EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_handshake)); |
| 1049 |
EXPECT_FALSE(transport_->client()->HasReadSecret(ssl_encryption_handshake)); |
| 1050 |
} |
| 1051 |
|
| 1052 |
// Test that |SSL_provide_quic_data| will reject data at the wrong level. |
| 1053 |
TEST_F(QUICMethodTest, ProvideWrongLevel) { |
| 1054 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1055 |
|
| 1056 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1057 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1058 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1059 |
|
| 1060 |
// Send the ClientHello and ServerHello through Finished. |
| 1061 |
ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 1062 |
ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_WANT_READ); |
| 1063 |
ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 1064 |
ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 1065 |
ASSERT_EQ(SSL_get_error(server_.get(), -1), SSL_ERROR_WANT_READ); |
| 1066 |
|
| 1067 |
// The client is still waiting for the ServerHello at initial |
| 1068 |
// encryption. |
| 1069 |
ASSERT_EQ(ssl_encryption_initial, SSL_quic_read_level(client_.get())); |
| 1070 |
|
| 1071 |
// Data cannot be provided at the next level. |
| 1072 |
std::vector<uint8_t> data; |
| 1073 |
ASSERT_TRUE( |
| 1074 |
transport_->client()->ReadHandshakeData(&data, ssl_encryption_initial)); |
| 1075 |
ASSERT_FALSE(SSL_provide_quic_data(client_.get(), ssl_encryption_handshake, |
| 1076 |
data.data(), data.size())); |
| 1077 |
ERR_clear_error(); |
| 1078 |
|
| 1079 |
// Progress to EncryptedExtensions. |
| 1080 |
ASSERT_TRUE(SSL_provide_quic_data(client_.get(), ssl_encryption_initial, |
| 1081 |
data.data(), data.size())); |
| 1082 |
ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 1083 |
ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_WANT_READ); |
| 1084 |
ASSERT_EQ(ssl_encryption_handshake, SSL_quic_read_level(client_.get())); |
| 1085 |
|
| 1086 |
// Data cannot be provided at the previous level. |
| 1087 |
ASSERT_TRUE( |
| 1088 |
transport_->client()->ReadHandshakeData(&data, ssl_encryption_handshake)); |
| 1089 |
ASSERT_FALSE(SSL_provide_quic_data(client_.get(), ssl_encryption_initial, |
| 1090 |
data.data(), data.size())); |
| 1091 |
} |
| 1092 |
|
| 1093 |
TEST_F(QUICMethodTest, TooMuchData) { |
| 1094 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1095 |
|
| 1096 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1097 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1098 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1099 |
|
| 1100 |
size_t limit = |
| 1101 |
SSL_quic_max_handshake_flight_len(client_.get(), ssl_encryption_initial); |
| 1102 |
uint8_t b = 0; |
| 1103 |
for (size_t i = 0; i < limit; i++) { |
| 1104 |
ASSERT_TRUE( |
| 1105 |
SSL_provide_quic_data(client_.get(), ssl_encryption_initial, &b, 1)); |
| 1106 |
} |
| 1107 |
|
| 1108 |
EXPECT_FALSE( |
| 1109 |
SSL_provide_quic_data(client_.get(), ssl_encryption_initial, &b, 1)); |
| 1110 |
} |
| 1111 |
|
| 1112 |
// Provide invalid post-handshake data. |
| 1113 |
TEST_F(QUICMethodTest, BadPostHandshake) { |
| 1114 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1115 |
|
| 1116 |
g_last_session = nullptr; |
| 1117 |
|
| 1118 |
SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 1119 |
SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 1120 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1121 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1122 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1123 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 1124 |
|
| 1125 |
EXPECT_EQ(SSL_do_handshake(client_.get()), 1); |
| 1126 |
EXPECT_EQ(SSL_do_handshake(server_.get()), 1); |
| 1127 |
EXPECT_TRUE(transport_->SecretsMatch(ssl_encryption_application)); |
| 1128 |
EXPECT_FALSE(transport_->client()->has_alert()); |
| 1129 |
EXPECT_FALSE(transport_->server()->has_alert()); |
| 1130 |
|
| 1131 |
// Junk sent as part of post-handshake data should cause an error. |
| 1132 |
uint8_t kJunk[] = {0x17, 0x0, 0x0, 0x4, 0xB, 0xE, 0xE, 0xF}; |
| 1133 |
ASSERT_TRUE(SSL_provide_quic_data(client_.get(), ssl_encryption_application, |
| 1134 |
kJunk, sizeof(kJunk))); |
| 1135 |
EXPECT_EQ(SSL_process_quic_post_handshake(client_.get()), 0); |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
static void ExpectReceivedTransportParamsEqual(const SSL *ssl, |
| 1140 |
Span<const uint8_t> expected) { |
| 1141 |
const uint8_t *received = nullptr; |
| 1142 |
size_t received_len = 0; |
| 1143 |
SSL_get_peer_quic_transport_params(ssl, &received, &received_len); |
| 1144 |
ASSERT_EQ(received_len, expected.size()); |
| 1145 |
EXPECT_EQ(Bytes(received, received_len), Bytes(expected)); |
| 1146 |
} |
| 1147 |
|
| 1148 |
TEST_F(QUICMethodTest, SetTransportParameters) { |
| 1149 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1150 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1151 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1152 |
|
| 1153 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1154 |
uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 1155 |
uint8_t kServerParams[] = {5, 6, 7}; |
| 1156 |
ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 1157 |
sizeof(kClientParams))); |
| 1158 |
ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 1159 |
sizeof(kServerParams))); |
| 1160 |
|
| 1161 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 1162 |
ExpectReceivedTransportParamsEqual(client_.get(), kServerParams); |
| 1163 |
ExpectReceivedTransportParamsEqual(server_.get(), kClientParams); |
| 1164 |
} |
| 1165 |
|
| 1166 |
TEST_F(QUICMethodTest, SetTransportParamsInCallback) { |
| 1167 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1168 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1169 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1170 |
|
| 1171 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1172 |
uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 1173 |
static uint8_t kServerParams[] = {5, 6, 7}; |
| 1174 |
ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 1175 |
sizeof(kClientParams))); |
| 1176 |
SSL_CTX_set_tlsext_servername_callback( |
| 1177 |
server_ctx_.get(), [](SSL *ssl, int *out_alert, void *arg) -> int { |
| 1178 |
EXPECT_TRUE(SSL_set_quic_transport_params(ssl, kServerParams, |
| 1179 |
sizeof(kServerParams))); |
| 1180 |
return SSL_TLSEXT_ERR_OK; |
| 1181 |
}); |
| 1182 |
|
| 1183 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 1184 |
ExpectReceivedTransportParamsEqual(client_.get(), kServerParams); |
| 1185 |
ExpectReceivedTransportParamsEqual(server_.get(), kClientParams); |
| 1186 |
} |
| 1187 |
|
| 1188 |
TEST_F(QUICMethodTest, ForbidCrossProtocolResumptionClient) { |
| 1189 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1190 |
|
| 1191 |
g_last_session = nullptr; |
| 1192 |
|
| 1193 |
SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 1194 |
SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 1195 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1196 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1197 |
|
| 1198 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1199 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 1200 |
|
| 1201 |
ExpectHandshakeSuccess(); |
| 1202 |
EXPECT_FALSE(SSL_session_reused(client_.get())); |
| 1203 |
EXPECT_FALSE(SSL_session_reused(server_.get())); |
| 1204 |
|
| 1205 |
// The server sent NewSessionTicket messages in the handshake. |
| 1206 |
EXPECT_FALSE(g_last_session); |
| 1207 |
ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 1208 |
EXPECT_EQ(SSL_process_quic_post_handshake(client_.get()), 1); |
| 1209 |
ASSERT_TRUE(g_last_session); |
| 1210 |
|
| 1211 |
// Pretend that g_last_session came from a TLS-over-TCP connection. |
| 1212 |
g_last_session->is_quic = false; |
| 1213 |
|
| 1214 |
// Create a second connection and verify that resumption does not occur with |
| 1215 |
// a session from a non-QUIC connection. This tests that the client does not |
| 1216 |
// offer over QUIC a session believed to be received over TCP. The server |
| 1217 |
// believes this is a QUIC session, so if the client offered the session, the |
| 1218 |
// server would have resumed it. |
| 1219 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1220 |
bssl::UniquePtr<SSL_SESSION> session = std::move(g_last_session); |
| 1221 |
SSL_set_session(client_.get(), session.get()); |
| 1222 |
|
| 1223 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 1224 |
ExpectHandshakeSuccess(); |
| 1225 |
EXPECT_FALSE(SSL_session_reused(client_.get())); |
| 1226 |
EXPECT_FALSE(SSL_session_reused(server_.get())); |
| 1227 |
} |
| 1228 |
|
| 1229 |
TEST_F(QUICMethodTest, ForbidCrossProtocolResumptionServer) { |
| 1230 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1231 |
|
| 1232 |
g_last_session = nullptr; |
| 1233 |
|
| 1234 |
SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 1235 |
SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 1236 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1237 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1238 |
|
| 1239 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1240 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 1241 |
|
| 1242 |
ExpectHandshakeSuccess(); |
| 1243 |
EXPECT_FALSE(SSL_session_reused(client_.get())); |
| 1244 |
EXPECT_FALSE(SSL_session_reused(server_.get())); |
| 1245 |
|
| 1246 |
// The server sent NewSessionTicket messages in the handshake. |
| 1247 |
EXPECT_FALSE(g_last_session); |
| 1248 |
ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 1249 |
EXPECT_EQ(SSL_process_quic_post_handshake(client_.get()), 1); |
| 1250 |
ASSERT_TRUE(g_last_session); |
| 1251 |
|
| 1252 |
// Attempt a resumption with g_last_session using TLS_method. |
| 1253 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1254 |
ASSERT_TRUE(client_ctx); |
| 1255 |
|
| 1256 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), nullptr)); |
| 1257 |
|
| 1258 |
bssl::UniquePtr<SSL> client(SSL_new(client_ctx.get())), |
| 1259 |
server(SSL_new(server_ctx_.get())); |
| 1260 |
ASSERT_TRUE(client); |
| 1261 |
ASSERT_TRUE(server); |
| 1262 |
SSL_set_connect_state(client.get()); |
| 1263 |
SSL_set_accept_state(server.get()); |
| 1264 |
|
| 1265 |
// The TLS-over-TCP client will refuse to resume with a quic session, so |
| 1266 |
// mark is_quic = false to bypass the client check to test the server check. |
| 1267 |
g_last_session->is_quic = false; |
| 1268 |
SSL_set_session(client.get(), g_last_session.get()); |
| 1269 |
|
| 1270 |
BIO *bio1 = nullptr, *bio2 = nullptr; |
| 1271 |
ASSERT_TRUE(BIO_new_bio_pair(&bio1, 0, &bio2, 0)); |
| 1272 |
|
| 1273 |
// SSL_set_bio takes ownership. |
| 1274 |
SSL_set_bio(client.get(), bio1, bio1); |
| 1275 |
SSL_set_bio(server.get(), bio2, bio2); |
| 1276 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 1277 |
|
| 1278 |
EXPECT_FALSE(SSL_session_reused(client.get())); |
| 1279 |
EXPECT_FALSE(SSL_session_reused(server.get())); |
| 1280 |
} |
| 1281 |
|
| 1282 |
TEST_F(QUICMethodTest, ClientRejectsMissingTransportParams) { |
| 1283 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1284 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1285 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1286 |
|
| 1287 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1288 |
ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), nullptr, 0)); |
| 1289 |
ASSERT_TRUE(RunQUICHandshakesAndExpectError(ExpectedError::kServerError)); |
| 1290 |
} |
| 1291 |
|
| 1292 |
TEST_F(QUICMethodTest, ServerRejectsMissingTransportParams) { |
| 1293 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1294 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1295 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1296 |
|
| 1297 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1298 |
ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), nullptr, 0)); |
| 1299 |
ASSERT_TRUE(RunQUICHandshakesAndExpectError(ExpectedError::kClientError)); |
| 1300 |
} |
| 1301 |
|
| 1302 |
TEST_F(QUICMethodTest, QuicLegacyCodepointEnabled) { |
| 1303 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1304 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1305 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1306 |
|
| 1307 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1308 |
uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 1309 |
uint8_t kServerParams[] = {5, 6, 7}; |
| 1310 |
SSL_set_quic_use_legacy_codepoint(client_.get(), 1); |
| 1311 |
SSL_set_quic_use_legacy_codepoint(server_.get(), 1); |
| 1312 |
ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 1313 |
sizeof(kClientParams))); |
| 1314 |
ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 1315 |
sizeof(kServerParams))); |
| 1316 |
|
| 1317 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 1318 |
ExpectReceivedTransportParamsEqual(client_.get(), kServerParams); |
| 1319 |
ExpectReceivedTransportParamsEqual(server_.get(), kClientParams); |
| 1320 |
} |
| 1321 |
|
| 1322 |
TEST_F(QUICMethodTest, QuicLegacyCodepointDisabled) { |
| 1323 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1324 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1325 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1326 |
|
| 1327 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1328 |
uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 1329 |
uint8_t kServerParams[] = {5, 6, 7}; |
| 1330 |
SSL_set_quic_use_legacy_codepoint(client_.get(), 0); |
| 1331 |
SSL_set_quic_use_legacy_codepoint(server_.get(), 0); |
| 1332 |
ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 1333 |
sizeof(kClientParams))); |
| 1334 |
ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 1335 |
sizeof(kServerParams))); |
| 1336 |
|
| 1337 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 1338 |
ExpectReceivedTransportParamsEqual(client_.get(), kServerParams); |
| 1339 |
ExpectReceivedTransportParamsEqual(server_.get(), kClientParams); |
| 1340 |
} |
| 1341 |
|
| 1342 |
TEST_F(QUICMethodTest, QuicLegacyCodepointClientOnly) { |
| 1343 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1344 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1345 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1346 |
|
| 1347 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1348 |
uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 1349 |
uint8_t kServerParams[] = {5, 6, 7}; |
| 1350 |
SSL_set_quic_use_legacy_codepoint(client_.get(), 1); |
| 1351 |
SSL_set_quic_use_legacy_codepoint(server_.get(), 0); |
| 1352 |
ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 1353 |
sizeof(kClientParams))); |
| 1354 |
ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 1355 |
sizeof(kServerParams))); |
| 1356 |
|
| 1357 |
ASSERT_TRUE(RunQUICHandshakesAndExpectError(ExpectedError::kServerError)); |
| 1358 |
} |
| 1359 |
|
| 1360 |
TEST_F(QUICMethodTest, QuicLegacyCodepointServerOnly) { |
| 1361 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1362 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1363 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1364 |
|
| 1365 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1366 |
uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 1367 |
uint8_t kServerParams[] = {5, 6, 7}; |
| 1368 |
SSL_set_quic_use_legacy_codepoint(client_.get(), 0); |
| 1369 |
SSL_set_quic_use_legacy_codepoint(server_.get(), 1); |
| 1370 |
ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 1371 |
sizeof(kClientParams))); |
| 1372 |
ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 1373 |
sizeof(kServerParams))); |
| 1374 |
|
| 1375 |
ASSERT_TRUE(RunQUICHandshakesAndExpectError(ExpectedError::kServerError)); |
| 1376 |
} |
| 1377 |
|
| 1378 |
// Test that the default QUIC code point is consistent with |
| 1379 |
// |TLSEXT_TYPE_quic_transport_parameters|. This test ensures we remember to |
| 1380 |
// update the two values together. |
| 1381 |
TEST_F(QUICMethodTest, QuicCodePointDefault) { |
| 1382 |
const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 1383 |
ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 1384 |
ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 1385 |
SSL_CTX_set_select_certificate_cb( |
| 1386 |
server_ctx_.get(), |
| 1387 |
[](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 1388 |
const uint8_t *data = nullptr; |
| 1389 |
size_t len = 0; |
| 1390 |
if (!SSL_early_callback_ctx_extension_get( |
| 1391 |
client_hello, TLSEXT_TYPE_quic_transport_parameters, &data, |
| 1392 |
&len)) { |
| 1393 |
ADD_FAILURE() << "Could not find quic_transport_parameters extension"; |
| 1394 |
return ssl_select_cert_error; |
| 1395 |
} |
| 1396 |
return ssl_select_cert_success; |
| 1397 |
}); |
| 1398 |
|
| 1399 |
ASSERT_TRUE(CreateClientAndServer()); |
| 1400 |
ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
BSSL_NAMESPACE_END |