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