2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
12 * The Original Code is the Netscape security libraries.
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation. All
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above. If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL. If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
35 * CMS envelopedData methods.
38 #include <Security/SecCmsEnvelopedData.h>
40 #include <Security/SecCmsContentInfo.h>
41 #include <Security/SecCmsRecipientInfo.h>
42 #include <Security/SecRandom.h>
46 #include "SecAsn1Item.h"
50 #include <security_asn1/secasn1.h>
51 #include <security_asn1/secerr.h>
52 #include <security_asn1/secport.h>
54 #include <Security/SecKeyPriv.h>
55 #include <CommonCrypto/CommonCryptor.h>
57 #include <AssertMacros.h>
60 * SecCmsEnvelopedDataCreate - create an enveloped data message
62 SecCmsEnvelopedDataRef
63 SecCmsEnvelopedDataCreate(SecCmsMessageRef cmsg
, SECOidTag algorithm
, int keysize
)
66 SecCmsEnvelopedDataRef envd
;
72 mark
= PORT_ArenaMark(poolp
);
74 envd
= (SecCmsEnvelopedDataRef
)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsEnvelopedData
));
78 envd
->contentInfo
.cmsg
= cmsg
;
80 /* version is set in SecCmsEnvelopedDataEncodeBeforeStart() */
82 rv
= SecCmsContentInfoSetContentEncAlg(&(envd
->contentInfo
), algorithm
, NULL
, keysize
);
86 PORT_ArenaUnmark(poolp
, mark
);
90 PORT_ArenaRelease(poolp
, mark
);
95 * SecCmsEnvelopedDataDestroy - destroy an enveloped data message
98 SecCmsEnvelopedDataDestroy(SecCmsEnvelopedDataRef edp
)
100 SecCmsRecipientInfoRef
*recipientinfos
;
101 SecCmsRecipientInfoRef ri
;
106 recipientinfos
= edp
->recipientInfos
;
107 if (recipientinfos
== NULL
)
110 while ((ri
= *recipientinfos
++) != NULL
)
111 SecCmsRecipientInfoDestroy(ri
);
113 SecCmsContentInfoDestroy(&(edp
->contentInfo
));
118 * SecCmsEnvelopedDataGetContentInfo - return pointer to this envelopedData's contentinfo
121 SecCmsEnvelopedDataGetContentInfo(SecCmsEnvelopedDataRef envd
)
123 return &(envd
->contentInfo
);
127 * SecCmsEnvelopedDataAddRecipient - add a recipientinfo to the enveloped data msg
129 * rip must be created on the same pool as edp - this is not enforced, though.
132 SecCmsEnvelopedDataAddRecipient(SecCmsEnvelopedDataRef edp
, SecCmsRecipientInfoRef rip
)
137 /* XXX compare pools, if not same, copy rip into edp's pool */
139 PR_ASSERT(edp
!= NULL
);
140 PR_ASSERT(rip
!= NULL
);
142 mark
= PORT_ArenaMark(edp
->contentInfo
.cmsg
->poolp
);
144 rv
= SecCmsArrayAdd(edp
->contentInfo
.cmsg
->poolp
, (void ***)&(edp
->recipientInfos
), (void *)rip
);
145 if (rv
!= SECSuccess
) {
146 PORT_ArenaRelease(edp
->contentInfo
.cmsg
->poolp
, mark
);
150 PORT_ArenaUnmark (edp
->contentInfo
.cmsg
->poolp
, mark
);
155 * SecCmsEnvelopedDataEncodeBeforeStart - prepare this envelopedData for encoding
157 * at this point, we need
158 * - recipientinfos set up with recipient's certificates
159 * - a content encryption algorithm (if none, 3DES will be used)
161 * this function will generate a random content encryption key (aka bulk key),
162 * initialize the recipientinfos with certificate identification and wrap the bulk key
163 * using the proper algorithm for every certificiate.
164 * it will finally set the bulk algorithm and key so that the encode step can find it.
167 SecCmsEnvelopedDataEncodeBeforeStart(SecCmsEnvelopedDataRef envd
)
170 SecCmsRecipientInfoRef
*recipientinfos
;
171 SecCmsContentInfoRef cinfo
;
172 SecSymmetricKeyRef bulkkey
= NULL
;
173 SECOidTag bulkalgtag
;
174 //CK_MECHANISM_TYPE type;
175 //PK11SlotInfo *slot;
179 extern const SecAsn1Template SecCmsRecipientInfoTemplate
[];
183 cinfo
= &(envd
->contentInfo
);
184 poolp
= cinfo
->cmsg
->poolp
;
186 recipientinfos
= envd
->recipientInfos
;
187 if (recipientinfos
== NULL
) {
188 PORT_SetError(SEC_ERROR_BAD_DATA
);
192 version
= SEC_CMS_ENVELOPED_DATA_VERSION_REG
;
193 if (envd
->originatorInfo
!= NULL
|| envd
->unprotectedAttr
!= NULL
) {
194 version
= SEC_CMS_ENVELOPED_DATA_VERSION_ADV
;
196 for (i
= 0; recipientinfos
[i
] != NULL
; i
++) {
197 if (SecCmsRecipientInfoGetVersion(recipientinfos
[i
]) != 0) {
198 version
= SEC_CMS_ENVELOPED_DATA_VERSION_ADV
;
203 dummy
= SEC_ASN1EncodeInteger(poolp
, &(envd
->version
), version
);
207 /* now we need to have a proper content encryption algorithm
208 * on the SMIME level, we would figure one out by looking at SMIME capabilities
209 * we cannot do that on our level, so if none is set already, we'll just go
210 * with one of the mandatory algorithms (3DES) */
211 if ((bulkalgtag
= SecCmsContentInfoGetContentEncAlgTag(cinfo
)) == SEC_OID_UNKNOWN
) {
212 rv
= SecCmsContentInfoSetContentEncAlg(cinfo
, SEC_OID_DES_EDE3_CBC
, NULL
, 192);
213 if (rv
!= SECSuccess
)
215 bulkalgtag
= SEC_OID_DES_EDE3_CBC
;
219 size_t keysize
= (cinfo
->keysize
+ 7)/8;
220 uint8_t key_material
[keysize
];
221 require_noerr(SecRandomCopyBytes(kSecRandomDefault
, keysize
, key_material
), loser
);
222 bulkkey
= (SecSymmetricKeyRef
)CFDataCreate(kCFAllocatorDefault
, key_material
, keysize
);
225 mark
= PORT_ArenaMark(poolp
);
227 /* Encrypt the bulk key with the public key of each recipient. */
228 for (i
= 0; recipientinfos
[i
] != NULL
; i
++) {
229 rv
= SecCmsRecipientInfoWrapBulkKey(recipientinfos
[i
], bulkkey
, bulkalgtag
);
230 if (rv
!= SECSuccess
)
231 goto loser
; /* error has been set by SecCmsRecipientInfoEncryptBulkKey */
232 /* could be: alg not supported etc. */
235 /* the recipientinfos are all finished. now sort them by DER for SET OF encoding */
236 rv
= SecCmsArraySortByDER((void **)envd
->recipientInfos
, SecCmsRecipientInfoTemplate
, NULL
);
237 if (rv
!= SECSuccess
)
238 goto loser
; /* error has been set by SecCmsArraySortByDER */
240 /* store the bulk key in the contentInfo so that the encoder can find it */
241 SecCmsContentInfoSetBulkKey(cinfo
, bulkkey
);
243 PORT_ArenaUnmark(poolp
, mark
);
251 PORT_ArenaRelease (poolp
, mark
);
259 * SecCmsEnvelopedDataEncodeBeforeData - set up encryption
261 * it is essential that this is called before the contentEncAlg is encoded, because
262 * setting up the encryption may generate IVs and thus change it!
265 SecCmsEnvelopedDataEncodeBeforeData(SecCmsEnvelopedDataRef envd
)
267 SecCmsContentInfoRef cinfo
;
268 SecSymmetricKeyRef bulkkey
;
269 SECAlgorithmID
*algid
;
271 cinfo
= &(envd
->contentInfo
);
273 /* find bulkkey and algorithm - must have been set by SecCmsEnvelopedDataEncodeBeforeStart */
274 bulkkey
= SecCmsContentInfoGetBulkKey(cinfo
);
277 algid
= SecCmsContentInfoGetContentEncAlg(cinfo
);
281 /* this may modify algid (with IVs generated in a token).
282 * it is essential that algid is a pointer to the contentEncAlg data, not a
283 * pointer to a copy! */
284 cinfo
->ciphcx
= SecCmsCipherContextStartEncrypt(cinfo
->cmsg
->poolp
, bulkkey
, algid
);
286 if (cinfo
->ciphcx
== NULL
)
293 * SecCmsEnvelopedDataEncodeAfterData - finalize this envelopedData for encoding
296 SecCmsEnvelopedDataEncodeAfterData(SecCmsEnvelopedDataRef envd
)
298 if (envd
->contentInfo
.ciphcx
) {
299 SecCmsCipherContextDestroy(envd
->contentInfo
.ciphcx
);
300 envd
->contentInfo
.ciphcx
= NULL
;
303 /* nothing else to do after data */
308 * SecCmsEnvelopedDataDecodeBeforeData - find our recipientinfo,
309 * derive bulk key & set up our contentinfo
312 SecCmsEnvelopedDataDecodeBeforeData(SecCmsEnvelopedDataRef envd
)
314 SecCmsRecipientInfoRef ri
;
315 SecSymmetricKeyRef bulkkey
= NULL
;
316 SECOidTag bulkalgtag
;
317 SECAlgorithmID
*bulkalg
;
318 OSStatus rv
= SECFailure
;
319 SecCmsContentInfoRef cinfo
;
320 SecCmsRecipient
**recipient_list
= NULL
;
321 SecCmsRecipient
*recipient
;
324 if (SecCmsArrayCount((void **)envd
->recipientInfos
) == 0) {
325 PORT_SetError(SEC_ERROR_BAD_DATA
);
327 PORT_SetErrorString("No recipient data in envelope.");
332 /* look if one of OUR cert's issuerSN is on the list of recipients, and if so, */
333 /* get the cert and private key for it right away */
334 recipient_list
= nss_cms_recipient_list_create(envd
->recipientInfos
);
335 if (recipient_list
== NULL
)
338 cinfo
= &(envd
->contentInfo
);
339 /* what about multiple recipientInfos that match?
340 * especially if, for some reason, we could not produce a bulk key with the first match?!
341 * we could loop & feed partial recipient_list to PK11_FindCertAndKeyByRecipientList...
343 rlIndex
= nss_cms_FindCertAndKeyByRecipientList(recipient_list
, cinfo
->cmsg
->pwfn_arg
);
345 /* if that fails, then we're not an intended recipient and cannot decrypt */
347 PORT_SetError(SEC_ERROR_NOT_A_RECIPIENT
);
349 PORT_SetErrorString("Cannot decrypt data because proper key cannot be found.");
354 recipient
= recipient_list
[rlIndex
];
355 if (!recipient
->cert
|| !recipient
->privkey
) {
356 /* XXX should set an error code ?!? */
359 /* get a pointer to "our" recipientinfo */
360 ri
= envd
->recipientInfos
[recipient
->riIndex
];
362 bulkalgtag
= SecCmsContentInfoGetContentEncAlgTag(cinfo
);
363 if (bulkalgtag
== SEC_OID_UNKNOWN
) {
364 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
366 bulkkey
= SecCmsRecipientInfoUnwrapBulkKey(ri
,recipient
->subIndex
,
370 if (bulkkey
== NULL
) {
371 /* no success finding a bulk key */
375 SecCmsContentInfoSetBulkKey(cinfo
, bulkkey
);
376 // @@@ See 3401088 for details. We need to CFRelease cinfo->bulkkey before recipient->privkey gets CFReleased. It's created with SecKeyCreate which is not safe currently. If the private key's SecKeyRef from which we extracted the CSP gets CFRelease before the builkkey does we crash. We should really fix SecKeyCreate which is a huge hack currently. To work around this we add recipient->privkey to the cinfo so it gets when cinfo is destroyed.
377 CFRetain(recipient
->privkey
);
378 cinfo
->privkey
= recipient
->privkey
;
380 bulkalg
= SecCmsContentInfoGetContentEncAlg(cinfo
);
382 cinfo
->ciphcx
= SecCmsCipherContextStartDecrypt(bulkkey
, bulkalg
);
383 if (cinfo
->ciphcx
== NULL
)
384 goto loser
; /* error has been set by SecCmsCipherContextStartDecrypt */
391 * For PKCS5 Encryption Algorithms, the bulkkey is actually a different
392 * structure. Therefore, we need to set the bulkkey to the actual key
393 * prior to freeing it.
395 if (SEC_PKCS5IsAlgorithmPBEAlg(bulkalg
)) {
396 SEC_PKCS5KeyAndPassword
*keyPwd
= (SEC_PKCS5KeyAndPassword
*)bulkkey
;
397 bulkkey
= keyPwd
->key
;
406 if (recipient_list
!= NULL
)
407 nss_cms_recipient_list_destroy(recipient_list
);
412 * SecCmsEnvelopedDataDecodeAfterData - finish decrypting this envelopedData's content
415 SecCmsEnvelopedDataDecodeAfterData(SecCmsEnvelopedDataRef envd
)
417 if (envd
&& envd
->contentInfo
.ciphcx
) {
418 SecCmsCipherContextDestroy(envd
->contentInfo
.ciphcx
);
419 envd
->contentInfo
.ciphcx
= NULL
;
426 * SecCmsEnvelopedDataDecodeAfterEnd - finish decoding this envelopedData
429 SecCmsEnvelopedDataDecodeAfterEnd(SecCmsEnvelopedDataRef envd
)
431 /* apply final touches */