This source file includes following definitions.
- GetNSSErrorMessage
- GetDefaultConfigDirectory
- GetInitialConfigDirectory
- PKCS11PasswordFunc
- UseLocalCacheOfNSSDatabaseIfNFS
- CrashOnNSSInitFailure
- is_primary_user_
- GetPublicSlot
- GetPrivateSlot
- SetPrivateSlot
- OpenPersistentNSSDB
- OpenPersistentNSSDBForPath
- EnableTPMTokenForNSS
- IsTPMTokenEnabledForNSS
- InitializeTPMToken
- InitializeTPMTokenOnWorkerThread
- OnInitializedTPMToken
- IsTPMTokenReady
- GetTPMSlotForIdOnWorkerThread
- InitializeNSSForChromeOSUser
- InitializeTPMForChromeOSUser
- OnInitializedTPMForChromeOSUser
- InitializePrivateSoftwareSlotForChromeOSUser
- GetPublicSlotForChromeOSUser
- GetPrivateSlotForChromeOSUser
- CloseTestChromeOSUser
- OpenTestNSSDB
- CloseTestNSSDB
- GetPublicNSSKeySlot
- GetPrivateNSSKeySlot
- write_lock
- ForceNoDBInit
- chromeos_user_logged_in_
- InitDefaultRootCerts
- LoadModule
- OpenUserDB
- DisableAESNIIfNeeded
- EarlySetupForNSSInit
- EnsureNSPRInit
- InitNSSSafely
- EnsureNSSInit
- ForceNSSNoDBInit
- DisableNSSForkCheck
- LoadNSSLibraries
- CheckNSSVersion
- GetNSSWriteLock
- OpenPersistentNSSDB
- EnableTPMTokenForNSS
- IsTPMTokenEnabledForNSS
- IsTPMTokenReady
- InitializeTPMToken
- constructed_successfully_
- FinishInit
- InitializeNSSForChromeOSUser
- InitializeTPMForChromeOSUser
- InitializePrivateSoftwareSlotForChromeOSUser
- GetPublicSlotForChromeOSUser
- GetPrivateSlotForChromeOSUser
- PRTimeToBaseTime
- BaseTimeToPRTime
- GetPublicNSSKeySlot
- GetPrivateNSSKeySlot
#include "crypto/nss_util.h"
#include "crypto/nss_util_internal.h"
#include <nss.h>
#include <pk11pub.h>
#include <plarena.h>
#include <prerror.h>
#include <prinit.h>
#include <prtime.h>
#include <secmod.h>
#if defined(OS_OPENBSD)
#include <sys/mount.h>
#include <sys/param.h>
#endif
#include <map>
#include <vector>
#include "base/bind.h"
#include "base/cpu.h"
#include "base/debug/alias.h"
#include "base/debug/stack_trace.h"
#include "base/environment.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/native_library.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_checker.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/worker_pool.h"
#include "build/build_config.h"
#if defined(USE_NSS)
#include "base/synchronization/lock.h"
#include "crypto/nss_crypto_module_delegate.h"
#endif
namespace crypto {
namespace {
#if defined(OS_CHROMEOS)
const char kNSSDatabaseName[] = "Real NSS database";
const char kChapsModuleName[] = "Chaps";
const char kChapsPath[] = "libchaps.so";
static const base::FilePath::CharType kReadOnlyCertDB[] =
FILE_PATH_LITERAL("/etc/fake_root_ca/nssdb");
#endif
std::string GetNSSErrorMessage() {
std::string result;
if (PR_GetErrorTextLength()) {
scoped_ptr<char[]> error_text(new char[PR_GetErrorTextLength() + 1]);
PRInt32 copied = PR_GetErrorText(error_text.get());
result = std::string(error_text.get(), copied);
} else {
result = base::StringPrintf("NSS error code: %d", PR_GetError());
}
return result;
}
#if defined(USE_NSS)
base::FilePath GetDefaultConfigDirectory() {
base::FilePath dir = base::GetHomeDir();
if (dir.empty()) {
LOG(ERROR) << "Failed to get home directory.";
return dir;
}
dir = dir.AppendASCII(".pki").AppendASCII("nssdb");
if (!base::CreateDirectory(dir)) {
LOG(ERROR) << "Failed to create " << dir.value() << " directory.";
dir.clear();
}
DVLOG(2) << "DefaultConfigDirectory: " << dir.value();
return dir;
}
base::FilePath GetInitialConfigDirectory() {
#if defined(OS_CHROMEOS)
base::FilePath database_dir = base::FilePath(kReadOnlyCertDB);
if (!base::PathExists(database_dir))
database_dir.clear();
return database_dir;
#else
return GetDefaultConfigDirectory();
#endif
}
char* PKCS11PasswordFunc(PK11SlotInfo* slot, PRBool retry, void* arg) {
crypto::CryptoModuleBlockingPasswordDelegate* delegate =
reinterpret_cast<crypto::CryptoModuleBlockingPasswordDelegate*>(arg);
if (delegate) {
bool cancelled = false;
std::string password = delegate->RequestPassword(PK11_GetTokenName(slot),
retry != PR_FALSE,
&cancelled);
if (cancelled)
return NULL;
char* result = PORT_Strdup(password.c_str());
password.replace(0, password.size(), password.size(), 0);
return result;
}
DLOG(ERROR) << "PK11 password requested with NULL arg";
return NULL;
}
void UseLocalCacheOfNSSDatabaseIfNFS(const base::FilePath& database_dir) {
bool db_on_nfs = false;
#if defined(OS_LINUX)
base::FileSystemType fs_type = base::FILE_SYSTEM_UNKNOWN;
if (base::GetFileSystemType(database_dir, &fs_type))
db_on_nfs = (fs_type == base::FILE_SYSTEM_NFS);
#elif defined(OS_OPENBSD)
struct statfs buf;
if (statfs(database_dir.value().c_str(), &buf) == 0)
db_on_nfs = (strcmp(buf.f_fstypename, MOUNT_NFS) == 0);
#else
NOTIMPLEMENTED();
#endif
if (db_on_nfs) {
scoped_ptr<base::Environment> env(base::Environment::Create());
static const char kUseCacheEnvVar[] = "NSS_SDB_USE_CACHE";
if (!env->HasVar(kUseCacheEnvVar))
env->SetVar(kUseCacheEnvVar, "yes");
}
}
#endif
class NSPRInitSingleton {
private:
friend struct base::DefaultLazyInstanceTraits<NSPRInitSingleton>;
NSPRInitSingleton() {
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
}
~NSPRInitSingleton() {
PL_ArenaFinish();
PRStatus prstatus = PR_Cleanup();
if (prstatus != PR_SUCCESS)
LOG(ERROR) << "PR_Cleanup failed; was NSPR initialized on wrong thread?";
}
};
base::LazyInstance<NSPRInitSingleton>::Leaky
g_nspr_singleton = LAZY_INSTANCE_INITIALIZER;
base::LazyInstance<base::ScopedTempDir> g_test_nss_db_dir =
LAZY_INSTANCE_INITIALIZER;
void CrashOnNSSInitFailure() {
int nss_error = PR_GetError();
int os_error = PR_GetOSError();
base::debug::Alias(&nss_error);
base::debug::Alias(&os_error);
LOG(ERROR) << "Error initializing NSS without a persistent database: "
<< GetNSSErrorMessage();
LOG(FATAL) << "nss_error=" << nss_error << ", os_error=" << os_error;
}
#if defined(OS_CHROMEOS)
class ChromeOSUserData {
public:
ChromeOSUserData(ScopedPK11Slot public_slot, bool is_primary_user)
: public_slot_(public_slot.Pass()),
is_primary_user_(is_primary_user) {}
~ChromeOSUserData() {
if (public_slot_ && !is_primary_user_) {
SECStatus status = SECMOD_CloseUserDB(public_slot_.get());
if (status != SECSuccess)
PLOG(ERROR) << "SECMOD_CloseUserDB failed: " << PORT_GetError();
}
}
ScopedPK11Slot GetPublicSlot() {
return ScopedPK11Slot(
public_slot_ ? PK11_ReferenceSlot(public_slot_.get()) : NULL);
}
ScopedPK11Slot GetPrivateSlot(
const base::Callback<void(ScopedPK11Slot)>& callback) {
if (private_slot_)
return ScopedPK11Slot(PK11_ReferenceSlot(private_slot_.get()));
if (!callback.is_null())
tpm_ready_callback_list_.push_back(callback);
return ScopedPK11Slot();
}
void SetPrivateSlot(ScopedPK11Slot private_slot) {
DCHECK(!private_slot_);
private_slot_ = private_slot.Pass();
SlotReadyCallbackList callback_list;
callback_list.swap(tpm_ready_callback_list_);
for (SlotReadyCallbackList::iterator i = callback_list.begin();
i != callback_list.end();
++i) {
(*i).Run(ScopedPK11Slot(PK11_ReferenceSlot(private_slot_.get())));
}
}
private:
ScopedPK11Slot public_slot_;
ScopedPK11Slot private_slot_;
bool is_primary_user_;
typedef std::vector<base::Callback<void(ScopedPK11Slot)> >
SlotReadyCallbackList;
SlotReadyCallbackList tpm_ready_callback_list_;
};
#endif
class NSSInitSingleton {
public:
#if defined(OS_CHROMEOS)
struct TPMModuleAndSlot {
explicit TPMModuleAndSlot(SECMODModule* init_chaps_module)
: chaps_module(init_chaps_module), tpm_slot(NULL) {}
SECMODModule* chaps_module;
PK11SlotInfo* tpm_slot;
};
void OpenPersistentNSSDB() {
DCHECK(thread_checker_.CalledOnValidThread());
if (!chromeos_user_logged_in_) {
base::ThreadRestrictions::ScopedAllowIO allow_io;
chromeos_user_logged_in_ = true;
software_slot_ = OpenUserDB(GetDefaultConfigDirectory(),
kNSSDatabaseName);
}
}
PK11SlotInfo* OpenPersistentNSSDBForPath(const base::FilePath& path) {
DCHECK(thread_checker_.CalledOnValidThread());
base::ThreadRestrictions::ScopedAllowIO allow_io;
base::FilePath nssdb_path = path.AppendASCII(".pki").AppendASCII("nssdb");
if (!base::CreateDirectory(nssdb_path)) {
LOG(ERROR) << "Failed to create " << nssdb_path.value() << " directory.";
return NULL;
}
return OpenUserDB(nssdb_path, kNSSDatabaseName);
}
void EnableTPMTokenForNSS() {
DCHECK(thread_checker_.CalledOnValidThread());
tpm_token_enabled_for_nss_ = true;
}
bool IsTPMTokenEnabledForNSS() {
DCHECK(thread_checker_.CalledOnValidThread());
return tpm_token_enabled_for_nss_;
}
void InitializeTPMToken(int token_slot_id,
const base::Callback<void(bool)>& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!initializing_tpm_token_);
if (!tpm_token_enabled_for_nss_) {
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, false));
return;
}
if (tpm_slot_) {
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, true));
return;
}
scoped_ptr<TPMModuleAndSlot> tpm_args(new TPMModuleAndSlot(chaps_module_));
TPMModuleAndSlot* tpm_args_ptr = tpm_args.get();
if (base::WorkerPool::PostTaskAndReply(
FROM_HERE,
base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread,
token_slot_id,
tpm_args_ptr),
base::Bind(&NSSInitSingleton::OnInitializedTPMToken,
base::Unretained(this),
callback,
base::Passed(&tpm_args)),
true
)) {
initializing_tpm_token_ = true;
} else {
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, false));
}
}
static void InitializeTPMTokenOnWorkerThread(CK_SLOT_ID token_slot_id,
TPMModuleAndSlot* tpm_args) {
if (!tpm_args->chaps_module) {
DVLOG(3) << "Loading chaps...";
tpm_args->chaps_module = LoadModule(
kChapsModuleName,
kChapsPath,
"NSS=\"slotParams=(0={slotFlags=[PublicCerts] askpw=only})\"");
}
if (tpm_args->chaps_module) {
tpm_args->tpm_slot =
GetTPMSlotForIdOnWorkerThread(tpm_args->chaps_module, token_slot_id);
}
}
void OnInitializedTPMToken(const base::Callback<void(bool)>& callback,
scoped_ptr<TPMModuleAndSlot> tpm_args) {
DCHECK(thread_checker_.CalledOnValidThread());
DVLOG(2) << "Loaded chaps: " << !!tpm_args->chaps_module
<< ", got tpm slot: " << !!tpm_args->tpm_slot;
chaps_module_ = tpm_args->chaps_module;
tpm_slot_ = tpm_args->tpm_slot;
if (!chaps_module_ && test_slot_) {
tpm_slot_ = PK11_ReferenceSlot(test_slot_);
}
initializing_tpm_token_ = false;
if (tpm_slot_) {
TPMReadyCallbackList callback_list;
callback_list.swap(tpm_ready_callback_list_);
for (TPMReadyCallbackList::iterator i = callback_list.begin();
i != callback_list.end();
++i) {
(*i).Run();
}
}
callback.Run(!!tpm_slot_);
}
bool IsTPMTokenReady(const base::Closure& callback) {
if (!callback.is_null()) {
DCHECK(thread_checker_.CalledOnValidThread());
} else if (!thread_checker_.CalledOnValidThread()) {
DVLOG(1) << "Called on wrong thread.\n"
<< base::debug::StackTrace().ToString();
}
if (tpm_slot_ != NULL)
return true;
if (!callback.is_null())
tpm_ready_callback_list_.push_back(callback);
return false;
}
static PK11SlotInfo* GetTPMSlotForIdOnWorkerThread(SECMODModule* chaps_module,
CK_SLOT_ID slot_id) {
DCHECK(chaps_module);
DVLOG(3) << "Poking chaps module.";
SECStatus rv = SECMOD_UpdateSlotList(chaps_module);
if (rv != SECSuccess)
PLOG(ERROR) << "SECMOD_UpdateSlotList failed: " << PORT_GetError();
PK11SlotInfo* slot = SECMOD_LookupSlot(chaps_module->moduleID, slot_id);
if (!slot)
LOG(ERROR) << "TPM slot " << slot_id << " not found.";
return slot;
}
bool InitializeNSSForChromeOSUser(
const std::string& email,
const std::string& username_hash,
bool is_primary_user,
const base::FilePath& path) {
DCHECK(thread_checker_.CalledOnValidThread());
if (chromeos_user_map_.find(username_hash) != chromeos_user_map_.end()) {
DVLOG(2) << username_hash << " already initialized.";
return false;
}
ScopedPK11Slot public_slot;
if (is_primary_user) {
DVLOG(2) << "Primary user, using GetPublicNSSKeySlot()";
public_slot.reset(GetPublicNSSKeySlot());
} else {
DVLOG(2) << "Opening NSS DB " << path.value();
public_slot.reset(OpenPersistentNSSDBForPath(path));
}
chromeos_user_map_[username_hash] =
new ChromeOSUserData(public_slot.Pass(), is_primary_user);
return true;
}
void InitializeTPMForChromeOSUser(const std::string& username_hash,
CK_SLOT_ID slot_id) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end());
if (!chaps_module_)
return;
scoped_ptr<TPMModuleAndSlot> tpm_args(new TPMModuleAndSlot(chaps_module_));
TPMModuleAndSlot* tpm_args_ptr = tpm_args.get();
base::WorkerPool::PostTaskAndReply(
FROM_HERE,
base::Bind(&NSSInitSingleton::InitializeTPMTokenOnWorkerThread,
slot_id,
tpm_args_ptr),
base::Bind(&NSSInitSingleton::OnInitializedTPMForChromeOSUser,
base::Unretained(this),
username_hash,
base::Passed(&tpm_args)),
true
);
}
void OnInitializedTPMForChromeOSUser(const std::string& username_hash,
scoped_ptr<TPMModuleAndSlot> tpm_args) {
DCHECK(thread_checker_.CalledOnValidThread());
DVLOG(2) << "Got tpm slot for " << username_hash << " "
<< !!tpm_args->tpm_slot;
chromeos_user_map_[username_hash]->SetPrivateSlot(
ScopedPK11Slot(tpm_args->tpm_slot));
}
void InitializePrivateSoftwareSlotForChromeOSUser(
const std::string& username_hash) {
DCHECK(thread_checker_.CalledOnValidThread());
VLOG(1) << "using software private slot for " << username_hash;
DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end());
chromeos_user_map_[username_hash]->SetPrivateSlot(
chromeos_user_map_[username_hash]->GetPublicSlot());
}
ScopedPK11Slot GetPublicSlotForChromeOSUser(
const std::string& username_hash) {
DCHECK(thread_checker_.CalledOnValidThread());
if (username_hash.empty()) {
DVLOG(2) << "empty username_hash";
return ScopedPK11Slot();
}
if (test_slot_) {
DVLOG(2) << "returning test_slot_ for " << username_hash;
return ScopedPK11Slot(PK11_ReferenceSlot(test_slot_));
}
if (chromeos_user_map_.find(username_hash) == chromeos_user_map_.end()) {
LOG(ERROR) << username_hash << " not initialized.";
return ScopedPK11Slot();
}
return chromeos_user_map_[username_hash]->GetPublicSlot();
}
ScopedPK11Slot GetPrivateSlotForChromeOSUser(
const std::string& username_hash,
const base::Callback<void(ScopedPK11Slot)>& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (username_hash.empty()) {
DVLOG(2) << "empty username_hash";
if (!callback.is_null()) {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(callback, base::Passed(ScopedPK11Slot())));
}
return ScopedPK11Slot();
}
DCHECK(chromeos_user_map_.find(username_hash) != chromeos_user_map_.end());
if (test_slot_) {
DVLOG(2) << "returning test_slot_ for " << username_hash;
return ScopedPK11Slot(PK11_ReferenceSlot(test_slot_));
}
return chromeos_user_map_[username_hash]->GetPrivateSlot(callback);
}
void CloseTestChromeOSUser(const std::string& username_hash) {
DCHECK(thread_checker_.CalledOnValidThread());
ChromeOSUserMap::iterator i = chromeos_user_map_.find(username_hash);
DCHECK(i != chromeos_user_map_.end());
delete i->second;
chromeos_user_map_.erase(i);
}
#endif
bool OpenTestNSSDB() {
DCHECK(thread_checker_.CalledOnValidThread());
base::ThreadRestrictions::ScopedAllowIO allow_io;
if (test_slot_)
return true;
if (!g_test_nss_db_dir.Get().CreateUniqueTempDir())
return false;
test_slot_ = OpenUserDB(g_test_nss_db_dir.Get().path(), kTestTPMTokenName);
return !!test_slot_;
}
void CloseTestNSSDB() {
DCHECK(thread_checker_.CalledOnValidThread());
base::ThreadRestrictions::ScopedAllowIO allow_io;
if (!test_slot_)
return;
SECStatus status = SECMOD_CloseUserDB(test_slot_);
if (status != SECSuccess)
PLOG(ERROR) << "SECMOD_CloseUserDB failed: " << PORT_GetError();
PK11_FreeSlot(test_slot_);
test_slot_ = NULL;
ignore_result(g_test_nss_db_dir.Get().Delete());
}
PK11SlotInfo* GetPublicNSSKeySlot() {
if (!thread_checker_.CalledOnValidThread()) {
DVLOG(1) << "Called on wrong thread.\n"
<< base::debug::StackTrace().ToString();
}
if (test_slot_)
return PK11_ReferenceSlot(test_slot_);
if (software_slot_)
return PK11_ReferenceSlot(software_slot_);
return PK11_GetInternalKeySlot();
}
PK11SlotInfo* GetPrivateNSSKeySlot() {
if (!thread_checker_.CalledOnValidThread()) {
DVLOG(1) << "Called on wrong thread.\n"
<< base::debug::StackTrace().ToString();
}
if (test_slot_)
return PK11_ReferenceSlot(test_slot_);
#if defined(OS_CHROMEOS)
if (tpm_token_enabled_for_nss_) {
if (IsTPMTokenReady(base::Closure())) {
return PK11_ReferenceSlot(tpm_slot_);
} else {
return NULL;
}
}
#endif
if (software_slot_)
return PK11_ReferenceSlot(software_slot_);
return PK11_GetInternalKeySlot();
}
#if defined(USE_NSS)
base::Lock* write_lock() {
return &write_lock_;
}
#endif
static void ForceNoDBInit() {
force_nodb_init_ = true;
}
private:
friend struct base::DefaultLazyInstanceTraits<NSSInitSingleton>;
NSSInitSingleton()
: tpm_token_enabled_for_nss_(false),
initializing_tpm_token_(false),
chaps_module_(NULL),
software_slot_(NULL),
test_slot_(NULL),
tpm_slot_(NULL),
root_(NULL),
chromeos_user_logged_in_(false) {
base::TimeTicks start_time = base::TimeTicks::Now();
thread_checker_.DetachFromThread();
DisableAESNIIfNeeded();
EnsureNSPRInit();
COMPILE_ASSERT(
(NSS_VMAJOR == 3 && NSS_VMINOR == 14 && NSS_VPATCH >= 3) ||
(NSS_VMAJOR == 3 && NSS_VMINOR > 14) ||
(NSS_VMAJOR > 3),
nss_version_check_failed);
if (!NSS_VersionCheck("3.14.3")) {
LOG(FATAL) << "NSS_VersionCheck(\"3.14.3\") failed. NSS >= 3.14.3 is "
"required. Please upgrade to the latest NSS, and if you "
"still get this error, contact your distribution "
"maintainer.";
}
SECStatus status = SECFailure;
bool nodb_init = force_nodb_init_;
#if !defined(USE_NSS)
nodb_init = true;
#endif
if (nodb_init) {
status = NSS_NoDB_Init(NULL);
if (status != SECSuccess) {
CrashOnNSSInitFailure();
return;
}
#if defined(OS_IOS)
root_ = InitDefaultRootCerts();
#endif
} else {
#if defined(USE_NSS)
base::FilePath database_dir = GetInitialConfigDirectory();
if (!database_dir.empty()) {
UseLocalCacheOfNSSDatabaseIfNFS(database_dir);
std::string nss_config_dir =
base::StringPrintf("sql:%s", database_dir.value().c_str());
#if defined(OS_CHROMEOS)
status = NSS_Init(nss_config_dir.c_str());
#else
status = NSS_InitReadWrite(nss_config_dir.c_str());
#endif
if (status != SECSuccess) {
LOG(ERROR) << "Error initializing NSS with a persistent "
"database (" << nss_config_dir
<< "): " << GetNSSErrorMessage();
}
}
if (status != SECSuccess) {
VLOG(1) << "Initializing NSS without a persistent database.";
status = NSS_NoDB_Init(NULL);
if (status != SECSuccess) {
CrashOnNSSInitFailure();
return;
}
}
PK11_SetPasswordFunc(PKCS11PasswordFunc);
PK11SlotInfo* slot = PK11_GetInternalKeySlot();
if (slot) {
if (PK11_NeedUserInit(slot))
PK11_InitPin(slot, NULL, NULL);
PK11_FreeSlot(slot);
}
root_ = InitDefaultRootCerts();
#endif
}
NSS_SetAlgorithmPolicy(SEC_OID_MD5, 0, NSS_USE_ALG_IN_CERT_SIGNATURE);
NSS_SetAlgorithmPolicy(SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION,
0, NSS_USE_ALG_IN_CERT_SIGNATURE);
HISTOGRAM_CUSTOM_TIMES("Startup.SlowStartupNSSInit",
base::TimeTicks::Now() - start_time,
base::TimeDelta::FromMilliseconds(10),
base::TimeDelta::FromHours(1),
50);
}
~NSSInitSingleton() {
#if defined(OS_CHROMEOS)
STLDeleteValues(&chromeos_user_map_);
#endif
if (tpm_slot_) {
PK11_FreeSlot(tpm_slot_);
tpm_slot_ = NULL;
}
if (software_slot_) {
SECMOD_CloseUserDB(software_slot_);
PK11_FreeSlot(software_slot_);
software_slot_ = NULL;
}
CloseTestNSSDB();
if (root_) {
SECMOD_UnloadUserModule(root_);
SECMOD_DestroyModule(root_);
root_ = NULL;
}
if (chaps_module_) {
SECMOD_UnloadUserModule(chaps_module_);
SECMOD_DestroyModule(chaps_module_);
chaps_module_ = NULL;
}
SECStatus status = NSS_Shutdown();
if (status != SECSuccess) {
VLOG(1) << "NSS_Shutdown failed; see http://crbug.com/4609";
}
}
#if defined(USE_NSS) || defined(OS_IOS)
SECMODModule* InitDefaultRootCerts() {
SECMODModule* root = LoadModule("Root Certs", "libnssckbi.so", NULL);
if (root)
return root;
return NULL;
}
static SECMODModule* LoadModule(const char* name,
const char* library_path,
const char* params) {
std::string modparams = base::StringPrintf(
"name=\"%s\" library=\"%s\" %s",
name, library_path, params ? params : "");
SECMODModule* module = SECMOD_LoadUserModule(
const_cast<char*>(modparams.c_str()), NULL, PR_FALSE);
if (!module) {
LOG(ERROR) << "Error loading " << name << " module into NSS: "
<< GetNSSErrorMessage();
return NULL;
}
if (!module->loaded) {
LOG(ERROR) << "After loading " << name << ", loaded==false: "
<< GetNSSErrorMessage();
SECMOD_DestroyModule(module);
return NULL;
}
return module;
}
#endif
static PK11SlotInfo* OpenUserDB(const base::FilePath& path,
const char* description) {
const std::string modspec =
base::StringPrintf("configDir='sql:%s' tokenDescription='%s'",
path.value().c_str(), description);
PK11SlotInfo* db_slot = SECMOD_OpenUserDB(modspec.c_str());
if (db_slot) {
if (PK11_NeedUserInit(db_slot))
PK11_InitPin(db_slot, NULL, NULL);
}
else {
LOG(ERROR) << "Error opening persistent database (" << modspec
<< "): " << GetNSSErrorMessage();
}
return db_slot;
}
static void DisableAESNIIfNeeded() {
if (NSS_VersionCheck("3.15") && !NSS_VersionCheck("3.15.4")) {
base::CPU cpu;
if (cpu.has_avx_hardware() && !cpu.has_avx()) {
base::Environment::Create()->SetVar("NSS_DISABLE_HW_AES", "1");
}
}
}
static bool force_nodb_init_;
bool tpm_token_enabled_for_nss_;
bool initializing_tpm_token_;
typedef std::vector<base::Closure> TPMReadyCallbackList;
TPMReadyCallbackList tpm_ready_callback_list_;
SECMODModule* chaps_module_;
PK11SlotInfo* software_slot_;
PK11SlotInfo* test_slot_;
PK11SlotInfo* tpm_slot_;
SECMODModule* root_;
bool chromeos_user_logged_in_;
#if defined(OS_CHROMEOS)
typedef std::map<std::string, ChromeOSUserData*> ChromeOSUserMap;
ChromeOSUserMap chromeos_user_map_;
#endif
#if defined(USE_NSS)
base::Lock write_lock_;
#endif
base::ThreadChecker thread_checker_;
};
bool NSSInitSingleton::force_nodb_init_ = false;
base::LazyInstance<NSSInitSingleton>::Leaky
g_nss_singleton = LAZY_INSTANCE_INITIALIZER;
}
const char kTestTPMTokenName[] = "Test DB";
#if defined(USE_NSS)
void EarlySetupForNSSInit() {
base::FilePath database_dir = GetInitialConfigDirectory();
if (!database_dir.empty())
UseLocalCacheOfNSSDatabaseIfNFS(database_dir);
}
#endif
void EnsureNSPRInit() {
g_nspr_singleton.Get();
}
void InitNSSSafely() {
DisableNSSForkCheck();
ForceNSSNoDBInit();
EnsureNSSInit();
}
void EnsureNSSInit() {
base::ThreadRestrictions::ScopedAllowIO allow_io;
g_nss_singleton.Get();
}
void ForceNSSNoDBInit() {
NSSInitSingleton::ForceNoDBInit();
}
void DisableNSSForkCheck() {
scoped_ptr<base::Environment> env(base::Environment::Create());
env->SetVar("NSS_STRICT_NOFORK", "DISABLED");
}
void LoadNSSLibraries() {
#if defined(USE_NSS)
std::vector<base::FilePath> paths;
paths.push_back(base::FilePath());
paths.push_back(base::FilePath("/usr/lib/nss"));
#if defined(ARCH_CPU_X86_64)
paths.push_back(base::FilePath("/usr/lib/x86_64-linux-gnu/nss"));
#elif defined(ARCH_CPU_X86)
paths.push_back(base::FilePath("/usr/lib/i386-linux-gnu/nss"));
#elif defined(ARCH_CPU_ARMEL)
#if defined(__ARM_PCS_VFP)
paths.push_back(base::FilePath("/usr/lib/arm-linux-gnueabihf/nss"));
#else
paths.push_back(base::FilePath("/usr/lib/arm-linux-gnueabi/nss"));
#endif
#elif defined(ARCH_CPU_MIPSEL)
paths.push_back(base::FilePath("/usr/lib/mipsel-linux-gnu/nss"));
#endif
std::vector<std::string> libs;
libs.push_back("libsoftokn3.so");
libs.push_back("libfreebl3.so");
size_t loaded = 0;
for (size_t i = 0; i < libs.size(); ++i) {
for (size_t j = 0; j < paths.size(); ++j) {
base::FilePath path = paths[j].Append(libs[i]);
base::NativeLibrary lib = base::LoadNativeLibrary(path, NULL);
if (lib) {
++loaded;
break;
}
}
}
if (loaded == libs.size()) {
VLOG(3) << "NSS libraries loaded.";
} else {
LOG(ERROR) << "Failed to load NSS libraries.";
}
#endif
}
bool CheckNSSVersion(const char* version) {
return !!NSS_VersionCheck(version);
}
#if defined(USE_NSS)
ScopedTestNSSDB::ScopedTestNSSDB()
: is_open_(g_nss_singleton.Get().OpenTestNSSDB()) {
}
ScopedTestNSSDB::~ScopedTestNSSDB() {
if (NSS_VersionCheck("3.15.1")) {
g_nss_singleton.Get().CloseTestNSSDB();
}
}
base::Lock* GetNSSWriteLock() {
return g_nss_singleton.Get().write_lock();
}
AutoNSSWriteLock::AutoNSSWriteLock() : lock_(GetNSSWriteLock()) {
if (lock_)
lock_->Acquire();
}
AutoNSSWriteLock::~AutoNSSWriteLock() {
if (lock_) {
lock_->AssertAcquired();
lock_->Release();
}
}
AutoSECMODListReadLock::AutoSECMODListReadLock()
: lock_(SECMOD_GetDefaultModuleListLock()) {
SECMOD_GetReadLock(lock_);
}
AutoSECMODListReadLock::~AutoSECMODListReadLock() {
SECMOD_ReleaseReadLock(lock_);
}
#endif
#if defined(OS_CHROMEOS)
void OpenPersistentNSSDB() {
g_nss_singleton.Get().OpenPersistentNSSDB();
}
void EnableTPMTokenForNSS() {
g_nss_singleton.Get().EnableTPMTokenForNSS();
}
bool IsTPMTokenEnabledForNSS() {
return g_nss_singleton.Get().IsTPMTokenEnabledForNSS();
}
bool IsTPMTokenReady(const base::Closure& callback) {
return g_nss_singleton.Get().IsTPMTokenReady(callback);
}
void InitializeTPMToken(int token_slot_id,
const base::Callback<void(bool)>& callback) {
g_nss_singleton.Get().InitializeTPMToken(token_slot_id, callback);
}
ScopedTestNSSChromeOSUser::ScopedTestNSSChromeOSUser(
const std::string& username_hash)
: username_hash_(username_hash), constructed_successfully_(false) {
if (!temp_dir_.CreateUniqueTempDir())
return;
constructed_successfully_ =
InitializeNSSForChromeOSUser(username_hash,
username_hash,
false ,
temp_dir_.path());
}
ScopedTestNSSChromeOSUser::~ScopedTestNSSChromeOSUser() {
if (constructed_successfully_)
g_nss_singleton.Get().CloseTestChromeOSUser(username_hash_);
}
void ScopedTestNSSChromeOSUser::FinishInit() {
InitializePrivateSoftwareSlotForChromeOSUser(username_hash_);
}
bool InitializeNSSForChromeOSUser(
const std::string& email,
const std::string& username_hash,
bool is_primary_user,
const base::FilePath& path) {
return g_nss_singleton.Get().InitializeNSSForChromeOSUser(
email, username_hash, is_primary_user, path);
}
void InitializeTPMForChromeOSUser(
const std::string& username_hash,
CK_SLOT_ID slot_id) {
g_nss_singleton.Get().InitializeTPMForChromeOSUser(username_hash, slot_id);
}
void InitializePrivateSoftwareSlotForChromeOSUser(
const std::string& username_hash) {
g_nss_singleton.Get().InitializePrivateSoftwareSlotForChromeOSUser(
username_hash);
}
ScopedPK11Slot GetPublicSlotForChromeOSUser(const std::string& username_hash) {
return g_nss_singleton.Get().GetPublicSlotForChromeOSUser(username_hash);
}
ScopedPK11Slot GetPrivateSlotForChromeOSUser(
const std::string& username_hash,
const base::Callback<void(ScopedPK11Slot)>& callback) {
return g_nss_singleton.Get().GetPrivateSlotForChromeOSUser(username_hash,
callback);
}
#endif
base::Time PRTimeToBaseTime(PRTime prtime) {
return base::Time::FromInternalValue(
prtime + base::Time::UnixEpoch().ToInternalValue());
}
PRTime BaseTimeToPRTime(base::Time time) {
return time.ToInternalValue() - base::Time::UnixEpoch().ToInternalValue();
}
PK11SlotInfo* GetPublicNSSKeySlot() {
return g_nss_singleton.Get().GetPublicNSSKeySlot();
}
PK11SlotInfo* GetPrivateNSSKeySlot() {
return g_nss_singleton.Get().GetPrivateNSSKeySlot();
}
}