1 /* crypto/cms/cms_env.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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>
63 #include "asn1_locl.h"
65 /* CMS EnvelopedData Utilities */
67 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
68 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
69 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
70 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
72 DECLARE_STACK_OF(CMS_RecipientInfo)
74 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
76 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped)
78 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
79 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
82 return cms->d.envelopedData;
85 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
87 if (cms->d.other == NULL)
89 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
90 if (!cms->d.envelopedData)
92 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT,
93 ERR_R_MALLOC_FAILURE);
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;
103 return cms_get0_enveloped(cms);
106 static int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
108 EVP_PKEY *pkey = ri->d.ktri->pkey;
110 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
112 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
115 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
116 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
121 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
127 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
129 CMS_EnvelopedData *env;
130 env = cms_get0_enveloped(cms);
133 return env->recipientInfos;
136 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
141 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
143 if (ri->type == CMS_RECIPINFO_TRANS)
144 return ri->d.ktri->pctx;
148 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
150 CMS_ContentInfo *cms;
151 CMS_EnvelopedData *env;
152 cms = CMS_ContentInfo_new();
155 env = cms_enveloped_data_init(cms);
158 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
164 CMS_ContentInfo_free(cms);
165 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
169 /* Key Transport Recipient Info (KTRI) routines */
171 /* Add a recipient certificate. For now only handle key transport.
172 * If we ever handle key agreement will need updating.
175 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
176 X509 *recip, unsigned int flags)
178 CMS_RecipientInfo *ri = NULL;
179 CMS_KeyTransRecipientInfo *ktri;
180 CMS_EnvelopedData *env;
183 env = cms_get0_enveloped(cms);
187 /* Initialize recipient info */
188 ri = M_ASN1_new_of(CMS_RecipientInfo);
192 /* Initialize and add key transport recipient info */
194 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
197 ri->type = CMS_RECIPINFO_TRANS;
201 X509_check_purpose(recip, -1, -1);
202 pk = X509_get_pubkey(recip);
205 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
206 CMS_R_ERROR_GETTING_PUBLIC_KEY);
209 CRYPTO_add(&recip->references, 1, CRYPTO_LOCK_X509);
213 if (flags & CMS_USE_KEYID)
216 type = CMS_RECIPINFO_KEYIDENTIFIER;
221 type = CMS_RECIPINFO_ISSUER_SERIAL;
224 /* Not a typo: RecipientIdentifier and SignerIdentifier are the
228 if (!cms_set1_SignerIdentifier(ktri->rid, recip, type))
231 if (flags & CMS_KEY_PARAM)
233 ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
236 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
239 else if (!cms_env_asn1_ctrl(ri, 0))
242 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
248 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
251 M_ASN1_free_of(ri, CMS_RecipientInfo);
256 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
257 EVP_PKEY **pk, X509 **recip,
260 CMS_KeyTransRecipientInfo *ktri;
261 if (ri->type != CMS_RECIPINFO_TRANS)
263 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
264 CMS_R_NOT_KEY_TRANSPORT);
273 *recip = ktri->recip;
275 *palg = ktri->keyEncryptionAlgorithm;
279 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
280 ASN1_OCTET_STRING **keyid,
281 X509_NAME **issuer, ASN1_INTEGER **sno)
283 CMS_KeyTransRecipientInfo *ktri;
284 if (ri->type != CMS_RECIPINFO_TRANS)
286 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
287 CMS_R_NOT_KEY_TRANSPORT);
292 return cms_SignerIdentifier_get0_signer_id(ktri->rid,
296 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
298 if (ri->type != CMS_RECIPINFO_TRANS)
300 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
301 CMS_R_NOT_KEY_TRANSPORT);
304 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
307 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
309 if (ri->type != CMS_RECIPINFO_TRANS)
311 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY,
312 CMS_R_NOT_KEY_TRANSPORT);
315 ri->d.ktri->pkey = pkey;
319 /* Encrypt content key in key transport recipient info */
321 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
322 CMS_RecipientInfo *ri)
324 CMS_KeyTransRecipientInfo *ktri;
325 CMS_EncryptedContentInfo *ec;
327 unsigned char *ek = NULL;
332 if (ri->type != CMS_RECIPINFO_TRANS)
334 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
335 CMS_R_NOT_KEY_TRANSPORT);
339 ec = cms->d.envelopedData->encryptedContentInfo;
345 if (!cms_env_asn1_ctrl(ri, 0))
350 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
354 if (EVP_PKEY_encrypt_init(pctx) <= 0)
358 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
359 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0)
361 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
365 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
368 ek = OPENSSL_malloc(eklen);
372 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
373 ERR_R_MALLOC_FAILURE);
377 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
380 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
388 EVP_PKEY_CTX_free(pctx);
397 /* Decrypt content key from KTRI */
399 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
400 CMS_RecipientInfo *ri)
402 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
403 EVP_PKEY *pkey = ktri->pkey;
404 unsigned char *ek = NULL;
407 CMS_EncryptedContentInfo *ec;
408 ec = cms->d.envelopedData->encryptedContentInfo;
410 if (ktri->pkey == NULL)
412 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
413 CMS_R_NO_PRIVATE_KEY);
417 ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
421 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
424 if (!cms_env_asn1_ctrl(ri, 1))
427 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
428 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0)
430 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
434 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
435 ktri->encryptedKey->data,
436 ktri->encryptedKey->length) <= 0)
439 ek = OPENSSL_malloc(eklen);
443 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
444 ERR_R_MALLOC_FAILURE);
448 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
449 ktri->encryptedKey->data,
450 ktri->encryptedKey->length) <= 0)
452 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
460 OPENSSL_cleanse(ec->key, ec->keylen);
461 OPENSSL_free(ec->key);
470 EVP_PKEY_CTX_free(ktri->pctx);
479 /* Key Encrypted Key (KEK) RecipientInfo routines */
481 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
482 const unsigned char *id, size_t idlen)
484 ASN1_OCTET_STRING tmp_os;
485 CMS_KEKRecipientInfo *kekri;
486 if (ri->type != CMS_RECIPINFO_KEK)
488 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
492 tmp_os.type = V_ASN1_OCTET_STRING;
494 tmp_os.data = (unsigned char *)id;
495 tmp_os.length = (int)idlen;
496 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
499 /* For now hard code AES key wrap info */
501 static size_t aes_wrap_keylen(int nid)
505 case NID_id_aes128_wrap:
508 case NID_id_aes192_wrap:
511 case NID_id_aes256_wrap:
519 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
520 unsigned char *key, size_t keylen,
521 unsigned char *id, size_t idlen,
522 ASN1_GENERALIZEDTIME *date,
523 ASN1_OBJECT *otherTypeId,
524 ASN1_TYPE *otherType)
526 CMS_RecipientInfo *ri = NULL;
527 CMS_EnvelopedData *env;
528 CMS_KEKRecipientInfo *kekri;
529 env = cms_get0_enveloped(cms);
533 if (nid == NID_undef)
538 nid = NID_id_aes128_wrap;
542 nid = NID_id_aes192_wrap;
546 nid = NID_id_aes256_wrap;
550 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
551 CMS_R_INVALID_KEY_LENGTH);
559 size_t exp_keylen = aes_wrap_keylen(nid);
563 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
564 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
568 if (keylen != exp_keylen)
570 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
571 CMS_R_INVALID_KEY_LENGTH);
577 /* Initialize recipient info */
578 ri = M_ASN1_new_of(CMS_RecipientInfo);
582 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
585 ri->type = CMS_RECIPINFO_KEK;
591 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
592 if (kekri->kekid->other == NULL)
596 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
600 /* After this point no calls can fail */
605 kekri->keylen = keylen;
607 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
609 kekri->kekid->date = date;
611 if (kekri->kekid->other)
613 kekri->kekid->other->keyAttrId = otherTypeId;
614 kekri->kekid->other->keyAttr = otherType;
617 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
618 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
623 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
626 M_ASN1_free_of(ri, CMS_RecipientInfo);
631 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
633 ASN1_OCTET_STRING **pid,
634 ASN1_GENERALIZEDTIME **pdate,
635 ASN1_OBJECT **potherid,
636 ASN1_TYPE **pothertype)
638 CMS_KEKIdentifier *rkid;
639 if (ri->type != CMS_RECIPINFO_KEK)
641 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
644 rkid = ri->d.kekri->kekid;
646 *palg = ri->d.kekri->keyEncryptionAlgorithm;
648 *pid = rkid->keyIdentifier;
654 *potherid = rkid->other->keyAttrId;
661 *pothertype = rkid->other->keyAttr;
668 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
669 unsigned char *key, size_t keylen)
671 CMS_KEKRecipientInfo *kekri;
672 if (ri->type != CMS_RECIPINFO_KEK)
674 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
680 kekri->keylen = keylen;
685 /* Encrypt content key in KEK recipient info */
687 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
688 CMS_RecipientInfo *ri)
690 CMS_EncryptedContentInfo *ec;
691 CMS_KEKRecipientInfo *kekri;
693 unsigned char *wkey = NULL;
697 ec = cms->d.envelopedData->encryptedContentInfo;
703 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
707 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx))
709 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
710 CMS_R_ERROR_SETTING_KEY);
714 wkey = OPENSSL_malloc(ec->keylen + 8);
718 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
719 ERR_R_MALLOC_FAILURE);
723 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
727 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
731 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
739 OPENSSL_cleanse(&actx, sizeof(actx));
745 /* Decrypt content key in KEK recipient info */
747 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
748 CMS_RecipientInfo *ri)
750 CMS_EncryptedContentInfo *ec;
751 CMS_KEKRecipientInfo *kekri;
753 unsigned char *ukey = NULL;
757 ec = cms->d.envelopedData->encryptedContentInfo;
763 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
767 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
768 if (aes_wrap_keylen(wrap_nid) != kekri->keylen)
770 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
771 CMS_R_INVALID_KEY_LENGTH);
775 /* If encrypted key length is invalid don't bother */
777 if (kekri->encryptedKey->length < 16)
779 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
780 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
784 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx))
786 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
787 CMS_R_ERROR_SETTING_KEY);
791 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
795 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
796 ERR_R_MALLOC_FAILURE);
800 ukeylen = AES_unwrap_key(&actx, NULL, ukey,
801 kekri->encryptedKey->data,
802 kekri->encryptedKey->length);
806 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
812 ec->keylen = ukeylen;
820 OPENSSL_cleanse(&actx, sizeof(actx));
826 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
830 case CMS_RECIPINFO_TRANS:
831 return cms_RecipientInfo_ktri_decrypt(cms, ri);
833 case CMS_RECIPINFO_KEK:
834 return cms_RecipientInfo_kekri_decrypt(cms, ri);
836 case CMS_RECIPINFO_PASS:
837 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
840 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
841 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
846 int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
850 case CMS_RECIPINFO_TRANS:
851 return cms_RecipientInfo_ktri_encrypt(cms, ri);
853 case CMS_RECIPINFO_KEK:
854 return cms_RecipientInfo_kekri_encrypt(cms, ri);
857 case CMS_RECIPINFO_PASS:
858 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
862 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
863 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
868 /* Check structures and fixup version numbers (if necessary) */
870 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
872 CMS_OriginatorInfo *org = env->originatorInfo;
876 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++)
878 CMS_CertificateChoices *cch;
879 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
880 if (cch->type == CMS_CERTCHOICE_OTHER)
885 else if (cch->type == CMS_CERTCHOICE_V2ACERT)
887 if (env->version < 3)
892 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++)
894 CMS_RevocationInfoChoice *rch;
895 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
896 if (rch->type == CMS_REVCHOICE_OTHER)
904 static void cms_env_set_version(CMS_EnvelopedData *env)
907 CMS_RecipientInfo *ri;
909 /* Can't set version higher than 4 so if 4 or more already nothing
912 if (env->version >= 4)
915 cms_env_set_originfo_version(env);
917 if (env->version >= 3)
920 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++)
922 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
923 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER)
928 else if (ri->type != CMS_RECIPINFO_TRANS)
933 if (env->version == 2)
935 if (env->originatorInfo || env->unprotectedAttrs)
940 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
942 CMS_EncryptedContentInfo *ec;
943 STACK_OF(CMS_RecipientInfo) *rinfos;
944 CMS_RecipientInfo *ri;
948 /* Get BIO first to set up key */
950 ec = cms->d.envelopedData->encryptedContentInfo;
951 ret = cms_EncryptedContent_init_bio(ec);
953 /* If error or no cipher end of processing */
955 if (!ret || !ec->cipher)
958 /* Now encrypt content key according to each RecipientInfo type */
960 rinfos = cms->d.envelopedData->recipientInfos;
962 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++)
964 ri = sk_CMS_RecipientInfo_value(rinfos, i);
965 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
967 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
968 CMS_R_ERROR_SETTING_RECIPIENTINFO);
972 cms_env_set_version(cms->d.envelopedData);
980 OPENSSL_cleanse(ec->key, ec->keylen);
981 OPENSSL_free(ec->key);