| 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" |
| 11 #include "net/base/net_log.h" | 11 #include "net/base/net_log.h" |
| 12 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
| 13 #include "net/http/http_security_headers.h" | 13 #include "net/http/http_security_headers.h" |
| 14 #include "net/http/http_util.h" | 14 #include "net/http/http_util.h" |
| 15 #include "net/http/transport_security_state.h" | 15 #include "net/http/transport_security_state.h" |
| 16 #include "net/ssl/ssl_info.h" | 16 #include "net/ssl/ssl_info.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 HashValue GetTestHashValue(uint8 label, HashValueTag tag) { | 23 HashValue GetTestHashValue(uint8 label, HashValueTag tag) { |
| 24 HashValue hash_value(tag); | 24 HashValue hash_value(tag); |
| 25 memset(hash_value.data(), label, hash_value.size()); | 25 memset(hash_value.data(), label, hash_value.size()); |
| 26 return hash_value; | 26 return hash_value; |
| 27 } | 27 } |
| 28 | 28 |
| 29 std::string GetTestPin(uint8 label, HashValueTag tag) { | 29 std::string GetTestPinImpl(uint8 label, HashValueTag tag, bool quoted) { |
| 30 HashValue hash_value = GetTestHashValue(label, tag); | 30 HashValue hash_value = GetTestHashValue(label, tag); |
| 31 std::string base64; | 31 std::string base64; |
| 32 base::Base64Encode(base::StringPiece( | 32 base::Base64Encode(base::StringPiece( |
| 33 reinterpret_cast<char*>(hash_value.data()), hash_value.size()), &base64); | 33 reinterpret_cast<char*>(hash_value.data()), hash_value.size()), &base64); |
| 34 | 34 |
| 35 std::string ret; |
| 35 switch (hash_value.tag) { | 36 switch (hash_value.tag) { |
| 36 case HASH_VALUE_SHA1: | 37 case HASH_VALUE_SHA1: |
| 37 return std::string("pin-sha1=\"") + base64 + "\""; | 38 ret = "pin-sha1="; |
| 39 break; |
| 38 case HASH_VALUE_SHA256: | 40 case HASH_VALUE_SHA256: |
| 39 return std::string("pin-sha256=\"") + base64 + "\""; | 41 ret = "pin-sha256="; |
| 42 break; |
| 40 default: | 43 default: |
| 41 NOTREACHED() << "Unknown HashValueTag " << hash_value.tag; | 44 NOTREACHED() << "Unknown HashValueTag " << hash_value.tag; |
| 42 return std::string("ERROR"); | 45 return std::string("ERROR"); |
| 43 } | 46 } |
| 47 if (quoted) |
| 48 ret += '\"'; |
| 49 ret += base64; |
| 50 if (quoted) |
| 51 ret += '\"'; |
| 52 return ret; |
| 53 } |
| 54 |
| 55 std::string GetTestPin(uint8 label, HashValueTag tag) { |
| 56 return GetTestPinImpl(label, tag, true); |
| 57 } |
| 58 |
| 59 std::string GetTestPinUnquoted(uint8 label, HashValueTag tag) { |
| 60 return GetTestPinImpl(label, tag, false); |
| 44 } | 61 } |
| 45 | 62 |
| 46 }; | 63 }; |
| 47 | 64 |
| 48 | 65 |
| 49 class HttpSecurityHeadersTest : public testing::Test { | 66 class HttpSecurityHeadersTest : public testing::Test { |
| 50 }; | 67 }; |
| 51 | 68 |
| 52 | 69 |
| 53 TEST_F(HttpSecurityHeadersTest, BogusHeaders) { | 70 TEST_F(HttpSecurityHeadersTest, BogusHeaders) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 HashValueVector hashes; | 152 HashValueVector hashes; |
| 136 HashValueVector chain_hashes; | 153 HashValueVector chain_hashes; |
| 137 | 154 |
| 138 // Set some fake "chain" hashes | 155 // Set some fake "chain" hashes |
| 139 chain_hashes.push_back(GetTestHashValue(1, tag)); | 156 chain_hashes.push_back(GetTestHashValue(1, tag)); |
| 140 chain_hashes.push_back(GetTestHashValue(2, tag)); | 157 chain_hashes.push_back(GetTestHashValue(2, tag)); |
| 141 chain_hashes.push_back(GetTestHashValue(3, tag)); | 158 chain_hashes.push_back(GetTestHashValue(3, tag)); |
| 142 | 159 |
| 143 // The good pin must be in the chain, the backup pin must not be | 160 // The good pin must be in the chain, the backup pin must not be |
| 144 std::string good_pin = GetTestPin(2, tag); | 161 std::string good_pin = GetTestPin(2, tag); |
| 162 std::string good_pin_unquoted = GetTestPinUnquoted(2, tag); |
| 145 std::string backup_pin = GetTestPin(4, tag); | 163 std::string backup_pin = GetTestPin(4, tag); |
| 146 | 164 |
| 147 EXPECT_FALSE(ParseHPKPHeader(std::string(), chain_hashes, &max_age, | 165 EXPECT_FALSE(ParseHPKPHeader(std::string(), chain_hashes, &max_age, |
| 148 &include_subdomains, &hashes)); | 166 &include_subdomains, &hashes)); |
| 149 EXPECT_FALSE(ParseHPKPHeader(" ", chain_hashes, &max_age, | 167 EXPECT_FALSE(ParseHPKPHeader(" ", chain_hashes, &max_age, |
| 150 &include_subdomains, &hashes)); | 168 &include_subdomains, &hashes)); |
| 151 EXPECT_FALSE(ParseHPKPHeader("abc", chain_hashes, &max_age, | 169 EXPECT_FALSE(ParseHPKPHeader("abc", chain_hashes, &max_age, |
| 152 &include_subdomains, &hashes)); | 170 &include_subdomains, &hashes)); |
| 153 EXPECT_FALSE(ParseHPKPHeader(" abc", chain_hashes, &max_age, | 171 EXPECT_FALSE(ParseHPKPHeader(" abc", chain_hashes, &max_age, |
| 154 &include_subdomains, &hashes)); | 172 &include_subdomains, &hashes)); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 &include_subdomains, &hashes)); | 224 &include_subdomains, &hashes)); |
| 207 EXPECT_FALSE(ParseHPKPHeader("max-age=3488923;", chain_hashes, &max_age, | 225 EXPECT_FALSE(ParseHPKPHeader("max-age=3488923;", chain_hashes, &max_age, |
| 208 &include_subdomains, &hashes)); | 226 &include_subdomains, &hashes)); |
| 209 EXPECT_FALSE(ParseHPKPHeader("max-age=3488923 e", chain_hashes, | 227 EXPECT_FALSE(ParseHPKPHeader("max-age=3488923 e", chain_hashes, |
| 210 &max_age, &include_subdomains, &hashes)); | 228 &max_age, &include_subdomains, &hashes)); |
| 211 EXPECT_FALSE(ParseHPKPHeader("max-age=3488923 includesubdomain", | 229 EXPECT_FALSE(ParseHPKPHeader("max-age=3488923 includesubdomain", |
| 212 chain_hashes, &max_age, &include_subdomains, | 230 chain_hashes, &max_age, &include_subdomains, |
| 213 &hashes)); | 231 &hashes)); |
| 214 EXPECT_FALSE(ParseHPKPHeader("max-age=34889.23", chain_hashes, &max_age, | 232 EXPECT_FALSE(ParseHPKPHeader("max-age=34889.23", chain_hashes, &max_age, |
| 215 &include_subdomains, &hashes)); | 233 &include_subdomains, &hashes)); |
| 234 EXPECT_FALSE( |
| 235 ParseHPKPHeader("max-age=243; " + good_pin_unquoted + ";" + backup_pin, |
| 236 chain_hashes, &max_age, &include_subdomains, &hashes)); |
| 216 | 237 |
| 217 // Check the out args were not updated by checking the default | 238 // Check the out args were not updated by checking the default |
| 218 // values for its predictable fields. | 239 // values for its predictable fields. |
| 219 EXPECT_EQ(0, max_age.InSeconds()); | 240 EXPECT_EQ(0, max_age.InSeconds()); |
| 220 EXPECT_EQ(hashes.size(), (size_t)0); | 241 EXPECT_EQ(hashes.size(), (size_t)0); |
| 221 } | 242 } |
| 222 | 243 |
| 223 TEST_F(HttpSecurityHeadersTest, ValidSTSHeaders) { | 244 TEST_F(HttpSecurityHeadersTest, ValidSTSHeaders) { |
| 224 base::TimeDelta max_age; | 245 base::TimeDelta max_age; |
| 225 base::TimeDelta expect_max_age; | 246 base::TimeDelta expect_max_age; |
| (...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 EXPECT_TRUE(state.ShouldUpgradeToSSL(domain)); | 730 EXPECT_TRUE(state.ShouldUpgradeToSSL(domain)); |
| 710 // The dynamic pins, which do not match |saved_hashes|, should take | 731 // The dynamic pins, which do not match |saved_hashes|, should take |
| 711 // precedence over the static pins and cause the check to fail. | 732 // precedence over the static pins and cause the check to fail. |
| 712 EXPECT_FALSE(state.CheckPublicKeyPins(domain, | 733 EXPECT_FALSE(state.CheckPublicKeyPins(domain, |
| 713 is_issued_by_known_root, | 734 is_issued_by_known_root, |
| 714 saved_hashes, | 735 saved_hashes, |
| 715 &failure_log)); | 736 &failure_log)); |
| 716 } | 737 } |
| 717 | 738 |
| 718 }; // namespace net | 739 }; // namespace net |
| OLD | NEW |