1 /* crypto/cms/cms_env.c */
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
56 #include <openssl/asn1t.h>
57 #include <openssl/pem.h>
58 #include <openssl/x509v3.h>
59 #include <openssl/err.h>
60 #include <openssl/cms.h>
61 #include <openssl/rand.h>
62 #include <openssl/aes.h>
64 #include "internal/asn1_int.h"
66 /* CMS EnvelopedData Utilities */
68 DECLARE_ASN1_ITEM(CMS_EnvelopedData
)
69 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo
)
70 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo
)
71 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute
)
73 DECLARE_STACK_OF(CMS_RecipientInfo
)
75 CMS_EnvelopedData
*cms_get0_enveloped(CMS_ContentInfo
*cms
)
77 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
) {
88 cms
->d
.envelopedData
= M_ASN1_new_of(CMS_EnvelopedData
);
89 if (!cms
->d
.envelopedData
) {
90 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT
, ERR_R_MALLOC_FAILURE
);
93 cms
->d
.envelopedData
->version
= 0;
94 cms
->d
.envelopedData
->encryptedContentInfo
->contentType
=
95 OBJ_nid2obj(NID_pkcs7_data
);
96 ASN1_OBJECT_free(cms
->contentType
);
97 cms
->contentType
= OBJ_nid2obj(NID_pkcs7_enveloped
);
98 return cms
->d
.envelopedData
;
100 return cms_get0_enveloped(cms
);
103 int cms_env_asn1_ctrl(CMS_RecipientInfo
*ri
, int cmd
)
107 if (ri
->type
== CMS_RECIPINFO_TRANS
)
108 pkey
= ri
->d
.ktri
->pkey
;
109 else if (ri
->type
== CMS_RECIPINFO_AGREE
) {
110 EVP_PKEY_CTX
*pctx
= ri
->d
.kari
->pctx
;
113 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
118 if (!pkey
->ameth
|| !pkey
->ameth
->pkey_ctrl
)
120 i
= pkey
->ameth
->pkey_ctrl(pkey
, ASN1_PKEY_CTRL_CMS_ENVELOPE
, cmd
, ri
);
122 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL
,
123 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
127 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL
, CMS_R_CTRL_FAILURE
);
133 STACK_OF(CMS_RecipientInfo
) *CMS_get0_RecipientInfos(CMS_ContentInfo
*cms
)
135 CMS_EnvelopedData
*env
;
136 env
= cms_get0_enveloped(cms
);
139 return env
->recipientInfos
;
142 int CMS_RecipientInfo_type(CMS_RecipientInfo
*ri
)
147 EVP_PKEY_CTX
*CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo
*ri
)
149 if (ri
->type
== CMS_RECIPINFO_TRANS
)
150 return ri
->d
.ktri
->pctx
;
151 else if (ri
->type
== CMS_RECIPINFO_AGREE
)
152 return ri
->d
.kari
->pctx
;
156 CMS_ContentInfo
*CMS_EnvelopedData_create(const EVP_CIPHER
*cipher
)
158 CMS_ContentInfo
*cms
;
159 CMS_EnvelopedData
*env
;
160 cms
= CMS_ContentInfo_new();
163 env
= cms_enveloped_data_init(cms
);
166 if (!cms_EncryptedContent_init(env
->encryptedContentInfo
,
171 CMS_ContentInfo_free(cms
);
172 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE
, ERR_R_MALLOC_FAILURE
);
176 /* Key Transport Recipient Info (KTRI) routines */
178 /* Initialise a ktri based on passed certificate and key */
180 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo
*ri
, X509
*recip
,
181 EVP_PKEY
*pk
, unsigned int flags
)
183 CMS_KeyTransRecipientInfo
*ktri
;
186 ri
->d
.ktri
= M_ASN1_new_of(CMS_KeyTransRecipientInfo
);
189 ri
->type
= CMS_RECIPINFO_TRANS
;
193 if (flags
& CMS_USE_KEYID
) {
195 idtype
= CMS_RECIPINFO_KEYIDENTIFIER
;
198 idtype
= CMS_RECIPINFO_ISSUER_SERIAL
;
202 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
206 if (!cms_set1_SignerIdentifier(ktri
->rid
, recip
, idtype
))
209 CRYPTO_add(&recip
->references
, 1, CRYPTO_LOCK_X509
);
210 CRYPTO_add(&pk
->references
, 1, CRYPTO_LOCK_EVP_PKEY
);
214 if (flags
& CMS_KEY_PARAM
) {
215 ktri
->pctx
= EVP_PKEY_CTX_new(ktri
->pkey
, NULL
);
218 if (EVP_PKEY_encrypt_init(ktri
->pctx
) <= 0)
220 } else if (!cms_env_asn1_ctrl(ri
, 0))
226 * Add a recipient certificate using appropriate type of RecipientInfo
229 CMS_RecipientInfo
*CMS_add1_recipient_cert(CMS_ContentInfo
*cms
,
230 X509
*recip
, unsigned int flags
)
232 CMS_RecipientInfo
*ri
= NULL
;
233 CMS_EnvelopedData
*env
;
235 env
= cms_get0_enveloped(cms
);
239 /* Initialize recipient info */
240 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
244 pk
= X509_get_pubkey(recip
);
246 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT
, CMS_R_ERROR_GETTING_PUBLIC_KEY
);
250 switch (cms_pkey_get_ri_type(pk
)) {
252 case CMS_RECIPINFO_TRANS
:
253 if (!cms_RecipientInfo_ktri_init(ri
, recip
, pk
, flags
))
257 case CMS_RECIPINFO_AGREE
:
258 if (!cms_RecipientInfo_kari_init(ri
, recip
, pk
, flags
))
263 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT
,
264 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
269 if (!sk_CMS_RecipientInfo_push(env
->recipientInfos
, ri
))
277 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT
, ERR_R_MALLOC_FAILURE
);
279 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
285 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo
*ri
,
286 EVP_PKEY
**pk
, X509
**recip
,
289 CMS_KeyTransRecipientInfo
*ktri
;
290 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
291 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS
,
292 CMS_R_NOT_KEY_TRANSPORT
);
301 *recip
= ktri
->recip
;
303 *palg
= ktri
->keyEncryptionAlgorithm
;
307 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo
*ri
,
308 ASN1_OCTET_STRING
**keyid
,
312 CMS_KeyTransRecipientInfo
*ktri
;
313 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
314 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID
,
315 CMS_R_NOT_KEY_TRANSPORT
);
320 return cms_SignerIdentifier_get0_signer_id(ktri
->rid
, keyid
, issuer
, sno
);
323 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo
*ri
, X509
*cert
)
325 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
326 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP
,
327 CMS_R_NOT_KEY_TRANSPORT
);
330 return cms_SignerIdentifier_cert_cmp(ri
->d
.ktri
->rid
, cert
);
333 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo
*ri
, EVP_PKEY
*pkey
)
335 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
336 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY
, CMS_R_NOT_KEY_TRANSPORT
);
339 ri
->d
.ktri
->pkey
= pkey
;
343 /* Encrypt content key in key transport recipient info */
345 static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo
*cms
,
346 CMS_RecipientInfo
*ri
)
348 CMS_KeyTransRecipientInfo
*ktri
;
349 CMS_EncryptedContentInfo
*ec
;
351 unsigned char *ek
= NULL
;
356 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
357 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT
, CMS_R_NOT_KEY_TRANSPORT
);
361 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
366 if (!cms_env_asn1_ctrl(ri
, 0))
369 pctx
= EVP_PKEY_CTX_new(ktri
->pkey
, NULL
);
373 if (EVP_PKEY_encrypt_init(pctx
) <= 0)
377 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_ENCRYPT
,
378 EVP_PKEY_CTRL_CMS_ENCRYPT
, 0, ri
) <= 0) {
379 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT
, CMS_R_CTRL_ERROR
);
383 if (EVP_PKEY_encrypt(pctx
, NULL
, &eklen
, ec
->key
, ec
->keylen
) <= 0)
386 ek
= OPENSSL_malloc(eklen
);
389 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
393 if (EVP_PKEY_encrypt(pctx
, ek
, &eklen
, ec
->key
, ec
->keylen
) <= 0)
396 ASN1_STRING_set0(ktri
->encryptedKey
, ek
, eklen
);
402 EVP_PKEY_CTX_free(pctx
);
409 /* Decrypt content key from KTRI */
411 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo
*cms
,
412 CMS_RecipientInfo
*ri
)
414 CMS_KeyTransRecipientInfo
*ktri
= ri
->d
.ktri
;
415 EVP_PKEY
*pkey
= ktri
->pkey
;
416 unsigned char *ek
= NULL
;
419 CMS_EncryptedContentInfo
*ec
;
420 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
422 if (ktri
->pkey
== NULL
) {
423 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT
, CMS_R_NO_PRIVATE_KEY
);
427 ktri
->pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
431 if (EVP_PKEY_decrypt_init(ktri
->pctx
) <= 0)
434 if (!cms_env_asn1_ctrl(ri
, 1))
437 if (EVP_PKEY_CTX_ctrl(ktri
->pctx
, -1, EVP_PKEY_OP_DECRYPT
,
438 EVP_PKEY_CTRL_CMS_DECRYPT
, 0, ri
) <= 0) {
439 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT
, CMS_R_CTRL_ERROR
);
443 if (EVP_PKEY_decrypt(ktri
->pctx
, NULL
, &eklen
,
444 ktri
->encryptedKey
->data
,
445 ktri
->encryptedKey
->length
) <= 0)
448 ek
= OPENSSL_malloc(eklen
);
451 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT
, ERR_R_MALLOC_FAILURE
);
455 if (EVP_PKEY_decrypt(ktri
->pctx
, ek
, &eklen
,
456 ktri
->encryptedKey
->data
,
457 ktri
->encryptedKey
->length
) <= 0) {
458 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT
, CMS_R_CMS_LIB
);
464 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
469 EVP_PKEY_CTX_free(ktri
->pctx
);
477 /* Key Encrypted Key (KEK) RecipientInfo routines */
479 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo
*ri
,
480 const unsigned char *id
, size_t idlen
)
482 ASN1_OCTET_STRING tmp_os
;
483 CMS_KEKRecipientInfo
*kekri
;
484 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
485 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP
, CMS_R_NOT_KEK
);
489 tmp_os
.type
= V_ASN1_OCTET_STRING
;
491 tmp_os
.data
= (unsigned char *)id
;
492 tmp_os
.length
= (int)idlen
;
493 return ASN1_OCTET_STRING_cmp(&tmp_os
, kekri
->kekid
->keyIdentifier
);
496 /* For now hard code AES key wrap info */
498 static size_t aes_wrap_keylen(int nid
)
501 case NID_id_aes128_wrap
:
504 case NID_id_aes192_wrap
:
507 case NID_id_aes256_wrap
:
515 CMS_RecipientInfo
*CMS_add0_recipient_key(CMS_ContentInfo
*cms
, int nid
,
516 unsigned char *key
, size_t keylen
,
517 unsigned char *id
, size_t idlen
,
518 ASN1_GENERALIZEDTIME
*date
,
519 ASN1_OBJECT
*otherTypeId
,
520 ASN1_TYPE
*otherType
)
522 CMS_RecipientInfo
*ri
= NULL
;
523 CMS_EnvelopedData
*env
;
524 CMS_KEKRecipientInfo
*kekri
;
525 env
= cms_get0_enveloped(cms
);
529 if (nid
== NID_undef
) {
532 nid
= NID_id_aes128_wrap
;
536 nid
= NID_id_aes192_wrap
;
540 nid
= NID_id_aes256_wrap
;
544 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY
, CMS_R_INVALID_KEY_LENGTH
);
550 size_t exp_keylen
= aes_wrap_keylen(nid
);
553 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY
,
554 CMS_R_UNSUPPORTED_KEK_ALGORITHM
);
558 if (keylen
!= exp_keylen
) {
559 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY
, CMS_R_INVALID_KEY_LENGTH
);
565 /* Initialize recipient info */
566 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
570 ri
->d
.kekri
= M_ASN1_new_of(CMS_KEKRecipientInfo
);
573 ri
->type
= CMS_RECIPINFO_KEK
;
578 kekri
->kekid
->other
= M_ASN1_new_of(CMS_OtherKeyAttribute
);
579 if (kekri
->kekid
->other
== NULL
)
583 if (!sk_CMS_RecipientInfo_push(env
->recipientInfos
, ri
))
586 /* After this point no calls can fail */
591 kekri
->keylen
= keylen
;
593 ASN1_STRING_set0(kekri
->kekid
->keyIdentifier
, id
, idlen
);
595 kekri
->kekid
->date
= date
;
597 if (kekri
->kekid
->other
) {
598 kekri
->kekid
->other
->keyAttrId
= otherTypeId
;
599 kekri
->kekid
->other
->keyAttr
= otherType
;
602 X509_ALGOR_set0(kekri
->keyEncryptionAlgorithm
,
603 OBJ_nid2obj(nid
), V_ASN1_UNDEF
, NULL
);
608 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY
, ERR_R_MALLOC_FAILURE
);
610 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
615 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo
*ri
,
617 ASN1_OCTET_STRING
**pid
,
618 ASN1_GENERALIZEDTIME
**pdate
,
619 ASN1_OBJECT
**potherid
,
620 ASN1_TYPE
**pothertype
)
622 CMS_KEKIdentifier
*rkid
;
623 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
624 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID
, CMS_R_NOT_KEK
);
627 rkid
= ri
->d
.kekri
->kekid
;
629 *palg
= ri
->d
.kekri
->keyEncryptionAlgorithm
;
631 *pid
= rkid
->keyIdentifier
;
636 *potherid
= rkid
->other
->keyAttrId
;
642 *pothertype
= rkid
->other
->keyAttr
;
649 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo
*ri
,
650 unsigned char *key
, size_t keylen
)
652 CMS_KEKRecipientInfo
*kekri
;
653 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
654 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY
, CMS_R_NOT_KEK
);
660 kekri
->keylen
= keylen
;
664 /* Encrypt content key in KEK recipient info */
666 static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo
*cms
,
667 CMS_RecipientInfo
*ri
)
669 CMS_EncryptedContentInfo
*ec
;
670 CMS_KEKRecipientInfo
*kekri
;
672 unsigned char *wkey
= NULL
;
676 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
681 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT
, CMS_R_NO_KEY
);
685 if (AES_set_encrypt_key(kekri
->key
, kekri
->keylen
<< 3, &actx
)) {
686 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT
,
687 CMS_R_ERROR_SETTING_KEY
);
691 wkey
= OPENSSL_malloc(ec
->keylen
+ 8);
694 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
698 wkeylen
= AES_wrap_key(&actx
, NULL
, wkey
, ec
->key
, ec
->keylen
);
701 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT
, CMS_R_WRAP_ERROR
);
705 ASN1_STRING_set0(kekri
->encryptedKey
, wkey
, wkeylen
);
713 OPENSSL_cleanse(&actx
, sizeof(actx
));
719 /* Decrypt content key in KEK recipient info */
721 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo
*cms
,
722 CMS_RecipientInfo
*ri
)
724 CMS_EncryptedContentInfo
*ec
;
725 CMS_KEKRecipientInfo
*kekri
;
727 unsigned char *ukey
= NULL
;
731 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
736 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
, CMS_R_NO_KEY
);
740 wrap_nid
= OBJ_obj2nid(kekri
->keyEncryptionAlgorithm
->algorithm
);
741 if (aes_wrap_keylen(wrap_nid
) != kekri
->keylen
) {
742 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
,
743 CMS_R_INVALID_KEY_LENGTH
);
747 /* If encrypted key length is invalid don't bother */
749 if (kekri
->encryptedKey
->length
< 16) {
750 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
,
751 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH
);
755 if (AES_set_decrypt_key(kekri
->key
, kekri
->keylen
<< 3, &actx
)) {
756 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
,
757 CMS_R_ERROR_SETTING_KEY
);
761 ukey
= OPENSSL_malloc(kekri
->encryptedKey
->length
- 8);
764 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
, ERR_R_MALLOC_FAILURE
);
768 ukeylen
= AES_unwrap_key(&actx
, NULL
, ukey
,
769 kekri
->encryptedKey
->data
,
770 kekri
->encryptedKey
->length
);
773 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT
, CMS_R_UNWRAP_ERROR
);
778 ec
->keylen
= ukeylen
;
786 OPENSSL_cleanse(&actx
, sizeof(actx
));
792 int CMS_RecipientInfo_decrypt(CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
)
795 case CMS_RECIPINFO_TRANS
:
796 return cms_RecipientInfo_ktri_decrypt(cms
, ri
);
798 case CMS_RECIPINFO_KEK
:
799 return cms_RecipientInfo_kekri_decrypt(cms
, ri
);
801 case CMS_RECIPINFO_PASS
:
802 return cms_RecipientInfo_pwri_crypt(cms
, ri
, 0);
805 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT
,
806 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE
);
811 int CMS_RecipientInfo_encrypt(CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
)
814 case CMS_RECIPINFO_TRANS
:
815 return cms_RecipientInfo_ktri_encrypt(cms
, ri
);
817 case CMS_RECIPINFO_AGREE
:
818 return cms_RecipientInfo_kari_encrypt(cms
, ri
);
820 case CMS_RECIPINFO_KEK
:
821 return cms_RecipientInfo_kekri_encrypt(cms
, ri
);
823 case CMS_RECIPINFO_PASS
:
824 return cms_RecipientInfo_pwri_crypt(cms
, ri
, 1);
827 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT
,
828 CMS_R_UNSUPPORTED_RECIPIENT_TYPE
);
833 /* Check structures and fixup version numbers (if necessary) */
835 static void cms_env_set_originfo_version(CMS_EnvelopedData
*env
)
837 CMS_OriginatorInfo
*org
= env
->originatorInfo
;
841 for (i
= 0; i
< sk_CMS_CertificateChoices_num(org
->certificates
); i
++) {
842 CMS_CertificateChoices
*cch
;
843 cch
= sk_CMS_CertificateChoices_value(org
->certificates
, i
);
844 if (cch
->type
== CMS_CERTCHOICE_OTHER
) {
847 } else if (cch
->type
== CMS_CERTCHOICE_V2ACERT
) {
848 if (env
->version
< 3)
853 for (i
= 0; i
< sk_CMS_RevocationInfoChoice_num(org
->crls
); i
++) {
854 CMS_RevocationInfoChoice
*rch
;
855 rch
= sk_CMS_RevocationInfoChoice_value(org
->crls
, i
);
856 if (rch
->type
== CMS_REVCHOICE_OTHER
) {
863 static void cms_env_set_version(CMS_EnvelopedData
*env
)
866 CMS_RecipientInfo
*ri
;
869 * Can't set version higher than 4 so if 4 or more already nothing to do.
871 if (env
->version
>= 4)
874 cms_env_set_originfo_version(env
);
876 if (env
->version
>= 3)
879 for (i
= 0; i
< sk_CMS_RecipientInfo_num(env
->recipientInfos
); i
++) {
880 ri
= sk_CMS_RecipientInfo_value(env
->recipientInfos
, i
);
881 if (ri
->type
== CMS_RECIPINFO_PASS
|| ri
->type
== CMS_RECIPINFO_OTHER
) {
884 } else if (ri
->type
!= CMS_RECIPINFO_TRANS
885 || ri
->d
.ktri
->version
!= 0) {
889 if (env
->version
== 2)
891 if (env
->originatorInfo
|| env
->unprotectedAttrs
)
896 BIO
*cms_EnvelopedData_init_bio(CMS_ContentInfo
*cms
)
898 CMS_EncryptedContentInfo
*ec
;
899 STACK_OF(CMS_RecipientInfo
) *rinfos
;
900 CMS_RecipientInfo
*ri
;
904 /* Get BIO first to set up key */
906 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
907 ret
= cms_EncryptedContent_init_bio(ec
);
909 /* If error or no cipher end of processing */
911 if (!ret
|| !ec
->cipher
)
914 /* Now encrypt content key according to each RecipientInfo type */
916 rinfos
= cms
->d
.envelopedData
->recipientInfos
;
918 for (i
= 0; i
< sk_CMS_RecipientInfo_num(rinfos
); i
++) {
919 ri
= sk_CMS_RecipientInfo_value(rinfos
, i
);
920 if (CMS_RecipientInfo_encrypt(cms
, ri
) <= 0) {
921 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO
,
922 CMS_R_ERROR_SETTING_RECIPIENTINFO
);
926 cms_env_set_version(cms
->d
.envelopedData
);
932 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
943 * Get RecipientInfo type (if any) supported by a key (public or private). To
944 * retain compatibility with previous behaviour if the ctrl value isn't
945 * supported we assume key transport.
947 int cms_pkey_get_ri_type(EVP_PKEY
*pk
)
949 if (pk
->ameth
&& pk
->ameth
->pkey_ctrl
) {
951 i
= pk
->ameth
->pkey_ctrl(pk
, ASN1_PKEY_CTRL_CMS_RI_TYPE
, 0, &r
);
955 return CMS_RECIPINFO_TRANS
;