[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 5 | #include "chromeos/cert_loader.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | #include "base/chromeos/chromeos_version.h" |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 10 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 11 | #include "base/observer_list.h" |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 12 | #include "base/sequenced_task_runner.h" |
[email protected] | 5341905 | 2013-05-14 04:37:56 | [diff] [blame] | 13 | #include "base/strings/string_number_conversions.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 14 | #include "base/task_runner_util.h" |
| 15 | #include "base/threading/worker_pool.h" |
| 16 | #include "chromeos/dbus/cryptohome_client.h" |
| 17 | #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 | #include "crypto/encryptor.h" |
| 19 | #include "crypto/nss_util.h" |
| 20 | #include "crypto/sha2.h" |
| 21 | #include "crypto/symmetric_key.h" |
| 22 | #include "net/cert/nss_cert_database.h" |
| 23 | |
| 24 | namespace chromeos { |
| 25 | |
| 26 | namespace { |
| 27 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 28 | const int64 kInitialRequestDelayMs = 100; |
| 29 | const int64 kMaxRequestDelayMs = 300000; // 5 minutes |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 30 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 31 | // Calculates the delay before running next attempt to initiatialize the TPM |
| 32 | // token, if |last_delay| was the last or initial delay. |
| 33 | base::TimeDelta GetNextRequestDelayMs(base::TimeDelta last_delay) { |
| 34 | // This implements an exponential backoff, as we don't know in which order of |
| 35 | // magnitude the TPM token changes it's state. |
| 36 | base::TimeDelta next_delay = last_delay * 2; |
| 37 | |
| 38 | // Cap the delay to prevent an overflow. This threshold is arbitrarily chosen. |
| 39 | const base::TimeDelta max_delay = |
| 40 | base::TimeDelta::FromMilliseconds(kMaxRequestDelayMs); |
| 41 | if (next_delay > max_delay) |
| 42 | next_delay = max_delay; |
| 43 | return next_delay; |
| 44 | } |
| 45 | |
| 46 | void LoadNSSCertificates(net::CertificateList* cert_list) { |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 47 | net::NSSCertDatabase::GetInstance()->ListCerts(cert_list); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 50 | void CallOpenPersistentNSSDB() { |
| 51 | // Called from crypto_task_runner_. |
| 52 | VLOG(1) << "CallOpenPersistentNSSDB"; |
| 53 | |
| 54 | // Ensure we've opened the user's key/certificate database. |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 55 | if (base::chromeos::IsRunningOnChromeOS()) |
| 56 | crypto::OpenPersistentNSSDB(); |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 57 | crypto::EnableTPMTokenForNSS(); |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 60 | } // namespace |
| 61 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 62 | static CertLoader* g_cert_loader = NULL; |
[email protected] | 18e8d54f | 2013-08-06 17:15:48 | [diff] [blame] | 63 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 64 | // static |
| 65 | void CertLoader::Initialize() { |
| 66 | CHECK(!g_cert_loader); |
| 67 | g_cert_loader = new CertLoader(); |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // static |
| 71 | void CertLoader::Shutdown() { |
| 72 | CHECK(g_cert_loader); |
| 73 | delete g_cert_loader; |
| 74 | g_cert_loader = NULL; |
| 75 | } |
| 76 | |
| 77 | // static |
| 78 | CertLoader* CertLoader::Get() { |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 79 | CHECK(g_cert_loader) << "CertLoader::Get() called before Initialize()"; |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 80 | return g_cert_loader; |
| 81 | } |
| 82 | |
| 83 | // static |
| 84 | bool CertLoader::IsInitialized() { |
| 85 | return g_cert_loader; |
| 86 | } |
| 87 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 88 | CertLoader::CertLoader() |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 89 | : initialize_tpm_for_test_(false), |
| 90 | certificates_requested_(false), |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 91 | certificates_loaded_(false), |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 92 | certificates_update_required_(false), |
| 93 | certificates_update_running_(false), |
| 94 | tpm_token_state_(TPM_STATE_UNKNOWN), |
| 95 | tpm_request_delay_( |
| 96 | base::TimeDelta::FromMilliseconds(kInitialRequestDelayMs)), |
| 97 | initialize_token_factory_(this), |
| 98 | update_certificates_factory_(this) { |
[email protected] | 437a9f8f | 2013-05-28 18:33:30 | [diff] [blame] | 99 | if (LoginState::IsInitialized()) |
| 100 | LoginState::Get()->AddObserver(this); |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 103 | void CertLoader::InitializeTPMForTest() { |
| 104 | initialize_tpm_for_test_ = true; |
| 105 | } |
| 106 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 107 | void CertLoader::SetCryptoTaskRunner( |
| 108 | const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner) { |
| 109 | crypto_task_runner_ = crypto_task_runner; |
| 110 | MaybeRequestCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 18e8d54f | 2013-08-06 17:15:48 | [diff] [blame] | 113 | void CertLoader::SetSlowTaskRunnerForTest( |
[email protected] | b1f3f527 | 2013-08-12 15:22:49 | [diff] [blame] | 114 | const scoped_refptr<base::TaskRunner>& task_runner) { |
[email protected] | 18e8d54f | 2013-08-06 17:15:48 | [diff] [blame] | 115 | slow_task_runner_for_test_ = task_runner; |
| 116 | } |
| 117 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 118 | CertLoader::~CertLoader() { |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 119 | net::CertDatabase::GetInstance()->RemoveObserver(this); |
[email protected] | 437a9f8f | 2013-05-28 18:33:30 | [diff] [blame] | 120 | if (LoginState::IsInitialized()) |
| 121 | LoginState::Get()->RemoveObserver(this); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void CertLoader::AddObserver(CertLoader::Observer* observer) { |
| 125 | observers_.AddObserver(observer); |
| 126 | } |
| 127 | |
| 128 | void CertLoader::RemoveObserver(CertLoader::Observer* observer) { |
| 129 | observers_.RemoveObserver(observer); |
| 130 | } |
| 131 | |
| 132 | bool CertLoader::CertificatesLoading() const { |
| 133 | return certificates_requested_ && !certificates_loaded_; |
| 134 | } |
| 135 | |
| 136 | bool CertLoader::IsHardwareBacked() const { |
| 137 | return !tpm_token_name_.empty(); |
| 138 | } |
| 139 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 140 | void CertLoader::MaybeRequestCertificates() { |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 141 | CHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 142 | |
| 143 | // This is the entry point to the TPM token initialization process, |
| 144 | // which we should do at most once. |
| 145 | if (certificates_requested_ || !crypto_task_runner_.get()) |
| 146 | return; |
| 147 | |
[email protected] | 437a9f8f | 2013-05-28 18:33:30 | [diff] [blame] | 148 | const bool logged_in = LoginState::IsInitialized() ? |
| 149 | LoginState::Get()->IsUserLoggedIn() : false; |
| 150 | VLOG(1) << "RequestCertificates: " << logged_in; |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 151 | if (!logged_in) |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 152 | return; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 153 | |
| 154 | certificates_requested_ = true; |
| 155 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 156 | // Ensure we only initialize the TPM token once. |
| 157 | DCHECK_EQ(tpm_token_state_, TPM_STATE_UNKNOWN); |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 158 | if (!initialize_tpm_for_test_ && !base::chromeos::IsRunningOnChromeOS()) |
| 159 | tpm_token_state_ = TPM_DISABLED; |
| 160 | |
| 161 | // Treat TPM as disabled for guest users since they do not store certs. |
| 162 | if (LoginState::IsInitialized() && LoginState::Get()->IsGuestUser()) |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 163 | tpm_token_state_ = TPM_DISABLED; |
| 164 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 165 | InitializeTokenAndLoadCertificates(); |
| 166 | } |
| 167 | |
| 168 | void CertLoader::InitializeTokenAndLoadCertificates() { |
| 169 | CHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 170 | VLOG(1) << "InitializeTokenAndLoadCertificates: " << tpm_token_state_; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 171 | |
[email protected] | 437a9f8f | 2013-05-28 18:33:30 | [diff] [blame] | 172 | switch (tpm_token_state_) { |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 173 | case TPM_STATE_UNKNOWN: { |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 174 | crypto_task_runner_->PostTaskAndReply( |
| 175 | FROM_HERE, |
| 176 | base::Bind(&CallOpenPersistentNSSDB), |
| 177 | base::Bind(&CertLoader::OnPersistentNSSDBOpened, |
| 178 | initialize_token_factory_.GetWeakPtr())); |
| 179 | return; |
| 180 | } |
| 181 | case TPM_DB_OPENED: { |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 182 | DBusThreadManager::Get()->GetCryptohomeClient()->TpmIsEnabled( |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 183 | base::Bind(&CertLoader::OnTpmIsEnabled, |
| 184 | initialize_token_factory_.GetWeakPtr())); |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | case TPM_DISABLED: { |
| 188 | // TPM is disabled, so proceed with empty tpm token name. |
| 189 | StartLoadCertificates(); |
| 190 | return; |
| 191 | } |
| 192 | case TPM_ENABLED: { |
| 193 | DBusThreadManager::Get()->GetCryptohomeClient()->Pkcs11IsTpmTokenReady( |
| 194 | base::Bind(&CertLoader::OnPkcs11IsTpmTokenReady, |
| 195 | initialize_token_factory_.GetWeakPtr())); |
| 196 | return; |
| 197 | } |
| 198 | case TPM_TOKEN_READY: { |
| 199 | // Retrieve token_name_ and user_pin_ here since they will never change |
| 200 | // and CryptohomeClient calls are not thread safe. |
| 201 | DBusThreadManager::Get()->GetCryptohomeClient()->Pkcs11GetTpmTokenInfo( |
| 202 | base::Bind(&CertLoader::OnPkcs11GetTpmTokenInfo, |
| 203 | initialize_token_factory_.GetWeakPtr())); |
| 204 | return; |
| 205 | } |
| 206 | case TPM_TOKEN_INFO_RECEIVED: { |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 207 | base::PostTaskAndReplyWithResult( |
| 208 | crypto_task_runner_.get(), |
| 209 | FROM_HERE, |
| 210 | base::Bind( |
| 211 | &crypto::InitializeTPMToken, tpm_token_name_, tpm_user_pin_), |
| 212 | base::Bind(&CertLoader::OnTPMTokenInitialized, |
| 213 | initialize_token_factory_.GetWeakPtr())); |
| 214 | return; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 215 | } |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 216 | case TPM_TOKEN_INITIALIZED: { |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 217 | StartLoadCertificates(); |
| 218 | return; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void CertLoader::RetryTokenInitializationLater() { |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 224 | CHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 225 | LOG(WARNING) << "Retry token initialization later."; |
[email protected] | df90563 | 2013-05-29 23:04:36 | [diff] [blame] | 226 | base::MessageLoop::current()->PostDelayedTask( |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 227 | FROM_HERE, |
| 228 | base::Bind(&CertLoader::InitializeTokenAndLoadCertificates, |
| 229 | initialize_token_factory_.GetWeakPtr()), |
| 230 | tpm_request_delay_); |
| 231 | tpm_request_delay_ = GetNextRequestDelayMs(tpm_request_delay_); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 234 | void CertLoader::OnPersistentNSSDBOpened() { |
| 235 | VLOG(1) << "PersistentNSSDBOpened"; |
| 236 | tpm_token_state_ = TPM_DB_OPENED; |
| 237 | InitializeTokenAndLoadCertificates(); |
| 238 | } |
| 239 | |
[email protected] | b1f3f527 | 2013-08-12 15:22:49 | [diff] [blame] | 240 | // This is copied from chrome/common/net/x509_certificate_model_nss.cc. |
[email protected] | 5341905 | 2013-05-14 04:37:56 | [diff] [blame] | 241 | // For background see this discussion on dev-tech-crypto.lists.mozilla.org: |
| 242 | // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX |
| 243 | // |
| 244 | // NOTE: This function relies on the convention that the same PKCS#11 ID |
| 245 | // is shared between a certificate and its associated private and public |
| 246 | // keys. I tried to implement this with PK11_GetLowLevelKeyIDForCert(), |
| 247 | // but that always returns NULL on Chrome OS for me. |
[email protected] | 5341905 | 2013-05-14 04:37:56 | [diff] [blame] | 248 | |
[email protected] | b1f3f527 | 2013-08-12 15:22:49 | [diff] [blame] | 249 | // static |
| 250 | std::string CertLoader::GetPkcs11IdForCert(const net::X509Certificate& cert) { |
[email protected] | 5341905 | 2013-05-14 04:37:56 | [diff] [blame] | 251 | CERTCertificateStr* cert_handle = cert.os_cert_handle(); |
| 252 | SECKEYPrivateKey *priv_key = |
| 253 | PK11_FindKeyByAnyCert(cert_handle, NULL /* wincx */); |
| 254 | if (!priv_key) |
| 255 | return std::string(); |
| 256 | |
| 257 | // Get the CKA_ID attribute for a key. |
| 258 | SECItem* sec_item = PK11_GetLowLevelKeyIDForPrivateKey(priv_key); |
| 259 | std::string pkcs11_id; |
| 260 | if (sec_item) { |
| 261 | pkcs11_id = base::HexEncode(sec_item->data, sec_item->len); |
| 262 | SECITEM_FreeItem(sec_item, PR_TRUE); |
| 263 | } |
| 264 | SECKEY_DestroyPrivateKey(priv_key); |
| 265 | |
| 266 | return pkcs11_id; |
| 267 | } |
| 268 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 269 | void CertLoader::OnTpmIsEnabled(DBusMethodCallStatus call_status, |
| 270 | bool tpm_is_enabled) { |
| 271 | VLOG(1) << "OnTpmIsEnabled: " << tpm_is_enabled; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 272 | |
| 273 | if (call_status == DBUS_METHOD_CALL_SUCCESS && tpm_is_enabled) |
| 274 | tpm_token_state_ = TPM_ENABLED; |
| 275 | else |
| 276 | tpm_token_state_ = TPM_DISABLED; |
| 277 | |
| 278 | InitializeTokenAndLoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void CertLoader::OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, |
| 282 | bool is_tpm_token_ready) { |
| 283 | VLOG(1) << "OnPkcs11IsTpmTokenReady: " << is_tpm_token_ready; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 284 | |
| 285 | if (call_status == DBUS_METHOD_CALL_FAILURE || !is_tpm_token_ready) { |
| 286 | RetryTokenInitializationLater(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 287 | return; |
| 288 | } |
| 289 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 290 | tpm_token_state_ = TPM_TOKEN_READY; |
| 291 | InitializeTokenAndLoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void CertLoader::OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, |
| 295 | const std::string& token_name, |
| 296 | const std::string& user_pin) { |
| 297 | VLOG(1) << "OnPkcs11GetTpmTokenInfo: " << token_name; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 298 | |
| 299 | if (call_status == DBUS_METHOD_CALL_FAILURE) { |
| 300 | RetryTokenInitializationLater(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 301 | return; |
| 302 | } |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 303 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 304 | tpm_token_name_ = token_name; |
| 305 | // TODO(stevenjb): The network code expects a slot ID, not a label. See |
| 306 | // crbug.com/201101. For now, use a hard coded, well known slot instead. |
| 307 | const char kHardcodedTpmSlot[] = "0"; |
| 308 | tpm_token_slot_ = kHardcodedTpmSlot; |
| 309 | tpm_user_pin_ = user_pin; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 310 | tpm_token_state_ = TPM_TOKEN_INFO_RECEIVED; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 311 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 312 | InitializeTokenAndLoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 315 | void CertLoader::OnTPMTokenInitialized(bool success) { |
| 316 | VLOG(1) << "OnTPMTokenInitialized: " << success; |
| 317 | if (!success) { |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 318 | RetryTokenInitializationLater(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 319 | return; |
| 320 | } |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 321 | tpm_token_state_ = TPM_TOKEN_INITIALIZED; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 322 | InitializeTokenAndLoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void CertLoader::StartLoadCertificates() { |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 326 | DCHECK(!certificates_loaded_ && !certificates_update_running_); |
| 327 | net::CertDatabase::GetInstance()->AddObserver(this); |
| 328 | LoadCertificates(); |
| 329 | } |
| 330 | |
| 331 | void CertLoader::LoadCertificates() { |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 332 | CHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 333 | VLOG(1) << "LoadCertificates: " << certificates_update_running_; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 334 | |
| 335 | if (certificates_update_running_) { |
| 336 | certificates_update_required_ = true; |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | net::CertificateList* cert_list = new net::CertificateList; |
| 341 | certificates_update_running_ = true; |
[email protected] | 8e80c288 | 2013-05-23 10:43:40 | [diff] [blame] | 342 | certificates_update_required_ = false; |
[email protected] | 18e8d54f | 2013-08-06 17:15:48 | [diff] [blame] | 343 | |
| 344 | base::TaskRunner* task_runner = slow_task_runner_for_test_.get(); |
| 345 | if (!task_runner) |
| 346 | task_runner = base::WorkerPool::GetTaskRunner(true /* task is slow */); |
| 347 | task_runner->PostTaskAndReply( |
| 348 | FROM_HERE, |
| 349 | base::Bind(LoadNSSCertificates, cert_list), |
| 350 | base::Bind(&CertLoader::UpdateCertificates, |
| 351 | update_certificates_factory_.GetWeakPtr(), |
| 352 | base::Owned(cert_list))); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { |
| 356 | CHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 357 | DCHECK(certificates_update_running_); |
| 358 | VLOG(1) << "UpdateCertificates: " << cert_list->size(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 359 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 360 | // Ignore any existing certificates. |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 361 | cert_list_.swap(*cert_list); |
| 362 | |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 363 | bool initial_load = !certificates_loaded_; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 364 | certificates_loaded_ = true; |
[email protected] | 16674118 | 2013-08-06 11:31:27 | [diff] [blame] | 365 | NotifyCertificatesLoaded(initial_load); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 366 | |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 367 | certificates_update_running_ = false; |
| 368 | if (certificates_update_required_) |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 369 | LoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void CertLoader::NotifyCertificatesLoaded(bool initial_load) { |
| 373 | FOR_EACH_OBSERVER(Observer, observers_, |
| 374 | OnCertificatesLoaded(cert_list_, initial_load)); |
| 375 | } |
| 376 | |
| 377 | void CertLoader::OnCertTrustChanged(const net::X509Certificate* cert) { |
| 378 | } |
| 379 | |
| 380 | void CertLoader::OnCertAdded(const net::X509Certificate* cert) { |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 381 | VLOG(1) << "OnCertAdded"; |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 382 | LoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 386 | VLOG(1) << "OnCertRemoved"; |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame^] | 387 | LoadCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void CertLoader::LoggedInStateChanged(LoginState::LoggedInState state) { |
| 391 | VLOG(1) << "LoggedInStateChanged: " << state; |
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 392 | MaybeRequestCertificates(); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | } // namespace chromeos |