]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - crypto/openssl/crypto/cms/cms_env.c
Fix OpenSSL multiple vulnerabilities. [13:03]
[FreeBSD/releng/9.0.git] / crypto / openssl / 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
64 /* CMS EnvelopedData Utilities */
65
66 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
67 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
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 static 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 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         /* Since we have no EVP_PKEY_ASN1_METHOD in OpenSSL 0.9.8,
204          * hard code algorithm parameters.
205          */
206
207         if (pk->type == EVP_PKEY_RSA)
208                 {
209                 X509_ALGOR_set0(ktri->keyEncryptionAlgorithm,
210                                         OBJ_nid2obj(NID_rsaEncryption), 
211                                         V_ASN1_NULL, 0);
212                 }
213         else
214                 {
215                 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
216                                 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
217                 goto err;
218                 }
219
220         if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
221                 goto merr;
222
223         return ri;
224
225         merr:
226         CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
227         err:
228         if (ri)
229                 M_ASN1_free_of(ri, CMS_RecipientInfo);
230         return NULL;
231
232         }
233
234 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
235                                         EVP_PKEY **pk, X509 **recip,
236                                         X509_ALGOR **palg)
237         {
238         CMS_KeyTransRecipientInfo *ktri;
239         if (ri->type != CMS_RECIPINFO_TRANS)
240                 {
241                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
242                         CMS_R_NOT_KEY_TRANSPORT);
243                 return 0;
244                 }
245
246         ktri = ri->d.ktri;
247
248         if (pk)
249                 *pk = ktri->pkey;
250         if (recip)
251                 *recip = ktri->recip;
252         if (palg)
253                 *palg = ktri->keyEncryptionAlgorithm;
254         return 1;
255         }
256
257 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
258                                         ASN1_OCTET_STRING **keyid,
259                                         X509_NAME **issuer, ASN1_INTEGER **sno)
260         {
261         CMS_KeyTransRecipientInfo *ktri;
262         if (ri->type != CMS_RECIPINFO_TRANS)
263                 {
264                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
265                         CMS_R_NOT_KEY_TRANSPORT);
266                 return 0;
267                 }
268         ktri = ri->d.ktri;
269
270         return cms_SignerIdentifier_get0_signer_id(ktri->rid,
271                                                         keyid, issuer, sno);
272         }
273
274 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
275         {
276         if (ri->type != CMS_RECIPINFO_TRANS)
277                 {
278                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
279                         CMS_R_NOT_KEY_TRANSPORT);
280                 return -2;
281                 }
282         return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
283         }
284
285 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
286         {
287         if (ri->type != CMS_RECIPINFO_TRANS)
288                 {
289                 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY,
290                         CMS_R_NOT_KEY_TRANSPORT);
291                 return 0;
292                 }
293         ri->d.ktri->pkey = pkey;
294         return 1;
295         }
296
297 /* Encrypt content key in key transport recipient info */
298
299 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
300                                         CMS_RecipientInfo *ri)
301         {
302         CMS_KeyTransRecipientInfo *ktri;
303         CMS_EncryptedContentInfo *ec;
304         unsigned char *ek = NULL;
305         int eklen;
306
307         int ret = 0;
308
309         if (ri->type != CMS_RECIPINFO_TRANS)
310                 {
311                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
312                         CMS_R_NOT_KEY_TRANSPORT);
313                 return 0;
314                 }
315         ktri = ri->d.ktri;
316         ec = cms->d.envelopedData->encryptedContentInfo;
317
318         eklen = EVP_PKEY_size(ktri->pkey);
319
320         ek = OPENSSL_malloc(eklen);
321
322         if (ek == NULL)
323                 {
324                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
325                                                         ERR_R_MALLOC_FAILURE);
326                 goto err;
327                 }
328
329         eklen = EVP_PKEY_encrypt(ek, ec->key, ec->keylen, ktri->pkey);
330
331         if (eklen <= 0)
332                 goto err;
333
334         ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
335         ek = NULL;
336
337         ret = 1;
338
339         err:
340         if (ek)
341                 OPENSSL_free(ek);
342         return ret;
343
344         }
345
346 /* Decrypt content key from KTRI */
347
348 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
349                                                         CMS_RecipientInfo *ri)
350         {
351         CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
352         unsigned char *ek = NULL;
353         int eklen;
354         int ret = 0;
355         CMS_EncryptedContentInfo *ec;
356         ec = cms->d.envelopedData->encryptedContentInfo;
357
358         if (ktri->pkey == NULL)
359                 {
360                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
361                         CMS_R_NO_PRIVATE_KEY);
362                 return 0;
363                 }
364
365         eklen = EVP_PKEY_size(ktri->pkey);
366
367         ek = OPENSSL_malloc(eklen);
368
369         if (ek == NULL)
370                 {
371                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
372                                                         ERR_R_MALLOC_FAILURE);
373                 goto err;
374                 }
375
376         eklen = EVP_PKEY_decrypt(ek, 
377                                 ktri->encryptedKey->data,
378                                 ktri->encryptedKey->length, ktri->pkey);
379         if (eklen <= 0)
380                 {
381                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
382                 goto err;
383                 }
384
385         ret = 1;
386
387         if (ec->key)
388                 {
389                 OPENSSL_cleanse(ec->key, ec->keylen);
390                 OPENSSL_free(ec->key);
391                 }
392
393         ec->key = ek;
394         ec->keylen = eklen;
395
396         err:
397         if (!ret && ek)
398                 OPENSSL_free(ek);
399
400         return ret;
401         }
402
403 /* Key Encrypted Key (KEK) RecipientInfo routines */
404
405 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, 
406                                         const unsigned char *id, size_t idlen)
407         {
408         ASN1_OCTET_STRING tmp_os;
409         CMS_KEKRecipientInfo *kekri;
410         if (ri->type != CMS_RECIPINFO_KEK)
411                 {
412                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
413                 return -2;
414                 }
415         kekri = ri->d.kekri;
416         tmp_os.type = V_ASN1_OCTET_STRING;
417         tmp_os.flags = 0;
418         tmp_os.data = (unsigned char *)id;
419         tmp_os.length = (int)idlen;
420         return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
421         }
422
423 /* For now hard code AES key wrap info */
424
425 static size_t aes_wrap_keylen(int nid)
426         {
427         switch (nid)
428                 {
429                 case NID_id_aes128_wrap:
430                 return 16;
431
432                 case NID_id_aes192_wrap:
433                 return  24;
434
435                 case NID_id_aes256_wrap:
436                 return  32;
437
438                 default:
439                 return 0;
440                 }
441         }
442
443 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
444                                         unsigned char *key, size_t keylen,
445                                         unsigned char *id, size_t idlen,
446                                         ASN1_GENERALIZEDTIME *date,
447                                         ASN1_OBJECT *otherTypeId,
448                                         ASN1_TYPE *otherType)
449         {
450         CMS_RecipientInfo *ri = NULL;
451         CMS_EnvelopedData *env;
452         CMS_KEKRecipientInfo *kekri;
453         env = cms_get0_enveloped(cms);
454         if (!env)
455                 goto err;
456
457         if (nid == NID_undef)
458                 {
459                 switch (keylen)
460                         {
461                         case 16:
462                         nid = NID_id_aes128_wrap;
463                         break;
464
465                         case  24:
466                         nid = NID_id_aes192_wrap;
467                         break;
468
469                         case  32:
470                         nid = NID_id_aes256_wrap;
471                         break;
472
473                         default:
474                         CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
475                                                 CMS_R_INVALID_KEY_LENGTH);
476                         goto err;
477                         }
478
479                 }
480         else
481                 {
482
483                 size_t exp_keylen = aes_wrap_keylen(nid);
484
485                 if (!exp_keylen)
486                         {
487                         CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
488                                         CMS_R_UNSUPPORTED_KEK_ALGORITHM);
489                         goto err;
490                         }
491
492                 if (keylen != exp_keylen)
493                         {
494                         CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
495                                         CMS_R_INVALID_KEY_LENGTH);
496                         goto err;
497                         }
498
499                 }
500
501         /* Initialize recipient info */
502         ri = M_ASN1_new_of(CMS_RecipientInfo);
503         if (!ri)
504                 goto merr;
505
506         ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
507         if (!ri->d.kekri)
508                 goto merr;
509         ri->type = CMS_RECIPINFO_KEK;
510
511         kekri = ri->d.kekri;
512
513         if (otherTypeId)
514                 {
515                 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
516                 if (kekri->kekid->other == NULL)
517                         goto merr;
518                 }
519
520         if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
521                 goto merr;
522
523
524         /* After this point no calls can fail */
525
526         kekri->version = 4;
527
528         kekri->key = key;
529         kekri->keylen = keylen;
530
531         ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
532
533         kekri->kekid->date = date;
534
535         if (kekri->kekid->other)
536                 {
537                 kekri->kekid->other->keyAttrId = otherTypeId;
538                 kekri->kekid->other->keyAttr = otherType;
539                 }
540
541         X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
542                                 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
543
544         return ri;
545
546         merr:
547         CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
548         err:
549         if (ri)
550                 M_ASN1_free_of(ri, CMS_RecipientInfo);
551         return NULL;
552
553         }
554
555 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
556                                         X509_ALGOR **palg,
557                                         ASN1_OCTET_STRING **pid,
558                                         ASN1_GENERALIZEDTIME **pdate,
559                                         ASN1_OBJECT **potherid,
560                                         ASN1_TYPE **pothertype)
561         {
562         CMS_KEKIdentifier *rkid;
563         if (ri->type != CMS_RECIPINFO_KEK)
564                 {
565                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
566                 return 0;
567                 }
568         rkid =  ri->d.kekri->kekid;
569         if (palg)
570                 *palg = ri->d.kekri->keyEncryptionAlgorithm;
571         if (pid)
572                 *pid = rkid->keyIdentifier;
573         if (pdate)
574                 *pdate = rkid->date;
575         if (potherid)
576                 {
577                 if (rkid->other)
578                         *potherid = rkid->other->keyAttrId;
579                 else
580                         *potherid = NULL;
581                 }
582         if (pothertype)
583                 {
584                 if (rkid->other)
585                         *pothertype = rkid->other->keyAttr;
586                 else
587                         *pothertype = NULL;
588                 }
589         return 1;
590         }
591
592 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, 
593                                 unsigned char *key, size_t keylen)
594         {
595         CMS_KEKRecipientInfo *kekri;
596         if (ri->type != CMS_RECIPINFO_KEK)
597                 {
598                 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
599                 return 0;
600                 }
601
602         kekri = ri->d.kekri;
603         kekri->key = key;
604         kekri->keylen = keylen;
605         return 1;
606         }
607
608
609 /* Encrypt content key in KEK recipient info */
610
611 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
612                                         CMS_RecipientInfo *ri)
613         {
614         CMS_EncryptedContentInfo *ec;
615         CMS_KEKRecipientInfo *kekri;
616         AES_KEY actx;
617         unsigned char *wkey = NULL;
618         int wkeylen;
619         int r = 0;
620
621         ec = cms->d.envelopedData->encryptedContentInfo;
622
623         kekri = ri->d.kekri;
624
625         if (!kekri->key)
626                 {
627                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
628                 return 0;
629                 }
630
631         if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx))
632                 { 
633                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
634                                                 CMS_R_ERROR_SETTING_KEY);
635                 goto err;
636                 }
637
638         wkey = OPENSSL_malloc(ec->keylen + 8);
639
640         if (!wkey)
641                 { 
642                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
643                                                 ERR_R_MALLOC_FAILURE);
644                 goto err;
645                 }
646
647         wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
648
649         if (wkeylen <= 0)
650                 {
651                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
652                 goto err;
653                 }
654
655         ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
656
657         r = 1;
658
659         err:
660
661         if (!r && wkey)
662                 OPENSSL_free(wkey);
663         OPENSSL_cleanse(&actx, sizeof(actx));
664
665         return r;
666
667         }
668
669 /* Decrypt content key in KEK recipient info */
670
671 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
672                                         CMS_RecipientInfo *ri)
673         {
674         CMS_EncryptedContentInfo *ec;
675         CMS_KEKRecipientInfo *kekri;
676         AES_KEY actx;
677         unsigned char *ukey = NULL;
678         int ukeylen;
679         int r = 0, wrap_nid;
680
681         ec = cms->d.envelopedData->encryptedContentInfo;
682
683         kekri = ri->d.kekri;
684
685         if (!kekri->key)
686                 {
687                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
688                 return 0;
689                 }
690
691         wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
692         if (aes_wrap_keylen(wrap_nid) != kekri->keylen)
693                 {
694                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
695                         CMS_R_INVALID_KEY_LENGTH);
696                 return 0;
697                 }
698
699         /* If encrypted key length is invalid don't bother */
700
701         if (kekri->encryptedKey->length < 16)
702                 { 
703                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
704                                         CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
705                 goto err;
706                 }
707
708         if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx))
709                 { 
710                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
711                                                 CMS_R_ERROR_SETTING_KEY);
712                 goto err;
713                 }
714
715         ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
716
717         if (!ukey)
718                 { 
719                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
720                                                 ERR_R_MALLOC_FAILURE);
721                 goto err;
722                 }
723
724         ukeylen = AES_unwrap_key(&actx, NULL, ukey,
725                                         kekri->encryptedKey->data,
726                                         kekri->encryptedKey->length);
727
728         if (ukeylen <= 0)
729                 {
730                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
731                                                         CMS_R_UNWRAP_ERROR);
732                 goto err;
733                 }
734
735         ec->key = ukey;
736         ec->keylen = ukeylen;
737
738         r = 1;
739
740         err:
741
742         if (!r && ukey)
743                 OPENSSL_free(ukey);
744         OPENSSL_cleanse(&actx, sizeof(actx));
745
746         return r;
747
748         }
749
750 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
751         {
752         switch(ri->type)
753                 {
754                 case CMS_RECIPINFO_TRANS:
755                 return cms_RecipientInfo_ktri_decrypt(cms, ri);
756
757                 case CMS_RECIPINFO_KEK:
758                 return cms_RecipientInfo_kekri_decrypt(cms, ri);
759
760                 default:
761                 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
762                         CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
763                 return 0;
764                 }
765         }
766
767 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
768         {
769         CMS_EncryptedContentInfo *ec;
770         STACK_OF(CMS_RecipientInfo) *rinfos;
771         CMS_RecipientInfo *ri;
772         int i, r, ok = 0;
773         BIO *ret;
774
775         /* Get BIO first to set up key */
776
777         ec = cms->d.envelopedData->encryptedContentInfo;
778         ret = cms_EncryptedContent_init_bio(ec);
779
780         /* If error or no cipher end of processing */
781
782         if (!ret || !ec->cipher)
783                 return ret;
784
785         /* Now encrypt content key according to each RecipientInfo type */
786
787         rinfos = cms->d.envelopedData->recipientInfos;
788
789         for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++)
790                 {
791                 ri = sk_CMS_RecipientInfo_value(rinfos, i);
792
793                 switch (ri->type)
794                         {
795                         case CMS_RECIPINFO_TRANS:
796                         r = cms_RecipientInfo_ktri_encrypt(cms, ri);
797                         break;
798
799                         case CMS_RECIPINFO_KEK:
800                         r = cms_RecipientInfo_kekri_encrypt(cms, ri);
801                         break;
802
803                         default:
804                         CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
805                                 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
806                         goto err;
807                         }
808
809                 if (r <= 0)
810                         {
811                         CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
812                                 CMS_R_ERROR_SETTING_RECIPIENTINFO);
813                         goto err;
814                         }
815                 }
816
817         ok = 1;
818
819         err:
820         ec->cipher = NULL;
821         if (ec->key)
822                 {
823                 OPENSSL_cleanse(ec->key, ec->keylen);
824                 OPENSSL_free(ec->key);
825                 ec->key = NULL;
826                 ec->keylen = 0;
827                 }
828         if (ok)
829                 return ret;
830         BIO_free(ret);
831         return NULL;
832
833         }