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" |
(...skipping 14 matching lines...) Expand all Loading... |
25 using std::vector; | 25 using std::vector; |
26 | 26 |
27 namespace net { | 27 namespace net { |
28 namespace test { | 28 namespace test { |
29 | 29 |
30 class QuicCryptoServerConfigPeer { | 30 class QuicCryptoServerConfigPeer { |
31 public: | 31 public: |
32 explicit QuicCryptoServerConfigPeer(QuicCryptoServerConfig* server_config) | 32 explicit QuicCryptoServerConfigPeer(QuicCryptoServerConfig* server_config) |
33 : server_config_(server_config) {} | 33 : server_config_(server_config) {} |
34 | 34 |
35 string NewSourceAddressToken(IPEndPoint ip, | 35 scoped_refptr<QuicCryptoServerConfig::Config> GetConfig(string config_id) { |
36 QuicRandom* rand, | 36 base::AutoLock locked(server_config_->configs_lock_); |
37 QuicWallTime now) { | 37 if (config_id == "<primary>") { |
38 return server_config_->NewSourceAddressToken(ip, rand, now); | 38 return scoped_refptr<QuicCryptoServerConfig::Config>( |
| 39 server_config_->primary_config_); |
| 40 } else { |
| 41 return server_config_->GetConfigWithScid(config_id); |
| 42 } |
39 } | 43 } |
40 | 44 |
41 bool ValidateSourceAddressToken(StringPiece srct, | 45 bool ConfigHasDefaultSourceAddressTokenBoxer(string config_id) { |
| 46 scoped_refptr<QuicCryptoServerConfig::Config> config = GetConfig(config_id); |
| 47 return config->source_address_token_boxer == |
| 48 &(server_config_->default_source_address_token_boxer_); |
| 49 } |
| 50 |
| 51 string NewSourceAddressToken( |
| 52 string config_id, |
| 53 IPEndPoint ip, |
| 54 QuicRandom* rand, |
| 55 QuicWallTime now) { |
| 56 return server_config_->NewSourceAddressToken( |
| 57 *GetConfig(config_id), ip, rand, now); |
| 58 } |
| 59 |
| 60 bool ValidateSourceAddressToken(string config_id, |
| 61 StringPiece srct, |
42 IPEndPoint ip, | 62 IPEndPoint ip, |
43 QuicWallTime now) { | 63 QuicWallTime now) { |
44 return server_config_->ValidateSourceAddressToken(srct, ip, now); | 64 return server_config_->ValidateSourceAddressToken( |
| 65 *GetConfig(config_id), srct, ip, now); |
45 } | 66 } |
46 | 67 |
47 base::Lock* GetStrikeRegisterClientLock() { | 68 base::Lock* GetStrikeRegisterClientLock() { |
48 return &server_config_->strike_register_client_lock_; | 69 return &server_config_->strike_register_client_lock_; |
49 } | 70 } |
50 | 71 |
51 // CheckConfigs compares the state of the Configs in |server_config_| to the | 72 // CheckConfigs compares the state of the Configs in |server_config_| to the |
52 // description given as arguments. The arguments are given as NULL-terminated | 73 // description given as arguments. The arguments are given as NULL-terminated |
53 // pairs. The first of each pair is the server config ID of a Config. The | 74 // pairs. The first of each pair is the server config ID of a Config. The |
54 // second is a boolean describing whether the config is the primary. For | 75 // second is a boolean describing whether the config is the primary. For |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 new TestStrikeRegisterClient(&server); | 223 new TestStrikeRegisterClient(&server); |
203 server.SetStrikeRegisterClient(strike_register); | 224 server.SetStrikeRegisterClient(strike_register); |
204 | 225 |
205 QuicCryptoServerConfig::ConfigOptions options; | 226 QuicCryptoServerConfig::ConfigOptions options; |
206 scoped_ptr<CryptoHandshakeMessage>( | 227 scoped_ptr<CryptoHandshakeMessage>( |
207 server.AddDefaultConfig(rand, &clock, options)); | 228 server.AddDefaultConfig(rand, &clock, options)); |
208 EXPECT_TRUE(strike_register->is_known_orbit_called()); | 229 EXPECT_TRUE(strike_register->is_known_orbit_called()); |
209 } | 230 } |
210 | 231 |
211 TEST(QuicCryptoServerConfigTest, SourceAddressTokens) { | 232 TEST(QuicCryptoServerConfigTest, SourceAddressTokens) { |
| 233 const string kPrimary = "<primary>"; |
| 234 const string kOverride = "Config with custom source address token key"; |
| 235 |
| 236 MockClock clock; |
| 237 clock.AdvanceTime(QuicTime::Delta::FromSeconds(1000000)); |
| 238 |
| 239 QuicWallTime now = clock.WallNow(); |
| 240 const QuicWallTime original_time = now; |
| 241 |
212 QuicRandom* rand = QuicRandom::GetInstance(); | 242 QuicRandom* rand = QuicRandom::GetInstance(); |
213 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand); | 243 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand); |
| 244 QuicCryptoServerConfigPeer peer(&server); |
| 245 |
| 246 scoped_ptr<CryptoHandshakeMessage>( |
| 247 server.AddDefaultConfig(rand, &clock, |
| 248 QuicCryptoServerConfig::ConfigOptions())); |
| 249 |
| 250 // Add a config that overrides the default boxer. |
| 251 QuicCryptoServerConfig::ConfigOptions options; |
| 252 options.id = kOverride; |
| 253 scoped_ptr<QuicServerConfigProtobuf> protobuf( |
| 254 QuicCryptoServerConfig::GenerateConfig(rand, &clock, options)); |
| 255 protobuf->set_source_address_token_secret_override("a secret key"); |
| 256 // Lower priority than the default config. |
| 257 protobuf->set_priority(1); |
| 258 scoped_ptr<CryptoHandshakeMessage>( |
| 259 server.AddConfig(protobuf.get(), now)); |
| 260 |
| 261 EXPECT_TRUE(peer.ConfigHasDefaultSourceAddressTokenBoxer(kPrimary)); |
| 262 EXPECT_FALSE(peer.ConfigHasDefaultSourceAddressTokenBoxer(kOverride)); |
| 263 |
214 IPAddressNumber ip; | 264 IPAddressNumber ip; |
215 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); | 265 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
216 IPEndPoint ip4 = IPEndPoint(ip, 1); | 266 IPEndPoint ip4 = IPEndPoint(ip, 1); |
217 CHECK(ParseIPLiteralToNumber("2001:db8:0::42", &ip)); | 267 CHECK(ParseIPLiteralToNumber("2001:db8:0::42", &ip)); |
218 IPEndPoint ip6 = IPEndPoint(ip, 2); | 268 IPEndPoint ip6 = IPEndPoint(ip, 2); |
219 MockClock clock; | |
220 clock.AdvanceTime(QuicTime::Delta::FromSeconds(1000000)); | |
221 QuicCryptoServerConfigPeer peer(&server); | |
222 | 269 |
223 QuicWallTime now = clock.WallNow(); | 270 // Primary config generates configs that validate successfully. |
224 const QuicWallTime original_time = now; | 271 const string token4 = peer.NewSourceAddressToken(kPrimary, ip4, rand, now); |
| 272 const string token6 = peer.NewSourceAddressToken(kPrimary, ip6, rand, now); |
| 273 EXPECT_TRUE(peer.ValidateSourceAddressToken(kPrimary, token4, ip4, now)); |
| 274 EXPECT_FALSE(peer.ValidateSourceAddressToken(kPrimary, token4, ip6, now)); |
| 275 EXPECT_TRUE(peer.ValidateSourceAddressToken(kPrimary, token6, ip6, now)); |
225 | 276 |
226 const string token4 = peer.NewSourceAddressToken(ip4, rand, now); | 277 // Override config generates configs that validate successfully. |
227 const string token6 = peer.NewSourceAddressToken(ip6, rand, now); | 278 const string override_token4 = peer.NewSourceAddressToken( |
228 EXPECT_TRUE(peer.ValidateSourceAddressToken(token4, ip4, now)); | 279 kOverride, ip4, rand, now); |
229 EXPECT_FALSE(peer.ValidateSourceAddressToken(token4, ip6, now)); | 280 const string override_token6 = peer.NewSourceAddressToken( |
230 EXPECT_TRUE(peer.ValidateSourceAddressToken(token6, ip6, now)); | 281 kOverride, ip6, rand, now); |
| 282 EXPECT_TRUE(peer.ValidateSourceAddressToken( |
| 283 kOverride, override_token4, ip4, now)); |
| 284 EXPECT_FALSE(peer.ValidateSourceAddressToken( |
| 285 kOverride, override_token4, ip6, now)); |
| 286 EXPECT_TRUE(peer.ValidateSourceAddressToken( |
| 287 kOverride, override_token6, ip6, now)); |
231 | 288 |
| 289 // Tokens generated by the primary config do not validate |
| 290 // successfully against the override config, and vice versa. |
| 291 EXPECT_FALSE(peer.ValidateSourceAddressToken(kOverride, token4, ip4, now)); |
| 292 EXPECT_FALSE(peer.ValidateSourceAddressToken(kOverride, token6, ip6, now)); |
| 293 EXPECT_FALSE(peer.ValidateSourceAddressToken( |
| 294 kPrimary, override_token4, ip4, now)); |
| 295 EXPECT_FALSE(peer.ValidateSourceAddressToken( |
| 296 kPrimary, override_token6, ip6, now)); |
| 297 |
| 298 // Validation fails after tokens expire. |
232 now = original_time.Add(QuicTime::Delta::FromSeconds(86400 * 7)); | 299 now = original_time.Add(QuicTime::Delta::FromSeconds(86400 * 7)); |
233 EXPECT_FALSE(peer.ValidateSourceAddressToken(token4, ip4, now)); | 300 EXPECT_FALSE(peer.ValidateSourceAddressToken(kPrimary, token4, ip4, now)); |
234 | 301 |
235 now = original_time.Subtract(QuicTime::Delta::FromSeconds(3600 * 2)); | 302 now = original_time.Subtract(QuicTime::Delta::FromSeconds(3600 * 2)); |
236 EXPECT_FALSE(peer.ValidateSourceAddressToken(token4, ip4, now)); | 303 EXPECT_FALSE(peer.ValidateSourceAddressToken(kPrimary, token4, ip4, now)); |
237 } | 304 } |
238 | 305 |
239 class CryptoServerConfigsTest : public ::testing::Test { | 306 class CryptoServerConfigsTest : public ::testing::Test { |
240 public: | 307 public: |
241 CryptoServerConfigsTest() | 308 CryptoServerConfigsTest() |
242 : rand_(QuicRandom::GetInstance()), | 309 : rand_(QuicRandom::GetInstance()), |
243 config_(QuicCryptoServerConfig::TESTING, rand_), | 310 config_(QuicCryptoServerConfig::TESTING, rand_), |
244 test_peer_(&config_) {} | 311 test_peer_(&config_) {} |
245 | 312 |
246 virtual void SetUp() { | 313 virtual void SetUp() { |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 NULL); | 604 NULL); |
538 test_peer_.CheckConfigs( | 605 test_peer_.CheckConfigs( |
539 "a", false, | 606 "a", false, |
540 "b", true, | 607 "b", true, |
541 "c", false, | 608 "c", false, |
542 NULL); | 609 NULL); |
543 } | 610 } |
544 | 611 |
545 } // namespace test | 612 } // namespace test |
546 } // namespace net | 613 } // namespace net |
OLD | NEW |