[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/nss_util.h" |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 6 | |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 7 | #include <nss.h> |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 8 | #include <pk11pub.h> |
[email protected] | c72f16a | 2009-03-19 16:02:31 | [diff] [blame] | 9 | #include <plarena.h> |
[email protected] | 6e7e806 | 2009-04-13 17:35:09 | [diff] [blame] | 10 | #include <prerror.h> |
[email protected] | c72f16a | 2009-03-19 16:02:31 | [diff] [blame] | 11 | #include <prinit.h> |
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 12 | #include <prtime.h> |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 13 | #include <secmod.h> |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 14 | |
| 15 | #include <memory> |
dcheng | cf738a9 | 2015-12-31 16:11:45 | [diff] [blame] | 16 | #include <utility> |
| 17 | |
fdoray | 34e1fb8 | 2016-06-08 14:43:22 | [diff] [blame] | 18 | #include "base/location.h" |
| 19 | #include "base/single_thread_task_runner.h" |
| 20 | #include "base/threading/thread_task_runner_handle.h" |
dcheng | cf738a9 | 2015-12-31 16:11:45 | [diff] [blame] | 21 | #include "crypto/nss_util_internal.h" |
[email protected] | b43c97c | 2008-10-22 19:50:58 | [diff] [blame] | 22 | |
[email protected] | 5a77df5 | 2014-02-05 08:37:48 | [diff] [blame] | 23 | #if defined(OS_OPENBSD) |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 24 | #include <sys/mount.h> |
| 25 | #include <sys/param.h> |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 26 | #endif |
| 27 | |
spang | ba254c4 | 2015-05-14 00:00:23 | [diff] [blame] | 28 | #if defined(OS_CHROMEOS) |
| 29 | #include <dlfcn.h> |
| 30 | #endif |
| 31 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 32 | #include <map> |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
[email protected] | 6bdc5227 | 2014-05-27 00:12:33 | [diff] [blame] | 35 | #include "base/base_paths.h" |
[email protected] | 7037a43c | 2014-01-14 14:00:46 | [diff] [blame] | 36 | #include "base/bind.h" |
[email protected] | f3d445e | 2013-11-22 18:35:03 | [diff] [blame] | 37 | #include "base/cpu.h" |
[email protected] | 716fb11 | 2012-11-15 05:41:25 | [diff] [blame] | 38 | #include "base/debug/alias.h" |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 39 | #include "base/debug/stack_trace.h" |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 40 | #include "base/environment.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 41 | #include "base/files/file_path.h" |
thestig | c9e38a2 | 2014-09-13 01:02:11 | [diff] [blame] | 42 | #include "base/files/file_util.h" |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 43 | #include "base/lazy_instance.h" |
[email protected] | c1444fe | 2008-09-17 09:42:51 | [diff] [blame] | 44 | #include "base/logging.h" |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 45 | #include "base/native_library.h" |
[email protected] | 6bdc5227 | 2014-05-27 00:12:33 | [diff] [blame] | 46 | #include "base/path_service.h" |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 47 | #include "base/stl_util.h" |
[email protected] | 0d8db08 | 2013-06-11 07:27:01 | [diff] [blame] | 48 | #include "base/strings/stringprintf.h" |
rsleevi | a3ad8d0 | 2016-06-07 18:22:33 | [diff] [blame] | 49 | #include "base/synchronization/lock.h" |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 50 | #include "base/threading/thread_checker.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 51 | #include "base/threading/thread_restrictions.h" |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 52 | #include "base/threading/worker_pool.h" |
[email protected] | 26661c2 | 2011-10-07 01:33:58 | [diff] [blame] | 53 | #include "build/build_config.h" |
[email protected] | 99e5e952 | 2013-12-16 13:05:27 | [diff] [blame] | 54 | #include "crypto/nss_crypto_module_delegate.h" |
[email protected] | abd4aba | 2010-01-27 19:36:22 | [diff] [blame] | 55 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 56 | namespace crypto { |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 57 | |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 58 | namespace { |
| 59 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 60 | #if defined(OS_CHROMEOS) |
[email protected] | 83e1ae3 | 2014-07-18 10:57:07 | [diff] [blame] | 61 | const char kUserNSSDatabaseName[] = "UserNSSDB"; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 62 | |
[email protected] | 84e4772 | 2011-11-17 05:12:02 | [diff] [blame] | 63 | // Constants for loading the Chrome OS TPM-backed PKCS #11 library. |
| 64 | const char kChapsModuleName[] = "Chaps"; |
| 65 | const char kChapsPath[] = "libchaps.so"; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 66 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 67 | // Fake certificate authority database used for testing. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 68 | static const base::FilePath::CharType kReadOnlyCertDB[] = |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 69 | FILE_PATH_LITERAL("/etc/fake_root_ca/nssdb"); |
| 70 | #endif // defined(OS_CHROMEOS) |
| 71 | |
| 72 | std::string GetNSSErrorMessage() { |
| 73 | std::string result; |
| 74 | if (PR_GetErrorTextLength()) { |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 75 | std::unique_ptr<char[]> error_text(new char[PR_GetErrorTextLength() + 1]); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 76 | PRInt32 copied = PR_GetErrorText(error_text.get()); |
| 77 | result = std::string(error_text.get(), copied); |
| 78 | } else { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 79 | result = base::StringPrintf("NSS error code: %d", PR_GetError()); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 80 | } |
| 81 | return result; |
| 82 | } |
| 83 | |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 84 | #if !defined(OS_CHROMEOS) |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 85 | base::FilePath GetDefaultConfigDirectory() { |
[email protected] | 6bdc5227 | 2014-05-27 00:12:33 | [diff] [blame] | 86 | base::FilePath dir; |
| 87 | PathService::Get(base::DIR_HOME, &dir); |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 88 | if (dir.empty()) { |
| 89 | LOG(ERROR) << "Failed to get home directory."; |
| 90 | return dir; |
[email protected] | 8691334 | 2009-05-25 02:14:34 | [diff] [blame] | 91 | } |
[email protected] | 8691334 | 2009-05-25 02:14:34 | [diff] [blame] | 92 | dir = dir.AppendASCII(".pki").AppendASCII("nssdb"); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 93 | if (!base::CreateDirectory(dir)) { |
[email protected] | 948a707b | 2011-06-07 22:51:44 | [diff] [blame] | 94 | LOG(ERROR) << "Failed to create " << dir.value() << " directory."; |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 95 | dir.clear(); |
[email protected] | 8691334 | 2009-05-25 02:14:34 | [diff] [blame] | 96 | } |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 97 | DVLOG(2) << "DefaultConfigDirectory: " << dir.value(); |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 98 | return dir; |
[email protected] | 8691334 | 2009-05-25 02:14:34 | [diff] [blame] | 99 | } |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 100 | #endif // !defined(IS_CHROMEOS) |
[email protected] | 8691334 | 2009-05-25 02:14:34 | [diff] [blame] | 101 | |
[email protected] | 259c42f | 2013-09-12 20:32:22 | [diff] [blame] | 102 | // On non-Chrome OS platforms, return the default config directory. On Chrome OS |
| 103 | // test images, return a read-only directory with fake root CA certs (which are |
| 104 | // used by the local Google Accounts server mock we use when testing our login |
| 105 | // code). On Chrome OS non-test images (where the read-only directory doesn't |
| 106 | // exist), return an empty path. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 107 | base::FilePath GetInitialConfigDirectory() { |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 108 | #if defined(OS_CHROMEOS) |
[email protected] | 259c42f | 2013-09-12 20:32:22 | [diff] [blame] | 109 | base::FilePath database_dir = base::FilePath(kReadOnlyCertDB); |
| 110 | if (!base::PathExists(database_dir)) |
| 111 | database_dir.clear(); |
| 112 | return database_dir; |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 113 | #else |
| 114 | return GetDefaultConfigDirectory(); |
| 115 | #endif // defined(OS_CHROMEOS) |
| 116 | } |
| 117 | |
[email protected] | 88b9db7 | 2011-01-13 01:48:43 | [diff] [blame] | 118 | // This callback for NSS forwards all requests to a caller-specified |
[email protected] | 3f1f841 | 2011-01-19 03:01:23 | [diff] [blame] | 119 | // CryptoModuleBlockingPasswordDelegate object. |
| 120 | char* PKCS11PasswordFunc(PK11SlotInfo* slot, PRBool retry, void* arg) { |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 121 | crypto::CryptoModuleBlockingPasswordDelegate* delegate = |
| 122 | reinterpret_cast<crypto::CryptoModuleBlockingPasswordDelegate*>(arg); |
[email protected] | 88b9db7 | 2011-01-13 01:48:43 | [diff] [blame] | 123 | if (delegate) { |
| 124 | bool cancelled = false; |
| 125 | std::string password = delegate->RequestPassword(PK11_GetTokenName(slot), |
| 126 | retry != PR_FALSE, |
| 127 | &cancelled); |
| 128 | if (cancelled) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 129 | return nullptr; |
[email protected] | 88b9db7 | 2011-01-13 01:48:43 | [diff] [blame] | 130 | char* result = PORT_Strdup(password.c_str()); |
| 131 | password.replace(0, password.size(), password.size(), 0); |
| 132 | return result; |
| 133 | } |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 134 | DLOG(ERROR) << "PK11 password requested with nullptr arg"; |
| 135 | return nullptr; |
[email protected] | 88b9db7 | 2011-01-13 01:48:43 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 138 | // NSS creates a local cache of the sqlite database if it detects that the |
| 139 | // filesystem the database is on is much slower than the local disk. The |
| 140 | // detection doesn't work with the latest versions of sqlite, such as 3.6.22 |
| 141 | // (NSS bug https://bugzilla.mozilla.org/show_bug.cgi?id=578561). So we set |
| 142 | // the NSS environment variable NSS_SDB_USE_CACHE to "yes" to override NSS's |
| 143 | // detection when database_dir is on NFS. See http://crbug.com/48585. |
| 144 | // |
[email protected] | ac3d597 | 2011-01-13 20:33:45 | [diff] [blame] | 145 | // Because this function sets an environment variable it must be run before we |
| 146 | // go multi-threaded. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 147 | void UseLocalCacheOfNSSDatabaseIfNFS(const base::FilePath& database_dir) { |
[email protected] | 5a77df5 | 2014-02-05 08:37:48 | [diff] [blame] | 148 | bool db_on_nfs = false; |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 149 | #if defined(OS_LINUX) |
[email protected] | a26f4ae | 2014-03-13 17:26:21 | [diff] [blame] | 150 | base::FileSystemType fs_type = base::FILE_SYSTEM_UNKNOWN; |
| 151 | if (base::GetFileSystemType(database_dir, &fs_type)) |
| 152 | db_on_nfs = (fs_type == base::FILE_SYSTEM_NFS); |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 153 | #elif defined(OS_OPENBSD) |
[email protected] | 5a77df5 | 2014-02-05 08:37:48 | [diff] [blame] | 154 | struct statfs buf; |
| 155 | if (statfs(database_dir.value().c_str(), &buf) == 0) |
| 156 | db_on_nfs = (strcmp(buf.f_fstypename, MOUNT_NFS) == 0); |
| 157 | #else |
| 158 | NOTIMPLEMENTED(); |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 159 | #endif |
[email protected] | 5a77df5 | 2014-02-05 08:37:48 | [diff] [blame] | 160 | |
| 161 | if (db_on_nfs) { |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 162 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 5a77df5 | 2014-02-05 08:37:48 | [diff] [blame] | 163 | static const char kUseCacheEnvVar[] = "NSS_SDB_USE_CACHE"; |
| 164 | if (!env->HasVar(kUseCacheEnvVar)) |
| 165 | env->SetVar(kUseCacheEnvVar, "yes"); |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 166 | } |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 169 | // A singleton to initialize/deinitialize NSPR. |
| 170 | // Separate from the NSS singleton because we initialize NSPR on the UI thread. |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 171 | // Now that we're leaking the singleton, we could merge back with the NSS |
| 172 | // singleton. |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 173 | class NSPRInitSingleton { |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 174 | private: |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 175 | friend struct base::DefaultLazyInstanceTraits<NSPRInitSingleton>; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 176 | |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 177 | NSPRInitSingleton() { |
| 178 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| 179 | } |
| 180 | |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 181 | // NOTE(willchan): We don't actually execute this code since we leak NSS to |
| 182 | // prevent non-joinable threads from using NSS after it's already been shut |
| 183 | // down. |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 184 | ~NSPRInitSingleton() { |
[email protected] | 829296f | 2010-01-27 02:58:03 | [diff] [blame] | 185 | PL_ArenaFinish(); |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 186 | PRStatus prstatus = PR_Cleanup(); |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 187 | if (prstatus != PR_SUCCESS) |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 188 | LOG(ERROR) << "PR_Cleanup failed; was NSPR initialized on wrong thread?"; |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 189 | } |
| 190 | }; |
| 191 | |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 192 | base::LazyInstance<NSPRInitSingleton>::Leaky |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 193 | g_nspr_singleton = LAZY_INSTANCE_INITIALIZER; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 194 | |
[email protected] | 007f512 | 2012-11-21 16:00:21 | [diff] [blame] | 195 | // Force a crash with error info on NSS_NoDB_Init failure. |
| 196 | void CrashOnNSSInitFailure() { |
| 197 | int nss_error = PR_GetError(); |
| 198 | int os_error = PR_GetOSError(); |
[email protected] | 716fb11 | 2012-11-15 05:41:25 | [diff] [blame] | 199 | base::debug::Alias(&nss_error); |
| 200 | base::debug::Alias(&os_error); |
[email protected] | 007f512 | 2012-11-21 16:00:21 | [diff] [blame] | 201 | LOG(ERROR) << "Error initializing NSS without a persistent database: " |
| 202 | << GetNSSErrorMessage(); |
[email protected] | 2b12459a | 2012-11-16 03:45:32 | [diff] [blame] | 203 | LOG(FATAL) << "nss_error=" << nss_error << ", os_error=" << os_error; |
[email protected] | 716fb11 | 2012-11-15 05:41:25 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 206 | #if defined(OS_CHROMEOS) |
| 207 | class ChromeOSUserData { |
| 208 | public: |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 209 | explicit ChromeOSUserData(ScopedPK11Slot public_slot) |
dcheng | cf738a9 | 2015-12-31 16:11:45 | [diff] [blame] | 210 | : public_slot_(std::move(public_slot)), |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 211 | private_slot_initialization_started_(false) {} |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 212 | ~ChromeOSUserData() { |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 213 | if (public_slot_) { |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 214 | SECStatus status = SECMOD_CloseUserDB(public_slot_.get()); |
| 215 | if (status != SECSuccess) |
| 216 | PLOG(ERROR) << "SECMOD_CloseUserDB failed: " << PORT_GetError(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | ScopedPK11Slot GetPublicSlot() { |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 221 | return ScopedPK11Slot(public_slot_ ? PK11_ReferenceSlot(public_slot_.get()) |
| 222 | : nullptr); |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | ScopedPK11Slot GetPrivateSlot( |
| 226 | const base::Callback<void(ScopedPK11Slot)>& callback) { |
| 227 | if (private_slot_) |
| 228 | return ScopedPK11Slot(PK11_ReferenceSlot(private_slot_.get())); |
| 229 | if (!callback.is_null()) |
| 230 | tpm_ready_callback_list_.push_back(callback); |
| 231 | return ScopedPK11Slot(); |
| 232 | } |
| 233 | |
| 234 | void SetPrivateSlot(ScopedPK11Slot private_slot) { |
| 235 | DCHECK(!private_slot_); |
dcheng | cf738a9 | 2015-12-31 16:11:45 | [diff] [blame] | 236 | private_slot_ = std::move(private_slot); |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 237 | |
| 238 | SlotReadyCallbackList callback_list; |
| 239 | callback_list.swap(tpm_ready_callback_list_); |
| 240 | for (SlotReadyCallbackList::iterator i = callback_list.begin(); |
| 241 | i != callback_list.end(); |
| 242 | ++i) { |
| 243 | (*i).Run(ScopedPK11Slot(PK11_ReferenceSlot(private_slot_.get()))); |
| 244 | } |
| 245 | } |
| 246 | |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 247 | bool private_slot_initialization_started() const { |
| 248 | return private_slot_initialization_started_; |
| 249 | } |
| 250 | |
| 251 | void set_private_slot_initialization_started() { |
| 252 | private_slot_initialization_started_ = true; |
| 253 | } |
| 254 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 255 | private: |
| 256 | ScopedPK11Slot public_slot_; |
| 257 | ScopedPK11Slot private_slot_; |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 258 | |
| 259 | bool private_slot_initialization_started_; |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 260 | |
| 261 | typedef std::vector<base::Callback<void(ScopedPK11Slot)> > |
| 262 | SlotReadyCallbackList; |
| 263 | SlotReadyCallbackList tpm_ready_callback_list_; |
| 264 | }; |
spang | ba254c4 | 2015-05-14 00:00:23 | [diff] [blame] | 265 | |
| 266 | class ScopedChapsLoadFixup { |
| 267 | public: |
| 268 | ScopedChapsLoadFixup(); |
| 269 | ~ScopedChapsLoadFixup(); |
| 270 | |
| 271 | private: |
| 272 | #if defined(COMPONENT_BUILD) |
| 273 | void *chaps_handle_; |
| 274 | #endif |
| 275 | }; |
| 276 | |
| 277 | #if defined(COMPONENT_BUILD) |
| 278 | |
| 279 | ScopedChapsLoadFixup::ScopedChapsLoadFixup() { |
| 280 | // HACK: libchaps links the system protobuf and there are symbol conflicts |
| 281 | // with the bundled copy. Load chaps with RTLD_DEEPBIND to workaround. |
| 282 | chaps_handle_ = dlopen(kChapsPath, RTLD_LOCAL | RTLD_NOW | RTLD_DEEPBIND); |
| 283 | } |
| 284 | |
| 285 | ScopedChapsLoadFixup::~ScopedChapsLoadFixup() { |
| 286 | // LoadModule() will have taken a 2nd reference. |
| 287 | if (chaps_handle_) |
| 288 | dlclose(chaps_handle_); |
| 289 | } |
| 290 | |
| 291 | #else |
| 292 | |
| 293 | ScopedChapsLoadFixup::ScopedChapsLoadFixup() {} |
| 294 | ScopedChapsLoadFixup::~ScopedChapsLoadFixup() {} |
| 295 | |
| 296 | #endif // defined(COMPONENT_BUILD) |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 297 | #endif // defined(OS_CHROMEOS) |
| 298 | |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 299 | class NSSInitSingleton { |
| 300 | public: |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 301 | #if defined(OS_CHROMEOS) |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 302 | // Used with PostTaskAndReply to pass handles to worker thread and back. |
| 303 | struct TPMModuleAndSlot { |
| 304 | explicit TPMModuleAndSlot(SECMODModule* init_chaps_module) |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 305 | : chaps_module(init_chaps_module) {} |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 306 | SECMODModule* chaps_module; |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 307 | crypto::ScopedPK11Slot tpm_slot; |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 308 | }; |
| 309 | |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 310 | ScopedPK11Slot OpenPersistentNSSDBForPath(const std::string& db_name, |
| 311 | const base::FilePath& path) { |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 312 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 313 | // NSS is allowed to do IO on the current thread since dispatching |
| 314 | // to a dedicated thread would still have the affect of blocking |
| 315 | // the current thread, due to NSS's internal locking requirements |
| 316 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 317 | |
| 318 | base::FilePath nssdb_path = path.AppendASCII(".pki").AppendASCII("nssdb"); |
| 319 | if (!base::CreateDirectory(nssdb_path)) { |
| 320 | LOG(ERROR) << "Failed to create " << nssdb_path.value() << " directory."; |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 321 | return ScopedPK11Slot(); |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 322 | } |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 323 | return OpenSoftwareNSSDB(nssdb_path, db_name); |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 324 | } |
| 325 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 326 | void EnableTPMTokenForNSS() { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 327 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 328 | |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 329 | // If this gets set, then we'll use the TPM for certs with |
| 330 | // private keys, otherwise we'll fall back to the software |
| 331 | // implementation. |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 332 | tpm_token_enabled_for_nss_ = true; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 335 | bool IsTPMTokenEnabledForNSS() { |
| 336 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 337 | return tpm_token_enabled_for_nss_; |
| 338 | } |
| 339 | |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 340 | void InitializeTPMTokenAndSystemSlot( |
| 341 | int system_slot_id, |
| 342 | const base::Callback<void(bool)>& callback) { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 343 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 344 | // Should not be called while there is already an initialization in |
| 345 | // progress. |
| 346 | DCHECK(!initializing_tpm_token_); |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 347 | // If EnableTPMTokenForNSS hasn't been called, return false. |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 348 | if (!tpm_token_enabled_for_nss_) { |
fdoray | 34e1fb8 | 2016-06-08 14:43:22 | [diff] [blame] | 349 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 350 | FROM_HERE, base::Bind(callback, false)); |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 351 | return; |
| 352 | } |
[email protected] | 14172c8 | 2012-02-28 10:34:21 | [diff] [blame] | 353 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 354 | // If everything is already initialized, then return true. |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 355 | // Note that only |tpm_slot_| is checked, since |chaps_module_| could be |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 356 | // nullptr in tests while |tpm_slot_| has been set to the test DB. |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 357 | if (tpm_slot_) { |
fdoray | 34e1fb8 | 2016-06-08 14:43:22 | [diff] [blame] | 358 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 359 | base::Bind(callback, true)); |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 360 | return; |
| 361 | } |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 362 | |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 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. |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 365 | std::unique_ptr<TPMModuleAndSlot> tpm_args( |
| 366 | new TPMModuleAndSlot(chaps_module_)); |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 367 | TPMModuleAndSlot* tpm_args_ptr = tpm_args.get(); |
| 368 | if (base::WorkerPool::PostTaskAndReply( |
| 369 | FROM_HERE, |
| 370 | base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread, |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 371 | system_slot_id, |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 372 | tpm_args_ptr), |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 373 | base::Bind(&NSSInitSingleton::OnInitializedTPMTokenAndSystemSlot, |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 374 | base::Unretained(this), // NSSInitSingleton is leaky |
| 375 | callback, |
| 376 | base::Passed(&tpm_args)), |
| 377 | true /* task_is_slow */ |
| 378 | )) { |
| 379 | initializing_tpm_token_ = true; |
| 380 | } else { |
fdoray | 34e1fb8 | 2016-06-08 14:43:22 | [diff] [blame] | 381 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 382 | FROM_HERE, base::Bind(callback, false)); |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | static void InitializeTPMTokenOnWorkerThread(CK_SLOT_ID token_slot_id, |
| 387 | TPMModuleAndSlot* tpm_args) { |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 388 | // This tries to load the Chaps module so NSS can talk to the hardware |
| 389 | // TPM. |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 390 | if (!tpm_args->chaps_module) { |
spang | ba254c4 | 2015-05-14 00:00:23 | [diff] [blame] | 391 | ScopedChapsLoadFixup chaps_loader; |
| 392 | |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 393 | DVLOG(3) << "Loading chaps..."; |
| 394 | tpm_args->chaps_module = LoadModule( |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 395 | kChapsModuleName, |
| 396 | kChapsPath, |
| 397 | // For more details on these parameters, see: |
| 398 | // https://developer.mozilla.org/en/PKCS11_Module_Specs |
| 399 | // slotFlags=[PublicCerts] -- Certificates and public keys can be |
| 400 | // read from this slot without requiring a call to C_Login. |
| 401 | // askpw=only -- Only authenticate to the token when necessary. |
| 402 | "NSS=\"slotParams=(0={slotFlags=[PublicCerts] askpw=only})\""); |
[email protected] | 14172c8 | 2012-02-28 10:34:21 | [diff] [blame] | 403 | } |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 404 | if (tpm_args->chaps_module) { |
| 405 | tpm_args->tpm_slot = |
| 406 | GetTPMSlotForIdOnWorkerThread(tpm_args->chaps_module, token_slot_id); |
| 407 | } |
| 408 | } |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 409 | |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 410 | void OnInitializedTPMTokenAndSystemSlot( |
| 411 | const base::Callback<void(bool)>& callback, |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 412 | std::unique_ptr<TPMModuleAndSlot> tpm_args) { |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 413 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 414 | DVLOG(2) << "Loaded chaps: " << !!tpm_args->chaps_module |
| 415 | << ", got tpm slot: " << !!tpm_args->tpm_slot; |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 416 | |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 417 | chaps_module_ = tpm_args->chaps_module; |
dcheng | cf738a9 | 2015-12-31 16:11:45 | [diff] [blame] | 418 | tpm_slot_ = std::move(tpm_args->tpm_slot); |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 419 | if (!chaps_module_ && test_system_slot_) { |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 420 | // chromeos_unittests try to test the TPM initialization process. If we |
| 421 | // have a test DB open, pretend that it is the TPM slot. |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 422 | tpm_slot_.reset(PK11_ReferenceSlot(test_system_slot_.get())); |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 423 | } |
| 424 | initializing_tpm_token_ = false; |
| 425 | |
[email protected] | 442233d4 | 2014-08-02 07:37:24 | [diff] [blame] | 426 | if (tpm_slot_) |
| 427 | RunAndClearTPMReadyCallbackList(); |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 428 | |
| 429 | callback.Run(!!tpm_slot_); |
[email protected] | c175cdb | 2011-06-28 20:41:55 | [diff] [blame] | 430 | } |
| 431 | |
[email protected] | 442233d4 | 2014-08-02 07:37:24 | [diff] [blame] | 432 | void RunAndClearTPMReadyCallbackList() { |
| 433 | TPMReadyCallbackList callback_list; |
| 434 | callback_list.swap(tpm_ready_callback_list_); |
| 435 | for (TPMReadyCallbackList::iterator i = callback_list.begin(); |
| 436 | i != callback_list.end(); |
| 437 | ++i) { |
| 438 | i->Run(); |
| 439 | } |
| 440 | } |
| 441 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 442 | bool IsTPMTokenReady(const base::Closure& callback) { |
| 443 | if (!callback.is_null()) { |
| 444 | // Cannot DCHECK in the general case yet, but since the callback is |
| 445 | // a new addition to the API, DCHECK to make sure at least the new uses |
| 446 | // don't regress. |
| 447 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 448 | } else if (!thread_checker_.CalledOnValidThread()) { |
| 449 | // TODO(mattm): Change to DCHECK when callers have been fixed. |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 450 | DVLOG(1) << "Called on wrong thread.\n" |
| 451 | << base::debug::StackTrace().ToString(); |
| 452 | } |
| 453 | |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 454 | if (tpm_slot_) |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 455 | return true; |
| 456 | |
| 457 | if (!callback.is_null()) |
| 458 | tpm_ready_callback_list_.push_back(callback); |
| 459 | |
| 460 | return false; |
[email protected] | 74beead | 2011-04-12 20:40:12 | [diff] [blame] | 461 | } |
| 462 | |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 463 | // Note that CK_SLOT_ID is an unsigned long, but cryptohome gives us the slot |
| 464 | // id as an int. This should be safe since this is only used with chaps, which |
| 465 | // we also control. |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 466 | static crypto::ScopedPK11Slot GetTPMSlotForIdOnWorkerThread( |
| 467 | SECMODModule* chaps_module, |
| 468 | CK_SLOT_ID slot_id) { |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 469 | DCHECK(chaps_module); |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 470 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 471 | DVLOG(3) << "Poking chaps module."; |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 472 | SECStatus rv = SECMOD_UpdateSlotList(chaps_module); |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 473 | if (rv != SECSuccess) |
| 474 | PLOG(ERROR) << "SECMOD_UpdateSlotList failed: " << PORT_GetError(); |
| 475 | |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 476 | PK11SlotInfo* slot = SECMOD_LookupSlot(chaps_module->moduleID, slot_id); |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 477 | if (!slot) |
| 478 | LOG(ERROR) << "TPM slot " << slot_id << " not found."; |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 479 | return crypto::ScopedPK11Slot(slot); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 480 | } |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 481 | |
pneubeck | fa32f2e | 2014-09-12 09:59:00 | [diff] [blame] | 482 | bool InitializeNSSForChromeOSUser(const std::string& username_hash, |
| 483 | const base::FilePath& path) { |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 484 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 485 | if (chromeos_user_map_.find(username_hash) != chromeos_user_map_.end()) { |
| 486 | // This user already exists in our mapping. |
| 487 | DVLOG(2) << username_hash << " already initialized."; |
| 488 | return false; |
| 489 | } |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 490 | |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 491 | DVLOG(2) << "Opening NSS DB " << path.value(); |
[email protected] | 83e1ae3 | 2014-07-18 10:57:07 | [diff] [blame] | 492 | std::string db_name = base::StringPrintf( |
| 493 | "%s %s", kUserNSSDatabaseName, username_hash.c_str()); |
| 494 | ScopedPK11Slot public_slot(OpenPersistentNSSDBForPath(db_name, path)); |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 495 | chromeos_user_map_[username_hash] = |
dcheng | cf738a9 | 2015-12-31 16:11:45 | [diff] [blame] | 496 | new ChromeOSUserData(std::move(public_slot)); |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 497 | return true; |
| 498 | } |
| 499 | |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 500 | bool ShouldInitializeTPMForChromeOSUser(const std::string& username_hash) { |
| 501 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 502 | DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end()); |
| 503 | |
| 504 | return !chromeos_user_map_[username_hash] |
| 505 | ->private_slot_initialization_started(); |
| 506 | } |
| 507 | |
| 508 | void WillInitializeTPMForChromeOSUser(const std::string& username_hash) { |
| 509 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 510 | DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end()); |
| 511 | |
| 512 | chromeos_user_map_[username_hash] |
| 513 | ->set_private_slot_initialization_started(); |
| 514 | } |
| 515 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 516 | void InitializeTPMForChromeOSUser(const std::string& username_hash, |
| 517 | CK_SLOT_ID slot_id) { |
| 518 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 519 | DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end()); |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 520 | DCHECK(chromeos_user_map_[username_hash]-> |
| 521 | private_slot_initialization_started()); |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 522 | |
| 523 | if (!chaps_module_) |
| 524 | return; |
| 525 | |
| 526 | // 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. |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 528 | std::unique_ptr<TPMModuleAndSlot> tpm_args( |
| 529 | new TPMModuleAndSlot(chaps_module_)); |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 530 | TPMModuleAndSlot* tpm_args_ptr = tpm_args.get(); |
| 531 | base::WorkerPool::PostTaskAndReply( |
| 532 | FROM_HERE, |
| 533 | base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread, |
| 534 | slot_id, |
| 535 | tpm_args_ptr), |
| 536 | base::Bind(&NSSInitSingleton::OnInitializedTPMForChromeOSUser, |
| 537 | base::Unretained(this), // NSSInitSingleton is leaky |
| 538 | username_hash, |
| 539 | base::Passed(&tpm_args)), |
| 540 | true /* task_is_slow */ |
| 541 | ); |
| 542 | } |
| 543 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 544 | void OnInitializedTPMForChromeOSUser( |
| 545 | const std::string& username_hash, |
| 546 | std::unique_ptr<TPMModuleAndSlot> tpm_args) { |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 547 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 548 | DVLOG(2) << "Got tpm slot for " << username_hash << " " |
| 549 | << !!tpm_args->tpm_slot; |
| 550 | chromeos_user_map_[username_hash]->SetPrivateSlot( |
dcheng | cf738a9 | 2015-12-31 16:11:45 | [diff] [blame] | 551 | std::move(tpm_args->tpm_slot)); |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | void InitializePrivateSoftwareSlotForChromeOSUser( |
| 555 | const std::string& username_hash) { |
| 556 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 3194699 | 2014-01-31 22:29:02 | [diff] [blame] | 557 | VLOG(1) << "using software private slot for " << username_hash; |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 558 | DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end()); |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 559 | DCHECK(chromeos_user_map_[username_hash]-> |
| 560 | private_slot_initialization_started()); |
| 561 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 562 | chromeos_user_map_[username_hash]->SetPrivateSlot( |
| 563 | chromeos_user_map_[username_hash]->GetPublicSlot()); |
| 564 | } |
| 565 | |
| 566 | ScopedPK11Slot GetPublicSlotForChromeOSUser( |
| 567 | const std::string& username_hash) { |
| 568 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 7037a43c | 2014-01-14 14:00:46 | [diff] [blame] | 569 | |
| 570 | if (username_hash.empty()) { |
| 571 | DVLOG(2) << "empty username_hash"; |
| 572 | return ScopedPK11Slot(); |
| 573 | } |
| 574 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 575 | if (chromeos_user_map_.find(username_hash) == chromeos_user_map_.end()) { |
| 576 | LOG(ERROR) << username_hash << " not initialized."; |
| 577 | return ScopedPK11Slot(); |
| 578 | } |
| 579 | return chromeos_user_map_[username_hash]->GetPublicSlot(); |
| 580 | } |
| 581 | |
| 582 | ScopedPK11Slot GetPrivateSlotForChromeOSUser( |
| 583 | const std::string& username_hash, |
| 584 | const base::Callback<void(ScopedPK11Slot)>& callback) { |
| 585 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 7037a43c | 2014-01-14 14:00:46 | [diff] [blame] | 586 | |
| 587 | if (username_hash.empty()) { |
| 588 | DVLOG(2) << "empty username_hash"; |
| 589 | if (!callback.is_null()) { |
fdoray | 34e1fb8 | 2016-06-08 14:43:22 | [diff] [blame] | 590 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 7037a43c | 2014-01-14 14:00:46 | [diff] [blame] | 591 | FROM_HERE, base::Bind(callback, base::Passed(ScopedPK11Slot()))); |
| 592 | } |
| 593 | return ScopedPK11Slot(); |
| 594 | } |
| 595 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 596 | DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end()); |
| 597 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 598 | return chromeos_user_map_[username_hash]->GetPrivateSlot(callback); |
| 599 | } |
[email protected] | e53c0232 | 2013-12-17 00:09:00 | [diff] [blame] | 600 | |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 601 | void CloseChromeOSUserForTesting(const std::string& username_hash) { |
[email protected] | e53c0232 | 2013-12-17 00:09:00 | [diff] [blame] | 602 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 603 | ChromeOSUserMap::iterator i = chromeos_user_map_.find(username_hash); |
| 604 | DCHECK(i != chromeos_user_map_.end()); |
| 605 | delete i->second; |
| 606 | chromeos_user_map_.erase(i); |
| 607 | } |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 608 | |
| 609 | void SetSystemKeySlotForTesting(ScopedPK11Slot slot) { |
| 610 | // Ensure that a previous value of test_system_slot_ is not overwritten. |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 611 | // Unsetting, i.e. setting a nullptr, however is allowed. |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 612 | DCHECK(!slot || !test_system_slot_); |
dcheng | cf738a9 | 2015-12-31 16:11:45 | [diff] [blame] | 613 | test_system_slot_ = std::move(slot); |
[email protected] | 442233d4 | 2014-08-02 07:37:24 | [diff] [blame] | 614 | if (test_system_slot_) { |
| 615 | tpm_slot_.reset(PK11_ReferenceSlot(test_system_slot_.get())); |
| 616 | RunAndClearTPMReadyCallbackList(); |
| 617 | } else { |
| 618 | tpm_slot_.reset(); |
| 619 | } |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 620 | } |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 621 | #endif // defined(OS_CHROMEOS) |
| 622 | |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 623 | #if !defined(OS_CHROMEOS) |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 624 | PK11SlotInfo* GetPersistentNSSKeySlot() { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 625 | // TODO(mattm): Change to DCHECK when callers have been fixed. |
| 626 | if (!thread_checker_.CalledOnValidThread()) { |
| 627 | DVLOG(1) << "Called on wrong thread.\n" |
| 628 | << base::debug::StackTrace().ToString(); |
| 629 | } |
| 630 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 631 | return PK11_GetInternalKeySlot(); |
| 632 | } |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 633 | #endif |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 634 | |
[email protected] | c64b914 | 2011-04-19 18:49:54 | [diff] [blame] | 635 | #if defined(OS_CHROMEOS) |
[email protected] | 8edd721 | 2014-07-30 12:24:29 | [diff] [blame] | 636 | void GetSystemNSSKeySlotCallback( |
| 637 | const base::Callback<void(ScopedPK11Slot)>& callback) { |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 638 | callback.Run(ScopedPK11Slot(PK11_ReferenceSlot(tpm_slot_.get()))); |
[email protected] | 8edd721 | 2014-07-30 12:24:29 | [diff] [blame] | 639 | } |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 640 | |
[email protected] | 8edd721 | 2014-07-30 12:24:29 | [diff] [blame] | 641 | ScopedPK11Slot GetSystemNSSKeySlot( |
| 642 | const base::Callback<void(ScopedPK11Slot)>& callback) { |
| 643 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 644 | // TODO(mattm): chromeos::TPMTokenloader always calls |
| 645 | // InitializeTPMTokenAndSystemSlot with slot 0. If the system slot is |
| 646 | // disabled, tpm_slot_ will be the first user's slot instead. Can that be |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 647 | // detected and return nullptr instead? |
[email protected] | 8edd721 | 2014-07-30 12:24:29 | [diff] [blame] | 648 | |
| 649 | base::Closure wrapped_callback; |
| 650 | if (!callback.is_null()) { |
| 651 | wrapped_callback = |
| 652 | base::Bind(&NSSInitSingleton::GetSystemNSSKeySlotCallback, |
| 653 | base::Unretained(this) /* singleton is leaky */, |
| 654 | callback); |
| 655 | } |
| 656 | if (IsTPMTokenReady(wrapped_callback)) |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 657 | return ScopedPK11Slot(PK11_ReferenceSlot(tpm_slot_.get())); |
[email protected] | 8edd721 | 2014-07-30 12:24:29 | [diff] [blame] | 658 | return ScopedPK11Slot(); |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 659 | } |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 660 | #endif |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 661 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 662 | base::Lock* write_lock() { |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 663 | return &write_lock_; |
| 664 | } |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 665 | |
| 666 | private: |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 667 | friend struct base::DefaultLazyInstanceTraits<NSSInitSingleton>; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 668 | |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 669 | NSSInitSingleton() |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 670 | : tpm_token_enabled_for_nss_(false), |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 671 | initializing_tpm_token_(false), |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 672 | chaps_module_(nullptr), |
| 673 | root_(nullptr) { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 674 | // It's safe to construct on any thread, since LazyInstance will prevent any |
| 675 | // other threads from accessing until the constructor is done. |
| 676 | thread_checker_.DetachFromThread(); |
| 677 | |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 678 | EnsureNSPRInit(); |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 679 | |
[email protected] | 805acdc | 2013-08-07 22:57:00 | [diff] [blame] | 680 | // We *must* have NSS >= 3.14.3. |
anujk.sharma | 3cec2ab | 2015-01-22 02:35:02 | [diff] [blame] | 681 | static_assert( |
[email protected] | 805acdc | 2013-08-07 22:57:00 | [diff] [blame] | 682 | (NSS_VMAJOR == 3 && NSS_VMINOR == 14 && NSS_VPATCH >= 3) || |
| 683 | (NSS_VMAJOR == 3 && NSS_VMINOR > 14) || |
[email protected] | dc36c9c | 2010-01-20 20:45:00 | [diff] [blame] | 684 | (NSS_VMAJOR > 3), |
anujk.sharma | 3cec2ab | 2015-01-22 02:35:02 | [diff] [blame] | 685 | "nss version check failed"); |
[email protected] | dc36c9c | 2010-01-20 20:45:00 | [diff] [blame] | 686 | // Also check the run-time NSS version. |
| 687 | // NSS_VersionCheck is a >= check, not strict equality. |
[email protected] | 805acdc | 2013-08-07 22:57:00 | [diff] [blame] | 688 | if (!NSS_VersionCheck("3.14.3")) { |
| 689 | LOG(FATAL) << "NSS_VersionCheck(\"3.14.3\") failed. NSS >= 3.14.3 is " |
| 690 | "required. Please upgrade to the latest NSS, and if you " |
[email protected] | 1b8082d | 2010-02-19 12:21:48 | [diff] [blame] | 691 | "still get this error, contact your distribution " |
| 692 | "maintainer."; |
| 693 | } |
[email protected] | dc36c9c | 2010-01-20 20:45:00 | [diff] [blame] | 694 | |
[email protected] | 897f520 | 2009-09-08 17:40:27 | [diff] [blame] | 695 | SECStatus status = SECFailure; |
rsleevi | a3ad8d0 | 2016-06-07 18:22:33 | [diff] [blame] | 696 | base::FilePath database_dir = GetInitialConfigDirectory(); |
| 697 | if (!database_dir.empty()) { |
| 698 | // This duplicates the work which should have been done in |
| 699 | // EarlySetupForNSSInit. However, this function is idempotent so |
| 700 | // there's no harm done. |
| 701 | UseLocalCacheOfNSSDatabaseIfNFS(database_dir); |
davidben | 2e6b379 | 2015-08-11 18:18:58 | [diff] [blame] | 702 | |
rsleevi | a3ad8d0 | 2016-06-07 18:22:33 | [diff] [blame] | 703 | // Initialize with a persistent database (likely, ~/.pki/nssdb). |
| 704 | // Use "sql:" which can be shared by multiple processes safely. |
| 705 | std::string nss_config_dir = |
| 706 | base::StringPrintf("sql:%s", database_dir.value().c_str()); |
| 707 | #if defined(OS_CHROMEOS) |
| 708 | status = NSS_Init(nss_config_dir.c_str()); |
| 709 | #else |
| 710 | status = NSS_InitReadWrite(nss_config_dir.c_str()); |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 711 | #endif |
rsleevi | a3ad8d0 | 2016-06-07 18:22:33 | [diff] [blame] | 712 | if (status != SECSuccess) { |
| 713 | LOG(ERROR) << "Error initializing NSS with a persistent " |
| 714 | "database (" << nss_config_dir |
| 715 | << "): " << GetNSSErrorMessage(); |
| 716 | } |
| 717 | } |
| 718 | if (status != SECSuccess) { |
| 719 | VLOG(1) << "Initializing NSS without a persistent database."; |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 720 | status = NSS_NoDB_Init(nullptr); |
[email protected] | 897f520 | 2009-09-08 17:40:27 | [diff] [blame] | 721 | if (status != SECSuccess) { |
[email protected] | 007f512 | 2012-11-21 16:00:21 | [diff] [blame] | 722 | CrashOnNSSInitFailure(); |
[email protected] | 716fb11 | 2012-11-15 05:41:25 | [diff] [blame] | 723 | return; |
[email protected] | 897f520 | 2009-09-08 17:40:27 | [diff] [blame] | 724 | } |
[email protected] | 6e7e806 | 2009-04-13 17:35:09 | [diff] [blame] | 725 | } |
[email protected] | fa2d3dc | 2012-11-20 07:58:44 | [diff] [blame] | 726 | |
rsleevi | a3ad8d0 | 2016-06-07 18:22:33 | [diff] [blame] | 727 | PK11_SetPasswordFunc(PKCS11PasswordFunc); |
| 728 | |
| 729 | // If we haven't initialized the password for the NSS databases, |
| 730 | // initialize an empty-string password so that we don't need to |
| 731 | // log in. |
| 732 | PK11SlotInfo* slot = PK11_GetInternalKeySlot(); |
| 733 | if (slot) { |
| 734 | // PK11_InitPin may write to the keyDB, but no other thread can use NSS |
| 735 | // yet, so we don't need to lock. |
| 736 | if (PK11_NeedUserInit(slot)) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 737 | PK11_InitPin(slot, nullptr, nullptr); |
rsleevi | a3ad8d0 | 2016-06-07 18:22:33 | [diff] [blame] | 738 | PK11_FreeSlot(slot); |
| 739 | } |
| 740 | |
| 741 | root_ = InitDefaultRootCerts(); |
| 742 | |
[email protected] | fa2d3dc | 2012-11-20 07:58:44 | [diff] [blame] | 743 | // Disable MD5 certificate signatures. (They are disabled by default in |
| 744 | // NSS 3.14.) |
| 745 | NSS_SetAlgorithmPolicy(SEC_OID_MD5, 0, NSS_USE_ALG_IN_CERT_SIGNATURE); |
| 746 | NSS_SetAlgorithmPolicy(SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION, |
| 747 | 0, NSS_USE_ALG_IN_CERT_SIGNATURE); |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 748 | } |
| 749 | |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 750 | // NOTE(willchan): We don't actually execute this code since we leak NSS to |
| 751 | // prevent non-joinable threads from using NSS after it's already been shut |
| 752 | // down. |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 753 | ~NSSInitSingleton() { |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 754 | #if defined(OS_CHROMEOS) |
| 755 | STLDeleteValues(&chromeos_user_map_); |
| 756 | #endif |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 757 | tpm_slot_.reset(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 758 | if (root_) { |
| 759 | SECMOD_UnloadUserModule(root_); |
| 760 | SECMOD_DestroyModule(root_); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 761 | root_ = nullptr; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 762 | } |
[email protected] | 84e4772 | 2011-11-17 05:12:02 | [diff] [blame] | 763 | if (chaps_module_) { |
| 764 | SECMOD_UnloadUserModule(chaps_module_); |
| 765 | SECMOD_DestroyModule(chaps_module_); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 766 | chaps_module_ = nullptr; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 767 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 768 | |
[email protected] | c1444fe | 2008-09-17 09:42:51 | [diff] [blame] | 769 | SECStatus status = NSS_Shutdown(); |
[email protected] | f8adef5 | 2009-08-04 17:52:06 | [diff] [blame] | 770 | if (status != SECSuccess) { |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 771 | // We VLOG(1) because this failure is relatively harmless (leaking, but |
| 772 | // we're shutting down anyway). |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 773 | VLOG(1) << "NSS_Shutdown failed; see http://crbug.com/4609"; |
[email protected] | f8adef5 | 2009-08-04 17:52:06 | [diff] [blame] | 774 | } |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 775 | } |
[email protected] | c72f16a | 2009-03-19 16:02:31 | [diff] [blame] | 776 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 777 | // Load nss's built-in root certs. |
| 778 | SECMODModule* InitDefaultRootCerts() { |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 779 | SECMODModule* root = LoadModule("Root Certs", "libnssckbi.so", nullptr); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 780 | if (root) |
| 781 | return root; |
| 782 | |
| 783 | // Aw, snap. Can't find/load root cert shared library. |
| 784 | // This will make it hard to talk to anybody via https. |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 785 | // TODO(mattm): Re-add the NOTREACHED here when crbug.com/310972 is fixed. |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 786 | return nullptr; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // Load the given module for this NSS session. |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 790 | static SECMODModule* LoadModule(const char* name, |
| 791 | const char* library_path, |
| 792 | const char* params) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 793 | std::string modparams = base::StringPrintf( |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 794 | "name=\"%s\" library=\"%s\" %s", |
| 795 | name, library_path, params ? params : ""); |
| 796 | |
| 797 | // Shouldn't need to const_cast here, but SECMOD doesn't properly |
| 798 | // declare input string arguments as const. Bug |
| 799 | // https://bugzilla.mozilla.org/show_bug.cgi?id=642546 was filed |
| 800 | // on NSS codebase to address this. |
| 801 | SECMODModule* module = SECMOD_LoadUserModule( |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 802 | const_cast<char*>(modparams.c_str()), nullptr, PR_FALSE); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 803 | if (!module) { |
| 804 | LOG(ERROR) << "Error loading " << name << " module into NSS: " |
| 805 | << GetNSSErrorMessage(); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 806 | return nullptr; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 807 | } |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 808 | if (!module->loaded) { |
| 809 | LOG(ERROR) << "After loading " << name << ", loaded==false: " |
| 810 | << GetNSSErrorMessage(); |
| 811 | SECMOD_DestroyModule(module); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 812 | return nullptr; |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 813 | } |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 814 | return module; |
| 815 | } |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 816 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 817 | bool tpm_token_enabled_for_nss_; |
[email protected] | e2ea5ca | 2014-02-27 22:27:21 | [diff] [blame] | 818 | bool initializing_tpm_token_; |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 819 | typedef std::vector<base::Closure> TPMReadyCallbackList; |
| 820 | TPMReadyCallbackList tpm_ready_callback_list_; |
[email protected] | 84e4772 | 2011-11-17 05:12:02 | [diff] [blame] | 821 | SECMODModule* chaps_module_; |
[email protected] | 966669ae | 2014-07-30 21:03:45 | [diff] [blame] | 822 | crypto::ScopedPK11Slot tpm_slot_; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 823 | SECMODModule* root_; |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 824 | #if defined(OS_CHROMEOS) |
| 825 | typedef std::map<std::string, ChromeOSUserData*> ChromeOSUserMap; |
| 826 | ChromeOSUserMap chromeos_user_map_; |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 827 | ScopedPK11Slot test_system_slot_; |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 828 | #endif |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 829 | // TODO(davidben): When https://bugzilla.mozilla.org/show_bug.cgi?id=564011 |
| 830 | // is fixed, we will no longer need the lock. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 831 | base::Lock write_lock_; |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 832 | |
| 833 | base::ThreadChecker thread_checker_; |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 834 | }; |
| 835 | |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 836 | base::LazyInstance<NSSInitSingleton>::Leaky |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 837 | g_nss_singleton = LAZY_INSTANCE_INITIALIZER; |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 838 | } // namespace |
| 839 | |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 840 | ScopedPK11Slot OpenSoftwareNSSDB(const base::FilePath& path, |
| 841 | const std::string& description) { |
| 842 | const std::string modspec = |
| 843 | base::StringPrintf("configDir='sql:%s' tokenDescription='%s'", |
| 844 | path.value().c_str(), |
| 845 | description.c_str()); |
| 846 | PK11SlotInfo* db_slot = SECMOD_OpenUserDB(modspec.c_str()); |
| 847 | if (db_slot) { |
| 848 | if (PK11_NeedUserInit(db_slot)) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 849 | PK11_InitPin(db_slot, nullptr, nullptr); |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 850 | } else { |
| 851 | LOG(ERROR) << "Error opening persistent database (" << modspec |
| 852 | << "): " << GetNSSErrorMessage(); |
| 853 | } |
| 854 | return ScopedPK11Slot(db_slot); |
| 855 | } |
| 856 | |
[email protected] | ac3d597 | 2011-01-13 20:33:45 | [diff] [blame] | 857 | void EarlySetupForNSSInit() { |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 858 | base::FilePath database_dir = GetInitialConfigDirectory(); |
[email protected] | ac3d597 | 2011-01-13 20:33:45 | [diff] [blame] | 859 | if (!database_dir.empty()) |
| 860 | UseLocalCacheOfNSSDatabaseIfNFS(database_dir); |
| 861 | } |
[email protected] | ac3d597 | 2011-01-13 20:33:45 | [diff] [blame] | 862 | |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 863 | void EnsureNSPRInit() { |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 864 | g_nspr_singleton.Get(); |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 865 | } |
| 866 | |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 867 | void EnsureNSSInit() { |
[email protected] | a8e4b5a8 | 2010-10-27 00:05:47 | [diff] [blame] | 868 | // Initializing SSL causes us to do blocking IO. |
| 869 | // Temporarily allow it until we fix |
| 870 | // http://code.google.com/p/chromium/issues/detail?id=59847 |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 871 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 872 | g_nss_singleton.Get(); |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 873 | } |
| 874 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 875 | bool CheckNSSVersion(const char* version) { |
| 876 | return !!NSS_VersionCheck(version); |
| 877 | } |
| 878 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 879 | base::Lock* GetNSSWriteLock() { |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 880 | return g_nss_singleton.Get().write_lock(); |
[email protected] | 6913847 | 2010-06-25 22:44:48 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | AutoNSSWriteLock::AutoNSSWriteLock() : lock_(GetNSSWriteLock()) { |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 884 | // May be nullptr if the lock is not needed in our version of NSS. |
[email protected] | 6913847 | 2010-06-25 22:44:48 | [diff] [blame] | 885 | if (lock_) |
| 886 | lock_->Acquire(); |
| 887 | } |
| 888 | |
| 889 | AutoNSSWriteLock::~AutoNSSWriteLock() { |
| 890 | if (lock_) { |
| 891 | lock_->AssertAcquired(); |
| 892 | lock_->Release(); |
| 893 | } |
| 894 | } |
[email protected] | dd24ffc | 2011-06-08 19:46:42 | [diff] [blame] | 895 | |
| 896 | AutoSECMODListReadLock::AutoSECMODListReadLock() |
| 897 | : lock_(SECMOD_GetDefaultModuleListLock()) { |
| 898 | SECMOD_GetReadLock(lock_); |
| 899 | } |
| 900 | |
| 901 | AutoSECMODListReadLock::~AutoSECMODListReadLock() { |
| 902 | SECMOD_ReleaseReadLock(lock_); |
| 903 | } |
[email protected] | 6913847 | 2010-06-25 22:44:48 | [diff] [blame] | 904 | |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 905 | #if defined(OS_CHROMEOS) |
[email protected] | 8edd721 | 2014-07-30 12:24:29 | [diff] [blame] | 906 | ScopedPK11Slot GetSystemNSSKeySlot( |
| 907 | const base::Callback<void(ScopedPK11Slot)>& callback) { |
| 908 | return g_nss_singleton.Get().GetSystemNSSKeySlot(callback); |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 909 | } |
| 910 | |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 911 | void SetSystemKeySlotForTesting(ScopedPK11Slot slot) { |
dcheng | cf738a9 | 2015-12-31 16:11:45 | [diff] [blame] | 912 | g_nss_singleton.Get().SetSystemKeySlotForTesting(std::move(slot)); |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 913 | } |
| 914 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 915 | void EnableTPMTokenForNSS() { |
| 916 | g_nss_singleton.Get().EnableTPMTokenForNSS(); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 917 | } |
| 918 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 919 | bool IsTPMTokenEnabledForNSS() { |
| 920 | return g_nss_singleton.Get().IsTPMTokenEnabledForNSS(); |
| 921 | } |
| 922 | |
| 923 | bool IsTPMTokenReady(const base::Closure& callback) { |
| 924 | return g_nss_singleton.Get().IsTPMTokenReady(callback); |
[email protected] | 74beead | 2011-04-12 20:40:12 | [diff] [blame] | 925 | } |
[email protected] | c64b914 | 2011-04-19 18:49:54 | [diff] [blame] | 926 | |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 927 | void InitializeTPMTokenAndSystemSlot( |
| 928 | int token_slot_id, |
| 929 | const base::Callback<void(bool)>& callback) { |
| 930 | g_nss_singleton.Get().InitializeTPMTokenAndSystemSlot(token_slot_id, |
| 931 | callback); |
[email protected] | c175cdb | 2011-06-28 20:41:55 | [diff] [blame] | 932 | } |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 933 | |
pneubeck | fa32f2e | 2014-09-12 09:59:00 | [diff] [blame] | 934 | bool InitializeNSSForChromeOSUser(const std::string& username_hash, |
| 935 | const base::FilePath& path) { |
| 936 | return g_nss_singleton.Get().InitializeNSSForChromeOSUser(username_hash, |
| 937 | path); |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 938 | } |
[email protected] | 4071e6ac | 2014-07-12 12:46:17 | [diff] [blame] | 939 | |
| 940 | bool ShouldInitializeTPMForChromeOSUser(const std::string& username_hash) { |
| 941 | return g_nss_singleton.Get().ShouldInitializeTPMForChromeOSUser( |
| 942 | username_hash); |
| 943 | } |
| 944 | |
| 945 | void WillInitializeTPMForChromeOSUser(const std::string& username_hash) { |
| 946 | g_nss_singleton.Get().WillInitializeTPMForChromeOSUser(username_hash); |
| 947 | } |
| 948 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 949 | void InitializeTPMForChromeOSUser( |
| 950 | const std::string& username_hash, |
| 951 | CK_SLOT_ID slot_id) { |
| 952 | g_nss_singleton.Get().InitializeTPMForChromeOSUser(username_hash, slot_id); |
| 953 | } |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 954 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 955 | void InitializePrivateSoftwareSlotForChromeOSUser( |
| 956 | const std::string& username_hash) { |
| 957 | g_nss_singleton.Get().InitializePrivateSoftwareSlotForChromeOSUser( |
| 958 | username_hash); |
| 959 | } |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 960 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 961 | ScopedPK11Slot GetPublicSlotForChromeOSUser(const std::string& username_hash) { |
| 962 | return g_nss_singleton.Get().GetPublicSlotForChromeOSUser(username_hash); |
| 963 | } |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 964 | |
[email protected] | 557737f7 | 2013-12-06 22:24:07 | [diff] [blame] | 965 | ScopedPK11Slot GetPrivateSlotForChromeOSUser( |
| 966 | const std::string& username_hash, |
| 967 | const base::Callback<void(ScopedPK11Slot)>& callback) { |
| 968 | return g_nss_singleton.Get().GetPrivateSlotForChromeOSUser(username_hash, |
| 969 | callback); |
| 970 | } |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 971 | |
| 972 | void CloseChromeOSUserForTesting(const std::string& username_hash) { |
| 973 | g_nss_singleton.Get().CloseChromeOSUserForTesting(username_hash); |
| 974 | } |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 975 | #endif // defined(OS_CHROMEOS) |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 976 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 977 | base::Time PRTimeToBaseTime(PRTime prtime) { |
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 978 | return base::Time::FromInternalValue( |
| 979 | prtime + base::Time::UnixEpoch().ToInternalValue()); |
| 980 | } |
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 981 | |
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 982 | PRTime BaseTimeToPRTime(base::Time time) { |
| 983 | return time.ToInternalValue() - base::Time::UnixEpoch().ToInternalValue(); |
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 984 | } |
| 985 | |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 986 | #if !defined(OS_CHROMEOS) |
[email protected] | 49631886 | 2014-07-13 07:19:00 | [diff] [blame] | 987 | PK11SlotInfo* GetPersistentNSSKeySlot() { |
| 988 | return g_nss_singleton.Get().GetPersistentNSSKeySlot(); |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 989 | } |
[email protected] | 190933f | 2014-07-28 09:56:51 | [diff] [blame] | 990 | #endif |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 991 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 992 | } // namespace crypto |