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" |
| 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" |
18 #include "crypto/nss_util_internal.h" | 21 #include "crypto/nss_util_internal.h" |
19 | 22 |
20 #if defined(OS_OPENBSD) | 23 #if defined(OS_OPENBSD) |
21 #include <sys/mount.h> | 24 #include <sys/mount.h> |
22 #include <sys/param.h> | 25 #include <sys/param.h> |
23 #endif | 26 #endif |
24 | 27 |
25 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
26 #include <dlfcn.h> | 29 #include <dlfcn.h> |
27 #endif | 30 #endif |
28 | 31 |
29 #include <map> | 32 #include <map> |
30 #include <vector> | 33 #include <vector> |
31 | 34 |
32 #include "base/base_paths.h" | 35 #include "base/base_paths.h" |
33 #include "base/bind.h" | 36 #include "base/bind.h" |
34 #include "base/cpu.h" | 37 #include "base/cpu.h" |
35 #include "base/debug/alias.h" | 38 #include "base/debug/alias.h" |
36 #include "base/debug/stack_trace.h" | 39 #include "base/debug/stack_trace.h" |
37 #include "base/environment.h" | 40 #include "base/environment.h" |
38 #include "base/files/file_path.h" | 41 #include "base/files/file_path.h" |
39 #include "base/files/file_util.h" | 42 #include "base/files/file_util.h" |
40 #include "base/lazy_instance.h" | 43 #include "base/lazy_instance.h" |
41 #include "base/logging.h" | 44 #include "base/logging.h" |
42 #include "base/message_loop/message_loop.h" | |
43 #include "base/native_library.h" | 45 #include "base/native_library.h" |
44 #include "base/path_service.h" | 46 #include "base/path_service.h" |
45 #include "base/stl_util.h" | 47 #include "base/stl_util.h" |
46 #include "base/strings/stringprintf.h" | 48 #include "base/strings/stringprintf.h" |
47 #include "base/threading/thread_checker.h" | 49 #include "base/threading/thread_checker.h" |
48 #include "base/threading/thread_restrictions.h" | 50 #include "base/threading/thread_restrictions.h" |
49 #include "base/threading/worker_pool.h" | 51 #include "base/threading/worker_pool.h" |
50 #include "build/build_config.h" | 52 #include "build/build_config.h" |
51 | 53 |
52 // USE_NSS_CERTS means NSS is used for certificates and platform integration. | 54 // USE_NSS_CERTS means NSS is used for certificates and platform integration. |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 | 352 |
351 void InitializeTPMTokenAndSystemSlot( | 353 void InitializeTPMTokenAndSystemSlot( |
352 int system_slot_id, | 354 int system_slot_id, |
353 const base::Callback<void(bool)>& callback) { | 355 const base::Callback<void(bool)>& callback) { |
354 DCHECK(thread_checker_.CalledOnValidThread()); | 356 DCHECK(thread_checker_.CalledOnValidThread()); |
355 // Should not be called while there is already an initialization in | 357 // Should not be called while there is already an initialization in |
356 // progress. | 358 // progress. |
357 DCHECK(!initializing_tpm_token_); | 359 DCHECK(!initializing_tpm_token_); |
358 // If EnableTPMTokenForNSS hasn't been called, return false. | 360 // If EnableTPMTokenForNSS hasn't been called, return false. |
359 if (!tpm_token_enabled_for_nss_) { | 361 if (!tpm_token_enabled_for_nss_) { |
360 base::MessageLoop::current()->PostTask(FROM_HERE, | 362 base::ThreadTaskRunnerHandle::Get()->PostTask( |
361 base::Bind(callback, false)); | 363 FROM_HERE, base::Bind(callback, false)); |
362 return; | 364 return; |
363 } | 365 } |
364 | 366 |
365 // If everything is already initialized, then return true. | 367 // If everything is already initialized, then return true. |
366 // Note that only |tpm_slot_| is checked, since |chaps_module_| could be | 368 // Note that only |tpm_slot_| is checked, since |chaps_module_| could be |
367 // NULL in tests while |tpm_slot_| has been set to the test DB. | 369 // NULL in tests while |tpm_slot_| has been set to the test DB. |
368 if (tpm_slot_) { | 370 if (tpm_slot_) { |
369 base::MessageLoop::current()->PostTask(FROM_HERE, | 371 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
370 base::Bind(callback, true)); | 372 base::Bind(callback, true)); |
371 return; | 373 return; |
372 } | 374 } |
373 | 375 |
374 // Note that a reference is not taken to chaps_module_. This is safe since | 376 // Note that a reference is not taken to chaps_module_. This is safe since |
375 // NSSInitSingleton is Leaky, so the reference it holds is never released. | 377 // NSSInitSingleton is Leaky, so the reference it holds is never released. |
376 std::unique_ptr<TPMModuleAndSlot> tpm_args( | 378 std::unique_ptr<TPMModuleAndSlot> tpm_args( |
377 new TPMModuleAndSlot(chaps_module_)); | 379 new TPMModuleAndSlot(chaps_module_)); |
378 TPMModuleAndSlot* tpm_args_ptr = tpm_args.get(); | 380 TPMModuleAndSlot* tpm_args_ptr = tpm_args.get(); |
379 if (base::WorkerPool::PostTaskAndReply( | 381 if (base::WorkerPool::PostTaskAndReply( |
380 FROM_HERE, | 382 FROM_HERE, |
381 base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread, | 383 base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread, |
382 system_slot_id, | 384 system_slot_id, |
383 tpm_args_ptr), | 385 tpm_args_ptr), |
384 base::Bind(&NSSInitSingleton::OnInitializedTPMTokenAndSystemSlot, | 386 base::Bind(&NSSInitSingleton::OnInitializedTPMTokenAndSystemSlot, |
385 base::Unretained(this), // NSSInitSingleton is leaky | 387 base::Unretained(this), // NSSInitSingleton is leaky |
386 callback, | 388 callback, |
387 base::Passed(&tpm_args)), | 389 base::Passed(&tpm_args)), |
388 true /* task_is_slow */ | 390 true /* task_is_slow */ |
389 )) { | 391 )) { |
390 initializing_tpm_token_ = true; | 392 initializing_tpm_token_ = true; |
391 } else { | 393 } else { |
392 base::MessageLoop::current()->PostTask(FROM_HERE, | 394 base::ThreadTaskRunnerHandle::Get()->PostTask( |
393 base::Bind(callback, false)); | 395 FROM_HERE, base::Bind(callback, false)); |
394 } | 396 } |
395 } | 397 } |
396 | 398 |
397 static void InitializeTPMTokenOnWorkerThread(CK_SLOT_ID token_slot_id, | 399 static void InitializeTPMTokenOnWorkerThread(CK_SLOT_ID token_slot_id, |
398 TPMModuleAndSlot* tpm_args) { | 400 TPMModuleAndSlot* tpm_args) { |
399 // This tries to load the Chaps module so NSS can talk to the hardware | 401 // This tries to load the Chaps module so NSS can talk to the hardware |
400 // TPM. | 402 // TPM. |
401 if (!tpm_args->chaps_module) { | 403 if (!tpm_args->chaps_module) { |
402 ScopedChapsLoadFixup chaps_loader; | 404 ScopedChapsLoadFixup chaps_loader; |
403 | 405 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 } | 593 } |
592 | 594 |
593 ScopedPK11Slot GetPrivateSlotForChromeOSUser( | 595 ScopedPK11Slot GetPrivateSlotForChromeOSUser( |
594 const std::string& username_hash, | 596 const std::string& username_hash, |
595 const base::Callback<void(ScopedPK11Slot)>& callback) { | 597 const base::Callback<void(ScopedPK11Slot)>& callback) { |
596 DCHECK(thread_checker_.CalledOnValidThread()); | 598 DCHECK(thread_checker_.CalledOnValidThread()); |
597 | 599 |
598 if (username_hash.empty()) { | 600 if (username_hash.empty()) { |
599 DVLOG(2) << "empty username_hash"; | 601 DVLOG(2) << "empty username_hash"; |
600 if (!callback.is_null()) { | 602 if (!callback.is_null()) { |
601 base::MessageLoop::current()->PostTask( | 603 base::ThreadTaskRunnerHandle::Get()->PostTask( |
602 FROM_HERE, base::Bind(callback, base::Passed(ScopedPK11Slot()))); | 604 FROM_HERE, base::Bind(callback, base::Passed(ScopedPK11Slot()))); |
603 } | 605 } |
604 return ScopedPK11Slot(); | 606 return ScopedPK11Slot(); |
605 } | 607 } |
606 | 608 |
607 DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end()); | 609 DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end()); |
608 | 610 |
609 return chromeos_user_map_[username_hash]->GetPrivateSlot(callback); | 611 return chromeos_user_map_[username_hash]->GetPrivateSlot(callback); |
610 } | 612 } |
611 | 613 |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1022 return time.ToInternalValue() - base::Time::UnixEpoch().ToInternalValue(); | 1024 return time.ToInternalValue() - base::Time::UnixEpoch().ToInternalValue(); |
1023 } | 1025 } |
1024 | 1026 |
1025 #if !defined(OS_CHROMEOS) | 1027 #if !defined(OS_CHROMEOS) |
1026 PK11SlotInfo* GetPersistentNSSKeySlot() { | 1028 PK11SlotInfo* GetPersistentNSSKeySlot() { |
1027 return g_nss_singleton.Get().GetPersistentNSSKeySlot(); | 1029 return g_nss_singleton.Get().GetPersistentNSSKeySlot(); |
1028 } | 1030 } |
1029 #endif | 1031 #endif |
1030 | 1032 |
1031 } // namespace crypto | 1033 } // namespace crypto |
OLD | NEW |