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: * PKCS7 creation. michael@0: */ michael@0: michael@0: #include "p7local.h" michael@0: michael@0: #include "cert.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 "secder.h" michael@0: #include "secpkcs5.h" michael@0: michael@0: const int NSS_PBE_DEFAULT_ITERATION_COUNT = 2000; /* used in p12e.c too */ michael@0: michael@0: static SECStatus michael@0: sec_pkcs7_init_content_info (SEC_PKCS7ContentInfo *cinfo, PLArenaPool *poolp, michael@0: SECOidTag kind, PRBool detached) michael@0: { michael@0: void *thing; michael@0: int version; michael@0: SECItem *versionp; michael@0: SECStatus rv; michael@0: michael@0: PORT_Assert (cinfo != NULL && poolp != NULL); michael@0: if (cinfo == NULL || poolp == NULL) michael@0: return SECFailure; michael@0: michael@0: cinfo->contentTypeTag = SECOID_FindOIDByTag (kind); michael@0: PORT_Assert (cinfo->contentTypeTag michael@0: && cinfo->contentTypeTag->offset == kind); michael@0: michael@0: rv = SECITEM_CopyItem (poolp, &(cinfo->contentType), michael@0: &(cinfo->contentTypeTag->oid)); michael@0: if (rv != SECSuccess) michael@0: return rv; michael@0: michael@0: if (detached) michael@0: return SECSuccess; michael@0: michael@0: switch (kind) { michael@0: default: michael@0: case SEC_OID_PKCS7_DATA: michael@0: thing = PORT_ArenaZAlloc (poolp, sizeof(SECItem)); michael@0: cinfo->content.data = (SECItem*)thing; michael@0: versionp = NULL; michael@0: version = -1; michael@0: break; michael@0: case SEC_OID_PKCS7_DIGESTED_DATA: michael@0: thing = PORT_ArenaZAlloc (poolp, sizeof(SEC_PKCS7DigestedData)); michael@0: cinfo->content.digestedData = (SEC_PKCS7DigestedData*)thing; michael@0: versionp = &(cinfo->content.digestedData->version); michael@0: version = SEC_PKCS7_DIGESTED_DATA_VERSION; michael@0: break; michael@0: case SEC_OID_PKCS7_ENCRYPTED_DATA: michael@0: thing = PORT_ArenaZAlloc (poolp, sizeof(SEC_PKCS7EncryptedData)); michael@0: cinfo->content.encryptedData = (SEC_PKCS7EncryptedData*)thing; michael@0: versionp = &(cinfo->content.encryptedData->version); michael@0: version = SEC_PKCS7_ENCRYPTED_DATA_VERSION; michael@0: break; michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: thing = PORT_ArenaZAlloc (poolp, sizeof(SEC_PKCS7EnvelopedData)); michael@0: cinfo->content.envelopedData = michael@0: (SEC_PKCS7EnvelopedData*)thing; michael@0: versionp = &(cinfo->content.envelopedData->version); michael@0: version = SEC_PKCS7_ENVELOPED_DATA_VERSION; michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: thing = PORT_ArenaZAlloc (poolp, sizeof(SEC_PKCS7SignedData)); michael@0: cinfo->content.signedData = michael@0: (SEC_PKCS7SignedData*)thing; michael@0: versionp = &(cinfo->content.signedData->version); michael@0: version = SEC_PKCS7_SIGNED_DATA_VERSION; michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: thing = PORT_ArenaZAlloc(poolp,sizeof(SEC_PKCS7SignedAndEnvelopedData)); michael@0: cinfo->content.signedAndEnvelopedData = michael@0: (SEC_PKCS7SignedAndEnvelopedData*)thing; michael@0: versionp = &(cinfo->content.signedAndEnvelopedData->version); michael@0: version = SEC_PKCS7_SIGNED_AND_ENVELOPED_DATA_VERSION; michael@0: break; michael@0: } michael@0: michael@0: if (thing == NULL) michael@0: return SECFailure; michael@0: michael@0: if (versionp != NULL) { michael@0: SECItem *dummy; michael@0: michael@0: PORT_Assert (version >= 0); michael@0: dummy = SEC_ASN1EncodeInteger (poolp, versionp, version); michael@0: if (dummy == NULL) michael@0: return SECFailure; michael@0: PORT_Assert (dummy == versionp); michael@0: } michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: static SEC_PKCS7ContentInfo * michael@0: sec_pkcs7_create_content_info (SECOidTag kind, PRBool detached, michael@0: SECKEYGetPasswordKey pwfn, void *pwfn_arg) michael@0: { michael@0: SEC_PKCS7ContentInfo *cinfo; michael@0: PLArenaPool *poolp; michael@0: SECStatus rv; michael@0: michael@0: poolp = PORT_NewArena (1024); /* XXX what is right value? */ michael@0: if (poolp == NULL) michael@0: return NULL; michael@0: michael@0: cinfo = (SEC_PKCS7ContentInfo*)PORT_ArenaZAlloc (poolp, sizeof(*cinfo)); michael@0: if (cinfo == NULL) { michael@0: PORT_FreeArena (poolp, PR_FALSE); michael@0: return NULL; michael@0: } michael@0: michael@0: cinfo->poolp = poolp; michael@0: cinfo->pwfn = pwfn; michael@0: cinfo->pwfn_arg = pwfn_arg; michael@0: cinfo->created = PR_TRUE; michael@0: cinfo->refCount = 1; michael@0: michael@0: rv = sec_pkcs7_init_content_info (cinfo, poolp, kind, detached); michael@0: if (rv != SECSuccess) { michael@0: PORT_FreeArena (poolp, PR_FALSE); michael@0: return NULL; michael@0: } michael@0: michael@0: return cinfo; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Add a signer to a PKCS7 thing, verifying the signature cert first. michael@0: * Any error returns SECFailure. michael@0: * michael@0: * XXX Right now this only adds the *first* signer. It fails if you try michael@0: * to add a second one -- this needs to be fixed. michael@0: */ michael@0: static SECStatus michael@0: sec_pkcs7_add_signer (SEC_PKCS7ContentInfo *cinfo, michael@0: CERTCertificate * cert, michael@0: SECCertUsage certusage, michael@0: CERTCertDBHandle * certdb, michael@0: SECOidTag digestalgtag, michael@0: SECItem * digestdata) michael@0: { michael@0: SEC_PKCS7SignerInfo *signerinfo, **signerinfos, ***signerinfosp; michael@0: SECAlgorithmID *digestalg, **digestalgs, ***digestalgsp; michael@0: SECItem *digest, **digests, ***digestsp; michael@0: SECItem * dummy; michael@0: void * mark; michael@0: SECStatus rv; michael@0: SECOidTag kind; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: switch (kind) { michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: { michael@0: SEC_PKCS7SignedData *sdp; michael@0: michael@0: sdp = cinfo->content.signedData; michael@0: digestalgsp = &(sdp->digestAlgorithms); michael@0: digestsp = &(sdp->digests); michael@0: signerinfosp = &(sdp->signerInfos); michael@0: } michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: { michael@0: SEC_PKCS7SignedAndEnvelopedData *saedp; michael@0: michael@0: saedp = cinfo->content.signedAndEnvelopedData; michael@0: digestalgsp = &(saedp->digestAlgorithms); michael@0: digestsp = &(saedp->digests); michael@0: signerinfosp = &(saedp->signerInfos); michael@0: } michael@0: break; michael@0: default: michael@0: return SECFailure; /* XXX set an error? */ michael@0: } michael@0: michael@0: /* michael@0: * XXX I think that CERT_VerifyCert should do this if *it* is passed michael@0: * a NULL database. michael@0: */ michael@0: if (certdb == NULL) { michael@0: certdb = CERT_GetDefaultCertDB(); michael@0: if (certdb == NULL) michael@0: return SECFailure; /* XXX set an error? */ michael@0: } michael@0: michael@0: if (CERT_VerifyCert (certdb, cert, PR_TRUE, certusage, PR_Now(), michael@0: cinfo->pwfn_arg, NULL) != SECSuccess) michael@0: { michael@0: /* XXX Did CERT_VerifyCert set an error? */ michael@0: return SECFailure; michael@0: } michael@0: michael@0: /* michael@0: * XXX This is the check that we do not already have a signer. michael@0: * This is not what we really want -- we want to allow this michael@0: * and *add* the new signer. michael@0: */ michael@0: PORT_Assert (*signerinfosp == NULL michael@0: && *digestalgsp == NULL && *digestsp == NULL); michael@0: if (*signerinfosp != NULL || *digestalgsp != NULL || *digestsp != NULL) michael@0: return SECFailure; michael@0: michael@0: mark = PORT_ArenaMark (cinfo->poolp); michael@0: michael@0: signerinfo = (SEC_PKCS7SignerInfo*)PORT_ArenaZAlloc (cinfo->poolp, michael@0: sizeof(SEC_PKCS7SignerInfo)); michael@0: if (signerinfo == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: michael@0: dummy = SEC_ASN1EncodeInteger (cinfo->poolp, &signerinfo->version, michael@0: SEC_PKCS7_SIGNER_INFO_VERSION); michael@0: if (dummy == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: PORT_Assert (dummy == &signerinfo->version); michael@0: michael@0: signerinfo->cert = CERT_DupCertificate (cert); michael@0: if (signerinfo->cert == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: michael@0: signerinfo->issuerAndSN = CERT_GetCertIssuerAndSN (cinfo->poolp, cert); michael@0: if (signerinfo->issuerAndSN == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: michael@0: rv = SECOID_SetAlgorithmID (cinfo->poolp, &signerinfo->digestAlg, michael@0: digestalgtag, NULL); michael@0: if (rv != SECSuccess) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: michael@0: /* michael@0: * Okay, now signerinfo is all set. We just need to put it and its michael@0: * companions (another copy of the digest algorithm, and the digest michael@0: * itself if given) into the main structure. michael@0: * michael@0: * XXX If we are handling more than one signer, the following code michael@0: * needs to look through the digest algorithms already specified michael@0: * and see if the same one is there already. If it is, it does not michael@0: * need to be added again. Also, if it is there *and* the digest michael@0: * is not null, then the digest given should match the digest already michael@0: * specified -- if not, that is an error. Finally, the new signerinfo michael@0: * should be *added* to the set already found. michael@0: */ michael@0: michael@0: signerinfos = (SEC_PKCS7SignerInfo**)PORT_ArenaAlloc (cinfo->poolp, michael@0: 2 * sizeof(SEC_PKCS7SignerInfo *)); michael@0: if (signerinfos == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: signerinfos[0] = signerinfo; michael@0: signerinfos[1] = NULL; michael@0: michael@0: digestalg = PORT_ArenaZAlloc (cinfo->poolp, sizeof(SECAlgorithmID)); michael@0: digestalgs = PORT_ArenaAlloc (cinfo->poolp, 2 * sizeof(SECAlgorithmID *)); michael@0: if (digestalg == NULL || digestalgs == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: rv = SECOID_SetAlgorithmID (cinfo->poolp, digestalg, digestalgtag, NULL); michael@0: if (rv != SECSuccess) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: digestalgs[0] = digestalg; michael@0: digestalgs[1] = NULL; michael@0: michael@0: if (digestdata != NULL) { michael@0: digest = (SECItem*)PORT_ArenaAlloc (cinfo->poolp, sizeof(SECItem)); michael@0: digests = (SECItem**)PORT_ArenaAlloc (cinfo->poolp, michael@0: 2 * sizeof(SECItem *)); michael@0: if (digest == NULL || digests == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: rv = SECITEM_CopyItem (cinfo->poolp, digest, digestdata); michael@0: if (rv != SECSuccess) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: digests[0] = digest; michael@0: digests[1] = NULL; michael@0: } else { michael@0: digests = NULL; michael@0: } michael@0: michael@0: *signerinfosp = signerinfos; michael@0: *digestalgsp = digestalgs; michael@0: *digestsp = digests; michael@0: michael@0: PORT_ArenaUnmark(cinfo->poolp, mark); michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Helper function for creating an empty signedData. michael@0: */ michael@0: static SEC_PKCS7ContentInfo * michael@0: sec_pkcs7_create_signed_data (SECKEYGetPasswordKey pwfn, void *pwfn_arg) michael@0: { michael@0: SEC_PKCS7ContentInfo *cinfo; michael@0: SEC_PKCS7SignedData *sigd; michael@0: SECStatus rv; michael@0: michael@0: cinfo = sec_pkcs7_create_content_info (SEC_OID_PKCS7_SIGNED_DATA, PR_FALSE, michael@0: pwfn, pwfn_arg); michael@0: if (cinfo == NULL) michael@0: return NULL; michael@0: michael@0: sigd = cinfo->content.signedData; michael@0: PORT_Assert (sigd != NULL); michael@0: michael@0: /* michael@0: * XXX Might we want to allow content types other than data? michael@0: * If so, via what interface? michael@0: */ michael@0: rv = sec_pkcs7_init_content_info (&(sigd->contentInfo), cinfo->poolp, michael@0: SEC_OID_PKCS7_DATA, PR_TRUE); michael@0: if (rv != SECSuccess) { michael@0: SEC_PKCS7DestroyContentInfo (cinfo); michael@0: return NULL; michael@0: } michael@0: michael@0: return cinfo; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Start a PKCS7 signing context. michael@0: * michael@0: * "cert" is the cert that will be used to sign the data. It will be michael@0: * checked for validity. michael@0: * michael@0: * "certusage" describes the signing usage (e.g. certUsageEmailSigner) michael@0: * XXX Maybe SECCertUsage should be split so that our caller just says michael@0: * "email" and *we* add the "signing" part -- otherwise our caller michael@0: * could be lying about the usage; we do not want to allow encryption michael@0: * certs for signing or vice versa. michael@0: * michael@0: * "certdb" is the cert database to use for verifying the cert. michael@0: * It can be NULL if a default database is available (like in the client). michael@0: * michael@0: * "digestalg" names the digest algorithm (e.g. SEC_OID_SHA1). michael@0: * michael@0: * "digest" is the actual digest of the data. It must be provided in michael@0: * the case of detached data or NULL if the content will be included. michael@0: * michael@0: * The return value can be passed to functions which add things to michael@0: * it like attributes, then eventually to SEC_PKCS7Encode() or to michael@0: * SEC_PKCS7EncoderStart() to create the encoded data, and finally to michael@0: * SEC_PKCS7DestroyContentInfo(). 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: SEC_PKCS7ContentInfo * michael@0: SEC_PKCS7CreateSignedData (CERTCertificate *cert, michael@0: SECCertUsage certusage, michael@0: CERTCertDBHandle *certdb, michael@0: SECOidTag digestalg, michael@0: SECItem *digest, michael@0: SECKEYGetPasswordKey pwfn, void *pwfn_arg) michael@0: { michael@0: SEC_PKCS7ContentInfo *cinfo; michael@0: SECStatus rv; michael@0: michael@0: cinfo = sec_pkcs7_create_signed_data (pwfn, pwfn_arg); michael@0: if (cinfo == NULL) michael@0: return NULL; michael@0: michael@0: rv = sec_pkcs7_add_signer (cinfo, cert, certusage, certdb, michael@0: digestalg, digest); michael@0: if (rv != SECSuccess) { michael@0: SEC_PKCS7DestroyContentInfo (cinfo); michael@0: return NULL; michael@0: } michael@0: michael@0: return cinfo; michael@0: } michael@0: michael@0: michael@0: static SEC_PKCS7Attribute * michael@0: sec_pkcs7_create_attribute (PLArenaPool *poolp, SECOidTag oidtag, michael@0: SECItem *value, PRBool encoded) michael@0: { michael@0: SEC_PKCS7Attribute *attr; michael@0: SECItem **values; michael@0: void *mark; michael@0: michael@0: PORT_Assert (poolp != NULL); michael@0: mark = PORT_ArenaMark (poolp); michael@0: michael@0: attr = (SEC_PKCS7Attribute*)PORT_ArenaAlloc (poolp, michael@0: sizeof(SEC_PKCS7Attribute)); michael@0: if (attr == NULL) michael@0: goto loser; michael@0: michael@0: attr->typeTag = SECOID_FindOIDByTag (oidtag); michael@0: if (attr->typeTag == NULL) michael@0: goto loser; michael@0: michael@0: if (SECITEM_CopyItem (poolp, &(attr->type), michael@0: &(attr->typeTag->oid)) != SECSuccess) michael@0: goto loser; michael@0: michael@0: values = (SECItem**)PORT_ArenaAlloc (poolp, 2 * sizeof(SECItem *)); michael@0: if (values == NULL) michael@0: goto loser; michael@0: michael@0: if (value != NULL) { michael@0: SECItem *copy; michael@0: michael@0: copy = (SECItem*)PORT_ArenaAlloc (poolp, sizeof(SECItem)); michael@0: if (copy == NULL) michael@0: goto loser; michael@0: michael@0: if (SECITEM_CopyItem (poolp, copy, value) != SECSuccess) michael@0: goto loser; michael@0: michael@0: value = copy; michael@0: } michael@0: michael@0: values[0] = value; michael@0: values[1] = NULL; michael@0: attr->values = values; michael@0: attr->encoded = encoded; michael@0: michael@0: PORT_ArenaUnmark (poolp, mark); michael@0: return attr; michael@0: michael@0: loser: michael@0: PORT_Assert (mark != NULL); michael@0: PORT_ArenaRelease (poolp, mark); michael@0: return NULL; michael@0: } michael@0: michael@0: michael@0: static SECStatus michael@0: sec_pkcs7_add_attribute (SEC_PKCS7ContentInfo *cinfo, michael@0: SEC_PKCS7Attribute ***attrsp, michael@0: SEC_PKCS7Attribute *attr) michael@0: { michael@0: SEC_PKCS7Attribute **attrs; michael@0: SECItem *ct_value; michael@0: void *mark; michael@0: michael@0: PORT_Assert (SEC_PKCS7ContentType (cinfo) == SEC_OID_PKCS7_SIGNED_DATA); michael@0: if (SEC_PKCS7ContentType (cinfo) != SEC_OID_PKCS7_SIGNED_DATA) michael@0: return SECFailure; michael@0: michael@0: attrs = *attrsp; michael@0: if (attrs != NULL) { michael@0: int count; michael@0: michael@0: /* michael@0: * We already have some attributes, and just need to add this michael@0: * new one. michael@0: */ michael@0: michael@0: /* michael@0: * We should already have the *required* attributes, which were michael@0: * created/added at the same time the first attribute was added. michael@0: */ michael@0: PORT_Assert (sec_PKCS7FindAttribute (attrs, michael@0: SEC_OID_PKCS9_CONTENT_TYPE, michael@0: PR_FALSE) != NULL); michael@0: PORT_Assert (sec_PKCS7FindAttribute (attrs, michael@0: SEC_OID_PKCS9_MESSAGE_DIGEST, michael@0: PR_FALSE) != NULL); michael@0: michael@0: for (count = 0; attrs[count] != NULL; count++) michael@0: ; michael@0: attrs = (SEC_PKCS7Attribute**)PORT_ArenaGrow (cinfo->poolp, attrs, michael@0: (count + 1) * sizeof(SEC_PKCS7Attribute *), michael@0: (count + 2) * sizeof(SEC_PKCS7Attribute *)); michael@0: if (attrs == NULL) michael@0: return SECFailure; michael@0: michael@0: attrs[count] = attr; michael@0: attrs[count+1] = NULL; michael@0: *attrsp = attrs; michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: /* michael@0: * This is the first time an attribute is going in. michael@0: * We need to create and add the required attributes, and then michael@0: * we will also add in the one our caller gave us. michael@0: */ michael@0: michael@0: /* michael@0: * There are 2 required attributes, plus the one our caller wants michael@0: * to add, plus we always end with a NULL one. Thus, four slots. michael@0: */ michael@0: attrs = (SEC_PKCS7Attribute**)PORT_ArenaAlloc (cinfo->poolp, michael@0: 4 * sizeof(SEC_PKCS7Attribute *)); michael@0: if (attrs == NULL) michael@0: return SECFailure; michael@0: michael@0: mark = PORT_ArenaMark (cinfo->poolp); michael@0: michael@0: /* michael@0: * First required attribute is the content type of the data michael@0: * being signed. michael@0: */ michael@0: ct_value = &(cinfo->content.signedData->contentInfo.contentType); michael@0: attrs[0] = sec_pkcs7_create_attribute (cinfo->poolp, michael@0: SEC_OID_PKCS9_CONTENT_TYPE, michael@0: ct_value, PR_FALSE); michael@0: /* michael@0: * Second required attribute is the message digest of the data michael@0: * being signed; we leave the value NULL for now (just create michael@0: * the place for it to go), and the encoder will fill it in later. michael@0: */ michael@0: attrs[1] = sec_pkcs7_create_attribute (cinfo->poolp, michael@0: SEC_OID_PKCS9_MESSAGE_DIGEST, michael@0: NULL, PR_FALSE); michael@0: if (attrs[0] == NULL || attrs[1] == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: michael@0: attrs[2] = attr; michael@0: attrs[3] = NULL; michael@0: *attrsp = attrs; michael@0: michael@0: PORT_ArenaUnmark (cinfo->poolp, mark); michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Add the signing time to the authenticated (i.e. signed) attributes michael@0: * of "cinfo". This is expected to be included in outgoing signed michael@0: * messages for email (S/MIME) but is likely useful in other situations. michael@0: * michael@0: * This should only be added once; a second call will either do michael@0: * nothing or replace an old signing time with a newer one. michael@0: * michael@0: * XXX This will probably just shove the current time into "cinfo" michael@0: * but it will not actually get signed until the entire item is michael@0: * processed for encoding. Is this (expected to be small) delay okay? michael@0: * michael@0: * "cinfo" should be of type signedData (the only kind of pkcs7 data michael@0: * that is allowed authenticated attributes); SECFailure will be returned michael@0: * if it is not. michael@0: */ michael@0: SECStatus michael@0: SEC_PKCS7AddSigningTime (SEC_PKCS7ContentInfo *cinfo) michael@0: { michael@0: SEC_PKCS7SignerInfo **signerinfos; michael@0: SEC_PKCS7Attribute *attr; michael@0: SECItem stime; michael@0: SECStatus rv; michael@0: int si; michael@0: michael@0: PORT_Assert (SEC_PKCS7ContentType (cinfo) == SEC_OID_PKCS7_SIGNED_DATA); michael@0: if (SEC_PKCS7ContentType (cinfo) != SEC_OID_PKCS7_SIGNED_DATA) michael@0: return SECFailure; michael@0: michael@0: signerinfos = cinfo->content.signedData->signerInfos; michael@0: michael@0: /* There has to be a signer, or it makes no sense. */ michael@0: if (signerinfos == NULL || signerinfos[0] == NULL) michael@0: return SECFailure; michael@0: michael@0: rv = DER_EncodeTimeChoice(NULL, &stime, PR_Now()); michael@0: if (rv != SECSuccess) michael@0: return rv; michael@0: michael@0: attr = sec_pkcs7_create_attribute (cinfo->poolp, michael@0: SEC_OID_PKCS9_SIGNING_TIME, michael@0: &stime, PR_FALSE); michael@0: SECITEM_FreeItem (&stime, PR_FALSE); michael@0: michael@0: if (attr == NULL) michael@0: return SECFailure; michael@0: michael@0: rv = SECSuccess; michael@0: for (si = 0; signerinfos[si] != NULL; si++) { michael@0: SEC_PKCS7Attribute *oattr; michael@0: michael@0: oattr = sec_PKCS7FindAttribute (signerinfos[si]->authAttr, michael@0: SEC_OID_PKCS9_SIGNING_TIME, PR_FALSE); michael@0: PORT_Assert (oattr == NULL); michael@0: if (oattr != NULL) michael@0: continue; /* XXX or would it be better to replace it? */ michael@0: michael@0: rv = sec_pkcs7_add_attribute (cinfo, &(signerinfos[si]->authAttr), michael@0: attr); michael@0: if (rv != SECSuccess) michael@0: break; /* could try to continue, but may as well give up now */ michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Add the specified attribute to the authenticated (i.e. signed) attributes michael@0: * of "cinfo" -- "oidtag" describes the attribute and "value" is the michael@0: * value to be associated with it. NOTE! "value" must already be encoded; michael@0: * no interpretation of "oidtag" is done. Also, it is assumed that this michael@0: * signedData has only one signer -- if we ever need to add attributes michael@0: * when there is more than one signature, we need a way to specify *which* michael@0: * signature should get the attribute. michael@0: * michael@0: * XXX Technically, a signed attribute can have multiple values; if/when michael@0: * we ever need to support an attribute which takes multiple values, we michael@0: * either need to change this interface or create an AddSignedAttributeValue michael@0: * which can be called subsequently, and would then append a value. michael@0: * michael@0: * "cinfo" should be of type signedData (the only kind of pkcs7 data michael@0: * that is allowed authenticated attributes); SECFailure will be returned michael@0: * if it is not. michael@0: */ michael@0: SECStatus michael@0: SEC_PKCS7AddSignedAttribute (SEC_PKCS7ContentInfo *cinfo, michael@0: SECOidTag oidtag, michael@0: SECItem *value) michael@0: { michael@0: SEC_PKCS7SignerInfo **signerinfos; michael@0: SEC_PKCS7Attribute *attr; michael@0: michael@0: PORT_Assert (SEC_PKCS7ContentType (cinfo) == SEC_OID_PKCS7_SIGNED_DATA); michael@0: if (SEC_PKCS7ContentType (cinfo) != SEC_OID_PKCS7_SIGNED_DATA) michael@0: return SECFailure; michael@0: michael@0: signerinfos = cinfo->content.signedData->signerInfos; michael@0: michael@0: /* michael@0: * No signature or more than one means no deal. michael@0: */ michael@0: if (signerinfos == NULL || signerinfos[0] == NULL || signerinfos[1] != NULL) michael@0: return SECFailure; michael@0: michael@0: attr = sec_pkcs7_create_attribute (cinfo->poolp, oidtag, value, PR_TRUE); michael@0: if (attr == NULL) michael@0: return SECFailure; michael@0: michael@0: return sec_pkcs7_add_attribute (cinfo, &(signerinfos[0]->authAttr), attr); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Mark that the signer certificates and their issuing chain should michael@0: * be included in the encoded data. This is expected to be used michael@0: * in outgoing signed messages for email (S/MIME). michael@0: * michael@0: * "certdb" is the cert database to use for finding the chain. michael@0: * It can be NULL, meaning use the default database. michael@0: * michael@0: * "cinfo" should be of type signedData or signedAndEnvelopedData; michael@0: * SECFailure will be returned if it is not. michael@0: */ michael@0: SECStatus michael@0: SEC_PKCS7IncludeCertChain (SEC_PKCS7ContentInfo *cinfo, michael@0: CERTCertDBHandle *certdb) michael@0: { michael@0: SECOidTag kind; michael@0: SEC_PKCS7SignerInfo *signerinfo, **signerinfos; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: switch (kind) { michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: signerinfos = cinfo->content.signedData->signerInfos; michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: signerinfos = cinfo->content.signedAndEnvelopedData->signerInfos; michael@0: break; michael@0: default: michael@0: return SECFailure; /* XXX set an error? */ michael@0: } michael@0: michael@0: if (signerinfos == NULL) /* no signer, no certs? */ michael@0: return SECFailure; /* XXX set an error? */ michael@0: michael@0: if (certdb == NULL) { michael@0: certdb = CERT_GetDefaultCertDB(); michael@0: if (certdb == NULL) { michael@0: PORT_SetError (SEC_ERROR_BAD_DATABASE); michael@0: return SECFailure; michael@0: } michael@0: } michael@0: michael@0: /* XXX Should it be an error if we find no signerinfo or no certs? */ michael@0: while ((signerinfo = *signerinfos++) != NULL) { michael@0: if (signerinfo->cert != NULL) michael@0: /* get the cert chain. don't send the root to avoid contamination michael@0: * of old clients with a new root that they don't trust michael@0: */ michael@0: signerinfo->certList = CERT_CertChainFromCert (signerinfo->cert, michael@0: certUsageEmailSigner, michael@0: PR_FALSE); michael@0: } michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Helper function to add a certificate chain for inclusion in the michael@0: * bag of certificates in a signedData. michael@0: */ michael@0: static SECStatus michael@0: sec_pkcs7_add_cert_chain (SEC_PKCS7ContentInfo *cinfo, michael@0: CERTCertificate *cert, michael@0: CERTCertDBHandle *certdb) michael@0: { michael@0: SECOidTag kind; michael@0: CERTCertificateList *certlist, **certlists, ***certlistsp; michael@0: int count; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: switch (kind) { michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: { michael@0: SEC_PKCS7SignedData *sdp; michael@0: michael@0: sdp = cinfo->content.signedData; michael@0: certlistsp = &(sdp->certLists); michael@0: } michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: { michael@0: SEC_PKCS7SignedAndEnvelopedData *saedp; michael@0: michael@0: saedp = cinfo->content.signedAndEnvelopedData; michael@0: certlistsp = &(saedp->certLists); michael@0: } michael@0: break; michael@0: default: michael@0: return SECFailure; /* XXX set an error? */ michael@0: } michael@0: michael@0: if (certdb == NULL) { michael@0: certdb = CERT_GetDefaultCertDB(); michael@0: if (certdb == NULL) { michael@0: PORT_SetError (SEC_ERROR_BAD_DATABASE); michael@0: return SECFailure; michael@0: } michael@0: } michael@0: michael@0: certlist = CERT_CertChainFromCert (cert, certUsageEmailSigner, PR_FALSE); michael@0: if (certlist == NULL) michael@0: return SECFailure; michael@0: michael@0: certlists = *certlistsp; michael@0: if (certlists == NULL) { michael@0: count = 0; michael@0: certlists = (CERTCertificateList**)PORT_ArenaAlloc (cinfo->poolp, michael@0: 2 * sizeof(CERTCertificateList *)); michael@0: } else { michael@0: for (count = 0; certlists[count] != NULL; count++) michael@0: ; michael@0: PORT_Assert (count); /* should be at least one already */ michael@0: certlists = (CERTCertificateList**)PORT_ArenaGrow (cinfo->poolp, michael@0: certlists, michael@0: (count + 1) * sizeof(CERTCertificateList *), michael@0: (count + 2) * sizeof(CERTCertificateList *)); michael@0: } michael@0: michael@0: if (certlists == NULL) { michael@0: CERT_DestroyCertificateList (certlist); michael@0: return SECFailure; michael@0: } michael@0: michael@0: certlists[count] = certlist; michael@0: certlists[count + 1] = NULL; michael@0: michael@0: *certlistsp = certlists; michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Helper function to add a certificate for inclusion in the bag of michael@0: * certificates in a signedData. michael@0: */ michael@0: static SECStatus michael@0: sec_pkcs7_add_certificate (SEC_PKCS7ContentInfo *cinfo, michael@0: CERTCertificate *cert) michael@0: { michael@0: SECOidTag kind; michael@0: CERTCertificate **certs, ***certsp; michael@0: int count; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: switch (kind) { michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: { michael@0: SEC_PKCS7SignedData *sdp; michael@0: michael@0: sdp = cinfo->content.signedData; michael@0: certsp = &(sdp->certs); michael@0: } michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: { michael@0: SEC_PKCS7SignedAndEnvelopedData *saedp; michael@0: michael@0: saedp = cinfo->content.signedAndEnvelopedData; michael@0: certsp = &(saedp->certs); michael@0: } michael@0: break; michael@0: default: michael@0: return SECFailure; /* XXX set an error? */ michael@0: } michael@0: michael@0: cert = CERT_DupCertificate (cert); michael@0: if (cert == NULL) michael@0: return SECFailure; michael@0: michael@0: certs = *certsp; michael@0: if (certs == NULL) { michael@0: count = 0; michael@0: certs = (CERTCertificate**)PORT_ArenaAlloc (cinfo->poolp, michael@0: 2 * sizeof(CERTCertificate *)); michael@0: } else { michael@0: for (count = 0; certs[count] != NULL; count++) michael@0: ; michael@0: PORT_Assert (count); /* should be at least one already */ michael@0: certs = (CERTCertificate**)PORT_ArenaGrow (cinfo->poolp, certs, michael@0: (count + 1) * sizeof(CERTCertificate *), michael@0: (count + 2) * sizeof(CERTCertificate *)); michael@0: } michael@0: michael@0: if (certs == NULL) { michael@0: CERT_DestroyCertificate (cert); michael@0: return SECFailure; michael@0: } michael@0: michael@0: certs[count] = cert; michael@0: certs[count + 1] = NULL; michael@0: michael@0: *certsp = certs; michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Create a PKCS7 certs-only container. michael@0: * michael@0: * "cert" is the (first) cert that will be included. michael@0: * michael@0: * "include_chain" specifies whether the entire chain for "cert" should michael@0: * be included. michael@0: * michael@0: * "certdb" is the cert database to use for finding the chain. michael@0: * It can be NULL in when "include_chain" is false, or when meaning michael@0: * use the default database. michael@0: * michael@0: * More certs and chains can be added via AddCertificate and AddCertChain. 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: SEC_PKCS7ContentInfo * michael@0: SEC_PKCS7CreateCertsOnly (CERTCertificate *cert, michael@0: PRBool include_chain, michael@0: CERTCertDBHandle *certdb) michael@0: { michael@0: SEC_PKCS7ContentInfo *cinfo; michael@0: SECStatus rv; michael@0: michael@0: cinfo = sec_pkcs7_create_signed_data (NULL, NULL); michael@0: if (cinfo == NULL) michael@0: return NULL; michael@0: michael@0: if (include_chain) michael@0: rv = sec_pkcs7_add_cert_chain (cinfo, cert, certdb); michael@0: else michael@0: rv = sec_pkcs7_add_certificate (cinfo, cert); michael@0: michael@0: if (rv != SECSuccess) { michael@0: SEC_PKCS7DestroyContentInfo (cinfo); michael@0: return NULL; michael@0: } michael@0: michael@0: return cinfo; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Add "cert" and its entire chain to the set of certs included in "cinfo". michael@0: * michael@0: * "certdb" is the cert database to use for finding the chain. michael@0: * It can be NULL, meaning use the default database. michael@0: * michael@0: * "cinfo" should be of type signedData or signedAndEnvelopedData; michael@0: * SECFailure will be returned if it is not. michael@0: */ michael@0: SECStatus michael@0: SEC_PKCS7AddCertChain (SEC_PKCS7ContentInfo *cinfo, michael@0: CERTCertificate *cert, michael@0: CERTCertDBHandle *certdb) michael@0: { michael@0: SECOidTag kind; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: if (kind != SEC_OID_PKCS7_SIGNED_DATA michael@0: && kind != SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA) michael@0: return SECFailure; /* XXX set an error? */ michael@0: michael@0: return sec_pkcs7_add_cert_chain (cinfo, cert, certdb); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Add "cert" to the set of certs included in "cinfo". michael@0: * michael@0: * "cinfo" should be of type signedData or signedAndEnvelopedData; michael@0: * SECFailure will be returned if it is not. michael@0: */ michael@0: SECStatus michael@0: SEC_PKCS7AddCertificate (SEC_PKCS7ContentInfo *cinfo, CERTCertificate *cert) michael@0: { michael@0: SECOidTag kind; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: if (kind != SEC_OID_PKCS7_SIGNED_DATA michael@0: && kind != SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA) michael@0: return SECFailure; /* XXX set an error? */ michael@0: michael@0: return sec_pkcs7_add_certificate (cinfo, cert); michael@0: } michael@0: michael@0: michael@0: static SECStatus michael@0: sec_pkcs7_init_encrypted_content_info (SEC_PKCS7EncryptedContentInfo *enccinfo, michael@0: PLArenaPool *poolp, michael@0: SECOidTag kind, PRBool detached, michael@0: SECOidTag encalg, int keysize) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: PORT_Assert (enccinfo != NULL && poolp != NULL); michael@0: if (enccinfo == NULL || poolp == NULL) michael@0: return SECFailure; michael@0: michael@0: /* michael@0: * XXX Some day we may want to allow for other kinds. That needs michael@0: * more work and modifications to the creation interface, etc. michael@0: * For now, allow but notice callers who pass in other kinds. michael@0: * They are responsible for creating the inner type and encoding, michael@0: * if it is other than DATA. michael@0: */ michael@0: PORT_Assert (kind == SEC_OID_PKCS7_DATA); michael@0: michael@0: enccinfo->contentTypeTag = SECOID_FindOIDByTag (kind); michael@0: PORT_Assert (enccinfo->contentTypeTag michael@0: && enccinfo->contentTypeTag->offset == kind); michael@0: michael@0: rv = SECITEM_CopyItem (poolp, &(enccinfo->contentType), michael@0: &(enccinfo->contentTypeTag->oid)); michael@0: if (rv != SECSuccess) michael@0: return rv; michael@0: michael@0: /* Save keysize and algorithm for later. */ michael@0: enccinfo->keysize = keysize; michael@0: enccinfo->encalg = encalg; michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Add a recipient to a PKCS7 thing, verifying their cert first. michael@0: * Any error returns SECFailure. michael@0: */ michael@0: static SECStatus michael@0: sec_pkcs7_add_recipient (SEC_PKCS7ContentInfo *cinfo, michael@0: CERTCertificate *cert, michael@0: SECCertUsage certusage, michael@0: CERTCertDBHandle *certdb) michael@0: { michael@0: SECOidTag kind; michael@0: SEC_PKCS7RecipientInfo *recipientinfo, **recipientinfos, ***recipientinfosp; michael@0: SECItem *dummy; michael@0: void *mark; michael@0: int count; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: switch (kind) { michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: { michael@0: SEC_PKCS7EnvelopedData *edp; michael@0: michael@0: edp = cinfo->content.envelopedData; michael@0: recipientinfosp = &(edp->recipientInfos); michael@0: } michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: { michael@0: SEC_PKCS7SignedAndEnvelopedData *saedp; michael@0: michael@0: saedp = cinfo->content.signedAndEnvelopedData; michael@0: recipientinfosp = &(saedp->recipientInfos); michael@0: } michael@0: break; michael@0: default: michael@0: return SECFailure; /* XXX set an error? */ michael@0: } michael@0: michael@0: /* michael@0: * XXX I think that CERT_VerifyCert should do this if *it* is passed michael@0: * a NULL database. michael@0: */ michael@0: if (certdb == NULL) { michael@0: certdb = CERT_GetDefaultCertDB(); michael@0: if (certdb == NULL) michael@0: return SECFailure; /* XXX set an error? */ michael@0: } michael@0: michael@0: if (CERT_VerifyCert (certdb, cert, PR_TRUE, certusage, PR_Now(), michael@0: cinfo->pwfn_arg, NULL) != SECSuccess) michael@0: { michael@0: /* XXX Did CERT_VerifyCert set an error? */ michael@0: return SECFailure; michael@0: } michael@0: michael@0: mark = PORT_ArenaMark (cinfo->poolp); michael@0: michael@0: recipientinfo = (SEC_PKCS7RecipientInfo*)PORT_ArenaZAlloc (cinfo->poolp, michael@0: sizeof(SEC_PKCS7RecipientInfo)); michael@0: if (recipientinfo == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: michael@0: dummy = SEC_ASN1EncodeInteger (cinfo->poolp, &recipientinfo->version, michael@0: SEC_PKCS7_RECIPIENT_INFO_VERSION); michael@0: if (dummy == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: PORT_Assert (dummy == &recipientinfo->version); michael@0: michael@0: recipientinfo->cert = CERT_DupCertificate (cert); michael@0: if (recipientinfo->cert == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: michael@0: recipientinfo->issuerAndSN = CERT_GetCertIssuerAndSN (cinfo->poolp, cert); michael@0: if (recipientinfo->issuerAndSN == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: michael@0: /* michael@0: * Okay, now recipientinfo is all set. We just need to put it into michael@0: * the main structure. michael@0: * michael@0: * If this is the first recipient, allocate a new recipientinfos array; michael@0: * otherwise, reallocate the array, making room for the new entry. michael@0: */ michael@0: recipientinfos = *recipientinfosp; michael@0: if (recipientinfos == NULL) { michael@0: count = 0; michael@0: recipientinfos = (SEC_PKCS7RecipientInfo **)PORT_ArenaAlloc ( michael@0: cinfo->poolp, michael@0: 2 * sizeof(SEC_PKCS7RecipientInfo *)); michael@0: } else { michael@0: for (count = 0; recipientinfos[count] != NULL; count++) michael@0: ; michael@0: PORT_Assert (count); /* should be at least one already */ michael@0: recipientinfos = (SEC_PKCS7RecipientInfo **)PORT_ArenaGrow ( michael@0: cinfo->poolp, recipientinfos, michael@0: (count + 1) * sizeof(SEC_PKCS7RecipientInfo *), michael@0: (count + 2) * sizeof(SEC_PKCS7RecipientInfo *)); michael@0: } michael@0: michael@0: if (recipientinfos == NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: return SECFailure; michael@0: } michael@0: michael@0: recipientinfos[count] = recipientinfo; michael@0: recipientinfos[count + 1] = NULL; michael@0: michael@0: *recipientinfosp = recipientinfos; michael@0: michael@0: PORT_ArenaUnmark (cinfo->poolp, mark); michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Start a PKCS7 enveloping context. michael@0: * michael@0: * "cert" is the cert for the recipient. It will be checked for validity. michael@0: * michael@0: * "certusage" describes the encryption usage (e.g. certUsageEmailRecipient) michael@0: * XXX Maybe SECCertUsage should be split so that our caller just says michael@0: * "email" and *we* add the "recipient" part -- otherwise our caller michael@0: * could be lying about the usage; we do not want to allow encryption michael@0: * certs for signing or vice versa. michael@0: * michael@0: * "certdb" is the cert database to use for verifying the cert. michael@0: * It can be NULL if a default database is available (like in the client). michael@0: * michael@0: * "encalg" specifies the bulk encryption algorithm to use (e.g. SEC_OID_RC2). michael@0: * michael@0: * "keysize" specifies the bulk encryption key size, in bits. michael@0: * michael@0: * The return value can be passed to functions which add things to michael@0: * it like more recipients, then eventually to SEC_PKCS7Encode() or to michael@0: * SEC_PKCS7EncoderStart() to create the encoded data, and finally to michael@0: * SEC_PKCS7DestroyContentInfo(). 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: extern SEC_PKCS7ContentInfo * michael@0: SEC_PKCS7CreateEnvelopedData (CERTCertificate *cert, michael@0: SECCertUsage certusage, michael@0: CERTCertDBHandle *certdb, michael@0: SECOidTag encalg, michael@0: int keysize, michael@0: SECKEYGetPasswordKey pwfn, void *pwfn_arg) michael@0: { michael@0: SEC_PKCS7ContentInfo *cinfo; michael@0: SEC_PKCS7EnvelopedData *envd; michael@0: SECStatus rv; michael@0: michael@0: cinfo = sec_pkcs7_create_content_info (SEC_OID_PKCS7_ENVELOPED_DATA, michael@0: PR_FALSE, pwfn, pwfn_arg); michael@0: if (cinfo == NULL) michael@0: return NULL; michael@0: michael@0: rv = sec_pkcs7_add_recipient (cinfo, cert, certusage, certdb); michael@0: if (rv != SECSuccess) { michael@0: SEC_PKCS7DestroyContentInfo (cinfo); michael@0: return NULL; michael@0: } michael@0: michael@0: envd = cinfo->content.envelopedData; michael@0: PORT_Assert (envd != NULL); michael@0: michael@0: /* michael@0: * XXX Might we want to allow content types other than data? michael@0: * If so, via what interface? michael@0: */ michael@0: rv = sec_pkcs7_init_encrypted_content_info (&(envd->encContentInfo), michael@0: cinfo->poolp, michael@0: SEC_OID_PKCS7_DATA, PR_FALSE, michael@0: encalg, keysize); michael@0: if (rv != SECSuccess) { michael@0: SEC_PKCS7DestroyContentInfo (cinfo); michael@0: return NULL; michael@0: } michael@0: michael@0: /* XXX Anything more to do here? */ michael@0: michael@0: return cinfo; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Add another recipient to an encrypted message. michael@0: * michael@0: * "cinfo" should be of type envelopedData or signedAndEnvelopedData; michael@0: * SECFailure will be returned if it is not. michael@0: * michael@0: * "cert" is the cert for the recipient. It will be checked for validity. michael@0: * michael@0: * "certusage" describes the encryption usage (e.g. certUsageEmailRecipient) michael@0: * XXX Maybe SECCertUsage should be split so that our caller just says michael@0: * "email" and *we* add the "recipient" part -- otherwise our caller michael@0: * could be lying about the usage; we do not want to allow encryption michael@0: * certs for signing or vice versa. michael@0: * michael@0: * "certdb" is the cert database to use for verifying the cert. michael@0: * It can be NULL if a default database is available (like in the client). michael@0: */ michael@0: SECStatus michael@0: SEC_PKCS7AddRecipient (SEC_PKCS7ContentInfo *cinfo, michael@0: CERTCertificate *cert, michael@0: SECCertUsage certusage, michael@0: CERTCertDBHandle *certdb) michael@0: { michael@0: return sec_pkcs7_add_recipient (cinfo, cert, certusage, certdb); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Create an empty PKCS7 data content info. 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: SEC_PKCS7ContentInfo * michael@0: SEC_PKCS7CreateData (void) michael@0: { michael@0: return sec_pkcs7_create_content_info (SEC_OID_PKCS7_DATA, PR_FALSE, michael@0: NULL, NULL); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Create an empty PKCS7 encrypted content info. michael@0: * michael@0: * "algorithm" specifies the bulk encryption algorithm to use. 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: SEC_PKCS7ContentInfo * michael@0: SEC_PKCS7CreateEncryptedData (SECOidTag algorithm, int keysize, michael@0: SECKEYGetPasswordKey pwfn, void *pwfn_arg) michael@0: { michael@0: SEC_PKCS7ContentInfo *cinfo; michael@0: SECAlgorithmID *algid; michael@0: SEC_PKCS7EncryptedData *enc_data; michael@0: SECStatus rv; michael@0: michael@0: cinfo = sec_pkcs7_create_content_info (SEC_OID_PKCS7_ENCRYPTED_DATA, michael@0: PR_FALSE, pwfn, pwfn_arg); michael@0: if (cinfo == NULL) michael@0: return NULL; michael@0: michael@0: enc_data = cinfo->content.encryptedData; michael@0: algid = &(enc_data->encContentInfo.contentEncAlg); michael@0: michael@0: if (!SEC_PKCS5IsAlgorithmPBEAlgTag(algorithm)) { michael@0: rv = SECOID_SetAlgorithmID (cinfo->poolp, algid, algorithm, NULL); 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 SEC_PKCS7CreateEncryptedtedData michael@0: * to create pkcs5v2 PBEs */ michael@0: SECAlgorithmID *pbe_algid; michael@0: pbe_algid = PK11_CreatePBEAlgorithmID(algorithm, michael@0: NSS_PBE_DEFAULT_ITERATION_COUNT, michael@0: NULL); michael@0: if (pbe_algid == NULL) { michael@0: rv = SECFailure; michael@0: } else { michael@0: rv = SECOID_CopyAlgorithmID (cinfo->poolp, algid, pbe_algid); michael@0: SECOID_DestroyAlgorithmID (pbe_algid, PR_TRUE); michael@0: } michael@0: } michael@0: michael@0: if (rv != SECSuccess) { michael@0: SEC_PKCS7DestroyContentInfo (cinfo); michael@0: return NULL; michael@0: } michael@0: michael@0: rv = sec_pkcs7_init_encrypted_content_info (&(enc_data->encContentInfo), michael@0: cinfo->poolp, michael@0: SEC_OID_PKCS7_DATA, PR_FALSE, michael@0: algorithm, keysize); michael@0: if (rv != SECSuccess) { michael@0: SEC_PKCS7DestroyContentInfo (cinfo); michael@0: return NULL; michael@0: } michael@0: michael@0: return cinfo; michael@0: } michael@0: