2012-10-01 22:02:15 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-06-06 16:40:11 +04:00
|
|
|
/*
|
|
|
|
* Initialize the PCKS 11 subsystem
|
|
|
|
*/
|
|
|
|
#include "seccomon.h"
|
|
|
|
#include "secmod.h"
|
|
|
|
#include "nssilock.h"
|
|
|
|
#include "secmodi.h"
|
|
|
|
#include "secmodti.h"
|
|
|
|
#include "pk11func.h"
|
|
|
|
#include "pki3hack.h"
|
|
|
|
#include "secerr.h"
|
|
|
|
#include "dev.h"
|
2012-10-01 22:02:15 +04:00
|
|
|
#include "utilpars.h"
|
2017-05-16 18:12:24 +03:00
|
|
|
#include "pkcs11uri.h"
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
/* these are for displaying error messages */
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
static SECMODModuleList *modules = NULL;
|
|
|
|
static SECMODModuleList *modulesDB = NULL;
|
|
|
|
static SECMODModuleList *modulesUnload = NULL;
|
|
|
|
static SECMODModule *internalModule = NULL;
|
|
|
|
static SECMODModule *defaultDBModule = NULL;
|
|
|
|
static SECMODModule *pendingModule = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
static SECMODListLock *moduleLock = NULL;
|
|
|
|
|
|
|
|
int secmod_PrivateModuleCount = 0;
|
|
|
|
|
2014-04-25 17:06:01 +04:00
|
|
|
extern const PK11DefaultArrayEntry PK11_DefaultArray[];
|
|
|
|
extern const int num_pk11_default_mechanisms;
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
void
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_Init()
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
/* don't initialize twice */
|
2016-11-02 12:29:58 +03:00
|
|
|
if (moduleLock)
|
|
|
|
return;
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
moduleLock = SECMOD_NewListLock();
|
|
|
|
PK11_InitSlotLists();
|
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_Shutdown()
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
/* destroy the lock */
|
|
|
|
if (moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyListLock(moduleLock);
|
|
|
|
moduleLock = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
/* free the internal module */
|
|
|
|
if (internalModule) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModule(internalModule);
|
|
|
|
internalModule = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free the default database module */
|
|
|
|
if (defaultDBModule) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModule(defaultDBModule);
|
|
|
|
defaultDBModule = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2016-11-02 12:29:58 +03:00
|
|
|
|
2008-06-06 16:40:11 +04:00
|
|
|
/* destroy the list */
|
|
|
|
if (modules) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModuleList(modules);
|
|
|
|
modules = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2016-11-02 12:29:58 +03:00
|
|
|
|
2008-06-06 16:40:11 +04:00
|
|
|
if (modulesDB) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModuleList(modulesDB);
|
|
|
|
modulesDB = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modulesUnload) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModuleList(modulesUnload);
|
|
|
|
modulesUnload = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make all the slots and the lists go away */
|
|
|
|
PK11_DestroySlotLists();
|
|
|
|
|
|
|
|
nss_DumpModuleLog();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2016-02-02 13:50:47 +03:00
|
|
|
if (PR_GetEnvSecure("NSS_STRICT_SHUTDOWN")) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_Assert(secmod_PrivateModuleCount == 0);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (secmod_PrivateModuleCount) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_BUSY);
|
|
|
|
return SECFailure;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* retrieve the internal module
|
|
|
|
*/
|
|
|
|
SECMODModule *
|
|
|
|
SECMOD_GetInternalModule(void)
|
|
|
|
{
|
2016-11-02 12:29:58 +03:00
|
|
|
return internalModule;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus
|
2016-11-02 12:29:58 +03:00
|
|
|
secmod_AddModuleToList(SECMODModuleList **moduleList, SECMODModule *newModule)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECMODModuleList *mlp, *newListElement, *last = NULL;
|
|
|
|
|
|
|
|
newListElement = SECMOD_NewModuleListElement();
|
|
|
|
if (newListElement == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
return SECFailure;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
newListElement->module = SECMOD_ReferenceModule(newModule);
|
|
|
|
|
|
|
|
SECMOD_GetWriteLock(moduleLock);
|
|
|
|
/* Added it to the end (This is very inefficient, but Adding a module
|
|
|
|
* on the fly should happen maybe 2-3 times through the life this program
|
|
|
|
* on a given computer, and this list should be *SHORT*. */
|
2016-11-02 12:29:58 +03:00
|
|
|
for (mlp = *moduleList; mlp != NULL; mlp = mlp->next) {
|
|
|
|
last = mlp;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (last == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
*moduleList = newListElement;
|
2008-06-06 16:40:11 +04:00
|
|
|
} else {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_AddList(last, newListElement, NULL);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
SECMOD_ReleaseWriteLock(moduleLock);
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus
|
|
|
|
SECMOD_AddModuleToList(SECMODModule *newModule)
|
|
|
|
{
|
|
|
|
if (newModule->internal && !internalModule) {
|
2016-11-02 12:29:58 +03:00
|
|
|
internalModule = SECMOD_ReferenceModule(newModule);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2016-11-02 12:29:58 +03:00
|
|
|
return secmod_AddModuleToList(&modules, newModule);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus
|
|
|
|
SECMOD_AddModuleToDBOnlyList(SECMODModule *newModule)
|
|
|
|
{
|
2010-02-07 14:54:28 +03:00
|
|
|
if (defaultDBModule && SECMOD_GetDefaultModDBFlag(newModule)) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModule(defaultDBModule);
|
|
|
|
defaultDBModule = SECMOD_ReferenceModule(newModule);
|
2010-02-07 14:54:28 +03:00
|
|
|
} else if (defaultDBModule == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
defaultDBModule = SECMOD_ReferenceModule(newModule);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2016-11-02 12:29:58 +03:00
|
|
|
return secmod_AddModuleToList(&modulesDB, newModule);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus
|
|
|
|
SECMOD_AddModuleToUnloadList(SECMODModule *newModule)
|
|
|
|
{
|
2016-11-02 12:29:58 +03:00
|
|
|
return secmod_AddModuleToList(&modulesUnload, newModule);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the list of PKCS11 modules that are available.
|
|
|
|
*/
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMODModuleList *
|
|
|
|
SECMOD_GetDefaultModuleList()
|
|
|
|
{
|
|
|
|
return modules;
|
|
|
|
}
|
|
|
|
SECMODModuleList *
|
|
|
|
SECMOD_GetDeadModuleList()
|
|
|
|
{
|
|
|
|
return modulesUnload;
|
|
|
|
}
|
|
|
|
SECMODModuleList *
|
|
|
|
SECMOD_GetDBModuleList()
|
|
|
|
{
|
|
|
|
return modulesDB;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This lock protects the global module lists.
|
2016-11-02 12:29:58 +03:00
|
|
|
* it also protects changes to the slot array (module->slots[]) and slot count
|
|
|
|
* (module->slotCount) in each module. It is a read/write lock with multiple
|
|
|
|
* readers or one writer. Writes are uncommon.
|
|
|
|
* Because of legacy considerations protection of the slot array and count is
|
|
|
|
* only necessary in applications if the application calls
|
2008-06-06 16:40:11 +04:00
|
|
|
* SECMOD_UpdateSlotList() or SECMOD_WaitForAnyTokenEvent(), though all new
|
|
|
|
* applications are encouraged to acquire this lock when reading the
|
|
|
|
* slot array information directly.
|
|
|
|
*/
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMODListLock *
|
|
|
|
SECMOD_GetDefaultModuleListLock()
|
|
|
|
{
|
|
|
|
return moduleLock;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* find a module by name, and add a reference to it.
|
|
|
|
* return that module.
|
|
|
|
*/
|
|
|
|
SECMODModule *
|
|
|
|
SECMOD_FindModule(const char *name)
|
|
|
|
{
|
|
|
|
SECMODModuleList *mlp;
|
|
|
|
SECMODModule *module = NULL;
|
|
|
|
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return module;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
SECMOD_GetReadLock(moduleLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
for (mlp = modules; mlp != NULL; mlp = mlp->next) {
|
|
|
|
if (PORT_Strcmp(name, mlp->module->commonName) == 0) {
|
|
|
|
module = mlp->module;
|
|
|
|
SECMOD_ReferenceModule(module);
|
|
|
|
break;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
if (module) {
|
2016-11-02 12:29:58 +03:00
|
|
|
goto found;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2016-11-02 12:29:58 +03:00
|
|
|
for (mlp = modulesUnload; mlp != NULL; mlp = mlp->next) {
|
|
|
|
if (PORT_Strcmp(name, mlp->module->commonName) == 0) {
|
|
|
|
module = mlp->module;
|
|
|
|
SECMOD_ReferenceModule(module);
|
|
|
|
break;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
found:
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find a module by ID, and add a reference to it.
|
|
|
|
* return that module.
|
|
|
|
*/
|
|
|
|
SECMODModule *
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_FindModuleByID(SECMODModuleID id)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECMODModuleList *mlp;
|
|
|
|
SECMODModule *module = NULL;
|
|
|
|
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return module;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
SECMOD_GetReadLock(moduleLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
for (mlp = modules; mlp != NULL; mlp = mlp->next) {
|
|
|
|
if (id == mlp->module->moduleID) {
|
|
|
|
module = mlp->module;
|
|
|
|
SECMOD_ReferenceModule(module);
|
|
|
|
break;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
if (module == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NO_MODULE);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2010-02-07 14:54:28 +03:00
|
|
|
/*
|
|
|
|
* find the function pointer.
|
|
|
|
*/
|
|
|
|
SECMODModule *
|
2016-11-02 12:29:58 +03:00
|
|
|
secmod_FindModuleByFuncPtr(void *funcPtr)
|
2010-02-07 14:54:28 +03:00
|
|
|
{
|
|
|
|
SECMODModuleList *mlp;
|
|
|
|
SECMODModule *module = NULL;
|
|
|
|
|
|
|
|
SECMOD_GetReadLock(moduleLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
for (mlp = modules; mlp != NULL; mlp = mlp->next) {
|
|
|
|
/* paranoia, shouldn't ever happen */
|
|
|
|
if (!mlp->module) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (funcPtr == mlp->module->functionList) {
|
|
|
|
module = mlp->module;
|
|
|
|
SECMOD_ReferenceModule(module);
|
|
|
|
break;
|
|
|
|
}
|
2010-02-07 14:54:28 +03:00
|
|
|
}
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
if (module == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NO_MODULE);
|
2010-02-07 14:54:28 +03:00
|
|
|
}
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2008-06-06 16:40:11 +04:00
|
|
|
/*
|
|
|
|
* Find the Slot based on ID and the module.
|
|
|
|
*/
|
|
|
|
PK11SlotInfo *
|
|
|
|
SECMOD_FindSlotByID(SECMODModule *module, CK_SLOT_ID slotID)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PK11SlotInfo *slot = NULL;
|
|
|
|
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return slot;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
SECMOD_GetReadLock(moduleLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
for (i = 0; i < module->slotCount; i++) {
|
|
|
|
PK11SlotInfo *cSlot = module->slots[i];
|
2008-06-06 16:40:11 +04:00
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
if (cSlot->slotID == slotID) {
|
|
|
|
slot = PK11_ReferenceSlot(cSlot);
|
|
|
|
break;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
|
|
|
|
if (slot == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NO_SLOT_SELECTED);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lookup the Slot module based on it's module ID and slot ID.
|
|
|
|
*/
|
|
|
|
PK11SlotInfo *
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_LookupSlot(SECMODModuleID moduleID, CK_SLOT_ID slotID)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECMODModule *module;
|
|
|
|
PK11SlotInfo *slot;
|
|
|
|
|
|
|
|
module = SECMOD_FindModuleByID(moduleID);
|
2016-11-02 12:29:58 +03:00
|
|
|
if (module == NULL)
|
|
|
|
return NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
slot = SECMOD_FindSlotByID(module, slotID);
|
|
|
|
SECMOD_DestroyModule(module);
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find a module by name or module pointer and delete it off the module list.
|
|
|
|
* optionally remove it from secmod.db.
|
|
|
|
*/
|
|
|
|
SECStatus
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DeleteModuleEx(const char *name, SECMODModule *mod,
|
|
|
|
int *type, PRBool permdb)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECMODModuleList *mlp;
|
|
|
|
SECMODModuleList **mlpp;
|
|
|
|
SECStatus rv = SECFailure;
|
|
|
|
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return rv;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
|
|
|
|
2008-06-06 16:40:11 +04:00
|
|
|
*type = SECMOD_EXTERNAL;
|
|
|
|
|
|
|
|
SECMOD_GetWriteLock(moduleLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
for (mlpp = &modules, mlp = modules;
|
|
|
|
mlp != NULL; mlpp = &mlp->next, mlp = *mlpp) {
|
|
|
|
if ((name && (PORT_Strcmp(name, mlp->module->commonName) == 0)) ||
|
|
|
|
mod == mlp->module) {
|
|
|
|
/* don't delete the internal module */
|
|
|
|
if (!mlp->module->internal) {
|
|
|
|
SECMOD_RemoveList(mlpp, mlp);
|
|
|
|
/* delete it after we release the lock */
|
|
|
|
rv = STAN_RemoveModuleFromDefaultTrustDomain(mlp->module);
|
|
|
|
} else if (mlp->module->isFIPS) {
|
|
|
|
*type = SECMOD_FIPS;
|
|
|
|
} else {
|
|
|
|
*type = SECMOD_INTERNAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
if (mlp) {
|
2016-11-02 12:29:58 +03:00
|
|
|
goto found;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
/* not on the internal list, check the unload list */
|
2016-11-02 12:29:58 +03:00
|
|
|
for (mlpp = &modulesUnload, mlp = modulesUnload;
|
|
|
|
mlp != NULL; mlpp = &mlp->next, mlp = *mlpp) {
|
|
|
|
if ((name && (PORT_Strcmp(name, mlp->module->commonName) == 0)) ||
|
|
|
|
mod == mlp->module) {
|
|
|
|
/* don't delete the internal module */
|
|
|
|
if (!mlp->module->internal) {
|
|
|
|
SECMOD_RemoveList(mlpp, mlp);
|
|
|
|
rv = SECSuccess;
|
|
|
|
} else if (mlp->module->isFIPS) {
|
|
|
|
*type = SECMOD_FIPS;
|
|
|
|
} else {
|
|
|
|
*type = SECMOD_INTERNAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
found:
|
|
|
|
SECMOD_ReleaseWriteLock(moduleLock);
|
|
|
|
|
|
|
|
if (rv == SECSuccess) {
|
2016-11-02 12:29:58 +03:00
|
|
|
if (permdb) {
|
|
|
|
SECMOD_DeletePermDB(mlp->module);
|
|
|
|
}
|
|
|
|
SECMOD_DestroyModuleListElement(mlp);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find a module by name and delete it off the module list
|
|
|
|
*/
|
|
|
|
SECStatus
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DeleteModule(const char *name, int *type)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
return SECMOD_DeleteModuleEx(name, NULL, type, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find a module by name and delete it off the module list
|
|
|
|
*/
|
|
|
|
SECStatus
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DeleteInternalModule(const char *name)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECMODModuleList *mlp;
|
|
|
|
SECMODModuleList **mlpp;
|
|
|
|
SECStatus rv = SECFailure;
|
|
|
|
|
|
|
|
if (pendingModule) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_MODULE_STUCK);
|
|
|
|
return rv;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return rv;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
|
2017-11-24 11:00:26 +03:00
|
|
|
#ifdef NSS_FIPS_DISABLED
|
|
|
|
PORT_SetError(PR_OPERATION_NOT_SUPPORTED_ERROR);
|
|
|
|
return rv;
|
|
|
|
#endif
|
|
|
|
|
2008-06-06 16:40:11 +04:00
|
|
|
SECMOD_GetWriteLock(moduleLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
for (mlpp = &modules, mlp = modules;
|
|
|
|
mlp != NULL; mlpp = &mlp->next, mlp = *mlpp) {
|
|
|
|
if (PORT_Strcmp(name, mlp->module->commonName) == 0) {
|
|
|
|
/* don't delete the internal module */
|
|
|
|
if (mlp->module->internal) {
|
|
|
|
SECMOD_RemoveList(mlpp, mlp);
|
|
|
|
rv = STAN_RemoveModuleFromDefaultTrustDomain(mlp->module);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
SECMOD_ReleaseWriteLock(moduleLock);
|
|
|
|
|
|
|
|
if (rv == SECSuccess) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMODModule *newModule, *oldModule;
|
|
|
|
|
|
|
|
if (mlp->module->isFIPS) {
|
|
|
|
newModule = SECMOD_CreateModule(NULL, SECMOD_INT_NAME,
|
|
|
|
NULL, SECMOD_INT_FLAGS);
|
|
|
|
} else {
|
|
|
|
newModule = SECMOD_CreateModule(NULL, SECMOD_FIPS_NAME,
|
|
|
|
NULL, SECMOD_FIPS_FLAGS);
|
|
|
|
}
|
|
|
|
if (newModule) {
|
|
|
|
PK11SlotInfo *slot;
|
|
|
|
newModule->libraryParams =
|
|
|
|
PORT_ArenaStrdup(newModule->arena, mlp->module->libraryParams);
|
|
|
|
/* if an explicit internal key slot has been set, reset it */
|
|
|
|
slot = pk11_SwapInternalKeySlot(NULL);
|
|
|
|
if (slot) {
|
|
|
|
secmod_SetInternalKeySlotFlag(newModule, PR_TRUE);
|
|
|
|
}
|
|
|
|
rv = SECMOD_AddModule(newModule);
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
/* load failed, restore the internal key slot */
|
|
|
|
pk11_SetInternalKeySlot(slot);
|
|
|
|
SECMOD_DestroyModule(newModule);
|
|
|
|
newModule = NULL;
|
|
|
|
}
|
|
|
|
/* free the old explicit internal key slot, we now have a new one */
|
|
|
|
if (slot) {
|
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newModule == NULL) {
|
|
|
|
SECMODModuleList *last = NULL, *mlp2;
|
|
|
|
/* we're in pretty deep trouble if this happens...Security
|
|
|
|
* not going to work well... try to put the old module back on
|
|
|
|
* the list */
|
|
|
|
SECMOD_GetWriteLock(moduleLock);
|
|
|
|
for (mlp2 = modules; mlp2 != NULL; mlp2 = mlp->next) {
|
|
|
|
last = mlp2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last == NULL) {
|
|
|
|
modules = mlp;
|
|
|
|
} else {
|
|
|
|
SECMOD_AddList(last, mlp, NULL);
|
|
|
|
}
|
|
|
|
SECMOD_ReleaseWriteLock(moduleLock);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
pendingModule = oldModule = internalModule;
|
|
|
|
internalModule = NULL;
|
|
|
|
SECMOD_DestroyModule(oldModule);
|
|
|
|
SECMOD_DeletePermDB(mlp->module);
|
|
|
|
SECMOD_DestroyModuleListElement(mlp);
|
|
|
|
internalModule = newModule; /* adopt the module */
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_AddModule(SECMODModule *newModule)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECStatus rv;
|
|
|
|
SECMODModule *oldModule;
|
|
|
|
|
|
|
|
/* Test if a module w/ the same name already exists */
|
|
|
|
/* and return SECWouldBlock if so. */
|
|
|
|
/* We should probably add a new return value such as */
|
|
|
|
/* SECDublicateModule, but to minimize ripples, I'll */
|
|
|
|
/* give SECWouldBlock a new meaning */
|
|
|
|
if ((oldModule = SECMOD_FindModule(newModule->commonName)) != NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModule(oldModule);
|
2008-06-06 16:40:11 +04:00
|
|
|
return SECWouldBlock;
|
|
|
|
/* module already exists. */
|
|
|
|
}
|
|
|
|
|
2010-02-07 14:54:28 +03:00
|
|
|
rv = secmod_LoadPKCS11Module(newModule, NULL);
|
2008-06-06 16:40:11 +04:00
|
|
|
if (rv != SECSuccess) {
|
2016-11-02 12:29:58 +03:00
|
|
|
return rv;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newModule->parent == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
newModule->parent = SECMOD_ReferenceModule(defaultDBModule);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SECMOD_AddPermDB(newModule);
|
|
|
|
SECMOD_AddModuleToList(newModule);
|
|
|
|
|
|
|
|
rv = STAN_AddModuleToDefaultTrustDomain(newModule);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
PK11SlotInfo *
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_FindSlot(SECMODModule *module, const char *name)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *string;
|
|
|
|
PK11SlotInfo *retSlot = NULL;
|
|
|
|
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return retSlot;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
SECMOD_GetReadLock(moduleLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
for (i = 0; i < module->slotCount; i++) {
|
|
|
|
PK11SlotInfo *slot = module->slots[i];
|
|
|
|
|
|
|
|
if (PK11_IsPresent(slot)) {
|
|
|
|
string = PK11_GetTokenName(slot);
|
|
|
|
} else {
|
|
|
|
string = PK11_GetSlotName(slot);
|
|
|
|
}
|
|
|
|
if (PORT_Strcmp(name, string) == 0) {
|
|
|
|
retSlot = PK11_ReferenceSlot(slot);
|
|
|
|
break;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
|
|
|
|
if (retSlot == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NO_SLOT_SELECTED);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return retSlot;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus
|
2016-11-02 12:29:58 +03:00
|
|
|
PK11_GetModInfo(SECMODModule *mod, CK_INFO *info)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
CK_RV crv;
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
if (mod->functionList == NULL)
|
|
|
|
return SECFailure;
|
2008-06-06 16:40:11 +04:00
|
|
|
crv = PK11_GETTAB(mod)->C_GetInfo(info);
|
|
|
|
if (crv != CKR_OK) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(PK11_MapError(crv));
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
return (crv == CKR_OK) ? SECSuccess : SECFailure;
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:12:24 +03:00
|
|
|
char *
|
|
|
|
PK11_GetModuleURI(SECMODModule *mod)
|
|
|
|
{
|
|
|
|
CK_INFO info;
|
|
|
|
PK11URI *uri;
|
|
|
|
char *ret = NULL;
|
|
|
|
PK11URIAttribute attrs[3];
|
|
|
|
size_t nattrs = 0;
|
|
|
|
char libraryManufacturer[32 + 1], libraryDescription[32 + 1], libraryVersion[8];
|
|
|
|
|
|
|
|
if (PK11_GetModInfo(mod, &info) == SECFailure) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PK11_MakeString(NULL, libraryManufacturer, (char *)info.manufacturerID,
|
|
|
|
sizeof(info.manufacturerID));
|
|
|
|
if (*libraryManufacturer != '\0') {
|
|
|
|
attrs[nattrs].name = PK11URI_PATTR_LIBRARY_MANUFACTURER;
|
|
|
|
attrs[nattrs].value = libraryManufacturer;
|
|
|
|
nattrs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
PK11_MakeString(NULL, libraryDescription, (char *)info.libraryDescription,
|
|
|
|
sizeof(info.libraryDescription));
|
|
|
|
if (*libraryDescription != '\0') {
|
|
|
|
attrs[nattrs].name = PK11URI_PATTR_LIBRARY_DESCRIPTION;
|
|
|
|
attrs[nattrs].value = libraryDescription;
|
|
|
|
nattrs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_snprintf(libraryVersion, sizeof(libraryVersion), "%d.%d",
|
|
|
|
info.libraryVersion.major, info.libraryVersion.minor);
|
|
|
|
attrs[nattrs].name = PK11URI_PATTR_LIBRARY_VERSION;
|
|
|
|
attrs[nattrs].value = libraryVersion;
|
|
|
|
nattrs++;
|
|
|
|
|
|
|
|
uri = PK11URI_CreateURI(attrs, nattrs, NULL, 0);
|
|
|
|
if (uri == NULL) {
|
|
|
|
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = PK11URI_FormatURI(NULL, uri);
|
|
|
|
PK11URI_DestroyURI(uri);
|
|
|
|
if (ret == NULL) {
|
|
|
|
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-06-06 16:40:11 +04:00
|
|
|
/* Determine if we have the FIP's module loaded as the default
|
|
|
|
* module to trigger other bogus FIPS requirements in PKCS #12 and
|
|
|
|
* SSL
|
|
|
|
*/
|
|
|
|
PRBool
|
|
|
|
PK11_IsFIPS(void)
|
|
|
|
{
|
|
|
|
SECMODModule *mod = SECMOD_GetInternalModule();
|
|
|
|
|
|
|
|
if (mod && mod->internal) {
|
2016-11-02 12:29:58 +03:00
|
|
|
return mod->isFIPS;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* combines NewModule() & AddModule */
|
|
|
|
/* give a string for the module name & the full-path for the dll, */
|
|
|
|
/* installs the PKCS11 module & update registry */
|
2016-11-02 12:29:58 +03:00
|
|
|
SECStatus
|
|
|
|
SECMOD_AddNewModuleEx(const char *moduleName, const char *dllPath,
|
|
|
|
unsigned long defaultMechanismFlags,
|
|
|
|
unsigned long cipherEnableFlags,
|
|
|
|
char *modparms, char *nssparms)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECMODModule *module;
|
|
|
|
SECStatus result = SECFailure;
|
2016-11-02 12:29:58 +03:00
|
|
|
int s, i;
|
|
|
|
PK11SlotInfo *slot;
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
PR_SetErrorText(0, NULL);
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return result;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
module = SECMOD_CreateModule(dllPath, moduleName, modparms, nssparms);
|
|
|
|
|
|
|
|
if (module == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
return result;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (module->dllName != NULL) {
|
|
|
|
if (module->dllName[0] != 0) {
|
|
|
|
result = SECMOD_AddModule(module);
|
|
|
|
if (result == SECSuccess) {
|
|
|
|
/* turn on SSL cipher enable flags */
|
|
|
|
module->ssl[0] = cipherEnableFlags;
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_GetReadLock(moduleLock);
|
2008-06-06 16:40:11 +04:00
|
|
|
/* check each slot to turn on appropriate mechanisms */
|
|
|
|
for (s = 0; s < module->slotCount; s++) {
|
|
|
|
slot = (module->slots)[s];
|
|
|
|
/* for each possible mechanism */
|
2016-11-02 12:29:58 +03:00
|
|
|
for (i = 0; i < num_pk11_default_mechanisms; i++) {
|
2008-06-06 16:40:11 +04:00
|
|
|
/* we are told to turn it on by default ? */
|
2016-11-02 12:29:58 +03:00
|
|
|
PRBool add =
|
|
|
|
(PK11_DefaultArray[i].flag & defaultMechanismFlags) ? PR_TRUE : PR_FALSE;
|
|
|
|
result = PK11_UpdateSlotAttribute(slot,
|
|
|
|
&(PK11_DefaultArray[i]), add);
|
2016-01-25 18:14:18 +03:00
|
|
|
if (result != SECSuccess) {
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
SECMOD_DestroyModule(module);
|
|
|
|
return result;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
} /* for each mechanism */
|
|
|
|
/* disable each slot if the defaultFlags say so */
|
|
|
|
if (defaultMechanismFlags & PK11_DISABLE_FLAG) {
|
|
|
|
PK11_UserDisableSlot(slot);
|
|
|
|
}
|
|
|
|
} /* for each slot of this module */
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
2008-06-06 16:40:11 +04:00
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
/* delete and re-add module in order to save changes
|
|
|
|
* to the module */
|
|
|
|
result = SECMOD_UpdateModule(module);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SECMOD_DestroyModule(module);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
SECStatus
|
|
|
|
SECMOD_AddNewModule(const char *moduleName, const char *dllPath,
|
|
|
|
unsigned long defaultMechanismFlags,
|
|
|
|
unsigned long cipherEnableFlags)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
return SECMOD_AddNewModuleEx(moduleName, dllPath, defaultMechanismFlags,
|
2016-11-02 12:29:58 +03:00
|
|
|
cipherEnableFlags,
|
|
|
|
NULL, NULL); /* don't pass module or nss params */
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
SECStatus
|
2008-06-06 16:40:11 +04:00
|
|
|
SECMOD_UpdateModule(SECMODModule *module)
|
|
|
|
{
|
|
|
|
SECStatus result;
|
|
|
|
|
|
|
|
result = SECMOD_DeletePermDB(module);
|
2016-11-02 12:29:58 +03:00
|
|
|
|
|
|
|
if (result == SECSuccess) {
|
|
|
|
result = SECMOD_AddPermDB(module);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Public & Internal(Security Library) representation of
|
|
|
|
* encryption mechanism flags conversion */
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
/* Currently, the only difference is that internal representation
|
2008-06-06 16:40:11 +04:00
|
|
|
* puts RANDOM_FLAG at bit 31 (Most-significant bit), but
|
|
|
|
* public representation puts this bit at bit 28
|
|
|
|
*/
|
2016-11-02 12:29:58 +03:00
|
|
|
unsigned long
|
2008-06-06 16:40:11 +04:00
|
|
|
SECMOD_PubMechFlagstoInternal(unsigned long publicFlags)
|
|
|
|
{
|
|
|
|
unsigned long internalFlags = publicFlags;
|
|
|
|
|
|
|
|
if (publicFlags & PUBLIC_MECH_RANDOM_FLAG) {
|
|
|
|
internalFlags &= ~PUBLIC_MECH_RANDOM_FLAG;
|
|
|
|
internalFlags |= SECMOD_RANDOM_FLAG;
|
|
|
|
}
|
|
|
|
return internalFlags;
|
|
|
|
}
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
unsigned long
|
|
|
|
SECMOD_InternaltoPubMechFlags(unsigned long internalFlags)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
unsigned long publicFlags = internalFlags;
|
|
|
|
|
|
|
|
if (internalFlags & SECMOD_RANDOM_FLAG) {
|
|
|
|
publicFlags &= ~SECMOD_RANDOM_FLAG;
|
|
|
|
publicFlags |= PUBLIC_MECH_RANDOM_FLAG;
|
|
|
|
}
|
|
|
|
return publicFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Public & Internal(Security Library) representation of */
|
|
|
|
/* cipher flags conversion */
|
|
|
|
/* Note: currently they are just stubs */
|
2016-11-02 12:29:58 +03:00
|
|
|
unsigned long
|
|
|
|
SECMOD_PubCipherFlagstoInternal(unsigned long publicFlags)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
return publicFlags;
|
|
|
|
}
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
unsigned long
|
|
|
|
SECMOD_InternaltoPubCipherFlags(unsigned long internalFlags)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
return internalFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Funtion reports true if module of modType is installed/configured */
|
2016-11-02 12:29:58 +03:00
|
|
|
PRBool
|
|
|
|
SECMOD_IsModulePresent(unsigned long int pubCipherEnableFlags)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
PRBool result = PR_FALSE;
|
2009-07-29 04:01:39 +04:00
|
|
|
SECMODModuleList *mods;
|
2008-06-06 16:40:11 +04:00
|
|
|
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return result;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
|
|
|
SECMOD_GetReadLock(moduleLock);
|
|
|
|
mods = SECMOD_GetDefaultModuleList();
|
2016-11-02 12:29:58 +03:00
|
|
|
for (; mods != NULL; mods = mods->next) {
|
|
|
|
if (mods->module->ssl[0] &
|
|
|
|
SECMOD_PubCipherFlagstoInternal(pubCipherEnableFlags)) {
|
2008-06-06 16:40:11 +04:00
|
|
|
result = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a new ModuleListElement */
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMODModuleList *
|
|
|
|
SECMOD_NewModuleListElement(void)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECMODModuleList *newModList;
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
newModList = (SECMODModuleList *)PORT_Alloc(sizeof(SECMODModuleList));
|
2008-06-06 16:40:11 +04:00
|
|
|
if (newModList) {
|
2016-11-02 12:29:58 +03:00
|
|
|
newModList->next = NULL;
|
|
|
|
newModList->module = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return newModList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make a new reference to a module so It doesn't go away on us
|
|
|
|
*/
|
|
|
|
SECMODModule *
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_ReferenceModule(SECMODModule *module)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
PZ_Lock(module->refLock);
|
|
|
|
PORT_Assert(module->refCount > 0);
|
|
|
|
|
|
|
|
module->refCount++;
|
|
|
|
PZ_Unlock(module->refLock);
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy an existing module */
|
|
|
|
void
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModule(SECMODModule *module)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
PRBool willfree = PR_FALSE;
|
|
|
|
int slotCount;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
PZ_Lock(module->refLock);
|
|
|
|
if (module->refCount-- == 1) {
|
2016-11-02 12:29:58 +03:00
|
|
|
willfree = PR_TRUE;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
PORT_Assert(willfree || (module->refCount > 0));
|
|
|
|
PZ_Unlock(module->refLock);
|
|
|
|
|
|
|
|
if (!willfree) {
|
2016-11-02 12:29:58 +03:00
|
|
|
return;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2016-11-02 12:29:58 +03:00
|
|
|
|
2008-06-06 16:40:11 +04:00
|
|
|
if (module->parent != NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMODModule *parent = module->parent;
|
|
|
|
/* paranoia, don't loop forever if the modules are looped */
|
|
|
|
module->parent = NULL;
|
|
|
|
SECMOD_DestroyModule(parent);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* slots can't really disappear until our module starts freeing them,
|
|
|
|
* so this check is safe */
|
|
|
|
slotCount = module->slotCount;
|
|
|
|
if (slotCount == 0) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_SlotDestroyModule(module, PR_FALSE);
|
|
|
|
return;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now free all out slots, when they are done, they will cause the
|
|
|
|
* module to disappear altogether */
|
2016-11-02 12:29:58 +03:00
|
|
|
for (i = 0; i < slotCount; i++) {
|
|
|
|
if (!module->slots[i]->disabled) {
|
|
|
|
PK11_ClearSlotList(module->slots[i]);
|
|
|
|
}
|
|
|
|
PK11_FreeSlot(module->slots[i]);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
/* WARNING: once the last slot has been freed is it possible (even likely)
|
|
|
|
* that module is no more... touching it now is a good way to go south */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we can only get here if we've destroyed the module, or some one has
|
|
|
|
* erroneously freed a slot that wasn't referenced. */
|
|
|
|
void
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_SlotDestroyModule(SECMODModule *module, PRBool fromSlot)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
PRBool willfree = PR_FALSE;
|
|
|
|
if (fromSlot) {
|
|
|
|
PORT_Assert(module->refCount == 0);
|
2016-11-02 12:29:58 +03:00
|
|
|
PZ_Lock(module->refLock);
|
|
|
|
if (module->slotCount-- == 1) {
|
|
|
|
willfree = PR_TRUE;
|
|
|
|
}
|
|
|
|
PORT_Assert(willfree || (module->slotCount > 0));
|
|
|
|
PZ_Unlock(module->refLock);
|
|
|
|
if (!willfree)
|
|
|
|
return;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (module == pendingModule) {
|
2016-11-02 12:29:58 +03:00
|
|
|
pendingModule = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (module->loaded) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_UnloadModule(module);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
PZ_DestroyLock(module->refLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_FreeArena(module->arena, PR_FALSE);
|
2008-06-06 16:40:11 +04:00
|
|
|
secmod_PrivateModuleCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy a list element
|
|
|
|
* this destroys a single element, and returns the next element
|
|
|
|
* on the chain. It makes it easy to implement for loops to delete
|
|
|
|
* the chain. It also make deleting a single element easy */
|
|
|
|
SECMODModuleList *
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModuleListElement(SECMODModuleList *element)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECMODModuleList *next = element->next;
|
|
|
|
|
|
|
|
if (element->module) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModule(element->module);
|
|
|
|
element->module = NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
PORT_Free(element);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy an entire module list
|
|
|
|
*/
|
|
|
|
void
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMOD_DestroyModuleList(SECMODModuleList *list)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
SECMODModuleList *lp;
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
for (lp = list; lp != NULL; lp = SECMOD_DestroyModuleListElement(lp))
|
|
|
|
;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
SECMOD_CanDeleteInternalModule(void)
|
|
|
|
{
|
2017-11-24 11:00:26 +03:00
|
|
|
#ifdef NSS_FIPS_DISABLED
|
|
|
|
return PR_FALSE;
|
|
|
|
#else
|
2016-11-02 12:29:58 +03:00
|
|
|
return (PRBool)(pendingModule == NULL);
|
2017-11-24 11:00:26 +03:00
|
|
|
#endif
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check to see if the module has added new slots. PKCS 11 v2.20 allows for
|
2016-11-02 12:29:58 +03:00
|
|
|
* modules to add new slots, but never remove them. Slots cannot be added
|
2008-06-06 16:40:11 +04:00
|
|
|
* between a call to C_GetSlotLlist(Flag, NULL, &count) and the subsequent
|
|
|
|
* C_GetSlotList(flag, &data, &count) so that the array doesn't accidently
|
|
|
|
* grow on the caller. It is permissible for the slots to increase between
|
|
|
|
* successive calls with NULL to get the size.
|
|
|
|
*/
|
|
|
|
SECStatus
|
|
|
|
SECMOD_UpdateSlotList(SECMODModule *mod)
|
|
|
|
{
|
|
|
|
CK_RV crv;
|
|
|
|
CK_ULONG count;
|
|
|
|
CK_ULONG i, oldCount;
|
|
|
|
PRBool freeRef = PR_FALSE;
|
|
|
|
void *mark = NULL;
|
|
|
|
CK_ULONG *slotIDs = NULL;
|
|
|
|
PK11SlotInfo **newSlots = NULL;
|
|
|
|
PK11SlotInfo **oldSlots = NULL;
|
|
|
|
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return SECFailure;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
/* C_GetSlotList is not a session function, make sure
|
2008-06-06 16:40:11 +04:00
|
|
|
* calls are serialized */
|
|
|
|
PZ_Lock(mod->refLock);
|
|
|
|
freeRef = PR_TRUE;
|
|
|
|
/* see if the number of slots have changed */
|
|
|
|
crv = PK11_GETTAB(mod)->C_GetSlotList(PR_FALSE, NULL, &count);
|
|
|
|
if (crv != CKR_OK) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(PK11_MapError(crv));
|
|
|
|
goto loser;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
/* nothing new, blow out early, we want this function to be quick
|
|
|
|
* and cheap in the normal case */
|
|
|
|
if (count == mod->slotCount) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
return SECSuccess;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
if (count < (CK_ULONG)mod->slotCount) {
|
2016-11-02 12:29:58 +03:00
|
|
|
/* shouldn't happen with a properly functioning PKCS #11 module */
|
|
|
|
PORT_SetError(SEC_ERROR_INCOMPATIBLE_PKCS11);
|
|
|
|
goto loser;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get the new slot list */
|
|
|
|
slotIDs = PORT_NewArray(CK_SLOT_ID, count);
|
|
|
|
if (slotIDs == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
goto loser;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
crv = PK11_GETTAB(mod)->C_GetSlotList(PR_FALSE, slotIDs, &count);
|
|
|
|
if (crv != CKR_OK) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(PK11_MapError(crv));
|
|
|
|
goto loser;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
freeRef = PR_FALSE;
|
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
mark = PORT_ArenaMark(mod->arena);
|
|
|
|
if (mark == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
goto loser;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2016-11-02 12:29:58 +03:00
|
|
|
newSlots = PORT_ArenaZNewArray(mod->arena, PK11SlotInfo *, count);
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
/* walk down the new slot ID list returned from the module. We keep
|
2016-11-02 12:29:58 +03:00
|
|
|
* the old slots which match a returned ID, and we initialize the new
|
2008-06-06 16:40:11 +04:00
|
|
|
* slots. */
|
2016-11-02 12:29:58 +03:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
PK11SlotInfo *slot = SECMOD_FindSlotByID(mod, slotIDs[i]);
|
|
|
|
|
|
|
|
if (!slot) {
|
|
|
|
/* we have a new slot create a new slot data structure */
|
|
|
|
slot = PK11_NewSlotInfo(mod);
|
|
|
|
if (!slot) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
PK11_InitSlot(mod, slotIDs[i], slot);
|
|
|
|
STAN_InitTokenForSlotInfo(NULL, slot);
|
|
|
|
}
|
|
|
|
newSlots[i] = slot;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
STAN_ResetTokenInterator(NULL);
|
|
|
|
PORT_Free(slotIDs);
|
|
|
|
slotIDs = NULL;
|
|
|
|
PORT_ArenaUnmark(mod->arena, mark);
|
|
|
|
|
|
|
|
/* until this point we're still using the old slot list. Now we update
|
2016-11-02 12:29:58 +03:00
|
|
|
* module slot list. We update the slots (array) first then the count,
|
|
|
|
* since we've already guarrenteed that count has increased (just in case
|
|
|
|
* someone is looking at the slots field of module without holding the
|
2008-06-06 16:40:11 +04:00
|
|
|
* moduleLock */
|
|
|
|
SECMOD_GetWriteLock(moduleLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
oldCount = mod->slotCount;
|
2008-06-06 16:40:11 +04:00
|
|
|
oldSlots = mod->slots;
|
|
|
|
mod->slots = newSlots; /* typical arena 'leak'... old mod->slots is
|
2016-11-02 12:29:58 +03:00
|
|
|
* allocated out of the module arena and won't
|
|
|
|
* be freed until the module is freed */
|
2008-06-06 16:40:11 +04:00
|
|
|
mod->slotCount = count;
|
|
|
|
SECMOD_ReleaseWriteLock(moduleLock);
|
|
|
|
/* free our old references before forgetting about oldSlot*/
|
2016-11-02 12:29:58 +03:00
|
|
|
for (i = 0; i < oldCount; i++) {
|
|
|
|
PK11_FreeSlot(oldSlots[i]);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return SECSuccess;
|
|
|
|
|
|
|
|
loser:
|
|
|
|
if (freeRef) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PZ_Unlock(mod->refLock);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
if (slotIDs) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_Free(slotIDs);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
/* free all the slots we allocated. newSlots are part of the
|
|
|
|
* mod arena. NOTE: the newSlots array contain both new and old
|
|
|
|
* slots, but we kept a reference to the old slots when we built the new
|
|
|
|
* array, so we need to free all the slots in newSlots array. */
|
|
|
|
if (newSlots) {
|
2016-11-02 12:29:58 +03:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (newSlots[i] == NULL) {
|
|
|
|
break; /* hit the last one */
|
|
|
|
}
|
|
|
|
PK11_FreeSlot(newSlots[i]);
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
/* must come after freeing newSlots */
|
|
|
|
if (mark) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_ArenaRelease(mod->arena, mark);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this handles modules that do not support C_WaitForSlotEvent().
|
|
|
|
* The internal flags are stored. Note that C_WaitForSlotEvent() does not
|
|
|
|
* have a timeout, so we don't have one for handleWaitForSlotEvent() either.
|
|
|
|
*/
|
|
|
|
PK11SlotInfo *
|
2016-11-02 12:29:58 +03:00
|
|
|
secmod_HandleWaitForSlotEvent(SECMODModule *mod, unsigned long flags,
|
|
|
|
PRIntervalTime latency)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
PRBool removableSlotsFound = PR_FALSE;
|
|
|
|
int i;
|
|
|
|
int error = SEC_ERROR_NO_EVENT;
|
|
|
|
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return NULL;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
PZ_Lock(mod->refLock);
|
|
|
|
if (mod->evControlMask & SECMOD_END_WAIT) {
|
2016-11-02 12:29:58 +03:00
|
|
|
mod->evControlMask &= ~SECMOD_END_WAIT;
|
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
PORT_SetError(SEC_ERROR_NO_EVENT);
|
|
|
|
return NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
mod->evControlMask |= SECMOD_WAIT_SIMULATED_EVENT;
|
|
|
|
while (mod->evControlMask & SECMOD_WAIT_SIMULATED_EVENT) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
/* now is a good time to see if new slots have been added */
|
|
|
|
SECMOD_UpdateSlotList(mod);
|
|
|
|
|
|
|
|
/* loop through all the slots on a module */
|
|
|
|
SECMOD_GetReadLock(moduleLock);
|
|
|
|
for (i = 0; i < mod->slotCount; i++) {
|
|
|
|
PK11SlotInfo *slot = mod->slots[i];
|
|
|
|
PRUint16 series;
|
|
|
|
PRBool present;
|
|
|
|
|
|
|
|
/* perm modules do not change */
|
|
|
|
if (slot->isPerm) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
removableSlotsFound = PR_TRUE;
|
|
|
|
/* simulate the PKCS #11 module flags. are the flags different
|
|
|
|
* from the last time we called? */
|
|
|
|
series = slot->series;
|
|
|
|
present = PK11_IsPresent(slot);
|
|
|
|
if ((slot->flagSeries != series) || (slot->flagState != present)) {
|
|
|
|
slot->flagState = present;
|
|
|
|
slot->flagSeries = series;
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
PZ_Lock(mod->refLock);
|
|
|
|
mod->evControlMask &= ~SECMOD_END_WAIT;
|
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
return PK11_ReferenceSlot(slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
/* if everything was perm modules, don't lock up forever */
|
|
|
|
if ((mod->slotCount != 0) && !removableSlotsFound) {
|
|
|
|
error = SEC_ERROR_NO_SLOT_SELECTED;
|
|
|
|
PZ_Lock(mod->refLock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (flags & CKF_DONT_BLOCK) {
|
|
|
|
PZ_Lock(mod->refLock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
PR_Sleep(latency);
|
|
|
|
PZ_Lock(mod->refLock);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
mod->evControlMask &= ~SECMOD_END_WAIT;
|
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
PORT_SetError(error);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this function waits for a token event on any slot of a given module
|
|
|
|
* This function should not be called from more than one thread of the
|
|
|
|
* same process (though other threads can make other library calls
|
|
|
|
* on this module while this call is blocked).
|
|
|
|
*/
|
|
|
|
PK11SlotInfo *
|
|
|
|
SECMOD_WaitForAnyTokenEvent(SECMODModule *mod, unsigned long flags,
|
2016-11-02 12:29:58 +03:00
|
|
|
PRIntervalTime latency)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
CK_SLOT_ID id;
|
|
|
|
CK_RV crv;
|
|
|
|
PK11SlotInfo *slot;
|
|
|
|
|
|
|
|
if (!pk11_getFinalizeModulesOption() ||
|
|
|
|
((mod->cryptokiVersion.major == 2) &&
|
2016-11-02 12:29:58 +03:00
|
|
|
(mod->cryptokiVersion.minor < 1))) {
|
2008-06-06 16:40:11 +04:00
|
|
|
/* if we are sharing the module with other software in our
|
|
|
|
* address space, we can't reliably use C_WaitForSlotEvent(),
|
|
|
|
* and if the module is version 2.0, C_WaitForSlotEvent() doesn't
|
|
|
|
* exist */
|
2016-11-02 12:29:58 +03:00
|
|
|
return secmod_HandleWaitForSlotEvent(mod, flags, latency);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
/* first the the PKCS #11 call */
|
|
|
|
PZ_Lock(mod->refLock);
|
|
|
|
if (mod->evControlMask & SECMOD_END_WAIT) {
|
2016-11-02 12:29:58 +03:00
|
|
|
goto end_wait;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
mod->evControlMask |= SECMOD_WAIT_PKCS11_EVENT;
|
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
crv = PK11_GETTAB(mod)->C_WaitForSlotEvent(flags, &id, NULL);
|
|
|
|
PZ_Lock(mod->refLock);
|
|
|
|
mod->evControlMask &= ~SECMOD_WAIT_PKCS11_EVENT;
|
|
|
|
/* if we are in end wait, short circuit now, don't even risk
|
|
|
|
* going into secmod_HandleWaitForSlotEvent */
|
|
|
|
if (mod->evControlMask & SECMOD_END_WAIT) {
|
2016-11-02 12:29:58 +03:00
|
|
|
goto end_wait;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
if (crv == CKR_FUNCTION_NOT_SUPPORTED) {
|
2016-11-02 12:29:58 +03:00
|
|
|
/* module doesn't support that call, simulate it */
|
|
|
|
return secmod_HandleWaitForSlotEvent(mod, flags, latency);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
if (crv != CKR_OK) {
|
2016-11-02 12:29:58 +03:00
|
|
|
/* we can get this error if finalize was called while we were
|
|
|
|
* still running. This is the only way to force a C_WaitForSlotEvent()
|
|
|
|
* to return in PKCS #11. In this case, just return that there
|
|
|
|
* was no event. */
|
|
|
|
if (crv == CKR_CRYPTOKI_NOT_INITIALIZED) {
|
|
|
|
PORT_SetError(SEC_ERROR_NO_EVENT);
|
|
|
|
} else {
|
|
|
|
PORT_SetError(PK11_MapError(crv));
|
|
|
|
}
|
|
|
|
return NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
slot = SECMOD_FindSlotByID(mod, id);
|
|
|
|
if (slot == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
/* possibly a new slot that was added? */
|
|
|
|
SECMOD_UpdateSlotList(mod);
|
|
|
|
slot = SECMOD_FindSlotByID(mod, id);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
/* if we are in the delay period for the "isPresent" call, reset
|
|
|
|
* the delay since we know things have probably changed... */
|
|
|
|
if (slot && slot->nssToken && slot->nssToken->slot) {
|
2016-11-02 12:29:58 +03:00
|
|
|
nssSlot_ResetDelay(slot->nssToken->slot);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
return slot;
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
/* must be called with the lock on. */
|
2008-06-06 16:40:11 +04:00
|
|
|
end_wait:
|
|
|
|
mod->evControlMask &= ~SECMOD_END_WAIT;
|
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
PORT_SetError(SEC_ERROR_NO_EVENT);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function "wakes up" WaitForAnyTokenEvent. It's a pretty drastic
|
|
|
|
* function, possibly bringing down the pkcs #11 module in question. This
|
|
|
|
* should be OK because 1) it does reinitialize, and 2) it should only be
|
|
|
|
* called when we are on our way to tear the whole system down anyway.
|
|
|
|
*/
|
|
|
|
SECStatus
|
|
|
|
SECMOD_CancelWait(SECMODModule *mod)
|
|
|
|
{
|
2014-10-25 02:34:34 +04:00
|
|
|
unsigned long controlMask;
|
2008-06-06 16:40:11 +04:00
|
|
|
SECStatus rv = SECSuccess;
|
|
|
|
CK_RV crv;
|
|
|
|
|
|
|
|
PZ_Lock(mod->refLock);
|
|
|
|
mod->evControlMask |= SECMOD_END_WAIT;
|
|
|
|
controlMask = mod->evControlMask;
|
|
|
|
if (controlMask & SECMOD_WAIT_PKCS11_EVENT) {
|
|
|
|
if (!pk11_getFinalizeModulesOption()) {
|
|
|
|
/* can't get here unless pk11_getFinalizeModulesOption is set */
|
|
|
|
PORT_Assert(0);
|
|
|
|
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
|
|
|
|
rv = SECFailure;
|
|
|
|
goto loser;
|
|
|
|
}
|
2016-11-02 12:29:58 +03:00
|
|
|
/* NOTE: this call will drop all transient keys, in progress
|
|
|
|
* operations, and any authentication. This is the only documented
|
|
|
|
* way to get WaitForSlotEvent to return. Also note: for non-thread
|
|
|
|
* safe tokens, we need to hold the module lock, this is not yet at
|
|
|
|
* system shutdown/startup time, so we need to protect these calls */
|
|
|
|
crv = PK11_GETTAB(mod)->C_Finalize(NULL);
|
|
|
|
/* ok, we slammed the module down, now we need to reinit it in case
|
|
|
|
* we intend to use it again */
|
|
|
|
if (CKR_OK == crv) {
|
2008-06-06 16:40:11 +04:00
|
|
|
PRBool alreadyLoaded;
|
2016-11-02 12:29:58 +03:00
|
|
|
secmod_ModuleInit(mod, NULL, &alreadyLoaded);
|
|
|
|
} else {
|
|
|
|
/* Finalized failed for some reason, notify the application
|
|
|
|
* so maybe it has a prayer of recovering... */
|
|
|
|
PORT_SetError(PK11_MapError(crv));
|
|
|
|
rv = SECFailure;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
} else if (controlMask & SECMOD_WAIT_SIMULATED_EVENT) {
|
2016-11-02 12:29:58 +03:00
|
|
|
mod->evControlMask &= ~SECMOD_WAIT_SIMULATED_EVENT;
|
|
|
|
/* Simulated events will eventually timeout
|
|
|
|
* and wake up in the loop */
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
loser:
|
|
|
|
PZ_Unlock(mod->refLock);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check to see if the module has removable slots that we may need to
|
|
|
|
* watch for.
|
|
|
|
*/
|
|
|
|
PRBool
|
|
|
|
SECMOD_HasRemovableSlots(SECMODModule *mod)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PRBool ret = PR_FALSE;
|
|
|
|
|
2009-07-29 04:01:39 +04:00
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return ret;
|
2009-07-29 04:01:39 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
SECMOD_GetReadLock(moduleLock);
|
2016-11-02 12:29:58 +03:00
|
|
|
for (i = 0; i < mod->slotCount; i++) {
|
|
|
|
PK11SlotInfo *slot = mod->slots[i];
|
|
|
|
/* perm modules are not inserted or removed */
|
|
|
|
if (slot->isPerm) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ret = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (mod->slotCount == 0) {
|
|
|
|
ret = PR_TRUE;
|
2012-10-01 22:02:15 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* helper function to actually create and destroy user defined slots
|
|
|
|
*/
|
|
|
|
static SECStatus
|
2016-11-02 12:29:58 +03:00
|
|
|
secmod_UserDBOp(PK11SlotInfo *slot, CK_OBJECT_CLASS objClass,
|
|
|
|
const char *sendSpec)
|
2008-06-06 16:40:11 +04:00
|
|
|
{
|
|
|
|
CK_OBJECT_HANDLE dummy;
|
2016-11-02 12:29:58 +03:00
|
|
|
CK_ATTRIBUTE template[2];
|
2008-06-06 16:40:11 +04:00
|
|
|
CK_ATTRIBUTE *attrs = template;
|
|
|
|
CK_RV crv;
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
PK11_SETATTRS(attrs, CKA_CLASS, &objClass, sizeof(objClass));
|
|
|
|
attrs++;
|
|
|
|
PK11_SETATTRS(attrs, CKA_NETSCAPE_MODULE_SPEC, (unsigned char *)sendSpec,
|
|
|
|
strlen(sendSpec) + 1);
|
|
|
|
attrs++;
|
2008-06-06 16:40:11 +04:00
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_Assert(attrs - template <= 2);
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
PK11_EnterSlotMonitor(slot);
|
|
|
|
crv = PK11_CreateNewObject(slot, slot->session,
|
2016-11-02 12:29:58 +03:00
|
|
|
template, attrs - template, PR_FALSE, &dummy);
|
2008-06-06 16:40:11 +04:00
|
|
|
PK11_ExitSlotMonitor(slot);
|
|
|
|
|
|
|
|
if (crv != CKR_OK) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(PK11_MapError(crv));
|
|
|
|
return SECFailure;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2009-08-19 17:59:06 +04:00
|
|
|
return SECMOD_UpdateSlotList(slot->module);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
2009-08-19 17:59:06 +04:00
|
|
|
/*
|
|
|
|
* return true if the selected slot ID is not present or doesn't exist
|
|
|
|
*/
|
|
|
|
static PRBool
|
2016-11-02 12:29:58 +03:00
|
|
|
secmod_SlotIsEmpty(SECMODModule *mod, CK_SLOT_ID slotID)
|
2009-08-19 17:59:06 +04:00
|
|
|
{
|
|
|
|
PK11SlotInfo *slot = SECMOD_LookupSlot(mod->moduleID, slotID);
|
|
|
|
if (slot) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PRBool present = PK11_IsPresent(slot);
|
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
if (present) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
/* it doesn't exist or isn't present, it's available */
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find an unused slot id in module.
|
|
|
|
*/
|
|
|
|
static CK_SLOT_ID
|
|
|
|
secmod_FindFreeSlot(SECMODModule *mod)
|
|
|
|
{
|
|
|
|
CK_SLOT_ID i, minSlotID, maxSlotID;
|
|
|
|
|
|
|
|
/* look for a free slot id on the internal module */
|
|
|
|
if (mod->internal && mod->isFIPS) {
|
2016-11-02 12:29:58 +03:00
|
|
|
minSlotID = SFTK_MIN_FIPS_USER_SLOT_ID;
|
|
|
|
maxSlotID = SFTK_MAX_FIPS_USER_SLOT_ID;
|
2009-08-19 17:59:06 +04:00
|
|
|
} else {
|
2016-11-02 12:29:58 +03:00
|
|
|
minSlotID = SFTK_MIN_USER_SLOT_ID;
|
|
|
|
maxSlotID = SFTK_MAX_USER_SLOT_ID;
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
2016-11-02 12:29:58 +03:00
|
|
|
for (i = minSlotID; i < maxSlotID; i++) {
|
|
|
|
if (secmod_SlotIsEmpty(mod, i)) {
|
|
|
|
return i;
|
|
|
|
}
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
PORT_SetError(SEC_ERROR_NO_SLOT_SELECTED);
|
2016-11-02 12:29:58 +03:00
|
|
|
return (CK_SLOT_ID)-1;
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to open a new slot.
|
|
|
|
*
|
|
|
|
* This works the same os OpenUserDB except it can be called against
|
|
|
|
* any module that understands the softoken protocol for opening new
|
|
|
|
* slots, not just the softoken itself. If the selected module does not
|
2016-11-02 12:29:58 +03:00
|
|
|
* understand the protocol, C_CreateObject will fail with
|
2009-08-19 17:59:06 +04:00
|
|
|
* CKR_INVALID_ATTRIBUTE, and SECMOD_OpenNewSlot will return NULL and set
|
|
|
|
* SEC_ERROR_BAD_DATA.
|
2016-11-02 12:29:58 +03:00
|
|
|
*
|
2009-08-19 17:59:06 +04:00
|
|
|
* NewSlots can be closed with SECMOD_CloseUserDB();
|
|
|
|
*
|
|
|
|
* Modulespec is module dependent.
|
|
|
|
*/
|
|
|
|
PK11SlotInfo *
|
|
|
|
SECMOD_OpenNewSlot(SECMODModule *mod, const char *moduleSpec)
|
|
|
|
{
|
|
|
|
CK_SLOT_ID slotID = 0;
|
|
|
|
PK11SlotInfo *slot;
|
|
|
|
char *escSpec;
|
|
|
|
char *sendSpec;
|
|
|
|
SECStatus rv;
|
|
|
|
|
|
|
|
slotID = secmod_FindFreeSlot(mod);
|
2016-11-02 12:29:58 +03:00
|
|
|
if (slotID == (CK_SLOT_ID)-1) {
|
|
|
|
return NULL;
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mod->slotCount == 0) {
|
2016-11-02 12:29:58 +03:00
|
|
|
return NULL;
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* just grab the first slot in the module, any present slot should work */
|
|
|
|
slot = PK11_ReferenceSlot(mod->slots[0]);
|
|
|
|
if (slot == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
return NULL;
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we've found the slot, now build the moduleSpec */
|
2012-10-01 22:02:15 +04:00
|
|
|
escSpec = NSSUTIL_DoubleEscape(moduleSpec, '>', ']');
|
2009-08-19 17:59:06 +04:00
|
|
|
if (escSpec == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
return NULL;
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
sendSpec = PR_smprintf("tokens=[0x%x=<%s>]", slotID, escSpec);
|
|
|
|
PORT_Free(escSpec);
|
|
|
|
|
|
|
|
if (sendSpec == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
/* PR_smprintf does not set SEC_ERROR_NO_MEMORY on failure. */
|
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
PORT_SetError(SEC_ERROR_NO_MEMORY);
|
|
|
|
return NULL;
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
rv = secmod_UserDBOp(slot, CKO_NETSCAPE_NEWSLOT, sendSpec);
|
|
|
|
PR_smprintf_free(sendSpec);
|
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
if (rv != SECSuccess) {
|
2016-11-02 12:29:58 +03:00
|
|
|
return NULL;
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-01 22:02:15 +04:00
|
|
|
slot = SECMOD_FindSlotByID(mod, slotID);
|
|
|
|
if (slot) {
|
2016-11-02 12:29:58 +03:00
|
|
|
/* if we are in the delay period for the "isPresent" call, reset
|
|
|
|
* the delay since we know things have probably changed... */
|
|
|
|
if (slot->nssToken && slot->nssToken->slot) {
|
|
|
|
nssSlot_ResetDelay(slot->nssToken->slot);
|
|
|
|
}
|
|
|
|
/* force the slot info structures to properly reset */
|
|
|
|
(void)PK11_IsPresent(slot);
|
2012-10-01 22:02:15 +04:00
|
|
|
}
|
|
|
|
return slot;
|
2009-08-19 17:59:06 +04:00
|
|
|
}
|
|
|
|
|
2016-11-02 12:29:58 +03:00
|
|
|
/*
|
|
|
|
* given a module spec, find the slot in the module for it.
|
|
|
|
*/
|
|
|
|
PK11SlotInfo *
|
|
|
|
secmod_FindSlotFromModuleSpec(const char *moduleSpec, SECMODModule *module)
|
|
|
|
{
|
|
|
|
CK_SLOT_ID slot_id = secmod_GetSlotIDFromModuleSpec(moduleSpec, module);
|
|
|
|
if (slot_id == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SECMOD_FindSlotByID(module, slot_id);
|
|
|
|
}
|
|
|
|
|
2008-06-06 16:40:11 +04:00
|
|
|
/*
|
|
|
|
* Open a new database using the softoken. The caller is responsible for making
|
|
|
|
* sure the module spec is correct and usable. The caller should ask for one
|
2016-11-02 12:29:58 +03:00
|
|
|
* new database per call if the caller wants to get meaningful information
|
2008-06-06 16:40:11 +04:00
|
|
|
* about the new database.
|
|
|
|
*
|
2016-11-02 12:29:58 +03:00
|
|
|
* moduleSpec is the same data that you would pass to softoken at
|
2008-06-06 16:40:11 +04:00
|
|
|
* initialization time under the 'tokens' options. For example, if you were
|
|
|
|
* to specify tokens=<0x4=[configdir='./mybackup' tokenDescription='Backup']>
|
|
|
|
* You would specify "configdir='./mybackup' tokenDescription='Backup'" as your
|
2016-11-02 12:29:58 +03:00
|
|
|
* module spec here. The slot ID will be calculated for you by
|
2008-06-06 16:40:11 +04:00
|
|
|
* SECMOD_OpenUserDB().
|
|
|
|
*
|
|
|
|
* Typical parameters here are configdir, tokenDescription and flags.
|
|
|
|
*
|
|
|
|
* a Full list is below:
|
|
|
|
*
|
|
|
|
*
|
2016-11-02 12:29:58 +03:00
|
|
|
* configDir - The location of the databases for this token. If configDir is
|
2008-06-06 16:40:11 +04:00
|
|
|
* not specified, and noCertDB and noKeyDB is not specified, the load
|
|
|
|
* will fail.
|
|
|
|
* certPrefix - Cert prefix for this token.
|
|
|
|
* keyPrefix - Prefix for the key database for this token. (if not specified,
|
|
|
|
* certPrefix will be used).
|
2016-11-02 12:29:58 +03:00
|
|
|
* tokenDescription - The label value for this token returned in the
|
|
|
|
* CK_TOKEN_INFO structure with an internationalize string (UTF8).
|
|
|
|
* This value will be truncated at 32 bytes (no NULL, partial UTF8
|
2008-06-06 16:40:11 +04:00
|
|
|
* characters dropped). You should specify a user friendly name here
|
2016-11-02 12:29:58 +03:00
|
|
|
* as this is the value the token will be referred to in most
|
2008-06-06 16:40:11 +04:00
|
|
|
* application UI's. You should make sure tokenDescription is unique.
|
2016-11-02 12:29:58 +03:00
|
|
|
* slotDescription - The slotDescription value for this token returned
|
|
|
|
* in the CK_SLOT_INFO structure with an internationalize string
|
|
|
|
* (UTF8). This value will be truncated at 64 bytes (no NULL, partial
|
|
|
|
* UTF8 characters dropped). This name will not change after the
|
2008-06-06 16:40:11 +04:00
|
|
|
* database is closed. It should have some number to make this unique.
|
|
|
|
* minPWLen - minimum password length for this token.
|
|
|
|
* flags - comma separated list of flag values, parsed case-insensitive.
|
|
|
|
* Valid flags are:
|
|
|
|
* readOnly - Databases should be opened read only.
|
|
|
|
* noCertDB - Don't try to open a certificate database.
|
|
|
|
* noKeyDB - Don't try to open a key database.
|
2016-11-02 12:29:58 +03:00
|
|
|
* forceOpen - Don't fail to initialize the token if the
|
2008-06-06 16:40:11 +04:00
|
|
|
* databases could not be opened.
|
2016-11-02 12:29:58 +03:00
|
|
|
* passwordRequired - zero length passwords are not acceptable
|
2008-06-06 16:40:11 +04:00
|
|
|
* (valid only if there is a keyDB).
|
|
|
|
* optimizeSpace - allocate smaller hash tables and lock tables.
|
2016-11-02 12:29:58 +03:00
|
|
|
* When this flag is not specified, Softoken will allocate
|
|
|
|
* large tables to prevent lock contention.
|
2008-06-06 16:40:11 +04:00
|
|
|
*/
|
|
|
|
PK11SlotInfo *
|
|
|
|
SECMOD_OpenUserDB(const char *moduleSpec)
|
|
|
|
{
|
|
|
|
SECMODModule *mod;
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMODConfigList *conflist = NULL;
|
|
|
|
int count = 0;
|
2008-06-06 16:40:11 +04:00
|
|
|
|
|
|
|
if (moduleSpec == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
return NULL;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: unlike most PK11 function, this does not return a reference
|
|
|
|
* to the module */
|
|
|
|
mod = SECMOD_GetInternalModule();
|
|
|
|
if (!mod) {
|
2016-11-02 12:29:58 +03:00
|
|
|
/* shouldn't happen */
|
|
|
|
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure we don't open the same database twice. We only understand
|
|
|
|
* the moduleSpec for internal databases well enough to do this, so only
|
|
|
|
* do this in OpenUserDB */
|
|
|
|
conflist = secmod_GetConfigList(mod->isFIPS, mod->libraryParams, &count);
|
|
|
|
if (conflist) {
|
|
|
|
PK11SlotInfo *slot = NULL;
|
|
|
|
if (secmod_MatchConfigList(moduleSpec, conflist, count)) {
|
|
|
|
slot = secmod_FindSlotFromModuleSpec(moduleSpec, mod);
|
|
|
|
}
|
|
|
|
secmod_FreeConfigList(conflist, count);
|
|
|
|
if (slot) {
|
|
|
|
return slot;
|
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2009-08-19 17:59:06 +04:00
|
|
|
return SECMOD_OpenNewSlot(mod, moduleSpec);
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close an already opened user database. NOTE: the database must be
|
|
|
|
* in the internal token, and must be one created with SECMOD_OpenUserDB().
|
|
|
|
* Once the database is closed, the slot will remain as an empty slot
|
2009-08-19 17:59:06 +04:00
|
|
|
* until it's used again with SECMOD_OpenUserDB() or SECMOD_OpenNewSlot().
|
2008-06-06 16:40:11 +04:00
|
|
|
*/
|
|
|
|
SECStatus
|
|
|
|
SECMOD_CloseUserDB(PK11SlotInfo *slot)
|
|
|
|
{
|
|
|
|
SECStatus rv;
|
|
|
|
char *sendSpec;
|
2016-11-02 12:29:58 +03:00
|
|
|
|
2008-06-06 16:40:11 +04:00
|
|
|
sendSpec = PR_smprintf("tokens=[0x%x=<>]", slot->slotID);
|
|
|
|
if (sendSpec == NULL) {
|
2016-11-02 12:29:58 +03:00
|
|
|
/* PR_smprintf does not set no memory error */
|
|
|
|
PORT_SetError(SEC_ERROR_NO_MEMORY);
|
|
|
|
return SECFailure;
|
2008-06-06 16:40:11 +04:00
|
|
|
}
|
2009-08-19 17:59:06 +04:00
|
|
|
rv = secmod_UserDBOp(slot, CKO_NETSCAPE_DELSLOT, sendSpec);
|
2008-06-06 16:40:11 +04:00
|
|
|
PR_smprintf_free(sendSpec);
|
2013-06-11 23:07:58 +04:00
|
|
|
/* if we are in the delay period for the "isPresent" call, reset
|
|
|
|
* the delay since we know things have probably changed... */
|
|
|
|
if (slot->nssToken && slot->nssToken->slot) {
|
2016-11-02 12:29:58 +03:00
|
|
|
nssSlot_ResetDelay(slot->nssToken->slot);
|
|
|
|
/* force the slot info structures to properly reset */
|
|
|
|
(void)PK11_IsPresent(slot);
|
2013-06-11 23:07:58 +04:00
|
|
|
}
|
2008-06-06 16:40:11 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2010-12-09 14:04:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Restart PKCS #11 modules after a fork(). See secmod.h for more information.
|
|
|
|
*/
|
|
|
|
SECStatus
|
|
|
|
SECMOD_RestartModules(PRBool force)
|
|
|
|
{
|
|
|
|
SECMODModuleList *mlp;
|
|
|
|
SECStatus rrv = SECSuccess;
|
|
|
|
int lastError = 0;
|
|
|
|
|
|
|
|
if (!moduleLock) {
|
2016-11-02 12:29:58 +03:00
|
|
|
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
|
|
|
|
return SECFailure;
|
2010-12-09 14:04:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only need to restart the PKCS #11 modules that were initialized */
|
|
|
|
SECMOD_GetReadLock(moduleLock);
|
|
|
|
for (mlp = modules; mlp != NULL; mlp = mlp->next) {
|
2016-11-02 12:29:58 +03:00
|
|
|
SECMODModule *mod = mlp->module;
|
|
|
|
CK_ULONG count;
|
|
|
|
SECStatus rv;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* If the module needs to be reset, do so */
|
|
|
|
if (force || (PK11_GETTAB(mod)->C_GetSlotList(CK_FALSE, NULL, &count) != CKR_OK)) {
|
2010-12-09 14:04:11 +03:00
|
|
|
PRBool alreadyLoaded;
|
2016-11-02 12:29:58 +03:00
|
|
|
/* first call Finalize. This is not required by PKCS #11, but some
|
2010-12-09 14:04:11 +03:00
|
|
|
* older modules require it, and it doesn't hurt (compliant modules
|
|
|
|
* will return CKR_NOT_INITIALIZED */
|
2016-11-02 12:29:58 +03:00
|
|
|
(void)PK11_GETTAB(mod)->C_Finalize(NULL);
|
|
|
|
/* now initialize the module, this function reinitializes
|
|
|
|
* a module in place, preserving existing slots (even if they
|
|
|
|
* no longer exist) */
|
|
|
|
rv = secmod_ModuleInit(mod, NULL, &alreadyLoaded);
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
/* save the last error code */
|
|
|
|
lastError = PORT_GetError();
|
|
|
|
rrv = rv;
|
|
|
|
/* couldn't reinit the module, disable all its slots */
|
|
|
|
for (i = 0; i < mod->slotCount; i++) {
|
|
|
|
mod->slots[i]->disabled = PR_TRUE;
|
|
|
|
mod->slots[i]->reason = PK11_DIS_COULD_NOT_INIT_TOKEN;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (i = 0; i < mod->slotCount; i++) {
|
|
|
|
/* get new token sessions, bump the series up so that
|
|
|
|
* we refresh other old sessions. This will tell much of
|
|
|
|
* NSS to flush cached handles it may hold as well */
|
|
|
|
rv = PK11_InitToken(mod->slots[i], PR_TRUE);
|
|
|
|
/* PK11_InitToken could fail if the slot isn't present.
|
|
|
|
* If it is present, though, something is wrong and we should
|
|
|
|
* disable the slot and let the caller know. */
|
|
|
|
if (rv != SECSuccess && PK11_IsPresent(mod->slots[i])) {
|
|
|
|
/* save the last error code */
|
|
|
|
lastError = PORT_GetError();
|
|
|
|
rrv = rv;
|
|
|
|
/* disable the token */
|
|
|
|
mod->slots[i]->disabled = PR_TRUE;
|
|
|
|
mod->slots[i]->reason = PK11_DIS_COULD_NOT_INIT_TOKEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-09 14:04:11 +03:00
|
|
|
}
|
|
|
|
SECMOD_ReleaseReadLock(moduleLock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* on multiple failures, we are only returning the lastError. The caller
|
|
|
|
* can determine which slots are bad by calling PK11_IsDisabled().
|
|
|
|
*/
|
|
|
|
if (rrv != SECSuccess) {
|
2016-11-02 12:29:58 +03:00
|
|
|
/* restore the last error code */
|
|
|
|
PORT_SetError(lastError);
|
2010-12-09 14:04:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rrv;
|
|
|
|
}
|