OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/core/quic_crypto_server_stream.h" | 5 #include "net/quic/core/quic_crypto_server_stream.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "net/quic/core/crypto/aes_128_gcm_12_encrypter.h" | 12 #include "net/quic/core/crypto/aes_128_gcm_12_encrypter.h" |
13 #include "net/quic/core/crypto/crypto_framer.h" | 13 #include "net/quic/core/crypto/crypto_framer.h" |
14 #include "net/quic/core/crypto/crypto_handshake.h" | 14 #include "net/quic/core/crypto/crypto_handshake.h" |
15 #include "net/quic/core/crypto/crypto_protocol.h" | 15 #include "net/quic/core/crypto/crypto_protocol.h" |
16 #include "net/quic/core/crypto/crypto_utils.h" | 16 #include "net/quic/core/crypto/crypto_utils.h" |
17 #include "net/quic/core/crypto/quic_crypto_server_config.h" | 17 #include "net/quic/core/crypto/quic_crypto_server_config.h" |
18 #include "net/quic/core/crypto/quic_decrypter.h" | 18 #include "net/quic/core/crypto/quic_decrypter.h" |
19 #include "net/quic/core/crypto/quic_encrypter.h" | 19 #include "net/quic/core/crypto/quic_encrypter.h" |
20 #include "net/quic/core/crypto/quic_random.h" | 20 #include "net/quic/core/crypto/quic_random.h" |
21 #include "net/quic/core/quic_crypto_client_stream.h" | 21 #include "net/quic/core/quic_crypto_client_stream.h" |
22 #include "net/quic/core/quic_flags.h" | 22 #include "net/quic/core/quic_flags.h" |
23 #include "net/quic/core/quic_protocol.h" | 23 #include "net/quic/core/quic_protocol.h" |
24 #include "net/quic/core/quic_session.h" | 24 #include "net/quic/core/quic_session.h" |
25 #include "net/quic/test_tools/crypto_test_utils.h" | 25 #include "net/quic/test_tools/crypto_test_utils.h" |
26 #include "net/quic/test_tools/delayed_verify_strike_register_client.h" | |
27 #include "net/quic/test_tools/quic_crypto_server_config_peer.h" | 26 #include "net/quic/test_tools/quic_crypto_server_config_peer.h" |
28 #include "net/quic/test_tools/quic_test_utils.h" | 27 #include "net/quic/test_tools/quic_test_utils.h" |
29 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
31 | 30 |
32 namespace net { | 31 namespace net { |
33 class QuicConnection; | 32 class QuicConnection; |
34 class QuicStream; | 33 class QuicStream; |
35 } // namespace net | 34 } // namespace net |
36 | 35 |
(...skipping 24 matching lines...) Expand all Loading... |
61 | 60 |
62 explicit QuicCryptoServerStreamTest(std::unique_ptr<ProofSource> proof_source) | 61 explicit QuicCryptoServerStreamTest(std::unique_ptr<ProofSource> proof_source) |
63 : server_crypto_config_(QuicCryptoServerConfig::TESTING, | 62 : server_crypto_config_(QuicCryptoServerConfig::TESTING, |
64 QuicRandom::GetInstance(), | 63 QuicRandom::GetInstance(), |
65 std::move(proof_source)), | 64 std::move(proof_source)), |
66 server_compressed_certs_cache_( | 65 server_compressed_certs_cache_( |
67 QuicCompressedCertsCache::kQuicCompressedCertsCacheSize), | 66 QuicCompressedCertsCache::kQuicCompressedCertsCacheSize), |
68 server_id_(kServerHostname, kServerPort, PRIVACY_MODE_DISABLED), | 67 server_id_(kServerHostname, kServerPort, PRIVACY_MODE_DISABLED), |
69 client_crypto_config_(CryptoTestUtils::ProofVerifierForTesting()) { | 68 client_crypto_config_(CryptoTestUtils::ProofVerifierForTesting()) { |
70 FLAGS_enable_quic_stateless_reject_support = false; | 69 FLAGS_enable_quic_stateless_reject_support = false; |
71 server_crypto_config_.set_strike_register_no_startup_period(); | |
72 } | 70 } |
73 | 71 |
74 void Initialize() { | 72 void Initialize() { InitializeServer(); } |
75 InitializeServer(); | |
76 | |
77 if (AsyncStrikeRegisterVerification()) { | |
78 QuicCryptoServerConfigPeer peer(&server_crypto_config_); | |
79 strike_register_client_ = new DelayedVerifyStrikeRegisterClient( | |
80 10000, // strike_register_max_entries | |
81 static_cast<uint32_t>( | |
82 server_connection_->clock()->WallNow().ToUNIXSeconds()), | |
83 60, // strike_register_window_secs | |
84 peer.GetPrimaryConfig()->orbit, | |
85 StrikeRegister::NO_STARTUP_PERIOD_NEEDED); | |
86 strike_register_client_->StartDelayingVerification(); | |
87 server_crypto_config_.SetStrikeRegisterClient(strike_register_client_); | |
88 } | |
89 } | |
90 | 73 |
91 ~QuicCryptoServerStreamTest() override { | 74 ~QuicCryptoServerStreamTest() override { |
92 // Ensure that anything that might reference |helpers_| is destroyed before | 75 // Ensure that anything that might reference |helpers_| is destroyed before |
93 // |helpers_| is destroyed. | 76 // |helpers_| is destroyed. |
94 server_session_.reset(); | 77 server_session_.reset(); |
95 client_session_.reset(); | 78 client_session_.reset(); |
96 helpers_.clear(); | 79 helpers_.clear(); |
97 alarm_factories_.clear(); | 80 alarm_factories_.clear(); |
98 } | 81 } |
99 | 82 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 alarm_factories_.push_back(base::MakeUnique<MockAlarmFactory>()); | 116 alarm_factories_.push_back(base::MakeUnique<MockAlarmFactory>()); |
134 CreateClientSessionForTest( | 117 CreateClientSessionForTest( |
135 server_id_, supports_stateless_rejects, | 118 server_id_, supports_stateless_rejects, |
136 QuicTime::Delta::FromSeconds(100000), supported_versions_, | 119 QuicTime::Delta::FromSeconds(100000), supported_versions_, |
137 helpers_.back().get(), alarm_factories_.back().get(), | 120 helpers_.back().get(), alarm_factories_.back().get(), |
138 &client_crypto_config_, &client_connection_, &client_session); | 121 &client_crypto_config_, &client_connection_, &client_session); |
139 CHECK(client_session); | 122 CHECK(client_session); |
140 client_session_.reset(client_session); | 123 client_session_.reset(client_session); |
141 } | 124 } |
142 | 125 |
143 bool AsyncStrikeRegisterVerification() { | |
144 if (server_connection_->version() > QUIC_VERSION_32) { | |
145 return false; | |
146 } | |
147 return GetParam(); | |
148 } | |
149 | |
150 void ConstructHandshakeMessage() { | 126 void ConstructHandshakeMessage() { |
151 CryptoFramer framer; | 127 CryptoFramer framer; |
152 message_data_.reset(framer.ConstructHandshakeMessage(message_)); | 128 message_data_.reset(framer.ConstructHandshakeMessage(message_)); |
153 } | 129 } |
154 | 130 |
155 int CompleteCryptoHandshake() { | 131 int CompleteCryptoHandshake() { |
156 CHECK(server_connection_); | 132 CHECK(server_connection_); |
157 CHECK(server_session_ != nullptr); | 133 CHECK(server_session_ != nullptr); |
158 return CryptoTestUtils::HandshakeWithFakeClient( | 134 return CryptoTestUtils::HandshakeWithFakeClient( |
159 helpers_.back().get(), alarm_factories_.back().get(), | 135 helpers_.back().get(), alarm_factories_.back().get(), |
(...skipping 29 matching lines...) Expand all Loading... |
189 QuicServerId server_id_; | 165 QuicServerId server_id_; |
190 | 166 |
191 // Client state. | 167 // Client state. |
192 PacketSavingConnection* client_connection_; | 168 PacketSavingConnection* client_connection_; |
193 QuicCryptoClientConfig client_crypto_config_; | 169 QuicCryptoClientConfig client_crypto_config_; |
194 std::unique_ptr<TestQuicSpdyClientSession> client_session_; | 170 std::unique_ptr<TestQuicSpdyClientSession> client_session_; |
195 | 171 |
196 CryptoHandshakeMessage message_; | 172 CryptoHandshakeMessage message_; |
197 std::unique_ptr<QuicData> message_data_; | 173 std::unique_ptr<QuicData> message_data_; |
198 CryptoTestUtils::FakeClientOptions client_options_; | 174 CryptoTestUtils::FakeClientOptions client_options_; |
199 DelayedVerifyStrikeRegisterClient* strike_register_client_; | |
200 | 175 |
201 // Which QUIC versions the client and server support. | 176 // Which QUIC versions the client and server support. |
202 QuicVersionVector supported_versions_ = AllSupportedVersions(); | 177 QuicVersionVector supported_versions_ = AllSupportedVersions(); |
203 }; | 178 }; |
204 | 179 |
205 INSTANTIATE_TEST_CASE_P(Tests, QuicCryptoServerStreamTest, testing::Bool()); | 180 INSTANTIATE_TEST_CASE_P(Tests, QuicCryptoServerStreamTest, testing::Bool()); |
206 | 181 |
207 TEST_P(QuicCryptoServerStreamTest, NotInitiallyConected) { | 182 TEST_P(QuicCryptoServerStreamTest, NotInitiallyConected) { |
208 Initialize(); | 183 Initialize(); |
209 EXPECT_FALSE(server_stream()->encryption_established()); | 184 EXPECT_FALSE(server_stream()->encryption_established()); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 server_connection_->random_generator()->RandUint64(); | 282 server_connection_->random_generator()->RandUint64(); |
308 EXPECT_EQ(expected_id, server_designated_connection_id); | 283 EXPECT_EQ(expected_id, server_designated_connection_id); |
309 EXPECT_FALSE(client_state->has_server_designated_connection_id()); | 284 EXPECT_FALSE(client_state->has_server_designated_connection_id()); |
310 ASSERT_TRUE(client_state->IsComplete(QuicWallTime::FromUNIXSeconds(0))); | 285 ASSERT_TRUE(client_state->IsComplete(QuicWallTime::FromUNIXSeconds(0))); |
311 | 286 |
312 // Now create new client and server streams with the existing config | 287 // Now create new client and server streams with the existing config |
313 // and try the handshake again (0-RTT handshake). | 288 // and try the handshake again (0-RTT handshake). |
314 InitializeServer(); | 289 InitializeServer(); |
315 | 290 |
316 InitializeFakeClient(/* supports_stateless_rejects= */ true); | 291 InitializeFakeClient(/* supports_stateless_rejects= */ true); |
317 | |
318 // In the stateless case, the second handshake contains a server-nonce, so the | 292 // In the stateless case, the second handshake contains a server-nonce, so the |
319 // AsyncStrikeRegisterVerification() case will still succeed (unlike a 0-RTT | 293 // AsyncStrikeRegisterVerification() case will still succeed (unlike a 0-RTT |
320 // handshake). | 294 // handshake). |
321 AdvanceHandshakeWithFakeClient(); | 295 AdvanceHandshakeWithFakeClient(); |
322 | 296 |
323 // On the second round, encryption will be established. | 297 // On the second round, encryption will be established. |
324 EXPECT_TRUE(server_stream()->encryption_established()); | 298 EXPECT_TRUE(server_stream()->encryption_established()); |
325 EXPECT_TRUE(server_stream()->handshake_confirmed()); | 299 EXPECT_TRUE(server_stream()->handshake_confirmed()); |
326 EXPECT_EQ(1, server_stream()->NumHandshakeMessages()); | 300 EXPECT_EQ(1, server_stream()->NumHandshakeMessages()); |
327 EXPECT_EQ(1, server_stream()->NumHandshakeMessagesWithServerNonces()); | 301 EXPECT_EQ(1, server_stream()->NumHandshakeMessagesWithServerNonces()); |
(...skipping 30 matching lines...) Expand all Loading... |
358 // information. | 332 // information. |
359 AdvanceHandshakeWithFakeClient(); | 333 AdvanceHandshakeWithFakeClient(); |
360 | 334 |
361 // Now do another handshake, hopefully in 0-RTT. | 335 // Now do another handshake, hopefully in 0-RTT. |
362 DVLOG(1) << "Resetting for 0-RTT handshake attempt"; | 336 DVLOG(1) << "Resetting for 0-RTT handshake attempt"; |
363 InitializeFakeClient(/* supports_stateless_rejects= */ false); | 337 InitializeFakeClient(/* supports_stateless_rejects= */ false); |
364 InitializeServer(); | 338 InitializeServer(); |
365 | 339 |
366 client_stream()->CryptoConnect(); | 340 client_stream()->CryptoConnect(); |
367 | 341 |
368 if (AsyncStrikeRegisterVerification()) { | 342 CryptoTestUtils::CommunicateHandshakeMessages( |
369 EXPECT_FALSE(client_stream()->handshake_confirmed()); | 343 client_connection_, client_stream(), server_connection_, server_stream()); |
370 EXPECT_FALSE(server_stream()->handshake_confirmed()); | |
371 | |
372 // Advance the handshake. Expect that the server will be stuck waiting for | |
373 // client nonce verification to complete. | |
374 std::pair<size_t, size_t> messages_moved = | |
375 CryptoTestUtils::AdvanceHandshake(client_connection_, client_stream(), | |
376 0, server_connection_, | |
377 server_stream(), 0); | |
378 EXPECT_EQ(1u, messages_moved.first); | |
379 EXPECT_EQ(0u, messages_moved.second); | |
380 EXPECT_EQ(1, strike_register_client_->PendingVerifications()); | |
381 EXPECT_FALSE(client_stream()->handshake_confirmed()); | |
382 EXPECT_FALSE(server_stream()->handshake_confirmed()); | |
383 | |
384 // The server handshake completes once the nonce verification completes. | |
385 strike_register_client_->RunPendingVerifications(); | |
386 EXPECT_FALSE(client_stream()->handshake_confirmed()); | |
387 EXPECT_TRUE(server_stream()->handshake_confirmed()); | |
388 | |
389 messages_moved = CryptoTestUtils::AdvanceHandshake( | |
390 client_connection_, client_stream(), messages_moved.first, | |
391 server_connection_, server_stream(), messages_moved.second); | |
392 EXPECT_EQ(1u, messages_moved.first); | |
393 EXPECT_EQ(1u, messages_moved.second); | |
394 EXPECT_TRUE(client_stream()->handshake_confirmed()); | |
395 EXPECT_TRUE(server_stream()->handshake_confirmed()); | |
396 } else { | |
397 CryptoTestUtils::CommunicateHandshakeMessages( | |
398 client_connection_, client_stream(), server_connection_, | |
399 server_stream()); | |
400 } | |
401 | 344 |
402 EXPECT_EQ(1, client_stream()->num_sent_client_hellos()); | 345 EXPECT_EQ(1, client_stream()->num_sent_client_hellos()); |
403 } | 346 } |
404 | 347 |
405 TEST_P(QuicCryptoServerStreamTest, FailByPolicy) { | 348 TEST_P(QuicCryptoServerStreamTest, FailByPolicy) { |
406 Initialize(); | 349 Initialize(); |
407 InitializeFakeClient(/* supports_stateless_rejects= */ false); | 350 InitializeFakeClient(/* supports_stateless_rejects= */ false); |
408 | 351 |
409 EXPECT_CALL(*server_session_->helper(), CanAcceptClientHello(_, _, _)) | 352 EXPECT_CALL(*server_session_->helper(), CanAcceptClientHello(_, _, _)) |
410 .WillOnce(testing::Return(false)); | 353 .WillOnce(testing::Return(false)); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 TEST_P(QuicCryptoServerStreamTest, NoTokenBindingWithoutClientSupport) { | 469 TEST_P(QuicCryptoServerStreamTest, NoTokenBindingWithoutClientSupport) { |
527 Initialize(); | 470 Initialize(); |
528 | 471 |
529 CompleteCryptoHandshake(); | 472 CompleteCryptoHandshake(); |
530 EXPECT_EQ( | 473 EXPECT_EQ( |
531 0u, server_stream()->crypto_negotiated_params().token_binding_key_param); | 474 0u, server_stream()->crypto_negotiated_params().token_binding_key_param); |
532 EXPECT_TRUE(server_stream()->encryption_established()); | 475 EXPECT_TRUE(server_stream()->encryption_established()); |
533 EXPECT_TRUE(server_stream()->handshake_confirmed()); | 476 EXPECT_TRUE(server_stream()->handshake_confirmed()); |
534 } | 477 } |
535 | 478 |
536 TEST_P(QuicCryptoServerStreamTest, CancelRPCBeforeVerificationCompletes) { | |
537 FLAGS_quic_require_handshake_confirmation_pre33 = false; | |
538 // Tests that the client can close the connection while the remote strike | |
539 // register verification RPC is still pending. | |
540 | |
541 // Set version to QUIC_VERSION_32 as QUIC_VERSION_33 and later don't support | |
542 // asynchronous strike register RPCs. | |
543 supported_versions_ = {QUIC_VERSION_32}; | |
544 Initialize(); | |
545 if (!AsyncStrikeRegisterVerification()) { | |
546 return; | |
547 } | |
548 InitializeFakeClient(/* supports_stateless_rejects= */ false); | |
549 | |
550 // Do a first handshake in order to prime the client config with the server's | |
551 // information. | |
552 AdvanceHandshakeWithFakeClient(); | |
553 | |
554 // Now start another handshake, this time the server will attempt to verify | |
555 // the client's nonce with the strike registers. | |
556 InitializeFakeClient(/* supports_stateless_rejects= */ false); | |
557 InitializeServer(); | |
558 client_stream()->CryptoConnect(); | |
559 EXPECT_FALSE(client_stream()->handshake_confirmed()); | |
560 EXPECT_FALSE(server_stream()->handshake_confirmed()); | |
561 | |
562 // Advance the handshake. Expect that the server will be stuck waiting for | |
563 // client nonce verification to complete. | |
564 CryptoTestUtils::AdvanceHandshake(client_connection_, client_stream(), 0, | |
565 server_connection_, server_stream(), 0); | |
566 EXPECT_EQ(1, strike_register_client_->PendingVerifications()); | |
567 EXPECT_FALSE(client_stream()->handshake_confirmed()); | |
568 EXPECT_FALSE(server_stream()->handshake_confirmed()); | |
569 | |
570 // While waiting for the asynchronous verification to complete, the client | |
571 // decides to close the connection. | |
572 server_session_->connection()->CloseConnection( | |
573 QUIC_NO_ERROR, "", ConnectionCloseBehavior::SILENT_CLOSE); | |
574 | |
575 // The outstanding nonce verification RPC now completes. | |
576 strike_register_client_->RunPendingVerifications(); | |
577 } | |
578 | |
579 class FailingProofSource : public ProofSource { | 479 class FailingProofSource : public ProofSource { |
580 public: | 480 public: |
581 bool GetProof(const IPAddress& server_ip, | 481 bool GetProof(const IPAddress& server_ip, |
582 const string& hostname, | 482 const string& hostname, |
583 const string& server_config, | 483 const string& server_config, |
584 QuicVersion quic_version, | 484 QuicVersion quic_version, |
585 StringPiece chlo_hash, | 485 StringPiece chlo_hash, |
586 const QuicTagVector& connection_options, | 486 const QuicTagVector& connection_options, |
587 scoped_refptr<ProofSource::Chain>* out_chain, | 487 scoped_refptr<ProofSource::Chain>* out_chain, |
588 string* out_signature, | 488 QuicCryptoProof* out_proof) override { |
589 string* out_leaf_cert_sct) override { | |
590 return false; | 489 return false; |
591 } | 490 } |
592 | 491 |
593 void GetProof(const IPAddress& server_ip, | 492 void GetProof(const IPAddress& server_ip, |
594 const string& hostname, | 493 const string& hostname, |
595 const string& server_config, | 494 const string& server_config, |
596 QuicVersion quic_version, | 495 QuicVersion quic_version, |
597 StringPiece chlo_hash, | 496 StringPiece chlo_hash, |
598 const QuicTagVector& connection_options, | 497 const QuicTagVector& connection_options, |
599 std::unique_ptr<Callback> callback) override { | 498 std::unique_ptr<Callback> callback) override { |
600 callback->Run(false, nullptr, "", "", nullptr); | 499 callback->Run(false, nullptr, QuicCryptoProof(), nullptr); |
601 } | 500 } |
602 }; | 501 }; |
603 | 502 |
604 class QuicCryptoServerStreamTestWithFailingProofSource | 503 class QuicCryptoServerStreamTestWithFailingProofSource |
605 : public QuicCryptoServerStreamTest { | 504 : public QuicCryptoServerStreamTest { |
606 public: | 505 public: |
607 QuicCryptoServerStreamTestWithFailingProofSource() | 506 QuicCryptoServerStreamTestWithFailingProofSource() |
608 : QuicCryptoServerStreamTest( | 507 : QuicCryptoServerStreamTest( |
609 std::unique_ptr<FailingProofSource>(new FailingProofSource)) {} | 508 std::unique_ptr<FailingProofSource>(new FailingProofSource)) {} |
610 }; | 509 }; |
611 | 510 |
612 INSTANTIATE_TEST_CASE_P(MoreTests, | 511 INSTANTIATE_TEST_CASE_P(MoreTests, |
613 QuicCryptoServerStreamTestWithFailingProofSource, | 512 QuicCryptoServerStreamTestWithFailingProofSource, |
614 testing::Bool()); | 513 testing::Bool()); |
615 | 514 |
616 TEST_P(QuicCryptoServerStreamTestWithFailingProofSource, Test) { | 515 TEST_P(QuicCryptoServerStreamTestWithFailingProofSource, Test) { |
617 Initialize(); | 516 Initialize(); |
618 InitializeFakeClient(/* supports_stateless_rejects= */ false); | 517 InitializeFakeClient(/* supports_stateless_rejects= */ false); |
619 | 518 |
620 // Regression test for b/31521252, in which a crash would happen here. | 519 // Regression test for b/31521252, in which a crash would happen here. |
621 AdvanceHandshakeWithFakeClient(); | 520 AdvanceHandshakeWithFakeClient(); |
622 EXPECT_FALSE(server_stream()->encryption_established()); | 521 EXPECT_FALSE(server_stream()->encryption_established()); |
623 EXPECT_FALSE(server_stream()->handshake_confirmed()); | 522 EXPECT_FALSE(server_stream()->handshake_confirmed()); |
624 } | 523 } |
625 | 524 |
626 } // namespace | 525 } // namespace |
627 | 526 |
628 } // namespace test | 527 } // namespace test |
629 } // namespace net | 528 } // namespace net |
OLD | NEW |