1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/cert_loader.h"
8 #include "base/files/file_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "crypto/nss_util_internal.h"
13 #include "crypto/scoped_nss_types.h"
14 #include "crypto/scoped_test_nss_chromeos_user.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/test_data_directory.h"
17 #include "net/cert/nss_cert_database_chromeos.h"
18 #include "net/cert/x509_certificate.h"
19 #include "net/test/cert_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 // http://crbug.com/418369
28 bool IsCertInCertificateList(const net::X509Certificate
* cert
,
29 const net::CertificateList
& cert_list
) {
30 for (net::CertificateList::const_iterator it
= cert_list
.begin();
31 it
!= cert_list
.end();
33 if (net::X509Certificate::IsSameOSCert((*it
)->os_cert_handle(),
34 cert
->os_cert_handle())) {
41 void FailOnPrivateSlotCallback(crypto::ScopedPK11Slot slot
) {
42 EXPECT_FALSE(true) << "GetPrivateSlotForChromeOSUser callback called even "
43 << "though the private slot had been initialized.";
46 class CertLoaderTest
: public testing::Test
,
47 public CertLoader::Observer
{
49 CertLoaderTest() : cert_loader_(NULL
),
50 primary_user_("primary"),
51 certificates_loaded_events_count_(0U) {
54 virtual ~CertLoaderTest() {}
56 virtual void SetUp() override
{
57 ASSERT_TRUE(primary_user_
.constructed_successfully());
59 crypto::GetPublicSlotForChromeOSUser(primary_user_
.username_hash()));
61 CertLoader::Initialize();
62 cert_loader_
= CertLoader::Get();
63 cert_loader_
->AddObserver(this);
66 virtual void TearDown() {
67 cert_loader_
->RemoveObserver(this);
68 CertLoader::Shutdown();
72 void StartCertLoaderWithPrimaryUser() {
73 FinishUserInitAndGetDatabase(&primary_user_
, &primary_db_
);
74 cert_loader_
->StartWithNSSDB(primary_db_
.get());
76 base::RunLoop().RunUntilIdle();
77 GetAndResetCertificatesLoadedEventsCount();
80 // CertLoader::Observer:
81 // The test keeps count of times the observer method was called.
82 virtual void OnCertificatesLoaded(const net::CertificateList
& cert_list
,
83 bool initial_load
) override
{
84 EXPECT_TRUE(certificates_loaded_events_count_
== 0 || !initial_load
);
85 certificates_loaded_events_count_
++;
88 // Returns the number of |OnCertificatesLoaded| calls observed since the
89 // last call to this method equals |value|.
90 size_t GetAndResetCertificatesLoadedEventsCount() {
91 size_t result
= certificates_loaded_events_count_
;
92 certificates_loaded_events_count_
= 0;
96 // Finishes initialization for the |user| and returns a user's NSS database
98 void FinishUserInitAndGetDatabase(
99 crypto::ScopedTestNSSChromeOSUser
* user
,
100 scoped_ptr
<net::NSSCertDatabaseChromeOS
>* database
) {
101 ASSERT_TRUE(user
->constructed_successfully());
105 crypto::ScopedPK11Slot
private_slot(
106 crypto::GetPrivateSlotForChromeOSUser(
107 user
->username_hash(),
108 base::Bind(&FailOnPrivateSlotCallback
)));
109 ASSERT_TRUE(private_slot
);
111 database
->reset(new net::NSSCertDatabaseChromeOS(
112 crypto::GetPublicSlotForChromeOSUser(user
->username_hash()),
113 private_slot
.Pass()));
114 (*database
)->SetSlowTaskRunnerForTest(message_loop_
.message_loop_proxy());
117 int GetDbPrivateSlotId(net::NSSCertDatabase
* db
) {
118 return static_cast<int>(PK11_GetSlotID(db
->GetPrivateSlot().get()));
121 void ImportCACert(const std::string
& cert_file
,
122 net::NSSCertDatabase
* database
,
123 net::CertificateList
* imported_certs
) {
124 ASSERT_TRUE(database
);
125 ASSERT_TRUE(imported_certs
);
127 // Add a certificate to the user's db.
128 *imported_certs
= net::CreateCertificateListFromFile(
129 net::GetTestCertsDirectory(),
131 net::X509Certificate::FORMAT_AUTO
);
132 ASSERT_EQ(1U, imported_certs
->size());
134 net::NSSCertDatabase::ImportCertFailureList failed
;
135 ASSERT_TRUE(database
->ImportCACerts(*imported_certs
,
136 net::NSSCertDatabase::TRUST_DEFAULT
,
138 ASSERT_TRUE(failed
.empty());
141 void ImportClientCertAndKey(const std::string
& pkcs12_file
,
142 net::NSSCertDatabase
* database
,
143 net::CertificateList
* imported_certs
) {
144 ASSERT_TRUE(database
);
145 ASSERT_TRUE(imported_certs
);
147 std::string pkcs12_data
;
148 base::FilePath pkcs12_file_path
=
149 net::GetTestCertsDirectory().Append(pkcs12_file
);
150 ASSERT_TRUE(base::ReadFileToString(pkcs12_file_path
, &pkcs12_data
));
152 net::CertificateList client_cert_list
;
153 scoped_refptr
<net::CryptoModule
> module(net::CryptoModule::CreateFromHandle(
154 database
->GetPrivateSlot().get()));
156 database
->ImportFromPKCS12(module
.get(),
161 ASSERT_EQ(1U, imported_certs
->size());
164 CertLoader
* cert_loader_
;
166 // The user is primary as the one whose certificates CertLoader handles, it
167 // has nothing to do with crypto::InitializeNSSForChromeOSUser is_primary_user
168 // parameter (which is irrelevant for these tests).
169 crypto::ScopedTestNSSChromeOSUser primary_user_
;
170 scoped_ptr
<net::NSSCertDatabaseChromeOS
> primary_db_
;
172 base::MessageLoop message_loop_
;
175 size_t certificates_loaded_events_count_
;
178 TEST_F(CertLoaderTest
, Basic
) {
179 EXPECT_FALSE(cert_loader_
->CertificatesLoading());
180 EXPECT_FALSE(cert_loader_
->certificates_loaded());
181 EXPECT_FALSE(cert_loader_
->IsHardwareBacked());
183 FinishUserInitAndGetDatabase(&primary_user_
, &primary_db_
);
185 cert_loader_
->StartWithNSSDB(primary_db_
.get());
187 EXPECT_FALSE(cert_loader_
->certificates_loaded());
188 EXPECT_TRUE(cert_loader_
->CertificatesLoading());
189 EXPECT_TRUE(cert_loader_
->cert_list().empty());
191 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
192 base::RunLoop().RunUntilIdle();
193 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
195 EXPECT_TRUE(cert_loader_
->certificates_loaded());
196 EXPECT_FALSE(cert_loader_
->CertificatesLoading());
198 // Default CA cert roots should get loaded.
199 EXPECT_FALSE(cert_loader_
->cert_list().empty());
202 TEST_F(CertLoaderTest
, CertLoaderUpdatesCertListOnNewCert
) {
203 StartCertLoaderWithPrimaryUser();
205 net::CertificateList certs
;
206 ImportCACert("root_ca_cert.pem", primary_db_
.get(), &certs
);
208 // Certs are loaded asynchronously, so the new cert should not yet be in the
211 IsCertInCertificateList(certs
[0].get(), cert_loader_
->cert_list()));
213 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
214 base::RunLoop().RunUntilIdle();
215 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
217 // The certificate list should be updated now, as the message loop's been run.
219 IsCertInCertificateList(certs
[0].get(), cert_loader_
->cert_list()));
222 TEST_F(CertLoaderTest
, CertLoaderNoUpdateOnSecondaryDbChanges
) {
223 crypto::ScopedTestNSSChromeOSUser
secondary_user("secondary");
224 scoped_ptr
<net::NSSCertDatabaseChromeOS
> secondary_db
;
226 StartCertLoaderWithPrimaryUser();
227 FinishUserInitAndGetDatabase(&secondary_user
, &secondary_db
);
229 net::CertificateList certs
;
230 ImportCACert("root_ca_cert.pem", secondary_db
.get(), &certs
);
232 base::RunLoop().RunUntilIdle();
235 IsCertInCertificateList(certs
[0].get(), cert_loader_
->cert_list()));
238 TEST_F(CertLoaderTest
, ClientLoaderUpdateOnNewClientCert
) {
239 StartCertLoaderWithPrimaryUser();
241 net::CertificateList certs
;
242 ImportClientCertAndKey("websocket_client_cert.p12",
246 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
247 base::RunLoop().RunUntilIdle();
248 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
251 IsCertInCertificateList(certs
[0].get(), cert_loader_
->cert_list()));
254 TEST_F(CertLoaderTest
, CertLoaderNoUpdateOnNewClientCertInSecondaryDb
) {
255 crypto::ScopedTestNSSChromeOSUser
secondary_user("secondary");
256 scoped_ptr
<net::NSSCertDatabaseChromeOS
> secondary_db
;
258 StartCertLoaderWithPrimaryUser();
259 FinishUserInitAndGetDatabase(&secondary_user
, &secondary_db
);
261 net::CertificateList certs
;
262 ImportClientCertAndKey("websocket_client_cert.p12",
266 base::RunLoop().RunUntilIdle();
269 IsCertInCertificateList(certs
[0].get(), cert_loader_
->cert_list()));
272 TEST_F(CertLoaderTest
, UpdatedOnCertRemoval
) {
273 StartCertLoaderWithPrimaryUser();
275 net::CertificateList certs
;
276 ImportClientCertAndKey("websocket_client_cert.p12",
280 base::RunLoop().RunUntilIdle();
282 ASSERT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
284 IsCertInCertificateList(certs
[0].get(), cert_loader_
->cert_list()));
286 primary_db_
->DeleteCertAndKey(certs
[0].get());
288 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
289 base::RunLoop().RunUntilIdle();
290 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
293 IsCertInCertificateList(certs
[0].get(), cert_loader_
->cert_list()));
296 TEST_F(CertLoaderTest
, UpdatedOnCACertTrustChange
) {
297 StartCertLoaderWithPrimaryUser();
299 net::CertificateList certs
;
300 ImportCACert("root_ca_cert.pem", primary_db_
.get(), &certs
);
302 base::RunLoop().RunUntilIdle();
303 ASSERT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
305 IsCertInCertificateList(certs
[0].get(), cert_loader_
->cert_list()));
307 // The value that should have been set by |ImportCACert|.
308 ASSERT_EQ(net::NSSCertDatabase::TRUST_DEFAULT
,
309 primary_db_
->GetCertTrust(certs
[0].get(), net::CA_CERT
));
310 ASSERT_TRUE(primary_db_
->SetCertTrust(
311 certs
[0].get(), net::CA_CERT
, net::NSSCertDatabase::TRUSTED_SSL
));
313 // Cert trust change should trigger certificate reload in cert_loader_.
314 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
315 base::RunLoop().RunUntilIdle();
316 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
320 } // namespace chromeos