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