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.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; | |
|
hashimoto
2013/05/03 16:07:44
nit: How about moving this into the unnamed namesp
stevenjb
2013/05/03 17:02:32
Global for debugging.
| |
| 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 : tpm_token_ready_(false), | |
| 59 certificates_requested_(false), | |
| 60 certificates_loaded_(false), | |
| 61 key_store_loaded_(false), | |
| 62 weak_ptr_factory_(this) { | |
| 63 net::CertDatabase::GetInstance()->AddObserver(this); | |
| 64 LoginState::Get()->AddObserver(this); | |
| 65 if (LoginState::Get()->IsUserLoggedIn()) | |
| 66 RequestCertificates(); | |
| 67 } | |
| 68 | |
| 69 CertLoader::~CertLoader() { | |
| 70 DCHECK(request_task_.is_null()); | |
| 71 net::CertDatabase::GetInstance()->RemoveObserver(this); | |
| 72 LoginState::Get()->RemoveObserver(this); | |
| 73 } | |
| 74 | |
| 75 void CertLoader::AddObserver(CertLoader::Observer* observer) { | |
| 76 observers_.AddObserver(observer); | |
| 77 } | |
| 78 | |
| 79 void CertLoader::RemoveObserver(CertLoader::Observer* observer) { | |
| 80 observers_.RemoveObserver(observer); | |
| 81 } | |
| 82 | |
| 83 bool CertLoader::CertificatesLoading() const { | |
| 84 return certificates_requested_ && !certificates_loaded_; | |
| 85 } | |
| 86 | |
| 87 bool CertLoader::IsHardwareBacked() const { | |
| 88 return !tpm_token_name_.empty(); | |
| 89 } | |
| 90 | |
| 91 void CertLoader::RequestCertificates() { | |
| 92 CHECK(thread_checker_.CalledOnValidThread()); | |
| 93 VLOG(1) << "RequestCertificates: " << LoginState::Get()->IsUserLoggedIn(); | |
| 94 if (!LoginState::Get()->IsUserLoggedIn()) | |
| 95 return; // Certificates will be requested on login. | |
| 96 | |
| 97 if (!key_store_loaded_) { | |
| 98 // Ensure we've opened the real user's key/certificate database. | |
| 99 crypto::OpenPersistentNSSDB(); | |
| 100 | |
| 101 if (base::chromeos::IsRunningOnChromeOS()) | |
| 102 crypto::EnableTPMTokenForNSS(); | |
| 103 | |
| 104 key_store_loaded_ = true; | |
| 105 } | |
| 106 | |
| 107 certificates_requested_ = true; | |
| 108 | |
| 109 VLOG(1) << "Requesting Certificates."; | |
| 110 DBusThreadManager::Get()->GetCryptohomeClient()->TpmIsEnabled( | |
| 111 base::Bind(&CertLoader::OnTpmIsEnabled, | |
| 112 weak_ptr_factory_.GetWeakPtr())); | |
| 113 | |
| 114 return; | |
| 115 } | |
| 116 | |
| 117 void CertLoader::OnTpmIsEnabled(DBusMethodCallStatus call_status, | |
| 118 bool tpm_is_enabled) { | |
| 119 VLOG(1) << "OnTpmIsEnabled: " << tpm_is_enabled; | |
| 120 if (call_status != DBUS_METHOD_CALL_SUCCESS || !tpm_is_enabled) { | |
| 121 // TPM is not enabled, so proceed with empty tpm token name. | |
| 122 VLOG(1) << "TPM not available."; | |
| 123 StartLoadCertificates(); | |
| 124 } else if (tpm_token_ready_) { | |
| 125 // Once the TPM token is ready, initialize it. | |
| 126 InitializeTPMToken(); | |
| 127 } else { | |
| 128 DBusThreadManager::Get()->GetCryptohomeClient()->Pkcs11IsTpmTokenReady( | |
| 129 base::Bind(&CertLoader::OnPkcs11IsTpmTokenReady, | |
| 130 weak_ptr_factory_.GetWeakPtr())); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 void CertLoader::OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, | |
| 135 bool is_tpm_token_ready) { | |
| 136 VLOG(1) << "OnPkcs11IsTpmTokenReady: " << is_tpm_token_ready; | |
| 137 if (call_status != DBUS_METHOD_CALL_SUCCESS || !is_tpm_token_ready) { | |
| 138 MaybeRetryRequestCertificates(); | |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 // Retrieve token_name_ and user_pin_ here since they will never change | |
| 143 // and CryptohomeClient calls are not thread safe. | |
| 144 DBusThreadManager::Get()->GetCryptohomeClient()->Pkcs11GetTpmTokenInfo( | |
| 145 base::Bind(&CertLoader::OnPkcs11GetTpmTokenInfo, | |
| 146 weak_ptr_factory_.GetWeakPtr())); | |
| 147 } | |
| 148 | |
| 149 void CertLoader::OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, | |
| 150 const std::string& token_name, | |
| 151 const std::string& user_pin) { | |
| 152 VLOG(1) << "OnPkcs11GetTpmTokenInfo: " << token_name; | |
| 153 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | |
| 154 MaybeRetryRequestCertificates(); | |
| 155 return; | |
| 156 } | |
| 157 tpm_token_name_ = token_name; | |
| 158 // TODO(stevenjb): The network code expects a slot ID, not a label. See | |
| 159 // crbug.com/201101. For now, use a hard coded, well known slot instead. | |
| 160 const char kHardcodedTpmSlot[] = "0"; | |
| 161 tpm_token_slot_ = kHardcodedTpmSlot; | |
| 162 tpm_user_pin_ = user_pin; | |
| 163 tpm_token_ready_ = true; | |
| 164 | |
| 165 InitializeTPMToken(); | |
| 166 } | |
| 167 | |
| 168 void CertLoader::InitializeTPMToken() { | |
| 169 VLOG(1) << "InitializeTPMToken"; | |
| 170 if (!crypto::InitializeTPMToken(tpm_token_name_, tpm_user_pin_)) { | |
| 171 MaybeRetryRequestCertificates(); | |
| 172 return; | |
| 173 } | |
| 174 StartLoadCertificates(); | |
| 175 } | |
| 176 | |
| 177 void CertLoader::StartLoadCertificates() { | |
| 178 VLOG(1) << "Start Load Certificates"; | |
| 179 base::PostTaskAndReplyWithResult( | |
| 180 base::WorkerPool::GetTaskRunner(true /* task_is_slow */), | |
| 181 FROM_HERE, | |
| 182 base::Bind(LoadNSSCertificates), | |
| 183 base::Bind(&CertLoader::UpdateCertificates, | |
| 184 weak_ptr_factory_.GetWeakPtr())); | |
| 185 } | |
| 186 | |
| 187 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { | |
| 188 CHECK(thread_checker_.CalledOnValidThread()); | |
| 189 DCHECK(cert_list); | |
| 190 VLOG(1) << "Update Certificates: " << cert_list->size(); | |
| 191 | |
| 192 // Clear any existing certificates. | |
| 193 cert_list_.swap(*cert_list); | |
| 194 | |
| 195 // cert_list is constructed in LoadCertificates. | |
| 196 delete cert_list; | |
| 197 | |
| 198 // Set loaded state and notify observers. | |
| 199 if (!certificates_loaded_) { | |
| 200 certificates_loaded_ = true; | |
| 201 NotifyCertificatesLoaded(true); | |
| 202 } else { | |
| 203 NotifyCertificatesLoaded(false); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 void CertLoader::MaybeRetryRequestCertificates() { | |
| 208 if (!request_task_.is_null()) | |
| 209 return; | |
| 210 | |
| 211 // Cryptohome does not notify us when the token is ready, so call | |
| 212 // this again after a delay. | |
| 213 request_task_ = base::Bind(&CertLoader::RequestCertificatesTask, | |
| 214 weak_ptr_factory_.GetWeakPtr()); | |
| 215 MessageLoop::current()->PostDelayedTask( | |
| 216 FROM_HERE, | |
| 217 request_task_, | |
| 218 base::TimeDelta::FromMilliseconds(kRequestDelayMs)); | |
| 219 } | |
| 220 | |
| 221 void CertLoader::RequestCertificatesTask() { | |
| 222 // Reset the task to the initial state so is_null() returns true. | |
| 223 request_task_.Reset(); | |
| 224 RequestCertificates(); | |
| 225 } | |
| 226 | |
| 227 void CertLoader::NotifyCertificatesLoaded(bool initial_load) { | |
| 228 FOR_EACH_OBSERVER(Observer, observers_, | |
| 229 OnCertificatesLoaded(cert_list_, initial_load)); | |
| 230 } | |
| 231 | |
| 232 void CertLoader::OnCertTrustChanged(const net::X509Certificate* cert) { | |
| 233 } | |
| 234 | |
| 235 void CertLoader::OnCertAdded(const net::X509Certificate* cert) { | |
| 236 // Only load certificates if we have completed an initial request. | |
| 237 if (!certificates_loaded_) | |
| 238 return; | |
| 239 StartLoadCertificates(); | |
| 240 } | |
| 241 | |
| 242 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { | |
| 243 // Only load certificates if we have completed an initial request. | |
| 244 if (!certificates_loaded_) | |
| 245 return; | |
| 246 StartLoadCertificates(); | |
| 247 } | |
| 248 | |
| 249 void CertLoader::LoggedInStateChanged(LoginState::LoggedInState state) { | |
| 250 VLOG(1) << "LoggedInStateChanged: " << state; | |
| 251 if (LoginState::Get()->IsUserLoggedIn() && !certificates_requested_) | |
| 252 RequestCertificates(); | |
| 253 } | |
| 254 | |
| 255 } // namespace chromeos | |
| OLD | NEW |