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 message methods.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cmslocal.h"
|
|
|
|
|
|
|
|
#include "cert.h"
|
|
|
|
#include "secasn1.h"
|
|
|
|
#include "secitem.h"
|
|
|
|
#include "secoid.h"
|
|
|
|
#include "pk11func.h"
|
|
|
|
#include "secerr.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_Create - create a CMS message object
|
|
|
|
*
|
|
|
|
* "poolp" - arena to allocate memory from, or NULL if new arena should be created
|
|
|
|
*/
|
|
|
|
NSSCMSMessage *
|
|
|
|
NSS_CMSMessage_Create(PLArenaPool *poolp)
|
|
|
|
{
|
|
|
|
void *mark = NULL;
|
|
|
|
NSSCMSMessage *cmsg;
|
|
|
|
PRBool poolp_is_ours = PR_FALSE;
|
|
|
|
|
|
|
|
if (poolp == NULL) {
|
cherry-picked mozilla NSS upstream changes (to rev bad5fd065fa1, which is on par with 3.20):
bug1001332, 56b691c003ad, bug1086145, bug1054069, bug1155922, bug991783, bug1125025, bug1162521, bug1162644, bug1132941, bug1164364, bug1166205, bug1166163, bug1166515, bug1138554, bug1167046, bug1167043, bug1169451, bug1172128, bug1170322, bug102794, bug1128184, bug557830, bug1174648, bug1180244, bug1177784, bug1173413, bug1169174, bug1084669, bug951455, bug1183395, bug1177430, bug1183827, bug1160139, bug1154106, bug1142209, bug1185033, bug1193467, bug1182667(with sha512 changes backed out, which breaks VC6 compilation), bug1158489, bug337796
2018-07-12 15:44:51 +02:00
|
|
|
poolp = PORT_NewArena (1024); /* XXX what is right value? */
|
|
|
|
if (poolp == NULL)
|
|
|
|
return NULL;
|
|
|
|
poolp_is_ours = PR_TRUE;
|
|
|
|
}
|
2015-10-21 05:03:22 +02:00
|
|
|
|
|
|
|
if (!poolp_is_ours)
|
cherry-picked mozilla NSS upstream changes (to rev bad5fd065fa1, which is on par with 3.20):
bug1001332, 56b691c003ad, bug1086145, bug1054069, bug1155922, bug991783, bug1125025, bug1162521, bug1162644, bug1132941, bug1164364, bug1166205, bug1166163, bug1166515, bug1138554, bug1167046, bug1167043, bug1169451, bug1172128, bug1170322, bug102794, bug1128184, bug557830, bug1174648, bug1180244, bug1177784, bug1173413, bug1169174, bug1084669, bug951455, bug1183395, bug1177430, bug1183827, bug1160139, bug1154106, bug1142209, bug1185033, bug1193467, bug1182667(with sha512 changes backed out, which breaks VC6 compilation), bug1158489, bug337796
2018-07-12 15:44:51 +02:00
|
|
|
mark = PORT_ArenaMark(poolp);
|
2015-10-21 05:03:22 +02:00
|
|
|
|
cherry-picked mozilla NSS upstream changes (to rev bad5fd065fa1, which is on par with 3.20):
bug1001332, 56b691c003ad, bug1086145, bug1054069, bug1155922, bug991783, bug1125025, bug1162521, bug1162644, bug1132941, bug1164364, bug1166205, bug1166163, bug1166515, bug1138554, bug1167046, bug1167043, bug1169451, bug1172128, bug1170322, bug102794, bug1128184, bug557830, bug1174648, bug1180244, bug1177784, bug1173413, bug1169174, bug1084669, bug951455, bug1183395, bug1177430, bug1183827, bug1160139, bug1154106, bug1142209, bug1185033, bug1193467, bug1182667(with sha512 changes backed out, which breaks VC6 compilation), bug1158489, bug337796
2018-07-12 15:44:51 +02:00
|
|
|
cmsg = (NSSCMSMessage *)PORT_ArenaZAlloc(poolp, sizeof(NSSCMSMessage));
|
|
|
|
if (cmsg == NULL ||
|
|
|
|
NSS_CMSContentInfo_Private_Init(&(cmsg->contentInfo)) != SECSuccess) {
|
|
|
|
if (!poolp_is_ours) {
|
|
|
|
if (mark) {
|
|
|
|
PORT_ArenaRelease(poolp, mark);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
PORT_FreeArena(poolp, PR_FALSE);
|
|
|
|
return NULL;
|
2015-10-21 05:03:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cmsg->poolp = poolp;
|
|
|
|
cmsg->poolp_is_ours = poolp_is_ours;
|
|
|
|
cmsg->refCount = 1;
|
|
|
|
|
|
|
|
if (mark)
|
|
|
|
PORT_ArenaUnmark(poolp, mark);
|
|
|
|
|
|
|
|
return cmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_SetEncodingParams - set up a CMS message object for encoding or decoding
|
|
|
|
*
|
|
|
|
* "cmsg" - message object
|
|
|
|
* "pwfn", pwfn_arg" - callback function for getting token password
|
|
|
|
* "decrypt_key_cb", "decrypt_key_cb_arg" - callback function for getting bulk key for encryptedData
|
|
|
|
* "detached_digestalgs", "detached_digests" - digests from detached content
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
NSS_CMSMessage_SetEncodingParams(NSSCMSMessage *cmsg,
|
|
|
|
PK11PasswordFunc pwfn, void *pwfn_arg,
|
|
|
|
NSSCMSGetDecryptKeyCallback decrypt_key_cb, void *decrypt_key_cb_arg,
|
|
|
|
SECAlgorithmID **detached_digestalgs, SECItem **detached_digests)
|
|
|
|
{
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cmsg == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
if (pwfn)
|
|
|
|
PK11_SetPasswordFunc(pwfn);
|
|
|
|
cmsg->pwfn_arg = pwfn_arg;
|
|
|
|
cmsg->decrypt_key_cb = decrypt_key_cb;
|
|
|
|
cmsg->decrypt_key_cb_arg = decrypt_key_cb_arg;
|
|
|
|
cmsg->detached_digestalgs = detached_digestalgs;
|
|
|
|
cmsg->detached_digests = detached_digests;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_Destroy - destroy a CMS message and all of its sub-pieces.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
NSS_CMSMessage_Destroy(NSSCMSMessage *cmsg)
|
|
|
|
{
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cmsg == NULL)
|
|
|
|
return;
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
PORT_Assert (cmsg->refCount > 0);
|
|
|
|
if (cmsg->refCount <= 0) /* oops */
|
|
|
|
return;
|
|
|
|
|
|
|
|
cmsg->refCount--; /* thread safety? */
|
|
|
|
if (cmsg->refCount > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NSS_CMSContentInfo_Destroy(&(cmsg->contentInfo));
|
|
|
|
|
|
|
|
/* if poolp is not NULL, cmsg is the owner of its arena */
|
|
|
|
if (cmsg->poolp_is_ours)
|
|
|
|
PORT_FreeArena (cmsg->poolp, PR_FALSE); /* XXX clear it? */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_Copy - return a copy of the given message.
|
|
|
|
*
|
|
|
|
* The copy may be virtual or may be real -- either way, the result needs
|
|
|
|
* to be passed to NSS_CMSMessage_Destroy later (as does the original).
|
|
|
|
*/
|
|
|
|
NSSCMSMessage *
|
|
|
|
NSS_CMSMessage_Copy(NSSCMSMessage *cmsg)
|
|
|
|
{
|
|
|
|
if (cmsg == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
PORT_Assert (cmsg->refCount > 0);
|
|
|
|
|
|
|
|
cmsg->refCount++; /* XXX chrisk thread safety? */
|
|
|
|
return cmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_GetArena - return a pointer to the message's arena pool
|
|
|
|
*/
|
|
|
|
PLArenaPool *
|
|
|
|
NSS_CMSMessage_GetArena(NSSCMSMessage *cmsg)
|
|
|
|
{
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cmsg == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
return cmsg->poolp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_GetContentInfo - return a pointer to the top level contentInfo
|
|
|
|
*/
|
|
|
|
NSSCMSContentInfo *
|
|
|
|
NSS_CMSMessage_GetContentInfo(NSSCMSMessage *cmsg)
|
|
|
|
{
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cmsg == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
return &(cmsg->contentInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a pointer to the actual content.
|
|
|
|
* In the case of those types which are encrypted, this returns the *plain* content.
|
|
|
|
* In case of nested contentInfos, this descends and retrieves the innermost content.
|
|
|
|
*/
|
|
|
|
SECItem *
|
|
|
|
NSS_CMSMessage_GetContent(NSSCMSMessage *cmsg)
|
|
|
|
{
|
2020-01-08 00:39:56 +01:00
|
|
|
NSSCMSContentInfo *cinfo;
|
|
|
|
SECItem *pItem;
|
|
|
|
|
|
|
|
if (cmsg == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
/* this is a shortcut */
|
2020-01-08 00:39:56 +01:00
|
|
|
cinfo = NSS_CMSMessage_GetContentInfo(cmsg);
|
|
|
|
pItem = NSS_CMSContentInfo_GetInnerContent(cinfo);
|
2015-10-21 05:03:22 +02:00
|
|
|
return pItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_ContentLevelCount - count number of levels of CMS content objects in this message
|
|
|
|
*
|
|
|
|
* CMS data content objects do not count.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NSS_CMSMessage_ContentLevelCount(NSSCMSMessage *cmsg)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
NSSCMSContentInfo *cinfo;
|
|
|
|
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cmsg == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
/* walk down the chain of contentinfos */
|
|
|
|
for (cinfo = &(cmsg->contentInfo); cinfo != NULL; ) {
|
|
|
|
count++;
|
|
|
|
cinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_ContentLevel - find content level #n
|
|
|
|
*
|
|
|
|
* CMS data content objects do not count.
|
|
|
|
*/
|
|
|
|
NSSCMSContentInfo *
|
|
|
|
NSS_CMSMessage_ContentLevel(NSSCMSMessage *cmsg, int n)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
NSSCMSContentInfo *cinfo;
|
|
|
|
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cmsg == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
/* walk down the chain of contentinfos */
|
|
|
|
for (cinfo = &(cmsg->contentInfo); cinfo != NULL && count < n; cinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo)) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_ContainsCertsOrCrls - see if message contains certs along the way
|
|
|
|
*/
|
|
|
|
PRBool
|
|
|
|
NSS_CMSMessage_ContainsCertsOrCrls(NSSCMSMessage *cmsg)
|
|
|
|
{
|
|
|
|
NSSCMSContentInfo *cinfo;
|
|
|
|
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cmsg == NULL) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
/* descend into CMS message */
|
|
|
|
for (cinfo = &(cmsg->contentInfo); cinfo != NULL; cinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo)) {
|
2018-05-04 16:08:28 +02:00
|
|
|
if (!NSS_CMSType_IsData(NSS_CMSContentInfo_GetContentTypeTag(cinfo)))
|
2015-10-21 05:03:22 +02:00
|
|
|
continue; /* next level */
|
|
|
|
|
|
|
|
if (NSS_CMSSignedData_ContainsCertsOrCrls(cinfo->content.signedData))
|
|
|
|
return PR_TRUE;
|
2018-05-04 16:08:28 +02:00
|
|
|
/* callback here for generic wrappers? */
|
2015-10-21 05:03:22 +02:00
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_IsEncrypted - see if message contains a encrypted submessage
|
|
|
|
*/
|
|
|
|
PRBool
|
|
|
|
NSS_CMSMessage_IsEncrypted(NSSCMSMessage *cmsg)
|
|
|
|
{
|
|
|
|
NSSCMSContentInfo *cinfo;
|
|
|
|
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cmsg == NULL) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
/* walk down the chain of contentinfos */
|
|
|
|
for (cinfo = &(cmsg->contentInfo); cinfo != NULL; cinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo))
|
|
|
|
{
|
|
|
|
switch (NSS_CMSContentInfo_GetContentTypeTag(cinfo)) {
|
|
|
|
case SEC_OID_PKCS7_ENVELOPED_DATA:
|
|
|
|
case SEC_OID_PKCS7_ENCRYPTED_DATA:
|
|
|
|
return PR_TRUE;
|
|
|
|
default:
|
2018-05-04 16:08:28 +02:00
|
|
|
/* callback here for generic wrappers? */
|
2015-10-21 05:03:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_IsSigned - see if message contains a signed submessage
|
|
|
|
*
|
|
|
|
* If the CMS message has a SignedData with a signature (not just a SignedData)
|
|
|
|
* return true; false otherwise. This can/should be called before calling
|
|
|
|
* VerifySignature, which will always indicate failure if no signature is
|
|
|
|
* present, but that does not mean there even was a signature!
|
|
|
|
* Note that the content itself can be empty (detached content was sent
|
|
|
|
* another way); it is the presence of the signature that matters.
|
|
|
|
*/
|
|
|
|
PRBool
|
|
|
|
NSS_CMSMessage_IsSigned(NSSCMSMessage *cmsg)
|
|
|
|
{
|
|
|
|
NSSCMSContentInfo *cinfo;
|
|
|
|
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cmsg == NULL) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
/* walk down the chain of contentinfos */
|
|
|
|
for (cinfo = &(cmsg->contentInfo); cinfo != NULL; cinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo))
|
|
|
|
{
|
|
|
|
switch (NSS_CMSContentInfo_GetContentTypeTag(cinfo)) {
|
|
|
|
case SEC_OID_PKCS7_SIGNED_DATA:
|
2020-01-08 00:39:56 +01:00
|
|
|
if (cinfo->content.signedData == NULL) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2015-10-21 05:03:22 +02:00
|
|
|
if (!NSS_CMSArray_IsEmpty((void **)cinfo->content.signedData->signerInfos))
|
|
|
|
return PR_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
2018-05-04 16:08:28 +02:00
|
|
|
/* callback here for generic wrappers? */
|
2015-10-21 05:03:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS_CMSMessage_IsContentEmpty - see if content is empty
|
|
|
|
*
|
|
|
|
* returns PR_TRUE is innermost content length is < minLen
|
|
|
|
* XXX need the encrypted content length (why?)
|
|
|
|
*/
|
|
|
|
PRBool
|
|
|
|
NSS_CMSMessage_IsContentEmpty(NSSCMSMessage *cmsg, unsigned int minLen)
|
|
|
|
{
|
|
|
|
SECItem *item = NULL;
|
|
|
|
|
|
|
|
if (cmsg == NULL)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
item = NSS_CMSContentInfo_GetContent(NSS_CMSMessage_GetContentInfo(cmsg));
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return PR_TRUE;
|
|
|
|
} else if(item->len <= minLen) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|