andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: /* andre@0: * This file implements the Symkey wrapper and the PKCS context andre@0: * Interfaces. andre@0: */ andre@0: andre@0: #include "seccomon.h" andre@0: #include "secmod.h" andre@0: #include "nssilock.h" andre@0: #include "secmodi.h" andre@0: #include "secmodti.h" andre@0: #include "pkcs11.h" andre@0: #include "pk11func.h" andre@0: #include "secitem.h" andre@0: #include "secoid.h" andre@0: #include "secerr.h" andre@0: #include "hasht.h" andre@0: andre@0: static void andre@0: pk11_EnterKeyMonitor(PK11SymKey *symKey) { andre@0: if (!symKey->sessionOwner || !(symKey->slot->isThreadSafe)) andre@0: PK11_EnterSlotMonitor(symKey->slot); andre@0: } andre@0: andre@0: static void andre@0: pk11_ExitKeyMonitor(PK11SymKey *symKey) { andre@0: if (!symKey->sessionOwner || !(symKey->slot->isThreadSafe)) andre@0: PK11_ExitSlotMonitor(symKey->slot); andre@0: } andre@0: andre@0: /* andre@0: * pk11_getKeyFromList returns a symKey that has a session (if needSession andre@0: * was specified), or explicitly does not have a session (if needSession andre@0: * was not specified). andre@0: */ andre@0: static PK11SymKey * andre@0: pk11_getKeyFromList(PK11SlotInfo *slot, PRBool needSession) { andre@0: PK11SymKey *symKey = NULL; andre@0: andre@0: PZ_Lock(slot->freeListLock); andre@0: /* own session list are symkeys with sessions that the symkey owns. andre@0: * 'most' symkeys will own their own session. */ andre@0: if (needSession) { andre@0: if (slot->freeSymKeysWithSessionHead) { andre@0: symKey = slot->freeSymKeysWithSessionHead; andre@0: slot->freeSymKeysWithSessionHead = symKey->next; andre@0: slot->keyCount--; andre@0: } andre@0: } andre@0: /* if we don't need a symkey with its own session, or we couldn't find andre@0: * one on the owner list, get one from the non-owner free list. */ andre@0: if (!symKey) { andre@0: if (slot->freeSymKeysHead) { andre@0: symKey = slot->freeSymKeysHead; andre@0: slot->freeSymKeysHead = symKey->next; andre@0: slot->keyCount--; andre@0: } andre@0: } andre@0: PZ_Unlock(slot->freeListLock); andre@0: if (symKey) { andre@0: symKey->next = NULL; andre@0: if (!needSession) { andre@0: return symKey; andre@0: } andre@0: /* if we are getting an owner key, make sure we have a valid session. andre@0: * session could be invalid if the token has been removed or because andre@0: * we got it from the non-owner free list */ andre@0: if ((symKey->series != slot->series) || andre@0: (symKey->session == CK_INVALID_SESSION)) { andre@0: symKey->session = pk11_GetNewSession(slot, &symKey->sessionOwner); andre@0: } andre@0: PORT_Assert(symKey->session != CK_INVALID_SESSION); andre@0: if (symKey->session != CK_INVALID_SESSION) andre@0: return symKey; andre@0: PK11_FreeSymKey(symKey); andre@0: /* if we are here, we need a session, but couldn't get one, it's andre@0: * unlikely we pk11_GetNewSession will succeed if we call it a second andre@0: * time. */ andre@0: return NULL; andre@0: } andre@0: andre@0: symKey = PORT_New(PK11SymKey); andre@0: if (symKey == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: symKey->next = NULL; andre@0: if (needSession) { andre@0: symKey->session = pk11_GetNewSession(slot,&symKey->sessionOwner); andre@0: PORT_Assert(symKey->session != CK_INVALID_SESSION); andre@0: if (symKey->session == CK_INVALID_SESSION) { andre@0: PK11_FreeSymKey(symKey); andre@0: symKey = NULL; andre@0: } andre@0: } else { andre@0: symKey->session = CK_INVALID_SESSION; andre@0: } andre@0: return symKey; andre@0: } andre@0: andre@0: /* Caller MUST hold slot->freeListLock (or ref count == 0?) !! */ andre@0: void andre@0: PK11_CleanKeyList(PK11SlotInfo *slot) andre@0: { andre@0: PK11SymKey *symKey = NULL; andre@0: andre@0: while (slot->freeSymKeysWithSessionHead) { andre@0: symKey = slot->freeSymKeysWithSessionHead; andre@0: slot->freeSymKeysWithSessionHead = symKey->next; andre@0: pk11_CloseSession(slot, symKey->session, symKey->sessionOwner); andre@0: PORT_Free(symKey); andre@0: } andre@0: while (slot->freeSymKeysHead) { andre@0: symKey = slot->freeSymKeysHead; andre@0: slot->freeSymKeysHead = symKey->next; andre@0: pk11_CloseSession(slot, symKey->session, symKey->sessionOwner); andre@0: PORT_Free(symKey); andre@0: } andre@0: return; andre@0: } andre@0: andre@0: /* andre@0: * create a symetric key: andre@0: * Slot is the slot to create the key in. andre@0: * type is the mechanism type andre@0: * owner is does this symKey structure own it's object handle (rare andre@0: * that this is false). andre@0: * needSession means the returned symKey will return with a valid session andre@0: * allocated already. andre@0: */ andre@0: static PK11SymKey * andre@0: pk11_CreateSymKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, andre@0: PRBool owner, PRBool needSession, void *wincx) andre@0: { andre@0: andre@0: PK11SymKey *symKey = pk11_getKeyFromList(slot, needSession); andre@0: andre@0: if (symKey == NULL) { andre@0: return NULL; andre@0: } andre@0: /* if needSession was specified, make sure we have a valid session. andre@0: * callers which specify needSession as false should do their own andre@0: * check of the session before returning the symKey */ andre@0: if (needSession && symKey->session == CK_INVALID_SESSION) { andre@0: PK11_FreeSymKey(symKey); andre@0: PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); andre@0: return NULL; andre@0: } andre@0: andre@0: symKey->type = type; andre@0: symKey->data.type = siBuffer; andre@0: symKey->data.data = NULL; andre@0: symKey->data.len = 0; andre@0: symKey->owner = owner; andre@0: symKey->objectID = CK_INVALID_HANDLE; andre@0: symKey->slot = slot; andre@0: symKey->series = slot->series; andre@0: symKey->cx = wincx; andre@0: symKey->size = 0; andre@0: symKey->refCount = 1; andre@0: symKey->origin = PK11_OriginNULL; andre@0: symKey->parent = NULL; andre@0: symKey->freeFunc = NULL; andre@0: symKey->userData = NULL; andre@0: PK11_ReferenceSlot(slot); andre@0: return symKey; andre@0: } andre@0: andre@0: /* andre@0: * destroy a symetric key andre@0: */ andre@0: void andre@0: PK11_FreeSymKey(PK11SymKey *symKey) andre@0: { andre@0: PK11SlotInfo *slot; andre@0: PRBool freeit = PR_TRUE; andre@0: andre@0: if (PR_ATOMIC_DECREMENT(&symKey->refCount) == 0) { andre@0: PK11SymKey *parent = symKey->parent; andre@0: andre@0: symKey->parent = NULL; andre@0: if ((symKey->owner) && symKey->objectID != CK_INVALID_HANDLE) { andre@0: pk11_EnterKeyMonitor(symKey); andre@0: (void) PK11_GETTAB(symKey->slot)-> andre@0: C_DestroyObject(symKey->session, symKey->objectID); andre@0: pk11_ExitKeyMonitor(symKey); andre@0: } andre@0: if (symKey->data.data) { andre@0: PORT_Memset(symKey->data.data, 0, symKey->data.len); andre@0: PORT_Free(symKey->data.data); andre@0: } andre@0: /* free any existing data */ andre@0: if (symKey->userData && symKey->freeFunc) { andre@0: (*symKey->freeFunc)(symKey->userData); andre@0: } andre@0: slot = symKey->slot; andre@0: PZ_Lock(slot->freeListLock); andre@0: if (slot->keyCount < slot->maxKeyCount) { andre@0: /* andre@0: * freeSymkeysWithSessionHead contain a list of reusable andre@0: * SymKey structures with valid sessions. andre@0: * sessionOwner must be true. andre@0: * session must be valid. andre@0: * freeSymKeysHead contain a list of SymKey structures without andre@0: * valid session. andre@0: * session must be CK_INVALID_SESSION. andre@0: * though sessionOwner is false, callers should not depend on andre@0: * this fact. andre@0: */ andre@0: if (symKey->sessionOwner) { andre@0: PORT_Assert (symKey->session != CK_INVALID_SESSION); andre@0: symKey->next = slot->freeSymKeysWithSessionHead; andre@0: slot->freeSymKeysWithSessionHead = symKey; andre@0: } else { andre@0: symKey->session = CK_INVALID_SESSION; andre@0: symKey->next = slot->freeSymKeysHead; andre@0: slot->freeSymKeysHead = symKey; andre@0: } andre@0: slot->keyCount++; andre@0: symKey->slot = NULL; andre@0: freeit = PR_FALSE; andre@0: } andre@0: PZ_Unlock(slot->freeListLock); andre@0: if (freeit) { andre@0: pk11_CloseSession(symKey->slot, symKey->session, andre@0: symKey->sessionOwner); andre@0: PORT_Free(symKey); andre@0: } andre@0: PK11_FreeSlot(slot); andre@0: andre@0: if (parent) { andre@0: PK11_FreeSymKey(parent); andre@0: } andre@0: } andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_ReferenceSymKey(PK11SymKey *symKey) andre@0: { andre@0: PR_ATOMIC_INCREMENT(&symKey->refCount); andre@0: return symKey; andre@0: } andre@0: andre@0: /* andre@0: * Accessors andre@0: */ andre@0: CK_MECHANISM_TYPE andre@0: PK11_GetMechanism(PK11SymKey *symKey) andre@0: { andre@0: return symKey->type; andre@0: } andre@0: andre@0: /* andre@0: * return the slot associated with a symetric key andre@0: */ andre@0: PK11SlotInfo * andre@0: PK11_GetSlotFromKey(PK11SymKey *symKey) andre@0: { andre@0: return PK11_ReferenceSlot(symKey->slot); andre@0: } andre@0: andre@0: CK_KEY_TYPE PK11_GetSymKeyType(PK11SymKey *symKey) andre@0: { andre@0: return PK11_GetKeyType(symKey->type,symKey->size); andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_GetNextSymKey(PK11SymKey *symKey) andre@0: { andre@0: return symKey ? symKey->next : NULL; andre@0: } andre@0: andre@0: char * andre@0: PK11_GetSymKeyNickname(PK11SymKey *symKey) andre@0: { andre@0: return PK11_GetObjectNickname(symKey->slot,symKey->objectID); andre@0: } andre@0: andre@0: SECStatus andre@0: PK11_SetSymKeyNickname(PK11SymKey *symKey, const char *nickname) andre@0: { andre@0: return PK11_SetObjectNickname(symKey->slot,symKey->objectID,nickname); andre@0: } andre@0: andre@0: void * andre@0: PK11_GetSymKeyUserData(PK11SymKey *symKey) andre@0: { andre@0: return symKey->userData; andre@0: } andre@0: andre@0: void andre@0: PK11_SetSymKeyUserData(PK11SymKey *symKey, void *userData, andre@0: PK11FreeDataFunc freeFunc) andre@0: { andre@0: /* free any existing data */ andre@0: if (symKey->userData && symKey->freeFunc) { andre@0: (*symKey->freeFunc)(symKey->userData); andre@0: } andre@0: symKey->userData = userData; andre@0: symKey->freeFunc = freeFunc; andre@0: return; andre@0: } andre@0: andre@0: /* andre@0: * turn key handle into an appropriate key object andre@0: */ andre@0: PK11SymKey * andre@0: PK11_SymKeyFromHandle(PK11SlotInfo *slot, PK11SymKey *parent, PK11Origin origin, andre@0: CK_MECHANISM_TYPE type, CK_OBJECT_HANDLE keyID, PRBool owner, void *wincx) andre@0: { andre@0: PK11SymKey *symKey; andre@0: PRBool needSession = !(owner && parent); andre@0: andre@0: if (keyID == CK_INVALID_HANDLE) { andre@0: return NULL; andre@0: } andre@0: andre@0: symKey = pk11_CreateSymKey(slot, type, owner, needSession, wincx); andre@0: if (symKey == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: symKey->objectID = keyID; andre@0: symKey->origin = origin; andre@0: andre@0: /* adopt the parent's session */ andre@0: /* This is only used by SSL. What we really want here is a session andre@0: * structure with a ref count so the session goes away only after all the andre@0: * keys do. */ andre@0: if (!needSession) { andre@0: symKey->sessionOwner = PR_FALSE; andre@0: symKey->session = parent->session; andre@0: symKey->parent = PK11_ReferenceSymKey(parent); andre@0: /* This is the only case where pk11_CreateSymKey does not explicitly andre@0: * check symKey->session. We need to assert here to make sure. andre@0: * the session isn't invalid. */ andre@0: PORT_Assert(parent->session != CK_INVALID_SESSION); andre@0: if (parent->session == CK_INVALID_SESSION) { andre@0: PK11_FreeSymKey(symKey); andre@0: PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); andre@0: return NULL; andre@0: } andre@0: } andre@0: andre@0: return symKey; andre@0: } andre@0: andre@0: /* andre@0: * turn key handle into an appropriate key object andre@0: */ andre@0: PK11SymKey * andre@0: PK11_GetWrapKey(PK11SlotInfo *slot, int wrap, CK_MECHANISM_TYPE type, andre@0: int series, void *wincx) andre@0: { andre@0: PK11SymKey *symKey = NULL; andre@0: andre@0: if (slot->series != series) return NULL; andre@0: if (slot->refKeys[wrap] == CK_INVALID_HANDLE) return NULL; andre@0: if (type == CKM_INVALID_MECHANISM) type = slot->wrapMechanism; andre@0: andre@0: symKey = PK11_SymKeyFromHandle(slot, NULL, PK11_OriginDerive, andre@0: slot->wrapMechanism, slot->refKeys[wrap], PR_FALSE, wincx); andre@0: return symKey; andre@0: } andre@0: andre@0: /* andre@0: * This function is not thread-safe because it sets wrapKey->sessionOwner andre@0: * without using a lock or atomic routine. It can only be called when andre@0: * only one thread has a reference to wrapKey. andre@0: */ andre@0: void andre@0: PK11_SetWrapKey(PK11SlotInfo *slot, int wrap, PK11SymKey *wrapKey) andre@0: { andre@0: /* save the handle and mechanism for the wrapping key */ andre@0: /* mark the key and session as not owned by us to they don't get freed andre@0: * when the key goes way... that lets us reuse the key later */ andre@0: slot->refKeys[wrap] = wrapKey->objectID; andre@0: wrapKey->owner = PR_FALSE; andre@0: wrapKey->sessionOwner = PR_FALSE; andre@0: slot->wrapMechanism = wrapKey->type; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * figure out if a key is still valid or if it is stale. andre@0: */ andre@0: PRBool andre@0: PK11_VerifyKeyOK(PK11SymKey *key) { andre@0: if (!PK11_IsPresent(key->slot)) { andre@0: return PR_FALSE; andre@0: } andre@0: return (PRBool)(key->series == key->slot->series); andre@0: } andre@0: andre@0: static PK11SymKey * andre@0: pk11_ImportSymKeyWithTempl(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, andre@0: PK11Origin origin, PRBool isToken, CK_ATTRIBUTE *keyTemplate, andre@0: unsigned int templateCount, SECItem *key, void *wincx) andre@0: { andre@0: PK11SymKey * symKey; andre@0: SECStatus rv; andre@0: andre@0: symKey = pk11_CreateSymKey(slot, type, !isToken, PR_TRUE, wincx); andre@0: if (symKey == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: symKey->size = key->len; andre@0: andre@0: PK11_SETATTRS(&keyTemplate[templateCount], CKA_VALUE, key->data, key->len); andre@0: templateCount++; andre@0: andre@0: if (SECITEM_CopyItem(NULL,&symKey->data,key) != SECSuccess) { andre@0: PK11_FreeSymKey(symKey); andre@0: return NULL; andre@0: } andre@0: andre@0: symKey->origin = origin; andre@0: andre@0: /* import the keys */ andre@0: rv = PK11_CreateNewObject(slot, symKey->session, keyTemplate, andre@0: templateCount, isToken, &symKey->objectID); andre@0: if ( rv != SECSuccess) { andre@0: PK11_FreeSymKey(symKey); andre@0: return NULL; andre@0: } andre@0: andre@0: return symKey; andre@0: } andre@0: andre@0: /* andre@0: * turn key bits into an appropriate key object andre@0: */ andre@0: PK11SymKey * andre@0: PK11_ImportSymKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, andre@0: PK11Origin origin, CK_ATTRIBUTE_TYPE operation, SECItem *key,void *wincx) andre@0: { andre@0: PK11SymKey * symKey; andre@0: unsigned int templateCount = 0; andre@0: CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; andre@0: CK_KEY_TYPE keyType = CKK_GENERIC_SECRET; andre@0: CK_BBOOL cktrue = CK_TRUE; /* sigh */ andre@0: CK_ATTRIBUTE keyTemplate[5]; andre@0: CK_ATTRIBUTE * attrs = keyTemplate; andre@0: andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &keyClass, sizeof(keyClass) ); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof(keyType) ); attrs++; andre@0: PK11_SETATTRS(attrs, operation, &cktrue, 1); attrs++; andre@0: templateCount = attrs - keyTemplate; andre@0: PR_ASSERT(templateCount+1 <= sizeof(keyTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: keyType = PK11_GetKeyType(type,key->len); andre@0: symKey = pk11_ImportSymKeyWithTempl(slot, type, origin, PR_FALSE, andre@0: keyTemplate, templateCount, key, wincx); andre@0: return symKey; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * turn key bits into an appropriate key object andre@0: */ andre@0: PK11SymKey * andre@0: PK11_ImportSymKeyWithFlags(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, andre@0: PK11Origin origin, CK_ATTRIBUTE_TYPE operation, SECItem *key, andre@0: CK_FLAGS flags, PRBool isPerm, void *wincx) andre@0: { andre@0: PK11SymKey * symKey; andre@0: unsigned int templateCount = 0; andre@0: CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; andre@0: CK_KEY_TYPE keyType = CKK_GENERIC_SECRET; andre@0: CK_BBOOL cktrue = CK_TRUE; /* sigh */ andre@0: CK_ATTRIBUTE keyTemplate[MAX_TEMPL_ATTRS]; andre@0: CK_ATTRIBUTE * attrs = keyTemplate; andre@0: andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &keyClass, sizeof(keyClass) ); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof(keyType) ); attrs++; andre@0: if (isPerm) { andre@0: PK11_SETATTRS(attrs, CKA_TOKEN, &cktrue, sizeof(cktrue) ); attrs++; andre@0: /* sigh some tokens think CKA_PRIVATE = false is a reasonable andre@0: * default for secret keys */ andre@0: PK11_SETATTRS(attrs, CKA_PRIVATE, &cktrue, sizeof(cktrue) ); attrs++; andre@0: } andre@0: attrs += pk11_OpFlagsToAttributes(flags, attrs, &cktrue); andre@0: if ((operation != CKA_FLAGS_ONLY) && andre@0: !pk11_FindAttrInTemplate(keyTemplate, attrs-keyTemplate, operation)) { andre@0: PK11_SETATTRS(attrs, operation, &cktrue, sizeof(cktrue)); attrs++; andre@0: } andre@0: templateCount = attrs - keyTemplate; andre@0: PR_ASSERT(templateCount+1 <= sizeof(keyTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: keyType = PK11_GetKeyType(type,key->len); andre@0: symKey = pk11_ImportSymKeyWithTempl(slot, type, origin, isPerm, andre@0: keyTemplate, templateCount, key, wincx); andre@0: if (symKey && isPerm) { andre@0: symKey->owner = PR_FALSE; andre@0: } andre@0: return symKey; andre@0: } andre@0: andre@0: andre@0: PK11SymKey * andre@0: PK11_FindFixedKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, SECItem *keyID, andre@0: void *wincx) andre@0: { andre@0: CK_ATTRIBUTE findTemp[4]; andre@0: CK_ATTRIBUTE *attrs; andre@0: CK_BBOOL ckTrue = CK_TRUE; andre@0: CK_OBJECT_CLASS keyclass = CKO_SECRET_KEY; andre@0: int tsize = 0; andre@0: CK_OBJECT_HANDLE key_id; andre@0: andre@0: attrs = findTemp; andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &keyclass, sizeof(keyclass)); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_TOKEN, &ckTrue, sizeof(ckTrue)); attrs++; andre@0: if (keyID) { andre@0: PK11_SETATTRS(attrs, CKA_ID, keyID->data, keyID->len); attrs++; andre@0: } andre@0: tsize = attrs - findTemp; andre@0: PORT_Assert(tsize <= sizeof(findTemp)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: key_id = pk11_FindObjectByTemplate(slot,findTemp,tsize); andre@0: if (key_id == CK_INVALID_HANDLE) { andre@0: return NULL; andre@0: } andre@0: return PK11_SymKeyFromHandle(slot, NULL, PK11_OriginDerive, type, key_id, andre@0: PR_FALSE, wincx); andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_ListFixedKeysInSlot(PK11SlotInfo *slot, char *nickname, void *wincx) andre@0: { andre@0: CK_ATTRIBUTE findTemp[4]; andre@0: CK_ATTRIBUTE *attrs; andre@0: CK_BBOOL ckTrue = CK_TRUE; andre@0: CK_OBJECT_CLASS keyclass = CKO_SECRET_KEY; andre@0: int tsize = 0; andre@0: int objCount = 0; andre@0: CK_OBJECT_HANDLE *key_ids; andre@0: PK11SymKey *nextKey = NULL; andre@0: PK11SymKey *topKey = NULL; andre@0: int i,len; andre@0: andre@0: attrs = findTemp; andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &keyclass, sizeof(keyclass)); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_TOKEN, &ckTrue, sizeof(ckTrue)); attrs++; andre@0: if (nickname) { andre@0: len = PORT_Strlen(nickname); andre@0: PK11_SETATTRS(attrs, CKA_LABEL, nickname, len); attrs++; andre@0: } andre@0: tsize = attrs - findTemp; andre@0: PORT_Assert(tsize <= sizeof(findTemp)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: key_ids = pk11_FindObjectsByTemplate(slot,findTemp,tsize,&objCount); andre@0: if (key_ids == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: for (i=0; i < objCount ; i++) { andre@0: SECItem typeData; andre@0: CK_KEY_TYPE type = CKK_GENERIC_SECRET; andre@0: SECStatus rv = PK11_ReadAttribute(slot, key_ids[i], andre@0: CKA_KEY_TYPE, NULL, &typeData); andre@0: if (rv == SECSuccess) { andre@0: if (typeData.len == sizeof(CK_KEY_TYPE)) { andre@0: type = *(CK_KEY_TYPE *)typeData.data; andre@0: } andre@0: PORT_Free(typeData.data); andre@0: } andre@0: nextKey = PK11_SymKeyFromHandle(slot, NULL, PK11_OriginDerive, andre@0: PK11_GetKeyMechanism(type), key_ids[i], PR_FALSE, wincx); andre@0: if (nextKey) { andre@0: nextKey->next = topKey; andre@0: topKey = nextKey; andre@0: } andre@0: } andre@0: PORT_Free(key_ids); andre@0: return topKey; andre@0: } andre@0: andre@0: void * andre@0: PK11_GetWindow(PK11SymKey *key) andre@0: { andre@0: return key->cx; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * extract a symetric key value. NOTE: if the key is sensitive, we will andre@0: * not be able to do this operation. This function is used to move andre@0: * keys from one token to another */ andre@0: SECStatus andre@0: PK11_ExtractKeyValue(PK11SymKey *symKey) andre@0: { andre@0: SECStatus rv; andre@0: andre@0: if (symKey->data.data != NULL) { andre@0: if (symKey->size == 0) { andre@0: symKey->size = symKey->data.len; andre@0: } andre@0: return SECSuccess; andre@0: } andre@0: andre@0: if (symKey->slot == NULL) { andre@0: PORT_SetError( SEC_ERROR_INVALID_KEY ); andre@0: return SECFailure; andre@0: } andre@0: andre@0: rv = PK11_ReadAttribute(symKey->slot,symKey->objectID,CKA_VALUE,NULL, andre@0: &symKey->data); andre@0: if (rv == SECSuccess) { andre@0: symKey->size = symKey->data.len; andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: SECStatus andre@0: PK11_DeleteTokenSymKey(PK11SymKey *symKey) andre@0: { andre@0: if (!PK11_IsPermObject(symKey->slot, symKey->objectID)) { andre@0: return SECFailure; andre@0: } andre@0: PK11_DestroyTokenObject(symKey->slot,symKey->objectID); andre@0: symKey->objectID = CK_INVALID_HANDLE; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SECItem * andre@0: PK11_GetKeyData(PK11SymKey *symKey) andre@0: { andre@0: return &symKey->data; andre@0: } andre@0: andre@0: /* This symbol is exported for backward compatibility. */ andre@0: SECItem * andre@0: __PK11_GetKeyData(PK11SymKey *symKey) andre@0: { andre@0: return PK11_GetKeyData(symKey); andre@0: } andre@0: andre@0: andre@0: /* andre@0: * PKCS #11 key Types with predefined length andre@0: */ andre@0: unsigned int andre@0: pk11_GetPredefinedKeyLength(CK_KEY_TYPE keyType) andre@0: { andre@0: int length = 0; andre@0: switch (keyType) { andre@0: case CKK_DES: length = 8; break; andre@0: case CKK_DES2: length = 16; break; andre@0: case CKK_DES3: length = 24; break; andre@0: case CKK_SKIPJACK: length = 10; break; andre@0: case CKK_BATON: length = 20; break; andre@0: case CKK_JUNIPER: length = 20; break; andre@0: default: break; andre@0: } andre@0: return length; andre@0: } andre@0: andre@0: /* return the keylength if possible. '0' if not */ andre@0: unsigned int andre@0: PK11_GetKeyLength(PK11SymKey *key) andre@0: { andre@0: CK_KEY_TYPE keyType; andre@0: andre@0: if (key->size != 0) return key->size; andre@0: andre@0: /* First try to figure out the key length from its type */ andre@0: keyType = PK11_ReadULongAttribute(key->slot,key->objectID,CKA_KEY_TYPE); andre@0: key->size = pk11_GetPredefinedKeyLength(keyType); andre@0: if ((keyType == CKK_GENERIC_SECRET) && andre@0: (key->type == CKM_SSL3_PRE_MASTER_KEY_GEN)) { andre@0: key->size=48; andre@0: } andre@0: andre@0: if( key->size != 0 ) return key->size; andre@0: andre@0: if (key->data.data == NULL) { andre@0: PK11_ExtractKeyValue(key); andre@0: } andre@0: /* key is probably secret. Look up its length */ andre@0: /* this is new PKCS #11 version 2.0 functionality. */ andre@0: if (key->size == 0) { andre@0: CK_ULONG keyLength; andre@0: andre@0: keyLength = PK11_ReadULongAttribute(key->slot,key->objectID,CKA_VALUE_LEN); andre@0: if (keyLength != CK_UNAVAILABLE_INFORMATION) { andre@0: key->size = (unsigned int)keyLength; andre@0: } andre@0: } andre@0: andre@0: return key->size; andre@0: } andre@0: andre@0: /* return the strength of a key. This is different from length in that andre@0: * 1) it returns the size in bits, and 2) it returns only the secret portions andre@0: * of the key minus any checksums or parity. andre@0: */ andre@0: unsigned int andre@0: PK11_GetKeyStrength(PK11SymKey *key, SECAlgorithmID *algid) andre@0: { andre@0: int size=0; andre@0: CK_MECHANISM_TYPE mechanism= CKM_INVALID_MECHANISM; /* RC2 only */ andre@0: SECItem *param = NULL; /* RC2 only */ andre@0: CK_RC2_CBC_PARAMS *rc2_params = NULL; /* RC2 ONLY */ andre@0: unsigned int effectiveBits = 0; /* RC2 ONLY */ andre@0: andre@0: switch (PK11_GetKeyType(key->type,0)) { andre@0: case CKK_CDMF: andre@0: return 40; andre@0: case CKK_DES: andre@0: return 56; andre@0: case CKK_DES3: andre@0: case CKK_DES2: andre@0: size = PK11_GetKeyLength(key); andre@0: if (size == 16) { andre@0: /* double des */ andre@0: return 112; /* 16*7 */ andre@0: } andre@0: return 168; andre@0: /* andre@0: * RC2 has is different than other ciphers in that it allows the user andre@0: * to deprecating keysize while still requiring all the bits for the andre@0: * original key. The info andre@0: * on what the effective key strength is in the parameter for the key. andre@0: * In S/MIME this parameter is stored in the DER encoded algid. In Our andre@0: * other uses of RC2, effectiveBits == keyBits, so this code functions andre@0: * correctly without an algid. andre@0: */ andre@0: case CKK_RC2: andre@0: /* if no algid was provided, fall through to default */ andre@0: if (!algid) { andre@0: break; andre@0: } andre@0: /* verify that the algid is for RC2 */ andre@0: mechanism = PK11_AlgtagToMechanism(SECOID_GetAlgorithmTag(algid)); andre@0: if ((mechanism != CKM_RC2_CBC) && (mechanism != CKM_RC2_ECB)) { andre@0: break; andre@0: } andre@0: andre@0: /* now get effective bits from the algorithm ID. */ andre@0: param = PK11_ParamFromAlgid(algid); andre@0: /* if we couldn't get memory just use key length */ andre@0: if (param == NULL) { andre@0: break; andre@0: } andre@0: andre@0: rc2_params = (CK_RC2_CBC_PARAMS *) param->data; andre@0: /* paranoia... shouldn't happen */ andre@0: PORT_Assert(param->data != NULL); andre@0: if (param->data == NULL) { andre@0: SECITEM_FreeItem(param,PR_TRUE); andre@0: break; andre@0: } andre@0: effectiveBits = (unsigned int)rc2_params->ulEffectiveBits; andre@0: SECITEM_FreeItem(param,PR_TRUE); andre@0: param = NULL; rc2_params=NULL; /* paranoia */ andre@0: andre@0: /* we have effective bits, is and allocated memory is free, now andre@0: * we need to return the smaller of effective bits and keysize */ andre@0: size = PK11_GetKeyLength(key); andre@0: if ((unsigned int)size*8 > effectiveBits) { andre@0: return effectiveBits; andre@0: } andre@0: andre@0: return size*8; /* the actual key is smaller, the strength can't be andre@0: * greater than the actual key size */ andre@0: andre@0: default: andre@0: break; andre@0: } andre@0: return PK11_GetKeyLength(key) * 8; andre@0: } andre@0: andre@0: /* andre@0: * The next three utilities are to deal with the fact that a given operation andre@0: * may be a multi-slot affair. This creates a new key object that is copied andre@0: * into the new slot. andre@0: */ andre@0: PK11SymKey * andre@0: pk11_CopyToSlotPerm(PK11SlotInfo *slot,CK_MECHANISM_TYPE type, andre@0: CK_ATTRIBUTE_TYPE operation, CK_FLAGS flags, andre@0: PRBool isPerm, PK11SymKey *symKey) andre@0: { andre@0: SECStatus rv; andre@0: PK11SymKey *newKey = NULL; andre@0: andre@0: /* Extract the raw key data if possible */ andre@0: if (symKey->data.data == NULL) { andre@0: rv = PK11_ExtractKeyValue(symKey); andre@0: /* KEY is sensitive, we're try key exchanging it. */ andre@0: if (rv != SECSuccess) { andre@0: return pk11_KeyExchange(slot, type, operation, andre@0: flags, isPerm, symKey); andre@0: } andre@0: } andre@0: andre@0: newKey = PK11_ImportSymKeyWithFlags(slot, type, symKey->origin, andre@0: operation, &symKey->data, flags, isPerm, symKey->cx); andre@0: if (newKey == NULL) { andre@0: newKey = pk11_KeyExchange(slot, type, operation, flags, isPerm, symKey); andre@0: } andre@0: return newKey; andre@0: } andre@0: andre@0: PK11SymKey * andre@0: pk11_CopyToSlot(PK11SlotInfo *slot,CK_MECHANISM_TYPE type, andre@0: CK_ATTRIBUTE_TYPE operation, PK11SymKey *symKey) andre@0: { andre@0: return pk11_CopyToSlotPerm(slot, type, operation, 0, PR_FALSE, symKey); andre@0: } andre@0: andre@0: /* andre@0: * Make sure the slot we are in is the correct slot for the operation andre@0: * by verifying that it supports all of the specified mechanism types. andre@0: */ andre@0: PK11SymKey * andre@0: pk11_ForceSlotMultiple(PK11SymKey *symKey, CK_MECHANISM_TYPE *type, andre@0: int mechCount, CK_ATTRIBUTE_TYPE operation) andre@0: { andre@0: PK11SlotInfo *slot = symKey->slot; andre@0: PK11SymKey *newKey = NULL; andre@0: PRBool needToCopy = PR_FALSE; andre@0: int i; andre@0: andre@0: if (slot == NULL) { andre@0: needToCopy = PR_TRUE; andre@0: } else { andre@0: i = 0; andre@0: while ((i < mechCount) && (needToCopy == PR_FALSE)) { andre@0: if (!PK11_DoesMechanism(slot,type[i])) { andre@0: needToCopy = PR_TRUE; andre@0: } andre@0: i++; andre@0: } andre@0: } andre@0: andre@0: if (needToCopy == PR_TRUE) { andre@0: slot = PK11_GetBestSlotMultiple(type,mechCount,symKey->cx); andre@0: if (slot == NULL) { andre@0: PORT_SetError( SEC_ERROR_NO_MODULE ); andre@0: return NULL; andre@0: } andre@0: newKey = pk11_CopyToSlot(slot, type[0], operation, symKey); andre@0: PK11_FreeSlot(slot); andre@0: } andre@0: return newKey; andre@0: } andre@0: andre@0: /* andre@0: * Make sure the slot we are in is the correct slot for the operation andre@0: */ andre@0: PK11SymKey * andre@0: pk11_ForceSlot(PK11SymKey *symKey,CK_MECHANISM_TYPE type, andre@0: CK_ATTRIBUTE_TYPE operation) andre@0: { andre@0: return pk11_ForceSlotMultiple(symKey, &type, 1, operation); andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_MoveSymKey(PK11SlotInfo *slot, CK_ATTRIBUTE_TYPE operation, andre@0: CK_FLAGS flags, PRBool perm, PK11SymKey *symKey) andre@0: { andre@0: if (symKey->slot == slot) { andre@0: if (perm) { andre@0: return PK11_ConvertSessionSymKeyToTokenSymKey(symKey,symKey->cx); andre@0: } else { andre@0: return PK11_ReferenceSymKey(symKey); andre@0: } andre@0: } andre@0: andre@0: return pk11_CopyToSlotPerm(slot, symKey->type, andre@0: operation, flags, perm, symKey); andre@0: } andre@0: andre@0: /* andre@0: * Use the token to generate a key. andre@0: * andre@0: * keySize must be 'zero' for fixed key length algorithms. A nonzero andre@0: * keySize causes the CKA_VALUE_LEN attribute to be added to the template andre@0: * for the key. Most PKCS #11 modules fail if you specify the CKA_VALUE_LEN andre@0: * attribute for keys with fixed length. The exception is DES2. If you andre@0: * select a CKM_DES3_CBC mechanism, this code will not add the CKA_VALUE_LEN andre@0: * parameter and use the key size to determine which underlying DES keygen andre@0: * function to use (CKM_DES2_KEY_GEN or CKM_DES3_KEY_GEN). andre@0: * andre@0: * keyType must be -1 for most algorithms. Some PBE algorthims cannot andre@0: * determine the correct key type from the mechanism or the parameters, andre@0: * so key type must be specified. Other PKCS #11 mechanisms may do so in andre@0: * the future. Currently there is no need to export this publically. andre@0: * Keep it private until there is a need in case we need to expand the andre@0: * keygen parameters again... andre@0: * andre@0: * CK_FLAGS flags: key operation flags andre@0: * PK11AttrFlags attrFlags: PK11_ATTR_XXX key attribute flags andre@0: */ andre@0: PK11SymKey * andre@0: pk11_TokenKeyGenWithFlagsAndKeyType(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, andre@0: SECItem *param, CK_KEY_TYPE keyType, int keySize, SECItem *keyid, andre@0: CK_FLAGS opFlags, PK11AttrFlags attrFlags, void *wincx) andre@0: { andre@0: PK11SymKey *symKey; andre@0: CK_ATTRIBUTE genTemplate[MAX_TEMPL_ATTRS]; andre@0: CK_ATTRIBUTE *attrs = genTemplate; andre@0: int count = sizeof(genTemplate)/sizeof(genTemplate[0]); andre@0: CK_MECHANISM_TYPE keyGenType; andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_BBOOL ckfalse = CK_FALSE; andre@0: CK_ULONG ck_key_size; /* only used for variable-length keys */ andre@0: andre@0: if (pk11_BadAttrFlags(attrFlags)) { andre@0: PORT_SetError( SEC_ERROR_INVALID_ARGS ); andre@0: return NULL; andre@0: } andre@0: andre@0: if ((keySize != 0) && (type != CKM_DES3_CBC) && andre@0: (type !=CKM_DES3_CBC_PAD) && (type != CKM_DES3_ECB)) { andre@0: ck_key_size = keySize; /* Convert to PK11 type */ andre@0: andre@0: PK11_SETATTRS(attrs, CKA_VALUE_LEN, &ck_key_size, sizeof(ck_key_size)); andre@0: attrs++; andre@0: } andre@0: andre@0: if (keyType != -1) { andre@0: PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof(CK_KEY_TYPE)); andre@0: attrs++; andre@0: } andre@0: andre@0: /* Include key id value if provided */ andre@0: if (keyid) { andre@0: PK11_SETATTRS(attrs, CKA_ID, keyid->data, keyid->len); attrs++; andre@0: } andre@0: andre@0: attrs += pk11_AttrFlagsToAttributes(attrFlags, attrs, &cktrue, &ckfalse); andre@0: attrs += pk11_OpFlagsToAttributes(opFlags, attrs, &cktrue); andre@0: andre@0: count = attrs - genTemplate; andre@0: PR_ASSERT(count <= sizeof(genTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: keyGenType = PK11_GetKeyGenWithSize(type, keySize); andre@0: if (keyGenType == CKM_FAKE_RANDOM) { andre@0: PORT_SetError( SEC_ERROR_NO_MODULE ); andre@0: return NULL; andre@0: } andre@0: symKey = PK11_KeyGenWithTemplate(slot, type, keyGenType, andre@0: param, genTemplate, count, wincx); andre@0: if (symKey != NULL) { andre@0: symKey->size = keySize; andre@0: } andre@0: return symKey; andre@0: } andre@0: andre@0: /* andre@0: * Use the token to generate a key. - Public andre@0: * andre@0: * keySize must be 'zero' for fixed key length algorithms. A nonzero andre@0: * keySize causes the CKA_VALUE_LEN attribute to be added to the template andre@0: * for the key. Most PKCS #11 modules fail if you specify the CKA_VALUE_LEN andre@0: * attribute for keys with fixed length. The exception is DES2. If you andre@0: * select a CKM_DES3_CBC mechanism, this code will not add the CKA_VALUE_LEN andre@0: * parameter and use the key size to determine which underlying DES keygen andre@0: * function to use (CKM_DES2_KEY_GEN or CKM_DES3_KEY_GEN). andre@0: * andre@0: * CK_FLAGS flags: key operation flags andre@0: * PK11AttrFlags attrFlags: PK11_ATTR_XXX key attribute flags andre@0: */ andre@0: PK11SymKey * andre@0: PK11_TokenKeyGenWithFlags(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, andre@0: SECItem *param, int keySize, SECItem *keyid, CK_FLAGS opFlags, andre@0: PK11AttrFlags attrFlags, void *wincx) andre@0: { andre@0: return pk11_TokenKeyGenWithFlagsAndKeyType(slot, type, param, -1, keySize, andre@0: keyid, opFlags, attrFlags, wincx); andre@0: } andre@0: andre@0: /* andre@0: * Use the token to generate a key. keySize must be 'zero' for fixed key andre@0: * length algorithms. A nonzero keySize causes the CKA_VALUE_LEN attribute andre@0: * to be added to the template for the key. PKCS #11 modules fail if you andre@0: * specify the CKA_VALUE_LEN attribute for keys with fixed length. andre@0: * NOTE: this means to generate a DES2 key from this interface you must andre@0: * specify CKM_DES2_KEY_GEN as the mechanism directly; specifying andre@0: * CKM_DES3_CBC as the mechanism and 16 as keySize currently doesn't work. andre@0: */ andre@0: PK11SymKey * andre@0: PK11_TokenKeyGen(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, SECItem *param, andre@0: int keySize, SECItem *keyid, PRBool isToken, void *wincx) andre@0: { andre@0: PK11SymKey *symKey; andre@0: PRBool weird = PR_FALSE; /* hack for fortezza */ andre@0: CK_FLAGS opFlags = CKF_SIGN; andre@0: PK11AttrFlags attrFlags = 0; andre@0: andre@0: if ((keySize == -1) && (type == CKM_SKIPJACK_CBC64)) { andre@0: weird = PR_TRUE; andre@0: keySize = 0; andre@0: } andre@0: andre@0: opFlags |= weird ? CKF_DECRYPT : CKF_ENCRYPT; andre@0: andre@0: if (isToken) { andre@0: attrFlags |= (PK11_ATTR_TOKEN | PK11_ATTR_PRIVATE); andre@0: } andre@0: andre@0: symKey = pk11_TokenKeyGenWithFlagsAndKeyType(slot, type, param, andre@0: -1, keySize, keyid, opFlags, attrFlags, wincx); andre@0: if (symKey && weird) { andre@0: PK11_SetFortezzaHack(symKey); andre@0: } andre@0: andre@0: return symKey; andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_KeyGen(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, SECItem *param, andre@0: int keySize, void *wincx) andre@0: { andre@0: return PK11_TokenKeyGen(slot, type, param, keySize, 0, PR_FALSE, wincx); andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_KeyGenWithTemplate(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, andre@0: CK_MECHANISM_TYPE keyGenType, andre@0: SECItem *param, CK_ATTRIBUTE * attrs, andre@0: unsigned int attrsCount, void *wincx) andre@0: { andre@0: PK11SymKey *symKey; andre@0: CK_SESSION_HANDLE session; andre@0: CK_MECHANISM mechanism; andre@0: CK_RV crv; andre@0: PRBool isToken = CK_FALSE; andre@0: CK_ULONG keySize = 0; andre@0: unsigned i; andre@0: andre@0: /* Extract the template's CKA_VALUE_LEN into keySize and CKA_TOKEN into andre@0: isToken. */ andre@0: for (i = 0; i < attrsCount; ++i) { andre@0: switch (attrs[i].type) { andre@0: case CKA_VALUE_LEN: andre@0: if (attrs[i].pValue == NULL || andre@0: attrs[i].ulValueLen != sizeof(CK_ULONG)) { andre@0: PORT_SetError(PK11_MapError(CKR_TEMPLATE_INCONSISTENT)); andre@0: return NULL; andre@0: } andre@0: keySize = * (CK_ULONG *) attrs[i].pValue; andre@0: break; andre@0: case CKA_TOKEN: andre@0: if (attrs[i].pValue == NULL || andre@0: attrs[i].ulValueLen != sizeof(CK_BBOOL)) { andre@0: PORT_SetError(PK11_MapError(CKR_TEMPLATE_INCONSISTENT)); andre@0: return NULL; andre@0: } andre@0: isToken = (*(CK_BBOOL*)attrs[i].pValue) ? PR_TRUE : PR_FALSE; andre@0: break; andre@0: } andre@0: } andre@0: andre@0: /* find a slot to generate the key into */ andre@0: /* Only do slot management if this is not a token key */ andre@0: if (!isToken && (slot == NULL || !PK11_DoesMechanism(slot,type))) { andre@0: PK11SlotInfo *bestSlot = PK11_GetBestSlot(type,wincx); andre@0: if (bestSlot == NULL) { andre@0: PORT_SetError( SEC_ERROR_NO_MODULE ); andre@0: return NULL; andre@0: } andre@0: symKey = pk11_CreateSymKey(bestSlot, type, !isToken, PR_TRUE, wincx); andre@0: PK11_FreeSlot(bestSlot); andre@0: } else { andre@0: symKey = pk11_CreateSymKey(slot, type, !isToken, PR_TRUE, wincx); andre@0: } andre@0: if (symKey == NULL) return NULL; andre@0: andre@0: symKey->size = keySize; andre@0: symKey->origin = PK11_OriginGenerated; andre@0: andre@0: /* Set the parameters for the key gen if provided */ andre@0: mechanism.mechanism = keyGenType; andre@0: mechanism.pParameter = NULL; andre@0: mechanism.ulParameterLen = 0; andre@0: if (param) { andre@0: mechanism.pParameter = param->data; andre@0: mechanism.ulParameterLen = param->len; andre@0: } andre@0: andre@0: /* Get session and perform locking */ andre@0: if (isToken) { andre@0: PK11_Authenticate(symKey->slot,PR_TRUE,wincx); andre@0: /* Should always be original slot */ andre@0: session = PK11_GetRWSession(symKey->slot); andre@0: symKey->owner = PR_FALSE; andre@0: } else { andre@0: session = symKey->session; andre@0: if (session != CK_INVALID_SESSION) andre@0: pk11_EnterKeyMonitor(symKey); andre@0: } andre@0: if (session == CK_INVALID_SESSION) { andre@0: PK11_FreeSymKey(symKey); andre@0: PORT_SetError(SEC_ERROR_BAD_DATA); andre@0: return NULL; andre@0: } andre@0: andre@0: crv = PK11_GETTAB(symKey->slot)->C_GenerateKey(session, andre@0: &mechanism, attrs, attrsCount, &symKey->objectID); andre@0: andre@0: /* Release lock and session */ andre@0: if (isToken) { andre@0: PK11_RestoreROSession(symKey->slot, session); andre@0: } else { andre@0: pk11_ExitKeyMonitor(symKey); andre@0: } andre@0: andre@0: if (crv != CKR_OK) { andre@0: PK11_FreeSymKey(symKey); andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: return NULL; andre@0: } andre@0: andre@0: return symKey; andre@0: } andre@0: andre@0: andre@0: /* --- */ andre@0: PK11SymKey * andre@0: PK11_GenDES3TokenKey(PK11SlotInfo *slot, SECItem *keyid, void *cx) andre@0: { andre@0: return PK11_TokenKeyGen(slot, CKM_DES3_CBC, 0, 0, keyid, PR_TRUE, cx); andre@0: } andre@0: andre@0: PK11SymKey* andre@0: PK11_ConvertSessionSymKeyToTokenSymKey(PK11SymKey *symk, void *wincx) andre@0: { andre@0: PK11SlotInfo* slot = symk->slot; andre@0: CK_ATTRIBUTE template[1]; andre@0: CK_ATTRIBUTE *attrs = template; andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_RV crv; andre@0: CK_OBJECT_HANDLE newKeyID; andre@0: CK_SESSION_HANDLE rwsession; andre@0: andre@0: PK11_SETATTRS(attrs, CKA_TOKEN, &cktrue, sizeof(cktrue)); attrs++; andre@0: andre@0: PK11_Authenticate(slot, PR_TRUE, wincx); andre@0: rwsession = PK11_GetRWSession(slot); andre@0: if (rwsession == CK_INVALID_SESSION) { andre@0: PORT_SetError(SEC_ERROR_BAD_DATA); andre@0: return NULL; andre@0: } andre@0: crv = PK11_GETTAB(slot)->C_CopyObject(rwsession, symk->objectID, andre@0: template, 1, &newKeyID); andre@0: PK11_RestoreROSession(slot, rwsession); andre@0: andre@0: if (crv != CKR_OK) { andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: return NULL; andre@0: } andre@0: andre@0: return PK11_SymKeyFromHandle(slot, NULL /*parent*/, symk->origin, andre@0: symk->type, newKeyID, PR_FALSE /*owner*/, NULL /*wincx*/); andre@0: } andre@0: andre@0: /* andre@0: * This function does a straight public key wrap (which only RSA can do). andre@0: * Use PK11_PubGenKey and PK11_WrapSymKey to implement the FORTEZZA and andre@0: * Diffie-Hellman Ciphers. */ andre@0: SECStatus andre@0: PK11_PubWrapSymKey(CK_MECHANISM_TYPE type, SECKEYPublicKey *pubKey, andre@0: PK11SymKey *symKey, SECItem *wrappedKey) andre@0: { andre@0: PK11SlotInfo *slot; andre@0: CK_ULONG len = wrappedKey->len; andre@0: PK11SymKey *newKey = NULL; andre@0: CK_OBJECT_HANDLE id; andre@0: CK_MECHANISM mechanism; andre@0: PRBool owner = PR_TRUE; andre@0: CK_SESSION_HANDLE session; andre@0: CK_RV crv; andre@0: andre@0: if (symKey == NULL) { andre@0: PORT_SetError( SEC_ERROR_INVALID_ARGS ); andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* if this slot doesn't support the mechanism, go to a slot that does */ andre@0: newKey = pk11_ForceSlot(symKey,type,CKA_ENCRYPT); andre@0: if (newKey != NULL) { andre@0: symKey = newKey; andre@0: } andre@0: andre@0: if (symKey->slot == NULL) { andre@0: PORT_SetError( SEC_ERROR_NO_MODULE ); andre@0: return SECFailure; andre@0: } andre@0: andre@0: slot = symKey->slot; andre@0: mechanism.mechanism = pk11_mapWrapKeyType(pubKey->keyType); andre@0: mechanism.pParameter = NULL; andre@0: mechanism.ulParameterLen = 0; andre@0: andre@0: id = PK11_ImportPublicKey(slot,pubKey,PR_FALSE); andre@0: if (id == CK_INVALID_HANDLE) { andre@0: if (newKey) { andre@0: PK11_FreeSymKey(newKey); andre@0: } andre@0: return SECFailure; /* Error code has been set. */ andre@0: } andre@0: andre@0: session = pk11_GetNewSession(slot,&owner); andre@0: if (!owner || !(slot->isThreadSafe)) PK11_EnterSlotMonitor(slot); andre@0: crv = PK11_GETTAB(slot)->C_WrapKey(session,&mechanism, andre@0: id,symKey->objectID,wrappedKey->data,&len); andre@0: if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); andre@0: pk11_CloseSession(slot,session,owner); andre@0: if (newKey) { andre@0: PK11_FreeSymKey(newKey); andre@0: } andre@0: andre@0: if (crv != CKR_OK) { andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: return SECFailure; andre@0: } andre@0: wrappedKey->len = len; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* andre@0: * this little function uses the Encrypt function to wrap a key, just in andre@0: * case we have problems with the wrap implementation for a token. andre@0: */ andre@0: static SECStatus andre@0: pk11_HandWrap(PK11SymKey *wrappingKey, SECItem *param, CK_MECHANISM_TYPE type, andre@0: SECItem *inKey, SECItem *outKey) andre@0: { andre@0: PK11SlotInfo *slot; andre@0: CK_ULONG len; andre@0: SECItem *data; andre@0: CK_MECHANISM mech; andre@0: PRBool owner = PR_TRUE; andre@0: CK_SESSION_HANDLE session; andre@0: CK_RV crv; andre@0: andre@0: slot = wrappingKey->slot; andre@0: /* use NULL IV's for wrapping */ andre@0: mech.mechanism = type; andre@0: if (param) { andre@0: mech.pParameter = param->data; andre@0: mech.ulParameterLen = param->len; andre@0: } else { andre@0: mech.pParameter = NULL; andre@0: mech.ulParameterLen = 0; andre@0: } andre@0: session = pk11_GetNewSession(slot,&owner); andre@0: if (!owner || !(slot->isThreadSafe)) PK11_EnterSlotMonitor(slot); andre@0: crv = PK11_GETTAB(slot)->C_EncryptInit(session,&mech, andre@0: wrappingKey->objectID); andre@0: if (crv != CKR_OK) { andre@0: if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); andre@0: pk11_CloseSession(slot,session,owner); andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* keys are almost always aligned, but if we get this far, andre@0: * we've gone above and beyond anyway... */ andre@0: data = PK11_BlockData(inKey,PK11_GetBlockSize(type,param)); andre@0: if (data == NULL) { andre@0: if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); andre@0: pk11_CloseSession(slot,session,owner); andre@0: PORT_SetError(SEC_ERROR_NO_MEMORY); andre@0: return SECFailure; andre@0: } andre@0: len = outKey->len; andre@0: crv = PK11_GETTAB(slot)->C_Encrypt(session,data->data,data->len, andre@0: outKey->data, &len); andre@0: if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); andre@0: pk11_CloseSession(slot,session,owner); andre@0: SECITEM_FreeItem(data,PR_TRUE); andre@0: outKey->len = len; andre@0: if (crv != CKR_OK) { andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: return SECFailure; andre@0: } andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* andre@0: * This function does a symetric based wrap. andre@0: */ andre@0: SECStatus andre@0: PK11_WrapSymKey(CK_MECHANISM_TYPE type, SECItem *param, andre@0: PK11SymKey *wrappingKey, PK11SymKey *symKey, SECItem *wrappedKey) andre@0: { andre@0: PK11SlotInfo *slot; andre@0: CK_ULONG len = wrappedKey->len; andre@0: PK11SymKey *newKey = NULL; andre@0: SECItem *param_save = NULL; andre@0: CK_MECHANISM mechanism; andre@0: PRBool owner = PR_TRUE; andre@0: CK_SESSION_HANDLE session; andre@0: CK_RV crv; andre@0: SECStatus rv; andre@0: andre@0: /* if this slot doesn't support the mechanism, go to a slot that does */ andre@0: /* Force symKey and wrappingKey into the same slot */ andre@0: if ((wrappingKey->slot == NULL) || (symKey->slot != wrappingKey->slot)) { andre@0: /* first try copying the wrapping Key to the symKey slot */ andre@0: if (symKey->slot && PK11_DoesMechanism(symKey->slot,type)) { andre@0: newKey = pk11_CopyToSlot(symKey->slot,type,CKA_WRAP,wrappingKey); andre@0: } andre@0: /* Nope, try it the other way */ andre@0: if (newKey == NULL) { andre@0: if (wrappingKey->slot) { andre@0: newKey = pk11_CopyToSlot(wrappingKey->slot, andre@0: symKey->type, CKA_ENCRYPT, symKey); andre@0: } andre@0: /* just not playing... one last thing, can we get symKey's data? andre@0: * If it's possible, we it should already be in the andre@0: * symKey->data.data pointer because pk11_CopyToSlot would have andre@0: * tried to put it there. */ andre@0: if (newKey == NULL) { andre@0: /* Can't get symKey's data: Game Over */ andre@0: if (symKey->data.data == NULL) { andre@0: PORT_SetError( SEC_ERROR_NO_MODULE ); andre@0: return SECFailure; andre@0: } andre@0: if (param == NULL) { andre@0: param_save = param = PK11_ParamFromIV(type,NULL); andre@0: } andre@0: rv = pk11_HandWrap(wrappingKey, param, type, andre@0: &symKey->data,wrappedKey); andre@0: if (param_save) SECITEM_FreeItem(param_save,PR_TRUE); andre@0: return rv; andre@0: } andre@0: /* we successfully moved the sym Key */ andre@0: symKey = newKey; andre@0: } else { andre@0: /* we successfully moved the wrapping Key */ andre@0: wrappingKey = newKey; andre@0: } andre@0: } andre@0: andre@0: /* at this point both keys are in the same token */ andre@0: slot = wrappingKey->slot; andre@0: mechanism.mechanism = type; andre@0: /* use NULL IV's for wrapping */ andre@0: if (param == NULL) { andre@0: param_save = param = PK11_ParamFromIV(type,NULL); andre@0: } andre@0: if (param) { andre@0: mechanism.pParameter = param->data; andre@0: mechanism.ulParameterLen = param->len; andre@0: } else { andre@0: mechanism.pParameter = NULL; andre@0: mechanism.ulParameterLen = 0; andre@0: } andre@0: andre@0: len = wrappedKey->len; andre@0: andre@0: session = pk11_GetNewSession(slot,&owner); andre@0: if (!owner || !(slot->isThreadSafe)) PK11_EnterSlotMonitor(slot); andre@0: crv = PK11_GETTAB(slot)->C_WrapKey(session, &mechanism, andre@0: wrappingKey->objectID, symKey->objectID, andre@0: wrappedKey->data, &len); andre@0: if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); andre@0: pk11_CloseSession(slot,session,owner); andre@0: rv = SECSuccess; andre@0: if (crv != CKR_OK) { andre@0: /* can't wrap it? try hand wrapping it... */ andre@0: do { andre@0: if (symKey->data.data == NULL) { andre@0: rv = PK11_ExtractKeyValue(symKey); andre@0: if (rv != SECSuccess) break; andre@0: } andre@0: rv = pk11_HandWrap(wrappingKey, param, type, &symKey->data, andre@0: wrappedKey); andre@0: } while (PR_FALSE); andre@0: } else { andre@0: wrappedKey->len = len; andre@0: } andre@0: if (newKey) PK11_FreeSymKey(newKey); andre@0: if (param_save) SECITEM_FreeItem(param_save,PR_TRUE); andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * This Generates a new key based on a symetricKey andre@0: */ andre@0: PK11SymKey * andre@0: PK11_Derive( PK11SymKey *baseKey, CK_MECHANISM_TYPE derive, SECItem *param, andre@0: CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, andre@0: int keySize) andre@0: { andre@0: return PK11_DeriveWithTemplate(baseKey, derive, param, target, operation, andre@0: keySize, NULL, 0, PR_FALSE); andre@0: } andre@0: andre@0: andre@0: PK11SymKey * andre@0: PK11_DeriveWithFlags( PK11SymKey *baseKey, CK_MECHANISM_TYPE derive, andre@0: SECItem *param, CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, andre@0: int keySize, CK_FLAGS flags) andre@0: { andre@0: CK_BBOOL ckTrue = CK_TRUE; andre@0: CK_ATTRIBUTE keyTemplate[MAX_TEMPL_ATTRS]; andre@0: unsigned int templateCount; andre@0: andre@0: templateCount = pk11_OpFlagsToAttributes(flags, keyTemplate, &ckTrue); andre@0: return PK11_DeriveWithTemplate(baseKey, derive, param, target, operation, andre@0: keySize, keyTemplate, templateCount, PR_FALSE); andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_DeriveWithFlagsPerm( PK11SymKey *baseKey, CK_MECHANISM_TYPE derive, andre@0: SECItem *param, CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, andre@0: int keySize, CK_FLAGS flags, PRBool isPerm) andre@0: { andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_ATTRIBUTE keyTemplate[MAX_TEMPL_ATTRS]; andre@0: CK_ATTRIBUTE *attrs; andre@0: unsigned int templateCount = 0; andre@0: andre@0: attrs = keyTemplate; andre@0: if (isPerm) { andre@0: PK11_SETATTRS(attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL)); attrs++; andre@0: } andre@0: templateCount = attrs - keyTemplate; andre@0: templateCount += pk11_OpFlagsToAttributes(flags, attrs, &cktrue); andre@0: return PK11_DeriveWithTemplate(baseKey, derive, param, target, operation, andre@0: keySize, keyTemplate, templateCount, isPerm); andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_DeriveWithTemplate( PK11SymKey *baseKey, CK_MECHANISM_TYPE derive, andre@0: SECItem *param, CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, andre@0: int keySize, CK_ATTRIBUTE *userAttr, unsigned int numAttrs, andre@0: PRBool isPerm) andre@0: { andre@0: PK11SlotInfo * slot = baseKey->slot; andre@0: PK11SymKey * symKey; andre@0: PK11SymKey * newBaseKey = NULL; andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; andre@0: CK_KEY_TYPE keyType = CKK_GENERIC_SECRET; andre@0: CK_ULONG valueLen = 0; andre@0: CK_MECHANISM mechanism; andre@0: CK_RV crv; andre@0: #define MAX_ADD_ATTRS 4 andre@0: CK_ATTRIBUTE keyTemplate[MAX_TEMPL_ATTRS + MAX_ADD_ATTRS]; andre@0: #undef MAX_ADD_ATTRS andre@0: CK_ATTRIBUTE * attrs = keyTemplate; andre@0: CK_SESSION_HANDLE session; andre@0: unsigned int templateCount; andre@0: andre@0: if (numAttrs > MAX_TEMPL_ATTRS) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: andre@0: /* first copy caller attributes in. */ andre@0: for (templateCount = 0; templateCount < numAttrs; ++templateCount) { andre@0: *attrs++ = *userAttr++; andre@0: } andre@0: andre@0: /* We only add the following attributes to the template if the caller andre@0: ** didn't already supply them. andre@0: */ andre@0: if (!pk11_FindAttrInTemplate(keyTemplate, numAttrs, CKA_CLASS)) { andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &keyClass, sizeof keyClass); andre@0: attrs++; andre@0: } andre@0: if (!pk11_FindAttrInTemplate(keyTemplate, numAttrs, CKA_KEY_TYPE)) { andre@0: keyType = PK11_GetKeyType(target, keySize); andre@0: PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof keyType ); andre@0: attrs++; andre@0: } andre@0: if (keySize > 0 && andre@0: !pk11_FindAttrInTemplate(keyTemplate, numAttrs, CKA_VALUE_LEN)) { andre@0: valueLen = (CK_ULONG)keySize; andre@0: PK11_SETATTRS(attrs, CKA_VALUE_LEN, &valueLen, sizeof valueLen); andre@0: attrs++; andre@0: } andre@0: if ((operation != CKA_FLAGS_ONLY) && andre@0: !pk11_FindAttrInTemplate(keyTemplate, numAttrs, operation)) { andre@0: PK11_SETATTRS(attrs, operation, &cktrue, sizeof cktrue); attrs++; andre@0: } andre@0: andre@0: templateCount = attrs - keyTemplate; andre@0: PR_ASSERT(templateCount <= sizeof(keyTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: /* move the key to a slot that can do the function */ andre@0: if (!PK11_DoesMechanism(slot,derive)) { andre@0: /* get a new base key & slot */ andre@0: PK11SlotInfo *newSlot = PK11_GetBestSlot(derive, baseKey->cx); andre@0: andre@0: if (newSlot == NULL) return NULL; andre@0: andre@0: newBaseKey = pk11_CopyToSlot (newSlot, derive, CKA_DERIVE, andre@0: baseKey); andre@0: PK11_FreeSlot(newSlot); andre@0: if (newBaseKey == NULL) andre@0: return NULL; andre@0: baseKey = newBaseKey; andre@0: slot = baseKey->slot; andre@0: } andre@0: andre@0: andre@0: /* get our key Structure */ andre@0: symKey = pk11_CreateSymKey(slot, target, !isPerm, PR_TRUE, baseKey->cx); andre@0: if (symKey == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: symKey->size = keySize; andre@0: andre@0: mechanism.mechanism = derive; andre@0: if (param) { andre@0: mechanism.pParameter = param->data; andre@0: mechanism.ulParameterLen = param->len; andre@0: } else { andre@0: mechanism.pParameter = NULL; andre@0: mechanism.ulParameterLen = 0; andre@0: } andre@0: symKey->origin=PK11_OriginDerive; andre@0: andre@0: if (isPerm) { andre@0: session = PK11_GetRWSession(slot); andre@0: } else { andre@0: pk11_EnterKeyMonitor(symKey); andre@0: session = symKey->session; andre@0: } andre@0: if (session == CK_INVALID_SESSION) { andre@0: if (!isPerm) andre@0: pk11_ExitKeyMonitor(symKey); andre@0: crv = CKR_SESSION_HANDLE_INVALID; andre@0: } else { andre@0: crv = PK11_GETTAB(slot)->C_DeriveKey(session, &mechanism, andre@0: baseKey->objectID, keyTemplate, templateCount, &symKey->objectID); andre@0: if (isPerm) { andre@0: PK11_RestoreROSession(slot, session); andre@0: } else { andre@0: pk11_ExitKeyMonitor(symKey); andre@0: } andre@0: } andre@0: if (newBaseKey) andre@0: PK11_FreeSymKey(newBaseKey); andre@0: if (crv != CKR_OK) { andre@0: PK11_FreeSymKey(symKey); andre@0: return NULL; andre@0: } andre@0: return symKey; andre@0: } andre@0: andre@0: /* Create a new key by concatenating base and data andre@0: */ andre@0: static PK11SymKey *pk11_ConcatenateBaseAndData(PK11SymKey *base, andre@0: CK_BYTE *data, CK_ULONG dataLen, CK_MECHANISM_TYPE target, andre@0: CK_ATTRIBUTE_TYPE operation) andre@0: { andre@0: CK_KEY_DERIVATION_STRING_DATA mechParams; andre@0: SECItem param; andre@0: andre@0: if (base == NULL) { andre@0: PORT_SetError( SEC_ERROR_INVALID_ARGS ); andre@0: return NULL; andre@0: } andre@0: andre@0: mechParams.pData = data; andre@0: mechParams.ulLen = dataLen; andre@0: param.data = (unsigned char *)&mechParams; andre@0: param.len = sizeof(CK_KEY_DERIVATION_STRING_DATA); andre@0: andre@0: return PK11_Derive(base, CKM_CONCATENATE_BASE_AND_DATA, andre@0: ¶m, target, operation, 0); andre@0: } andre@0: andre@0: /* Create a new key by concatenating base and key andre@0: */ andre@0: static PK11SymKey *pk11_ConcatenateBaseAndKey(PK11SymKey *base, andre@0: PK11SymKey *key, CK_MECHANISM_TYPE target, andre@0: CK_ATTRIBUTE_TYPE operation, CK_ULONG keySize) andre@0: { andre@0: SECItem param; andre@0: andre@0: if ((base == NULL) || (key == NULL)) { andre@0: PORT_SetError( SEC_ERROR_INVALID_ARGS ); andre@0: return NULL; andre@0: } andre@0: andre@0: param.data = (unsigned char *)&(key->objectID); andre@0: param.len = sizeof(CK_OBJECT_HANDLE); andre@0: andre@0: return PK11_Derive(base, CKM_CONCATENATE_BASE_AND_KEY, andre@0: ¶m, target, operation, keySize); andre@0: } andre@0: andre@0: /* Create a new key whose value is the hash of tobehashed. andre@0: * type is the mechanism for the derived key. andre@0: */ andre@0: static PK11SymKey *pk11_HashKeyDerivation(PK11SymKey *toBeHashed, andre@0: CK_MECHANISM_TYPE hashMechanism, CK_MECHANISM_TYPE target, andre@0: CK_ATTRIBUTE_TYPE operation, CK_ULONG keySize) andre@0: { andre@0: return PK11_Derive(toBeHashed, hashMechanism, NULL, target, operation, keySize); andre@0: } andre@0: andre@0: /* This function implements the ANSI X9.63 key derivation function andre@0: */ andre@0: static PK11SymKey *pk11_ANSIX963Derive(PK11SymKey *sharedSecret, andre@0: CK_EC_KDF_TYPE kdf, SECItem *sharedData, andre@0: CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, andre@0: CK_ULONG keySize) andre@0: { andre@0: CK_KEY_TYPE keyType; andre@0: CK_MECHANISM_TYPE hashMechanism, mechanismArray[4]; andre@0: CK_ULONG derivedKeySize, HashLen, counter, maxCounter, bufferLen; andre@0: CK_ULONG SharedInfoLen; andre@0: CK_BYTE *buffer = NULL; andre@0: PK11SymKey *toBeHashed, *hashOutput; andre@0: PK11SymKey *newSharedSecret = NULL; andre@0: PK11SymKey *oldIntermediateResult, *intermediateResult = NULL; andre@0: andre@0: if (sharedSecret == NULL) { andre@0: PORT_SetError( SEC_ERROR_INVALID_ARGS ); andre@0: return NULL; andre@0: } andre@0: andre@0: switch (kdf) { andre@0: case CKD_SHA1_KDF: andre@0: HashLen = SHA1_LENGTH; andre@0: hashMechanism = CKM_SHA1_KEY_DERIVATION; andre@0: break; andre@0: case CKD_SHA224_KDF: andre@0: HashLen = SHA224_LENGTH; andre@0: hashMechanism = CKM_SHA224_KEY_DERIVATION; andre@0: break; andre@0: case CKD_SHA256_KDF: andre@0: HashLen = SHA256_LENGTH; andre@0: hashMechanism = CKM_SHA256_KEY_DERIVATION; andre@0: break; andre@0: case CKD_SHA384_KDF: andre@0: HashLen = SHA384_LENGTH; andre@0: hashMechanism = CKM_SHA384_KEY_DERIVATION; andre@0: break; andre@0: case CKD_SHA512_KDF: andre@0: HashLen = SHA512_LENGTH; andre@0: hashMechanism = CKM_SHA512_KEY_DERIVATION; andre@0: break; andre@0: default: andre@0: PORT_SetError( SEC_ERROR_INVALID_ARGS ); andre@0: return NULL; andre@0: } andre@0: andre@0: derivedKeySize = keySize; andre@0: if (derivedKeySize == 0) { andre@0: keyType = PK11_GetKeyType(target,keySize); andre@0: derivedKeySize = pk11_GetPredefinedKeyLength(keyType); andre@0: if (derivedKeySize == 0) { andre@0: derivedKeySize = HashLen; andre@0: } andre@0: } andre@0: andre@0: /* Check that key_len isn't too long. The maximum key length could be andre@0: * greatly increased if the code below did not limit the 4-byte counter andre@0: * to a maximum value of 255. */ andre@0: if (derivedKeySize > 254 * HashLen) { andre@0: PORT_SetError( SEC_ERROR_INVALID_ARGS ); andre@0: return NULL; andre@0: } andre@0: andre@0: maxCounter = derivedKeySize / HashLen; andre@0: if (derivedKeySize > maxCounter * HashLen) andre@0: maxCounter++; andre@0: andre@0: if ((sharedData == NULL) || (sharedData->data == NULL)) andre@0: SharedInfoLen = 0; andre@0: else andre@0: SharedInfoLen = sharedData->len; andre@0: andre@0: bufferLen = SharedInfoLen + 4; andre@0: andre@0: /* Populate buffer with Counter || sharedData andre@0: * where Counter is 0x00000001. */ andre@0: buffer = (unsigned char *)PORT_Alloc(bufferLen); andre@0: if (buffer == NULL) { andre@0: PORT_SetError(SEC_ERROR_NO_MEMORY); andre@0: return NULL; andre@0: } andre@0: andre@0: buffer[0] = 0; andre@0: buffer[1] = 0; andre@0: buffer[2] = 0; andre@0: buffer[3] = 1; andre@0: if (SharedInfoLen > 0) { andre@0: PORT_Memcpy(&buffer[4], sharedData->data, SharedInfoLen); andre@0: } andre@0: andre@0: /* Look for a slot that supports the mechanisms needed andre@0: * to implement the ANSI X9.63 KDF as well as the andre@0: * target mechanism. andre@0: */ andre@0: mechanismArray[0] = CKM_CONCATENATE_BASE_AND_DATA; andre@0: mechanismArray[1] = hashMechanism; andre@0: mechanismArray[2] = CKM_CONCATENATE_BASE_AND_KEY; andre@0: mechanismArray[3] = target; andre@0: andre@0: newSharedSecret = pk11_ForceSlotMultiple(sharedSecret, andre@0: mechanismArray, 4, operation); andre@0: if (newSharedSecret != NULL) { andre@0: sharedSecret = newSharedSecret; andre@0: } andre@0: andre@0: for(counter=1; counter <= maxCounter; counter++) { andre@0: /* Concatenate shared_secret and buffer */ andre@0: toBeHashed = pk11_ConcatenateBaseAndData(sharedSecret, buffer, andre@0: bufferLen, hashMechanism, operation); andre@0: if (toBeHashed == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* Hash value */ andre@0: if (maxCounter == 1) { andre@0: /* In this case the length of the key to be derived is andre@0: * less than or equal to the length of the hash output. andre@0: * So, the output of the hash operation will be the andre@0: * dervied key. */ andre@0: hashOutput = pk11_HashKeyDerivation(toBeHashed, hashMechanism, andre@0: target, operation, keySize); andre@0: } else { andre@0: /* In this case, the output of the hash operation will be andre@0: * concatenated with other data to create the derived key. */ andre@0: hashOutput = pk11_HashKeyDerivation(toBeHashed, hashMechanism, andre@0: CKM_CONCATENATE_BASE_AND_KEY, operation, 0); andre@0: } andre@0: PK11_FreeSymKey(toBeHashed); andre@0: if (hashOutput == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* Append result to intermediate result, if necessary */ andre@0: oldIntermediateResult = intermediateResult; andre@0: andre@0: if (oldIntermediateResult == NULL) { andre@0: intermediateResult = hashOutput; andre@0: } else { andre@0: if (counter == maxCounter) { andre@0: /* This is the final concatenation, and so the output andre@0: * will be the derived key. */ andre@0: intermediateResult = andre@0: pk11_ConcatenateBaseAndKey(oldIntermediateResult, andre@0: hashOutput, target, operation, keySize); andre@0: } else { andre@0: /* The output of this concatenation will be concatenated andre@0: * with other data to create the derived key. */ andre@0: intermediateResult = andre@0: pk11_ConcatenateBaseAndKey(oldIntermediateResult, andre@0: hashOutput, CKM_CONCATENATE_BASE_AND_KEY, andre@0: operation, 0); andre@0: } andre@0: andre@0: PK11_FreeSymKey(hashOutput); andre@0: PK11_FreeSymKey(oldIntermediateResult); andre@0: if (intermediateResult == NULL) { andre@0: goto loser; andre@0: } andre@0: } andre@0: andre@0: /* Increment counter (assumes maxCounter < 255) */ andre@0: buffer[3]++; andre@0: } andre@0: andre@0: PORT_ZFree(buffer, bufferLen); andre@0: if (newSharedSecret != NULL) andre@0: PK11_FreeSymKey(newSharedSecret); andre@0: return intermediateResult; andre@0: andre@0: loser: andre@0: if (buffer != NULL) andre@0: PORT_ZFree(buffer, bufferLen); andre@0: if (newSharedSecret != NULL) andre@0: PK11_FreeSymKey(newSharedSecret); andre@0: if (intermediateResult != NULL) andre@0: PK11_FreeSymKey(intermediateResult); andre@0: return NULL; andre@0: } andre@0: andre@0: /* andre@0: * This Generates a wrapping key based on a privateKey, publicKey, and two andre@0: * random numbers. For Mail usage RandomB should be NULL. In the Sender's andre@0: * case RandomA is generate, outherwize it is passed. andre@0: */ andre@0: static unsigned char *rb_email = NULL; andre@0: andre@0: PK11SymKey * andre@0: PK11_PubDerive(SECKEYPrivateKey *privKey, SECKEYPublicKey *pubKey, andre@0: PRBool isSender, SECItem *randomA, SECItem *randomB, andre@0: CK_MECHANISM_TYPE derive, CK_MECHANISM_TYPE target, andre@0: CK_ATTRIBUTE_TYPE operation, int keySize,void *wincx) andre@0: { andre@0: PK11SlotInfo *slot = privKey->pkcs11Slot; andre@0: CK_MECHANISM mechanism; andre@0: PK11SymKey *symKey; andre@0: CK_RV crv; andre@0: andre@0: andre@0: if (rb_email == NULL) { andre@0: rb_email = PORT_ZAlloc(128); andre@0: if (rb_email == NULL) { andre@0: return NULL; andre@0: } andre@0: rb_email[127] = 1; andre@0: } andre@0: andre@0: /* get our key Structure */ andre@0: symKey = pk11_CreateSymKey(slot, target, PR_TRUE, PR_TRUE, wincx); andre@0: if (symKey == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: symKey->origin = PK11_OriginDerive; andre@0: andre@0: switch (privKey->keyType) { andre@0: case rsaKey: andre@0: case nullKey: andre@0: PORT_SetError(SEC_ERROR_BAD_KEY); andre@0: break; andre@0: case dsaKey: andre@0: case keaKey: andre@0: case fortezzaKey: andre@0: { andre@0: CK_KEA_DERIVE_PARAMS param; andre@0: param.isSender = (CK_BBOOL) isSender; andre@0: param.ulRandomLen = randomA->len; andre@0: param.pRandomA = randomA->data; andre@0: param.pRandomB = rb_email; andre@0: if (randomB) andre@0: param.pRandomB = randomB->data; andre@0: if (pubKey->keyType == fortezzaKey) { andre@0: param.ulPublicDataLen = pubKey->u.fortezza.KEAKey.len; andre@0: param.pPublicData = pubKey->u.fortezza.KEAKey.data; andre@0: } else { andre@0: /* assert type == keaKey */ andre@0: /* XXX change to match key key types */ andre@0: param.ulPublicDataLen = pubKey->u.fortezza.KEAKey.len; andre@0: param.pPublicData = pubKey->u.fortezza.KEAKey.data; andre@0: } andre@0: andre@0: mechanism.mechanism = derive; andre@0: mechanism.pParameter = ¶m; andre@0: mechanism.ulParameterLen = sizeof(param); andre@0: andre@0: /* get a new symKey structure */ andre@0: pk11_EnterKeyMonitor(symKey); andre@0: crv=PK11_GETTAB(slot)->C_DeriveKey(symKey->session, &mechanism, andre@0: privKey->pkcs11ID, NULL, 0, &symKey->objectID); andre@0: pk11_ExitKeyMonitor(symKey); andre@0: if (crv == CKR_OK) return symKey; andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: } andre@0: break; andre@0: case dhKey: andre@0: { andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; andre@0: CK_KEY_TYPE keyType = CKK_GENERIC_SECRET; andre@0: CK_ULONG key_size = 0; andre@0: CK_ATTRIBUTE keyTemplate[4]; andre@0: int templateCount; andre@0: CK_ATTRIBUTE *attrs = keyTemplate; andre@0: andre@0: if (pubKey->keyType != dhKey) { andre@0: PORT_SetError(SEC_ERROR_BAD_KEY); andre@0: break; andre@0: } andre@0: andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &keyClass, sizeof(keyClass)); andre@0: attrs++; andre@0: PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof(keyType)); andre@0: attrs++; andre@0: PK11_SETATTRS(attrs, operation, &cktrue, 1); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_VALUE_LEN, &key_size, sizeof(key_size)); andre@0: attrs++; andre@0: templateCount = attrs - keyTemplate; andre@0: PR_ASSERT(templateCount <= sizeof(keyTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: keyType = PK11_GetKeyType(target,keySize); andre@0: key_size = keySize; andre@0: symKey->size = keySize; andre@0: if (key_size == 0) templateCount--; andre@0: andre@0: mechanism.mechanism = derive; andre@0: andre@0: /* we can undefine these when we define diffie-helman keys */ andre@0: andre@0: mechanism.pParameter = pubKey->u.dh.publicValue.data; andre@0: mechanism.ulParameterLen = pubKey->u.dh.publicValue.len; andre@0: andre@0: pk11_EnterKeyMonitor(symKey); andre@0: crv = PK11_GETTAB(slot)->C_DeriveKey(symKey->session, &mechanism, andre@0: privKey->pkcs11ID, keyTemplate, templateCount, &symKey->objectID); andre@0: pk11_ExitKeyMonitor(symKey); andre@0: if (crv == CKR_OK) return symKey; andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: } andre@0: break; andre@0: case ecKey: andre@0: { andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; andre@0: CK_KEY_TYPE keyType = CKK_GENERIC_SECRET; andre@0: CK_ULONG key_size = 0; andre@0: CK_ATTRIBUTE keyTemplate[4]; andre@0: int templateCount; andre@0: CK_ATTRIBUTE *attrs = keyTemplate; andre@0: CK_ECDH1_DERIVE_PARAMS *mechParams = NULL; andre@0: andre@0: if (pubKey->keyType != ecKey) { andre@0: PORT_SetError(SEC_ERROR_BAD_KEY); andre@0: break; andre@0: } andre@0: andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &keyClass, sizeof(keyClass)); andre@0: attrs++; andre@0: PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof(keyType)); andre@0: attrs++; andre@0: PK11_SETATTRS(attrs, operation, &cktrue, 1); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_VALUE_LEN, &key_size, sizeof(key_size)); andre@0: attrs++; andre@0: templateCount = attrs - keyTemplate; andre@0: PR_ASSERT(templateCount <= sizeof(keyTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: keyType = PK11_GetKeyType(target,keySize); andre@0: key_size = keySize; andre@0: if (key_size == 0) { andre@0: if ((key_size = pk11_GetPredefinedKeyLength(keyType))) { andre@0: templateCount --; andre@0: } else { andre@0: /* sigh, some tokens can't figure this out and require andre@0: * CKA_VALUE_LEN to be set */ andre@0: key_size = SHA1_LENGTH; andre@0: } andre@0: } andre@0: symKey->size = key_size; andre@0: andre@0: mechParams = PORT_ZNew(CK_ECDH1_DERIVE_PARAMS); andre@0: mechParams->kdf = CKD_SHA1_KDF; andre@0: mechParams->ulSharedDataLen = 0; andre@0: mechParams->pSharedData = NULL; andre@0: mechParams->ulPublicDataLen = pubKey->u.ec.publicValue.len; andre@0: mechParams->pPublicData = pubKey->u.ec.publicValue.data; andre@0: andre@0: mechanism.mechanism = derive; andre@0: mechanism.pParameter = mechParams; andre@0: mechanism.ulParameterLen = sizeof(CK_ECDH1_DERIVE_PARAMS); andre@0: andre@0: pk11_EnterKeyMonitor(symKey); andre@0: crv = PK11_GETTAB(slot)->C_DeriveKey(symKey->session, andre@0: &mechanism, privKey->pkcs11ID, keyTemplate, andre@0: templateCount, &symKey->objectID); andre@0: pk11_ExitKeyMonitor(symKey); andre@0: andre@0: /* old PKCS #11 spec was ambiguous on what needed to be passed, andre@0: * try this again with and encoded public key */ andre@0: if (crv != CKR_OK) { andre@0: SECItem *pubValue = SEC_ASN1EncodeItem(NULL, NULL, andre@0: &pubKey->u.ec.publicValue, andre@0: SEC_ASN1_GET(SEC_OctetStringTemplate)); andre@0: if (pubValue == NULL) { andre@0: PORT_ZFree(mechParams, sizeof(CK_ECDH1_DERIVE_PARAMS)); andre@0: break; andre@0: } andre@0: mechParams->ulPublicDataLen = pubValue->len; andre@0: mechParams->pPublicData = pubValue->data; andre@0: andre@0: pk11_EnterKeyMonitor(symKey); andre@0: crv = PK11_GETTAB(slot)->C_DeriveKey(symKey->session, andre@0: &mechanism, privKey->pkcs11ID, keyTemplate, andre@0: templateCount, &symKey->objectID); andre@0: pk11_ExitKeyMonitor(symKey); andre@0: andre@0: SECITEM_FreeItem(pubValue,PR_TRUE); andre@0: } andre@0: andre@0: PORT_ZFree(mechParams, sizeof(CK_ECDH1_DERIVE_PARAMS)); andre@0: andre@0: if (crv == CKR_OK) return symKey; andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: } andre@0: } andre@0: andre@0: PK11_FreeSymKey(symKey); andre@0: return NULL; andre@0: } andre@0: andre@0: /* Returns the size of the public key, or 0 if there andre@0: * is an error. */ andre@0: static CK_ULONG andre@0: pk11_ECPubKeySize(SECItem *publicValue) andre@0: { andre@0: if (publicValue->data[0] == 0x04) { andre@0: /* key encoded in uncompressed form */ andre@0: return((publicValue->len - 1)/2); andre@0: } else if ( (publicValue->data[0] == 0x02) || andre@0: (publicValue->data[0] == 0x03)) { andre@0: /* key encoded in compressed form */ andre@0: return(publicValue->len - 1); andre@0: } andre@0: /* key encoding not recognized */ andre@0: return(0); andre@0: } andre@0: andre@0: static PK11SymKey * andre@0: pk11_PubDeriveECKeyWithKDF( andre@0: SECKEYPrivateKey *privKey, SECKEYPublicKey *pubKey, andre@0: PRBool isSender, SECItem *randomA, SECItem *randomB, andre@0: CK_MECHANISM_TYPE derive, CK_MECHANISM_TYPE target, andre@0: CK_ATTRIBUTE_TYPE operation, int keySize, andre@0: CK_ULONG kdf, SECItem *sharedData, void *wincx) andre@0: { andre@0: PK11SlotInfo *slot = privKey->pkcs11Slot; andre@0: PK11SymKey *symKey; andre@0: PK11SymKey *SharedSecret; andre@0: CK_MECHANISM mechanism; andre@0: CK_RV crv; andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; andre@0: CK_KEY_TYPE keyType = CKK_GENERIC_SECRET; andre@0: CK_ULONG key_size = 0; andre@0: CK_ATTRIBUTE keyTemplate[4]; andre@0: int templateCount; andre@0: CK_ATTRIBUTE *attrs = keyTemplate; andre@0: CK_ECDH1_DERIVE_PARAMS *mechParams = NULL; andre@0: andre@0: if (pubKey->keyType != ecKey) { andre@0: PORT_SetError(SEC_ERROR_BAD_KEY); andre@0: return NULL; andre@0: } andre@0: if ((kdf != CKD_NULL) && (kdf != CKD_SHA1_KDF) && andre@0: (kdf != CKD_SHA224_KDF) && (kdf != CKD_SHA256_KDF) && andre@0: (kdf != CKD_SHA384_KDF) && (kdf != CKD_SHA512_KDF)) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: return NULL; andre@0: } andre@0: andre@0: /* get our key Structure */ andre@0: symKey = pk11_CreateSymKey(slot, target, PR_TRUE, PR_TRUE, wincx); andre@0: if (symKey == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: symKey->origin = PK11_OriginDerive; andre@0: andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &keyClass, sizeof(keyClass)); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof(keyType)); attrs++; andre@0: PK11_SETATTRS(attrs, operation, &cktrue, 1); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_VALUE_LEN, &key_size, sizeof(key_size)); attrs++; andre@0: templateCount = attrs - keyTemplate; andre@0: PR_ASSERT(templateCount <= sizeof(keyTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: keyType = PK11_GetKeyType(target,keySize); andre@0: key_size = keySize; andre@0: if (key_size == 0) { andre@0: if ((key_size = pk11_GetPredefinedKeyLength(keyType))) { andre@0: templateCount --; andre@0: } else { andre@0: /* sigh, some tokens can't figure this out and require andre@0: * CKA_VALUE_LEN to be set */ andre@0: switch (kdf) { andre@0: case CKD_NULL: andre@0: key_size = pk11_ECPubKeySize(&pubKey->u.ec.publicValue); andre@0: if (key_size == 0) { andre@0: PK11_FreeSymKey(symKey); andre@0: return NULL; andre@0: } andre@0: break; andre@0: case CKD_SHA1_KDF: andre@0: key_size = SHA1_LENGTH; andre@0: break; andre@0: case CKD_SHA224_KDF: andre@0: key_size = SHA224_LENGTH; andre@0: break; andre@0: case CKD_SHA256_KDF: andre@0: key_size = SHA256_LENGTH; andre@0: break; andre@0: case CKD_SHA384_KDF: andre@0: key_size = SHA384_LENGTH; andre@0: break; andre@0: case CKD_SHA512_KDF: andre@0: key_size = SHA512_LENGTH; andre@0: break; andre@0: default: andre@0: PORT_Assert(!"Invalid CKD"); andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: return NULL; andre@0: } andre@0: } andre@0: } andre@0: symKey->size = key_size; andre@0: andre@0: mechParams = PORT_ZNew(CK_ECDH1_DERIVE_PARAMS); andre@0: if (!mechParams) { andre@0: PK11_FreeSymKey(symKey); andre@0: return NULL; andre@0: } andre@0: mechParams->kdf = kdf; andre@0: if (sharedData == NULL) { andre@0: mechParams->ulSharedDataLen = 0; andre@0: mechParams->pSharedData = NULL; andre@0: } else { andre@0: mechParams->ulSharedDataLen = sharedData->len; andre@0: mechParams->pSharedData = sharedData->data; andre@0: } andre@0: mechParams->ulPublicDataLen = pubKey->u.ec.publicValue.len; andre@0: mechParams->pPublicData = pubKey->u.ec.publicValue.data; andre@0: andre@0: mechanism.mechanism = derive; andre@0: mechanism.pParameter = mechParams; andre@0: mechanism.ulParameterLen = sizeof(CK_ECDH1_DERIVE_PARAMS); andre@0: andre@0: pk11_EnterKeyMonitor(symKey); andre@0: crv = PK11_GETTAB(slot)->C_DeriveKey(symKey->session, &mechanism, andre@0: privKey->pkcs11ID, keyTemplate, templateCount, &symKey->objectID); andre@0: pk11_ExitKeyMonitor(symKey); andre@0: andre@0: /* old PKCS #11 spec was ambiguous on what needed to be passed, andre@0: * try this again with an encoded public key */ andre@0: if (crv != CKR_OK) { andre@0: SECItem *pubValue = SEC_ASN1EncodeItem(NULL, NULL, andre@0: &pubKey->u.ec.publicValue, andre@0: SEC_ASN1_GET(SEC_OctetStringTemplate)); andre@0: if (pubValue == NULL) { andre@0: goto loser; andre@0: } andre@0: mechParams->ulPublicDataLen = pubValue->len; andre@0: mechParams->pPublicData = pubValue->data; andre@0: andre@0: pk11_EnterKeyMonitor(symKey); andre@0: crv = PK11_GETTAB(slot)->C_DeriveKey(symKey->session, andre@0: &mechanism, privKey->pkcs11ID, keyTemplate, andre@0: templateCount, &symKey->objectID); andre@0: pk11_ExitKeyMonitor(symKey); andre@0: andre@0: if ((crv != CKR_OK) && (kdf != CKD_NULL)) { andre@0: /* Some PKCS #11 libraries cannot perform the key derivation andre@0: * function. So, try calling C_DeriveKey with CKD_NULL and then andre@0: * performing the KDF separately. andre@0: */ andre@0: CK_ULONG derivedKeySize = key_size; andre@0: andre@0: keyType = CKK_GENERIC_SECRET; andre@0: key_size = pk11_ECPubKeySize(&pubKey->u.ec.publicValue); andre@0: if (key_size == 0) { andre@0: SECITEM_FreeItem(pubValue,PR_TRUE); andre@0: goto loser; andre@0: } andre@0: SharedSecret = symKey; andre@0: SharedSecret->size = key_size; andre@0: andre@0: mechParams->kdf = CKD_NULL; andre@0: mechParams->ulSharedDataLen = 0; andre@0: mechParams->pSharedData = NULL; andre@0: mechParams->ulPublicDataLen = pubKey->u.ec.publicValue.len; andre@0: mechParams->pPublicData = pubKey->u.ec.publicValue.data; andre@0: andre@0: pk11_EnterKeyMonitor(SharedSecret); andre@0: crv = PK11_GETTAB(slot)->C_DeriveKey(SharedSecret->session, andre@0: &mechanism, privKey->pkcs11ID, keyTemplate, andre@0: templateCount, &SharedSecret->objectID); andre@0: pk11_ExitKeyMonitor(SharedSecret); andre@0: andre@0: if (crv != CKR_OK) { andre@0: /* old PKCS #11 spec was ambiguous on what needed to be passed, andre@0: * try this one final time with an encoded public key */ andre@0: mechParams->ulPublicDataLen = pubValue->len; andre@0: mechParams->pPublicData = pubValue->data; andre@0: andre@0: pk11_EnterKeyMonitor(SharedSecret); andre@0: crv = PK11_GETTAB(slot)->C_DeriveKey(SharedSecret->session, andre@0: &mechanism, privKey->pkcs11ID, keyTemplate, andre@0: templateCount, &SharedSecret->objectID); andre@0: pk11_ExitKeyMonitor(SharedSecret); andre@0: } andre@0: andre@0: /* Perform KDF. */ andre@0: if (crv == CKR_OK) { andre@0: symKey = pk11_ANSIX963Derive(SharedSecret, kdf, andre@0: sharedData, target, operation, andre@0: derivedKeySize); andre@0: PK11_FreeSymKey(SharedSecret); andre@0: if (symKey == NULL) { andre@0: SECITEM_FreeItem(pubValue,PR_TRUE); andre@0: PORT_ZFree(mechParams, sizeof(CK_ECDH1_DERIVE_PARAMS)); andre@0: return NULL; andre@0: } andre@0: } andre@0: } andre@0: SECITEM_FreeItem(pubValue,PR_TRUE); andre@0: } andre@0: andre@0: loser: andre@0: PORT_ZFree(mechParams, sizeof(CK_ECDH1_DERIVE_PARAMS)); andre@0: andre@0: if (crv != CKR_OK) { andre@0: PK11_FreeSymKey(symKey); andre@0: symKey = NULL; andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: } andre@0: return symKey; andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_PubDeriveWithKDF(SECKEYPrivateKey *privKey, SECKEYPublicKey *pubKey, andre@0: PRBool isSender, SECItem *randomA, SECItem *randomB, andre@0: CK_MECHANISM_TYPE derive, CK_MECHANISM_TYPE target, andre@0: CK_ATTRIBUTE_TYPE operation, int keySize, andre@0: CK_ULONG kdf, SECItem *sharedData, void *wincx) andre@0: { andre@0: andre@0: switch (privKey->keyType) { andre@0: case rsaKey: andre@0: case nullKey: andre@0: case dsaKey: andre@0: case keaKey: andre@0: case fortezzaKey: andre@0: case dhKey: andre@0: return PK11_PubDerive(privKey, pubKey, isSender, randomA, randomB, andre@0: derive, target, operation, keySize, wincx); andre@0: case ecKey: andre@0: return pk11_PubDeriveECKeyWithKDF( privKey, pubKey, isSender, andre@0: randomA, randomB, derive, target, operation, keySize, andre@0: kdf, sharedData, wincx); andre@0: default: andre@0: PORT_SetError(SEC_ERROR_BAD_KEY); andre@0: break; andre@0: } andre@0: andre@0: return NULL; andre@0: } andre@0: andre@0: /* andre@0: * this little function uses the Decrypt function to unwrap a key, just in andre@0: * case we are having problem with unwrap. NOTE: The key size may andre@0: * not be preserved properly for some algorithms! andre@0: */ andre@0: static PK11SymKey * andre@0: pk11_HandUnwrap(PK11SlotInfo *slot, CK_OBJECT_HANDLE wrappingKey, andre@0: CK_MECHANISM *mech, SECItem *inKey, CK_MECHANISM_TYPE target, andre@0: CK_ATTRIBUTE *keyTemplate, unsigned int templateCount, andre@0: int key_size, void * wincx, CK_RV *crvp, PRBool isPerm) andre@0: { andre@0: CK_ULONG len; andre@0: SECItem outKey; andre@0: PK11SymKey *symKey; andre@0: CK_RV crv; andre@0: PRBool owner = PR_TRUE; andre@0: CK_SESSION_HANDLE session; andre@0: andre@0: /* remove any VALUE_LEN parameters */ andre@0: if (keyTemplate[templateCount-1].type == CKA_VALUE_LEN) { andre@0: templateCount--; andre@0: } andre@0: andre@0: /* keys are almost always aligned, but if we get this far, andre@0: * we've gone above and beyond anyway... */ andre@0: outKey.data = (unsigned char*)PORT_Alloc(inKey->len); andre@0: if (outKey.data == NULL) { andre@0: PORT_SetError( SEC_ERROR_NO_MEMORY ); andre@0: if (crvp) *crvp = CKR_HOST_MEMORY; andre@0: return NULL; andre@0: } andre@0: len = inKey->len; andre@0: andre@0: /* use NULL IV's for wrapping */ andre@0: session = pk11_GetNewSession(slot,&owner); andre@0: if (!owner || !(slot->isThreadSafe)) PK11_EnterSlotMonitor(slot); andre@0: crv = PK11_GETTAB(slot)->C_DecryptInit(session,mech,wrappingKey); andre@0: if (crv != CKR_OK) { andre@0: if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); andre@0: pk11_CloseSession(slot,session,owner); andre@0: PORT_Free(outKey.data); andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: if (crvp) *crvp =crv; andre@0: return NULL; andre@0: } andre@0: crv = PK11_GETTAB(slot)->C_Decrypt(session,inKey->data,inKey->len, andre@0: outKey.data, &len); andre@0: if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); andre@0: pk11_CloseSession(slot,session,owner); andre@0: if (crv != CKR_OK) { andre@0: PORT_Free(outKey.data); andre@0: PORT_SetError( PK11_MapError(crv) ); andre@0: if (crvp) *crvp =crv; andre@0: return NULL; andre@0: } andre@0: andre@0: outKey.len = (key_size == 0) ? len : key_size; andre@0: outKey.type = siBuffer; andre@0: andre@0: if (PK11_DoesMechanism(slot,target)) { andre@0: symKey = pk11_ImportSymKeyWithTempl(slot, target, PK11_OriginUnwrap, andre@0: isPerm, keyTemplate, andre@0: templateCount, &outKey, wincx); andre@0: } else { andre@0: slot = PK11_GetBestSlot(target,wincx); andre@0: if (slot == NULL) { andre@0: PORT_SetError( SEC_ERROR_NO_MODULE ); andre@0: PORT_Free(outKey.data); andre@0: if (crvp) *crvp = CKR_DEVICE_ERROR; andre@0: return NULL; andre@0: } andre@0: symKey = pk11_ImportSymKeyWithTempl(slot, target, PK11_OriginUnwrap, andre@0: isPerm, keyTemplate, andre@0: templateCount, &outKey, wincx); andre@0: PK11_FreeSlot(slot); andre@0: } andre@0: PORT_Free(outKey.data); andre@0: andre@0: if (crvp) *crvp = symKey? CKR_OK : CKR_DEVICE_ERROR; andre@0: return symKey; andre@0: } andre@0: andre@0: /* andre@0: * The wrap/unwrap function is pretty much the same for private and andre@0: * public keys. It's just getting the Object ID and slot right. This is andre@0: * the combined unwrap function. andre@0: */ andre@0: static PK11SymKey * andre@0: pk11_AnyUnwrapKey(PK11SlotInfo *slot, CK_OBJECT_HANDLE wrappingKey, andre@0: CK_MECHANISM_TYPE wrapType, SECItem *param, SECItem *wrappedKey, andre@0: CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, int keySize, andre@0: void *wincx, CK_ATTRIBUTE *userAttr, unsigned int numAttrs, PRBool isPerm) andre@0: { andre@0: PK11SymKey * symKey; andre@0: SECItem * param_free = NULL; andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; andre@0: CK_KEY_TYPE keyType = CKK_GENERIC_SECRET; andre@0: CK_ULONG valueLen = 0; andre@0: CK_MECHANISM mechanism; andre@0: CK_SESSION_HANDLE rwsession; andre@0: CK_RV crv; andre@0: CK_MECHANISM_INFO mechanism_info; andre@0: #define MAX_ADD_ATTRS 4 andre@0: CK_ATTRIBUTE keyTemplate[MAX_TEMPL_ATTRS + MAX_ADD_ATTRS]; andre@0: #undef MAX_ADD_ATTRS andre@0: CK_ATTRIBUTE * attrs = keyTemplate; andre@0: unsigned int templateCount; andre@0: andre@0: if (numAttrs > MAX_TEMPL_ATTRS) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: andre@0: /* first copy caller attributes in. */ andre@0: for (templateCount = 0; templateCount < numAttrs; ++templateCount) { andre@0: *attrs++ = *userAttr++; andre@0: } andre@0: andre@0: /* We only add the following attributes to the template if the caller andre@0: ** didn't already supply them. andre@0: */ andre@0: if (!pk11_FindAttrInTemplate(keyTemplate, numAttrs, CKA_CLASS)) { andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &keyClass, sizeof keyClass); andre@0: attrs++; andre@0: } andre@0: if (!pk11_FindAttrInTemplate(keyTemplate, numAttrs, CKA_KEY_TYPE)) { andre@0: keyType = PK11_GetKeyType(target, keySize); andre@0: PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof keyType ); andre@0: attrs++; andre@0: } andre@0: if ((operation != CKA_FLAGS_ONLY) && andre@0: !pk11_FindAttrInTemplate(keyTemplate, numAttrs, operation)) { andre@0: PK11_SETATTRS(attrs, operation, &cktrue, 1); attrs++; andre@0: } andre@0: andre@0: /* andre@0: * must be last in case we need to use this template to import the key andre@0: */ andre@0: if (keySize > 0 && andre@0: !pk11_FindAttrInTemplate(keyTemplate, numAttrs, CKA_VALUE_LEN)) { andre@0: valueLen = (CK_ULONG)keySize; andre@0: PK11_SETATTRS(attrs, CKA_VALUE_LEN, &valueLen, sizeof valueLen); andre@0: attrs++; andre@0: } andre@0: andre@0: templateCount = attrs - keyTemplate; andre@0: PR_ASSERT(templateCount <= sizeof(keyTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: andre@0: /* find out if we can do wrap directly. Because the RSA case if *very* andre@0: * common, cache the results for it. */ andre@0: if ((wrapType == CKM_RSA_PKCS) && (slot->hasRSAInfo)) { andre@0: mechanism_info.flags = slot->RSAInfoFlags; andre@0: } else { andre@0: if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot); andre@0: crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID,wrapType, andre@0: &mechanism_info); andre@0: if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot); andre@0: if (crv != CKR_OK) { andre@0: mechanism_info.flags = 0; andre@0: } andre@0: if (wrapType == CKM_RSA_PKCS) { andre@0: slot->RSAInfoFlags = mechanism_info.flags; andre@0: slot->hasRSAInfo = PR_TRUE; andre@0: } andre@0: } andre@0: andre@0: /* initialize the mechanism structure */ andre@0: mechanism.mechanism = wrapType; andre@0: /* use NULL IV's for wrapping */ andre@0: if (param == NULL) andre@0: param = param_free = PK11_ParamFromIV(wrapType,NULL); andre@0: if (param) { andre@0: mechanism.pParameter = param->data; andre@0: mechanism.ulParameterLen = param->len; andre@0: } else { andre@0: mechanism.pParameter = NULL; andre@0: mechanism.ulParameterLen = 0; andre@0: } andre@0: andre@0: if ((mechanism_info.flags & CKF_DECRYPT) andre@0: && !PK11_DoesMechanism(slot,target)) { andre@0: symKey = pk11_HandUnwrap(slot, wrappingKey, &mechanism, wrappedKey, andre@0: target, keyTemplate, templateCount, keySize, andre@0: wincx, &crv, isPerm); andre@0: if (symKey) { andre@0: if (param_free) SECITEM_FreeItem(param_free,PR_TRUE); andre@0: return symKey; andre@0: } andre@0: /* andre@0: * if the RSA OP simply failed, don't try to unwrap again andre@0: * with this module. andre@0: */ andre@0: if (crv == CKR_DEVICE_ERROR){ andre@0: if (param_free) SECITEM_FreeItem(param_free,PR_TRUE); andre@0: return NULL; andre@0: } andre@0: /* fall through, maybe they incorrectly set CKF_DECRYPT */ andre@0: } andre@0: andre@0: /* get our key Structure */ andre@0: symKey = pk11_CreateSymKey(slot, target, !isPerm, PR_TRUE, wincx); andre@0: if (symKey == NULL) { andre@0: if (param_free) SECITEM_FreeItem(param_free,PR_TRUE); andre@0: return NULL; andre@0: } andre@0: andre@0: symKey->size = keySize; andre@0: symKey->origin = PK11_OriginUnwrap; andre@0: andre@0: if (isPerm) { andre@0: rwsession = PK11_GetRWSession(slot); andre@0: } else { andre@0: pk11_EnterKeyMonitor(symKey); andre@0: rwsession = symKey->session; andre@0: } andre@0: PORT_Assert(rwsession != CK_INVALID_SESSION); andre@0: if (rwsession == CK_INVALID_SESSION) andre@0: crv = CKR_SESSION_HANDLE_INVALID; andre@0: else andre@0: crv = PK11_GETTAB(slot)->C_UnwrapKey(rwsession,&mechanism,wrappingKey, andre@0: wrappedKey->data, wrappedKey->len, keyTemplate, templateCount, andre@0: &symKey->objectID); andre@0: if (isPerm) { andre@0: if (rwsession != CK_INVALID_SESSION) andre@0: PK11_RestoreROSession(slot, rwsession); andre@0: } else { andre@0: pk11_ExitKeyMonitor(symKey); andre@0: } andre@0: if (param_free) SECITEM_FreeItem(param_free,PR_TRUE); andre@0: if (crv != CKR_OK) { andre@0: PK11_FreeSymKey(symKey); andre@0: symKey = NULL; andre@0: if (crv != CKR_DEVICE_ERROR) { andre@0: /* try hand Unwrapping */ andre@0: symKey = pk11_HandUnwrap(slot, wrappingKey, &mechanism, wrappedKey, andre@0: target, keyTemplate, templateCount, andre@0: keySize, wincx, NULL, isPerm); andre@0: } andre@0: } andre@0: andre@0: return symKey; andre@0: } andre@0: andre@0: /* use a symetric key to unwrap another symetric key */ andre@0: PK11SymKey * andre@0: PK11_UnwrapSymKey( PK11SymKey *wrappingKey, CK_MECHANISM_TYPE wrapType, andre@0: SECItem *param, SECItem *wrappedKey, andre@0: CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, andre@0: int keySize) andre@0: { andre@0: return pk11_AnyUnwrapKey(wrappingKey->slot, wrappingKey->objectID, andre@0: wrapType, param, wrappedKey, target, operation, keySize, andre@0: wrappingKey->cx, NULL, 0, PR_FALSE); andre@0: } andre@0: andre@0: /* use a symetric key to unwrap another symetric key */ andre@0: PK11SymKey * andre@0: PK11_UnwrapSymKeyWithFlags(PK11SymKey *wrappingKey, CK_MECHANISM_TYPE wrapType, andre@0: SECItem *param, SECItem *wrappedKey, andre@0: CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, andre@0: int keySize, CK_FLAGS flags) andre@0: { andre@0: CK_BBOOL ckTrue = CK_TRUE; andre@0: CK_ATTRIBUTE keyTemplate[MAX_TEMPL_ATTRS]; andre@0: unsigned int templateCount; andre@0: andre@0: templateCount = pk11_OpFlagsToAttributes(flags, keyTemplate, &ckTrue); andre@0: return pk11_AnyUnwrapKey(wrappingKey->slot, wrappingKey->objectID, andre@0: wrapType, param, wrappedKey, target, operation, keySize, andre@0: wrappingKey->cx, keyTemplate, templateCount, PR_FALSE); andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_UnwrapSymKeyWithFlagsPerm(PK11SymKey *wrappingKey, andre@0: CK_MECHANISM_TYPE wrapType, andre@0: SECItem *param, SECItem *wrappedKey, andre@0: CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, andre@0: int keySize, CK_FLAGS flags, PRBool isPerm) andre@0: { andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_ATTRIBUTE keyTemplate[MAX_TEMPL_ATTRS]; andre@0: CK_ATTRIBUTE *attrs; andre@0: unsigned int templateCount; andre@0: andre@0: attrs = keyTemplate; andre@0: if (isPerm) { andre@0: PK11_SETATTRS(attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL)); attrs++; andre@0: } andre@0: templateCount = attrs-keyTemplate; andre@0: templateCount += pk11_OpFlagsToAttributes(flags, attrs, &cktrue); andre@0: andre@0: return pk11_AnyUnwrapKey(wrappingKey->slot, wrappingKey->objectID, andre@0: wrapType, param, wrappedKey, target, operation, keySize, andre@0: wrappingKey->cx, keyTemplate, templateCount, isPerm); andre@0: } andre@0: andre@0: andre@0: /* unwrap a symetric key with a private key. */ andre@0: PK11SymKey * andre@0: PK11_PubUnwrapSymKey(SECKEYPrivateKey *wrappingKey, SECItem *wrappedKey, andre@0: CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, int keySize) andre@0: { andre@0: CK_MECHANISM_TYPE wrapType = pk11_mapWrapKeyType(wrappingKey->keyType); andre@0: PK11SlotInfo *slot = wrappingKey->pkcs11Slot; andre@0: andre@0: if (SECKEY_HAS_ATTRIBUTE_SET(wrappingKey,CKA_PRIVATE)) { andre@0: PK11_HandlePasswordCheck(slot,wrappingKey->wincx); andre@0: } andre@0: andre@0: return pk11_AnyUnwrapKey(slot, wrappingKey->pkcs11ID, andre@0: wrapType, NULL, wrappedKey, target, operation, keySize, andre@0: wrappingKey->wincx, NULL, 0, PR_FALSE); andre@0: } andre@0: andre@0: /* unwrap a symetric key with a private key. */ andre@0: PK11SymKey * andre@0: PK11_PubUnwrapSymKeyWithFlags(SECKEYPrivateKey *wrappingKey, andre@0: SECItem *wrappedKey, CK_MECHANISM_TYPE target, andre@0: CK_ATTRIBUTE_TYPE operation, int keySize, CK_FLAGS flags) andre@0: { andre@0: CK_MECHANISM_TYPE wrapType = pk11_mapWrapKeyType(wrappingKey->keyType); andre@0: CK_BBOOL ckTrue = CK_TRUE; andre@0: CK_ATTRIBUTE keyTemplate[MAX_TEMPL_ATTRS]; andre@0: unsigned int templateCount; andre@0: PK11SlotInfo *slot = wrappingKey->pkcs11Slot; andre@0: andre@0: templateCount = pk11_OpFlagsToAttributes(flags, keyTemplate, &ckTrue); andre@0: andre@0: if (SECKEY_HAS_ATTRIBUTE_SET(wrappingKey,CKA_PRIVATE)) { andre@0: PK11_HandlePasswordCheck(slot,wrappingKey->wincx); andre@0: } andre@0: andre@0: return pk11_AnyUnwrapKey(slot, wrappingKey->pkcs11ID, andre@0: wrapType, NULL, wrappedKey, target, operation, keySize, andre@0: wrappingKey->wincx, keyTemplate, templateCount, PR_FALSE); andre@0: } andre@0: andre@0: PK11SymKey * andre@0: PK11_PubUnwrapSymKeyWithFlagsPerm(SECKEYPrivateKey *wrappingKey, andre@0: SECItem *wrappedKey, CK_MECHANISM_TYPE target, andre@0: CK_ATTRIBUTE_TYPE operation, int keySize, andre@0: CK_FLAGS flags, PRBool isPerm) andre@0: { andre@0: CK_MECHANISM_TYPE wrapType = pk11_mapWrapKeyType(wrappingKey->keyType); andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_ATTRIBUTE keyTemplate[MAX_TEMPL_ATTRS]; andre@0: CK_ATTRIBUTE *attrs; andre@0: unsigned int templateCount; andre@0: PK11SlotInfo *slot = wrappingKey->pkcs11Slot; andre@0: andre@0: attrs = keyTemplate; andre@0: if (isPerm) { andre@0: PK11_SETATTRS(attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL)); attrs++; andre@0: } andre@0: templateCount = attrs-keyTemplate; andre@0: andre@0: templateCount += pk11_OpFlagsToAttributes(flags, attrs, &cktrue); andre@0: andre@0: if (SECKEY_HAS_ATTRIBUTE_SET(wrappingKey,CKA_PRIVATE)) { andre@0: PK11_HandlePasswordCheck(slot,wrappingKey->wincx); andre@0: } andre@0: andre@0: return pk11_AnyUnwrapKey(slot, wrappingKey->pkcs11ID, andre@0: wrapType, NULL, wrappedKey, target, operation, keySize, andre@0: wrappingKey->wincx, keyTemplate, templateCount, isPerm); andre@0: } andre@0: andre@0: PK11SymKey* andre@0: PK11_CopySymKeyForSigning(PK11SymKey *originalKey, CK_MECHANISM_TYPE mech) andre@0: { andre@0: CK_RV crv; andre@0: CK_ATTRIBUTE setTemplate; andre@0: CK_BBOOL ckTrue = CK_TRUE; andre@0: PK11SlotInfo *slot = originalKey->slot; andre@0: andre@0: /* first just try to set this key up for signing */ andre@0: PK11_SETATTRS(&setTemplate, CKA_SIGN, &ckTrue, sizeof(ckTrue)); andre@0: pk11_EnterKeyMonitor(originalKey); andre@0: crv = PK11_GETTAB(slot)-> C_SetAttributeValue(originalKey->session, andre@0: originalKey->objectID, &setTemplate, 1); andre@0: pk11_ExitKeyMonitor(originalKey); andre@0: if (crv == CKR_OK) { andre@0: return PK11_ReferenceSymKey(originalKey); andre@0: } andre@0: andre@0: /* nope, doesn't like it, use the pk11 copy object command */ andre@0: return pk11_CopyToSlot(slot, mech, CKA_SIGN, originalKey); andre@0: } andre@0: andre@0: void andre@0: PK11_SetFortezzaHack(PK11SymKey *symKey) { andre@0: symKey->origin = PK11_OriginFortezzaHack; andre@0: } andre@0: andre@0: /* andre@0: * This is required to allow FORTEZZA_NULL and FORTEZZA_RC4 andre@0: * working. This function simply gets a valid IV for the keys. andre@0: */ andre@0: SECStatus andre@0: PK11_GenerateFortezzaIV(PK11SymKey *symKey,unsigned char *iv,int len) andre@0: { andre@0: CK_MECHANISM mech_info; andre@0: CK_ULONG count = 0; andre@0: CK_RV crv; andre@0: SECStatus rv = SECFailure; andre@0: andre@0: mech_info.mechanism = CKM_SKIPJACK_CBC64; andre@0: mech_info.pParameter = iv; andre@0: mech_info.ulParameterLen = len; andre@0: andre@0: /* generate the IV for fortezza */ andre@0: PK11_EnterSlotMonitor(symKey->slot); andre@0: crv=PK11_GETTAB(symKey->slot)->C_EncryptInit(symKey->slot->session, andre@0: &mech_info, symKey->objectID); andre@0: if (crv == CKR_OK) { andre@0: PK11_GETTAB(symKey->slot)->C_EncryptFinal(symKey->slot->session, andre@0: NULL, &count); andre@0: rv = SECSuccess; andre@0: } andre@0: PK11_ExitSlotMonitor(symKey->slot); andre@0: return rv; andre@0: } andre@0: andre@0: CK_OBJECT_HANDLE andre@0: PK11_GetSymKeyHandle(PK11SymKey *symKey) andre@0: { andre@0: return symKey->objectID; andre@0: } andre@0: