/src/openssl/crypto/cms/cms_kari.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* crypto/cms/cms_kari.c */ |
2 | | /* |
3 | | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
4 | | * project. |
5 | | */ |
6 | | /* ==================================================================== |
7 | | * Copyright (c) 2013 The OpenSSL Project. All rights reserved. |
8 | | * |
9 | | * Redistribution and use in source and binary forms, with or without |
10 | | * modification, are permitted provided that the following conditions |
11 | | * are met: |
12 | | * |
13 | | * 1. Redistributions of source code must retain the above copyright |
14 | | * notice, this list of conditions and the following disclaimer. |
15 | | * |
16 | | * 2. Redistributions in binary form must reproduce the above copyright |
17 | | * notice, this list of conditions and the following disclaimer in |
18 | | * the documentation and/or other materials provided with the |
19 | | * distribution. |
20 | | * |
21 | | * 3. All advertising materials mentioning features or use of this |
22 | | * software must display the following acknowledgment: |
23 | | * "This product includes software developed by the OpenSSL Project |
24 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
25 | | * |
26 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
27 | | * endorse or promote products derived from this software without |
28 | | * prior written permission. For written permission, please contact |
29 | | * licensing@OpenSSL.org. |
30 | | * |
31 | | * 5. Products derived from this software may not be called "OpenSSL" |
32 | | * nor may "OpenSSL" appear in their names without prior written |
33 | | * permission of the OpenSSL Project. |
34 | | * |
35 | | * 6. Redistributions of any form whatsoever must retain the following |
36 | | * acknowledgment: |
37 | | * "This product includes software developed by the OpenSSL Project |
38 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
41 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
43 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
44 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
45 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
46 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
47 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
49 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
50 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
51 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
52 | | * ==================================================================== |
53 | | */ |
54 | | |
55 | | #include "cryptlib.h" |
56 | | #include <openssl/asn1t.h> |
57 | | #include <openssl/pem.h> |
58 | | #include <openssl/x509v3.h> |
59 | | #include <openssl/err.h> |
60 | | #include <openssl/cms.h> |
61 | | #include <openssl/rand.h> |
62 | | #include <openssl/aes.h> |
63 | | #include "cms_lcl.h" |
64 | | #include "asn1_locl.h" |
65 | | |
66 | | DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo) |
67 | | DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey) |
68 | | DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey) |
69 | | DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier) |
70 | | |
71 | | /* Key Agreement Recipient Info (KARI) routines */ |
72 | | |
73 | | int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri, |
74 | | X509_ALGOR **palg, |
75 | | ASN1_OCTET_STRING **pukm) |
76 | 0 | { |
77 | 0 | if (ri->type != CMS_RECIPINFO_AGREE) { |
78 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG, |
79 | 0 | CMS_R_NOT_KEY_AGREEMENT); |
80 | 0 | return 0; |
81 | 0 | } |
82 | 0 | if (palg) |
83 | 0 | *palg = ri->d.kari->keyEncryptionAlgorithm; |
84 | 0 | if (pukm) |
85 | 0 | *pukm = ri->d.kari->ukm; |
86 | 0 | return 1; |
87 | 0 | } |
88 | | |
89 | | /* Retrieve recipient encrypted keys from a kari */ |
90 | | |
91 | | STACK_OF(CMS_RecipientEncryptedKey) |
92 | | *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri) |
93 | 0 | { |
94 | 0 | if (ri->type != CMS_RECIPINFO_AGREE) { |
95 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS, |
96 | 0 | CMS_R_NOT_KEY_AGREEMENT); |
97 | 0 | return NULL; |
98 | 0 | } |
99 | 0 | return ri->d.kari->recipientEncryptedKeys; |
100 | 0 | } |
101 | | |
102 | | int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri, |
103 | | X509_ALGOR **pubalg, |
104 | | ASN1_BIT_STRING **pubkey, |
105 | | ASN1_OCTET_STRING **keyid, |
106 | | X509_NAME **issuer, |
107 | | ASN1_INTEGER **sno) |
108 | 0 | { |
109 | 0 | CMS_OriginatorIdentifierOrKey *oik; |
110 | 0 | if (ri->type != CMS_RECIPINFO_AGREE) { |
111 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID, |
112 | 0 | CMS_R_NOT_KEY_AGREEMENT); |
113 | 0 | return 0; |
114 | 0 | } |
115 | 0 | oik = ri->d.kari->originator; |
116 | 0 | if (issuer) |
117 | 0 | *issuer = NULL; |
118 | 0 | if (sno) |
119 | 0 | *sno = NULL; |
120 | 0 | if (keyid) |
121 | 0 | *keyid = NULL; |
122 | 0 | if (pubalg) |
123 | 0 | *pubalg = NULL; |
124 | 0 | if (pubkey) |
125 | 0 | *pubkey = NULL; |
126 | 0 | if (oik->type == CMS_OIK_ISSUER_SERIAL) { |
127 | 0 | if (issuer) |
128 | 0 | *issuer = oik->d.issuerAndSerialNumber->issuer; |
129 | 0 | if (sno) |
130 | 0 | *sno = oik->d.issuerAndSerialNumber->serialNumber; |
131 | 0 | } else if (oik->type == CMS_OIK_KEYIDENTIFIER) { |
132 | 0 | if (keyid) |
133 | 0 | *keyid = oik->d.subjectKeyIdentifier; |
134 | 0 | } else if (oik->type == CMS_OIK_PUBKEY) { |
135 | 0 | if (pubalg) |
136 | 0 | *pubalg = oik->d.originatorKey->algorithm; |
137 | 0 | if (pubkey) |
138 | 0 | *pubkey = oik->d.originatorKey->publicKey; |
139 | 0 | } else |
140 | 0 | return 0; |
141 | 0 | return 1; |
142 | 0 | } |
143 | | |
144 | | int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert) |
145 | 0 | { |
146 | 0 | CMS_OriginatorIdentifierOrKey *oik; |
147 | 0 | if (ri->type != CMS_RECIPINFO_AGREE) { |
148 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP, |
149 | 0 | CMS_R_NOT_KEY_AGREEMENT); |
150 | 0 | return -2; |
151 | 0 | } |
152 | 0 | oik = ri->d.kari->originator; |
153 | 0 | if (oik->type == CMS_OIK_ISSUER_SERIAL) |
154 | 0 | return cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert); |
155 | 0 | else if (oik->type == CMS_OIK_KEYIDENTIFIER) |
156 | 0 | return cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert); |
157 | 0 | return -1; |
158 | 0 | } |
159 | | |
160 | | int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek, |
161 | | ASN1_OCTET_STRING **keyid, |
162 | | ASN1_GENERALIZEDTIME **tm, |
163 | | CMS_OtherKeyAttribute **other, |
164 | | X509_NAME **issuer, ASN1_INTEGER **sno) |
165 | 0 | { |
166 | 0 | CMS_KeyAgreeRecipientIdentifier *rid = rek->rid; |
167 | 0 | if (rid->type == CMS_REK_ISSUER_SERIAL) { |
168 | 0 | if (issuer) |
169 | 0 | *issuer = rid->d.issuerAndSerialNumber->issuer; |
170 | 0 | if (sno) |
171 | 0 | *sno = rid->d.issuerAndSerialNumber->serialNumber; |
172 | 0 | if (keyid) |
173 | 0 | *keyid = NULL; |
174 | 0 | if (tm) |
175 | 0 | *tm = NULL; |
176 | 0 | if (other) |
177 | 0 | *other = NULL; |
178 | 0 | } else if (rid->type == CMS_REK_KEYIDENTIFIER) { |
179 | 0 | if (keyid) |
180 | 0 | *keyid = rid->d.rKeyId->subjectKeyIdentifier; |
181 | 0 | if (tm) |
182 | 0 | *tm = rid->d.rKeyId->date; |
183 | 0 | if (other) |
184 | 0 | *other = rid->d.rKeyId->other; |
185 | 0 | if (issuer) |
186 | 0 | *issuer = NULL; |
187 | 0 | if (sno) |
188 | 0 | *sno = NULL; |
189 | 0 | } else |
190 | 0 | return 0; |
191 | 0 | return 1; |
192 | 0 | } |
193 | | |
194 | | int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek, |
195 | | X509 *cert) |
196 | 0 | { |
197 | 0 | CMS_KeyAgreeRecipientIdentifier *rid = rek->rid; |
198 | 0 | if (rid->type == CMS_REK_ISSUER_SERIAL) |
199 | 0 | return cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert); |
200 | 0 | else if (rid->type == CMS_REK_KEYIDENTIFIER) |
201 | 0 | return cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier, cert); |
202 | 0 | else |
203 | 0 | return -1; |
204 | 0 | } |
205 | | |
206 | | int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk) |
207 | 0 | { |
208 | 0 | EVP_PKEY_CTX *pctx; |
209 | 0 | CMS_KeyAgreeRecipientInfo *kari = ri->d.kari; |
210 | 0 | if (kari->pctx) { |
211 | 0 | EVP_PKEY_CTX_free(kari->pctx); |
212 | 0 | kari->pctx = NULL; |
213 | 0 | } |
214 | 0 | if (!pk) |
215 | 0 | return 1; |
216 | 0 | pctx = EVP_PKEY_CTX_new(pk, NULL); |
217 | 0 | if (!pctx || !EVP_PKEY_derive_init(pctx)) |
218 | 0 | goto err; |
219 | 0 | kari->pctx = pctx; |
220 | 0 | return 1; |
221 | 0 | err: |
222 | 0 | if (pctx) |
223 | 0 | EVP_PKEY_CTX_free(pctx); |
224 | 0 | return 0; |
225 | 0 | } |
226 | | |
227 | | EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri) |
228 | 0 | { |
229 | 0 | if (ri->type == CMS_RECIPINFO_AGREE) |
230 | 0 | return &ri->d.kari->ctx; |
231 | 0 | return NULL; |
232 | 0 | } |
233 | | |
234 | | /* |
235 | | * Derive KEK and decrypt/encrypt with it to produce either the original CEK |
236 | | * or the encrypted CEK. |
237 | | */ |
238 | | |
239 | | static int cms_kek_cipher(unsigned char **pout, size_t *poutlen, |
240 | | const unsigned char *in, size_t inlen, |
241 | | CMS_KeyAgreeRecipientInfo *kari, int enc) |
242 | 0 | { |
243 | | /* Key encryption key */ |
244 | 0 | unsigned char kek[EVP_MAX_KEY_LENGTH]; |
245 | 0 | size_t keklen; |
246 | 0 | int rv = 0; |
247 | 0 | unsigned char *out = NULL; |
248 | 0 | int outlen; |
249 | 0 | keklen = EVP_CIPHER_CTX_key_length(&kari->ctx); |
250 | 0 | if (keklen > EVP_MAX_KEY_LENGTH) |
251 | 0 | return 0; |
252 | | /* Derive KEK */ |
253 | 0 | if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0) |
254 | 0 | goto err; |
255 | | /* Set KEK in context */ |
256 | 0 | if (!EVP_CipherInit_ex(&kari->ctx, NULL, NULL, kek, NULL, enc)) |
257 | 0 | goto err; |
258 | | /* obtain output length of ciphered key */ |
259 | 0 | if (!EVP_CipherUpdate(&kari->ctx, NULL, &outlen, in, inlen)) |
260 | 0 | goto err; |
261 | 0 | out = OPENSSL_malloc(outlen); |
262 | 0 | if (!out) |
263 | 0 | goto err; |
264 | 0 | if (!EVP_CipherUpdate(&kari->ctx, out, &outlen, in, inlen)) |
265 | 0 | goto err; |
266 | 0 | *pout = out; |
267 | 0 | *poutlen = (size_t)outlen; |
268 | 0 | rv = 1; |
269 | |
|
270 | 0 | err: |
271 | 0 | OPENSSL_cleanse(kek, keklen); |
272 | 0 | if (!rv && out) |
273 | 0 | OPENSSL_free(out); |
274 | 0 | EVP_CIPHER_CTX_cleanup(&kari->ctx); |
275 | 0 | EVP_PKEY_CTX_free(kari->pctx); |
276 | 0 | kari->pctx = NULL; |
277 | 0 | return rv; |
278 | 0 | } |
279 | | |
280 | | int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms, |
281 | | CMS_RecipientInfo *ri, |
282 | | CMS_RecipientEncryptedKey *rek) |
283 | 0 | { |
284 | 0 | int rv = 0; |
285 | 0 | unsigned char *enckey = NULL, *cek = NULL; |
286 | 0 | size_t enckeylen; |
287 | 0 | size_t ceklen; |
288 | 0 | CMS_EncryptedContentInfo *ec; |
289 | 0 | enckeylen = rek->encryptedKey->length; |
290 | 0 | enckey = rek->encryptedKey->data; |
291 | | /* Setup all parameters to derive KEK */ |
292 | 0 | if (!cms_env_asn1_ctrl(ri, 1)) |
293 | 0 | goto err; |
294 | | /* Attempt to decrypt CEK */ |
295 | 0 | if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0)) |
296 | 0 | goto err; |
297 | 0 | ec = cms->d.envelopedData->encryptedContentInfo; |
298 | 0 | if (ec->key) { |
299 | 0 | OPENSSL_cleanse(ec->key, ec->keylen); |
300 | 0 | OPENSSL_free(ec->key); |
301 | 0 | } |
302 | 0 | ec->key = cek; |
303 | 0 | ec->keylen = ceklen; |
304 | 0 | cek = NULL; |
305 | 0 | rv = 1; |
306 | 0 | err: |
307 | 0 | if (cek) |
308 | 0 | OPENSSL_free(cek); |
309 | 0 | return rv; |
310 | 0 | } |
311 | | |
312 | | /* Create ephemeral key and initialise context based on it */ |
313 | | static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari, |
314 | | EVP_PKEY *pk) |
315 | 0 | { |
316 | 0 | EVP_PKEY_CTX *pctx = NULL; |
317 | 0 | EVP_PKEY *ekey = NULL; |
318 | 0 | int rv = 0; |
319 | 0 | pctx = EVP_PKEY_CTX_new(pk, NULL); |
320 | 0 | if (!pctx) |
321 | 0 | goto err; |
322 | 0 | if (EVP_PKEY_keygen_init(pctx) <= 0) |
323 | 0 | goto err; |
324 | 0 | if (EVP_PKEY_keygen(pctx, &ekey) <= 0) |
325 | 0 | goto err; |
326 | 0 | EVP_PKEY_CTX_free(pctx); |
327 | 0 | pctx = EVP_PKEY_CTX_new(ekey, NULL); |
328 | 0 | if (!pctx) |
329 | 0 | goto err; |
330 | 0 | if (EVP_PKEY_derive_init(pctx) <= 0) |
331 | 0 | goto err; |
332 | 0 | kari->pctx = pctx; |
333 | 0 | rv = 1; |
334 | 0 | err: |
335 | 0 | if (!rv && pctx) |
336 | 0 | EVP_PKEY_CTX_free(pctx); |
337 | 0 | if (ekey) |
338 | 0 | EVP_PKEY_free(ekey); |
339 | 0 | return rv; |
340 | 0 | } |
341 | | |
342 | | /* Initialise a ktri based on passed certificate and key */ |
343 | | |
344 | | int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip, |
345 | | EVP_PKEY *pk, unsigned int flags) |
346 | 0 | { |
347 | 0 | CMS_KeyAgreeRecipientInfo *kari; |
348 | 0 | CMS_RecipientEncryptedKey *rek = NULL; |
349 | |
|
350 | 0 | ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo); |
351 | 0 | if (!ri->d.kari) |
352 | 0 | return 0; |
353 | 0 | ri->type = CMS_RECIPINFO_AGREE; |
354 | |
|
355 | 0 | kari = ri->d.kari; |
356 | 0 | kari->version = 3; |
357 | |
|
358 | 0 | rek = M_ASN1_new_of(CMS_RecipientEncryptedKey); |
359 | 0 | if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) { |
360 | 0 | M_ASN1_free_of(rek, CMS_RecipientEncryptedKey); |
361 | 0 | return 0; |
362 | 0 | } |
363 | | |
364 | 0 | if (flags & CMS_USE_KEYID) { |
365 | 0 | rek->rid->type = CMS_REK_KEYIDENTIFIER; |
366 | 0 | rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier); |
367 | 0 | if (rek->rid->d.rKeyId == NULL) |
368 | 0 | return 0; |
369 | 0 | if (!cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip)) |
370 | 0 | return 0; |
371 | 0 | } else { |
372 | 0 | rek->rid->type = CMS_REK_ISSUER_SERIAL; |
373 | 0 | if (!cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip)) |
374 | 0 | return 0; |
375 | 0 | } |
376 | | |
377 | | /* Create ephemeral key */ |
378 | 0 | if (!cms_kari_create_ephemeral_key(kari, pk)) |
379 | 0 | return 0; |
380 | | |
381 | 0 | CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY); |
382 | 0 | rek->pkey = pk; |
383 | 0 | return 1; |
384 | 0 | } |
385 | | |
386 | | static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari, |
387 | | const EVP_CIPHER *cipher) |
388 | 0 | { |
389 | 0 | EVP_CIPHER_CTX *ctx = &kari->ctx; |
390 | 0 | const EVP_CIPHER *kekcipher; |
391 | 0 | int keylen = EVP_CIPHER_key_length(cipher); |
392 | | /* If a suitable wrap algorithm is already set nothing to do */ |
393 | 0 | kekcipher = EVP_CIPHER_CTX_cipher(ctx); |
394 | |
|
395 | 0 | if (kekcipher) { |
396 | 0 | if (EVP_CIPHER_CTX_mode(ctx) != EVP_CIPH_WRAP_MODE) |
397 | 0 | return 0; |
398 | 0 | return 1; |
399 | 0 | } |
400 | | /* |
401 | | * Pick a cipher based on content encryption cipher. If it is DES3 use |
402 | | * DES3 wrap otherwise use AES wrap similar to key size. |
403 | | */ |
404 | 0 | #ifndef OPENSSL_NO_DES |
405 | 0 | if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc) |
406 | 0 | kekcipher = EVP_des_ede3_wrap(); |
407 | 0 | else |
408 | 0 | #endif |
409 | 0 | if (keylen <= 16) |
410 | 0 | kekcipher = EVP_aes_128_wrap(); |
411 | 0 | else if (keylen <= 24) |
412 | 0 | kekcipher = EVP_aes_192_wrap(); |
413 | 0 | else |
414 | 0 | kekcipher = EVP_aes_256_wrap(); |
415 | 0 | return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL); |
416 | 0 | } |
417 | | |
418 | | /* Encrypt content key in key agreement recipient info */ |
419 | | |
420 | | int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms, |
421 | | CMS_RecipientInfo *ri) |
422 | 0 | { |
423 | 0 | CMS_KeyAgreeRecipientInfo *kari; |
424 | 0 | CMS_EncryptedContentInfo *ec; |
425 | 0 | CMS_RecipientEncryptedKey *rek; |
426 | 0 | STACK_OF(CMS_RecipientEncryptedKey) *reks; |
427 | 0 | int i; |
428 | |
|
429 | 0 | if (ri->type != CMS_RECIPINFO_AGREE) { |
430 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, CMS_R_NOT_KEY_AGREEMENT); |
431 | 0 | return 0; |
432 | 0 | } |
433 | 0 | kari = ri->d.kari; |
434 | 0 | reks = kari->recipientEncryptedKeys; |
435 | 0 | ec = cms->d.envelopedData->encryptedContentInfo; |
436 | | /* Initialise wrap algorithm parameters */ |
437 | 0 | if (!cms_wrap_init(kari, ec->cipher)) |
438 | 0 | return 0; |
439 | | /* |
440 | | * If no orignator key set up initialise for ephemeral key the public key |
441 | | * ASN1 structure will set the actual public key value. |
442 | | */ |
443 | 0 | if (kari->originator->type == -1) { |
444 | 0 | CMS_OriginatorIdentifierOrKey *oik = kari->originator; |
445 | 0 | oik->type = CMS_OIK_PUBKEY; |
446 | 0 | oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey); |
447 | 0 | if (!oik->d.originatorKey) |
448 | 0 | return 0; |
449 | 0 | } |
450 | | /* Initialise KDF algorithm */ |
451 | 0 | if (!cms_env_asn1_ctrl(ri, 0)) |
452 | 0 | return 0; |
453 | | /* For each rek, derive KEK, encrypt CEK */ |
454 | 0 | for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) { |
455 | 0 | unsigned char *enckey; |
456 | 0 | size_t enckeylen; |
457 | 0 | rek = sk_CMS_RecipientEncryptedKey_value(reks, i); |
458 | 0 | if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0) |
459 | 0 | return 0; |
460 | 0 | if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen, |
461 | 0 | kari, 1)) |
462 | 0 | return 0; |
463 | 0 | ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen); |
464 | 0 | } |
465 | | |
466 | 0 | return 1; |
467 | |
|
468 | 0 | } |