]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_smime/lib/cmsencdata.c
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / libsecurity_smime / lib / cmsencdata.c
1 /*
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/
6 *
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.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
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
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
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
31 * GPL.
32 */
33
34 /*
35 * CMS encryptedData methods.
36 */
37
38 #include <Security/SecCmsEncryptedData.h>
39
40 #include <Security/SecCmsContentInfo.h>
41
42 #include "cmslocal.h"
43
44 #include "secitem.h"
45 #include "secoid.h"
46 #include <security_asn1/secasn1.h>
47 #include <security_asn1/secerr.h>
48
49 /*
50 * SecCmsEncryptedDataCreate - create an empty encryptedData object.
51 *
52 * "algorithm" specifies the bulk encryption algorithm to use.
53 * "keysize" is the key size.
54 *
55 * An error results in a return value of NULL and an error set.
56 * (Retrieve specific errors via PORT_GetError()/XP_GetError().)
57 */
58 SecCmsEncryptedDataRef
59 SecCmsEncryptedDataCreate(SecCmsMessageRef cmsg, SECOidTag algorithm, int keysize)
60 {
61 void *mark;
62 SecCmsEncryptedDataRef encd;
63 PLArenaPool *poolp;
64 SECAlgorithmID *pbe_algid;
65 OSStatus rv;
66
67 poolp = cmsg->poolp;
68
69 mark = PORT_ArenaMark(poolp);
70
71 encd = (SecCmsEncryptedDataRef)PORT_ArenaZAlloc(poolp, sizeof(SecCmsEncryptedData));
72 if (encd == NULL)
73 goto loser;
74
75 encd->cmsg = cmsg;
76
77 /* version is set in SecCmsEncryptedDataEncodeBeforeStart() */
78
79 switch (algorithm) {
80 /* XXX hmmm... hardcoded algorithms? */
81 case SEC_OID_RC2_CBC:
82 case SEC_OID_DES_EDE3_CBC:
83 case SEC_OID_DES_CBC:
84 rv = SecCmsContentInfoSetContentEncAlg((SecArenaPoolRef)poolp, &(encd->contentInfo), algorithm, NULL, keysize);
85 break;
86 default:
87 /* Assume password-based-encryption. At least, try that. */
88 #if 1
89 // @@@ Fix me
90 pbe_algid = NULL;
91 #else
92 pbe_algid = PK11_CreatePBEAlgorithmID(algorithm, 1, NULL);
93 #endif
94 if (pbe_algid == NULL) {
95 rv = SECFailure;
96 break;
97 }
98 rv = SecCmsContentInfoSetContentEncAlgID((SecArenaPoolRef)poolp, &(encd->contentInfo), pbe_algid, keysize);
99 SECOID_DestroyAlgorithmID (pbe_algid, PR_TRUE);
100 break;
101 }
102 if (rv != SECSuccess)
103 goto loser;
104
105 PORT_ArenaUnmark(poolp, mark);
106 return encd;
107
108 loser:
109 PORT_ArenaRelease(poolp, mark);
110 return NULL;
111 }
112
113 /*
114 * SecCmsEncryptedDataDestroy - destroy an encryptedData object
115 */
116 void
117 SecCmsEncryptedDataDestroy(SecCmsEncryptedDataRef encd)
118 {
119 if (encd == NULL) {
120 return;
121 }
122 /* everything's in a pool, so don't worry about the storage */
123 SecCmsContentInfoDestroy(&(encd->contentInfo));
124 return;
125 }
126
127 /*
128 * SecCmsEncryptedDataGetContentInfo - return pointer to encryptedData object's contentInfo
129 */
130 SecCmsContentInfoRef
131 SecCmsEncryptedDataGetContentInfo(SecCmsEncryptedDataRef encd)
132 {
133 return &(encd->contentInfo);
134 }
135
136 /*
137 * SecCmsEncryptedDataEncodeBeforeStart - do all the necessary things to a EncryptedData
138 * before encoding begins.
139 *
140 * In particular:
141 * - set the correct version value.
142 * - get the encryption key
143 */
144 OSStatus
145 SecCmsEncryptedDataEncodeBeforeStart(SecCmsEncryptedDataRef encd)
146 {
147 int version;
148 SecSymmetricKeyRef bulkkey = NULL;
149 CSSM_DATA_PTR dummy;
150 SecCmsContentInfoRef cinfo = &(encd->contentInfo);
151
152 if (SecCmsArrayIsEmpty((void **)encd->unprotectedAttr))
153 version = SEC_CMS_ENCRYPTED_DATA_VERSION;
154 else
155 version = SEC_CMS_ENCRYPTED_DATA_VERSION_UPATTR;
156
157 dummy = SEC_ASN1EncodeInteger (encd->cmsg->poolp, &(encd->version), version);
158 if (dummy == NULL)
159 return SECFailure;
160
161 /* now get content encryption key (bulk key) by using our cmsg callback */
162 if (encd->cmsg->decrypt_key_cb)
163 bulkkey = (*encd->cmsg->decrypt_key_cb)(encd->cmsg->decrypt_key_cb_arg,
164 SecCmsContentInfoGetContentEncAlg(cinfo));
165 if (bulkkey == NULL)
166 return SECFailure;
167
168 /* store the bulk key in the contentInfo so that the encoder can find it */
169 SecCmsContentInfoSetBulkKey(cinfo, bulkkey);
170 CFRelease(bulkkey); /* This assumes the decrypt_key_cb hands us a copy of the key --mb */
171
172 return SECSuccess;
173 }
174
175 /*
176 * SecCmsEncryptedDataEncodeBeforeData - set up encryption
177 */
178 OSStatus
179 SecCmsEncryptedDataEncodeBeforeData(SecCmsEncryptedDataRef encd)
180 {
181 SecCmsContentInfoRef cinfo;
182 SecSymmetricKeyRef bulkkey;
183 SECAlgorithmID *algid;
184
185 cinfo = &(encd->contentInfo);
186
187 /* find bulkkey and algorithm - must have been set by SecCmsEncryptedDataEncodeBeforeStart */
188 bulkkey = SecCmsContentInfoGetBulkKey(cinfo);
189 if (bulkkey == NULL)
190 return SECFailure;
191 algid = SecCmsContentInfoGetContentEncAlg(cinfo);
192 if (algid == NULL)
193 return SECFailure;
194
195 /* this may modify algid (with IVs generated in a token).
196 * it is therefore essential that algid is a pointer to the "real" contentEncAlg,
197 * not just to a copy */
198 cinfo->ciphcx = SecCmsCipherContextStartEncrypt(encd->cmsg->poolp, bulkkey, algid);
199 CFRelease(bulkkey);
200 if (cinfo->ciphcx == NULL)
201 return SECFailure;
202
203 return SECSuccess;
204 }
205
206 /*
207 * SecCmsEncryptedDataEncodeAfterData - finalize this encryptedData for encoding
208 */
209 OSStatus
210 SecCmsEncryptedDataEncodeAfterData(SecCmsEncryptedDataRef encd)
211 {
212 if (encd->contentInfo.ciphcx) {
213 SecCmsCipherContextDestroy(encd->contentInfo.ciphcx);
214 encd->contentInfo.ciphcx = NULL;
215 }
216
217 /* nothing to do after data */
218 return SECSuccess;
219 }
220
221
222 /*
223 * SecCmsEncryptedDataDecodeBeforeData - find bulk key & set up decryption
224 */
225 OSStatus
226 SecCmsEncryptedDataDecodeBeforeData(SecCmsEncryptedDataRef encd)
227 {
228 SecSymmetricKeyRef bulkkey = NULL;
229 SecCmsContentInfoRef cinfo;
230 SECAlgorithmID *bulkalg;
231 OSStatus rv = SECFailure;
232
233 cinfo = &(encd->contentInfo);
234
235 bulkalg = SecCmsContentInfoGetContentEncAlg(cinfo);
236
237 if (encd->cmsg->decrypt_key_cb == NULL) /* no callback? no key../ */
238 goto loser;
239
240 bulkkey = (*encd->cmsg->decrypt_key_cb)(encd->cmsg->decrypt_key_cb_arg, bulkalg);
241 if (bulkkey == NULL)
242 /* no success finding a bulk key */
243 goto loser;
244
245 SecCmsContentInfoSetBulkKey(cinfo, bulkkey);
246
247 cinfo->ciphcx = SecCmsCipherContextStartDecrypt(bulkkey, bulkalg);
248 if (cinfo->ciphcx == NULL)
249 goto loser; /* error has been set by SecCmsCipherContextStartDecrypt */
250
251 #if 1
252 // @@@ Not done yet
253 #else
254 /*
255 * HACK ALERT!!
256 * For PKCS5 Encryption Algorithms, the bulkkey is actually a different
257 * structure. Therefore, we need to set the bulkkey to the actual key
258 * prior to freeing it.
259 */
260 if (SEC_PKCS5IsAlgorithmPBEAlg(bulkalg)) {
261 SEC_PKCS5KeyAndPassword *keyPwd = (SEC_PKCS5KeyAndPassword *)bulkkey;
262 bulkkey = keyPwd->key;
263 }
264 #endif
265
266 /* we are done with (this) bulkkey now. */
267 CFRelease(bulkkey);
268
269 rv = SECSuccess;
270
271 loser:
272 return rv;
273 }
274
275 /*
276 * SecCmsEncryptedDataDecodeAfterData - finish decrypting this encryptedData's content
277 */
278 OSStatus
279 SecCmsEncryptedDataDecodeAfterData(SecCmsEncryptedDataRef encd)
280 {
281 if (encd->contentInfo.ciphcx) {
282 SecCmsCipherContextDestroy(encd->contentInfo.ciphcx);
283 encd->contentInfo.ciphcx = NULL;
284 }
285
286 return SECSuccess;
287 }
288
289 /*
290 * SecCmsEncryptedDataDecodeAfterEnd - finish decoding this encryptedData
291 */
292 OSStatus
293 SecCmsEncryptedDataDecodeAfterEnd(SecCmsEncryptedDataRef encd)
294 {
295 /* apply final touches */
296 return SECSuccess;
297 }