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 encryptedData methods.
38 #include <Security/SecCmsEncryptedData.h>
40 #include <Security/SecCmsContentInfo.h>
46 #include <security_asn1/secasn1.h>
47 #include <security_asn1/secerr.h>
50 * SecCmsEncryptedDataCreate - create an empty encryptedData object.
52 * "algorithm" specifies the bulk encryption algorithm to use.
53 * "keysize" is the key size.
55 * An error results in a return value of NULL and an error set.
56 * (Retrieve specific errors via PORT_GetError()/XP_GetError().)
58 SecCmsEncryptedDataRef
59 SecCmsEncryptedDataCreate(SecCmsMessageRef cmsg
, SECOidTag algorithm
, int keysize
)
62 SecCmsEncryptedDataRef encd
;
64 SECAlgorithmID
*pbe_algid
;
69 mark
= PORT_ArenaMark(poolp
);
71 encd
= (SecCmsEncryptedDataRef
)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsEncryptedData
));
77 /* version is set in SecCmsEncryptedDataEncodeBeforeStart() */
80 /* XXX hmmm... hardcoded algorithms? */
82 case SEC_OID_DES_EDE3_CBC
:
84 rv
= SecCmsContentInfoSetContentEncAlg((SecArenaPoolRef
)poolp
, &(encd
->contentInfo
), algorithm
, NULL
, keysize
);
87 /* Assume password-based-encryption. At least, try that. */
92 pbe_algid
= PK11_CreatePBEAlgorithmID(algorithm
, 1, NULL
);
94 if (pbe_algid
== NULL
) {
98 rv
= SecCmsContentInfoSetContentEncAlgID((SecArenaPoolRef
)poolp
, &(encd
->contentInfo
), pbe_algid
, keysize
);
99 SECOID_DestroyAlgorithmID (pbe_algid
, PR_TRUE
);
102 if (rv
!= SECSuccess
)
105 PORT_ArenaUnmark(poolp
, mark
);
109 PORT_ArenaRelease(poolp
, mark
);
114 * SecCmsEncryptedDataDestroy - destroy an encryptedData object
117 SecCmsEncryptedDataDestroy(SecCmsEncryptedDataRef encd
)
119 /* everything's in a pool, so don't worry about the storage */
120 SecCmsContentInfoDestroy(&(encd
->contentInfo
));
125 * SecCmsEncryptedDataGetContentInfo - return pointer to encryptedData object's contentInfo
128 SecCmsEncryptedDataGetContentInfo(SecCmsEncryptedDataRef encd
)
130 return &(encd
->contentInfo
);
134 * SecCmsEncryptedDataEncodeBeforeStart - do all the necessary things to a EncryptedData
135 * before encoding begins.
138 * - set the correct version value.
139 * - get the encryption key
142 SecCmsEncryptedDataEncodeBeforeStart(SecCmsEncryptedDataRef encd
)
145 SecSymmetricKeyRef bulkkey
= NULL
;
147 SecCmsContentInfoRef cinfo
= &(encd
->contentInfo
);
149 if (SecCmsArrayIsEmpty((void **)encd
->unprotectedAttr
))
150 version
= SEC_CMS_ENCRYPTED_DATA_VERSION
;
152 version
= SEC_CMS_ENCRYPTED_DATA_VERSION_UPATTR
;
154 dummy
= SEC_ASN1EncodeInteger (encd
->cmsg
->poolp
, &(encd
->version
), version
);
158 /* now get content encryption key (bulk key) by using our cmsg callback */
159 if (encd
->cmsg
->decrypt_key_cb
)
160 bulkkey
= (*encd
->cmsg
->decrypt_key_cb
)(encd
->cmsg
->decrypt_key_cb_arg
,
161 SecCmsContentInfoGetContentEncAlg(cinfo
));
165 /* store the bulk key in the contentInfo so that the encoder can find it */
166 SecCmsContentInfoSetBulkKey(cinfo
, bulkkey
);
167 CFRelease(bulkkey
); /* This assumes the decrypt_key_cb hands us a copy of the key --mb */
173 * SecCmsEncryptedDataEncodeBeforeData - set up encryption
176 SecCmsEncryptedDataEncodeBeforeData(SecCmsEncryptedDataRef encd
)
178 SecCmsContentInfoRef cinfo
;
179 SecSymmetricKeyRef bulkkey
;
180 SECAlgorithmID
*algid
;
182 cinfo
= &(encd
->contentInfo
);
184 /* find bulkkey and algorithm - must have been set by SecCmsEncryptedDataEncodeBeforeStart */
185 bulkkey
= SecCmsContentInfoGetBulkKey(cinfo
);
188 algid
= SecCmsContentInfoGetContentEncAlg(cinfo
);
192 /* this may modify algid (with IVs generated in a token).
193 * it is therefore essential that algid is a pointer to the "real" contentEncAlg,
194 * not just to a copy */
195 cinfo
->ciphcx
= SecCmsCipherContextStartEncrypt(encd
->cmsg
->poolp
, bulkkey
, algid
);
197 if (cinfo
->ciphcx
== NULL
)
204 * SecCmsEncryptedDataEncodeAfterData - finalize this encryptedData for encoding
207 SecCmsEncryptedDataEncodeAfterData(SecCmsEncryptedDataRef encd
)
209 if (encd
->contentInfo
.ciphcx
) {
210 SecCmsCipherContextDestroy(encd
->contentInfo
.ciphcx
);
211 encd
->contentInfo
.ciphcx
= NULL
;
214 /* nothing to do after data */
220 * SecCmsEncryptedDataDecodeBeforeData - find bulk key & set up decryption
223 SecCmsEncryptedDataDecodeBeforeData(SecCmsEncryptedDataRef encd
)
225 SecSymmetricKeyRef bulkkey
= NULL
;
226 SecCmsContentInfoRef cinfo
;
227 SECAlgorithmID
*bulkalg
;
228 OSStatus rv
= SECFailure
;
230 cinfo
= &(encd
->contentInfo
);
232 bulkalg
= SecCmsContentInfoGetContentEncAlg(cinfo
);
234 if (encd
->cmsg
->decrypt_key_cb
== NULL
) /* no callback? no key../ */
237 bulkkey
= (*encd
->cmsg
->decrypt_key_cb
)(encd
->cmsg
->decrypt_key_cb_arg
, bulkalg
);
239 /* no success finding a bulk key */
242 SecCmsContentInfoSetBulkKey(cinfo
, bulkkey
);
244 cinfo
->ciphcx
= SecCmsCipherContextStartDecrypt(bulkkey
, bulkalg
);
245 if (cinfo
->ciphcx
== NULL
)
246 goto loser
; /* error has been set by SecCmsCipherContextStartDecrypt */
253 * For PKCS5 Encryption Algorithms, the bulkkey is actually a different
254 * structure. Therefore, we need to set the bulkkey to the actual key
255 * prior to freeing it.
257 if (SEC_PKCS5IsAlgorithmPBEAlg(bulkalg
)) {
258 SEC_PKCS5KeyAndPassword
*keyPwd
= (SEC_PKCS5KeyAndPassword
*)bulkkey
;
259 bulkkey
= keyPwd
->key
;
263 /* we are done with (this) bulkkey now. */
273 * SecCmsEncryptedDataDecodeAfterData - finish decrypting this encryptedData's content
276 SecCmsEncryptedDataDecodeAfterData(SecCmsEncryptedDataRef encd
)
278 if (encd
->contentInfo
.ciphcx
) {
279 SecCmsCipherContextDestroy(encd
->contentInfo
.ciphcx
);
280 encd
->contentInfo
.ciphcx
= NULL
;
287 * SecCmsEncryptedDataDecodeAfterEnd - finish decoding this encryptedData
290 SecCmsEncryptedDataDecodeAfterEnd(SecCmsEncryptedDataRef encd
)
292 /* apply final touches */