1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
// Copyright 2014 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 "chromeos/cert_loader.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "crypto/nss_util_internal.h"
#include "crypto/scoped_nss_types.h"
#include "crypto/scoped_test_nss_chromeos_user.h"
#include "net/base/net_errors.h"
#include "net/base/test_data_directory.h"
#include "net/cert/nss_cert_database_chromeos.h"
#include "net/cert/x509_certificate.h"
#include "net/test/cert_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
// http://crbug.com/418369
#ifdef NDEBUG
namespace chromeos {
namespace {
bool IsCertInCertificateList(const net::X509Certificate* cert,
const net::CertificateList& cert_list) {
for (net::CertificateList::const_iterator it = cert_list.begin();
it != cert_list.end();
++it) {
if (net::X509Certificate::IsSameOSCert((*it)->os_cert_handle(),
cert->os_cert_handle())) {
return true;
}
}
return false;
}
void FailOnPrivateSlotCallback(crypto::ScopedPK11Slot slot) {
EXPECT_FALSE(true) << "GetPrivateSlotForChromeOSUser callback called even "
<< "though the private slot had been initialized.";
}
class CertLoaderTest : public testing::Test,
public CertLoader::Observer {
public:
CertLoaderTest() : cert_loader_(NULL),
primary_user_("primary"),
certificates_loaded_events_count_(0U) {
}
virtual ~CertLoaderTest() {}
virtual void SetUp() override {
ASSERT_TRUE(primary_user_.constructed_successfully());
ASSERT_TRUE(
crypto::GetPublicSlotForChromeOSUser(primary_user_.username_hash()));
CertLoader::Initialize();
cert_loader_ = CertLoader::Get();
cert_loader_->AddObserver(this);
}
virtual void TearDown() {
cert_loader_->RemoveObserver(this);
CertLoader::Shutdown();
}
protected:
void StartCertLoaderWithPrimaryUser() {
FinishUserInitAndGetDatabase(&primary_user_, &primary_db_);
cert_loader_->StartWithNSSDB(primary_db_.get());
base::RunLoop().RunUntilIdle();
GetAndResetCertificatesLoadedEventsCount();
}
// CertLoader::Observer:
// The test keeps count of times the observer method was called.
virtual void OnCertificatesLoaded(const net::CertificateList& cert_list,
bool initial_load) override {
EXPECT_TRUE(certificates_loaded_events_count_ == 0 || !initial_load);
certificates_loaded_events_count_++;
}
// Returns the number of |OnCertificatesLoaded| calls observed since the
// last call to this method equals |value|.
size_t GetAndResetCertificatesLoadedEventsCount() {
size_t result = certificates_loaded_events_count_;
certificates_loaded_events_count_ = 0;
return result;
}
// Finishes initialization for the |user| and returns a user's NSS database
// instance.
void FinishUserInitAndGetDatabase(
crypto::ScopedTestNSSChromeOSUser* user,
scoped_ptr<net::NSSCertDatabaseChromeOS>* database) {
ASSERT_TRUE(user->constructed_successfully());
user->FinishInit();
crypto::ScopedPK11Slot private_slot(
crypto::GetPrivateSlotForChromeOSUser(
user->username_hash(),
base::Bind(&FailOnPrivateSlotCallback)));
ASSERT_TRUE(private_slot);
database->reset(new net::NSSCertDatabaseChromeOS(
crypto::GetPublicSlotForChromeOSUser(user->username_hash()),
private_slot.Pass()));
(*database)->SetSlowTaskRunnerForTest(message_loop_.message_loop_proxy());
}
int GetDbPrivateSlotId(net::NSSCertDatabase* db) {
return static_cast<int>(PK11_GetSlotID(db->GetPrivateSlot().get()));
}
void ImportCACert(const std::string& cert_file,
net::NSSCertDatabase* database,
net::CertificateList* imported_certs) {
ASSERT_TRUE(database);
ASSERT_TRUE(imported_certs);
// Add a certificate to the user's db.
*imported_certs = net::CreateCertificateListFromFile(
net::GetTestCertsDirectory(),
cert_file,
net::X509Certificate::FORMAT_AUTO);
ASSERT_EQ(1U, imported_certs->size());
net::NSSCertDatabase::ImportCertFailureList failed;
ASSERT_TRUE(database->ImportCACerts(*imported_certs,
net::NSSCertDatabase::TRUST_DEFAULT,
&failed));
ASSERT_TRUE(failed.empty());
}
void ImportClientCertAndKey(const std::string& pkcs12_file,
net::NSSCertDatabase* database,
net::CertificateList* imported_certs) {
ASSERT_TRUE(database);
ASSERT_TRUE(imported_certs);
std::string pkcs12_data;
base::FilePath pkcs12_file_path =
net::GetTestCertsDirectory().Append(pkcs12_file);
ASSERT_TRUE(base::ReadFileToString(pkcs12_file_path, &pkcs12_data));
net::CertificateList client_cert_list;
scoped_refptr<net::CryptoModule> module(net::CryptoModule::CreateFromHandle(
database->GetPrivateSlot().get()));
ASSERT_EQ(net::OK,
database->ImportFromPKCS12(module.get(),
pkcs12_data,
base::string16(),
false,
imported_certs));
ASSERT_EQ(1U, imported_certs->size());
}
CertLoader* cert_loader_;
// The user is primary as the one whose certificates CertLoader handles, it
// has nothing to do with crypto::InitializeNSSForChromeOSUser is_primary_user
// parameter (which is irrelevant for these tests).
crypto::ScopedTestNSSChromeOSUser primary_user_;
scoped_ptr<net::NSSCertDatabaseChromeOS> primary_db_;
base::MessageLoop message_loop_;
private:
size_t certificates_loaded_events_count_;
};
TEST_F(CertLoaderTest, Basic) {
EXPECT_FALSE(cert_loader_->CertificatesLoading());
EXPECT_FALSE(cert_loader_->certificates_loaded());
FinishUserInitAndGetDatabase(&primary_user_, &primary_db_);
cert_loader_->StartWithNSSDB(primary_db_.get());
EXPECT_FALSE(cert_loader_->certificates_loaded());
EXPECT_TRUE(cert_loader_->CertificatesLoading());
EXPECT_TRUE(cert_loader_->cert_list().empty());
ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
EXPECT_TRUE(cert_loader_->certificates_loaded());
EXPECT_FALSE(cert_loader_->CertificatesLoading());
// Default CA cert roots should get loaded.
EXPECT_FALSE(cert_loader_->cert_list().empty());
}
TEST_F(CertLoaderTest, CertLoaderUpdatesCertListOnNewCert) {
StartCertLoaderWithPrimaryUser();
net::CertificateList certs;
ImportCACert("root_ca_cert.pem", primary_db_.get(), &certs);
// Certs are loaded asynchronously, so the new cert should not yet be in the
// cert list.
EXPECT_FALSE(
IsCertInCertificateList(certs[0].get(), cert_loader_->cert_list()));
ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
// The certificate list should be updated now, as the message loop's been run.
EXPECT_TRUE(
IsCertInCertificateList(certs[0].get(), cert_loader_->cert_list()));
EXPECT_FALSE(cert_loader_->IsCertificateHardwareBacked(certs[0].get()));
}
TEST_F(CertLoaderTest, CertLoaderNoUpdateOnSecondaryDbChanges) {
crypto::ScopedTestNSSChromeOSUser secondary_user("secondary");
scoped_ptr<net::NSSCertDatabaseChromeOS> secondary_db;
StartCertLoaderWithPrimaryUser();
FinishUserInitAndGetDatabase(&secondary_user, &secondary_db);
net::CertificateList certs;
ImportCACert("root_ca_cert.pem", secondary_db.get(), &certs);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(
IsCertInCertificateList(certs[0].get(), cert_loader_->cert_list()));
}
TEST_F(CertLoaderTest, ClientLoaderUpdateOnNewClientCert) {
StartCertLoaderWithPrimaryUser();
net::CertificateList certs;
ImportClientCertAndKey("websocket_client_cert.p12",
primary_db_.get(),
&certs);
ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
EXPECT_TRUE(
IsCertInCertificateList(certs[0].get(), cert_loader_->cert_list()));
}
TEST_F(CertLoaderTest, CertLoaderNoUpdateOnNewClientCertInSecondaryDb) {
crypto::ScopedTestNSSChromeOSUser secondary_user("secondary");
scoped_ptr<net::NSSCertDatabaseChromeOS> secondary_db;
StartCertLoaderWithPrimaryUser();
FinishUserInitAndGetDatabase(&secondary_user, &secondary_db);
net::CertificateList certs;
ImportClientCertAndKey("websocket_client_cert.p12",
secondary_db.get(),
&certs);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(
IsCertInCertificateList(certs[0].get(), cert_loader_->cert_list()));
}
TEST_F(CertLoaderTest, UpdatedOnCertRemoval) {
StartCertLoaderWithPrimaryUser();
net::CertificateList certs;
ImportClientCertAndKey("websocket_client_cert.p12",
primary_db_.get(),
&certs);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
ASSERT_TRUE(
IsCertInCertificateList(certs[0].get(), cert_loader_->cert_list()));
primary_db_->DeleteCertAndKey(certs[0].get());
ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
ASSERT_FALSE(
IsCertInCertificateList(certs[0].get(), cert_loader_->cert_list()));
}
TEST_F(CertLoaderTest, UpdatedOnCACertTrustChange) {
StartCertLoaderWithPrimaryUser();
net::CertificateList certs;
ImportCACert("root_ca_cert.pem", primary_db_.get(), &certs);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
ASSERT_TRUE(
IsCertInCertificateList(certs[0].get(), cert_loader_->cert_list()));
// The value that should have been set by |ImportCACert|.
ASSERT_EQ(net::NSSCertDatabase::TRUST_DEFAULT,
primary_db_->GetCertTrust(certs[0].get(), net::CA_CERT));
ASSERT_TRUE(primary_db_->SetCertTrust(
certs[0].get(), net::CA_CERT, net::NSSCertDatabase::TRUSTED_SSL));
// Cert trust change should trigger certificate reload in cert_loader_.
ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
}
} // namespace
} // namespace chromeos
#endif
|