summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/easy_unlock/easy_unlock_tpm_key_manager.cc
blob: 3321ed9a336e55cc9240c61ff3b2ba9d7e67ee62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_tpm_key_manager.h"

#include <cryptohi.h>

#include "base/base64.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/worker_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "crypto/nss_util_internal.h"
#include "crypto/rsa_private_key.h"
#include "crypto/scoped_nss_types.h"

namespace {

// The modulus length for RSA keys used by easy sign-in.
const int kKeyModulusLength = 2048;

// Relays |GetSystemSlotOnIOThread| callback to |response_task_runner|.
void RunCallbackOnThreadRunner(
    const scoped_refptr<base::SingleThreadTaskRunner>& response_task_runner,
    const base::Callback<void(crypto::ScopedPK11Slot)>& callback,
    crypto::ScopedPK11Slot slot) {
  response_task_runner->PostTask(FROM_HERE,
                                 base::Bind(callback, base::Passed(&slot)));
}

// Gets TPM system slot. Must be called on IO thread.
// The callback wil be relayed to |response_task_runner|.
void GetSystemSlotOnIOThread(
    const scoped_refptr<base::SingleThreadTaskRunner>& response_task_runner,
    const base::Callback<void(crypto::ScopedPK11Slot)>& callback) {
  base::Callback<void(crypto::ScopedPK11Slot)> callback_on_origin_thread =
      base::Bind(&RunCallbackOnThreadRunner, response_task_runner, callback);

  crypto::ScopedPK11Slot system_slot =
      crypto::GetSystemNSSKeySlot(callback_on_origin_thread);
  if (system_slot)
    callback_on_origin_thread.Run(system_slot.Pass());
}

// Checks if a private RSA key associated with |public_key| can be found in
// |slot|.
// Must be called on a worker thread.
scoped_ptr<crypto::RSAPrivateKey> GetPrivateKeyOnWorkerThread(
    PK11SlotInfo* slot,
    const std::string& public_key) {
  const uint8* public_key_uint8 =
      reinterpret_cast<const uint8*>(public_key.data());
  std::vector<uint8> public_key_vector(
      public_key_uint8, public_key_uint8 + public_key.size());

  scoped_ptr<crypto::RSAPrivateKey> rsa_key(
      crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key_vector));
  if (!rsa_key || rsa_key->key()->pkcs11Slot != slot)
    return scoped_ptr<crypto::RSAPrivateKey>();
  return rsa_key.Pass();
}

// Signs |data| using a private key associated with |public_key| and stored in
// |slot|. Once the data is signed, callback is run on |response_task_runner|.
// In case of an error, the callback will be passed an empty string.
void SignDataOnWorkerThread(
    crypto::ScopedPK11Slot slot,
    const std::string& public_key,
    const std::string& data,
    const scoped_refptr<base::SingleThreadTaskRunner>& response_task_runner,
    const base::Callback<void(const std::string&)>& callback) {
  scoped_ptr<crypto::RSAPrivateKey> private_key(
      GetPrivateKeyOnWorkerThread(slot.get(), public_key));
  if (!private_key) {
    LOG(ERROR) << "Private key for signing data not found";
    response_task_runner->PostTask(FROM_HERE,
                                   base::Bind(callback, std::string()));
    return;
  }

  crypto::ScopedSECItem sign_result(SECITEM_AllocItem(NULL, NULL, 0));
  if (SEC_SignData(sign_result.get(),
                   reinterpret_cast<const unsigned char*>(data.data()),
                   data.size(),
                   private_key->key(),
                   SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION) != SECSuccess) {
    LOG(ERROR) << "Failed to sign data";
    response_task_runner->PostTask(FROM_HERE,
                                   base::Bind(callback, std::string()));
    return;
  }

  std::string signature(reinterpret_cast<const char*>(sign_result->data),
                        sign_result->len);
  response_task_runner->PostTask(FROM_HERE, base::Bind(callback, signature));
}

