Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "chromeos/network/cert_loader.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/chromeos/chromeos_version.h" | |
| 10 #include "base/observer_list_threadsafe.h" | |
| 11 #include "base/task_runner_util.h" | |
| 12 #include "base/threading/worker_pool.h" | |
| 13 #include "chromeos/dbus/cryptohome_client.h" | |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 15 #include "crypto/encryptor.h" | |
| 16 #include "crypto/nss_util.h" | |
| 17 #include "crypto/sha2.h" | |
| 18 #include "crypto/symmetric_key.h" | |
| 19 #include "net/cert/nss_cert_database.h" | |
| 20 | |
| 21 namespace chromeos { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Delay between certificate requests while waiting for TPM/PKCS#11 init. | |
| 26 const int kRequestDelayMs = 500; | |
| 27 | |
| 28 net::CertificateList* LoadNSSCertificates() { | |
| 29 net::CertificateList* cert_list(new net::CertificateList()); | |
| 30 net::NSSCertDatabase::GetInstance()->ListCerts(cert_list); | |
| 31 return cert_list; | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 static CertLoader* g_cert_loader = NULL; | |
| 37 | |
| 38 // static | |
| 39 void CertLoader::Initialize() { | |
| 40 CHECK(!g_cert_loader); | |
| 41 g_cert_loader = new CertLoader(); | |
| 42 } | |
| 43 | |
| 44 // static | |
| 45 void CertLoader::Shutdown() { | |
| 46 CHECK(g_cert_loader); | |
| 47 delete g_cert_loader; | |
| 48 g_cert_loader = NULL; | |
| 49 } | |
| 50 | |
| 51 // static | |
| 52 CertLoader* CertLoader::Get() { | |
| 53 CHECK(g_cert_loader) << "CertLoader::Get() called before Initialize()"; | |
| 54 return g_cert_loader; | |
| 55 } | |
| 56 | |
| 57 CertLoader::CertLoader() | |
| 58 : observer_list_(new ObserverList), | |
| 59 tpm_token_ready_(false), | |
| 60 certificates_requested_(false), | |
| 61 certificates_loaded_(false), | |
| 62 key_store_loaded_(false), | |
| 63 weak_ptr_factory_(this) { | |
| 64 net::CertDatabase::GetInstance()->AddObserver(this); | |
| 65 LoginState::Get()->AddObserver(this); | |
| 66 if (LoginState::Get()->IsUserLoggedIn()) | |
| 67 RequestCertificates(); | |
| 68 } | |
| 69 | |
| 70 CertLoader::~CertLoader() { | |
| 71 DCHECK(request_task_.is_null()); | |
| 72 net::CertDatabase::GetInstance()->RemoveObserver(this); | |
| 73 LoginState::Get()->RemoveObserver(this); | |
| 74 } | |
| 75 | |
| 76 void CertLoader::AddObserver(CertLoader::Observer* observer) { | |
| 77 observer_list_->AddObserver(observer); | |
| 78 } | |
| 79 | |
| 80 void CertLoader::RemoveObserver(CertLoader::Observer* observer) { | |
| 81 observer_list_->RemoveObserver(observer); | |
| 82 } | |
| 83 | |
| 84 bool CertLoader::RequestCertificates() { | |
| 85 CHECK(thread_checker_.CalledOnValidThread()); | |
| 86 VLOG(1) << "RequestCertificates: " << LoginState::Get()->IsUserLoggedIn(); | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
nit: clarify VLOG to explain the printed value.
E.
stevenjb
2013/05/03 01:00:31
I don't feel that would really be more helpful for
| |
| 87 if (!LoginState::Get()->IsUserLoggedIn()) | |
| 88 return false; | |
| 89 | |
| 90 if (!key_store_loaded_) { | |
| 91 // Ensure we've opened the real user's key/certificate database. | |
| 92 crypto::OpenPersistentNSSDB(); | |
| 93 | |
| 94 if (base::chromeos::IsRunningOnChromeOS()) | |
| 95 crypto::EnableTPMTokenForNSS(); | |
| 96 | |
| 97 key_store_loaded_ = true; | |
| 98 } | |
| 99 | |
| 100 certificates_requested_ = true; | |
| 101 | |
| 102 VLOG(1) << "Requesting Certificates."; | |
| 103 DBusThreadManager::Get()->GetCryptohomeClient()->TpmIsEnabled( | |
| 104 base::Bind(&CertLoader::OnTpmIsEnabled, | |
| 105 weak_ptr_factory_.GetWeakPtr())); | |
| 106 | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 bool CertLoader::CertificatesLoading() const { | |
| 111 return certificates_requested_ && !certificates_loaded_; | |
| 112 } | |
| 113 | |
| 114 bool CertLoader::IsHardwareBacked() const { | |
| 115 return !tpm_token_name_.empty(); | |
| 116 } | |
| 117 | |
| 118 // private methods | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
nit: not helpful?
stevenjb
2013/05/03 01:00:31
Done.
| |
| 119 | |
| 120 void CertLoader::OnTpmIsEnabled(DBusMethodCallStatus call_status, | |
| 121 bool tpm_is_enabled) { | |
| 122 VLOG(1) << "OnTpmIsEnabled: " << tpm_is_enabled; | |
| 123 if (call_status != DBUS_METHOD_CALL_SUCCESS || !tpm_is_enabled) { | |
| 124 // TPM is not enabled, so proceed with empty tpm token name. | |
| 125 VLOG(1) << "TPM not available."; | |
| 126 StartLoadCertificates(); | |
| 127 } else if (tpm_token_ready_) { | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
verbally this doesn't make any sense
.. if it's re
stevenjb
2013/05/03 01:00:31
Um, yes? The TPM needs to be initialized, once it
pneubeck (no reviews)
2013/05/03 09:42:54
"ready" sounds to me more like the initialization
| |
| 128 InitializeTPMToken(); | |
| 129 } else { | |
| 130 DBusThreadManager::Get()->GetCryptohomeClient()->Pkcs11IsTpmTokenReady( | |
| 131 base::Bind(&CertLoader::OnPkcs11IsTpmTokenReady, | |
| 132 weak_ptr_factory_.GetWeakPtr())); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 void CertLoader::OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, | |
| 137 bool is_tpm_token_ready) { | |
| 138 VLOG(1) << "OnPkcs11IsTpmTokenReady: " << is_tpm_token_ready; | |
| 139 if (call_status != DBUS_METHOD_CALL_SUCCESS || !is_tpm_token_ready) { | |
| 140 MaybeRetryRequestCertificates(); | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 // Retrieve token_name_ and user_pin_ here since they will never change | |
| 145 // and CryptohomeClient calls are not thread safe. | |
| 146 DBusThreadManager::Get()->GetCryptohomeClient()->Pkcs11GetTpmTokenInfo( | |
| 147 base::Bind(&CertLoader::OnPkcs11GetTpmTokenInfo, | |
| 148 weak_ptr_factory_.GetWeakPtr())); | |
| 149 } | |
| 150 | |
| 151 void CertLoader::OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, | |
| 152 const std::string& token_name, | |
| 153 const std::string& user_pin) { | |
| 154 VLOG(1) << "OnPkcs11GetTpmTokenInfo: " << token_name; | |
| 155 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | |
| 156 MaybeRetryRequestCertificates(); | |
| 157 return; | |
| 158 } | |
| 159 tpm_token_name_ = token_name; | |
| 160 tpm_user_pin_ = user_pin; | |
| 161 tpm_token_ready_ = true; | |
| 162 | |
| 163 InitializeTPMToken(); | |
| 164 } | |
| 165 | |
| 166 void CertLoader::InitializeTPMToken() { | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
maybe rename to "InitializeNSSForTPM".
| |
| 167 VLOG(1) << "InitializeTPMToken"; | |
| 168 if (!crypto::InitializeTPMToken(tpm_token_name_, tpm_user_pin_)) { | |
| 169 MaybeRetryRequestCertificates(); | |
| 170 return; | |
| 171 } | |
| 172 StartLoadCertificates(); | |
| 173 } | |
| 174 | |
| 175 void CertLoader::StartLoadCertificates() { | |
| 176 VLOG(1) << "Start Load Certificates"; | |
| 177 base::PostTaskAndReplyWithResult( | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
Not that it's really problematic, but I'd guess th
stevenjb
2013/05/03 01:00:31
Not sure I follow right now, but seems like a chan
| |
| 178 base::WorkerPool::GetTaskRunner(true /* task_is_slow */), | |
| 179 FROM_HERE, | |
| 180 base::Bind(LoadNSSCertificates), | |
| 181 base::Bind(&CertLoader::UpdateCertificates, | |
| 182 weak_ptr_factory_.GetWeakPtr())); | |
| 183 } | |
| 184 | |
| 185 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { | |
| 186 CHECK(thread_checker_.CalledOnValidThread()); | |
| 187 DCHECK(cert_list); | |
| 188 VLOG(1) << "Update Certificates: " << cert_list->size(); | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
nit: make vlog self explanatory. "Number of certs:
stevenjb
2013/05/03 01:00:31
Again, doesn't seem helpful to me, will leave for
| |
| 189 | |
| 190 // Clear any existing certificates. | |
| 191 cert_list_ = *cert_list; // Copy vector | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
nit: why not swap?
stevenjb
2013/05/03 01:00:31
Done.
| |
| 192 | |
| 193 // cert_list is constructed in LoadCertificates. | |
| 194 delete cert_list; | |
| 195 | |
| 196 // Set loaded state and notify observers. | |
| 197 if (!certificates_loaded_) { | |
| 198 certificates_loaded_ = true; | |
| 199 NotifyCertificatesLoaded(true); | |
| 200 } else { | |
| 201 NotifyCertificatesLoaded(false); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 void CertLoader::MaybeRetryRequestCertificates() { | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
Rename to RetryRequestCertificates
| |
| 206 if (!request_task_.is_null()) | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
I don't see the gain of storing the closure reques
stevenjb
2013/05/03 01:00:31
TODO: Refactor
| |
| 207 return; | |
| 208 | |
| 209 // Cryptohome does not notify us when the token is ready, so call | |
| 210 // this again after a delay. | |
| 211 request_task_ = base::Bind(&CertLoader::RequestCertificatesTask, | |
| 212 weak_ptr_factory_.GetWeakPtr()); | |
| 213 MessageLoop::current()->PostDelayedTask( | |
| 214 FROM_HERE, | |
| 215 request_task_, | |
| 216 base::TimeDelta::FromMilliseconds(kRequestDelayMs)); | |
| 217 } | |
| 218 | |
| 219 void CertLoader::RequestCertificatesTask() { | |
| 220 // Reset the task to the initial state so is_null() returns true. | |
| 221 request_task_.Reset(); | |
| 222 RequestCertificates(); | |
| 223 } | |
| 224 | |
| 225 void CertLoader::NotifyCertificatesLoaded(bool initial_load) { | |
| 226 observer_list_->Notify( | |
| 227 &CertLoader::Observer::OnCertificatesLoaded, cert_list_, initial_load); | |
| 228 } | |
| 229 | |
| 230 void CertLoader::OnCertTrustChanged(const net::X509Certificate* cert) { | |
| 231 } | |
| 232 | |
| 233 void CertLoader::OnCertAdded(const net::X509Certificate* cert) { | |
| 234 // Only load certificates if we have completed an initial request. | |
| 235 if (!certificates_loaded_) | |
| 236 return; | |
| 237 StartLoadCertificates(); | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
There is a race:
if LoadNSSCertificates() is curr
stevenjb
2013/05/03 01:00:31
TODO: Refactor
| |
| 238 } | |
| 239 | |
| 240 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { | |
| 241 // Only load certificates if we have completed an initial request. | |
| 242 if (!certificates_loaded_) | |
| 243 return; | |
| 244 StartLoadCertificates(); | |
| 245 } | |
| 246 | |
| 247 void CertLoader::LoggedInStateChanged(LoginState::LoggedInState state) { | |
| 248 VLOG(1) << "LoggedInStateChanged: " << state; | |
| 249 if (LoginState::Get()->IsUserLoggedIn() && !certificates_requested_) | |
|
pneubeck (no reviews)
2013/05/02 14:58:35
the conditional !certificates_requested_ shouldn't
stevenjb
2013/05/03 01:00:31
TODO:Refactor
| |
| 250 RequestCertificates(); | |
| 251 } | |
| 252 | |
| 253 } // namespace chromeos | |
| OLD | NEW |