andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: /* andre@0: * This file implements the Symkey wrapper and the PKCS context andre@0: * Interfaces. andre@0: */ andre@0: andre@0: #include "seccomon.h" andre@0: #include "secmod.h" andre@0: #include "nssilock.h" andre@0: #include "secmodi.h" andre@0: #include "secmodti.h" andre@0: #include "pkcs11.h" andre@0: #include "pk11func.h" andre@0: #include "secitem.h" andre@0: #include "key.h" andre@0: #include "secasn1.h" andre@0: #include "sechash.h" andre@0: #include "cert.h" andre@0: #include "secerr.h" andre@0: andre@0: /* andre@0: * find an RSA public key on a card andre@0: */ andre@0: static CK_OBJECT_HANDLE andre@0: pk11_FindRSAPubKey(PK11SlotInfo *slot) andre@0: { andre@0: CK_KEY_TYPE key_type = CKK_RSA; andre@0: CK_OBJECT_CLASS class_type = CKO_PUBLIC_KEY; andre@0: CK_ATTRIBUTE theTemplate[2]; andre@0: int template_count = sizeof(theTemplate)/sizeof(theTemplate[0]); andre@0: CK_ATTRIBUTE *attrs = theTemplate; andre@0: andre@0: PK11_SETATTRS(attrs,CKA_CLASS,&class_type,sizeof(class_type)); attrs++; andre@0: PK11_SETATTRS(attrs,CKA_KEY_TYPE,&key_type,sizeof(key_type)); attrs++; andre@0: template_count = attrs - theTemplate; andre@0: PR_ASSERT(template_count <= sizeof(theTemplate)/sizeof(CK_ATTRIBUTE)); andre@0: andre@0: return pk11_FindObjectByTemplate(slot,theTemplate,template_count); andre@0: } andre@0: andre@0: PK11SymKey * andre@0: pk11_KeyExchange(PK11SlotInfo *slot,CK_MECHANISM_TYPE type, andre@0: CK_ATTRIBUTE_TYPE operation, CK_FLAGS flags, andre@0: PRBool isPerm, PK11SymKey *symKey) andre@0: { andre@0: PK11SymKey *newSymKey = NULL; andre@0: SECStatus rv; andre@0: /* performance improvement can go here --- use a generated key at startup andre@0: * to generate a per token wrapping key. If it exists, use it, otherwise andre@0: * do a full key exchange. */ andre@0: andre@0: /* find a common Key Exchange algorithm */ andre@0: /* RSA */ andre@0: if (PK11_DoesMechanism(symKey->slot, CKM_RSA_PKCS) && andre@0: PK11_DoesMechanism(slot,CKM_RSA_PKCS)) { andre@0: CK_OBJECT_HANDLE pubKeyHandle = CK_INVALID_HANDLE; andre@0: CK_OBJECT_HANDLE privKeyHandle = CK_INVALID_HANDLE; andre@0: SECKEYPublicKey *pubKey = NULL; andre@0: SECKEYPrivateKey *privKey = NULL; andre@0: SECItem wrapData; andre@0: unsigned int symKeyLength = PK11_GetKeyLength(symKey); andre@0: andre@0: wrapData.data = NULL; andre@0: andre@0: /* find RSA Public Key on target */ andre@0: pubKeyHandle = pk11_FindRSAPubKey(slot); andre@0: if (pubKeyHandle != CK_INVALID_HANDLE) { andre@0: privKeyHandle = PK11_MatchItem(slot,pubKeyHandle,CKO_PRIVATE_KEY); andre@0: } andre@0: andre@0: /* if no key exists, generate a key pair */ andre@0: if (privKeyHandle == CK_INVALID_HANDLE) { andre@0: PK11RSAGenParams rsaParams; andre@0: andre@0: if (symKeyLength > 53) /* bytes */ { andre@0: /* we'd have to generate an RSA key pair > 512 bits long, andre@0: ** and that's too costly. Don't even try. andre@0: */ andre@0: PORT_SetError( SEC_ERROR_CANNOT_MOVE_SENSITIVE_KEY ); andre@0: goto rsa_failed; andre@0: } andre@0: rsaParams.keySizeInBits = andre@0: (symKeyLength > 21 || symKeyLength == 0) ? 512 : 256; andre@0: rsaParams.pe = 0x10001; andre@0: privKey = PK11_GenerateKeyPair(slot,CKM_RSA_PKCS_KEY_PAIR_GEN, andre@0: &rsaParams, &pubKey,PR_FALSE,PR_TRUE,symKey->cx); andre@0: } else { andre@0: /* if keys exist, build SECKEY data structures for them */ andre@0: privKey = PK11_MakePrivKey(slot,nullKey, PR_TRUE, privKeyHandle, andre@0: symKey->cx); andre@0: if (privKey != NULL) { andre@0: pubKey = PK11_ExtractPublicKey(slot, rsaKey, pubKeyHandle); andre@0: if (pubKey && pubKey->pkcs11Slot) { andre@0: PK11_FreeSlot(pubKey->pkcs11Slot); andre@0: pubKey->pkcs11Slot = NULL; andre@0: pubKey->pkcs11ID = CK_INVALID_HANDLE; andre@0: } andre@0: } andre@0: } andre@0: if (privKey == NULL) goto rsa_failed; andre@0: if (pubKey == NULL) goto rsa_failed; andre@0: andre@0: wrapData.len = SECKEY_PublicKeyStrength(pubKey); andre@0: if (!wrapData.len) goto rsa_failed; andre@0: wrapData.data = PORT_Alloc(wrapData.len); andre@0: if (wrapData.data == NULL) goto rsa_failed; andre@0: andre@0: /* now wrap the keys in and out */ andre@0: rv = PK11_PubWrapSymKey(CKM_RSA_PKCS, pubKey, symKey, &wrapData); andre@0: if (rv == SECSuccess) { andre@0: newSymKey = PK11_PubUnwrapSymKeyWithFlagsPerm(privKey, andre@0: &wrapData,type,operation,symKeyLength,flags,isPerm); andre@0: /* make sure we wound up where we wanted to be! */ andre@0: if (newSymKey && newSymKey->slot != slot) { andre@0: PK11_FreeSymKey(newSymKey); andre@0: newSymKey = NULL; andre@0: } andre@0: } andre@0: rsa_failed: andre@0: if (wrapData.data != NULL) PORT_Free(wrapData.data); andre@0: if (privKey != NULL) SECKEY_DestroyPrivateKey(privKey); andre@0: if (pubKey != NULL) SECKEY_DestroyPublicKey(pubKey); andre@0: andre@0: return newSymKey; andre@0: } andre@0: PORT_SetError( SEC_ERROR_NO_MODULE ); andre@0: return NULL; andre@0: } andre@0: