| 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/sha1.h" | 8 #include "base/sha1.h" |
| 9 #include "base/strings/string_piece.h" | 9 #include "base/strings/string_piece.h" |
| 10 #include "crypto/sha2.h" | 10 #include "crypto/sha2.h" |
| (...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 new_dynamic_domain_state.pkp.spki_hashes.end(), | 565 new_dynamic_domain_state.pkp.spki_hashes.end(), |
| 566 HashValuesEqual(good_hash)); | 566 HashValuesEqual(good_hash)); |
| 567 EXPECT_NE(new_dynamic_domain_state.pkp.spki_hashes.end(), hash); | 567 EXPECT_NE(new_dynamic_domain_state.pkp.spki_hashes.end(), hash); |
| 568 | 568 |
| 569 hash = std::find_if(new_dynamic_domain_state.pkp.spki_hashes.begin(), | 569 hash = std::find_if(new_dynamic_domain_state.pkp.spki_hashes.begin(), |
| 570 new_dynamic_domain_state.pkp.spki_hashes.end(), | 570 new_dynamic_domain_state.pkp.spki_hashes.end(), |
| 571 HashValuesEqual(backup_hash)); | 571 HashValuesEqual(backup_hash)); |
| 572 EXPECT_NE(new_dynamic_domain_state.pkp.spki_hashes.end(), hash); | 572 EXPECT_NE(new_dynamic_domain_state.pkp.spki_hashes.end(), hash); |
| 573 } | 573 } |
| 574 | 574 |
| 575 TEST_F(HttpSecurityHeadersTest, UpdateDynamicPKPMaxAge0) { |
| 576 TransportSecurityState state; |
| 577 TransportSecurityState::DomainState static_domain_state; |
| 578 |
| 579 // docs.google.com has preloaded pins. |
| 580 const bool sni_enabled = true; |
| 581 std::string domain = "docs.google.com"; |
| 582 ASSERT_TRUE( |
| 583 state.GetStaticDomainState(domain, sni_enabled, &static_domain_state)); |
| 584 EXPECT_GT(static_domain_state.pkp.spki_hashes.size(), 1UL); |
| 585 HashValueVector saved_hashes = static_domain_state.pkp.spki_hashes; |
| 586 |
| 587 // Add a header, which should only update the dynamic state. |
| 588 HashValue good_hash = GetTestHashValue(1, HASH_VALUE_SHA1); |
| 589 std::string good_pin = GetTestPin(1, HASH_VALUE_SHA1); |
| 590 std::string backup_pin = GetTestPin(2, HASH_VALUE_SHA1); |
| 591 std::string header = "max-age = 10000; " + good_pin + "; " + backup_pin; |
| 592 |
| 593 // Construct a fake SSLInfo that will pass AddHPKPHeader's checks. |
| 594 SSLInfo ssl_info; |
| 595 ssl_info.public_key_hashes.push_back(good_hash); |
| 596 ssl_info.public_key_hashes.push_back(saved_hashes[0]); |
| 597 EXPECT_TRUE(state.AddHPKPHeader(domain, header, ssl_info)); |
| 598 |
| 599 // Expect the static state to remain unchanged. |
| 600 TransportSecurityState::DomainState new_static_domain_state; |
| 601 EXPECT_TRUE(state.GetStaticDomainState( |
| 602 domain, sni_enabled, &new_static_domain_state)); |
| 603 EXPECT_EQ(saved_hashes.size(), |
| 604 new_static_domain_state.pkp.spki_hashes.size()); |
| 605 for (size_t i = 0; i < saved_hashes.size(); ++i) { |
| 606 EXPECT_TRUE(HashValuesEqual(saved_hashes[i])( |
| 607 new_static_domain_state.pkp.spki_hashes[i])); |
| 608 } |
| 609 |
| 610 // Expect the dynamic state to have pins. |
| 611 TransportSecurityState::DomainState new_dynamic_domain_state; |
| 612 EXPECT_TRUE(state.GetDynamicDomainState(domain, &new_dynamic_domain_state)); |
| 613 EXPECT_EQ(2UL, new_dynamic_domain_state.pkp.spki_hashes.size()); |
| 614 EXPECT_TRUE(new_dynamic_domain_state.HasPublicKeyPins()); |
| 615 |
| 616 // Now set another header with max-age=0, and check that the pins are |
| 617 // cleared in the dynamic state only. |
| 618 header = "max-age = 0; " + good_pin + "; " + backup_pin; |
| 619 EXPECT_TRUE(state.AddHPKPHeader(domain, header, ssl_info)); |
| 620 |
| 621 // Expect the static state to remain unchanged. |
| 622 TransportSecurityState::DomainState new_static_domain_state2; |
| 623 EXPECT_TRUE(state.GetStaticDomainState( |
| 624 domain, sni_enabled, &new_static_domain_state2)); |
| 625 EXPECT_EQ(saved_hashes.size(), |
| 626 new_static_domain_state2.pkp.spki_hashes.size()); |
| 627 for (size_t i = 0; i < saved_hashes.size(); ++i) { |
| 628 EXPECT_TRUE(HashValuesEqual(saved_hashes[i])( |
| 629 new_static_domain_state2.pkp.spki_hashes[i])); |
| 630 } |
| 631 |
| 632 // Expect the dynamic pins to be gone. |
| 633 TransportSecurityState::DomainState new_dynamic_domain_state2; |
| 634 EXPECT_FALSE(state.GetDynamicDomainState(domain, &new_dynamic_domain_state2)); |
| 635 |
| 636 // Expect the exact-matching static policy to continue to apply, even |
| 637 // though dynamic policy has been removed. (This policy may change in the |
| 638 // future, in which case this test must be updated.) |
| 639 EXPECT_TRUE(state.HasPublicKeyPins(domain, true)); |
| 640 EXPECT_TRUE(state.ShouldSSLErrorsBeFatal(domain, true)); |
| 641 std::string failure_log; |
| 642 // Damage the hashes to cause a pin validation failure. |
| 643 new_static_domain_state2.pkp.spki_hashes[0].data()[0] ^= 0x80; |
| 644 new_static_domain_state2.pkp.spki_hashes[1].data()[0] ^= 0x80; |
| 645 EXPECT_FALSE(state.CheckPublicKeyPins( |
| 646 domain, true, new_static_domain_state2.pkp.spki_hashes, &failure_log)); |
| 647 EXPECT_NE(0UL, failure_log.length()); |
| 648 } |
| 649 |
| 575 // Tests that when a static HSTS and a static HPKP entry are present, adding a | 650 // Tests that when a static HSTS and a static HPKP entry are present, adding a |
| 576 // dynamic HSTS header does not clobber the static HPKP entry. Further, adding a | 651 // dynamic HSTS header does not clobber the static HPKP entry. Further, adding a |
| 577 // dynamic HPKP entry could not affect the HSTS entry for the site. | 652 // dynamic HPKP entry could not affect the HSTS entry for the site. |
| 578 TEST_F(HttpSecurityHeadersTest, NoClobberPins) { | 653 TEST_F(HttpSecurityHeadersTest, NoClobberPins) { |
| 579 TransportSecurityState state; | 654 TransportSecurityState state; |
| 580 TransportSecurityState::DomainState domain_state; | 655 TransportSecurityState::DomainState domain_state; |
| 581 | 656 |
| 582 // accounts.google.com has preloaded pins. | 657 // accounts.google.com has preloaded pins. |
| 583 std::string domain = "accounts.google.com"; | 658 std::string domain = "accounts.google.com"; |
| 584 | 659 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 // HSTS should still be configured for this domain. | 692 // HSTS should still be configured for this domain. |
| 618 EXPECT_TRUE(domain_state.ShouldUpgradeToSSL()); | 693 EXPECT_TRUE(domain_state.ShouldUpgradeToSSL()); |
| 619 EXPECT_TRUE(state.ShouldUpgradeToSSL(domain, sni_enabled)); | 694 EXPECT_TRUE(state.ShouldUpgradeToSSL(domain, sni_enabled)); |
| 620 // The dynamic pins, which do not match |saved_hashes|, should take | 695 // The dynamic pins, which do not match |saved_hashes|, should take |
| 621 // precedence over the static pins and cause the check to fail. | 696 // precedence over the static pins and cause the check to fail. |
| 622 EXPECT_FALSE(state.CheckPublicKeyPins( | 697 EXPECT_FALSE(state.CheckPublicKeyPins( |
| 623 domain, sni_enabled, saved_hashes, &failure_log)); | 698 domain, sni_enabled, saved_hashes, &failure_log)); |
| 624 } | 699 } |
| 625 | 700 |
| 626 }; // namespace net | 701 }; // namespace net |
| OLD | NEW |