Ejemplo n.º 1
0
/*
 * nssCKFWSlot_Destroy
 *
 */
NSS_IMPLEMENT CK_RV
nssCKFWSlot_Destroy(
    NSSCKFWSlot *fwSlot)
{
    CK_RV error = CKR_OK;

#ifdef NSSDEBUG
    error = nssCKFWSlot_verifyPointer(fwSlot);
    if (CKR_OK != error) {
        return error;
    }
#endif /* NSSDEBUG */
    if (fwSlot->fwToken) {
        nssCKFWToken_Destroy(fwSlot->fwToken);
    }

    (void)nssCKFWMutex_Destroy(fwSlot->mutex);

    if (fwSlot->mdSlot->Destroy) {
        fwSlot->mdSlot->Destroy(fwSlot->mdSlot, fwSlot,
                                fwSlot->mdInstance, fwSlot->fwInstance);
    }

#ifdef DEBUG
    error = slot_remove_pointer(fwSlot);
#endif /* DEBUG */
    (void)nss_ZFreeIf(fwSlot);
    return error;
}
Ejemplo n.º 2
0
/*
 * nssCKFWHash_Destroy
 *
 */
NSS_IMPLEMENT void
nssCKFWHash_Destroy
(
  nssCKFWHash *hash
)
{
  (void)nssCKFWMutex_Destroy(hash->mutex);
  PL_HashTableDestroy(hash->plHashTable);
  (void)nss_ZFreeIf(hash);
}
Ejemplo n.º 3
0
/*
 * nssCKFWHash_Create
 *
 */
NSS_IMPLEMENT nssCKFWHash *
nssCKFWHash_Create
(
  NSSCKFWInstance *fwInstance,
  NSSArena *arena,
  CK_RV *pError
)
{
  nssCKFWHash *rv;

#ifdef NSSDEBUG
  if (!pError) {
    return (nssCKFWHash *)NULL;
  }

  if( PR_SUCCESS != nssArena_verifyPointer(arena) ) {
    *pError = CKR_ARGUMENTS_BAD;
    return (nssCKFWHash *)NULL;
  }
#endif /* NSSDEBUG */

  rv = nss_ZNEW(arena, nssCKFWHash);
  if (!rv) {
    *pError = CKR_HOST_MEMORY;
    return (nssCKFWHash *)NULL;
  }

  rv->mutex = nssCKFWInstance_CreateMutex(fwInstance, arena, pError);
  if (!rv->mutex) {
    if( CKR_OK == *pError ) {
      (void)nss_ZFreeIf(rv);
      *pError = CKR_GENERAL_ERROR;
    }
    return (nssCKFWHash *)NULL;
  }

  rv->plHashTable = PL_NewHashTable(0, nss_ckfw_identity_hash, 
    PL_CompareValues, PL_CompareValues, &nssArenaHashAllocOps, arena);
  if (!rv->plHashTable) {
    (void)nssCKFWMutex_Destroy(rv->mutex);
    (void)nss_ZFreeIf(rv);
    *pError = CKR_HOST_MEMORY;
    return (nssCKFWHash *)NULL;
  }

  rv->count = 0;

  return rv;
}
Ejemplo n.º 4
0
/*
 * nssCKFWFindObjects_Destroy
 *
 */
NSS_EXTERN void
nssCKFWFindObjects_Destroy
(
  NSSCKFWFindObjects *fwFindObjects
)
{
#ifdef NSSDEBUG
  if( CKR_OK != nssCKFWFindObjects_verifyPointer(fwFindObjects) ) {
    return;
  }
#endif /* NSSDEBUG */

  (void)nssCKFWMutex_Destroy(fwFindObjects->mutex);

  if (fwFindObjects->mdfo1) {
    if (fwFindObjects->mdfo1->Final) {
      fwFindObjects->mdFindObjects = fwFindObjects->mdfo1;
      fwFindObjects->mdfo1->Final(fwFindObjects->mdfo1, fwFindObjects,
        fwFindObjects->mdSession, fwFindObjects->fwSession, 
        fwFindObjects->mdToken, fwFindObjects->fwToken,
        fwFindObjects->mdInstance, fwFindObjects->fwInstance);
    }
  }

  if (fwFindObjects->mdfo2) {
    if (fwFindObjects->mdfo2->Final) {
      fwFindObjects->mdFindObjects = fwFindObjects->mdfo2;
      fwFindObjects->mdfo2->Final(fwFindObjects->mdfo2, fwFindObjects,
        fwFindObjects->mdSession, fwFindObjects->fwSession, 
        fwFindObjects->mdToken, fwFindObjects->fwToken,
        fwFindObjects->mdInstance, fwFindObjects->fwInstance);
    }
  }

  nss_ZFreeIf(fwFindObjects);

#ifdef DEBUG
  (void)findObjects_remove_pointer(fwFindObjects);
#endif /* DEBUG */

  return;
}
Ejemplo n.º 5
0
/*
 * nssCKFWObject_Finalize
 *
 */