// Creates a RSA key pair in |slot|. When done, it runs |callback| with the
// created public key on |response_task_runner|.
// If |public_key| is not empty, a key pair will be created only if the private
// key associated with |public_key| does not exist in |slot|. Otherwise the
// callback will be run with |public_key|.
void CreateTpmKeyPairOnWorkerThread(
    crypto::ScopedPK11Slot slot,
    const std::string& public_key,
    const scoped_refptr<base::SingleThreadTaskRunner>& response_task_runner,
    const base::Callback<void(const std::string&)>& callback) {
  if (!public_key.empty() &&
      GetPrivateKeyOnWorkerThread(slot.get(), public_key)) {
    response_task_runner->PostTask(FROM_HERE, base::Bind(callback, public_key));
    return;
  }

  scoped_ptr<crypto::RSAPrivateKey> rsa_key(
      crypto::RSAPrivateKey::CreateSensitive(slot.get(), kKeyModulusLength));
  if (!rsa_key) {
    LOG(ERROR) << "Failed to create an RSA key.";
    response_task_runner->PostTask(FROM_HERE,
                                   base::Bind(callback, std::string()));
    return;
  }

  std::vector<uint8> created_public_key;
  if (!rsa_key->ExportPublicKey(&created_public_key)) {
    LOG(ERROR) << "Failed to export public key.";
    response_task_runner->PostTask(FROM_HERE,
                                   base::Bind(callback, std::string()));
    return;
  }

  response_task_runner->PostTask(
      FROM_HERE,
      base::Bind(callback,
                 std::string(created_public_key.begin(),
                             created_public_key.end())));
}

}  // namespace

// static
void EasyUnlockTpmKeyManager::RegisterLocalStatePrefs(
    PrefRegistrySimple* registry) {
  registry->RegisterDictionaryPref(prefs::kEasyUnlockLocalStateTpmKeys);
}

// static
void EasyUnlockTpmKeyManager::ResetLocalStateForUser(
    const std::string& user_id) {
  if (!g_browser_process)
    return;
  PrefService* local_state = g_browser_process->local_state();
  if (!local_state)
    return;

  DictionaryPrefUpdate update(local_state, prefs::kEasyUnlockLocalStateTpmKeys);
  update->RemoveWithoutPathExpansion(user_id, NULL);
}

EasyUnlockTpmKeyManager::EasyUnlockTpmKeyManager(const std::string& user_id,
                                                 PrefService* local_state)
    : user_id_(user_id),
      local_state_(local_state),
      create_tpm_key_state_(CREATE_TPM_KEY_NOT_STARTED),
      get_tpm_slot_weak_ptr_factory_(this),
      weak_ptr_factory_(this) {
}

EasyUnlockTpmKeyManager::~EasyUnlockTpmKeyManager() {
}

bool EasyUnlockTpmKeyManager::PrepareTpmKey(
    bool check_private_key,
    const base::Closure& callback) {
  CHECK(!user_id_.empty());

  if (create_tpm_key_state_ == CREATE_TPM_KEY_DONE)
    return true;

  std::string key = GetPublicTpmKey(user_id_);
  if (!check_private_key && !key.empty() &&
      create_tpm_key_state_ == CREATE_TPM_KEY_NOT_STARTED) {
    return true;
  }

  prepare_tpm_key_callbacks_.push_back(callback);

  if (create_tpm_key_state_ == CREATE_TPM_KEY_NOT_STARTED) {
    create_tpm_key_state_ = CREATE_TPM_KEY_WAITING_FOR_SYSTEM_SLOT;

    base::Callback<void(crypto::ScopedPK11Slot)> create_key_with_system_slot =
        base::Bind(&EasyUnlockTpmKeyManager::CreateKeyInSystemSlot,
                   get_tpm_slot_weak_ptr_factory_.GetWeakPtr(),
                   key);

    content::BrowserThread::PostTask(
        content::BrowserThread::IO,
        FROM_HERE,
        base::Bind(&GetSystemSlotOnIOThread,
                   base::ThreadTaskRunnerHandle::Get(),
                   create_key_with_system_slot));
  }

  return false;
}

bool EasyUnlockTpmKeyManager::StartGetSystemSlotTimeoutMs(size_t timeout_ms) {
  if (create_tpm_key_state_ == CREATE_TPM_KEY_DONE ||
      create_tpm_key_state_ == CREATE_TPM_KEY_GOT_SYSTEM_SLOT) {
    return false;
  }

  base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&EasyUnlockTpmKeyManager::OnTpmKeyCreated,
                 get_tpm_slot_weak_ptr_factory_.GetWeakPtr(),
                 std::string()),
      base::TimeDelta::FromMilliseconds(timeout_ms));
  return true;
}

std::string EasyUnlockTpmKeyManager::GetPublicTpmKey(
    const std::string& user_id) {
  if (!local_state_)
    return std::string();
  const base::DictionaryValue* dict =
      local_state_->GetDictionary(prefs::kEasyUnlockLocalStateTpmKeys);
  std::string key;
  if (dict)
    dict->GetStringWithoutPathExpansion(user_id, &key);
  std::string decoded;
  base::Base64Decode(key, &decoded);
  return decoded;
}

