12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295 |
- /* 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/. */
- /*
- * instance.c
- *
- * This file implements the NSSCKFWInstance type and methods.
- */
- #ifndef CK_T
- #include "ck.h"
- #endif /* CK_T */
- /*
- * NSSCKFWInstance
- *
- * -- create/destroy --
- * nssCKFWInstance_Create
- * nssCKFWInstance_Destroy
- *
- * -- public accessors --
- * NSSCKFWInstance_GetMDInstance
- * NSSCKFWInstance_GetArena
- * NSSCKFWInstance_MayCreatePthreads
- * NSSCKFWInstance_CreateMutex
- * NSSCKFWInstance_GetConfigurationData
- * NSSCKFWInstance_GetInitArgs
- * NSSCKFWInstance_DestroySessionHandle
- * NSSCKFWInstance_FindSessionHandle
- *
- * -- implement public accessors --
- * nssCKFWInstance_GetMDInstance
- * nssCKFWInstance_GetArena
- * nssCKFWInstance_MayCreatePthreads
- * nssCKFWInstance_CreateMutex
- * nssCKFWInstance_GetConfigurationData
- * nssCKFWInstance_GetInitArgs
- * nssCKFWInstance_DestroySessionHandle
- * nssCKFWInstance_FindSessionHandle
- *
- * -- private accessors --
- * nssCKFWInstance_CreateSessionHandle
- * nssCKFWInstance_ResolveSessionHandle
- * nssCKFWInstance_CreateObjectHandle
- * nssCKFWInstance_ResolveObjectHandle
- * nssCKFWInstance_DestroyObjectHandle
- *
- * -- module fronts --
- * nssCKFWInstance_GetNSlots
- * nssCKFWInstance_GetCryptokiVersion
- * nssCKFWInstance_GetManufacturerID
- * nssCKFWInstance_GetFlags
- * nssCKFWInstance_GetLibraryDescription
- * nssCKFWInstance_GetLibraryVersion
- * nssCKFWInstance_GetModuleHandlesSessionObjects
- * nssCKFWInstance_GetSlots
- * nssCKFWInstance_WaitForSlotEvent
- *
- * -- debugging versions only --
- * nssCKFWInstance_verifyPointer
- */
- struct NSSCKFWInstanceStr {
- NSSCKFWMutex *mutex;
- NSSArena *arena;
- NSSCKMDInstance *mdInstance;
- CK_C_INITIALIZE_ARGS_PTR pInitArgs;
- CK_C_INITIALIZE_ARGS initArgs;
- CryptokiLockingState LockingState;
- CK_BBOOL mayCreatePthreads;
- NSSUTF8 *configurationData;
- CK_ULONG nSlots;
- NSSCKFWSlot **fwSlotList;
- NSSCKMDSlot **mdSlotList;
- CK_BBOOL moduleHandlesSessionObjects;
- /*
- * Everything above is set at creation time, and then not modified.
- * The invariants the mutex protects are:
- *
- * 1) Each of the cached descriptions (versions, etc.) are in an
- * internally consistant state.
- *
- * 2) The session handle hashes and count are consistant
- *
- * 3) The object handle hashes and count are consistant.
- *
- * I could use multiple locks, but let's wait to see if that's
- * really necessary.
- *
- * Note that the calls accessing the cached descriptions will
- * call the NSSCKMDInstance methods with the mutex locked. Those
- * methods may then call the public NSSCKFWInstance routines.
- * Those public routines only access the constant data above, so
- * there's no problem. But be careful if you add to this object;
- * mutexes are in general not reentrant, so don't create deadlock
- * situations.
- */
- CK_VERSION cryptokiVersion;
- NSSUTF8 *manufacturerID;
- NSSUTF8 *libraryDescription;
- CK_VERSION libraryVersion;
- CK_ULONG lastSessionHandle;
- nssCKFWHash *sessionHandleHash;
- CK_ULONG lastObjectHandle;
- nssCKFWHash *objectHandleHash;
- };
- #ifdef DEBUG
- /*
- * But first, the pointer-tracking stuff.
- *
- * NOTE: the pointer-tracking support in NSS/base currently relies
- * upon NSPR's CallOnce support. That, however, relies upon NSPR's
- * locking, which is tied into the runtime. We need a pointer-tracker
- * implementation that uses the locks supplied through C_Initialize.
- * That support, however, can be filled in later. So for now, I'll
- * just do this routines as no-ops.
- */
- static CK_RV
- instance_add_pointer(
- const NSSCKFWInstance *fwInstance)
- {
- return CKR_OK;
- }
- static CK_RV
- instance_remove_pointer(
- const NSSCKFWInstance *fwInstance)
- {
- return CKR_OK;
- }
- NSS_IMPLEMENT CK_RV
- nssCKFWInstance_verifyPointer(
- const NSSCKFWInstance *fwInstance)
- {
- return CKR_OK;
- }
- #endif /* DEBUG */
- /*
- * nssCKFWInstance_Create
- *
- */
- NSS_IMPLEMENT NSSCKFWInstance *
- nssCKFWInstance_Create(
- CK_C_INITIALIZE_ARGS_PTR pInitArgs,
- CryptokiLockingState LockingState,
- NSSCKMDInstance *mdInstance,
- CK_RV *pError)
- {
- NSSCKFWInstance *fwInstance;
- NSSArena *arena = (NSSArena *)NULL;
- CK_ULONG i;
- CK_BBOOL called_Initialize = CK_FALSE;
- #ifdef NSSDEBUG
- if ((CK_RV)NULL == pError) {
- return (NSSCKFWInstance *)NULL;
- }
- if (!mdInstance) {
- *pError = CKR_ARGUMENTS_BAD;
- return (NSSCKFWInstance *)NULL;
- }
- #endif /* NSSDEBUG */
- arena = NSSArena_Create();
- if (!arena) {
- *pError = CKR_HOST_MEMORY;
- return (NSSCKFWInstance *)NULL;
- }
- fwInstance = nss_ZNEW(arena, NSSCKFWInstance);
- if (!fwInstance) {
- goto nomem;
- }
- fwInstance->arena = arena;
- fwInstance->mdInstance = mdInstance;
- fwInstance->LockingState = LockingState;
- if ((CK_C_INITIALIZE_ARGS_PTR)NULL != pInitArgs) {
- fwInstance->initArgs = *pInitArgs;
- fwInstance->pInitArgs = &fwInstance->initArgs;
- if (pInitArgs->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS) {
- fwInstance->mayCreatePthreads = CK_FALSE;
- } else {
- fwInstance->mayCreatePthreads = CK_TRUE;
- }
- fwInstance->configurationData = (NSSUTF8 *)(pInitArgs->pReserved);
- } else {
- fwInstance->mayCreatePthreads = CK_TRUE;
- }
- fwInstance->mutex = nssCKFWMutex_Create(pInitArgs, LockingState, arena,
- pError);
- if (!fwInstance->mutex) {
- if (CKR_OK == *pError) {
- *pError = CKR_GENERAL_ERROR;
- }
- goto loser;
- }
- if (mdInstance->Initialize) {
- *pError = mdInstance->Initialize(mdInstance, fwInstance, fwInstance->configurationData);
- if (CKR_OK != *pError) {
- goto loser;
- }
- called_Initialize = CK_TRUE;
- }
- if (mdInstance->ModuleHandlesSessionObjects) {
- fwInstance->moduleHandlesSessionObjects =
- mdInstance->ModuleHandlesSessionObjects(mdInstance, fwInstance);
- } else {
- fwInstance->moduleHandlesSessionObjects = CK_FALSE;
- }
- if (!mdInstance->GetNSlots) {
- /* That routine is required */
- *pError = CKR_GENERAL_ERROR;
- goto loser;
- }
- fwInstance->nSlots = mdInstance->GetNSlots(mdInstance, fwInstance, pError);
- if ((CK_ULONG)0 == fwInstance->nSlots) {
- if (CKR_OK == *pError) {
- /* Zero is not a legitimate answer */
- *pError = CKR_GENERAL_ERROR;
- }
- goto loser;
- }
- fwInstance->fwSlotList = nss_ZNEWARRAY(arena, NSSCKFWSlot *, fwInstance->nSlots);
- if ((NSSCKFWSlot **)NULL == fwInstance->fwSlotList) {
- goto nomem;
- }
- fwInstance->mdSlotList = nss_ZNEWARRAY(arena, NSSCKMDSlot *, fwInstance->nSlots);
- if ((NSSCKMDSlot **)NULL == fwInstance->mdSlotList) {
- goto nomem;
- }
- fwInstance->sessionHandleHash = nssCKFWHash_Create(fwInstance,
- fwInstance->arena, pError);
- if (!fwInstance->sessionHandleHash) {
- goto loser;
- }
- fwInstance->objectHandleHash = nssCKFWHash_Create(fwInstance,
- fwInstance->arena, pError);
- if (!fwInstance->objectHandleHash) {
- goto loser;
- }
- if (!mdInstance->GetSlots) {
- /* That routine is required */
- *pError = CKR_GENERAL_ERROR;
- goto loser;
- }
- *pError = mdInstance->GetSlots(mdInstance, fwInstance, fwInstance->mdSlotList);
- if (CKR_OK != *pError) {
- goto loser;
- }
- for (i = 0; i < fwInstance->nSlots; i++) {
- NSSCKMDSlot *mdSlot = fwInstance->mdSlotList[i];
- if (!mdSlot) {
- *pError = CKR_GENERAL_ERROR;
- goto loser;
- }
- fwInstance->fwSlotList[i] = nssCKFWSlot_Create(fwInstance, mdSlot, i, pError);
- if (CKR_OK != *pError) {
- CK_ULONG j;
- for (j = 0; j < i; j++) {
- (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[j]);
- }
- for (j = i; j < fwInstance->nSlots; j++) {
- NSSCKMDSlot *mds = fwInstance->mdSlotList[j];
- if (mds->Destroy) {
- mds->Destroy(mds, (NSSCKFWSlot *)NULL, mdInstance, fwInstance);
- }
- }
- goto loser;
- }
- }
- #ifdef DEBUG
- *pError = instance_add_pointer(fwInstance);
- if (CKR_OK != *pError) {
- for (i = 0; i < fwInstance->nSlots; i++) {
- (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[i]);
- }
- goto loser;
- }
- #endif /* DEBUG */
- *pError = CKR_OK;
- return fwInstance;
- nomem:
- *pError = CKR_HOST_MEMORY;
- /*FALLTHROUGH*/
- loser:
- if (CK_TRUE == called_Initialize) {
- if (mdInstance->Finalize) {
- mdInstance->Finalize(mdInstance, fwInstance);
- }
- }
- if (fwInstance && fwInstance->mutex) {
- nssCKFWMutex_Destroy(fwInstance->mutex);
- }
- if (arena) {
- (void)NSSArena_Destroy(arena);
- }
- return (NSSCKFWInstance *)NULL;
- }
- /*
- * nssCKFWInstance_Destroy
- *
- */
- NSS_IMPLEMENT CK_RV
- nssCKFWInstance_Destroy(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef NSSDEBUG
- CK_RV error = CKR_OK;
- #endif /* NSSDEBUG */
- CK_ULONG i;
- #ifdef NSSDEBUG
- error = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != error) {
- return error;
- }
- #endif /* NSSDEBUG */
- nssCKFWMutex_Destroy(fwInstance->mutex);
- for (i = 0; i < fwInstance->nSlots; i++) {
- (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[i]);
- }
- if (fwInstance->mdInstance->Finalize) {
- fwInstance->mdInstance->Finalize(fwInstance->mdInstance, fwInstance);
- }
- if (fwInstance->sessionHandleHash) {
- nssCKFWHash_Destroy(fwInstance->sessionHandleHash);
- }
- if (fwInstance->objectHandleHash) {
- nssCKFWHash_Destroy(fwInstance->objectHandleHash);
- }
- #ifdef DEBUG
- (void)instance_remove_pointer(fwInstance);
- #endif /* DEBUG */
- (void)NSSArena_Destroy(fwInstance->arena);
- return CKR_OK;
- }
- /*
- * nssCKFWInstance_GetMDInstance
- *
- */
- NSS_IMPLEMENT NSSCKMDInstance *
- nssCKFWInstance_GetMDInstance(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (NSSCKMDInstance *)NULL;
- }
- #endif /* NSSDEBUG */
- return fwInstance->mdInstance;
- }
- /*
- * nssCKFWInstance_GetArena
- *
- */
- NSS_IMPLEMENT NSSArena *
- nssCKFWInstance_GetArena(
- NSSCKFWInstance *fwInstance,
- CK_RV *pError)
- {
- #ifdef NSSDEBUG
- if (!pError) {
- return (NSSArena *)NULL;
- }
- *pError = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != *pError) {
- return (NSSArena *)NULL;
- }
- #endif /* NSSDEBUG */
- *pError = CKR_OK;
- return fwInstance->arena;
- }
- /*
- * nssCKFWInstance_MayCreatePthreads
- *
- */
- NSS_IMPLEMENT CK_BBOOL
- nssCKFWInstance_MayCreatePthreads(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return CK_FALSE;
- }
- #endif /* NSSDEBUG */
- return fwInstance->mayCreatePthreads;
- }
- /*
- * nssCKFWInstance_CreateMutex
- *
- */
- NSS_IMPLEMENT NSSCKFWMutex *
- nssCKFWInstance_CreateMutex(
- NSSCKFWInstance *fwInstance,
- NSSArena *arena,
- CK_RV *pError)
- {
- NSSCKFWMutex *mutex;
- #ifdef NSSDEBUG
- if (!pError) {
- return (NSSCKFWMutex *)NULL;
- }
- *pError = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != *pError) {
- return (NSSCKFWMutex *)NULL;
- }
- #endif /* NSSDEBUG */
- mutex = nssCKFWMutex_Create(fwInstance->pInitArgs, fwInstance->LockingState,
- arena, pError);
- if (!mutex) {
- if (CKR_OK == *pError) {
- *pError = CKR_GENERAL_ERROR;
- }
- return (NSSCKFWMutex *)NULL;
- }
- return mutex;
- }
- /*
- * nssCKFWInstance_GetConfigurationData
- *
- */
- NSS_IMPLEMENT NSSUTF8 *
- nssCKFWInstance_GetConfigurationData(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (NSSUTF8 *)NULL;
- }
- #endif /* NSSDEBUG */
- return fwInstance->configurationData;
- }
- /*
- * nssCKFWInstance_GetInitArgs
- *
- */
- CK_C_INITIALIZE_ARGS_PTR
- nssCKFWInstance_GetInitArgs(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (CK_C_INITIALIZE_ARGS_PTR)NULL;
- }
- #endif /* NSSDEBUG */
- return fwInstance->pInitArgs;
- }
- /*
- * nssCKFWInstance_CreateSessionHandle
- *
- */
- NSS_IMPLEMENT CK_SESSION_HANDLE
- nssCKFWInstance_CreateSessionHandle(
- NSSCKFWInstance *fwInstance,
- NSSCKFWSession *fwSession,
- CK_RV *pError)
- {
- CK_SESSION_HANDLE hSession;
- #ifdef NSSDEBUG
- if (!pError) {
- return (CK_SESSION_HANDLE)0;
- }
- *pError = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != *pError) {
- return (CK_SESSION_HANDLE)0;
- }
- #endif /* NSSDEBUG */
- *pError = nssCKFWMutex_Lock(fwInstance->mutex);
- if (CKR_OK != *pError) {
- return (CK_SESSION_HANDLE)0;
- }
- hSession = ++(fwInstance->lastSessionHandle);
- /* Alan would say I should unlock for this call. */
- *pError = nssCKFWSession_SetHandle(fwSession, hSession);
- if (CKR_OK != *pError) {
- goto done;
- }
- *pError = nssCKFWHash_Add(fwInstance->sessionHandleHash,
- (const void *)hSession, (const void *)fwSession);
- if (CKR_OK != *pError) {
- hSession = (CK_SESSION_HANDLE)0;
- goto done;
- }
- done:
- nssCKFWMutex_Unlock(fwInstance->mutex);
- return hSession;
- }
- /*
- * nssCKFWInstance_ResolveSessionHandle
- *
- */
- NSS_IMPLEMENT NSSCKFWSession *
- nssCKFWInstance_ResolveSessionHandle(
- NSSCKFWInstance *fwInstance,
- CK_SESSION_HANDLE hSession)
- {
- NSSCKFWSession *fwSession;
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (NSSCKFWSession *)NULL;
- }
- #endif /* NSSDEBUG */
- if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
- return (NSSCKFWSession *)NULL;
- }
- fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
- fwInstance->sessionHandleHash, (const void *)hSession);
- /* Assert(hSession == nssCKFWSession_GetHandle(fwSession)) */
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return fwSession;
- }
- /*
- * nssCKFWInstance_DestroySessionHandle
- *
- */
- NSS_IMPLEMENT void
- nssCKFWInstance_DestroySessionHandle(
- NSSCKFWInstance *fwInstance,
- CK_SESSION_HANDLE hSession)
- {
- NSSCKFWSession *fwSession;
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return;
- }
- #endif /* NSSDEBUG */
- if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
- return;
- }
- fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
- fwInstance->sessionHandleHash, (const void *)hSession);
- if (fwSession) {
- nssCKFWHash_Remove(fwInstance->sessionHandleHash, (const void *)hSession);
- nssCKFWSession_SetHandle(fwSession, (CK_SESSION_HANDLE)0);
- }
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return;
- }
- /*
- * nssCKFWInstance_FindSessionHandle
- *
- */
- NSS_IMPLEMENT CK_SESSION_HANDLE
- nssCKFWInstance_FindSessionHandle(
- NSSCKFWInstance *fwInstance,
- NSSCKFWSession *fwSession)
- {
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (CK_SESSION_HANDLE)0;
- }
- if (CKR_OK != nssCKFWSession_verifyPointer(fwSession)) {
- return (CK_SESSION_HANDLE)0;
- }
- #endif /* NSSDEBUG */
- return nssCKFWSession_GetHandle(fwSession);
- /* look it up and assert? */
- }
- /*
- * nssCKFWInstance_CreateObjectHandle
- *
- */
- NSS_IMPLEMENT CK_OBJECT_HANDLE
- nssCKFWInstance_CreateObjectHandle(
- NSSCKFWInstance *fwInstance,
- NSSCKFWObject *fwObject,
- CK_RV *pError)
- {
- CK_OBJECT_HANDLE hObject;
- #ifdef NSSDEBUG
- if (!pError) {
- return (CK_OBJECT_HANDLE)0;
- }
- *pError = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != *pError) {
- return (CK_OBJECT_HANDLE)0;
- }
- #endif /* NSSDEBUG */
- *pError = nssCKFWMutex_Lock(fwInstance->mutex);
- if (CKR_OK != *pError) {
- return (CK_OBJECT_HANDLE)0;
- }
- hObject = ++(fwInstance->lastObjectHandle);
- *pError = nssCKFWObject_SetHandle(fwObject, hObject);
- if (CKR_OK != *pError) {
- hObject = (CK_OBJECT_HANDLE)0;
- goto done;
- }
- *pError = nssCKFWHash_Add(fwInstance->objectHandleHash,
- (const void *)hObject, (const void *)fwObject);
- if (CKR_OK != *pError) {
- hObject = (CK_OBJECT_HANDLE)0;
- goto done;
- }
- done:
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return hObject;
- }
- /*
- * nssCKFWInstance_ResolveObjectHandle
- *
- */
- NSS_IMPLEMENT NSSCKFWObject *
- nssCKFWInstance_ResolveObjectHandle(
- NSSCKFWInstance *fwInstance,
- CK_OBJECT_HANDLE hObject)
- {
- NSSCKFWObject *fwObject;
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (NSSCKFWObject *)NULL;
- }
- #endif /* NSSDEBUG */
- if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
- return (NSSCKFWObject *)NULL;
- }
- fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
- fwInstance->objectHandleHash, (const void *)hObject);
- /* Assert(hObject == nssCKFWObject_GetHandle(fwObject)) */
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return fwObject;
- }
- /*
- * nssCKFWInstance_ReassignObjectHandle
- *
- */
- NSS_IMPLEMENT CK_RV
- nssCKFWInstance_ReassignObjectHandle(
- NSSCKFWInstance *fwInstance,
- CK_OBJECT_HANDLE hObject,
- NSSCKFWObject *fwObject)
- {
- CK_RV error = CKR_OK;
- NSSCKFWObject *oldObject;
- #ifdef NSSDEBUG
- error = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != error) {
- return error;
- }
- #endif /* NSSDEBUG */
- error = nssCKFWMutex_Lock(fwInstance->mutex);
- if (CKR_OK != error) {
- return error;
- }
- oldObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
- fwInstance->objectHandleHash, (const void *)hObject);
- if (oldObject) {
- /* Assert(hObject == nssCKFWObject_GetHandle(oldObject) */
- (void)nssCKFWObject_SetHandle(oldObject, (CK_SESSION_HANDLE)0);
- nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)hObject);
- }
- error = nssCKFWObject_SetHandle(fwObject, hObject);
- if (CKR_OK != error) {
- goto done;
- }
- error = nssCKFWHash_Add(fwInstance->objectHandleHash,
- (const void *)hObject, (const void *)fwObject);
- done:
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return error;
- }
- /*
- * nssCKFWInstance_DestroyObjectHandle
- *
- */
- NSS_IMPLEMENT void
- nssCKFWInstance_DestroyObjectHandle(
- NSSCKFWInstance *fwInstance,
- CK_OBJECT_HANDLE hObject)
- {
- NSSCKFWObject *fwObject;
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return;
- }
- #endif /* NSSDEBUG */
- if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
- return;
- }
- fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
- fwInstance->objectHandleHash, (const void *)hObject);
- if (fwObject) {
- /* Assert(hObject = nssCKFWObject_GetHandle(fwObject)) */
- nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)hObject);
- (void)nssCKFWObject_SetHandle(fwObject, (CK_SESSION_HANDLE)0);
- }
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return;
- }
- /*
- * nssCKFWInstance_FindObjectHandle
- *
- */
- NSS_IMPLEMENT CK_OBJECT_HANDLE
- nssCKFWInstance_FindObjectHandle(
- NSSCKFWInstance *fwInstance,
- NSSCKFWObject *fwObject)
- {
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (CK_OBJECT_HANDLE)0;
- }
- if (CKR_OK != nssCKFWObject_verifyPointer(fwObject)) {
- return (CK_OBJECT_HANDLE)0;
- }
- #endif /* NSSDEBUG */
- return nssCKFWObject_GetHandle(fwObject);
- }
- /*
- * nssCKFWInstance_GetNSlots
- *
- */
- NSS_IMPLEMENT CK_ULONG
- nssCKFWInstance_GetNSlots(
- NSSCKFWInstance *fwInstance,
- CK_RV *pError)
- {
- #ifdef NSSDEBUG
- if (!pError) {
- return (CK_ULONG)0;
- }
- *pError = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != *pError) {
- return (CK_ULONG)0;
- }
- #endif /* NSSDEBUG */
- *pError = CKR_OK;
- return fwInstance->nSlots;
- }
- /*
- * nssCKFWInstance_GetCryptokiVersion
- *
- */
- NSS_IMPLEMENT CK_VERSION
- nssCKFWInstance_GetCryptokiVersion(
- NSSCKFWInstance *fwInstance)
- {
- CK_VERSION rv;
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- rv.major = rv.minor = 0;
- return rv;
- }
- #endif /* NSSDEBUG */
- if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
- rv.major = rv.minor = 0;
- return rv;
- }
- if ((0 != fwInstance->cryptokiVersion.major) ||
- (0 != fwInstance->cryptokiVersion.minor)) {
- rv = fwInstance->cryptokiVersion;
- goto done;
- }
- if (fwInstance->mdInstance->GetCryptokiVersion) {
- fwInstance->cryptokiVersion = fwInstance->mdInstance->GetCryptokiVersion(
- fwInstance->mdInstance, fwInstance);
- } else {
- fwInstance->cryptokiVersion.major = 2;
- fwInstance->cryptokiVersion.minor = 1;
- }
- rv = fwInstance->cryptokiVersion;
- done:
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return rv;
- }
- /*
- * nssCKFWInstance_GetManufacturerID
- *
- */
- NSS_IMPLEMENT CK_RV
- nssCKFWInstance_GetManufacturerID(
- NSSCKFWInstance *fwInstance,
- CK_CHAR manufacturerID[32])
- {
- CK_RV error = CKR_OK;
- #ifdef NSSDEBUG
- if ((CK_CHAR_PTR)NULL == manufacturerID) {
- return CKR_ARGUMENTS_BAD;
- }
- error = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != error) {
- return error;
- }
- #endif /* NSSDEBUG */
- error = nssCKFWMutex_Lock(fwInstance->mutex);
- if (CKR_OK != error) {
- return error;
- }
- if (!fwInstance->manufacturerID) {
- if (fwInstance->mdInstance->GetManufacturerID) {
- fwInstance->manufacturerID = fwInstance->mdInstance->GetManufacturerID(
- fwInstance->mdInstance, fwInstance, &error);
- if ((!fwInstance->manufacturerID) && (CKR_OK != error)) {
- goto done;
- }
- } else {
- fwInstance->manufacturerID = (NSSUTF8 *)"";
- }
- }
- (void)nssUTF8_CopyIntoFixedBuffer(fwInstance->manufacturerID, (char *)manufacturerID, 32, ' ');
- error = CKR_OK;
- done:
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return error;
- }
- /*
- * nssCKFWInstance_GetFlags
- *
- */
- NSS_IMPLEMENT CK_ULONG
- nssCKFWInstance_GetFlags(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (CK_ULONG)0;
- }
- #endif /* NSSDEBUG */
- /* No "instance flags" are yet defined by Cryptoki. */
- return (CK_ULONG)0;
- }
- /*
- * nssCKFWInstance_GetLibraryDescription
- *
- */
- NSS_IMPLEMENT CK_RV
- nssCKFWInstance_GetLibraryDescription(
- NSSCKFWInstance *fwInstance,
- CK_CHAR libraryDescription[32])
- {
- CK_RV error = CKR_OK;
- #ifdef NSSDEBUG
- if ((CK_CHAR_PTR)NULL == libraryDescription) {
- return CKR_ARGUMENTS_BAD;
- }
- error = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != error) {
- return error;
- }
- #endif /* NSSDEBUG */
- error = nssCKFWMutex_Lock(fwInstance->mutex);
- if (CKR_OK != error) {
- return error;
- }
- if (!fwInstance->libraryDescription) {
- if (fwInstance->mdInstance->GetLibraryDescription) {
- fwInstance->libraryDescription = fwInstance->mdInstance->GetLibraryDescription(
- fwInstance->mdInstance, fwInstance, &error);
- if ((!fwInstance->libraryDescription) && (CKR_OK != error)) {
- goto done;
- }
- } else {
- fwInstance->libraryDescription = (NSSUTF8 *)"";
- }
- }
- (void)nssUTF8_CopyIntoFixedBuffer(fwInstance->libraryDescription, (char *)libraryDescription, 32, ' ');
- error = CKR_OK;
- done:
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return error;
- }
- /*
- * nssCKFWInstance_GetLibraryVersion
- *
- */
- NSS_IMPLEMENT CK_VERSION
- nssCKFWInstance_GetLibraryVersion(
- NSSCKFWInstance *fwInstance)
- {
- CK_VERSION rv;
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- rv.major = rv.minor = 0;
- return rv;
- }
- #endif /* NSSDEBUG */
- if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
- rv.major = rv.minor = 0;
- return rv;
- }
- if ((0 != fwInstance->libraryVersion.major) ||
- (0 != fwInstance->libraryVersion.minor)) {
- rv = fwInstance->libraryVersion;
- goto done;
- }
- if (fwInstance->mdInstance->GetLibraryVersion) {
- fwInstance->libraryVersion = fwInstance->mdInstance->GetLibraryVersion(
- fwInstance->mdInstance, fwInstance);
- } else {
- fwInstance->libraryVersion.major = 0;
- fwInstance->libraryVersion.minor = 3;
- }
- rv = fwInstance->libraryVersion;
- done:
- (void)nssCKFWMutex_Unlock(fwInstance->mutex);
- return rv;
- }
- /*
- * nssCKFWInstance_GetModuleHandlesSessionObjects
- *
- */
- NSS_IMPLEMENT CK_BBOOL
- nssCKFWInstance_GetModuleHandlesSessionObjects(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef NSSDEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return CK_FALSE;
- }
- #endif /* NSSDEBUG */
- return fwInstance->moduleHandlesSessionObjects;
- }
- /*
- * nssCKFWInstance_GetSlots
- *
- */
- NSS_IMPLEMENT NSSCKFWSlot **
- nssCKFWInstance_GetSlots(
- NSSCKFWInstance *fwInstance,
- CK_RV *pError)
- {
- #ifdef NSSDEBUG
- if (!pError) {
- return (NSSCKFWSlot **)NULL;
- }
- *pError = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != *pError) {
- return (NSSCKFWSlot **)NULL;
- }
- #endif /* NSSDEBUG */
- return fwInstance->fwSlotList;
- }
- /*
- * nssCKFWInstance_WaitForSlotEvent
- *
- */
- NSS_IMPLEMENT NSSCKFWSlot *
- nssCKFWInstance_WaitForSlotEvent(
- NSSCKFWInstance *fwInstance,
- CK_BBOOL block,
- CK_RV *pError)
- {
- NSSCKFWSlot *fwSlot = (NSSCKFWSlot *)NULL;
- NSSCKMDSlot *mdSlot;
- CK_ULONG i, n;
- #ifdef NSSDEBUG
- if (!pError) {
- return (NSSCKFWSlot *)NULL;
- }
- *pError = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != *pError) {
- return (NSSCKFWSlot *)NULL;
- }
- switch (block) {
- case CK_TRUE:
- case CK_FALSE:
- break;
- default:
- *pError = CKR_ARGUMENTS_BAD;
- return (NSSCKFWSlot *)NULL;
- }
- #endif /* NSSDEBUG */
- if (!fwInstance->mdInstance->WaitForSlotEvent) {
- *pError = CKR_NO_EVENT;
- return (NSSCKFWSlot *)NULL;
- }
- mdSlot = fwInstance->mdInstance->WaitForSlotEvent(
- fwInstance->mdInstance,
- fwInstance,
- block,
- pError);
- if (!mdSlot) {
- return (NSSCKFWSlot *)NULL;
- }
- n = nssCKFWInstance_GetNSlots(fwInstance, pError);
- if (((CK_ULONG)0 == n) && (CKR_OK != *pError)) {
- return (NSSCKFWSlot *)NULL;
- }
- for (i = 0; i < n; i++) {
- if (fwInstance->mdSlotList[i] == mdSlot) {
- fwSlot = fwInstance->fwSlotList[i];
- break;
- }
- }
- if (!fwSlot) {
- /* Internal error */
- *pError = CKR_GENERAL_ERROR;
- return (NSSCKFWSlot *)NULL;
- }
- return fwSlot;
- }
- /*
- * NSSCKFWInstance_GetMDInstance
- *
- */
- NSS_IMPLEMENT NSSCKMDInstance *
- NSSCKFWInstance_GetMDInstance(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef DEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (NSSCKMDInstance *)NULL;
- }
- #endif /* DEBUG */
- return nssCKFWInstance_GetMDInstance(fwInstance);
- }
- /*
- * NSSCKFWInstance_GetArena
- *
- */
- NSS_IMPLEMENT NSSArena *
- NSSCKFWInstance_GetArena(
- NSSCKFWInstance *fwInstance,
- CK_RV *pError)
- {
- #ifdef DEBUG
- if (!pError) {
- return (NSSArena *)NULL;
- }
- *pError = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != *pError) {
- return (NSSArena *)NULL;
- }
- #endif /* DEBUG */
- return nssCKFWInstance_GetArena(fwInstance, pError);
- }
- /*
- * NSSCKFWInstance_MayCreatePthreads
- *
- */
- NSS_IMPLEMENT CK_BBOOL
- NSSCKFWInstance_MayCreatePthreads(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef DEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return CK_FALSE;
- }
- #endif /* DEBUG */
- return nssCKFWInstance_MayCreatePthreads(fwInstance);
- }
- /*
- * NSSCKFWInstance_CreateMutex
- *
- */
- NSS_IMPLEMENT NSSCKFWMutex *
- NSSCKFWInstance_CreateMutex(
- NSSCKFWInstance *fwInstance,
- NSSArena *arena,
- CK_RV *pError)
- {
- #ifdef DEBUG
- if (!pError) {
- return (NSSCKFWMutex *)NULL;
- }
- *pError = nssCKFWInstance_verifyPointer(fwInstance);
- if (CKR_OK != *pError) {
- return (NSSCKFWMutex *)NULL;
- }
- #endif /* DEBUG */
- return nssCKFWInstance_CreateMutex(fwInstance, arena, pError);
- }
- /*
- * NSSCKFWInstance_GetConfigurationData
- *
- */
- NSS_IMPLEMENT NSSUTF8 *
- NSSCKFWInstance_GetConfigurationData(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef DEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (NSSUTF8 *)NULL;
- }
- #endif /* DEBUG */
- return nssCKFWInstance_GetConfigurationData(fwInstance);
- }
- /*
- * NSSCKFWInstance_GetInitArgs
- *
- */
- NSS_IMPLEMENT CK_C_INITIALIZE_ARGS_PTR
- NSSCKFWInstance_GetInitArgs(
- NSSCKFWInstance *fwInstance)
- {
- #ifdef DEBUG
- if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
- return (CK_C_INITIALIZE_ARGS_PTR)NULL;
- }
- #endif /* DEBUG */
- return nssCKFWInstance_GetInitArgs(fwInstance);
- }
- /*
- * nssCKFWInstance_DestroySessionHandle
- *
- */
- NSS_IMPLEMENT void
- NSSCKFWInstance_DestroySessionHandle(
- NSSCKFWInstance *fwInstance,
- CK_SESSION_HANDLE hSession)
- {
- nssCKFWInstance_DestroySessionHandle(fwInstance, hSession);
- }
- /*
- * nssCKFWInstance_FindSessionHandle
- *
- */
- NSS_IMPLEMENT CK_SESSION_HANDLE
- NSSCKFWInstance_FindSessionHandle(
- NSSCKFWInstance *fwInstance,
- NSSCKFWSession *fwSession)
- {
- return nssCKFWInstance_FindSessionHandle(fwInstance, fwSession);
- }
|