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