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 encoding. michael@0: */ michael@0: michael@0: #include "p7local.h" michael@0: michael@0: #include "cert.h" michael@0: #include "cryptohi.h" michael@0: #include "keyhi.h" michael@0: #include "secasn1.h" michael@0: #include "secoid.h" michael@0: #include "secitem.h" michael@0: #include "pk11func.h" michael@0: #include "secerr.h" michael@0: #include "sechash.h" /* for HASH_GetHashObject() */ michael@0: michael@0: struct sec_pkcs7_encoder_output { michael@0: SEC_PKCS7EncoderOutputCallback outputfn; michael@0: void *outputarg; michael@0: }; michael@0: michael@0: struct SEC_PKCS7EncoderContextStr { michael@0: SEC_ASN1EncoderContext *ecx; michael@0: SEC_PKCS7ContentInfo *cinfo; michael@0: struct sec_pkcs7_encoder_output output; michael@0: sec_PKCS7CipherObject *encryptobj; michael@0: const SECHashObject *digestobj; michael@0: void *digestcx; michael@0: }; michael@0: michael@0: michael@0: /* michael@0: * The little output function that the ASN.1 encoder calls to hand michael@0: * us bytes which we in turn hand back to our caller (via the callback michael@0: * they gave us). michael@0: */ michael@0: static void michael@0: sec_pkcs7_encoder_out(void *arg, const char *buf, unsigned long len, michael@0: int depth, SEC_ASN1EncodingPart data_kind) michael@0: { michael@0: struct sec_pkcs7_encoder_output *output; michael@0: michael@0: output = (struct sec_pkcs7_encoder_output*)arg; michael@0: output->outputfn (output->outputarg, buf, len); michael@0: } michael@0: michael@0: static sec_PKCS7CipherObject * michael@0: sec_pkcs7_encoder_start_encrypt (SEC_PKCS7ContentInfo *cinfo, michael@0: PK11SymKey *orig_bulkkey) michael@0: { michael@0: SECOidTag kind; michael@0: sec_PKCS7CipherObject *encryptobj; michael@0: SEC_PKCS7RecipientInfo **recipientinfos, *ri; michael@0: SEC_PKCS7EncryptedContentInfo *enccinfo; michael@0: SECKEYPublicKey *publickey = NULL; michael@0: SECKEYPrivateKey *ourPrivKey = NULL; michael@0: PK11SymKey *bulkkey; michael@0: void *mark, *wincx; michael@0: int i; michael@0: PLArenaPool *arena = NULL; michael@0: michael@0: /* Get the context in case we need it below. */ michael@0: wincx = cinfo->pwfn_arg; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: switch (kind) { michael@0: default: michael@0: case SEC_OID_PKCS7_DATA: michael@0: case SEC_OID_PKCS7_DIGESTED_DATA: michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: recipientinfos = NULL; michael@0: enccinfo = NULL; michael@0: break; michael@0: case SEC_OID_PKCS7_ENCRYPTED_DATA: michael@0: { michael@0: SEC_PKCS7EncryptedData *encdp; michael@0: michael@0: /* To do EncryptedData we *must* be given a bulk key. */ michael@0: PORT_Assert (orig_bulkkey != NULL); michael@0: if (orig_bulkkey == NULL) { michael@0: /* XXX error? */ michael@0: return NULL; michael@0: } michael@0: michael@0: encdp = cinfo->content.encryptedData; michael@0: recipientinfos = NULL; michael@0: enccinfo = &(encdp->encContentInfo); michael@0: } michael@0: break; michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: { michael@0: SEC_PKCS7EnvelopedData *envdp; michael@0: michael@0: envdp = cinfo->content.envelopedData; michael@0: recipientinfos = envdp->recipientInfos; michael@0: enccinfo = &(envdp->encContentInfo); 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: recipientinfos = saedp->recipientInfos; michael@0: enccinfo = &(saedp->encContentInfo); michael@0: } michael@0: break; michael@0: } michael@0: michael@0: if (enccinfo == NULL) michael@0: return NULL; michael@0: michael@0: bulkkey = orig_bulkkey; michael@0: if (bulkkey == NULL) { michael@0: CK_MECHANISM_TYPE type = PK11_AlgtagToMechanism(enccinfo->encalg); michael@0: PK11SlotInfo *slot; michael@0: michael@0: michael@0: slot = PK11_GetBestSlot(type,cinfo->pwfn_arg); michael@0: if (slot == NULL) { michael@0: return NULL; michael@0: } michael@0: bulkkey = PK11_KeyGen(slot,type,NULL, enccinfo->keysize/8, michael@0: cinfo->pwfn_arg); michael@0: PK11_FreeSlot(slot); michael@0: if (bulkkey == NULL) { michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: encryptobj = NULL; michael@0: mark = PORT_ArenaMark (cinfo->poolp); michael@0: michael@0: /* michael@0: * Encrypt the bulk key with the public key of each recipient. michael@0: */ michael@0: for (i = 0; recipientinfos && (ri = recipientinfos[i]) != NULL; i++) { michael@0: CERTCertificate *cert; michael@0: SECOidTag certalgtag, encalgtag; michael@0: SECStatus rv; michael@0: int data_len; michael@0: SECItem *params = NULL; michael@0: michael@0: cert = ri->cert; michael@0: PORT_Assert (cert != NULL); michael@0: if (cert == NULL) michael@0: continue; michael@0: michael@0: /* michael@0: * XXX Want an interface that takes a cert and some data and michael@0: * fills in an algorithmID and encrypts the data with the public michael@0: * key from the cert. Or, give me two interfaces -- one which michael@0: * gets the algorithm tag from a cert (I should not have to go michael@0: * down into the subjectPublicKeyInfo myself) and another which michael@0: * takes a public key and algorithm tag and data and encrypts michael@0: * the data. Or something like that. The point is that all michael@0: * of the following hardwired RSA stuff should be done elsewhere. michael@0: */ michael@0: michael@0: certalgtag=SECOID_GetAlgorithmTag(&(cert->subjectPublicKeyInfo.algorithm)); michael@0: michael@0: switch (certalgtag) { michael@0: case SEC_OID_PKCS1_RSA_ENCRYPTION: michael@0: encalgtag = certalgtag; michael@0: publickey = CERT_ExtractPublicKey (cert); michael@0: if (publickey == NULL) goto loser; michael@0: michael@0: data_len = SECKEY_PublicKeyStrength(publickey); michael@0: ri->encKey.data = michael@0: (unsigned char*)PORT_ArenaAlloc(cinfo->poolp ,data_len); michael@0: ri->encKey.len = data_len; michael@0: if (ri->encKey.data == NULL) goto loser; michael@0: michael@0: rv = PK11_PubWrapSymKey(PK11_AlgtagToMechanism(certalgtag),publickey, michael@0: bulkkey,&ri->encKey); michael@0: michael@0: SECKEY_DestroyPublicKey(publickey); michael@0: publickey = NULL; michael@0: if (rv != SECSuccess) goto loser; michael@0: params = NULL; /* paranoia */ michael@0: break; michael@0: default: michael@0: PORT_SetError (SEC_ERROR_INVALID_ALGORITHM); michael@0: goto loser; michael@0: } michael@0: michael@0: rv = SECOID_SetAlgorithmID(cinfo->poolp, &ri->keyEncAlg, encalgtag, michael@0: params); michael@0: if (rv != SECSuccess) michael@0: goto loser; michael@0: if (arena) PORT_FreeArena(arena,PR_FALSE); michael@0: arena = NULL; michael@0: } michael@0: michael@0: encryptobj = sec_PKCS7CreateEncryptObject (cinfo->poolp, bulkkey, michael@0: enccinfo->encalg, michael@0: &(enccinfo->contentEncAlg)); michael@0: if (encryptobj != NULL) { michael@0: PORT_ArenaUnmark (cinfo->poolp, mark); michael@0: mark = NULL; /* good one; do not want to release */ michael@0: } michael@0: /* fallthru */ michael@0: michael@0: loser: michael@0: if (arena) { michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: } michael@0: if (publickey) { michael@0: SECKEY_DestroyPublicKey(publickey); michael@0: } michael@0: if (ourPrivKey) { michael@0: SECKEY_DestroyPrivateKey(ourPrivKey); michael@0: } michael@0: if (mark != NULL) { michael@0: PORT_ArenaRelease (cinfo->poolp, mark); michael@0: } michael@0: if (orig_bulkkey == NULL) { michael@0: if (bulkkey) PK11_FreeSymKey(bulkkey); michael@0: } michael@0: michael@0: return encryptobj; michael@0: } michael@0: michael@0: michael@0: static void michael@0: sec_pkcs7_encoder_notify (void *arg, PRBool before, void *dest, int depth) michael@0: { michael@0: SEC_PKCS7EncoderContext *p7ecx; michael@0: SEC_PKCS7ContentInfo *cinfo; michael@0: SECOidTag kind; michael@0: PRBool before_content; michael@0: michael@0: /* michael@0: * We want to notice just before the content field. After fields are michael@0: * not interesting to us. michael@0: */ michael@0: if (!before) michael@0: return; michael@0: michael@0: p7ecx = (SEC_PKCS7EncoderContext*)arg; michael@0: cinfo = p7ecx->cinfo; michael@0: michael@0: before_content = PR_FALSE; michael@0: michael@0: /* michael@0: * Watch for the content field, at which point we want to instruct michael@0: * the ASN.1 encoder to start taking bytes from the buffer. michael@0: * michael@0: * XXX The following assumes the inner content type is data; michael@0: * if/when we want to handle fully nested types, this will have michael@0: * to recurse until reaching the innermost data content. michael@0: */ michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: switch (kind) { michael@0: default: michael@0: case SEC_OID_PKCS7_DATA: michael@0: if (dest == &(cinfo->content.data)) michael@0: before_content = PR_TRUE; michael@0: break; michael@0: michael@0: case SEC_OID_PKCS7_DIGESTED_DATA: michael@0: { michael@0: SEC_PKCS7DigestedData *digd; michael@0: michael@0: digd = cinfo->content.digestedData; michael@0: if (digd == NULL) michael@0: break; michael@0: michael@0: if (dest == &(digd->contentInfo.content)) michael@0: before_content = PR_TRUE; michael@0: } michael@0: break; michael@0: michael@0: case SEC_OID_PKCS7_ENCRYPTED_DATA: michael@0: { michael@0: SEC_PKCS7EncryptedData *encd; michael@0: michael@0: encd = cinfo->content.encryptedData; michael@0: if (encd == NULL) michael@0: break; michael@0: michael@0: if (dest == &(encd->encContentInfo.encContent)) michael@0: before_content = PR_TRUE; michael@0: } michael@0: break; michael@0: michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: { michael@0: SEC_PKCS7EnvelopedData *envd; michael@0: michael@0: envd = cinfo->content.envelopedData; michael@0: if (envd == NULL) michael@0: break; michael@0: michael@0: if (dest == &(envd->encContentInfo.encContent)) michael@0: before_content = PR_TRUE; michael@0: } michael@0: break; michael@0: michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: { michael@0: SEC_PKCS7SignedData *sigd; michael@0: michael@0: sigd = cinfo->content.signedData; michael@0: if (sigd == NULL) michael@0: break; michael@0: michael@0: if (dest == &(sigd->contentInfo.content)) michael@0: before_content = PR_TRUE; michael@0: } michael@0: break; michael@0: michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: { michael@0: SEC_PKCS7SignedAndEnvelopedData *saed; michael@0: michael@0: saed = cinfo->content.signedAndEnvelopedData; michael@0: if (saed == NULL) michael@0: break; michael@0: michael@0: if (dest == &(saed->encContentInfo.encContent)) michael@0: before_content = PR_TRUE; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: if (before_content) { michael@0: /* michael@0: * This will cause the next SEC_ASN1EncoderUpdate to take the michael@0: * contents bytes from the passed-in buffer. michael@0: */ michael@0: SEC_ASN1EncoderSetTakeFromBuf (p7ecx->ecx); michael@0: /* michael@0: * And that is all we needed this notify function for. michael@0: */ michael@0: SEC_ASN1EncoderClearNotifyProc (p7ecx->ecx); michael@0: } michael@0: } michael@0: michael@0: michael@0: static SEC_PKCS7EncoderContext * michael@0: sec_pkcs7_encoder_start_contexts (SEC_PKCS7ContentInfo *cinfo, michael@0: PK11SymKey *bulkkey) michael@0: { michael@0: SEC_PKCS7EncoderContext *p7ecx; michael@0: SECOidTag kind; michael@0: PRBool encrypt; michael@0: SECItem **digests; michael@0: SECAlgorithmID *digestalg, **digestalgs; michael@0: michael@0: p7ecx = michael@0: (SEC_PKCS7EncoderContext*)PORT_ZAlloc (sizeof(SEC_PKCS7EncoderContext)); michael@0: if (p7ecx == NULL) michael@0: return NULL; michael@0: michael@0: digests = NULL; michael@0: digestalg = NULL; michael@0: digestalgs = NULL; michael@0: encrypt = PR_FALSE; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: switch (kind) { michael@0: default: michael@0: case SEC_OID_PKCS7_DATA: michael@0: break; michael@0: case SEC_OID_PKCS7_DIGESTED_DATA: michael@0: digestalg = &(cinfo->content.digestedData->digestAlg); michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: digests = cinfo->content.signedData->digests; michael@0: digestalgs = cinfo->content.signedData->digestAlgorithms; michael@0: break; michael@0: case SEC_OID_PKCS7_ENCRYPTED_DATA: michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: encrypt = PR_TRUE; michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: digests = cinfo->content.signedAndEnvelopedData->digests; michael@0: digestalgs = cinfo->content.signedAndEnvelopedData->digestAlgorithms; michael@0: encrypt = PR_TRUE; michael@0: break; michael@0: } michael@0: michael@0: if (encrypt) { michael@0: p7ecx->encryptobj = sec_pkcs7_encoder_start_encrypt (cinfo, bulkkey); michael@0: if (p7ecx->encryptobj == NULL) { michael@0: PORT_Free (p7ecx); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: if (digestalgs != NULL) { michael@0: if (digests != NULL) { michael@0: /* digests already created (probably for detached data) */ michael@0: digestalg = NULL; michael@0: } else { michael@0: /* michael@0: * XXX Some day we should handle multiple digests; for now, michael@0: * assume only one will be done. michael@0: */ michael@0: PORT_Assert (digestalgs[0] != NULL && digestalgs[1] == NULL); michael@0: digestalg = digestalgs[0]; michael@0: } michael@0: } michael@0: michael@0: if (digestalg != NULL) { michael@0: SECOidTag oidTag = SECOID_FindOIDTag(&(digestalg->algorithm)); michael@0: michael@0: p7ecx->digestobj = HASH_GetHashObjectByOidTag(oidTag); michael@0: if (p7ecx->digestobj != NULL) { michael@0: p7ecx->digestcx = (* p7ecx->digestobj->create) (); michael@0: if (p7ecx->digestcx == NULL) michael@0: p7ecx->digestobj = NULL; michael@0: else michael@0: (* p7ecx->digestobj->begin) (p7ecx->digestcx); michael@0: } michael@0: if (p7ecx->digestobj == NULL) { michael@0: if (p7ecx->encryptobj != NULL) michael@0: sec_PKCS7DestroyEncryptObject (p7ecx->encryptobj); michael@0: PORT_Free (p7ecx); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: p7ecx->cinfo = cinfo; michael@0: return p7ecx; michael@0: } michael@0: michael@0: michael@0: SEC_PKCS7EncoderContext * michael@0: SEC_PKCS7EncoderStart (SEC_PKCS7ContentInfo *cinfo, michael@0: SEC_PKCS7EncoderOutputCallback outputfn, michael@0: void *outputarg, michael@0: PK11SymKey *bulkkey) michael@0: { michael@0: SEC_PKCS7EncoderContext *p7ecx; michael@0: SECStatus rv; michael@0: michael@0: p7ecx = sec_pkcs7_encoder_start_contexts (cinfo, bulkkey); michael@0: if (p7ecx == NULL) michael@0: return NULL; michael@0: michael@0: p7ecx->output.outputfn = outputfn; michael@0: p7ecx->output.outputarg = outputarg; michael@0: michael@0: /* michael@0: * Initialize the BER encoder. michael@0: */ michael@0: p7ecx->ecx = SEC_ASN1EncoderStart (cinfo, sec_PKCS7ContentInfoTemplate, michael@0: sec_pkcs7_encoder_out, &(p7ecx->output)); michael@0: if (p7ecx->ecx == NULL) { michael@0: PORT_Free (p7ecx); michael@0: return NULL; michael@0: } michael@0: michael@0: /* michael@0: * Indicate that we are streaming. We will be streaming until we michael@0: * get past the contents bytes. michael@0: */ michael@0: SEC_ASN1EncoderSetStreaming (p7ecx->ecx); michael@0: michael@0: /* michael@0: * The notify function will watch for the contents field. michael@0: */ michael@0: SEC_ASN1EncoderSetNotifyProc (p7ecx->ecx, sec_pkcs7_encoder_notify, p7ecx); michael@0: michael@0: /* michael@0: * This will encode everything up to the content bytes. (The notify michael@0: * function will then cause the encoding to stop there.) Then our michael@0: * caller can start passing contents bytes to our Update, which we michael@0: * will pass along. michael@0: */ michael@0: rv = SEC_ASN1EncoderUpdate (p7ecx->ecx, NULL, 0); michael@0: if (rv != SECSuccess) { michael@0: PORT_Free (p7ecx); michael@0: return NULL; michael@0: } michael@0: michael@0: return p7ecx; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * XXX If/when we support nested contents, this needs to be revised. michael@0: */ michael@0: static SECStatus michael@0: sec_pkcs7_encoder_work_data (SEC_PKCS7EncoderContext *p7ecx, SECItem *dest, michael@0: const unsigned char *data, unsigned long len, michael@0: PRBool final) michael@0: { michael@0: unsigned char *buf = NULL; michael@0: SECStatus rv; michael@0: michael@0: michael@0: rv = SECSuccess; /* may as well be optimistic */ michael@0: michael@0: /* michael@0: * We should really have data to process, or we should be trying michael@0: * to finish/flush the last block. (This is an overly paranoid michael@0: * check since all callers are in this file and simple inspection michael@0: * proves they do it right. But it could find a bug in future michael@0: * modifications/development, that is why it is here.) michael@0: */ michael@0: PORT_Assert ((data != NULL && len) || final); michael@0: michael@0: /* michael@0: * Update the running digest. michael@0: * XXX This needs modification if/when we handle multiple digests. michael@0: */ michael@0: if (len && p7ecx->digestobj != NULL) { michael@0: (* p7ecx->digestobj->update) (p7ecx->digestcx, data, len); michael@0: } michael@0: michael@0: /* michael@0: * Encrypt this chunk. michael@0: */ michael@0: if (p7ecx->encryptobj != NULL) { michael@0: /* XXX the following lengths should all be longs? */ michael@0: unsigned int inlen; /* length of data being encrypted */ michael@0: unsigned int outlen; /* length of encrypted data */ michael@0: unsigned int buflen; /* length available for encrypted data */ michael@0: michael@0: inlen = len; michael@0: buflen = sec_PKCS7EncryptLength (p7ecx->encryptobj, inlen, final); michael@0: if (buflen == 0) { michael@0: /* michael@0: * No output is expected, but the input data may be buffered michael@0: * so we still have to call Encrypt. michael@0: */ michael@0: rv = sec_PKCS7Encrypt (p7ecx->encryptobj, NULL, NULL, 0, michael@0: data, inlen, final); michael@0: if (final) { michael@0: len = 0; michael@0: goto done; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: if (dest != NULL) michael@0: buf = (unsigned char*)PORT_ArenaAlloc(p7ecx->cinfo->poolp, buflen); michael@0: else michael@0: buf = (unsigned char*)PORT_Alloc (buflen); michael@0: michael@0: if (buf == NULL) { michael@0: rv = SECFailure; michael@0: } else { michael@0: rv = sec_PKCS7Encrypt (p7ecx->encryptobj, buf, &outlen, buflen, michael@0: data, inlen, final); michael@0: data = buf; michael@0: len = outlen; michael@0: } michael@0: if (rv != SECSuccess) { michael@0: if (final) michael@0: goto done; michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: if (p7ecx->ecx != NULL) { michael@0: /* michael@0: * Encode the contents bytes. michael@0: */ michael@0: if(len) { michael@0: rv = SEC_ASN1EncoderUpdate (p7ecx->ecx, (const char *)data, len); michael@0: } michael@0: } michael@0: michael@0: done: michael@0: if (p7ecx->encryptobj != NULL) { michael@0: if (final) michael@0: sec_PKCS7DestroyEncryptObject (p7ecx->encryptobj); michael@0: if (dest != NULL) { michael@0: dest->data = buf; michael@0: dest->len = len; michael@0: } else if (buf != NULL) { michael@0: PORT_Free (buf); michael@0: } michael@0: } michael@0: michael@0: if (final && p7ecx->digestobj != NULL) { michael@0: SECItem *digest, **digests, ***digestsp; michael@0: unsigned char *digdata; michael@0: SECOidTag kind; michael@0: michael@0: kind = SEC_PKCS7ContentType (p7ecx->cinfo); michael@0: switch (kind) { michael@0: default: michael@0: PORT_Assert (0); michael@0: return SECFailure; michael@0: case SEC_OID_PKCS7_DIGESTED_DATA: michael@0: digest = &(p7ecx->cinfo->content.digestedData->digest); michael@0: digestsp = NULL; michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_DATA: michael@0: digest = NULL; michael@0: digestsp = &(p7ecx->cinfo->content.signedData->digests); michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: digest = NULL; michael@0: digestsp = &(p7ecx->cinfo->content.signedAndEnvelopedData->digests); michael@0: break; michael@0: } michael@0: michael@0: digdata = (unsigned char*)PORT_ArenaAlloc (p7ecx->cinfo->poolp, michael@0: p7ecx->digestobj->length); michael@0: if (digdata == NULL) michael@0: return SECFailure; michael@0: michael@0: if (digestsp != NULL) { michael@0: PORT_Assert (digest == NULL); michael@0: michael@0: digest = (SECItem*)PORT_ArenaAlloc (p7ecx->cinfo->poolp, michael@0: sizeof(SECItem)); michael@0: digests = (SECItem**)PORT_ArenaAlloc (p7ecx->cinfo->poolp, michael@0: 2 * sizeof(SECItem *)); michael@0: if (digests == NULL || digest == NULL) michael@0: return SECFailure; michael@0: michael@0: digests[0] = digest; michael@0: digests[1] = NULL; michael@0: michael@0: *digestsp = digests; michael@0: } michael@0: michael@0: PORT_Assert (digest != NULL); michael@0: michael@0: digest->data = digdata; michael@0: digest->len = p7ecx->digestobj->length; michael@0: michael@0: (* p7ecx->digestobj->end) (p7ecx->digestcx, digest->data, michael@0: &(digest->len), digest->len); michael@0: (* p7ecx->digestobj->destroy) (p7ecx->digestcx, PR_TRUE); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: SECStatus michael@0: SEC_PKCS7EncoderUpdate (SEC_PKCS7EncoderContext *p7ecx, michael@0: const char *data, unsigned long len) michael@0: { michael@0: /* XXX Error handling needs help. Return what? Do "Finish" on failure? */ michael@0: return sec_pkcs7_encoder_work_data (p7ecx, NULL, michael@0: (const unsigned char *)data, len, michael@0: PR_FALSE); michael@0: } michael@0: michael@0: static SECStatus michael@0: sec_pkcs7_encoder_sig_and_certs (SEC_PKCS7ContentInfo *cinfo, michael@0: SECKEYGetPasswordKey pwfn, void *pwfnarg) michael@0: { michael@0: SECOidTag kind; michael@0: CERTCertificate **certs; michael@0: CERTCertificateList **certlists; michael@0: SECAlgorithmID **digestalgs; michael@0: SECItem **digests; michael@0: SEC_PKCS7SignerInfo *signerinfo, **signerinfos; michael@0: SECItem **rawcerts, ***rawcertsp; michael@0: PLArenaPool *poolp; michael@0: int certcount; michael@0: int ci, cli, rci, si; michael@0: michael@0: kind = SEC_PKCS7ContentType (cinfo); michael@0: switch (kind) { michael@0: default: michael@0: case SEC_OID_PKCS7_DATA: michael@0: case SEC_OID_PKCS7_DIGESTED_DATA: michael@0: case SEC_OID_PKCS7_ENCRYPTED_DATA: michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: certs = NULL; michael@0: certlists = NULL; michael@0: digestalgs = NULL; michael@0: digests = NULL; michael@0: signerinfos = NULL; michael@0: rawcertsp = NULL; michael@0: break; 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: certs = sdp->certs; michael@0: certlists = sdp->certLists; michael@0: digestalgs = sdp->digestAlgorithms; michael@0: digests = sdp->digests; michael@0: signerinfos = sdp->signerInfos; michael@0: rawcertsp = &(sdp->rawCerts); 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: certs = saedp->certs; michael@0: certlists = saedp->certLists; michael@0: digestalgs = saedp->digestAlgorithms; michael@0: digests = saedp->digests; michael@0: signerinfos = saedp->signerInfos; michael@0: rawcertsp = &(saedp->rawCerts); michael@0: } michael@0: break; michael@0: } michael@0: michael@0: if (certs == NULL && certlists == NULL && signerinfos == NULL) michael@0: return SECSuccess; /* nothing for us to do! */ michael@0: michael@0: poolp = cinfo->poolp; michael@0: certcount = 0; michael@0: michael@0: if (signerinfos != NULL) { michael@0: SECOidTag digestalgtag; michael@0: int di; michael@0: SECStatus rv; michael@0: CERTCertificate *cert; michael@0: SECKEYPrivateKey *privkey; michael@0: SECItem signature; michael@0: SECOidTag signalgtag; michael@0: michael@0: PORT_Assert (digestalgs != NULL && digests != NULL); michael@0: michael@0: /* michael@0: * If one fails, we bail right then. If we want to continue and michael@0: * try to do subsequent signatures, this loop, and the departures michael@0: * from it, will need to be reworked. michael@0: */ michael@0: for (si = 0; signerinfos[si] != NULL; si++) { michael@0: michael@0: signerinfo = signerinfos[si]; michael@0: michael@0: /* find right digest */ michael@0: digestalgtag = SECOID_GetAlgorithmTag (&(signerinfo->digestAlg)); michael@0: for (di = 0; digestalgs[di] != NULL; di++) { michael@0: /* XXX Should I be comparing more than the tag? */ michael@0: if (digestalgtag == SECOID_GetAlgorithmTag (digestalgs[di])) michael@0: break; michael@0: } michael@0: if (digestalgs[di] == NULL) { michael@0: /* XXX oops; do what? set an error? */ michael@0: return SECFailure; michael@0: } michael@0: PORT_Assert (digests[di] != NULL); michael@0: michael@0: cert = signerinfo->cert; michael@0: privkey = PK11_FindKeyByAnyCert (cert, pwfnarg); michael@0: if (privkey == NULL) michael@0: return SECFailure; michael@0: michael@0: /* michael@0: * XXX I think there should be a cert-level interface for this, michael@0: * so that I do not have to know about subjectPublicKeyInfo... michael@0: */ michael@0: signalgtag = SECOID_GetAlgorithmTag (&(cert->subjectPublicKeyInfo.algorithm)); michael@0: michael@0: if (signerinfo->authAttr != NULL) { michael@0: SEC_PKCS7Attribute *attr; michael@0: SECItem encoded_attrs; michael@0: SECItem *dummy; michael@0: SECOidTag algid; michael@0: michael@0: /* michael@0: * First, find and fill in the message digest attribute. michael@0: */ michael@0: attr = sec_PKCS7FindAttribute (signerinfo->authAttr, michael@0: SEC_OID_PKCS9_MESSAGE_DIGEST, michael@0: PR_TRUE); michael@0: PORT_Assert (attr != NULL); michael@0: if (attr == NULL) { michael@0: SECKEY_DestroyPrivateKey (privkey); michael@0: return SECFailure; michael@0: } michael@0: michael@0: /* michael@0: * XXX The second half of the following assertion prevents michael@0: * the encoder from being called twice on the same content. michael@0: * Either just remove the second half the assertion, or michael@0: * change the code to check if the value already there is michael@0: * the same as digests[di], whichever seems more right. michael@0: */ michael@0: PORT_Assert (attr->values != NULL && attr->values[0] == NULL); michael@0: attr->values[0] = digests[di]; michael@0: michael@0: /* michael@0: * Before encoding, reorder the attributes so that when they michael@0: * are encoded, they will be conforming DER, which is required michael@0: * to have a specific order and that is what must be used for michael@0: * the hash/signature. We do this here, rather than building michael@0: * it into EncodeAttributes, because we do not want to do michael@0: * such reordering on incoming messages (which also uses michael@0: * EncodeAttributes) or our old signatures (and other "broken" michael@0: * implementations) will not verify. So, we want to guarantee michael@0: * that we send out good DER encodings of attributes, but not michael@0: * to expect to receive them. michael@0: */ michael@0: rv = sec_PKCS7ReorderAttributes (signerinfo->authAttr); michael@0: if (rv != SECSuccess) { michael@0: SECKEY_DestroyPrivateKey (privkey); michael@0: return SECFailure; michael@0: } michael@0: michael@0: encoded_attrs.data = NULL; michael@0: encoded_attrs.len = 0; michael@0: dummy = sec_PKCS7EncodeAttributes (NULL, &encoded_attrs, michael@0: &(signerinfo->authAttr)); michael@0: if (dummy == NULL) { michael@0: SECKEY_DestroyPrivateKey (privkey); michael@0: return SECFailure; michael@0: } michael@0: michael@0: algid = SEC_GetSignatureAlgorithmOidTag(privkey->keyType, michael@0: digestalgtag); michael@0: if (algid == SEC_OID_UNKNOWN) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); michael@0: SECKEY_DestroyPrivateKey (privkey); michael@0: return SECFailure; michael@0: } michael@0: rv = SEC_SignData (&signature, michael@0: encoded_attrs.data, encoded_attrs.len, michael@0: privkey, michael@0: algid); michael@0: SECITEM_FreeItem (&encoded_attrs, PR_FALSE); michael@0: } else { michael@0: rv = SGN_Digest (privkey, digestalgtag, &signature, michael@0: digests[di]); michael@0: } michael@0: michael@0: SECKEY_DestroyPrivateKey (privkey); michael@0: michael@0: if (rv != SECSuccess) michael@0: return rv; michael@0: michael@0: rv = SECITEM_CopyItem (poolp, &(signerinfo->encDigest), &signature); michael@0: if (rv != SECSuccess) michael@0: return rv; michael@0: michael@0: SECITEM_FreeItem (&signature, PR_FALSE); michael@0: michael@0: rv = SECOID_SetAlgorithmID (poolp, &(signerinfo->digestEncAlg), michael@0: signalgtag, NULL); michael@0: if (rv != SECSuccess) michael@0: return SECFailure; michael@0: michael@0: /* michael@0: * Count the cert chain for this signer. michael@0: */ michael@0: if (signerinfo->certList != NULL) michael@0: certcount += signerinfo->certList->len; michael@0: } michael@0: } michael@0: michael@0: if (certs != NULL) { michael@0: for (ci = 0; certs[ci] != NULL; ci++) michael@0: certcount++; michael@0: } michael@0: michael@0: if (certlists != NULL) { michael@0: for (cli = 0; certlists[cli] != NULL; cli++) michael@0: certcount += certlists[cli]->len; michael@0: } michael@0: michael@0: if (certcount == 0) michael@0: return SECSuccess; /* signing done; no certs */ michael@0: michael@0: /* michael@0: * Combine all of the certs and cert chains into rawcerts. michael@0: * Note: certcount is an upper bound; we may not need that many slots michael@0: * but we will allocate anyway to avoid having to do another pass. michael@0: * (The temporary space saving is not worth it.) michael@0: */ michael@0: rawcerts = (SECItem**)PORT_ArenaAlloc (poolp, michael@0: (certcount + 1) * sizeof(SECItem *)); michael@0: if (rawcerts == NULL) michael@0: return SECFailure; michael@0: michael@0: /* michael@0: * XXX Want to check for duplicates and not add *any* cert that is michael@0: * already in the set. This will be more important when we start michael@0: * dealing with larger sets of certs, dual-key certs (signing and michael@0: * encryption), etc. For the time being we can slide by... michael@0: */ michael@0: rci = 0; michael@0: if (signerinfos != NULL) { michael@0: for (si = 0; signerinfos[si] != NULL; si++) { michael@0: signerinfo = signerinfos[si]; michael@0: for (ci = 0; ci < signerinfo->certList->len; ci++) michael@0: rawcerts[rci++] = &(signerinfo->certList->certs[ci]); michael@0: } michael@0: michael@0: } michael@0: michael@0: if (certs != NULL) { michael@0: for (ci = 0; certs[ci] != NULL; ci++) michael@0: rawcerts[rci++] = &(certs[ci]->derCert); michael@0: } michael@0: michael@0: if (certlists != NULL) { michael@0: for (cli = 0; certlists[cli] != NULL; cli++) { michael@0: for (ci = 0; ci < certlists[cli]->len; ci++) michael@0: rawcerts[rci++] = &(certlists[cli]->certs[ci]); michael@0: } michael@0: } michael@0: michael@0: rawcerts[rci] = NULL; michael@0: *rawcertsp = rawcerts; michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: SECStatus michael@0: SEC_PKCS7EncoderFinish (SEC_PKCS7EncoderContext *p7ecx, michael@0: SECKEYGetPasswordKey pwfn, void *pwfnarg) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: /* michael@0: * Flush out any remaining data. michael@0: */ michael@0: rv = sec_pkcs7_encoder_work_data (p7ecx, NULL, NULL, 0, PR_TRUE); michael@0: michael@0: /* michael@0: * Turn off streaming stuff. michael@0: */ michael@0: SEC_ASN1EncoderClearTakeFromBuf (p7ecx->ecx); michael@0: SEC_ASN1EncoderClearStreaming (p7ecx->ecx); michael@0: michael@0: if (rv != SECSuccess) michael@0: goto loser; michael@0: michael@0: rv = sec_pkcs7_encoder_sig_and_certs (p7ecx->cinfo, pwfn, pwfnarg); michael@0: if (rv != SECSuccess) michael@0: goto loser; michael@0: michael@0: rv = SEC_ASN1EncoderUpdate (p7ecx->ecx, NULL, 0); michael@0: michael@0: loser: michael@0: SEC_ASN1EncoderFinish (p7ecx->ecx); michael@0: PORT_Free (p7ecx); michael@0: return rv; michael@0: } michael@0: michael@0: /* michael@0: * Abort the ASN.1 stream. Used by pkcs 12 michael@0: */ michael@0: void michael@0: SEC_PKCS7EncoderAbort(SEC_PKCS7EncoderContext *p7ecx, int error) michael@0: { michael@0: PORT_Assert(p7ecx); michael@0: SEC_ASN1EncoderAbort(p7ecx->ecx, error); michael@0: } michael@0: michael@0: /* michael@0: * After this routine is called, the entire PKCS7 contentInfo is ready michael@0: * to be encoded. This is used internally, but can also be called from michael@0: * elsewhere for those who want to be able to just have pointers to michael@0: * the ASN1 template for pkcs7 contentInfo built into their own encodings. michael@0: */ michael@0: SECStatus michael@0: SEC_PKCS7PrepareForEncode (SEC_PKCS7ContentInfo *cinfo, michael@0: PK11SymKey *bulkkey, michael@0: SECKEYGetPasswordKey pwfn, michael@0: void *pwfnarg) michael@0: { michael@0: SEC_PKCS7EncoderContext *p7ecx; michael@0: SECItem *content, *enc_content; michael@0: SECStatus rv; michael@0: michael@0: p7ecx = sec_pkcs7_encoder_start_contexts (cinfo, bulkkey); michael@0: if (p7ecx == NULL) michael@0: return SECFailure; michael@0: michael@0: content = SEC_PKCS7GetContent (cinfo); michael@0: michael@0: if (p7ecx->encryptobj != NULL) { michael@0: SECOidTag kind; michael@0: SEC_PKCS7EncryptedContentInfo *enccinfo; michael@0: michael@0: kind = SEC_PKCS7ContentType (p7ecx->cinfo); michael@0: switch (kind) { michael@0: default: michael@0: PORT_Assert (0); michael@0: rv = SECFailure; michael@0: goto loser; michael@0: case SEC_OID_PKCS7_ENCRYPTED_DATA: michael@0: enccinfo = &(p7ecx->cinfo->content.encryptedData->encContentInfo); michael@0: break; michael@0: case SEC_OID_PKCS7_ENVELOPED_DATA: michael@0: enccinfo = &(p7ecx->cinfo->content.envelopedData->encContentInfo); michael@0: break; michael@0: case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: michael@0: enccinfo = &(p7ecx->cinfo->content.signedAndEnvelopedData->encContentInfo); michael@0: break; michael@0: } michael@0: enc_content = &(enccinfo->encContent); michael@0: } else { michael@0: enc_content = NULL; michael@0: } michael@0: michael@0: if (content != NULL && content->data != NULL && content->len) { michael@0: rv = sec_pkcs7_encoder_work_data (p7ecx, enc_content, michael@0: content->data, content->len, PR_TRUE); michael@0: if (rv != SECSuccess) michael@0: goto loser; michael@0: } michael@0: michael@0: rv = sec_pkcs7_encoder_sig_and_certs (cinfo, pwfn, pwfnarg); michael@0: michael@0: loser: michael@0: PORT_Free (p7ecx); michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Encode a PKCS7 object, in one shot. All necessary components michael@0: * of the object must already be specified. Either the data has michael@0: * already been included (via SetContent), or the data is detached, michael@0: * or there is no data at all (certs-only). michael@0: * michael@0: * "cinfo" specifies the object to be encoded. michael@0: * michael@0: * "outputfn" is where the encoded bytes will be passed. michael@0: * michael@0: * "outputarg" is an opaque argument to the above callback. michael@0: * michael@0: * "bulkkey" specifies the bulk encryption key to use. This argument michael@0: * can be NULL if no encryption is being done, or if the bulk key should michael@0: * be generated internally (usually the case for EnvelopedData but never michael@0: * for EncryptedData, which *must* provide a bulk encryption key). michael@0: * michael@0: * "pwfn" is a callback for getting the password which protects the michael@0: * private key of the signer. This argument can be NULL if it is known michael@0: * that no signing is going to be done. michael@0: * michael@0: * "pwfnarg" is an opaque argument to the above callback. michael@0: */ michael@0: SECStatus michael@0: SEC_PKCS7Encode (SEC_PKCS7ContentInfo *cinfo, michael@0: SEC_PKCS7EncoderOutputCallback outputfn, michael@0: void *outputarg, michael@0: PK11SymKey *bulkkey, michael@0: SECKEYGetPasswordKey pwfn, michael@0: void *pwfnarg) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: rv = SEC_PKCS7PrepareForEncode (cinfo, bulkkey, pwfn, pwfnarg); michael@0: if (rv == SECSuccess) { michael@0: struct sec_pkcs7_encoder_output outputcx; michael@0: michael@0: outputcx.outputfn = outputfn; michael@0: outputcx.outputarg = outputarg; michael@0: michael@0: rv = SEC_ASN1Encode (cinfo, sec_PKCS7ContentInfoTemplate, michael@0: sec_pkcs7_encoder_out, &outputcx); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Encode a PKCS7 object, in one shot. All necessary components michael@0: * of the object must already be specified. Either the data has michael@0: * already been included (via SetContent), or the data is detached, michael@0: * or there is no data at all (certs-only). The output, rather than michael@0: * being passed to an output function as is done above, is all put michael@0: * into a SECItem. michael@0: * michael@0: * "pool" specifies a pool from which to allocate the result. michael@0: * It can be NULL, in which case memory is allocated generically. michael@0: * michael@0: * "dest" specifies a SECItem in which to put the result data. michael@0: * It can be NULL, in which case the entire item is allocated, too. michael@0: * michael@0: * "cinfo" specifies the object to be encoded. michael@0: * michael@0: * "bulkkey" specifies the bulk encryption key to use. This argument michael@0: * can be NULL if no encryption is being done, or if the bulk key should michael@0: * be generated internally (usually the case for EnvelopedData but never michael@0: * for EncryptedData, which *must* provide a bulk encryption key). michael@0: * michael@0: * "pwfn" is a callback for getting the password which protects the michael@0: * private key of the signer. This argument can be NULL if it is known michael@0: * that no signing is going to be done. michael@0: * michael@0: * "pwfnarg" is an opaque argument to the above callback. michael@0: */ michael@0: SECItem * michael@0: SEC_PKCS7EncodeItem (PLArenaPool *pool, michael@0: SECItem *dest, michael@0: SEC_PKCS7ContentInfo *cinfo, michael@0: PK11SymKey *bulkkey, michael@0: SECKEYGetPasswordKey pwfn, michael@0: void *pwfnarg) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: rv = SEC_PKCS7PrepareForEncode (cinfo, bulkkey, pwfn, pwfnarg); michael@0: if (rv != SECSuccess) michael@0: return NULL; michael@0: michael@0: return SEC_ASN1EncodeItem (pool, dest, cinfo, sec_PKCS7ContentInfoTemplate); michael@0: } michael@0: