Blame nss/lib/pk11wrap/pk11cxt.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
/*
Packit 40b132
 * This file PK11Contexts which are  used in multipart hashing, 
Packit 40b132
 * encryption/decryption, and signing/verication operations.
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "seccomon.h"
Packit 40b132
#include "secmod.h"
Packit 40b132
#include "nssilock.h"
Packit 40b132
#include "secmodi.h"
Packit 40b132
#include "secmodti.h"
Packit 40b132
#include "pkcs11.h"
Packit 40b132
#include "pk11func.h"
Packit 40b132
#include "secitem.h"
Packit 40b132
#include "secoid.h" 
Packit 40b132
#include "sechash.h"
Packit 40b132
#include "secerr.h"
Packit 40b132
Packit 40b132
static const SECItem pk11_null_params = { 0 };
Packit 40b132
Packit 40b132
/**********************************************************************
Packit 40b132
 *
Packit 40b132
 *                   Now Deal with Crypto Contexts
Packit 40b132
 *
Packit 40b132
 **********************************************************************/
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * the monitors...
Packit 40b132
 */
Packit 40b132
void
Packit 40b132
PK11_EnterContextMonitor(PK11Context *cx) {
Packit 40b132
    /* if we own the session and our slot is ThreadSafe, only monitor
Packit 40b132
     * the Context */
Packit 40b132
    if ((cx->ownSession) && (cx->slot->isThreadSafe)) {
Packit 40b132
	/* Should this use monitors instead? */
Packit 40b132
	PZ_Lock(cx->sessionLock);
Packit 40b132
    } else {
Packit 40b132
	PK11_EnterSlotMonitor(cx->slot);
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
PK11_ExitContextMonitor(PK11Context *cx) {
Packit 40b132
    /* if we own the session and our slot is ThreadSafe, only monitor
Packit 40b132
     * the Context */
Packit 40b132
    if ((cx->ownSession) && (cx->slot->isThreadSafe)) {
Packit 40b132
	/* Should this use monitors instead? */
Packit 40b132
	PZ_Unlock(cx->sessionLock);
Packit 40b132
    } else {
Packit 40b132
	PK11_ExitSlotMonitor(cx->slot);
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Free up a Cipher Context
Packit 40b132
 */
Packit 40b132
void
Packit 40b132
PK11_DestroyContext(PK11Context *context, PRBool freeit)
Packit 40b132
{
Packit 40b132
    pk11_CloseSession(context->slot,context->session,context->ownSession);
Packit 40b132
    /* initialize the critical fields of the context */
Packit 40b132
    if (context->savedData != NULL ) PORT_Free(context->savedData);
Packit 40b132
    if (context->key) PK11_FreeSymKey(context->key);
Packit 40b132
    if (context->param && context->param != &pk11_null_params)
Packit 40b132
	SECITEM_FreeItem(context->param, PR_TRUE);
Packit 40b132
    if (context->sessionLock) PZ_DestroyLock(context->sessionLock);
Packit 40b132
    PK11_FreeSlot(context->slot);
Packit 40b132
    if (freeit) PORT_Free(context);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * save the current context. Allocate Space if necessary.
Packit 40b132
 */
Packit 40b132
static unsigned char *
Packit 40b132
pk11_saveContextHelper(PK11Context *context, unsigned char *buffer, 
Packit 40b132
                       unsigned long *savedLength)
Packit 40b132
{
Packit 40b132
    CK_RV crv;
Packit 40b132
Packit 40b132
    /* If buffer is NULL, this will get the length */
Packit 40b132
    crv = PK11_GETTAB(context->slot)->C_GetOperationState(context->session,
Packit 40b132
                                                          (CK_BYTE_PTR)buffer,
Packit 40b132
                                                          savedLength);
Packit 40b132
    if (!buffer || (crv == CKR_BUFFER_TOO_SMALL)) {
Packit 40b132
	/* the given buffer wasn't big enough (or was NULL), but we 
Packit 40b132
	 * have the length, so try again with a new buffer and the 
Packit 40b132
	 * correct length
Packit 40b132
	 */
Packit 40b132
	unsigned long bufLen = *savedLength;
Packit 40b132
	buffer = PORT_Alloc(bufLen);
Packit 40b132
	if (buffer == NULL) {
Packit 40b132
	    return (unsigned char *)NULL;
Packit 40b132
	}
Packit 40b132
	crv = PK11_GETTAB(context->slot)->C_GetOperationState(
Packit 40b132
	                                                  context->session,
Packit 40b132
                                                          (CK_BYTE_PTR)buffer,
Packit 40b132
                                                          savedLength);
Packit 40b132
	if (crv != CKR_OK) {
Packit 40b132
	    PORT_ZFree(buffer, bufLen);
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
	PORT_SetError( PK11_MapError(crv) );
Packit 40b132
	return (unsigned char *)NULL;
Packit 40b132
    }
Packit 40b132
    return buffer;
Packit 40b132
}
Packit 40b132
Packit 40b132
void *
Packit 40b132
pk11_saveContext(PK11Context *context, void *space, unsigned long *savedLength)
Packit 40b132
{
Packit 40b132
    return pk11_saveContextHelper(context, 
Packit 40b132
                                  (unsigned char *)space, savedLength);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * restore the current context
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
pk11_restoreContext(PK11Context *context,void *space, unsigned long savedLength)
Packit 40b132
{
Packit 40b132
    CK_RV crv;
Packit 40b132
    CK_OBJECT_HANDLE objectID = (context->key) ? context->key->objectID:
Packit 40b132
			CK_INVALID_HANDLE;
Packit 40b132
Packit 40b132
    PORT_Assert(space != NULL);
Packit 40b132
    if (space == NULL) {
Packit 40b132
	PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
    crv = PK11_GETTAB(context->slot)->C_SetOperationState(context->session,
Packit 40b132
        (CK_BYTE_PTR)space, savedLength, objectID, 0);
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
       PORT_SetError( PK11_MapError(crv));
Packit 40b132
       return SECFailure;
Packit 40b132
   }
Packit 40b132
   return SECSuccess;
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus pk11_Finalize(PK11Context *context);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Context initialization. Used by all flavors of CreateContext
Packit 40b132
 */
Packit 40b132
static SECStatus 
Packit 40b132
pk11_context_init(PK11Context *context, CK_MECHANISM *mech_info)
Packit 40b132
{
Packit 40b132
    CK_RV crv;
Packit 40b132
    PK11SymKey *symKey = context->key;
Packit 40b132
    SECStatus rv = SECSuccess;
Packit 40b132
Packit 40b132
    switch (context->operation) {
Packit 40b132
    case CKA_ENCRYPT:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_EncryptInit(context->session,
Packit 40b132
				mech_info, symKey->objectID);
Packit 40b132
	break;
Packit 40b132
    case CKA_DECRYPT:
Packit 40b132
	if (context->fortezzaHack) {
Packit 40b132
	    CK_ULONG count = 0;;
Packit 40b132
	    /* generate the IV for fortezza */
Packit 40b132
	    crv=PK11_GETTAB(context->slot)->C_EncryptInit(context->session,
Packit 40b132
				mech_info, symKey->objectID);
Packit 40b132
	    if (crv != CKR_OK) break;
Packit 40b132
	    PK11_GETTAB(context->slot)->C_EncryptFinal(context->session,
Packit 40b132
				NULL, &count);
Packit 40b132
	}
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_DecryptInit(context->session,
Packit 40b132
				mech_info, symKey->objectID);
Packit 40b132
	break;
Packit 40b132
    case CKA_SIGN:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_SignInit(context->session,
Packit 40b132
				mech_info, symKey->objectID);
Packit 40b132
	break;
Packit 40b132
    case CKA_VERIFY:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_SignInit(context->session,
Packit 40b132
				mech_info, symKey->objectID);
Packit 40b132
	break;
Packit 40b132
    case CKA_DIGEST:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_DigestInit(context->session,
Packit 40b132
				mech_info);
Packit 40b132
	break;
Packit 40b132
    default:
Packit 40b132
	crv = CKR_OPERATION_NOT_INITIALIZED;
Packit 40b132
	break;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
        PORT_SetError( PK11_MapError(crv) );
Packit 40b132
        return SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * handle session starvation case.. use our last session to multiplex
Packit 40b132
     */
Packit 40b132
    if (!context->ownSession) {
Packit 40b132
	context->savedData = pk11_saveContext(context,context->savedData,
Packit 40b132
				&context->savedLength);
Packit 40b132
	if (context->savedData == NULL) rv = SECFailure;
Packit 40b132
	/* clear out out session for others to use */
Packit 40b132
	pk11_Finalize(context);
Packit 40b132
    }
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Common Helper Function do come up with a new context.
Packit 40b132
 */
Packit 40b132
static PK11Context *pk11_CreateNewContextInSlot(CK_MECHANISM_TYPE type,
Packit 40b132
     PK11SlotInfo *slot, CK_ATTRIBUTE_TYPE operation, PK11SymKey *symKey,
Packit 40b132
							     SECItem *param)
Packit 40b132
{
Packit 40b132
    CK_MECHANISM mech_info;
Packit 40b132
    PK11Context *context;
Packit 40b132
    SECStatus rv;
Packit 40b132
	
Packit 40b132
    PORT_Assert(slot != NULL);
Packit 40b132
    if (!slot || (!symKey && ((operation != CKA_DIGEST) || 
Packit 40b132
	                      (type == CKM_SKIPJACK_CBC64)))) {
Packit 40b132
	PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
    context = (PK11Context *) PORT_Alloc(sizeof(PK11Context));
Packit 40b132
    if (context == NULL) {
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* now deal with the fortezza hack... the fortezza hack is an attempt
Packit 40b132
     * to get around the issue of the card not allowing you to do a FORTEZZA
Packit 40b132
     * LoadIV/Encrypt, which was added because such a combination could be
Packit 40b132
     * use to circumvent the key escrow system. Unfortunately SSL needs to
Packit 40b132
     * do this kind of operation, so in SSL we do a loadIV (to verify it),
Packit 40b132
     * Then GenerateIV, and through away the first 8 bytes on either side
Packit 40b132
     * of the connection.*/
Packit 40b132
    context->fortezzaHack = PR_FALSE;
Packit 40b132
    if (type == CKM_SKIPJACK_CBC64) {
Packit 40b132
	if (symKey->origin == PK11_OriginFortezzaHack) {
Packit 40b132
	    context->fortezzaHack = PR_TRUE;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* initialize the critical fields of the context */
Packit 40b132
    context->operation = operation;
Packit 40b132
    context->key = symKey ? PK11_ReferenceSymKey(symKey) : NULL;
Packit 40b132
    context->slot = PK11_ReferenceSlot(slot);
Packit 40b132
    context->session = pk11_GetNewSession(slot,&context->ownSession);
Packit 40b132
    context->cx = symKey ? symKey->cx : NULL;
Packit 40b132
    /* get our session */
Packit 40b132
    context->savedData = NULL;
Packit 40b132
Packit 40b132
    /* save the parameters so that some digesting stuff can do multiple
Packit 40b132
     * begins on a single context */
Packit 40b132
    context->type = type;
Packit 40b132
    if (param) {
Packit 40b132
	if (param->len > 0) {
Packit 40b132
	    context->param = SECITEM_DupItem(param);
Packit 40b132
	} else {
Packit 40b132
	    context->param = (SECItem *)&pk11_null_params;
Packit 40b132
	}
Packit 40b132
    } else {
Packit 40b132
	PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
	context->param = NULL;
Packit 40b132
    }
Packit 40b132
    context->init = PR_FALSE;
Packit 40b132
    context->sessionLock = PZ_NewLock(nssILockPK11cxt);
Packit 40b132
    if ((context->param == NULL) || (context->sessionLock == NULL)) {
Packit 40b132
	PK11_DestroyContext(context,PR_TRUE);
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    mech_info.mechanism = type;
Packit 40b132
    mech_info.pParameter = param->data;
Packit 40b132
    mech_info.ulParameterLen = param->len;
Packit 40b132
    PK11_EnterContextMonitor(context);
Packit 40b132
    rv = pk11_context_init(context,&mech_info);
Packit 40b132
    PK11_ExitContextMonitor(context);
Packit 40b132
Packit 40b132
    if (rv != SECSuccess) {
Packit 40b132
	PK11_DestroyContext(context,PR_TRUE);
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
    context->init = PR_TRUE;
Packit 40b132
    return context;
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * put together the various PK11_Create_Context calls used by different
Packit 40b132
 * parts of libsec.
Packit 40b132
 */
Packit 40b132
PK11Context *
Packit 40b132
__PK11_CreateContextByRawKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE type,
Packit 40b132
     PK11Origin origin, CK_ATTRIBUTE_TYPE operation, SECItem *key, 
Packit 40b132
						SECItem *param, void *wincx)
Packit 40b132
{
Packit 40b132
    PK11SymKey *symKey = NULL;
Packit 40b132
    PK11Context *context = NULL;
Packit 40b132
Packit 40b132
    /* first get a slot */
Packit 40b132
    if (slot == NULL) {
Packit 40b132
	slot = PK11_GetBestSlot(type,wincx);
Packit 40b132
	if (slot == NULL) {
Packit 40b132
	    PORT_SetError( SEC_ERROR_NO_MODULE );
Packit 40b132
	    goto loser;
Packit 40b132
	}
Packit 40b132
    } else {
Packit 40b132
	PK11_ReferenceSlot(slot);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* now import the key */
Packit 40b132
    symKey = PK11_ImportSymKey(slot, type, origin, operation,  key, wincx);
Packit 40b132
    if (symKey == NULL) goto loser;
Packit 40b132
Packit 40b132
    context = PK11_CreateContextBySymKey(type, operation, symKey, param);
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    if (symKey) {
Packit 40b132
        PK11_FreeSymKey(symKey);
Packit 40b132
    }
Packit 40b132
    if (slot) {
Packit 40b132
        PK11_FreeSlot(slot);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return context;
Packit 40b132
}
Packit 40b132
Packit 40b132
PK11Context *
Packit 40b132
PK11_CreateContextByRawKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE type,
Packit 40b132
     PK11Origin origin, CK_ATTRIBUTE_TYPE operation, SECItem *key, 
Packit 40b132
						SECItem *param, void *wincx)
Packit 40b132
{
Packit 40b132
    return __PK11_CreateContextByRawKey(slot, type, origin, operation,
Packit 40b132
                                        key, param, wincx);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Create a context from a key. We really should make sure we aren't using
Packit 40b132
 * the same key in multiple session!
Packit 40b132
 */
Packit 40b132
PK11Context *
Packit 40b132
PK11_CreateContextBySymKey(CK_MECHANISM_TYPE type,CK_ATTRIBUTE_TYPE operation,
Packit 40b132
			PK11SymKey *symKey, SECItem *param)
Packit 40b132
{
Packit 40b132
    PK11SymKey *newKey;
Packit 40b132
    PK11Context *context;
Packit 40b132
Packit 40b132
    /* if this slot doesn't support the mechanism, go to a slot that does */
Packit 40b132
    newKey = pk11_ForceSlot(symKey,type,operation);
Packit 40b132
    if (newKey == NULL) {
Packit 40b132
	PK11_ReferenceSymKey(symKey);
Packit 40b132
    } else {
Packit 40b132
	symKey = newKey;
Packit 40b132
    }
Packit 40b132
Packit 40b132
Packit 40b132
    /* Context Adopts the symKey.... */
Packit 40b132
    context = pk11_CreateNewContextInSlot(type, symKey->slot, operation, symKey,
Packit 40b132
							     param);
Packit 40b132
    PK11_FreeSymKey(symKey);
Packit 40b132
    return context;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Digest contexts don't need keys, but the do need to find a slot.
Packit 40b132
 * Macing should use PK11_CreateContextBySymKey.
Packit 40b132
 */
Packit 40b132
PK11Context *
Packit 40b132
PK11_CreateDigestContext(SECOidTag hashAlg)
Packit 40b132
{
Packit 40b132
    /* digesting has to work without authentication to the slot */
Packit 40b132
    CK_MECHANISM_TYPE type;
Packit 40b132
    PK11SlotInfo *slot;
Packit 40b132
    PK11Context *context;
Packit 40b132
    SECItem param;
Packit 40b132
Packit 40b132
    type = PK11_AlgtagToMechanism(hashAlg);
Packit 40b132
    slot = PK11_GetBestSlot(type, NULL);
Packit 40b132
    if (slot == NULL) {
Packit 40b132
	PORT_SetError( SEC_ERROR_NO_MODULE );
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* maybe should really be PK11_GenerateNewParam?? */
Packit 40b132
    param.data = NULL;
Packit 40b132
    param.len = 0;
Packit 40b132
    param.type = 0;
Packit 40b132
Packit 40b132
    context = pk11_CreateNewContextInSlot(type, slot, CKA_DIGEST, NULL, ¶m;;
Packit 40b132
    PK11_FreeSlot(slot);
Packit 40b132
    return context;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * create a new context which is the clone of the state of old context.
Packit 40b132
 */
Packit 40b132
PK11Context * PK11_CloneContext(PK11Context *old)
Packit 40b132
{
Packit 40b132
     PK11Context *newcx;
Packit 40b132
     PRBool needFree = PR_FALSE;
Packit 40b132
     SECStatus rv = SECSuccess;
Packit 40b132
     void *data;
Packit 40b132
     unsigned long len;
Packit 40b132
Packit 40b132
     newcx = pk11_CreateNewContextInSlot(old->type, old->slot, old->operation,
Packit 40b132
						old->key, old->param);
Packit 40b132
     if (newcx == NULL) return NULL;
Packit 40b132
Packit 40b132
     /* now clone the save state. First we need to find the save state
Packit 40b132
      * of the old session. If the old context owns it's session,
Packit 40b132
      * the state needs to be saved, otherwise the state is in saveData. */
Packit 40b132
     if (old->ownSession) {
Packit 40b132
        PK11_EnterContextMonitor(old);
Packit 40b132
	data=pk11_saveContext(old,NULL,&len;;
Packit 40b132
        PK11_ExitContextMonitor(old);
Packit 40b132
	needFree = PR_TRUE;
Packit 40b132
     } else {
Packit 40b132
	data = old->savedData;
Packit 40b132
	len = old->savedLength;
Packit 40b132
     }
Packit 40b132
Packit 40b132
     if (data == NULL) {
Packit 40b132
	PK11_DestroyContext(newcx,PR_TRUE);
Packit 40b132
	return NULL;
Packit 40b132
     }
Packit 40b132
Packit 40b132
     /* now copy that state into our new context. Again we have different
Packit 40b132
      * work if the new context owns it's own session. If it does, we
Packit 40b132
      * restore the state gathered above. If it doesn't, we copy the
Packit 40b132
      * saveData pointer... */
Packit 40b132
     if (newcx->ownSession) {
Packit 40b132
        PK11_EnterContextMonitor(newcx);
Packit 40b132
	rv = pk11_restoreContext(newcx,data,len);
Packit 40b132
        PK11_ExitContextMonitor(newcx);
Packit 40b132
     } else {
Packit 40b132
	PORT_Assert(newcx->savedData != NULL);
Packit 40b132
	if ((newcx->savedData == NULL) || (newcx->savedLength < len)) {
Packit 40b132
	    PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
Packit 40b132
	    rv = SECFailure;
Packit 40b132
	} else {
Packit 40b132
	    PORT_Memcpy(newcx->savedData,data,len);
Packit 40b132
	    newcx->savedLength = len;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (needFree) PORT_Free(data);
Packit 40b132
Packit 40b132
    if (rv != SECSuccess) {
Packit 40b132
	PK11_DestroyContext(newcx,PR_TRUE);
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
    return newcx;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * save the current context state into a variable. Required to make FORTEZZA
Packit 40b132
 * work.
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
PK11_SaveContext(PK11Context *cx,unsigned char *save,int *len, int saveLength)
Packit 40b132
{
Packit 40b132
    unsigned char * data = NULL;
Packit 40b132
    CK_ULONG length = saveLength;
Packit 40b132
Packit 40b132
    if (cx->ownSession) {
Packit 40b132
        PK11_EnterContextMonitor(cx);
Packit 40b132
	data = pk11_saveContextHelper(cx, save, &length);
Packit 40b132
        PK11_ExitContextMonitor(cx);
Packit 40b132
	if (data) *len = length;
Packit 40b132
    } else if ((unsigned) saveLength >= cx->savedLength) {
Packit 40b132
	data = (unsigned char*)cx->savedData;
Packit 40b132
	if (cx->savedData) {
Packit 40b132
	    PORT_Memcpy(save,cx->savedData,cx->savedLength);
Packit 40b132
	}
Packit 40b132
	*len = cx->savedLength;
Packit 40b132
    }
Packit 40b132
    if (data != NULL) {
Packit 40b132
	if (cx->ownSession) {
Packit 40b132
	    PORT_ZFree(data, length);
Packit 40b132
	}
Packit 40b132
	return SECSuccess;
Packit 40b132
    } else {
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
/* same as above, but may allocate the return buffer. */
Packit 40b132
unsigned char *
Packit 40b132
PK11_SaveContextAlloc(PK11Context *cx,
Packit 40b132
                      unsigned char *preAllocBuf, unsigned int pabLen,
Packit 40b132
                      unsigned int *stateLen)
Packit 40b132
{
Packit 40b132
    unsigned char *stateBuf = NULL;
Packit 40b132
    unsigned long length = (unsigned long)pabLen;
Packit 40b132
Packit 40b132
    if (cx->ownSession) {
Packit 40b132
        PK11_EnterContextMonitor(cx);
Packit 40b132
	stateBuf = pk11_saveContextHelper(cx, preAllocBuf, &length);
Packit 40b132
        PK11_ExitContextMonitor(cx);
Packit 40b132
	*stateLen = (stateBuf != NULL) ? length : 0;
Packit 40b132
    } else {
Packit 40b132
	if (pabLen < cx->savedLength) {
Packit 40b132
	    stateBuf = (unsigned char *)PORT_Alloc(cx->savedLength);
Packit 40b132
	    if (!stateBuf) {
Packit 40b132
		return (unsigned char *)NULL;
Packit 40b132
	    }
Packit 40b132
	} else {
Packit 40b132
	    stateBuf = preAllocBuf;
Packit 40b132
	}
Packit 40b132
	if (cx->savedData) {
Packit 40b132
	    PORT_Memcpy(stateBuf, cx->savedData, cx->savedLength);
Packit 40b132
	}
Packit 40b132
	*stateLen = cx->savedLength;
Packit 40b132
    }
Packit 40b132
    return stateBuf;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * restore the context state into a new running context. Also required for
Packit 40b132
 * FORTEZZA .
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
PK11_RestoreContext(PK11Context *cx,unsigned char *save,int len)
Packit 40b132
{
Packit 40b132
    SECStatus rv = SECSuccess;
Packit 40b132
    if (cx->ownSession) {
Packit 40b132
        PK11_EnterContextMonitor(cx);
Packit 40b132
	pk11_Finalize(cx);
Packit 40b132
	rv = pk11_restoreContext(cx,save,len);
Packit 40b132
        PK11_ExitContextMonitor(cx);
Packit 40b132
    } else {
Packit 40b132
	PORT_Assert(cx->savedData != NULL);
Packit 40b132
	if ((cx->savedData == NULL) || (cx->savedLength < (unsigned) len)) {
Packit 40b132
	    PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
Packit 40b132
	    rv = SECFailure;
Packit 40b132
	} else {
Packit 40b132
	    PORT_Memcpy(cx->savedData,save,len);
Packit 40b132
	    cx->savedLength = len;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * This is  to get FIPS compliance until we can convert
Packit 40b132
 * libjar to use PK11_ hashing functions. It returns PR_FALSE
Packit 40b132
 * if we can't get a PK11 Context.
Packit 40b132
 */
Packit 40b132
PRBool
Packit 40b132
PK11_HashOK(SECOidTag algID) {
Packit 40b132
    PK11Context *cx;
Packit 40b132
Packit 40b132
    cx = PK11_CreateDigestContext(algID);
Packit 40b132
    if (cx == NULL) return PR_FALSE;
Packit 40b132
    PK11_DestroyContext(cx, PR_TRUE);
Packit 40b132
    return PR_TRUE;
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * start a new digesting or Mac'ing operation on this context
Packit 40b132
 */
Packit 40b132
SECStatus PK11_DigestBegin(PK11Context *cx)
Packit 40b132
{
Packit 40b132
    CK_MECHANISM mech_info;
Packit 40b132
    SECStatus rv;
Packit 40b132
Packit 40b132
    if (cx->init == PR_TRUE) {
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * make sure the old context is clear first
Packit 40b132
     */
Packit 40b132
    PK11_EnterContextMonitor(cx);
Packit 40b132
    pk11_Finalize(cx);
Packit 40b132
Packit 40b132
    mech_info.mechanism = cx->type;
Packit 40b132
    mech_info.pParameter = cx->param->data;
Packit 40b132
    mech_info.ulParameterLen = cx->param->len;
Packit 40b132
    rv = pk11_context_init(cx,&mech_info);
Packit 40b132
    PK11_ExitContextMonitor(cx);
Packit 40b132
Packit 40b132
    if (rv != SECSuccess) {
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
    cx->init = PR_TRUE;
Packit 40b132
    return SECSuccess;
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
PK11_HashBuf(SECOidTag hashAlg, unsigned char *out, const unsigned char *in,
Packit 40b132
								PRInt32 len) {
Packit 40b132
    PK11Context *context;
Packit 40b132
    unsigned int max_length;
Packit 40b132
    unsigned int out_length;
Packit 40b132
    SECStatus rv;
Packit 40b132
Packit 40b132
    /* len will be passed to PK11_DigestOp as unsigned. */
Packit 40b132
    if (len < 0) {
Packit 40b132
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
        return SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    context = PK11_CreateDigestContext(hashAlg);
Packit 40b132
    if (context == NULL) return SECFailure;
Packit 40b132
Packit 40b132
    rv = PK11_DigestBegin(context);
Packit 40b132
    if (rv != SECSuccess) {
Packit 40b132
	PK11_DestroyContext(context, PR_TRUE);
Packit 40b132
	return rv;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    rv = PK11_DigestOp(context, in, len);
Packit 40b132
    if (rv != SECSuccess) {
Packit 40b132
	PK11_DestroyContext(context, PR_TRUE);
Packit 40b132
	return rv;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* XXX This really should have been an argument to this function! */
Packit 40b132
    max_length = HASH_ResultLenByOidTag(hashAlg);
Packit 40b132
    PORT_Assert(max_length);
Packit 40b132
    if (!max_length)
Packit 40b132
    	max_length = HASH_LENGTH_MAX;
Packit 40b132
Packit 40b132
    rv = PK11_DigestFinal(context,out,&out_length,max_length);
Packit 40b132
    PK11_DestroyContext(context, PR_TRUE);
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * execute a bulk encryption operation
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
PK11_CipherOp(PK11Context *context, unsigned char * out, int *outlen, 
Packit 40b132
				int maxout, const unsigned char *in, int inlen)
Packit 40b132
{
Packit 40b132
    CK_RV crv = CKR_OK;
Packit 40b132
    CK_ULONG length = maxout;
Packit 40b132
    CK_ULONG offset =0;
Packit 40b132
    SECStatus rv = SECSuccess;
Packit 40b132
    unsigned char *saveOut = out;
Packit 40b132
    unsigned char *allocOut = NULL;
Packit 40b132
Packit 40b132
    /* if we ran out of session, we need to restore our previously stored
Packit 40b132
     * state.
Packit 40b132
     */
Packit 40b132
    PK11_EnterContextMonitor(context);
Packit 40b132
    if (!context->ownSession) {
Packit 40b132
        rv = pk11_restoreContext(context,context->savedData,
Packit 40b132
							context->savedLength);
Packit 40b132
	if (rv != SECSuccess) {
Packit 40b132
	    PK11_ExitContextMonitor(context);
Packit 40b132
	    return rv;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * The fortezza hack is to send 8 extra bytes on the first encrypted and
Packit 40b132
     * lose them on the first decrypt.
Packit 40b132
     */
Packit 40b132
    if (context->fortezzaHack) {
Packit 40b132
	unsigned char random[8];
Packit 40b132
	if (context->operation == CKA_ENCRYPT) {
Packit 40b132
	    PK11_ExitContextMonitor(context);
Packit 40b132
	    rv = PK11_GenerateRandom(random,sizeof(random));
Packit 40b132
    	    PK11_EnterContextMonitor(context);
Packit 40b132
Packit 40b132
	    /* since we are offseting the output, we can't encrypt back into
Packit 40b132
	     * the same buffer... allocate a temporary buffer just for this
Packit 40b132
	     * call. */
Packit 40b132
	    allocOut = out = (unsigned char*)PORT_Alloc(maxout);
Packit 40b132
	    if (out == NULL) {
Packit 40b132
		PK11_ExitContextMonitor(context);
Packit 40b132
		return SECFailure;
Packit 40b132
	    }
Packit 40b132
	    crv = PK11_GETTAB(context->slot)->C_EncryptUpdate(context->session,
Packit 40b132
		random,sizeof(random),out,&length);
Packit 40b132
Packit 40b132
	    out += length;
Packit 40b132
	    maxout -= length;
Packit 40b132
	    offset = length;
Packit 40b132
	} else if (context->operation == CKA_DECRYPT) {
Packit 40b132
	    length = sizeof(random);
Packit 40b132
	    crv = PK11_GETTAB(context->slot)->C_DecryptUpdate(context->session,
Packit 40b132
		(CK_BYTE_PTR)in,sizeof(random),random,&length);
Packit 40b132
	    inlen -= length;
Packit 40b132
	    in += length;
Packit 40b132
	    context->fortezzaHack = PR_FALSE;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    switch (context->operation) {
Packit 40b132
    case CKA_ENCRYPT:
Packit 40b132
	length = maxout;
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_EncryptUpdate(context->session,
Packit 40b132
							(CK_BYTE_PTR)in, inlen,
Packit 40b132
							out, &length);
Packit 40b132
	length += offset;
Packit 40b132
	break;
Packit 40b132
    case CKA_DECRYPT:
Packit 40b132
	length = maxout;
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_DecryptUpdate(context->session,
Packit 40b132
							(CK_BYTE_PTR)in, inlen,
Packit 40b132
							out, &length);
Packit 40b132
	break;
Packit 40b132
    default:
Packit 40b132
	crv = CKR_OPERATION_NOT_INITIALIZED;
Packit 40b132
	break;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
        PORT_SetError( PK11_MapError(crv) );
Packit 40b132
	*outlen = 0;
Packit 40b132
        rv = SECFailure;
Packit 40b132
    } else {
Packit 40b132
    	*outlen = length;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (context->fortezzaHack) {
Packit 40b132
	if (context->operation == CKA_ENCRYPT) {
Packit 40b132
	    PORT_Assert(allocOut);
Packit 40b132
	    PORT_Memcpy(saveOut, allocOut, length);
Packit 40b132
	    PORT_Free(allocOut);
Packit 40b132
	}
Packit 40b132
	context->fortezzaHack = PR_FALSE;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * handle session starvation case.. use our last session to multiplex
Packit 40b132
     */
Packit 40b132
    if (!context->ownSession) {
Packit 40b132
	context->savedData = pk11_saveContext(context,context->savedData,
Packit 40b132
				&context->savedLength);
Packit 40b132
	if (context->savedData == NULL) rv = SECFailure;
Packit 40b132
	
Packit 40b132
	/* clear out out session for others to use */
Packit 40b132
	pk11_Finalize(context);
Packit 40b132
    }
Packit 40b132
    PK11_ExitContextMonitor(context);
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * execute a digest/signature operation
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
PK11_DigestOp(PK11Context *context, const unsigned char * in, unsigned inLen) 
Packit 40b132
{
Packit 40b132
    CK_RV crv = CKR_OK;
Packit 40b132
    SECStatus rv = SECSuccess;
Packit 40b132
Packit 40b132
    if (inLen == 0) {
Packit 40b132
        return SECSuccess;
Packit 40b132
    }
Packit 40b132
    if (!in) {
Packit 40b132
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
        return SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* if we ran out of session, we need to restore our previously stored
Packit 40b132
     * state.
Packit 40b132
     */
Packit 40b132
    context->init = PR_FALSE;
Packit 40b132
    PK11_EnterContextMonitor(context);
Packit 40b132
    if (!context->ownSession) {
Packit 40b132
        rv = pk11_restoreContext(context,context->savedData,
Packit 40b132
							context->savedLength);
Packit 40b132
	if (rv != SECSuccess) {
Packit 40b132
	    PK11_ExitContextMonitor(context);
Packit 40b132
	    return rv;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    switch (context->operation) {
Packit 40b132
    /* also for MAC'ing */
Packit 40b132
    case CKA_SIGN:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_SignUpdate(context->session,
Packit 40b132
						     (unsigned char *)in, 
Packit 40b132
						     inLen);
Packit 40b132
	break;
Packit 40b132
    case CKA_VERIFY:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_VerifyUpdate(context->session,
Packit 40b132
						       (unsigned char *)in, 
Packit 40b132
						       inLen);
Packit 40b132
	break;
Packit 40b132
    case CKA_DIGEST:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_DigestUpdate(context->session,
Packit 40b132
						       (unsigned char *)in, 
Packit 40b132
						       inLen);
Packit 40b132
	break;
Packit 40b132
    default:
Packit 40b132
	crv = CKR_OPERATION_NOT_INITIALIZED;
Packit 40b132
	break;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
        PORT_SetError( PK11_MapError(crv) );
Packit 40b132
        rv = SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * handle session starvation case.. use our last session to multiplex
Packit 40b132
     */
Packit 40b132
    if (!context->ownSession) {
Packit 40b132
	context->savedData = pk11_saveContext(context,context->savedData,
Packit 40b132
				&context->savedLength);
Packit 40b132
	if (context->savedData == NULL) rv = SECFailure;
Packit 40b132
	
Packit 40b132
	/* clear out out session for others to use */
Packit 40b132
	pk11_Finalize(context);
Packit 40b132
    }
Packit 40b132
    PK11_ExitContextMonitor(context);
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Digest a key if possible./
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
PK11_DigestKey(PK11Context *context, PK11SymKey *key)
Packit 40b132
{
Packit 40b132
    CK_RV crv = CKR_OK;
Packit 40b132
    SECStatus rv = SECSuccess;
Packit 40b132
    PK11SymKey *newKey = NULL;
Packit 40b132
Packit 40b132
    if (!context || !key) {
Packit 40b132
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
        return SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* if we ran out of session, we need to restore our previously stored
Packit 40b132
     * state.
Packit 40b132
     */
Packit 40b132
    if (context->slot != key->slot) {
Packit 40b132
	newKey = pk11_CopyToSlot(context->slot,CKM_SSL3_SHA1_MAC,CKA_SIGN,key);
Packit 40b132
    } else {
Packit 40b132
	newKey = PK11_ReferenceSymKey(key);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    context->init = PR_FALSE;
Packit 40b132
    PK11_EnterContextMonitor(context);
Packit 40b132
    if (!context->ownSession) {
Packit 40b132
        rv = pk11_restoreContext(context,context->savedData,
Packit 40b132
							context->savedLength);
Packit 40b132
	if (rv != SECSuccess) {
Packit 40b132
	    PK11_ExitContextMonitor(context);
Packit 40b132
            PK11_FreeSymKey(newKey);
Packit 40b132
	    return rv;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
Packit 40b132
    if (newKey == NULL) {
Packit 40b132
	crv = CKR_KEY_TYPE_INCONSISTENT;
Packit 40b132
	if (key->data.data) {
Packit 40b132
	    crv=PK11_GETTAB(context->slot)->C_DigestUpdate(context->session,
Packit 40b132
					key->data.data,key->data.len);
Packit 40b132
	}
Packit 40b132
    } else {
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_DigestKey(context->session,
Packit 40b132
							newKey->objectID);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
        PORT_SetError( PK11_MapError(crv) );
Packit 40b132
        rv = SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * handle session starvation case.. use our last session to multiplex
Packit 40b132
     */
Packit 40b132
    if (!context->ownSession) {
Packit 40b132
	context->savedData = pk11_saveContext(context,context->savedData,
Packit 40b132
				&context->savedLength);
Packit 40b132
	if (context->savedData == NULL) rv = SECFailure;
Packit 40b132
	
Packit 40b132
	/* clear out out session for others to use */
Packit 40b132
	pk11_Finalize(context);
Packit 40b132
    }
Packit 40b132
    PK11_ExitContextMonitor(context);
Packit 40b132
    if (newKey) PK11_FreeSymKey(newKey);
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * externally callable version of the lowercase pk11_finalize().
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
PK11_Finalize(PK11Context *context) {
Packit 40b132
    SECStatus rv;
Packit 40b132
Packit 40b132
    PK11_EnterContextMonitor(context);
Packit 40b132
    rv = pk11_Finalize(context);
Packit 40b132
    PK11_ExitContextMonitor(context);
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * clean up a cipher operation, so the session can be used by
Packit 40b132
 * someone new.
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
pk11_Finalize(PK11Context *context)
Packit 40b132
{
Packit 40b132
    CK_ULONG count = 0;
Packit 40b132
    CK_RV crv;
Packit 40b132
    unsigned char stackBuf[256];
Packit 40b132
    unsigned char *buffer = NULL;
Packit 40b132
Packit 40b132
    if (!context->ownSession) {
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
finalize:
Packit 40b132
    switch (context->operation) {
Packit 40b132
    case CKA_ENCRYPT:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_EncryptFinal(context->session,
Packit 40b132
	                                               buffer, &count);
Packit 40b132
	break;
Packit 40b132
    case CKA_DECRYPT:
Packit 40b132
	crv = PK11_GETTAB(context->slot)->C_DecryptFinal(context->session,
Packit 40b132
	                                                 buffer, &count);
Packit 40b132
	break;
Packit 40b132
    case CKA_SIGN:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_SignFinal(context->session,
Packit 40b132
	                                            buffer, &count);
Packit 40b132
	break;
Packit 40b132
    case CKA_VERIFY:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_VerifyFinal(context->session,
Packit 40b132
	                                              buffer, count);
Packit 40b132
	break;
Packit 40b132
    case CKA_DIGEST:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_DigestFinal(context->session,
Packit 40b132
	                                              buffer, &count);
Packit 40b132
	break;
Packit 40b132
    default:
Packit 40b132
	crv = CKR_OPERATION_NOT_INITIALIZED;
Packit 40b132
	break;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
	if (buffer != stackBuf) {
Packit 40b132
	    PORT_Free(buffer);
Packit 40b132
	}
Packit 40b132
	if (crv == CKR_OPERATION_NOT_INITIALIZED) {
Packit 40b132
	    /* if there's no operation, it is finalized */
Packit 40b132
	    return SECSuccess;
Packit 40b132
	}
Packit 40b132
        PORT_SetError( PK11_MapError(crv) );
Packit 40b132
        return SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* try to finalize the session with a buffer */
Packit 40b132
    if (buffer == NULL) { 
Packit 40b132
	if (count <= sizeof stackBuf) {
Packit 40b132
	    buffer = stackBuf;
Packit 40b132
	} else {
Packit 40b132
	    buffer = PORT_Alloc(count);
Packit 40b132
	    if (buffer == NULL) {
Packit 40b132
		PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
		return SECFailure;
Packit 40b132
	    }
Packit 40b132
	}
Packit 40b132
	goto finalize;
Packit 40b132
    }
Packit 40b132
    if (buffer != stackBuf) {
Packit 40b132
	PORT_Free(buffer);
Packit 40b132
    }
Packit 40b132
    return SECSuccess;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 *  Return the final digested or signed data...
Packit 40b132
 *  this routine can either take pre initialized data, or allocate data
Packit 40b132
 *  either out of an arena or out of the standard heap.
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
PK11_DigestFinal(PK11Context *context,unsigned char *data, 
Packit 40b132
			unsigned int *outLen, unsigned int length)
Packit 40b132
{
Packit 40b132
    CK_ULONG len;
Packit 40b132
    CK_RV crv;
Packit 40b132
    SECStatus rv;
Packit 40b132
Packit 40b132
Packit 40b132
    /* if we ran out of session, we need to restore our previously stored
Packit 40b132
     * state.
Packit 40b132
     */
Packit 40b132
    PK11_EnterContextMonitor(context);
Packit 40b132
    if (!context->ownSession) {
Packit 40b132
        rv = pk11_restoreContext(context,context->savedData,
Packit 40b132
							context->savedLength);
Packit 40b132
	if (rv != SECSuccess) {
Packit 40b132
	    PK11_ExitContextMonitor(context);
Packit 40b132
	    return rv;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    len = length;
Packit 40b132
    switch (context->operation) {
Packit 40b132
    case CKA_SIGN:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_SignFinal(context->session,
Packit 40b132
				data,&len;;
Packit 40b132
	break;
Packit 40b132
    case CKA_VERIFY:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_VerifyFinal(context->session,
Packit 40b132
				data,len);
Packit 40b132
	break;
Packit 40b132
    case CKA_DIGEST:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_DigestFinal(context->session,
Packit 40b132
				data,&len;;
Packit 40b132
	break;
Packit 40b132
    case CKA_ENCRYPT:
Packit 40b132
	crv=PK11_GETTAB(context->slot)->C_EncryptFinal(context->session,
Packit 40b132
				data, &len;;
Packit 40b132
	break;
Packit 40b132
    case CKA_DECRYPT:
Packit 40b132
	crv = PK11_GETTAB(context->slot)->C_DecryptFinal(context->session,
Packit 40b132
				data, &len;;
Packit 40b132
	break;
Packit 40b132
    default:
Packit 40b132
	crv = CKR_OPERATION_NOT_INITIALIZED;
Packit 40b132
	break;
Packit 40b132
    }
Packit 40b132
    PK11_ExitContextMonitor(context);
Packit 40b132
Packit 40b132
    *outLen = (unsigned int) len;
Packit 40b132
    context->init = PR_FALSE; /* allow Begin to start up again */
Packit 40b132
Packit 40b132
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
        PORT_SetError( PK11_MapError(crv) );
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
    return SECSuccess;
Packit 40b132
}
Packit 40b132