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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/server_certificate_database/server_certificate_database_service.h"
#include <memory>
#include "base/files/scoped_temp_dir.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "components/server_certificate_database/server_certificate_database_test_util.h"
#include "net/test/cert_builder.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "crypto/scoped_test_nss_db.h"
#include "net/cert/nss_cert_database.h"
#include "net/cert/x509_util_nss.h"
#endif
using chrome_browser_server_certificate_database::CertificateTrust;
using ::testing::UnorderedElementsAre;
namespace net {
namespace {
#if BUILDFLAG(IS_CHROMEOS)
void NssSlotGetter(crypto::ScopedPK11Slot slot,
base::OnceCallback<void(crypto::ScopedPK11Slot)> callback) {
std::move(callback).Run(std::move(slot));
}
#endif
} // namespace
class ServerCertificateDatabaseServiceTest : public testing::Test {
public:
void SetUp() override {
ASSERT_TRUE(temp_profile_dir_.CreateUniqueTempDir());
#if BUILDFLAG(IS_CHROMEOS)
ServerCertificateDatabaseService::RegisterProfilePrefs(
pref_service_.registry());
test_nss_slot_ = std::make_unique<crypto::ScopedTestNSSDB>();
ASSERT_TRUE(test_nss_slot_->is_open());
nss_cert_database_ = std::make_unique<NSSCertDatabase>(
crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nss_slot_->slot())),
crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nss_slot_->slot())));
#endif
}
std::unique_ptr<net::ServerCertificateDatabaseService> CreateService() {
#if BUILDFLAG(IS_CHROMEOS)
return std::make_unique<ServerCertificateDatabaseService>(
temp_profile_dir_.GetPath(), &pref_service_,
base::BindOnce(&NssSlotGetter,
crypto::ScopedPK11Slot(
PK11_ReferenceSlot(test_nss_slot_->slot()))));
#else
return std::make_unique<ServerCertificateDatabaseService>(
temp_profile_dir_.GetPath());
#endif
}
#if BUILDFLAG(IS_CHROMEOS)
PrefService* pref_service() { return &pref_service_; }
NSSCertDatabase* nss_cert_database() { return nss_cert_database_.get(); }
#endif
private:
base::test::TaskEnvironment task_environment_;
base::ScopedTempDir temp_profile_dir_;
#if BUILDFLAG(IS_CHROMEOS)
TestingPrefServiceSimple pref_service_;
std::unique_ptr<crypto::ScopedTestNSSDB> test_nss_slot_;
std::unique_ptr<net::NSSCertDatabase> nss_cert_database_;
#endif
};
TEST_F(ServerCertificateDatabaseServiceTest, TestNotifications) {
auto [leaf, root] = CertBuilder::CreateSimpleChain2();
std::unique_ptr<net::ServerCertificateDatabaseService> cert_db_service =
CreateService();
base::test::TestFuture<void> update_waiter;
auto scoped_observer_subscription =
cert_db_service->AddObserver(update_waiter.GetRepeatingCallback());
// Insert a new cert.
{
base::test::TestFuture<bool> insert_waiter;
std::vector<net::ServerCertificateDatabase::CertInformation> cert_infos;
cert_infos.push_back(MakeCertInfo(
root->GetDER(), CertificateTrust::CERTIFICATE_TRUST_TYPE_TRUSTED));
cert_db_service->AddOrUpdateUserCertificates(std::move(cert_infos),
insert_waiter.GetCallback());
// Insert should be successful.
EXPECT_TRUE(insert_waiter.Take());
}
// Observer notification should have been delivered.
EXPECT_TRUE(update_waiter.WaitAndClear());
// Update metadata for existing cert.
{
base::test::TestFuture<bool> insert_waiter;
std::vector<net::ServerCertificateDatabase::CertInformation> cert_infos;
cert_infos.push_back(MakeCertInfo(
root->GetDER(), CertificateTrust::CERTIFICATE_TRUST_TYPE_DISTRUSTED));
cert_db_service->AddOrUpdateUserCertificates(std::move(cert_infos),
insert_waiter.GetCallback());
// Update should be successful.
EXPECT_TRUE(insert_waiter.Take());
}
// Observer notification should have been delivered.
EXPECT_TRUE(update_waiter.WaitAndClear());
// Delete a cert.
{
base::test::TestFuture<bool> delete_waiter;
auto cert_info = MakeCertInfo(
root->GetDER(), CertificateTrust::CERTIFICATE_TRUST_TYPE_DISTRUSTED);
cert_db_service->DeleteCertificate(cert_info.sha256hash_hex,
delete_waiter.GetCallback());
// Delete should be successful.
EXPECT_TRUE(delete_waiter.Take());
}
// Observer notification should have been delivered.
EXPECT_TRUE(update_waiter.WaitAndClear());
// Try to delete a cert that doesn't exist.
{
base::test::TestFuture<bool> delete_waiter;
auto cert_info = MakeCertInfo(
root->GetDER(), CertificateTrust::CERTIFICATE_TRUST_TYPE_DISTRUSTED);
cert_db_service->DeleteCertificate(cert_info.sha256hash_hex,
delete_waiter.GetCallback());
// Delete should fail since the cert doesn't exist in the database.
EXPECT_FALSE(delete_waiter.Take());
}
// Observer notification should not be delivered since nothing was actually
// changed.
EXPECT_FALSE(update_waiter.IsReady());
}
#if BUILDFLAG(IS_CHROMEOS)
using ServerCertificateDatabaseServiceNSSMigratorTest =
ServerCertificateDatabaseServiceTest;
TEST_F(ServerCertificateDatabaseServiceNSSMigratorTest, TestMigration) {
auto [leaf, root] = CertBuilder::CreateSimpleChain2();
// Import test certificate into NSS user database.
NSSCertDatabase::ImportCertFailureList not_imported;
EXPECT_TRUE(nss_cert_database()->ImportCACerts(
x509_util::CreateCERTCertificateListFromX509Certificate(
root->GetX509Certificate().get()),
NSSCertDatabase::TRUSTED_SSL, ¬_imported));
EXPECT_TRUE(not_imported.empty());
std::unique_ptr<net::ServerCertificateDatabaseService> cert_db_service =
CreateService();
// Verify that server cert database starts empty and migration pref default
// is false.
{
base::test::TestFuture<uint32_t> get_certs_count_waiter;
cert_db_service->GetCertificatesCount(get_certs_count_waiter.GetCallback());
EXPECT_EQ(0U, get_certs_count_waiter.Get());
EXPECT_EQ(
pref_service()->GetInteger(prefs::kNSSCertsMigratedToServerCertDb),
static_cast<int>(ServerCertificateDatabaseService::
NSSMigrationResultPref::kNotMigrated));
}
// Call GetAllCertificates to begin the migration.
{
base::test::TestFuture<
std::vector<net::ServerCertificateDatabase::CertInformation>>
migrate_and_get_certs_waiter;
cert_db_service->GetAllCertificates(
migrate_and_get_certs_waiter.GetCallback());
std::vector<net::ServerCertificateDatabase::CertInformation> cert_infos =
migrate_and_get_certs_waiter.Take();
// Test that the the result includes the migrated cert.
ServerCertificateDatabase::CertInformation expected_nss_root_info =
MakeCertInfo(root->GetDER(),
CertificateTrust::CERTIFICATE_TRUST_TYPE_TRUSTED);
EXPECT_THAT(
cert_infos,
UnorderedElementsAre(CertInfoEquals(std::ref(expected_nss_root_info))));
// Migration pref should be true now.
EXPECT_EQ(
pref_service()->GetInteger(prefs::kNSSCertsMigratedToServerCertDb),
static_cast<int>(ServerCertificateDatabaseService::
NSSMigrationResultPref::kMigratedSuccessfully));
}
// Change the settings of the cert that was imported.
{
base::test::TestFuture<bool> update_cert_waiter;
std::vector<net::ServerCertificateDatabase::CertInformation> cert_infos;
cert_infos.push_back(MakeCertInfo(
root->GetDER(), CertificateTrust::CERTIFICATE_TRUST_TYPE_DISTRUSTED));
cert_db_service->AddOrUpdateUserCertificates(
std::move(cert_infos), update_cert_waiter.GetCallback());
EXPECT_TRUE(update_cert_waiter.Take());
}
// Call GetAllCertificates again. Since the migration already completed, this
// should just get the current contents of the database without re-doing the
// migration.
{
base::test::TestFuture<
std::vector<net::ServerCertificateDatabase::CertInformation>>
migrate_and_get_certs_waiter;
cert_db_service->GetAllCertificates(
migrate_and_get_certs_waiter.GetCallback());
std::vector<net::ServerCertificateDatabase::CertInformation> cert_infos =
migrate_and_get_certs_waiter.Take();
// Test that the the result still includes the modified cert data and hasn't
// been overwritten by the NSS settings (which is what would happen if the
// migration was repeated).
ServerCertificateDatabase::CertInformation expected_modified_root_info =
MakeCertInfo(root->GetDER(),
CertificateTrust::CERTIFICATE_TRUST_TYPE_DISTRUSTED);
EXPECT_THAT(cert_infos, UnorderedElementsAre(CertInfoEquals(
std::ref(expected_modified_root_info))));
}
}
TEST_F(ServerCertificateDatabaseServiceNSSMigratorTest, SimultaneousCalls) {
auto [leaf, root] = CertBuilder::CreateSimpleChain2();
// Import test certificate into NSS user database.
NSSCertDatabase::ImportCertFailureList not_imported;
EXPECT_TRUE(nss_cert_database()->ImportCACerts(
x509_util::CreateCERTCertificateListFromX509Certificate(
root->GetX509Certificate().get()),
NSSCertDatabase::TRUSTED_SSL, ¬_imported));
EXPECT_TRUE(not_imported.empty());
std::unique_ptr<net::ServerCertificateDatabaseService> cert_db_service =
CreateService();
// Call GetAllCertificates multiple times.
base::test::TestFuture<
std::vector<net::ServerCertificateDatabase::CertInformation>>
waiter1;
base::test::TestFuture<
std::vector<net::ServerCertificateDatabase::CertInformation>>
waiter2;
cert_db_service->GetAllCertificates(waiter1.GetCallback());
cert_db_service->GetAllCertificates(waiter2.GetCallback());
// Migration pref should be false still.
EXPECT_EQ(pref_service()->GetInteger(prefs::kNSSCertsMigratedToServerCertDb),
static_cast<int>(ServerCertificateDatabaseService::
NSSMigrationResultPref::kNotMigrated));
// Both callbacks should get run and both should have the migrated cert.
ServerCertificateDatabase::CertInformation expected_nss_root_info =
MakeCertInfo(root->GetDER(),
CertificateTrust::CERTIFICATE_TRUST_TYPE_TRUSTED);
std::vector<net::ServerCertificateDatabase::CertInformation> cert_infos1 =
waiter1.Take();
std::vector<net::ServerCertificateDatabase::CertInformation> cert_infos2 =
waiter2.Take();
EXPECT_THAT(
cert_infos1,
UnorderedElementsAre(CertInfoEquals(std::ref(expected_nss_root_info))));
EXPECT_THAT(
cert_infos2,
UnorderedElementsAre(CertInfoEquals(std::ref(expected_nss_root_info))));
// Migration pref should be true now.
EXPECT_EQ(
pref_service()->GetInteger(prefs::kNSSCertsMigratedToServerCertDb),
static_cast<int>(ServerCertificateDatabaseService::
NSSMigrationResultPref::kMigratedSuccessfully));
}
#endif // BUILDFLAG(IS_CHROMEOS)
} // namespace net
|