OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "crypto/nss_util.h" |
| 6 #include "crypto/nss_util_internal.h" |
| 7 |
| 8 #include <nss.h> |
| 9 #include <plarena.h> |
| 10 #include <prerror.h> |
| 11 #include <prinit.h> |
| 12 #include <prtime.h> |
| 13 #include <pk11pub.h> |
| 14 #include <secmod.h> |
| 15 |
| 16 #if defined(OS_LINUX) |
| 17 #include <linux/nfs_fs.h> |
| 18 #include <sys/vfs.h> |
| 19 #endif |
| 20 |
| 21 #include <vector> |
| 22 |
| 23 #include "base/environment.h" |
| 24 #include "base/file_path.h" |
| 25 #include "base/file_util.h" |
| 26 #include "base/lazy_instance.h" |
| 27 #include "base/logging.h" |
| 28 #include "base/memory/scoped_ptr.h" |
| 29 #include "base/native_library.h" |
| 30 #include "base/stringprintf.h" |
| 31 #include "base/threading/thread_restrictions.h" |
| 32 #include "crypto/scoped_nss_types.h" |
| 33 |
| 34 // USE_NSS means we use NSS for everything crypto-related. If USE_NSS is not |
| 35 // defined, such as on Mac and Windows, we use NSS for SSL only -- we don't |
| 36 // use NSS for crypto or certificate verification, and we don't use the NSS |
| 37 // certificate and key databases. |
| 38 #if defined(USE_NSS) |
| 39 #include "base/synchronization/lock.h" |
| 40 #include "crypto/crypto_module_blocking_password_delegate.h" |
| 41 #endif // defined(USE_NSS) |
| 42 |
| 43 namespace crypto { |
| 44 |
| 45 namespace { |
| 46 |
| 47 #if defined(OS_CHROMEOS) |
| 48 const char kNSSDatabaseName[] = "Real NSS database"; |
| 49 |
| 50 // Constants for loading opencryptoki. |
| 51 const char kOpencryptokiModuleName[] = "opencryptoki"; |
| 52 const char kOpencryptokiPath[] = "/usr/lib/opencryptoki/libopencryptoki.so"; |
| 53 |
| 54 // TODO(gspencer): Get these values from cryptohomed's dbus API when |
| 55 // we ask if it has initialized the TPM yet. These should not be |
| 56 // hard-coded here. |
| 57 const char kTPMTokenName[] = "Initialized by CrOS"; |
| 58 const char kTPMUserPIN[] = "111111"; |
| 59 const char kTPMSecurityOfficerPIN[] = "000000"; |
| 60 |
| 61 // Fake certificate authority database used for testing. |
| 62 static const FilePath::CharType kReadOnlyCertDB[] = |
| 63 FILE_PATH_LITERAL("/etc/fake_root_ca/nssdb"); |
| 64 #endif // defined(OS_CHROMEOS) |
| 65 |
| 66 std::string GetNSSErrorMessage() { |
| 67 std::string result; |
| 68 if (PR_GetErrorTextLength()) { |
| 69 scoped_array<char> error_text(new char[PR_GetErrorTextLength() + 1]); |
| 70 PRInt32 copied = PR_GetErrorText(error_text.get()); |
| 71 result = std::string(error_text.get(), copied); |
| 72 } else { |
| 73 result = StringPrintf("NSS error code: %d", PR_GetError()); |
| 74 } |
| 75 return result; |
| 76 } |
| 77 |
| 78 #if defined(USE_NSS) |
| 79 FilePath GetDefaultConfigDirectory() { |
| 80 FilePath dir = file_util::GetHomeDir(); |
| 81 if (dir.empty()) { |
| 82 LOG(ERROR) << "Failed to get home directory."; |
| 83 return dir; |
| 84 } |
| 85 dir = dir.AppendASCII(".pki").AppendASCII("nssdb"); |
| 86 if (!file_util::CreateDirectory(dir)) { |
| 87 LOG(ERROR) << "Failed to create ~/.pki/nssdb directory."; |
| 88 dir.clear(); |
| 89 } |
| 90 return dir; |
| 91 } |
| 92 |
| 93 // On non-chromeos platforms, return the default config directory. |
| 94 // On chromeos, return a read-only directory with fake root CA certs for testing |
| 95 // (which will not exist on non-testing images). These root CA certs are used |
| 96 // by the local Google Accounts server mock we use when testing our login code. |
| 97 // If this directory is not present, NSS_Init() will fail. It is up to the |
| 98 // caller to failover to NSS_NoDB_Init() at that point. |
| 99 FilePath GetInitialConfigDirectory() { |
| 100 #if defined(OS_CHROMEOS) |
| 101 return FilePath(kReadOnlyCertDB); |
| 102 #else |
| 103 return GetDefaultConfigDirectory(); |
| 104 #endif // defined(OS_CHROMEOS) |
| 105 } |
| 106 |
| 107 // This callback for NSS forwards all requests to a caller-specified |
| 108 // CryptoModuleBlockingPasswordDelegate object. |
| 109 char* PKCS11PasswordFunc(PK11SlotInfo* slot, PRBool retry, void* arg) { |
| 110 #if defined(OS_CHROMEOS) |
| 111 // If we get asked for a password for the TPM, then return the |
| 112 // static password we use. |
| 113 if (PK11_GetTokenName(slot) == crypto::GetTPMTokenName()) |
| 114 return PORT_Strdup(GetTPMUserPIN().c_str()); |
| 115 #endif |
| 116 crypto::CryptoModuleBlockingPasswordDelegate* delegate = |
| 117 reinterpret_cast<crypto::CryptoModuleBlockingPasswordDelegate*>(arg); |
| 118 if (delegate) { |
| 119 bool cancelled = false; |
| 120 std::string password = delegate->RequestPassword(PK11_GetTokenName(slot), |
| 121 retry != PR_FALSE, |
| 122 &cancelled); |
| 123 if (cancelled) |
| 124 return NULL; |
| 125 char* result = PORT_Strdup(password.c_str()); |
| 126 password.replace(0, password.size(), password.size(), 0); |
| 127 return result; |
| 128 } |
| 129 DLOG(ERROR) << "PK11 password requested with NULL arg"; |
| 130 return NULL; |
| 131 } |
| 132 |
| 133 // NSS creates a local cache of the sqlite database if it detects that the |
| 134 // filesystem the database is on is much slower than the local disk. The |
| 135 // detection doesn't work with the latest versions of sqlite, such as 3.6.22 |
| 136 // (NSS bug https://bugzilla.mozilla.org/show_bug.cgi?id=578561). So we set |
| 137 // the NSS environment variable NSS_SDB_USE_CACHE to "yes" to override NSS's |
| 138 // detection when database_dir is on NFS. See http://crbug.com/48585. |
| 139 // |
| 140 // TODO(wtc): port this function to other USE_NSS platforms. It is defined |
| 141 // only for OS_LINUX simply because the statfs structure is OS-specific. |
| 142 // |
| 143 // Because this function sets an environment variable it must be run before we |
| 144 // go multi-threaded. |
| 145 void UseLocalCacheOfNSSDatabaseIfNFS(const FilePath& database_dir) { |
| 146 #if defined(OS_LINUX) |
| 147 struct statfs buf; |
| 148 if (statfs(database_dir.value().c_str(), &buf) == 0) { |
| 149 if (buf.f_type == NFS_SUPER_MAGIC) { |
| 150 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 151 const char* use_cache_env_var = "NSS_SDB_USE_CACHE"; |
| 152 if (!env->HasVar(use_cache_env_var)) |
| 153 env->SetVar(use_cache_env_var, "yes"); |
| 154 } |
| 155 } |
| 156 #endif // defined(OS_LINUX) |
| 157 } |
| 158 |
| 159 // A helper class that acquires the SECMOD list read lock while the |
| 160 // AutoSECMODListReadLock is in scope. |
| 161 class AutoSECMODListReadLock { |
| 162 public: |
| 163 AutoSECMODListReadLock() |
| 164 : lock_(SECMOD_GetDefaultModuleListLock()) { |
| 165 SECMOD_GetReadLock(lock_); |
| 166 } |
| 167 |
| 168 ~AutoSECMODListReadLock() { |
| 169 SECMOD_ReleaseReadLock(lock_); |
| 170 } |
| 171 |
| 172 private: |
| 173 SECMODListLock* lock_; |
| 174 DISALLOW_COPY_AND_ASSIGN(AutoSECMODListReadLock); |
| 175 }; |
| 176 |
| 177 PK11SlotInfo* FindSlotWithTokenName(const std::string& token_name) { |
| 178 AutoSECMODListReadLock auto_lock; |
| 179 SECMODModuleList* head = SECMOD_GetDefaultModuleList(); |
| 180 for (SECMODModuleList* item = head; item != NULL; item = item->next) { |
| 181 int slot_count = item->module->loaded ? item->module->slotCount : 0; |
| 182 for (int i = 0; i < slot_count; i++) { |
| 183 PK11SlotInfo* slot = item->module->slots[i]; |
| 184 if (PK11_GetTokenName(slot) == token_name) |
| 185 return PK11_ReferenceSlot(slot); |
| 186 } |
| 187 } |
| 188 return NULL; |
| 189 } |
| 190 |
| 191 #endif // defined(USE_NSS) |
| 192 |
| 193 // A singleton to initialize/deinitialize NSPR. |
| 194 // Separate from the NSS singleton because we initialize NSPR on the UI thread. |
| 195 // Now that we're leaking the singleton, we could merge back with the NSS |
| 196 // singleton. |
| 197 class NSPRInitSingleton { |
| 198 private: |
| 199 friend struct base::DefaultLazyInstanceTraits<NSPRInitSingleton>; |
| 200 |
| 201 NSPRInitSingleton() { |
| 202 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| 203 } |
| 204 |
| 205 // NOTE(willchan): We don't actually execute this code since we leak NSS to |
| 206 // prevent non-joinable threads from using NSS after it's already been shut |
| 207 // down. |
| 208 ~NSPRInitSingleton() { |
| 209 PL_ArenaFinish(); |
| 210 PRStatus prstatus = PR_Cleanup(); |
| 211 if (prstatus != PR_SUCCESS) { |
| 212 LOG(ERROR) << "PR_Cleanup failed; was NSPR initialized on wrong thread?"; |
| 213 } |
| 214 } |
| 215 }; |
| 216 |
| 217 base::LazyInstance<NSPRInitSingleton, |
| 218 base::LeakyLazyInstanceTraits<NSPRInitSingleton> > |
| 219 g_nspr_singleton(base::LINKER_INITIALIZED); |
| 220 |
| 221 class NSSInitSingleton { |
| 222 public: |
| 223 #if defined(OS_CHROMEOS) |
| 224 void OpenPersistentNSSDB() { |
| 225 if (!chromeos_user_logged_in_) { |
| 226 // GetDefaultConfigDirectory causes us to do blocking IO on UI thread. |
| 227 // Temporarily allow it until we fix http://crbug.com/70119 |
| 228 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 229 chromeos_user_logged_in_ = true; |
| 230 |
| 231 // This creates another DB slot in NSS that is read/write, unlike |
| 232 // the fake root CA cert DB and the "default" crypto key |
| 233 // provider, which are still read-only (because we initialized |
| 234 // NSS before we had a cryptohome mounted). |
| 235 software_slot_ = OpenUserDB(GetDefaultConfigDirectory(), |
| 236 kNSSDatabaseName); |
| 237 } |
| 238 } |
| 239 |
| 240 bool EnableTPMForNSS() { |
| 241 if (!opencryptoki_module_) { |
| 242 // This loads the opencryptoki module so we can talk to the |
| 243 // hardware TPM. |
| 244 opencryptoki_module_ = LoadModule( |
| 245 kOpencryptokiModuleName, |
| 246 kOpencryptokiPath, |
| 247 // trustOrder=100 -- means it'll select this as the most |
| 248 // trusted slot for the mechanisms it provides. |
| 249 // slotParams=... -- selects RSA as the only mechanism, and only |
| 250 // asks for the password when necessary (instead of every |
| 251 // time, or after a timeout). |
| 252 "trustOrder=100 slotParams=(1={slotFlags=[RSA] askpw=only})"); |
| 253 if (opencryptoki_module_) { |
| 254 // We shouldn't need to initialize the TPM PIN here because |
| 255 // it'll be taken care of by cryptohomed, but we have to make |
| 256 // sure that it is initialized. |
| 257 |
| 258 // TODO(gspencer): replace this with a dbus call that will |
| 259 // check to see that cryptohomed has initialized the PINs, and |
| 260 // will fetch the token name and PINs for accessing the TPM. |
| 261 EnsureTPMInit(); |
| 262 |
| 263 // If this is set, then we'll use the TPM by default. |
| 264 tpm_slot_ = GetTPMSlot(); |
| 265 return true; |
| 266 } |
| 267 } |
| 268 return false; |
| 269 } |
| 270 |
| 271 std::string GetTPMTokenName() { |
| 272 // TODO(gspencer): This should come from the dbus interchange with |
| 273 // cryptohomed instead of being hard-coded. |
| 274 return std::string(kTPMTokenName); |
| 275 } |
| 276 |
| 277 std::string GetTPMUserPIN() { |
| 278 // TODO(gspencer): This should come from the dbus interchange with |
| 279 // cryptohomed instead of being hard-coded. |
| 280 return std::string(kTPMUserPIN); |
| 281 } |
| 282 |
| 283 PK11SlotInfo* GetTPMSlot() { |
| 284 return FindSlotWithTokenName(GetTPMTokenName()); |
| 285 } |
| 286 #endif // defined(OS_CHROMEOS) |
| 287 |
| 288 |
| 289 bool OpenTestNSSDB(const FilePath& path, const char* description) { |
| 290 test_slot_ = OpenUserDB(path, description); |
| 291 return !!test_slot_; |
| 292 } |
| 293 |
| 294 void CloseTestNSSDB() { |
| 295 if (test_slot_) { |
| 296 SECStatus status = SECMOD_CloseUserDB(test_slot_); |
| 297 if (status != SECSuccess) |
| 298 LOG(ERROR) << "SECMOD_CloseUserDB failed: " << PORT_GetError(); |
| 299 PK11_FreeSlot(test_slot_); |
| 300 test_slot_ = NULL; |
| 301 } |
| 302 } |
| 303 |
| 304 PK11SlotInfo* GetPublicNSSKeySlot() { |
| 305 if (test_slot_) |
| 306 return PK11_ReferenceSlot(test_slot_); |
| 307 if (software_slot_) |
| 308 return PK11_ReferenceSlot(software_slot_); |
| 309 return PK11_GetInternalKeySlot(); |
| 310 } |
| 311 |
| 312 PK11SlotInfo* GetPrivateNSSKeySlot() { |
| 313 if (test_slot_) |
| 314 return PK11_ReferenceSlot(test_slot_); |
| 315 // If the TPM slot has been opened, then return that one. |
| 316 if (tpm_slot_) |
| 317 return PK11_ReferenceSlot(tpm_slot_); |
| 318 // If it hasn't, then return the software slot. |
| 319 if (software_slot_) |
| 320 return PK11_ReferenceSlot(software_slot_); |
| 321 return PK11_GetInternalKeySlot(); |
| 322 } |
| 323 |
| 324 #if defined(USE_NSS) |
| 325 base::Lock* write_lock() { |
| 326 return &write_lock_; |
| 327 } |
| 328 #endif // defined(USE_NSS) |
| 329 |
| 330 // This method is used to force NSS to be initialized without a DB. |
| 331 // Call this method before NSSInitSingleton() is constructed. |
| 332 static void ForceNoDBInit() { |
| 333 force_nodb_init_ = true; |
| 334 } |
| 335 |
| 336 private: |
| 337 friend struct base::DefaultLazyInstanceTraits<NSSInitSingleton>; |
| 338 |
| 339 NSSInitSingleton() |
| 340 : opencryptoki_module_(NULL), |
| 341 software_slot_(NULL), |
| 342 test_slot_(NULL), |
| 343 tpm_slot_(NULL), |
| 344 root_(NULL), |
| 345 chromeos_user_logged_in_(false) { |
| 346 EnsureNSPRInit(); |
| 347 |
| 348 // We *must* have NSS >= 3.12.3. See bug 26448. |
| 349 COMPILE_ASSERT( |
| 350 (NSS_VMAJOR == 3 && NSS_VMINOR == 12 && NSS_VPATCH >= 3) || |
| 351 (NSS_VMAJOR == 3 && NSS_VMINOR > 12) || |
| 352 (NSS_VMAJOR > 3), |
| 353 nss_version_check_failed); |
| 354 // Also check the run-time NSS version. |
| 355 // NSS_VersionCheck is a >= check, not strict equality. |
| 356 if (!NSS_VersionCheck("3.12.3")) { |
| 357 // It turns out many people have misconfigured NSS setups, where |
| 358 // their run-time NSPR doesn't match the one their NSS was compiled |
| 359 // against. So rather than aborting, complain loudly. |
| 360 LOG(ERROR) << "NSS_VersionCheck(\"3.12.3\") failed. " |
| 361 "We depend on NSS >= 3.12.3, and this error is not fatal " |
| 362 "only because many people have busted NSS setups (for " |
| 363 "example, using the wrong version of NSPR). " |
| 364 "Please upgrade to the latest NSS and NSPR, and if you " |
| 365 "still get this error, contact your distribution " |
| 366 "maintainer."; |
| 367 } |
| 368 |
| 369 SECStatus status = SECFailure; |
| 370 bool nodb_init = force_nodb_init_; |
| 371 |
| 372 #if !defined(USE_NSS) |
| 373 // Use the system certificate store, so initialize NSS without database. |
| 374 nodb_init = true; |
| 375 #endif |
| 376 |
| 377 if (nodb_init) { |
| 378 status = NSS_NoDB_Init(NULL); |
| 379 if (status != SECSuccess) { |
| 380 LOG(ERROR) << "Error initializing NSS without a persistent " |
| 381 "database: " << GetNSSErrorMessage(); |
| 382 } |
| 383 } else { |
| 384 #if defined(USE_NSS) |
| 385 FilePath database_dir = GetInitialConfigDirectory(); |
| 386 if (!database_dir.empty()) { |
| 387 // This duplicates the work which should have been done in |
| 388 // EarlySetupForNSSInit. However, this function is idempotent so |
| 389 // there's no harm done. |
| 390 UseLocalCacheOfNSSDatabaseIfNFS(database_dir); |
| 391 |
| 392 // Initialize with a persistent database (likely, ~/.pki/nssdb). |
| 393 // Use "sql:" which can be shared by multiple processes safely. |
| 394 std::string nss_config_dir = |
| 395 StringPrintf("sql:%s", database_dir.value().c_str()); |
| 396 #if defined(OS_CHROMEOS) |
| 397 status = NSS_Init(nss_config_dir.c_str()); |
| 398 #else |
| 399 status = NSS_InitReadWrite(nss_config_dir.c_str()); |
| 400 #endif |
| 401 if (status != SECSuccess) { |
| 402 LOG(ERROR) << "Error initializing NSS with a persistent " |
| 403 "database (" << nss_config_dir |
| 404 << "): " << GetNSSErrorMessage(); |
| 405 } |
| 406 } |
| 407 if (status != SECSuccess) { |
| 408 VLOG(1) << "Initializing NSS without a persistent database."; |
| 409 status = NSS_NoDB_Init(NULL); |
| 410 if (status != SECSuccess) { |
| 411 LOG(ERROR) << "Error initializing NSS without a persistent " |
| 412 "database: " << GetNSSErrorMessage(); |
| 413 return; |
| 414 } |
| 415 } |
| 416 |
| 417 PK11_SetPasswordFunc(PKCS11PasswordFunc); |
| 418 |
| 419 // If we haven't initialized the password for the NSS databases, |
| 420 // initialize an empty-string password so that we don't need to |
| 421 // log in. |
| 422 PK11SlotInfo* slot = PK11_GetInternalKeySlot(); |
| 423 if (slot) { |
| 424 // PK11_InitPin may write to the keyDB, but no other thread can use NSS |
| 425 // yet, so we don't need to lock. |
| 426 if (PK11_NeedUserInit(slot)) |
| 427 PK11_InitPin(slot, NULL, NULL); |
| 428 PK11_FreeSlot(slot); |
| 429 } |
| 430 |
| 431 root_ = InitDefaultRootCerts(); |
| 432 #endif // defined(USE_NSS) |
| 433 } |
| 434 } |
| 435 |
| 436 // NOTE(willchan): We don't actually execute this code since we leak NSS to |
| 437 // prevent non-joinable threads from using NSS after it's already been shut |
| 438 // down. |
| 439 ~NSSInitSingleton() { |
| 440 if (tpm_slot_) { |
| 441 PK11_FreeSlot(tpm_slot_); |
| 442 tpm_slot_ = NULL; |
| 443 } |
| 444 if (software_slot_) { |
| 445 SECMOD_CloseUserDB(software_slot_); |
| 446 PK11_FreeSlot(software_slot_); |
| 447 software_slot_ = NULL; |
| 448 } |
| 449 CloseTestNSSDB(); |
| 450 if (root_) { |
| 451 SECMOD_UnloadUserModule(root_); |
| 452 SECMOD_DestroyModule(root_); |
| 453 root_ = NULL; |
| 454 } |
| 455 if (opencryptoki_module_) { |
| 456 SECMOD_UnloadUserModule(opencryptoki_module_); |
| 457 SECMOD_DestroyModule(opencryptoki_module_); |
| 458 opencryptoki_module_ = NULL; |
| 459 } |
| 460 |
| 461 SECStatus status = NSS_Shutdown(); |
| 462 if (status != SECSuccess) { |
| 463 // We VLOG(1) because this failure is relatively harmless (leaking, but |
| 464 // we're shutting down anyway). |
| 465 VLOG(1) << "NSS_Shutdown failed; see http://crbug.com/4609"; |
| 466 } |
| 467 } |
| 468 |
| 469 #if defined(USE_NSS) |
| 470 // Load nss's built-in root certs. |
| 471 SECMODModule* InitDefaultRootCerts() { |
| 472 SECMODModule* root = LoadModule("Root Certs", "libnssckbi.so", NULL); |
| 473 if (root) |
| 474 return root; |
| 475 |
| 476 // Aw, snap. Can't find/load root cert shared library. |
| 477 // This will make it hard to talk to anybody via https. |
| 478 NOTREACHED(); |
| 479 return NULL; |
| 480 } |
| 481 |
| 482 // Load the given module for this NSS session. |
| 483 SECMODModule* LoadModule(const char* name, |
| 484 const char* library_path, |
| 485 const char* params) { |
| 486 std::string modparams = StringPrintf( |
| 487 "name=\"%s\" library=\"%s\" %s", |
| 488 name, library_path, params ? params : ""); |
| 489 |
| 490 // Shouldn't need to const_cast here, but SECMOD doesn't properly |
| 491 // declare input string arguments as const. Bug |
| 492 // https://bugzilla.mozilla.org/show_bug.cgi?id=642546 was filed |
| 493 // on NSS codebase to address this. |
| 494 SECMODModule* module = SECMOD_LoadUserModule( |
| 495 const_cast<char*>(modparams.c_str()), NULL, PR_FALSE); |
| 496 if (!module) { |
| 497 LOG(ERROR) << "Error loading " << name << " module into NSS: " |
| 498 << GetNSSErrorMessage(); |
| 499 return NULL; |
| 500 } |
| 501 return module; |
| 502 } |
| 503 #endif |
| 504 |
| 505 #if defined(OS_CHROMEOS) |
| 506 void EnsureTPMInit() { |
| 507 crypto::ScopedPK11Slot tpm_slot(GetTPMSlot()); |
| 508 if (tpm_slot.get()) { |
| 509 // TODO(gspencer): Remove this in favor of the dbus API for |
| 510 // cryptohomed when that is available. |
| 511 if (PK11_NeedUserInit(tpm_slot.get())) { |
| 512 PK11_InitPin(tpm_slot.get(), |
| 513 kTPMSecurityOfficerPIN, |
| 514 kTPMUserPIN); |
| 515 } |
| 516 } |
| 517 } |
| 518 #endif |
| 519 |
| 520 static PK11SlotInfo* OpenUserDB(const FilePath& path, |
| 521 const char* description) { |
| 522 const std::string modspec = |
| 523 StringPrintf("configDir='sql:%s' tokenDescription='%s'", |
| 524 path.value().c_str(), description); |
| 525 PK11SlotInfo* db_slot = SECMOD_OpenUserDB(modspec.c_str()); |
| 526 if (db_slot) { |
| 527 if (PK11_NeedUserInit(db_slot)) |
| 528 PK11_InitPin(db_slot, NULL, NULL); |
| 529 } |
| 530 else { |
| 531 LOG(ERROR) << "Error opening persistent database (" << modspec |
| 532 << "): " << GetNSSErrorMessage(); |
| 533 } |
| 534 return db_slot; |
| 535 } |
| 536 |
| 537 // If this is set to true NSS is forced to be initialized without a DB. |
| 538 static bool force_nodb_init_; |
| 539 |
| 540 SECMODModule* opencryptoki_module_; |
| 541 PK11SlotInfo* software_slot_; |
| 542 PK11SlotInfo* test_slot_; |
| 543 PK11SlotInfo* tpm_slot_; |
| 544 SECMODModule* root_; |
| 545 bool chromeos_user_logged_in_; |
| 546 #if defined(USE_NSS) |
| 547 // TODO(davidben): When https://bugzilla.mozilla.org/show_bug.cgi?id=564011 |
| 548 // is fixed, we will no longer need the lock. |
| 549 base::Lock write_lock_; |
| 550 #endif // defined(USE_NSS) |
| 551 }; |
| 552 |
| 553 // static |
| 554 bool NSSInitSingleton::force_nodb_init_ = false; |
| 555 |
| 556 base::LazyInstance<NSSInitSingleton, |
| 557 base::LeakyLazyInstanceTraits<NSSInitSingleton> > |
| 558 g_nss_singleton(base::LINKER_INITIALIZED); |
| 559 |
| 560 } // namespace |
| 561 |
| 562 #if defined(USE_NSS) |
| 563 void EarlySetupForNSSInit() { |
| 564 FilePath database_dir = GetInitialConfigDirectory(); |
| 565 if (!database_dir.empty()) |
| 566 UseLocalCacheOfNSSDatabaseIfNFS(database_dir); |
| 567 } |
| 568 #endif |
| 569 |
| 570 void EnsureNSPRInit() { |
| 571 g_nspr_singleton.Get(); |
| 572 } |
| 573 |
| 574 void EnsureNSSInit() { |
| 575 // Initializing SSL causes us to do blocking IO. |
| 576 // Temporarily allow it until we fix |
| 577 // http://code.google.com/p/chromium/issues/detail?id=59847 |
| 578 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 579 g_nss_singleton.Get(); |
| 580 } |
| 581 |
| 582 void ForceNSSNoDBInit() { |
| 583 NSSInitSingleton::ForceNoDBInit(); |
| 584 } |
| 585 |
| 586 void DisableNSSForkCheck() { |
| 587 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 588 env->SetVar("NSS_STRICT_NOFORK", "DISABLED"); |
| 589 } |
| 590 |
| 591 void LoadNSSLibraries() { |
| 592 // Some NSS libraries are linked dynamically so load them here. |
| 593 #if defined(USE_NSS) |
| 594 // Try to search for multiple directories to load the libraries. |
| 595 std::vector<FilePath> paths; |
| 596 |
| 597 // Use relative path to Search PATH for the library files. |
| 598 paths.push_back(FilePath()); |
| 599 |
| 600 // For Debian derivaties NSS libraries are located here. |
| 601 paths.push_back(FilePath("/usr/lib/nss")); |
| 602 |
| 603 // A list of library files to load. |
| 604 std::vector<std::string> libs; |
| 605 libs.push_back("libsoftokn3.so"); |
| 606 libs.push_back("libfreebl3.so"); |
| 607 |
| 608 // For each combination of library file and path, check for existence and |
| 609 // then load. |
| 610 size_t loaded = 0; |
| 611 for (size_t i = 0; i < libs.size(); ++i) { |
| 612 for (size_t j = 0; j < paths.size(); ++j) { |
| 613 FilePath path = paths[j].Append(libs[i]); |
| 614 base::NativeLibrary lib = base::LoadNativeLibrary(path); |
| 615 if (lib) { |
| 616 ++loaded; |
| 617 break; |
| 618 } |
| 619 } |
| 620 } |
| 621 |
| 622 if (loaded == libs.size()) { |
| 623 VLOG(3) << "NSS libraries loaded."; |
| 624 } else { |
| 625 LOG(WARNING) << "Failed to load NSS libraries."; |
| 626 } |
| 627 #endif |
| 628 } |
| 629 |
| 630 bool CheckNSSVersion(const char* version) { |
| 631 return !!NSS_VersionCheck(version); |
| 632 } |
| 633 |
| 634 #if defined(USE_NSS) |
| 635 bool OpenTestNSSDB(const FilePath& path, const char* description) { |
| 636 return g_nss_singleton.Get().OpenTestNSSDB(path, description); |
| 637 } |
| 638 |
| 639 void CloseTestNSSDB() { |
| 640 g_nss_singleton.Get().CloseTestNSSDB(); |
| 641 } |
| 642 |
| 643 base::Lock* GetNSSWriteLock() { |
| 644 return g_nss_singleton.Get().write_lock(); |
| 645 } |
| 646 |
| 647 AutoNSSWriteLock::AutoNSSWriteLock() : lock_(GetNSSWriteLock()) { |
| 648 // May be NULL if the lock is not needed in our version of NSS. |
| 649 if (lock_) |
| 650 lock_->Acquire(); |
| 651 } |
| 652 |
| 653 AutoNSSWriteLock::~AutoNSSWriteLock() { |
| 654 if (lock_) { |
| 655 lock_->AssertAcquired(); |
| 656 lock_->Release(); |
| 657 } |
| 658 } |
| 659 #endif // defined(USE_NSS) |
| 660 |
| 661 #if defined(OS_CHROMEOS) |
| 662 void OpenPersistentNSSDB() { |
| 663 g_nss_singleton.Get().OpenPersistentNSSDB(); |
| 664 } |
| 665 |
| 666 bool EnableTPMForNSS() { |
| 667 return g_nss_singleton.Get().EnableTPMForNSS(); |
| 668 } |
| 669 |
| 670 std::string GetTPMTokenName() { |
| 671 return g_nss_singleton.Get().GetTPMTokenName(); |
| 672 } |
| 673 |
| 674 std::string GetTPMUserPIN() { |
| 675 return g_nss_singleton.Get().GetTPMUserPIN(); |
| 676 } |
| 677 #endif // defined(OS_CHROMEOS) |
| 678 |
| 679 // TODO(port): Implement this more simply. We can convert by subtracting an |
| 680 // offset (the difference between NSPR's and base::Time's epochs). |
| 681 base::Time PRTimeToBaseTime(PRTime prtime) { |
| 682 PRExplodedTime prxtime; |
| 683 PR_ExplodeTime(prtime, PR_GMTParameters, &prxtime); |
| 684 |
| 685 base::Time::Exploded exploded; |
| 686 exploded.year = prxtime.tm_year; |
| 687 exploded.month = prxtime.tm_month + 1; |
| 688 exploded.day_of_week = prxtime.tm_wday; |
| 689 exploded.day_of_month = prxtime.tm_mday; |
| 690 exploded.hour = prxtime.tm_hour; |
| 691 exploded.minute = prxtime.tm_min; |
| 692 exploded.second = prxtime.tm_sec; |
| 693 exploded.millisecond = prxtime.tm_usec / 1000; |
| 694 |
| 695 return base::Time::FromUTCExploded(exploded); |
| 696 } |
| 697 |
| 698 PK11SlotInfo* GetPublicNSSKeySlot() { |
| 699 return g_nss_singleton.Get().GetPublicNSSKeySlot(); |
| 700 } |
| 701 |
| 702 PK11SlotInfo* GetPrivateNSSKeySlot() { |
| 703 return g_nss_singleton.Get().GetPrivateNSSKeySlot(); |
| 704 } |
| 705 |
| 706 } // namespace crypto |
OLD | NEW |