2 * Copyright 2008-2020 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 "crypto/asn1.h"
18 #include "crypto/evp.h"
19 #include "crypto/x509.h"
20 #include "cms_local.h"
22 /* CMS EnvelopedData Utilities */
23 static void cms_env_set_version(CMS_EnvelopedData *env);
25 #define CMS_ENVELOPED_STANDARD 1
26 #define CMS_ENVELOPED_AUTH 2
28 static int cms_get_enveloped_type(const CMS_ContentInfo *cms)
30 int nid = OBJ_obj2nid(cms->contentType);
33 case NID_pkcs7_enveloped:
34 return CMS_ENVELOPED_STANDARD;
36 case NID_id_smime_ct_authEnvelopedData:
37 return CMS_ENVELOPED_AUTH;
40 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
45 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
47 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
48 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
51 return cms->d.envelopedData;
54 CMS_AuthEnvelopedData *cms_get0_auth_enveloped(CMS_ContentInfo *cms)
56 if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_authEnvelopedData) {
57 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
60 return cms->d.authEnvelopedData;
63 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
65 if (cms->d.other == NULL) {
66 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
67 if (cms->d.envelopedData == NULL) {
68 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
71 cms->d.envelopedData->version = 0;
72 cms->d.envelopedData->encryptedContentInfo->contentType =
73 OBJ_nid2obj(NID_pkcs7_data);
74 ASN1_OBJECT_free(cms->contentType);
75 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
76 return cms->d.envelopedData;
78 return cms_get0_enveloped(cms);
81 static CMS_AuthEnvelopedData *
82 cms_auth_enveloped_data_init(CMS_ContentInfo *cms)
84 if (cms->d.other == NULL) {
85 cms->d.authEnvelopedData = M_ASN1_new_of(CMS_AuthEnvelopedData);
86 if (cms->d.authEnvelopedData == NULL) {
87 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
90 /* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */
91 cms->d.authEnvelopedData->version = 0;
92 cms->d.authEnvelopedData->authEncryptedContentInfo->contentType =
93 OBJ_nid2obj(NID_pkcs7_data);
94 ASN1_OBJECT_free(cms->contentType);
95 cms->contentType = OBJ_nid2obj(NID_id_smime_ct_authEnvelopedData);
96 return cms->d.authEnvelopedData;
98 return cms_get0_auth_enveloped(cms);
101 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
105 if (ri->type == CMS_RECIPINFO_TRANS)
106 pkey = ri->d.ktri->pkey;
107 else if (ri->type == CMS_RECIPINFO_AGREE) {
108 EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
112 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
118 if (EVP_PKEY_is_a(pkey, "DHX"))
119 return cms_dh_envelope(ri, cmd);
120 else if (EVP_PKEY_is_a(pkey, "EC"))
121 return cms_ecdh_envelope(ri, cmd);
122 else if (EVP_PKEY_is_a(pkey, "RSA"))
123 return cms_rsa_envelope(ri, cmd);
125 /* Something else? We'll give engines etc a chance to handle this */
126 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
128 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
130 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
134 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
140 CMS_EncryptedContentInfo* cms_get0_env_enc_content(const CMS_ContentInfo *cms)
142 switch (cms_get_enveloped_type(cms)) {
143 case CMS_ENVELOPED_STANDARD:
144 return cms->d.envelopedData->encryptedContentInfo;
146 case CMS_ENVELOPED_AUTH:
147 return cms->d.authEnvelopedData->authEncryptedContentInfo;
154 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
156 switch (cms_get_enveloped_type(cms)) {
157 case CMS_ENVELOPED_STANDARD:
158 return cms->d.envelopedData->recipientInfos;
160 case CMS_ENVELOPED_AUTH:
161 return cms->d.authEnvelopedData->recipientInfos;
168 void cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms)
171 CMS_RecipientInfo *ri;
172 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
173 STACK_OF(CMS_RecipientInfo) *rinfos = CMS_get0_RecipientInfos(cms);
175 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
176 ri = sk_CMS_RecipientInfo_value(rinfos, i);
179 case CMS_RECIPINFO_AGREE:
180 ri->d.kari->cms_ctx = ctx;
182 case CMS_RECIPINFO_TRANS:
183 ri->d.ktri->cms_ctx = ctx;
184 x509_set0_libctx(ri->d.ktri->recip, ctx->libctx, ctx->propq);
186 case CMS_RECIPINFO_KEK:
187 ri->d.kekri->cms_ctx = ctx;
189 case CMS_RECIPINFO_PASS:
190 ri->d.pwri->cms_ctx = ctx;
199 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
204 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
206 if (ri->type == CMS_RECIPINFO_TRANS)
207 return ri->d.ktri->pctx;
208 else if (ri->type == CMS_RECIPINFO_AGREE)
209 return ri->d.kari->pctx;
213 CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher,
214 OSSL_LIB_CTX *libctx,
217 CMS_ContentInfo *cms;
218 CMS_EnvelopedData *env;
220 cms = CMS_ContentInfo_new_ex(libctx, propq);
223 env = cms_enveloped_data_init(cms);
227 if (!cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL, 0,
228 cms_get0_cmsctx(cms)))
232 CMS_ContentInfo_free(cms);
233 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
237 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
239 return CMS_EnvelopedData_create_ex(cipher, NULL, NULL);
243 CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx,
246 CMS_ContentInfo *cms;
247 CMS_AuthEnvelopedData *aenv;
249 cms = CMS_ContentInfo_new_ex(libctx, propq);
252 aenv = cms_auth_enveloped_data_init(cms);
255 if (!cms_EncryptedContent_init(aenv->authEncryptedContentInfo,
256 cipher, NULL, 0, cms_get0_cmsctx(cms)))
260 CMS_ContentInfo_free(cms);
261 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
266 CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher)
268 return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL);
271 /* Key Transport Recipient Info (KTRI) routines */
273 /* Initialise a ktri based on passed certificate and key */
275 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
276 EVP_PKEY *pk, unsigned int flags,
279 CMS_KeyTransRecipientInfo *ktri;
282 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
285 ri->type = CMS_RECIPINFO_TRANS;
290 if (flags & CMS_USE_KEYID) {
292 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
295 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
299 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
303 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx))
312 if (flags & CMS_KEY_PARAM) {
313 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ktri->pkey,
315 if (ktri->pctx == NULL)
317 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
319 } else if (!cms_env_asn1_ctrl(ri, 0))
325 * Add a recipient certificate using appropriate type of RecipientInfo
328 CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
329 EVP_PKEY *originatorPrivKey,
330 X509 *originator, unsigned int flags)
332 CMS_RecipientInfo *ri = NULL;
333 STACK_OF(CMS_RecipientInfo) *ris;
335 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
337 ris = CMS_get0_RecipientInfos(cms);
341 /* Initialize recipient info */
342 ri = M_ASN1_new_of(CMS_RecipientInfo);
346 pk = X509_get0_pubkey(recip);
348 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY);
352 switch (cms_pkey_get_ri_type(pk)) {
354 case CMS_RECIPINFO_TRANS:
355 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx))
359 case CMS_RECIPINFO_AGREE:
360 if (!cms_RecipientInfo_kari_init(ri, recip, pk, originator,
361 originatorPrivKey, flags, ctx))
366 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
371 if (!sk_CMS_RecipientInfo_push(ris, ri))
377 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
379 M_ASN1_free_of(ri, CMS_RecipientInfo);
384 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
387 return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
390 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
391 EVP_PKEY **pk, X509 **recip,
394 CMS_KeyTransRecipientInfo *ktri;
395 if (ri->type != CMS_RECIPINFO_TRANS) {
396 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
405 *recip = ktri->recip;
407 *palg = ktri->keyEncryptionAlgorithm;
411 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
412 ASN1_OCTET_STRING **keyid,
416 CMS_KeyTransRecipientInfo *ktri;
417 if (ri->type != CMS_RECIPINFO_TRANS) {
418 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
423 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
426 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
428 if (ri->type != CMS_RECIPINFO_TRANS) {
429 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
432 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
435 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
437 if (ri->type != CMS_RECIPINFO_TRANS) {
438 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
441 EVP_PKEY_free(ri->d.ktri->pkey);
442 ri->d.ktri->pkey = pkey;
446 /* Encrypt content key in key transport recipient info */
448 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
449 CMS_RecipientInfo *ri)
451 CMS_KeyTransRecipientInfo *ktri;
452 CMS_EncryptedContentInfo *ec;
454 unsigned char *ek = NULL;
456 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
460 if (ri->type != CMS_RECIPINFO_TRANS) {
461 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
465 ec = cms_get0_env_enc_content(cms);
470 if (!cms_env_asn1_ctrl(ri, 0))
473 pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ktri->pkey, ctx->propq);
477 if (EVP_PKEY_encrypt_init(pctx) <= 0)
481 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
482 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
483 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_ERROR);
487 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
490 ek = OPENSSL_malloc(eklen);
493 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
497 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
500 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
506 EVP_PKEY_CTX_free(pctx);
512 /* Decrypt content key from KTRI */
514 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
515 CMS_RecipientInfo *ri)
517 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
518 EVP_PKEY *pkey = ktri->pkey;
519 unsigned char *ek = NULL;
523 const EVP_CIPHER *cipher = NULL;
524 EVP_CIPHER *fetched_cipher = NULL;
525 CMS_EncryptedContentInfo *ec;
526 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
528 ec = cms_get0_env_enc_content(cms);
530 if (ktri->pkey == NULL) {
531 ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
535 if (cms->d.envelopedData->encryptedContentInfo->havenocert
536 && !cms->d.envelopedData->encryptedContentInfo->debug) {
537 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
538 const char *name = OBJ_nid2sn(OBJ_obj2nid(calg->algorithm));
540 (void)ERR_set_mark();
541 fetched_cipher = EVP_CIPHER_fetch(ctx->libctx, name, ctx->propq);
543 if (fetched_cipher != NULL)
544 cipher = fetched_cipher;
546 cipher = EVP_get_cipherbyobj(calg->algorithm);
547 if (cipher == NULL) {
548 (void)ERR_clear_last_mark();
549 ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
552 (void)ERR_pop_to_mark();
554 fixlen = EVP_CIPHER_key_length(cipher);
555 EVP_CIPHER_free(fetched_cipher);
558 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkey, ctx->propq);
559 if (ktri->pctx == NULL)
562 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
565 if (!cms_env_asn1_ctrl(ri, 1))
568 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
569 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
570 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_ERROR);
574 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
575 ktri->encryptedKey->data,
576 ktri->encryptedKey->length) <= 0)
579 ek = OPENSSL_malloc(eklen);
581 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
585 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
586 ktri->encryptedKey->data,
587 ktri->encryptedKey->length) <= 0
589 || (fixlen != 0 && eklen != fixlen)) {
590 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
596 OPENSSL_clear_free(ec->key, ec->keylen);
601 EVP_PKEY_CTX_free(ktri->pctx);
609 /* Key Encrypted Key (KEK) RecipientInfo routines */
611 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
612 const unsigned char *id, size_t idlen)
614 ASN1_OCTET_STRING tmp_os;
615 CMS_KEKRecipientInfo *kekri;
616 if (ri->type != CMS_RECIPINFO_KEK) {
617 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
621 tmp_os.type = V_ASN1_OCTET_STRING;
623 tmp_os.data = (unsigned char *)id;
624 tmp_os.length = (int)idlen;
625 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
628 /* For now hard code AES key wrap info */
630 static size_t aes_wrap_keylen(int nid)
633 case NID_id_aes128_wrap:
636 case NID_id_aes192_wrap:
639 case NID_id_aes256_wrap:
647 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
648 unsigned char *key, size_t keylen,
649 unsigned char *id, size_t idlen,
650 ASN1_GENERALIZEDTIME *date,
651 ASN1_OBJECT *otherTypeId,
652 ASN1_TYPE *otherType)
654 CMS_RecipientInfo *ri = NULL;
655 CMS_KEKRecipientInfo *kekri;
656 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
661 if (nid == NID_undef) {
664 nid = NID_id_aes128_wrap;
668 nid = NID_id_aes192_wrap;
672 nid = NID_id_aes256_wrap;
676 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
682 size_t exp_keylen = aes_wrap_keylen(nid);
685 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM);
689 if (keylen != exp_keylen) {
690 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
696 /* Initialize recipient info */
697 ri = M_ASN1_new_of(CMS_RecipientInfo);
701 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
704 ri->type = CMS_RECIPINFO_KEK;
709 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
710 if (kekri->kekid->other == NULL)
714 if (!sk_CMS_RecipientInfo_push(ris, ri))
717 /* After this point no calls can fail */
722 kekri->keylen = keylen;
724 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
726 kekri->kekid->date = date;
728 if (kekri->kekid->other) {
729 kekri->kekid->other->keyAttrId = otherTypeId;
730 kekri->kekid->other->keyAttr = otherType;
733 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
734 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
739 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
741 M_ASN1_free_of(ri, CMS_RecipientInfo);
745 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
747 ASN1_OCTET_STRING **pid,
748 ASN1_GENERALIZEDTIME **pdate,
749 ASN1_OBJECT **potherid,
750 ASN1_TYPE **pothertype)
752 CMS_KEKIdentifier *rkid;
753 if (ri->type != CMS_RECIPINFO_KEK) {
754 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
757 rkid = ri->d.kekri->kekid;
759 *palg = ri->d.kekri->keyEncryptionAlgorithm;
761 *pid = rkid->keyIdentifier;
766 *potherid = rkid->other->keyAttrId;
772 *pothertype = rkid->other->keyAttr;
779 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
780 unsigned char *key, size_t keylen)
782 CMS_KEKRecipientInfo *kekri;
783 if (ri->type != CMS_RECIPINFO_KEK) {
784 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
790 kekri->keylen = keylen;
794 static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
796 const char *alg = NULL;
800 alg = "AES-128-WRAP";
803 alg = "AES-192-WRAP";
806 alg = "AES-256-WRAP";
811 return EVP_CIPHER_fetch(ctx->libctx, alg, ctx->propq);
815 /* Encrypt content key in KEK recipient info */
817 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
818 CMS_RecipientInfo *ri)
820 CMS_EncryptedContentInfo *ec;
821 CMS_KEKRecipientInfo *kekri;
822 unsigned char *wkey = NULL;
825 EVP_CIPHER *cipher = NULL;
827 EVP_CIPHER_CTX *ctx = NULL;
828 const CMS_CTX *cms_ctx = cms_get0_cmsctx(cms);
830 ec = cms_get0_env_enc_content(cms);
836 if (kekri->key == NULL) {
837 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
841 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
842 if (cipher == NULL) {
843 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
847 /* 8 byte prefix for AES wrap ciphers */
848 wkey = OPENSSL_malloc(ec->keylen + 8);
850 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
854 ctx = EVP_CIPHER_CTX_new();
856 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
860 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
861 if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
862 || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
863 || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
864 ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
868 if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
869 ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
873 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
878 EVP_CIPHER_free(cipher);
881 EVP_CIPHER_CTX_free(ctx);
886 /* Decrypt content key in KEK recipient info */
888 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
889 CMS_RecipientInfo *ri)
891 CMS_EncryptedContentInfo *ec;
892 CMS_KEKRecipientInfo *kekri;
893 unsigned char *ukey = NULL;
896 EVP_CIPHER *cipher = NULL;
898 EVP_CIPHER_CTX *ctx = NULL;
899 const CMS_CTX *cms_ctx = cms_get0_cmsctx(cms);
901 ec = cms_get0_env_enc_content(cms);
908 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
912 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
913 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
914 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
918 /* If encrypted key length is invalid don't bother */
920 if (kekri->encryptedKey->length < 16) {
921 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
925 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
926 if (cipher == NULL) {
927 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
931 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
933 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
937 ctx = EVP_CIPHER_CTX_new();
939 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
943 if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
944 || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
945 kekri->encryptedKey->data,
946 kekri->encryptedKey->length)
947 || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
948 ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR);
954 ec->keylen = ukeylen;
959 EVP_CIPHER_free(cipher);
962 EVP_CIPHER_CTX_free(ctx);
967 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
970 case CMS_RECIPINFO_TRANS:
971 return cms_RecipientInfo_ktri_decrypt(cms, ri);
973 case CMS_RECIPINFO_KEK:
974 return cms_RecipientInfo_kekri_decrypt(cms, ri);
976 case CMS_RECIPINFO_PASS:
977 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
980 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
985 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
988 case CMS_RECIPINFO_TRANS:
989 return cms_RecipientInfo_ktri_encrypt(cms, ri);
991 case CMS_RECIPINFO_AGREE:
992 return cms_RecipientInfo_kari_encrypt(cms, ri);
994 case CMS_RECIPINFO_KEK:
995 return cms_RecipientInfo_kekri_encrypt(cms, ri);
997 case CMS_RECIPINFO_PASS:
998 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
1001 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
1006 /* Check structures and fixup version numbers (if necessary) */
1008 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
1010 CMS_OriginatorInfo *org = env->originatorInfo;
1014 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
1015 CMS_CertificateChoices *cch;
1016 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
1017 if (cch->type == CMS_CERTCHOICE_OTHER) {
1020 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
1021 if (env->version < 3)
1026 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
1027 CMS_RevocationInfoChoice *rch;
1028 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
1029 if (rch->type == CMS_REVCHOICE_OTHER) {
1036 static void cms_env_set_version(CMS_EnvelopedData *env)
1039 CMS_RecipientInfo *ri;
1042 * Can't set version higher than 4 so if 4 or more already nothing to do.
1044 if (env->version >= 4)
1047 cms_env_set_originfo_version(env);
1049 if (env->version >= 3)
1052 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
1053 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
1054 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
1057 } else if (ri->type != CMS_RECIPINFO_TRANS
1058 || ri->d.ktri->version != 0) {
1062 if (env->originatorInfo || env->unprotectedAttrs)
1064 if (env->version == 2)
1069 static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms,
1070 STACK_OF(CMS_RecipientInfo) *ris)
1073 CMS_RecipientInfo *ri;
1075 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
1076 ri = sk_CMS_RecipientInfo_value(ris, i);
1077 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
1083 static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec)
1086 OPENSSL_clear_free(ec->key, ec->keylen);
1091 static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
1093 CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
1094 BIO *contentBio = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1095 EVP_CIPHER_CTX *ctx = NULL;
1097 if (contentBio == NULL)
1100 BIO_get_cipher_ctx(contentBio, &ctx);
1102 BIO_free(contentBio);
1106 * If the selected cipher supports unprotected attributes,
1107 * deal with it using special ctrl function
1109 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
1110 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
1111 cms->d.envelopedData->unprotectedAttrs) <= 0) {
1112 BIO_free(contentBio);
1118 static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
1120 CMS_EncryptedContentInfo *ec;
1121 STACK_OF(CMS_RecipientInfo) *rinfos;
1124 CMS_EnvelopedData *env = cms->d.envelopedData;
1126 /* Get BIO first to set up key */
1128 ec = env->encryptedContentInfo;
1129 ret = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1131 /* If error end of processing */
1135 /* Now encrypt content key according to each RecipientInfo type */
1136 rinfos = env->recipientInfos;
1137 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1138 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1142 /* And finally set the version */
1143 cms_env_set_version(env);
1148 cms_env_clear_ec(ec);
1155 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
1157 if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
1158 /* If cipher is set it's encryption */
1159 return cms_EnvelopedData_Encryption_init_bio(cms);
1162 /* If cipher is not set it's decryption */
1163 return cms_EnvelopedData_Decryption_init_bio(cms);
1166 BIO *cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms)
1168 CMS_EncryptedContentInfo *ec;
1169 STACK_OF(CMS_RecipientInfo) *rinfos;
1172 CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData;
1174 /* Get BIO first to set up key */
1175 ec = aenv->authEncryptedContentInfo;
1176 /* Set tag for decryption */
1177 if (ec->cipher == NULL) {
1178 ec->tag = aenv->mac->data;
1179 ec->taglen = aenv->mac->length;
1181 ret = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1183 /* If error or no cipher end of processing */
1184 if (ret == NULL || ec->cipher == NULL)
1187 /* Now encrypt content key according to each RecipientInfo type */
1188 rinfos = aenv->recipientInfos;
1189 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1190 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1194 /* And finally set the version */
1200 cms_env_clear_ec(ec);
1207 int cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
1209 CMS_EnvelopedData *env = NULL;
1210 EVP_CIPHER_CTX *ctx = NULL;
1211 BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
1213 env = cms_get0_enveloped(cms);
1218 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
1222 BIO_get_cipher_ctx(mbio, &ctx);
1225 * If the selected cipher supports unprotected attributes,
1226 * deal with it using special ctrl function
1228 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
1229 if (env->unprotectedAttrs == NULL)
1230 env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
1232 if (env->unprotectedAttrs == NULL) {
1233 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
1237 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
1238 1, env->unprotectedAttrs) <= 0) {
1239 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
1244 cms_env_set_version(cms->d.envelopedData);
1248 int cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio)
1250 EVP_CIPHER_CTX *ctx;
1251 unsigned char *tag = NULL;
1254 BIO_get_cipher_ctx(cmsbio, &ctx);
1257 * The tag is set only for encryption. There is nothing to do for
1260 if (!EVP_CIPHER_CTX_encrypting(ctx))
1263 taglen = EVP_CIPHER_CTX_tag_length(ctx);
1265 || (tag = OPENSSL_malloc(taglen)) == NULL
1266 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
1268 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG);
1272 if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen))
1282 * Get RecipientInfo type (if any) supported by a key (public or private). To
1283 * retain compatibility with previous behaviour if the ctrl value isn't
1284 * supported we assume key transport.
1286 int cms_pkey_get_ri_type(EVP_PKEY *pk)
1288 /* Check types that we know about */
1289 if (EVP_PKEY_is_a(pk, "DH"))
1290 return CMS_RECIPINFO_AGREE;
1291 else if (EVP_PKEY_is_a(pk, "DSA"))
1292 return CMS_RECIPINFO_NONE;
1293 else if (EVP_PKEY_is_a(pk, "EC"))
1294 return CMS_RECIPINFO_AGREE;
1295 else if (EVP_PKEY_is_a(pk, "RSA"))
1296 return CMS_RECIPINFO_TRANS;
1299 * Otherwise this might ben an engine implementation, so see if we can get
1300 * the type from the ameth.
1302 if (pk->ameth && pk->ameth->pkey_ctrl) {
1304 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1308 return CMS_RECIPINFO_TRANS;
1311 int cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
1313 int supportedRiType;
1315 if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
1318 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED,
1324 supportedRiType = cms_pkey_get_ri_type(pk);
1325 if (supportedRiType < 0)
1328 return (supportedRiType == ri_type);