Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "chrome/browser/chromeos/login/easy_unlock/easy_unlock_tpm_key_manager. h" | |
| 6 | |
| 7 #include <cryptohi.h> | |
| 8 | |
| 9 #include "base/base64.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "base/location.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/prefs/pref_registry_simple.h" | |
| 15 #include "base/prefs/pref_service.h" | |
| 16 #include "base/prefs/scoped_user_pref_update.h" | |
| 17 #include "base/single_thread_task_runner.h" | |
| 18 #include "base/thread_task_runner_handle.h" | |
| 19 #include "base/threading/worker_pool.h" | |
| 20 #include "base/time/time.h" | |
| 21 #include "base/values.h" | |
| 22 #include "chrome/browser/browser_process.h" | |
| 23 #include "chrome/common/pref_names.h" | |
| 24 #include "content/public/browser/browser_thread.h" | |
| 25 #include "crypto/nss_util_internal.h" | |
| 26 #include "crypto/rsa_private_key.h" | |
| 27 #include "crypto/scoped_nss_types.h" | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // The modulus length for RSA keys used by easy sign-in. | |
| 32 const int kKeyModulusLength = 2048; | |
| 33 | |
| 34 // Relays |GetSystemSlotOnIOThread| callback to |response_task_runner|. | |
| 35 void RunCallbackOnThreadRunner( | |
| 36 const scoped_refptr<base::SingleThreadTaskRunner>& response_task_runner, | |
| 37 const base::Callback<void(crypto::ScopedPK11Slot)>& callback, | |
| 38 crypto::ScopedPK11Slot slot) { | |
| 39 response_task_runner->PostTask(FROM_HERE, | |
| 40 base::Bind(callback, base::Passed(&slot))); | |
| 41 } | |
| 42 | |
| 43 // Gets TPM system slot. Must be called on IO thread. | |
| 44 // The callback wil be relayed to |response_task_runner|. | |
| 45 void GetSystemSlotOnIOThread( | |
| 46 const scoped_refptr<base::SingleThreadTaskRunner>& response_task_runner, | |
| 47 const base::Callback<void(crypto::ScopedPK11Slot)>& callback) { | |
| 48 base::Callback<void(crypto::ScopedPK11Slot)> callback_on_origin_thread = | |
| 49 base::Bind(&RunCallbackOnThreadRunner, response_task_runner, callback); | |
| 50 | |
| 51 crypto::ScopedPK11Slot system_slot = | |
| 52 crypto::GetSystemNSSKeySlot(callback_on_origin_thread); | |
| 53 if (system_slot) | |
| 54 callback_on_origin_thread.Run(system_slot.Pass()); | |
| 55 } | |
| 56 | |
| 57 // Checks if a private RSA key associated with |public_key| can be found in | |
| 58 // |slot|. | |
| 59 // Must be called on a worker thread. | |
| 60 scoped_ptr<crypto::RSAPrivateKey> GetPrivateKeyOnWorkerThread( | |
| 61 PK11SlotInfo* slot, | |
| 62 const std::string& public_key) { | |
| 63 const uint8* public_key_uint8 = | |
| 64 reinterpret_cast<const uint8*>(public_key.data()); | |
| 65 std::vector<uint8> public_key_vector( | |
| 66 public_key_uint8, public_key_uint8 + public_key.size()); | |
| 67 | |
| 68 scoped_ptr<crypto::RSAPrivateKey> rsa_key( | |
| 69 crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key_vector)); | |
| 70 if (!rsa_key || rsa_key->key()->pkcs11Slot != slot) | |
| 71 return scoped_ptr<crypto::RSAPrivateKey>(); | |
| 72 return rsa_key.Pass(); | |
| 73 } | |
| 74 | |
| 75 // Signs |data| using a private key associated with |public_key| and stored in | |
| 76 // |slot|. Once the data is signed, callback is run on |response_task_runner|. | |
| 77 // In case of an error, the callback will be passed an empty string. | |
| 78 void SignDataOnWorkerThread( | |
| 79 crypto::ScopedPK11Slot slot, | |
| 80 const std::string& public_key, | |
| 81 const std::string& data, | |
| 82 const scoped_refptr<base::SingleThreadTaskRunner>& response_task_runner, | |
| 83 const base::Callback<void(const std::string&)>& callback) { | |
| 84 scoped_ptr<crypto::RSAPrivateKey> private_key( | |
| 85 GetPrivateKeyOnWorkerThread(slot.get(), public_key)); | |
| 86 if (!private_key) { | |
| 87 LOG(ERROR) << "Private key for signing data not found"; | |
| 88 response_task_runner->PostTask(FROM_HERE, | |
| 89 base::Bind(callback, std::string())); | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 SECItem sign_result = {siBuffer, NULL, 0}; | |
| 94 if (SEC_SignData(&sign_result, | |
| 95 reinterpret_cast<const unsigned char*>(data.data()), | |
| 96 data.size(), | |
| 97 private_key->key(), | |
| 98 SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION) != SECSuccess) { | |
| 99 LOG(ERROR) << "Failed to sign data"; | |
| 100 response_task_runner->PostTask(FROM_HERE, | |
| 101 base::Bind(callback, std::string())); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 std::string signature(reinterpret_cast<const char*>(sign_result.data), | |
| 106 sign_result.len); | |
| 107 response_task_runner->PostTask(FROM_HERE, base::Bind(callback, signature)); | |
| 108 } | |
| 109 | |
| 110 // Creates a RSA key pair in |slot|. When done, it runs |callback| with the | |
| 111 // created public key on |response_task_runner|. | |
| 112 // If |public_key| is not empty, a key pair will be created only if the private | |
| 113 // key associated with |public_key| does not exist in |slot|. Otherwise the | |
| 114 // callback will be run with |public_key|. | |
| 115 void CreateTpmKeyPairOnWorkerThread( | |
| 116 crypto::ScopedPK11Slot slot, | |
| 117 const std::string& public_key, | |
| 118 const scoped_refptr<base::SingleThreadTaskRunner>& response_task_runner, | |
| 119 const base::Callback<void(const std::string&)>& callback) { | |
| 120 if (!public_key.empty() && | |
| 121 GetPrivateKeyOnWorkerThread(slot.get(), public_key)) { | |
| 122 response_task_runner->PostTask(FROM_HERE, base::Bind(callback, public_key)); | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 scoped_ptr<crypto::RSAPrivateKey> rsa_key( | |
| 127 crypto::RSAPrivateKey::CreateSensitive(slot.get(), kKeyModulusLength)); | |
| 128 if (!rsa_key) { | |
| 129 LOG(ERROR) << "Failed to create an RSA key."; | |
| 130 response_task_runner->PostTask(FROM_HERE, | |
| 131 base::Bind(callback, std::string())); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 std::vector<uint8> created_public_key; | |
| 136 if (!rsa_key->ExportPublicKey(&created_public_key)) { | |
| 137 LOG(ERROR) << "Failed to export public key."; | |
| 138 response_task_runner->PostTask(FROM_HERE, | |
| 139 base::Bind(callback, std::string())); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 response_task_runner->PostTask( | |
| 144 FROM_HERE, | |
| 145 base::Bind(callback, | |
| 146 std::string(created_public_key.begin(), | |
| 147 created_public_key.end()))); | |
| 148 } | |
| 149 | |
| 150 } // namespace | |
| 151 | |
| 152 // static | |
| 153 void EasyUnlockTpmKeyManager::RegisterLocalStatePrefs( | |
| 154 PrefRegistrySimple* registry) { | |
| 155 registry->RegisterDictionaryPref(prefs::kEasyUnlockLocalStateTpmKeys); | |
| 156 } | |
| 157 | |
| 158 // static | |
| 159 void EasyUnlockTpmKeyManager::ResetLocalStateForUser( | |
| 160 const std::string& user_id) { | |
| 161 if (!g_browser_process) | |
| 162 return; | |
| 163 PrefService* local_state = g_browser_process->local_state(); | |
| 164 if (!local_state) | |
| 165 return; | |
| 166 | |
| 167 DictionaryPrefUpdate update(local_state, prefs::kEasyUnlockLocalStateTpmKeys); | |
| 168 update->RemoveWithoutPathExpansion(user_id, NULL); | |
| 169 } | |
| 170 | |
| 171 EasyUnlockTpmKeyManager::EasyUnlockTpmKeyManager(const std::string& user_id, | |
| 172 PrefService* local_state) | |
| 173 : user_id_(user_id), | |
| 174 local_state_(local_state), | |
| 175 creating_tpm_key_pair_(false), | |
| 176 got_tpm_slot_(false), | |
| 177 get_tpm_slot_weak_ptr_factory_(this), | |
| 178 weak_ptr_factory_(this) { | |
| 179 } | |
| 180 | |
| 181 EasyUnlockTpmKeyManager::~EasyUnlockTpmKeyManager() { | |
| 182 } | |
| 183 | |
| 184 bool EasyUnlockTpmKeyManager::PrepareTpmKey( | |
| 185 const std::string& user_id, | |
| 186 bool check_private_key, | |
| 187 const base::Closure& callback) { | |
| 188 CHECK(!user_id_.empty()); | |
| 189 CHECK_EQ(user_id_, user_id); | |
| 190 | |
| 191 std::string key = GetPublicTpmKey(user_id); | |
| 192 if (!check_private_key && !creating_tpm_key_pair_ && !key.empty()) | |
| 193 return true; | |
| 194 | |
| 195 tpm_key_present_callbacks_.push_back(callback); | |
| 196 if (!creating_tpm_key_pair_) { | |
| 197 creating_tpm_key_pair_ = true; | |
| 198 | |
| 199 base::Callback<void(crypto::ScopedPK11Slot)> create_key_with_system_slot = | |
| 200 base::Bind(&EasyUnlockTpmKeyManager::CreateKeyInSystemSlot, | |
| 201 get_tpm_slot_weak_ptr_factory_.GetWeakPtr(), | |
| 202 user_id, | |
| 203 key); | |
| 204 | |
| 205 content::BrowserThread::PostTask( | |
| 206 content::BrowserThread::IO, | |
| 207 FROM_HERE, | |
| 208 base::Bind(&GetSystemSlotOnIOThread, | |
| 209 base::ThreadTaskRunnerHandle::Get(), | |
| 210 create_key_with_system_slot)); | |
| 211 } | |
| 212 | |
| 213 return false; | |
| 214 } | |
| 215 | |
| 216 bool EasyUnlockTpmKeyManager::SetGetSystemSlotTimeoutMs(size_t timeout_ms) { | |
|
pneubeck (no reviews)
2014/12/05 12:16:04
SetTimeout sounds like you would set the timeout d
pneubeck (no reviews)
2014/12/05 12:16:04
it seems to be invalid/uneffective if this is call
tbarzic
2014/12/11 23:43:27
Done.
tbarzic
2014/12/11 23:43:28
Hm, it should still be effective if it's called ju
| |
| 217 if (got_tpm_slot_) | |
| 218 return false; | |
| 219 | |
| 220 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 221 FROM_HERE, | |
| 222 base::Bind(&EasyUnlockTpmKeyManager::OnTpmKeyCreated, | |
| 223 get_tpm_slot_weak_ptr_factory_.GetWeakPtr(), | |
| 224 user_id_, | |
| 225 std::string()), | |
| 226 base::TimeDelta::FromMilliseconds(timeout_ms)); | |
| 227 return true; | |
| 228 } | |
| 229 | |
| 230 std::string EasyUnlockTpmKeyManager::GetPublicTpmKey( | |
| 231 const std::string& user_id) { | |
| 232 if (!local_state_) | |
| 233 return std::string(); | |
| 234 const base::DictionaryValue* dict = | |
| 235 local_state_->GetDictionary(prefs::kEasyUnlockLocalStateTpmKeys); | |
| 236 std::string key; | |
| 237 if (dict) | |
| 238 dict->GetStringWithoutPathExpansion(user_id, &key); | |
| 239 std::string decoded; | |
| 240 base::Base64Decode(key, &decoded); | |
| 241 return decoded; | |
| 242 } | |
| 243 | |
| 244 void EasyUnlockTpmKeyManager::SignUsingTpmKey( | |
| 245 const std::string& user_id, | |
| 246 const std::string& data, | |
| 247 const base::Callback<void(const std::string& data)> callback) { | |
| 248 std::string key = GetPublicTpmKey(user_id); | |
| 249 if (key.empty()) { | |
| 250 callback.Run(std::string()); | |
| 251 return; | |
| 252 } | |
| 253 | |
| 254 base::Callback<void(crypto::ScopedPK11Slot)> sign_with_system_slot = | |
| 255 base::Bind(&EasyUnlockTpmKeyManager::SignDataWithSystemSlot, | |
| 256 weak_ptr_factory_.GetWeakPtr(), | |
| 257 key, data, callback); | |
| 258 | |
| 259 content::BrowserThread::PostTask( | |
| 260 content::BrowserThread::IO, | |
| 261 FROM_HERE, | |
| 262 base::Bind(&GetSystemSlotOnIOThread, | |
| 263 base::ThreadTaskRunnerHandle::Get(), | |
| 264 sign_with_system_slot)); | |
| 265 } | |
| 266 | |
| 267 void EasyUnlockTpmKeyManager::SetKeyInLocalState(const std::string& user_id, | |
| 268 const std::string& value) { | |
| 269 if (!local_state_) | |
| 270 return; | |
| 271 | |
| 272 std::string encoded; | |
| 273 base::Base64Encode(value, &encoded); | |
| 274 DictionaryPrefUpdate update(local_state_, | |
| 275 prefs::kEasyUnlockLocalStateTpmKeys); | |
| 276 update->SetStringWithoutPathExpansion(user_id, encoded); | |
| 277 } | |
| 278 | |
| 279 void EasyUnlockTpmKeyManager::CreateKeyInSystemSlot( | |
| 280 const std::string& user_id, | |
| 281 const std::string& public_key, | |
| 282 crypto::ScopedPK11Slot system_slot) { | |
| 283 CHECK(system_slot); | |
| 284 | |
| 285 got_tpm_slot_ = true; | |
| 286 get_tpm_slot_weak_ptr_factory_.InvalidateWeakPtrs(); | |
|
pneubeck (no reviews)
2014/12/05 12:16:04
probably document what effect this should have. Cu
tbarzic
2014/12/11 23:43:27
Done.
| |
| 287 | |
| 288 base::WorkerPool::PostTask( | |
| 289 FROM_HERE, | |
| 290 base::Bind(&CreateTpmKeyPairOnWorkerThread, | |
|
pneubeck (no reviews)
2014/12/05 12:16:04
what if this hangs? why does the timeout not skip
tbarzic
2014/12/11 23:43:27
My main concern is with GetSystemSlot hanging inde
| |
| 291 base::Passed(&system_slot), | |
| 292 public_key, | |
| 293 base::ThreadTaskRunnerHandle::Get(), | |
| 294 base::Bind(&EasyUnlockTpmKeyManager::OnTpmKeyCreated, | |
| 295 weak_ptr_factory_.GetWeakPtr(), | |
| 296 user_id)), | |
| 297 true /* long task */); | |
| 298 } | |
| 299 | |
| 300 void EasyUnlockTpmKeyManager::SignDataWithSystemSlot( | |
| 301 const std::string& public_key, | |
| 302 const std::string& data, | |
| 303 const base::Callback<void(const std::string& data)> callback, | |
| 304 crypto::ScopedPK11Slot system_slot) { | |
| 305 CHECK(system_slot); | |
| 306 | |
| 307 base::WorkerPool::PostTask( | |
| 308 FROM_HERE, | |
| 309 base::Bind(&SignDataOnWorkerThread, | |
| 310 base::Passed(&system_slot), | |
| 311 public_key, | |
| 312 data, | |
| 313 base::ThreadTaskRunnerHandle::Get(), | |
| 314 base::Bind(&EasyUnlockTpmKeyManager::OnDataSigned, | |
| 315 weak_ptr_factory_.GetWeakPtr(), | |
| 316 callback)), | |
| 317 true /* long task */); | |
| 318 } | |
| 319 | |
| 320 void EasyUnlockTpmKeyManager::OnTpmKeyCreated( | |
| 321 const std::string& user_id, | |
| 322 const std::string& public_key) { | |
| 323 creating_tpm_key_pair_ = false; | |
| 324 | |
| 325 get_tpm_slot_weak_ptr_factory_.InvalidateWeakPtrs(); | |
|
pneubeck (no reviews)
2014/12/05 12:16:04
same comment as in line 286
tbarzic
2014/12/11 23:43:27
Done.
| |
| 326 | |
| 327 if (!public_key.empty()) | |
| 328 SetKeyInLocalState(user_id, public_key); | |
| 329 | |
| 330 for (size_t i = 0; i < tpm_key_present_callbacks_.size(); ++i) { | |
| 331 if (!tpm_key_present_callbacks_[i].is_null()) | |
| 332 tpm_key_present_callbacks_[i].Run(); | |
| 333 } | |
| 334 | |
| 335 tpm_key_present_callbacks_.clear(); | |
| 336 } | |
| 337 | |
| 338 void EasyUnlockTpmKeyManager::OnDataSigned( | |
| 339 const base::Callback<void(const std::string&)>& callback, | |
| 340 const std::string& signature) { | |
| 341 callback.Run(signature); | |
| 342 } | |
| OLD | NEW |