2 * Copyright 2008-2021 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);
246 CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx,
249 CMS_ContentInfo *cms;
250 CMS_AuthEnvelopedData *aenv;
252 cms = CMS_ContentInfo_new_ex(libctx, propq);
255 aenv = cms_auth_enveloped_data_init(cms);
258 if (!ossl_cms_EncryptedContent_init(aenv->authEncryptedContentInfo,
260 ossl_cms_get0_cmsctx(cms)))
264 CMS_ContentInfo_free(cms);
265 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
270 CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher)
272 return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL);
275 /* Key Transport Recipient Info (KTRI) routines */
277 /* Initialise a ktri based on passed certificate and key */
279 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
280 EVP_PKEY *pk, unsigned int flags,
283 CMS_KeyTransRecipientInfo *ktri;
286 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
289 ri->type = CMS_RECIPINFO_TRANS;
294 if (flags & CMS_USE_KEYID) {
296 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
299 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
303 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
307 if (!ossl_cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx))
316 if (flags & CMS_KEY_PARAM) {
317 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
319 ossl_cms_ctx_get0_propq(ctx));
320 if (ktri->pctx == NULL)
322 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
324 } else if (!ossl_cms_env_asn1_ctrl(ri, 0))
330 * Add a recipient certificate using appropriate type of RecipientInfo
333 CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
334 EVP_PKEY *originatorPrivKey,
335 X509 *originator, unsigned int flags)
337 CMS_RecipientInfo *ri = NULL;
338 STACK_OF(CMS_RecipientInfo) *ris;
340 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
342 ris = CMS_get0_RecipientInfos(cms);
346 /* Initialize recipient info */
347 ri = M_ASN1_new_of(CMS_RecipientInfo);
351 pk = X509_get0_pubkey(recip);
353 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY);
357 switch (ossl_cms_pkey_get_ri_type(pk)) {
359 case CMS_RECIPINFO_TRANS:
360 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx))
364 case CMS_RECIPINFO_AGREE:
365 if (!ossl_cms_RecipientInfo_kari_init(ri, recip, pk, originator,
366 originatorPrivKey, flags, ctx))
371 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
376 if (!sk_CMS_RecipientInfo_push(ris, ri))
382 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
384 M_ASN1_free_of(ri, CMS_RecipientInfo);
389 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
392 return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
395 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
396 EVP_PKEY **pk, X509 **recip,
399 CMS_KeyTransRecipientInfo *ktri;
400 if (ri->type != CMS_RECIPINFO_TRANS) {
401 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
410 *recip = ktri->recip;
412 *palg = ktri->keyEncryptionAlgorithm;
416 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
417 ASN1_OCTET_STRING **keyid,
421 CMS_KeyTransRecipientInfo *ktri;
422 if (ri->type != CMS_RECIPINFO_TRANS) {
423 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
428 return ossl_cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer,
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 ossl_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 = ossl_cms_get0_cmsctx(cms);
466 if (ri->type != CMS_RECIPINFO_TRANS) {
467 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
471 ec = ossl_cms_get0_env_enc_content(cms);
476 if (!ossl_cms_env_asn1_ctrl(ri, 0))
479 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
481 ossl_cms_ctx_get0_propq(ctx));
485 if (EVP_PKEY_encrypt_init(pctx) <= 0)
489 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
492 ek = OPENSSL_malloc(eklen);
495 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
499 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
502 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
508 EVP_PKEY_CTX_free(pctx);
514 /* Decrypt content key from KTRI */
516 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
517 CMS_RecipientInfo *ri)
519 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
520 EVP_PKEY *pkey = ktri->pkey;
521 unsigned char *ek = NULL;
525 const EVP_CIPHER *cipher = NULL;
526 EVP_CIPHER *fetched_cipher = NULL;
527 CMS_EncryptedContentInfo *ec;
528 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
529 OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
530 const char *propq = ossl_cms_ctx_get0_propq(ctx);
532 ec = ossl_cms_get0_env_enc_content(cms);
534 if (ktri->pkey == NULL) {
535 ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
539 if (cms->d.envelopedData->encryptedContentInfo->havenocert
540 && !cms->d.envelopedData->encryptedContentInfo->debug) {
541 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
542 char name[OSSL_MAX_NAME_SIZE];
544 OBJ_obj2txt(name, sizeof(name), calg->algorithm, 0);
546 (void)ERR_set_mark();
547 fetched_cipher = EVP_CIPHER_fetch(libctx, name, 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(libctx, pkey, propq);
565 if (ktri->pctx == NULL)
568 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
571 if (!ossl_cms_env_asn1_ctrl(ri, 1))
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(ossl_cms_ctx_get0_libctx(ctx), alg,
812 ossl_cms_ctx_get0_propq(ctx));
816 /* Encrypt content key in KEK recipient info */
818 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
819 CMS_RecipientInfo *ri)
821 CMS_EncryptedContentInfo *ec;
822 CMS_KEKRecipientInfo *kekri;
823 unsigned char *wkey = NULL;
826 EVP_CIPHER *cipher = NULL;
828 EVP_CIPHER_CTX *ctx = NULL;
829 const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
831 ec = ossl_cms_get0_env_enc_content(cms);
837 if (kekri->key == NULL) {
838 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
842 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
843 if (cipher == NULL) {
844 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
848 /* 8 byte prefix for AES wrap ciphers */
849 wkey = OPENSSL_malloc(ec->keylen + 8);
851 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
855 ctx = EVP_CIPHER_CTX_new();
857 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
861 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
862 if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
863 || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
864 || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
865 ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
869 if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
870 ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
874 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
879 EVP_CIPHER_free(cipher);
882 EVP_CIPHER_CTX_free(ctx);
887 /* Decrypt content key in KEK recipient info */
889 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
890 CMS_RecipientInfo *ri)
892 CMS_EncryptedContentInfo *ec;
893 CMS_KEKRecipientInfo *kekri;
894 unsigned char *ukey = NULL;
897 EVP_CIPHER *cipher = NULL;
899 EVP_CIPHER_CTX *ctx = NULL;
900 const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
902 ec = ossl_cms_get0_env_enc_content(cms);
909 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
913 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
914 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
915 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
919 /* If encrypted key length is invalid don't bother */
921 if (kekri->encryptedKey->length < 16) {
922 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
926 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
927 if (cipher == NULL) {
928 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
932 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
934 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
938 ctx = EVP_CIPHER_CTX_new();
940 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
944 if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
945 || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
946 kekri->encryptedKey->data,
947 kekri->encryptedKey->length)
948 || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
949 ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR);
955 ec->keylen = ukeylen;
960 EVP_CIPHER_free(cipher);
963 EVP_CIPHER_CTX_free(ctx);
968 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
971 case CMS_RECIPINFO_TRANS:
972 return cms_RecipientInfo_ktri_decrypt(cms, ri);
974 case CMS_RECIPINFO_KEK:
975 return cms_RecipientInfo_kekri_decrypt(cms, ri);
977 case CMS_RECIPINFO_PASS:
978 return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 0);
981 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
986 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
989 case CMS_RECIPINFO_TRANS:
990 return cms_RecipientInfo_ktri_encrypt(cms, ri);
992 case CMS_RECIPINFO_AGREE:
993 return ossl_cms_RecipientInfo_kari_encrypt(cms, ri);
995 case CMS_RECIPINFO_KEK:
996 return cms_RecipientInfo_kekri_encrypt(cms, ri);
998 case CMS_RECIPINFO_PASS:
999 return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 1);
1002 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
1007 /* Check structures and fixup version numbers (if necessary) */
1009 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
1011 CMS_OriginatorInfo *org = env->originatorInfo;
1015 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
1016 CMS_CertificateChoices *cch;
1017 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
1018 if (cch->type == CMS_CERTCHOICE_OTHER) {
1021 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
1022 if (env->version < 3)
1027 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
1028 CMS_RevocationInfoChoice *rch;
1029 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
1030 if (rch->type == CMS_REVCHOICE_OTHER) {
1037 static void cms_env_set_version(CMS_EnvelopedData *env)
1040 CMS_RecipientInfo *ri;
1043 * Can't set version higher than 4 so if 4 or more already nothing to do.
1045 if (env->version >= 4)
1048 cms_env_set_originfo_version(env);
1050 if (env->version >= 3)
1053 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
1054 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
1055 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
1058 } else if (ri->type != CMS_RECIPINFO_TRANS
1059 || ri->d.ktri->version != 0) {
1063 if (env->originatorInfo || env->unprotectedAttrs)
1065 if (env->version == 2)
1070 static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms,
1071 STACK_OF(CMS_RecipientInfo) *ris)
1074 CMS_RecipientInfo *ri;
1076 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
1077 ri = sk_CMS_RecipientInfo_value(ris, i);
1078 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
1084 static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec)
1087 OPENSSL_clear_free(ec->key, ec->keylen);
1092 static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
1094 CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
1095 BIO *contentBio = ossl_cms_EncryptedContent_init_bio(ec,
1096 ossl_cms_get0_cmsctx(cms));
1097 EVP_CIPHER_CTX *ctx = NULL;
1099 if (contentBio == NULL)
1102 BIO_get_cipher_ctx(contentBio, &ctx);
1104 BIO_free(contentBio);
1108 * If the selected cipher supports unprotected attributes,
1109 * deal with it using special ctrl function
1111 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
1112 & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0
1113 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
1114 cms->d.envelopedData->unprotectedAttrs) <= 0) {
1115 BIO_free(contentBio);
1121 static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
1123 CMS_EncryptedContentInfo *ec;
1124 STACK_OF(CMS_RecipientInfo) *rinfos;
1127 CMS_EnvelopedData *env = cms->d.envelopedData;
1129 /* Get BIO first to set up key */
1131 ec = env->encryptedContentInfo;
1132 ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
1134 /* If error end of processing */
1138 /* Now encrypt content key according to each RecipientInfo type */
1139 rinfos = env->recipientInfos;
1140 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1141 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1145 /* And finally set the version */
1146 cms_env_set_version(env);
1151 cms_env_clear_ec(ec);
1158 BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
1160 if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
1161 /* If cipher is set it's encryption */
1162 return cms_EnvelopedData_Encryption_init_bio(cms);
1165 /* If cipher is not set it's decryption */
1166 return cms_EnvelopedData_Decryption_init_bio(cms);
1169 BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms)
1171 CMS_EncryptedContentInfo *ec;
1172 STACK_OF(CMS_RecipientInfo) *rinfos;
1175 CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData;
1177 /* Get BIO first to set up key */
1178 ec = aenv->authEncryptedContentInfo;
1179 /* Set tag for decryption */
1180 if (ec->cipher == NULL) {
1181 ec->tag = aenv->mac->data;
1182 ec->taglen = aenv->mac->length;
1184 ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
1186 /* If error or no cipher end of processing */
1187 if (ret == NULL || ec->cipher == NULL)
1190 /* Now encrypt content key according to each RecipientInfo type */
1191 rinfos = aenv->recipientInfos;
1192 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1193 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1197 /* And finally set the version */
1203 cms_env_clear_ec(ec);
1210 int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
1212 CMS_EnvelopedData *env = NULL;
1213 EVP_CIPHER_CTX *ctx = NULL;
1214 BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
1216 env = ossl_cms_get0_enveloped(cms);
1221 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
1225 BIO_get_cipher_ctx(mbio, &ctx);
1228 * If the selected cipher supports unprotected attributes,
1229 * deal with it using special ctrl function
1231 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
1232 & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
1233 if (env->unprotectedAttrs == NULL)
1234 env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
1236 if (env->unprotectedAttrs == NULL) {
1237 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
1241 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
1242 1, env->unprotectedAttrs) <= 0) {
1243 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
1248 cms_env_set_version(cms->d.envelopedData);
1252 int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio)
1254 EVP_CIPHER_CTX *ctx;
1255 unsigned char *tag = NULL;
1258 BIO_get_cipher_ctx(cmsbio, &ctx);
1261 * The tag is set only for encryption. There is nothing to do for
1264 if (!EVP_CIPHER_CTX_encrypting(ctx))
1267 taglen = EVP_CIPHER_CTX_tag_length(ctx);
1269 || (tag = OPENSSL_malloc(taglen)) == NULL
1270 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
1272 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG);
1276 if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen))
1286 * Get RecipientInfo type (if any) supported by a key (public or private). To
1287 * retain compatibility with previous behaviour if the ctrl value isn't
1288 * supported we assume key transport.
1290 int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk)
1292 /* Check types that we know about */
1293 if (EVP_PKEY_is_a(pk, "DH"))
1294 return CMS_RECIPINFO_AGREE;
1295 else if (EVP_PKEY_is_a(pk, "DHX"))
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 ossl_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 = ossl_cms_pkey_get_ri_type(pk);
1331 if (supportedRiType < 0)
1334 return (supportedRiType == ri_type);