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 /* CMS EnvelopedData Utilities */
23 static void cms_env_set_version(CMS_EnvelopedData *env);
25 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
27 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
28 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
29 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
32 return cms->d.envelopedData;
35 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
37 if (cms->d.other == NULL) {
38 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
39 if (!cms->d.envelopedData) {
40 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
43 cms->d.envelopedData->version = 0;
44 cms->d.envelopedData->encryptedContentInfo->contentType =
45 OBJ_nid2obj(NID_pkcs7_data);
46 ASN1_OBJECT_free(cms->contentType);
47 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
48 return cms->d.envelopedData;
50 return cms_get0_enveloped(cms);
53 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
57 if (ri->type == CMS_RECIPINFO_TRANS)
58 pkey = ri->d.ktri->pkey;
59 else if (ri->type == CMS_RECIPINFO_AGREE) {
60 EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
64 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
69 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
71 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
73 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
74 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
78 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
84 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
86 CMS_EnvelopedData *env;
87 env = cms_get0_enveloped(cms);
90 return env->recipientInfos;
93 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
98 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
100 if (ri->type == CMS_RECIPINFO_TRANS)
101 return ri->d.ktri->pctx;
102 else if (ri->type == CMS_RECIPINFO_AGREE)
103 return ri->d.kari->pctx;
107 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
109 CMS_ContentInfo *cms;
110 CMS_EnvelopedData *env;
111 cms = CMS_ContentInfo_new();
114 env = cms_enveloped_data_init(cms);
117 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
122 CMS_ContentInfo_free(cms);
123 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
127 int cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
129 CMS_EnvelopedData *env = NULL;
130 EVP_CIPHER_CTX *ctx = NULL;
131 BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
133 env = cms_get0_enveloped(cms);
138 CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, CMS_R_CONTENT_NOT_FOUND);
142 BIO_get_cipher_ctx(mbio, &ctx);
145 * If the selected cipher supports unprotected attributes,
146 * deal with it using special ctrl function
148 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
149 if (cms->d.envelopedData->unprotectedAttrs == NULL)
150 cms->d.envelopedData->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
152 if (cms->d.envelopedData->unprotectedAttrs == NULL) {
153 CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, ERR_R_MALLOC_FAILURE);
157 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
158 1, env->unprotectedAttrs) <= 0) {
159 CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, CMS_R_CTRL_FAILURE);
164 cms_env_set_version(cms->d.envelopedData);
168 /* Key Transport Recipient Info (KTRI) routines */
170 /* Initialise a ktri based on passed certificate and key */
172 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
173 EVP_PKEY *pk, unsigned int flags)
175 CMS_KeyTransRecipientInfo *ktri;
178 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
181 ri->type = CMS_RECIPINFO_TRANS;
185 if (flags & CMS_USE_KEYID) {
187 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
190 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
194 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
198 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype))
207 if (flags & CMS_KEY_PARAM) {
208 ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
209 if (ktri->pctx == NULL)
211 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
213 } else if (!cms_env_asn1_ctrl(ri, 0))
219 * Add a recipient certificate using appropriate type of RecipientInfo
222 CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
223 EVP_PKEY *originatorPrivKey,
224 X509 *originator, unsigned int flags)
226 CMS_RecipientInfo *ri = NULL;
227 CMS_EnvelopedData *env;
229 env = cms_get0_enveloped(cms);
233 /* Initialize recipient info */
234 ri = M_ASN1_new_of(CMS_RecipientInfo);
238 pk = X509_get0_pubkey(recip);
240 CMSerr(CMS_F_CMS_ADD1_RECIPIENT, CMS_R_ERROR_GETTING_PUBLIC_KEY);
244 switch (cms_pkey_get_ri_type(pk)) {
246 case CMS_RECIPINFO_TRANS:
247 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags))
251 case CMS_RECIPINFO_AGREE:
252 if (!cms_RecipientInfo_kari_init(ri, recip, pk, originator, originatorPrivKey, flags))
257 CMSerr(CMS_F_CMS_ADD1_RECIPIENT,
258 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
263 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
269 CMSerr(CMS_F_CMS_ADD1_RECIPIENT, ERR_R_MALLOC_FAILURE);
271 M_ASN1_free_of(ri, CMS_RecipientInfo);
276 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
277 X509 *recip, unsigned int flags)
279 return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
282 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
283 EVP_PKEY **pk, X509 **recip,
286 CMS_KeyTransRecipientInfo *ktri;
287 if (ri->type != CMS_RECIPINFO_TRANS) {
288 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
289 CMS_R_NOT_KEY_TRANSPORT);
298 *recip = ktri->recip;
300 *palg = ktri->keyEncryptionAlgorithm;
304 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
305 ASN1_OCTET_STRING **keyid,
309 CMS_KeyTransRecipientInfo *ktri;
310 if (ri->type != CMS_RECIPINFO_TRANS) {
311 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
312 CMS_R_NOT_KEY_TRANSPORT);
317 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
320 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
322 if (ri->type != CMS_RECIPINFO_TRANS) {
323 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
324 CMS_R_NOT_KEY_TRANSPORT);
327 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
330 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
332 if (ri->type != CMS_RECIPINFO_TRANS) {
333 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
336 EVP_PKEY_free(ri->d.ktri->pkey);
337 ri->d.ktri->pkey = pkey;
341 /* Encrypt content key in key transport recipient info */
343 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
344 CMS_RecipientInfo *ri)
346 CMS_KeyTransRecipientInfo *ktri;
347 CMS_EncryptedContentInfo *ec;
349 unsigned char *ek = NULL;
354 if (ri->type != CMS_RECIPINFO_TRANS) {
355 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT);
359 ec = cms->d.envelopedData->encryptedContentInfo;
364 if (!cms_env_asn1_ctrl(ri, 0))
367 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
371 if (EVP_PKEY_encrypt_init(pctx) <= 0)
375 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
376 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
377 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
381 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
384 ek = OPENSSL_malloc(eklen);
387 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
391 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
394 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
400 EVP_PKEY_CTX_free(pctx);
407 /* Decrypt content key from KTRI */
409 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
410 CMS_RecipientInfo *ri)
412 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
413 EVP_PKEY *pkey = ktri->pkey;
414 unsigned char *ek = NULL;
418 CMS_EncryptedContentInfo *ec;
419 ec = cms->d.envelopedData->encryptedContentInfo;
421 if (ktri->pkey == NULL) {
422 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
426 if (cms->d.envelopedData->encryptedContentInfo->havenocert
427 && !cms->d.envelopedData->encryptedContentInfo->debug) {
428 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
429 const EVP_CIPHER *ciph = EVP_get_cipherbyobj(calg->algorithm);
432 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_UNKNOWN_CIPHER);
436 fixlen = EVP_CIPHER_key_length(ciph);
439 ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
440 if (ktri->pctx == NULL)
443 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
446 if (!cms_env_asn1_ctrl(ri, 1))
449 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
450 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
451 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
455 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
456 ktri->encryptedKey->data,
457 ktri->encryptedKey->length) <= 0)
460 ek = OPENSSL_malloc(eklen);
463 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
467 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
468 ktri->encryptedKey->data,
469 ktri->encryptedKey->length) <= 0
471 || (fixlen != 0 && eklen != fixlen)) {
472 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
478 OPENSSL_clear_free(ec->key, ec->keylen);
483 EVP_PKEY_CTX_free(ktri->pctx);
491 /* Key Encrypted Key (KEK) RecipientInfo routines */
493 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
494 const unsigned char *id, size_t idlen)
496 ASN1_OCTET_STRING tmp_os;
497 CMS_KEKRecipientInfo *kekri;
498 if (ri->type != CMS_RECIPINFO_KEK) {
499 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
503 tmp_os.type = V_ASN1_OCTET_STRING;
505 tmp_os.data = (unsigned char *)id;
506 tmp_os.length = (int)idlen;
507 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
510 /* For now hard code AES key wrap info */
512 static size_t aes_wrap_keylen(int nid)
515 case NID_id_aes128_wrap:
518 case NID_id_aes192_wrap:
521 case NID_id_aes256_wrap:
529 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
530 unsigned char *key, size_t keylen,
531 unsigned char *id, size_t idlen,
532 ASN1_GENERALIZEDTIME *date,
533 ASN1_OBJECT *otherTypeId,
534 ASN1_TYPE *otherType)
536 CMS_RecipientInfo *ri = NULL;
537 CMS_EnvelopedData *env;
538 CMS_KEKRecipientInfo *kekri;
539 env = cms_get0_enveloped(cms);
543 if (nid == NID_undef) {
546 nid = NID_id_aes128_wrap;
550 nid = NID_id_aes192_wrap;
554 nid = NID_id_aes256_wrap;
558 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
564 size_t exp_keylen = aes_wrap_keylen(nid);
567 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
568 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
572 if (keylen != exp_keylen) {
573 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
579 /* Initialize recipient info */
580 ri = M_ASN1_new_of(CMS_RecipientInfo);
584 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
587 ri->type = CMS_RECIPINFO_KEK;
592 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
593 if (kekri->kekid->other == NULL)
597 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
600 /* After this point no calls can fail */
605 kekri->keylen = keylen;
607 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
609 kekri->kekid->date = date;
611 if (kekri->kekid->other) {
612 kekri->kekid->other->keyAttrId = otherTypeId;
613 kekri->kekid->other->keyAttr = otherType;
616 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
617 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
622 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
624 M_ASN1_free_of(ri, CMS_RecipientInfo);
629 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
631 ASN1_OCTET_STRING **pid,
632 ASN1_GENERALIZEDTIME **pdate,
633 ASN1_OBJECT **potherid,
634 ASN1_TYPE **pothertype)
636 CMS_KEKIdentifier *rkid;
637 if (ri->type != CMS_RECIPINFO_KEK) {
638 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
641 rkid = ri->d.kekri->kekid;
643 *palg = ri->d.kekri->keyEncryptionAlgorithm;
645 *pid = rkid->keyIdentifier;
650 *potherid = rkid->other->keyAttrId;
656 *pothertype = rkid->other->keyAttr;
663 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
664 unsigned char *key, size_t keylen)
666 CMS_KEKRecipientInfo *kekri;
667 if (ri->type != CMS_RECIPINFO_KEK) {
668 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
674 kekri->keylen = keylen;
678 static const EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen)
682 return EVP_aes_128_wrap();
685 return EVP_aes_192_wrap();
688 return EVP_aes_256_wrap();
695 /* Encrypt content key in KEK recipient info */
697 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
698 CMS_RecipientInfo *ri)
700 CMS_EncryptedContentInfo *ec;
701 CMS_KEKRecipientInfo *kekri;
702 unsigned char *wkey = NULL;
705 const EVP_CIPHER *cipher = NULL;
707 EVP_CIPHER_CTX *ctx = NULL;
709 ec = cms->d.envelopedData->encryptedContentInfo;
713 if (kekri->key == NULL) {
714 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
718 cipher = cms_get_key_wrap_cipher(kekri->keylen);
719 if (cipher == NULL) {
720 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_INVALID_KEY_LENGTH);
724 /* 8 byte prefix for AES wrap ciphers */
725 wkey = OPENSSL_malloc(ec->keylen + 8);
727 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
731 ctx = EVP_CIPHER_CTX_new();
733 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
737 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
738 if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
739 || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
740 || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
741 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
745 if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
746 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
750 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
757 EVP_CIPHER_CTX_free(ctx);
763 /* Decrypt content key in KEK recipient info */
765 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
766 CMS_RecipientInfo *ri)
768 CMS_EncryptedContentInfo *ec;
769 CMS_KEKRecipientInfo *kekri;
770 unsigned char *ukey = NULL;
773 const EVP_CIPHER *cipher = NULL;
775 EVP_CIPHER_CTX *ctx = NULL;
777 ec = cms->d.envelopedData->encryptedContentInfo;
782 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
786 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
787 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
788 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
789 CMS_R_INVALID_KEY_LENGTH);
793 /* If encrypted key length is invalid don't bother */
795 if (kekri->encryptedKey->length < 16) {
796 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
797 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
801 cipher = cms_get_key_wrap_cipher(kekri->keylen);
802 if (cipher == NULL) {
803 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_INVALID_KEY_LENGTH);
807 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
809 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
813 ctx = EVP_CIPHER_CTX_new();
815 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
819 if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
820 || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
821 kekri->encryptedKey->data,
822 kekri->encryptedKey->length)
823 || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
824 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
830 ec->keylen = ukeylen;
837 EVP_CIPHER_CTX_free(ctx);
843 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
846 case CMS_RECIPINFO_TRANS:
847 return cms_RecipientInfo_ktri_decrypt(cms, ri);
849 case CMS_RECIPINFO_KEK:
850 return cms_RecipientInfo_kekri_decrypt(cms, ri);
852 case CMS_RECIPINFO_PASS:
853 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
856 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
857 CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
862 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
865 case CMS_RECIPINFO_TRANS:
866 return cms_RecipientInfo_ktri_encrypt(cms, ri);
868 case CMS_RECIPINFO_AGREE:
869 return cms_RecipientInfo_kari_encrypt(cms, ri);
871 case CMS_RECIPINFO_KEK:
872 return cms_RecipientInfo_kekri_encrypt(cms, ri);
874 case CMS_RECIPINFO_PASS:
875 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
878 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
879 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
884 /* Check structures and fixup version numbers (if necessary) */
886 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
888 CMS_OriginatorInfo *org = env->originatorInfo;
892 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
893 CMS_CertificateChoices *cch;
894 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
895 if (cch->type == CMS_CERTCHOICE_OTHER) {
898 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
899 if (env->version < 3)
904 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
905 CMS_RevocationInfoChoice *rch;
906 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
907 if (rch->type == CMS_REVCHOICE_OTHER) {
914 static void cms_env_set_version(CMS_EnvelopedData *env)
917 CMS_RecipientInfo *ri;
920 * Can't set version higher than 4 so if 4 or more already nothing to do.
922 if (env->version >= 4)
925 cms_env_set_originfo_version(env);
927 if (env->version >= 3)
930 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
931 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
932 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
935 } else if (ri->type != CMS_RECIPINFO_TRANS
936 || ri->d.ktri->version != 0) {
940 if (env->originatorInfo || env->unprotectedAttrs)
942 if (env->version == 2)
947 static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
949 CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
950 BIO *contentBio = cms_EncryptedContent_init_bio(ec);
951 EVP_CIPHER_CTX *ctx = NULL;
953 if (contentBio == NULL)
956 BIO_get_cipher_ctx(contentBio, &ctx);
958 BIO_free(contentBio);
962 * If the selected cipher supports unprotected attributes,
963 * deal with it using special ctrl function
965 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
966 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
967 cms->d.envelopedData->unprotectedAttrs) <= 0) {
968 BIO_free(contentBio);
974 static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
976 CMS_EncryptedContentInfo *ec;
977 STACK_OF(CMS_RecipientInfo) *rinfos;
978 CMS_RecipientInfo *ri;
982 /* Get BIO first to set up key */
984 ec = cms->d.envelopedData->encryptedContentInfo;
985 ret = cms_EncryptedContent_init_bio(ec);
987 /* If error end of processing */
991 /* Now encrypt content key according to each RecipientInfo type */
992 rinfos = cms->d.envelopedData->recipientInfos;
994 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
995 ri = sk_CMS_RecipientInfo_value(rinfos, i);
996 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
997 CMSerr(0, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1001 cms_env_set_version(cms->d.envelopedData);
1007 OPENSSL_clear_free(ec->key, ec->keylen);
1016 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
1018 if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
1019 /* If cipher is set it's encryption */
1020 return cms_EnvelopedData_Encryption_init_bio(cms);
1023 /* If cipher is not set it's decryption */
1024 return cms_EnvelopedData_Decryption_init_bio(cms);
1028 * Get RecipientInfo type (if any) supported by a key (public or private). To
1029 * retain compatibility with previous behaviour if the ctrl value isn't
1030 * supported we assume key transport.
1032 int cms_pkey_get_ri_type(EVP_PKEY *pk)
1034 if (pk->ameth && pk->ameth->pkey_ctrl) {
1036 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1040 return CMS_RECIPINFO_TRANS;
1043 int cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
1045 int supportedRiType;
1047 if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
1050 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED, ri_type, &r);
1055 supportedRiType = cms_pkey_get_ri_type(pk);
1056 if (supportedRiType < 0)
1059 return (supportedRiType == ri_type);