| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/crypto/quic_crypto_server_config.h" | 5 #include "net/quic/crypto/quic_crypto_server_config.h" |
| 6 | 6 |
| 7 #include <stdarg.h> | 7 #include <stdarg.h> |
| 8 | 8 |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" | 10 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" |
| 11 #include "net/quic/crypto/crypto_handshake_message.h" | 11 #include "net/quic/crypto/crypto_handshake_message.h" |
| 12 #include "net/quic/crypto/crypto_secret_boxer.h" | 12 #include "net/quic/crypto/crypto_secret_boxer.h" |
| 13 #include "net/quic/crypto/crypto_server_config_protobuf.h" | 13 #include "net/quic/crypto/crypto_server_config_protobuf.h" |
| 14 #include "net/quic/crypto/quic_random.h" | 14 #include "net/quic/crypto/quic_random.h" |
| 15 #include "net/quic/crypto/strike_register_client.h" | 15 #include "net/quic/crypto/strike_register_client.h" |
| 16 #include "net/quic/quic_flags.h" | 16 #include "net/quic/quic_flags.h" |
| 17 #include "net/quic/quic_time.h" | 17 #include "net/quic/quic_time.h" |
| 18 #include "net/quic/test_tools/crypto_test_utils.h" |
| 18 #include "net/quic/test_tools/mock_clock.h" | 19 #include "net/quic/test_tools/mock_clock.h" |
| 19 #include "net/quic/test_tools/quic_test_utils.h" | 20 #include "net/quic/test_tools/quic_test_utils.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using base::StringPiece; | 24 using base::StringPiece; |
| 24 using std::map; | 25 using std::map; |
| 25 using std::pair; | 26 using std::pair; |
| 26 using std::string; | 27 using std::string; |
| 27 using std::vector; | 28 using std::vector; |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 | 212 |
| 212 bool is_known_orbit_called() { return is_known_orbit_called_; } | 213 bool is_known_orbit_called() { return is_known_orbit_called_; } |
| 213 | 214 |
| 214 private: | 215 private: |
| 215 QuicCryptoServerConfig* config_; | 216 QuicCryptoServerConfig* config_; |
| 216 mutable bool is_known_orbit_called_; | 217 mutable bool is_known_orbit_called_; |
| 217 }; | 218 }; |
| 218 | 219 |
| 219 TEST(QuicCryptoServerConfigTest, ServerConfig) { | 220 TEST(QuicCryptoServerConfigTest, ServerConfig) { |
| 220 QuicRandom* rand = QuicRandom::GetInstance(); | 221 QuicRandom* rand = QuicRandom::GetInstance(); |
| 221 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand); | 222 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand, |
| 223 #if defined(USE_OPENSSL) |
| 224 CryptoTestUtils::ProofSourceForTesting()); |
| 225 #else |
| 226 CryptoTestUtils::FakeProofSourceForTesting()); |
| 227 #endif |
| 222 MockClock clock; | 228 MockClock clock; |
| 223 | 229 |
| 224 scoped_ptr<CryptoHandshakeMessage>( | 230 scoped_ptr<CryptoHandshakeMessage>( |
| 225 server.AddDefaultConfig(rand, &clock, | 231 server.AddDefaultConfig(rand, &clock, |
| 226 QuicCryptoServerConfig::ConfigOptions())); | 232 QuicCryptoServerConfig::ConfigOptions())); |
| 227 } | 233 } |
| 228 | 234 |
| 229 TEST(QuicCryptoServerConfigTest, GetOrbitIsCalledWithoutTheStrikeRegisterLock) { | 235 TEST(QuicCryptoServerConfigTest, GetOrbitIsCalledWithoutTheStrikeRegisterLock) { |
| 230 QuicRandom* rand = QuicRandom::GetInstance(); | 236 QuicRandom* rand = QuicRandom::GetInstance(); |
| 231 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand); | 237 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand, |
| 238 #if defined(USE_OPENSSL) |
| 239 CryptoTestUtils::ProofSourceForTesting()); |
| 240 #else |
| 241 CryptoTestUtils::FakeProofSourceForTesting()); |
| 242 #endif |
| 232 MockClock clock; | 243 MockClock clock; |
| 233 | 244 |
| 234 TestStrikeRegisterClient* strike_register = | 245 TestStrikeRegisterClient* strike_register = |
| 235 new TestStrikeRegisterClient(&server); | 246 new TestStrikeRegisterClient(&server); |
| 236 server.SetStrikeRegisterClient(strike_register); | 247 server.SetStrikeRegisterClient(strike_register); |
| 237 | 248 |
| 238 QuicCryptoServerConfig::ConfigOptions options; | 249 QuicCryptoServerConfig::ConfigOptions options; |
| 239 scoped_ptr<CryptoHandshakeMessage> message( | 250 scoped_ptr<CryptoHandshakeMessage> message( |
| 240 server.AddDefaultConfig(rand, &clock, options)); | 251 server.AddDefaultConfig(rand, &clock, options)); |
| 241 EXPECT_TRUE(strike_register->is_known_orbit_called()); | 252 EXPECT_TRUE(strike_register->is_known_orbit_called()); |
| 242 } | 253 } |
| 243 | 254 |
| 244 class SourceAddressTokenTest : public ::testing::Test { | 255 class SourceAddressTokenTest : public ::testing::Test { |
| 245 public: | 256 public: |
| 246 SourceAddressTokenTest() | 257 SourceAddressTokenTest() |
| 247 : ip4_(Loopback4()), | 258 : ip4_(Loopback4()), |
| 248 ip4_dual_(ConvertIPv4NumberToIPv6Number(ip4_)), | 259 ip4_dual_(ConvertIPv4NumberToIPv6Number(ip4_)), |
| 249 ip6_(Loopback6()), | 260 ip6_(Loopback6()), |
| 250 original_time_(QuicWallTime::Zero()), | 261 original_time_(QuicWallTime::Zero()), |
| 251 rand_(QuicRandom::GetInstance()), | 262 rand_(QuicRandom::GetInstance()), |
| 252 server_(QuicCryptoServerConfig::TESTING, rand_), | 263 server_(QuicCryptoServerConfig::TESTING, |
| 264 rand_, |
| 265 #if defined(USE_OPENSSL) |
| 266 CryptoTestUtils::ProofSourceForTesting()), |
| 267 #else |
| 268 CryptoTestUtils::FakeProofSourceForTesting()), |
| 269 #endif |
| 253 peer_(&server_) { | 270 peer_(&server_) { |
| 254 // Advance the clock to some non-zero time. | 271 // Advance the clock to some non-zero time. |
| 255 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1000000)); | 272 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1000000)); |
| 256 original_time_ = clock_.WallNow(); | 273 original_time_ = clock_.WallNow(); |
| 257 | 274 |
| 258 primary_config_.reset(server_.AddDefaultConfig( | 275 primary_config_.reset(server_.AddDefaultConfig( |
| 259 rand_, &clock_, QuicCryptoServerConfig::ConfigOptions())); | 276 rand_, &clock_, QuicCryptoServerConfig::ConfigOptions())); |
| 260 | 277 |
| 261 // Add a config that overrides the default boxer. | 278 // Add a config that overrides the default boxer. |
| 262 QuicCryptoServerConfig::ConfigOptions options; | 279 QuicCryptoServerConfig::ConfigOptions options; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 NewSourceAddressToken(kPrimary, ip4_, previous_tokens); | 432 NewSourceAddressToken(kPrimary, ip4_, previous_tokens); |
| 416 | 433 |
| 417 EXPECT_EQ(HANDSHAKE_OK, | 434 EXPECT_EQ(HANDSHAKE_OK, |
| 418 ValidateSourceAddressTokens(kPrimary, token4or6, ip4_)); | 435 ValidateSourceAddressTokens(kPrimary, token4or6, ip4_)); |
| 419 ASSERT_EQ(HANDSHAKE_OK, | 436 ASSERT_EQ(HANDSHAKE_OK, |
| 420 ValidateSourceAddressTokens(kPrimary, token4or6, ip6_)); | 437 ValidateSourceAddressTokens(kPrimary, token4or6, ip6_)); |
| 421 } | 438 } |
| 422 | 439 |
| 423 TEST(QuicCryptoServerConfigTest, ValidateServerNonce) { | 440 TEST(QuicCryptoServerConfigTest, ValidateServerNonce) { |
| 424 QuicRandom* rand = QuicRandom::GetInstance(); | 441 QuicRandom* rand = QuicRandom::GetInstance(); |
| 425 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand); | 442 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand, |
| 443 #if defined(USE_OPENSSL) |
| 444 CryptoTestUtils::ProofSourceForTesting()); |
| 445 #else |
| 446 CryptoTestUtils::FakeProofSourceForTesting()); |
| 447 #endif |
| 426 QuicCryptoServerConfigPeer peer(&server); | 448 QuicCryptoServerConfigPeer peer(&server); |
| 427 | 449 |
| 428 StringPiece message("hello world"); | 450 StringPiece message("hello world"); |
| 429 const size_t key_size = CryptoSecretBoxer::GetKeySize(); | 451 const size_t key_size = CryptoSecretBoxer::GetKeySize(); |
| 430 scoped_ptr<uint8[]> key(new uint8[key_size]); | 452 scoped_ptr<uint8[]> key(new uint8[key_size]); |
| 431 memset(key.get(), 0x11, key_size); | 453 memset(key.get(), 0x11, key_size); |
| 432 | 454 |
| 433 CryptoSecretBoxer boxer; | 455 CryptoSecretBoxer boxer; |
| 434 boxer.SetKey(StringPiece(reinterpret_cast<char*>(key.get()), key_size)); | 456 boxer.SetKey(StringPiece(reinterpret_cast<char*>(key.get()), key_size)); |
| 435 const string box = boxer.Box(rand, message); | 457 const string box = boxer.Box(rand, message); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 446 | 468 |
| 447 now = original_time.Add(QuicTime::Delta::FromSeconds(1000 * 7)); | 469 now = original_time.Add(QuicTime::Delta::FromSeconds(1000 * 7)); |
| 448 server_nonce = peer.NewServerNonce(rand, now); | 470 server_nonce = peer.NewServerNonce(rand, now); |
| 449 EXPECT_EQ(HANDSHAKE_OK, peer.ValidateServerNonce(server_nonce, now)); | 471 EXPECT_EQ(HANDSHAKE_OK, peer.ValidateServerNonce(server_nonce, now)); |
| 450 } | 472 } |
| 451 | 473 |
| 452 class CryptoServerConfigsTest : public ::testing::Test { | 474 class CryptoServerConfigsTest : public ::testing::Test { |
| 453 public: | 475 public: |
| 454 CryptoServerConfigsTest() | 476 CryptoServerConfigsTest() |
| 455 : rand_(QuicRandom::GetInstance()), | 477 : rand_(QuicRandom::GetInstance()), |
| 456 config_(QuicCryptoServerConfig::TESTING, rand_), | 478 config_(QuicCryptoServerConfig::TESTING, |
| 457 test_peer_(&config_) {} | 479 rand_, |
| 480 #if defined(USE_OPENSSL) |
| 481 CryptoTestUtils::ProofSourceForTesting()), |
| 482 #else |
| 483 CryptoTestUtils::FakeProofSourceForTesting()), |
| 484 #endif |
| 485 test_peer_(&config_) { |
| 486 } |
| 458 | 487 |
| 459 void SetUp() override { | 488 void SetUp() override { |
| 460 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1000)); | 489 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1000)); |
| 461 } | 490 } |
| 462 | 491 |
| 463 // SetConfigs constructs suitable config protobufs and calls SetConfigs on | 492 // SetConfigs constructs suitable config protobufs and calls SetConfigs on |
| 464 // |config_|. The arguments are given as nullptr-terminated pairs. The first | 493 // |config_|. The arguments are given as nullptr-terminated pairs. The first |
| 465 // of each pair is the server config ID of a Config. The second is the | 494 // of each pair is the server config ID of a Config. The second is the |
| 466 // |primary_time| of that Config, given in epoch seconds. (Although note that, | 495 // |primary_time| of that Config, given in epoch seconds. (Although note that, |
| 467 // in these tests, time is set to 1000 seconds since the epoch.) For example: | 496 // in these tests, time is set to 1000 seconds since the epoch.) For example: |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 nullptr); | 778 nullptr); |
| 750 test_peer_.CheckConfigs( | 779 test_peer_.CheckConfigs( |
| 751 "a", false, | 780 "a", false, |
| 752 "b", true, | 781 "b", true, |
| 753 "c", false, | 782 "c", false, |
| 754 nullptr); | 783 nullptr); |
| 755 } | 784 } |
| 756 | 785 |
| 757 } // namespace test | 786 } // namespace test |
| 758 } // namespace net | 787 } // namespace net |
| OLD | NEW |