2 * Copyright 2008-2022 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/evp.h>
17 #include "internal/sizes.h"
18 #include "crypto/asn1.h"
19 #include "crypto/evp.h"
20 #include "crypto/x509.h"
21 #include "cms_local.h"
23 /* CMS EnvelopedData Utilities */
24 static void cms_env_set_version(CMS_EnvelopedData
*env
);
26 #define CMS_ENVELOPED_STANDARD 1
27 #define CMS_ENVELOPED_AUTH 2
29 static int cms_get_enveloped_type(const CMS_ContentInfo
*cms
)
31 int nid
= OBJ_obj2nid(cms
->contentType
);
34 case NID_pkcs7_enveloped
:
35 return CMS_ENVELOPED_STANDARD
;
37 case NID_id_smime_ct_authEnvelopedData
:
38 return CMS_ENVELOPED_AUTH
;
41 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA
);
46 CMS_EnvelopedData
*ossl_cms_get0_enveloped(CMS_ContentInfo
*cms
)
48 if (OBJ_obj2nid(cms
->contentType
) != NID_pkcs7_enveloped
) {
49 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA
);
52 return cms
->d
.envelopedData
;
55 CMS_AuthEnvelopedData
*ossl_cms_get0_auth_enveloped(CMS_ContentInfo
*cms
)
57 if (OBJ_obj2nid(cms
->contentType
) != NID_id_smime_ct_authEnvelopedData
) {
58 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA
);
61 return cms
->d
.authEnvelopedData
;
64 static CMS_EnvelopedData
*cms_enveloped_data_init(CMS_ContentInfo
*cms
)
66 if (cms
->d
.other
== NULL
) {
67 cms
->d
.envelopedData
= M_ASN1_new_of(CMS_EnvelopedData
);
68 if (cms
->d
.envelopedData
== NULL
) {
69 ERR_raise(ERR_LIB_CMS
, ERR_R_ASN1_LIB
);
72 cms
->d
.envelopedData
->version
= 0;
73 cms
->d
.envelopedData
->encryptedContentInfo
->contentType
=
74 OBJ_nid2obj(NID_pkcs7_data
);
75 ASN1_OBJECT_free(cms
->contentType
);
76 cms
->contentType
= OBJ_nid2obj(NID_pkcs7_enveloped
);
77 return cms
->d
.envelopedData
;
79 return ossl_cms_get0_enveloped(cms
);
82 static CMS_AuthEnvelopedData
*
83 cms_auth_enveloped_data_init(CMS_ContentInfo
*cms
)
85 if (cms
->d
.other
== NULL
) {
86 cms
->d
.authEnvelopedData
= M_ASN1_new_of(CMS_AuthEnvelopedData
);
87 if (cms
->d
.authEnvelopedData
== NULL
) {
88 ERR_raise(ERR_LIB_CMS
, ERR_R_ASN1_LIB
);
91 /* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */
92 cms
->d
.authEnvelopedData
->version
= 0;
93 cms
->d
.authEnvelopedData
->authEncryptedContentInfo
->contentType
=
94 OBJ_nid2obj(NID_pkcs7_data
);
95 ASN1_OBJECT_free(cms
->contentType
);
96 cms
->contentType
= OBJ_nid2obj(NID_id_smime_ct_authEnvelopedData
);
97 return cms
->d
.authEnvelopedData
;
99 return ossl_cms_get0_auth_enveloped(cms
);
102 int ossl_cms_env_asn1_ctrl(CMS_RecipientInfo
*ri
, int cmd
)
106 if (ri
->type
== CMS_RECIPINFO_TRANS
)
107 pkey
= ri
->d
.ktri
->pkey
;
108 else if (ri
->type
== CMS_RECIPINFO_AGREE
) {
109 EVP_PKEY_CTX
*pctx
= ri
->d
.kari
->pctx
;
113 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
119 if (EVP_PKEY_is_a(pkey
, "DHX") || EVP_PKEY_is_a(pkey
, "DH"))
120 return ossl_cms_dh_envelope(ri
, cmd
);
121 else if (EVP_PKEY_is_a(pkey
, "EC"))
122 return ossl_cms_ecdh_envelope(ri
, cmd
);
123 else if (EVP_PKEY_is_a(pkey
, "RSA"))
124 return ossl_cms_rsa_envelope(ri
, cmd
);
126 /* Something else? We'll give engines etc a chance to handle this */
127 if (pkey
->ameth
== NULL
|| pkey
->ameth
->pkey_ctrl
== NULL
)
129 i
= pkey
->ameth
->pkey_ctrl(pkey
, ASN1_PKEY_CTRL_CMS_ENVELOPE
, cmd
, ri
);
131 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
135 ERR_raise(ERR_LIB_CMS
, CMS_R_CTRL_FAILURE
);
141 CMS_EncryptedContentInfo
*ossl_cms_get0_env_enc_content(const CMS_ContentInfo
*cms
)
143 switch (cms_get_enveloped_type(cms
)) {
144 case CMS_ENVELOPED_STANDARD
:
145 return cms
->d
.envelopedData
->encryptedContentInfo
;
147 case CMS_ENVELOPED_AUTH
:
148 return cms
->d
.authEnvelopedData
->authEncryptedContentInfo
;
155 STACK_OF(CMS_RecipientInfo
) *CMS_get0_RecipientInfos(CMS_ContentInfo
*cms
)
157 switch (cms_get_enveloped_type(cms
)) {
158 case CMS_ENVELOPED_STANDARD
:
159 return cms
->d
.envelopedData
->recipientInfos
;
161 case CMS_ENVELOPED_AUTH
:
162 return cms
->d
.authEnvelopedData
->recipientInfos
;
169 void ossl_cms_RecipientInfos_set_cmsctx(CMS_ContentInfo
*cms
)
172 CMS_RecipientInfo
*ri
;
173 const CMS_CTX
*ctx
= ossl_cms_get0_cmsctx(cms
);
174 STACK_OF(CMS_RecipientInfo
) *rinfos
= CMS_get0_RecipientInfos(cms
);
176 for (i
= 0; i
< sk_CMS_RecipientInfo_num(rinfos
); i
++) {
177 ri
= sk_CMS_RecipientInfo_value(rinfos
, i
);
180 case CMS_RECIPINFO_AGREE
:
181 ri
->d
.kari
->cms_ctx
= ctx
;
183 case CMS_RECIPINFO_TRANS
:
184 ri
->d
.ktri
->cms_ctx
= ctx
;
185 ossl_x509_set0_libctx(ri
->d
.ktri
->recip
,
186 ossl_cms_ctx_get0_libctx(ctx
),
187 ossl_cms_ctx_get0_propq(ctx
));
189 case CMS_RECIPINFO_KEK
:
190 ri
->d
.kekri
->cms_ctx
= ctx
;
192 case CMS_RECIPINFO_PASS
:
193 ri
->d
.pwri
->cms_ctx
= ctx
;
202 int CMS_RecipientInfo_type(CMS_RecipientInfo
*ri
)
207 EVP_PKEY_CTX
*CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo
*ri
)
209 if (ri
->type
== CMS_RECIPINFO_TRANS
)
210 return ri
->d
.ktri
->pctx
;
211 else if (ri
->type
== CMS_RECIPINFO_AGREE
)
212 return ri
->d
.kari
->pctx
;
216 CMS_ContentInfo
*CMS_EnvelopedData_create_ex(const EVP_CIPHER
*cipher
,
217 OSSL_LIB_CTX
*libctx
,
220 CMS_ContentInfo
*cms
;
221 CMS_EnvelopedData
*env
;
223 cms
= CMS_ContentInfo_new_ex(libctx
, propq
);
226 env
= cms_enveloped_data_init(cms
);
230 if (!ossl_cms_EncryptedContent_init(env
->encryptedContentInfo
, cipher
, NULL
,
231 0, ossl_cms_get0_cmsctx(cms
)))
235 CMS_ContentInfo_free(cms
);
236 ERR_raise(ERR_LIB_CMS
, ERR_R_CMS_LIB
);
240 CMS_ContentInfo
*CMS_EnvelopedData_create(const EVP_CIPHER
*cipher
)
242 return CMS_EnvelopedData_create_ex(cipher
, NULL
, NULL
);
245 BIO
*CMS_EnvelopedData_decrypt(CMS_EnvelopedData
*env
, BIO
*detached_data
,
246 EVP_PKEY
*pkey
, X509
*cert
,
247 ASN1_OCTET_STRING
*secret
, unsigned int flags
,
248 OSSL_LIB_CTX
*libctx
, const char *propq
)
255 ERR_raise(ERR_LIB_CMS
, ERR_R_PASSED_NULL_PARAMETER
);
259 if ((ci
= CMS_ContentInfo_new_ex(libctx
, propq
)) == NULL
260 || (bio
= BIO_new(BIO_s_mem())) == NULL
)
262 ci
->contentType
= OBJ_nid2obj(NID_pkcs7_enveloped
);
263 ci
->d
.envelopedData
= env
;
265 && CMS_decrypt_set1_password(ci
, (unsigned char *)
266 ASN1_STRING_get0_data(secret
),
267 ASN1_STRING_length(secret
)) != 1)
269 res
= CMS_decrypt(ci
, secret
== NULL
? pkey
: NULL
,
270 secret
== NULL
? cert
: NULL
, detached_data
, bio
, flags
);
274 ci
->d
.envelopedData
= NULL
; /* do not indirectly free |env| */
275 CMS_ContentInfo_free(ci
);
284 CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER
*cipher
, OSSL_LIB_CTX
*libctx
,
287 CMS_ContentInfo
*cms
;
288 CMS_AuthEnvelopedData
*aenv
;
290 cms
= CMS_ContentInfo_new_ex(libctx
, propq
);
293 aenv
= cms_auth_enveloped_data_init(cms
);
296 if (!ossl_cms_EncryptedContent_init(aenv
->authEncryptedContentInfo
,
298 ossl_cms_get0_cmsctx(cms
)))
302 CMS_ContentInfo_free(cms
);
303 ERR_raise(ERR_LIB_CMS
, ERR_R_CMS_LIB
);
308 CMS_ContentInfo
*CMS_AuthEnvelopedData_create(const EVP_CIPHER
*cipher
)
310 return CMS_AuthEnvelopedData_create_ex(cipher
, NULL
, NULL
);
313 /* Key Transport Recipient Info (KTRI) routines */
315 /* Initialise a ktri based on passed certificate and key */
317 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo
*ri
, X509
*recip
,
318 EVP_PKEY
*pk
, unsigned int flags
,
321 CMS_KeyTransRecipientInfo
*ktri
;
324 ri
->d
.ktri
= M_ASN1_new_of(CMS_KeyTransRecipientInfo
);
327 ri
->type
= CMS_RECIPINFO_TRANS
;
332 if (flags
& CMS_USE_KEYID
) {
334 idtype
= CMS_RECIPINFO_KEYIDENTIFIER
;
337 idtype
= CMS_RECIPINFO_ISSUER_SERIAL
;
341 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
345 if (!ossl_cms_set1_SignerIdentifier(ktri
->rid
, recip
, idtype
, ctx
))
354 if (flags
& CMS_KEY_PARAM
) {
355 ktri
->pctx
= EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx
),
357 ossl_cms_ctx_get0_propq(ctx
));
358 if (ktri
->pctx
== NULL
)
360 if (EVP_PKEY_encrypt_init(ktri
->pctx
) <= 0)
362 } else if (!ossl_cms_env_asn1_ctrl(ri
, 0))
368 * Add a recipient certificate using appropriate type of RecipientInfo
371 CMS_RecipientInfo
*CMS_add1_recipient(CMS_ContentInfo
*cms
, X509
*recip
,
372 EVP_PKEY
*originatorPrivKey
,
373 X509
*originator
, unsigned int flags
)
375 CMS_RecipientInfo
*ri
= NULL
;
376 STACK_OF(CMS_RecipientInfo
) *ris
;
378 const CMS_CTX
*ctx
= ossl_cms_get0_cmsctx(cms
);
380 ris
= CMS_get0_RecipientInfos(cms
);
384 /* Initialize recipient info */
385 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
387 ERR_raise(ERR_LIB_CMS
, ERR_R_ASN1_LIB
);
391 pk
= X509_get0_pubkey(recip
);
393 ERR_raise(ERR_LIB_CMS
, CMS_R_ERROR_GETTING_PUBLIC_KEY
);
397 switch (ossl_cms_pkey_get_ri_type(pk
)) {
399 case CMS_RECIPINFO_TRANS
:
400 if (!cms_RecipientInfo_ktri_init(ri
, recip
, pk
, flags
, ctx
))
404 case CMS_RECIPINFO_AGREE
:
405 if (!ossl_cms_RecipientInfo_kari_init(ri
, recip
, pk
, originator
,
406 originatorPrivKey
, flags
, ctx
))
411 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
416 if (!sk_CMS_RecipientInfo_push(ris
, ri
)) {
417 ERR_raise(ERR_LIB_CMS
, ERR_R_CRYPTO_LIB
);
424 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
429 CMS_RecipientInfo
*CMS_add1_recipient_cert(CMS_ContentInfo
*cms
, X509
*recip
,
432 return CMS_add1_recipient(cms
, recip
, NULL
, NULL
, flags
);
435 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo
*ri
,
436 EVP_PKEY
**pk
, X509
**recip
,
439 CMS_KeyTransRecipientInfo
*ktri
;
440 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
441 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
450 *recip
= ktri
->recip
;
452 *palg
= ktri
->keyEncryptionAlgorithm
;
456 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo
*ri
,
457 ASN1_OCTET_STRING
**keyid
,
461 CMS_KeyTransRecipientInfo
*ktri
;
462 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
463 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
468 return ossl_cms_SignerIdentifier_get0_signer_id(ktri
->rid
, keyid
, issuer
,
472 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo
*ri
, X509
*cert
)
474 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
475 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
478 return ossl_cms_SignerIdentifier_cert_cmp(ri
->d
.ktri
->rid
, cert
);
481 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo
*ri
, EVP_PKEY
*pkey
)
483 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
484 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
487 EVP_PKEY_free(ri
->d
.ktri
->pkey
);
488 ri
->d
.ktri
->pkey
= pkey
;
492 /* Encrypt content key in key transport recipient info */
494 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo
*cms
,
495 CMS_RecipientInfo
*ri
)
497 CMS_KeyTransRecipientInfo
*ktri
;
498 CMS_EncryptedContentInfo
*ec
;
500 unsigned char *ek
= NULL
;
502 const CMS_CTX
*ctx
= ossl_cms_get0_cmsctx(cms
);
506 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
507 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
511 ec
= ossl_cms_get0_env_enc_content(cms
);
516 if (!ossl_cms_env_asn1_ctrl(ri
, 0))
519 pctx
= EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx
),
521 ossl_cms_ctx_get0_propq(ctx
));
525 if (EVP_PKEY_encrypt_init(pctx
) <= 0)
529 if (EVP_PKEY_encrypt(pctx
, NULL
, &eklen
, ec
->key
, ec
->keylen
) <= 0)
532 ek
= OPENSSL_malloc(eklen
);
536 if (EVP_PKEY_encrypt(pctx
, ek
, &eklen
, ec
->key
, ec
->keylen
) <= 0)
539 ASN1_STRING_set0(ktri
->encryptedKey
, ek
, eklen
);
545 EVP_PKEY_CTX_free(pctx
);
551 /* Decrypt content key from KTRI */
553 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo
*cms
,
554 CMS_RecipientInfo
*ri
)
556 CMS_KeyTransRecipientInfo
*ktri
= ri
->d
.ktri
;
557 EVP_PKEY
*pkey
= ktri
->pkey
;
558 unsigned char *ek
= NULL
;
562 const EVP_CIPHER
*cipher
= NULL
;
563 EVP_CIPHER
*fetched_cipher
= NULL
;
564 CMS_EncryptedContentInfo
*ec
;
565 const CMS_CTX
*ctx
= ossl_cms_get0_cmsctx(cms
);
566 OSSL_LIB_CTX
*libctx
= ossl_cms_ctx_get0_libctx(ctx
);
567 const char *propq
= ossl_cms_ctx_get0_propq(ctx
);
569 ec
= ossl_cms_get0_env_enc_content(cms
);
571 if (ktri
->pkey
== NULL
) {
572 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_PRIVATE_KEY
);
576 if (cms
->d
.envelopedData
->encryptedContentInfo
->havenocert
577 && !cms
->d
.envelopedData
->encryptedContentInfo
->debug
) {
578 X509_ALGOR
*calg
= ec
->contentEncryptionAlgorithm
;
579 char name
[OSSL_MAX_NAME_SIZE
];
581 OBJ_obj2txt(name
, sizeof(name
), calg
->algorithm
, 0);
583 (void)ERR_set_mark();
584 fetched_cipher
= EVP_CIPHER_fetch(libctx
, name
, propq
);
586 if (fetched_cipher
!= NULL
)
587 cipher
= fetched_cipher
;
589 cipher
= EVP_get_cipherbyobj(calg
->algorithm
);
590 if (cipher
== NULL
) {
591 (void)ERR_clear_last_mark();
592 ERR_raise(ERR_LIB_CMS
, CMS_R_UNKNOWN_CIPHER
);
595 (void)ERR_pop_to_mark();
597 fixlen
= EVP_CIPHER_get_key_length(cipher
);
598 EVP_CIPHER_free(fetched_cipher
);
601 ktri
->pctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
);
602 if (ktri
->pctx
== NULL
)
605 if (EVP_PKEY_decrypt_init(ktri
->pctx
) <= 0)
608 if (!ossl_cms_env_asn1_ctrl(ri
, 1))
611 if (EVP_PKEY_is_a(pkey
, "RSA"))
612 /* upper layer CMS code incorrectly assumes that a successful RSA
613 * decryption means that the key matches ciphertext (which never
614 * was the case, implicit rejection or not), so to make it work
615 * disable implicit rejection for RSA keys */
616 EVP_PKEY_CTX_ctrl_str(ktri
->pctx
, "rsa_pkcs1_implicit_rejection", "0");
618 if (EVP_PKEY_decrypt(ktri
->pctx
, NULL
, &eklen
,
619 ktri
->encryptedKey
->data
,
620 ktri
->encryptedKey
->length
) <= 0)
623 ek
= OPENSSL_malloc(eklen
);
627 if (EVP_PKEY_decrypt(ktri
->pctx
, ek
, &eklen
,
628 ktri
->encryptedKey
->data
,
629 ktri
->encryptedKey
->length
) <= 0
631 || (fixlen
!= 0 && eklen
!= fixlen
)) {
632 ERR_raise(ERR_LIB_CMS
, CMS_R_CMS_LIB
);
638 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
643 EVP_PKEY_CTX_free(ktri
->pctx
);
651 /* Key Encrypted Key (KEK) RecipientInfo routines */
653 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo
*ri
,
654 const unsigned char *id
, size_t idlen
)
656 ASN1_OCTET_STRING tmp_os
;
657 CMS_KEKRecipientInfo
*kekri
;
658 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
659 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEK
);
663 tmp_os
.type
= V_ASN1_OCTET_STRING
;
665 tmp_os
.data
= (unsigned char *)id
;
666 tmp_os
.length
= (int)idlen
;
667 return ASN1_OCTET_STRING_cmp(&tmp_os
, kekri
->kekid
->keyIdentifier
);
670 /* For now hard code AES key wrap info */
672 static size_t aes_wrap_keylen(int nid
)
675 case NID_id_aes128_wrap
:
678 case NID_id_aes192_wrap
:
681 case NID_id_aes256_wrap
:
689 CMS_RecipientInfo
*CMS_add0_recipient_key(CMS_ContentInfo
*cms
, int nid
,
690 unsigned char *key
, size_t keylen
,
691 unsigned char *id
, size_t idlen
,
692 ASN1_GENERALIZEDTIME
*date
,
693 ASN1_OBJECT
*otherTypeId
,
694 ASN1_TYPE
*otherType
)
696 CMS_RecipientInfo
*ri
= NULL
;
697 CMS_KEKRecipientInfo
*kekri
;
698 STACK_OF(CMS_RecipientInfo
) *ris
= CMS_get0_RecipientInfos(cms
);
703 if (nid
== NID_undef
) {
706 nid
= NID_id_aes128_wrap
;
710 nid
= NID_id_aes192_wrap
;
714 nid
= NID_id_aes256_wrap
;
718 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
724 size_t exp_keylen
= aes_wrap_keylen(nid
);
727 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_KEK_ALGORITHM
);
731 if (keylen
!= exp_keylen
) {
732 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
738 /* Initialize recipient info */
739 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
741 ERR_raise(ERR_LIB_CMS
, ERR_R_ASN1_LIB
);
745 ri
->d
.kekri
= M_ASN1_new_of(CMS_KEKRecipientInfo
);
747 ERR_raise(ERR_LIB_CMS
, ERR_R_ASN1_LIB
);
750 ri
->type
= CMS_RECIPINFO_KEK
;
755 kekri
->kekid
->other
= M_ASN1_new_of(CMS_OtherKeyAttribute
);
756 if (kekri
->kekid
->other
== NULL
) {
757 ERR_raise(ERR_LIB_CMS
, ERR_R_ASN1_LIB
);
762 if (!sk_CMS_RecipientInfo_push(ris
, ri
)) {
763 ERR_raise(ERR_LIB_CMS
, ERR_R_CRYPTO_LIB
);
767 /* After this point no calls can fail */
772 kekri
->keylen
= keylen
;
774 ASN1_STRING_set0(kekri
->kekid
->keyIdentifier
, id
, idlen
);
776 kekri
->kekid
->date
= date
;
778 if (kekri
->kekid
->other
) {
779 kekri
->kekid
->other
->keyAttrId
= otherTypeId
;
780 kekri
->kekid
->other
->keyAttr
= otherType
;
783 (void)X509_ALGOR_set0(kekri
->keyEncryptionAlgorithm
, OBJ_nid2obj(nid
),
784 V_ASN1_UNDEF
, NULL
); /* cannot fail */
789 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
793 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo
*ri
,
795 ASN1_OCTET_STRING
**pid
,
796 ASN1_GENERALIZEDTIME
**pdate
,
797 ASN1_OBJECT
**potherid
,
798 ASN1_TYPE
**pothertype
)
800 CMS_KEKIdentifier
*rkid
;
801 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
802 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEK
);
805 rkid
= ri
->d
.kekri
->kekid
;
807 *palg
= ri
->d
.kekri
->keyEncryptionAlgorithm
;
809 *pid
= rkid
->keyIdentifier
;
814 *potherid
= rkid
->other
->keyAttrId
;
820 *pothertype
= rkid
->other
->keyAttr
;
827 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo
*ri
,
828 unsigned char *key
, size_t keylen
)
830 CMS_KEKRecipientInfo
*kekri
;
831 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
832 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEK
);
838 kekri
->keylen
= keylen
;
842 static EVP_CIPHER
*cms_get_key_wrap_cipher(size_t keylen
, const CMS_CTX
*ctx
)
844 const char *alg
= NULL
;
848 alg
= "AES-128-WRAP";
851 alg
= "AES-192-WRAP";
854 alg
= "AES-256-WRAP";
859 return EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(ctx
), alg
,
860 ossl_cms_ctx_get0_propq(ctx
));
864 /* Encrypt content key in KEK recipient info */
866 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo
*cms
,
867 CMS_RecipientInfo
*ri
)
869 CMS_EncryptedContentInfo
*ec
;
870 CMS_KEKRecipientInfo
*kekri
;
871 unsigned char *wkey
= NULL
;
874 EVP_CIPHER
*cipher
= NULL
;
876 EVP_CIPHER_CTX
*ctx
= NULL
;
877 const CMS_CTX
*cms_ctx
= ossl_cms_get0_cmsctx(cms
);
879 ec
= ossl_cms_get0_env_enc_content(cms
);
885 if (kekri
->key
== NULL
) {
886 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_KEY
);
890 cipher
= cms_get_key_wrap_cipher(kekri
->keylen
, cms_ctx
);
891 if (cipher
== NULL
) {
892 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
896 /* 8 byte prefix for AES wrap ciphers */
897 wkey
= OPENSSL_malloc(ec
->keylen
+ 8);
901 ctx
= EVP_CIPHER_CTX_new();
903 ERR_raise(ERR_LIB_CMS
, ERR_R_EVP_LIB
);
907 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
908 if (!EVP_EncryptInit_ex(ctx
, cipher
, NULL
, kekri
->key
, NULL
)
909 || !EVP_EncryptUpdate(ctx
, wkey
, &wkeylen
, ec
->key
, ec
->keylen
)
910 || !EVP_EncryptFinal_ex(ctx
, wkey
+ wkeylen
, &outlen
)) {
911 ERR_raise(ERR_LIB_CMS
, CMS_R_WRAP_ERROR
);
915 if (!ossl_assert((size_t)wkeylen
== ec
->keylen
+ 8)) {
916 ERR_raise(ERR_LIB_CMS
, CMS_R_WRAP_ERROR
);
920 ASN1_STRING_set0(kekri
->encryptedKey
, wkey
, wkeylen
);
925 EVP_CIPHER_free(cipher
);
928 EVP_CIPHER_CTX_free(ctx
);
933 /* Decrypt content key in KEK recipient info */
935 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo
*cms
,
936 CMS_RecipientInfo
*ri
)
938 CMS_EncryptedContentInfo
*ec
;
939 CMS_KEKRecipientInfo
*kekri
;
940 unsigned char *ukey
= NULL
;
943 EVP_CIPHER
*cipher
= NULL
;
945 EVP_CIPHER_CTX
*ctx
= NULL
;
946 const CMS_CTX
*cms_ctx
= ossl_cms_get0_cmsctx(cms
);
948 ec
= ossl_cms_get0_env_enc_content(cms
);
955 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_KEY
);
959 wrap_nid
= OBJ_obj2nid(kekri
->keyEncryptionAlgorithm
->algorithm
);
960 if (aes_wrap_keylen(wrap_nid
) != kekri
->keylen
) {
961 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
965 /* If encrypted key length is invalid don't bother */
967 if (kekri
->encryptedKey
->length
< 16) {
968 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH
);
972 cipher
= cms_get_key_wrap_cipher(kekri
->keylen
, cms_ctx
);
973 if (cipher
== NULL
) {
974 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
978 ukey
= OPENSSL_malloc(kekri
->encryptedKey
->length
- 8);
982 ctx
= EVP_CIPHER_CTX_new();
984 ERR_raise(ERR_LIB_CMS
, ERR_R_EVP_LIB
);
988 if (!EVP_DecryptInit_ex(ctx
, cipher
, NULL
, kekri
->key
, NULL
)
989 || !EVP_DecryptUpdate(ctx
, ukey
, &ukeylen
,
990 kekri
->encryptedKey
->data
,
991 kekri
->encryptedKey
->length
)
992 || !EVP_DecryptFinal_ex(ctx
, ukey
+ ukeylen
, &outlen
)) {
993 ERR_raise(ERR_LIB_CMS
, CMS_R_UNWRAP_ERROR
);
998 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
1000 ec
->keylen
= ukeylen
;
1005 EVP_CIPHER_free(cipher
);
1008 EVP_CIPHER_CTX_free(ctx
);
1013 int CMS_RecipientInfo_decrypt(CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
)
1016 case CMS_RECIPINFO_TRANS
:
1017 return cms_RecipientInfo_ktri_decrypt(cms
, ri
);
1019 case CMS_RECIPINFO_KEK
:
1020 return cms_RecipientInfo_kekri_decrypt(cms
, ri
);
1022 case CMS_RECIPINFO_PASS
:
1023 return ossl_cms_RecipientInfo_pwri_crypt(cms
, ri
, 0);
1026 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE
);
1031 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
)
1034 case CMS_RECIPINFO_TRANS
:
1035 return cms_RecipientInfo_ktri_encrypt(cms
, ri
);
1037 case CMS_RECIPINFO_AGREE
:
1038 return ossl_cms_RecipientInfo_kari_encrypt(cms
, ri
);
1040 case CMS_RECIPINFO_KEK
:
1041 return cms_RecipientInfo_kekri_encrypt(cms
, ri
);
1043 case CMS_RECIPINFO_PASS
:
1044 return ossl_cms_RecipientInfo_pwri_crypt(cms
, ri
, 1);
1047 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_RECIPIENT_TYPE
);
1052 /* Check structures and fixup version numbers (if necessary) */
1054 static void cms_env_set_originfo_version(CMS_EnvelopedData
*env
)
1056 CMS_OriginatorInfo
*org
= env
->originatorInfo
;
1060 for (i
= 0; i
< sk_CMS_CertificateChoices_num(org
->certificates
); i
++) {
1061 CMS_CertificateChoices
*cch
;
1062 cch
= sk_CMS_CertificateChoices_value(org
->certificates
, i
);
1063 if (cch
->type
== CMS_CERTCHOICE_OTHER
) {
1066 } else if (cch
->type
== CMS_CERTCHOICE_V2ACERT
) {
1067 if (env
->version
< 3)
1072 for (i
= 0; i
< sk_CMS_RevocationInfoChoice_num(org
->crls
); i
++) {
1073 CMS_RevocationInfoChoice
*rch
;
1074 rch
= sk_CMS_RevocationInfoChoice_value(org
->crls
, i
);
1075 if (rch
->type
== CMS_REVCHOICE_OTHER
) {
1082 static void cms_env_set_version(CMS_EnvelopedData
*env
)
1085 CMS_RecipientInfo
*ri
;
1088 * Can't set version higher than 4 so if 4 or more already nothing to do.
1090 if (env
->version
>= 4)
1093 cms_env_set_originfo_version(env
);
1095 if (env
->version
>= 3)
1098 for (i
= 0; i
< sk_CMS_RecipientInfo_num(env
->recipientInfos
); i
++) {
1099 ri
= sk_CMS_RecipientInfo_value(env
->recipientInfos
, i
);
1100 if (ri
->type
== CMS_RECIPINFO_PASS
|| ri
->type
== CMS_RECIPINFO_OTHER
) {
1103 } else if (ri
->type
!= CMS_RECIPINFO_TRANS
1104 || ri
->d
.ktri
->version
!= 0) {
1108 if (env
->originatorInfo
|| env
->unprotectedAttrs
)
1110 if (env
->version
== 2)
1115 static int cms_env_encrypt_content_key(const CMS_ContentInfo
*cms
,
1116 STACK_OF(CMS_RecipientInfo
) *ris
)
1119 CMS_RecipientInfo
*ri
;
1121 for (i
= 0; i
< sk_CMS_RecipientInfo_num(ris
); i
++) {
1122 ri
= sk_CMS_RecipientInfo_value(ris
, i
);
1123 if (CMS_RecipientInfo_encrypt(cms
, ri
) <= 0)
1129 static void cms_env_clear_ec(CMS_EncryptedContentInfo
*ec
)
1132 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
1137 static BIO
*cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo
*cms
)
1139 CMS_EncryptedContentInfo
*ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
1140 BIO
*contentBio
= ossl_cms_EncryptedContent_init_bio(ec
,
1141 ossl_cms_get0_cmsctx(cms
));
1142 EVP_CIPHER_CTX
*ctx
= NULL
;
1144 if (contentBio
== NULL
)
1147 BIO_get_cipher_ctx(contentBio
, &ctx
);
1149 BIO_free(contentBio
);
1153 * If the selected cipher supports unprotected attributes,
1154 * deal with it using special ctrl function
1156 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx
))
1157 & EVP_CIPH_FLAG_CIPHER_WITH_MAC
) != 0
1158 && EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_PROCESS_UNPROTECTED
, 0,
1159 cms
->d
.envelopedData
->unprotectedAttrs
) <= 0) {
1160 BIO_free(contentBio
);
1166 static BIO
*cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo
*cms
)
1168 CMS_EncryptedContentInfo
*ec
;
1169 STACK_OF(CMS_RecipientInfo
) *rinfos
;
1172 CMS_EnvelopedData
*env
= cms
->d
.envelopedData
;
1174 /* Get BIO first to set up key */
1176 ec
= env
->encryptedContentInfo
;
1177 ret
= ossl_cms_EncryptedContent_init_bio(ec
, ossl_cms_get0_cmsctx(cms
));
1179 /* If error end of processing */
1183 /* Now encrypt content key according to each RecipientInfo type */
1184 rinfos
= env
->recipientInfos
;
1185 if (cms_env_encrypt_content_key(cms
, rinfos
) < 0) {
1186 ERR_raise(ERR_LIB_CMS
, CMS_R_ERROR_SETTING_RECIPIENTINFO
);
1190 /* And finally set the version */
1191 cms_env_set_version(env
);
1196 cms_env_clear_ec(ec
);
1203 BIO
*ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo
*cms
)
1205 if (cms
->d
.envelopedData
->encryptedContentInfo
->cipher
!= NULL
) {
1206 /* If cipher is set it's encryption */
1207 return cms_EnvelopedData_Encryption_init_bio(cms
);
1210 /* If cipher is not set it's decryption */
1211 return cms_EnvelopedData_Decryption_init_bio(cms
);
1214 BIO
*ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo
*cms
)
1216 CMS_EncryptedContentInfo
*ec
;
1217 STACK_OF(CMS_RecipientInfo
) *rinfos
;
1220 CMS_AuthEnvelopedData
*aenv
= cms
->d
.authEnvelopedData
;
1222 /* Get BIO first to set up key */
1223 ec
= aenv
->authEncryptedContentInfo
;
1224 /* Set tag for decryption */
1225 if (ec
->cipher
== NULL
) {
1226 ec
->tag
= aenv
->mac
->data
;
1227 ec
->taglen
= aenv
->mac
->length
;
1229 ret
= ossl_cms_EncryptedContent_init_bio(ec
, ossl_cms_get0_cmsctx(cms
));
1231 /* If error or no cipher end of processing */
1232 if (ret
== NULL
|| ec
->cipher
== NULL
)
1235 /* Now encrypt content key according to each RecipientInfo type */
1236 rinfos
= aenv
->recipientInfos
;
1237 if (cms_env_encrypt_content_key(cms
, rinfos
) < 0) {
1238 ERR_raise(ERR_LIB_CMS
, CMS_R_ERROR_SETTING_RECIPIENTINFO
);
1242 /* And finally set the version */
1248 cms_env_clear_ec(ec
);
1255 int ossl_cms_EnvelopedData_final(CMS_ContentInfo
*cms
, BIO
*chain
)
1257 CMS_EnvelopedData
*env
= NULL
;
1258 EVP_CIPHER_CTX
*ctx
= NULL
;
1259 BIO
*mbio
= BIO_find_type(chain
, BIO_TYPE_CIPHER
);
1261 env
= ossl_cms_get0_enveloped(cms
);
1266 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENT_NOT_FOUND
);
1270 BIO_get_cipher_ctx(mbio
, &ctx
);
1273 * If the selected cipher supports unprotected attributes,
1274 * deal with it using special ctrl function
1276 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx
))
1277 & EVP_CIPH_FLAG_CIPHER_WITH_MAC
) != 0) {
1278 if (env
->unprotectedAttrs
== NULL
)
1279 env
->unprotectedAttrs
= sk_X509_ATTRIBUTE_new_null();
1281 if (env
->unprotectedAttrs
== NULL
) {
1282 ERR_raise(ERR_LIB_CMS
, ERR_R_CRYPTO_LIB
);
1286 if (EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_PROCESS_UNPROTECTED
,
1287 1, env
->unprotectedAttrs
) <= 0) {
1288 ERR_raise(ERR_LIB_CMS
, CMS_R_CTRL_FAILURE
);
1293 cms_env_set_version(cms
->d
.envelopedData
);
1297 int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo
*cms
, BIO
*cmsbio
)
1299 EVP_CIPHER_CTX
*ctx
;
1300 unsigned char *tag
= NULL
;
1303 BIO_get_cipher_ctx(cmsbio
, &ctx
);
1306 * The tag is set only for encryption. There is nothing to do for
1309 if (!EVP_CIPHER_CTX_is_encrypting(ctx
))
1312 taglen
= EVP_CIPHER_CTX_get_tag_length(ctx
);
1314 || (tag
= OPENSSL_malloc(taglen
)) == NULL
1315 || EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
, taglen
,
1317 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_GET_TAG
);
1321 if (!ASN1_OCTET_STRING_set(cms
->d
.authEnvelopedData
->mac
, tag
, taglen
))
1331 * Get RecipientInfo type (if any) supported by a key (public or private). To
1332 * retain compatibility with previous behaviour if the ctrl value isn't
1333 * supported we assume key transport.
1335 int ossl_cms_pkey_get_ri_type(EVP_PKEY
*pk
)
1337 /* Check types that we know about */
1338 if (EVP_PKEY_is_a(pk
, "DH"))
1339 return CMS_RECIPINFO_AGREE
;
1340 else if (EVP_PKEY_is_a(pk
, "DHX"))
1341 return CMS_RECIPINFO_AGREE
;
1342 else if (EVP_PKEY_is_a(pk
, "DSA"))
1343 return CMS_RECIPINFO_NONE
;
1344 else if (EVP_PKEY_is_a(pk
, "EC"))
1345 return CMS_RECIPINFO_AGREE
;
1346 else if (EVP_PKEY_is_a(pk
, "RSA"))
1347 return CMS_RECIPINFO_TRANS
;
1350 * Otherwise this might ben an engine implementation, so see if we can get
1351 * the type from the ameth.
1353 if (pk
->ameth
&& pk
->ameth
->pkey_ctrl
) {
1355 i
= pk
->ameth
->pkey_ctrl(pk
, ASN1_PKEY_CTRL_CMS_RI_TYPE
, 0, &r
);
1359 return CMS_RECIPINFO_TRANS
;
1362 int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY
*pk
, int ri_type
)
1364 int supportedRiType
;
1366 if (pk
->ameth
!= NULL
&& pk
->ameth
->pkey_ctrl
!= NULL
) {
1369 i
= pk
->ameth
->pkey_ctrl(pk
, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED
,
1375 supportedRiType
= ossl_cms_pkey_get_ri_type(pk
);
1376 if (supportedRiType
< 0)
1379 return (supportedRiType
== ri_type
);