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