Backport of password based CMS support from HEAD.
[openssl.git] / crypto / cms / cms_env.c
1 /* crypto/cms/cms_env.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54 #include "cryptlib.h"
55 #include <openssl/asn1t.h>
56 #include <openssl/pem.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/err.h>
59 #include <openssl/cms.h>
60 #include <openssl/rand.h>
61 #include <openssl/aes.h>
62 #include "cms_lcl.h"
63 #include "asn1_locl.h"
64
65 /* CMS EnvelopedData Utilities */
66
67 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
68 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
69 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
70 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
71
72 DECLARE_STACK_OF(CMS_RecipientInfo)
73
74 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
75         {
76         if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped)
77                 {
78                 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
79                                 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
80                 return NULL;
81                 }
82         return cms->d.envelopedData;
83         }
84
85 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
86         {
87         if (cms->d.other == NULL)
88                 {
89                 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
90                 if (!cms->d.envelopedData)
91                         {
92                         CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT,
93                                                         ERR_R_MALLOC_FAILURE);
94                         return NULL;
95                         }
96                 cms->d.envelopedData->version = 0;
97                 cms->d.envelopedData->encryptedContentInfo->contentType =
98                                                 OBJ_nid2obj(NID_pkcs7_data);
99                 ASN1_OBJECT_free(cms->contentType);
100                 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
101                 return cms->d.envelopedData;
102                 }
103         return cms_get0_enveloped(cms);
104         }
105
106 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
107         {
108         CMS_EnvelopedData *env;
109         env = cms_get0_enveloped(cms);
110         if (!env)
111                 return NULL;
112         return env->recipientInfos;
113         }
114
115 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
116         {
117         return ri->type;
118         }
119
120 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
121         {
122         CMS_ContentInfo *cms;
123         CMS_EnvelopedData *env;
124         cms = CMS_ContentInfo_new();
125         if (!cms)
126                 goto merr;
127         env = cms_enveloped_data_init(cms);
128         if (!env)
129                 goto merr;
130         if (!cms_EncryptedContent_init(env->encryptedContentInfo,
131                                         cipher, NULL, 0))
132                 goto merr;
133         return cms;
134         merr:
135         if (cms)
136                 CMS_ContentInfo_free(cms);
137         CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
138         return NULL;
139         }
140
141 /* Key Transport Recipient Info (KTRI) routines */
142
143 /* Add a recipient certificate. For now only handle key transport.
144  * If we ever handle key agreement will need updating.
145  */
146
147 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
148                                         X509 *recip, unsigned int flags)
149         {
150         CMS_RecipientInfo *ri = NULL;
151         CMS_KeyTransRecipientInfo *ktri;
152         CMS_EnvelopedData *env;
153         EVP_PKEY *pk = NULL;
154         int i, type;
155         env = cms_get0_enveloped(cms);
156         if (!env)
157                 goto err;
158
159         /* Initialize recipient info */
160         ri = M_ASN1_new_of(CMS_RecipientInfo);
161         if (!ri)
162                 goto merr;
163
164         /* Initialize and add key transport recipient info */
165
166         ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
167         if (!ri->d.ktri)
168                 goto merr;
169         ri->type = CMS_RECIPINFO_TRANS;
170
171         ktri = ri->d.ktri;
172
173         X509_check_purpose(recip, -1, -1);
174         pk = X509_get_pubkey(recip);
175         if (!pk)
176                 {
177                 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
178                                 CMS_R_ERROR_GETTING_PUBLIC_KEY);
179                 goto err;
180                 }
181         CRYPTO_add(&recip->references, 1, CRYPTO_LOCK_X509);
182         ktri->pkey = pk;
183         ktri->recip = recip;
184
185         if (flags & CMS_USE_KEYID)
186                 {
187                 ktri->version = 2;
188                 type = CMS_RECIPINFO_KEYIDENTIFIER;
189                 }
190         else
191                 {
192                 ktri->version = 0;
193                 type = CMS_RECIPINFO_ISSUER_SERIAL;
194                 }
195
196         /* Not a typo: RecipientIdentifier and SignerIdentifier are the
197          * same structure.
198          */
199
200         if (!cms_set1_SignerIdentifier(ktri->rid, recip, type))
201                 goto err;
202
203         if (pk->ameth && pk->ameth->pkey_ctrl)
204                 {
205                 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_ENVELOPE,
206                                                 0, ri);
207                 if (i == -2)
208                         {
209                         CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
210                                 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
211                         goto err;
212                         }
213                 if (i <= 0)
214                         {
215                         CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
216                                 CMS_R_CTRL_FAILURE);
217                         goto err;
218                         }
219                 }
220
221         if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
222                 goto merr;
223
224         return ri;
225
226         merr:
227         CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
228         err:
229         if (ri)
230                 M_ASN1_free_of(ri, CMS_RecipientInfo);
231         return NULL;
232
233         }
234
235 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
236                                         EVP_PKEY **pk, X509 **recip,
237                                         X509_ALGOR **palg)
238         {
239         CMS_KeyTransRecipientInfo *ktri;
240         if (ri->type != CMS_RECIPINFO_TRANS)
241                 {
242                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
243                         CMS_R_NOT_KEY_TRANSPORT);
244                 return 0;
245                 }
246
247         ktri = ri->d.ktri;
248
249         if (pk)
250                 *pk = ktri->pkey;
251         if (recip)
252                 *recip = ktri->recip;
253         if (palg)
254                 *palg = ktri->keyEncryptionAlgorithm;
255         return 1;
256         }
257
258 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
259                                         ASN1_OCTET_STRING **keyid,
260                                         X509_NAME **issuer, ASN1_INTEGER **sno)
261         {
262         CMS_KeyTransRecipientInfo *ktri;
263         if (ri->type != CMS_RECIPINFO_TRANS)
264                 {
265                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
266                         CMS_R_NOT_KEY_TRANSPORT);
267                 return 0;
268                 }
269         ktri = ri->d.ktri;
270
271         return cms_SignerIdentifier_get0_signer_id(ktri->rid,
272                                                         keyid, issuer, sno);
273         }
274
275 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
276         {
277         if (ri->type != CMS_RECIPINFO_TRANS)
278                 {
279                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
280                         CMS_R_NOT_KEY_TRANSPORT);
281                 return -2;
282                 }
283         return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
284         }
285
286 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
287         {
288         if (ri->type != CMS_RECIPINFO_TRANS)
289                 {
290                 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY,
291                         CMS_R_NOT_KEY_TRANSPORT);
292                 return 0;
293                 }
294         ri->d.ktri->pkey = pkey;
295         return 1;
296         }
297
298 /* Encrypt content key in key transport recipient info */
299
300 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
301                                         CMS_RecipientInfo *ri)
302         {
303         CMS_KeyTransRecipientInfo *ktri;
304         CMS_EncryptedContentInfo *ec;
305         EVP_PKEY_CTX *pctx = NULL;
306         unsigned char *ek = NULL;
307         size_t eklen;
308
309         int ret = 0;
310
311         if (ri->type != CMS_RECIPINFO_TRANS)
312                 {
313                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
314                         CMS_R_NOT_KEY_TRANSPORT);
315                 return 0;
316                 }
317         ktri = ri->d.ktri;
318         ec = cms->d.envelopedData->encryptedContentInfo;
319
320         pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
321         if (!pctx)
322                 return 0;
323
324         if (EVP_PKEY_encrypt_init(pctx) <= 0)
325                 goto err;
326
327         if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
328                                 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0)
329                 {
330                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
331                 goto err;
332                 }
333
334         if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
335                 goto err;
336
337         ek = OPENSSL_malloc(eklen);
338
339         if (ek == NULL)
340                 {
341                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
342                                                         ERR_R_MALLOC_FAILURE);
343                 goto err;
344                 }
345
346         if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
347                 goto err;
348
349         ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
350         ek = NULL;
351
352         ret = 1;
353
354         err:
355         if (pctx)
356                 EVP_PKEY_CTX_free(pctx);
357         if (ek)
358                 OPENSSL_free(ek);
359         return ret;
360
361         }
362
363 /* Decrypt content key from KTRI */
364
365 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
366                                                         CMS_RecipientInfo *ri)
367         {
368         CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
369         EVP_PKEY_CTX *pctx = NULL;
370         unsigned char *ek = NULL;
371         size_t eklen;
372         int ret = 0;
373
374         if (ktri->pkey == NULL)
375                 {
376                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
377                         CMS_R_NO_PRIVATE_KEY);
378                 return 0;
379                 }
380
381         pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
382         if (!pctx)
383                 return 0;
384
385         if (EVP_PKEY_decrypt_init(pctx) <= 0)
386                 goto err;
387
388         if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
389                                 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0)
390                 {
391                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
392                 goto err;
393                 }
394
395         if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
396                                 ktri->encryptedKey->data,
397                                 ktri->encryptedKey->length) <= 0)
398                 goto err;
399
400         ek = OPENSSL_malloc(eklen);
401
402         if (ek == NULL)
403                 {
404                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
405                                                         ERR_R_MALLOC_FAILURE);
406                 goto err;
407                 }
408
409         if (EVP_PKEY_decrypt(pctx, ek, &eklen,
410                                 ktri->encryptedKey->data,
411                                 ktri->encryptedKey->length) <= 0)
412                 {
413                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
414                 goto err;
415                 }
416
417         ret = 1;
418
419         cms->d.envelopedData->encryptedContentInfo->key = ek;
420         cms->d.envelopedData->encryptedContentInfo->keylen = eklen;
421
422         err:
423         if (pctx)
424                 EVP_PKEY_CTX_free(pctx);
425         if (!ret && ek)
426                 OPENSSL_free(ek);
427
428         return ret;
429         }
430
431 /* Key Encrypted Key (KEK) RecipientInfo routines */
432
433 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, 
434                                         const unsigned char *id, size_t idlen)
435         {
436         ASN1_OCTET_STRING tmp_os;
437         CMS_KEKRecipientInfo *kekri;
438         if (ri->type != CMS_RECIPINFO_KEK)
439                 {
440                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
441                 return -2;
442                 }
443         kekri = ri->d.kekri;
444         tmp_os.type = V_ASN1_OCTET_STRING;
445         tmp_os.flags = 0;
446         tmp_os.data = (unsigned char *)id;
447         tmp_os.length = (int)idlen;
448         return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
449         }
450
451 /* For now hard code AES key wrap info */
452
453 static size_t aes_wrap_keylen(int nid)
454         {
455         switch (nid)
456                 {
457                 case NID_id_aes128_wrap:
458                 return 16;
459
460                 case NID_id_aes192_wrap:
461                 return  24;
462
463                 case NID_id_aes256_wrap:
464                 return  32;
465
466                 default:
467                 return 0;
468                 }
469         }
470
471 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
472                                         unsigned char *key, size_t keylen,
473                                         unsigned char *id, size_t idlen,
474                                         ASN1_GENERALIZEDTIME *date,
475                                         ASN1_OBJECT *otherTypeId,
476                                         ASN1_TYPE *otherType)
477         {
478         CMS_RecipientInfo *ri = NULL;
479         CMS_EnvelopedData *env;
480         CMS_KEKRecipientInfo *kekri;
481         env = cms_get0_enveloped(cms);
482         if (!env)
483                 goto err;
484
485         if (nid == NID_undef)
486                 {
487                 switch (keylen)
488                         {
489                         case 16:
490                         nid = NID_id_aes128_wrap;
491                         break;
492
493                         case  24:
494                         nid = NID_id_aes192_wrap;
495                         break;
496
497                         case  32:
498                         nid = NID_id_aes256_wrap;
499                         break;
500
501                         default:
502                         CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
503                                                 CMS_R_INVALID_KEY_LENGTH);
504                         goto err;
505                         }
506
507                 }
508         else
509                 {
510
511                 size_t exp_keylen = aes_wrap_keylen(nid);
512
513                 if (!exp_keylen)
514                         {
515                         CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
516                                         CMS_R_UNSUPPORTED_KEK_ALGORITHM);
517                         goto err;
518                         }
519
520                 if (keylen != exp_keylen)
521                         {
522                         CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
523                                         CMS_R_INVALID_KEY_LENGTH);
524                         goto err;
525                         }
526
527                 }
528
529         /* Initialize recipient info */
530         ri = M_ASN1_new_of(CMS_RecipientInfo);
531         if (!ri)
532                 goto merr;
533
534         ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
535         if (!ri->d.kekri)
536                 goto merr;
537         ri->type = CMS_RECIPINFO_KEK;
538
539         kekri = ri->d.kekri;
540
541         if (otherTypeId)
542                 {
543                 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
544                 if (kekri->kekid->other == NULL)
545                         goto merr;
546                 }
547
548         if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
549                 goto merr;
550
551
552         /* After this point no calls can fail */
553
554         kekri->version = 4;
555
556         kekri->key = key;
557         kekri->keylen = keylen;
558
559         ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
560
561         kekri->kekid->date = date;
562
563         if (kekri->kekid->other)
564                 {
565                 kekri->kekid->other->keyAttrId = otherTypeId;
566                 kekri->kekid->other->keyAttr = otherType;
567                 }
568
569         X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
570                                 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
571
572         return ri;
573
574         merr:
575         CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
576         err:
577         if (ri)
578                 M_ASN1_free_of(ri, CMS_RecipientInfo);
579         return NULL;
580
581         }
582
583 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
584                                         X509_ALGOR **palg,
585                                         ASN1_OCTET_STRING **pid,
586                                         ASN1_GENERALIZEDTIME **pdate,
587                                         ASN1_OBJECT **potherid,
588                                         ASN1_TYPE **pothertype)
589         {
590         CMS_KEKIdentifier *rkid;
591         if (ri->type != CMS_RECIPINFO_KEK)
592                 {
593                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
594                 return 0;
595                 }
596         rkid =  ri->d.kekri->kekid;
597         if (palg)
598                 *palg = ri->d.kekri->keyEncryptionAlgorithm;
599         if (pid)
600                 *pid = rkid->keyIdentifier;
601         if (pdate)
602                 *pdate = rkid->date;
603         if (potherid)
604                 {
605                 if (rkid->other)
606                         *potherid = rkid->other->keyAttrId;
607                 else
608                         *potherid = NULL;
609                 }
610         if (pothertype)
611                 {
612                 if (rkid->other)
613                         *pothertype = rkid->other->keyAttr;
614                 else
615                         *pothertype = NULL;
616                 }
617         return 1;
618         }
619
620 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, 
621                                 unsigned char *key, size_t keylen)
622         {
623         CMS_KEKRecipientInfo *kekri;
624         if (ri->type != CMS_RECIPINFO_KEK)
625                 {
626                 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
627                 return 0;
628                 }
629
630         kekri = ri->d.kekri;
631         kekri->key = key;
632         kekri->keylen = keylen;
633         return 1;
634         }
635
636
637 /* Encrypt content key in KEK recipient info */
638
639 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
640                                         CMS_RecipientInfo *ri)
641         {
642         CMS_EncryptedContentInfo *ec;
643         CMS_KEKRecipientInfo *kekri;
644         AES_KEY actx;
645         unsigned char *wkey = NULL;
646         int wkeylen;
647         int r = 0;
648
649         ec = cms->d.envelopedData->encryptedContentInfo;
650
651         kekri = ri->d.kekri;
652
653         if (!kekri->key)
654                 {
655                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
656                 return 0;
657                 }
658
659         if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx))
660                 { 
661                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
662                                                 CMS_R_ERROR_SETTING_KEY);
663                 goto err;
664                 }
665
666         wkey = OPENSSL_malloc(ec->keylen + 8);
667
668         if (!wkey)
669                 { 
670                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
671                                                 ERR_R_MALLOC_FAILURE);
672                 goto err;
673                 }
674
675         wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
676
677         if (wkeylen <= 0)
678                 {
679                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
680                 goto err;
681                 }
682
683         ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
684
685         r = 1;
686
687         err:
688
689         if (!r && wkey)
690                 OPENSSL_free(wkey);
691         OPENSSL_cleanse(&actx, sizeof(actx));
692
693         return r;
694
695         }
696
697 /* Decrypt content key in KEK recipient info */
698
699 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
700                                         CMS_RecipientInfo *ri)
701         {
702         CMS_EncryptedContentInfo *ec;
703         CMS_KEKRecipientInfo *kekri;
704         AES_KEY actx;
705         unsigned char *ukey = NULL;
706         int ukeylen;
707         int r = 0, wrap_nid;
708
709         ec = cms->d.envelopedData->encryptedContentInfo;
710
711         kekri = ri->d.kekri;
712
713         if (!kekri->key)
714                 {
715                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
716                 return 0;
717                 }
718
719         wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
720         if (aes_wrap_keylen(wrap_nid) != kekri->keylen)
721                 {
722                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
723                         CMS_R_INVALID_KEY_LENGTH);
724                 return 0;
725                 }
726
727         /* If encrypted key length is invalid don't bother */
728
729         if (kekri->encryptedKey->length < 16)
730                 { 
731                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
732                                         CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
733                 goto err;
734                 }
735
736         if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx))
737                 { 
738                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
739                                                 CMS_R_ERROR_SETTING_KEY);
740                 goto err;
741                 }
742
743         ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
744
745         if (!ukey)
746                 { 
747                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
748                                                 ERR_R_MALLOC_FAILURE);
749                 goto err;
750                 }
751
752         ukeylen = AES_unwrap_key(&actx, NULL, ukey,
753                                         kekri->encryptedKey->data,
754                                         kekri->encryptedKey->length);
755
756         if (ukeylen <= 0)
757                 {
758                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
759                                                         CMS_R_UNWRAP_ERROR);
760                 goto err;
761                 }
762
763         ec->key = ukey;
764         ec->keylen = ukeylen;
765
766         r = 1;
767
768         err:
769
770         if (!r && ukey)
771                 OPENSSL_free(ukey);
772         OPENSSL_cleanse(&actx, sizeof(actx));
773
774         return r;
775
776         }
777
778 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
779         {
780         switch(ri->type)
781                 {
782                 case CMS_RECIPINFO_TRANS:
783                 return cms_RecipientInfo_ktri_decrypt(cms, ri);
784
785                 case CMS_RECIPINFO_KEK:
786                 return cms_RecipientInfo_kekri_decrypt(cms, ri);
787
788                 case CMS_RECIPINFO_PASS:
789                 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
790
791                 default:
792                 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
793                         CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
794                 return 0;
795                 }
796         }
797
798 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
799         {
800         CMS_EncryptedContentInfo *ec;
801         STACK_OF(CMS_RecipientInfo) *rinfos;
802         CMS_RecipientInfo *ri;
803         int i, r, ok = 0;
804         BIO *ret;
805
806         /* Get BIO first to set up key */
807
808         ec = cms->d.envelopedData->encryptedContentInfo;
809         ret = cms_EncryptedContent_init_bio(ec);
810
811         /* If error or no cipher end of processing */
812
813         if (!ret || !ec->cipher)
814                 return ret;
815
816         /* Now encrypt content key according to each RecipientInfo type */
817
818         rinfos = cms->d.envelopedData->recipientInfos;
819
820         for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++)
821                 {
822                 ri = sk_CMS_RecipientInfo_value(rinfos, i);
823
824                 switch (ri->type)
825                         {
826                         case CMS_RECIPINFO_TRANS:
827                         r = cms_RecipientInfo_ktri_encrypt(cms, ri);
828                         break;
829
830                         case CMS_RECIPINFO_KEK:
831                         r = cms_RecipientInfo_kekri_encrypt(cms, ri);
832                         break;
833
834                         case CMS_RECIPINFO_PASS:
835                         r = cms_RecipientInfo_pwri_crypt(cms, ri, 1);
836                         break;
837
838                         default:
839                         CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
840                                 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
841                         goto err;
842                         }
843
844                 if (r <= 0)
845                         {
846                         CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
847                                 CMS_R_ERROR_SETTING_RECIPIENTINFO);
848                         goto err;
849                         }
850                 }
851
852         ok = 1;
853
854         err:
855         ec->cipher = NULL;
856         if (ec->key)
857                 {
858                 OPENSSL_cleanse(ec->key, ec->keylen);
859                 OPENSSL_free(ec->key);
860                 ec->key = NULL;
861                 ec->keylen = 0;
862                 }
863         if (ok)
864                 return ret;
865         BIO_free(ret);
866         return NULL;
867
868         }