2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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/rand.h>
17 #include <openssl/aes.h>
19 #include "internal/asn1_int.h"
20 #include "internal/evp_int.h"
22 /* CMS EnvelopedData Utilities */
24 CMS_EnvelopedData
*cms_get0_enveloped(CMS_ContentInfo
*cms
)
26 if (OBJ_obj2nid(cms
->contentType
) != NID_pkcs7_enveloped
) {
27 CMSerr(CMS_F_CMS_GET0_ENVELOPED
,
28 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA
);
31 return cms
->d
.envelopedData
;
34 static CMS_EnvelopedData
*cms_enveloped_data_init(CMS_ContentInfo
*cms
)
36 if (cms
->d
.other
== NULL
) {
37 cms
->d
.envelopedData
= M_ASN1_new_of(CMS_EnvelopedData
);
38 if (!cms
->d
.envelopedData
) {
39 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT
, ERR_R_MALLOC_FAILURE
);
42 cms
->d
.envelopedData
->version
= 0;
43 cms
->d
.envelopedData
->encryptedContentInfo
->contentType
=
44 OBJ_nid2obj(NID_pkcs7_data
);
45 ASN1_OBJECT_free(cms
->contentType
);
46 cms
->contentType
= OBJ_nid2obj(NID_pkcs7_enveloped
);
47 return cms
->d
.envelopedData
;
49 return cms_get0_enveloped(cms
);
52 int cms_env_asn1_ctrl(CMS_RecipientInfo
*ri
, int cmd
)
56 if (ri
->type
== CMS_RECIPINFO_TRANS
)
57 pkey
= ri
->d
.ktri
->pkey
;
58 else if (ri
->type
== CMS_RECIPINFO_AGREE
) {
59 EVP_PKEY_CTX
*pctx
= ri
->d
.kari
->pctx
;
62 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
67 if (!pkey
->ameth
|| !pkey
->ameth
->pkey_ctrl
)
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 ri
->d
.ktri
->pkey
= pkey
;
290 /* Encrypt content key in key transport recipient info */
292 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo
*cms
,
293 CMS_RecipientInfo
*ri
)
295 CMS_KeyTransRecipientInfo
*ktri
;
296 CMS_EncryptedContentInfo
*ec
;
298 unsigned char *ek
= NULL
;
303 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
304 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT
, CMS_R_NOT_KEY_TRANSPORT
);
308 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
313 if (!cms_env_asn1_ctrl(ri
, 0))
316 pctx
= EVP_PKEY_CTX_new(ktri
->pkey
, NULL
);
320 if (EVP_PKEY_encrypt_init(pctx
) <= 0)
324 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_ENCRYPT
,
325 EVP_PKEY_CTRL_CMS_ENCRYPT
, 0, ri
) <= 0) {
326 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT
, CMS_R_CTRL_ERROR
);
330 if (EVP_PKEY_encrypt(pctx
, NULL
, &eklen
, ec
->key
, ec
->keylen
) <= 0)
333 ek
= OPENSSL_malloc(eklen
);
336 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
340 if (EVP_PKEY_encrypt(pctx
, ek
, &eklen
, ec
->key
, ec
->keylen
) <= 0)
343 ASN1_STRING_set0(ktri
->encryptedKey
, ek
, eklen
);
349 EVP_PKEY_CTX_free(pctx
);
356 /* Decrypt content key from KTRI */
358 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo
*cms
,
359 CMS_RecipientInfo
*ri
)
361 CMS_KeyTransRecipientInfo
*ktri
= ri
->d
.ktri
;
362 EVP_PKEY
*pkey
= ktri
->pkey
;
363 unsigned char *ek
= NULL
;
366 CMS_EncryptedContentInfo
*ec
;
367 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
369 if (ktri
->pkey
== NULL
) {
370 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT
, CMS_R_NO_PRIVATE_KEY
);
374 ktri
->pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
375 if (ktri
->pctx
== NULL
)
378 if (EVP_PKEY_decrypt_init(ktri
->pctx
) <= 0)
381 if (!cms_env_asn1_ctrl(ri
, 1))
384 if (EVP_PKEY_CTX_ctrl(ktri
->pctx
, -1, EVP_PKEY_OP_DECRYPT
,
385 EVP_PKEY_CTRL_CMS_DECRYPT
, 0, ri
) <= 0) {
386 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT
, CMS_R_CTRL_ERROR
);
390 if (EVP_PKEY_decrypt(ktri
->pctx
, NULL
, &eklen
,
391 ktri
->encryptedKey
->data
,
392 ktri
->encryptedKey
->length
) <= 0)
395 ek
= OPENSSL_malloc(eklen
);
398 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT
, ERR_R_MALLOC_FAILURE
);
402 if (EVP_PKEY_decrypt(ktri
->pctx
, ek
, &eklen
,
403 ktri
->encryptedKey
->data
,
404 ktri
->encryptedKey
->length
) <= 0) {
405 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT
, CMS_R_CMS_LIB
);
411 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
416 EVP_PKEY_CTX_free(ktri
->pctx
);
424 /* Key Encrypted Key (KEK) RecipientInfo routines */
426 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo
*ri
,
427 const unsigned char *id
, size_t idlen
)
429 ASN1_OCTET_STRING tmp_os
;
430 CMS_KEKRecipientInfo
*kekri
;
431 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
432 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP
, CMS_R_NOT_KEK
);
436 tmp_os
.type
= V_ASN1_OCTET_STRING
;
438 tmp_os
.data
= (unsigned char *)id
;
439 tmp_os
.length
= (int)idlen
;
440 return ASN1_OCTET_STRING_cmp(&tmp_os
, kekri
->kekid
->keyIdentifier
);
443 /* For now hard code AES key wrap info */
445 static size_t aes_wrap_keylen(int nid
)
448 case NID_id_aes128_wrap
:
451 case NID_id_aes192_wrap
:
454 case NID_id_aes256_wrap
:
462 CMS_RecipientInfo
*CMS_add0_recipient_key(CMS_ContentInfo
*cms
, int nid
,
463 unsigned char *key
, size_t keylen
,
464 unsigned char *id
, size_t idlen
,
465 ASN1_GENERALIZEDTIME
*date
,
466 ASN1_OBJECT
*otherTypeId
,
467 ASN1_TYPE
*otherType
)
469 CMS_RecipientInfo
*ri
= NULL
;
470 CMS_EnvelopedData
*env
;
471 CMS_KEKRecipientInfo
*kekri
;
472 env
= cms_get0_enveloped(cms
);
476 if (nid
== NID_undef
) {
479 nid
= NID_id_aes128_wrap
;
483 nid
= NID_id_aes192_wrap
;
487 nid
= NID_id_aes256_wrap
;
491 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY
, CMS_R_INVALID_KEY_LENGTH
);
497 size_t exp_keylen
= aes_wrap_keylen(nid
);
500 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY
,
501 CMS_R_UNSUPPORTED_KEK_ALGORITHM
);
505 if (keylen
!= exp_keylen
) {
506 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY
, CMS_R_INVALID_KEY_LENGTH
);
512 /* Initialize recipient info */
513 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
517 ri
->d
.kekri
= M_ASN1_new_of(CMS_KEKRecipientInfo
);
520 ri
->type
= CMS_RECIPINFO_KEK
;
525 kekri
->kekid
->other
= M_ASN1_new_of(CMS_OtherKeyAttribute
);
526 if (kekri
->kekid
->other
== NULL
)
530 if (!sk_CMS_RecipientInfo_push(env
->recipientInfos
, ri
))
533 /* After this point no calls can fail */
538 kekri
->keylen
= keylen
;
540 ASN1_STRING_set0(kekri
->kekid
->keyIdentifier
, id
, idlen
);
542 kekri
->kekid
->date
= date
;
544 if (kekri
->kekid
->other
) {
545 kekri
->kekid
->other
->keyAttrId
= otherTypeId
;
546 kekri
->kekid
->other
->keyAttr
= otherType
;
549 X509_ALGOR_set0(kekri
->keyEncryptionAlgorithm
,
550 OBJ_nid2obj(nid
), V_ASN1_UNDEF
, NULL
);
555 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY
, ERR_R_MALLOC_FAILURE
);
557 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
562 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo
*ri
,
564 ASN1_OCTET_STRING
**pid
,
565 ASN1_GENERALIZEDTIME
**pdate
,
566 ASN1_OBJECT
**potherid
,
567 ASN1_TYPE
**pothertype
)
569 CMS_KEKIdentifier
*rkid
;
570 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
571 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID
, CMS_R_NOT_KEK
);
574 rkid
= ri
->d
.kekri
->kekid
;
576 *palg
= ri
->d
.kekri
->keyEncryptionAlgorithm
;
578 *pid
= rkid
->keyIdentifier
;
583 *potherid
= rkid
->other
->keyAttrId
;
589 *pothertype
= rkid
->other
->keyAttr
;
596 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo
*ri
,
597 unsigned char *key
, size_t keylen
)
599 CMS_KEKRecipientInfo
*kekri
;
600 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
601 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY
, CMS_R_NOT_KEK
);
607 kekri
->keylen
= keylen
;
611 /* Encrypt content key in KEK recipient info */
613 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo
*cms
,
614 CMS_RecipientInfo
*ri
)
616 CMS_EncryptedContentInfo
*ec
;
617 CMS_KEKRecipientInfo
*kekri
;
619 unsigned char *wkey
= NULL
;
623 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
628 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT
, CMS_R_NO_KEY
);
632 if (AES_set_encrypt_key(kekri
->key
, kekri
->keylen
<< 3, &actx
)) {
633 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT
,
634 CMS_R_ERROR_SETTING_KEY
);
638 wkey
= OPENSSL_malloc(ec
->keylen
+ 8);
641 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
645 wkeylen
= AES_wrap_key(&actx
, NULL
, wkey
, ec
->key
, ec
->keylen
);
648 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT
, CMS_R_WRAP_ERROR
);
652 ASN1_STRING_set0(kekri
->encryptedKey
, wkey
, wkeylen
);
660 OPENSSL_cleanse(&actx
, sizeof(actx
));
666 /* Decrypt content key in KEK recipient info */
668 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo
*cms
,
669 CMS_RecipientInfo
*ri
)
671 CMS_EncryptedContentInfo
*ec
;
672 CMS_KEKRecipientInfo
*kekri
;
674 unsigned char *ukey
= NULL
;
678 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
683 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
, CMS_R_NO_KEY
);
687 wrap_nid
= OBJ_obj2nid(kekri
->keyEncryptionAlgorithm
->algorithm
);
688 if (aes_wrap_keylen(wrap_nid
) != kekri
->keylen
) {
689 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
,
690 CMS_R_INVALID_KEY_LENGTH
);
694 /* If encrypted key length is invalid don't bother */
696 if (kekri
->encryptedKey
->length
< 16) {
697 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
,
698 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH
);
702 if (AES_set_decrypt_key(kekri
->key
, kekri
->keylen
<< 3, &actx
)) {
703 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
,
704 CMS_R_ERROR_SETTING_KEY
);
708 ukey
= OPENSSL_malloc(kekri
->encryptedKey
->length
- 8);
711 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
, ERR_R_MALLOC_FAILURE
);
715 ukeylen
= AES_unwrap_key(&actx
, NULL
, ukey
,
716 kekri
->encryptedKey
->data
,
717 kekri
->encryptedKey
->length
);
720 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
, CMS_R_UNWRAP_ERROR
);
725 ec
->keylen
= ukeylen
;
733 OPENSSL_cleanse(&actx
, sizeof(actx
));
739 int CMS_RecipientInfo_decrypt(CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
)
742 case CMS_RECIPINFO_TRANS
:
743 return cms_RecipientInfo_ktri_decrypt(cms
, ri
);
745 case CMS_RECIPINFO_KEK
:
746 return cms_RecipientInfo_kekri_decrypt(cms
, ri
);
748 case CMS_RECIPINFO_PASS
:
749 return cms_RecipientInfo_pwri_crypt(cms
, ri
, 0);
752 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT
,
753 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE
);
758 int CMS_RecipientInfo_encrypt(CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
)
761 case CMS_RECIPINFO_TRANS
:
762 return cms_RecipientInfo_ktri_encrypt(cms
, ri
);
764 case CMS_RECIPINFO_AGREE
:
765 return cms_RecipientInfo_kari_encrypt(cms
, ri
);
767 case CMS_RECIPINFO_KEK
:
768 return cms_RecipientInfo_kekri_encrypt(cms
, ri
);
770 case CMS_RECIPINFO_PASS
:
771 return cms_RecipientInfo_pwri_crypt(cms
, ri
, 1);
774 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT
,
775 CMS_R_UNSUPPORTED_RECIPIENT_TYPE
);
780 /* Check structures and fixup version numbers (if necessary) */
782 static void cms_env_set_originfo_version(CMS_EnvelopedData
*env
)
784 CMS_OriginatorInfo
*org
= env
->originatorInfo
;
788 for (i
= 0; i
< sk_CMS_CertificateChoices_num(org
->certificates
); i
++) {
789 CMS_CertificateChoices
*cch
;
790 cch
= sk_CMS_CertificateChoices_value(org
->certificates
, i
);
791 if (cch
->type
== CMS_CERTCHOICE_OTHER
) {
794 } else if (cch
->type
== CMS_CERTCHOICE_V2ACERT
) {
795 if (env
->version
< 3)
800 for (i
= 0; i
< sk_CMS_RevocationInfoChoice_num(org
->crls
); i
++) {
801 CMS_RevocationInfoChoice
*rch
;
802 rch
= sk_CMS_RevocationInfoChoice_value(org
->crls
, i
);
803 if (rch
->type
== CMS_REVCHOICE_OTHER
) {
810 static void cms_env_set_version(CMS_EnvelopedData
*env
)
813 CMS_RecipientInfo
*ri
;
816 * Can't set version higher than 4 so if 4 or more already nothing to do.
818 if (env
->version
>= 4)
821 cms_env_set_originfo_version(env
);
823 if (env
->version
>= 3)
826 for (i
= 0; i
< sk_CMS_RecipientInfo_num(env
->recipientInfos
); i
++) {
827 ri
= sk_CMS_RecipientInfo_value(env
->recipientInfos
, i
);
828 if (ri
->type
== CMS_RECIPINFO_PASS
|| ri
->type
== CMS_RECIPINFO_OTHER
) {
831 } else if (ri
->type
!= CMS_RECIPINFO_TRANS
832 || ri
->d
.ktri
->version
!= 0) {
836 if (env
->version
== 2)
838 if (env
->originatorInfo
|| env
->unprotectedAttrs
)
843 BIO
*cms_EnvelopedData_init_bio(CMS_ContentInfo
*cms
)
845 CMS_EncryptedContentInfo
*ec
;
846 STACK_OF(CMS_RecipientInfo
) *rinfos
;
847 CMS_RecipientInfo
*ri
;
851 /* Get BIO first to set up key */
853 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
854 ret
= cms_EncryptedContent_init_bio(ec
);
856 /* If error or no cipher end of processing */
858 if (!ret
|| !ec
->cipher
)
861 /* Now encrypt content key according to each RecipientInfo type */
863 rinfos
= cms
->d
.envelopedData
->recipientInfos
;
865 for (i
= 0; i
< sk_CMS_RecipientInfo_num(rinfos
); i
++) {
866 ri
= sk_CMS_RecipientInfo_value(rinfos
, i
);
867 if (CMS_RecipientInfo_encrypt(cms
, ri
) <= 0) {
868 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO
,
869 CMS_R_ERROR_SETTING_RECIPIENTINFO
);
873 cms_env_set_version(cms
->d
.envelopedData
);
879 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
890 * Get RecipientInfo type (if any) supported by a key (public or private). To
891 * retain compatibility with previous behaviour if the ctrl value isn't
892 * supported we assume key transport.
894 int cms_pkey_get_ri_type(EVP_PKEY
*pk
)
896 if (pk
->ameth
&& pk
->ameth
->pkey_ctrl
) {
898 i
= pk
->ameth
->pkey_ctrl(pk
, ASN1_PKEY_CTRL_CMS_RI_TYPE
, 0, &r
);
902 return CMS_RECIPINFO_TRANS
;