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 "key.h" michael@0: #include "secasn1.h" michael@0: #include "sechash.h" michael@0: #include "cert.h" michael@0: #include "secerr.h" michael@0: michael@0: /* michael@0: * find an RSA public key on a card michael@0: */ michael@0: static CK_OBJECT_HANDLE michael@0: pk11_FindRSAPubKey(PK11SlotInfo *slot) michael@0: { michael@0: CK_KEY_TYPE key_type = CKK_RSA; michael@0: CK_OBJECT_CLASS class_type = CKO_PUBLIC_KEY; michael@0: CK_ATTRIBUTE theTemplate[2]; michael@0: int template_count = sizeof(theTemplate)/sizeof(theTemplate[0]); michael@0: CK_ATTRIBUTE *attrs = theTemplate; michael@0: michael@0: PK11_SETATTRS(attrs,CKA_CLASS,&class_type,sizeof(class_type)); attrs++; michael@0: PK11_SETATTRS(attrs,CKA_KEY_TYPE,&key_type,sizeof(key_type)); attrs++; michael@0: template_count = attrs - theTemplate; michael@0: PR_ASSERT(template_count <= sizeof(theTemplate)/sizeof(CK_ATTRIBUTE)); michael@0: michael@0: return pk11_FindObjectByTemplate(slot,theTemplate,template_count); michael@0: } michael@0: michael@0: PK11SymKey * michael@0: pk11_KeyExchange(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: PK11SymKey *newSymKey = NULL; michael@0: SECStatus rv; michael@0: /* performance improvement can go here --- use a generated key at startup michael@0: * to generate a per token wrapping key. If it exists, use it, otherwise michael@0: * do a full key exchange. */ michael@0: michael@0: /* find a common Key Exchange algorithm */ michael@0: /* RSA */ michael@0: if (PK11_DoesMechanism(symKey->slot, CKM_RSA_PKCS) && michael@0: PK11_DoesMechanism(slot,CKM_RSA_PKCS)) { michael@0: CK_OBJECT_HANDLE pubKeyHandle = CK_INVALID_HANDLE; michael@0: CK_OBJECT_HANDLE privKeyHandle = CK_INVALID_HANDLE; michael@0: SECKEYPublicKey *pubKey = NULL; michael@0: SECKEYPrivateKey *privKey = NULL; michael@0: SECItem wrapData; michael@0: unsigned int symKeyLength = PK11_GetKeyLength(symKey); michael@0: michael@0: wrapData.data = NULL; michael@0: michael@0: /* find RSA Public Key on target */ michael@0: pubKeyHandle = pk11_FindRSAPubKey(slot); michael@0: if (pubKeyHandle != CK_INVALID_HANDLE) { michael@0: privKeyHandle = PK11_MatchItem(slot,pubKeyHandle,CKO_PRIVATE_KEY); michael@0: } michael@0: michael@0: /* if no key exists, generate a key pair */ michael@0: if (privKeyHandle == CK_INVALID_HANDLE) { michael@0: PK11RSAGenParams rsaParams; michael@0: michael@0: if (symKeyLength > 53) /* bytes */ { michael@0: /* we'd have to generate an RSA key pair > 512 bits long, michael@0: ** and that's too costly. Don't even try. michael@0: */ michael@0: PORT_SetError( SEC_ERROR_CANNOT_MOVE_SENSITIVE_KEY ); michael@0: goto rsa_failed; michael@0: } michael@0: rsaParams.keySizeInBits = michael@0: (symKeyLength > 21 || symKeyLength == 0) ? 512 : 256; michael@0: rsaParams.pe = 0x10001; michael@0: privKey = PK11_GenerateKeyPair(slot,CKM_RSA_PKCS_KEY_PAIR_GEN, michael@0: &rsaParams, &pubKey,PR_FALSE,PR_TRUE,symKey->cx); michael@0: } else { michael@0: /* if keys exist, build SECKEY data structures for them */ michael@0: privKey = PK11_MakePrivKey(slot,nullKey, PR_TRUE, privKeyHandle, michael@0: symKey->cx); michael@0: if (privKey != NULL) { michael@0: pubKey = PK11_ExtractPublicKey(slot, rsaKey, pubKeyHandle); michael@0: if (pubKey && pubKey->pkcs11Slot) { michael@0: PK11_FreeSlot(pubKey->pkcs11Slot); michael@0: pubKey->pkcs11Slot = NULL; michael@0: pubKey->pkcs11ID = CK_INVALID_HANDLE; michael@0: } michael@0: } michael@0: } michael@0: if (privKey == NULL) goto rsa_failed; michael@0: if (pubKey == NULL) goto rsa_failed; michael@0: michael@0: wrapData.len = SECKEY_PublicKeyStrength(pubKey); michael@0: if (!wrapData.len) goto rsa_failed; michael@0: wrapData.data = PORT_Alloc(wrapData.len); michael@0: if (wrapData.data == NULL) goto rsa_failed; michael@0: michael@0: /* now wrap the keys in and out */ michael@0: rv = PK11_PubWrapSymKey(CKM_RSA_PKCS, pubKey, symKey, &wrapData); michael@0: if (rv == SECSuccess) { michael@0: newSymKey = PK11_PubUnwrapSymKeyWithFlagsPerm(privKey, michael@0: &wrapData,type,operation,symKeyLength,flags,isPerm); michael@0: /* make sure we wound up where we wanted to be! */ michael@0: if (newSymKey && newSymKey->slot != slot) { michael@0: PK11_FreeSymKey(newSymKey); michael@0: newSymKey = NULL; michael@0: } michael@0: } michael@0: rsa_failed: michael@0: if (wrapData.data != NULL) PORT_Free(wrapData.data); michael@0: if (privKey != NULL) SECKEY_DestroyPrivateKey(privKey); michael@0: if (pubKey != NULL) SECKEY_DestroyPublicKey(pubKey); michael@0: michael@0: return newSymKey; michael@0: } michael@0: PORT_SetError( SEC_ERROR_NO_MODULE ); michael@0: return NULL; michael@0: } michael@0: