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