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 "cms_local.h"
18 #include "crypto/asn1.h"
19 #include "crypto/evp.h"
21 DEFINE_STACK_OF(CMS_RecipientInfo)
22 DEFINE_STACK_OF(CMS_RevocationInfoChoice)
23 DEFINE_STACK_OF(X509_ATTRIBUTE)
25 /* CMS EnvelopedData Utilities */
27 static void cms_env_set_version(CMS_EnvelopedData *env);
29 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
31 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
32 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
33 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
36 return cms->d.envelopedData;
39 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
41 if (cms->d.other == NULL) {
42 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
43 if (!cms->d.envelopedData) {
44 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
47 cms->d.envelopedData->version = 0;
48 cms->d.envelopedData->encryptedContentInfo->contentType =
49 OBJ_nid2obj(NID_pkcs7_data);
50 ASN1_OBJECT_free(cms->contentType);
51 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
52 return cms->d.envelopedData;
54 return cms_get0_enveloped(cms);
57 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
61 if (ri->type == CMS_RECIPINFO_TRANS)
62 pkey = ri->d.ktri->pkey;
63 else if (ri->type == CMS_RECIPINFO_AGREE) {
64 EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
68 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
73 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
75 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
77 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
78 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
82 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
88 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
90 CMS_EnvelopedData *env;
91 env = cms_get0_enveloped(cms);
94 return env->recipientInfos;
97 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
102 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
104 if (ri->type == CMS_RECIPINFO_TRANS)
105 return ri->d.ktri->pctx;
106 else if (ri->type == CMS_RECIPINFO_AGREE)
107 return ri->d.kari->pctx;
111 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
113 CMS_ContentInfo *cms;
114 CMS_EnvelopedData *env;
115 cms = CMS_ContentInfo_new();
118 env = cms_enveloped_data_init(cms);
121 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
126 CMS_ContentInfo_free(cms);
127 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
131 int cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
133 CMS_EnvelopedData *env = NULL;
134 EVP_CIPHER_CTX *ctx = NULL;
135 BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
137 env = cms_get0_enveloped(cms);
142 CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, CMS_R_CONTENT_NOT_FOUND);
146 BIO_get_cipher_ctx(mbio, &ctx);
149 * If the selected cipher supports unprotected attributes,
150 * deal with it using special ctrl function
152 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
153 if (cms->d.envelopedData->unprotectedAttrs == NULL)
154 cms->d.envelopedData->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
156 if (cms->d.envelopedData->unprotectedAttrs == NULL) {
157 CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, ERR_R_MALLOC_FAILURE);
161 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
162 1, env->unprotectedAttrs) <= 0) {
163 CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, CMS_R_CTRL_FAILURE);
168 cms_env_set_version(cms->d.envelopedData);
172 /* Key Transport Recipient Info (KTRI) routines */
174 /* Initialise a ktri based on passed certificate and key */
176 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
177 EVP_PKEY *pk, unsigned int flags)
179 CMS_KeyTransRecipientInfo *ktri;
182 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
185 ri->type = CMS_RECIPINFO_TRANS;
189 if (flags & CMS_USE_KEYID) {
191 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
194 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
198 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
202 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype))
211 if (flags & CMS_KEY_PARAM) {
212 ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
213 if (ktri->pctx == NULL)
215 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
217 } else if (!cms_env_asn1_ctrl(ri, 0))
223 * Add a recipient certificate using appropriate type of RecipientInfo
226 CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
227 EVP_PKEY *originatorPrivKey,
228 X509 *originator, unsigned int flags)
230 CMS_RecipientInfo *ri = NULL;
231 CMS_EnvelopedData *env;
233 env = cms_get0_enveloped(cms);
237 /* Initialize recipient info */
238 ri = M_ASN1_new_of(CMS_RecipientInfo);
242 pk = X509_get0_pubkey(recip);
244 CMSerr(CMS_F_CMS_ADD1_RECIPIENT, CMS_R_ERROR_GETTING_PUBLIC_KEY);
248 switch (cms_pkey_get_ri_type(pk)) {
250 case CMS_RECIPINFO_TRANS:
251 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags))
255 case CMS_RECIPINFO_AGREE:
256 if (!cms_RecipientInfo_kari_init(ri, recip, pk, originator, originatorPrivKey, flags))
261 CMSerr(CMS_F_CMS_ADD1_RECIPIENT,
262 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
267 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
273 CMSerr(CMS_F_CMS_ADD1_RECIPIENT, ERR_R_MALLOC_FAILURE);
275 M_ASN1_free_of(ri, CMS_RecipientInfo);
280 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
281 X509 *recip, unsigned int flags)
283 return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
286 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
287 EVP_PKEY **pk, X509 **recip,
290 CMS_KeyTransRecipientInfo *ktri;
291 if (ri->type != CMS_RECIPINFO_TRANS) {
292 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
293 CMS_R_NOT_KEY_TRANSPORT);
302 *recip = ktri->recip;
304 *palg = ktri->keyEncryptionAlgorithm;
308 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
309 ASN1_OCTET_STRING **keyid,
313 CMS_KeyTransRecipientInfo *ktri;
314 if (ri->type != CMS_RECIPINFO_TRANS) {
315 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
316 CMS_R_NOT_KEY_TRANSPORT);
321 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
324 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
326 if (ri->type != CMS_RECIPINFO_TRANS) {
327 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
328 CMS_R_NOT_KEY_TRANSPORT);
331 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
334 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
336 if (ri->type != CMS_RECIPINFO_TRANS) {
337 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
340 EVP_PKEY_free(ri->d.ktri->pkey);
341 ri->d.ktri->pkey = pkey;
345 /* Encrypt content key in key transport recipient info */
347 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
348 CMS_RecipientInfo *ri)
350 CMS_KeyTransRecipientInfo *ktri;
351 CMS_EncryptedContentInfo *ec;
353 unsigned char *ek = NULL;
358 if (ri->type != CMS_RECIPINFO_TRANS) {
359 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT);
363 ec = cms->d.envelopedData->encryptedContentInfo;
368 if (!cms_env_asn1_ctrl(ri, 0))
371 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
375 if (EVP_PKEY_encrypt_init(pctx) <= 0)
379 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
380 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
381 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
385 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
388 ek = OPENSSL_malloc(eklen);
391 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
395 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
398 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
404 EVP_PKEY_CTX_free(pctx);
411 /* Decrypt content key from KTRI */
413 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
414 CMS_RecipientInfo *ri)
416 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
417 EVP_PKEY *pkey = ktri->pkey;
418 unsigned char *ek = NULL;
422 CMS_EncryptedContentInfo *ec;
423 ec = cms->d.envelopedData->encryptedContentInfo;
425 if (ktri->pkey == NULL) {
426 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
430 if (cms->d.envelopedData->encryptedContentInfo->havenocert
431 && !cms->d.envelopedData->encryptedContentInfo->debug) {
432 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
433 const EVP_CIPHER *ciph = EVP_get_cipherbyobj(calg->algorithm);
436 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_UNKNOWN_CIPHER);
440 fixlen = EVP_CIPHER_key_length(ciph);
443 ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
444 if (ktri->pctx == NULL)
447 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
450 if (!cms_env_asn1_ctrl(ri, 1))
453 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
454 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
455 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
459 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
460 ktri->encryptedKey->data,
461 ktri->encryptedKey->length) <= 0)
464 ek = OPENSSL_malloc(eklen);
467 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
471 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
472 ktri->encryptedKey->data,
473 ktri->encryptedKey->length) <= 0
475 || (fixlen != 0 && eklen != fixlen)) {
476 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
482 OPENSSL_clear_free(ec->key, ec->keylen);
487 EVP_PKEY_CTX_free(ktri->pctx);
495 /* Key Encrypted Key (KEK) RecipientInfo routines */
497 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
498 const unsigned char *id, size_t idlen)
500 ASN1_OCTET_STRING tmp_os;
501 CMS_KEKRecipientInfo *kekri;
502 if (ri->type != CMS_RECIPINFO_KEK) {
503 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
507 tmp_os.type = V_ASN1_OCTET_STRING;
509 tmp_os.data = (unsigned char *)id;
510 tmp_os.length = (int)idlen;
511 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
514 /* For now hard code AES key wrap info */
516 static size_t aes_wrap_keylen(int nid)
519 case NID_id_aes128_wrap:
522 case NID_id_aes192_wrap:
525 case NID_id_aes256_wrap:
533 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
534 unsigned char *key, size_t keylen,
535 unsigned char *id, size_t idlen,
536 ASN1_GENERALIZEDTIME *date,
537 ASN1_OBJECT *otherTypeId,
538 ASN1_TYPE *otherType)
540 CMS_RecipientInfo *ri = NULL;
541 CMS_EnvelopedData *env;
542 CMS_KEKRecipientInfo *kekri;
543 env = cms_get0_enveloped(cms);
547 if (nid == NID_undef) {
550 nid = NID_id_aes128_wrap;
554 nid = NID_id_aes192_wrap;
558 nid = NID_id_aes256_wrap;
562 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
568 size_t exp_keylen = aes_wrap_keylen(nid);
571 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
572 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
576 if (keylen != exp_keylen) {
577 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
583 /* Initialize recipient info */
584 ri = M_ASN1_new_of(CMS_RecipientInfo);
588 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
591 ri->type = CMS_RECIPINFO_KEK;
596 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
597 if (kekri->kekid->other == NULL)
601 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
604 /* After this point no calls can fail */
609 kekri->keylen = keylen;
611 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
613 kekri->kekid->date = date;
615 if (kekri->kekid->other) {
616 kekri->kekid->other->keyAttrId = otherTypeId;
617 kekri->kekid->other->keyAttr = otherType;
620 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
621 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
626 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
628 M_ASN1_free_of(ri, CMS_RecipientInfo);
633 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
635 ASN1_OCTET_STRING **pid,
636 ASN1_GENERALIZEDTIME **pdate,
637 ASN1_OBJECT **potherid,
638 ASN1_TYPE **pothertype)
640 CMS_KEKIdentifier *rkid;
641 if (ri->type != CMS_RECIPINFO_KEK) {
642 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
645 rkid = ri->d.kekri->kekid;
647 *palg = ri->d.kekri->keyEncryptionAlgorithm;
649 *pid = rkid->keyIdentifier;
654 *potherid = rkid->other->keyAttrId;
660 *pothertype = rkid->other->keyAttr;
667 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
668 unsigned char *key, size_t keylen)
670 CMS_KEKRecipientInfo *kekri;
671 if (ri->type != CMS_RECIPINFO_KEK) {
672 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
678 kekri->keylen = keylen;
682 static const EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen)
686 return EVP_aes_128_wrap();
689 return EVP_aes_192_wrap();
692 return EVP_aes_256_wrap();
699 /* Encrypt content key in KEK recipient info */
701 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
702 CMS_RecipientInfo *ri)
704 CMS_EncryptedContentInfo *ec;
705 CMS_KEKRecipientInfo *kekri;
706 unsigned char *wkey = NULL;
709 const EVP_CIPHER *cipher = NULL;
711 EVP_CIPHER_CTX *ctx = NULL;
713 ec = cms->d.envelopedData->encryptedContentInfo;
717 if (kekri->key == NULL) {
718 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
722 cipher = cms_get_key_wrap_cipher(kekri->keylen);
723 if (cipher == NULL) {
724 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_INVALID_KEY_LENGTH);
728 /* 8 byte prefix for AES wrap ciphers */
729 wkey = OPENSSL_malloc(ec->keylen + 8);
731 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
735 ctx = EVP_CIPHER_CTX_new();
737 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
741 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
742 if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
743 || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
744 || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
745 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
749 if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
750 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
754 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
761 EVP_CIPHER_CTX_free(ctx);
767 /* Decrypt content key in KEK recipient info */
769 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
770 CMS_RecipientInfo *ri)
772 CMS_EncryptedContentInfo *ec;
773 CMS_KEKRecipientInfo *kekri;
774 unsigned char *ukey = NULL;
777 const EVP_CIPHER *cipher = NULL;
779 EVP_CIPHER_CTX *ctx = NULL;
781 ec = cms->d.envelopedData->encryptedContentInfo;
786 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
790 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
791 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
792 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
793 CMS_R_INVALID_KEY_LENGTH);
797 /* If encrypted key length is invalid don't bother */
799 if (kekri->encryptedKey->length < 16) {
800 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
801 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
805 cipher = cms_get_key_wrap_cipher(kekri->keylen);
806 if (cipher == NULL) {
807 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_INVALID_KEY_LENGTH);
811 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
813 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
817 ctx = EVP_CIPHER_CTX_new();
819 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
823 if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
824 || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
825 kekri->encryptedKey->data,
826 kekri->encryptedKey->length)
827 || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
828 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
834 ec->keylen = ukeylen;
841 EVP_CIPHER_CTX_free(ctx);
847 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
850 case CMS_RECIPINFO_TRANS:
851 return cms_RecipientInfo_ktri_decrypt(cms, ri);
853 case CMS_RECIPINFO_KEK:
854 return cms_RecipientInfo_kekri_decrypt(cms, ri);
856 case CMS_RECIPINFO_PASS:
857 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
860 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
861 CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
866 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
869 case CMS_RECIPINFO_TRANS:
870 return cms_RecipientInfo_ktri_encrypt(cms, ri);
872 case CMS_RECIPINFO_AGREE:
873 return cms_RecipientInfo_kari_encrypt(cms, ri);
875 case CMS_RECIPINFO_KEK:
876 return cms_RecipientInfo_kekri_encrypt(cms, ri);
878 case CMS_RECIPINFO_PASS:
879 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
882 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
883 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
888 /* Check structures and fixup version numbers (if necessary) */
890 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
892 CMS_OriginatorInfo *org = env->originatorInfo;
896 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
897 CMS_CertificateChoices *cch;
898 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
899 if (cch->type == CMS_CERTCHOICE_OTHER) {
902 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
903 if (env->version < 3)
908 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
909 CMS_RevocationInfoChoice *rch;
910 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
911 if (rch->type == CMS_REVCHOICE_OTHER) {
918 static void cms_env_set_version(CMS_EnvelopedData *env)
921 CMS_RecipientInfo *ri;
924 * Can't set version higher than 4 so if 4 or more already nothing to do.
926 if (env->version >= 4)
929 cms_env_set_originfo_version(env);
931 if (env->version >= 3)
934 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
935 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
936 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
939 } else if (ri->type != CMS_RECIPINFO_TRANS
940 || ri->d.ktri->version != 0) {
944 if (env->originatorInfo || env->unprotectedAttrs)
946 if (env->version == 2)
951 static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
953 CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
954 BIO *contentBio = cms_EncryptedContent_init_bio(ec);
955 EVP_CIPHER_CTX *ctx = NULL;
957 if (contentBio == NULL)
960 BIO_get_cipher_ctx(contentBio, &ctx);
962 BIO_free(contentBio);
966 * If the selected cipher supports unprotected attributes,
967 * deal with it using special ctrl function
969 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
970 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
971 cms->d.envelopedData->unprotectedAttrs) <= 0) {
972 BIO_free(contentBio);
978 static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
980 CMS_EncryptedContentInfo *ec;
981 STACK_OF(CMS_RecipientInfo) *rinfos;
982 CMS_RecipientInfo *ri;
986 /* Get BIO first to set up key */
988 ec = cms->d.envelopedData->encryptedContentInfo;
989 ret = cms_EncryptedContent_init_bio(ec);
991 /* If error end of processing */
995 /* Now encrypt content key according to each RecipientInfo type */
996 rinfos = cms->d.envelopedData->recipientInfos;
998 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
999 ri = sk_CMS_RecipientInfo_value(rinfos, i);
1000 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
1001 CMSerr(0, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1005 cms_env_set_version(cms->d.envelopedData);
1011 OPENSSL_clear_free(ec->key, ec->keylen);
1020 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
1022 if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
1023 /* If cipher is set it's encryption */
1024 return cms_EnvelopedData_Encryption_init_bio(cms);
1027 /* If cipher is not set it's decryption */
1028 return cms_EnvelopedData_Decryption_init_bio(cms);
1032 * Get RecipientInfo type (if any) supported by a key (public or private). To
1033 * retain compatibility with previous behaviour if the ctrl value isn't
1034 * supported we assume key transport.
1036 int cms_pkey_get_ri_type(EVP_PKEY *pk)
1038 if (pk->ameth && pk->ameth->pkey_ctrl) {
1040 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1044 return CMS_RECIPINFO_TRANS;
1047 int cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
1049 int supportedRiType;
1051 if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
1054 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED, ri_type, &r);
1059 supportedRiType = cms_pkey_get_ri_type(pk);
1060 if (supportedRiType < 0)
1063 return (supportedRiType == ri_type);