| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/cert/ct_policy_enforcer.h" | 5 #include "net/cert/ct_policy_enforcer.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 const base::Time& start, | 116 const base::Time& start, |
| 117 const base::Time& end, | 117 const base::Time& end, |
| 118 size_t required_scts) { | 118 size_t required_scts) { |
| 119 scoped_refptr<X509Certificate> cert( | 119 scoped_refptr<X509Certificate> cert( |
| 120 new X509Certificate("subject", "issuer", start, end)); | 120 new X509Certificate("subject", "issuer", start, end)); |
| 121 ct::CTVerifyResult result; | 121 ct::CTVerifyResult result; |
| 122 | 122 |
| 123 for (size_t i = 0; i < required_scts - 1; ++i) { | 123 for (size_t i = 0; i < required_scts - 1; ++i) { |
| 124 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, | 124 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, |
| 125 1, std::vector<std::string>(), false, &result); | 125 1, std::vector<std::string>(), false, &result); |
| 126 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( | 126 EXPECT_FALSE( |
| 127 cert.get(), nullptr, result, BoundNetLog())) | 127 policy_enforcer_->DoesConformToCertPolicy(cert.get(), result)) |
| 128 << " for: " << (end - start).InDays() << " and " << required_scts | 128 << " for: " << (end - start).InDays() << " and " << required_scts |
| 129 << " scts=" << result.verified_scts.size() << " i=" << i; | 129 << " scts=" << result.verified_scts.size() << " i=" << i; |
| 130 } | 130 } |
| 131 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, | 131 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 132 std::vector<std::string>(), false, &result); | 132 std::vector<std::string>(), false, &result); |
| 133 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy( | 133 EXPECT_TRUE(policy_enforcer_->DoesConformToCertPolicy(cert.get(), result)) |
| 134 cert.get(), nullptr, result, BoundNetLog())) | |
| 135 << " for: " << (end - start).InDays() << " and " << required_scts | 134 << " for: " << (end - start).InDays() << " and " << required_scts |
| 136 << " scts=" << result.verified_scts.size(); | 135 << " scts=" << result.verified_scts.size(); |
| 137 } | 136 } |
| 138 | 137 |
| 139 protected: | 138 protected: |
| 140 scoped_ptr<CTPolicyEnforcer> policy_enforcer_; | 139 scoped_ptr<CTPolicyEnforcer> policy_enforcer_; |
| 141 scoped_refptr<X509Certificate> chain_; | 140 scoped_refptr<X509Certificate> chain_; |
| 142 std::string google_log_id_; | 141 std::string google_log_id_; |
| 143 std::string non_google_log_id_; | 142 std::string non_google_log_id_; |
| 144 }; | 143 }; |
| 145 | 144 |
| 146 TEST_F(CTPolicyEnforcerTest, | 145 TEST_F(CTPolicyEnforcerTest, |
| 147 DoesNotConformToCTEVPolicyNotEnoughDiverseSCTsAllGoogle) { | 146 DoesNotConformToCTEVPolicyNotEnoughDiverseSCTsAllGoogle) { |
| 148 ct::CTVerifyResult result; | 147 ct::CTVerifyResult result; |
| 149 FillResultWithRepeatedLogID(google_log_id_, 2, true, &result); | 148 FillResultWithRepeatedLogID(google_log_id_, 2, true, &result); |
| 150 | 149 |
| 151 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( | 150 EXPECT_FALSE(policy_enforcer_->DoesConformToCertPolicy(chain_.get(), result)); |
| 152 chain_.get(), nullptr, result, BoundNetLog())); | |
| 153 } | 151 } |
| 154 | 152 |
| 155 TEST_F(CTPolicyEnforcerTest, | 153 TEST_F(CTPolicyEnforcerTest, |
| 156 DoesNotConformToCTEVPolicyNotEnoughDiverseSCTsAllNonGoogle) { | 154 DoesNotConformToCTEVPolicyNotEnoughDiverseSCTsAllNonGoogle) { |
| 157 ct::CTVerifyResult result; | 155 ct::CTVerifyResult result; |
| 158 FillResultWithRepeatedLogID(non_google_log_id_, 2, true, &result); | 156 FillResultWithRepeatedLogID(non_google_log_id_, 2, true, &result); |
| 159 | 157 |
| 160 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( | 158 EXPECT_FALSE(policy_enforcer_->DoesConformToCertPolicy(chain_.get(), result)); |
| 161 chain_.get(), nullptr, result, BoundNetLog())); | |
| 162 } | 159 } |
| 163 | 160 |
| 164 TEST_F(CTPolicyEnforcerTest, ConformsToCTEVPolicyIfSCTBeforeEnforcementDate) { | 161 TEST_F(CTPolicyEnforcerTest, ConformsToCTEVPolicyIfSCTBeforeEnforcementDate) { |
| 165 ct::CTVerifyResult result; | 162 ct::CTVerifyResult result; |
| 166 FillResultWithRepeatedLogID(non_google_log_id_, 2, false, &result); | 163 FillResultWithRepeatedLogID(non_google_log_id_, 2, false, &result); |
| 167 | 164 |
| 168 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy(chain_.get(), nullptr, | 165 EXPECT_TRUE(policy_enforcer_->DoesConformToCertPolicy(chain_.get(), result)); |
| 169 result, BoundNetLog())); | |
| 170 } | 166 } |
| 171 | 167 |
| 172 TEST_F(CTPolicyEnforcerTest, ConformsToCTEVPolicyWithNonEmbeddedSCTs) { | 168 TEST_F(CTPolicyEnforcerTest, ConformsToCTEVPolicyWithNonEmbeddedSCTs) { |
| 173 ct::CTVerifyResult result; | 169 ct::CTVerifyResult result; |
| 174 FillResultWithSCTsOfOrigin( | 170 FillResultWithSCTsOfOrigin( |
| 175 ct::SignedCertificateTimestamp::SCT_FROM_TLS_EXTENSION, 2, &result); | 171 ct::SignedCertificateTimestamp::SCT_FROM_TLS_EXTENSION, 2, &result); |
| 176 | 172 |
| 177 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy(chain_.get(), nullptr, | 173 EXPECT_TRUE(policy_enforcer_->DoesConformToCertPolicy(chain_.get(), result)); |
| 178 result, BoundNetLog())); | |
| 179 } | 174 } |
| 180 | 175 |
| 181 TEST_F(CTPolicyEnforcerTest, ConformsToCTEVPolicyWithEmbeddedSCTs) { | 176 TEST_F(CTPolicyEnforcerTest, ConformsToCTEVPolicyWithEmbeddedSCTs) { |
| 182 // This chain_ is valid for 10 years - over 121 months - so requires 5 SCTs. | 177 // This chain_ is valid for 10 years - over 121 months - so requires 5 SCTs. |
| 183 ct::CTVerifyResult result; | 178 ct::CTVerifyResult result; |
| 184 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 5, | 179 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 5, |
| 185 &result); | 180 &result); |
| 186 | 181 |
| 187 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy(chain_.get(), nullptr, | 182 EXPECT_TRUE(policy_enforcer_->DoesConformToCertPolicy(chain_.get(), result)); |
| 188 result, BoundNetLog())); | |
| 189 } | 183 } |
| 190 | 184 |
| 191 TEST_F(CTPolicyEnforcerTest, DoesNotConformToCTEVPolicyNotEnoughSCTs) { | 185 TEST_F(CTPolicyEnforcerTest, DoesNotConformToCTEVPolicyNotEnoughSCTs) { |
| 192 scoped_refptr<ct::EVCertsWhitelist> non_including_whitelist( | 186 scoped_refptr<ct::EVCertsWhitelist> non_including_whitelist( |
| 193 new DummyEVCertsWhitelist(true, false)); | 187 new DummyEVCertsWhitelist(true, false)); |
| 194 // This chain_ is valid for 10 years - over 121 months - so requires 5 SCTs. | 188 // This chain_ is valid for 10 years - over 121 months - so requires 5 SCTs. |
| 195 // However, as there are only two logs, two SCTs will be required - supply one | 189 // However, as there are only two logs, two SCTs will be required - supply one |
| 196 // to guarantee the test fails. | 190 // to guarantee the test fails. |
| 197 ct::CTVerifyResult result; | 191 ct::CTVerifyResult result; |
| 198 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, | 192 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 199 &result); | 193 &result); |
| 200 | 194 |
| 201 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( | 195 EXPECT_FALSE(policy_enforcer_->DoesConformToCertPolicy(chain_.get(), result)); |
| 202 chain_.get(), non_including_whitelist.get(), result, BoundNetLog())); | 196 EXPECT_FALSE(policy_enforcer_->DoesConformToEVPolicy( |
| 197 chain_.get(), result, non_including_whitelist.get(), BoundNetLog())); |
| 203 | 198 |
| 204 // ... but should be OK if whitelisted. | 199 // ... but should be OK if whitelisted. |
| 205 scoped_refptr<ct::EVCertsWhitelist> whitelist( | 200 scoped_refptr<ct::EVCertsWhitelist> whitelist( |
| 206 new DummyEVCertsWhitelist(true, true)); | 201 new DummyEVCertsWhitelist(true, true)); |
| 207 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy( | 202 EXPECT_TRUE(policy_enforcer_->DoesConformToEVPolicy( |
| 208 chain_.get(), whitelist.get(), result, BoundNetLog())); | 203 chain_.get(), result, whitelist.get(), BoundNetLog())); |
| 209 } | 204 } |
| 210 | 205 |
| 211 TEST_F(CTPolicyEnforcerTest, DoesNotConformToPolicyInvalidDates) { | 206 TEST_F(CTPolicyEnforcerTest, DoesNotConformToPolicyInvalidDates) { |
| 212 scoped_refptr<X509Certificate> no_valid_dates_cert(new X509Certificate( | 207 scoped_refptr<X509Certificate> no_valid_dates_cert(new X509Certificate( |
| 213 "subject", "issuer", base::Time(), base::Time::Now())); | 208 "subject", "issuer", base::Time(), base::Time::Now())); |
| 214 ct::CTVerifyResult result; | 209 ct::CTVerifyResult result; |
| 215 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 5, | 210 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 5, |
| 216 &result); | 211 &result); |
| 217 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( | 212 EXPECT_FALSE(policy_enforcer_->DoesConformToCertPolicy( |
| 218 no_valid_dates_cert.get(), nullptr, result, BoundNetLog())); | 213 no_valid_dates_cert.get(), result)); |
| 219 // ... but should be OK if whitelisted. | 214 // ... but should be OK if whitelisted. |
| 220 scoped_refptr<ct::EVCertsWhitelist> whitelist( | 215 scoped_refptr<ct::EVCertsWhitelist> whitelist( |
| 221 new DummyEVCertsWhitelist(true, true)); | 216 new DummyEVCertsWhitelist(true, true)); |
| 222 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy( | 217 EXPECT_TRUE(policy_enforcer_->DoesConformToEVPolicy( |
| 223 chain_.get(), whitelist.get(), result, BoundNetLog())); | 218 chain_.get(), result, whitelist.get(), BoundNetLog())); |
| 224 } | 219 } |
| 225 | 220 |
| 226 TEST_F(CTPolicyEnforcerTest, | 221 TEST_F(CTPolicyEnforcerTest, |
| 227 ConformsToPolicyExactNumberOfSCTsForValidityPeriod) { | 222 ConformsToPolicyExactNumberOfSCTsForValidityPeriod) { |
| 228 // Test multiple validity periods | 223 // Test multiple validity periods |
| 229 const struct TestData { | 224 const struct TestData { |
| 230 base::Time validity_start; | 225 base::Time validity_start; |
| 231 base::Time validity_end; | 226 base::Time validity_end; |
| 232 size_t scts_required; | 227 size_t scts_required; |
| 233 } kTestData[] = {{// Cert valid for 14 months, needs 2 SCTs. | 228 } kTestData[] = { |
| 234 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), | 229 {// Cert valid for 14 months, needs 2 SCTs. |
| 235 base::Time::FromUTCExploded({2016, 6, 0, 6, 11, 25, 0, 0}), | 230 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), |
| 236 2}, | 231 base::Time::FromUTCExploded({2016, 6, 0, 6, 11, 25, 0, 0}), 2}, |
| 237 {// Cert valid for exactly 15 months, needs 3 SCTs. | 232 {// Cert valid for exactly 15 months, needs 3 SCTs. |
| 238 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), | 233 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), |
| 239 base::Time::FromUTCExploded({2016, 6, 0, 25, 11, 25, 0, 0}), | 234 base::Time::FromUTCExploded({2016, 6, 0, 25, 11, 25, 0, 0}), 3}, |
| 240 3}, | 235 {// Cert valid for over 15 months, needs 3 SCTs. |
| 241 {// Cert valid for over 15 months, needs 3 SCTs. | 236 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), |
| 242 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), | 237 base::Time::FromUTCExploded({2016, 6, 0, 27, 11, 25, 0, 0}), 3}, |
| 243 base::Time::FromUTCExploded({2016, 6, 0, 27, 11, 25, 0, 0}), | 238 {// Cert valid for exactly 27 months, needs 3 SCTs. |
| 244 3}, | 239 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), |
| 245 {// Cert valid for exactly 27 months, needs 3 SCTs. | 240 base::Time::FromUTCExploded({2017, 6, 0, 25, 11, 25, 0, 0}), 3}, |
| 246 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), | 241 {// Cert valid for over 27 months, needs 4 SCTs. |
| 247 base::Time::FromUTCExploded({2017, 6, 0, 25, 11, 25, 0, 0}), | 242 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), |
| 248 3}, | 243 base::Time::FromUTCExploded({2017, 6, 0, 28, 11, 25, 0, 0}), 4}, |
| 249 {// Cert valid for over 27 months, needs 4 SCTs. | 244 {// Cert valid for exactly 39 months, needs 4 SCTs. |
| 250 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), | 245 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), |
| 251 base::Time::FromUTCExploded({2017, 6, 0, 28, 11, 25, 0, 0}), | 246 base::Time::FromUTCExploded({2018, 6, 0, 25, 11, 25, 0, 0}), 4}, |
| 252 4}, | 247 {// Cert valid for over 39 months, needs 5 SCTs. |
| 253 {// Cert valid for exactly 39 months, needs 4 SCTs. | 248 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), |
| 254 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), | 249 base::Time::FromUTCExploded({2018, 6, 0, 27, 11, 25, 0, 0}), 5}}; |
| 255 base::Time::FromUTCExploded({2018, 6, 0, 25, 11, 25, 0, 0}), | |
| 256 4}, | |
| 257 {// Cert valid for over 39 months, needs 5 SCTs. | |
| 258 base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}), | |
| 259 base::Time::FromUTCExploded({2018, 6, 0, 27, 11, 25, 0, 0}), | |
| 260 5}}; | |
| 261 | 250 |
| 262 for (size_t i = 0; i < arraysize(kTestData); ++i) { | 251 for (size_t i = 0; i < arraysize(kTestData); ++i) { |
| 263 SCOPED_TRACE(i); | 252 SCOPED_TRACE(i); |
| 264 CheckCertificateCompliesWithExactNumberOfEmbeddedSCTs( | 253 CheckCertificateCompliesWithExactNumberOfEmbeddedSCTs( |
| 265 kTestData[i].validity_start, kTestData[i].validity_end, | 254 kTestData[i].validity_start, kTestData[i].validity_end, |
| 266 kTestData[i].scts_required); | 255 kTestData[i].scts_required); |
| 267 } | 256 } |
| 268 } | 257 } |
| 269 | 258 |
| 270 TEST_F(CTPolicyEnforcerTest, ConformsToPolicyByEVWhitelistPresence) { | 259 TEST_F(CTPolicyEnforcerTest, ConformsToPolicyByEVWhitelistPresence) { |
| 271 scoped_refptr<ct::EVCertsWhitelist> whitelist( | 260 scoped_refptr<ct::EVCertsWhitelist> whitelist( |
| 272 new DummyEVCertsWhitelist(true, true)); | 261 new DummyEVCertsWhitelist(true, true)); |
| 273 | 262 |
| 274 ct::CTVerifyResult result; | 263 ct::CTVerifyResult result; |
| 275 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, | 264 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 276 &result); | 265 &result); |
| 277 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy( | 266 EXPECT_FALSE(policy_enforcer_->DoesConformToCertPolicy(chain_.get(), result)); |
| 278 chain_.get(), whitelist.get(), result, BoundNetLog())); | 267 EXPECT_TRUE(policy_enforcer_->DoesConformToEVPolicy( |
| 268 chain_.get(), result, whitelist.get(), BoundNetLog())); |
| 279 } | 269 } |
| 280 | 270 |
| 281 TEST_F(CTPolicyEnforcerTest, IgnoresInvalidEVWhitelist) { | 271 TEST_F(CTPolicyEnforcerTest, IgnoresInvalidEVWhitelist) { |
| 282 scoped_refptr<ct::EVCertsWhitelist> whitelist( | 272 scoped_refptr<ct::EVCertsWhitelist> whitelist( |
| 283 new DummyEVCertsWhitelist(false, true)); | 273 new DummyEVCertsWhitelist(false, true)); |
| 284 | 274 |
| 285 ct::CTVerifyResult result; | 275 ct::CTVerifyResult result; |
| 286 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, | 276 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 287 &result); | 277 &result); |
| 288 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( | 278 EXPECT_FALSE(policy_enforcer_->DoesConformToCertPolicy(chain_.get(), result)); |
| 289 chain_.get(), whitelist.get(), result, BoundNetLog())); | 279 EXPECT_FALSE(policy_enforcer_->DoesConformToEVPolicy( |
| 280 chain_.get(), result, whitelist.get(), BoundNetLog())); |
| 290 } | 281 } |
| 291 | 282 |
| 292 TEST_F(CTPolicyEnforcerTest, IgnoresNullEVWhitelist) { | 283 TEST_F(CTPolicyEnforcerTest, IgnoresNullEVWhitelist) { |
| 293 ct::CTVerifyResult result; | 284 ct::CTVerifyResult result; |
| 294 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, | 285 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 295 &result); | 286 &result); |
| 296 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( | 287 EXPECT_FALSE(policy_enforcer_->DoesConformToCertPolicy(chain_.get(), result)); |
| 297 chain_.get(), nullptr, result, BoundNetLog())); | 288 EXPECT_FALSE(policy_enforcer_->DoesConformToEVPolicy(chain_.get(), result, |
| 289 nullptr, BoundNetLog())); |
| 298 } | 290 } |
| 299 | 291 |
| 300 } // namespace | 292 } // namespace |
| 301 | 293 |
| 302 } // namespace net | 294 } // namespace net |
| OLD | NEW |