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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/cert/nss_cert_database_chromeos.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "crypto/nss_util_internal.h"
#include "crypto/scoped_test_nss_chromeos_user.h"
#include "crypto/scoped_test_nss_db.h"
#include "net/cert/cert_database.h"
#include "net/cert/x509_util_nss.h"
#include "net/test/cert_builder.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
#include "net/test/test_with_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
bool IsCertInCertificateList(const X509Certificate* cert,
const ScopedCERTCertificateList& cert_list) {
for (const auto& other : cert_list) {
if (x509_util::IsSameCertificate(other.get(), cert))
return true;
}
return false;
}
bool IsCertInCertificateList(CERTCertificate* cert,
const ScopedCERTCertificateList& cert_list) {
for (const auto& other : cert_list) {
if (x509_util::IsSameCertificate(other.get(), cert))
return true;
}
return false;
}
void SwapCertLists(ScopedCERTCertificateList* destination,
ScopedCERTCertificateList source) {
ASSERT_TRUE(destination);
destination->swap(source);
}
} // namespace
class NSSCertDatabaseChromeOSTest : public TestWithTaskEnvironment,
public CertDatabase::Observer {
public:
NSSCertDatabaseChromeOSTest() : user_1_("user1"), user_2_("user2") {}
void SetUp() override {
// Initialize nss_util slots.
ASSERT_TRUE(user_1_.constructed_successfully());
ASSERT_TRUE(user_2_.constructed_successfully());
user_1_.FinishInit();
user_2_.FinishInit();
// Create NSSCertDatabaseChromeOS for each user.
db_1_ = std::make_unique<NSSCertDatabaseChromeOS>(
crypto::GetPublicSlotForChromeOSUser(user_1_.username_hash()),
crypto::GetPrivateSlotForChromeOSUser(
user_1_.username_hash(),
base::OnceCallback<void(crypto::ScopedPK11Slot)>()));
db_1_->SetSystemSlot(
crypto::ScopedPK11Slot(PK11_ReferenceSlot(system_db_.slot())));
db_2_ = std::make_unique<NSSCertDatabaseChromeOS>(
crypto::GetPublicSlotForChromeOSUser(user_2_.username_hash()),
crypto::GetPrivateSlotForChromeOSUser(
user_2_.username_hash(),
base::OnceCallback<void(crypto::ScopedPK11Slot)>()));
// Add observer to CertDatabase for checking that notifications from
// NSSCertDatabaseChromeOS are proxied to the CertDatabase.
CertDatabase::GetInstance()->AddObserver(this);
observer_added_ = true;
}
void TearDown() override {
if (observer_added_)
CertDatabase::GetInstance()->RemoveObserver(this);
}
// CertDatabase::Observer:
void OnTrustStoreChanged() override { trust_store_changed_count_++; }
void OnClientCertStoreChanged() override { client_cert_changed_count_++; }
protected:
bool observer_added_ = false;
int trust_store_changed_count_ = 0;
int client_cert_changed_count_ = 0;
crypto::ScopedTestNSSChromeOSUser user_1_;
crypto::ScopedTestNSSChromeOSUser user_2_;
crypto::ScopedTestNSSDB system_db_;
std::unique_ptr<NSSCertDatabaseChromeOS> db_1_;
std::unique_ptr<NSSCertDatabaseChromeOS> db_2_;
};
// Test that ListModules() on each user includes that user's NSS software slot,
// and does not include the software slot of the other user. (Does not check the
// private slot, since it is the same as the public slot in tests.)
TEST_F(NSSCertDatabaseChromeOSTest, ListModules) {
std::vector<crypto::ScopedPK11Slot> modules_1;
std::vector<crypto::ScopedPK11Slot> modules_2;
db_1_->ListModules(&modules_1, false /* need_rw */);
db_2_->ListModules(&modules_2, false /* need_rw */);
bool found_1 = false;
for (std::vector<crypto::ScopedPK11Slot>::iterator it = modules_1.begin();
it != modules_1.end(); ++it) {
EXPECT_NE(db_2_->GetPublicSlot().get(), (*it).get());
if ((*it).get() == db_1_->GetPublicSlot().get())
found_1 = true;
}
EXPECT_TRUE(found_1);
bool found_2 = false;
for (std::vector<crypto::ScopedPK11Slot>::iterator it = modules_2.begin();
it != modules_2.end(); ++it) {
EXPECT_NE(db_1_->GetPublicSlot().get(), (*it).get());
if ((*it).get() == db_2_->GetPublicSlot().get())
found_2 = true;
}
EXPECT_TRUE(found_2);
}
// Test that ImportCACerts imports the cert to the correct slot, and that
// ListCerts includes the added cert for the correct user, and does not include
// it for the other user.
TEST_F(NSSCertDatabaseChromeOSTest, ImportCACerts) {
// Load test certs from disk.
ScopedCERTCertificateList certs_1 = CreateCERTCertificateListFromFile(
GetTestCertsDirectory(), "root_ca_cert.pem",
X509Certificate::FORMAT_AUTO);
ASSERT_EQ(1U, certs_1.size());
auto [leaf2, root2] = CertBuilder::CreateSimpleChain2();
ScopedCERTCertificateList certs_2 =
x509_util::CreateCERTCertificateListFromX509Certificate(
root2->GetX509Certificate().get());
ASSERT_EQ(1U, certs_2.size());
// Import one cert for each user.
NSSCertDatabase::ImportCertFailureList failed;
EXPECT_TRUE(
db_1_->ImportCACerts(certs_1, NSSCertDatabase::TRUSTED_SSL, &failed));
EXPECT_EQ(0U, failed.size());
failed.clear();
EXPECT_TRUE(
db_2_->ImportCACerts(certs_2, NSSCertDatabase::TRUSTED_SSL, &failed));
EXPECT_EQ(0U, failed.size());
// Get cert list for each user.
ScopedCERTCertificateList user_1_certlist;
ScopedCERTCertificateList user_2_certlist;
db_1_->ListCerts(
base::BindOnce(&SwapCertLists, base::Unretained(&user_1_certlist)));
db_2_->ListCerts(
base::BindOnce(&SwapCertLists, base::Unretained(&user_2_certlist)));
// Run the message loop so the observer notifications get processed and
// lookups are completed.
RunUntilIdle();
// Should have gotten two OnTrustStoreChanged notifications.
EXPECT_EQ(2, trust_store_changed_count_);
EXPECT_EQ(0, client_cert_changed_count_);
EXPECT_TRUE(IsCertInCertificateList(certs_1[0].get(), user_1_certlist));
EXPECT_FALSE(IsCertInCertificateList(certs_1[0].get(), user_2_certlist));
EXPECT_TRUE(IsCertInCertificateList(certs_2[0].get(), user_2_certlist));
EXPECT_FALSE(IsCertInCertificateList(certs_2[0].get(), user_1_certlist));
}
// Test that ImportServerCerts imports the cert to the correct slot, and that
// ListCerts includes the added cert for the correct user, and does not include
// it for the other user.
TEST_F(NSSCertDatabaseChromeOSTest, ImportServerCert) {
// Load test certs from disk.
ScopedCERTCertificateList certs_1 = CreateCERTCertificateListFromFile(
GetTestCertsDirectory(), "ok_cert.pem", X509Certificate::FORMAT_AUTO);
ASSERT_EQ(1U, certs_1.size());
auto [leaf2, root2] = CertBuilder::CreateSimpleChain2();
ScopedCERTCertificateList certs_2 =
x509_util::CreateCERTCertificateListFromX509Certificate(
leaf2->GetX509Certificate().get());
ASSERT_EQ(1U, certs_2.size());
// Import one cert for each user.
NSSCertDatabase::ImportCertFailureList failed;
EXPECT_TRUE(
db_1_->ImportServerCert(certs_1, NSSCertDatabase::TRUSTED_SSL, &failed));
EXPECT_EQ(0U, failed.size());
failed.clear();
EXPECT_TRUE(
db_2_->ImportServerCert(certs_2, NSSCertDatabase::TRUSTED_SSL, &failed));
EXPECT_EQ(0U, failed.size());
// Get cert list for each user.
ScopedCERTCertificateList user_1_certlist;
ScopedCERTCertificateList user_2_certlist;
db_1_->ListCerts(
base::BindOnce(&SwapCertLists, base::Unretained(&user_1_certlist)));
db_2_->ListCerts(
base::BindOnce(&SwapCertLists, base::Unretained(&user_2_certlist)));
// Run the message loop so the observer notifications get processed and
// lookups are completed.
RunUntilIdle();
// TODO(mattm): this should be 2, but ImportServerCert doesn't currently
// generate notifications.
EXPECT_EQ(0, trust_store_changed_count_);
EXPECT_EQ(0, client_cert_changed_count_);
EXPECT_TRUE(IsCertInCertificateList(certs_1[0].get(), user_1_certlist));
EXPECT_FALSE(IsCertInCertificateList(certs_1[0].get(), user_2_certlist));
EXPECT_TRUE(IsCertInCertificateList(certs_2[0].get(), user_2_certlist));
EXPECT_FALSE(IsCertInCertificateList(certs_2[0].get(), user_1_certlist));
}
// Tests that There is no crash if the database is deleted while ListCerts
// is being processed on the worker pool.
TEST_F(NSSCertDatabaseChromeOSTest, NoCrashIfShutdownBeforeDoneOnWorkerPool) {
ScopedCERTCertificateList certlist;
db_1_->ListCerts(base::BindOnce(&SwapCertLists, base::Unretained(&certlist)));
EXPECT_EQ(0U, certlist.size());
db_1_.reset();
RunUntilIdle();
EXPECT_LT(0U, certlist.size());
}
TEST_F(NSSCertDatabaseChromeOSTest, ListCertsReadsSystemSlot) {
scoped_refptr<X509Certificate> cert_1(
ImportClientCertAndKeyFromFile(GetTestCertsDirectory(),
"client_1.pem",
"client_1.pk8",
db_1_->GetPublicSlot().get()));
scoped_refptr<X509Certificate> cert_2(
ImportClientCertAndKeyFromFile(GetTestCertsDirectory(),
"client_2.pem",
"client_2.pk8",
db_1_->GetSystemSlot().get()));
ScopedCERTCertificateList certs;
db_1_->ListCerts(base::BindOnce(&SwapCertLists, base::Unretained(&certs)));
RunUntilIdle();
EXPECT_TRUE(IsCertInCertificateList(cert_1.get(), certs));
EXPECT_TRUE(IsCertInCertificateList(cert_2.get(), certs));
}
TEST_F(NSSCertDatabaseChromeOSTest, ListCertsDoesNotCrossReadSystemSlot) {
scoped_refptr<X509Certificate> cert_1(
ImportClientCertAndKeyFromFile(GetTestCertsDirectory(),
"client_1.pem",
"client_1.pk8",
db_2_->GetPublicSlot().get()));
scoped_refptr<X509Certificate> cert_2(
ImportClientCertAndKeyFromFile(GetTestCertsDirectory(),
"client_2.pem",
"client_2.pk8",
system_db_.slot()));
ScopedCERTCertificateList certs;
db_2_->ListCerts(base::BindOnce(&SwapCertLists, base::Unretained(&certs)));
RunUntilIdle();
EXPECT_TRUE(IsCertInCertificateList(cert_1.get(), certs));
EXPECT_FALSE(IsCertInCertificateList(cert_2.get(), certs));
}
TEST_F(NSSCertDatabaseChromeOSTest, SetCertTrustCertIsAlreadyOnPublicSlot) {
// Import a certificate onto the public slot (and safety check that it ended
// up there).
ScopedCERTCertificateList certs = CreateCERTCertificateListFromFile(
GetTestCertsDirectory(), "root_ca_cert.pem",
X509Certificate::FORMAT_AUTO);
ASSERT_EQ(1U, certs.size());
NSSCertDatabase::ImportCertFailureList failed;
EXPECT_TRUE(
db_1_->ImportCACerts(certs, NSSCertDatabase::TRUST_DEFAULT, &failed));
EXPECT_EQ(0U, failed.size());
ASSERT_TRUE(NSSCertDatabase::IsCertificateOnSlot(
certs[0].get(), db_1_->GetPublicSlot().get()));
// Check that trust settings modification works.
EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT,
db_1_->GetCertTrust(certs[0].get(), CA_CERT));
EXPECT_TRUE(db_1_->SetCertTrust(certs[0].get(), CA_CERT,
NSSCertDatabase::TRUSTED_SSL));
EXPECT_EQ(NSSCertDatabase::TRUSTED_SSL,
db_1_->GetCertTrust(certs[0].get(), CA_CERT));
}
TEST_F(NSSCertDatabaseChromeOSTest, SetCertTrustCertIsOnlyOnOtherSlot) {
crypto::ScopedTestNSSDB other_slot;
// Import a certificate onto a slot known by NSS which is not the
// NSSCertDatabase's public slot.
ScopedCERTCertificateList certs = CreateCERTCertificateListFromFile(
GetTestCertsDirectory(), "root_ca_cert.pem",
X509Certificate::FORMAT_AUTO);
ASSERT_EQ(1U, certs.size());
ASSERT_EQ(SECSuccess, PK11_ImportCert(other_slot.slot(), certs[0].get(),
CK_INVALID_HANDLE, "cert0",
PR_FALSE /* includeTrust (unused) */));
ASSERT_FALSE(NSSCertDatabase::IsCertificateOnSlot(
certs[0].get(), db_1_->GetPublicSlot().get()));
// Check that trust settings modification works.
EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT,
db_1_->GetCertTrust(certs[0].get(), CA_CERT));
EXPECT_TRUE(db_1_->SetCertTrust(certs[0].get(), CA_CERT,
NSSCertDatabase::TRUSTED_SSL));
EXPECT_EQ(NSSCertDatabase::TRUSTED_SSL,
db_1_->GetCertTrust(certs[0].get(), CA_CERT));
// Check that the certificate has been put onto the public slot as a side
// effect of changing trust.
EXPECT_TRUE(NSSCertDatabase::IsCertificateOnSlot(
certs[0].get(), db_1_->GetPublicSlot().get()));
}
TEST_F(NSSCertDatabaseChromeOSTest, SetCertTrustPublicSlotIsSystemSlot) {
// Create a NSSCertDatabase with |public_slot|==|system_slot|.
NSSCertDatabaseChromeOS test_db_for_system_slot(
/*public_slot=*/crypto::ScopedPK11Slot(
PK11_ReferenceSlot(system_db_.slot())),
/*private_slot=*/{});
test_db_for_system_slot.SetSystemSlot(
crypto::ScopedPK11Slot(PK11_ReferenceSlot(system_db_.slot())));
// Import a certificate onto a slot known by NSS which is not the
// NSSCertDatabase's public slot.
crypto::ScopedTestNSSDB other_slot;
ScopedCERTCertificateList certs = CreateCERTCertificateListFromFile(
GetTestCertsDirectory(), "root_ca_cert.pem",
X509Certificate::FORMAT_AUTO);
ASSERT_EQ(1U, certs.size());
ASSERT_EQ(SECSuccess, PK11_ImportCert(other_slot.slot(), certs[0].get(),
CK_INVALID_HANDLE, "cert0",
PR_FALSE /* includeTrust (unused) */));
ASSERT_FALSE(NSSCertDatabase::IsCertificateOnSlot(
certs[0].get(), test_db_for_system_slot.GetPublicSlot().get()));
// Changing trust through |test_db_for_system_slot| should fail and not do
// anything, because the database is not allowed to put the certificate onto
// its public slot (because it is also the system slot).
EXPECT_FALSE(test_db_for_system_slot.SetCertTrust(
certs[0].get(), CA_CERT, NSSCertDatabase::TRUSTED_SSL));
EXPECT_FALSE(NSSCertDatabase::IsCertificateOnSlot(
certs[0].get(), test_db_for_system_slot.GetPublicSlot().get()));
}
} // namespace net
|