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: /* michael@0: * CMS encryptedData methods. michael@0: */ michael@0: michael@0: #include "cmslocal.h" michael@0: michael@0: #include "key.h" michael@0: #include "secasn1.h" michael@0: #include "secitem.h" michael@0: #include "secoid.h" michael@0: #include "pk11func.h" michael@0: #include "prtime.h" michael@0: #include "secerr.h" michael@0: #include "secpkcs5.h" michael@0: michael@0: /* michael@0: * NSS_CMSEncryptedData_Create - create an empty encryptedData object. michael@0: * michael@0: * "algorithm" specifies the bulk encryption algorithm to use. michael@0: * "keysize" is the key size. michael@0: * michael@0: * An error results in a return value of NULL and an error set. michael@0: * (Retrieve specific errors via PORT_GetError()/XP_GetError().) michael@0: */ michael@0: NSSCMSEncryptedData * michael@0: NSS_CMSEncryptedData_Create(NSSCMSMessage *cmsg, SECOidTag algorithm, michael@0: int keysize) michael@0: { michael@0: void *mark; michael@0: NSSCMSEncryptedData *encd; michael@0: PLArenaPool *poolp; michael@0: SECAlgorithmID *pbe_algid; michael@0: SECStatus rv; michael@0: michael@0: poolp = cmsg->poolp; michael@0: michael@0: mark = PORT_ArenaMark(poolp); michael@0: michael@0: encd = PORT_ArenaZNew(poolp, NSSCMSEncryptedData); michael@0: if (encd == NULL) michael@0: goto loser; michael@0: michael@0: encd->cmsg = cmsg; michael@0: michael@0: /* version is set in NSS_CMSEncryptedData_Encode_BeforeStart() */ michael@0: michael@0: if (!SEC_PKCS5IsAlgorithmPBEAlgTag(algorithm)) { michael@0: rv = NSS_CMSContentInfo_SetContentEncAlg(poolp, &(encd->contentInfo), michael@0: algorithm, NULL, keysize); michael@0: } else { michael@0: /* Assume password-based-encryption. michael@0: * Note: we can't generate pkcs5v2 from this interface. michael@0: * PK11_CreateBPEAlgorithmID generates pkcs5v2 by accepting michael@0: * non-PBE oids and assuming that they are pkcs5v2 oids, but michael@0: * NSS_CMSEncryptedData_Create accepts non-PBE oids as regular michael@0: * CMS encrypted data, so we can't tell NSS_CMS_EncryptedData_Create michael@0: * to create pkcs5v2 PBEs */ michael@0: pbe_algid = PK11_CreatePBEAlgorithmID(algorithm, 1, NULL); michael@0: if (pbe_algid == NULL) { michael@0: rv = SECFailure; michael@0: } else { michael@0: rv = NSS_CMSContentInfo_SetContentEncAlgID(poolp, michael@0: &(encd->contentInfo), pbe_algid, keysize); michael@0: SECOID_DestroyAlgorithmID (pbe_algid, PR_TRUE); michael@0: } michael@0: } michael@0: if (rv != SECSuccess) michael@0: goto loser; michael@0: michael@0: PORT_ArenaUnmark(poolp, mark); michael@0: return encd; michael@0: michael@0: loser: michael@0: PORT_ArenaRelease(poolp, mark); michael@0: return NULL; michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSEncryptedData_Destroy - destroy an encryptedData object michael@0: */ michael@0: void michael@0: NSS_CMSEncryptedData_Destroy(NSSCMSEncryptedData *encd) michael@0: { michael@0: /* everything's in a pool, so don't worry about the storage */ michael@0: NSS_CMSContentInfo_Destroy(&(encd->contentInfo)); michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSEncryptedData_GetContentInfo - return pointer to encryptedData object's contentInfo michael@0: */ michael@0: NSSCMSContentInfo * michael@0: NSS_CMSEncryptedData_GetContentInfo(NSSCMSEncryptedData *encd) michael@0: { michael@0: return &(encd->contentInfo); michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSEncryptedData_Encode_BeforeStart - do all the necessary things to a EncryptedData michael@0: * before encoding begins. michael@0: * michael@0: * In particular: michael@0: * - set the correct version value. michael@0: * - get the encryption key michael@0: */ michael@0: SECStatus michael@0: NSS_CMSEncryptedData_Encode_BeforeStart(NSSCMSEncryptedData *encd) michael@0: { michael@0: int version; michael@0: PK11SymKey *bulkkey = NULL; michael@0: SECItem *dummy; michael@0: NSSCMSContentInfo *cinfo = &(encd->contentInfo); michael@0: michael@0: if (NSS_CMSArray_IsEmpty((void **)encd->unprotectedAttr)) michael@0: version = NSS_CMS_ENCRYPTED_DATA_VERSION; michael@0: else michael@0: version = NSS_CMS_ENCRYPTED_DATA_VERSION_UPATTR; michael@0: michael@0: dummy = SEC_ASN1EncodeInteger (encd->cmsg->poolp, &(encd->version), version); michael@0: if (dummy == NULL) michael@0: return SECFailure; michael@0: michael@0: /* now get content encryption key (bulk key) by using our cmsg callback */ michael@0: if (encd->cmsg->decrypt_key_cb) michael@0: bulkkey = (*encd->cmsg->decrypt_key_cb)(encd->cmsg->decrypt_key_cb_arg, michael@0: NSS_CMSContentInfo_GetContentEncAlg(cinfo)); michael@0: if (bulkkey == NULL) michael@0: return SECFailure; michael@0: michael@0: /* store the bulk key in the contentInfo so that the encoder can find it */ michael@0: NSS_CMSContentInfo_SetBulkKey(cinfo, bulkkey); michael@0: PK11_FreeSymKey (bulkkey); michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSEncryptedData_Encode_BeforeData - set up encryption michael@0: */ michael@0: SECStatus michael@0: NSS_CMSEncryptedData_Encode_BeforeData(NSSCMSEncryptedData *encd) michael@0: { michael@0: NSSCMSContentInfo *cinfo; michael@0: PK11SymKey *bulkkey; michael@0: SECAlgorithmID *algid; michael@0: SECStatus rv; michael@0: michael@0: cinfo = &(encd->contentInfo); michael@0: michael@0: /* find bulkkey and algorithm - must have been set by NSS_CMSEncryptedData_Encode_BeforeStart */ michael@0: bulkkey = NSS_CMSContentInfo_GetBulkKey(cinfo); michael@0: if (bulkkey == NULL) michael@0: return SECFailure; michael@0: algid = NSS_CMSContentInfo_GetContentEncAlg(cinfo); michael@0: if (algid == NULL) michael@0: return SECFailure; michael@0: michael@0: rv = NSS_CMSContentInfo_Private_Init(cinfo); michael@0: if (rv != SECSuccess) { michael@0: return SECFailure; michael@0: } michael@0: /* this may modify algid (with IVs generated in a token). michael@0: * it is therefore essential that algid is a pointer to the "real" contentEncAlg, michael@0: * not just to a copy */ michael@0: cinfo->privateInfo->ciphcx = NSS_CMSCipherContext_StartEncrypt(encd->cmsg->poolp, bulkkey, algid); michael@0: PK11_FreeSymKey(bulkkey); michael@0: if (cinfo->privateInfo->ciphcx == NULL) michael@0: return SECFailure; michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSEncryptedData_Encode_AfterData - finalize this encryptedData for encoding michael@0: */ michael@0: SECStatus michael@0: NSS_CMSEncryptedData_Encode_AfterData(NSSCMSEncryptedData *encd) michael@0: { michael@0: if (encd->contentInfo.privateInfo && encd->contentInfo.privateInfo->ciphcx) { michael@0: NSS_CMSCipherContext_Destroy(encd->contentInfo.privateInfo->ciphcx); michael@0: encd->contentInfo.privateInfo->ciphcx = NULL; michael@0: } michael@0: michael@0: /* nothing to do after data */ michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * NSS_CMSEncryptedData_Decode_BeforeData - find bulk key & set up decryption michael@0: */ michael@0: SECStatus michael@0: NSS_CMSEncryptedData_Decode_BeforeData(NSSCMSEncryptedData *encd) michael@0: { michael@0: PK11SymKey *bulkkey = NULL; michael@0: NSSCMSContentInfo *cinfo; michael@0: SECAlgorithmID *bulkalg; michael@0: SECStatus rv = SECFailure; michael@0: michael@0: cinfo = &(encd->contentInfo); michael@0: michael@0: bulkalg = NSS_CMSContentInfo_GetContentEncAlg(cinfo); michael@0: michael@0: if (encd->cmsg->decrypt_key_cb == NULL) /* no callback? no key../ */ michael@0: goto loser; michael@0: michael@0: bulkkey = (*encd->cmsg->decrypt_key_cb)(encd->cmsg->decrypt_key_cb_arg, bulkalg); michael@0: if (bulkkey == NULL) michael@0: /* no success finding a bulk key */ michael@0: goto loser; michael@0: michael@0: NSS_CMSContentInfo_SetBulkKey(cinfo, bulkkey); michael@0: michael@0: rv = NSS_CMSContentInfo_Private_Init(cinfo); michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: rv = SECFailure; michael@0: michael@0: cinfo->privateInfo->ciphcx = NSS_CMSCipherContext_StartDecrypt(bulkkey, bulkalg); michael@0: if (cinfo->privateInfo->ciphcx == NULL) michael@0: goto loser; /* error has been set by NSS_CMSCipherContext_StartDecrypt */ michael@0: michael@0: michael@0: /* we are done with (this) bulkkey now. */ michael@0: PK11_FreeSymKey(bulkkey); michael@0: michael@0: rv = SECSuccess; michael@0: michael@0: loser: michael@0: return rv; michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSEncryptedData_Decode_AfterData - finish decrypting this encryptedData's content michael@0: */ michael@0: SECStatus michael@0: NSS_CMSEncryptedData_Decode_AfterData(NSSCMSEncryptedData *encd) michael@0: { michael@0: if (encd->contentInfo.privateInfo && encd->contentInfo.privateInfo->ciphcx) { michael@0: NSS_CMSCipherContext_Destroy(encd->contentInfo.privateInfo->ciphcx); michael@0: encd->contentInfo.privateInfo->ciphcx = NULL; michael@0: } michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSEncryptedData_Decode_AfterEnd - finish decoding this encryptedData michael@0: */ michael@0: SECStatus michael@0: NSS_CMSEncryptedData_Decode_AfterEnd(NSSCMSEncryptedData *encd) michael@0: { michael@0: /* apply final touches */ michael@0: return SECSuccess; michael@0: }