[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" |
| 6 | #include "crypto/nss_util_internal.h" |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 7 | |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 8 | #include <nss.h> |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 9 | #include <pk11pub.h> |
[email protected] | c72f16a | 2009-03-19 16:02:31 | [diff] [blame] | 10 | #include <plarena.h> |
[email protected] | 6e7e806 | 2009-04-13 17:35:09 | [diff] [blame] | 11 | #include <prerror.h> |
[email protected] | c72f16a | 2009-03-19 16:02:31 | [diff] [blame] | 12 | #include <prinit.h> |
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 13 | #include <prtime.h> |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 14 | #include <secmod.h> |
[email protected] | b43c97c | 2008-10-22 19:50:58 | [diff] [blame] | 15 | |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 16 | #if defined(OS_LINUX) |
[email protected] | 1f59edcb | 2010-11-04 00:32:25 | [diff] [blame] | 17 | #include <linux/nfs_fs.h> |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 18 | #include <sys/vfs.h> |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 19 | #elif defined(OS_OPENBSD) |
| 20 | #include <sys/mount.h> |
| 21 | #include <sys/param.h> |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 22 | #endif |
| 23 | |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
[email protected] | f3d445e | 2013-11-22 18:35:03 | [diff] [blame] | 26 | #include "base/cpu.h" |
[email protected] | 716fb11 | 2012-11-15 05:41:25 | [diff] [blame] | 27 | #include "base/debug/alias.h" |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 28 | #include "base/debug/stack_trace.h" |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 29 | #include "base/environment.h" |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 30 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 31 | #include "base/files/file_path.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 32 | #include "base/files/scoped_temp_dir.h" |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 33 | #include "base/lazy_instance.h" |
[email protected] | c1444fe | 2008-09-17 09:42:51 | [diff] [blame] | 34 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 35 | #include "base/memory/scoped_ptr.h" |
[email protected] | 68bf448 | 2013-01-22 16:39:34 | [diff] [blame] | 36 | #include "base/metrics/histogram.h" |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 37 | #include "base/native_library.h" |
[email protected] | 0d8db08 | 2013-06-11 07:27:01 | [diff] [blame] | 38 | #include "base/strings/stringprintf.h" |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 39 | #include "base/threading/thread_checker.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 40 | #include "base/threading/thread_restrictions.h" |
[email protected] | 26661c2 | 2011-10-07 01:33:58 | [diff] [blame] | 41 | #include "build/build_config.h" |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 42 | |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 43 | // USE_NSS means we use NSS for everything crypto-related. If USE_NSS is not |
| 44 | // defined, such as on Mac and Windows, we use NSS for SSL only -- we don't |
| 45 | // use NSS for crypto or certificate verification, and we don't use the NSS |
| 46 | // certificate and key databases. |
[email protected] | 6913847 | 2010-06-25 22:44:48 | [diff] [blame] | 47 | #if defined(USE_NSS) |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 48 | #include "base/synchronization/lock.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 49 | #include "crypto/crypto_module_blocking_password_delegate.h" |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 50 | #endif // defined(USE_NSS) |
[email protected] | abd4aba | 2010-01-27 19:36:22 | [diff] [blame] | 51 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 52 | namespace crypto { |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 53 | |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 54 | namespace { |
| 55 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 56 | #if defined(OS_CHROMEOS) |
| 57 | const char kNSSDatabaseName[] = "Real NSS database"; |
| 58 | |
[email protected] | 84e4772 | 2011-11-17 05:12:02 | [diff] [blame] | 59 | // Constants for loading the Chrome OS TPM-backed PKCS #11 library. |
| 60 | const char kChapsModuleName[] = "Chaps"; |
| 61 | const char kChapsPath[] = "libchaps.so"; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 62 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 63 | // Fake certificate authority database used for testing. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 64 | static const base::FilePath::CharType kReadOnlyCertDB[] = |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 65 | FILE_PATH_LITERAL("/etc/fake_root_ca/nssdb"); |
| 66 | #endif // defined(OS_CHROMEOS) |
| 67 | |
| 68 | std::string GetNSSErrorMessage() { |
| 69 | std::string result; |
| 70 | if (PR_GetErrorTextLength()) { |
[email protected] | e832895 | 2013-04-09 17:35:42 | [diff] [blame] | 71 | scoped_ptr<char[]> error_text(new char[PR_GetErrorTextLength() + 1]); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 72 | PRInt32 copied = PR_GetErrorText(error_text.get()); |
| 73 | result = std::string(error_text.get(), copied); |
| 74 | } else { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 75 | result = base::StringPrintf("NSS error code: %d", PR_GetError()); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 76 | } |
| 77 | return result; |
| 78 | } |
| 79 | |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 80 | #if defined(USE_NSS) |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 81 | base::FilePath GetDefaultConfigDirectory() { |
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 82 | base::FilePath dir = base::GetHomeDir(); |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 83 | if (dir.empty()) { |
| 84 | LOG(ERROR) << "Failed to get home directory."; |
| 85 | return dir; |
[email protected] | 8691334 | 2009-05-25 02:14:34 | [diff] [blame] | 86 | } |
[email protected] | 8691334 | 2009-05-25 02:14:34 | [diff] [blame] | 87 | dir = dir.AppendASCII(".pki").AppendASCII("nssdb"); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame^] | 88 | if (!base::CreateDirectory(dir)) { |
[email protected] | 948a707b | 2011-06-07 22:51:44 | [diff] [blame] | 89 | LOG(ERROR) << "Failed to create " << dir.value() << " directory."; |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 90 | dir.clear(); |
[email protected] | 8691334 | 2009-05-25 02:14:34 | [diff] [blame] | 91 | } |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 92 | return dir; |
[email protected] | 8691334 | 2009-05-25 02:14:34 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 259c42f | 2013-09-12 20:32:22 | [diff] [blame] | 95 | // On non-Chrome OS platforms, return the default config directory. On Chrome OS |
| 96 | // test images, return a read-only directory with fake root CA certs (which are |
| 97 | // used by the local Google Accounts server mock we use when testing our login |
| 98 | // code). On Chrome OS non-test images (where the read-only directory doesn't |
| 99 | // exist), return an empty path. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 100 | base::FilePath GetInitialConfigDirectory() { |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 101 | #if defined(OS_CHROMEOS) |
[email protected] | 259c42f | 2013-09-12 20:32:22 | [diff] [blame] | 102 | base::FilePath database_dir = base::FilePath(kReadOnlyCertDB); |
| 103 | if (!base::PathExists(database_dir)) |
| 104 | database_dir.clear(); |
| 105 | return database_dir; |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 106 | #else |
| 107 | return GetDefaultConfigDirectory(); |
| 108 | #endif // defined(OS_CHROMEOS) |
| 109 | } |
| 110 | |
[email protected] | 88b9db7 | 2011-01-13 01:48:43 | [diff] [blame] | 111 | // This callback for NSS forwards all requests to a caller-specified |
[email protected] | 3f1f841 | 2011-01-19 03:01:23 | [diff] [blame] | 112 | // CryptoModuleBlockingPasswordDelegate object. |
| 113 | char* PKCS11PasswordFunc(PK11SlotInfo* slot, PRBool retry, void* arg) { |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 114 | crypto::CryptoModuleBlockingPasswordDelegate* delegate = |
| 115 | reinterpret_cast<crypto::CryptoModuleBlockingPasswordDelegate*>(arg); |
[email protected] | 88b9db7 | 2011-01-13 01:48:43 | [diff] [blame] | 116 | if (delegate) { |
| 117 | bool cancelled = false; |
| 118 | std::string password = delegate->RequestPassword(PK11_GetTokenName(slot), |
| 119 | retry != PR_FALSE, |
| 120 | &cancelled); |
| 121 | if (cancelled) |
| 122 | return NULL; |
| 123 | char* result = PORT_Strdup(password.c_str()); |
| 124 | password.replace(0, password.size(), password.size(), 0); |
| 125 | return result; |
| 126 | } |
| 127 | DLOG(ERROR) << "PK11 password requested with NULL arg"; |
| 128 | return NULL; |
| 129 | } |
| 130 | |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 131 | // NSS creates a local cache of the sqlite database if it detects that the |
| 132 | // filesystem the database is on is much slower than the local disk. The |
| 133 | // detection doesn't work with the latest versions of sqlite, such as 3.6.22 |
| 134 | // (NSS bug https://bugzilla.mozilla.org/show_bug.cgi?id=578561). So we set |
| 135 | // the NSS environment variable NSS_SDB_USE_CACHE to "yes" to override NSS's |
| 136 | // detection when database_dir is on NFS. See http://crbug.com/48585. |
| 137 | // |
| 138 | // TODO(wtc): port this function to other USE_NSS platforms. It is defined |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 139 | // only for OS_LINUX and OS_OPENBSD simply because the statfs structure |
| 140 | // is OS-specific. |
[email protected] | ac3d597 | 2011-01-13 20:33:45 | [diff] [blame] | 141 | // |
| 142 | // Because this function sets an environment variable it must be run before we |
| 143 | // go multi-threaded. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 144 | void UseLocalCacheOfNSSDatabaseIfNFS(const base::FilePath& database_dir) { |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 145 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 146 | struct statfs buf; |
| 147 | if (statfs(database_dir.value().c_str(), &buf) == 0) { |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 148 | #if defined(OS_LINUX) |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 149 | if (buf.f_type == NFS_SUPER_MAGIC) { |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 150 | #elif defined(OS_OPENBSD) |
| 151 | if (strcmp(buf.f_fstypename, MOUNT_NFS) == 0) { |
| 152 | #endif |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 153 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 154 | const char* use_cache_env_var = "NSS_SDB_USE_CACHE"; |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 155 | if (!env->HasVar(use_cache_env_var)) |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 156 | env->SetVar(use_cache_env_var, "yes"); |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 157 | } |
| 158 | } |
[email protected] | d816516f | 2011-10-25 19:11:59 | [diff] [blame] | 159 | #endif // defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 162 | #endif // defined(USE_NSS) |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 163 | |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 164 | // A singleton to initialize/deinitialize NSPR. |
| 165 | // Separate from the NSS singleton because we initialize NSPR on the UI thread. |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 166 | // Now that we're leaking the singleton, we could merge back with the NSS |
| 167 | // singleton. |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 168 | class NSPRInitSingleton { |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 169 | private: |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 170 | friend struct base::DefaultLazyInstanceTraits<NSPRInitSingleton>; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 171 | |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 172 | NSPRInitSingleton() { |
| 173 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| 174 | } |
| 175 | |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 176 | // NOTE(willchan): We don't actually execute this code since we leak NSS to |
| 177 | // prevent non-joinable threads from using NSS after it's already been shut |
| 178 | // down. |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 179 | ~NSPRInitSingleton() { |
[email protected] | 829296f | 2010-01-27 02:58:03 | [diff] [blame] | 180 | PL_ArenaFinish(); |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 181 | PRStatus prstatus = PR_Cleanup(); |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 182 | if (prstatus != PR_SUCCESS) |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 183 | LOG(ERROR) << "PR_Cleanup failed; was NSPR initialized on wrong thread?"; |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 184 | } |
| 185 | }; |
| 186 | |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 187 | base::LazyInstance<NSPRInitSingleton>::Leaky |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 188 | g_nspr_singleton = LAZY_INSTANCE_INITIALIZER; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 189 | |
[email protected] | 0274629 | 2012-01-25 04:37:51 | [diff] [blame] | 190 | // This is a LazyInstance so that it will be deleted automatically when the |
| 191 | // unittest exits. NSSInitSingleton is a LeakySingleton, so it would not be |
| 192 | // deleted if it were a regular member. |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 193 | base::LazyInstance<base::ScopedTempDir> g_test_nss_db_dir = |
| 194 | LAZY_INSTANCE_INITIALIZER; |
[email protected] | 0274629 | 2012-01-25 04:37:51 | [diff] [blame] | 195 | |
[email protected] | 007f512 | 2012-11-21 16:00:21 | [diff] [blame] | 196 | // Force a crash with error info on NSS_NoDB_Init failure. |
| 197 | void CrashOnNSSInitFailure() { |
| 198 | int nss_error = PR_GetError(); |
| 199 | int os_error = PR_GetOSError(); |
[email protected] | 716fb11 | 2012-11-15 05:41:25 | [diff] [blame] | 200 | base::debug::Alias(&nss_error); |
| 201 | base::debug::Alias(&os_error); |
[email protected] | 007f512 | 2012-11-21 16:00:21 | [diff] [blame] | 202 | LOG(ERROR) << "Error initializing NSS without a persistent database: " |
| 203 | << GetNSSErrorMessage(); |
[email protected] | 2b12459a | 2012-11-16 03:45:32 | [diff] [blame] | 204 | LOG(FATAL) << "nss_error=" << nss_error << ", os_error=" << os_error; |
[email protected] | 716fb11 | 2012-11-15 05:41:25 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 207 | class NSSInitSingleton { |
| 208 | public: |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 209 | #if defined(OS_CHROMEOS) |
| 210 | void OpenPersistentNSSDB() { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 211 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 212 | |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 213 | if (!chromeos_user_logged_in_) { |
[email protected] | 61f9321 | 2011-01-24 14:37:08 | [diff] [blame] | 214 | // GetDefaultConfigDirectory causes us to do blocking IO on UI thread. |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 215 | // Temporarily allow it until we fix http://crbug.com/70119 |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 216 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 217 | chromeos_user_logged_in_ = true; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 218 | |
| 219 | // This creates another DB slot in NSS that is read/write, unlike |
| 220 | // the fake root CA cert DB and the "default" crypto key |
| 221 | // provider, which are still read-only (because we initialized |
| 222 | // NSS before we had a cryptohome mounted). |
| 223 | software_slot_ = OpenUserDB(GetDefaultConfigDirectory(), |
[email protected] | 74beead | 2011-04-12 20:40:12 | [diff] [blame] | 224 | kNSSDatabaseName); |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 225 | } |
| 226 | } |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 227 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 228 | void EnableTPMTokenForNSS() { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 229 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 230 | |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 231 | // If this gets set, then we'll use the TPM for certs with |
| 232 | // private keys, otherwise we'll fall back to the software |
| 233 | // implementation. |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 234 | tpm_token_enabled_for_nss_ = true; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | bee9b54 | 2013-11-25 21:45:04 | [diff] [blame] | 237 | bool InitializeTPMToken(int token_slot_id) { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 238 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 239 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 240 | // If EnableTPMTokenForNSS hasn't been called, return false. |
| 241 | if (!tpm_token_enabled_for_nss_) |
| 242 | return false; |
[email protected] | 14172c8 | 2012-02-28 10:34:21 | [diff] [blame] | 243 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 244 | // If everything is already initialized, then return true. |
| 245 | if (chaps_module_ && tpm_slot_) |
| 246 | return true; |
| 247 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 248 | // This tries to load the Chaps module so NSS can talk to the hardware |
| 249 | // TPM. |
| 250 | if (!chaps_module_) { |
| 251 | chaps_module_ = LoadModule( |
| 252 | kChapsModuleName, |
| 253 | kChapsPath, |
| 254 | // For more details on these parameters, see: |
| 255 | // https://developer.mozilla.org/en/PKCS11_Module_Specs |
| 256 | // slotFlags=[PublicCerts] -- Certificates and public keys can be |
| 257 | // read from this slot without requiring a call to C_Login. |
| 258 | // askpw=only -- Only authenticate to the token when necessary. |
| 259 | "NSS=\"slotParams=(0={slotFlags=[PublicCerts] askpw=only})\""); |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 260 | if (!chaps_module_ && test_slot_) { |
| 261 | // chromeos_unittests try to test the TPM initialization process. If we |
| 262 | // have a test DB open, pretend that it is the TPM slot. |
| 263 | tpm_slot_ = PK11_ReferenceSlot(test_slot_); |
| 264 | return true; |
| 265 | } |
[email protected] | 14172c8 | 2012-02-28 10:34:21 | [diff] [blame] | 266 | } |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 267 | if (chaps_module_){ |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 268 | tpm_slot_ = GetTPMSlotForId(token_slot_id); |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 269 | |
| 270 | return tpm_slot_ != NULL; |
| 271 | } |
| 272 | return false; |
[email protected] | c175cdb | 2011-06-28 20:41:55 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | c64b914 | 2011-04-19 18:49:54 | [diff] [blame] | 275 | bool IsTPMTokenReady() { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 276 | // TODO(mattm): Change to DCHECK when callers have been fixed. |
| 277 | if (!thread_checker_.CalledOnValidThread()) { |
| 278 | DVLOG(1) << "Called on wrong thread.\n" |
| 279 | << base::debug::StackTrace().ToString(); |
| 280 | } |
| 281 | |
[email protected] | c64b914 | 2011-04-19 18:49:54 | [diff] [blame] | 282 | return tpm_slot_ != NULL; |
[email protected] | 74beead | 2011-04-12 20:40:12 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 285 | // Note that CK_SLOT_ID is an unsigned long, but cryptohome gives us the slot |
| 286 | // id as an int. This should be safe since this is only used with chaps, which |
| 287 | // we also control. |
| 288 | PK11SlotInfo* GetTPMSlotForId(CK_SLOT_ID slot_id) { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 289 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 290 | |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 291 | if (!chaps_module_) |
| 292 | return NULL; |
| 293 | |
| 294 | VLOG(1) << "Poking chaps module."; |
| 295 | SECStatus rv = SECMOD_UpdateSlotList(chaps_module_); |
| 296 | if (rv != SECSuccess) |
| 297 | PLOG(ERROR) << "SECMOD_UpdateSlotList failed: " << PORT_GetError(); |
| 298 | |
| 299 | PK11SlotInfo* slot = SECMOD_LookupSlot(chaps_module_->moduleID, slot_id); |
| 300 | if (!slot) |
| 301 | LOG(ERROR) << "TPM slot " << slot_id << " not found."; |
| 302 | return slot; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 303 | } |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 304 | #endif // defined(OS_CHROMEOS) |
| 305 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 306 | |
[email protected] | 0274629 | 2012-01-25 04:37:51 | [diff] [blame] | 307 | bool OpenTestNSSDB() { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 308 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 309 | |
[email protected] | 0274629 | 2012-01-25 04:37:51 | [diff] [blame] | 310 | if (test_slot_) |
| 311 | return true; |
| 312 | if (!g_test_nss_db_dir.Get().CreateUniqueTempDir()) |
| 313 | return false; |
[email protected] | b17bc366 | 2013-08-12 14:09:19 | [diff] [blame] | 314 | test_slot_ = OpenUserDB(g_test_nss_db_dir.Get().path(), kTestTPMTokenName); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 315 | return !!test_slot_; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void CloseTestNSSDB() { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 319 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 320 | |
[email protected] | 166fac0d | 2013-06-29 21:18:16 | [diff] [blame] | 321 | if (!test_slot_) |
| 322 | return; |
| 323 | SECStatus status = SECMOD_CloseUserDB(test_slot_); |
| 324 | if (status != SECSuccess) |
| 325 | PLOG(ERROR) << "SECMOD_CloseUserDB failed: " << PORT_GetError(); |
| 326 | PK11_FreeSlot(test_slot_); |
| 327 | test_slot_ = NULL; |
| 328 | ignore_result(g_test_nss_db_dir.Get().Delete()); |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 331 | PK11SlotInfo* GetPublicNSSKeySlot() { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 332 | // TODO(mattm): Change to DCHECK when callers have been fixed. |
| 333 | if (!thread_checker_.CalledOnValidThread()) { |
| 334 | DVLOG(1) << "Called on wrong thread.\n" |
| 335 | << base::debug::StackTrace().ToString(); |
| 336 | } |
| 337 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 338 | if (test_slot_) |
| 339 | return PK11_ReferenceSlot(test_slot_); |
| 340 | if (software_slot_) |
| 341 | return PK11_ReferenceSlot(software_slot_); |
| 342 | return PK11_GetInternalKeySlot(); |
| 343 | } |
| 344 | |
| 345 | PK11SlotInfo* GetPrivateNSSKeySlot() { |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 346 | // TODO(mattm): Change to DCHECK when callers have been fixed. |
| 347 | if (!thread_checker_.CalledOnValidThread()) { |
| 348 | DVLOG(1) << "Called on wrong thread.\n" |
| 349 | << base::debug::StackTrace().ToString(); |
| 350 | } |
| 351 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 352 | if (test_slot_) |
| 353 | return PK11_ReferenceSlot(test_slot_); |
[email protected] | c64b914 | 2011-04-19 18:49:54 | [diff] [blame] | 354 | |
| 355 | #if defined(OS_CHROMEOS) |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 356 | if (tpm_token_enabled_for_nss_) { |
[email protected] | 20ba9d8 | 2012-02-15 04:17:00 | [diff] [blame] | 357 | if (IsTPMTokenReady()) { |
[email protected] | c64b914 | 2011-04-19 18:49:54 | [diff] [blame] | 358 | return PK11_ReferenceSlot(tpm_slot_); |
| 359 | } else { |
| 360 | // If we were supposed to get the hardware token, but were |
| 361 | // unable to, return NULL rather than fall back to sofware. |
| 362 | return NULL; |
| 363 | } |
| 364 | } |
| 365 | #endif |
| 366 | // If we weren't supposed to enable the TPM for NSS, then return |
| 367 | // the software slot. |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 368 | if (software_slot_) |
| 369 | return PK11_ReferenceSlot(software_slot_); |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 370 | return PK11_GetInternalKeySlot(); |
| 371 | } |
| 372 | |
| 373 | #if defined(USE_NSS) |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 374 | base::Lock* write_lock() { |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 375 | return &write_lock_; |
| 376 | } |
| 377 | #endif // defined(USE_NSS) |
| 378 | |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 379 | // This method is used to force NSS to be initialized without a DB. |
| 380 | // Call this method before NSSInitSingleton() is constructed. |
| 381 | static void ForceNoDBInit() { |
| 382 | force_nodb_init_ = true; |
| 383 | } |
| 384 | |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 385 | private: |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 386 | friend struct base::DefaultLazyInstanceTraits<NSSInitSingleton>; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 387 | |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 388 | NSSInitSingleton() |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 389 | : tpm_token_enabled_for_nss_(false), |
| 390 | chaps_module_(NULL), |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 391 | software_slot_(NULL), |
| 392 | test_slot_(NULL), |
| 393 | tpm_slot_(NULL), |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 394 | root_(NULL), |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 395 | chromeos_user_logged_in_(false) { |
[email protected] | 68bf448 | 2013-01-22 16:39:34 | [diff] [blame] | 396 | base::TimeTicks start_time = base::TimeTicks::Now(); |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 397 | |
| 398 | // It's safe to construct on any thread, since LazyInstance will prevent any |
| 399 | // other threads from accessing until the constructor is done. |
| 400 | thread_checker_.DetachFromThread(); |
| 401 | |
[email protected] | f3d445e | 2013-11-22 18:35:03 | [diff] [blame] | 402 | DisableAESNIIfNeeded(); |
| 403 | |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 404 | EnsureNSPRInit(); |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 405 | |
[email protected] | 805acdc | 2013-08-07 22:57:00 | [diff] [blame] | 406 | // We *must* have NSS >= 3.14.3. |
[email protected] | dc36c9c | 2010-01-20 20:45:00 | [diff] [blame] | 407 | COMPILE_ASSERT( |
[email protected] | 805acdc | 2013-08-07 22:57:00 | [diff] [blame] | 408 | (NSS_VMAJOR == 3 && NSS_VMINOR == 14 && NSS_VPATCH >= 3) || |
| 409 | (NSS_VMAJOR == 3 && NSS_VMINOR > 14) || |
[email protected] | dc36c9c | 2010-01-20 20:45:00 | [diff] [blame] | 410 | (NSS_VMAJOR > 3), |
| 411 | nss_version_check_failed); |
| 412 | // Also check the run-time NSS version. |
| 413 | // NSS_VersionCheck is a >= check, not strict equality. |
[email protected] | 805acdc | 2013-08-07 22:57:00 | [diff] [blame] | 414 | if (!NSS_VersionCheck("3.14.3")) { |
| 415 | LOG(FATAL) << "NSS_VersionCheck(\"3.14.3\") failed. NSS >= 3.14.3 is " |
| 416 | "required. Please upgrade to the latest NSS, and if you " |
[email protected] | 1b8082d | 2010-02-19 12:21:48 | [diff] [blame] | 417 | "still get this error, contact your distribution " |
| 418 | "maintainer."; |
| 419 | } |
[email protected] | dc36c9c | 2010-01-20 20:45:00 | [diff] [blame] | 420 | |
[email protected] | 897f520 | 2009-09-08 17:40:27 | [diff] [blame] | 421 | SECStatus status = SECFailure; |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 422 | bool nodb_init = force_nodb_init_; |
| 423 | |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 424 | #if !defined(USE_NSS) |
[email protected] | abd4aba | 2010-01-27 19:36:22 | [diff] [blame] | 425 | // Use the system certificate store, so initialize NSS without database. |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 426 | nodb_init = true; |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 427 | #endif |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 428 | |
| 429 | if (nodb_init) { |
[email protected] | 897f520 | 2009-09-08 17:40:27 | [diff] [blame] | 430 | status = NSS_NoDB_Init(NULL); |
| 431 | if (status != SECSuccess) { |
[email protected] | 007f512 | 2012-11-21 16:00:21 | [diff] [blame] | 432 | CrashOnNSSInitFailure(); |
[email protected] | 716fb11 | 2012-11-15 05:41:25 | [diff] [blame] | 433 | return; |
[email protected] | 897f520 | 2009-09-08 17:40:27 | [diff] [blame] | 434 | } |
[email protected] | a88f87e | 2012-08-14 19:46:28 | [diff] [blame] | 435 | #if defined(OS_IOS) |
| 436 | root_ = InitDefaultRootCerts(); |
| 437 | #endif // defined(OS_IOS) |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 438 | } else { |
| 439 | #if defined(USE_NSS) |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 440 | base::FilePath database_dir = GetInitialConfigDirectory(); |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 441 | if (!database_dir.empty()) { |
| 442 | // This duplicates the work which should have been done in |
| 443 | // EarlySetupForNSSInit. However, this function is idempotent so |
| 444 | // there's no harm done. |
| 445 | UseLocalCacheOfNSSDatabaseIfNFS(database_dir); |
| 446 | |
| 447 | // Initialize with a persistent database (likely, ~/.pki/nssdb). |
| 448 | // Use "sql:" which can be shared by multiple processes safely. |
| 449 | std::string nss_config_dir = |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 450 | base::StringPrintf("sql:%s", database_dir.value().c_str()); |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 451 | #if defined(OS_CHROMEOS) |
| 452 | status = NSS_Init(nss_config_dir.c_str()); |
| 453 | #else |
| 454 | status = NSS_InitReadWrite(nss_config_dir.c_str()); |
| 455 | #endif |
| 456 | if (status != SECSuccess) { |
| 457 | LOG(ERROR) << "Error initializing NSS with a persistent " |
| 458 | "database (" << nss_config_dir |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 459 | << "): " << GetNSSErrorMessage(); |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | if (status != SECSuccess) { |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 463 | VLOG(1) << "Initializing NSS without a persistent database."; |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 464 | status = NSS_NoDB_Init(NULL); |
| 465 | if (status != SECSuccess) { |
[email protected] | 007f512 | 2012-11-21 16:00:21 | [diff] [blame] | 466 | CrashOnNSSInitFailure(); |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 467 | return; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | PK11_SetPasswordFunc(PKCS11PasswordFunc); |
| 472 | |
| 473 | // If we haven't initialized the password for the NSS databases, |
| 474 | // initialize an empty-string password so that we don't need to |
| 475 | // log in. |
| 476 | PK11SlotInfo* slot = PK11_GetInternalKeySlot(); |
| 477 | if (slot) { |
| 478 | // PK11_InitPin may write to the keyDB, but no other thread can use NSS |
| 479 | // yet, so we don't need to lock. |
| 480 | if (PK11_NeedUserInit(slot)) |
| 481 | PK11_InitPin(slot, NULL, NULL); |
| 482 | PK11_FreeSlot(slot); |
| 483 | } |
| 484 | |
| 485 | root_ = InitDefaultRootCerts(); |
| 486 | #endif // defined(USE_NSS) |
[email protected] | 6e7e806 | 2009-04-13 17:35:09 | [diff] [blame] | 487 | } |
[email protected] | fa2d3dc | 2012-11-20 07:58:44 | [diff] [blame] | 488 | |
[email protected] | fa2d3dc | 2012-11-20 07:58:44 | [diff] [blame] | 489 | // Disable MD5 certificate signatures. (They are disabled by default in |
| 490 | // NSS 3.14.) |
| 491 | NSS_SetAlgorithmPolicy(SEC_OID_MD5, 0, NSS_USE_ALG_IN_CERT_SIGNATURE); |
| 492 | NSS_SetAlgorithmPolicy(SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION, |
| 493 | 0, NSS_USE_ALG_IN_CERT_SIGNATURE); |
[email protected] | 68bf448 | 2013-01-22 16:39:34 | [diff] [blame] | 494 | |
| 495 | // The UMA bit is conditionally set for this histogram in |
| 496 | // chrome/common/startup_metric_utils.cc . |
| 497 | HISTOGRAM_CUSTOM_TIMES("Startup.SlowStartupNSSInit", |
| 498 | base::TimeTicks::Now() - start_time, |
| 499 | base::TimeDelta::FromMilliseconds(10), |
| 500 | base::TimeDelta::FromHours(1), |
| 501 | 50); |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 502 | } |
| 503 | |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 504 | // NOTE(willchan): We don't actually execute this code since we leak NSS to |
| 505 | // prevent non-joinable threads from using NSS after it's already been shut |
| 506 | // down. |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 507 | ~NSSInitSingleton() { |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 508 | if (tpm_slot_) { |
| 509 | PK11_FreeSlot(tpm_slot_); |
| 510 | tpm_slot_ = NULL; |
| 511 | } |
| 512 | if (software_slot_) { |
| 513 | SECMOD_CloseUserDB(software_slot_); |
| 514 | PK11_FreeSlot(software_slot_); |
| 515 | software_slot_ = NULL; |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 516 | } |
[email protected] | bb639038 | 2010-08-12 19:49:40 | [diff] [blame] | 517 | CloseTestNSSDB(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 518 | if (root_) { |
| 519 | SECMOD_UnloadUserModule(root_); |
| 520 | SECMOD_DestroyModule(root_); |
| 521 | root_ = NULL; |
| 522 | } |
[email protected] | 84e4772 | 2011-11-17 05:12:02 | [diff] [blame] | 523 | if (chaps_module_) { |
| 524 | SECMOD_UnloadUserModule(chaps_module_); |
| 525 | SECMOD_DestroyModule(chaps_module_); |
| 526 | chaps_module_ = NULL; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 527 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 528 | |
[email protected] | c1444fe | 2008-09-17 09:42:51 | [diff] [blame] | 529 | SECStatus status = NSS_Shutdown(); |
[email protected] | f8adef5 | 2009-08-04 17:52:06 | [diff] [blame] | 530 | if (status != SECSuccess) { |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 531 | // We VLOG(1) because this failure is relatively harmless (leaking, but |
| 532 | // we're shutting down anyway). |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 533 | VLOG(1) << "NSS_Shutdown failed; see http://crbug.com/4609"; |
[email protected] | f8adef5 | 2009-08-04 17:52:06 | [diff] [blame] | 534 | } |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 535 | } |
[email protected] | c72f16a | 2009-03-19 16:02:31 | [diff] [blame] | 536 | |
[email protected] | a88f87e | 2012-08-14 19:46:28 | [diff] [blame] | 537 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 538 | // Load nss's built-in root certs. |
| 539 | SECMODModule* InitDefaultRootCerts() { |
| 540 | SECMODModule* root = LoadModule("Root Certs", "libnssckbi.so", NULL); |
| 541 | if (root) |
| 542 | return root; |
| 543 | |
| 544 | // Aw, snap. Can't find/load root cert shared library. |
| 545 | // This will make it hard to talk to anybody via https. |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 546 | // TODO(mattm): Re-add the NOTREACHED here when crbug.com/310972 is fixed. |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | // Load the given module for this NSS session. |
| 551 | SECMODModule* LoadModule(const char* name, |
| 552 | const char* library_path, |
| 553 | const char* params) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 554 | std::string modparams = base::StringPrintf( |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 555 | "name=\"%s\" library=\"%s\" %s", |
| 556 | name, library_path, params ? params : ""); |
| 557 | |
| 558 | // Shouldn't need to const_cast here, but SECMOD doesn't properly |
| 559 | // declare input string arguments as const. Bug |
| 560 | // https://bugzilla.mozilla.org/show_bug.cgi?id=642546 was filed |
| 561 | // on NSS codebase to address this. |
| 562 | SECMODModule* module = SECMOD_LoadUserModule( |
| 563 | const_cast<char*>(modparams.c_str()), NULL, PR_FALSE); |
| 564 | if (!module) { |
| 565 | LOG(ERROR) << "Error loading " << name << " module into NSS: " |
| 566 | << GetNSSErrorMessage(); |
| 567 | return NULL; |
| 568 | } |
[email protected] | 3f3b9b1 | 2013-10-25 22:03:26 | [diff] [blame] | 569 | if (!module->loaded) { |
| 570 | LOG(ERROR) << "After loading " << name << ", loaded==false: " |
| 571 | << GetNSSErrorMessage(); |
| 572 | SECMOD_DestroyModule(module); |
| 573 | return NULL; |
| 574 | } |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 575 | return module; |
| 576 | } |
| 577 | #endif |
| 578 | |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 579 | static PK11SlotInfo* OpenUserDB(const base::FilePath& path, |
[email protected] | bb639038 | 2010-08-12 19:49:40 | [diff] [blame] | 580 | const char* description) { |
| 581 | const std::string modspec = |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 582 | base::StringPrintf("configDir='sql:%s' tokenDescription='%s'", |
| 583 | path.value().c_str(), description); |
[email protected] | bb639038 | 2010-08-12 19:49:40 | [diff] [blame] | 584 | PK11SlotInfo* db_slot = SECMOD_OpenUserDB(modspec.c_str()); |
| 585 | if (db_slot) { |
| 586 | if (PK11_NeedUserInit(db_slot)) |
| 587 | PK11_InitPin(db_slot, NULL, NULL); |
| 588 | } |
| 589 | else { |
| 590 | LOG(ERROR) << "Error opening persistent database (" << modspec |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 591 | << "): " << GetNSSErrorMessage(); |
[email protected] | bb639038 | 2010-08-12 19:49:40 | [diff] [blame] | 592 | } |
| 593 | return db_slot; |
| 594 | } |
| 595 | |
[email protected] | f3d445e | 2013-11-22 18:35:03 | [diff] [blame] | 596 | static void DisableAESNIIfNeeded() { |
| 597 | if (NSS_VersionCheck("3.15") && !NSS_VersionCheck("3.15.4")) { |
| 598 | // Some versions of NSS have a bug that causes AVX instructions to be |
| 599 | // used without testing whether XSAVE is enabled by the operating system. |
| 600 | // In order to work around this, we disable AES-NI in NSS when we find |
| 601 | // that |has_avx()| is false (which includes the XSAVE test). See |
| 602 | // https://bugzilla.mozilla.org/show_bug.cgi?id=940794 |
| 603 | base::CPU cpu; |
| 604 | |
| 605 | if (cpu.has_avx_hardware() && !cpu.has_avx()) { |
| 606 | base::Environment::Create()->SetVar("NSS_DISABLE_HW_AES", "1"); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 611 | // If this is set to true NSS is forced to be initialized without a DB. |
| 612 | static bool force_nodb_init_; |
| 613 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 614 | bool tpm_token_enabled_for_nss_; |
[email protected] | 84e4772 | 2011-11-17 05:12:02 | [diff] [blame] | 615 | SECMODModule* chaps_module_; |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 616 | PK11SlotInfo* software_slot_; |
| 617 | PK11SlotInfo* test_slot_; |
| 618 | PK11SlotInfo* tpm_slot_; |
| 619 | SECMODModule* root_; |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 620 | bool chromeos_user_logged_in_; |
[email protected] | 6913847 | 2010-06-25 22:44:48 | [diff] [blame] | 621 | #if defined(USE_NSS) |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 622 | // TODO(davidben): When https://bugzilla.mozilla.org/show_bug.cgi?id=564011 |
| 623 | // is fixed, we will no longer need the lock. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 624 | base::Lock write_lock_; |
[email protected] | a8b58f4 | 2010-07-14 20:21:35 | [diff] [blame] | 625 | #endif // defined(USE_NSS) |
[email protected] | 0f8f69c | 2013-11-12 02:56:31 | [diff] [blame] | 626 | |
| 627 | base::ThreadChecker thread_checker_; |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 628 | }; |
| 629 | |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 630 | // static |
| 631 | bool NSSInitSingleton::force_nodb_init_ = false; |
| 632 | |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 633 | base::LazyInstance<NSSInitSingleton>::Leaky |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 634 | g_nss_singleton = LAZY_INSTANCE_INITIALIZER; |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 635 | } // namespace |
| 636 | |
[email protected] | b17bc366 | 2013-08-12 14:09:19 | [diff] [blame] | 637 | const char kTestTPMTokenName[] = "Test DB"; |
| 638 | |
[email protected] | ac3d597 | 2011-01-13 20:33:45 | [diff] [blame] | 639 | #if defined(USE_NSS) |
| 640 | void EarlySetupForNSSInit() { |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 641 | base::FilePath database_dir = GetInitialConfigDirectory(); |
[email protected] | ac3d597 | 2011-01-13 20:33:45 | [diff] [blame] | 642 | if (!database_dir.empty()) |
| 643 | UseLocalCacheOfNSSDatabaseIfNFS(database_dir); |
| 644 | } |
| 645 | #endif |
| 646 | |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 647 | void EnsureNSPRInit() { |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 648 | g_nspr_singleton.Get(); |
[email protected] | 730fb13 | 2009-09-02 22:50:25 | [diff] [blame] | 649 | } |
| 650 | |
[email protected] | 2fdd99e8 | 2012-11-29 04:46:41 | [diff] [blame] | 651 | void InitNSSSafely() { |
[email protected] | 5fe0c769 | 2012-11-17 03:57:56 | [diff] [blame] | 652 | // We might fork, but we haven't loaded any security modules. |
[email protected] | 2fdd99e8 | 2012-11-29 04:46:41 | [diff] [blame] | 653 | DisableNSSForkCheck(); |
[email protected] | 5fe0c769 | 2012-11-17 03:57:56 | [diff] [blame] | 654 | // If we're sandboxed, we shouldn't be able to open user security modules, |
| 655 | // but it's more correct to tell NSS to not even try. |
| 656 | // Loading user security modules would have security implications. |
[email protected] | 2fdd99e8 | 2012-11-29 04:46:41 | [diff] [blame] | 657 | ForceNSSNoDBInit(); |
[email protected] | 5fe0c769 | 2012-11-17 03:57:56 | [diff] [blame] | 658 | // Initialize NSS. |
[email protected] | 2fdd99e8 | 2012-11-29 04:46:41 | [diff] [blame] | 659 | EnsureNSSInit(); |
[email protected] | 5fe0c769 | 2012-11-17 03:57:56 | [diff] [blame] | 660 | } |
| 661 | |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 662 | void EnsureNSSInit() { |
[email protected] | a8e4b5a8 | 2010-10-27 00:05:47 | [diff] [blame] | 663 | // Initializing SSL causes us to do blocking IO. |
| 664 | // Temporarily allow it until we fix |
| 665 | // http://code.google.com/p/chromium/issues/detail?id=59847 |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 666 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 667 | g_nss_singleton.Get(); |
[email protected] | bc1e07c7 | 2008-09-16 14:32:44 | [diff] [blame] | 668 | } |
| 669 | |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 670 | void ForceNSSNoDBInit() { |
| 671 | NSSInitSingleton::ForceNoDBInit(); |
| 672 | } |
| 673 | |
| 674 | void DisableNSSForkCheck() { |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 675 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 676 | env->SetVar("NSS_STRICT_NOFORK", "DISABLED"); |
| 677 | } |
| 678 | |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 679 | void LoadNSSLibraries() { |
| 680 | // Some NSS libraries are linked dynamically so load them here. |
| 681 | #if defined(USE_NSS) |
| 682 | // Try to search for multiple directories to load the libraries. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 683 | std::vector<base::FilePath> paths; |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 684 | |
| 685 | // Use relative path to Search PATH for the library files. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 686 | paths.push_back(base::FilePath()); |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 687 | |
[email protected] | 26661c2 | 2011-10-07 01:33:58 | [diff] [blame] | 688 | // For Debian derivatives NSS libraries are located here. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 689 | paths.push_back(base::FilePath("/usr/lib/nss")); |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 690 | |
[email protected] | 532983bb | 2013-08-01 02:46:46 | [diff] [blame] | 691 | // Ubuntu 11.10 (Oneiric) and Debian Wheezy place the libraries here. |
[email protected] | 26661c2 | 2011-10-07 01:33:58 | [diff] [blame] | 692 | #if defined(ARCH_CPU_X86_64) |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 693 | paths.push_back(base::FilePath("/usr/lib/x86_64-linux-gnu/nss")); |
[email protected] | 26661c2 | 2011-10-07 01:33:58 | [diff] [blame] | 694 | #elif defined(ARCH_CPU_X86) |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 695 | paths.push_back(base::FilePath("/usr/lib/i386-linux-gnu/nss")); |
[email protected] | 26661c2 | 2011-10-07 01:33:58 | [diff] [blame] | 696 | #elif defined(ARCH_CPU_ARMEL) |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 697 | paths.push_back(base::FilePath("/usr/lib/arm-linux-gnueabi/nss")); |
[email protected] | 532983bb | 2013-08-01 02:46:46 | [diff] [blame] | 698 | #elif defined(ARCH_CPU_MIPSEL) |
| 699 | paths.push_back(base::FilePath("/usr/lib/mipsel-linux-gnu/nss")); |
[email protected] | 26661c2 | 2011-10-07 01:33:58 | [diff] [blame] | 700 | #endif |
| 701 | |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 702 | // A list of library files to load. |
| 703 | std::vector<std::string> libs; |
| 704 | libs.push_back("libsoftokn3.so"); |
| 705 | libs.push_back("libfreebl3.so"); |
| 706 | |
| 707 | // For each combination of library file and path, check for existence and |
| 708 | // then load. |
| 709 | size_t loaded = 0; |
| 710 | for (size_t i = 0; i < libs.size(); ++i) { |
| 711 | for (size_t j = 0; j < paths.size(); ++j) { |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 712 | base::FilePath path = paths[j].Append(libs[i]); |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 713 | base::NativeLibrary lib = base::LoadNativeLibrary(path, NULL); |
[email protected] | 9f9b4f2 | 2011-04-08 21:57:46 | [diff] [blame] | 714 | if (lib) { |
| 715 | ++loaded; |
| 716 | break; |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | if (loaded == libs.size()) { |
| 722 | VLOG(3) << "NSS libraries loaded."; |
| 723 | } else { |
[email protected] | 26661c2 | 2011-10-07 01:33:58 | [diff] [blame] | 724 | LOG(ERROR) << "Failed to load NSS libraries."; |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 725 | } |
| 726 | #endif |
| 727 | } |
| 728 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 729 | bool CheckNSSVersion(const char* version) { |
| 730 | return !!NSS_VersionCheck(version); |
| 731 | } |
| 732 | |
[email protected] | 6913847 | 2010-06-25 22:44:48 | [diff] [blame] | 733 | #if defined(USE_NSS) |
[email protected] | 7025e93 | 2012-10-18 07:02:54 | [diff] [blame] | 734 | ScopedTestNSSDB::ScopedTestNSSDB() |
| 735 | : is_open_(g_nss_singleton.Get().OpenTestNSSDB()) { |
| 736 | } |
| 737 | |
| 738 | ScopedTestNSSDB::~ScopedTestNSSDB() { |
[email protected] | 166fac0d | 2013-06-29 21:18:16 | [diff] [blame] | 739 | // Don't close when NSS is < 3.15.1, because it would require an additional |
| 740 | // sleep for 1 second after closing the database, due to |
| 741 | // http://bugzil.la/875601. |
| 742 | if (NSS_VersionCheck("3.15.1")) { |
| 743 | g_nss_singleton.Get().CloseTestNSSDB(); |
| 744 | } |
[email protected] | bb639038 | 2010-08-12 19:49:40 | [diff] [blame] | 745 | } |
| 746 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 747 | base::Lock* GetNSSWriteLock() { |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 748 | return g_nss_singleton.Get().write_lock(); |
[email protected] | 6913847 | 2010-06-25 22:44:48 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | AutoNSSWriteLock::AutoNSSWriteLock() : lock_(GetNSSWriteLock()) { |
| 752 | // May be NULL if the lock is not needed in our version of NSS. |
| 753 | if (lock_) |
| 754 | lock_->Acquire(); |
| 755 | } |
| 756 | |
| 757 | AutoNSSWriteLock::~AutoNSSWriteLock() { |
| 758 | if (lock_) { |
| 759 | lock_->AssertAcquired(); |
| 760 | lock_->Release(); |
| 761 | } |
| 762 | } |
[email protected] | dd24ffc | 2011-06-08 19:46:42 | [diff] [blame] | 763 | |
| 764 | AutoSECMODListReadLock::AutoSECMODListReadLock() |
| 765 | : lock_(SECMOD_GetDefaultModuleListLock()) { |
| 766 | SECMOD_GetReadLock(lock_); |
| 767 | } |
| 768 | |
| 769 | AutoSECMODListReadLock::~AutoSECMODListReadLock() { |
| 770 | SECMOD_ReleaseReadLock(lock_); |
| 771 | } |
| 772 | |
[email protected] | 6913847 | 2010-06-25 22:44:48 | [diff] [blame] | 773 | #endif // defined(USE_NSS) |
| 774 | |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 775 | #if defined(OS_CHROMEOS) |
| 776 | void OpenPersistentNSSDB() { |
[email protected] | f615bda3 | 2010-11-21 01:04:56 | [diff] [blame] | 777 | g_nss_singleton.Get().OpenPersistentNSSDB(); |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 778 | } |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 779 | |
[email protected] | 450b4ad7 | 2012-05-17 10:04:17 | [diff] [blame] | 780 | void EnableTPMTokenForNSS() { |
| 781 | g_nss_singleton.Get().EnableTPMTokenForNSS(); |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 782 | } |
| 783 | |
[email protected] | c64b914 | 2011-04-19 18:49:54 | [diff] [blame] | 784 | bool IsTPMTokenReady() { |
| 785 | return g_nss_singleton.Get().IsTPMTokenReady(); |
[email protected] | 74beead | 2011-04-12 20:40:12 | [diff] [blame] | 786 | } |
[email protected] | c64b914 | 2011-04-19 18:49:54 | [diff] [blame] | 787 | |
[email protected] | bee9b54 | 2013-11-25 21:45:04 | [diff] [blame] | 788 | bool InitializeTPMToken(int token_slot_id) { |
| 789 | return g_nss_singleton.Get().InitializeTPMToken(token_slot_id); |
[email protected] | c175cdb | 2011-06-28 20:41:55 | [diff] [blame] | 790 | } |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 791 | #endif // defined(OS_CHROMEOS) |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 792 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 793 | base::Time PRTimeToBaseTime(PRTime prtime) { |
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 794 | return base::Time::FromInternalValue( |
| 795 | prtime + base::Time::UnixEpoch().ToInternalValue()); |
| 796 | } |
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 797 | |
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 798 | PRTime BaseTimeToPRTime(base::Time time) { |
| 799 | return time.ToInternalValue() - base::Time::UnixEpoch().ToInternalValue(); |
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 800 | } |
| 801 | |
[email protected] | 6a89ef2 | 2011-04-07 17:34:21 | [diff] [blame] | 802 | PK11SlotInfo* GetPublicNSSKeySlot() { |
| 803 | return g_nss_singleton.Get().GetPublicNSSKeySlot(); |
| 804 | } |
| 805 | |
| 806 | PK11SlotInfo* GetPrivateNSSKeySlot() { |
| 807 | return g_nss_singleton.Get().GetPrivateNSSKeySlot(); |
[email protected] | dcce6cf | 2010-04-29 17:50:06 | [diff] [blame] | 808 | } |
| 809 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 810 | } // namespace crypto |