OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/cert/cert_policy_enforcer.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "net/base/test_data_directory.h" |
| 11 #include "net/cert/ct_ev_whitelist.h" |
| 12 #include "net/cert/ct_verify_result.h" |
| 13 #include "net/cert/x509_certificate.h" |
| 14 #include "net/test/cert_test_util.h" |
| 15 #include "net/test/ct_test_util.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace net { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class DummyEVCertsWhitelist : public ct::EVCertsWhitelist { |
| 24 public: |
| 25 DummyEVCertsWhitelist(bool is_valid_response, bool contains_hash_response) |
| 26 : canned_is_valid_(is_valid_response), |
| 27 canned_contains_response_(contains_hash_response) {} |
| 28 |
| 29 bool IsValid() const override { return canned_is_valid_; } |
| 30 |
| 31 bool ContainsCertificateHash( |
| 32 const std::string& certificate_hash) const override { |
| 33 return canned_contains_response_; |
| 34 } |
| 35 |
| 36 protected: |
| 37 ~DummyEVCertsWhitelist() override {} |
| 38 |
| 39 private: |
| 40 bool canned_is_valid_; |
| 41 bool canned_contains_response_; |
| 42 }; |
| 43 |
| 44 class CertPolicyEnforcerTest : public ::testing::Test { |
| 45 public: |
| 46 virtual void SetUp() override { |
| 47 policy_enforcer_.reset(new CertPolicyEnforcer(5, true)); |
| 48 |
| 49 std::string der_test_cert(ct::GetDerEncodedX509Cert()); |
| 50 chain_ = X509Certificate::CreateFromBytes(der_test_cert.data(), |
| 51 der_test_cert.size()); |
| 52 ASSERT_TRUE(chain_.get()); |
| 53 } |
| 54 |
| 55 void FillResultWithSCTsOfOrigin( |
| 56 ct::SignedCertificateTimestamp::Origin desired_origin, |
| 57 int num_scts, |
| 58 ct::CTVerifyResult* result) { |
| 59 for (int i = 0; i < num_scts; ++i) { |
| 60 scoped_refptr<ct::SignedCertificateTimestamp> sct( |
| 61 new ct::SignedCertificateTimestamp()); |
| 62 sct->origin = desired_origin; |
| 63 result->verified_scts.push_back(sct); |
| 64 } |
| 65 } |
| 66 |
| 67 protected: |
| 68 scoped_ptr<CertPolicyEnforcer> policy_enforcer_; |
| 69 scoped_refptr<X509Certificate> chain_; |
| 70 }; |
| 71 |
| 72 TEST_F(CertPolicyEnforcerTest, ConformsToCTEVPolicyWithNonEmbeddedSCTs) { |
| 73 ct::CTVerifyResult result; |
| 74 FillResultWithSCTsOfOrigin( |
| 75 ct::SignedCertificateTimestamp::SCT_FROM_TLS_EXTENSION, 2, &result); |
| 76 |
| 77 EXPECT_TRUE( |
| 78 policy_enforcer_->DoesConformToCTEVPolicy(chain_.get(), nullptr, result)); |
| 79 } |
| 80 |
| 81 TEST_F(CertPolicyEnforcerTest, ConformsToCTEVPolicyWithEmbeddedSCTs) { |
| 82 // This chain_ is valid for 10 years - over 121 months - so requires 5 SCTs. |
| 83 ct::CTVerifyResult result; |
| 84 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 5, |
| 85 &result); |
| 86 |
| 87 EXPECT_TRUE( |
| 88 policy_enforcer_->DoesConformToCTEVPolicy(chain_.get(), nullptr, result)); |
| 89 } |
| 90 |
| 91 TEST_F(CertPolicyEnforcerTest, DoesNotConformToCTEVPolicyNotEnoughSCTs) { |
| 92 scoped_refptr<ct::EVCertsWhitelist> non_including_whitelist( |
| 93 new DummyEVCertsWhitelist(true, false)); |
| 94 // This chain_ is valid for 10 years - over 121 months - so requires 5 SCTs. |
| 95 // However, as there are only two logs, two SCTs will be required - supply one |
| 96 // to guarantee the test fails. |
| 97 ct::CTVerifyResult result; |
| 98 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 99 &result); |
| 100 |
| 101 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( |
| 102 chain_.get(), non_including_whitelist.get(), result)); |
| 103 |
| 104 // ... but should be OK if whitelisted. |
| 105 scoped_refptr<ct::EVCertsWhitelist> whitelist( |
| 106 new DummyEVCertsWhitelist(true, true)); |
| 107 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy( |
| 108 chain_.get(), whitelist.get(), result)); |
| 109 } |
| 110 |
| 111 TEST_F(CertPolicyEnforcerTest, DoesNotEnforceCTPolicyIfNotRequired) { |
| 112 scoped_ptr<CertPolicyEnforcer> enforcer(new CertPolicyEnforcer(3, false)); |
| 113 |
| 114 ct::CTVerifyResult result; |
| 115 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 116 &result); |
| 117 // Expect true despite the chain not having enough SCTs as the policy |
| 118 // is not enforced. |
| 119 EXPECT_TRUE(enforcer->DoesConformToCTEVPolicy(chain_.get(), nullptr, result)); |
| 120 } |
| 121 |
| 122 TEST_F(CertPolicyEnforcerTest, DoesNotConformToPolicyInvalidDates) { |
| 123 scoped_refptr<X509Certificate> no_valid_dates_cert(new X509Certificate( |
| 124 "subject", "issuer", base::Time(), base::Time::Now())); |
| 125 ct::CTVerifyResult result; |
| 126 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 5, |
| 127 &result); |
| 128 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( |
| 129 no_valid_dates_cert.get(), nullptr, result)); |
| 130 // ... but should be OK if whitelisted. |
| 131 scoped_refptr<ct::EVCertsWhitelist> whitelist( |
| 132 new DummyEVCertsWhitelist(true, true)); |
| 133 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy( |
| 134 chain_.get(), whitelist.get(), result)); |
| 135 } |
| 136 |
| 137 TEST_F(CertPolicyEnforcerTest, |
| 138 ConformsToPolicyExactNumberOfSCTsForValidityPeriod) { |
| 139 // Test multiple validity periods: Over 27 months, Over 15 months (but less |
| 140 // than 27 months), |
| 141 // Less than 15 months. |
| 142 const size_t validity_period[] = {12, 19, 30, 50}; |
| 143 const size_t needed_scts[] = {2, 3, 4, 5}; |
| 144 |
| 145 for (int i = 0; i < 3; ++i) { |
| 146 size_t curr_validity = validity_period[i]; |
| 147 scoped_refptr<X509Certificate> cert(new X509Certificate( |
| 148 "subject", "issuer", base::Time::Now(), |
| 149 base::Time::Now() + base::TimeDelta::FromDays(31 * curr_validity))); |
| 150 size_t curr_required_scts = needed_scts[i]; |
| 151 ct::CTVerifyResult result; |
| 152 for (size_t j = 0; j < curr_required_scts - 1; ++j) { |
| 153 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, |
| 154 1, &result); |
| 155 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy(cert.get(), |
| 156 nullptr, result)) |
| 157 << " for: " << curr_validity << " and " << curr_required_scts |
| 158 << " scts=" << result.verified_scts.size() << " j=" << j; |
| 159 } |
| 160 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 161 &result); |
| 162 EXPECT_TRUE( |
| 163 policy_enforcer_->DoesConformToCTEVPolicy(cert.get(), nullptr, result)); |
| 164 } |
| 165 } |
| 166 |
| 167 TEST_F(CertPolicyEnforcerTest, |
| 168 ConformsToPolicyButDoesNotRequireMoreThanNumLogs) { |
| 169 scoped_ptr<CertPolicyEnforcer> enforcer(new CertPolicyEnforcer(2, true)); |
| 170 |
| 171 ct::CTVerifyResult result; |
| 172 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 2, |
| 173 &result); |
| 174 // Expect true despite the chain not having enough SCTs according to the |
| 175 // policy |
| 176 // since we only have 2 logs. |
| 177 EXPECT_TRUE(enforcer->DoesConformToCTEVPolicy(chain_.get(), nullptr, result)); |
| 178 } |
| 179 |
| 180 TEST_F(CertPolicyEnforcerTest, ConformsToPolicyByEVWhitelistPresence) { |
| 181 scoped_refptr<ct::EVCertsWhitelist> whitelist( |
| 182 new DummyEVCertsWhitelist(true, true)); |
| 183 |
| 184 ct::CTVerifyResult result; |
| 185 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 186 &result); |
| 187 EXPECT_TRUE(policy_enforcer_->DoesConformToCTEVPolicy( |
| 188 chain_.get(), whitelist.get(), result)); |
| 189 } |
| 190 |
| 191 TEST_F(CertPolicyEnforcerTest, IgnoresInvalidEVWhitelist) { |
| 192 scoped_refptr<ct::EVCertsWhitelist> whitelist( |
| 193 new DummyEVCertsWhitelist(false, true)); |
| 194 |
| 195 ct::CTVerifyResult result; |
| 196 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 197 &result); |
| 198 EXPECT_FALSE(policy_enforcer_->DoesConformToCTEVPolicy( |
| 199 chain_.get(), whitelist.get(), result)); |
| 200 } |
| 201 |
| 202 TEST_F(CertPolicyEnforcerTest, IgnoresNullEVWhitelist) { |
| 203 ct::CTVerifyResult result; |
| 204 FillResultWithSCTsOfOrigin(ct::SignedCertificateTimestamp::SCT_EMBEDDED, 1, |
| 205 &result); |
| 206 EXPECT_FALSE( |
| 207 policy_enforcer_->DoesConformToCTEVPolicy(chain_.get(), nullptr, result)); |
| 208 } |
| 209 |
| 210 } // namespace |
| 211 |
| 212 } // namespace net |
OLD | NEW |