OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "crypto/nss_util.h" | 5 #include "crypto/nss_util.h" |
6 | 6 |
7 #include <nss.h> | 7 #include <nss.h> |
8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
9 #include <plarena.h> | 9 #include <plarena.h> |
10 #include <prerror.h> | 10 #include <prerror.h> |
11 #include <prinit.h> | 11 #include <prinit.h> |
12 #include <prtime.h> | 12 #include <prtime.h> |
13 #include <secmod.h> | 13 #include <secmod.h> |
14 | 14 |
15 #include <memory> | 15 #include <memory> |
16 #include <utility> | 16 #include <utility> |
17 | 17 |
18 #include "base/location.h" | 18 #include "base/location.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "base/task_scheduler/post_task.h" | |
20 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
21 #include "crypto/nss_util_internal.h" | 22 #include "crypto/nss_util_internal.h" |
22 | 23 |
23 #if defined(OS_OPENBSD) | 24 #if defined(OS_OPENBSD) |
24 #include <sys/mount.h> | 25 #include <sys/mount.h> |
25 #include <sys/param.h> | 26 #include <sys/param.h> |
26 #endif | 27 #endif |
27 | 28 |
28 #if defined(OS_CHROMEOS) | 29 #if defined(OS_CHROMEOS) |
29 #include <dlfcn.h> | 30 #include <dlfcn.h> |
(...skipping 12 matching lines...) Expand all Loading... | |
42 #include "base/files/file_util.h" | 43 #include "base/files/file_util.h" |
43 #include "base/lazy_instance.h" | 44 #include "base/lazy_instance.h" |
44 #include "base/logging.h" | 45 #include "base/logging.h" |
45 #include "base/memory/ptr_util.h" | 46 #include "base/memory/ptr_util.h" |
46 #include "base/native_library.h" | 47 #include "base/native_library.h" |
47 #include "base/path_service.h" | 48 #include "base/path_service.h" |
48 #include "base/strings/stringprintf.h" | 49 #include "base/strings/stringprintf.h" |
49 #include "base/synchronization/lock.h" | 50 #include "base/synchronization/lock.h" |
50 #include "base/threading/thread_checker.h" | 51 #include "base/threading/thread_checker.h" |
51 #include "base/threading/thread_restrictions.h" | 52 #include "base/threading/thread_restrictions.h" |
52 #include "base/threading/worker_pool.h" | |
53 #include "build/build_config.h" | 53 #include "build/build_config.h" |
54 #include "crypto/nss_crypto_module_delegate.h" | 54 #include "crypto/nss_crypto_module_delegate.h" |
55 | 55 |
56 namespace crypto { | 56 namespace crypto { |
57 | 57 |
58 namespace { | 58 namespace { |
59 | 59 |
60 #if defined(OS_CHROMEOS) | 60 #if defined(OS_CHROMEOS) |
61 const char kUserNSSDatabaseName[] = "UserNSSDB"; | 61 const char kUserNSSDatabaseName[] = "UserNSSDB"; |
62 | 62 |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
336 DCHECK(thread_checker_.CalledOnValidThread()); | 336 DCHECK(thread_checker_.CalledOnValidThread()); |
337 return tpm_token_enabled_for_nss_; | 337 return tpm_token_enabled_for_nss_; |
338 } | 338 } |
339 | 339 |
340 void InitializeTPMTokenAndSystemSlot( | 340 void InitializeTPMTokenAndSystemSlot( |
341 int system_slot_id, | 341 int system_slot_id, |
342 const base::Callback<void(bool)>& callback) { | 342 const base::Callback<void(bool)>& callback) { |
343 DCHECK(thread_checker_.CalledOnValidThread()); | 343 DCHECK(thread_checker_.CalledOnValidThread()); |
344 // Should not be called while there is already an initialization in | 344 // Should not be called while there is already an initialization in |
345 // progress. | 345 // progress. |
346 DCHECK(!initializing_tpm_token_); | 346 DCHECK(!initializing_tpm_token_); |
gab
2017/01/12 15:44:50
According to |initializing_tpm_token_| I'm led to
| |
347 // If EnableTPMTokenForNSS hasn't been called, return false. | 347 // If EnableTPMTokenForNSS hasn't been called, return false. |
348 if (!tpm_token_enabled_for_nss_) { | 348 if (!tpm_token_enabled_for_nss_) { |
349 base::ThreadTaskRunnerHandle::Get()->PostTask( | 349 base::ThreadTaskRunnerHandle::Get()->PostTask( |
350 FROM_HERE, base::Bind(callback, false)); | 350 FROM_HERE, base::Bind(callback, false)); |
351 return; | 351 return; |
352 } | 352 } |
353 | 353 |
354 // If everything is already initialized, then return true. | 354 // If everything is already initialized, then return true. |
355 // Note that only |tpm_slot_| is checked, since |chaps_module_| could be | 355 // Note that only |tpm_slot_| is checked, since |chaps_module_| could be |
356 // nullptr in tests while |tpm_slot_| has been set to the test DB. | 356 // nullptr in tests while |tpm_slot_| has been set to the test DB. |
357 if (tpm_slot_) { | 357 if (tpm_slot_) { |
358 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 358 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
359 base::Bind(callback, true)); | 359 base::Bind(callback, true)); |
360 return; | 360 return; |
361 } | 361 } |
362 | 362 |
363 // Note that a reference is not taken to chaps_module_. This is safe since | 363 // Note that a reference is not taken to chaps_module_. This is safe since |
364 // NSSInitSingleton is Leaky, so the reference it holds is never released. | 364 // NSSInitSingleton is Leaky, so the reference it holds is never released. |
365 std::unique_ptr<TPMModuleAndSlot> tpm_args( | 365 std::unique_ptr<TPMModuleAndSlot> tpm_args( |
366 new TPMModuleAndSlot(chaps_module_)); | 366 new TPMModuleAndSlot(chaps_module_)); |
367 TPMModuleAndSlot* tpm_args_ptr = tpm_args.get(); | 367 TPMModuleAndSlot* tpm_args_ptr = tpm_args.get(); |
368 if (base::WorkerPool::PostTaskAndReply( | 368 base::PostTaskWithTraitsAndReply( |
369 FROM_HERE, | 369 FROM_HERE, base::TaskTraits() |
370 base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread, | 370 .WithShutdownBehavior( |
371 system_slot_id, | 371 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
372 tpm_args_ptr), | 372 .MayBlock(), |
373 base::Bind(&NSSInitSingleton::OnInitializedTPMTokenAndSystemSlot, | 373 base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread, |
374 base::Unretained(this), // NSSInitSingleton is leaky | 374 system_slot_id, tpm_args_ptr), |
375 callback, | 375 base::Bind(&NSSInitSingleton::OnInitializedTPMTokenAndSystemSlot, |
376 base::Passed(&tpm_args)), | 376 base::Unretained(this), // NSSInitSingleton is leaky |
377 true /* task_is_slow */ | 377 callback, base::Passed(&tpm_args))); |
378 )) { | 378 initializing_tpm_token_ = true; |
379 initializing_tpm_token_ = true; | |
380 } else { | |
381 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
382 FROM_HERE, base::Bind(callback, false)); | |
383 } | |
384 } | 379 } |
385 | 380 |
386 static void InitializeTPMTokenOnWorkerThread(CK_SLOT_ID token_slot_id, | 381 static void InitializeTPMTokenOnWorkerThread(CK_SLOT_ID token_slot_id, |
387 TPMModuleAndSlot* tpm_args) { | 382 TPMModuleAndSlot* tpm_args) { |
388 // This tries to load the Chaps module so NSS can talk to the hardware | 383 // This tries to load the Chaps module so NSS can talk to the hardware |
389 // TPM. | 384 // TPM. |
390 if (!tpm_args->chaps_module) { | 385 if (!tpm_args->chaps_module) { |
391 ScopedChapsLoadFixup chaps_loader; | 386 ScopedChapsLoadFixup chaps_loader; |
392 | 387 |
393 DVLOG(3) << "Loading chaps..."; | 388 DVLOG(3) << "Loading chaps..."; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
521 private_slot_initialization_started()); | 516 private_slot_initialization_started()); |
522 | 517 |
523 if (!chaps_module_) | 518 if (!chaps_module_) |
524 return; | 519 return; |
525 | 520 |
526 // Note that a reference is not taken to chaps_module_. This is safe since | 521 // Note that a reference is not taken to chaps_module_. This is safe since |
527 // NSSInitSingleton is Leaky, so the reference it holds is never released. | 522 // NSSInitSingleton is Leaky, so the reference it holds is never released. |
528 std::unique_ptr<TPMModuleAndSlot> tpm_args( | 523 std::unique_ptr<TPMModuleAndSlot> tpm_args( |
529 new TPMModuleAndSlot(chaps_module_)); | 524 new TPMModuleAndSlot(chaps_module_)); |
530 TPMModuleAndSlot* tpm_args_ptr = tpm_args.get(); | 525 TPMModuleAndSlot* tpm_args_ptr = tpm_args.get(); |
531 base::WorkerPool::PostTaskAndReply( | 526 base::PostTaskWithTraitsAndReply( |
532 FROM_HERE, | 527 FROM_HERE, base::TaskTraits() |
533 base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread, | 528 .WithShutdownBehavior( |
534 slot_id, | 529 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
530 .MayBlock(), | |
531 base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread, slot_id, | |
535 tpm_args_ptr), | 532 tpm_args_ptr), |
536 base::Bind(&NSSInitSingleton::OnInitializedTPMForChromeOSUser, | 533 base::Bind(&NSSInitSingleton::OnInitializedTPMForChromeOSUser, |
537 base::Unretained(this), // NSSInitSingleton is leaky | 534 base::Unretained(this), // NSSInitSingleton is leaky |
538 username_hash, | 535 username_hash, base::Passed(&tpm_args))); |
539 base::Passed(&tpm_args)), | |
540 true /* task_is_slow */ | |
541 ); | |
542 } | 536 } |
543 | 537 |
544 void OnInitializedTPMForChromeOSUser( | 538 void OnInitializedTPMForChromeOSUser( |
545 const std::string& username_hash, | 539 const std::string& username_hash, |
546 std::unique_ptr<TPMModuleAndSlot> tpm_args) { | 540 std::unique_ptr<TPMModuleAndSlot> tpm_args) { |
547 DCHECK(thread_checker_.CalledOnValidThread()); | 541 DCHECK(thread_checker_.CalledOnValidThread()); |
548 DVLOG(2) << "Got tpm slot for " << username_hash << " " | 542 DVLOG(2) << "Got tpm slot for " << username_hash << " " |
549 << !!tpm_args->tpm_slot; | 543 << !!tpm_args->tpm_slot; |
550 chromeos_user_map_[username_hash]->SetPrivateSlot( | 544 chromeos_user_map_[username_hash]->SetPrivateSlot( |
551 std::move(tpm_args->tpm_slot)); | 545 std::move(tpm_args->tpm_slot)); |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
981 return time.ToInternalValue() - base::Time::UnixEpoch().ToInternalValue(); | 975 return time.ToInternalValue() - base::Time::UnixEpoch().ToInternalValue(); |
982 } | 976 } |
983 | 977 |
984 #if !defined(OS_CHROMEOS) | 978 #if !defined(OS_CHROMEOS) |
985 PK11SlotInfo* GetPersistentNSSKeySlot() { | 979 PK11SlotInfo* GetPersistentNSSKeySlot() { |
986 return g_nss_singleton.Get().GetPersistentNSSKeySlot(); | 980 return g_nss_singleton.Get().GetPersistentNSSKeySlot(); |
987 } | 981 } |
988 #endif | 982 #endif |
989 | 983 |
990 } // namespace crypto | 984 } // namespace crypto |
OLD | NEW |