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