void EasyUnlockTpmKeyManager::SignUsingTpmKey(
    const std::string& user_id,
    const std::string& data,
    const base::Callback<void(const std::string& data)> callback) {
  std::string key = GetPublicTpmKey(user_id);
  if (key.empty()) {
    callback.Run(std::string());
    return;
  }

  base::Callback<void(crypto::ScopedPK11Slot)> sign_with_system_slot =
      base::Bind(&EasyUnlockTpmKeyManager::SignDataWithSystemSlot,
                 weak_ptr_factory_.GetWeakPtr(),
                 key, data, callback);

  content::BrowserThread::PostTask(
      content::BrowserThread::IO,
      FROM_HERE,
      base::Bind(&GetSystemSlotOnIOThread,
                 base::ThreadTaskRunnerHandle::Get(),
                 sign_with_system_slot));
}

void EasyUnlockTpmKeyManager::SetKeyInLocalState(const std::string& user_id,
                                                 const std::string& value) {
  if (!local_state_)
    return;

  std::string encoded;
  base::Base64Encode(value, &encoded);
  DictionaryPrefUpdate update(local_state_,
                              prefs::kEasyUnlockLocalStateTpmKeys);
  update->SetStringWithoutPathExpansion(user_id, encoded);
}

void EasyUnlockTpmKeyManager::CreateKeyInSystemSlot(
    const std::string& public_key,
    crypto::ScopedPK11Slot system_slot) {
  CHECK(system_slot);

  create_tpm_key_state_ = CREATE_TPM_KEY_GOT_SYSTEM_SLOT;

  // If there are any delayed tasks posted using |StartGetSystemSlotTimeoutMs|,
  // this will cancel them.
  // Note that this would cancel other pending |CreateKeyInSystemSlot| tasks,
  // but there should be at most one such task at a time.
  get_tpm_slot_weak_ptr_factory_.InvalidateWeakPtrs();

  base::WorkerPool::PostTask(
      FROM_HERE,
      base::Bind(&CreateTpmKeyPairOnWorkerThread,
                 base::Passed(&system_slot),
                 public_key,
                 base::ThreadTaskRunnerHandle::Get(),
                 base::Bind(&EasyUnlockTpmKeyManager::OnTpmKeyCreated,
                            weak_ptr_factory_.GetWeakPtr())),
      true /* long task */);
}

void EasyUnlockTpmKeyManager::SignDataWithSystemSlot(
    const std::string& public_key,
    const std::string& data,
    const base::Callback<void(const std::string& data)> callback,
    crypto::ScopedPK11Slot system_slot) {
  CHECK(system_slot);

  base::WorkerPool::PostTask(
      FROM_HERE,
      base::Bind(&SignDataOnWorkerThread,
                 base::Passed(&system_slot),
                 public_key,
                 data,
                 base::ThreadTaskRunnerHandle::Get(),
                 base::Bind(&EasyUnlockTpmKeyManager::OnDataSigned,
                            weak_ptr_factory_.GetWeakPtr(),
                            callback)),
      true /* long task */);
}

void EasyUnlockTpmKeyManager::OnTpmKeyCreated(const std::string& public_key) {
  // |OnTpmKeyCreated| is called by a timeout task posted by
  // |StartGetSystemSlotTimeoutMs|. Invalidating the factory will have
  // an effect of canceling any pending |GetSystemSlotOnIOThread| callbacks,
  // as well as other pending timeouts.
  // Note that in the case |OnTpmKeyCreated| was called as a result of
  // |CreateKeyInSystemSlot|, this should have no effect as no weak ptrs from
  // this factory should be in use in this case.
  get_tpm_slot_weak_ptr_factory_.InvalidateWeakPtrs();

  if (!public_key.empty())
    SetKeyInLocalState(user_id_, public_key);

  for (size_t i = 0; i < prepare_tpm_key_callbacks_.size(); ++i) {
    if (!prepare_tpm_key_callbacks_[i].is_null())
      prepare_tpm_key_callbacks_[i].Run();
  }

  prepare_tpm_key_callbacks_.clear();

  // If key creation failed, reset the state machine.
  create_tpm_key_state_ =
      public_key.empty() ? CREATE_TPM_KEY_NOT_STARTED : CREATE_TPM_KEY_DONE;
}

void EasyUnlockTpmKeyManager::OnDataSigned(
    const base::Callback<void(const std::string&)>& callback,
    const std::string& signature) {
  callback.Run(signature);
}