2 * Copyright 2008-2023 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_simple(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;
45 static int cms_get_enveloped_type(const CMS_ContentInfo *cms)
47 int ret = cms_get_enveloped_type_simple(cms);
50 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
54 CMS_EnvelopedData *ossl_cms_get0_enveloped(CMS_ContentInfo *cms)
56 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
57 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
60 return cms->d.envelopedData;
63 CMS_AuthEnvelopedData *ossl_cms_get0_auth_enveloped(CMS_ContentInfo *cms)
65 if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_authEnvelopedData) {
66 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
69 return cms->d.authEnvelopedData;
72 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
74 if (cms->d.other == NULL) {
75 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
76 if (cms->d.envelopedData == NULL) {
77 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
80 cms->d.envelopedData->version = 0;
81 cms->d.envelopedData->encryptedContentInfo->contentType =
82 OBJ_nid2obj(NID_pkcs7_data);
83 ASN1_OBJECT_free(cms->contentType);
84 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
85 return cms->d.envelopedData;
87 return ossl_cms_get0_enveloped(cms);
90 static CMS_AuthEnvelopedData *
91 cms_auth_enveloped_data_init(CMS_ContentInfo *cms)
93 if (cms->d.other == NULL) {
94 cms->d.authEnvelopedData = M_ASN1_new_of(CMS_AuthEnvelopedData);
95 if (cms->d.authEnvelopedData == NULL) {
96 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
99 /* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */
100 cms->d.authEnvelopedData->version = 0;
101 cms->d.authEnvelopedData->authEncryptedContentInfo->contentType =
102 OBJ_nid2obj(NID_pkcs7_data);
103 ASN1_OBJECT_free(cms->contentType);
104 cms->contentType = OBJ_nid2obj(NID_id_smime_ct_authEnvelopedData);
105 return cms->d.authEnvelopedData;
107 return ossl_cms_get0_auth_enveloped(cms);
110 int ossl_cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
114 if (ri->type == CMS_RECIPINFO_TRANS)
115 pkey = ri->d.ktri->pkey;
116 else if (ri->type == CMS_RECIPINFO_AGREE) {
117 EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
121 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
127 if (EVP_PKEY_is_a(pkey, "DHX") || EVP_PKEY_is_a(pkey, "DH"))
128 return ossl_cms_dh_envelope(ri, cmd);
129 else if (EVP_PKEY_is_a(pkey, "EC"))
130 return ossl_cms_ecdh_envelope(ri, cmd);
131 else if (EVP_PKEY_is_a(pkey, "RSA"))
132 return ossl_cms_rsa_envelope(ri, cmd);
134 /* Something else? We'll give engines etc a chance to handle this */
135 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
137 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
139 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
143 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
149 CMS_EncryptedContentInfo *ossl_cms_get0_env_enc_content(const CMS_ContentInfo *cms)
151 switch (cms_get_enveloped_type(cms)) {
152 case CMS_ENVELOPED_STANDARD:
153 return cms->d.envelopedData == NULL ? NULL
154 : cms->d.envelopedData->encryptedContentInfo;
156 case CMS_ENVELOPED_AUTH:
157 return cms->d.authEnvelopedData == NULL ? NULL
158 : cms->d.authEnvelopedData->authEncryptedContentInfo;
165 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
167 switch (cms_get_enveloped_type(cms)) {
168 case CMS_ENVELOPED_STANDARD:
169 return cms->d.envelopedData->recipientInfos;
171 case CMS_ENVELOPED_AUTH:
172 return cms->d.authEnvelopedData->recipientInfos;
179 void ossl_cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms)
182 CMS_RecipientInfo *ri;
183 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
184 STACK_OF(CMS_RecipientInfo) *rinfos = CMS_get0_RecipientInfos(cms);
186 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
187 ri = sk_CMS_RecipientInfo_value(rinfos, i);
190 case CMS_RECIPINFO_AGREE:
191 ri->d.kari->cms_ctx = ctx;
193 case CMS_RECIPINFO_TRANS:
194 ri->d.ktri->cms_ctx = ctx;
195 ossl_x509_set0_libctx(ri->d.ktri->recip,
196 ossl_cms_ctx_get0_libctx(ctx),
197 ossl_cms_ctx_get0_propq(ctx));
199 case CMS_RECIPINFO_KEK:
200 ri->d.kekri->cms_ctx = ctx;
202 case CMS_RECIPINFO_PASS:
203 ri->d.pwri->cms_ctx = ctx;
212 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
217 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
219 if (ri->type == CMS_RECIPINFO_TRANS)
220 return ri->d.ktri->pctx;
221 else if (ri->type == CMS_RECIPINFO_AGREE)
222 return ri->d.kari->pctx;
226 CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher,
227 OSSL_LIB_CTX *libctx,
230 CMS_ContentInfo *cms;
231 CMS_EnvelopedData *env;
233 cms = CMS_ContentInfo_new_ex(libctx, propq);
236 env = cms_enveloped_data_init(cms);
240 if (!ossl_cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL,
241 0, ossl_cms_get0_cmsctx(cms)))
245 CMS_ContentInfo_free(cms);
246 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
250 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
252 return CMS_EnvelopedData_create_ex(cipher, NULL, NULL);
255 BIO *CMS_EnvelopedData_decrypt(CMS_EnvelopedData *env, BIO *detached_data,
256 EVP_PKEY *pkey, X509 *cert,
257 ASN1_OCTET_STRING *secret, unsigned int flags,
258 OSSL_LIB_CTX *libctx, const char *propq)
265 ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER);
269 if ((ci = CMS_ContentInfo_new_ex(libctx, propq)) == NULL
270 || (bio = BIO_new(BIO_s_mem())) == NULL)
272 ci->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
273 ci->d.envelopedData = env;
275 && CMS_decrypt_set1_password(ci, (unsigned char *)
276 ASN1_STRING_get0_data(secret),
277 ASN1_STRING_length(secret)) != 1)
279 res = CMS_decrypt(ci, secret == NULL ? pkey : NULL,
280 secret == NULL ? cert : NULL, detached_data, bio, flags);
284 ci->d.envelopedData = NULL; /* do not indirectly free |env| */
285 ci->contentType = NULL;
287 CMS_ContentInfo_free(ci);
296 CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx,
299 CMS_ContentInfo *cms;
300 CMS_AuthEnvelopedData *aenv;
302 cms = CMS_ContentInfo_new_ex(libctx, propq);
305 aenv = cms_auth_enveloped_data_init(cms);
308 if (!ossl_cms_EncryptedContent_init(aenv->authEncryptedContentInfo,
310 ossl_cms_get0_cmsctx(cms)))
314 CMS_ContentInfo_free(cms);
315 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
320 CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher)
322 return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL);
325 /* Key Transport Recipient Info (KTRI) routines */
327 /* Initialise a ktri based on passed certificate and key */
329 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
330 EVP_PKEY *pk, unsigned int flags,
333 CMS_KeyTransRecipientInfo *ktri;
336 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
339 ri->type = CMS_RECIPINFO_TRANS;
344 if (flags & CMS_USE_KEYID) {
346 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
349 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
353 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
357 if (!ossl_cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx))
366 if (flags & CMS_KEY_PARAM) {
367 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
369 ossl_cms_ctx_get0_propq(ctx));
370 if (ktri->pctx == NULL)
372 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
374 } else if (!ossl_cms_env_asn1_ctrl(ri, 0))
380 * Add a recipient certificate using appropriate type of RecipientInfo
383 CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
384 EVP_PKEY *originatorPrivKey,
385 X509 *originator, unsigned int flags)
387 CMS_RecipientInfo *ri = NULL;
388 STACK_OF(CMS_RecipientInfo) *ris;
390 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
392 ris = CMS_get0_RecipientInfos(cms);
396 /* Initialize recipient info */
397 ri = M_ASN1_new_of(CMS_RecipientInfo);
399 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
403 pk = X509_get0_pubkey(recip);
405 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY);
409 switch (ossl_cms_pkey_get_ri_type(pk)) {
411 case CMS_RECIPINFO_TRANS:
412 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx))
416 case CMS_RECIPINFO_AGREE:
417 if (!ossl_cms_RecipientInfo_kari_init(ri, recip, pk, originator,
418 originatorPrivKey, flags, ctx))
423 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
428 if (!sk_CMS_RecipientInfo_push(ris, ri)) {
429 ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
436 M_ASN1_free_of(ri, CMS_RecipientInfo);
441 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
444 return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
447 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
448 EVP_PKEY **pk, X509 **recip,
451 CMS_KeyTransRecipientInfo *ktri;
452 if (ri->type != CMS_RECIPINFO_TRANS) {
453 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
462 *recip = ktri->recip;
464 *palg = ktri->keyEncryptionAlgorithm;
468 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
469 ASN1_OCTET_STRING **keyid,
473 CMS_KeyTransRecipientInfo *ktri;
474 if (ri->type != CMS_RECIPINFO_TRANS) {
475 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
480 return ossl_cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer,
484 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
486 if (ri->type != CMS_RECIPINFO_TRANS) {
487 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
490 return ossl_cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
493 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
495 if (ri->type != CMS_RECIPINFO_TRANS) {
496 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
499 EVP_PKEY_free(ri->d.ktri->pkey);
500 ri->d.ktri->pkey = pkey;
504 /* Encrypt content key in key transport recipient info */
506 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
507 CMS_RecipientInfo *ri)
509 CMS_KeyTransRecipientInfo *ktri;
510 CMS_EncryptedContentInfo *ec;
512 unsigned char *ek = NULL;
514 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
518 if (ri->type != CMS_RECIPINFO_TRANS) {
519 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
523 ec = ossl_cms_get0_env_enc_content(cms);
528 if (!ossl_cms_env_asn1_ctrl(ri, 0))
531 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
533 ossl_cms_ctx_get0_propq(ctx));
537 if (EVP_PKEY_encrypt_init(pctx) <= 0)
541 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
544 ek = OPENSSL_malloc(eklen);
548 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
551 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
557 EVP_PKEY_CTX_free(pctx);
563 /* Decrypt content key from KTRI */
565 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
566 CMS_RecipientInfo *ri)
568 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
569 EVP_PKEY *pkey = ktri->pkey;
570 unsigned char *ek = NULL;
574 const EVP_CIPHER *cipher = NULL;
575 EVP_CIPHER *fetched_cipher = NULL;
576 CMS_EncryptedContentInfo *ec;
577 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
578 OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
579 const char *propq = ossl_cms_ctx_get0_propq(ctx);
581 ec = ossl_cms_get0_env_enc_content(cms);
583 if (ktri->pkey == NULL) {
584 ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
588 if (cms->d.envelopedData->encryptedContentInfo->havenocert
589 && !cms->d.envelopedData->encryptedContentInfo->debug) {
590 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
591 char name[OSSL_MAX_NAME_SIZE];
593 OBJ_obj2txt(name, sizeof(name), calg->algorithm, 0);
595 (void)ERR_set_mark();
596 fetched_cipher = EVP_CIPHER_fetch(libctx, name, propq);
598 if (fetched_cipher != NULL)
599 cipher = fetched_cipher;
601 cipher = EVP_get_cipherbyobj(calg->algorithm);
602 if (cipher == NULL) {
603 (void)ERR_clear_last_mark();
604 ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
607 (void)ERR_pop_to_mark();
609 fixlen = EVP_CIPHER_get_key_length(cipher);
610 EVP_CIPHER_free(fetched_cipher);
613 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
614 if (ktri->pctx == NULL)
617 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
620 if (!ossl_cms_env_asn1_ctrl(ri, 1))
623 if (EVP_PKEY_is_a(pkey, "RSA"))
624 /* upper layer CMS code incorrectly assumes that a successful RSA
625 * decryption means that the key matches ciphertext (which never
626 * was the case, implicit rejection or not), so to make it work
627 * disable implicit rejection for RSA keys */
628 EVP_PKEY_CTX_ctrl_str(ktri->pctx, "rsa_pkcs1_implicit_rejection", "0");
630 if (evp_pkey_decrypt_alloc(ktri->pctx, &ek, &eklen, fixlen,
631 ktri->encryptedKey->data,
632 ktri->encryptedKey->length) <= 0)
637 OPENSSL_clear_free(ec->key, ec->keylen);
642 EVP_PKEY_CTX_free(ktri->pctx);
650 /* Key Encrypted Key (KEK) RecipientInfo routines */
652 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
653 const unsigned char *id, size_t idlen)
655 ASN1_OCTET_STRING tmp_os;
656 CMS_KEKRecipientInfo *kekri;
657 if (ri->type != CMS_RECIPINFO_KEK) {
658 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
662 tmp_os.type = V_ASN1_OCTET_STRING;
664 tmp_os.data = (unsigned char *)id;
665 tmp_os.length = (int)idlen;
666 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
669 /* For now hard code AES key wrap info */
671 static size_t aes_wrap_keylen(int nid)
674 case NID_id_aes128_wrap:
677 case NID_id_aes192_wrap:
680 case NID_id_aes256_wrap:
688 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
689 unsigned char *key, size_t keylen,
690 unsigned char *id, size_t idlen,
691 ASN1_GENERALIZEDTIME *date,
692 ASN1_OBJECT *otherTypeId,
693 ASN1_TYPE *otherType)
695 CMS_RecipientInfo *ri = NULL;
696 CMS_KEKRecipientInfo *kekri;
697 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
702 if (nid == NID_undef) {
705 nid = NID_id_aes128_wrap;
709 nid = NID_id_aes192_wrap;
713 nid = NID_id_aes256_wrap;
717 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
723 size_t exp_keylen = aes_wrap_keylen(nid);
726 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM);
730 if (keylen != exp_keylen) {
731 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
737 /* Initialize recipient info */
738 ri = M_ASN1_new_of(CMS_RecipientInfo);
740 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
744 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
746 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
749 ri->type = CMS_RECIPINFO_KEK;
754 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
755 if (kekri->kekid->other == NULL) {
756 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
761 if (!sk_CMS_RecipientInfo_push(ris, ri)) {
762 ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
766 /* After this point no calls can fail */
771 kekri->keylen = keylen;
773 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
775 kekri->kekid->date = date;
777 if (kekri->kekid->other) {
778 kekri->kekid->other->keyAttrId = otherTypeId;
779 kekri->kekid->other->keyAttr = otherType;
782 (void)X509_ALGOR_set0(kekri->keyEncryptionAlgorithm, OBJ_nid2obj(nid),
783 V_ASN1_UNDEF, NULL); /* cannot fail */
788 M_ASN1_free_of(ri, CMS_RecipientInfo);
792 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
794 ASN1_OCTET_STRING **pid,
795 ASN1_GENERALIZEDTIME **pdate,
796 ASN1_OBJECT **potherid,
797 ASN1_TYPE **pothertype)
799 CMS_KEKIdentifier *rkid;
800 if (ri->type != CMS_RECIPINFO_KEK) {
801 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
804 rkid = ri->d.kekri->kekid;
806 *palg = ri->d.kekri->keyEncryptionAlgorithm;
808 *pid = rkid->keyIdentifier;
813 *potherid = rkid->other->keyAttrId;
819 *pothertype = rkid->other->keyAttr;
826 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
827 unsigned char *key, size_t keylen)
829 CMS_KEKRecipientInfo *kekri;
830 if (ri->type != CMS_RECIPINFO_KEK) {
831 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
837 kekri->keylen = keylen;
841 static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
843 const char *alg = NULL;
847 alg = "AES-128-WRAP";
850 alg = "AES-192-WRAP";
853 alg = "AES-256-WRAP";
858 return EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
859 ossl_cms_ctx_get0_propq(ctx));
863 /* Encrypt content key in KEK recipient info */
865 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
866 CMS_RecipientInfo *ri)
868 CMS_EncryptedContentInfo *ec;
869 CMS_KEKRecipientInfo *kekri;
870 unsigned char *wkey = NULL;
873 EVP_CIPHER *cipher = NULL;
875 EVP_CIPHER_CTX *ctx = NULL;
876 const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
878 ec = ossl_cms_get0_env_enc_content(cms);
884 if (kekri->key == NULL) {
885 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
889 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
890 if (cipher == NULL) {
891 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
895 /* 8 byte prefix for AES wrap ciphers */
896 wkey = OPENSSL_malloc(ec->keylen + 8);
900 ctx = EVP_CIPHER_CTX_new();
902 ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
906 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
907 if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
908 || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
909 || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
910 ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
914 if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
915 ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
919 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
924 EVP_CIPHER_free(cipher);
927 EVP_CIPHER_CTX_free(ctx);
932 /* Decrypt content key in KEK recipient info */
934 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
935 CMS_RecipientInfo *ri)
937 CMS_EncryptedContentInfo *ec;
938 CMS_KEKRecipientInfo *kekri;
939 unsigned char *ukey = NULL;
942 EVP_CIPHER *cipher = NULL;
944 EVP_CIPHER_CTX *ctx = NULL;
945 const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
947 ec = ossl_cms_get0_env_enc_content(cms);
954 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
958 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
959 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
960 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
964 /* If encrypted key length is invalid don't bother */
966 if (kekri->encryptedKey->length < 16) {
967 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
971 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
972 if (cipher == NULL) {
973 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
977 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
981 ctx = EVP_CIPHER_CTX_new();
983 ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
987 if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
988 || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
989 kekri->encryptedKey->data,
990 kekri->encryptedKey->length)
991 || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
992 ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR);
997 OPENSSL_clear_free(ec->key, ec->keylen);
999 ec->keylen = ukeylen;
1004 EVP_CIPHER_free(cipher);
1007 EVP_CIPHER_CTX_free(ctx);
1012 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
1015 case CMS_RECIPINFO_TRANS:
1016 return cms_RecipientInfo_ktri_decrypt(cms, ri);
1018 case CMS_RECIPINFO_KEK:
1019 return cms_RecipientInfo_kekri_decrypt(cms, ri);
1021 case CMS_RECIPINFO_PASS:
1022 return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 0);
1025 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
1030 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
1033 case CMS_RECIPINFO_TRANS:
1034 return cms_RecipientInfo_ktri_encrypt(cms, ri);
1036 case CMS_RECIPINFO_AGREE:
1037 return ossl_cms_RecipientInfo_kari_encrypt(cms, ri);
1039 case CMS_RECIPINFO_KEK:
1040 return cms_RecipientInfo_kekri_encrypt(cms, ri);
1042 case CMS_RECIPINFO_PASS:
1043 return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 1);
1046 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
1051 /* Check structures and fixup version numbers (if necessary) */
1053 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
1055 CMS_OriginatorInfo *org = env->originatorInfo;
1059 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
1060 CMS_CertificateChoices *cch;
1061 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
1062 if (cch->type == CMS_CERTCHOICE_OTHER) {
1065 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
1066 if (env->version < 3)
1071 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
1072 CMS_RevocationInfoChoice *rch;
1073 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
1074 if (rch->type == CMS_REVCHOICE_OTHER) {
1081 static void cms_env_set_version(CMS_EnvelopedData *env)
1084 CMS_RecipientInfo *ri;
1087 * Can't set version higher than 4 so if 4 or more already nothing to do.
1089 if (env->version >= 4)
1092 cms_env_set_originfo_version(env);
1094 if (env->version >= 3)
1097 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
1098 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
1099 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
1102 } else if (ri->type != CMS_RECIPINFO_TRANS
1103 || ri->d.ktri->version != 0) {
1107 if (env->originatorInfo || env->unprotectedAttrs)
1109 if (env->version == 2)
1114 static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms,
1115 STACK_OF(CMS_RecipientInfo) *ris)
1118 CMS_RecipientInfo *ri;
1120 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
1121 ri = sk_CMS_RecipientInfo_value(ris, i);
1122 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
1128 static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec)
1131 OPENSSL_clear_free(ec->key, ec->keylen);
1136 static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
1138 CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
1139 BIO *contentBio = ossl_cms_EncryptedContent_init_bio(ec,
1140 ossl_cms_get0_cmsctx(cms));
1141 EVP_CIPHER_CTX *ctx = NULL;
1143 if (contentBio == NULL)
1146 BIO_get_cipher_ctx(contentBio, &ctx);
1148 BIO_free(contentBio);
1152 * If the selected cipher supports unprotected attributes,
1153 * deal with it using special ctrl function
1155 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
1156 & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0
1157 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
1158 cms->d.envelopedData->unprotectedAttrs) <= 0) {
1159 BIO_free(contentBio);
1165 static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
1167 CMS_EncryptedContentInfo *ec;
1168 STACK_OF(CMS_RecipientInfo) *rinfos;
1171 CMS_EnvelopedData *env = cms->d.envelopedData;
1173 /* Get BIO first to set up key */
1175 ec = env->encryptedContentInfo;
1176 ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
1178 /* If error end of processing */
1182 /* Now encrypt content key according to each RecipientInfo type */
1183 rinfos = env->recipientInfos;
1184 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1185 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1189 /* And finally set the version */
1190 cms_env_set_version(env);
1195 cms_env_clear_ec(ec);
1202 BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
1204 if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
1205 /* If cipher is set it's encryption */
1206 return cms_EnvelopedData_Encryption_init_bio(cms);
1209 /* If cipher is not set it's decryption */
1210 return cms_EnvelopedData_Decryption_init_bio(cms);
1213 BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms)
1215 CMS_EncryptedContentInfo *ec;
1216 STACK_OF(CMS_RecipientInfo) *rinfos;
1219 CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData;
1221 /* Get BIO first to set up key */
1222 ec = aenv->authEncryptedContentInfo;
1223 /* Set tag for decryption */
1224 if (ec->cipher == NULL) {
1225 ec->tag = aenv->mac->data;
1226 ec->taglen = aenv->mac->length;
1228 ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
1230 /* If error or no cipher end of processing */
1231 if (ret == NULL || ec->cipher == NULL)
1234 /* Now encrypt content key according to each RecipientInfo type */
1235 rinfos = aenv->recipientInfos;
1236 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1237 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1241 /* And finally set the version */
1247 cms_env_clear_ec(ec);
1254 int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
1256 CMS_EnvelopedData *env = NULL;
1257 EVP_CIPHER_CTX *ctx = NULL;
1258 BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
1260 env = ossl_cms_get0_enveloped(cms);
1265 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
1269 BIO_get_cipher_ctx(mbio, &ctx);
1272 * If the selected cipher supports unprotected attributes,
1273 * deal with it using special ctrl function
1275 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
1276 & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
1277 if (env->unprotectedAttrs == NULL)
1278 env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
1280 if (env->unprotectedAttrs == NULL) {
1281 ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
1285 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
1286 1, env->unprotectedAttrs) <= 0) {
1287 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
1292 cms_env_set_version(cms->d.envelopedData);
1296 int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio)
1298 EVP_CIPHER_CTX *ctx;
1299 unsigned char *tag = NULL;
1302 BIO_get_cipher_ctx(cmsbio, &ctx);
1305 * The tag is set only for encryption. There is nothing to do for
1308 if (!EVP_CIPHER_CTX_is_encrypting(ctx))
1311 taglen = EVP_CIPHER_CTX_get_tag_length(ctx);
1313 || (tag = OPENSSL_malloc(taglen)) == NULL
1314 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
1316 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG);
1320 if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen))
1330 * Get RecipientInfo type (if any) supported by a key (public or private). To
1331 * retain compatibility with previous behaviour if the ctrl value isn't
1332 * supported we assume key transport.
1334 int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk)
1336 /* Check types that we know about */
1337 if (EVP_PKEY_is_a(pk, "DH"))
1338 return CMS_RECIPINFO_AGREE;
1339 else if (EVP_PKEY_is_a(pk, "DHX"))
1340 return CMS_RECIPINFO_AGREE;
1341 else if (EVP_PKEY_is_a(pk, "DSA"))
1342 return CMS_RECIPINFO_NONE;
1343 else if (EVP_PKEY_is_a(pk, "EC"))
1344 return CMS_RECIPINFO_AGREE;
1345 else if (EVP_PKEY_is_a(pk, "RSA"))
1346 return CMS_RECIPINFO_TRANS;
1349 * Otherwise this might ben an engine implementation, so see if we can get
1350 * the type from the ameth.
1352 if (pk->ameth && pk->ameth->pkey_ctrl) {
1354 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1358 return CMS_RECIPINFO_TRANS;
1361 int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
1363 int supportedRiType;
1365 if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
1368 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED,
1374 supportedRiType = ossl_cms_pkey_get_ri_type(pk);
1375 if (supportedRiType < 0)
1378 return (supportedRiType == ri_type);