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_MALLOC_FAILURE
);
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_MALLOC_FAILURE
);
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_MALLOC_FAILURE
);
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
, pkey
, cert
, detached_data
, bio
, flags
);
273 ci
->d
.envelopedData
= NULL
; /* do not indirectly free |env| */
274 CMS_ContentInfo_free(ci
);
283 CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER
*cipher
, OSSL_LIB_CTX
*libctx
,
286 CMS_ContentInfo
*cms
;
287 CMS_AuthEnvelopedData
*aenv
;
289 cms
= CMS_ContentInfo_new_ex(libctx
, propq
);
292 aenv
= cms_auth_enveloped_data_init(cms
);
295 if (!ossl_cms_EncryptedContent_init(aenv
->authEncryptedContentInfo
,
297 ossl_cms_get0_cmsctx(cms
)))
301 CMS_ContentInfo_free(cms
);
302 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
307 CMS_ContentInfo
*CMS_AuthEnvelopedData_create(const EVP_CIPHER
*cipher
)
309 return CMS_AuthEnvelopedData_create_ex(cipher
, NULL
, NULL
);
312 /* Key Transport Recipient Info (KTRI) routines */
314 /* Initialise a ktri based on passed certificate and key */
316 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo
*ri
, X509
*recip
,
317 EVP_PKEY
*pk
, unsigned int flags
,
320 CMS_KeyTransRecipientInfo
*ktri
;
323 ri
->d
.ktri
= M_ASN1_new_of(CMS_KeyTransRecipientInfo
);
326 ri
->type
= CMS_RECIPINFO_TRANS
;
331 if (flags
& CMS_USE_KEYID
) {
333 idtype
= CMS_RECIPINFO_KEYIDENTIFIER
;
336 idtype
= CMS_RECIPINFO_ISSUER_SERIAL
;
340 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
344 if (!ossl_cms_set1_SignerIdentifier(ktri
->rid
, recip
, idtype
, ctx
))
353 if (flags
& CMS_KEY_PARAM
) {
354 ktri
->pctx
= EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx
),
356 ossl_cms_ctx_get0_propq(ctx
));
357 if (ktri
->pctx
== NULL
)
359 if (EVP_PKEY_encrypt_init(ktri
->pctx
) <= 0)
361 } else if (!ossl_cms_env_asn1_ctrl(ri
, 0))
367 * Add a recipient certificate using appropriate type of RecipientInfo
370 CMS_RecipientInfo
*CMS_add1_recipient(CMS_ContentInfo
*cms
, X509
*recip
,
371 EVP_PKEY
*originatorPrivKey
,
372 X509
*originator
, unsigned int flags
)
374 CMS_RecipientInfo
*ri
= NULL
;
375 STACK_OF(CMS_RecipientInfo
) *ris
;
377 const CMS_CTX
*ctx
= ossl_cms_get0_cmsctx(cms
);
379 ris
= CMS_get0_RecipientInfos(cms
);
383 /* Initialize recipient info */
384 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
388 pk
= X509_get0_pubkey(recip
);
390 ERR_raise(ERR_LIB_CMS
, CMS_R_ERROR_GETTING_PUBLIC_KEY
);
394 switch (ossl_cms_pkey_get_ri_type(pk
)) {
396 case CMS_RECIPINFO_TRANS
:
397 if (!cms_RecipientInfo_ktri_init(ri
, recip
, pk
, flags
, ctx
))
401 case CMS_RECIPINFO_AGREE
:
402 if (!ossl_cms_RecipientInfo_kari_init(ri
, recip
, pk
, originator
,
403 originatorPrivKey
, flags
, ctx
))
408 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);
413 if (!sk_CMS_RecipientInfo_push(ris
, ri
))
419 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
421 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
426 CMS_RecipientInfo
*CMS_add1_recipient_cert(CMS_ContentInfo
*cms
, X509
*recip
,
429 return CMS_add1_recipient(cms
, recip
, NULL
, NULL
, flags
);
432 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo
*ri
,
433 EVP_PKEY
**pk
, X509
**recip
,
436 CMS_KeyTransRecipientInfo
*ktri
;
437 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
438 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
447 *recip
= ktri
->recip
;
449 *palg
= ktri
->keyEncryptionAlgorithm
;
453 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo
*ri
,
454 ASN1_OCTET_STRING
**keyid
,
458 CMS_KeyTransRecipientInfo
*ktri
;
459 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
460 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
465 return ossl_cms_SignerIdentifier_get0_signer_id(ktri
->rid
, keyid
, issuer
,
469 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo
*ri
, X509
*cert
)
471 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
472 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
475 return ossl_cms_SignerIdentifier_cert_cmp(ri
->d
.ktri
->rid
, cert
);
478 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo
*ri
, EVP_PKEY
*pkey
)
480 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
481 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
484 EVP_PKEY_free(ri
->d
.ktri
->pkey
);
485 ri
->d
.ktri
->pkey
= pkey
;
489 /* Encrypt content key in key transport recipient info */
491 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo
*cms
,
492 CMS_RecipientInfo
*ri
)
494 CMS_KeyTransRecipientInfo
*ktri
;
495 CMS_EncryptedContentInfo
*ec
;
497 unsigned char *ek
= NULL
;
499 const CMS_CTX
*ctx
= ossl_cms_get0_cmsctx(cms
);
503 if (ri
->type
!= CMS_RECIPINFO_TRANS
) {
504 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEY_TRANSPORT
);
508 ec
= ossl_cms_get0_env_enc_content(cms
);
513 if (!ossl_cms_env_asn1_ctrl(ri
, 0))
516 pctx
= EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx
),
518 ossl_cms_ctx_get0_propq(ctx
));
522 if (EVP_PKEY_encrypt_init(pctx
) <= 0)
526 if (EVP_PKEY_encrypt(pctx
, NULL
, &eklen
, ec
->key
, ec
->keylen
) <= 0)
529 ek
= OPENSSL_malloc(eklen
);
532 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
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_decrypt(ktri
->pctx
, NULL
, &eklen
,
612 ktri
->encryptedKey
->data
,
613 ktri
->encryptedKey
->length
) <= 0)
616 ek
= OPENSSL_malloc(eklen
);
618 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
622 if (EVP_PKEY_decrypt(ktri
->pctx
, ek
, &eklen
,
623 ktri
->encryptedKey
->data
,
624 ktri
->encryptedKey
->length
) <= 0
626 || (fixlen
!= 0 && eklen
!= fixlen
)) {
627 ERR_raise(ERR_LIB_CMS
, CMS_R_CMS_LIB
);
633 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
638 EVP_PKEY_CTX_free(ktri
->pctx
);
646 /* Key Encrypted Key (KEK) RecipientInfo routines */
648 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo
*ri
,
649 const unsigned char *id
, size_t idlen
)
651 ASN1_OCTET_STRING tmp_os
;
652 CMS_KEKRecipientInfo
*kekri
;
653 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
654 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEK
);
658 tmp_os
.type
= V_ASN1_OCTET_STRING
;
660 tmp_os
.data
= (unsigned char *)id
;
661 tmp_os
.length
= (int)idlen
;
662 return ASN1_OCTET_STRING_cmp(&tmp_os
, kekri
->kekid
->keyIdentifier
);
665 /* For now hard code AES key wrap info */
667 static size_t aes_wrap_keylen(int nid
)
670 case NID_id_aes128_wrap
:
673 case NID_id_aes192_wrap
:
676 case NID_id_aes256_wrap
:
684 CMS_RecipientInfo
*CMS_add0_recipient_key(CMS_ContentInfo
*cms
, int nid
,
685 unsigned char *key
, size_t keylen
,
686 unsigned char *id
, size_t idlen
,
687 ASN1_GENERALIZEDTIME
*date
,
688 ASN1_OBJECT
*otherTypeId
,
689 ASN1_TYPE
*otherType
)
691 CMS_RecipientInfo
*ri
= NULL
;
692 CMS_KEKRecipientInfo
*kekri
;
693 STACK_OF(CMS_RecipientInfo
) *ris
= CMS_get0_RecipientInfos(cms
);
698 if (nid
== NID_undef
) {
701 nid
= NID_id_aes128_wrap
;
705 nid
= NID_id_aes192_wrap
;
709 nid
= NID_id_aes256_wrap
;
713 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
719 size_t exp_keylen
= aes_wrap_keylen(nid
);
722 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_KEK_ALGORITHM
);
726 if (keylen
!= exp_keylen
) {
727 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
733 /* Initialize recipient info */
734 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
738 ri
->d
.kekri
= M_ASN1_new_of(CMS_KEKRecipientInfo
);
741 ri
->type
= CMS_RECIPINFO_KEK
;
746 kekri
->kekid
->other
= M_ASN1_new_of(CMS_OtherKeyAttribute
);
747 if (kekri
->kekid
->other
== NULL
)
751 if (!sk_CMS_RecipientInfo_push(ris
, ri
))
754 /* After this point no calls can fail */
759 kekri
->keylen
= keylen
;
761 ASN1_STRING_set0(kekri
->kekid
->keyIdentifier
, id
, idlen
);
763 kekri
->kekid
->date
= date
;
765 if (kekri
->kekid
->other
) {
766 kekri
->kekid
->other
->keyAttrId
= otherTypeId
;
767 kekri
->kekid
->other
->keyAttr
= otherType
;
770 (void)X509_ALGOR_set0(kekri
->keyEncryptionAlgorithm
, OBJ_nid2obj(nid
),
771 V_ASN1_UNDEF
, NULL
); /* cannot fail */
776 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
778 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
782 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo
*ri
,
784 ASN1_OCTET_STRING
**pid
,
785 ASN1_GENERALIZEDTIME
**pdate
,
786 ASN1_OBJECT
**potherid
,
787 ASN1_TYPE
**pothertype
)
789 CMS_KEKIdentifier
*rkid
;
790 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
791 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEK
);
794 rkid
= ri
->d
.kekri
->kekid
;
796 *palg
= ri
->d
.kekri
->keyEncryptionAlgorithm
;
798 *pid
= rkid
->keyIdentifier
;
803 *potherid
= rkid
->other
->keyAttrId
;
809 *pothertype
= rkid
->other
->keyAttr
;
816 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo
*ri
,
817 unsigned char *key
, size_t keylen
)
819 CMS_KEKRecipientInfo
*kekri
;
820 if (ri
->type
!= CMS_RECIPINFO_KEK
) {
821 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_KEK
);
827 kekri
->keylen
= keylen
;
831 static EVP_CIPHER
*cms_get_key_wrap_cipher(size_t keylen
, const CMS_CTX
*ctx
)
833 const char *alg
= NULL
;
837 alg
= "AES-128-WRAP";
840 alg
= "AES-192-WRAP";
843 alg
= "AES-256-WRAP";
848 return EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(ctx
), alg
,
849 ossl_cms_ctx_get0_propq(ctx
));
853 /* Encrypt content key in KEK recipient info */
855 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo
*cms
,
856 CMS_RecipientInfo
*ri
)
858 CMS_EncryptedContentInfo
*ec
;
859 CMS_KEKRecipientInfo
*kekri
;
860 unsigned char *wkey
= NULL
;
863 EVP_CIPHER
*cipher
= NULL
;
865 EVP_CIPHER_CTX
*ctx
= NULL
;
866 const CMS_CTX
*cms_ctx
= ossl_cms_get0_cmsctx(cms
);
868 ec
= ossl_cms_get0_env_enc_content(cms
);
874 if (kekri
->key
== NULL
) {
875 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_KEY
);
879 cipher
= cms_get_key_wrap_cipher(kekri
->keylen
, cms_ctx
);
880 if (cipher
== NULL
) {
881 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
885 /* 8 byte prefix for AES wrap ciphers */
886 wkey
= OPENSSL_malloc(ec
->keylen
+ 8);
888 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
892 ctx
= EVP_CIPHER_CTX_new();
894 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
898 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
899 if (!EVP_EncryptInit_ex(ctx
, cipher
, NULL
, kekri
->key
, NULL
)
900 || !EVP_EncryptUpdate(ctx
, wkey
, &wkeylen
, ec
->key
, ec
->keylen
)
901 || !EVP_EncryptFinal_ex(ctx
, wkey
+ wkeylen
, &outlen
)) {
902 ERR_raise(ERR_LIB_CMS
, CMS_R_WRAP_ERROR
);
906 if (!ossl_assert((size_t)wkeylen
== ec
->keylen
+ 8)) {
907 ERR_raise(ERR_LIB_CMS
, CMS_R_WRAP_ERROR
);
911 ASN1_STRING_set0(kekri
->encryptedKey
, wkey
, wkeylen
);
916 EVP_CIPHER_free(cipher
);
919 EVP_CIPHER_CTX_free(ctx
);
924 /* Decrypt content key in KEK recipient info */
926 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo
*cms
,
927 CMS_RecipientInfo
*ri
)
929 CMS_EncryptedContentInfo
*ec
;
930 CMS_KEKRecipientInfo
*kekri
;
931 unsigned char *ukey
= NULL
;
934 EVP_CIPHER
*cipher
= NULL
;
936 EVP_CIPHER_CTX
*ctx
= NULL
;
937 const CMS_CTX
*cms_ctx
= ossl_cms_get0_cmsctx(cms
);
939 ec
= ossl_cms_get0_env_enc_content(cms
);
946 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_KEY
);
950 wrap_nid
= OBJ_obj2nid(kekri
->keyEncryptionAlgorithm
->algorithm
);
951 if (aes_wrap_keylen(wrap_nid
) != kekri
->keylen
) {
952 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
956 /* If encrypted key length is invalid don't bother */
958 if (kekri
->encryptedKey
->length
< 16) {
959 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH
);
963 cipher
= cms_get_key_wrap_cipher(kekri
->keylen
, cms_ctx
);
964 if (cipher
== NULL
) {
965 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
969 ukey
= OPENSSL_malloc(kekri
->encryptedKey
->length
- 8);
971 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
975 ctx
= EVP_CIPHER_CTX_new();
977 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
981 if (!EVP_DecryptInit_ex(ctx
, cipher
, NULL
, kekri
->key
, NULL
)
982 || !EVP_DecryptUpdate(ctx
, ukey
, &ukeylen
,
983 kekri
->encryptedKey
->data
,
984 kekri
->encryptedKey
->length
)
985 || !EVP_DecryptFinal_ex(ctx
, ukey
+ ukeylen
, &outlen
)) {
986 ERR_raise(ERR_LIB_CMS
, CMS_R_UNWRAP_ERROR
);
991 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
993 ec
->keylen
= ukeylen
;
998 EVP_CIPHER_free(cipher
);
1001 EVP_CIPHER_CTX_free(ctx
);
1006 int CMS_RecipientInfo_decrypt(CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
)
1009 case CMS_RECIPINFO_TRANS
:
1010 return cms_RecipientInfo_ktri_decrypt(cms
, ri
);
1012 case CMS_RECIPINFO_KEK
:
1013 return cms_RecipientInfo_kekri_decrypt(cms
, ri
);
1015 case CMS_RECIPINFO_PASS
:
1016 return ossl_cms_RecipientInfo_pwri_crypt(cms
, ri
, 0);
1019 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE
);
1024 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
)
1027 case CMS_RECIPINFO_TRANS
:
1028 return cms_RecipientInfo_ktri_encrypt(cms
, ri
);
1030 case CMS_RECIPINFO_AGREE
:
1031 return ossl_cms_RecipientInfo_kari_encrypt(cms
, ri
);
1033 case CMS_RECIPINFO_KEK
:
1034 return cms_RecipientInfo_kekri_encrypt(cms
, ri
);
1036 case CMS_RECIPINFO_PASS
:
1037 return ossl_cms_RecipientInfo_pwri_crypt(cms
, ri
, 1);
1040 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_RECIPIENT_TYPE
);
1045 /* Check structures and fixup version numbers (if necessary) */
1047 static void cms_env_set_originfo_version(CMS_EnvelopedData
*env
)
1049 CMS_OriginatorInfo
*org
= env
->originatorInfo
;
1053 for (i
= 0; i
< sk_CMS_CertificateChoices_num(org
->certificates
); i
++) {
1054 CMS_CertificateChoices
*cch
;
1055 cch
= sk_CMS_CertificateChoices_value(org
->certificates
, i
);
1056 if (cch
->type
== CMS_CERTCHOICE_OTHER
) {
1059 } else if (cch
->type
== CMS_CERTCHOICE_V2ACERT
) {
1060 if (env
->version
< 3)
1065 for (i
= 0; i
< sk_CMS_RevocationInfoChoice_num(org
->crls
); i
++) {
1066 CMS_RevocationInfoChoice
*rch
;
1067 rch
= sk_CMS_RevocationInfoChoice_value(org
->crls
, i
);
1068 if (rch
->type
== CMS_REVCHOICE_OTHER
) {
1075 static void cms_env_set_version(CMS_EnvelopedData
*env
)
1078 CMS_RecipientInfo
*ri
;
1081 * Can't set version higher than 4 so if 4 or more already nothing to do.
1083 if (env
->version
>= 4)
1086 cms_env_set_originfo_version(env
);
1088 if (env
->version
>= 3)
1091 for (i
= 0; i
< sk_CMS_RecipientInfo_num(env
->recipientInfos
); i
++) {
1092 ri
= sk_CMS_RecipientInfo_value(env
->recipientInfos
, i
);
1093 if (ri
->type
== CMS_RECIPINFO_PASS
|| ri
->type
== CMS_RECIPINFO_OTHER
) {
1096 } else if (ri
->type
!= CMS_RECIPINFO_TRANS
1097 || ri
->d
.ktri
->version
!= 0) {
1101 if (env
->originatorInfo
|| env
->unprotectedAttrs
)
1103 if (env
->version
== 2)
1108 static int cms_env_encrypt_content_key(const CMS_ContentInfo
*cms
,
1109 STACK_OF(CMS_RecipientInfo
) *ris
)
1112 CMS_RecipientInfo
*ri
;
1114 for (i
= 0; i
< sk_CMS_RecipientInfo_num(ris
); i
++) {
1115 ri
= sk_CMS_RecipientInfo_value(ris
, i
);
1116 if (CMS_RecipientInfo_encrypt(cms
, ri
) <= 0)
1122 static void cms_env_clear_ec(CMS_EncryptedContentInfo
*ec
)
1125 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
1130 static BIO
*cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo
*cms
)
1132 CMS_EncryptedContentInfo
*ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
1133 BIO
*contentBio
= ossl_cms_EncryptedContent_init_bio(ec
,
1134 ossl_cms_get0_cmsctx(cms
));
1135 EVP_CIPHER_CTX
*ctx
= NULL
;
1137 if (contentBio
== NULL
)
1140 BIO_get_cipher_ctx(contentBio
, &ctx
);
1142 BIO_free(contentBio
);
1146 * If the selected cipher supports unprotected attributes,
1147 * deal with it using special ctrl function
1149 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx
))
1150 & EVP_CIPH_FLAG_CIPHER_WITH_MAC
) != 0
1151 && EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_PROCESS_UNPROTECTED
, 0,
1152 cms
->d
.envelopedData
->unprotectedAttrs
) <= 0) {
1153 BIO_free(contentBio
);
1159 static BIO
*cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo
*cms
)
1161 CMS_EncryptedContentInfo
*ec
;
1162 STACK_OF(CMS_RecipientInfo
) *rinfos
;
1165 CMS_EnvelopedData
*env
= cms
->d
.envelopedData
;
1167 /* Get BIO first to set up key */
1169 ec
= env
->encryptedContentInfo
;
1170 ret
= ossl_cms_EncryptedContent_init_bio(ec
, ossl_cms_get0_cmsctx(cms
));
1172 /* If error end of processing */
1176 /* Now encrypt content key according to each RecipientInfo type */
1177 rinfos
= env
->recipientInfos
;
1178 if (cms_env_encrypt_content_key(cms
, rinfos
) < 0) {
1179 ERR_raise(ERR_LIB_CMS
, CMS_R_ERROR_SETTING_RECIPIENTINFO
);
1183 /* And finally set the version */
1184 cms_env_set_version(env
);
1189 cms_env_clear_ec(ec
);
1196 BIO
*ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo
*cms
)
1198 if (cms
->d
.envelopedData
->encryptedContentInfo
->cipher
!= NULL
) {
1199 /* If cipher is set it's encryption */
1200 return cms_EnvelopedData_Encryption_init_bio(cms
);
1203 /* If cipher is not set it's decryption */
1204 return cms_EnvelopedData_Decryption_init_bio(cms
);
1207 BIO
*ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo
*cms
)
1209 CMS_EncryptedContentInfo
*ec
;
1210 STACK_OF(CMS_RecipientInfo
) *rinfos
;
1213 CMS_AuthEnvelopedData
*aenv
= cms
->d
.authEnvelopedData
;
1215 /* Get BIO first to set up key */
1216 ec
= aenv
->authEncryptedContentInfo
;
1217 /* Set tag for decryption */
1218 if (ec
->cipher
== NULL
) {
1219 ec
->tag
= aenv
->mac
->data
;
1220 ec
->taglen
= aenv
->mac
->length
;
1222 ret
= ossl_cms_EncryptedContent_init_bio(ec
, ossl_cms_get0_cmsctx(cms
));
1224 /* If error or no cipher end of processing */
1225 if (ret
== NULL
|| ec
->cipher
== NULL
)
1228 /* Now encrypt content key according to each RecipientInfo type */
1229 rinfos
= aenv
->recipientInfos
;
1230 if (cms_env_encrypt_content_key(cms
, rinfos
) < 0) {
1231 ERR_raise(ERR_LIB_CMS
, CMS_R_ERROR_SETTING_RECIPIENTINFO
);
1235 /* And finally set the version */
1241 cms_env_clear_ec(ec
);
1248 int ossl_cms_EnvelopedData_final(CMS_ContentInfo
*cms
, BIO
*chain
)
1250 CMS_EnvelopedData
*env
= NULL
;
1251 EVP_CIPHER_CTX
*ctx
= NULL
;
1252 BIO
*mbio
= BIO_find_type(chain
, BIO_TYPE_CIPHER
);
1254 env
= ossl_cms_get0_enveloped(cms
);
1259 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENT_NOT_FOUND
);
1263 BIO_get_cipher_ctx(mbio
, &ctx
);
1266 * If the selected cipher supports unprotected attributes,
1267 * deal with it using special ctrl function
1269 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx
))
1270 & EVP_CIPH_FLAG_CIPHER_WITH_MAC
) != 0) {
1271 if (env
->unprotectedAttrs
== NULL
)
1272 env
->unprotectedAttrs
= sk_X509_ATTRIBUTE_new_null();
1274 if (env
->unprotectedAttrs
== NULL
) {
1275 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
1279 if (EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_PROCESS_UNPROTECTED
,
1280 1, env
->unprotectedAttrs
) <= 0) {
1281 ERR_raise(ERR_LIB_CMS
, CMS_R_CTRL_FAILURE
);
1286 cms_env_set_version(cms
->d
.envelopedData
);
1290 int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo
*cms
, BIO
*cmsbio
)
1292 EVP_CIPHER_CTX
*ctx
;
1293 unsigned char *tag
= NULL
;
1296 BIO_get_cipher_ctx(cmsbio
, &ctx
);
1299 * The tag is set only for encryption. There is nothing to do for
1302 if (!EVP_CIPHER_CTX_is_encrypting(ctx
))
1305 taglen
= EVP_CIPHER_CTX_get_tag_length(ctx
);
1307 || (tag
= OPENSSL_malloc(taglen
)) == NULL
1308 || EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
, taglen
,
1310 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_GET_TAG
);
1314 if (!ASN1_OCTET_STRING_set(cms
->d
.authEnvelopedData
->mac
, tag
, taglen
))
1324 * Get RecipientInfo type (if any) supported by a key (public or private). To
1325 * retain compatibility with previous behaviour if the ctrl value isn't
1326 * supported we assume key transport.
1328 int ossl_cms_pkey_get_ri_type(EVP_PKEY
*pk
)
1330 /* Check types that we know about */
1331 if (EVP_PKEY_is_a(pk
, "DH"))
1332 return CMS_RECIPINFO_AGREE
;
1333 else if (EVP_PKEY_is_a(pk
, "DHX"))
1334 return CMS_RECIPINFO_AGREE
;
1335 else if (EVP_PKEY_is_a(pk
, "DSA"))
1336 return CMS_RECIPINFO_NONE
;
1337 else if (EVP_PKEY_is_a(pk
, "EC"))
1338 return CMS_RECIPINFO_AGREE
;
1339 else if (EVP_PKEY_is_a(pk
, "RSA"))
1340 return CMS_RECIPINFO_TRANS
;
1343 * Otherwise this might ben an engine implementation, so see if we can get
1344 * the type from the ameth.
1346 if (pk
->ameth
&& pk
->ameth
->pkey_ctrl
) {
1348 i
= pk
->ameth
->pkey_ctrl(pk
, ASN1_PKEY_CTRL_CMS_RI_TYPE
, 0, &r
);
1352 return CMS_RECIPINFO_TRANS
;
1355 int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY
*pk
, int ri_type
)
1357 int supportedRiType
;
1359 if (pk
->ameth
!= NULL
&& pk
->ameth
->pkey_ctrl
!= NULL
) {
1362 i
= pk
->ameth
->pkey_ctrl(pk
, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED
,
1368 supportedRiType
= ossl_cms_pkey_get_ri_type(pk
);
1369 if (supportedRiType
< 0)
1372 return (supportedRiType
== ri_type
);