2 * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/pem.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
16 #include <openssl/aes.h>
17 #include "cms_local.h"
18 #include "crypto/asn1.h"
19 #include "crypto/evp.h"
21 /* CMS EnvelopedData Utilities */
23 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
25 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
26 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
27 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
30 return cms->d.envelopedData;
33 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
35 if (cms->d.other == NULL) {
36 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
37 if (!cms->d.envelopedData) {
38 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
41 cms->d.envelopedData->version = 0;
42 cms->d.envelopedData->encryptedContentInfo->contentType =
43 OBJ_nid2obj(NID_pkcs7_data);
44 ASN1_OBJECT_free(cms->contentType);
45 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
46 return cms->d.envelopedData;
48 return cms_get0_enveloped(cms);
51 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
55 if (ri->type == CMS_RECIPINFO_TRANS)
56 pkey = ri->d.ktri->pkey;
57 else if (ri->type == CMS_RECIPINFO_AGREE) {
58 EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
62 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
67 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
69 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
71 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
72 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
76 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
82 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
84 CMS_EnvelopedData *env;
85 env = cms_get0_enveloped(cms);
88 return env->recipientInfos;
91 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
96 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
98 if (ri->type == CMS_RECIPINFO_TRANS)
99 return ri->d.ktri->pctx;
100 else if (ri->type == CMS_RECIPINFO_AGREE)
101 return ri->d.kari->pctx;
105 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
107 CMS_ContentInfo *cms;
108 CMS_EnvelopedData *env;
109 cms = CMS_ContentInfo_new();
112 env = cms_enveloped_data_init(cms);
115 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
120 CMS_ContentInfo_free(cms);
121 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
125 /* Key Transport Recipient Info (KTRI) routines */
127 /* Initialise a ktri based on passed certificate and key */
129 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
130 EVP_PKEY *pk, unsigned int flags)
132 CMS_KeyTransRecipientInfo *ktri;
135 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
138 ri->type = CMS_RECIPINFO_TRANS;
142 if (flags & CMS_USE_KEYID) {
144 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
147 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
151 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
155 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype))
164 if (flags & CMS_KEY_PARAM) {
165 ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
166 if (ktri->pctx == NULL)
168 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
170 } else if (!cms_env_asn1_ctrl(ri, 0))
176 * Add a recipient certificate using appropriate type of RecipientInfo
179 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
180 X509 *recip, unsigned int flags)
182 CMS_RecipientInfo *ri = NULL;
183 CMS_EnvelopedData *env;
185 env = cms_get0_enveloped(cms);
189 /* Initialize recipient info */
190 ri = M_ASN1_new_of(CMS_RecipientInfo);
194 pk = X509_get0_pubkey(recip);
196 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_ERROR_GETTING_PUBLIC_KEY);
200 switch (cms_pkey_get_ri_type(pk)) {
202 case CMS_RECIPINFO_TRANS:
203 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags))
207 case CMS_RECIPINFO_AGREE:
208 if (!cms_RecipientInfo_kari_init(ri, recip, pk, flags))
213 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
214 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
219 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
225 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
227 M_ASN1_free_of(ri, CMS_RecipientInfo);
232 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
233 EVP_PKEY **pk, X509 **recip,
236 CMS_KeyTransRecipientInfo *ktri;
237 if (ri->type != CMS_RECIPINFO_TRANS) {
238 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
239 CMS_R_NOT_KEY_TRANSPORT);
248 *recip = ktri->recip;
250 *palg = ktri->keyEncryptionAlgorithm;
254 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
255 ASN1_OCTET_STRING **keyid,
259 CMS_KeyTransRecipientInfo *ktri;
260 if (ri->type != CMS_RECIPINFO_TRANS) {
261 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
262 CMS_R_NOT_KEY_TRANSPORT);
267 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
270 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
272 if (ri->type != CMS_RECIPINFO_TRANS) {
273 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
274 CMS_R_NOT_KEY_TRANSPORT);
277 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
280 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
282 if (ri->type != CMS_RECIPINFO_TRANS) {
283 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
286 EVP_PKEY_free(ri->d.ktri->pkey);
287 ri->d.ktri->pkey = pkey;
291 /* Encrypt content key in key transport recipient info */
293 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
294 CMS_RecipientInfo *ri)
296 CMS_KeyTransRecipientInfo *ktri;
297 CMS_EncryptedContentInfo *ec;
299 unsigned char *ek = NULL;
304 if (ri->type != CMS_RECIPINFO_TRANS) {
305 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT);
309 ec = cms->d.envelopedData->encryptedContentInfo;
314 if (!cms_env_asn1_ctrl(ri, 0))
317 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
321 if (EVP_PKEY_encrypt_init(pctx) <= 0)
325 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
326 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
327 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
331 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
334 ek = OPENSSL_malloc(eklen);
337 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
341 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
344 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
350 EVP_PKEY_CTX_free(pctx);
357 /* Decrypt content key from KTRI */
359 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
360 CMS_RecipientInfo *ri)
362 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
363 EVP_PKEY *pkey = ktri->pkey;
364 unsigned char *ek = NULL;
368 CMS_EncryptedContentInfo *ec;
369 ec = cms->d.envelopedData->encryptedContentInfo;
371 if (ktri->pkey == NULL) {
372 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
376 if (cms->d.envelopedData->encryptedContentInfo->havenocert
377 && !cms->d.envelopedData->encryptedContentInfo->debug) {
378 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
379 const EVP_CIPHER *ciph = EVP_get_cipherbyobj(calg->algorithm);
382 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_UNKNOWN_CIPHER);
386 fixlen = EVP_CIPHER_key_length(ciph);
389 ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
390 if (ktri->pctx == NULL)
393 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
396 if (!cms_env_asn1_ctrl(ri, 1))
399 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
400 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
401 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
405 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
406 ktri->encryptedKey->data,
407 ktri->encryptedKey->length) <= 0)
410 ek = OPENSSL_malloc(eklen);
413 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
417 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
418 ktri->encryptedKey->data,
419 ktri->encryptedKey->length) <= 0
421 || (fixlen != 0 && eklen != fixlen)) {
422 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
428 OPENSSL_clear_free(ec->key, ec->keylen);
433 EVP_PKEY_CTX_free(ktri->pctx);
441 /* Key Encrypted Key (KEK) RecipientInfo routines */
443 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
444 const unsigned char *id, size_t idlen)
446 ASN1_OCTET_STRING tmp_os;
447 CMS_KEKRecipientInfo *kekri;
448 if (ri->type != CMS_RECIPINFO_KEK) {
449 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
453 tmp_os.type = V_ASN1_OCTET_STRING;
455 tmp_os.data = (unsigned char *)id;
456 tmp_os.length = (int)idlen;
457 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
460 /* For now hard code AES key wrap info */
462 static size_t aes_wrap_keylen(int nid)
465 case NID_id_aes128_wrap:
468 case NID_id_aes192_wrap:
471 case NID_id_aes256_wrap:
479 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
480 unsigned char *key, size_t keylen,
481 unsigned char *id, size_t idlen,
482 ASN1_GENERALIZEDTIME *date,
483 ASN1_OBJECT *otherTypeId,
484 ASN1_TYPE *otherType)
486 CMS_RecipientInfo *ri = NULL;
487 CMS_EnvelopedData *env;
488 CMS_KEKRecipientInfo *kekri;
489 env = cms_get0_enveloped(cms);
493 if (nid == NID_undef) {
496 nid = NID_id_aes128_wrap;
500 nid = NID_id_aes192_wrap;
504 nid = NID_id_aes256_wrap;
508 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
514 size_t exp_keylen = aes_wrap_keylen(nid);
517 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
518 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
522 if (keylen != exp_keylen) {
523 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
529 /* Initialize recipient info */
530 ri = M_ASN1_new_of(CMS_RecipientInfo);
534 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
537 ri->type = CMS_RECIPINFO_KEK;
542 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
543 if (kekri->kekid->other == NULL)
547 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
550 /* After this point no calls can fail */
555 kekri->keylen = keylen;
557 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
559 kekri->kekid->date = date;
561 if (kekri->kekid->other) {
562 kekri->kekid->other->keyAttrId = otherTypeId;
563 kekri->kekid->other->keyAttr = otherType;
566 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
567 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
572 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
574 M_ASN1_free_of(ri, CMS_RecipientInfo);
579 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
581 ASN1_OCTET_STRING **pid,
582 ASN1_GENERALIZEDTIME **pdate,
583 ASN1_OBJECT **potherid,
584 ASN1_TYPE **pothertype)
586 CMS_KEKIdentifier *rkid;
587 if (ri->type != CMS_RECIPINFO_KEK) {
588 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
591 rkid = ri->d.kekri->kekid;
593 *palg = ri->d.kekri->keyEncryptionAlgorithm;
595 *pid = rkid->keyIdentifier;
600 *potherid = rkid->other->keyAttrId;
606 *pothertype = rkid->other->keyAttr;
613 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
614 unsigned char *key, size_t keylen)
616 CMS_KEKRecipientInfo *kekri;
617 if (ri->type != CMS_RECIPINFO_KEK) {
618 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
624 kekri->keylen = keylen;
628 /* Encrypt content key in KEK recipient info */
630 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
631 CMS_RecipientInfo *ri)
633 CMS_EncryptedContentInfo *ec;
634 CMS_KEKRecipientInfo *kekri;
636 unsigned char *wkey = NULL;
640 ec = cms->d.envelopedData->encryptedContentInfo;
645 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
649 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
650 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
651 CMS_R_ERROR_SETTING_KEY);
655 wkey = OPENSSL_malloc(ec->keylen + 8);
658 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
662 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
665 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
669 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
677 OPENSSL_cleanse(&actx, sizeof(actx));
683 /* Decrypt content key in KEK recipient info */
685 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
686 CMS_RecipientInfo *ri)
688 CMS_EncryptedContentInfo *ec;
689 CMS_KEKRecipientInfo *kekri;
691 unsigned char *ukey = NULL;
695 ec = cms->d.envelopedData->encryptedContentInfo;
700 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
704 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
705 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
706 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
707 CMS_R_INVALID_KEY_LENGTH);
711 /* If encrypted key length is invalid don't bother */
713 if (kekri->encryptedKey->length < 16) {
714 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
715 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
719 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
720 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
721 CMS_R_ERROR_SETTING_KEY);
725 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
728 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
732 ukeylen = AES_unwrap_key(&actx, NULL, ukey,
733 kekri->encryptedKey->data,
734 kekri->encryptedKey->length);
737 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
742 ec->keylen = ukeylen;
750 OPENSSL_cleanse(&actx, sizeof(actx));
756 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
759 case CMS_RECIPINFO_TRANS:
760 return cms_RecipientInfo_ktri_decrypt(cms, ri);
762 case CMS_RECIPINFO_KEK:
763 return cms_RecipientInfo_kekri_decrypt(cms, ri);
765 case CMS_RECIPINFO_PASS:
766 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
769 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
770 CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
775 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
778 case CMS_RECIPINFO_TRANS:
779 return cms_RecipientInfo_ktri_encrypt(cms, ri);
781 case CMS_RECIPINFO_AGREE:
782 return cms_RecipientInfo_kari_encrypt(cms, ri);
784 case CMS_RECIPINFO_KEK:
785 return cms_RecipientInfo_kekri_encrypt(cms, ri);
787 case CMS_RECIPINFO_PASS:
788 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
791 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
792 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
797 /* Check structures and fixup version numbers (if necessary) */
799 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
801 CMS_OriginatorInfo *org = env->originatorInfo;
805 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
806 CMS_CertificateChoices *cch;
807 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
808 if (cch->type == CMS_CERTCHOICE_OTHER) {
811 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
812 if (env->version < 3)
817 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
818 CMS_RevocationInfoChoice *rch;
819 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
820 if (rch->type == CMS_REVCHOICE_OTHER) {
827 static void cms_env_set_version(CMS_EnvelopedData *env)
830 CMS_RecipientInfo *ri;
833 * Can't set version higher than 4 so if 4 or more already nothing to do.
835 if (env->version >= 4)
838 cms_env_set_originfo_version(env);
840 if (env->version >= 3)
843 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
844 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
845 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
848 } else if (ri->type != CMS_RECIPINFO_TRANS
849 || ri->d.ktri->version != 0) {
853 if (env->originatorInfo || env->unprotectedAttrs)
855 if (env->version == 2)
860 BIO *cms_EnvelopedData_init_bio(const CMS_ContentInfo *cms)
862 CMS_EncryptedContentInfo *ec;
863 STACK_OF(CMS_RecipientInfo) *rinfos;
864 CMS_RecipientInfo *ri;
868 /* Get BIO first to set up key */
870 ec = cms->d.envelopedData->encryptedContentInfo;
871 ret = cms_EncryptedContent_init_bio(ec);
873 /* If error or no cipher end of processing */
875 if (!ret || !ec->cipher)
878 /* Now encrypt content key according to each RecipientInfo type */
880 rinfos = cms->d.envelopedData->recipientInfos;
882 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
883 ri = sk_CMS_RecipientInfo_value(rinfos, i);
884 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
885 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
886 CMS_R_ERROR_SETTING_RECIPIENTINFO);
890 cms_env_set_version(cms->d.envelopedData);
896 OPENSSL_clear_free(ec->key, ec->keylen);
907 * Get RecipientInfo type (if any) supported by a key (public or private). To
908 * retain compatibility with previous behaviour if the ctrl value isn't
909 * supported we assume key transport.
911 int cms_pkey_get_ri_type(EVP_PKEY *pk)
913 if (pk->ameth && pk->ameth->pkey_ctrl) {
915 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
919 return CMS_RECIPINFO_TRANS;