| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/cert_loader.h" | 5 #include "chromeos/cert_loader.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/chromeos/chromeos_version.h" | 9 #include "base/chromeos/chromeos_version.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 // Cap the delay to prevent an overflow. This threshold is arbitrarily chosen. | 38 // Cap the delay to prevent an overflow. This threshold is arbitrarily chosen. |
| 39 const base::TimeDelta max_delay = | 39 const base::TimeDelta max_delay = |
| 40 base::TimeDelta::FromMilliseconds(kMaxRequestDelayMs); | 40 base::TimeDelta::FromMilliseconds(kMaxRequestDelayMs); |
| 41 if (next_delay > max_delay) | 41 if (next_delay > max_delay) |
| 42 next_delay = max_delay; | 42 next_delay = max_delay; |
| 43 return next_delay; | 43 return next_delay; |
| 44 } | 44 } |
| 45 | 45 |
| 46 void LoadNSSCertificates(net::CertificateList* cert_list) { | 46 void LoadNSSCertificates(net::CertificateList* cert_list) { |
| 47 if (base::chromeos::IsRunningOnChromeOS()) | 47 net::NSSCertDatabase::GetInstance()->ListCerts(cert_list); |
| 48 net::NSSCertDatabase::GetInstance()->ListCerts(cert_list); | |
| 49 } | 48 } |
| 50 | 49 |
| 51 void CallOpenPersistentNSSDB() { | 50 void CallOpenPersistentNSSDB() { |
| 52 // Called from crypto_task_runner_. | 51 // Called from crypto_task_runner_. |
| 53 VLOG(1) << "CallOpenPersistentNSSDB"; | 52 VLOG(1) << "CallOpenPersistentNSSDB"; |
| 54 | 53 |
| 55 // Ensure we've opened the user's key/certificate database. | 54 // Ensure we've opened the user's key/certificate database. |
| 56 crypto::OpenPersistentNSSDB(); | 55 crypto::OpenPersistentNSSDB(); |
| 57 if (base::chromeos::IsRunningOnChromeOS()) | 56 crypto::EnableTPMTokenForNSS(); |
| 58 crypto::EnableTPMTokenForNSS(); | |
| 59 } | 57 } |
| 60 | 58 |
| 61 } // namespace | 59 } // namespace |
| 62 | 60 |
| 63 static CertLoader* g_cert_loader = NULL; | 61 static CertLoader* g_cert_loader = NULL; |
| 64 // static | 62 // static |
| 65 void CertLoader::Initialize() { | 63 void CertLoader::Initialize() { |
| 66 CHECK(!g_cert_loader); | 64 CHECK(!g_cert_loader); |
| 67 g_cert_loader = new CertLoader(); | 65 g_cert_loader = new CertLoader(); |
| 68 g_cert_loader->Init(); | 66 g_cert_loader->Init(); |
| 69 } | 67 } |
| 70 | 68 |
| 71 // static | 69 // static |
| 72 void CertLoader::Shutdown() { | 70 void CertLoader::Shutdown() { |
| 73 CHECK(g_cert_loader); | 71 CHECK(g_cert_loader); |
| 74 delete g_cert_loader; | 72 delete g_cert_loader; |
| 75 g_cert_loader = NULL; | 73 g_cert_loader = NULL; |
| 76 } | 74 } |
| 77 | 75 |
| 78 // static | 76 // static |
| 79 CertLoader* CertLoader::Get() { | 77 CertLoader* CertLoader::Get() { |
| 80 CHECK(g_cert_loader) | 78 CHECK(g_cert_loader) << "CertLoader::Get() called before Initialize()"; |
| 81 << "CertLoader::Get() called before Initialize()"; | |
| 82 return g_cert_loader; | 79 return g_cert_loader; |
| 83 } | 80 } |
| 84 | 81 |
| 85 // static | 82 // static |
| 86 bool CertLoader::IsInitialized() { | 83 bool CertLoader::IsInitialized() { |
| 87 return g_cert_loader; | 84 return g_cert_loader; |
| 88 } | 85 } |
| 89 | 86 |
| 90 CertLoader::CertLoader() | 87 CertLoader::CertLoader() |
| 91 : certificates_requested_(false), | 88 : certificates_requested_(false), |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 const bool logged_in = LoginState::IsInitialized() ? | 141 const bool logged_in = LoginState::IsInitialized() ? |
| 145 LoginState::Get()->IsUserLoggedIn() : false; | 142 LoginState::Get()->IsUserLoggedIn() : false; |
| 146 VLOG(1) << "RequestCertificates: " << logged_in; | 143 VLOG(1) << "RequestCertificates: " << logged_in; |
| 147 if (!logged_in) | 144 if (!logged_in) |
| 148 return; | 145 return; |
| 149 | 146 |
| 150 certificates_requested_ = true; | 147 certificates_requested_ = true; |
| 151 | 148 |
| 152 // Ensure we only initialize the TPM token once. | 149 // Ensure we only initialize the TPM token once. |
| 153 DCHECK_EQ(tpm_token_state_, TPM_STATE_UNKNOWN); | 150 DCHECK_EQ(tpm_token_state_, TPM_STATE_UNKNOWN); |
| 151 if (!base::chromeos::IsRunningOnChromeOS()) |
| 152 tpm_token_state_ = TPM_DISABLED; |
| 153 |
| 154 InitializeTokenAndLoadCertificates(); | 154 InitializeTokenAndLoadCertificates(); |
| 155 } | 155 } |
| 156 | 156 |
| 157 void CertLoader::InitializeTokenAndLoadCertificates() { | 157 void CertLoader::InitializeTokenAndLoadCertificates() { |
| 158 CHECK(thread_checker_.CalledOnValidThread()); | 158 CHECK(thread_checker_.CalledOnValidThread()); |
| 159 VLOG(1) << "InitializeTokenAndLoadCertificates: " << tpm_token_state_; | 159 VLOG(1) << "InitializeTokenAndLoadCertificates: " << tpm_token_state_; |
| 160 | 160 |
| 161 switch (tpm_token_state_) { | 161 switch (tpm_token_state_) { |
| 162 case TPM_STATE_UNKNOWN: { | 162 case TPM_STATE_UNKNOWN: { |
| 163 crypto_task_runner_->PostTaskAndReply( | 163 crypto_task_runner_->PostTaskAndReply( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 186 } | 186 } |
| 187 case TPM_TOKEN_READY: { | 187 case TPM_TOKEN_READY: { |
| 188 // Retrieve token_name_ and user_pin_ here since they will never change | 188 // Retrieve token_name_ and user_pin_ here since they will never change |
| 189 // and CryptohomeClient calls are not thread safe. | 189 // and CryptohomeClient calls are not thread safe. |
| 190 DBusThreadManager::Get()->GetCryptohomeClient()->Pkcs11GetTpmTokenInfo( | 190 DBusThreadManager::Get()->GetCryptohomeClient()->Pkcs11GetTpmTokenInfo( |
| 191 base::Bind(&CertLoader::OnPkcs11GetTpmTokenInfo, | 191 base::Bind(&CertLoader::OnPkcs11GetTpmTokenInfo, |
| 192 initialize_token_factory_.GetWeakPtr())); | 192 initialize_token_factory_.GetWeakPtr())); |
| 193 return; | 193 return; |
| 194 } | 194 } |
| 195 case TPM_TOKEN_INFO_RECEIVED: { | 195 case TPM_TOKEN_INFO_RECEIVED: { |
| 196 if (base::chromeos::IsRunningOnChromeOS()) { | 196 base::PostTaskAndReplyWithResult( |
| 197 base::PostTaskAndReplyWithResult( | 197 crypto_task_runner_.get(), |
| 198 crypto_task_runner_.get(), | 198 FROM_HERE, |
| 199 FROM_HERE, | 199 base::Bind( |
| 200 base::Bind(&crypto::InitializeTPMToken, | 200 &crypto::InitializeTPMToken, tpm_token_name_, tpm_user_pin_), |
| 201 tpm_token_name_, tpm_user_pin_), | 201 base::Bind(&CertLoader::OnTPMTokenInitialized, |
| 202 base::Bind(&CertLoader::OnTPMTokenInitialized, | 202 initialize_token_factory_.GetWeakPtr())); |
| 203 initialize_token_factory_.GetWeakPtr())); | 203 return; |
| 204 return; | |
| 205 } | |
| 206 tpm_token_state_ = TPM_TOKEN_INITIALIZED; | 204 tpm_token_state_ = TPM_TOKEN_INITIALIZED; |
| 207 // FALL_THROUGH_INTENDED | 205 // FALL_THROUGH_INTENDED |
| 208 } | 206 } |
| 209 case TPM_TOKEN_INITIALIZED: { | 207 case TPM_TOKEN_INITIALIZED: { |
| 210 StartLoadCertificates(); | 208 StartLoadCertificates(); |
| 211 return; | 209 return; |
| 212 } | 210 } |
| 213 } | 211 } |
| 214 } | 212 } |
| 215 | 213 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 } | 336 } |
| 339 | 337 |
| 340 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { | 338 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { |
| 341 CHECK(thread_checker_.CalledOnValidThread()); | 339 CHECK(thread_checker_.CalledOnValidThread()); |
| 342 DCHECK(certificates_update_running_); | 340 DCHECK(certificates_update_running_); |
| 343 VLOG(1) << "UpdateCertificates: " << cert_list->size(); | 341 VLOG(1) << "UpdateCertificates: " << cert_list->size(); |
| 344 | 342 |
| 345 // Ignore any existing certificates. | 343 // Ignore any existing certificates. |
| 346 cert_list_.swap(*cert_list); | 344 cert_list_.swap(*cert_list); |
| 347 | 345 |
| 348 NotifyCertificatesLoaded(!certificates_loaded_); | 346 bool initial_load = !certificates_loaded_; |
| 349 certificates_loaded_ = true; | 347 certificates_loaded_ = true; |
| 348 NotifyCertificatesLoaded(initial_load); |
| 350 | 349 |
| 351 certificates_update_running_ = false; | 350 certificates_update_running_ = false; |
| 352 if (certificates_update_required_) | 351 if (certificates_update_required_) |
| 353 StartLoadCertificates(); | 352 StartLoadCertificates(); |
| 354 } | 353 } |
| 355 | 354 |
| 356 void CertLoader::NotifyCertificatesLoaded(bool initial_load) { | 355 void CertLoader::NotifyCertificatesLoaded(bool initial_load) { |
| 357 FOR_EACH_OBSERVER(Observer, observers_, | 356 FOR_EACH_OBSERVER(Observer, observers_, |
| 358 OnCertificatesLoaded(cert_list_, initial_load)); | 357 OnCertificatesLoaded(cert_list_, initial_load)); |
| 359 } | 358 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 370 VLOG(1) << "OnCertRemoved"; | 369 VLOG(1) << "OnCertRemoved"; |
| 371 StartLoadCertificates(); | 370 StartLoadCertificates(); |
| 372 } | 371 } |
| 373 | 372 |
| 374 void CertLoader::LoggedInStateChanged(LoginState::LoggedInState state) { | 373 void CertLoader::LoggedInStateChanged(LoginState::LoggedInState state) { |
| 375 VLOG(1) << "LoggedInStateChanged: " << state; | 374 VLOG(1) << "LoggedInStateChanged: " << state; |
| 376 MaybeRequestCertificates(); | 375 MaybeRequestCertificates(); |
| 377 } | 376 } |
| 378 | 377 |
| 379 } // namespace chromeos | 378 } // namespace chromeos |
| OLD | NEW |