| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/time/time.h" | 5 #include "base/time/time.h" |
| 6 #include "components/cast_certificate/cast_cert_validator.h" | 6 #include "components/cast_certificate/cast_cert_validator.h" |
| 7 #include "components/cast_certificate/cast_cert_validator_test_helpers.h" | 7 #include "components/cast_certificate/cast_cert_validator_test_helpers.h" |
| 8 #include "components/cast_certificate/cast_crl.h" | 8 #include "components/cast_certificate/cast_crl.h" |
| 9 #include "components/cast_certificate/proto/test_suite.pb.h" | 9 #include "components/cast_certificate/proto/test_suite.pb.h" |
| 10 #include "net/cert/internal/cert_errors.h" | 10 #include "net/cert/internal/cert_errors.h" |
| 11 #include "net/cert/internal/trust_store_in_memory.h" | 11 #include "net/cert/internal/trust_store_in_memory.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace cast_certificate { | 14 namespace cast_certificate { |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Creates a trust store using the test roots encoded in the PEM file at |path|. | |
| 18 std::unique_ptr<net::TrustStoreInMemory> CreateTrustStoreFromFile( | |
| 19 const std::string& path) { | |
| 20 std::unique_ptr<net::TrustStoreInMemory> trust_store( | |
| 21 new net::TrustStoreInMemory()); | |
| 22 const auto trusted_test_roots = | |
| 23 cast_certificate::testing::ReadCertificateChainFromFile(path); | |
| 24 for (const auto& trusted_root : trusted_test_roots) { | |
| 25 net::CertErrors errors; | |
| 26 scoped_refptr<net::ParsedCertificate> cert( | |
| 27 net::ParsedCertificate::Create(trusted_root, {}, &errors)); | |
| 28 EXPECT_TRUE(cert) << errors.ToDebugString(); | |
| 29 scoped_refptr<net::TrustAnchor> anchor = | |
| 30 net::TrustAnchor::CreateFromCertificateWithConstraints(std::move(cert)); | |
| 31 trust_store->AddTrustAnchor(std::move(anchor)); | |
| 32 } | |
| 33 return trust_store; | |
| 34 } | |
| 35 | |
| 36 // Converts uint64_t unix timestamp in seconds to base::Time. | |
| 37 base::Time ConvertUnixTimestampSeconds(uint64_t time) { | |
| 38 return base::Time::UnixEpoch() + | |
| 39 base::TimeDelta::FromMilliseconds(time * 1000); | |
| 40 } | |
| 41 | |
| 42 // Indicates the expected result of test step's verification. | 17 // Indicates the expected result of test step's verification. |
| 43 enum TestStepResult { | 18 enum TestStepResult { |
| 44 RESULT_SUCCESS, | 19 RESULT_SUCCESS, |
| 45 RESULT_FAIL, | 20 RESULT_FAIL, |
| 46 }; | 21 }; |
| 47 | 22 |
| 48 // Verifies that the provided certificate chain is valid at the specified time | 23 // Verifies that the provided certificate chain is valid at the specified time |
| 49 // and chains up to a trust anchor. | 24 // and chains up to a trust anchor. |
| 50 bool TestVerifyCertificate(TestStepResult expected_result, | 25 bool TestVerifyCertificate(TestStepResult expected_result, |
| 51 const std::vector<std::string>& certificate_chain, | 26 const std::vector<std::string>& certificate_chain, |
| 52 const base::Time& time, | 27 const base::Time& time, |
| 53 net::TrustStore* cast_trust_store) { | 28 net::TrustStore* cast_trust_store) { |
| 54 std::unique_ptr<CertVerificationContext> context; | 29 std::unique_ptr<CertVerificationContext> context; |
| 55 CastDeviceCertPolicy policy; | 30 CastDeviceCertPolicy policy; |
| 56 int result; | 31 int result = VerifyDeviceCertUsingCustomTrustStore( |
| 57 if (cast_trust_store != nullptr) { | 32 certificate_chain, time, &context, &policy, nullptr, |
| 58 result = VerifyDeviceCertForTest(certificate_chain, time, &context, &policy, | 33 CRLPolicy::CRL_OPTIONAL, cast_trust_store); |
| 59 nullptr, CRLPolicy::CRL_OPTIONAL, | |
| 60 cast_trust_store); | |
| 61 } else { | |
| 62 result = VerifyDeviceCert(certificate_chain, time, &context, &policy, | |
| 63 nullptr, CRLPolicy::CRL_OPTIONAL); | |
| 64 } | |
| 65 if (expected_result != RESULT_SUCCESS) { | 34 if (expected_result != RESULT_SUCCESS) { |
| 66 EXPECT_FALSE(result); | 35 EXPECT_FALSE(result); |
| 67 return !result; | 36 return !result; |
| 68 } | 37 } |
| 69 EXPECT_TRUE(result); | 38 EXPECT_TRUE(result); |
| 70 return result; | 39 return result; |
| 71 } | 40 } |
| 72 | 41 |
| 73 // Verifies that the provided Cast CRL is signed by a trusted issuer | 42 // Verifies that the provided Cast CRL is signed by a trusted issuer |
| 74 // and that the CRL can be parsed successfully. | 43 // and that the CRL can be parsed successfully. |
| 75 // The validity of the CRL is also checked at the specified time. | 44 // The validity of the CRL is also checked at the specified time. |
| 76 bool TestVerifyCRL(TestStepResult expected_result, | 45 bool TestVerifyCRL(TestStepResult expected_result, |
| 77 const std::string& crl_bundle, | 46 const std::string& crl_bundle, |
| 78 const base::Time& time, | 47 const base::Time& time, |
| 79 net::TrustStore* crl_trust_store) { | 48 net::TrustStore* crl_trust_store) { |
| 80 std::unique_ptr<CastCRL> crl; | 49 std::unique_ptr<CastCRL> crl = |
| 81 if (crl_trust_store != nullptr) { | 50 ParseAndVerifyCRLUsingCustomTrustStore(crl_bundle, time, crl_trust_store); |
| 82 crl = ParseAndVerifyCRLForTest(crl_bundle, time, crl_trust_store); | 51 |
| 83 } else { | |
| 84 crl = ParseAndVerifyCRL(crl_bundle, time); | |
| 85 } | |
| 86 if (expected_result != RESULT_SUCCESS) { | 52 if (expected_result != RESULT_SUCCESS) { |
| 87 EXPECT_EQ(crl, nullptr); | 53 EXPECT_EQ(crl, nullptr); |
| 88 return crl == nullptr; | 54 return crl == nullptr; |
| 89 } | 55 } |
| 90 EXPECT_NE(crl, nullptr); | 56 EXPECT_NE(crl, nullptr); |
| 91 return crl != nullptr; | 57 return crl != nullptr; |
| 92 } | 58 } |
| 93 | 59 |
| 94 // Verifies that the certificate chain provided is not revoked according to | 60 // Verifies that the certificate chain provided is not revoked according to |
| 95 // the provided Cast CRL at |cert_time|. | 61 // the provided Cast CRL at |cert_time|. |
| 96 // The provided CRL is verified at |crl_time|. | 62 // The provided CRL is verified at |crl_time|. |
| 97 // If |crl_required| is set, then a valid Cast CRL must be provided. | 63 // If |crl_required| is set, then a valid Cast CRL must be provided. |
| 98 // Otherwise, a missing CRL is be ignored. | 64 // Otherwise, a missing CRL is be ignored. |
| 99 bool TestVerifyRevocation(TestStepResult expected_result, | 65 bool TestVerifyRevocation(TestStepResult expected_result, |
| 100 const std::vector<std::string>& certificate_chain, | 66 const std::vector<std::string>& certificate_chain, |
| 101 const std::string& crl_bundle, | 67 const std::string& crl_bundle, |
| 102 const base::Time& crl_time, | 68 const base::Time& crl_time, |
| 103 const base::Time& cert_time, | 69 const base::Time& cert_time, |
| 104 bool crl_required, | 70 bool crl_required, |
| 105 net::TrustStore* cast_trust_store, | 71 net::TrustStore* cast_trust_store, |
| 106 net::TrustStore* crl_trust_store) { | 72 net::TrustStore* crl_trust_store) { |
| 107 std::unique_ptr<CastCRL> crl; | 73 std::unique_ptr<CastCRL> crl; |
| 108 if (!crl_bundle.empty()) { | 74 if (!crl_bundle.empty()) { |
| 109 if (crl_trust_store != nullptr) { | 75 crl = ParseAndVerifyCRLUsingCustomTrustStore(crl_bundle, crl_time, |
| 110 crl = ParseAndVerifyCRLForTest(crl_bundle, crl_time, crl_trust_store); | 76 crl_trust_store); |
| 111 } else { | |
| 112 crl = ParseAndVerifyCRL(crl_bundle, crl_time); | |
| 113 } | |
| 114 EXPECT_NE(crl.get(), nullptr); | 77 EXPECT_NE(crl.get(), nullptr); |
| 115 } | 78 } |
| 116 | 79 |
| 117 std::unique_ptr<CertVerificationContext> context; | 80 std::unique_ptr<CertVerificationContext> context; |
| 118 CastDeviceCertPolicy policy; | 81 CastDeviceCertPolicy policy; |
| 119 CRLPolicy crl_policy = CRLPolicy::CRL_REQUIRED; | 82 CRLPolicy crl_policy = CRLPolicy::CRL_REQUIRED; |
| 120 if (!crl_required) | 83 if (!crl_required) |
| 121 crl_policy = CRLPolicy::CRL_OPTIONAL; | 84 crl_policy = CRLPolicy::CRL_OPTIONAL; |
| 122 int result; | 85 int result = VerifyDeviceCertUsingCustomTrustStore( |
| 123 if (cast_trust_store != nullptr) { | 86 certificate_chain, cert_time, &context, &policy, crl.get(), crl_policy, |
| 124 result = | 87 cast_trust_store); |
| 125 VerifyDeviceCertForTest(certificate_chain, cert_time, &context, &policy, | |
| 126 crl.get(), crl_policy, cast_trust_store); | |
| 127 } else { | |
| 128 result = VerifyDeviceCert(certificate_chain, cert_time, &context, &policy, | |
| 129 crl.get(), crl_policy); | |
| 130 } | |
| 131 if (expected_result != RESULT_SUCCESS) { | 88 if (expected_result != RESULT_SUCCESS) { |
| 132 EXPECT_FALSE(result); | 89 EXPECT_FALSE(result); |
| 133 return !result; | 90 return !result; |
| 134 } | 91 } |
| 135 EXPECT_TRUE(result); | 92 EXPECT_TRUE(result); |
| 136 return result; | 93 return result; |
| 137 } | 94 } |
| 138 | 95 |
| 139 // Runs a single test case. | 96 // Runs a single test case. |
| 140 bool RunTest(const DeviceCertTest& test_case) { | 97 bool RunTest(const DeviceCertTest& test_case) { |
| 141 std::unique_ptr<net::TrustStoreInMemory> crl_trust_store; | 98 std::unique_ptr<net::TrustStoreInMemory> crl_trust_store; |
| 142 std::unique_ptr<net::TrustStoreInMemory> cast_trust_store; | 99 std::unique_ptr<net::TrustStoreInMemory> cast_trust_store; |
| 143 if (test_case.use_test_trust_anchors()) { | 100 if (test_case.use_test_trust_anchors()) { |
| 144 crl_trust_store = | 101 crl_trust_store = testing::CreateTrustStoreFromFile( |
| 145 CreateTrustStoreFromFile("certificates/cast_crl_test_root_ca.pem"); | 102 "certificates/cast_crl_test_root_ca.pem"); |
| 146 cast_trust_store = | 103 cast_trust_store = |
| 147 CreateTrustStoreFromFile("certificates/cast_test_root_ca.pem"); | 104 testing::CreateTrustStoreFromFile("certificates/cast_test_root_ca.pem"); |
| 148 | 105 |
| 149 EXPECT_TRUE(crl_trust_store.get()); | 106 EXPECT_TRUE(crl_trust_store.get()); |
| 150 EXPECT_TRUE(cast_trust_store.get()); | 107 EXPECT_TRUE(cast_trust_store.get()); |
| 151 } | 108 } |
| 152 | 109 |
| 153 std::vector<std::string> certificate_chain; | 110 std::vector<std::string> certificate_chain; |
| 154 for (auto const& cert : test_case.der_cert_path()) { | 111 for (auto const& cert : test_case.der_cert_path()) { |
| 155 certificate_chain.push_back(cert); | 112 certificate_chain.push_back(cert); |
| 156 } | 113 } |
| 157 | 114 |
| 158 base::Time cert_verification_time = | 115 base::Time cert_verification_time = testing::ConvertUnixTimestampSeconds( |
| 159 ConvertUnixTimestampSeconds(test_case.cert_verification_time_seconds()); | 116 test_case.cert_verification_time_seconds()); |
| 160 | 117 |
| 161 uint64_t crl_verify_time = test_case.crl_verification_time_seconds(); | 118 uint64_t crl_verify_time = test_case.crl_verification_time_seconds(); |
| 162 base::Time crl_verification_time = | 119 base::Time crl_verification_time = |
| 163 ConvertUnixTimestampSeconds(crl_verify_time); | 120 testing::ConvertUnixTimestampSeconds(crl_verify_time); |
| 164 if (crl_verify_time == 0) | 121 if (crl_verify_time == 0) |
| 165 crl_verification_time = cert_verification_time; | 122 crl_verification_time = cert_verification_time; |
| 166 | 123 |
| 167 std::string crl_bundle = test_case.crl_bundle(); | 124 std::string crl_bundle = test_case.crl_bundle(); |
| 168 switch (test_case.expected_result()) { | 125 switch (test_case.expected_result()) { |
| 169 case PATH_VERIFICATION_FAILED: | 126 case PATH_VERIFICATION_FAILED: |
| 170 return TestVerifyCertificate(RESULT_FAIL, certificate_chain, | 127 return TestVerifyCertificate(RESULT_FAIL, certificate_chain, |
| 171 cert_verification_time, | 128 cert_verification_time, |
| 172 cast_trust_store.get()); | 129 cast_trust_store.get()); |
| 173 break; | 130 break; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 } | 208 } |
| 252 } | 209 } |
| 253 | 210 |
| 254 TEST(CastCertificateTest, TestSuite1) { | 211 TEST(CastCertificateTest, TestSuite1) { |
| 255 RunTestSuite("testsuite/testsuite1.pb"); | 212 RunTestSuite("testsuite/testsuite1.pb"); |
| 256 } | 213 } |
| 257 | 214 |
| 258 } // namespace | 215 } // namespace |
| 259 | 216 |
| 260 } // namespace cast_certificate | 217 } // namespace cast_certificate |
| OLD | NEW |