| // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "net/cert/cert_verify_proc.h" |
| |
| #include <vector> |
| |
| #include "base/bind.h" |
| #include "base/callback_helpers.h" |
| #include "base/files/file_path.h" |
| #include "base/files/file_util.h" |
| #include "base/logging.h" |
| #include "base/macros.h" |
| #include "base/message_loop/message_pump_type.h" |
| #include "base/rand_util.h" |
| #include "base/stl_util.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_util.h" |
| #include "base/test/metrics/histogram_tester.h" |
| #include "base/test/scoped_feature_list.h" |
| #include "base/test/task_environment.h" |
| #include "base/threading/thread.h" |
| #include "build/build_config.h" |
| #include "crypto/sha2.h" |
| #include "net/base/net_errors.h" |
| #include "net/cert/asn1_util.h" |
| #include "net/cert/cert_net_fetcher.h" |
| #include "net/cert/cert_status_flags.h" |
| #include "net/cert/cert_verifier.h" |
| #include "net/cert/cert_verify_proc_builtin.h" |
| #include "net/cert/cert_verify_result.h" |
| #include "net/cert/crl_set.h" |
| #include "net/cert/ev_root_ca_metadata.h" |
| #include "net/cert/internal/parse_certificate.h" |
| #include "net/cert/internal/signature_algorithm.h" |
| #include "net/cert/pem.h" |
| #include "net/cert/test_root_certs.h" |
| #include "net/cert/x509_certificate.h" |
| #include "net/cert/x509_util.h" |
| #include "net/cert_net/cert_net_fetcher_url_request.h" |
| #include "net/der/input.h" |
| #include "net/der/parser.h" |
| #include "net/log/test_net_log.h" |
| #include "net/proxy_resolution/proxy_config.h" |
| #include "net/proxy_resolution/proxy_config_service_fixed.h" |
| #include "net/test/cert_builder.h" |
| #include "net/test/cert_test_util.h" |
| #include "net/test/embedded_test_server/embedded_test_server.h" |
| #include "net/test/embedded_test_server/http_request.h" |
| #include "net/test/embedded_test_server/http_response.h" |
| #include "net/test/gtest_util.h" |
| #include "net/test/revocation_builder.h" |
| #include "net/test/test_certificate_data.h" |
| #include "net/test/test_data_directory.h" |
| #include "net/url_request/url_request_context.h" |
| #include "net/url_request/url_request_context_builder.h" |
| #include "net/url_request/url_request_context_getter.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "third_party/boringssl/src/include/openssl/mem.h" |
| |
| #if defined(OS_ANDROID) |
| #include "base/android/build_info.h" |
| #include "net/cert/cert_verify_proc_android.h" |
| #elif defined(OS_IOS) |
| #include "base/ios/ios_util.h" |
| #include "net/cert/cert_verify_proc_ios.h" |
| #elif defined(OS_MAC) |
| #include "base/mac/mac_util.h" |
| #include "net/cert/cert_verify_proc_mac.h" |
| #include "net/cert/internal/trust_store_mac.h" |
| #elif defined(OS_WIN) |
| #include "base/win/windows_version.h" |
| #include "net/cert/cert_verify_proc_win.h" |
| #endif |
| |
| // TODO(crbug.com/649017): Add tests that only certificates with |
| // serverAuth are accepted. |
| |
| using net::test::IsError; |
| using net::test::IsOk; |
| |
| using base::HexEncode; |
| |
| namespace net { |
| |
| namespace { |
| |
| const char kTLSFeatureExtensionHistogram[] = |
| "Net.Certificate.TLSFeatureExtensionWithPrivateRoot"; |
| const char kTLSFeatureExtensionOCSPHistogram[] = |
| "Net.Certificate.TLSFeatureExtensionWithPrivateRootHasOCSP"; |
| const char kTrustAnchorVerifyHistogram[] = "Net.Certificate.TrustAnchor.Verify"; |
| const char kTrustAnchorVerifyOutOfDateHistogram[] = |
| "Net.Certificate.TrustAnchor.VerifyOutOfDate"; |
| |
| // Mock CertVerifyProc that sets the CertVerifyResult to a given value for |
| // all certificates that are Verify()'d |
| class MockCertVerifyProc : public CertVerifyProc { |
| public: |
| explicit MockCertVerifyProc(const CertVerifyResult& result) |
| : result_(result) {} |
| // CertVerifyProc implementation: |
| bool SupportsAdditionalTrustAnchors() const override { return false; } |
| |
| protected: |
| ~MockCertVerifyProc() override = default; |
| |
| private: |
| int VerifyInternal(X509Certificate* cert, |
| const std::string& hostname, |
| const std::string& ocsp_response, |
| const std::string& sct_list, |
| int flags, |
| CRLSet* crl_set, |
| const CertificateList& additional_trust_anchors, |
| CertVerifyResult* verify_result, |
| const NetLogWithSource& net_log) override; |
| |
| const CertVerifyResult result_; |
| |
| DISALLOW_COPY_AND_ASSIGN(MockCertVerifyProc); |
| }; |
| |
| int MockCertVerifyProc::VerifyInternal( |
| X509Certificate* cert, |
| const std::string& hostname, |
| const std::string& ocsp_response, |
| const std::string& sct_list, |
| int flags, |
| CRLSet* crl_set, |
| const CertificateList& additional_trust_anchors, |
| CertVerifyResult* verify_result, |
| const NetLogWithSource& net_log) { |
| *verify_result = result_; |
| verify_result->verified_cert = cert; |
| return OK; |
| } |
| |
| // This enum identifies a concrete implemenation of CertVerifyProc. |
| // |
| // The type is erased by CreateCertVerifyProc(), however needs to be known for |
| // some of the test expectations. |
| enum CertVerifyProcType { |
| CERT_VERIFY_PROC_ANDROID, |
| CERT_VERIFY_PROC_IOS, |
| CERT_VERIFY_PROC_MAC, |
| CERT_VERIFY_PROC_WIN, |
| CERT_VERIFY_PROC_BUILTIN, |
| }; |
| |
| // Wrapper for base::mac::IsAtLeastOS10_12() to avoid littering ifdefs. |
| bool IsMacAtLeastOS10_12() { |
| #if defined(OS_MAC) |
| return base::mac::IsAtLeastOS10_12(); |
| #else |
| return false; |
| #endif |
| } |
| |
| // Returns a textual description of the CertVerifyProc implementation |
| // that is being tested, used to give better names to parameterized |
| // tests. |
| std::string VerifyProcTypeToName( |
| const testing::TestParamInfo<CertVerifyProcType>& params) { |
| switch (params.param) { |
| case CERT_VERIFY_PROC_ANDROID: |
| return "CertVerifyProcAndroid"; |
| case CERT_VERIFY_PROC_IOS: |
| return "CertVerifyProcIOS"; |
| case CERT_VERIFY_PROC_MAC: |
| return "CertVerifyProcMac"; |
| case CERT_VERIFY_PROC_WIN: |
| return "CertVerifyProcWin"; |
| case CERT_VERIFY_PROC_BUILTIN: |
| return "CertVerifyProcBuiltin"; |
| } |
| |
| return nullptr; |
| } |
| |
| scoped_refptr<CertVerifyProc> CreateCertVerifyProc( |
| CertVerifyProcType type, |
| scoped_refptr<CertNetFetcher> cert_net_fetcher) { |
| switch (type) { |
| #if defined(OS_ANDROID) |
| case CERT_VERIFY_PROC_ANDROID: |
| return new CertVerifyProcAndroid(std::move(cert_net_fetcher)); |
| #elif defined(OS_IOS) |
| case CERT_VERIFY_PROC_IOS: |
| return new CertVerifyProcIOS(); |
| #elif defined(OS_MAC) |
| case CERT_VERIFY_PROC_MAC: |
| return new CertVerifyProcMac(); |
| #elif defined(OS_WIN) |
| case CERT_VERIFY_PROC_WIN: |
| return new CertVerifyProcWin(); |
| #endif |
| case CERT_VERIFY_PROC_BUILTIN: |
| return CreateCertVerifyProcBuiltin( |
| std::move(cert_net_fetcher), |
| SystemTrustStoreProvider::CreateDefaultForSSL()); |
| default: |
| return nullptr; |
| } |
| } |
| |
| // The set of all CertVerifyProcTypes that tests should be parameterized on. |
| // This needs to be kept in sync with CertVerifyProc::CreateSystemVerifyProc() |
| // and the platforms where CreateSslSystemTrustStore() is not a dummy store. |
| // TODO(crbug.com/649017): Enable CERT_VERIFY_PROC_BUILTIN everywhere. Right |
| // now this is gated on having CertVerifyProcBuiltin understand the roots added |
| // via TestRootCerts. |
| const std::vector<CertVerifyProcType> kAllCertVerifiers = { |
| #if defined(OS_ANDROID) |
| CERT_VERIFY_PROC_ANDROID |
| #elif defined(OS_IOS) |
| CERT_VERIFY_PROC_IOS |
| #elif defined(OS_MAC) |
| CERT_VERIFY_PROC_MAC, CERT_VERIFY_PROC_BUILTIN |
| #elif defined(OS_WIN) |
| CERT_VERIFY_PROC_WIN |
| #elif defined(OS_FUCHSIA) || defined(OS_LINUX) || defined(OS_CHROMEOS) |
| CERT_VERIFY_PROC_BUILTIN |
| #else |
| #error Unsupported platform |
| #endif |
| }; // namespace |
| |
| // Returns true if a test root added through ScopedTestRoot can verify |
| // successfully as a target certificate with chain of length 1 on the given |
| // CertVerifyProcType. |
| bool ScopedTestRootCanTrustTargetCert(CertVerifyProcType verify_proc_type) { |
| return verify_proc_type == CERT_VERIFY_PROC_MAC || |
| verify_proc_type == CERT_VERIFY_PROC_IOS || |
| verify_proc_type == CERT_VERIFY_PROC_ANDROID; |
| } |
| |
| // Returns true if a non-self-signed CA certificate added through |
| // ScopedTestRoot can verify successfully as the root of a chain by the given |
| // CertVerifyProcType. |
| bool ScopedTestRootCanTrustIntermediateCert( |
| CertVerifyProcType verify_proc_type) { |
| return verify_proc_type == CERT_VERIFY_PROC_MAC || |
| verify_proc_type == CERT_VERIFY_PROC_IOS || |
| verify_proc_type == CERT_VERIFY_PROC_BUILTIN || |
| verify_proc_type == CERT_VERIFY_PROC_ANDROID; |
| } |
| |
| // TODO(crbug.com/649017): This is not parameterized by the CertVerifyProc |
| // because the CertVerifyProc::Verify() does this unconditionally based on the |
| // platform. |
| bool AreSHA1IntermediatesAllowed() { |
| #if defined(OS_WIN) |
| // TODO(rsleevi): Remove this once https://crbug.com/588789 is resolved |
| // for Windows 7/2008 users. |
| // Note: This must be kept in sync with cert_verify_proc.cc |
| return base::win::GetVersion() < base::win::Version::WIN8; |
| #else |
| return false; |
| #endif |
| } |
| |
| std::string MakeRandomHexString(size_t num_bytes) { |
| std::vector<char> rand_bytes; |
| rand_bytes.resize(num_bytes); |
| |
| base::RandBytes(rand_bytes.data(), rand_bytes.size()); |
| return base::HexEncode(rand_bytes.data(), rand_bytes.size()); |
| } |
| |
| } // namespace |
| |
| // This fixture is for tests that apply to concrete implementations of |
| // CertVerifyProc. It will be run for all of the concrete CertVerifyProc types. |
| // |
| // It is called "Internal" as it tests the internal methods like |
| // "VerifyInternal()". |
| class CertVerifyProcInternalTest |
| : public testing::TestWithParam<CertVerifyProcType> { |
| protected: |
| void SetUp() override { SetUpWithCertNetFetcher(nullptr); } |
| |
| // CertNetFetcher may be initialized by subclasses that want to use net |
| // fetching by calling SetUpWithCertNetFetcher instead of SetUp. |
| void SetUpWithCertNetFetcher(scoped_refptr<CertNetFetcher> cert_net_fetcher) { |
| CertVerifyProcType type = verify_proc_type(); |
| verify_proc_ = CreateCertVerifyProc(type, std::move(cert_net_fetcher)); |
| ASSERT_TRUE(verify_proc_); |
| } |
| |
| int Verify(X509Certificate* cert, |
| const std::string& hostname, |
| int flags, |
| CRLSet* crl_set, |
| const CertificateList& additional_trust_anchors, |
| CertVerifyResult* verify_result, |
| const NetLogWithSource& net_log) { |
| return verify_proc_->Verify(cert, hostname, /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), flags, crl_set, |
| additional_trust_anchors, verify_result, |
| net_log); |
| } |
| |
| int Verify(X509Certificate* cert, |
| const std::string& hostname, |
| int flags, |
| CRLSet* crl_set, |
| const CertificateList& additional_trust_anchors, |
| CertVerifyResult* verify_result) { |
| return Verify(cert, hostname, flags, crl_set, additional_trust_anchors, |
| verify_result, NetLogWithSource()); |
| } |
| |
| CertVerifyProcType verify_proc_type() const { return GetParam(); } |
| |
| bool SupportsAdditionalTrustAnchors() const { |
| return verify_proc_->SupportsAdditionalTrustAnchors(); |
| } |
| |
| bool SupportsReturningVerifiedChain() const { |
| #if defined(OS_ANDROID) |
| // Before API level 17 (SDK_VERSION_JELLY_BEAN_MR1), Android does |
| // not expose the APIs necessary to get at the verified |
| // certificate chain. |
| if (verify_proc_type() == CERT_VERIFY_PROC_ANDROID && |
| base::android::BuildInfo::GetInstance()->sdk_int() < |
| base::android::SDK_VERSION_JELLY_BEAN_MR1) |
| return false; |
| #endif |
| return true; |
| } |
| |
| // Returns true if the RSA/DSA keysize will be considered weak on the current |
| // platform. IsInvalidRsaDsaKeySize should be checked prior, since some very |
| // weak keys may be considered invalid. |
| bool IsWeakRsaDsaKeySize(int size) const { |
| #if defined(OS_IOS) |
| // Beginning with iOS 13, the minimum key size for RSA/DSA algorithms is |
| // 2048 bits. See https://support.apple.com/en-us/HT210176 |
| if (verify_proc_type() == CERT_VERIFY_PROC_IOS && |
| base::ios::IsRunningOnIOS13OrLater()) { |
| return size < 2048; |
| } |
| #elif defined(OS_MAC) |
| // Beginning with macOS 10.15, the minimum key size for RSA/DSA algorithms |
| // is 2048 bits. See https://support.apple.com/en-us/HT210176 |
| if (verify_proc_type() == CERT_VERIFY_PROC_MAC && |
| base::mac::IsAtLeastOS10_15()) { |
| return size < 2048; |
| } |
| #endif |
| |
| return size < 1024; |
| } |
| |
| // Returns true if the RSA/DSA keysize will be considered invalid on the |
| // current platform. |
| bool IsInvalidRsaDsaKeySize(int size) const { |
| #if defined(OS_IOS) |
| if (base::ios::IsRunningOnIOS12OrLater()) { |
| // On iOS using SecTrustEvaluateWithError it is not possible to |
| // distinguish between weak and invalid key sizes. |
| return IsWeakRsaDsaKeySize(size); |
| } |
| #elif defined(OS_MAC) |
| // Starting with Mac OS 10.12, certs with keys < 1024 are invalid. |
| if (verify_proc_type() == CERT_VERIFY_PROC_MAC && |
| base::mac::IsAtLeastOS10_12()) { |
| return size < 1024; |
| } |
| #endif |
| |
| // This platform does not mark certificates with weak keys as invalid. |
| return false; |
| } |
| |
| static bool ParseKeyType(const std::string& key_type, |
| std::string* type, |
| int* size) { |
| size_t pos = key_type.find("-"); |
| std::string size_str = key_type.substr(0, pos); |
| *type = key_type.substr(pos + 1); |
| return base::StringToInt(size_str, size); |
| } |
| |
| // Some platforms may reject certificates with very weak keys as invalid. |
| bool IsInvalidKeyType(const std::string& key_type) const { |
| std::string type; |
| int size = 0; |
| if (!ParseKeyType(key_type, &type, &size)) |
| return false; |
| |
| if (type == "rsa" || type == "dsa") |
| return IsInvalidRsaDsaKeySize(size); |
| |
| return false; |
| } |
| |
| // Currently, only RSA and DSA keys are checked for weakness, and our example |
| // weak size is 768. These could change in the future. |
| // |
| // Note that this means there may be false negatives: keys for other |
| // algorithms and which are weak will pass this test. |
| // |
| // Also, IsInvalidKeyType should be checked prior, since some weak keys may be |
| // considered invalid. |
| bool IsWeakKeyType(const std::string& key_type) const { |
| std::string type; |
| int size = 0; |
| if (!ParseKeyType(key_type, &type, &size)) |
| return false; |
| |
| if (type == "rsa" || type == "dsa") |
| return IsWeakRsaDsaKeySize(size); |
| |
| return false; |
| } |
| |
| bool SupportsCRLSet() const { |
| return verify_proc_type() == CERT_VERIFY_PROC_WIN || |
| verify_proc_type() == CERT_VERIFY_PROC_MAC || |
| verify_proc_type() == CERT_VERIFY_PROC_BUILTIN; |
| } |
| |
| bool SupportsCRLSetsInPathBuilding() const { |
| return verify_proc_type() == CERT_VERIFY_PROC_WIN || |
| verify_proc_type() == CERT_VERIFY_PROC_BUILTIN; |
| } |
| |
| bool SupportsEV() const { |
| // TODO(crbug.com/117478): Android and iOS do not support EV. |
| return verify_proc_type() == CERT_VERIFY_PROC_WIN || |
| verify_proc_type() == CERT_VERIFY_PROC_MAC || |
| verify_proc_type() == CERT_VERIFY_PROC_BUILTIN; |
| } |
| |
| bool SupportsSoftFailRevChecking() const { |
| return verify_proc_type() == CERT_VERIFY_PROC_WIN || |
| verify_proc_type() == CERT_VERIFY_PROC_MAC || |
| verify_proc_type() == CERT_VERIFY_PROC_BUILTIN; |
| } |
| |
| bool SupportsRevCheckingRequiredLocalAnchors() const { |
| return verify_proc_type() == CERT_VERIFY_PROC_WIN || |
| verify_proc_type() == CERT_VERIFY_PROC_BUILTIN; |
| } |
| |
| CertVerifyProc* verify_proc() const { return verify_proc_.get(); } |
| |
| private: |
| scoped_refptr<CertVerifyProc> verify_proc_; |
| }; |
| |
| INSTANTIATE_TEST_SUITE_P(All, |
| CertVerifyProcInternalTest, |
| testing::ValuesIn(kAllCertVerifiers), |
| VerifyProcTypeToName); |
| |
| // Tests that a certificate is recognized as EV, when the valid EV policy OID |
| // for the trust anchor is the second candidate EV oid in the target |
| // certificate. This is a regression test for crbug.com/705285. |
| // Started failing: https://crbug.com/1094358 |
| TEST_P(CertVerifyProcInternalTest, DISABLED_EVVerificationMultipleOID) { |
| if (!SupportsEV()) { |
| LOG(INFO) << "Skipping test as EV verification is not yet supported"; |
| return; |
| } |
| |
| // TODO(eroman): Update this test to use a synthetic certificate, so the test |
| // does not break in the future. The certificate chain in question expires on |
| // Jun 12 14:33:43 2020 GMT, at which point this test will start failing. |
| if (base::Time::Now() > |
| base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(1591972423)) { |
| FAIL() << "This test uses a certificate chain which is now expired. Please " |
| "disable and file a bug."; |
| return; |
| } |
| |
| scoped_refptr<X509Certificate> chain = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "login.trustwave.com.pem", |
| X509Certificate::FORMAT_PEM_CERT_SEQUENCE); |
| ASSERT_TRUE(chain); |
| |
| // Build a CRLSet that covers the target certificate. |
| // |
| // This way CRLSet coverage will be sufficient for EV revocation checking, |
| // so this test does not depend on online revocation checking. |
| ASSERT_GE(chain->intermediate_buffers().size(), 1u); |
| base::StringPiece spki; |
| ASSERT_TRUE( |
| asn1::ExtractSPKIFromDERCert(x509_util::CryptoBufferAsStringPiece( |
| chain->intermediate_buffers()[0].get()), |
| &spki)); |
| SHA256HashValue spki_sha256; |
| crypto::SHA256HashString(spki, spki_sha256.data, sizeof(spki_sha256.data)); |
| scoped_refptr<CRLSet> crl_set( |
| CRLSet::ForTesting(false, &spki_sha256, "", "", {})); |
| |
| CertVerifyResult verify_result; |
| int flags = 0; |
| int error = Verify(chain.get(), "login.trustwave.com", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_IS_EV); |
| } |
| |
| // Target cert has an EV policy, and verifies successfully, but has a chain of |
| // length 1 because the target cert was directly trusted in the trust store. |
| // Should verify OK but not with STATUS_IS_EV. |
| TEST_P(CertVerifyProcInternalTest, TrustedTargetCertWithEVPolicy) { |
| // The policy that "explicit-policy-chain.pem" target certificate asserts. |
| static const char kEVTestCertPolicy[] = "1.2.3.4"; |
| ScopedTestEVPolicy scoped_test_ev_policy( |
| EVRootCAMetadata::GetInstance(), SHA256HashValue(), kEVTestCertPolicy); |
| |
| scoped_refptr<X509Certificate> cert = |
| ImportCertFromFile(GetTestCertsDirectory(), "explicit-policy-chain.pem"); |
| ASSERT_TRUE(cert); |
| ScopedTestRoot scoped_test_root(cert.get()); |
| |
| CertVerifyResult verify_result; |
| int flags = 0; |
| int error = |
| Verify(cert.get(), "policy_test.example", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| if (ScopedTestRootCanTrustTargetCert(verify_proc_type())) { |
| EXPECT_THAT(error, IsOk()); |
| ASSERT_TRUE(verify_result.verified_cert); |
| EXPECT_TRUE(verify_result.verified_cert->intermediate_buffers().empty()); |
| } else { |
| EXPECT_THAT(error, IsError(ERR_CERT_AUTHORITY_INVALID)); |
| } |
| EXPECT_FALSE(verify_result.cert_status & CERT_STATUS_IS_EV); |
| } |
| |
| // Target cert has an EV policy, and verifies successfully with a chain of |
| // length 1, and its fingerprint matches the cert fingerprint for that ev |
| // policy. This should never happen in reality, but just test that things don't |
| // explode if it does. |
| TEST_P(CertVerifyProcInternalTest, |
| TrustedTargetCertWithEVPolicyAndEVFingerprint) { |
| // The policy that "explicit-policy-chain.pem" target certificate asserts. |
| static const char kEVTestCertPolicy[] = "1.2.3.4"; |
| // This the fingerprint of the "explicit-policy-chain.pem" target certificate. |
| // See net/data/ssl/certificates/explicit-policy-chain.pem |
| static const SHA256HashValue kEVTestCertFingerprint = { |
| {0x71, 0xac, 0xfa, 0x12, 0xa4, 0x42, 0x31, 0x3c, 0xff, 0x10, 0xd2, |
| 0x9d, 0xb6, 0x1b, 0x4a, 0xe8, 0x25, 0x4e, 0x77, 0xd3, 0x9f, 0xa3, |
| 0x2f, 0xb3, 0x19, 0x8d, 0x46, 0x9f, 0xb7, 0x73, 0x07, 0x30}}; |
| ScopedTestEVPolicy scoped_test_ev_policy(EVRootCAMetadata::GetInstance(), |
| kEVTestCertFingerprint, |
| kEVTestCertPolicy); |
| |
| scoped_refptr<X509Certificate> cert = |
| ImportCertFromFile(GetTestCertsDirectory(), "explicit-policy-chain.pem"); |
| ASSERT_TRUE(cert); |
| ScopedTestRoot scoped_test_root(cert.get()); |
| |
| CertVerifyResult verify_result; |
| int flags = 0; |
| int error = |
| Verify(cert.get(), "policy_test.example", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| if (ScopedTestRootCanTrustTargetCert(verify_proc_type())) { |
| EXPECT_THAT(error, IsOk()); |
| ASSERT_TRUE(verify_result.verified_cert); |
| EXPECT_TRUE(verify_result.verified_cert->intermediate_buffers().empty()); |
| } else { |
| EXPECT_THAT(error, IsError(ERR_CERT_AUTHORITY_INVALID)); |
| } |
| // An EV Root certificate should never be used as an end-entity certificate. |
| EXPECT_FALSE(verify_result.cert_status & CERT_STATUS_IS_EV); |
| } |
| |
| // Target cert has an EV policy, and has a valid path to the EV root, but the |
| // intermediate has been trusted directly. Should stop building the path at the |
| // intermediate and verify OK but not with STATUS_IS_EV. |
| // See https://crbug.com/979801 |
| TEST_P(CertVerifyProcInternalTest, TrustedIntermediateCertWithEVPolicy) { |
| if (!SupportsEV()) { |
| LOG(INFO) << "Skipping test as EV verification is not yet supported"; |
| return; |
| } |
| if (!ScopedTestRootCanTrustIntermediateCert(verify_proc_type())) { |
| LOG(INFO) << "Skipping test as intermediate cert cannot be trusted"; |
| return; |
| } |
| |
| CertificateList orig_certs = CreateCertificateListFromFile( |
| GetTestCertsDirectory(), "explicit-policy-chain.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(3U, orig_certs.size()); |
| |
| for (bool trust_the_intermediate : {false, true}) { |
| // Need to build unique certs for each try otherwise caching can break |
| // things. |
| CertBuilder root(orig_certs[2]->cert_buffer(), nullptr); |
| CertBuilder intermediate(orig_certs[1]->cert_buffer(), &root); |
| CertBuilder leaf(orig_certs[0]->cert_buffer(), &intermediate); |
| |
| // The policy that "explicit-policy-chain.pem" target certificate asserts. |
| static const char kEVTestCertPolicy[] = "1.2.3.4"; |
| // Consider the root of the test chain a valid EV root for the test policy. |
| ScopedTestEVPolicy scoped_test_ev_policy( |
| EVRootCAMetadata::GetInstance(), |
| X509Certificate::CalculateFingerprint256(root.GetCertBuffer()), |
| kEVTestCertPolicy); |
| |
| // CRLSet which covers the leaf. |
| base::StringPiece intermediate_spki; |
| ASSERT_TRUE(asn1::ExtractSPKIFromDERCert( |
| x509_util::CryptoBufferAsStringPiece(intermediate.GetCertBuffer()), |
| &intermediate_spki)); |
| SHA256HashValue intermediate_spki_hash; |
| crypto::SHA256HashString(intermediate_spki, &intermediate_spki_hash, |
| sizeof(SHA256HashValue)); |
| scoped_refptr<CRLSet> crl_set = |
| CRLSet::ForTesting(false, &intermediate_spki_hash, "", "", {}); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(intermediate.GetCertBuffer())); |
| scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(leaf.GetCertBuffer()), std::move(intermediates)); |
| ASSERT_TRUE(cert.get()); |
| |
| scoped_refptr<X509Certificate> intermediate_cert = |
| X509Certificate::CreateFromBuffer( |
| bssl::UpRef(intermediate.GetCertBuffer()), {}); |
| ASSERT_TRUE(intermediate_cert.get()); |
| |
| scoped_refptr<X509Certificate> root_cert = |
| X509Certificate::CreateFromBuffer(bssl::UpRef(root.GetCertBuffer()), |
| {}); |
| ASSERT_TRUE(root_cert.get()); |
| |
| if (!trust_the_intermediate) { |
| // First trust just the root. This verifies that the test setup is |
| // actually correct. |
| ScopedTestRoot scoped_test_root({root_cert}); |
| CertVerifyResult verify_result; |
| int flags = 0; |
| int error = Verify(cert.get(), "policy_test.example", flags, |
| crl_set.get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| ASSERT_TRUE(verify_result.verified_cert); |
| // Verified chain should include the intermediate and the root. |
| EXPECT_EQ(2U, verify_result.verified_cert->intermediate_buffers().size()); |
| // Should be EV. |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_IS_EV); |
| } else { |
| // Now try with trusting both the intermediate and the root. |
| ScopedTestRoot scoped_test_root({intermediate_cert, root_cert}); |
| CertVerifyResult verify_result; |
| int flags = 0; |
| int error = Verify(cert.get(), "policy_test.example", flags, |
| crl_set.get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| ASSERT_TRUE(verify_result.verified_cert); |
| // Verified chain should only go to the trusted intermediate, not the |
| // root. |
| EXPECT_EQ(1U, verify_result.verified_cert->intermediate_buffers().size()); |
| // Should not be EV. |
| EXPECT_FALSE(verify_result.cert_status & CERT_STATUS_IS_EV); |
| } |
| } |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, CertWithNullInCommonNameAndNoSAN) { |
| std::unique_ptr<CertBuilder> leaf, intermediate, root; |
| CertBuilder::CreateSimpleChain(&leaf, &intermediate, &root); |
| ASSERT_TRUE(leaf && intermediate && root); |
| |
| leaf->EraseExtension(SubjectAltNameOid()); |
| |
| std::string common_name; |
| common_name += "www.fake.com"; |
| common_name += '\0'; |
| common_name += "a" + MakeRandomHexString(12) + ".example.com"; |
| leaf->SetSubjectCommonName(common_name); |
| |
| // Trust the root and build a chain to verify that includes the intermediate. |
| ScopedTestRoot scoped_root(root->GetX509Certificate().get()); |
| scoped_refptr<X509Certificate> chain = leaf->GetX509CertificateChain(); |
| ASSERT_TRUE(chain.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(chain.get(), "www.fake.com", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| |
| // This actually fails because Chrome only looks for hostnames in |
| // SubjectAltNames now and no SubjectAltName is present. |
| EXPECT_THAT(error, IsError(ERR_CERT_COMMON_NAME_INVALID)); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, CertWithNullInCommonNameAndValidSAN) { |
| std::unique_ptr<CertBuilder> leaf, intermediate, root; |
| CertBuilder::CreateSimpleChain(&leaf, &intermediate, &root); |
| ASSERT_TRUE(leaf && intermediate && root); |
| |
| leaf->SetSubjectAltName("www.fake.com"); |
| |
| std::string common_name; |
| common_name += "www.fake.com"; |
| common_name += '\0'; |
| common_name += "a" + MakeRandomHexString(12) + ".example.com"; |
| leaf->SetSubjectCommonName(common_name); |
| |
| // Trust the root and build a chain to verify that includes the intermediate. |
| ScopedTestRoot scoped_root(root->GetX509Certificate().get()); |
| scoped_refptr<X509Certificate> chain = leaf->GetX509CertificateChain(); |
| ASSERT_TRUE(chain.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(chain.get(), "www.fake.com", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| |
| // SubjectAltName is valid and Chrome does not use the common name. |
| EXPECT_THAT(error, IsOk()); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, CertWithNullInSAN) { |
| std::unique_ptr<CertBuilder> leaf, intermediate, root; |
| CertBuilder::CreateSimpleChain(&leaf, &intermediate, &root); |
| ASSERT_TRUE(leaf && intermediate && root); |
| |
| std::string hostname; |
| hostname += "www.fake.com"; |
| hostname += '\0'; |
| hostname += "a" + MakeRandomHexString(12) + ".example.com"; |
| leaf->SetSubjectAltName(hostname); |
| |
| // Trust the root and build a chain to verify that includes the intermediate. |
| ScopedTestRoot scoped_root(root->GetX509Certificate().get()); |
| scoped_refptr<X509Certificate> chain = leaf->GetX509CertificateChain(); |
| ASSERT_TRUE(chain.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(chain.get(), "www.fake.com", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| |
| // SubjectAltName is invalid. |
| EXPECT_THAT(error, IsError(ERR_CERT_COMMON_NAME_INVALID)); |
| } |
| |
| // Tests the case where the target certificate is accepted by |
| // X509CertificateBytes, but has errors that should cause verification to fail. |
| TEST_P(CertVerifyProcInternalTest, InvalidTarget) { |
| base::FilePath certs_dir = |
| GetTestNetDataDirectory().AppendASCII("parse_certificate_unittest"); |
| scoped_refptr<X509Certificate> bad_cert = |
| ImportCertFromFile(certs_dir, "signature_algorithm_null.pem"); |
| ASSERT_TRUE(bad_cert); |
| |
| scoped_refptr<X509Certificate> ok_cert( |
| ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem")); |
| ASSERT_TRUE(ok_cert); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(ok_cert->cert_buffer())); |
| scoped_refptr<X509Certificate> cert_with_bad_target( |
| X509Certificate::CreateFromBuffer(bssl::UpRef(bad_cert->cert_buffer()), |
| std::move(intermediates))); |
| ASSERT_TRUE(cert_with_bad_target); |
| EXPECT_EQ(1U, cert_with_bad_target->intermediate_buffers().size()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert_with_bad_target.get(), "127.0.0.1", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_INVALID); |
| EXPECT_THAT(error, IsError(ERR_CERT_INVALID)); |
| } |
| |
| // Tests the case where an intermediate certificate is accepted by |
| // X509CertificateBytes, but has errors that should prevent using it during |
| // verification. The verification should succeed, since the intermediate |
| // wasn't necessary. |
| TEST_P(CertVerifyProcInternalTest, UnnecessaryInvalidIntermediate) { |
| ScopedTestRoot test_root( |
| ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem").get()); |
| |
| base::FilePath certs_dir = |
| GetTestNetDataDirectory().AppendASCII("parse_certificate_unittest"); |
| bssl::UniquePtr<CRYPTO_BUFFER> bad_cert = |
| x509_util::CreateCryptoBuffer(base::StringPiece("invalid")); |
| ASSERT_TRUE(bad_cert); |
| |
| scoped_refptr<X509Certificate> ok_cert( |
| ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem")); |
| ASSERT_TRUE(ok_cert); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(std::move(bad_cert)); |
| scoped_refptr<X509Certificate> cert_with_bad_intermediate( |
| X509Certificate::CreateFromBuffer(bssl::UpRef(ok_cert->cert_buffer()), |
| std::move(intermediates))); |
| ASSERT_TRUE(cert_with_bad_intermediate); |
| EXPECT_EQ(1U, cert_with_bad_intermediate->intermediate_buffers().size()); |
| |
| RecordingNetLogObserver net_log_observer(NetLogCaptureMode::kDefault); |
| NetLogWithSource net_log(NetLogWithSource::Make( |
| net::NetLog::Get(), net::NetLogSourceType::CERT_VERIFIER_TASK)); |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = Verify(cert_with_bad_intermediate.get(), "127.0.0.1", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), |
| &verify_result, net_log); |
| |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0u, verify_result.cert_status); |
| |
| auto events = net_log_observer.GetEntriesForSource(net_log.source()); |
| EXPECT_FALSE(events.empty()); |
| |
| auto event = std::find_if(events.begin(), events.end(), [](const auto& e) { |
| return e.type == NetLogEventType::CERT_VERIFY_PROC; |
| }); |
| ASSERT_NE(event, events.end()); |
| EXPECT_EQ(net::NetLogEventPhase::BEGIN, event->phase); |
| ASSERT_TRUE(event->params.is_dict()); |
| const std::string* host = event->params.FindStringKey("host"); |
| ASSERT_TRUE(host); |
| EXPECT_EQ("127.0.0.1", *host); |
| |
| if (verify_proc_type() == CERT_VERIFY_PROC_BUILTIN) { |
| event = std::find_if(events.begin(), events.end(), [](const auto& e) { |
| return e.type == NetLogEventType::CERT_VERIFY_PROC_INPUT_CERT; |
| }); |
| ASSERT_NE(event, events.end()); |
| EXPECT_EQ(net::NetLogEventPhase::NONE, event->phase); |
| ASSERT_TRUE(event->params.is_dict()); |
| const std::string* errors = event->params.FindStringKey("errors"); |
| ASSERT_TRUE(errors); |
| EXPECT_EQ( |
| "ERROR: Failed parsing Certificate SEQUENCE\nERROR: Failed parsing " |
| "Certificate\n", |
| *errors); |
| } |
| } |
| |
| // A regression test for http://crbug.com/31497. |
| TEST_P(CertVerifyProcInternalTest, IntermediateCARequireExplicitPolicy) { |
| if (verify_proc_type() == CERT_VERIFY_PROC_ANDROID) { |
| // Disabled on Android, as the Android verification libraries require an |
| // explicit policy to be specified, even when anyPolicy is permitted. |
| LOG(INFO) << "Skipping test on Android"; |
| return; |
| } |
| |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| |
| CertificateList certs = CreateCertificateListFromFile( |
| certs_dir, "explicit-policy-chain.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(3U, certs.size()); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(certs[1]->cert_buffer())); |
| |
| scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates)); |
| ASSERT_TRUE(cert.get()); |
| |
| ScopedTestRoot scoped_root(certs[2].get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert.get(), "policy_test.example", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0u, verify_result.cert_status); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, RejectExpiredCert) { |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| |
| // Load root_ca_cert.pem into the test root store. |
| ScopedTestRoot test_root( |
| ImportCertFromFile(certs_dir, "root_ca_cert.pem").get()); |
| |
| scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile( |
| certs_dir, "expired_cert.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(cert); |
| ASSERT_EQ(0U, cert->intermediate_buffers().size()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert.get(), "127.0.0.1", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_DATE_INVALID)); |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_DATE_INVALID); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, RejectWeakKeys) { |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| typedef std::vector<std::string> Strings; |
| Strings key_types; |
| |
| // generate-weak-test-chains.sh currently has: |
| // key_types="768-rsa 1024-rsa 2048-rsa prime256v1-ecdsa" |
| // We must use the same key types here. The filenames generated look like: |
| // 2048-rsa-ee-by-768-rsa-intermediate.pem |
| key_types.push_back("768-rsa"); |
| key_types.push_back("1024-rsa"); |
| key_types.push_back("2048-rsa"); |
| key_types.push_back("prime256v1-ecdsa"); |
| |
| // Add the root that signed the intermediates for this test. |
| scoped_refptr<X509Certificate> root_cert = |
| ImportCertFromFile(certs_dir, "2048-rsa-root.pem"); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), root_cert.get()); |
| ScopedTestRoot scoped_root(root_cert.get()); |
| |
| // Now test each chain. |
| for (Strings::const_iterator ee_type = key_types.begin(); |
| ee_type != key_types.end(); ++ee_type) { |
| for (Strings::const_iterator signer_type = key_types.begin(); |
| signer_type != key_types.end(); ++signer_type) { |
| std::string basename = |
| *ee_type + "-ee-by-" + *signer_type + "-intermediate.pem"; |
| SCOPED_TRACE(basename); |
| scoped_refptr<X509Certificate> ee_cert = |
| ImportCertFromFile(certs_dir, basename); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), ee_cert.get()); |
| |
| basename = *signer_type + "-intermediate.pem"; |
| scoped_refptr<X509Certificate> intermediate = |
| ImportCertFromFile(certs_dir, basename); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), intermediate.get()); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(intermediate->cert_buffer())); |
| scoped_refptr<X509Certificate> cert_chain = |
| X509Certificate::CreateFromBuffer(bssl::UpRef(ee_cert->cert_buffer()), |
| std::move(intermediates)); |
| ASSERT_TRUE(cert_chain); |
| |
| CertVerifyResult verify_result; |
| int error = Verify(cert_chain.get(), "127.0.0.1", 0, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), |
| &verify_result); |
| |
| if (IsInvalidKeyType(*ee_type) || IsInvalidKeyType(*signer_type)) { |
| EXPECT_NE(OK, error); |
| EXPECT_EQ(CERT_STATUS_INVALID, |
| verify_result.cert_status & CERT_STATUS_INVALID); |
| } else if (IsWeakKeyType(*ee_type) || IsWeakKeyType(*signer_type)) { |
| EXPECT_NE(OK, error); |
| EXPECT_EQ(CERT_STATUS_WEAK_KEY, |
| verify_result.cert_status & CERT_STATUS_WEAK_KEY); |
| EXPECT_EQ(0u, verify_result.cert_status & CERT_STATUS_INVALID); |
| } else { |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status & CERT_STATUS_WEAK_KEY); |
| } |
| } |
| } |
| } |
| |
| // Regression test for http://crbug.com/108514. |
| TEST_P(CertVerifyProcInternalTest, ExtraneousMD5RootCert) { |
| if (!SupportsReturningVerifiedChain()) { |
| LOG(INFO) << "Skipping this test in this platform."; |
| return; |
| } |
| |
| if (verify_proc_type() == CERT_VERIFY_PROC_MAC) { |
| // Disabled on OS X - Security.framework doesn't ignore superflous |
| // certificates provided by servers. |
| // TODO(eroman): Is this still needed? |
| LOG(INFO) << "Skipping this test as Security.framework doesn't ignore " |
| "superflous certificates provided by servers."; |
| return; |
| } |
| |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| |
| scoped_refptr<X509Certificate> server_cert = |
| ImportCertFromFile(certs_dir, "cross-signed-leaf.pem"); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), server_cert.get()); |
| |
| scoped_refptr<X509Certificate> extra_cert = |
| ImportCertFromFile(certs_dir, "cross-signed-root-md5.pem"); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), extra_cert.get()); |
| |
| scoped_refptr<X509Certificate> root_cert = |
| ImportCertFromFile(certs_dir, "cross-signed-root-sha256.pem"); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), root_cert.get()); |
| |
| ScopedTestRoot scoped_root(root_cert.get()); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(extra_cert->cert_buffer())); |
| scoped_refptr<X509Certificate> cert_chain = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(server_cert->cert_buffer()), std::move(intermediates)); |
| ASSERT_TRUE(cert_chain); |
| |
| CertVerifyResult verify_result; |
| int flags = 0; |
| int error = |
| Verify(cert_chain.get(), "127.0.0.1", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| |
| // The extra MD5 root should be discarded |
| ASSERT_TRUE(verify_result.verified_cert.get()); |
| ASSERT_EQ(1u, verify_result.verified_cert->intermediate_buffers().size()); |
| EXPECT_TRUE(x509_util::CryptoBufferEqual( |
| verify_result.verified_cert->intermediate_buffers().front().get(), |
| root_cert->cert_buffer())); |
| |
| EXPECT_FALSE(verify_result.has_md5); |
| } |
| |
| // Test for bug 94673. |
| TEST_P(CertVerifyProcInternalTest, GoogleDigiNotarTest) { |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| |
| scoped_refptr<X509Certificate> server_cert = |
| ImportCertFromFile(certs_dir, "google_diginotar.pem"); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), server_cert.get()); |
| |
| scoped_refptr<X509Certificate> intermediate_cert = |
| ImportCertFromFile(certs_dir, "diginotar_public_ca_2025.pem"); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), intermediate_cert.get()); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(intermediate_cert->cert_buffer())); |
| scoped_refptr<X509Certificate> cert_chain = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(server_cert->cert_buffer()), std::move(intermediates)); |
| ASSERT_TRUE(cert_chain); |
| |
| CertVerifyResult verify_result; |
| int flags = CertVerifyProc::VERIFY_REV_CHECKING_ENABLED; |
| int error = |
| Verify(cert_chain.get(), "mail.google.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_NE(OK, error); |
| |
| // Now turn off revocation checking. Certificate verification should still |
| // fail. |
| flags = 0; |
| error = |
| Verify(cert_chain.get(), "mail.google.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_NE(OK, error); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, NameConstraintsOk) { |
| CertificateList ca_cert_list = |
| CreateCertificateListFromFile(GetTestCertsDirectory(), "root_ca_cert.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, ca_cert_list.size()); |
| ScopedTestRoot test_root(ca_cert_list[0].get()); |
| |
| scoped_refptr<X509Certificate> leaf = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "name_constraint_good.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(leaf); |
| ASSERT_EQ(0U, leaf->intermediate_buffers().size()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(leaf.get(), "test.example.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| |
| error = |
| Verify(leaf.get(), "foo.test2.example.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| } |
| |
| // This fixture is for testing the verification of a certificate chain which |
| // has some sort of mismatched signature algorithm (i.e. |
| // Certificate.signatureAlgorithm and TBSCertificate.algorithm are different). |
| class CertVerifyProcInspectSignatureAlgorithmsTest : public ::testing::Test { |
| protected: |
| // In the test setup, SHA384 is given special treatment as an unknown |
| // algorithm. |
| static constexpr DigestAlgorithm kUnknownDigestAlgorithm = |
| DigestAlgorithm::Sha384; |
| |
| struct CertParams { |
| // Certificate.signatureAlgorithm |
| DigestAlgorithm cert_algorithm; |
| |
| // TBSCertificate.algorithm |
| DigestAlgorithm tbs_algorithm; |
| }; |
| |
| // On some platforms trying to import a certificate with mismatched signature |
| // will fail. Consequently the rest of the tests can't be performed. |
| WARN_UNUSED_RESULT bool SupportsImportingMismatchedAlgorithms() const { |
| #if defined(OS_IOS) |
| LOG(INFO) << "Skipping test on iOS because certs with mismatched " |
| "algorithms cannot be imported"; |
| return false; |
| #elif defined(OS_MAC) |
| if (base::mac::IsAtLeastOS10_12()) { |
| LOG(INFO) << "Skipping test on macOS >= 10.12 because certs with " |
| "mismatched algorithms cannot be imported"; |
| return false; |
| } |
| return true; |
| #else |
| return true; |
| #endif |
| } |
| |
| // Shorthand for VerifyChain() where only the leaf's parameters need |
| // to be specified. |
| WARN_UNUSED_RESULT int VerifyLeaf(const CertParams& leaf_params) { |
| return VerifyChain({// Target |
| leaf_params, |
| // Root |
| {DigestAlgorithm::Sha256, DigestAlgorithm::Sha256}}); |
| } |
| |
| // Shorthand for VerifyChain() where only the intermediate's parameters need |
| // to be specified. |
| WARN_UNUSED_RESULT int VerifyIntermediate( |
| const CertParams& intermediate_params) { |
| return VerifyChain({// Target |
| {DigestAlgorithm::Sha256, DigestAlgorithm::Sha256}, |
| // Intermediate |
| intermediate_params, |
| // Root |
| {DigestAlgorithm::Sha256, DigestAlgorithm::Sha256}}); |
| } |
| |
| // Shorthand for VerifyChain() where only the root's parameters need to be |
| // specified. |
| WARN_UNUSED_RESULT int VerifyRoot(const CertParams& root_params) { |
| return VerifyChain({// Target |
| {DigestAlgorithm::Sha256, DigestAlgorithm::Sha256}, |
| // Intermediate |
| {DigestAlgorithm::Sha256, DigestAlgorithm::Sha256}, |
| // Root |
| root_params}); |
| } |
| |
| // Manufactures a certificate chain where each certificate has the indicated |
| // signature algorithms, and then returns the result of verifying this chain. |
| // |
| // TODO(eroman): Instead of building certificates at runtime, move their |
| // generation to external scripts. |
| WARN_UNUSED_RESULT int VerifyChain( |
| const std::vector<CertParams>& chain_params) { |
| auto chain = CreateChain(chain_params); |
| if (!chain) { |
| ADD_FAILURE() << "Failed creating certificate chain"; |
| return ERR_UNEXPECTED; |
| } |
| |
| int flags = 0; |
| CertVerifyResult dummy_result; |
| CertVerifyResult verify_result; |
| |
| scoped_refptr<CertVerifyProc> verify_proc = |
| new MockCertVerifyProc(dummy_result); |
| |
| return verify_proc->Verify( |
| chain.get(), "test.example.com", /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result, NetLogWithSource()); |
| } |
| |
| private: |
| // Overwrites the AlgorithmIdentifier pointed to by |algorithm_sequence| with |
| // |algorithm|. Note this violates the constness of StringPiece. |
| WARN_UNUSED_RESULT static bool SetAlgorithmSequence( |
| DigestAlgorithm algorithm, |
| base::StringPiece* algorithm_sequence) { |
| // This string of bytes is the full SEQUENCE for an AlgorithmIdentifier. |
| std::vector<uint8_t> replacement_sequence; |
| switch (algorithm) { |
| case DigestAlgorithm::Sha1: |
| // sha1WithRSAEncryption |
| replacement_sequence = {0x30, 0x0D, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00}; |
| break; |
| case DigestAlgorithm::Sha256: |
| // sha256WithRSAEncryption |
| replacement_sequence = {0x30, 0x0D, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00}; |
| break; |
| case kUnknownDigestAlgorithm: |
| // This shouldn't be anything meaningful (modified numbers at random). |
| replacement_sequence = {0x30, 0x0D, 0x06, 0x09, 0x8a, 0x87, 0x18, 0x46, |
| 0xd7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00}; |
| break; |
| default: |
| ADD_FAILURE() << "Unsupported digest algorithm"; |
| return false; |
| } |
| |
| // For this simple replacement to work (without modifying any |
| // other sequence lengths) the original algorithm and replacement |
| // algorithm must have the same encoded length. |
| if (algorithm_sequence->size() != replacement_sequence.size()) { |
| ADD_FAILURE() << "AlgorithmIdentifier must have length " |
| << replacement_sequence.size(); |
| return false; |
| } |
| |
| memcpy(const_cast<char*>(algorithm_sequence->data()), |
| replacement_sequence.data(), replacement_sequence.size()); |
| return true; |
| } |
| |
| // Locate the serial number bytes. |
| WARN_UNUSED_RESULT static bool ExtractSerialNumberFromDERCert( |
| base::StringPiece der_cert, |
| base::StringPiece* serial_value) { |
| der::Parser parser((der::Input(der_cert))); |
| der::Parser certificate; |
| if (!parser.ReadSequence(&certificate)) |
| return false; |
| |
| der::Parser tbs_certificate; |
| if (!certificate.ReadSequence(&tbs_certificate)) |
| return false; |
| |
| bool unused; |
| if (!tbs_certificate.SkipOptionalTag( |
| der::kTagConstructed | der::kTagContextSpecific | 0, &unused)) { |
| return false; |
| } |
| |
| // serialNumber |
| der::Input serial_value_der; |
| if (!tbs_certificate.ReadTag(der::kInteger, &serial_value_der)) |
| return false; |
| |
| *serial_value = serial_value_der.AsStringPiece(); |
| return true; |
| } |
| |
| // Creates a certificate (based on some base certificate file) using the |
| // specified signature algorithms. |
| static scoped_refptr<X509Certificate> CreateCertificate( |
| const CertParams& params) { |
| // Dosn't really matter which base certificate is used, so long as it is |
| // valid and uses a signature AlgorithmIdentifier with the same encoded |
| // length as sha1WithRSASignature. |
| const char* kLeafFilename = "name_constraint_good.pem"; |
| |
| auto cert = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), kLeafFilename, X509Certificate::FORMAT_AUTO); |
| if (!cert) { |
| ADD_FAILURE() << "Failed to load certificate: " << kLeafFilename; |
| return nullptr; |
| } |
| |
| // Start with the DER bytes of a valid certificate. The der data is copied |
| // to a new std::string as it will modified to create a new certificate. |
| std::string cert_der( |
| x509_util::CryptoBufferAsStringPiece(cert->cert_buffer())); |
| |
| // Parse the certificate and identify the locations of interest within |
| // |cert_der|. |
| base::StringPiece cert_algorithm_sequence; |
| base::StringPiece tbs_algorithm_sequence; |
| if (!asn1::ExtractSignatureAlgorithmsFromDERCert( |
| cert_der, &cert_algorithm_sequence, &tbs_algorithm_sequence)) { |
| ADD_FAILURE() << "Failed parsing certificate algorithms"; |
| return nullptr; |
| } |
| |
| base::StringPiece serial_value; |
| if (!ExtractSerialNumberFromDERCert(cert_der, &serial_value)) { |
| ADD_FAILURE() << "Failed parsing certificate serial number"; |
| return nullptr; |
| } |
| |
| // Give each certificate a unique serial number based on its content (which |
| // in turn is a function of |params|, otherwise importing it may fail. |
| |
| // Upper bound for last entry in DigestAlgorithm |
| const int kNumDigestAlgorithms = 15; |
| *const_cast<char*>(serial_value.data()) += |
| static_cast<int>(params.tbs_algorithm) * kNumDigestAlgorithms + |
| static_cast<int>(params.cert_algorithm); |
| |
| // Change the signature AlgorithmIdentifiers. |
| if (!SetAlgorithmSequence(params.cert_algorithm, |
| &cert_algorithm_sequence) || |
| !SetAlgorithmSequence(params.tbs_algorithm, &tbs_algorithm_sequence)) { |
| return nullptr; |
| } |
| |
| // NOTE: The signature is NOT recomputed over TBSCertificate -- for these |
| // tests it isn't needed. |
| return X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size()); |
| } |
| |
| static scoped_refptr<X509Certificate> CreateChain( |
| const std::vector<CertParams>& params) { |
| // Manufacture a chain with the given combinations of signature algorithms. |
| // This chain isn't actually a valid chain, but it is good enough for |
| // testing the base CertVerifyProc. |
| CertificateList certs; |
| for (const auto& cert_params : params) { |
| certs.push_back(CreateCertificate(cert_params)); |
| if (!certs.back()) |
| return nullptr; |
| } |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| for (size_t i = 1; i < certs.size(); ++i) |
| intermediates.push_back(bssl::UpRef(certs[i]->cert_buffer())); |
| |
| return X509Certificate::CreateFromBuffer( |
| bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates)); |
| } |
| }; |
| |
| // This is a control test to make sure that the test helper |
| // VerifyLeaf() works as expected. There is no actual mismatch in the |
| // algorithms used here. |
| // |
| // Certificate.signatureAlgorithm: sha1WithRSASignature |
| // TBSCertificate.algorithm: sha1WithRSAEncryption |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, LeafSha1Sha1) { |
| int rv = VerifyLeaf({DigestAlgorithm::Sha1, DigestAlgorithm::Sha1}); |
| ASSERT_THAT(rv, IsError(ERR_CERT_WEAK_SIGNATURE_ALGORITHM)); |
| } |
| |
| // This is a control test to make sure that the test helper |
| // VerifyLeaf() works as expected. There is no actual mismatch in the |
| // algorithms used here. |
| // |
| // Certificate.signatureAlgorithm: sha256WithRSASignature |
| // TBSCertificate.algorithm: sha256WithRSAEncryption |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, LeafSha256Sha256) { |
| int rv = VerifyLeaf({DigestAlgorithm::Sha256, DigestAlgorithm::Sha256}); |
| ASSERT_THAT(rv, IsOk()); |
| } |
| |
| // Mismatched signature algorithms in the leaf certificate. |
| // |
| // Certificate.signatureAlgorithm: sha1WithRSASignature |
| // TBSCertificate.algorithm: sha256WithRSAEncryption |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, LeafSha1Sha256) { |
| if (!SupportsImportingMismatchedAlgorithms()) |
| return; |
| |
| int rv = VerifyLeaf({DigestAlgorithm::Sha1, DigestAlgorithm::Sha256}); |
| ASSERT_THAT(rv, IsError(ERR_CERT_INVALID)); |
| } |
| |
| // Mismatched signature algorithms in the leaf certificate. |
| // |
| // Certificate.signatureAlgorithm: sha256WithRSAEncryption |
| // TBSCertificate.algorithm: sha1WithRSASignature |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, LeafSha256Sha1) { |
| if (!SupportsImportingMismatchedAlgorithms()) |
| return; |
| |
| int rv = VerifyLeaf({DigestAlgorithm::Sha256, DigestAlgorithm::Sha1}); |
| ASSERT_THAT(rv, IsError(ERR_CERT_INVALID)); |
| } |
| |
| // Unrecognized signature algorithm in the leaf certificate. |
| // |
| // Certificate.signatureAlgorithm: sha256WithRSAEncryption |
| // TBSCertificate.algorithm: ? |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, LeafSha256Unknown) { |
| if (!SupportsImportingMismatchedAlgorithms()) |
| return; |
| |
| int rv = VerifyLeaf({DigestAlgorithm::Sha256, kUnknownDigestAlgorithm}); |
| ASSERT_THAT(rv, IsError(ERR_CERT_INVALID)); |
| } |
| |
| // Unrecognized signature algorithm in the leaf certificate. |
| // |
| // Certificate.signatureAlgorithm: ? |
| // TBSCertificate.algorithm: sha256WithRSAEncryption |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, LeafUnknownSha256) { |
| if (!SupportsImportingMismatchedAlgorithms()) |
| return; |
| |
| int rv = VerifyLeaf({kUnknownDigestAlgorithm, DigestAlgorithm::Sha256}); |
| ASSERT_THAT(rv, IsError(ERR_CERT_INVALID)); |
| } |
| |
| // Mismatched signature algorithms in the intermediate certificate. |
| // |
| // Certificate.signatureAlgorithm: sha1WithRSASignature |
| // TBSCertificate.algorithm: sha256WithRSAEncryption |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, IntermediateSha1Sha256) { |
| if (!SupportsImportingMismatchedAlgorithms()) |
| return; |
| |
| int rv = VerifyIntermediate({DigestAlgorithm::Sha1, DigestAlgorithm::Sha256}); |
| ASSERT_THAT(rv, IsError(ERR_CERT_INVALID)); |
| } |
| |
| // Mismatched signature algorithms in the intermediate certificate. |
| // |
| // Certificate.signatureAlgorithm: sha256WithRSAEncryption |
| // TBSCertificate.algorithm: sha1WithRSASignature |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, IntermediateSha256Sha1) { |
| if (!SupportsImportingMismatchedAlgorithms()) |
| return; |
| |
| int rv = VerifyIntermediate({DigestAlgorithm::Sha256, DigestAlgorithm::Sha1}); |
| ASSERT_THAT(rv, IsError(ERR_CERT_INVALID)); |
| } |
| |
| // Mismatched signature algorithms in the root certificate. |
| // |
| // Certificate.signatureAlgorithm: sha256WithRSAEncryption |
| // TBSCertificate.algorithm: sha1WithRSASignature |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, RootSha256Sha1) { |
| if (!SupportsImportingMismatchedAlgorithms()) |
| return; |
| |
| int rv = VerifyRoot({DigestAlgorithm::Sha256, DigestAlgorithm::Sha1}); |
| ASSERT_THAT(rv, IsOk()); |
| } |
| |
| // Unrecognized signature algorithm in the root certificate. |
| // |
| // Certificate.signatureAlgorithm: ? |
| // TBSCertificate.algorithm: sha256WithRSAEncryption |
| TEST_F(CertVerifyProcInspectSignatureAlgorithmsTest, RootUnknownSha256) { |
| if (!SupportsImportingMismatchedAlgorithms()) |
| return; |
| |
| int rv = VerifyRoot({kUnknownDigestAlgorithm, DigestAlgorithm::Sha256}); |
| ASSERT_THAT(rv, IsOk()); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, NameConstraintsFailure) { |
| if (!SupportsReturningVerifiedChain()) { |
| LOG(INFO) << "Skipping this test in this platform."; |
| return; |
| } |
| |
| CertificateList ca_cert_list = |
| CreateCertificateListFromFile(GetTestCertsDirectory(), "root_ca_cert.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, ca_cert_list.size()); |
| ScopedTestRoot test_root(ca_cert_list[0].get()); |
| |
| CertificateList cert_list = CreateCertificateListFromFile( |
| GetTestCertsDirectory(), "name_constraint_bad.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, cert_list.size()); |
| |
| scoped_refptr<X509Certificate> leaf = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(cert_list[0]->cert_buffer()), {}); |
| ASSERT_TRUE(leaf); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(leaf.get(), "test.example.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_NAME_CONSTRAINT_VIOLATION)); |
| EXPECT_EQ(CERT_STATUS_NAME_CONSTRAINT_VIOLATION, |
| verify_result.cert_status & CERT_STATUS_NAME_CONSTRAINT_VIOLATION); |
| } |
| |
| TEST(CertVerifyProcTest, TestHasTooLongValidity) { |
| struct { |
| const char* const file; |
| bool is_valid_too_long; |
| } tests[] = { |
| {"daltonridgeapts.com-chain.pem", false}, |
| {"start_after_expiry.pem", true}, |
| {"pre_br_validity_ok.pem", false}, |
| {"pre_br_validity_bad_121.pem", true}, |
| {"pre_br_validity_bad_2020.pem", true}, |
| {"10_year_validity.pem", false}, |
| {"11_year_validity.pem", true}, |
| {"39_months_after_2015_04.pem", false}, |
| {"40_months_after_2015_04.pem", true}, |
| {"60_months_after_2012_07.pem", false}, |
| {"61_months_after_2012_07.pem", true}, |
| {"825_days_after_2018_03_01.pem", false}, |
| {"826_days_after_2018_03_01.pem", true}, |
| {"825_days_1_second_after_2018_03_01.pem", true}, |
| {"39_months_based_on_last_day.pem", false}, |
| {"398_days_after_2020_09_01.pem", false}, |
| {"399_days_after_2020_09_01.pem", true}, |
| {"398_days_1_second_after_2020_09_01.pem", true}, |
| }; |
| |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| |
| for (const auto& test : tests) { |
| SCOPED_TRACE(test.file); |
| |
| scoped_refptr<X509Certificate> certificate = |
| ImportCertFromFile(certs_dir, test.file); |
| ASSERT_TRUE(certificate); |
| EXPECT_EQ(test.is_valid_too_long, |
| CertVerifyProc::HasTooLongValidity(*certificate)); |
| } |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, TestKnownRoot) { |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| scoped_refptr<X509Certificate> cert_chain = CreateCertificateChainFromFile( |
| certs_dir, "daltonridgeapts.com-chain.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(cert_chain); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert_chain.get(), "daltonridgeapts.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()) << "This test relies on a real certificate that " |
| << "expires on May 28, 2021. If failing on/after " |
| << "that date, please disable and file a bug " |
| << "against rsleevi."; |
| EXPECT_TRUE(verify_result.is_issued_by_known_root); |
| #if defined(OS_MAC) |
| if (verify_proc_type() == CERT_VERIFY_PROC_BUILTIN) { |
| auto* mac_trust_debug_info = |
| net::TrustStoreMac::ResultDebugData::Get(&verify_result); |
| ASSERT_TRUE(mac_trust_debug_info); |
| // Since this test queries the real trust store, can't know exactly |
| // what bits should be set in the trust debug info, but it should at |
| // least have something set. |
| EXPECT_NE(0, mac_trust_debug_info->combined_trust_debug_info()); |
| } |
| #endif |
| } |
| |
| // This tests that on successful certificate verification, |
| // CertVerifyResult::public_key_hashes is filled with a SHA256 hash for each |
| // of the certificates in the chain. |
| TEST_P(CertVerifyProcInternalTest, PublicKeyHashes) { |
| if (!SupportsReturningVerifiedChain()) { |
| LOG(INFO) << "Skipping this test in this platform."; |
| return; |
| } |
| |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| CertificateList certs = CreateCertificateListFromFile( |
| certs_dir, "x509_verify_results.chain.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(3U, certs.size()); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(certs[1]->cert_buffer())); |
| intermediates.push_back(bssl::UpRef(certs[2]->cert_buffer())); |
| |
| ScopedTestRoot scoped_root(certs[2].get()); |
| scoped_refptr<X509Certificate> cert_chain = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates)); |
| ASSERT_TRUE(cert_chain); |
| ASSERT_EQ(2U, cert_chain->intermediate_buffers().size()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert_chain.get(), "127.0.0.1", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| |
| EXPECT_EQ(3u, verify_result.public_key_hashes.size()); |
| |
| // Convert |public_key_hashes| to strings for ease of comparison. |
| std::vector<std::string> public_key_hash_strings; |
| for (const auto& public_key_hash : verify_result.public_key_hashes) |
| public_key_hash_strings.push_back(public_key_hash.ToString()); |
| |
| std::vector<std::string> expected_public_key_hashes = { |
| // Target |
| "sha256/DZMTp9cNNYkzUG6baDB6T306ekLUYJpeEEtYpaeQpYE=", |
| |
| // Intermediate |
| "sha256/D9u0epgvPYlG9YiVp7V+IMT+xhUpB5BhsS/INjDXc4Y=", |
| |
| // Trust anchor |
| "sha256/VypP3VWL7OaqTJ7mIBehWYlv8khPuFHpWiearZI2YjI="}; |
| |
| // |public_key_hashes| does not have an ordering guarantee. |
| EXPECT_THAT(expected_public_key_hashes, |
| testing::UnorderedElementsAreArray(public_key_hash_strings)); |
| } |
| |
| // A regression test for http://crbug.com/70293. |
| // The certificate in question has a key purpose of clientAuth, and also lacks |
| // the required key usage for serverAuth. |
| // TODO(mattm): This cert fails for many reasons, replace with a generated one |
| // that tests only the desired case. |
| TEST_P(CertVerifyProcInternalTest, WrongKeyPurpose) { |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| |
| scoped_refptr<X509Certificate> server_cert = |
| ImportCertFromFile(certs_dir, "invalid_key_usage_cert.der"); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), server_cert.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(server_cert.get(), "jira.aquameta.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_COMMON_NAME_INVALID); |
| |
| #if defined(OS_IOS) |
| if (verify_proc_type() == CERT_VERIFY_PROC_IOS) { |
| if (base::ios::IsRunningOnIOS13OrLater() || |
| !base::ios::IsRunningOnIOS12OrLater()) { |
| EXPECT_THAT(error, IsError(ERR_CERT_INVALID)); |
| } else { |
| EXPECT_THAT(error, IsError(ERR_CERT_AUTHORITY_INVALID)); |
| } |
| return; |
| } |
| #endif |
| |
| // TODO(crbug.com/649017): Don't special-case builtin verifier. |
| if (verify_proc_type() != CERT_VERIFY_PROC_BUILTIN) |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_INVALID); |
| |
| if (verify_proc_type() != CERT_VERIFY_PROC_ANDROID) { |
| // The certificate is issued by an unknown CA. |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_AUTHORITY_INVALID); |
| } |
| |
| // TODO(crbug.com/649017): Don't special-case builtin verifier. |
| if (verify_proc_type() == CERT_VERIFY_PROC_BUILTIN) { |
| EXPECT_THAT(error, IsError(ERR_CERT_AUTHORITY_INVALID)); |
| } else { |
| EXPECT_THAT(error, IsError(ERR_CERT_INVALID)); |
| } |
| } |
| |
| // Tests that a Netscape Server Gated crypto is accepted in place of a |
| // serverAuth EKU. |
| // TODO(crbug.com/843735): Deprecate support for this. |
| TEST_P(CertVerifyProcInternalTest, Sha1IntermediateUsesServerGatedCrypto) { |
| base::FilePath certs_dir = |
| GetTestNetDataDirectory() |
| .AppendASCII("verify_certificate_chain_unittest") |
| .AppendASCII("intermediate-eku-server-gated-crypto"); |
| |
| scoped_refptr<X509Certificate> cert_chain = CreateCertificateChainFromFile( |
| certs_dir, "sha1-chain.pem", X509Certificate::FORMAT_AUTO); |
| |
| ASSERT_TRUE(cert_chain); |
| ASSERT_FALSE(cert_chain->intermediate_buffers().empty()); |
| |
| auto root = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(cert_chain->intermediate_buffers().back().get()), {}); |
| |
| ScopedTestRoot scoped_root(root.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert_chain.get(), "test.example", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| |
| if (AreSHA1IntermediatesAllowed()) { |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_SHA1_SIGNATURE_PRESENT); |
| } else { |
| EXPECT_NE(error, OK); |
| EXPECT_TRUE(verify_result.cert_status & |
| CERT_STATUS_WEAK_SIGNATURE_ALGORITHM); |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_SHA1_SIGNATURE_PRESENT); |
| } |
| } |
| |
| // Basic test for returning the chain in CertVerifyResult. Note that the |
| // returned chain may just be a reflection of the originally supplied chain; |
| // that is, if any errors occur, the default chain returned is an exact copy |
| // of the certificate to be verified. The remaining VerifyReturn* tests are |
| // used to ensure that the actual, verified chain is being returned by |
| // Verify(). |
| TEST_P(CertVerifyProcInternalTest, VerifyReturnChainBasic) { |
| if (!SupportsReturningVerifiedChain()) { |
| LOG(INFO) << "Skipping this test in this platform."; |
| return; |
| } |
| |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| CertificateList certs = CreateCertificateListFromFile( |
| certs_dir, "x509_verify_results.chain.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(3U, certs.size()); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(certs[1]->cert_buffer())); |
| intermediates.push_back(bssl::UpRef(certs[2]->cert_buffer())); |
| |
| ScopedTestRoot scoped_root(certs[2].get()); |
| |
| scoped_refptr<X509Certificate> google_full_chain = |
| X509Certificate::CreateFromBuffer(bssl::UpRef(certs[0]->cert_buffer()), |
| std::move(intermediates)); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), google_full_chain.get()); |
| ASSERT_EQ(2U, google_full_chain->intermediate_buffers().size()); |
| |
| CertVerifyResult verify_result; |
| EXPECT_EQ(static_cast<X509Certificate*>(nullptr), |
| verify_result.verified_cert.get()); |
| int error = |
| Verify(google_full_chain.get(), "127.0.0.1", 0, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), |
| verify_result.verified_cert.get()); |
| |
| EXPECT_NE(google_full_chain, verify_result.verified_cert); |
| EXPECT_TRUE( |
| x509_util::CryptoBufferEqual(google_full_chain->cert_buffer(), |
| verify_result.verified_cert->cert_buffer())); |
| const auto& return_intermediates = |
| verify_result.verified_cert->intermediate_buffers(); |
| ASSERT_EQ(2U, return_intermediates.size()); |
| EXPECT_TRUE(x509_util::CryptoBufferEqual(return_intermediates[0].get(), |
| certs[1]->cert_buffer())); |
| EXPECT_TRUE(x509_util::CryptoBufferEqual(return_intermediates[1].get(), |
| certs[2]->cert_buffer())); |
| } |
| |
| // Test that certificates issued for 'intranet' names (that is, containing no |
| // known public registry controlled domain information) issued by well-known |
| // CAs are flagged appropriately, while certificates that are issued by |
| // internal CAs are not flagged. |
| TEST(CertVerifyProcTest, IntranetHostsRejected) { |
| CertificateList cert_list = CreateCertificateListFromFile( |
| GetTestCertsDirectory(), "reject_intranet_hosts.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, cert_list.size()); |
| scoped_refptr<X509Certificate> cert(cert_list[0]); |
| |
| CertVerifyResult verify_result; |
| int error = 0; |
| |
| // Intranet names for public CAs should be flagged: |
| CertVerifyResult dummy_result; |
| dummy_result.is_issued_by_known_root = true; |
| scoped_refptr<CertVerifyProc> verify_proc = |
| new MockCertVerifyProc(dummy_result); |
| error = verify_proc->Verify( |
| cert.get(), "webmail", /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), 0, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result, NetLogWithSource()); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_NON_UNIQUE_NAME); |
| |
| // However, if the CA is not well known, these should not be flagged: |
| dummy_result.Reset(); |
| dummy_result.is_issued_by_known_root = false; |
| verify_proc = base::MakeRefCounted<MockCertVerifyProc>(dummy_result); |
| error = verify_proc->Verify( |
| cert.get(), "webmail", /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), 0, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result, NetLogWithSource()); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_FALSE(verify_result.cert_status & CERT_STATUS_NON_UNIQUE_NAME); |
| } |
| |
| // Tests that certificates issued by Symantec's legacy infrastructure |
| // are rejected according to the policies outlined in |
| // https://security.googleblog.com/2017/09/chromes-plan-to-distrust-symantec.html |
| // unless the caller has explicitly disabled that enforcement. |
| TEST(CertVerifyProcTest, SymantecCertsRejected) { |
| constexpr SHA256HashValue kSymantecHashValue = { |
| {0xb2, 0xde, 0xf5, 0x36, 0x2a, 0xd3, 0xfa, 0xcd, 0x04, 0xbd, 0x29, |
| 0x04, 0x7a, 0x43, 0x84, 0x4f, 0x76, 0x70, 0x34, 0xea, 0x48, 0x92, |
| 0xf8, 0x0e, 0x56, 0xbe, 0xe6, 0x90, 0x24, 0x3e, 0x25, 0x02}}; |
| constexpr SHA256HashValue kGoogleHashValue = { |
| {0xec, 0x72, 0x29, 0x69, 0xcb, 0x64, 0x20, 0x0a, 0xb6, 0x63, 0x8f, |
| 0x68, 0xac, 0x53, 0x8e, 0x40, 0xab, 0xab, 0x5b, 0x19, 0xa6, 0x48, |
| 0x56, 0x61, 0x04, 0x2a, 0x10, 0x61, 0xc4, 0x61, 0x27, 0x76}}; |
| |
| // Test that certificates from the legacy Symantec infrastructure are |
| // rejected: |
| // - dec_2017.pem : A certificate issued after 2017-12-01, which is rejected |
| // as of M65 |
| // - pre_june_2016.pem : A certificate issued prior to 2016-06-01, which is |
| // rejected as of M66. |
| for (const char* rejected_cert : {"dec_2017.pem", "pre_june_2016.pem"}) { |
| scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), rejected_cert, X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(cert); |
| |
| scoped_refptr<CertVerifyProc> verify_proc; |
| int error = 0; |
| |
| // Test that a legacy Symantec certificate is rejected. |
| CertVerifyResult symantec_result; |
| symantec_result.verified_cert = cert; |
| symantec_result.public_key_hashes.push_back(HashValue(kSymantecHashValue)); |
| symantec_result.is_issued_by_known_root = true; |
| verify_proc = base::MakeRefCounted<MockCertVerifyProc>(symantec_result); |
| |
| CertVerifyResult test_result_1; |
| error = verify_proc->Verify( |
| cert.get(), "127.0.0.1", /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), 0, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &test_result_1, NetLogWithSource()); |
| EXPECT_THAT(error, IsError(ERR_CERT_SYMANTEC_LEGACY)); |
| EXPECT_TRUE(test_result_1.cert_status & CERT_STATUS_SYMANTEC_LEGACY); |
| |
| // ... Unless the Symantec cert chains through a allowlisted intermediate. |
| CertVerifyResult allowlisted_result; |
| allowlisted_result.verified_cert = cert; |
| allowlisted_result.public_key_hashes.push_back( |
| HashValue(kSymantecHashValue)); |
| allowlisted_result.public_key_hashes.push_back(HashValue(kGoogleHashValue)); |
| allowlisted_result.is_issued_by_known_root = true; |
| verify_proc = base::MakeRefCounted<MockCertVerifyProc>(allowlisted_result); |
| |
| CertVerifyResult test_result_2; |
| error = verify_proc->Verify( |
| cert.get(), "127.0.0.1", /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), 0, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &test_result_2, NetLogWithSource()); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_FALSE(test_result_2.cert_status & CERT_STATUS_AUTHORITY_INVALID); |
| |
| // ... Or the caller disabled enforcement of Symantec policies. |
| CertVerifyResult test_result_3; |
| error = verify_proc->Verify( |
| cert.get(), "127.0.0.1", /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), |
| CertVerifyProc::VERIFY_DISABLE_SYMANTEC_ENFORCEMENT, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &test_result_3, |
| NetLogWithSource()); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_FALSE(test_result_3.cert_status & CERT_STATUS_SYMANTEC_LEGACY); |
| } |
| |
| // Test that certificates from the legacy Symantec infrastructure issued |
| // after 2016-06-01 approriately accept or reject based on the base::Feature |
| // flag. |
| for (bool feature_flag_enabled : {false, true}) { |
| base::test::ScopedFeatureList scoped_feature_list; |
| scoped_feature_list.InitWithFeatureState( |
| CertVerifyProc::kLegacySymantecPKIEnforcement, feature_flag_enabled); |
| |
| scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "post_june_2016.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(cert); |
| |
| scoped_refptr<CertVerifyProc> verify_proc; |
| int error = 0; |
| |
| // Test that a legacy Symantec certificate is rejected if the feature |
| // flag is enabled, and accepted if it is not. |
| CertVerifyResult symantec_result; |
| symantec_result.verified_cert = cert; |
| symantec_result.public_key_hashes.push_back(HashValue(kSymantecHashValue)); |
| symantec_result.is_issued_by_known_root = true; |
| verify_proc = base::MakeRefCounted<MockCertVerifyProc>(symantec_result); |
| |
| CertVerifyResult test_result_1; |
| error = verify_proc->Verify( |
| cert.get(), "127.0.0.1", /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), 0, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &test_result_1, NetLogWithSource()); |
| if (feature_flag_enabled) { |
| EXPECT_THAT(error, IsError(ERR_CERT_SYMANTEC_LEGACY)); |
| EXPECT_TRUE(test_result_1.cert_status & CERT_STATUS_SYMANTEC_LEGACY); |
| } else { |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_FALSE(test_result_1.cert_status & CERT_STATUS_SYMANTEC_LEGACY); |
| } |
| |
| // ... Unless the Symantec cert chains through a allowlisted intermediate. |
| CertVerifyResult allowlisted_result; |
| allowlisted_result.verified_cert = cert; |
| allowlisted_result.public_key_hashes.push_back( |
| HashValue(kSymantecHashValue)); |
| allowlisted_result.public_key_hashes.push_back(HashValue(kGoogleHashValue)); |
| allowlisted_result.is_issued_by_known_root = true; |
| verify_proc = base::MakeRefCounted<MockCertVerifyProc>(allowlisted_result); |
| |
| CertVerifyResult test_result_2; |
| error = verify_proc->Verify( |
| cert.get(), "127.0.0.1", /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), 0, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &test_result_2, NetLogWithSource()); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_FALSE(test_result_2.cert_status & CERT_STATUS_AUTHORITY_INVALID); |
| |
| // ... Or the caller disabled enforcement of Symantec policies. |
| CertVerifyResult test_result_3; |
| error = verify_proc->Verify( |
| cert.get(), "127.0.0.1", /*ocsp_response=*/std::string(), |
| /*sct_list=*/std::string(), |
| CertVerifyProc::VERIFY_DISABLE_SYMANTEC_ENFORCEMENT, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &test_result_3, |
| NetLogWithSource()); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_FALSE(test_result_3.cert_status & CERT_STATUS_SYMANTEC_LEGACY); |
| } |
| } |
| |
| // Test that the certificate returned in CertVerifyResult is able to reorder |
| // certificates that are not ordered from end-entity to root. While this is |
| // a protocol violation if sent during a TLS handshake, if multiple sources |
| // of intermediate certificates are combined, it's possible that order may |
| // not be maintained. |
| TEST_P(CertVerifyProcInternalTest, VerifyReturnChainProperlyOrdered) { |
| if (!SupportsReturningVerifiedChain()) { |
| LOG(INFO) << "Skipping this test in this platform."; |
| return; |
| } |
| |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| CertificateList certs = CreateCertificateListFromFile( |
| certs_dir, "x509_verify_results.chain.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(3U, certs.size()); |
| |
| // Construct the chain out of order. |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(certs[2]->cert_buffer())); |
| intermediates.push_back(bssl::UpRef(certs[1]->cert_buffer())); |
| |
| ScopedTestRoot scoped_root(certs[2].get()); |
| |
| scoped_refptr<X509Certificate> google_full_chain = |
| X509Certificate::CreateFromBuffer(bssl::UpRef(certs[0]->cert_buffer()), |
| std::move(intermediates)); |
| ASSERT_TRUE(google_full_chain); |
| ASSERT_EQ(2U, google_full_chain->intermediate_buffers().size()); |
| |
| CertVerifyResult verify_result; |
| EXPECT_FALSE(verify_result.verified_cert); |
| int error = |
| Verify(google_full_chain.get(), "127.0.0.1", 0, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| ASSERT_TRUE(verify_result.verified_cert); |
| |
| EXPECT_NE(google_full_chain, verify_result.verified_cert); |
| EXPECT_TRUE( |
| x509_util::CryptoBufferEqual(google_full_chain->cert_buffer(), |
| verify_result.verified_cert->cert_buffer())); |
| const auto& return_intermediates = |
| verify_result.verified_cert->intermediate_buffers(); |
| ASSERT_EQ(2U, return_intermediates.size()); |
| EXPECT_TRUE(x509_util::CryptoBufferEqual(return_intermediates[0].get(), |
| certs[1]->cert_buffer())); |
| EXPECT_TRUE(x509_util::CryptoBufferEqual(return_intermediates[1].get(), |
| certs[2]->cert_buffer())); |
| } |
| |
| // Test that Verify() filters out certificates which are not related to |
| // or part of the certificate chain being verified. |
| TEST_P(CertVerifyProcInternalTest, VerifyReturnChainFiltersUnrelatedCerts) { |
| if (!SupportsReturningVerifiedChain()) { |
| LOG(INFO) << "Skipping this test in this platform."; |
| return; |
| } |
| |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| CertificateList certs = CreateCertificateListFromFile( |
| certs_dir, "x509_verify_results.chain.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(3U, certs.size()); |
| ScopedTestRoot scoped_root(certs[2].get()); |
| |
| scoped_refptr<X509Certificate> unrelated_certificate = |
| ImportCertFromFile(certs_dir, "duplicate_cn_1.pem"); |
| scoped_refptr<X509Certificate> unrelated_certificate2 = |
| ImportCertFromFile(certs_dir, "aia-cert.pem"); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), |
| unrelated_certificate.get()); |
| ASSERT_NE(static_cast<X509Certificate*>(nullptr), |
| unrelated_certificate2.get()); |
| |
| // Interject unrelated certificates into the list of intermediates. |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(unrelated_certificate->cert_buffer())); |
| intermediates.push_back(bssl::UpRef(certs[1]->cert_buffer())); |
| intermediates.push_back(bssl::UpRef(unrelated_certificate2->cert_buffer())); |
| intermediates.push_back(bssl::UpRef(certs[2]->cert_buffer())); |
| |
| scoped_refptr<X509Certificate> google_full_chain = |
| X509Certificate::CreateFromBuffer(bssl::UpRef(certs[0]->cert_buffer()), |
| std::move(intermediates)); |
| ASSERT_TRUE(google_full_chain); |
| ASSERT_EQ(4U, google_full_chain->intermediate_buffers().size()); |
| |
| CertVerifyResult verify_result; |
| EXPECT_FALSE(verify_result.verified_cert); |
| int error = |
| Verify(google_full_chain.get(), "127.0.0.1", 0, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| ASSERT_TRUE(verify_result.verified_cert); |
| |
| EXPECT_NE(google_full_chain, verify_result.verified_cert); |
| EXPECT_TRUE( |
| x509_util::CryptoBufferEqual(google_full_chain->cert_buffer(), |
| verify_result.verified_cert->cert_buffer())); |
| const auto& return_intermediates = |
| verify_result.verified_cert->intermediate_buffers(); |
| ASSERT_EQ(2U, return_intermediates.size()); |
| EXPECT_TRUE(x509_util::CryptoBufferEqual(return_intermediates[0].get(), |
| certs[1]->cert_buffer())); |
| EXPECT_TRUE(x509_util::CryptoBufferEqual(return_intermediates[1].get(), |
| certs[2]->cert_buffer())); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, AdditionalTrustAnchors) { |
| if (!SupportsAdditionalTrustAnchors()) { |
| LOG(INFO) << "Skipping this test in this platform."; |
| return; |
| } |
| |
| // |ca_cert| is the issuer of |cert|. |
| CertificateList ca_cert_list = |
| CreateCertificateListFromFile(GetTestCertsDirectory(), "root_ca_cert.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, ca_cert_list.size()); |
| scoped_refptr<X509Certificate> ca_cert(ca_cert_list[0]); |
| |
| CertificateList cert_list = CreateCertificateListFromFile( |
| GetTestCertsDirectory(), "ok_cert.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, cert_list.size()); |
| scoped_refptr<X509Certificate> cert(cert_list[0]); |
| |
| // Verification of |cert| fails when |ca_cert| is not in the trust anchors |
| // list. |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert.get(), "127.0.0.1", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_AUTHORITY_INVALID)); |
| EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, verify_result.cert_status); |
| EXPECT_FALSE(verify_result.is_issued_by_additional_trust_anchor); |
| |
| // Now add the |ca_cert| to the |trust_anchors|, and verification should pass. |
| CertificateList trust_anchors; |
| trust_anchors.push_back(ca_cert); |
| error = Verify(cert.get(), "127.0.0.1", flags, CRLSet::BuiltinCRLSet().get(), |
| trust_anchors, &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| EXPECT_TRUE(verify_result.is_issued_by_additional_trust_anchor); |
| |
| // Clearing the |trust_anchors| makes verification fail again (the cache |
| // should be skipped). |
| error = Verify(cert.get(), "127.0.0.1", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_AUTHORITY_INVALID)); |
| EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, verify_result.cert_status); |
| EXPECT_FALSE(verify_result.is_issued_by_additional_trust_anchor); |
| } |
| |
| // Tests that certificates issued by user-supplied roots are not flagged as |
| // issued by a known root. This should pass whether or not the platform supports |
| // detecting known roots. |
| TEST_P(CertVerifyProcInternalTest, IsIssuedByKnownRootIgnoresTestRoots) { |
| // Load root_ca_cert.pem into the test root store. |
| ScopedTestRoot test_root( |
| ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem").get()); |
| |
| scoped_refptr<X509Certificate> cert( |
| ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem")); |
| |
| // Verification should pass. |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert.get(), "127.0.0.1", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| // But should not be marked as a known root. |
| EXPECT_FALSE(verify_result.is_issued_by_known_root); |
| } |
| |
| // Test verification with a leaf that does not contain embedded SCTs, and which |
| // has a notBefore date after 2018/10/15, and passing a valid |sct_list| to |
| // Verify(). Verification should succeed on all platforms. (Assuming the |
| // verifier trusts the SCT Logs used in |sct_list|.) |
| // |
| // Fails on multiple plaforms, see crbug.com/1050152. |
| TEST_P(CertVerifyProcInternalTest, |
| DISABLED_LeafNewerThan20181015WithTlsSctList) { |
| scoped_refptr<X509Certificate> chain = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "treadclimber.pem", |
| X509Certificate::FORMAT_PEM_CERT_SEQUENCE); |
| ASSERT_TRUE(chain); |
| if (base::Time::Now() > chain->valid_expiry()) { |
| FAIL() << "This test uses a certificate chain which is now expired. Please " |
| "disable and file a bug against mattm."; |
| return; |
| } |
| |
| std::string sct_list; |
| ASSERT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII("treadclimber.sctlist"), &sct_list)); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| verify_proc()->Verify(chain.get(), "treadclimber.com", |
| /*ocsp_response=*/std::string(), sct_list, flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), |
| &verify_result, NetLogWithSource()); |
| |
| // Since the valid |sct_list| was passed to Verify, verification should |
| // succeed on all verifiers and OS versions. |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| EXPECT_TRUE(verify_result.is_issued_by_known_root); |
| } |
| |
| // Test that CRLSets are effective in making a certificate appear to be |
| // revoked. |
| TEST_P(CertVerifyProcInternalTest, CRLSet) { |
| if (!SupportsCRLSet()) { |
| LOG(INFO) << "Skipping test as verifier doesn't support CRLSet"; |
| return; |
| } |
| |
| CertificateList ca_cert_list = |
| CreateCertificateListFromFile(GetTestCertsDirectory(), "root_ca_cert.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, ca_cert_list.size()); |
| ScopedTestRoot test_root(ca_cert_list[0].get()); |
| |
| CertificateList cert_list = CreateCertificateListFromFile( |
| GetTestCertsDirectory(), "ok_cert.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, cert_list.size()); |
| scoped_refptr<X509Certificate> cert(cert_list[0]); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert.get(), "127.0.0.1", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| |
| scoped_refptr<CRLSet> crl_set; |
| std::string crl_set_bytes; |
| |
| // First test blocking by SPKI. |
| EXPECT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII("crlset_by_leaf_spki.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_REVOKED)); |
| |
| // Second, test revocation by serial number of a cert directly under the |
| // root. |
| crl_set_bytes.clear(); |
| EXPECT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII("crlset_by_root_serial.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_REVOKED)); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, CRLSetLeafSerial) { |
| if (!SupportsCRLSet()) { |
| LOG(INFO) << "Skipping test as verifier doesn't support CRLSet"; |
| return; |
| } |
| |
| CertificateList ca_cert_list = |
| CreateCertificateListFromFile(GetTestCertsDirectory(), "root_ca_cert.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, ca_cert_list.size()); |
| ScopedTestRoot test_root(ca_cert_list[0].get()); |
| |
| scoped_refptr<X509Certificate> leaf = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "ok_cert_by_intermediate.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(leaf); |
| ASSERT_EQ(1U, leaf->intermediate_buffers().size()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(leaf.get(), "127.0.0.1", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| |
| // Test revocation by serial number of a certificate not under the root. |
| scoped_refptr<CRLSet> crl_set; |
| std::string crl_set_bytes; |
| ASSERT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII("crlset_by_intermediate_serial.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(leaf.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_REVOKED)); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, CRLSetRootReturnsChain) { |
| if (!SupportsCRLSet()) { |
| LOG(INFO) << "Skipping test as verifier doesn't support CRLSet"; |
| return; |
| } |
| |
| CertificateList ca_cert_list = |
| CreateCertificateListFromFile(GetTestCertsDirectory(), "root_ca_cert.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(1U, ca_cert_list.size()); |
| ScopedTestRoot test_root(ca_cert_list[0].get()); |
| |
| scoped_refptr<X509Certificate> leaf = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "ok_cert_by_intermediate.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(leaf); |
| ASSERT_EQ(1U, leaf->intermediate_buffers().size()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(leaf.get(), "127.0.0.1", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| |
| // Test revocation of the root itself. |
| scoped_refptr<CRLSet> crl_set; |
| std::string crl_set_bytes; |
| ASSERT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII("crlset_by_root_spki.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(leaf.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_REVOKED)); |
| |
| EXPECT_EQ(3u, verify_result.public_key_hashes.size()); |
| ASSERT_TRUE(verify_result.verified_cert); |
| EXPECT_EQ(2u, verify_result.verified_cert->intermediate_buffers().size()); |
| } |
| |
| // Tests that CertVerifyProc implementations apply CRLSet revocations by |
| // subject. |
| TEST_P(CertVerifyProcInternalTest, CRLSetRevokedBySubject) { |
| if (!SupportsCRLSet()) { |
| LOG(INFO) << "Skipping test as verifier doesn't support CRLSet"; |
| return; |
| } |
| |
| scoped_refptr<X509Certificate> root( |
| ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem")); |
| ASSERT_TRUE(root); |
| |
| scoped_refptr<X509Certificate> leaf( |
| ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem")); |
| ASSERT_TRUE(leaf); |
| |
| ScopedTestRoot scoped_root(root.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| |
| // Confirm that verifying the certificate chain with an empty CRLSet succeeds. |
| scoped_refptr<CRLSet> crl_set = CRLSet::EmptyCRLSetForTesting(); |
| int error = Verify(leaf.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| |
| std::string crl_set_bytes; |
| |
| // Revoke the leaf by subject. Verification should now fail. |
| ASSERT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII("crlset_by_leaf_subject_no_spki.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(leaf.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_REVOKED)); |
| |
| // Revoke the root by subject. Verification should now fail. |
| ASSERT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII("crlset_by_root_subject_no_spki.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(leaf.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsError(ERR_CERT_REVOKED)); |
| |
| // Revoke the leaf by subject, but only if the SPKI doesn't match the given |
| // one. Verification should pass when using the certificate's actual SPKI. |
| ASSERT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII("crlset_by_root_subject.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(leaf.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| } |
| |
| // Ensures that CRLSets can be used to block known interception roots on |
| // platforms that support CRLSets, while otherwise detect known interception |
| // on platforms that do not. |
| TEST_P(CertVerifyProcInternalTest, BlockedInterceptionByRoot) { |
| scoped_refptr<X509Certificate> root = |
| ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"); |
| ASSERT_TRUE(root); |
| ScopedTestRoot test_root(root.get()); |
| |
| scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "ok_cert_by_intermediate.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(cert); |
| |
| // A default/built-in CRLSet should not block |
| scoped_refptr<CRLSet> crl_set = CRLSet::BuiltinCRLSet(); |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = Verify(cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| |
| // Read in a CRLSet that marks the root as blocked for interception. |
| std::string crl_set_bytes; |
| ASSERT_TRUE( |
| base::ReadFileToString(GetTestCertsDirectory().AppendASCII( |
| "crlset_blocked_interception_by_root.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| if (SupportsCRLSet()) { |
| EXPECT_THAT(error, IsError(ERR_CERT_KNOWN_INTERCEPTION_BLOCKED)); |
| EXPECT_TRUE(verify_result.cert_status & |
| CERT_STATUS_KNOWN_INTERCEPTION_BLOCKED); |
| } else { |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_TRUE(verify_result.cert_status & |
| CERT_STATUS_KNOWN_INTERCEPTION_DETECTED); |
| } |
| } |
| |
| // Ensures that CRLSets can be used to block known interception intermediates, |
| // while still allowing other certificates from that root.. |
| TEST_P(CertVerifyProcInternalTest, BlockedInterceptionByIntermediate) { |
| scoped_refptr<X509Certificate> root = |
| ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"); |
| ASSERT_TRUE(root); |
| ScopedTestRoot test_root(root.get()); |
| |
| scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "ok_cert_by_intermediate.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(cert); |
| |
| // A default/built-in CRLSEt should not block |
| scoped_refptr<CRLSet> crl_set = CRLSet::BuiltinCRLSet(); |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = Verify(cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| |
| // Read in a CRLSet that marks the intermediate as blocked for interception. |
| std::string crl_set_bytes; |
| ASSERT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII( |
| "crlset_blocked_interception_by_intermediate.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| if (SupportsCRLSet()) { |
| EXPECT_THAT(error, IsError(ERR_CERT_KNOWN_INTERCEPTION_BLOCKED)); |
| EXPECT_TRUE(verify_result.cert_status & |
| CERT_STATUS_KNOWN_INTERCEPTION_BLOCKED); |
| } else { |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_TRUE(verify_result.cert_status & |
| CERT_STATUS_KNOWN_INTERCEPTION_DETECTED); |
| } |
| |
| // Load a different certificate from that root, which should be unaffected. |
| scoped_refptr<X509Certificate> second_cert = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "ok_cert.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(second_cert); |
| |
| error = Verify(second_cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| } |
| |
| // Ensures that CRLSets can be used to flag known interception roots, even |
| // when they are not blocked. |
| TEST_P(CertVerifyProcInternalTest, DetectsInterceptionByRoot) { |
| scoped_refptr<X509Certificate> root = |
| ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"); |
| ASSERT_TRUE(root); |
| ScopedTestRoot test_root(root.get()); |
| |
| scoped_refptr<X509Certificate> cert = CreateCertificateChainFromFile( |
| GetTestCertsDirectory(), "ok_cert_by_intermediate.pem", |
| X509Certificate::FORMAT_AUTO); |
| ASSERT_TRUE(cert); |
| |
| // A default/built-in CRLSet should not block |
| scoped_refptr<CRLSet> crl_set = CRLSet::BuiltinCRLSet(); |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = Verify(cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_EQ(0U, verify_result.cert_status); |
| |
| // Read in a CRLSet that marks the root as blocked for interception. |
| std::string crl_set_bytes; |
| ASSERT_TRUE( |
| base::ReadFileToString(GetTestCertsDirectory().AppendASCII( |
| "crlset_known_interception_by_root.raw"), |
| &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| error = Verify(cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| EXPECT_THAT(error, IsOk()); |
| EXPECT_TRUE(verify_result.cert_status & |
| CERT_STATUS_KNOWN_INTERCEPTION_DETECTED); |
| } |
| |
| // Tests that CRLSets participate in path building functions, and that as |
| // long as a valid path exists within the verification graph, verification |
| // succeeds. |
| // |
| // In this test, there are two roots (D and E), and three possible paths |
| // to validate a leaf (A): |
| // 1. A(B) -> B(C) -> C(D) -> D(D) |
| // 2. A(B) -> B(C) -> C(E) -> E(E) |
| // 3. A(B) -> B(F) -> F(E) -> E(E) |
| // |
| // Each permutation of revocation is tried: |
| // 1. Revoking E by SPKI, so that only Path 1 is valid (as E is in Paths 2 & 3) |
| // 2. Revoking C(D) and F(E) by serial, so that only Path 2 is valid. |
| // 3. Revoking C by SPKI, so that only Path 3 is valid (as C is in Paths 1 & 2) |
| TEST_P(CertVerifyProcInternalTest, CRLSetDuringPathBuilding) { |
| if (!SupportsCRLSetsInPathBuilding()) { |
| LOG(INFO) << "Skipping this test on this platform."; |
| return; |
| } |
| |
| CertificateList path_1_certs; |
| ASSERT_TRUE( |
| LoadCertificateFiles({"multi-root-A-by-B.pem", "multi-root-B-by-C.pem", |
| "multi-root-C-by-D.pem", "multi-root-D-by-D.pem"}, |
| &path_1_certs)); |
| |
| CertificateList path_2_certs; |
| ASSERT_TRUE( |
| LoadCertificateFiles({"multi-root-A-by-B.pem", "multi-root-B-by-C.pem", |
| "multi-root-C-by-E.pem", "multi-root-E-by-E.pem"}, |
| &path_2_certs)); |
| |
| CertificateList path_3_certs; |
| ASSERT_TRUE( |
| LoadCertificateFiles({"multi-root-A-by-B.pem", "multi-root-B-by-F.pem", |
| "multi-root-F-by-E.pem", "multi-root-E-by-E.pem"}, |
| &path_3_certs)); |
| |
| // Add D and E as trust anchors. |
| ScopedTestRoot test_root_D(path_1_certs[3].get()); // D-by-D |
| ScopedTestRoot test_root_E(path_2_certs[3].get()); // E-by-E |
| |
| // Create a chain that contains all the certificate paths possible. |
| // CertVerifyProcInternalTest.VerifyReturnChainFiltersUnrelatedCerts already |
| // ensures that it's safe to send additional certificates as inputs, and |
| // that they're ignored if not necessary. |
| // This is to avoid relying on AIA or internal object caches when |
| // interacting with the underlying library. |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back( |
| bssl::UpRef(path_1_certs[1]->cert_buffer())); // B-by-C |
| intermediates.push_back( |
| bssl::UpRef(path_1_certs[2]->cert_buffer())); // C-by-D |
| intermediates.push_back( |
| bssl::UpRef(path_2_certs[2]->cert_buffer())); // C-by-E |
| intermediates.push_back( |
| bssl::UpRef(path_3_certs[1]->cert_buffer())); // B-by-F |
| intermediates.push_back( |
| bssl::UpRef(path_3_certs[2]->cert_buffer())); // F-by-E |
| scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(path_1_certs[0]->cert_buffer()), std::move(intermediates)); |
| ASSERT_TRUE(cert); |
| |
| struct TestPermutations { |
| const char* crlset; |
| bool expect_valid; |
| scoped_refptr<X509Certificate> expected_intermediate; |
| } kTests[] = { |
| {"multi-root-crlset-D-and-E.raw", false, nullptr}, |
| {"multi-root-crlset-E.raw", true, path_1_certs[2].get()}, |
| {"multi-root-crlset-CD-and-FE.raw", true, path_2_certs[2].get()}, |
| {"multi-root-crlset-C.raw", true, path_3_certs[2].get()}, |
| {"multi-root-crlset-unrelated.raw", true, nullptr}}; |
| |
| for (const auto& testcase : kTests) { |
| SCOPED_TRACE(testcase.crlset); |
| scoped_refptr<CRLSet> crl_set; |
| std::string crl_set_bytes; |
| EXPECT_TRUE(base::ReadFileToString( |
| GetTestCertsDirectory().AppendASCII(testcase.crlset), &crl_set_bytes)); |
| ASSERT_TRUE(CRLSet::Parse(crl_set_bytes, &crl_set)); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = Verify(cert.get(), "127.0.0.1", flags, crl_set.get(), |
| CertificateList(), &verify_result); |
| |
| if (!testcase.expect_valid) { |
| EXPECT_NE(OK, error); |
| EXPECT_NE(0U, verify_result.cert_status); |
| continue; |
| } |
| |
| ASSERT_THAT(error, IsOk()); |
| ASSERT_EQ(0U, verify_result.cert_status); |
| ASSERT_TRUE(verify_result.verified_cert.get()); |
| |
| if (!testcase.expected_intermediate) |
| continue; |
| |
| const auto& verified_intermediates = |
| verify_result.verified_cert->intermediate_buffers(); |
| ASSERT_EQ(3U, verified_intermediates.size()); |
| |
| scoped_refptr<X509Certificate> intermediate = |
| X509Certificate::CreateFromBuffer( |
| bssl::UpRef(verified_intermediates[1].get()), {}); |
| ASSERT_TRUE(intermediate); |
| |
| EXPECT_TRUE(testcase.expected_intermediate->EqualsExcludingChain( |
| intermediate.get())) |
| << "Expected: " << testcase.expected_intermediate->subject().common_name |
| << " issued by " << testcase.expected_intermediate->issuer().common_name |
| << "; Got: " << intermediate->subject().common_name << " issued by " |
| << intermediate->issuer().common_name; |
| } |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, ValidityDayPlus5MinutesBeforeNotBefore) { |
| std::unique_ptr<CertBuilder> leaf, intermediate, root; |
| CertBuilder::CreateSimpleChain(&leaf, &intermediate, &root); |
| ASSERT_TRUE(leaf && intermediate && root); |
| base::Time not_before = base::Time::Now() + base::TimeDelta::FromDays(1) + |
| base::TimeDelta::FromMinutes(5); |
| base::Time not_after = base::Time::Now() + base::TimeDelta::FromDays(30); |
| leaf->SetValidity(not_before, not_after); |
| |
| // Trust the root and build a chain to verify that includes the intermediate. |
| ScopedTestRoot scoped_root(root->GetX509Certificate().get()); |
| scoped_refptr<X509Certificate> chain = leaf->GetX509CertificateChain(); |
| ASSERT_TRUE(chain.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(chain.get(), "www.example.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| // Current time is before certificate's notBefore. Verification should fail. |
| EXPECT_THAT(error, IsError(ERR_CERT_DATE_INVALID)); |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_DATE_INVALID); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, ValidityDayBeforeNotBefore) { |
| std::unique_ptr<CertBuilder> leaf, intermediate, root; |
| CertBuilder::CreateSimpleChain(&leaf, &intermediate, &root); |
| ASSERT_TRUE(leaf && intermediate && root); |
| base::Time not_before = base::Time::Now() + base::TimeDelta::FromDays(1); |
| base::Time not_after = base::Time::Now() + base::TimeDelta::FromDays(30); |
| leaf->SetValidity(not_before, not_after); |
| |
| // Trust the root and build a chain to verify that includes the intermediate. |
| ScopedTestRoot scoped_root(root->GetX509Certificate().get()); |
| scoped_refptr<X509Certificate> chain = leaf->GetX509CertificateChain(); |
| ASSERT_TRUE(chain.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(chain.get(), "www.example.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| // Current time is before certificate's notBefore. Verification should fail. |
| EXPECT_THAT(error, IsError(ERR_CERT_DATE_INVALID)); |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_DATE_INVALID); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, ValidityJustBeforeNotBefore) { |
| std::unique_ptr<CertBuilder> leaf, intermediate, root; |
| CertBuilder::CreateSimpleChain(&leaf, &intermediate, &root); |
| ASSERT_TRUE(leaf && intermediate && root); |
| base::Time not_before = base::Time::Now() + base::TimeDelta::FromMinutes(5); |
| base::Time not_after = base::Time::Now() + base::TimeDelta::FromDays(30); |
| leaf->SetValidity(not_before, not_after); |
| |
| // Trust the root and build a chain to verify that includes the intermediate. |
| ScopedTestRoot scoped_root(root->GetX509Certificate().get()); |
| scoped_refptr<X509Certificate> chain = leaf->GetX509CertificateChain(); |
| ASSERT_TRUE(chain.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(chain.get(), "www.example.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| // Current time is before certificate's notBefore. Verification should fail. |
| EXPECT_THAT(error, IsError(ERR_CERT_DATE_INVALID)); |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_DATE_INVALID); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, ValidityJustAfterNotBefore) { |
| std::unique_ptr<CertBuilder> leaf, intermediate, root; |
| CertBuilder::CreateSimpleChain(&leaf, &intermediate, &root); |
| ASSERT_TRUE(leaf && intermediate && root); |
| base::Time not_before = base::Time::Now() - base::TimeDelta::FromSeconds(1); |
| base::Time not_after = base::Time::Now() + base::TimeDelta::FromDays(30); |
| leaf->SetValidity(not_before, not_after); |
| |
| // Trust the root and build a chain to verify that includes the intermediate. |
| ScopedTestRoot scoped_root(root->GetX509Certificate().get()); |
| scoped_refptr<X509Certificate> chain = leaf->GetX509CertificateChain(); |
| ASSERT_TRUE(chain.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(chain.get(), "www.example.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| // Current time is between notBefore and notAfter. Verification should |
| // succeed. |
| EXPECT_THAT(error, IsOk()); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, ValidityJustBeforeNotAfter) { |
| std::unique_ptr<CertBuilder> leaf, intermediate, root; |
| CertBuilder::CreateSimpleChain(&leaf, &intermediate, &root); |
| ASSERT_TRUE(leaf && intermediate && root); |
| base::Time not_before = base::Time::Now() - base::TimeDelta::FromDays(30); |
| base::Time not_after = base::Time::Now() + base::TimeDelta::FromMinutes(5); |
| leaf->SetValidity(not_before, not_after); |
| |
| // Trust the root and build a chain to verify that includes the intermediate. |
| ScopedTestRoot scoped_root(root->GetX509Certificate().get()); |
| scoped_refptr<X509Certificate> chain = leaf->GetX509CertificateChain(); |
| ASSERT_TRUE(chain.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(chain.get(), "www.example.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| // Current time is between notBefore and notAfter. Verification should |
| // succeed. |
| EXPECT_THAT(error, IsOk()); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, ValidityJustAfterNotAfter) { |
| std::unique_ptr<CertBuilder> leaf, intermediate, root; |
| CertBuilder::CreateSimpleChain(&leaf, &intermediate, &root); |
| ASSERT_TRUE(leaf && intermediate && root); |
| base::Time not_before = base::Time::Now() - base::TimeDelta::FromDays(30); |
| base::Time not_after = base::Time::Now() - base::TimeDelta::FromSeconds(1); |
| leaf->SetValidity(not_before, not_after); |
| |
| // Trust the root and build a chain to verify that includes the intermediate. |
| ScopedTestRoot scoped_root(root->GetX509Certificate().get()); |
| scoped_refptr<X509Certificate> chain = leaf->GetX509CertificateChain(); |
| ASSERT_TRUE(chain.get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(chain.get(), "www.example.com", flags, |
| CRLSet::BuiltinCRLSet().get(), CertificateList(), &verify_result); |
| // Current time is after certificate's notAfter. Verification should fail. |
| EXPECT_THAT(error, IsError(ERR_CERT_DATE_INVALID)); |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_DATE_INVALID); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, FailedIntermediateSignatureValidation) { |
| base::FilePath certs_dir = |
| GetTestNetDataDirectory() |
| .AppendASCII("verify_certificate_chain_unittest") |
| .AppendASCII( |
| "intermediate-wrong-signature-no-authority-key-identifier"); |
| |
| CertificateList certs = CreateCertificateListFromFile( |
| certs_dir, "chain.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(3U, certs.size()); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(certs[1]->cert_buffer())); |
| |
| scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates)); |
| ASSERT_TRUE(cert.get()); |
| |
| // Trust the root certificate. |
| ScopedTestRoot scoped_root(certs.back().get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert.get(), "test.example", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| |
| // The intermediate was signed by a different root with a different key but |
| // with the same name as the trusted one, and the intermediate has no |
| // authorityKeyIdentifier, so the verifier must try verifying the signature. |
| // Should fail with AUTHORITY_INVALID. |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_AUTHORITY_INVALID); |
| EXPECT_THAT(error, IsError(ERR_CERT_AUTHORITY_INVALID)); |
| } |
| |
| TEST_P(CertVerifyProcInternalTest, FailedTargetSignatureValidation) { |
| base::FilePath certs_dir = |
| GetTestNetDataDirectory() |
| .AppendASCII("verify_certificate_chain_unittest") |
| .AppendASCII("target-wrong-signature-no-authority-key-identifier"); |
| |
| CertificateList certs = CreateCertificateListFromFile( |
| certs_dir, "chain.pem", X509Certificate::FORMAT_AUTO); |
| ASSERT_EQ(3U, certs.size()); |
| |
| std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates; |
| intermediates.push_back(bssl::UpRef(certs[1]->cert_buffer())); |
| |
| scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromBuffer( |
| bssl::UpRef(certs[0]->cert_buffer()), std::move(intermediates)); |
| ASSERT_TRUE(cert.get()); |
| |
| // Trust the root certificate. |
| ScopedTestRoot scoped_root(certs.back().get()); |
| |
| int flags = 0; |
| CertVerifyResult verify_result; |
| int error = |
| Verify(cert.get(), "test.example", flags, CRLSet::BuiltinCRLSet().get(), |
| CertificateList(), &verify_result); |
| |
| // The leaf was signed by a different intermediate with a different key but |
| // with the same name as the one in the chain, and the leaf has no |
| // authorityKeyIdentifier, so the verifier must try verifying the signature. |
| // Should fail with AUTHORITY_INVALID. |
| EXPECT_TRUE(verify_result.cert_status & CERT_STATUS_AUTHORITY_INVALID); |
| EXPECT_THAT(error, IsError(ERR_CERT_AUTHORITY_INVALID)); |
| } |
| |
| class CertVerifyProcNameNormalizationTest : public CertVerifyProcInternalTest { |
| protected: |
| void SetUp() override { |
| CertVerifyProcInternalTest::SetUp(); |
| |
| scoped_refptr<X509Certificate> root_cert = |
| ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
| ASSERT_TRUE(root_cert); |
| test_root_.reset(new ScopedTestRoot(root_cert.get())); |
| } |
| |
| std::string HistogramName() const { |
| std::string prefix("Net.CertVerifier.NameNormalizationPrivateRoots."); |
| switch (verify_proc_type()) { |
| case CERT_VERIFY_PROC_ANDROID: |
| return prefix + "Android"; |
| case CERT_VERIFY_PROC_IOS: |
| return prefix + "IOS"; |
| case CERT_VERIFY_PROC_MAC: |
| return prefix + "Mac"; |
| case CERT_VERIFY_PROC_WIN: |
| return prefix + "Win"; |
| case CERT_VERIFY_PROC_BUILTIN: |
| return prefix + "Builtin"; |
| } |
| } |
| |
| void ExpectNormalizationHistogram(int verify_error) { |
| if (verify_error == OK) { |
| histograms_.ExpectUniqueSample( |
| HistogramName(), CertVerifyProc::NameNormalizationResult::kNormalized, |
| 1); |
| } else { |
| histograms_.ExpectTotalCount(HistogramName(), 0); |
| } |
| } |
| |
| void ExpectByteEqualHistogram() { |
| histograms_.ExpectUniqueSample( |
| HistogramName(), CertVerifyProc::NameNormalizationResult::kByteEqual, |
| 1); |
| } |
| |
| private: |
| std::unique_ptr<ScopedTestRoot> test_root_; |
| base::HistogramTester histograms_; |
| }; |
| |
| INSTANTIATE_TEST_SUITE_P(All, |
| CertVerifyProcNameNormalizationTest, |
| testing::ValuesIn(kAllCertVerifiers), |
| VerifyProcTypeToName); |
| |
| // Tries to verify a chain where the leaf's issuer CN is PrintableString, while |
| // the intermediate's subject CN is UTF8String, and verifies the proper |
| // histogram is logged. |
| TEST_P(CertVerifyProcNameNormalizationTest, StringType) { |
| scoped_refptr<X509Certificate> chain = CreateCertificateChainFromFile( |
| GetTestCertsDirectory |