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 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
110 if (ri->type == CMS_RECIPINFO_TRANS)
111 pkey = ri->d.ktri->pkey;
112 else if (ri->type == CMS_RECIPINFO_AGREE)
114 EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
117 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
123 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
125 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
128 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
129 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
134 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
140 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
142 CMS_EnvelopedData *env;
143 env = cms_get0_enveloped(cms);
146 return env->recipientInfos;
149 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
154 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
156 if (ri->type == CMS_RECIPINFO_TRANS)
157 return ri->d.ktri->pctx;
158 else if (ri->type == CMS_RECIPINFO_AGREE)
159 return ri->d.kari->pctx;
163 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
165 CMS_ContentInfo *cms;
166 CMS_EnvelopedData *env;
167 cms = CMS_ContentInfo_new();
170 env = cms_enveloped_data_init(cms);
173 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
179 CMS_ContentInfo_free(cms);
180 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
184 /* Key Transport Recipient Info (KTRI) routines */
186 /* Initialise a ktri based on passed certificate and key */
188 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
189 EVP_PKEY *pk, unsigned int flags)
191 CMS_KeyTransRecipientInfo *ktri;
194 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
197 ri->type = CMS_RECIPINFO_TRANS;
201 if (flags & CMS_USE_KEYID)
204 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
209 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
212 /* Not a typo: RecipientIdentifier and SignerIdentifier are the
216 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype))
219 CRYPTO_add(&recip->references, 1, CRYPTO_LOCK_X509);
220 CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
224 if (flags & CMS_KEY_PARAM)
226 ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
229 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
232 else if (!cms_env_asn1_ctrl(ri, 0))
237 /* Add a recipient certificate using appropriate type of RecipientInfo
240 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
241 X509 *recip, unsigned int flags)
243 CMS_RecipientInfo *ri = NULL;
244 CMS_EnvelopedData *env;
246 env = cms_get0_enveloped(cms);
250 /* Initialize recipient info */
251 ri = M_ASN1_new_of(CMS_RecipientInfo);
255 pk = X509_get_pubkey(recip);
258 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
259 CMS_R_ERROR_GETTING_PUBLIC_KEY);
263 switch (cms_pkey_get_ri_type(pk))
266 case CMS_RECIPINFO_TRANS:
267 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags))
271 case CMS_RECIPINFO_AGREE:
272 if (!cms_RecipientInfo_kari_init(ri, recip, pk, flags))
277 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
278 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
283 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
291 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
294 M_ASN1_free_of(ri, CMS_RecipientInfo);
301 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
302 EVP_PKEY **pk, X509 **recip,
305 CMS_KeyTransRecipientInfo *ktri;
306 if (ri->type != CMS_RECIPINFO_TRANS)
308 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
309 CMS_R_NOT_KEY_TRANSPORT);
318 *recip = ktri->recip;
320 *palg = ktri->keyEncryptionAlgorithm;
324 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
325 ASN1_OCTET_STRING **keyid,
326 X509_NAME **issuer, ASN1_INTEGER **sno)
328 CMS_KeyTransRecipientInfo *ktri;
329 if (ri->type != CMS_RECIPINFO_TRANS)
331 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
332 CMS_R_NOT_KEY_TRANSPORT);
337 return cms_SignerIdentifier_get0_signer_id(ktri->rid,
341 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
343 if (ri->type != CMS_RECIPINFO_TRANS)
345 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
346 CMS_R_NOT_KEY_TRANSPORT);
349 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
352 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
354 if (ri->type != CMS_RECIPINFO_TRANS)
356 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY,
357 CMS_R_NOT_KEY_TRANSPORT);
360 ri->d.ktri->pkey = pkey;
364 /* Encrypt content key in key transport recipient info */
366 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
367 CMS_RecipientInfo *ri)
369 CMS_KeyTransRecipientInfo *ktri;
370 CMS_EncryptedContentInfo *ec;
372 unsigned char *ek = NULL;
377 if (ri->type != CMS_RECIPINFO_TRANS)
379 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
380 CMS_R_NOT_KEY_TRANSPORT);
384 ec = cms->d.envelopedData->encryptedContentInfo;
390 if (!cms_env_asn1_ctrl(ri, 0))
395 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
399 if (EVP_PKEY_encrypt_init(pctx) <= 0)
403 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
404 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0)
406 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
410 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
413 ek = OPENSSL_malloc(eklen);
417 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
418 ERR_R_MALLOC_FAILURE);
422 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
425 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
433 EVP_PKEY_CTX_free(pctx);
442 /* Decrypt content key from KTRI */
444 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
445 CMS_RecipientInfo *ri)
447 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
448 EVP_PKEY *pkey = ktri->pkey;
449 unsigned char *ek = NULL;
452 CMS_EncryptedContentInfo *ec;
453 ec = cms->d.envelopedData->encryptedContentInfo;
455 if (ktri->pkey == NULL)
457 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
458 CMS_R_NO_PRIVATE_KEY);
462 ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
466 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
469 if (!cms_env_asn1_ctrl(ri, 1))
472 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
473 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0)
475 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
479 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
480 ktri->encryptedKey->data,
481 ktri->encryptedKey->length) <= 0)
484 ek = OPENSSL_malloc(eklen);
488 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
489 ERR_R_MALLOC_FAILURE);
493 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
494 ktri->encryptedKey->data,
495 ktri->encryptedKey->length) <= 0)
497 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
505 OPENSSL_cleanse(ec->key, ec->keylen);
506 OPENSSL_free(ec->key);
515 EVP_PKEY_CTX_free(ktri->pctx);
524 /* Key Encrypted Key (KEK) RecipientInfo routines */
526 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
527 const unsigned char *id, size_t idlen)
529 ASN1_OCTET_STRING tmp_os;
530 CMS_KEKRecipientInfo *kekri;
531 if (ri->type != CMS_RECIPINFO_KEK)
533 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
537 tmp_os.type = V_ASN1_OCTET_STRING;
539 tmp_os.data = (unsigned char *)id;
540 tmp_os.length = (int)idlen;
541 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
544 /* For now hard code AES key wrap info */
546 static size_t aes_wrap_keylen(int nid)
550 case NID_id_aes128_wrap:
553 case NID_id_aes192_wrap:
556 case NID_id_aes256_wrap:
564 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
565 unsigned char *key, size_t keylen,
566 unsigned char *id, size_t idlen,
567 ASN1_GENERALIZEDTIME *date,
568 ASN1_OBJECT *otherTypeId,
569 ASN1_TYPE *otherType)
571 CMS_RecipientInfo *ri = NULL;
572 CMS_EnvelopedData *env;
573 CMS_KEKRecipientInfo *kekri;
574 env = cms_get0_enveloped(cms);
578 if (nid == NID_undef)
583 nid = NID_id_aes128_wrap;
587 nid = NID_id_aes192_wrap;
591 nid = NID_id_aes256_wrap;
595 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
596 CMS_R_INVALID_KEY_LENGTH);
604 size_t exp_keylen = aes_wrap_keylen(nid);
608 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
609 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
613 if (keylen != exp_keylen)
615 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
616 CMS_R_INVALID_KEY_LENGTH);
622 /* Initialize recipient info */
623 ri = M_ASN1_new_of(CMS_RecipientInfo);
627 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
630 ri->type = CMS_RECIPINFO_KEK;
636 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
637 if (kekri->kekid->other == NULL)
641 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
645 /* After this point no calls can fail */
650 kekri->keylen = keylen;
652 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
654 kekri->kekid->date = date;
656 if (kekri->kekid->other)
658 kekri->kekid->other->keyAttrId = otherTypeId;
659 kekri->kekid->other->keyAttr = otherType;
662 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
663 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
668 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
671 M_ASN1_free_of(ri, CMS_RecipientInfo);
676 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
678 ASN1_OCTET_STRING **pid,
679 ASN1_GENERALIZEDTIME **pdate,
680 ASN1_OBJECT **potherid,
681 ASN1_TYPE **pothertype)
683 CMS_KEKIdentifier *rkid;
684 if (ri->type != CMS_RECIPINFO_KEK)
686 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
689 rkid = ri->d.kekri->kekid;
691 *palg = ri->d.kekri->keyEncryptionAlgorithm;
693 *pid = rkid->keyIdentifier;
699 *potherid = rkid->other->keyAttrId;
706 *pothertype = rkid->other->keyAttr;
713 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
714 unsigned char *key, size_t keylen)
716 CMS_KEKRecipientInfo *kekri;
717 if (ri->type != CMS_RECIPINFO_KEK)
719 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
725 kekri->keylen = keylen;
730 /* Encrypt content key in KEK recipient info */
732 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
733 CMS_RecipientInfo *ri)
735 CMS_EncryptedContentInfo *ec;
736 CMS_KEKRecipientInfo *kekri;
738 unsigned char *wkey = NULL;
742 ec = cms->d.envelopedData->encryptedContentInfo;
748 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
752 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx))
754 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
755 CMS_R_ERROR_SETTING_KEY);
759 wkey = OPENSSL_malloc(ec->keylen + 8);
763 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
764 ERR_R_MALLOC_FAILURE);
768 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
772 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
776 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
784 OPENSSL_cleanse(&actx, sizeof(actx));
790 /* Decrypt content key in KEK recipient info */
792 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
793 CMS_RecipientInfo *ri)
795 CMS_EncryptedContentInfo *ec;
796 CMS_KEKRecipientInfo *kekri;
798 unsigned char *ukey = NULL;
802 ec = cms->d.envelopedData->encryptedContentInfo;
808 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
812 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
813 if (aes_wrap_keylen(wrap_nid) != kekri->keylen)
815 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
816 CMS_R_INVALID_KEY_LENGTH);
820 /* If encrypted key length is invalid don't bother */
822 if (kekri->encryptedKey->length < 16)
824 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
825 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
829 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx))
831 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
832 CMS_R_ERROR_SETTING_KEY);
836 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
840 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
841 ERR_R_MALLOC_FAILURE);
845 ukeylen = AES_unwrap_key(&actx, NULL, ukey,
846 kekri->encryptedKey->data,
847 kekri->encryptedKey->length);
851 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
857 ec->keylen = ukeylen;
865 OPENSSL_cleanse(&actx, sizeof(actx));
871 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
875 case CMS_RECIPINFO_TRANS:
876 return cms_RecipientInfo_ktri_decrypt(cms, ri);
878 case CMS_RECIPINFO_KEK:
879 return cms_RecipientInfo_kekri_decrypt(cms, ri);
881 case CMS_RECIPINFO_PASS:
882 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
885 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
886 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
891 int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
895 case CMS_RECIPINFO_TRANS:
896 return cms_RecipientInfo_ktri_encrypt(cms, ri);
898 case CMS_RECIPINFO_AGREE:
899 return cms_RecipientInfo_kari_encrypt(cms, ri);
901 case CMS_RECIPINFO_KEK:
902 return cms_RecipientInfo_kekri_encrypt(cms, ri);
905 case CMS_RECIPINFO_PASS:
906 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
910 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
911 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
916 /* Check structures and fixup version numbers (if necessary) */
918 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
920 CMS_OriginatorInfo *org = env->originatorInfo;
924 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++)
926 CMS_CertificateChoices *cch;
927 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
928 if (cch->type == CMS_CERTCHOICE_OTHER)
933 else if (cch->type == CMS_CERTCHOICE_V2ACERT)
935 if (env->version < 3)
940 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++)
942 CMS_RevocationInfoChoice *rch;
943 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
944 if (rch->type == CMS_REVCHOICE_OTHER)
952 static void cms_env_set_version(CMS_EnvelopedData *env)
955 CMS_RecipientInfo *ri;
957 /* Can't set version higher than 4 so if 4 or more already nothing
960 if (env->version >= 4)
963 cms_env_set_originfo_version(env);
965 if (env->version >= 3)
968 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++)
970 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
971 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER)
976 else if (ri->type != CMS_RECIPINFO_TRANS || ri->d.ktri->version != 0)
981 if (env->version == 2)
983 if (env->originatorInfo || env->unprotectedAttrs)
988 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
990 CMS_EncryptedContentInfo *ec;
991 STACK_OF(CMS_RecipientInfo) *rinfos;
992 CMS_RecipientInfo *ri;
996 /* Get BIO first to set up key */
998 ec = cms->d.envelopedData->encryptedContentInfo;
999 ret = cms_EncryptedContent_init_bio(ec);
1001 /* If error or no cipher end of processing */
1003 if (!ret || !ec->cipher)
1006 /* Now encrypt content key according to each RecipientInfo type */
1008 rinfos = cms->d.envelopedData->recipientInfos;
1010 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++)
1012 ri = sk_CMS_RecipientInfo_value(rinfos, i);
1013 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
1015 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
1016 CMS_R_ERROR_SETTING_RECIPIENTINFO);
1020 cms_env_set_version(cms->d.envelopedData);
1028 OPENSSL_cleanse(ec->key, ec->keylen);
1029 OPENSSL_free(ec->key);
1039 /* Get RecipientInfo type (if any) supported by a key (public or private).
1040 * To retain compatibility with previous behaviour if the ctrl value isn't
1041 * supported we assume key transport.
1043 int cms_pkey_get_ri_type(EVP_PKEY *pk)
1045 if (pk->ameth && pk->ameth->pkey_ctrl)
1048 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1052 return CMS_RECIPINFO_TRANS;