security/nss/lib/ckfw/instance.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/ckfw/instance.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1340 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * instance.c
    1.10 + *
    1.11 + * This file implements the NSSCKFWInstance type and methods.
    1.12 + */
    1.13 +
    1.14 +#ifndef CK_T
    1.15 +#include "ck.h"
    1.16 +#endif /* CK_T */
    1.17 +
    1.18 +/*
    1.19 + * NSSCKFWInstance
    1.20 + *
    1.21 + *  -- create/destroy --
    1.22 + *  nssCKFWInstance_Create
    1.23 + *  nssCKFWInstance_Destroy
    1.24 + *
    1.25 + *  -- public accessors --
    1.26 + *  NSSCKFWInstance_GetMDInstance
    1.27 + *  NSSCKFWInstance_GetArena
    1.28 + *  NSSCKFWInstance_MayCreatePthreads
    1.29 + *  NSSCKFWInstance_CreateMutex
    1.30 + *  NSSCKFWInstance_GetConfigurationData
    1.31 + *  NSSCKFWInstance_GetInitArgs
    1.32 + *
    1.33 + *  -- implement public accessors --
    1.34 + *  nssCKFWInstance_GetMDInstance
    1.35 + *  nssCKFWInstance_GetArena
    1.36 + *  nssCKFWInstance_MayCreatePthreads
    1.37 + *  nssCKFWInstance_CreateMutex
    1.38 + *  nssCKFWInstance_GetConfigurationData
    1.39 + *  nssCKFWInstance_GetInitArgs 
    1.40 + *
    1.41 + *  -- private accessors --
    1.42 + *  nssCKFWInstance_CreateSessionHandle
    1.43 + *  nssCKFWInstance_ResolveSessionHandle
    1.44 + *  nssCKFWInstance_DestroySessionHandle
    1.45 + *  nssCKFWInstance_FindSessionHandle
    1.46 + *  nssCKFWInstance_CreateObjectHandle
    1.47 + *  nssCKFWInstance_ResolveObjectHandle
    1.48 + *  nssCKFWInstance_DestroyObjectHandle
    1.49 + *
    1.50 + *  -- module fronts --
    1.51 + *  nssCKFWInstance_GetNSlots
    1.52 + *  nssCKFWInstance_GetCryptokiVersion
    1.53 + *  nssCKFWInstance_GetManufacturerID
    1.54 + *  nssCKFWInstance_GetFlags
    1.55 + *  nssCKFWInstance_GetLibraryDescription
    1.56 + *  nssCKFWInstance_GetLibraryVersion
    1.57 + *  nssCKFWInstance_GetModuleHandlesSessionObjects
    1.58 + *  nssCKFWInstance_GetSlots
    1.59 + *  nssCKFWInstance_WaitForSlotEvent
    1.60 + *
    1.61 + *  -- debugging versions only --
    1.62 + *  nssCKFWInstance_verifyPointer
    1.63 + */
    1.64 +
    1.65 +struct NSSCKFWInstanceStr {
    1.66 +  NSSCKFWMutex *mutex;
    1.67 +  NSSArena *arena;
    1.68 +  NSSCKMDInstance *mdInstance;
    1.69 +  CK_C_INITIALIZE_ARGS_PTR pInitArgs;
    1.70 +  CK_C_INITIALIZE_ARGS initArgs;
    1.71 +  CryptokiLockingState LockingState;
    1.72 +  CK_BBOOL mayCreatePthreads;
    1.73 +  NSSUTF8 *configurationData;
    1.74 +  CK_ULONG nSlots;
    1.75 +  NSSCKFWSlot **fwSlotList;
    1.76 +  NSSCKMDSlot **mdSlotList;
    1.77 +  CK_BBOOL moduleHandlesSessionObjects;
    1.78 +
    1.79 +  /*
    1.80 +   * Everything above is set at creation time, and then not modified.
    1.81 +   * The invariants the mutex protects are:
    1.82 +   *
    1.83 +   *  1) Each of the cached descriptions (versions, etc.) are in an
    1.84 +   *     internally consistant state.
    1.85 +   *
    1.86 +   *  2) The session handle hashes and count are consistant
    1.87 +   *
    1.88 +   *  3) The object handle hashes and count are consistant.
    1.89 +   *
    1.90 +   * I could use multiple locks, but let's wait to see if that's 
    1.91 +   * really necessary.
    1.92 +   *
    1.93 +   * Note that the calls accessing the cached descriptions will 
    1.94 +   * call the NSSCKMDInstance methods with the mutex locked.  Those
    1.95 +   * methods may then call the public NSSCKFWInstance routines.
    1.96 +   * Those public routines only access the constant data above, so
    1.97 +   * there's no problem.  But be careful if you add to this object;
    1.98 +   * mutexes are in general not reentrant, so don't create deadlock
    1.99 +   * situations.
   1.100 +   */
   1.101 +
   1.102 +  CK_VERSION cryptokiVersion;
   1.103 +  NSSUTF8 *manufacturerID;
   1.104 +  NSSUTF8 *libraryDescription;
   1.105 +  CK_VERSION libraryVersion;
   1.106 +
   1.107 +  CK_ULONG lastSessionHandle;
   1.108 +  nssCKFWHash *sessionHandleHash;
   1.109 +
   1.110 +  CK_ULONG lastObjectHandle;
   1.111 +  nssCKFWHash *objectHandleHash;
   1.112 +};
   1.113 +
   1.114 +#ifdef DEBUG
   1.115 +/*
   1.116 + * But first, the pointer-tracking stuff.
   1.117 + *
   1.118 + * NOTE: the pointer-tracking support in NSS/base currently relies
   1.119 + * upon NSPR's CallOnce support.  That, however, relies upon NSPR's
   1.120 + * locking, which is tied into the runtime.  We need a pointer-tracker
   1.121 + * implementation that uses the locks supplied through C_Initialize.
   1.122 + * That support, however, can be filled in later.  So for now, I'll
   1.123 + * just do this routines as no-ops.
   1.124 + */
   1.125 +
   1.126 +static CK_RV
   1.127 +instance_add_pointer
   1.128 +(
   1.129 +  const NSSCKFWInstance *fwInstance
   1.130 +)
   1.131 +{
   1.132 +  return CKR_OK;
   1.133 +}
   1.134 +
   1.135 +static CK_RV
   1.136 +instance_remove_pointer
   1.137 +(
   1.138 +  const NSSCKFWInstance *fwInstance
   1.139 +)
   1.140 +{
   1.141 +  return CKR_OK;
   1.142 +}
   1.143 +
   1.144 +NSS_IMPLEMENT CK_RV
   1.145 +nssCKFWInstance_verifyPointer
   1.146 +(
   1.147 +  const NSSCKFWInstance *fwInstance
   1.148 +)
   1.149 +{
   1.150 +  return CKR_OK;
   1.151 +}
   1.152 +
   1.153 +#endif /* DEBUG */
   1.154 +
   1.155 +/*
   1.156 + * nssCKFWInstance_Create
   1.157 + *
   1.158 + */
   1.159 +NSS_IMPLEMENT NSSCKFWInstance *
   1.160 +nssCKFWInstance_Create
   1.161 +(
   1.162 +  CK_C_INITIALIZE_ARGS_PTR pInitArgs,
   1.163 +  CryptokiLockingState LockingState,
   1.164 +  NSSCKMDInstance *mdInstance,
   1.165 +  CK_RV *pError
   1.166 +)
   1.167 +{
   1.168 +  NSSCKFWInstance *fwInstance;
   1.169 +  NSSArena *arena = (NSSArena *)NULL;
   1.170 +  CK_ULONG i;
   1.171 +  CK_BBOOL called_Initialize = CK_FALSE;
   1.172 +
   1.173 +#ifdef NSSDEBUG
   1.174 +  if( (CK_RV)NULL == pError ) {
   1.175 +    return (NSSCKFWInstance *)NULL;
   1.176 +  }
   1.177 +
   1.178 +  if (!mdInstance) {
   1.179 +    *pError = CKR_ARGUMENTS_BAD;
   1.180 +    return (NSSCKFWInstance *)NULL;
   1.181 +  }
   1.182 +#endif /* NSSDEBUG */
   1.183 +
   1.184 +  arena = NSSArena_Create();
   1.185 +  if (!arena) {
   1.186 +    *pError = CKR_HOST_MEMORY;
   1.187 +    return (NSSCKFWInstance *)NULL;
   1.188 +  }
   1.189 +
   1.190 +  fwInstance = nss_ZNEW(arena, NSSCKFWInstance);
   1.191 +  if (!fwInstance) {
   1.192 +    goto nomem;
   1.193 +  }
   1.194 +
   1.195 +  fwInstance->arena = arena;
   1.196 +  fwInstance->mdInstance = mdInstance;
   1.197 +
   1.198 +  fwInstance->LockingState = LockingState;
   1.199 +  if( (CK_C_INITIALIZE_ARGS_PTR)NULL != pInitArgs ) {
   1.200 +    fwInstance->initArgs = *pInitArgs;
   1.201 +    fwInstance->pInitArgs = &fwInstance->initArgs;
   1.202 +    if( pInitArgs->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS ) {
   1.203 +      fwInstance->mayCreatePthreads = CK_FALSE;
   1.204 +    } else {
   1.205 +      fwInstance->mayCreatePthreads = CK_TRUE;
   1.206 +    }
   1.207 +    fwInstance->configurationData = (NSSUTF8 *)(pInitArgs->pReserved);
   1.208 +  } else {
   1.209 +    fwInstance->mayCreatePthreads = CK_TRUE;
   1.210 +  }
   1.211 +
   1.212 +  fwInstance->mutex = nssCKFWMutex_Create(pInitArgs, LockingState, arena,
   1.213 +                                          pError);
   1.214 +  if (!fwInstance->mutex) {
   1.215 +    if( CKR_OK == *pError ) {
   1.216 +      *pError = CKR_GENERAL_ERROR;
   1.217 +    }
   1.218 +    goto loser;
   1.219 +  }
   1.220 +
   1.221 +  if (mdInstance->Initialize) {
   1.222 +    *pError = mdInstance->Initialize(mdInstance, fwInstance, fwInstance->configurationData);
   1.223 +    if( CKR_OK != *pError ) {
   1.224 +      goto loser;
   1.225 +    }
   1.226 +
   1.227 +    called_Initialize = CK_TRUE;
   1.228 +  }
   1.229 +
   1.230 +  if (mdInstance->ModuleHandlesSessionObjects) {
   1.231 +    fwInstance->moduleHandlesSessionObjects = 
   1.232 +      mdInstance->ModuleHandlesSessionObjects(mdInstance, fwInstance);
   1.233 +  } else {
   1.234 +    fwInstance->moduleHandlesSessionObjects = CK_FALSE;
   1.235 +  }
   1.236 +
   1.237 +  if (!mdInstance->GetNSlots) {
   1.238 +    /* That routine is required */
   1.239 +    *pError = CKR_GENERAL_ERROR;
   1.240 +    goto loser;
   1.241 +  }
   1.242 +
   1.243 +  fwInstance->nSlots = mdInstance->GetNSlots(mdInstance, fwInstance, pError);
   1.244 +  if( (CK_ULONG)0 == fwInstance->nSlots ) {
   1.245 +    if( CKR_OK == *pError ) {
   1.246 +      /* Zero is not a legitimate answer */
   1.247 +      *pError = CKR_GENERAL_ERROR;
   1.248 +    }
   1.249 +    goto loser;
   1.250 +  }
   1.251 +
   1.252 +  fwInstance->fwSlotList = nss_ZNEWARRAY(arena, NSSCKFWSlot *, fwInstance->nSlots);
   1.253 +  if( (NSSCKFWSlot **)NULL == fwInstance->fwSlotList ) {
   1.254 +    goto nomem;
   1.255 +  }
   1.256 +
   1.257 +  fwInstance->mdSlotList = nss_ZNEWARRAY(arena, NSSCKMDSlot *, fwInstance->nSlots);
   1.258 +  if( (NSSCKMDSlot **)NULL == fwInstance->mdSlotList ) {
   1.259 +    goto nomem;
   1.260 +  }
   1.261 +
   1.262 +  fwInstance->sessionHandleHash = nssCKFWHash_Create(fwInstance, 
   1.263 +    fwInstance->arena, pError);
   1.264 +  if (!fwInstance->sessionHandleHash) {
   1.265 +    goto loser;
   1.266 +  }
   1.267 +
   1.268 +  fwInstance->objectHandleHash = nssCKFWHash_Create(fwInstance,
   1.269 +    fwInstance->arena, pError);
   1.270 +  if (!fwInstance->objectHandleHash) {
   1.271 +    goto loser;
   1.272 +  }
   1.273 +
   1.274 +  if (!mdInstance->GetSlots) {
   1.275 +    /* That routine is required */
   1.276 +    *pError = CKR_GENERAL_ERROR;
   1.277 +    goto loser;
   1.278 +  }
   1.279 +
   1.280 +  *pError = mdInstance->GetSlots(mdInstance, fwInstance, fwInstance->mdSlotList);
   1.281 +  if( CKR_OK != *pError ) {
   1.282 +    goto loser;
   1.283 +  }
   1.284 +
   1.285 +  for( i = 0; i < fwInstance->nSlots; i++ ) {
   1.286 +    NSSCKMDSlot *mdSlot = fwInstance->mdSlotList[i];
   1.287 +
   1.288 +    if (!mdSlot) {
   1.289 +      *pError = CKR_GENERAL_ERROR;
   1.290 +      goto loser;
   1.291 +    }
   1.292 +
   1.293 +    fwInstance->fwSlotList[i] = nssCKFWSlot_Create(fwInstance, mdSlot, i, pError);
   1.294 +    if( CKR_OK != *pError ) {
   1.295 +      CK_ULONG j;
   1.296 +
   1.297 +      for( j = 0; j < i; j++ ) {
   1.298 +        (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[j]);
   1.299 +      }
   1.300 +
   1.301 +      for( j = i; j < fwInstance->nSlots; j++ ) {
   1.302 +        NSSCKMDSlot *mds = fwInstance->mdSlotList[j];
   1.303 +        if (mds->Destroy) {
   1.304 +          mds->Destroy(mds, (NSSCKFWSlot *)NULL, mdInstance, fwInstance);
   1.305 +        }
   1.306 +      }
   1.307 +
   1.308 +      goto loser;
   1.309 +    }
   1.310 +  }
   1.311 +
   1.312 +#ifdef DEBUG
   1.313 +  *pError = instance_add_pointer(fwInstance);
   1.314 +  if( CKR_OK != *pError ) {
   1.315 +    for( i = 0; i < fwInstance->nSlots; i++ ) {
   1.316 +      (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[i]);
   1.317 +    }
   1.318 +    
   1.319 +    goto loser;
   1.320 +  }
   1.321 +#endif /* DEBUG */
   1.322 +
   1.323 +  *pError = CKR_OK;
   1.324 +  return fwInstance;
   1.325 +
   1.326 + nomem:
   1.327 +  *pError = CKR_HOST_MEMORY;
   1.328 +  /*FALLTHROUGH*/
   1.329 + loser:
   1.330 +
   1.331 +  if( CK_TRUE == called_Initialize ) {
   1.332 +    if (mdInstance->Finalize) {
   1.333 +      mdInstance->Finalize(mdInstance, fwInstance);
   1.334 +    }
   1.335 +  }
   1.336 +
   1.337 +  if (fwInstance && fwInstance->mutex) {
   1.338 +    nssCKFWMutex_Destroy(fwInstance->mutex);
   1.339 +  }
   1.340 +
   1.341 +  if (arena) {
   1.342 +    (void)NSSArena_Destroy(arena);
   1.343 +  }
   1.344 +  return (NSSCKFWInstance *)NULL;
   1.345 +}
   1.346 +
   1.347 +/*
   1.348 + * nssCKFWInstance_Destroy
   1.349 + *
   1.350 + */
   1.351 +NSS_IMPLEMENT CK_RV
   1.352 +nssCKFWInstance_Destroy
   1.353 +(
   1.354 +  NSSCKFWInstance *fwInstance
   1.355 +)
   1.356 +{
   1.357 +#ifdef NSSDEBUG
   1.358 +  CK_RV error = CKR_OK;
   1.359 +#endif /* NSSDEBUG */
   1.360 +  CK_ULONG i;
   1.361 +
   1.362 +#ifdef NSSDEBUG
   1.363 +  error = nssCKFWInstance_verifyPointer(fwInstance);
   1.364 +  if( CKR_OK != error ) {
   1.365 +    return error;
   1.366 +  }
   1.367 +#endif /* NSSDEBUG */
   1.368 +
   1.369 +  nssCKFWMutex_Destroy(fwInstance->mutex);
   1.370 +
   1.371 +  for( i = 0; i < fwInstance->nSlots; i++ ) {
   1.372 +    (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[i]);
   1.373 +  }
   1.374 +
   1.375 +  if (fwInstance->mdInstance->Finalize) {
   1.376 +    fwInstance->mdInstance->Finalize(fwInstance->mdInstance, fwInstance);
   1.377 +  }
   1.378 +
   1.379 +  if (fwInstance->sessionHandleHash) {
   1.380 +     nssCKFWHash_Destroy(fwInstance->sessionHandleHash);
   1.381 +  }
   1.382 +
   1.383 +  if (fwInstance->objectHandleHash) {
   1.384 +     nssCKFWHash_Destroy(fwInstance->objectHandleHash);
   1.385 +  }
   1.386 +
   1.387 +#ifdef DEBUG
   1.388 +  (void)instance_remove_pointer(fwInstance);
   1.389 +#endif /* DEBUG */
   1.390 +
   1.391 +  (void)NSSArena_Destroy(fwInstance->arena);
   1.392 +  return CKR_OK;
   1.393 +}
   1.394 +
   1.395 +/*
   1.396 + * nssCKFWInstance_GetMDInstance
   1.397 + *
   1.398 + */
   1.399 +NSS_IMPLEMENT NSSCKMDInstance *
   1.400 +nssCKFWInstance_GetMDInstance
   1.401 +(
   1.402 +  NSSCKFWInstance *fwInstance
   1.403 +)
   1.404 +{
   1.405 +#ifdef NSSDEBUG
   1.406 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.407 +    return (NSSCKMDInstance *)NULL;
   1.408 +  }
   1.409 +#endif /* NSSDEBUG */
   1.410 +
   1.411 +  return fwInstance->mdInstance;
   1.412 +}
   1.413 +
   1.414 +/*
   1.415 + * nssCKFWInstance_GetArena
   1.416 + *
   1.417 + */
   1.418 +NSS_IMPLEMENT NSSArena *
   1.419 +nssCKFWInstance_GetArena
   1.420 +(
   1.421 +  NSSCKFWInstance *fwInstance,
   1.422 +  CK_RV *pError
   1.423 +)
   1.424 +{
   1.425 +#ifdef NSSDEBUG
   1.426 +  if (!pError) {
   1.427 +    return (NSSArena *)NULL;
   1.428 +  }
   1.429 +
   1.430 +  *pError = nssCKFWInstance_verifyPointer(fwInstance);
   1.431 +  if( CKR_OK != *pError ) {
   1.432 +    return (NSSArena *)NULL;
   1.433 +  }
   1.434 +#endif /* NSSDEBUG */
   1.435 +
   1.436 +  *pError = CKR_OK;
   1.437 +  return fwInstance->arena;
   1.438 +}
   1.439 +
   1.440 +/*
   1.441 + * nssCKFWInstance_MayCreatePthreads
   1.442 + *
   1.443 + */
   1.444 +NSS_IMPLEMENT CK_BBOOL
   1.445 +nssCKFWInstance_MayCreatePthreads
   1.446 +(
   1.447 +  NSSCKFWInstance *fwInstance
   1.448 +)
   1.449 +{
   1.450 +#ifdef NSSDEBUG
   1.451 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.452 +    return CK_FALSE;
   1.453 +  }
   1.454 +#endif /* NSSDEBUG */
   1.455 +
   1.456 +  return fwInstance->mayCreatePthreads;
   1.457 +}
   1.458 +
   1.459 +/*
   1.460 + * nssCKFWInstance_CreateMutex
   1.461 + *
   1.462 + */
   1.463 +NSS_IMPLEMENT NSSCKFWMutex *
   1.464 +nssCKFWInstance_CreateMutex
   1.465 +(
   1.466 +  NSSCKFWInstance *fwInstance,
   1.467 +  NSSArena *arena,
   1.468 +  CK_RV *pError
   1.469 +)
   1.470 +{
   1.471 +  NSSCKFWMutex *mutex;
   1.472 +
   1.473 +#ifdef NSSDEBUG
   1.474 +  if (!pError) {
   1.475 +    return (NSSCKFWMutex *)NULL;
   1.476 +  }
   1.477 +
   1.478 +  *pError = nssCKFWInstance_verifyPointer(fwInstance);
   1.479 +  if( CKR_OK != *pError ) {
   1.480 +    return (NSSCKFWMutex *)NULL;
   1.481 +  }
   1.482 +#endif /* NSSDEBUG */
   1.483 +
   1.484 +  mutex = nssCKFWMutex_Create(fwInstance->pInitArgs, fwInstance->LockingState,
   1.485 +                              arena, pError);
   1.486 +  if (!mutex) {
   1.487 +    if( CKR_OK == *pError ) {
   1.488 +      *pError = CKR_GENERAL_ERROR;
   1.489 +    }
   1.490 +
   1.491 +    return (NSSCKFWMutex *)NULL;
   1.492 +  }
   1.493 +
   1.494 +  return mutex;
   1.495 +}
   1.496 +
   1.497 +/*
   1.498 + * nssCKFWInstance_GetConfigurationData
   1.499 + *
   1.500 + */
   1.501 +NSS_IMPLEMENT NSSUTF8 *
   1.502 +nssCKFWInstance_GetConfigurationData
   1.503 +(
   1.504 +  NSSCKFWInstance *fwInstance
   1.505 +)
   1.506 +{
   1.507 +#ifdef NSSDEBUG
   1.508 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.509 +    return (NSSUTF8 *)NULL;
   1.510 +  }
   1.511 +#endif /* NSSDEBUG */
   1.512 +
   1.513 +  return fwInstance->configurationData;
   1.514 +}
   1.515 +
   1.516 +/*
   1.517 + * nssCKFWInstance_GetInitArgs
   1.518 + *
   1.519 + */
   1.520 +CK_C_INITIALIZE_ARGS_PTR
   1.521 +nssCKFWInstance_GetInitArgs
   1.522 +(
   1.523 +  NSSCKFWInstance *fwInstance
   1.524 +)
   1.525 +{
   1.526 +#ifdef NSSDEBUG
   1.527 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.528 +    return (CK_C_INITIALIZE_ARGS_PTR)NULL;
   1.529 +  }
   1.530 +#endif /* NSSDEBUG */
   1.531 +
   1.532 +    return fwInstance->pInitArgs;
   1.533 +}
   1.534 +
   1.535 +/*
   1.536 + * nssCKFWInstance_CreateSessionHandle
   1.537 + *
   1.538 + */
   1.539 +NSS_IMPLEMENT CK_SESSION_HANDLE
   1.540 +nssCKFWInstance_CreateSessionHandle
   1.541 +(
   1.542 +  NSSCKFWInstance *fwInstance,
   1.543 +  NSSCKFWSession *fwSession,
   1.544 +  CK_RV *pError
   1.545 +)
   1.546 +{
   1.547 +  CK_SESSION_HANDLE hSession;
   1.548 +
   1.549 +#ifdef NSSDEBUG
   1.550 +  if (!pError) {
   1.551 +    return (CK_SESSION_HANDLE)0;
   1.552 +  }
   1.553 +
   1.554 +  *pError = nssCKFWInstance_verifyPointer(fwInstance);
   1.555 +  if( CKR_OK != *pError ) {
   1.556 +    return (CK_SESSION_HANDLE)0;
   1.557 +  }
   1.558 +#endif /* NSSDEBUG */
   1.559 +
   1.560 +  *pError = nssCKFWMutex_Lock(fwInstance->mutex);
   1.561 +  if( CKR_OK != *pError ) {
   1.562 +    return (CK_SESSION_HANDLE)0;
   1.563 +  }
   1.564 +
   1.565 +  hSession = ++(fwInstance->lastSessionHandle);
   1.566 +
   1.567 +  /* Alan would say I should unlock for this call. */
   1.568 +  
   1.569 +  *pError = nssCKFWSession_SetHandle(fwSession, hSession);
   1.570 +  if( CKR_OK != *pError ) {
   1.571 +    goto done;
   1.572 +  }
   1.573 +
   1.574 +  *pError = nssCKFWHash_Add(fwInstance->sessionHandleHash, 
   1.575 +              (const void *)hSession, (const void *)fwSession);
   1.576 +  if( CKR_OK != *pError ) {
   1.577 +    hSession = (CK_SESSION_HANDLE)0;
   1.578 +    goto done;
   1.579 +  }
   1.580 +
   1.581 + done:
   1.582 +  nssCKFWMutex_Unlock(fwInstance->mutex);
   1.583 +  return hSession;
   1.584 +}
   1.585 +
   1.586 +/*
   1.587 + * nssCKFWInstance_ResolveSessionHandle
   1.588 + *
   1.589 + */
   1.590 +NSS_IMPLEMENT NSSCKFWSession *
   1.591 +nssCKFWInstance_ResolveSessionHandle
   1.592 +(
   1.593 +  NSSCKFWInstance *fwInstance,
   1.594 +  CK_SESSION_HANDLE hSession
   1.595 +)
   1.596 +{
   1.597 +  NSSCKFWSession *fwSession;
   1.598 +
   1.599 +#ifdef NSSDEBUG
   1.600 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.601 +    return (NSSCKFWSession *)NULL;
   1.602 +  }
   1.603 +#endif /* NSSDEBUG */
   1.604 +
   1.605 +  if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   1.606 +    return (NSSCKFWSession *)NULL;
   1.607 +  }
   1.608 +
   1.609 +  fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
   1.610 +                fwInstance->sessionHandleHash, (const void *)hSession);
   1.611 +
   1.612 +  /* Assert(hSession == nssCKFWSession_GetHandle(fwSession)) */
   1.613 +
   1.614 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   1.615 +
   1.616 +  return fwSession;
   1.617 +}
   1.618 +
   1.619 +/*
   1.620 + * nssCKFWInstance_DestroySessionHandle
   1.621 + *
   1.622 + */
   1.623 +NSS_IMPLEMENT void
   1.624 +nssCKFWInstance_DestroySessionHandle
   1.625 +(
   1.626 +  NSSCKFWInstance *fwInstance,
   1.627 +  CK_SESSION_HANDLE hSession
   1.628 +)
   1.629 +{
   1.630 +  NSSCKFWSession *fwSession;
   1.631 +
   1.632 +#ifdef NSSDEBUG
   1.633 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.634 +    return;
   1.635 +  }
   1.636 +#endif /* NSSDEBUG */
   1.637 +
   1.638 +  if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   1.639 +    return;
   1.640 +  }
   1.641 +
   1.642 +  fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
   1.643 +                fwInstance->sessionHandleHash, (const void *)hSession);
   1.644 +  if (fwSession) {
   1.645 +    nssCKFWHash_Remove(fwInstance->sessionHandleHash, (const void *)hSession);
   1.646 +    nssCKFWSession_SetHandle(fwSession, (CK_SESSION_HANDLE)0);
   1.647 +  }
   1.648 +
   1.649 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   1.650 +
   1.651 +  return;
   1.652 +}
   1.653 +
   1.654 +/*
   1.655 + * nssCKFWInstance_FindSessionHandle
   1.656 + *
   1.657 + */
   1.658 +NSS_IMPLEMENT CK_SESSION_HANDLE
   1.659 +nssCKFWInstance_FindSessionHandle
   1.660 +(
   1.661 +  NSSCKFWInstance *fwInstance,
   1.662 +  NSSCKFWSession *fwSession
   1.663 +)
   1.664 +{
   1.665 +#ifdef NSSDEBUG
   1.666 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.667 +    return (CK_SESSION_HANDLE)0;
   1.668 +  }
   1.669 +
   1.670 +  if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {
   1.671 +    return (CK_SESSION_HANDLE)0;
   1.672 +  }
   1.673 +#endif /* NSSDEBUG */
   1.674 +
   1.675 +  return nssCKFWSession_GetHandle(fwSession);
   1.676 +  /* look it up and assert? */
   1.677 +}
   1.678 +
   1.679 +/*
   1.680 + * nssCKFWInstance_CreateObjectHandle
   1.681 + *
   1.682 + */
   1.683 +NSS_IMPLEMENT CK_OBJECT_HANDLE
   1.684 +nssCKFWInstance_CreateObjectHandle
   1.685 +(
   1.686 +  NSSCKFWInstance *fwInstance,
   1.687 +  NSSCKFWObject *fwObject,
   1.688 +  CK_RV *pError
   1.689 +)
   1.690 +{
   1.691 +  CK_OBJECT_HANDLE hObject;
   1.692 +
   1.693 +#ifdef NSSDEBUG
   1.694 +  if (!pError) {
   1.695 +    return (CK_OBJECT_HANDLE)0;
   1.696 +  }
   1.697 +
   1.698 +  *pError = nssCKFWInstance_verifyPointer(fwInstance);
   1.699 +  if( CKR_OK != *pError ) {
   1.700 +    return (CK_OBJECT_HANDLE)0;
   1.701 +  }
   1.702 +#endif /* NSSDEBUG */
   1.703 +
   1.704 +  *pError = nssCKFWMutex_Lock(fwInstance->mutex);
   1.705 +  if( CKR_OK != *pError ) {
   1.706 +    return (CK_OBJECT_HANDLE)0;
   1.707 +  }
   1.708 +
   1.709 +  hObject = ++(fwInstance->lastObjectHandle);
   1.710 +
   1.711 +  *pError = nssCKFWObject_SetHandle(fwObject, hObject);
   1.712 +  if( CKR_OK != *pError ) {
   1.713 +    hObject = (CK_OBJECT_HANDLE)0;
   1.714 +    goto done;
   1.715 +  }
   1.716 +
   1.717 +  *pError = nssCKFWHash_Add(fwInstance->objectHandleHash, 
   1.718 +              (const void *)hObject, (const void *)fwObject);
   1.719 +  if( CKR_OK != *pError ) {
   1.720 +    hObject = (CK_OBJECT_HANDLE)0;
   1.721 +    goto done;
   1.722 +  }
   1.723 +
   1.724 + done:
   1.725 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   1.726 +  return hObject;
   1.727 +}
   1.728 +
   1.729 +/*
   1.730 + * nssCKFWInstance_ResolveObjectHandle
   1.731 + *
   1.732 + */
   1.733 +NSS_IMPLEMENT NSSCKFWObject *
   1.734 +nssCKFWInstance_ResolveObjectHandle
   1.735 +(
   1.736 +  NSSCKFWInstance *fwInstance,
   1.737 +  CK_OBJECT_HANDLE hObject
   1.738 +)
   1.739 +{
   1.740 +  NSSCKFWObject *fwObject;
   1.741 +
   1.742 +#ifdef NSSDEBUG
   1.743 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.744 +    return (NSSCKFWObject *)NULL;
   1.745 +  }
   1.746 +#endif /* NSSDEBUG */
   1.747 +
   1.748 +  if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   1.749 +    return (NSSCKFWObject *)NULL;
   1.750 +  }
   1.751 +
   1.752 +  fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
   1.753 +                fwInstance->objectHandleHash, (const void *)hObject);
   1.754 +
   1.755 +  /* Assert(hObject == nssCKFWObject_GetHandle(fwObject)) */
   1.756 +
   1.757 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   1.758 +  return fwObject;
   1.759 +}
   1.760 +
   1.761 +/*
   1.762 + * nssCKFWInstance_ReassignObjectHandle
   1.763 + *
   1.764 + */
   1.765 +NSS_IMPLEMENT CK_RV
   1.766 +nssCKFWInstance_ReassignObjectHandle
   1.767 +(
   1.768 +  NSSCKFWInstance *fwInstance,
   1.769 +  CK_OBJECT_HANDLE hObject,
   1.770 +  NSSCKFWObject *fwObject
   1.771 +)
   1.772 +{
   1.773 +  CK_RV error = CKR_OK;
   1.774 +  NSSCKFWObject *oldObject;
   1.775 +
   1.776 +#ifdef NSSDEBUG
   1.777 +  error = nssCKFWInstance_verifyPointer(fwInstance);
   1.778 +  if( CKR_OK != error ) {
   1.779 +    return error;
   1.780 +  }
   1.781 +#endif /* NSSDEBUG */
   1.782 +
   1.783 +  error = nssCKFWMutex_Lock(fwInstance->mutex);
   1.784 +  if( CKR_OK != error ) {
   1.785 +    return error;
   1.786 +  }
   1.787 +
   1.788 +  oldObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
   1.789 +                 fwInstance->objectHandleHash, (const void *)hObject);
   1.790 +  if(oldObject) {
   1.791 +    /* Assert(hObject == nssCKFWObject_GetHandle(oldObject) */
   1.792 +    (void)nssCKFWObject_SetHandle(oldObject, (CK_SESSION_HANDLE)0);
   1.793 +    nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)hObject);
   1.794 +  }
   1.795 +
   1.796 +  error = nssCKFWObject_SetHandle(fwObject, hObject);
   1.797 +  if( CKR_OK != error ) {
   1.798 +    goto done;
   1.799 +  }
   1.800 +  error = nssCKFWHash_Add(fwInstance->objectHandleHash, 
   1.801 +            (const void *)hObject, (const void *)fwObject);
   1.802 +
   1.803 + done:
   1.804 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   1.805 +  return error;
   1.806 +}
   1.807 +
   1.808 +/*
   1.809 + * nssCKFWInstance_DestroyObjectHandle
   1.810 + *
   1.811 + */
   1.812 +NSS_IMPLEMENT void
   1.813 +nssCKFWInstance_DestroyObjectHandle
   1.814 +(
   1.815 +  NSSCKFWInstance *fwInstance,
   1.816 +  CK_OBJECT_HANDLE hObject
   1.817 +)
   1.818 +{
   1.819 +  NSSCKFWObject *fwObject;
   1.820 +
   1.821 +#ifdef NSSDEBUG
   1.822 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.823 +    return;
   1.824 +  }
   1.825 +#endif /* NSSDEBUG */
   1.826 +
   1.827 +  if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   1.828 +    return;
   1.829 +  }
   1.830 +
   1.831 +  fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
   1.832 +                fwInstance->objectHandleHash, (const void *)hObject);
   1.833 +  if (fwObject) {
   1.834 +    /* Assert(hObject = nssCKFWObject_GetHandle(fwObject)) */
   1.835 +    nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)hObject);
   1.836 +    (void)nssCKFWObject_SetHandle(fwObject, (CK_SESSION_HANDLE)0);
   1.837 +  }
   1.838 +
   1.839 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   1.840 +  return;
   1.841 +}
   1.842 +
   1.843 +/*
   1.844 + * nssCKFWInstance_FindObjectHandle
   1.845 + *
   1.846 + */
   1.847 +NSS_IMPLEMENT CK_OBJECT_HANDLE
   1.848 +nssCKFWInstance_FindObjectHandle
   1.849 +(
   1.850 +  NSSCKFWInstance *fwInstance,
   1.851 +  NSSCKFWObject *fwObject
   1.852 +)
   1.853 +{
   1.854 +#ifdef NSSDEBUG
   1.855 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.856 +    return (CK_OBJECT_HANDLE)0;
   1.857 +  }
   1.858 +
   1.859 +  if( CKR_OK != nssCKFWObject_verifyPointer(fwObject) ) {
   1.860 +    return (CK_OBJECT_HANDLE)0;
   1.861 +  }
   1.862 +#endif /* NSSDEBUG */
   1.863 +  
   1.864 +  return nssCKFWObject_GetHandle(fwObject);
   1.865 +}
   1.866 +
   1.867 +/*
   1.868 + * nssCKFWInstance_GetNSlots
   1.869 + *
   1.870 + */
   1.871 +NSS_IMPLEMENT CK_ULONG
   1.872 +nssCKFWInstance_GetNSlots
   1.873 +(
   1.874 +  NSSCKFWInstance *fwInstance,
   1.875 +  CK_RV *pError
   1.876 +)
   1.877 +{
   1.878 +#ifdef NSSDEBUG
   1.879 +  if (!pError) {
   1.880 +    return (CK_ULONG)0;
   1.881 +  }
   1.882 +
   1.883 +  *pError = nssCKFWInstance_verifyPointer(fwInstance);
   1.884 +  if( CKR_OK != *pError ) {
   1.885 +    return (CK_ULONG)0;
   1.886 +  }
   1.887 +#endif /* NSSDEBUG */
   1.888 +
   1.889 +  *pError = CKR_OK;
   1.890 +  return fwInstance->nSlots;
   1.891 +}  
   1.892 +
   1.893 +/*
   1.894 + * nssCKFWInstance_GetCryptokiVersion
   1.895 + *
   1.896 + */
   1.897 +NSS_IMPLEMENT CK_VERSION
   1.898 +nssCKFWInstance_GetCryptokiVersion
   1.899 +(
   1.900 +  NSSCKFWInstance *fwInstance
   1.901 +)
   1.902 +{
   1.903 +  CK_VERSION rv;
   1.904 +
   1.905 +#ifdef NSSDEBUG
   1.906 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.907 +    rv.major = rv.minor = 0;
   1.908 +    return rv;
   1.909 +  }
   1.910 +#endif /* NSSDEBUG */
   1.911 +
   1.912 +  if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
   1.913 +    rv.major = rv.minor = 0;
   1.914 +    return rv;
   1.915 +  }
   1.916 +
   1.917 +  if( (0 != fwInstance->cryptokiVersion.major) ||
   1.918 +      (0 != fwInstance->cryptokiVersion.minor) ) {
   1.919 +    rv = fwInstance->cryptokiVersion;
   1.920 +    goto done;
   1.921 +  }
   1.922 +
   1.923 +  if (fwInstance->mdInstance->GetCryptokiVersion) {
   1.924 +    fwInstance->cryptokiVersion = fwInstance->mdInstance->GetCryptokiVersion(
   1.925 +      fwInstance->mdInstance, fwInstance);
   1.926 +  } else {
   1.927 +    fwInstance->cryptokiVersion.major = 2;
   1.928 +    fwInstance->cryptokiVersion.minor = 1;
   1.929 +  }
   1.930 +
   1.931 +  rv = fwInstance->cryptokiVersion;
   1.932 +
   1.933 + done:
   1.934 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   1.935 +  return rv;
   1.936 +}
   1.937 +
   1.938 +/*
   1.939 + * nssCKFWInstance_GetManufacturerID
   1.940 + *
   1.941 + */
   1.942 +NSS_IMPLEMENT CK_RV
   1.943 +nssCKFWInstance_GetManufacturerID
   1.944 +(
   1.945 +  NSSCKFWInstance *fwInstance,
   1.946 +  CK_CHAR manufacturerID[32]
   1.947 +)
   1.948 +{
   1.949 +  CK_RV error = CKR_OK;
   1.950 +
   1.951 +#ifdef NSSDEBUG
   1.952 +  if( (CK_CHAR_PTR)NULL == manufacturerID ) {
   1.953 +    return CKR_ARGUMENTS_BAD;
   1.954 +  }
   1.955 +
   1.956 +  error = nssCKFWInstance_verifyPointer(fwInstance);
   1.957 +  if( CKR_OK != error ) {
   1.958 +    return error;
   1.959 +  }
   1.960 +#endif /* NSSDEBUG */
   1.961 +
   1.962 +  error = nssCKFWMutex_Lock(fwInstance->mutex);
   1.963 +  if( CKR_OK != error ) {
   1.964 +    return error;
   1.965 +  }
   1.966 +
   1.967 +  if (!fwInstance->manufacturerID) {
   1.968 +    if (fwInstance->mdInstance->GetManufacturerID) {
   1.969 +      fwInstance->manufacturerID = fwInstance->mdInstance->GetManufacturerID(
   1.970 +        fwInstance->mdInstance, fwInstance, &error);
   1.971 +      if ((!fwInstance->manufacturerID) && (CKR_OK != error)) {
   1.972 +        goto done;
   1.973 +      }
   1.974 +    } else {
   1.975 +      fwInstance->manufacturerID = (NSSUTF8 *) "";
   1.976 +    }
   1.977 +  }
   1.978 +
   1.979 +  (void)nssUTF8_CopyIntoFixedBuffer(fwInstance->manufacturerID, (char *)manufacturerID, 32, ' ');
   1.980 +  error = CKR_OK;
   1.981 +
   1.982 + done:
   1.983 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
   1.984 +  return error;
   1.985 +}
   1.986 +
   1.987 +/*
   1.988 + * nssCKFWInstance_GetFlags
   1.989 + *
   1.990 + */
   1.991 +NSS_IMPLEMENT CK_ULONG
   1.992 +nssCKFWInstance_GetFlags
   1.993 +(
   1.994 +  NSSCKFWInstance *fwInstance
   1.995 +)
   1.996 +{
   1.997 +#ifdef NSSDEBUG
   1.998 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
   1.999 +    return (CK_ULONG)0;
  1.1000 +  }
  1.1001 +#endif /* NSSDEBUG */
  1.1002 +
  1.1003 +  /* No "instance flags" are yet defined by Cryptoki. */
  1.1004 +  return (CK_ULONG)0;
  1.1005 +}
  1.1006 +
  1.1007 +/*
  1.1008 + * nssCKFWInstance_GetLibraryDescription
  1.1009 + *
  1.1010 + */
  1.1011 +NSS_IMPLEMENT CK_RV
  1.1012 +nssCKFWInstance_GetLibraryDescription
  1.1013 +(
  1.1014 +  NSSCKFWInstance *fwInstance,
  1.1015 +  CK_CHAR libraryDescription[32]
  1.1016 +)
  1.1017 +{
  1.1018 +  CK_RV error = CKR_OK;
  1.1019 +
  1.1020 +#ifdef NSSDEBUG
  1.1021 +  if( (CK_CHAR_PTR)NULL == libraryDescription ) {
  1.1022 +    return CKR_ARGUMENTS_BAD;
  1.1023 +  }
  1.1024 +
  1.1025 +  error = nssCKFWInstance_verifyPointer(fwInstance);
  1.1026 +  if( CKR_OK != error ) {
  1.1027 +    return error;
  1.1028 +  }
  1.1029 +#endif /* NSSDEBUG */
  1.1030 +
  1.1031 +  error = nssCKFWMutex_Lock(fwInstance->mutex);
  1.1032 +  if( CKR_OK != error ) {
  1.1033 +    return error;
  1.1034 +  }
  1.1035 +
  1.1036 +  if (!fwInstance->libraryDescription) {
  1.1037 +    if (fwInstance->mdInstance->GetLibraryDescription) {
  1.1038 +      fwInstance->libraryDescription = fwInstance->mdInstance->GetLibraryDescription(
  1.1039 +        fwInstance->mdInstance, fwInstance, &error);
  1.1040 +      if ((!fwInstance->libraryDescription) && (CKR_OK != error)) {
  1.1041 +        goto done;
  1.1042 +      }
  1.1043 +    } else {
  1.1044 +      fwInstance->libraryDescription = (NSSUTF8 *) "";
  1.1045 +    }
  1.1046 +  }
  1.1047 +
  1.1048 +  (void)nssUTF8_CopyIntoFixedBuffer(fwInstance->libraryDescription, (char *)libraryDescription, 32, ' ');
  1.1049 +  error = CKR_OK;
  1.1050 +
  1.1051 + done:
  1.1052 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
  1.1053 +  return error;
  1.1054 +}
  1.1055 +
  1.1056 +/*
  1.1057 + * nssCKFWInstance_GetLibraryVersion
  1.1058 + *
  1.1059 + */
  1.1060 +NSS_IMPLEMENT CK_VERSION
  1.1061 +nssCKFWInstance_GetLibraryVersion
  1.1062 +(
  1.1063 +  NSSCKFWInstance *fwInstance
  1.1064 +)
  1.1065 +{
  1.1066 +  CK_VERSION rv;
  1.1067 +
  1.1068 +#ifdef NSSDEBUG
  1.1069 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1.1070 +    rv.major = rv.minor = 0;
  1.1071 +    return rv;
  1.1072 +  }
  1.1073 +#endif /* NSSDEBUG */
  1.1074 +
  1.1075 +  if( CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex) ) {
  1.1076 +    rv.major = rv.minor = 0;
  1.1077 +    return rv;
  1.1078 +  }
  1.1079 +
  1.1080 +  if( (0 != fwInstance->libraryVersion.major) ||
  1.1081 +      (0 != fwInstance->libraryVersion.minor) ) {
  1.1082 +    rv = fwInstance->libraryVersion;
  1.1083 +    goto done;
  1.1084 +  }
  1.1085 +
  1.1086 +  if (fwInstance->mdInstance->GetLibraryVersion) {
  1.1087 +    fwInstance->libraryVersion = fwInstance->mdInstance->GetLibraryVersion(
  1.1088 +      fwInstance->mdInstance, fwInstance);
  1.1089 +  } else {
  1.1090 +    fwInstance->libraryVersion.major = 0;
  1.1091 +    fwInstance->libraryVersion.minor = 3;
  1.1092 +  }
  1.1093 +
  1.1094 +  rv = fwInstance->libraryVersion;
  1.1095 + done:
  1.1096 +  (void)nssCKFWMutex_Unlock(fwInstance->mutex);
  1.1097 +  return rv;
  1.1098 +}
  1.1099 +
  1.1100 +/*
  1.1101 + * nssCKFWInstance_GetModuleHandlesSessionObjects
  1.1102 + *
  1.1103 + */
  1.1104 +NSS_IMPLEMENT CK_BBOOL
  1.1105 +nssCKFWInstance_GetModuleHandlesSessionObjects
  1.1106 +(
  1.1107 +  NSSCKFWInstance *fwInstance
  1.1108 +)
  1.1109 +{
  1.1110 +#ifdef NSSDEBUG
  1.1111 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1.1112 +    return CK_FALSE;
  1.1113 +  }
  1.1114 +#endif /* NSSDEBUG */
  1.1115 +
  1.1116 +  return fwInstance->moduleHandlesSessionObjects;
  1.1117 +}
  1.1118 +
  1.1119 +/*
  1.1120 + * nssCKFWInstance_GetSlots
  1.1121 + *
  1.1122 + */
  1.1123 +NSS_IMPLEMENT NSSCKFWSlot **
  1.1124 +nssCKFWInstance_GetSlots
  1.1125 +(
  1.1126 +  NSSCKFWInstance *fwInstance,
  1.1127 +  CK_RV *pError
  1.1128 +)
  1.1129 +{
  1.1130 +#ifdef NSSDEBUG
  1.1131 +  if (!pError) {
  1.1132 +    return (NSSCKFWSlot **)NULL;
  1.1133 +  }
  1.1134 +
  1.1135 +  *pError = nssCKFWInstance_verifyPointer(fwInstance);
  1.1136 +  if( CKR_OK != *pError ) {
  1.1137 +    return (NSSCKFWSlot **)NULL;
  1.1138 +  }
  1.1139 +#endif /* NSSDEBUG */
  1.1140 +
  1.1141 +  return fwInstance->fwSlotList;
  1.1142 +}
  1.1143 +
  1.1144 +/*
  1.1145 + * nssCKFWInstance_WaitForSlotEvent
  1.1146 + *
  1.1147 + */
  1.1148 +NSS_IMPLEMENT NSSCKFWSlot *
  1.1149 +nssCKFWInstance_WaitForSlotEvent
  1.1150 +(
  1.1151 +  NSSCKFWInstance *fwInstance,
  1.1152 +  CK_BBOOL block,
  1.1153 +  CK_RV *pError
  1.1154 +)
  1.1155 +{
  1.1156 +  NSSCKFWSlot *fwSlot = (NSSCKFWSlot *)NULL;
  1.1157 +  NSSCKMDSlot *mdSlot;
  1.1158 +  CK_ULONG i, n;
  1.1159 +
  1.1160 +#ifdef NSSDEBUG
  1.1161 +  if (!pError) {
  1.1162 +    return (NSSCKFWSlot *)NULL;
  1.1163 +  }
  1.1164 +
  1.1165 +  *pError = nssCKFWInstance_verifyPointer(fwInstance);
  1.1166 +  if( CKR_OK != *pError ) {
  1.1167 +    return (NSSCKFWSlot *)NULL;
  1.1168 +  }
  1.1169 +
  1.1170 +  switch( block ) {
  1.1171 +  case CK_TRUE:
  1.1172 +  case CK_FALSE:
  1.1173 +    break;
  1.1174 +  default:
  1.1175 +    *pError = CKR_ARGUMENTS_BAD;
  1.1176 +    return (NSSCKFWSlot *)NULL;
  1.1177 +  }
  1.1178 +#endif /* NSSDEBUG */
  1.1179 +
  1.1180 +  if (!fwInstance->mdInstance->WaitForSlotEvent) {
  1.1181 +    *pError = CKR_NO_EVENT;
  1.1182 +    return (NSSCKFWSlot *)NULL;
  1.1183 +  }
  1.1184 +
  1.1185 +  mdSlot = fwInstance->mdInstance->WaitForSlotEvent(
  1.1186 +    fwInstance->mdInstance,
  1.1187 +    fwInstance,
  1.1188 +    block,
  1.1189 +    pError
  1.1190 +  );
  1.1191 +
  1.1192 +  if (!mdSlot) {
  1.1193 +    return (NSSCKFWSlot *)NULL;
  1.1194 +  }
  1.1195 +
  1.1196 +  n = nssCKFWInstance_GetNSlots(fwInstance, pError);
  1.1197 +  if( ((CK_ULONG)0 == n) && (CKR_OK != *pError) ) {
  1.1198 +    return (NSSCKFWSlot *)NULL;
  1.1199 +  }
  1.1200 +
  1.1201 +  for( i = 0; i < n; i++ ) {
  1.1202 +    if( fwInstance->mdSlotList[i] == mdSlot ) {
  1.1203 +      fwSlot = fwInstance->fwSlotList[i];
  1.1204 +      break;
  1.1205 +    }
  1.1206 +  }
  1.1207 +
  1.1208 +  if (!fwSlot) {
  1.1209 +    /* Internal error */
  1.1210 +    *pError = CKR_GENERAL_ERROR;
  1.1211 +    return (NSSCKFWSlot *)NULL;
  1.1212 +  }
  1.1213 +
  1.1214 +  return fwSlot;
  1.1215 +}
  1.1216 +
  1.1217 +/*
  1.1218 + * NSSCKFWInstance_GetMDInstance
  1.1219 + *
  1.1220 + */
  1.1221 +NSS_IMPLEMENT NSSCKMDInstance *
  1.1222 +NSSCKFWInstance_GetMDInstance
  1.1223 +(
  1.1224 +  NSSCKFWInstance *fwInstance
  1.1225 +)
  1.1226 +{
  1.1227 +#ifdef DEBUG
  1.1228 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1.1229 +    return (NSSCKMDInstance *)NULL;
  1.1230 +  }
  1.1231 +#endif /* DEBUG */
  1.1232 +
  1.1233 +  return nssCKFWInstance_GetMDInstance(fwInstance);
  1.1234 +}
  1.1235 +
  1.1236 +/*
  1.1237 + * NSSCKFWInstance_GetArena
  1.1238 + *
  1.1239 + */
  1.1240 +NSS_IMPLEMENT NSSArena *
  1.1241 +NSSCKFWInstance_GetArena
  1.1242 +(
  1.1243 +  NSSCKFWInstance *fwInstance,
  1.1244 +  CK_RV *pError
  1.1245 +)
  1.1246 +{
  1.1247 +#ifdef DEBUG
  1.1248 +  if (!pError) {
  1.1249 +    return (NSSArena *)NULL;
  1.1250 +  }
  1.1251 +
  1.1252 +  *pError = nssCKFWInstance_verifyPointer(fwInstance);
  1.1253 +  if( CKR_OK != *pError ) {
  1.1254 +    return (NSSArena *)NULL;
  1.1255 +  }
  1.1256 +#endif /* DEBUG */
  1.1257 +
  1.1258 +  return nssCKFWInstance_GetArena(fwInstance, pError);
  1.1259 +}
  1.1260 +
  1.1261 +/*
  1.1262 + * NSSCKFWInstance_MayCreatePthreads
  1.1263 + *
  1.1264 + */
  1.1265 +NSS_IMPLEMENT CK_BBOOL
  1.1266 +NSSCKFWInstance_MayCreatePthreads
  1.1267 +(
  1.1268 +  NSSCKFWInstance *fwInstance
  1.1269 +)
  1.1270 +{
  1.1271 +#ifdef DEBUG
  1.1272 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1.1273 +    return CK_FALSE;
  1.1274 +  }
  1.1275 +#endif /* DEBUG */
  1.1276 +
  1.1277 +  return nssCKFWInstance_MayCreatePthreads(fwInstance);
  1.1278 +}
  1.1279 +
  1.1280 +/*
  1.1281 + * NSSCKFWInstance_CreateMutex
  1.1282 + *
  1.1283 + */
  1.1284 +NSS_IMPLEMENT NSSCKFWMutex *
  1.1285 +NSSCKFWInstance_CreateMutex
  1.1286 +(
  1.1287 +  NSSCKFWInstance *fwInstance,
  1.1288 +  NSSArena *arena,
  1.1289 +  CK_RV *pError
  1.1290 +)
  1.1291 +{
  1.1292 +#ifdef DEBUG
  1.1293 +  if (!pError) {
  1.1294 +    return (NSSCKFWMutex *)NULL;
  1.1295 +  }
  1.1296 +
  1.1297 +  *pError = nssCKFWInstance_verifyPointer(fwInstance);
  1.1298 +  if( CKR_OK != *pError ) {
  1.1299 +    return (NSSCKFWMutex *)NULL;
  1.1300 +  }
  1.1301 +#endif /* DEBUG */
  1.1302 +
  1.1303 +  return nssCKFWInstance_CreateMutex(fwInstance, arena, pError);
  1.1304 +}
  1.1305 +
  1.1306 +/*
  1.1307 + * NSSCKFWInstance_GetConfigurationData
  1.1308 + *
  1.1309 + */
  1.1310 +NSS_IMPLEMENT NSSUTF8 *
  1.1311 +NSSCKFWInstance_GetConfigurationData
  1.1312 +(
  1.1313 +  NSSCKFWInstance *fwInstance
  1.1314 +)
  1.1315 +{
  1.1316 +#ifdef DEBUG
  1.1317 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1.1318 +    return (NSSUTF8 *)NULL;
  1.1319 +  }
  1.1320 +#endif /* DEBUG */
  1.1321 +
  1.1322 +  return nssCKFWInstance_GetConfigurationData(fwInstance);
  1.1323 +}
  1.1324 +
  1.1325 +/*
  1.1326 + * NSSCKFWInstance_GetInitArgs
  1.1327 + *
  1.1328 + */
  1.1329 +NSS_IMPLEMENT CK_C_INITIALIZE_ARGS_PTR
  1.1330 +NSSCKFWInstance_GetInitArgs
  1.1331 +(
  1.1332 +  NSSCKFWInstance *fwInstance
  1.1333 +)
  1.1334 +{
  1.1335 +#ifdef DEBUG
  1.1336 +  if( CKR_OK != nssCKFWInstance_verifyPointer(fwInstance) ) {
  1.1337 +    return (CK_C_INITIALIZE_ARGS_PTR)NULL;
  1.1338 +  }
  1.1339 +#endif /* DEBUG */
  1.1340 +
  1.1341 +  return nssCKFWInstance_GetInitArgs(fwInstance);
  1.1342 +}
  1.1343 +

mercurial