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 #ifndef OPENSSL_NO_DH
119 if (EVP_PKEY_is_a(pkey, "DHX"))
120 return cms_dh_envelope(ri, cmd);
123 #ifndef OPENSSL_NO_EC
124 if (EVP_PKEY_is_a(pkey, "EC"))
125 return cms_ecdh_envelope(ri, cmd);
128 if (EVP_PKEY_is_a(pkey, "RSA"))
129 return cms_rsa_envelope(ri, cmd);
131 /* Something else? We'll give engines etc a chance to handle this */
132 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
134 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
136 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
140 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
146 CMS_EncryptedContentInfo* cms_get0_env_enc_content(const CMS_ContentInfo *cms)
148 switch (cms_get_enveloped_type(cms)) {
149 case CMS_ENVELOPED_STANDARD:
150 return cms->d.envelopedData->encryptedContentInfo;
152 case CMS_ENVELOPED_AUTH:
153 return cms->d.authEnvelopedData->authEncryptedContentInfo;
160 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
162 switch (cms_get_enveloped_type(cms)) {
163 case CMS_ENVELOPED_STANDARD:
164 return cms->d.envelopedData->recipientInfos;
166 case CMS_ENVELOPED_AUTH:
167 return cms->d.authEnvelopedData->recipientInfos;
174 void cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms)
177 CMS_RecipientInfo *ri;
178 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
179 STACK_OF(CMS_RecipientInfo) *rinfos = CMS_get0_RecipientInfos(cms);
181 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
182 ri = sk_CMS_RecipientInfo_value(rinfos, i);
185 case CMS_RECIPINFO_AGREE:
186 ri->d.kari->cms_ctx = ctx;
188 case CMS_RECIPINFO_TRANS:
189 ri->d.ktri->cms_ctx = ctx;
190 x509_set0_libctx(ri->d.ktri->recip, ctx->libctx, ctx->propq);
192 case CMS_RECIPINFO_KEK:
193 ri->d.kekri->cms_ctx = ctx;
195 case CMS_RECIPINFO_PASS:
196 ri->d.pwri->cms_ctx = ctx;
205 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
210 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
212 if (ri->type == CMS_RECIPINFO_TRANS)
213 return ri->d.ktri->pctx;
214 else if (ri->type == CMS_RECIPINFO_AGREE)
215 return ri->d.kari->pctx;
219 CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher,
220 OSSL_LIB_CTX *libctx,
223 CMS_ContentInfo *cms;
224 CMS_EnvelopedData *env;
226 cms = CMS_ContentInfo_new_ex(libctx, propq);
229 env = cms_enveloped_data_init(cms);
233 if (!cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL, 0,
234 cms_get0_cmsctx(cms)))
238 CMS_ContentInfo_free(cms);
239 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
243 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
245 return CMS_EnvelopedData_create_ex(cipher, NULL, NULL);
249 CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx,
252 CMS_ContentInfo *cms;
253 CMS_AuthEnvelopedData *aenv;
255 cms = CMS_ContentInfo_new_ex(libctx, propq);
258 aenv = cms_auth_enveloped_data_init(cms);
261 if (!cms_EncryptedContent_init(aenv->authEncryptedContentInfo,
262 cipher, NULL, 0, cms_get0_cmsctx(cms)))
266 CMS_ContentInfo_free(cms);
267 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
272 CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher)
274 return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL);
277 /* Key Transport Recipient Info (KTRI) routines */
279 /* Initialise a ktri based on passed certificate and key */
281 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
282 EVP_PKEY *pk, unsigned int flags,
285 CMS_KeyTransRecipientInfo *ktri;
288 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
291 ri->type = CMS_RECIPINFO_TRANS;
296 if (flags & CMS_USE_KEYID) {
298 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
301 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
305 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
309 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx))
318 if (flags & CMS_KEY_PARAM) {
319 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ktri->pkey,
321 if (ktri->pctx == NULL)
323 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
325 } else if (!cms_env_asn1_ctrl(ri, 0))
331 * Add a recipient certificate using appropriate type of RecipientInfo
334 CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
335 EVP_PKEY *originatorPrivKey,
336 X509 *originator, unsigned int flags)
338 CMS_RecipientInfo *ri = NULL;
339 STACK_OF(CMS_RecipientInfo) *ris;
341 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
343 ris = CMS_get0_RecipientInfos(cms);
347 /* Initialize recipient info */
348 ri = M_ASN1_new_of(CMS_RecipientInfo);
352 pk = X509_get0_pubkey(recip);
354 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY);
358 switch (cms_pkey_get_ri_type(pk)) {
360 case CMS_RECIPINFO_TRANS:
361 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx))
365 case CMS_RECIPINFO_AGREE:
366 if (!cms_RecipientInfo_kari_init(ri, recip, pk, originator,
367 originatorPrivKey, flags, ctx))
372 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
377 if (!sk_CMS_RecipientInfo_push(ris, ri))
383 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
385 M_ASN1_free_of(ri, CMS_RecipientInfo);
390 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
393 return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
396 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
397 EVP_PKEY **pk, X509 **recip,
400 CMS_KeyTransRecipientInfo *ktri;
401 if (ri->type != CMS_RECIPINFO_TRANS) {
402 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
411 *recip = ktri->recip;
413 *palg = ktri->keyEncryptionAlgorithm;
417 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
418 ASN1_OCTET_STRING **keyid,
422 CMS_KeyTransRecipientInfo *ktri;
423 if (ri->type != CMS_RECIPINFO_TRANS) {
424 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
429 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
432 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
434 if (ri->type != CMS_RECIPINFO_TRANS) {
435 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
438 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
441 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
443 if (ri->type != CMS_RECIPINFO_TRANS) {
444 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
447 EVP_PKEY_free(ri->d.ktri->pkey);
448 ri->d.ktri->pkey = pkey;
452 /* Encrypt content key in key transport recipient info */
454 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
455 CMS_RecipientInfo *ri)
457 CMS_KeyTransRecipientInfo *ktri;
458 CMS_EncryptedContentInfo *ec;
460 unsigned char *ek = NULL;
462 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
466 if (ri->type != CMS_RECIPINFO_TRANS) {
467 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
471 ec = cms_get0_env_enc_content(cms);
476 if (!cms_env_asn1_ctrl(ri, 0))
479 pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ktri->pkey, ctx->propq);
483 if (EVP_PKEY_encrypt_init(pctx) <= 0)
487 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
488 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
489 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_ERROR);
493 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
496 ek = OPENSSL_malloc(eklen);
499 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
503 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
506 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
512 EVP_PKEY_CTX_free(pctx);
518 /* Decrypt content key from KTRI */
520 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
521 CMS_RecipientInfo *ri)
523 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
524 EVP_PKEY *pkey = ktri->pkey;
525 unsigned char *ek = NULL;
529 const EVP_CIPHER *cipher = NULL;
530 EVP_CIPHER *fetched_cipher = NULL;
531 CMS_EncryptedContentInfo *ec;
532 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
534 ec = cms_get0_env_enc_content(cms);
536 if (ktri->pkey == NULL) {
537 ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
541 if (cms->d.envelopedData->encryptedContentInfo->havenocert
542 && !cms->d.envelopedData->encryptedContentInfo->debug) {
543 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
544 const char *name = OBJ_nid2sn(OBJ_obj2nid(calg->algorithm));
546 (void)ERR_set_mark();
547 fetched_cipher = EVP_CIPHER_fetch(ctx->libctx, name, ctx->propq);
549 if (fetched_cipher != NULL)
550 cipher = fetched_cipher;
552 cipher = EVP_get_cipherbyobj(calg->algorithm);
553 if (cipher == NULL) {
554 (void)ERR_clear_last_mark();
555 ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
558 (void)ERR_pop_to_mark();
560 fixlen = EVP_CIPHER_key_length(cipher);
561 EVP_CIPHER_free(fetched_cipher);
564 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkey, ctx->propq);
565 if (ktri->pctx == NULL)
568 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
571 if (!cms_env_asn1_ctrl(ri, 1))
574 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
575 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
576 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_ERROR);
580 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
581 ktri->encryptedKey->data,
582 ktri->encryptedKey->length) <= 0)
585 ek = OPENSSL_malloc(eklen);
587 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
591 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
592 ktri->encryptedKey->data,
593 ktri->encryptedKey->length) <= 0
595 || (fixlen != 0 && eklen != fixlen)) {
596 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
602 OPENSSL_clear_free(ec->key, ec->keylen);
607 EVP_PKEY_CTX_free(ktri->pctx);
615 /* Key Encrypted Key (KEK) RecipientInfo routines */
617 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
618 const unsigned char *id, size_t idlen)
620 ASN1_OCTET_STRING tmp_os;
621 CMS_KEKRecipientInfo *kekri;
622 if (ri->type != CMS_RECIPINFO_KEK) {
623 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
627 tmp_os.type = V_ASN1_OCTET_STRING;
629 tmp_os.data = (unsigned char *)id;
630 tmp_os.length = (int)idlen;
631 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
634 /* For now hard code AES key wrap info */
636 static size_t aes_wrap_keylen(int nid)
639 case NID_id_aes128_wrap:
642 case NID_id_aes192_wrap:
645 case NID_id_aes256_wrap:
653 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
654 unsigned char *key, size_t keylen,
655 unsigned char *id, size_t idlen,
656 ASN1_GENERALIZEDTIME *date,
657 ASN1_OBJECT *otherTypeId,
658 ASN1_TYPE *otherType)
660 CMS_RecipientInfo *ri = NULL;
661 CMS_KEKRecipientInfo *kekri;
662 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
667 if (nid == NID_undef) {
670 nid = NID_id_aes128_wrap;
674 nid = NID_id_aes192_wrap;
678 nid = NID_id_aes256_wrap;
682 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
688 size_t exp_keylen = aes_wrap_keylen(nid);
691 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM);
695 if (keylen != exp_keylen) {
696 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
702 /* Initialize recipient info */
703 ri = M_ASN1_new_of(CMS_RecipientInfo);
707 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
710 ri->type = CMS_RECIPINFO_KEK;
715 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
716 if (kekri->kekid->other == NULL)
720 if (!sk_CMS_RecipientInfo_push(ris, ri))
723 /* After this point no calls can fail */
728 kekri->keylen = keylen;
730 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
732 kekri->kekid->date = date;
734 if (kekri->kekid->other) {
735 kekri->kekid->other->keyAttrId = otherTypeId;
736 kekri->kekid->other->keyAttr = otherType;
739 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
740 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
745 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
747 M_ASN1_free_of(ri, CMS_RecipientInfo);
751 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
753 ASN1_OCTET_STRING **pid,
754 ASN1_GENERALIZEDTIME **pdate,
755 ASN1_OBJECT **potherid,
756 ASN1_TYPE **pothertype)
758 CMS_KEKIdentifier *rkid;
759 if (ri->type != CMS_RECIPINFO_KEK) {
760 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
763 rkid = ri->d.kekri->kekid;
765 *palg = ri->d.kekri->keyEncryptionAlgorithm;
767 *pid = rkid->keyIdentifier;
772 *potherid = rkid->other->keyAttrId;
778 *pothertype = rkid->other->keyAttr;
785 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
786 unsigned char *key, size_t keylen)
788 CMS_KEKRecipientInfo *kekri;
789 if (ri->type != CMS_RECIPINFO_KEK) {
790 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
796 kekri->keylen = keylen;
800 static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
802 const char *alg = NULL;
806 alg = "AES-128-WRAP";
809 alg = "AES-192-WRAP";
812 alg = "AES-256-WRAP";
817 return EVP_CIPHER_fetch(ctx->libctx, alg, ctx->propq);
821 /* Encrypt content key in KEK recipient info */
823 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
824 CMS_RecipientInfo *ri)
826 CMS_EncryptedContentInfo *ec;
827 CMS_KEKRecipientInfo *kekri;
828 unsigned char *wkey = NULL;
831 EVP_CIPHER *cipher = NULL;
833 EVP_CIPHER_CTX *ctx = NULL;
834 const CMS_CTX *cms_ctx = cms_get0_cmsctx(cms);
836 ec = cms_get0_env_enc_content(cms);
842 if (kekri->key == NULL) {
843 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
847 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
848 if (cipher == NULL) {
849 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
853 /* 8 byte prefix for AES wrap ciphers */
854 wkey = OPENSSL_malloc(ec->keylen + 8);
856 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
860 ctx = EVP_CIPHER_CTX_new();
862 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
866 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
867 if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
868 || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
869 || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
870 ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
874 if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
875 ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
879 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
884 EVP_CIPHER_free(cipher);
887 EVP_CIPHER_CTX_free(ctx);
892 /* Decrypt content key in KEK recipient info */
894 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
895 CMS_RecipientInfo *ri)
897 CMS_EncryptedContentInfo *ec;
898 CMS_KEKRecipientInfo *kekri;
899 unsigned char *ukey = NULL;
902 EVP_CIPHER *cipher = NULL;
904 EVP_CIPHER_CTX *ctx = NULL;
905 const CMS_CTX *cms_ctx = cms_get0_cmsctx(cms);
907 ec = cms_get0_env_enc_content(cms);
914 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
918 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
919 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
920 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
924 /* If encrypted key length is invalid don't bother */
926 if (kekri->encryptedKey->length < 16) {
927 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
931 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
932 if (cipher == NULL) {
933 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
937 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
939 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
943 ctx = EVP_CIPHER_CTX_new();
945 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
949 if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
950 || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
951 kekri->encryptedKey->data,
952 kekri->encryptedKey->length)
953 || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
954 ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR);
960 ec->keylen = ukeylen;
965 EVP_CIPHER_free(cipher);
968 EVP_CIPHER_CTX_free(ctx);
973 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
976 case CMS_RECIPINFO_TRANS:
977 return cms_RecipientInfo_ktri_decrypt(cms, ri);
979 case CMS_RECIPINFO_KEK:
980 return cms_RecipientInfo_kekri_decrypt(cms, ri);
982 case CMS_RECIPINFO_PASS:
983 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
986 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
991 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
994 case CMS_RECIPINFO_TRANS:
995 return cms_RecipientInfo_ktri_encrypt(cms, ri);
997 case CMS_RECIPINFO_AGREE:
998 return cms_RecipientInfo_kari_encrypt(cms, ri);
1000 case CMS_RECIPINFO_KEK:
1001 return cms_RecipientInfo_kekri_encrypt(cms, ri);
1003 case CMS_RECIPINFO_PASS:
1004 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
1007 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
1012 /* Check structures and fixup version numbers (if necessary) */
1014 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
1016 CMS_OriginatorInfo *org = env->originatorInfo;
1020 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
1021 CMS_CertificateChoices *cch;
1022 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
1023 if (cch->type == CMS_CERTCHOICE_OTHER) {
1026 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
1027 if (env->version < 3)
1032 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
1033 CMS_RevocationInfoChoice *rch;
1034 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
1035 if (rch->type == CMS_REVCHOICE_OTHER) {
1042 static void cms_env_set_version(CMS_EnvelopedData *env)
1045 CMS_RecipientInfo *ri;
1048 * Can't set version higher than 4 so if 4 or more already nothing to do.
1050 if (env->version >= 4)
1053 cms_env_set_originfo_version(env);
1055 if (env->version >= 3)
1058 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
1059 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
1060 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
1063 } else if (ri->type != CMS_RECIPINFO_TRANS
1064 || ri->d.ktri->version != 0) {
1068 if (env->originatorInfo || env->unprotectedAttrs)
1070 if (env->version == 2)
1075 static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms,
1076 STACK_OF(CMS_RecipientInfo) *ris)
1079 CMS_RecipientInfo *ri;
1081 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
1082 ri = sk_CMS_RecipientInfo_value(ris, i);
1083 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
1089 static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec)
1092 OPENSSL_clear_free(ec->key, ec->keylen);
1097 static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
1099 CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
1100 BIO *contentBio = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1101 EVP_CIPHER_CTX *ctx = NULL;
1103 if (contentBio == NULL)
1106 BIO_get_cipher_ctx(contentBio, &ctx);
1108 BIO_free(contentBio);
1112 * If the selected cipher supports unprotected attributes,
1113 * deal with it using special ctrl function
1115 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
1116 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
1117 cms->d.envelopedData->unprotectedAttrs) <= 0) {
1118 BIO_free(contentBio);
1124 static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
1126 CMS_EncryptedContentInfo *ec;
1127 STACK_OF(CMS_RecipientInfo) *rinfos;
1130 CMS_EnvelopedData *env = cms->d.envelopedData;
1132 /* Get BIO first to set up key */
1134 ec = env->encryptedContentInfo;
1135 ret = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1137 /* If error end of processing */
1141 /* Now encrypt content key according to each RecipientInfo type */
1142 rinfos = env->recipientInfos;
1143 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1144 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1148 /* And finally set the version */
1149 cms_env_set_version(env);
1154 cms_env_clear_ec(ec);
1161 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
1163 if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
1164 /* If cipher is set it's encryption */
1165 return cms_EnvelopedData_Encryption_init_bio(cms);
1168 /* If cipher is not set it's decryption */
1169 return cms_EnvelopedData_Decryption_init_bio(cms);
1172 BIO *cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms)
1174 CMS_EncryptedContentInfo *ec;
1175 STACK_OF(CMS_RecipientInfo) *rinfos;
1178 CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData;
1180 /* Get BIO first to set up key */
1181 ec = aenv->authEncryptedContentInfo;
1182 /* Set tag for decryption */
1183 if (ec->cipher == NULL) {
1184 ec->tag = aenv->mac->data;
1185 ec->taglen = aenv->mac->length;
1187 ret = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1189 /* If error or no cipher end of processing */
1190 if (ret == NULL || ec->cipher == NULL)
1193 /* Now encrypt content key according to each RecipientInfo type */
1194 rinfos = aenv->recipientInfos;
1195 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1196 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1200 /* And finally set the version */
1206 cms_env_clear_ec(ec);
1213 int cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
1215 CMS_EnvelopedData *env = NULL;
1216 EVP_CIPHER_CTX *ctx = NULL;
1217 BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
1219 env = cms_get0_enveloped(cms);
1224 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
1228 BIO_get_cipher_ctx(mbio, &ctx);
1231 * If the selected cipher supports unprotected attributes,
1232 * deal with it using special ctrl function
1234 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
1235 if (env->unprotectedAttrs == NULL)
1236 env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
1238 if (env->unprotectedAttrs == NULL) {
1239 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
1243 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
1244 1, env->unprotectedAttrs) <= 0) {
1245 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
1250 cms_env_set_version(cms->d.envelopedData);
1254 int cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio)
1256 EVP_CIPHER_CTX *ctx;
1257 unsigned char *tag = NULL;
1260 BIO_get_cipher_ctx(cmsbio, &ctx);
1263 * The tag is set only for encryption. There is nothing to do for
1266 if (!EVP_CIPHER_CTX_encrypting(ctx))
1269 taglen = EVP_CIPHER_CTX_tag_length(ctx);
1271 || (tag = OPENSSL_malloc(taglen)) == NULL
1272 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
1274 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG);
1278 if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen))
1288 * Get RecipientInfo type (if any) supported by a key (public or private). To
1289 * retain compatibility with previous behaviour if the ctrl value isn't
1290 * supported we assume key transport.
1292 int cms_pkey_get_ri_type(EVP_PKEY *pk)
1294 /* Check types that we know about */
1295 if (EVP_PKEY_is_a(pk, "DH"))
1296 return CMS_RECIPINFO_AGREE;
1297 else if (EVP_PKEY_is_a(pk, "DSA"))
1298 return CMS_RECIPINFO_NONE;
1299 else if (EVP_PKEY_is_a(pk, "EC"))
1300 return CMS_RECIPINFO_AGREE;
1301 else if (EVP_PKEY_is_a(pk, "RSA"))
1302 return CMS_RECIPINFO_TRANS;
1305 * Otherwise this might ben an engine implementation, so see if we can get
1306 * the type from the ameth.
1308 if (pk->ameth && pk->ameth->pkey_ctrl) {
1310 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1314 return CMS_RECIPINFO_TRANS;
1317 int cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
1319 int supportedRiType;
1321 if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
1324 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED,
1330 supportedRiType = cms_pkey_get_ri_type(pk);
1331 if (supportedRiType < 0)
1334 return (supportedRiType == ri_type);