QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 5 | #include <string> |
| 6 | |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/quic/core/crypto/tls_client_connection.h" |
| 8 | #include "net/third_party/quiche/src/quic/core/crypto/tls_server_connection.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/tls_client_handshaker.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/tls_server_handshaker.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 15 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
| 16 | #include "net/third_party/quiche/src/quic/test_tools/fake_proof_source.h" |
| 17 | #include "net/third_party/quiche/src/quic/test_tools/mock_quic_session_visitor.h" |
| 18 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 19 | |
| 20 | namespace quic { |
| 21 | namespace test { |
| 22 | namespace { |
| 23 | |
| 24 | using ::testing::_; |
| 25 | |
| 26 | class FakeProofVerifier : public ProofVerifier { |
| 27 | public: |
| 28 | FakeProofVerifier() |
| 29 | : verifier_(crypto_test_utils::ProofVerifierForTesting()) {} |
| 30 | |
| 31 | QuicAsyncStatus VerifyProof( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 32 | const std::string& hostname, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | const uint16_t port, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 34 | const std::string& server_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | QuicTransportVersion quic_version, |
| 36 | QuicStringPiece chlo_hash, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 37 | const std::vector<std::string>& certs, |
| 38 | const std::string& cert_sct, |
| 39 | const std::string& signature, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | const ProofVerifyContext* context, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 41 | std::string* error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 42 | std::unique_ptr<ProofVerifyDetails>* details, |
| 43 | std::unique_ptr<ProofVerifierCallback> callback) override { |
| 44 | return verifier_->VerifyProof( |
| 45 | hostname, port, server_config, quic_version, chlo_hash, certs, cert_sct, |
| 46 | signature, context, error_details, details, std::move(callback)); |
| 47 | } |
| 48 | |
| 49 | QuicAsyncStatus VerifyCertChain( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 50 | const std::string& hostname, |
| 51 | const std::vector<std::string>& certs, |
QUICHE team | 38c190b | 2019-05-08 09:12:01 -0700 | [diff] [blame] | 52 | const std::string& ocsp_response, |
| 53 | const std::string& cert_sct, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 54 | const ProofVerifyContext* context, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 55 | std::string* error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | std::unique_ptr<ProofVerifyDetails>* details, |
| 57 | std::unique_ptr<ProofVerifierCallback> callback) override { |
| 58 | if (!active_) { |
QUICHE team | 38c190b | 2019-05-08 09:12:01 -0700 | [diff] [blame] | 59 | return verifier_->VerifyCertChain(hostname, certs, ocsp_response, |
| 60 | cert_sct, context, error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 61 | details, std::move(callback)); |
| 62 | } |
| 63 | pending_ops_.push_back(QuicMakeUnique<VerifyChainPendingOp>( |
QUICHE team | 38c190b | 2019-05-08 09:12:01 -0700 | [diff] [blame] | 64 | hostname, certs, ocsp_response, cert_sct, context, error_details, |
| 65 | details, std::move(callback), verifier_.get())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 66 | return QUIC_PENDING; |
| 67 | } |
| 68 | |
| 69 | std::unique_ptr<ProofVerifyContext> CreateDefaultContext() override { |
| 70 | return nullptr; |
| 71 | } |
| 72 | |
| 73 | void Activate() { active_ = true; } |
| 74 | |
| 75 | size_t NumPendingCallbacks() const { return pending_ops_.size(); } |
| 76 | |
| 77 | void InvokePendingCallback(size_t n) { |
| 78 | CHECK(NumPendingCallbacks() > n); |
| 79 | pending_ops_[n]->Run(); |
| 80 | auto it = pending_ops_.begin() + n; |
| 81 | pending_ops_.erase(it); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | // Implementation of ProofVerifierCallback that fails if the callback is ever |
| 86 | // run. |
| 87 | class FailingProofVerifierCallback : public ProofVerifierCallback { |
| 88 | public: |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 89 | void Run(bool /*ok*/, |
| 90 | const std::string& /*error_details*/, |
| 91 | std::unique_ptr<ProofVerifyDetails>* /*details*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 92 | FAIL(); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | class VerifyChainPendingOp { |
| 97 | public: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 98 | VerifyChainPendingOp(const std::string& hostname, |
| 99 | const std::vector<std::string>& certs, |
QUICHE team | 38c190b | 2019-05-08 09:12:01 -0700 | [diff] [blame] | 100 | const std::string& ocsp_response, |
| 101 | const std::string& cert_sct, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 102 | const ProofVerifyContext* context, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 103 | std::string* error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 104 | std::unique_ptr<ProofVerifyDetails>* details, |
| 105 | std::unique_ptr<ProofVerifierCallback> callback, |
| 106 | ProofVerifier* delegate) |
| 107 | : hostname_(hostname), |
| 108 | certs_(certs), |
QUICHE team | 38c190b | 2019-05-08 09:12:01 -0700 | [diff] [blame] | 109 | ocsp_response_(ocsp_response), |
| 110 | cert_sct_(cert_sct), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 111 | context_(context), |
| 112 | error_details_(error_details), |
| 113 | details_(details), |
| 114 | callback_(std::move(callback)), |
| 115 | delegate_(delegate) {} |
| 116 | |
| 117 | void Run() { |
| 118 | // FakeProofVerifier depends on crypto_test_utils::ProofVerifierForTesting |
| 119 | // running synchronously. It passes a FailingProofVerifierCallback and |
| 120 | // runs the original callback after asserting that the verification ran |
| 121 | // synchronously. |
| 122 | QuicAsyncStatus status = delegate_->VerifyCertChain( |
QUICHE team | 38c190b | 2019-05-08 09:12:01 -0700 | [diff] [blame] | 123 | hostname_, certs_, ocsp_response_, cert_sct_, context_, |
| 124 | error_details_, details_, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 125 | QuicMakeUnique<FailingProofVerifierCallback>()); |
| 126 | ASSERT_NE(status, QUIC_PENDING); |
| 127 | callback_->Run(status == QUIC_SUCCESS, *error_details_, details_); |
| 128 | } |
| 129 | |
| 130 | private: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 131 | std::string hostname_; |
| 132 | std::vector<std::string> certs_; |
QUICHE team | 38c190b | 2019-05-08 09:12:01 -0700 | [diff] [blame] | 133 | std::string ocsp_response_; |
| 134 | std::string cert_sct_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 135 | const ProofVerifyContext* context_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 136 | std::string* error_details_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 137 | std::unique_ptr<ProofVerifyDetails>* details_; |
| 138 | std::unique_ptr<ProofVerifierCallback> callback_; |
| 139 | ProofVerifier* delegate_; |
| 140 | }; |
| 141 | |
| 142 | std::unique_ptr<ProofVerifier> verifier_; |
| 143 | bool active_ = false; |
| 144 | std::vector<std::unique_ptr<VerifyChainPendingOp>> pending_ops_; |
| 145 | }; |
| 146 | |
| 147 | class TestQuicCryptoStream : public QuicCryptoStream { |
| 148 | public: |
| 149 | explicit TestQuicCryptoStream(QuicSession* session) |
| 150 | : QuicCryptoStream(session) {} |
| 151 | |
| 152 | ~TestQuicCryptoStream() override = default; |
| 153 | |
| 154 | virtual TlsHandshaker* handshaker() const = 0; |
| 155 | |
| 156 | bool encryption_established() const override { |
| 157 | return handshaker()->encryption_established(); |
| 158 | } |
| 159 | |
| 160 | bool handshake_confirmed() const override { |
| 161 | return handshaker()->handshake_confirmed(); |
| 162 | } |
| 163 | |
| 164 | const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 165 | const override { |
| 166 | return handshaker()->crypto_negotiated_params(); |
| 167 | } |
| 168 | |
| 169 | CryptoMessageParser* crypto_message_parser() override { |
| 170 | return handshaker()->crypto_message_parser(); |
| 171 | } |
| 172 | |
| 173 | void WriteCryptoData(EncryptionLevel level, QuicStringPiece data) override { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 174 | pending_writes_.push_back(std::make_pair(std::string(data), level)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 175 | } |
| 176 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 177 | const std::vector<std::pair<std::string, EncryptionLevel>>& pending_writes() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 178 | return pending_writes_; |
| 179 | } |
| 180 | |
| 181 | // Sends the pending frames to |stream| and clears the array of pending |
| 182 | // writes. |
| 183 | void SendCryptoMessagesToPeer(QuicCryptoStream* stream) { |
| 184 | QUIC_LOG(INFO) << "Sending " << pending_writes_.size() << " frames"; |
| 185 | // This is a minimal re-implementation of QuicCryptoStream::OnDataAvailable. |
| 186 | // It doesn't work to call QuicStream::OnStreamFrame because |
| 187 | // QuicCryptoStream::OnDataAvailable currently (as an implementation detail) |
| 188 | // relies on the QuicConnection to know the EncryptionLevel to pass into |
| 189 | // CryptoMessageParser::ProcessInput. Since the crypto messages in this test |
| 190 | // never reach the framer or connection and never get encrypted/decrypted, |
| 191 | // QuicCryptoStream::OnDataAvailable isn't able to call ProcessInput with |
| 192 | // the correct EncryptionLevel. Instead, that can be short-circuited by |
| 193 | // directly calling ProcessInput here. |
| 194 | for (size_t i = 0; i < pending_writes_.size(); ++i) { |
| 195 | if (!stream->crypto_message_parser()->ProcessInput( |
| 196 | pending_writes_[i].first, pending_writes_[i].second)) { |
| 197 | CloseConnectionWithDetails( |
| 198 | stream->crypto_message_parser()->error(), |
| 199 | stream->crypto_message_parser()->error_detail()); |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | pending_writes_.clear(); |
| 204 | } |
| 205 | |
| 206 | private: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 207 | std::vector<std::pair<std::string, EncryptionLevel>> pending_writes_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | class TestQuicCryptoClientStream : public TestQuicCryptoStream { |
| 211 | public: |
| 212 | explicit TestQuicCryptoClientStream(QuicSession* session) |
| 213 | : TestQuicCryptoStream(session), |
| 214 | proof_verifier_(new FakeProofVerifier), |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 215 | ssl_ctx_(TlsClientConnection::CreateSslCtx()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 216 | handshaker_(new TlsClientHandshaker( |
| 217 | this, |
| 218 | session, |
| 219 | QuicServerId("test.example.com", 443, false), |
| 220 | proof_verifier_.get(), |
| 221 | ssl_ctx_.get(), |
| 222 | crypto_test_utils::ProofVerifyContextForTesting(), |
| 223 | "quic-tester")) {} |
| 224 | |
| 225 | ~TestQuicCryptoClientStream() override = default; |
| 226 | |
| 227 | TlsHandshaker* handshaker() const override { return handshaker_.get(); } |
| 228 | |
| 229 | bool CryptoConnect() { return handshaker_->CryptoConnect(); } |
| 230 | |
| 231 | FakeProofVerifier* GetFakeProofVerifier() const { |
| 232 | return proof_verifier_.get(); |
| 233 | } |
| 234 | |
| 235 | private: |
| 236 | std::unique_ptr<FakeProofVerifier> proof_verifier_; |
| 237 | bssl::UniquePtr<SSL_CTX> ssl_ctx_; |
| 238 | std::unique_ptr<TlsClientHandshaker> handshaker_; |
| 239 | }; |
| 240 | |
| 241 | class TestQuicCryptoServerStream : public TestQuicCryptoStream { |
| 242 | public: |
| 243 | TestQuicCryptoServerStream(QuicSession* session, |
| 244 | FakeProofSource* proof_source) |
| 245 | : TestQuicCryptoStream(session), |
| 246 | proof_source_(proof_source), |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 247 | ssl_ctx_(TlsServerConnection::CreateSslCtx()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 248 | handshaker_(new TlsServerHandshaker(this, |
| 249 | session, |
| 250 | ssl_ctx_.get(), |
| 251 | proof_source_)) {} |
| 252 | |
| 253 | ~TestQuicCryptoServerStream() override = default; |
| 254 | |
| 255 | void CancelOutstandingCallbacks() { |
| 256 | handshaker_->CancelOutstandingCallbacks(); |
| 257 | } |
| 258 | |
| 259 | TlsHandshaker* handshaker() const override { return handshaker_.get(); } |
| 260 | |
| 261 | FakeProofSource* GetFakeProofSource() const { return proof_source_; } |
| 262 | |
| 263 | private: |
| 264 | FakeProofSource* proof_source_; |
| 265 | bssl::UniquePtr<SSL_CTX> ssl_ctx_; |
| 266 | std::unique_ptr<TlsServerHandshaker> handshaker_; |
| 267 | }; |
| 268 | |
| 269 | void ExchangeHandshakeMessages(TestQuicCryptoStream* client, |
| 270 | TestQuicCryptoStream* server) { |
| 271 | while (!client->pending_writes().empty() || |
| 272 | !server->pending_writes().empty()) { |
| 273 | client->SendCryptoMessagesToPeer(server); |
| 274 | server->SendCryptoMessagesToPeer(client); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | class TlsHandshakerTest : public QuicTest { |
| 279 | public: |
| 280 | TlsHandshakerTest() |
nharper | 107ba5f | 2019-07-02 21:33:39 -0700 | [diff] [blame] | 281 | : client_conn_(new MockQuicConnection( |
| 282 | &conn_helper_, |
| 283 | &alarm_factory_, |
| 284 | Perspective::IS_CLIENT, |
| 285 | {ParsedQuicVersion(PROTOCOL_TLS1_3, QUIC_VERSION_99)})), |
| 286 | server_conn_(new MockQuicConnection( |
| 287 | &conn_helper_, |
| 288 | &alarm_factory_, |
| 289 | Perspective::IS_SERVER, |
| 290 | {ParsedQuicVersion(PROTOCOL_TLS1_3, QUIC_VERSION_99)})), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 291 | client_session_(client_conn_, /*create_mock_crypto_stream=*/false), |
| 292 | server_session_(server_conn_, /*create_mock_crypto_stream=*/false) { |
nharper | 107ba5f | 2019-07-02 21:33:39 -0700 | [diff] [blame] | 293 | SetQuicFlag(FLAGS_quic_supports_tls_handshake, true); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 294 | client_stream_ = new TestQuicCryptoClientStream(&client_session_); |
| 295 | client_session_.SetCryptoStream(client_stream_); |
| 296 | server_stream_ = |
| 297 | new TestQuicCryptoServerStream(&server_session_, &proof_source_); |
| 298 | server_session_.SetCryptoStream(server_stream_); |
| 299 | client_session_.Initialize(); |
| 300 | server_session_.Initialize(); |
| 301 | EXPECT_FALSE(client_stream_->encryption_established()); |
| 302 | EXPECT_FALSE(client_stream_->handshake_confirmed()); |
| 303 | EXPECT_FALSE(server_stream_->encryption_established()); |
| 304 | EXPECT_FALSE(server_stream_->handshake_confirmed()); |
| 305 | } |
| 306 | |
| 307 | MockQuicConnectionHelper conn_helper_; |
| 308 | MockAlarmFactory alarm_factory_; |
| 309 | MockQuicConnection* client_conn_; |
| 310 | MockQuicConnection* server_conn_; |
| 311 | MockQuicSession client_session_; |
| 312 | MockQuicSession server_session_; |
| 313 | |
| 314 | FakeProofSource proof_source_; |
| 315 | TestQuicCryptoClientStream* client_stream_; |
| 316 | TestQuicCryptoServerStream* server_stream_; |
| 317 | }; |
| 318 | |
| 319 | TEST_F(TlsHandshakerTest, CryptoHandshake) { |
| 320 | EXPECT_CALL(*client_conn_, CloseConnection(_, _, _)).Times(0); |
| 321 | EXPECT_CALL(*server_conn_, CloseConnection(_, _, _)).Times(0); |
nharper | 8a72e4f | 2019-08-13 19:17:08 -0700 | [diff] [blame] | 322 | EXPECT_CALL(client_session_, |
renjietang | ea71d6f | 2019-08-19 12:22:28 -0700 | [diff] [blame] | 323 | OnCryptoHandshakeEvent(QuicSession::ENCRYPTION_ESTABLISHED)); |
nharper | 8a72e4f | 2019-08-13 19:17:08 -0700 | [diff] [blame] | 324 | EXPECT_CALL(client_session_, |
| 325 | OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED)); |
| 326 | EXPECT_CALL(server_session_, |
| 327 | OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 328 | client_stream_->CryptoConnect(); |
| 329 | ExchangeHandshakeMessages(client_stream_, server_stream_); |
| 330 | |
| 331 | EXPECT_TRUE(client_stream_->handshake_confirmed()); |
| 332 | EXPECT_TRUE(client_stream_->encryption_established()); |
| 333 | EXPECT_TRUE(server_stream_->handshake_confirmed()); |
| 334 | EXPECT_TRUE(server_stream_->encryption_established()); |
| 335 | } |
| 336 | |
| 337 | TEST_F(TlsHandshakerTest, HandshakeWithAsyncProofSource) { |
| 338 | EXPECT_CALL(*client_conn_, CloseConnection(_, _, _)).Times(0); |
| 339 | EXPECT_CALL(*server_conn_, CloseConnection(_, _, _)).Times(0); |
| 340 | // Enable FakeProofSource to capture call to ComputeTlsSignature and run it |
| 341 | // asynchronously. |
| 342 | FakeProofSource* proof_source = server_stream_->GetFakeProofSource(); |
| 343 | proof_source->Activate(); |
| 344 | |
| 345 | // Start handshake. |
| 346 | client_stream_->CryptoConnect(); |
| 347 | ExchangeHandshakeMessages(client_stream_, server_stream_); |
| 348 | |
| 349 | ASSERT_EQ(proof_source->NumPendingCallbacks(), 1); |
| 350 | proof_source->InvokePendingCallback(0); |
| 351 | |
| 352 | ExchangeHandshakeMessages(client_stream_, server_stream_); |
| 353 | |
| 354 | EXPECT_TRUE(client_stream_->handshake_confirmed()); |
| 355 | EXPECT_TRUE(client_stream_->encryption_established()); |
| 356 | EXPECT_TRUE(server_stream_->handshake_confirmed()); |
| 357 | EXPECT_TRUE(server_stream_->encryption_established()); |
| 358 | } |
| 359 | |
| 360 | TEST_F(TlsHandshakerTest, CancelPendingProofSource) { |
| 361 | EXPECT_CALL(*client_conn_, CloseConnection(_, _, _)).Times(0); |
| 362 | EXPECT_CALL(*server_conn_, CloseConnection(_, _, _)).Times(0); |
| 363 | // Enable FakeProofSource to capture call to ComputeTlsSignature and run it |
| 364 | // asynchronously. |
| 365 | FakeProofSource* proof_source = server_stream_->GetFakeProofSource(); |
| 366 | proof_source->Activate(); |
| 367 | |
| 368 | // Start handshake. |
| 369 | client_stream_->CryptoConnect(); |
| 370 | ExchangeHandshakeMessages(client_stream_, server_stream_); |
| 371 | |
| 372 | ASSERT_EQ(proof_source->NumPendingCallbacks(), 1); |
| 373 | server_stream_ = nullptr; |
| 374 | |
| 375 | proof_source->InvokePendingCallback(0); |
| 376 | } |
| 377 | |
| 378 | TEST_F(TlsHandshakerTest, HandshakeWithAsyncProofVerifier) { |
| 379 | EXPECT_CALL(*client_conn_, CloseConnection(_, _, _)).Times(0); |
| 380 | EXPECT_CALL(*server_conn_, CloseConnection(_, _, _)).Times(0); |
| 381 | // Enable FakeProofVerifier to capture call to VerifyCertChain and run it |
| 382 | // asynchronously. |
| 383 | FakeProofVerifier* proof_verifier = client_stream_->GetFakeProofVerifier(); |
| 384 | proof_verifier->Activate(); |
| 385 | |
| 386 | // Start handshake. |
| 387 | client_stream_->CryptoConnect(); |
| 388 | ExchangeHandshakeMessages(client_stream_, server_stream_); |
| 389 | |
| 390 | ASSERT_EQ(proof_verifier->NumPendingCallbacks(), 1u); |
| 391 | proof_verifier->InvokePendingCallback(0); |
| 392 | |
| 393 | ExchangeHandshakeMessages(client_stream_, server_stream_); |
| 394 | |
| 395 | EXPECT_TRUE(client_stream_->handshake_confirmed()); |
| 396 | EXPECT_TRUE(client_stream_->encryption_established()); |
| 397 | EXPECT_TRUE(server_stream_->handshake_confirmed()); |
| 398 | EXPECT_TRUE(server_stream_->encryption_established()); |
| 399 | } |
| 400 | |
| 401 | TEST_F(TlsHandshakerTest, ClientConnectionClosedOnTlsError) { |
| 402 | // Have client send ClientHello. |
| 403 | client_stream_->CryptoConnect(); |
| 404 | EXPECT_CALL(*client_conn_, CloseConnection(QUIC_HANDSHAKE_FAILED, _, _)); |
| 405 | |
| 406 | // Send a zero-length ServerHello from server to client. |
| 407 | char bogus_handshake_message[] = { |
| 408 | // Handshake struct (RFC 8446 appendix B.3) |
| 409 | 2, // HandshakeType server_hello |
| 410 | 0, 0, 0, // uint24 length |
| 411 | }; |
| 412 | server_stream_->WriteCryptoData( |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 413 | ENCRYPTION_INITIAL, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 414 | QuicStringPiece(bogus_handshake_message, |
| 415 | QUIC_ARRAYSIZE(bogus_handshake_message))); |
| 416 | server_stream_->SendCryptoMessagesToPeer(client_stream_); |
| 417 | |
| 418 | EXPECT_FALSE(client_stream_->handshake_confirmed()); |
| 419 | } |
| 420 | |
| 421 | TEST_F(TlsHandshakerTest, ServerConnectionClosedOnTlsError) { |
| 422 | EXPECT_CALL(*server_conn_, CloseConnection(QUIC_HANDSHAKE_FAILED, _, _)); |
| 423 | |
| 424 | // Send a zero-length ClientHello from client to server. |
| 425 | char bogus_handshake_message[] = { |
| 426 | // Handshake struct (RFC 8446 appendix B.3) |
| 427 | 1, // HandshakeType client_hello |
| 428 | 0, 0, 0, // uint24 length |
| 429 | }; |
| 430 | client_stream_->WriteCryptoData( |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 431 | ENCRYPTION_INITIAL, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 432 | QuicStringPiece(bogus_handshake_message, |
| 433 | QUIC_ARRAYSIZE(bogus_handshake_message))); |
| 434 | client_stream_->SendCryptoMessagesToPeer(server_stream_); |
| 435 | |
| 436 | EXPECT_FALSE(server_stream_->handshake_confirmed()); |
| 437 | } |
| 438 | |
dschinazi | 9145364 | 2019-08-01 11:12:15 -0700 | [diff] [blame] | 439 | TEST_F(TlsHandshakerTest, ClientNotSendingALPN) { |
| 440 | static std::string kTestClientNoAlpn = ""; |
| 441 | quic_alpn_override_on_client_for_tests = &kTestClientNoAlpn; |
| 442 | EXPECT_CALL(*client_conn_, CloseConnection(QUIC_HANDSHAKE_FAILED, |
| 443 | "Server did not select ALPN", _)); |
| 444 | EXPECT_CALL(*server_conn_, |
| 445 | CloseConnection(QUIC_HANDSHAKE_FAILED, |
| 446 | "Server did not receive a known ALPN", _)); |
| 447 | client_stream_->CryptoConnect(); |
| 448 | ExchangeHandshakeMessages(client_stream_, server_stream_); |
| 449 | |
| 450 | EXPECT_FALSE(client_stream_->handshake_confirmed()); |
| 451 | EXPECT_FALSE(client_stream_->encryption_established()); |
| 452 | EXPECT_FALSE(server_stream_->handshake_confirmed()); |
| 453 | EXPECT_FALSE(server_stream_->encryption_established()); |
| 454 | quic_alpn_override_on_client_for_tests = nullptr; |
| 455 | } |
| 456 | |
| 457 | TEST_F(TlsHandshakerTest, ClientSendingBadALPN) { |
| 458 | static std::string kTestBadClientAlpn = "bad-client-alpn"; |
| 459 | quic_alpn_override_on_client_for_tests = &kTestBadClientAlpn; |
| 460 | EXPECT_CALL(*client_conn_, CloseConnection(QUIC_HANDSHAKE_FAILED, |
| 461 | "Server did not select ALPN", _)); |
| 462 | EXPECT_CALL(*server_conn_, |
| 463 | CloseConnection(QUIC_HANDSHAKE_FAILED, |
| 464 | "Server did not receive a known ALPN", _)); |
| 465 | client_stream_->CryptoConnect(); |
| 466 | ExchangeHandshakeMessages(client_stream_, server_stream_); |
| 467 | |
| 468 | EXPECT_FALSE(client_stream_->handshake_confirmed()); |
| 469 | EXPECT_FALSE(client_stream_->encryption_established()); |
| 470 | EXPECT_FALSE(server_stream_->handshake_confirmed()); |
| 471 | EXPECT_FALSE(server_stream_->encryption_established()); |
| 472 | quic_alpn_override_on_client_for_tests = nullptr; |
| 473 | } |
| 474 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 475 | } // namespace |
| 476 | } // namespace test |
| 477 | } // namespace quic |