NSS_IMPLEMENT void
nssCKFWObject_Finalize
(
  NSSCKFWObject *fwObject,
  PRBool removeFromHash
)
{
  nssCKFWHash *mdObjectHash;

#ifdef NSSDEBUG
  if( CKR_OK != nssCKFWObject_verifyPointer(fwObject) ) {
    return;
  }
#endif /* NSSDEBUG */

  (void)nssCKFWMutex_Destroy(fwObject->mutex);

  if( (void *)NULL != (void *)fwObject->mdObject->Finalize ) {
    fwObject->mdObject->Finalize(fwObject->mdObject, fwObject,
      fwObject->mdSession, fwObject->fwSession, fwObject->mdToken,
      fwObject->fwToken, fwObject->mdInstance, fwObject->fwInstance);
  }

  if (removeFromHash) {
    mdObjectHash = nssCKFWToken_GetMDObjectHash(fwObject->fwToken);
    if( (nssCKFWHash *)NULL != mdObjectHash ) {
      nssCKFWHash_Remove(mdObjectHash, fwObject->mdObject);
    }
 }

  if (fwObject->fwSession) {
    nssCKFWSession_DeregisterSessionObject(fwObject->fwSession, fwObject);
  }
  nss_ZFreeIf(fwObject);

#ifdef DEBUG
  (void)object_remove_pointer(fwObject);
#endif /* DEBUG */

  return;
}
Ejemplo n.º 6
0
/*
 * 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;
}
Ejemplo n.º 7
0
/*
 * nssCKFWSlot_Create
 *
 */
NSS_IMPLEMENT NSSCKFWSlot *
nssCKFWSlot_Create(
    NSSCKFWInstance *fwInstance,
    NSSCKMDSlot *mdSlot,
    CK_SLOT_ID slotID,
    CK_RV *pError)
{
    NSSCKFWSlot *fwSlot;
    NSSCKMDInstance *mdInstance;
    NSSArena *arena;

#ifdef NSSDEBUG
    if (!pError) {
        return (NSSCKFWSlot *)NULL;
    }

    *pError = nssCKFWInstance_verifyPointer(fwInstance);
    if (CKR_OK != *pError) {
        return (NSSCKFWSlot *)NULL;
    }
#endif /* NSSDEBUG */

    mdInstance = nssCKFWInstance_GetMDInstance(fwInstance);
    if (!mdInstance) {
        *pError = CKR_GENERAL_ERROR;
        return (NSSCKFWSlot *)NULL;
    }

    arena = nssCKFWInstance_GetArena(fwInstance, pError);
    if (!arena) {
        if (CKR_OK == *pError) {
            *pError = CKR_GENERAL_ERROR;
        }
    }

    fwSlot = nss_ZNEW(arena, NSSCKFWSlot);
    if (!fwSlot) {
        *pError = CKR_HOST_MEMORY;
        return (NSSCKFWSlot *)NULL;
    }

    fwSlot->mdSlot = mdSlot;
    fwSlot->fwInstance = fwInstance;
    fwSlot->mdInstance = mdInstance;
    fwSlot->slotID = slotID;

    fwSlot->mutex = nssCKFWInstance_CreateMutex(fwInstance, arena, pError);
    if (!fwSlot->mutex) {
        if (CKR_OK == *pError) {
            *pError = CKR_GENERAL_ERROR;
        }
        (void)nss_ZFreeIf(fwSlot);
        return (NSSCKFWSlot *)NULL;
    }

    if (mdSlot->Initialize) {
        *pError = CKR_OK;
        *pError = mdSlot->Initialize(mdSlot, fwSlot, mdInstance, fwInstance);
        if (CKR_OK != *pError) {
            (void)nssCKFWMutex_Destroy(fwSlot->mutex);
            (void)nss_ZFreeIf(fwSlot);
            return (NSSCKFWSlot *)NULL;
        }
    }

#ifdef DEBUG
    *pError = slot_add_pointer(fwSlot);
    if (CKR_OK != *pError) {
        if (mdSlot->Destroy) {
            mdSlot->Destroy(mdSlot, fwSlot, mdInstance, fwInstance);
        }

        (void)nssCKFWMutex_Destroy(fwSlot->mutex);
        (void)nss_ZFreeIf(fwSlot);
        return (NSSCKFWSlot *)NULL;
    }
#endif /* DEBUG */

    return fwSlot;
}
Ejemplo n.º 8
0
/*
 * 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;
}