| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 | 47 |
| 48 bool ConfigHasDefaultSourceAddressTokenBoxer(string config_id) { | 48 bool ConfigHasDefaultSourceAddressTokenBoxer(string config_id) { |
| 49 scoped_refptr<QuicCryptoServerConfig::Config> config = GetConfig(config_id); | 49 scoped_refptr<QuicCryptoServerConfig::Config> config = GetConfig(config_id); |
| 50 return config->source_address_token_boxer == | 50 return config->source_address_token_boxer == |
| 51 &(server_config_->default_source_address_token_boxer_); | 51 &(server_config_->default_source_address_token_boxer_); |
| 52 } | 52 } |
| 53 | 53 |
| 54 string NewSourceAddressToken(string config_id, | 54 string NewSourceAddressToken(string config_id, |
| 55 const IPEndPoint& ip, | 55 SourceAddressTokens previous_tokens, |
| 56 QuicRandom* rand, | |
| 57 QuicWallTime now) { | |
| 58 return NewSourceAddressToken(config_id, ip, rand, now, NULL); | |
| 59 } | |
| 60 | |
| 61 string NewSourceAddressToken(string config_id, | |
| 62 const IPEndPoint& ip, | |
| 63 QuicRandom* rand, | |
| 64 QuicWallTime now, | |
| 65 const SourceAddressTokens& previous_tokens) { | |
| 66 return server_config_->NewSourceAddressToken( | |
| 67 *GetConfig(config_id), previous_tokens, ip, rand, now, NULL); | |
| 68 } | |
| 69 | |
| 70 string NewSourceAddressToken(string config_id, | |
| 71 const IPEndPoint& ip, | 56 const IPEndPoint& ip, |
| 72 QuicRandom* rand, | 57 QuicRandom* rand, |
| 73 QuicWallTime now, | 58 QuicWallTime now, |
| 74 CachedNetworkParameters* cached_network_params) { | 59 CachedNetworkParameters* cached_network_params) { |
| 75 SourceAddressTokens previous_tokens; | |
| 76 return server_config_->NewSourceAddressToken(*GetConfig(config_id), | 60 return server_config_->NewSourceAddressToken(*GetConfig(config_id), |
| 77 previous_tokens, ip, rand, now, | 61 previous_tokens, ip, rand, now, |
| 78 cached_network_params); | 62 cached_network_params); |
| 79 } | 63 } |
| 80 | 64 |
| 81 HandshakeFailureReason ValidateSourceAddressToken(string config_id, | 65 HandshakeFailureReason ValidateSourceAddressToken(string config_id, |
| 82 StringPiece srct, | 66 StringPiece srct, |
| 83 const IPEndPoint& ip, | 67 const IPEndPoint& ip, |
| 84 QuicWallTime now) { | 68 QuicWallTime now) { |
| 85 return ValidateSourceAddressToken(config_id, srct, ip, now, NULL); | 69 return ValidateSourceAddressToken(config_id, srct, ip, now, NULL); |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 override_config_protobuf_.reset( | 298 override_config_protobuf_.reset( |
| 315 QuicCryptoServerConfig::GenerateConfig(rand_, &clock_, options)); | 299 QuicCryptoServerConfig::GenerateConfig(rand_, &clock_, options)); |
| 316 override_config_protobuf_->set_source_address_token_secret_override( | 300 override_config_protobuf_->set_source_address_token_secret_override( |
| 317 "a secret key"); | 301 "a secret key"); |
| 318 // Lower priority than the default config. | 302 // Lower priority than the default config. |
| 319 override_config_protobuf_->set_priority(1); | 303 override_config_protobuf_->set_priority(1); |
| 320 override_config_.reset( | 304 override_config_.reset( |
| 321 server_.AddConfig(override_config_protobuf_.get(), original_time_)); | 305 server_.AddConfig(override_config_protobuf_.get(), original_time_)); |
| 322 } | 306 } |
| 323 | 307 |
| 308 string NewSourceAddressToken(string config_id, const IPEndPoint& ip) { |
| 309 return NewSourceAddressToken(config_id, ip, NULL); |
| 310 } |
| 311 |
| 312 string NewSourceAddressToken(string config_id, |
| 313 const IPEndPoint& ip, |
| 314 const SourceAddressTokens& previous_tokens) { |
| 315 return peer_.NewSourceAddressToken(config_id, previous_tokens, ip, rand_, |
| 316 clock_.WallNow(), NULL); |
| 317 } |
| 318 |
| 319 string NewSourceAddressToken(string config_id, |
| 320 const IPEndPoint& ip, |
| 321 CachedNetworkParameters* cached_network_params) { |
| 322 SourceAddressTokens previous_tokens; |
| 323 return peer_.NewSourceAddressToken(config_id, previous_tokens, ip, rand_, |
| 324 clock_.WallNow(), cached_network_params); |
| 325 } |
| 326 |
| 327 HandshakeFailureReason ValidateSourceAddressToken(string config_id, |
| 328 StringPiece srct, |
| 329 const IPEndPoint& ip) { |
| 330 return ValidateSourceAddressToken(config_id, srct, ip, NULL); |
| 331 } |
| 332 |
| 333 HandshakeFailureReason ValidateSourceAddressToken( |
| 334 string config_id, |
| 335 StringPiece srct, |
| 336 const IPEndPoint& ip, |
| 337 CachedNetworkParameters* cached_network_params) { |
| 338 return peer_.ValidateSourceAddressToken( |
| 339 config_id, srct, ip, clock_.WallNow(), cached_network_params); |
| 340 } |
| 341 |
| 342 HandshakeFailureReason ValidateSourceAddressTokens(string config_id, |
| 343 StringPiece srct, |
| 344 const IPEndPoint& ip) { |
| 345 return ValidateSourceAddressTokens(config_id, srct, ip, NULL); |
| 346 } |
| 347 |
| 348 HandshakeFailureReason ValidateSourceAddressTokens( |
| 349 string config_id, |
| 350 StringPiece srct, |
| 351 const IPEndPoint& ip, |
| 352 CachedNetworkParameters* cached_network_params) { |
| 353 return peer_.ValidateSourceAddressTokens( |
| 354 config_id, srct, ip, clock_.WallNow(), cached_network_params); |
| 355 } |
| 356 |
| 324 const string kPrimary = "<primary>"; | 357 const string kPrimary = "<primary>"; |
| 325 const string kOverride = "Config with custom source address token key"; | 358 const string kOverride = "Config with custom source address token key"; |
| 326 | 359 |
| 327 IPEndPoint ip4_; | 360 IPEndPoint ip4_; |
| 328 IPEndPoint ip4_dual_; | 361 IPEndPoint ip4_dual_; |
| 329 IPEndPoint ip6_; | 362 IPEndPoint ip6_; |
| 330 | 363 |
| 331 MockClock clock_; | 364 MockClock clock_; |
| 332 QuicWallTime original_time_; | 365 QuicWallTime original_time_; |
| 333 QuicRandom* rand_ = QuicRandom::GetInstance(); | 366 QuicRandom* rand_ = QuicRandom::GetInstance(); |
| 334 QuicCryptoServerConfig server_; | 367 QuicCryptoServerConfig server_; |
| 335 QuicCryptoServerConfigPeer peer_; | 368 QuicCryptoServerConfigPeer peer_; |
| 336 // Stores the primary config. | 369 // Stores the primary config. |
| 337 scoped_ptr<CryptoHandshakeMessage> primary_config_; | 370 scoped_ptr<CryptoHandshakeMessage> primary_config_; |
| 338 scoped_ptr<QuicServerConfigProtobuf> override_config_protobuf_; | 371 scoped_ptr<QuicServerConfigProtobuf> override_config_protobuf_; |
| 339 scoped_ptr<CryptoHandshakeMessage> override_config_; | 372 scoped_ptr<CryptoHandshakeMessage> override_config_; |
| 340 }; | 373 }; |
| 341 | 374 |
| 342 TEST_F(SourceAddressTokenTest, SourceAddressToken) { | 375 TEST_F(SourceAddressTokenTest, SourceAddressToken) { |
| 343 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, | 376 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, |
| 344 false); | 377 false); |
| 345 | 378 |
| 346 QuicWallTime now = clock_.WallNow(); | |
| 347 | |
| 348 EXPECT_TRUE(peer_.ConfigHasDefaultSourceAddressTokenBoxer(kPrimary)); | 379 EXPECT_TRUE(peer_.ConfigHasDefaultSourceAddressTokenBoxer(kPrimary)); |
| 349 EXPECT_FALSE(peer_.ConfigHasDefaultSourceAddressTokenBoxer(kOverride)); | 380 EXPECT_FALSE(peer_.ConfigHasDefaultSourceAddressTokenBoxer(kOverride)); |
| 350 | 381 |
| 351 // Primary config generates configs that validate successfully. | 382 // Primary config generates configs that validate successfully. |
| 352 const string token4 = peer_.NewSourceAddressToken(kPrimary, ip4_, rand_, now); | 383 const string token4 = NewSourceAddressToken(kPrimary, ip4_); |
| 353 const string token4d = | 384 const string token4d = NewSourceAddressToken(kPrimary, ip4_dual_); |
| 354 peer_.NewSourceAddressToken(kPrimary, ip4_dual_, rand_, now); | 385 const string token6 = NewSourceAddressToken(kPrimary, ip6_); |
| 355 const string token6 = peer_.NewSourceAddressToken(kPrimary, ip6_, rand_, now); | 386 EXPECT_EQ(HANDSHAKE_OK, ValidateSourceAddressToken(kPrimary, token4, ip4_)); |
| 356 EXPECT_EQ(HANDSHAKE_OK, | |
| 357 peer_.ValidateSourceAddressToken(kPrimary, token4, ip4_, now)); | |
| 358 ASSERT_EQ(HANDSHAKE_OK, | 387 ASSERT_EQ(HANDSHAKE_OK, |
| 359 peer_.ValidateSourceAddressToken(kPrimary, token4, ip4_dual_, now)); | 388 ValidateSourceAddressToken(kPrimary, token4, ip4_dual_)); |
| 360 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, | 389 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, |
| 361 peer_.ValidateSourceAddressToken(kPrimary, token4, ip6_, now)); | 390 ValidateSourceAddressToken(kPrimary, token4, ip6_)); |
| 391 ASSERT_EQ(HANDSHAKE_OK, ValidateSourceAddressToken(kPrimary, token4d, ip4_)); |
| 362 ASSERT_EQ(HANDSHAKE_OK, | 392 ASSERT_EQ(HANDSHAKE_OK, |
| 363 peer_.ValidateSourceAddressToken(kPrimary, token4d, ip4_, now)); | 393 ValidateSourceAddressToken(kPrimary, token4d, ip4_dual_)); |
| 364 ASSERT_EQ(HANDSHAKE_OK, peer_.ValidateSourceAddressToken(kPrimary, token4d, | |
| 365 ip4_dual_, now)); | |
| 366 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, | 394 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, |
| 367 peer_.ValidateSourceAddressToken(kPrimary, token4d, ip6_, now)); | 395 ValidateSourceAddressToken(kPrimary, token4d, ip6_)); |
| 368 ASSERT_EQ(HANDSHAKE_OK, | 396 ASSERT_EQ(HANDSHAKE_OK, ValidateSourceAddressToken(kPrimary, token6, ip6_)); |
| 369 peer_.ValidateSourceAddressToken(kPrimary, token6, ip6_, now)); | |
| 370 | 397 |
| 371 // Override config generates configs that validate successfully. | 398 // Override config generates configs that validate successfully. |
| 372 const string override_token4 = | 399 const string override_token4 = NewSourceAddressToken(kOverride, ip4_); |
| 373 peer_.NewSourceAddressToken(kOverride, ip4_, rand_, now); | 400 const string override_token6 = NewSourceAddressToken(kOverride, ip6_); |
| 374 const string override_token6 = | 401 ASSERT_EQ(HANDSHAKE_OK, |
| 375 peer_.NewSourceAddressToken(kOverride, ip6_, rand_, now); | 402 ValidateSourceAddressToken(kOverride, override_token4, ip4_)); |
| 376 ASSERT_EQ(HANDSHAKE_OK, peer_.ValidateSourceAddressToken( | 403 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, |
| 377 kOverride, override_token4, ip4_, now)); | 404 ValidateSourceAddressToken(kOverride, override_token4, ip6_)); |
| 378 ASSERT_EQ( | 405 ASSERT_EQ(HANDSHAKE_OK, |
| 379 SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, | 406 ValidateSourceAddressToken(kOverride, override_token6, ip6_)); |
| 380 peer_.ValidateSourceAddressToken(kOverride, override_token4, ip6_, now)); | |
| 381 ASSERT_EQ(HANDSHAKE_OK, peer_.ValidateSourceAddressToken( | |
| 382 kOverride, override_token6, ip6_, now)); | |
| 383 | 407 |
| 384 // Tokens generated by the primary config do not validate | 408 // Tokens generated by the primary config do not validate |
| 385 // successfully against the override config, and vice versa. | 409 // successfully against the override config, and vice versa. |
| 386 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, | 410 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, |
| 387 peer_.ValidateSourceAddressToken(kOverride, token4, ip4_, now)); | 411 ValidateSourceAddressToken(kOverride, token4, ip4_)); |
| 388 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, | 412 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, |
| 389 peer_.ValidateSourceAddressToken(kOverride, token6, ip6_, now)); | 413 ValidateSourceAddressToken(kOverride, token6, ip6_)); |
| 390 ASSERT_EQ( | 414 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, |
| 391 SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, | 415 ValidateSourceAddressToken(kPrimary, override_token4, ip4_)); |
| 392 peer_.ValidateSourceAddressToken(kPrimary, override_token4, ip4_, now)); | 416 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, |
| 393 ASSERT_EQ( | 417 ValidateSourceAddressToken(kPrimary, override_token6, ip6_)); |
| 394 SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, | |
| 395 peer_.ValidateSourceAddressToken(kPrimary, override_token6, ip6_, now)); | |
| 396 } | 418 } |
| 397 | 419 |
| 398 TEST_F(SourceAddressTokenTest, SourceAddressTokenExpiration) { | 420 TEST_F(SourceAddressTokenTest, SourceAddressTokenExpiration) { |
| 399 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, | 421 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, |
| 400 false); | 422 false); |
| 401 | 423 |
| 402 QuicWallTime now = clock_.WallNow(); | 424 const string token = NewSourceAddressToken(kPrimary, ip4_); |
| 403 | 425 |
| 404 const string token = peer_.NewSourceAddressToken(kPrimary, ip4_, rand_, now); | 426 // Validation fails if the token is from the future. |
| 427 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(-3600 * 2)); |
| 428 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_CLOCK_SKEW_FAILURE, |
| 429 ValidateSourceAddressToken(kPrimary, token, ip4_)); |
| 405 | 430 |
| 406 // Validation fails after tokens expire. | 431 // Validation fails after tokens expire. |
| 407 now = original_time_.Add(QuicTime::Delta::FromSeconds(86400 * 7)); | 432 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(86400 * 7)); |
| 408 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_EXPIRED_FAILURE, | 433 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_EXPIRED_FAILURE, |
| 409 peer_.ValidateSourceAddressToken(kPrimary, token, ip4_, now)); | 434 ValidateSourceAddressToken(kPrimary, token, ip4_)); |
| 410 | |
| 411 now = original_time_.Subtract(QuicTime::Delta::FromSeconds(3600 * 2)); | |
| 412 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_CLOCK_SKEW_FAILURE, | |
| 413 peer_.ValidateSourceAddressToken(kPrimary, token, ip4_, now)); | |
| 414 } | 435 } |
| 415 | 436 |
| 416 TEST_F(SourceAddressTokenTest, SourceAddressTokenWithNetworkParams) { | 437 TEST_F(SourceAddressTokenTest, SourceAddressTokenWithNetworkParams) { |
| 417 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, | 438 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, |
| 418 false); | 439 false); |
| 419 | 440 |
| 420 QuicWallTime now = clock_.WallNow(); | |
| 421 | |
| 422 // Make sure that if the source address token contains CachedNetworkParameters | 441 // Make sure that if the source address token contains CachedNetworkParameters |
| 423 // that this gets written to ValidateSourceAddressToken output argument. | 442 // that this gets written to ValidateSourceAddressToken output argument. |
| 424 CachedNetworkParameters cached_network_params_input; | 443 CachedNetworkParameters cached_network_params_input; |
| 425 cached_network_params_input.set_bandwidth_estimate_bytes_per_second(1234); | 444 cached_network_params_input.set_bandwidth_estimate_bytes_per_second(1234); |
| 426 const string token4_with_cached_network_params = peer_.NewSourceAddressToken( | 445 const string token4_with_cached_network_params = |
| 427 kPrimary, ip4_, rand_, now, &cached_network_params_input); | 446 NewSourceAddressToken(kPrimary, ip4_, &cached_network_params_input); |
| 428 | 447 |
| 429 CachedNetworkParameters cached_network_params_output; | 448 CachedNetworkParameters cached_network_params_output; |
| 430 #if 0 | 449 #if 0 |
| 431 // TODO(rtenneti): For server, enable the following check after serialization | 450 // TODO(rtenneti): For server, enable the following check after serialization |
| 432 // of optional CachedNetworkParameters is implemented. | 451 // of optional CachedNetworkParameters is implemented. |
| 433 EXPECT_NE(cached_network_params_output.DebugString(), | 452 EXPECT_NE(cached_network_params_output.DebugString(), |
| 434 cached_network_params_input.DebugString()); | 453 cached_network_params_input.DebugString()); |
| 435 #endif | 454 #endif |
| 436 peer_.ValidateSourceAddressToken(kPrimary, token4_with_cached_network_params, | 455 ValidateSourceAddressToken(kPrimary, token4_with_cached_network_params, ip4_, |
| 437 ip4_, now, &cached_network_params_output); | 456 &cached_network_params_output); |
| 438 #if 0 | 457 #if 0 |
| 439 // TODO(rtenneti): For server, enable the following check after serialization | 458 // TODO(rtenneti): For server, enable the following check after serialization |
| 440 // of optional CachedNetworkParameters is implemented. | 459 // of optional CachedNetworkParameters is implemented. |
| 441 EXPECT_EQ(cached_network_params_output.DebugString(), | 460 EXPECT_EQ(cached_network_params_output.DebugString(), |
| 442 cached_network_params_input.DebugString()); | 461 cached_network_params_input.DebugString()); |
| 443 #endif | 462 #endif |
| 444 } | 463 } |
| 445 | 464 |
| 446 // Test basic behavior of source address tokens including being specific | 465 // Test basic behavior of source address tokens including being specific |
| 447 // to a single IP address and server config. | 466 // to a single IP address and server config. |
| 448 // | 467 // |
| 449 // TODO(rtenneti): For server, enable the following test after serialization of | 468 // TODO(rtenneti): For server, enable the following test after serialization of |
| 450 // SourceAddressTokens is implemented. | 469 // SourceAddressTokens is implemented. |
| 451 TEST_F(SourceAddressTokenTest, DISABLED_NewSourceAddressToken) { | 470 TEST_F(SourceAddressTokenTest, DISABLED_NewSourceAddressToken) { |
| 452 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, | 471 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, |
| 453 true); | 472 true); |
| 454 | 473 |
| 455 QuicWallTime now = clock_.WallNow(); | |
| 456 | |
| 457 // Primary config generates configs that validate successfully. | 474 // Primary config generates configs that validate successfully. |
| 458 const string token4 = peer_.NewSourceAddressToken(kPrimary, ip4_, rand_, now); | 475 const string token4 = NewSourceAddressToken(kPrimary, ip4_); |
| 459 const string token4d = | 476 const string token4d = NewSourceAddressToken(kPrimary, ip4_dual_); |
| 460 peer_.NewSourceAddressToken(kPrimary, ip4_dual_, rand_, now); | 477 const string token6 = NewSourceAddressToken(kPrimary, ip6_); |
| 461 const string token6 = peer_.NewSourceAddressToken(kPrimary, ip6_, rand_, now); | 478 EXPECT_EQ(HANDSHAKE_OK, ValidateSourceAddressTokens(kPrimary, token4, ip4_)); |
| 462 EXPECT_EQ(HANDSHAKE_OK, | 479 ASSERT_EQ(HANDSHAKE_OK, |
| 463 peer_.ValidateSourceAddressTokens(kPrimary, token4, ip4_, now)); | 480 ValidateSourceAddressTokens(kPrimary, token4, ip4_dual_)); |
| 464 ASSERT_EQ(HANDSHAKE_OK, peer_.ValidateSourceAddressTokens(kPrimary, token4, | |
| 465 ip4_dual_, now)); | |
| 466 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, | 481 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, |
| 467 peer_.ValidateSourceAddressTokens(kPrimary, token4, ip6_, now)); | 482 ValidateSourceAddressTokens(kPrimary, token4, ip6_)); |
| 483 ASSERT_EQ(HANDSHAKE_OK, ValidateSourceAddressTokens(kPrimary, token4d, ip4_)); |
| 468 ASSERT_EQ(HANDSHAKE_OK, | 484 ASSERT_EQ(HANDSHAKE_OK, |
| 469 peer_.ValidateSourceAddressTokens(kPrimary, token4d, ip4_, now)); | 485 ValidateSourceAddressTokens(kPrimary, token4d, ip4_dual_)); |
| 470 ASSERT_EQ(HANDSHAKE_OK, peer_.ValidateSourceAddressTokens(kPrimary, token4d, | |
| 471 ip4_dual_, now)); | |
| 472 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, | 486 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, |
| 473 peer_.ValidateSourceAddressTokens(kPrimary, token4d, ip6_, now)); | 487 ValidateSourceAddressTokens(kPrimary, token4d, ip6_)); |
| 474 ASSERT_EQ(HANDSHAKE_OK, | 488 ASSERT_EQ(HANDSHAKE_OK, ValidateSourceAddressTokens(kPrimary, token6, ip6_)); |
| 475 peer_.ValidateSourceAddressTokens(kPrimary, token6, ip6_, now)); | |
| 476 | 489 |
| 477 // Override config generates configs that validate successfully. | 490 // Override config generates configs that validate successfully. |
| 478 const string override_token4 = | 491 const string override_token4 = NewSourceAddressToken(kOverride, ip4_); |
| 479 peer_.NewSourceAddressToken(kOverride, ip4_, rand_, now); | 492 const string override_token6 = NewSourceAddressToken(kOverride, ip6_); |
| 480 const string override_token6 = | 493 ASSERT_EQ(HANDSHAKE_OK, |
| 481 peer_.NewSourceAddressToken(kOverride, ip6_, rand_, now); | 494 ValidateSourceAddressTokens(kOverride, override_token4, ip4_)); |
| 482 ASSERT_EQ(HANDSHAKE_OK, peer_.ValidateSourceAddressTokens( | 495 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, |
| 483 kOverride, override_token4, ip4_, now)); | 496 ValidateSourceAddressTokens(kOverride, override_token4, ip6_)); |
| 484 ASSERT_EQ( | 497 ASSERT_EQ(HANDSHAKE_OK, |
| 485 SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE, | 498 ValidateSourceAddressTokens(kOverride, override_token6, ip6_)); |
| 486 peer_.ValidateSourceAddressTokens(kOverride, override_token4, ip6_, now)); | |
| 487 ASSERT_EQ(HANDSHAKE_OK, peer_.ValidateSourceAddressTokens( | |
| 488 kOverride, override_token6, ip6_, now)); | |
| 489 | 499 |
| 490 // Tokens generated by the primary config do not validate | 500 // Tokens generated by the primary config do not validate |
| 491 // successfully against the override config, and vice versa. | 501 // successfully against the override config, and vice versa. |
| 492 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, | 502 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, |
| 493 peer_.ValidateSourceAddressTokens(kOverride, token4, ip4_, now)); | 503 ValidateSourceAddressTokens(kOverride, token4, ip4_)); |
| 494 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, | 504 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, |
| 495 peer_.ValidateSourceAddressTokens(kOverride, token6, ip6_, now)); | 505 ValidateSourceAddressTokens(kOverride, token6, ip6_)); |
| 496 ASSERT_EQ( | 506 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, |
| 497 SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, | 507 ValidateSourceAddressTokens(kPrimary, override_token4, ip4_)); |
| 498 peer_.ValidateSourceAddressTokens(kPrimary, override_token4, ip4_, now)); | 508 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, |
| 499 ASSERT_EQ( | 509 ValidateSourceAddressTokens(kPrimary, override_token6, ip6_)); |
| 500 SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE, | |
| 501 peer_.ValidateSourceAddressTokens(kPrimary, override_token6, ip6_, now)); | |
| 502 } | 510 } |
| 503 | 511 |
| 504 // TODO(rtenneti): For server, enable the following test after serialization of | 512 // TODO(rtenneti): For server, enable the following test after serialization of |
| 505 // SourceAddressTokens is implemented. | 513 // SourceAddressTokens is implemented. |
| 506 TEST_F(SourceAddressTokenTest, DISABLED_NewSourceAddressTokenExpiration) { | 514 TEST_F(SourceAddressTokenTest, DISABLED_NewSourceAddressTokenExpiration) { |
| 507 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, | 515 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, |
| 508 true); | 516 true); |
| 509 | 517 |
| 510 QuicWallTime now = clock_.WallNow(); | 518 const string token = NewSourceAddressToken(kPrimary, ip4_); |
| 511 | 519 |
| 512 const string token = peer_.NewSourceAddressToken(kPrimary, ip4_, rand_, now); | 520 // Validation fails if the token is from the future. |
| 521 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(-3600 * 2)); |
| 522 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_CLOCK_SKEW_FAILURE, |
| 523 ValidateSourceAddressTokens(kPrimary, token, ip4_)); |
| 513 | 524 |
| 514 // Validation fails after tokens expire. | 525 // Validation fails after tokens expire. |
| 515 now = original_time_.Add(QuicTime::Delta::FromSeconds(86400 * 7)); | 526 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(86400 * 7)); |
| 516 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_EXPIRED_FAILURE, | 527 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_EXPIRED_FAILURE, |
| 517 peer_.ValidateSourceAddressTokens(kPrimary, token, ip4_, now)); | 528 ValidateSourceAddressTokens(kPrimary, token, ip4_)); |
| 518 | |
| 519 now = original_time_.Subtract(QuicTime::Delta::FromSeconds(3600 * 2)); | |
| 520 ASSERT_EQ(SOURCE_ADDRESS_TOKEN_CLOCK_SKEW_FAILURE, | |
| 521 peer_.ValidateSourceAddressTokens(kPrimary, token, ip4_, now)); | |
| 522 } | 529 } |
| 523 | 530 |
| 524 TEST_F(SourceAddressTokenTest, NewSourceAddressTokenWithNetworkParams) { | 531 TEST_F(SourceAddressTokenTest, NewSourceAddressTokenWithNetworkParams) { |
| 525 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, | 532 ValueRestore<bool> old_flag(&FLAGS_quic_use_multiple_address_in_source_tokens, |
| 526 true); | 533 true); |
| 527 | 534 |
| 528 QuicWallTime now = clock_.WallNow(); | |
| 529 | |
| 530 // Make sure that if the source address token contains CachedNetworkParameters | 535 // Make sure that if the source address token contains CachedNetworkParameters |
| 531 // that this gets written to ValidateSourceAddressToken output argument. | 536 // that this gets written to ValidateSourceAddressToken output argument. |
| 532 CachedNetworkParameters cached_network_params_input; | 537 CachedNetworkParameters cached_network_params_input; |
| 533 cached_network_params_input.set_bandwidth_estimate_bytes_per_second(1234); | 538 cached_network_params_input.set_bandwidth_estimate_bytes_per_second(1234); |
| 534 const string token4_with_cached_network_params = peer_.NewSourceAddressToken( | 539 const string token4_with_cached_network_params = |
| 535 kPrimary, ip4_, rand_, now, &cached_network_params_input); | 540 NewSourceAddressToken(kPrimary, ip4_, &cached_network_params_input); |
| 536 | 541 |
| 537 CachedNetworkParameters cached_network_params_output; | 542 CachedNetworkParameters cached_network_params_output; |
| 538 #if 0 | 543 #if 0 |
| 539 // TODO(rtenneti): For server, enable the following check after serialization | 544 // TODO(rtenneti): For server, enable the following check after serialization |
| 540 // of optional CachedNetworkParameters is implemented. | 545 // of optional CachedNetworkParameters is implemented. |
| 541 EXPECT_NE(cached_network_params_output.DebugString(), | 546 EXPECT_NE(cached_network_params_output.DebugString(), |
| 542 cached_network_params_input.DebugString()); | 547 cached_network_params_input.DebugString()); |
| 543 #endif | 548 #endif |
| 544 peer_.ValidateSourceAddressTokens(kPrimary, token4_with_cached_network_params, | 549 ValidateSourceAddressTokens(kPrimary, token4_with_cached_network_params, ip4_, |
| 545 ip4_, now, &cached_network_params_output); | 550 &cached_network_params_output); |
| 546 #if 0 | 551 #if 0 |
| 547 // TODO(rtenneti): For server, enable the following check after serialization | 552 // TODO(rtenneti): For server, enable the following check after serialization |
| 548 // of optional CachedNetworkParameters is implemented. | 553 // of optional CachedNetworkParameters is implemented. |
| 549 EXPECT_EQ(cached_network_params_output.DebugString(), | 554 EXPECT_EQ(cached_network_params_output.DebugString(), |
| 550 cached_network_params_input.DebugString()); | 555 cached_network_params_input.DebugString()); |
| 551 #endif | 556 #endif |
| 552 } | 557 } |
| 553 | 558 |
| 554 // Test the ability for a source address token to be valid for multiple | 559 // Test the ability for a source address token to be valid for multiple |
| 555 // addresses. | 560 // addresses. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 566 SourceAddressToken previous_token; | 571 SourceAddressToken previous_token; |
| 567 IPAddressNumber ip_address = ip6_.address(); | 572 IPAddressNumber ip_address = ip6_.address(); |
| 568 if (ip6_.GetSockAddrFamily() == AF_INET) { | 573 if (ip6_.GetSockAddrFamily() == AF_INET) { |
| 569 ip_address = ConvertIPv4NumberToIPv6Number(ip_address); | 574 ip_address = ConvertIPv4NumberToIPv6Number(ip_address); |
| 570 } | 575 } |
| 571 previous_token.set_ip(IPAddressToPackedString(ip_address)); | 576 previous_token.set_ip(IPAddressToPackedString(ip_address)); |
| 572 previous_token.set_timestamp(now.ToUNIXSeconds()); | 577 previous_token.set_timestamp(now.ToUNIXSeconds()); |
| 573 SourceAddressTokens previous_tokens; | 578 SourceAddressTokens previous_tokens; |
| 574 (*previous_tokens.add_tokens()) = previous_token; | 579 (*previous_tokens.add_tokens()) = previous_token; |
| 575 const string token4or6 = | 580 const string token4or6 = |
| 576 peer_.NewSourceAddressToken(kPrimary, ip4_, rand_, now, previous_tokens); | 581 NewSourceAddressToken(kPrimary, ip4_, previous_tokens); |
| 577 | 582 |
| 578 EXPECT_EQ(HANDSHAKE_OK, | 583 EXPECT_EQ(HANDSHAKE_OK, |
| 579 peer_.ValidateSourceAddressTokens(kPrimary, token4or6, ip4_, now)); | 584 ValidateSourceAddressTokens(kPrimary, token4or6, ip4_)); |
| 580 ASSERT_EQ(HANDSHAKE_OK, | 585 ASSERT_EQ(HANDSHAKE_OK, |
| 581 peer_.ValidateSourceAddressTokens(kPrimary, token4or6, ip6_, now)); | 586 ValidateSourceAddressTokens(kPrimary, token4or6, ip6_)); |
| 582 } | 587 } |
| 583 | 588 |
| 584 TEST(QuicCryptoServerConfigTest, ValidateServerNonce) { | 589 TEST(QuicCryptoServerConfigTest, ValidateServerNonce) { |
| 585 QuicRandom* rand = QuicRandom::GetInstance(); | 590 QuicRandom* rand = QuicRandom::GetInstance(); |
| 586 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand); | 591 QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand); |
| 587 QuicCryptoServerConfigPeer peer(&server); | 592 QuicCryptoServerConfigPeer peer(&server); |
| 588 | 593 |
| 589 StringPiece message("hello world"); | 594 StringPiece message("hello world"); |
| 590 const size_t key_size = CryptoSecretBoxer::GetKeySize(); | 595 const size_t key_size = CryptoSecretBoxer::GetKeySize(); |
| 591 scoped_ptr<uint8[]> key(new uint8[key_size]); | 596 scoped_ptr<uint8[]> key(new uint8[key_size]); |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 nullptr); | 915 nullptr); |
| 911 test_peer_.CheckConfigs( | 916 test_peer_.CheckConfigs( |
| 912 "a", false, | 917 "a", false, |
| 913 "b", true, | 918 "b", true, |
| 914 "c", false, | 919 "c", false, |
| 915 nullptr); | 920 nullptr); |
| 916 } | 921 } |
| 917 | 922 |
| 918 } // namespace test | 923 } // namespace test |
| 919 } // namespace net | 924 } // namespace net |
| OLD | NEW |