Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "chrome/common/pref_names.h" | |
| 12 #include "chrome/test/base/testing_browser_process.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | |
| 14 #include "chrome/test/base/testing_profile_manager.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/test/test_browser_thread.h" | |
| 17 #include "crypto/nss_util.h" | |
| 18 #include "net/base/cert_test_util.h" | |
| 19 #include "net/base/cert_trust_anchor_provider.h" | |
| 20 #include "net/base/cert_verify_result.h" | |
| 21 #include "net/base/net_log.h" | |
| 22 #include "net/base/nss_cert_database.h" | |
| 23 #include "net/base/test_completion_callback.h" | |
| 24 #include "net/base/test_data_directory.h" | |
| 25 #include "net/base/x509_certificate.h" | |
| 26 #include "testing/gmock/include/gmock/gmock.h" | |
| 27 #include "testing/gtest/include/gtest/gtest.h" | |
| 28 | |
| 29 using testing::Mock; | |
| 30 using testing::ReturnRef; | |
| 31 | |
| 32 namespace policy { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 class MockCertTrustAnchorProvider : public net::CertTrustAnchorProvider { | |
| 37 public: | |
| 38 MockCertTrustAnchorProvider() {} | |
| 39 virtual ~MockCertTrustAnchorProvider() {} | |
| 40 | |
| 41 MOCK_METHOD0(GetAdditionalTrustAnchors, const net::CertificateList&()); | |
| 42 }; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 // This is actually a unit test, but is linked with browser_tests because | |
| 47 // importing a certificate into the NSS test database persists for the duration | |
| 48 // of a process; since each browser_test runs in a separate process then this | |
| 49 // won't affect subsequent tests. | |
| 50 // This can be moved to the unittests target once the TODO in ~ScopedTestNSSDB | |
| 51 // is fixed. | |
|
Ryan Sleevi
2013/03/25 21:09:53
The TODO has been fixed since NSS 3.13.
Since thi
| |
| 52 class PolicyCertVerifierTest : public testing::Test { | |
| 53 public: | |
| 54 PolicyCertVerifierTest() | |
| 55 : cert_db_(NULL), | |
| 56 ui_thread_(content::BrowserThread::UI, &loop_), | |
| 57 io_thread_(content::BrowserThread::IO, &loop_), | |
| 58 profile_manager_(TestingBrowserProcess::GetGlobal()), | |
| 59 profile_(NULL) {} | |
| 60 | |
| 61 virtual ~PolicyCertVerifierTest() {} | |
| 62 | |
| 63 virtual void SetUp() OVERRIDE { | |
| 64 ASSERT_TRUE(test_nssdb_.is_open()); | |
| 65 cert_db_ = net::NSSCertDatabase::GetInstance(); | |
| 66 | |
| 67 ASSERT_TRUE(profile_manager_.SetUp()); | |
| 68 profile_ = profile_manager_.CreateTestingProfile("profile"); | |
| 69 | |
| 70 cert_verifier_.reset(new PolicyCertVerifier(profile_, &trust_provider_)); | |
| 71 } | |
| 72 | |
| 73 scoped_refptr<net::X509Certificate> LoadCertificate(const std::string& name, | |
| 74 net::CertType type) { | |
| 75 scoped_refptr<net::X509Certificate> cert = | |
| 76 net::ImportCertFromFile(net::GetTestCertsDirectory(), name); | |
| 77 | |
| 78 // No certificate is trusted right after it's loaded. | |
| 79 net::NSSCertDatabase::TrustBits trust = | |
| 80 cert_db_->GetCertTrust(cert.get(), type); | |
| 81 EXPECT_EQ(net::NSSCertDatabase::TRUST_DEFAULT, trust); | |
| 82 | |
| 83 return cert; | |
| 84 } | |
| 85 | |
| 86 protected: | |
| 87 crypto::ScopedTestNSSDB test_nssdb_; | |
| 88 net::NSSCertDatabase* cert_db_; | |
| 89 MessageLoop loop_; | |
| 90 content::TestBrowserThread ui_thread_; | |
| 91 content::TestBrowserThread io_thread_; | |
| 92 TestingProfileManager profile_manager_; | |
| 93 TestingProfile* profile_; | |
| 94 MockCertTrustAnchorProvider trust_provider_; | |
| 95 scoped_ptr<PolicyCertVerifier> cert_verifier_; | |
| 96 const net::CertificateList empty_cert_list_; | |
| 97 }; | |
| 98 | |
| 99 TEST_F(PolicyCertVerifierTest, VerifyUntrustedCert) { | |
| 100 scoped_refptr<net::X509Certificate> cert = | |
| 101 LoadCertificate("ok_cert.pem", net::SERVER_CERT); | |
|
pneubeck (no reviews)
2013/03/26 11:04:50
again the same question: do you have an idea if we
Joao da Silva
2013/03/31 19:22:14
We certainly can generate the certs at runtime, bu
| |
| 102 ASSERT_TRUE(cert); | |
| 103 | |
| 104 // |cert| is untrusted, so Verify() fails. | |
| 105 int error; | |
| 106 net::CertVerifyResult verify_result; | |
| 107 net::TestCompletionCallback callback; | |
| 108 net::CertVerifier::RequestHandle request_handle; | |
| 109 EXPECT_CALL(trust_provider_, GetAdditionalTrustAnchors()) | |
| 110 .WillOnce(ReturnRef(empty_cert_list_)); | |
| 111 error = cert_verifier_->Verify(cert, "127.0.0.1", 0, NULL, | |
|
pneubeck (no reviews)
2013/03/26 11:04:50
int error = ...
Joao da Silva
2013/03/31 19:22:14
Done.
| |
| 112 &verify_result, callback.callback(), | |
| 113 &request_handle, net::BoundNetLog()); | |
| 114 Mock::VerifyAndClearExpectations(&trust_provider_); | |
| 115 ASSERT_EQ(net::ERR_IO_PENDING, error); | |
| 116 ASSERT_TRUE(request_handle); | |
| 117 error = callback.WaitForResult(); | |
| 118 EXPECT_EQ(net::ERR_CERT_AUTHORITY_INVALID, error); | |
| 119 | |
| 120 // Issuing the same request again hits the cache. This tests the synchronous | |
|
pneubeck (no reviews)
2013/03/26 11:04:50
The cache is in the implementation of MultiThreade
Joao da Silva
2013/03/31 19:22:14
This relies on that behavior of the MultiThreadedC
| |
| 121 // path. | |
| 122 EXPECT_CALL(trust_provider_, GetAdditionalTrustAnchors()) | |
| 123 .WillOnce(ReturnRef(empty_cert_list_)); | |
| 124 error = cert_verifier_->Verify(cert, "127.0.0.1", 0, NULL, | |
| 125 &verify_result, callback.callback(), | |
| 126 &request_handle, net::BoundNetLog()); | |
| 127 Mock::VerifyAndClearExpectations(&trust_provider_); | |
| 128 EXPECT_EQ(net::ERR_CERT_AUTHORITY_INVALID, error); | |
| 129 | |
| 130 // The profile is not tainted. | |
| 131 base::RunLoop().RunUntilIdle(); | |
| 132 EXPECT_FALSE( | |
| 133 profile_->GetPrefs()->GetBoolean(prefs::kUsedPolicyCertificatesOnce)); | |
| 134 } | |
| 135 | |
| 136 TEST_F(PolicyCertVerifierTest, VerifyTrustedCert) { | |
| 137 // |ca_cert| is the issuer of |cert|. | |
| 138 scoped_refptr<net::X509Certificate> ca_cert = | |
| 139 LoadCertificate("root_ca_cert.crt", net::CA_CERT); | |
| 140 ASSERT_TRUE(ca_cert); | |
| 141 scoped_refptr<net::X509Certificate> cert = | |
| 142 LoadCertificate("ok_cert.pem", net::SERVER_CERT); | |
| 143 ASSERT_TRUE(cert); | |
| 144 | |
| 145 // Make the database trust |ca_cert|. | |
| 146 net::CertificateList import_list; | |
| 147 import_list.push_back(ca_cert); | |
| 148 net::NSSCertDatabase::ImportCertFailureList failure_list; | |
| 149 ASSERT_TRUE(cert_db_->ImportCACerts( | |
| 150 import_list, net::NSSCertDatabase::TRUSTED_SSL, &failure_list)); | |
| 151 ASSERT_TRUE(failure_list.empty()); | |
| 152 | |
| 153 // Verify that it is now trusted. | |
| 154 net::NSSCertDatabase::TrustBits trust = | |
|
pneubeck (no reviews)
2013/03/26 11:04:50
This verifies that ImportCACerts works? Isn't that
Joao da Silva
2013/03/31 19:22:14
This test is meant to verify that manual imports d
| |
| 155 cert_db_->GetCertTrust(ca_cert.get(), net::CA_CERT); | |
| 156 EXPECT_EQ(net::NSSCertDatabase::TRUSTED_SSL, trust); | |
| 157 | |
| 158 // Verify() successfully verifies |cert| now. | |
|
pneubeck (no reviews)
2013/03/26 11:04:50
remove "now" as it seems to refer to the previous
Joao da Silva
2013/03/31 19:22:14
Done.
| |
| 159 int error; | |
| 160 net::CertVerifyResult verify_result; | |
| 161 net::TestCompletionCallback callback; | |
| 162 net::CertVerifier::RequestHandle request_handle; | |
| 163 EXPECT_CALL(trust_provider_, GetAdditionalTrustAnchors()) | |
| 164 .WillOnce(ReturnRef(empty_cert_list_)); | |
| 165 error = cert_verifier_->Verify(cert, "127.0.0.1", 0, NULL, | |
|
pneubeck (no reviews)
2013/03/26 11:04:50
int error =...
Joao da Silva
2013/03/31 19:22:14
Done.
| |
| 166 &verify_result, callback.callback(), | |
| 167 &request_handle, net::BoundNetLog()); | |
| 168 Mock::VerifyAndClearExpectations(&trust_provider_); | |
| 169 ASSERT_EQ(net::ERR_IO_PENDING, error); | |
| 170 ASSERT_TRUE(request_handle); | |
| 171 error = callback.WaitForResult(); | |
| 172 EXPECT_EQ(net::OK, error); | |
| 173 | |
| 174 // The profile is not tainted, since the certificate is trusted from the | |
| 175 // database. | |
| 176 base::RunLoop().RunUntilIdle(); | |
| 177 EXPECT_FALSE( | |
| 178 profile_->GetPrefs()->GetBoolean(prefs::kUsedPolicyCertificatesOnce)); | |
| 179 } | |
| 180 | |
| 181 TEST_F(PolicyCertVerifierTest, VerifyUsingAdditionalTrustAnchor) { | |
| 182 // |ca_cert| is the issuer of |cert|. | |
| 183 scoped_refptr<net::X509Certificate> ca_cert = | |
| 184 LoadCertificate("root_ca_cert.crt", net::CA_CERT); | |
| 185 ASSERT_TRUE(ca_cert); | |
| 186 scoped_refptr<net::X509Certificate> cert = | |
| 187 LoadCertificate("ok_cert.pem", net::SERVER_CERT); | |
| 188 ASSERT_TRUE(cert); | |
| 189 | |
| 190 net::CertificateList additional_trust_anchors; | |
| 191 additional_trust_anchors.push_back(ca_cert); | |
| 192 | |
| 193 // Verify() successfully verifies |cert|, using |ca_cert| from the list of | |
| 194 // |additional_trust_anchors|. | |
| 195 int error; | |
| 196 net::CertVerifyResult verify_result; | |
| 197 net::TestCompletionCallback callback; | |
| 198 net::CertVerifier::RequestHandle request_handle; | |
| 199 EXPECT_CALL(trust_provider_, GetAdditionalTrustAnchors()) | |
| 200 .WillOnce(ReturnRef(additional_trust_anchors)); | |
| 201 error = cert_verifier_->Verify(cert, "127.0.0.1", 0, NULL, | |
|
pneubeck (no reviews)
2013/03/26 11:04:50
int error = ...
Joao da Silva
2013/03/31 19:22:14
Done.
| |
| 202 &verify_result, callback.callback(), | |
| 203 &request_handle, net::BoundNetLog()); | |
| 204 Mock::VerifyAndClearExpectations(&trust_provider_); | |
| 205 ASSERT_EQ(net::ERR_IO_PENDING, error); | |
| 206 ASSERT_TRUE(request_handle); | |
| 207 error = callback.WaitForResult(); | |
| 208 EXPECT_EQ(net::OK, error); | |
| 209 | |
| 210 // The profile becomes tainted after using the trust anchors that came from | |
| 211 // the policy configuration. | |
| 212 base::RunLoop().RunUntilIdle(); | |
| 213 EXPECT_TRUE( | |
| 214 profile_->GetPrefs()->GetBoolean(prefs::kUsedPolicyCertificatesOnce)); | |
| 215 } | |
| 216 | |
| 217 } // namespace policy | |
| OLD | NEW |