security/nss/lib/ckfw/instance.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /*
     6  * instance.c
     7  *
     8  * This file implements the NSSCKFWInstance type and methods.
     9  */
    11 #ifndef CK_T
    12 #include "ck.h"
    13 #endif /* CK_T */
    15 /*
    16  * NSSCKFWInstance
    17  *
    18  *  -- create/destroy --
    19  *  nssCKFWInstance_Create
    20  *  nssCKFWInstance_Destroy
    21  *
    22  *  -- public accessors --
    23  *  NSSCKFWInstance_GetMDInstance
    24  *  NSSCKFWInstance_GetArena
    25  *  NSSCKFWInstance_MayCreatePthreads
    26  *  NSSCKFWInstance_CreateMutex
    27  *  NSSCKFWInstance_GetConfigurationData
    28  *  NSSCKFWInstance_GetInitArgs
    29  *
    30  *  -- implement public accessors --
    31  *  nssCKFWInstance_GetMDInstance
    32  *  nssCKFWInstance_GetArena
    33  *  nssCKFWInstance_MayCreatePthreads
    34  *  nssCKFWInstance_CreateMutex
    35  *  nssCKFWInstance_GetConfigurationData
    36  *  nssCKFWInstance_GetInitArgs 
    37  *
    38  *  -- private accessors --
    39  *  nssCKFWInstance_CreateSessionHandle
    40  *  nssCKFWInstance_ResolveSessionHandle
    41  *  nssCKFWInstance_DestroySessionHandle
    42  *  nssCKFWInstance_FindSessionHandle
    43  *  nssCKFWInstance_CreateObjectHandle
    44  *  nssCKFWInstance_ResolveObjectHandle
    45  *  nssCKFWInstance_DestroyObjectHandle
    46  *
    47  *  -- module fronts --
    48  *  nssCKFWInstance_GetNSlots
    49  *  nssCKFWInstance_GetCryptokiVersion
    50  *  nssCKFWInstance_GetManufacturerID
    51  *  nssCKFWInstance_GetFlags
    52  *  nssCKFWInstance_GetLibraryDescription
    53  *  nssCKFWInstance_GetLibraryVersion
    54  *  nssCKFWInstance_GetModuleHandlesSessionObjects
    55  *  nssCKFWInstance_GetSlots
    56  *  nssCKFWInstance_WaitForSlotEvent
    57  *
    58  *  -- debugging versions only --
    59  *  nssCKFWInstance_verifyPointer
    60  */
    62 struct NSSCKFWInstanceStr {
    63   NSSCKFWMutex *mutex;
    64   NSSArena *arena;
    65   NSSCKMDInstance *mdInstance;
    66   CK_C_INITIALIZE_ARGS_PTR pInitArgs;
    67   CK_C_INITIALIZE_ARGS initArgs;
    68   CryptokiLockingState LockingState;
    69   CK_BBOOL mayCreatePthreads;
    70   NSSUTF8 *configurationData;
    71   CK_ULONG nSlots;
    72   NSSCKFWSlot **fwSlotList;
    73   NSSCKMDSlot **mdSlotList;
    74   CK_BBOOL moduleHandlesSessionObjects;
    76   /*
    77    * Everything above is set at creation time, and then not modified.
    78    * The invariants the mutex protects are:
    79    *
    80    *  1) Each of the cached descriptions (versions, etc.) are in an
    81    *     internally consistant state.
    82    *
    83    *  2) The session handle hashes and count are consistant
    84    *
    85    *  3) The object handle hashes and count are consistant.
    86    *
    87    * I could use multiple locks, but let's wait to see if that's 
    88    * really necessary.
    89    *
    90    * Note that the calls accessing the cached descriptions will 
    91    * call the NSSCKMDInstance methods with the mutex locked.  Those
    92    * methods may then call the public NSSCKFWInstance routines.
    93    * Those public routines only access the constant data above, so
    94    * there's no problem.  But be careful if you add to this object;
    95    * mutexes are in general not reentrant, so don't create deadlock
    96    * situations.
    97    */
    99   CK_VERSION cryptokiVersion;
   100   NSSUTF8 *manufacturerID;
   101   NSSUTF8 *libraryDescription;
   102   CK_VERSION libraryVersion;
   104   CK_ULONG lastSessionHandle;
   105   nssCKFWHash *sessionHandleHash;
   107   CK_ULONG lastObjectHandle;
   108   nssCKFWHash *objectHandleHash;
   109 };
   111 #ifdef DEBUG
   112 /*
   113  * But first, the pointer-tracking stuff.
   114  *
   115  * NOTE: the pointer-tracking support in NSS/base currently relies
   116  * upon NSPR's CallOnce support.  That, however, relies upon NSPR's
   117  * locking, which is tied into the runtime.  We need a pointer-tracker
   118  * implementation that uses the locks supplied through C_Initialize.
   119  * That support, however, can be filled in later.  So for now, I'll
   120  * just do this routines as no-ops.
   121  */
   123 static CK_RV
   124 instance_add_pointer
   125 (
   126   const NSSCKFWInstance *fwInstance
   127 )
   128 {
   129   return CKR_OK;
   130 }
   132 static CK_RV
   133 instance_remove_pointer
   134 (
   135   const NSSCKFWInstance *fwInstance
   136 )
   137 {
   138   return CKR_OK;
   139 }
   141 NSS_IMPLEMENT CK_RV
   142 nssCKFWInstance_verifyPointer
   143 (
   144   const NSSCKFWInstance *fwInstance
   145 )
   146 {
   147   return CKR_OK;
   148 }
   150 #endif /* DEBUG */
   152 /*
   153  * nssCKFWInstance_Create
   154  *
   155  */
   156 NSS_IMPLEMENT NSSCKFWInstance *
   157 nssCKFWInstance_Create
   158 (
   159   CK_C_INITIALIZE_ARGS_PTR pInitArgs,
   160   CryptokiLockingState LockingState,
   161   NSSCKMDInstance *mdInstance,
   162   CK_RV *pError
   163 )
   164 {
   165   NSSCKFWInstance *fwInstance;
   166   NSSArena *arena = (NSSArena *)NULL;
   167   CK_ULONG i;
   168   CK_BBOOL called_Initialize = CK_FALSE;
   170 #ifdef NSSDEBUG
   171   if( (CK_RV)NULL == pError ) {
   172     return (NSSCKFWInstance *)NULL;
   173   }
   175   if (!mdInstance) {
   176     *pError = CKR_ARGUMENTS_BAD;
   177     return (NSSCKFWInstance *)NULL;
   178   }
   179 #endif /* NSSDEBUG */
   181   arena = NSSArena_Create();
   182   if (!arena) {
   183     *pError = CKR_HOST_MEMORY;
   184     return (NSSCKFWInstance *)NULL;
   185   }
   187   fwInstance = nss_ZNEW(arena, NSSCKFWInstance);
   188   if (!fwInstance) {
   189     goto nomem;
   190   }
   192   fwInstance->arena = arena;
   193   fwInstance->mdInstance = mdInstance;
   195   fwInstance->LockingState = LockingState;
   196   if( (CK_C_INITIALIZE_ARGS_PTR)NULL != pInitArgs ) {
   197     fwInstance->initArgs = *pInitArgs;
   198     fwInstance->pInitArgs = &fwInstance->initArgs;
   199     if( pInitArgs->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS ) {
   200       fwInstance->mayCreatePthreads = CK_FALSE;
   201     } else {
   202       fwInstance->mayCreatePthreads = CK_TRUE;
   203     }
   204     fwInstance->configurationData = (NSSUTF8 *)(pInitArgs->pReserved);
   205   } else {
   206     fwInstance->mayCreatePthreads = CK_TRUE;
   207   }
   209   fwInstance->mutex = nssCKFWMutex_Create(pInitArgs, LockingState, arena,
   210                                           pError);
   211   if (!fwInstance->mutex) {
   212     if( CKR_OK == *pError ) {
   213       *pError = CKR_GENERAL_ERROR;
   214     }
   215     goto loser;
   216   }
   218   if (mdInstance->Initialize) {
   219     *pError = mdInstance->Initialize(mdInstance, fwInstance, fwInstance->configurationData);
   220     if( CKR_OK != *pError ) {
   221       goto loser;
   222     }
   224     called_Initialize = CK_TRUE;
   225   }
   227   if (mdInstance->ModuleHandlesSessionObjects) {
   228     fwInstance->moduleHandlesSessionObjects = 
   229       mdInstance->ModuleHandlesSessionObjects(mdInstance, fwInstance);
   230   } else {
   231     fwInstance->moduleHandlesSessionObjects = CK_FALSE;
   232   }
   234   if (!mdInstance->GetNSlots) {
   235     /* That routine is required */
   236     *pError = CKR_GENERAL_ERROR;
   237     goto loser;
   238   }
   240   fwInstance->nSlots = mdInstance->GetNSlots(mdInstance, fwInstance, pError);
   241   if( (CK_ULONG)0 == fwInstance->nSlots ) {
   242     if( CKR_OK == *pError ) {
   243       /* Zero is not a legitimate answer */
   244       *pError = CKR_GENERAL_ERROR;
   245     }
   246     goto loser;
   247   }
   249   fwInstance->fwSlotList = nss_ZNEWARRAY(arena, NSSCKFWSlot *, fwInstance->nSlots);
   250   if( (NSSCKFWSlot **)NULL == fwInstance->fwSlotList ) {
   251     goto nomem;
   252   }
   254   fwInstance->mdSlotList = nss_ZNEWARRAY(arena, NSSCKMDSlot *, fwInstance->nSlots);
   255   if( (NSSCKMDSlot **)NULL == fwInstance->mdSlotList ) {
   256     goto nomem;
   257   }
   259   fwInstance->sessionHandleHash = nssCKFWHash_Create(fwInstance, 
   260     fwInstance->arena, pError);
   261   if (!fwInstance->sessionHandleHash) {
   262     goto loser;
   263   }
   265   fwInstance->objectHandleHash = nssCKFWHash_Create(fwInstance,
   266     fwInstance->arena, pError);
   267   if (!fwInstance->objectHandleHash) {
   268     goto loser;
   269   }
   271   if (!mdInstance->GetSlots) {
   272     /* That routine is required */
   273     *pError = CKR_GENERAL_ERROR;
   274     goto loser;
   275   }
   277   *pError = mdInstance->GetSlots(mdInstance, fwInstance, fwInstance->mdSlotList);
   278   if( CKR_OK != *pError ) {
   279     goto loser;
   280   }
   282   for( i = 0; i < fwInstance->nSlots; i++ ) {
   283     NSSCKMDSlot *mdSlot = fwInstance->mdSlotList[i];
   285     if (!mdSlot) {
   286       *pError = CKR_GENERAL_ERROR;
   287       goto loser;
   288     }
   290     fwInstance->fwSlotList[i] = nssCKFWSlot_Create(fwInstance, mdSlot, i, pError);
   291     if( CKR_OK != *pError ) {
   292       CK_ULONG j;
   294       for( j = 0; j < i; j++ ) {
   295         (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[j]);
   296       }
   298       for( j = i; j < fwInstance->nSlots; j++ ) {
   299         NSSCKMDSlot *mds = fwInstance->mdSlotList[j];
   300         if (mds->Destroy) {
   301           mds->Destroy(mds, (NSSCKFWSlot *)NULL, mdInstance, fwInstance);
   302         }
   303       }
   305       goto loser;
   306     }
   307   }
   309 #ifdef DEBUG
   310   *pError = instance_add_pointer(fwInstance);
   311   if( CKR_OK != *pError ) {
   312     for( i = 0; i < fwInstance->nSlots; i++ ) {
   313       (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[i]);
   314     }
   316     goto loser;
   317   }
   318 #endif /* DEBUG */
   320   *pError = CKR_OK;
   321   return fwInstance;
   323  nomem:
   324   *pError = CKR_HOST_MEMORY;
   325   /*FALLTHROUGH*/
   326  loser:
   328   if( CK_TRUE == called_Initialize ) {
   329     if (mdInstance->Finalize) {
   330       mdInstance->Finalize(mdInstance, fwInstance);
   331     }
   332   }
   334   if (fwInstance && fwInstance->mutex) {
   335     nssCKFWMutex_Destroy(fwInstance->mutex);
   336   }
   338   if (arena) {
   339     (void)NSSArena_Destroy(arena);
   340   }
   341   return (NSSCKFWInstance *)NULL;
   342 }
   344 /*
   345  * nssCKFWInstance_Destroy
   346  *
   347  */
   348 NSS_IMPLEMENT CK_RV
   349 nssCKFWInstance_Destroy
   350 (
   351   NSSCKFWInstance *fwInstance
   352 )
   353 {
   354 #ifdef NSSDEBUG
   355   CK_RV error = CKR_OK;
   356 #endif /* NSSDEBUG */
   357   CK_ULONG i;
   359 #ifdef NSSDEBUG
   360   error = nssCKFWInstance_verifyPointer(fwInstance);
   361   if( CKR_OK != error ) {
   362     return error;
   363   }
   364 #endif /* NSSDEBUG */
   366   nssCKFWMutex_Destroy(fwInstance->mutex);
   368   for( i = 0; i < fwInstance->nSlots; i++ ) {
   369     (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[i]);
   370   }
   372   if (fwInstance->mdInstance->Finalize) {
   373     fwInstance->mdInstance->Finalize(fwInstance->mdInstance, fwInstance);
   374   }
   376   if (fwInstance->sessionHandleHash) {
   377      nssCKFWHash_Destroy(fwInstance->sessionHandleHash);
   378   }
   380   if (fwInstance->objectHandleHash) {
   381      nssCKFWHash_Destroy(fwInstance->objectHandleHash);
   382   }
   384 #ifdef DEBUG
   385   (void)instance_remove_pointer(fwInstance);
   386 #endif /* DEBUG */
   388   (void)NSSArena_Destroy(fwInstance->arena);
   389   return CKR_OK;
   390 }
   392 /*
   393  * nssCKFWInstance_GetMDInstance
   394  *
   395  */
   396 NSS_IMPLEMENT NSSCKMDInstance *
   397 nssCKFWInstance_GetMDInstance
   398 (
   399   NSSCKFWInstance *fwInstance
   400 )
   401 {
   402 #ifdef NSSDEBUG
   403   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   404     return (NSSCKMDInstance *)NULL;
   405   }
   406 #endif /* NSSDEBUG */
   408   return fwInstance->mdInstance;
   409 }
   411 /*
   412  * nssCKFWInstance_GetArena
   413  *
   414  */
   415 NSS_IMPLEMENT NSSArena *
   416 nssCKFWInstance_GetArena
   417 (
   418   NSSCKFWInstance *fwInstance,
   419   CK_RV *pError
   420 )
   421 {
   422 #ifdef NSSDEBUG
   423   if (!pError) {
   424     return (NSSArena *)NULL;
   425   }
   427   *pError = nssCKFWInstance_verifyPointer(fwInstance);
   428   if( CKR_OK != *pError ) {
   429     return (NSSArena *)NULL;
   430   }
   431 #endif /* NSSDEBUG */
   433   *pError = CKR_OK;
   434   return fwInstance->arena;
   435 }
   437 /*
   438  * nssCKFWInstance_MayCreatePthreads
   439  *
   440  */
   441 NSS_IMPLEMENT CK_BBOOL
   442 nssCKFWInstance_MayCreatePthreads
   443 (
   444   NSSCKFWInstance *fwInstance
   445 )
   446 {
   447 #ifdef NSSDEBUG
   448   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   449     return CK_FALSE;
   450   }
   451 #endif /* NSSDEBUG */
   453   return fwInstance->mayCreatePthreads;
   454 }
   456 /*
   457  * nssCKFWInstance_CreateMutex
   458  *
   459  */
   460 NSS_IMPLEMENT NSSCKFWMutex *
   461 nssCKFWInstance_CreateMutex
   462 (
   463   NSSCKFWInstance *fwInstance,
   464   NSSArena *arena,
   465   CK_RV *pError
   466 )
   467 {
   468   NSSCKFWMutex *mutex;
   470 #ifdef NSSDEBUG
   471   if (!pError) {
   472     return (NSSCKFWMutex *)NULL;
   473   }
   475   *pError = nssCKFWInstance_verifyPointer(fwInstance);
   476   if( CKR_OK != *pError ) {
   477     return (NSSCKFWMutex *)NULL;
   478   }
   479 #endif /* NSSDEBUG */
   481   mutex = nssCKFWMutex_Create(fwInstance->pInitArgs, fwInstance->LockingState,
   482                               arena, pError);
   483   if (!mutex) {
   484     if( CKR_OK == *pError ) {
   485       *pError = CKR_GENERAL_ERROR;
   486     }
   488     return (NSSCKFWMutex *)NULL;
   489   }
   491   return mutex;
   492 }
   494 /*
   495  * nssCKFWInstance_GetConfigurationData
   496  *
   497  */
   498 NSS_IMPLEMENT NSSUTF8 *
   499 nssCKFWInstance_GetConfigurationData
   500 (
   501   NSSCKFWInstance *fwInstance
   502 )
   503 {
   504 #ifdef NSSDEBUG
   505   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   506     return (NSSUTF8 *)NULL;
   507   }
   508 #endif /* NSSDEBUG */
   510   return fwInstance->configurationData;
   511 }
   513 /*
   514  * nssCKFWInstance_GetInitArgs
   515  *
   516  */
   517 CK_C_INITIALIZE_ARGS_PTR
   518 nssCKFWInstance_GetInitArgs
   519 (
   520   NSSCKFWInstance *fwInstance
   521 )
   522 {
   523 #ifdef NSSDEBUG
   524   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   525     return (CK_C_INITIALIZE_ARGS_PTR)NULL;
   526   }
   527 #endif /* NSSDEBUG */
   529     return fwInstance->pInitArgs;
   530 }
   532 /*
   533  * nssCKFWInstance_CreateSessionHandle
   534  *
   535  */
   536 NSS_IMPLEMENT CK_SESSION_HANDLE
   537 nssCKFWInstance_CreateSessionHandle
   538 (
   539   NSSCKFWInstance *fwInstance,
   540   NSSCKFWSession *fwSession,
   541   CK_RV *pError
   542 )
   543 {
   544   CK_SESSION_HANDLE hSession;
   546 #ifdef NSSDEBUG
   547   if (!pError) {
   548     return (CK_SESSION_HANDLE)0;
   549   }
   551   *pError = nssCKFWInstance_verifyPointer(fwInstance);
   552   if( CKR_OK != *pError ) {
   553     return (CK_SESSION_HANDLE)0;
   554   }
   555 #endif /* NSSDEBUG */
   557   *pError = nssCKFWMutex_Lock(fwInstance->mutex);
   558   if( CKR_OK != *pError ) {
   559     return (CK_SESSION_HANDLE)0;
   560   }
   562   hSession = ++(fwInstance->lastSessionHandle);
   564   /* Alan would say I should unlock for this call. */
   566   *pError = nssCKFWSession_SetHandle(fwSession, hSession);
   567   if( CKR_OK != *pError ) {
   568     goto done;
   569   }
   571   *pError = nssCKFWHash_Add(fwInstance->sessionHandleHash, 
   572               (const void *)hSession, (const void *)fwSession);
   573   if( CKR_OK != *pError ) {
   574     hSession = (CK_SESSION_HANDLE)0;
   575     goto done;
   576   }
   578  done:
   579   nssCKFWMutex_Unlock(fwInstance->mutex);
   580   return hSession;
   581 }
   583 /*
   584  * nssCKFWInstance_ResolveSessionHandle
   585  *
   586  */
   587 NSS_IMPLEMENT NSSCKFWSession *
   588 nssCKFWInstance_ResolveSessionHandle
   589 (
   590   NSSCKFWInstance *fwInstance,
   591   CK_SESSION_HANDLE hSession
   592 )
   593 {
   594   NSSCKFWSession *fwSession;
   596 #ifdef NSSDEBUG
   597   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   598     return (NSSCKFWSession *)NULL;
   599   }
   600 #endif /* NSSDEBUG */
   602   if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   603     return (NSSCKFWSession *)NULL;
   604   }
   606   fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
   607                 fwInstance->sessionHandleHash, (const void *)hSession);
   609   /* Assert(hSession == nssCKFWSession_GetHandle(fwSession)) */
   611   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   613   return fwSession;
   614 }
   616 /*
   617  * nssCKFWInstance_DestroySessionHandle
   618  *
   619  */
   620 NSS_IMPLEMENT void
   621 nssCKFWInstance_DestroySessionHandle
   622 (
   623   NSSCKFWInstance *fwInstance,
   624   CK_SESSION_HANDLE hSession
   625 )
   626 {
   627   NSSCKFWSession *fwSession;
   629 #ifdef NSSDEBUG
   630   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   631     return;
   632   }
   633 #endif /* NSSDEBUG */
   635   if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   636     return;
   637   }
   639   fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
   640                 fwInstance->sessionHandleHash, (const void *)hSession);
   641   if (fwSession) {
   642     nssCKFWHash_Remove(fwInstance->sessionHandleHash, (const void *)hSession);
   643     nssCKFWSession_SetHandle(fwSession, (CK_SESSION_HANDLE)0);
   644   }
   646   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   648   return;
   649 }
   651 /*
   652  * nssCKFWInstance_FindSessionHandle
   653  *
   654  */
   655 NSS_IMPLEMENT CK_SESSION_HANDLE
   656 nssCKFWInstance_FindSessionHandle
   657 (
   658   NSSCKFWInstance *fwInstance,
   659   NSSCKFWSession *fwSession
   660 )
   661 {
   662 #ifdef NSSDEBUG
   663   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   664     return (CK_SESSION_HANDLE)0;
   665   }
   667   if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {
   668     return (CK_SESSION_HANDLE)0;
   669   }
   670 #endif /* NSSDEBUG */
   672   return nssCKFWSession_GetHandle(fwSession);
   673   /* look it up and assert? */
   674 }
   676 /*
   677  * nssCKFWInstance_CreateObjectHandle
   678  *
   679  */
   680 NSS_IMPLEMENT CK_OBJECT_HANDLE
   681 nssCKFWInstance_CreateObjectHandle
   682 (
   683   NSSCKFWInstance *fwInstance,
   684   NSSCKFWObject *fwObject,
   685   CK_RV *pError
   686 )
   687 {
   688   CK_OBJECT_HANDLE hObject;
   690 #ifdef NSSDEBUG
   691   if (!pError) {
   692     return (CK_OBJECT_HANDLE)0;
   693   }
   695   *pError = nssCKFWInstance_verifyPointer(fwInstance);
   696   if( CKR_OK != *pError ) {
   697     return (CK_OBJECT_HANDLE)0;
   698   }
   699 #endif /* NSSDEBUG */
   701   *pError = nssCKFWMutex_Lock(fwInstance->mutex);
   702   if( CKR_OK != *pError ) {
   703     return (CK_OBJECT_HANDLE)0;
   704   }
   706   hObject = ++(fwInstance->lastObjectHandle);
   708   *pError = nssCKFWObject_SetHandle(fwObject, hObject);
   709   if( CKR_OK != *pError ) {
   710     hObject = (CK_OBJECT_HANDLE)0;
   711     goto done;
   712   }
   714   *pError = nssCKFWHash_Add(fwInstance->objectHandleHash, 
   715               (const void *)hObject, (const void *)fwObject);
   716   if( CKR_OK != *pError ) {
   717     hObject = (CK_OBJECT_HANDLE)0;
   718     goto done;
   719   }
   721  done:
   722   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   723   return hObject;
   724 }
   726 /*
   727  * nssCKFWInstance_ResolveObjectHandle
   728  *
   729  */
   730 NSS_IMPLEMENT NSSCKFWObject *
   731 nssCKFWInstance_ResolveObjectHandle
   732 (
   733   NSSCKFWInstance *fwInstance,
   734   CK_OBJECT_HANDLE hObject
   735 )
   736 {
   737   NSSCKFWObject *fwObject;
   739 #ifdef NSSDEBUG
   740   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   741     return (NSSCKFWObject *)NULL;
   742   }
   743 #endif /* NSSDEBUG */
   745   if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   746     return (NSSCKFWObject *)NULL;
   747   }
   749   fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
   750                 fwInstance->objectHandleHash, (const void *)hObject);
   752   /* Assert(hObject == nssCKFWObject_GetHandle(fwObject)) */
   754   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   755   return fwObject;
   756 }
   758 /*
   759  * nssCKFWInstance_ReassignObjectHandle
   760  *
   761  */
   762 NSS_IMPLEMENT CK_RV
   763 nssCKFWInstance_ReassignObjectHandle
   764 (
   765   NSSCKFWInstance *fwInstance,
   766   CK_OBJECT_HANDLE hObject,
   767   NSSCKFWObject *fwObject
   768 )
   769 {
   770   CK_RV error = CKR_OK;
   771   NSSCKFWObject *oldObject;
   773 #ifdef NSSDEBUG
   774   error = nssCKFWInstance_verifyPointer(fwInstance);
   775   if( CKR_OK != error ) {
   776     return error;
   777   }
   778 #endif /* NSSDEBUG */
   780   error = nssCKFWMutex_Lock(fwInstance->mutex);
   781   if( CKR_OK != error ) {
   782     return error;
   783   }
   785   oldObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
   786                  fwInstance->objectHandleHash, (const void *)hObject);
   787   if(oldObject) {
   788     /* Assert(hObject == nssCKFWObject_GetHandle(oldObject) */
   789     (void)nssCKFWObject_SetHandle(oldObject, (CK_SESSION_HANDLE)0);
   790     nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)hObject);
   791   }
   793   error = nssCKFWObject_SetHandle(fwObject, hObject);
   794   if( CKR_OK != error ) {
   795     goto done;
   796   }
   797   error = nssCKFWHash_Add(fwInstance->objectHandleHash, 
   798             (const void *)hObject, (const void *)fwObject);
   800  done:
   801   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   802   return error;
   803 }
   805 /*
   806  * nssCKFWInstance_DestroyObjectHandle
   807  *
   808  */
   809 NSS_IMPLEMENT void
   810 nssCKFWInstance_DestroyObjectHandle
   811 (
   812   NSSCKFWInstance *fwInstance,
   813   CK_OBJECT_HANDLE hObject
   814 )
   815 {
   816   NSSCKFWObject *fwObject;
   818 #ifdef NSSDEBUG
   819   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   820     return;
   821   }
   822 #endif /* NSSDEBUG */
   824   if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   825     return;
   826   }
   828   fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
   829                 fwInstance->objectHandleHash, (const void *)hObject);
   830   if (fwObject) {
   831     /* Assert(hObject = nssCKFWObject_GetHandle(fwObject)) */
   832     nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)hObject);
   833     (void)nssCKFWObject_SetHandle(fwObject, (CK_SESSION_HANDLE)0);
   834   }
   836   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   837   return;
   838 }
   840 /*
   841  * nssCKFWInstance_FindObjectHandle
   842  *
   843  */
   844 NSS_IMPLEMENT CK_OBJECT_HANDLE
   845 nssCKFWInstance_FindObjectHandle
   846 (
   847   NSSCKFWInstance *fwInstance,
   848   NSSCKFWObject *fwObject
   849 )
   850 {
   851 #ifdef NSSDEBUG
   852   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   853     return (CK_OBJECT_HANDLE)0;
   854   }
   856   if( CKR_OK != nssCKFWObject_verifyPointer(fwObject) ) {
   857     return (CK_OBJECT_HANDLE)0;
   858   }
   859 #endif /* NSSDEBUG */
   861   return nssCKFWObject_GetHandle(fwObject);
   862 }
   864 /*
   865  * nssCKFWInstance_GetNSlots
   866  *
   867  */
   868 NSS_IMPLEMENT CK_ULONG
   869 nssCKFWInstance_GetNSlots
   870 (
   871   NSSCKFWInstance *fwInstance,
   872   CK_RV *pError
   873 )
   874 {
   875 #ifdef NSSDEBUG
   876   if (!pError) {
   877     return (CK_ULONG)0;
   878   }
   880   *pError = nssCKFWInstance_verifyPointer(fwInstance);
   881   if( CKR_OK != *pError ) {
   882     return (CK_ULONG)0;
   883   }
   884 #endif /* NSSDEBUG */
   886   *pError = CKR_OK;
   887   return fwInstance->nSlots;
   888 }  
   890 /*
   891  * nssCKFWInstance_GetCryptokiVersion
   892  *
   893  */
   894 NSS_IMPLEMENT CK_VERSION
   895 nssCKFWInstance_GetCryptokiVersion
   896 (
   897   NSSCKFWInstance *fwInstance
   898 )
   899 {
   900   CK_VERSION rv;
   902 #ifdef NSSDEBUG
   903   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   904     rv.major = rv.minor = 0;
   905     return rv;
   906   }
   907 #endif /* NSSDEBUG */
   909   if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   910     rv.major = rv.minor = 0;
   911     return rv;
   912   }
   914   if( (0 != fwInstance->cryptokiVersion.major) ||
   915       (0 != fwInstance->cryptokiVersion.minor) ) {
   916     rv = fwInstance->cryptokiVersion;
   917     goto done;
   918   }
   920   if (fwInstance->mdInstance->GetCryptokiVersion) {
   921     fwInstance->cryptokiVersion = fwInstance->mdInstance->GetCryptokiVersion(
   922       fwInstance->mdInstance, fwInstance);
   923   } else {
   924     fwInstance->cryptokiVersion.major = 2;
   925     fwInstance->cryptokiVersion.minor = 1;
   926   }
   928   rv = fwInstance->cryptokiVersion;
   930  done:
   931   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   932   return rv;
   933 }
   935 /*
   936  * nssCKFWInstance_GetManufacturerID
   937  *
   938  */
   939 NSS_IMPLEMENT CK_RV
   940 nssCKFWInstance_GetManufacturerID
   941 (
   942   NSSCKFWInstance *fwInstance,
   943   CK_CHAR manufacturerID[32]
   944 )
   945 {
   946   CK_RV error = CKR_OK;
   948 #ifdef NSSDEBUG
   949   if( (CK_CHAR_PTR)NULL == manufacturerID ) {
   950     return CKR_ARGUMENTS_BAD;
   951   }
   953   error = nssCKFWInstance_verifyPointer(fwInstance);
   954   if( CKR_OK != error ) {
   955     return error;
   956   }
   957 #endif /* NSSDEBUG */
   959   error = nssCKFWMutex_Lock(fwInstance->mutex);
   960   if( CKR_OK != error ) {
   961     return error;
   962   }
   964   if (!fwInstance->manufacturerID) {
   965     if (fwInstance->mdInstance->GetManufacturerID) {
   966       fwInstance->manufacturerID = fwInstance->mdInstance->GetManufacturerID(
   967         fwInstance->mdInstance, fwInstance, &error);
   968       if ((!fwInstance->manufacturerID) && (CKR_OK != error)) {
   969         goto done;
   970       }
   971     } else {
   972       fwInstance->manufacturerID = (NSSUTF8 *) "";
   973     }
   974   }
   976   (void)nssUTF8_CopyIntoFixedBuffer(fwInstance->manufacturerID, (char *)manufacturerID, 32, ' ');
   977   error = CKR_OK;
   979  done:
   980   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   981   return error;
   982 }
   984 /*
   985  * nssCKFWInstance_GetFlags
   986  *
   987  */
   988 NSS_IMPLEMENT CK_ULONG
   989 nssCKFWInstance_GetFlags
   990 (
   991   NSSCKFWInstance *fwInstance
   992 )
   993 {
   994 #ifdef NSSDEBUG
   995   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   996     return (CK_ULONG)0;
   997   }
   998 #endif /* NSSDEBUG */
  1000   /* No "instance flags" are yet defined by Cryptoki. */
  1001   return (CK_ULONG)0;
  1004 /*
  1005  * nssCKFWInstance_GetLibraryDescription
  1007  */
  1008 NSS_IMPLEMENT CK_RV
  1009 nssCKFWInstance_GetLibraryDescription
  1011   NSSCKFWInstance *fwInstance,
  1012   CK_CHAR libraryDescription[32]
  1015   CK_RV error = CKR_OK;
  1017 #ifdef NSSDEBUG
  1018   if( (CK_CHAR_PTR)NULL == libraryDescription ) {
  1019     return CKR_ARGUMENTS_BAD;
  1022   error = nssCKFWInstance_verifyPointer(fwInstance);
  1023   if( CKR_OK != error ) {
  1024     return error;
  1026 #endif /* NSSDEBUG */
  1028   error = nssCKFWMutex_Lock(fwInstance->mutex);
  1029   if( CKR_OK != error ) {
  1030     return error;
  1033   if (!fwInstance->libraryDescription) {
  1034     if (fwInstance->mdInstance->GetLibraryDescription) {
  1035       fwInstance->libraryDescription = fwInstance->mdInstance->GetLibraryDescription(
  1036         fwInstance->mdInstance, fwInstance, &error);
  1037       if ((!fwInstance->libraryDescription) && (CKR_OK != error)) {
  1038         goto done;
  1040     } else {
  1041       fwInstance->libraryDescription = (NSSUTF8 *) "";
  1045   (void)nssUTF8_CopyIntoFixedBuffer(fwInstance->libraryDescription, (char *)libraryDescription, 32, ' ');
  1046   error = CKR_OK;
  1048  done:
  1049   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
  1050   return error;
  1053 /*
  1054  * nssCKFWInstance_GetLibraryVersion
  1056  */
  1057 NSS_IMPLEMENT CK_VERSION
  1058 nssCKFWInstance_GetLibraryVersion
  1060   NSSCKFWInstance *fwInstance
  1063   CK_VERSION rv;
  1065 #ifdef NSSDEBUG
  1066   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1067     rv.major = rv.minor = 0;
  1068     return rv;
  1070 #endif /* NSSDEBUG */
  1072   if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
  1073     rv.major = rv.minor = 0;
  1074     return rv;
  1077   if( (0 != fwInstance->libraryVersion.major) ||
  1078       (0 != fwInstance->libraryVersion.minor) ) {
  1079     rv = fwInstance->libraryVersion;
  1080     goto done;
  1083   if (fwInstance->mdInstance->GetLibraryVersion) {
  1084     fwInstance->libraryVersion = fwInstance->mdInstance->GetLibraryVersion(
  1085       fwInstance->mdInstance, fwInstance);
  1086   } else {
  1087     fwInstance->libraryVersion.major = 0;
  1088     fwInstance->libraryVersion.minor = 3;
  1091   rv = fwInstance->libraryVersion;
  1092  done:
  1093   (void)nssCKFWMutex_Unlock(fwInstance->mutex);
  1094   return rv;
  1097 /*
  1098  * nssCKFWInstance_GetModuleHandlesSessionObjects
  1100  */
  1101 NSS_IMPLEMENT CK_BBOOL
  1102 nssCKFWInstance_GetModuleHandlesSessionObjects
  1104   NSSCKFWInstance *fwInstance
  1107 #ifdef NSSDEBUG
  1108   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1109     return CK_FALSE;
  1111 #endif /* NSSDEBUG */
  1113   return fwInstance->moduleHandlesSessionObjects;
  1116 /*
  1117  * nssCKFWInstance_GetSlots
  1119  */
  1120 NSS_IMPLEMENT NSSCKFWSlot **
  1121 nssCKFWInstance_GetSlots
  1123   NSSCKFWInstance *fwInstance,
  1124   CK_RV *pError
  1127 #ifdef NSSDEBUG
  1128   if (!pError) {
  1129     return (NSSCKFWSlot **)NULL;
  1132   *pError = nssCKFWInstance_verifyPointer(fwInstance);
  1133   if( CKR_OK != *pError ) {
  1134     return (NSSCKFWSlot **)NULL;
  1136 #endif /* NSSDEBUG */
  1138   return fwInstance->fwSlotList;
  1141 /*
  1142  * nssCKFWInstance_WaitForSlotEvent
  1144  */
  1145 NSS_IMPLEMENT NSSCKFWSlot *
  1146 nssCKFWInstance_WaitForSlotEvent
  1148   NSSCKFWInstance *fwInstance,
  1149   CK_BBOOL block,
  1150   CK_RV *pError
  1153   NSSCKFWSlot *fwSlot = (NSSCKFWSlot *)NULL;
  1154   NSSCKMDSlot *mdSlot;
  1155   CK_ULONG i, n;
  1157 #ifdef NSSDEBUG
  1158   if (!pError) {
  1159     return (NSSCKFWSlot *)NULL;
  1162   *pError = nssCKFWInstance_verifyPointer(fwInstance);
  1163   if( CKR_OK != *pError ) {
  1164     return (NSSCKFWSlot *)NULL;
  1167   switch( block ) {
  1168   case CK_TRUE:
  1169   case CK_FALSE:
  1170     break;
  1171   default:
  1172     *pError = CKR_ARGUMENTS_BAD;
  1173     return (NSSCKFWSlot *)NULL;
  1175 #endif /* NSSDEBUG */
  1177   if (!fwInstance->mdInstance->WaitForSlotEvent) {
  1178     *pError = CKR_NO_EVENT;
  1179     return (NSSCKFWSlot *)NULL;
  1182   mdSlot = fwInstance->mdInstance->WaitForSlotEvent(
  1183     fwInstance->mdInstance,
  1184     fwInstance,
  1185     block,
  1186     pError
  1187   );
  1189   if (!mdSlot) {
  1190     return (NSSCKFWSlot *)NULL;
  1193   n = nssCKFWInstance_GetNSlots(fwInstance, pError);
  1194   if( ((CK_ULONG)0 == n) && (CKR_OK != *pError) ) {
  1195     return (NSSCKFWSlot *)NULL;
  1198   for( i = 0; i < n; i++ ) {
  1199     if( fwInstance->mdSlotList[i] == mdSlot ) {
  1200       fwSlot = fwInstance->fwSlotList[i];
  1201       break;
  1205   if (!fwSlot) {
  1206     /* Internal error */
  1207     *pError = CKR_GENERAL_ERROR;
  1208     return (NSSCKFWSlot *)NULL;
  1211   return fwSlot;
  1214 /*
  1215  * NSSCKFWInstance_GetMDInstance
  1217  */
  1218 NSS_IMPLEMENT NSSCKMDInstance *
  1219 NSSCKFWInstance_GetMDInstance
  1221   NSSCKFWInstance *fwInstance
  1224 #ifdef DEBUG
  1225   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1226     return (NSSCKMDInstance *)NULL;
  1228 #endif /* DEBUG */
  1230   return nssCKFWInstance_GetMDInstance(fwInstance);
  1233 /*
  1234  * NSSCKFWInstance_GetArena
  1236  */
  1237 NSS_IMPLEMENT NSSArena *
  1238 NSSCKFWInstance_GetArena
  1240   NSSCKFWInstance *fwInstance,
  1241   CK_RV *pError
  1244 #ifdef DEBUG
  1245   if (!pError) {
  1246     return (NSSArena *)NULL;
  1249   *pError = nssCKFWInstance_verifyPointer(fwInstance);
  1250   if( CKR_OK != *pError ) {
  1251     return (NSSArena *)NULL;
  1253 #endif /* DEBUG */
  1255   return nssCKFWInstance_GetArena(fwInstance, pError);
  1258 /*
  1259  * NSSCKFWInstance_MayCreatePthreads
  1261  */
  1262 NSS_IMPLEMENT CK_BBOOL
  1263 NSSCKFWInstance_MayCreatePthreads
  1265   NSSCKFWInstance *fwInstance
  1268 #ifdef DEBUG
  1269   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1270     return CK_FALSE;
  1272 #endif /* DEBUG */
  1274   return nssCKFWInstance_MayCreatePthreads(fwInstance);
  1277 /*
  1278  * NSSCKFWInstance_CreateMutex
  1280  */
  1281 NSS_IMPLEMENT NSSCKFWMutex *
  1282 NSSCKFWInstance_CreateMutex
  1284   NSSCKFWInstance *fwInstance,
  1285   NSSArena *arena,
  1286   CK_RV *pError
  1289 #ifdef DEBUG
  1290   if (!pError) {
  1291     return (NSSCKFWMutex *)NULL;
  1294   *pError = nssCKFWInstance_verifyPointer(fwInstance);
  1295   if( CKR_OK != *pError ) {
  1296     return (NSSCKFWMutex *)NULL;
  1298 #endif /* DEBUG */
  1300   return nssCKFWInstance_CreateMutex(fwInstance, arena, pError);
  1303 /*
  1304  * NSSCKFWInstance_GetConfigurationData
  1306  */
  1307 NSS_IMPLEMENT NSSUTF8 *
  1308 NSSCKFWInstance_GetConfigurationData
  1310   NSSCKFWInstance *fwInstance
  1313 #ifdef DEBUG
  1314   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1315     return (NSSUTF8 *)NULL;
  1317 #endif /* DEBUG */
  1319   return nssCKFWInstance_GetConfigurationData(fwInstance);
  1322 /*
  1323  * NSSCKFWInstance_GetInitArgs
  1325  */
  1326 NSS_IMPLEMENT CK_C_INITIALIZE_ARGS_PTR
  1327 NSSCKFWInstance_GetInitArgs
  1329   NSSCKFWInstance *fwInstance
  1332 #ifdef DEBUG
  1333   if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1334     return (CK_C_INITIALIZE_ARGS_PTR)NULL;
  1336 #endif /* DEBUG */
  1338   return nssCKFWInstance_GetInitArgs(fwInstance);

mercurial