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 recipient list functions michael@0: */ michael@0: michael@0: #include "cmslocal.h" michael@0: michael@0: #include "cert.h" 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: michael@0: static int michael@0: nss_cms_recipients_traverse(NSSCMSRecipientInfo **recipientinfos, NSSCMSRecipient **recipient_list) michael@0: { michael@0: int count = 0; michael@0: int rlindex = 0; michael@0: int i, j; michael@0: NSSCMSRecipient *rle; michael@0: NSSCMSRecipientInfo *ri; michael@0: NSSCMSRecipientEncryptedKey *rek; michael@0: michael@0: for (i = 0; recipientinfos[i] != NULL; i++) { michael@0: ri = recipientinfos[i]; michael@0: switch (ri->recipientInfoType) { michael@0: case NSSCMSRecipientInfoID_KeyTrans: michael@0: if (recipient_list) { michael@0: NSSCMSRecipientIdentifier *recipId = michael@0: &ri->ri.keyTransRecipientInfo.recipientIdentifier; michael@0: michael@0: if (recipId->identifierType != NSSCMSRecipientID_IssuerSN && michael@0: recipId->identifierType != NSSCMSRecipientID_SubjectKeyID) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return -1; michael@0: } michael@0: /* alloc one & fill it out */ michael@0: rle = (NSSCMSRecipient *)PORT_ZAlloc(sizeof(NSSCMSRecipient)); michael@0: if (!rle) michael@0: return -1; michael@0: michael@0: rle->riIndex = i; michael@0: rle->subIndex = -1; michael@0: switch (recipId->identifierType) { michael@0: case NSSCMSRecipientID_IssuerSN: michael@0: rle->kind = RLIssuerSN; michael@0: rle->id.issuerAndSN = recipId->id.issuerAndSN; michael@0: break; michael@0: case NSSCMSRecipientID_SubjectKeyID: michael@0: rle->kind = RLSubjKeyID; michael@0: rle->id.subjectKeyID = recipId->id.subjectKeyID; michael@0: break; michael@0: default: /* we never get here because of identifierType check michael@0: we done before. Leaving it to kill compiler warning */ michael@0: break; michael@0: } michael@0: recipient_list[rlindex++] = rle; michael@0: } else { michael@0: count++; michael@0: } michael@0: break; michael@0: case NSSCMSRecipientInfoID_KeyAgree: michael@0: if (ri->ri.keyAgreeRecipientInfo.recipientEncryptedKeys == NULL) michael@0: break; michael@0: for (j=0; ri->ri.keyAgreeRecipientInfo.recipientEncryptedKeys[j] != NULL; j++) { michael@0: if (recipient_list) { michael@0: rek = ri->ri.keyAgreeRecipientInfo.recipientEncryptedKeys[j]; michael@0: /* alloc one & fill it out */ michael@0: rle = (NSSCMSRecipient *)PORT_ZAlloc(sizeof(NSSCMSRecipient)); michael@0: if (!rle) michael@0: return -1; michael@0: michael@0: rle->riIndex = i; michael@0: rle->subIndex = j; michael@0: switch (rek->recipientIdentifier.identifierType) { michael@0: case NSSCMSKeyAgreeRecipientID_IssuerSN: michael@0: rle->kind = RLIssuerSN; michael@0: rle->id.issuerAndSN = rek->recipientIdentifier.id.issuerAndSN; michael@0: break; michael@0: case NSSCMSKeyAgreeRecipientID_RKeyID: michael@0: rle->kind = RLSubjKeyID; michael@0: rle->id.subjectKeyID = rek->recipientIdentifier.id.recipientKeyIdentifier.subjectKeyIdentifier; michael@0: break; michael@0: } michael@0: recipient_list[rlindex++] = rle; michael@0: } else { michael@0: count++; michael@0: } michael@0: } michael@0: break; michael@0: case NSSCMSRecipientInfoID_KEK: michael@0: /* KEK is not implemented */ michael@0: break; michael@0: } michael@0: } michael@0: /* if we have a recipient list, we return on success (-1, above, on failure) */ michael@0: /* otherwise, we return the count. */ michael@0: if (recipient_list) { michael@0: recipient_list[rlindex] = NULL; michael@0: return 0; michael@0: } else { michael@0: return count; michael@0: } michael@0: } michael@0: michael@0: NSSCMSRecipient ** michael@0: nss_cms_recipient_list_create(NSSCMSRecipientInfo **recipientinfos) michael@0: { michael@0: int count, rv; michael@0: NSSCMSRecipient **recipient_list; michael@0: michael@0: /* count the number of recipient identifiers */ michael@0: count = nss_cms_recipients_traverse(recipientinfos, NULL); michael@0: if (count <= 0) { michael@0: /* no recipients? */ michael@0: PORT_SetError(SEC_ERROR_BAD_DATA); michael@0: #if 0 michael@0: PORT_SetErrorString("Cannot find recipient data in envelope."); michael@0: #endif michael@0: return NULL; michael@0: } michael@0: michael@0: /* allocate an array of pointers */ michael@0: recipient_list = (NSSCMSRecipient **) michael@0: PORT_ZAlloc((count + 1) * sizeof(NSSCMSRecipient *)); michael@0: if (recipient_list == NULL) michael@0: return NULL; michael@0: michael@0: /* now fill in the recipient_list */ michael@0: rv = nss_cms_recipients_traverse(recipientinfos, recipient_list); michael@0: if (rv < 0) { michael@0: nss_cms_recipient_list_destroy(recipient_list); michael@0: return NULL; michael@0: } michael@0: return recipient_list; michael@0: } michael@0: michael@0: void michael@0: nss_cms_recipient_list_destroy(NSSCMSRecipient **recipient_list) michael@0: { michael@0: int i; michael@0: NSSCMSRecipient *recipient; michael@0: michael@0: for (i=0; recipient_list[i] != NULL; i++) { michael@0: recipient = recipient_list[i]; michael@0: if (recipient->cert) michael@0: CERT_DestroyCertificate(recipient->cert); michael@0: if (recipient->privkey) michael@0: SECKEY_DestroyPrivateKey(recipient->privkey); michael@0: if (recipient->slot) michael@0: PK11_FreeSlot(recipient->slot); michael@0: PORT_Free(recipient); michael@0: } michael@0: PORT_Free(recipient_list); michael@0: } michael@0: michael@0: NSSCMSRecipientEncryptedKey * michael@0: NSS_CMSRecipientEncryptedKey_Create(PLArenaPool *poolp) michael@0: { michael@0: return (NSSCMSRecipientEncryptedKey *)PORT_ArenaZAlloc(poolp, sizeof(NSSCMSRecipientEncryptedKey)); michael@0: }