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 contentInfo methods. michael@0: */ michael@0: michael@0: #include "cmslocal.h" michael@0: michael@0: #include "pk11func.h" michael@0: #include "secitem.h" michael@0: #include "secoid.h" michael@0: #include "secerr.h" michael@0: michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_Create - create a content info michael@0: * michael@0: * version is set in the _Finalize procedures for each content type michael@0: */ michael@0: SECStatus michael@0: NSS_CMSContentInfo_Private_Init(NSSCMSContentInfo *cinfo) michael@0: { michael@0: if (cinfo->privateInfo) { michael@0: return SECSuccess; michael@0: } michael@0: cinfo->privateInfo = PORT_ZNew(NSSCMSContentInfoPrivate); michael@0: return (cinfo->privateInfo) ? SECSuccess : SECFailure; michael@0: } michael@0: michael@0: michael@0: static void michael@0: nss_cmsContentInfo_private_destroy(NSSCMSContentInfoPrivate *privateInfo) michael@0: { michael@0: if (privateInfo->digcx) { michael@0: /* must destroy digest objects */ michael@0: NSS_CMSDigestContext_Cancel(privateInfo->digcx); michael@0: privateInfo->digcx = NULL; michael@0: } michael@0: if (privateInfo->ciphcx) { michael@0: NSS_CMSCipherContext_Destroy(privateInfo->ciphcx); michael@0: privateInfo->ciphcx = NULL; michael@0: } michael@0: PORT_Free(privateInfo); michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_Destroy - destroy a CMS contentInfo and all of its sub-pieces. michael@0: */ michael@0: void michael@0: NSS_CMSContentInfo_Destroy(NSSCMSContentInfo *cinfo) michael@0: { michael@0: SECOidTag kind; michael@0: michael@0: kind = NSS_CMSContentInfo_GetContentTypeTag(cinfo); michael@0: switch (kind) { michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: NSS_CMSEnvelopedData_Destroy(cinfo->content.envelopedData); michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: NSS_CMSSignedData_Destroy(cinfo->content.signedData); michael@0: break; michael@0: case SEC_OID_PKCS7_ENCRYPTED_DATA: michael@0: NSS_CMSEncryptedData_Destroy(cinfo->content.encryptedData); michael@0: break; michael@0: case SEC_OID_PKCS7_DIGESTED_DATA: michael@0: NSS_CMSDigestedData_Destroy(cinfo->content.digestedData); michael@0: break; michael@0: default: michael@0: NSS_CMSGenericWrapperData_Destroy(kind, cinfo->content.genericData); michael@0: /* XXX Anything else that needs to be "manually" freed/destroyed? */ michael@0: break; michael@0: } michael@0: if (cinfo->privateInfo) { michael@0: nss_cmsContentInfo_private_destroy(cinfo->privateInfo); michael@0: cinfo->privateInfo = NULL; michael@0: } michael@0: if (cinfo->bulkkey) { michael@0: PK11_FreeSymKey(cinfo->bulkkey); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_GetChildContentInfo - get content's contentInfo (if it exists) michael@0: */ michael@0: NSSCMSContentInfo * michael@0: NSS_CMSContentInfo_GetChildContentInfo(NSSCMSContentInfo *cinfo) michael@0: { michael@0: NSSCMSContentInfo * ccinfo = NULL; michael@0: SECOidTag tag = NSS_CMSContentInfo_GetContentTypeTag(cinfo); michael@0: switch (tag) { michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: if (cinfo->content.signedData != NULL) { michael@0: ccinfo = &(cinfo->content.signedData->contentInfo); michael@0: } michael@0: break; michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: if (cinfo->content.envelopedData != NULL) { michael@0: ccinfo = &(cinfo->content.envelopedData->contentInfo); michael@0: } michael@0: break; michael@0: case SEC_OID_PKCS7_DIGESTED_DATA: michael@0: if (cinfo->content.digestedData != NULL) { michael@0: ccinfo = &(cinfo->content.digestedData->contentInfo); michael@0: } michael@0: break; michael@0: case SEC_OID_PKCS7_ENCRYPTED_DATA: michael@0: if (cinfo->content.encryptedData != NULL) { michael@0: ccinfo = &(cinfo->content.encryptedData->contentInfo); michael@0: } michael@0: break; michael@0: case SEC_OID_PKCS7_DATA: michael@0: default: michael@0: if (NSS_CMSType_IsWrapper(tag)) { michael@0: if (cinfo->content.genericData != NULL) { michael@0: ccinfo = &(cinfo->content.genericData->contentInfo); michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: if (ccinfo && !ccinfo->privateInfo) { michael@0: NSS_CMSContentInfo_Private_Init(ccinfo); michael@0: } michael@0: return ccinfo; michael@0: } michael@0: michael@0: SECStatus michael@0: NSS_CMSContentInfo_SetDontStream(NSSCMSContentInfo *cinfo, PRBool dontStream) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: rv = NSS_CMSContentInfo_Private_Init(cinfo); michael@0: if (rv != SECSuccess) { michael@0: /* default is streaming, failure to get ccinfo will not effect this */ michael@0: return dontStream ? SECFailure : SECSuccess ; michael@0: } michael@0: cinfo->privateInfo->dontStream = dontStream; michael@0: return SECSuccess; michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_SetContent - set content type & content michael@0: */ michael@0: SECStatus michael@0: NSS_CMSContentInfo_SetContent(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, SECOidTag type, void *ptr) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: cinfo->contentTypeTag = SECOID_FindOIDByTag(type); michael@0: if (cinfo->contentTypeTag == NULL) michael@0: return SECFailure; michael@0: michael@0: /* do not copy the oid, just create a reference */ michael@0: rv = SECITEM_CopyItem (cmsg->poolp, &(cinfo->contentType), &(cinfo->contentTypeTag->oid)); michael@0: if (rv != SECSuccess) michael@0: return SECFailure; michael@0: michael@0: cinfo->content.pointer = ptr; michael@0: michael@0: if (NSS_CMSType_IsData(type) && ptr) { michael@0: cinfo->rawContent = ptr; michael@0: } else { michael@0: /* as we always have some inner data, michael@0: * we need to set it to something, just to fool the encoder enough to work on it michael@0: * and get us into nss_cms_encoder_notify at that point */ michael@0: cinfo->rawContent = SECITEM_AllocItem(cmsg->poolp, NULL, 1); michael@0: if (cinfo->rawContent == NULL) { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: return SECFailure; michael@0: } michael@0: } michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_SetContent_XXXX - typesafe wrappers for NSS_CMSContentInfo_SetContent michael@0: */ michael@0: michael@0: /* michael@0: * data == NULL -> pass in data via NSS_CMSEncoder_Update michael@0: * data != NULL -> take this data michael@0: */ michael@0: SECStatus michael@0: NSS_CMSContentInfo_SetContent_Data(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, SECItem *data, PRBool detached) michael@0: { michael@0: if (NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_DATA, (void *)data) != SECSuccess) michael@0: return SECFailure; michael@0: if (detached) { michael@0: cinfo->rawContent = NULL; michael@0: } michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: SECStatus michael@0: NSS_CMSContentInfo_SetContent_SignedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSSignedData *sigd) michael@0: { michael@0: return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_SIGNED_DATA, (void *)sigd); michael@0: } michael@0: michael@0: SECStatus michael@0: NSS_CMSContentInfo_SetContent_EnvelopedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSEnvelopedData *envd) michael@0: { michael@0: return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_ENVELOPED_DATA, (void *)envd); michael@0: } michael@0: michael@0: SECStatus michael@0: NSS_CMSContentInfo_SetContent_DigestedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSDigestedData *digd) michael@0: { michael@0: return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_DIGESTED_DATA, (void *)digd); michael@0: } michael@0: michael@0: SECStatus michael@0: NSS_CMSContentInfo_SetContent_EncryptedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSEncryptedData *encd) michael@0: { michael@0: return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_ENCRYPTED_DATA, (void *)encd); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_GetContent - get pointer to inner content michael@0: * michael@0: * needs to be casted... michael@0: */ michael@0: void * michael@0: NSS_CMSContentInfo_GetContent(NSSCMSContentInfo *cinfo) michael@0: { michael@0: SECOidTag tag = (cinfo && cinfo->contentTypeTag) michael@0: ? cinfo->contentTypeTag->offset michael@0: : SEC_OID_UNKNOWN; michael@0: switch (tag) { michael@0: case SEC_OID_PKCS7_DATA: michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: case SEC_OID_PKCS7_DIGESTED_DATA: michael@0: case SEC_OID_PKCS7_ENCRYPTED_DATA: michael@0: return cinfo->content.pointer; michael@0: default: michael@0: return NSS_CMSType_IsWrapper(tag) ? cinfo->content.pointer : (NSS_CMSType_IsData(tag) ? cinfo->rawContent : NULL); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_GetInnerContent - get pointer to innermost content michael@0: * michael@0: * this is typically only called by NSS_CMSMessage_GetContent() michael@0: */ michael@0: michael@0: SECItem * michael@0: NSS_CMSContentInfo_GetInnerContent(NSSCMSContentInfo *cinfo) michael@0: { michael@0: NSSCMSContentInfo *ccinfo; michael@0: SECOidTag tag; michael@0: SECItem *pItem = NULL; michael@0: michael@0: tag = NSS_CMSContentInfo_GetContentTypeTag(cinfo); michael@0: if (NSS_CMSType_IsData(tag)) { michael@0: pItem = cinfo->content.data; michael@0: } else if (NSS_CMSType_IsWrapper(tag)) { michael@0: ccinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo); michael@0: if (ccinfo != NULL) { michael@0: pItem = NSS_CMSContentInfo_GetContent(ccinfo); michael@0: } michael@0: } else { michael@0: PORT_Assert(0); michael@0: } michael@0: michael@0: return pItem; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_GetContentType{Tag,OID} - find out (saving pointer to lookup result michael@0: * for future reference) and return the inner content type. michael@0: */ michael@0: SECOidTag michael@0: NSS_CMSContentInfo_GetContentTypeTag(NSSCMSContentInfo *cinfo) michael@0: { michael@0: if (cinfo->contentTypeTag == NULL) michael@0: cinfo->contentTypeTag = SECOID_FindOID(&(cinfo->contentType)); michael@0: michael@0: if (cinfo->contentTypeTag == NULL) michael@0: return SEC_OID_UNKNOWN; michael@0: michael@0: return cinfo->contentTypeTag->offset; michael@0: } michael@0: michael@0: SECItem * michael@0: NSS_CMSContentInfo_GetContentTypeOID(NSSCMSContentInfo *cinfo) michael@0: { michael@0: if (cinfo->contentTypeTag == NULL) michael@0: cinfo->contentTypeTag = SECOID_FindOID(&(cinfo->contentType)); michael@0: michael@0: if (cinfo->contentTypeTag == NULL) michael@0: return NULL; michael@0: michael@0: return &(cinfo->contentTypeTag->oid); michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_GetContentEncAlgTag - find out (saving pointer to lookup result michael@0: * for future reference) and return the content encryption algorithm tag. michael@0: */ michael@0: SECOidTag michael@0: NSS_CMSContentInfo_GetContentEncAlgTag(NSSCMSContentInfo *cinfo) michael@0: { michael@0: if (cinfo->contentEncAlgTag == SEC_OID_UNKNOWN) michael@0: cinfo->contentEncAlgTag = SECOID_GetAlgorithmTag(&(cinfo->contentEncAlg)); michael@0: michael@0: return cinfo->contentEncAlgTag; michael@0: } michael@0: michael@0: /* michael@0: * NSS_CMSContentInfo_GetContentEncAlg - find out and return the content encryption algorithm tag. michael@0: */ michael@0: SECAlgorithmID * michael@0: NSS_CMSContentInfo_GetContentEncAlg(NSSCMSContentInfo *cinfo) michael@0: { michael@0: return &(cinfo->contentEncAlg); michael@0: } michael@0: michael@0: SECStatus michael@0: NSS_CMSContentInfo_SetContentEncAlg(PLArenaPool *poolp, NSSCMSContentInfo *cinfo, michael@0: SECOidTag bulkalgtag, SECItem *parameters, int keysize) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: rv = SECOID_SetAlgorithmID(poolp, &(cinfo->contentEncAlg), bulkalgtag, parameters); michael@0: if (rv != SECSuccess) michael@0: return SECFailure; michael@0: cinfo->keysize = keysize; michael@0: return SECSuccess; michael@0: } michael@0: michael@0: SECStatus michael@0: NSS_CMSContentInfo_SetContentEncAlgID(PLArenaPool *poolp, NSSCMSContentInfo *cinfo, michael@0: SECAlgorithmID *algid, int keysize) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: rv = SECOID_CopyAlgorithmID(poolp, &(cinfo->contentEncAlg), algid); michael@0: if (rv != SECSuccess) michael@0: return SECFailure; michael@0: if (keysize >= 0) michael@0: cinfo->keysize = keysize; michael@0: return SECSuccess; michael@0: } michael@0: michael@0: void michael@0: NSS_CMSContentInfo_SetBulkKey(NSSCMSContentInfo *cinfo, PK11SymKey *bulkkey) michael@0: { michael@0: cinfo->bulkkey = PK11_ReferenceSymKey(bulkkey); michael@0: cinfo->keysize = PK11_GetKeyStrength(cinfo->bulkkey, &(cinfo->contentEncAlg)); michael@0: } michael@0: michael@0: PK11SymKey * michael@0: NSS_CMSContentInfo_GetBulkKey(NSSCMSContentInfo *cinfo) michael@0: { michael@0: if (cinfo->bulkkey == NULL) michael@0: return NULL; michael@0: michael@0: return PK11_ReferenceSymKey(cinfo->bulkkey); michael@0: } michael@0: michael@0: int michael@0: NSS_CMSContentInfo_GetBulkKeySize(NSSCMSContentInfo *cinfo) michael@0: { michael@0: return cinfo->keysize; michael@0: }