RetroZilla/security/nss/lib/smime/cmscinfo.c

433 lines
11 KiB
C
Raw Normal View History

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