2 * Copyright 2008-2023 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/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
16 #include "cms_local.h"
17 #include "crypto/asn1.h"
19 static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
24 rbio = BIO_new(BIO_s_null());
25 else if (flags & CMS_TEXT) {
26 rbio = BIO_new(BIO_s_mem());
27 BIO_set_mem_eof_return(rbio, 0);
33 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
35 unsigned char buf[4096];
39 tmpout = cms_get_text_bio(out, flags);
42 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
46 /* Read all content through chain to process digest, decrypt etc */
48 i = BIO_read(in, buf, sizeof(buf));
50 if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
51 if (BIO_get_cipher_status(in) <= 0)
59 if (tmpout != NULL && (BIO_write(tmpout, buf, i) != i))
63 if (flags & CMS_TEXT) {
64 if (!SMIME_text(tmpout, out)) {
65 ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
78 static int check_content(CMS_ContentInfo *cms)
80 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
82 if (pos == NULL || *pos == NULL) {
83 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
89 static void do_free_upto(BIO *f, BIO *upto)
98 } while (f != NULL && f != upto);
104 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
109 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
110 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DATA);
113 cont = CMS_dataInit(cms, NULL);
116 r = cms_copy_content(out, cont, flags);
121 CMS_ContentInfo *CMS_data_create_ex(BIO *in, unsigned int flags,
122 OSSL_LIB_CTX *libctx, const char *propq)
124 CMS_ContentInfo *cms = ossl_cms_Data_create(libctx, propq);
129 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
132 CMS_ContentInfo_free(cms);
136 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
138 return CMS_data_create_ex(in, flags, NULL, NULL);
141 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
147 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
148 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DIGESTED_DATA);
152 if (dcont == NULL && !check_content(cms))
155 cont = CMS_dataInit(cms, dcont);
159 r = cms_copy_content(out, cont, flags);
161 r = ossl_cms_DigestedData_do_final(cms, cont, 1);
162 do_free_upto(cont, dcont);
166 CMS_ContentInfo *CMS_digest_create_ex(BIO *in, const EVP_MD *md,
167 unsigned int flags, OSSL_LIB_CTX *ctx,
170 CMS_ContentInfo *cms;
173 * Because the EVP_MD is cached and can be a legacy algorithm, we
174 * cannot fetch the algorithm if it isn't supplied.
178 cms = ossl_cms_DigestedData_create(md, ctx, propq);
182 if (!(flags & CMS_DETACHED))
183 CMS_set_detached(cms, 0);
185 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
188 CMS_ContentInfo_free(cms);
192 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
195 return CMS_digest_create_ex(in, md, flags, NULL, NULL);
198 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
199 const unsigned char *key, size_t keylen,
200 BIO *dcont, BIO *out, unsigned int flags)
205 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
206 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENCRYPTED_DATA);
210 if (dcont == NULL && !check_content(cms))
213 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
215 cont = CMS_dataInit(cms, dcont);
218 r = cms_copy_content(out, cont, flags);
219 do_free_upto(cont, dcont);
223 CMS_ContentInfo *CMS_EncryptedData_encrypt_ex(BIO *in, const EVP_CIPHER *cipher,
224 const unsigned char *key,
225 size_t keylen, unsigned int flags,
226 OSSL_LIB_CTX *libctx,
229 CMS_ContentInfo *cms;
231 if (cipher == NULL) {
232 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER);
235 cms = CMS_ContentInfo_new_ex(libctx, propq);
238 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
241 if (!(flags & CMS_DETACHED))
242 CMS_set_detached(cms, 0);
244 if ((flags & (CMS_STREAM | CMS_PARTIAL))
245 || CMS_final(cms, in, NULL, flags))
249 CMS_ContentInfo_free(cms);
253 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
254 const unsigned char *key,
255 size_t keylen, unsigned int flags)
257 return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
261 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
263 STACK_OF(X509) *untrusted,
264 STACK_OF(X509_CRL) *crls,
265 STACK_OF(X509) **chain,
266 const CMS_CTX *cms_ctx)
272 ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx),
273 ossl_cms_ctx_get0_propq(cms_ctx));
275 ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
278 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
279 if (!X509_STORE_CTX_init(ctx, store, signer, untrusted)) {
280 ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR);
283 X509_STORE_CTX_set_default(ctx, "smime_sign");
285 X509_STORE_CTX_set0_crls(ctx, crls);
287 i = X509_verify_cert(ctx);
289 j = X509_STORE_CTX_get_error(ctx);
290 ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR,
291 "Verify error: %s", X509_verify_cert_error_string(j));
296 /* also send back the trust chain when required */
298 *chain = X509_STORE_CTX_get1_chain(ctx);
300 X509_STORE_CTX_free(ctx);
305 /* This strongly overlaps with PKCS7_verify() */
306 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
307 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
310 STACK_OF(CMS_SignerInfo) *sinfos;
311 STACK_OF(X509) *cms_certs = NULL;
312 STACK_OF(X509_CRL) *crls = NULL;
313 STACK_OF(X509) **si_chains = NULL;
315 int i, scount = 0, ret = 0;
316 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
317 int cadesVerify = (flags & CMS_CADES) != 0;
318 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
320 if (dcont == NULL && !check_content(cms))
322 if (dcont != NULL && !(flags & CMS_BINARY)) {
323 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
325 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
326 flags |= CMS_ASCIICRLF;
329 /* Attempt to find all signer certificates */
331 sinfos = CMS_get0_SignerInfos(cms);
333 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
334 ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
338 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
339 si = sk_CMS_SignerInfo_value(sinfos, i);
340 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
345 if (scount != sk_CMS_SignerInfo_num(sinfos))
346 scount += CMS_set1_signers_certs(cms, certs, flags);
348 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
349 ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
353 /* Attempt to verify all signers certs */
354 /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
356 if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
358 /* Certificate trust chain is required to check CAdES signature */
359 si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
360 if (si_chains == NULL)
363 cms_certs = CMS_get1_certs(cms);
364 if (!(flags & CMS_NOCRL))
365 crls = CMS_get1_crls(cms);
366 for (i = 0; i < scount; i++) {
367 si = sk_CMS_SignerInfo_value(sinfos, i);
369 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
370 si_chains ? &si_chains[i] : NULL,
376 /* Attempt to verify all SignerInfo signed attribute signatures */
378 if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
379 for (i = 0; i < scount; i++) {
380 si = sk_CMS_SignerInfo_value(sinfos, i);
381 if (CMS_signed_get_attr_count(si) < 0)
383 if (CMS_SignerInfo_verify(si) <= 0)
386 STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
388 if (ossl_cms_check_signing_certs(si, si_chain) <= 0)
395 * Performance optimization: if the content is a memory BIO then store
396 * its contents in a temporary read only memory BIO. This avoids
397 * potentially large numbers of slow copies of data which will occur when
398 * reading from a read write memory BIO when signatures are calculated.
401 if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
405 len = BIO_get_mem_data(dcont, &ptr);
406 tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
408 ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB);
415 * If not binary mode and detached generate digests by *writing* through
416 * the BIO. That makes it possible to canonicalise the input.
418 if (!(flags & SMIME_BINARY) && dcont) {
420 * Create output BIO so we can either handle text or to ensure
421 * included content doesn't override detached content.
423 tmpout = cms_get_text_bio(out, flags);
424 if (tmpout == NULL) {
425 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
428 cmsbio = CMS_dataInit(cms, tmpout);
432 * Don't use SMIME_TEXT for verify: it adds headers and we want to
435 if (!SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT))
438 if (flags & CMS_TEXT) {
439 if (!SMIME_text(tmpout, out)) {
440 ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
445 cmsbio = CMS_dataInit(cms, tmpin);
449 if (!cms_copy_content(out, cmsbio, flags))
453 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
454 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
455 si = sk_CMS_SignerInfo_value(sinfos, i);
456 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
457 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR);
465 if (!(flags & SMIME_BINARY) && dcont) {
466 do_free_upto(cmsbio, tmpout);
470 if (dcont && (tmpin == dcont))
471 do_free_upto(cmsbio, dcont);
473 BIO_free_all(cmsbio);
477 BIO_free_all(tmpout);
480 if (si_chains != NULL) {
481 for (i = 0; i < scount; ++i)
482 OSSL_STACK_OF_X509_free(si_chains[i]);
483 OPENSSL_free(si_chains);
485 OSSL_STACK_OF_X509_free(cms_certs);
486 sk_X509_CRL_pop_free(crls, X509_CRL_free);
491 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
492 STACK_OF(X509) *certs,
493 X509_STORE *store, unsigned int flags)
497 flags &= ~(CMS_DETACHED | CMS_TEXT);
498 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
501 return ossl_cms_Receipt_verify(rcms, ocms);
504 CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
505 STACK_OF(X509) *certs, BIO *data,
506 unsigned int flags, OSSL_LIB_CTX *libctx,
509 CMS_ContentInfo *cms;
512 cms = CMS_ContentInfo_new_ex(libctx, propq);
513 if (cms == NULL || !CMS_SignedData_init(cms)) {
514 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
517 if (flags & CMS_ASCIICRLF
518 && !CMS_set1_eContentType(cms,
519 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF))) {
520 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
524 if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
525 ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
529 for (i = 0; i < sk_X509_num(certs); i++) {
530 X509 *x = sk_X509_value(certs, i);
532 if (!CMS_add1_cert(cms, x)) {
533 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
538 if (!(flags & CMS_DETACHED))
539 CMS_set_detached(cms, 0);
541 if ((flags & (CMS_STREAM | CMS_PARTIAL))
542 || CMS_final(cms, data, NULL, flags))
548 CMS_ContentInfo_free(cms);
552 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
553 BIO *data, unsigned int flags)
555 return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
558 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
559 X509 *signcert, EVP_PKEY *pkey,
560 STACK_OF(X509) *certs, unsigned int flags)
562 CMS_SignerInfo *rct_si;
563 CMS_ContentInfo *cms = NULL;
564 ASN1_OCTET_STRING **pos, *os = NULL;
565 BIO *rct_cont = NULL;
567 const CMS_CTX *ctx = si->cms_ctx;
569 flags &= ~(CMS_STREAM | CMS_TEXT);
570 /* Not really detached but avoids content being allocated */
571 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
572 if (pkey == NULL || signcert == NULL) {
573 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
577 /* Initialize signed data */
579 cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags,
580 ossl_cms_ctx_get0_libctx(ctx),
581 ossl_cms_ctx_get0_propq(ctx));
585 /* Set inner content type to signed receipt */
586 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
589 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
591 ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
595 os = ossl_cms_encode_Receipt(si);
599 /* Set content to digest */
600 rct_cont = BIO_new_mem_buf(os->data, os->length);
601 if (rct_cont == NULL)
604 /* Add msgSigDigest attribute */
606 if (!ossl_cms_msgSigDigest_add1(rct_si, si))
609 /* Finalize structure */
610 if (!CMS_final(cms, rct_cont, NULL, flags))
613 /* Set embedded content */
614 pos = CMS_get0_content(cms);
625 CMS_ContentInfo_free(cms);
626 ASN1_OCTET_STRING_free(os);
631 CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
632 const EVP_CIPHER *cipher, unsigned int flags,
633 OSSL_LIB_CTX *libctx, const char *propq)
635 CMS_ContentInfo *cms;
640 cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
641 ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
642 : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
644 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
647 for (i = 0; i < sk_X509_num(certs); i++) {
648 recip = sk_X509_value(certs, i);
649 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
650 ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
655 if (!(flags & CMS_DETACHED))
656 CMS_set_detached(cms, 0);
658 if ((flags & (CMS_STREAM | CMS_PARTIAL))
659 || CMS_final(cms, data, NULL, flags))
662 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
665 CMS_ContentInfo_free(cms);
669 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
670 const EVP_CIPHER *cipher, unsigned int flags)
672 return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
675 static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
676 CMS_RecipientInfo *ri,
677 EVP_PKEY *pk, X509 *cert, X509 *peer)
680 STACK_OF(CMS_RecipientEncryptedKey) *reks;
681 CMS_RecipientEncryptedKey *rek;
683 reks = CMS_RecipientInfo_kari_get0_reks(ri);
684 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
687 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
688 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
690 CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
691 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
692 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
695 return cert == NULL ? 0 : -1;
700 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
702 return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
705 int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
706 X509 *cert, X509 *peer)
708 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
709 CMS_RecipientInfo *ri;
710 int i, r, cms_pkey_ri_type;
711 int debug = 0, match_ri = 0;
712 CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
714 /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
716 OPENSSL_clear_free(ec->key, ec->keylen);
721 if (ris != NULL && ec != NULL)
724 cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk);
725 if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
726 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
730 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
733 ri = sk_CMS_RecipientInfo_value(ris, i);
734 ri_type = CMS_RecipientInfo_type(ri);
735 if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type))
738 if (ri_type == CMS_RECIPINFO_AGREE) {
739 r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
745 /* If we have a cert, try matching RecipientInfo, else try them all */
746 else if (cert == NULL || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
748 CMS_RecipientInfo_set0_pkey(ri, pk);
749 r = CMS_RecipientInfo_decrypt(cms, ri);
750 CMS_RecipientInfo_set0_pkey(ri, NULL);
753 * If not debugging clear any error and return success to
754 * avoid leaking of information useful to MMA
762 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
766 * If no cert and not debugging don't leave loop after first
767 * successful decrypt. Always attempt to decrypt all recipients
768 * to avoid leaking timing of a successful decrypt.
770 else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
774 /* If no cert, key transport and not debugging always return success */
776 && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
784 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
789 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
790 unsigned char *key, size_t keylen,
791 const unsigned char *id, size_t idlen)
793 STACK_OF(CMS_RecipientInfo) *ris;
794 CMS_RecipientInfo *ri;
795 int i, r, match_ri = 0;
797 ris = CMS_get0_RecipientInfos(cms);
798 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
799 ri = sk_CMS_RecipientInfo_value(ris, i);
800 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
803 /* If we have an id, try matching RecipientInfo, else try them all */
805 || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
807 CMS_RecipientInfo_set0_key(ri, key, keylen);
808 r = CMS_RecipientInfo_decrypt(cms, ri);
809 CMS_RecipientInfo_set0_key(ri, NULL, 0);
813 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
821 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
826 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
827 unsigned char *pass, ossl_ssize_t passlen)
829 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
830 CMS_RecipientInfo *ri;
831 int i, r, match_ri = 0;
832 CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
834 /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
836 OPENSSL_clear_free(ec->key, ec->keylen);
841 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
842 ri = sk_CMS_RecipientInfo_value(ris, i);
843 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
846 /* Must try each PasswordRecipientInfo */
848 CMS_RecipientInfo_set0_password(ri, pass, passlen);
849 r = CMS_RecipientInfo_decrypt(cms, ri);
850 CMS_RecipientInfo_set0_password(ri, NULL, 0);
856 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
861 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
862 BIO *dcont, BIO *out, unsigned int flags)
866 CMS_EncryptedContentInfo *ec;
867 int nid = OBJ_obj2nid(CMS_get0_type(cms));
869 if (nid != NID_pkcs7_enveloped
870 && nid != NID_id_smime_ct_authEnvelopedData) {
871 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA);
874 if (dcont == NULL && !check_content(cms))
876 ec = ossl_cms_get0_env_enc_content(cms);
877 ec->debug = (flags & CMS_DEBUG_DECRYPT) != 0;
878 ec->havenocert = cert == NULL;
879 if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
881 if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
883 cont = CMS_dataInit(cms, dcont);
886 r = cms_copy_content(out, cont, flags);
887 do_free_upto(cont, dcont);
891 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
896 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
897 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
901 if (!SMIME_crlf_copy(data, cmsbio, flags)) {
905 (void)BIO_flush(cmsbio);
907 if (!CMS_dataFinal(cms, cmsbio)) {
908 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
915 do_free_upto(cmsbio, dcont);
921 int CMS_final_digest(CMS_ContentInfo *cms,
922 const unsigned char *md, unsigned int mdlen,
923 BIO *dcont, unsigned int flags)
928 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
929 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
933 (void)BIO_flush(cmsbio);
935 if (!ossl_cms_DataFinal(cms, cmsbio, md, mdlen)) {
936 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
942 do_free_upto(cmsbio, dcont);
946 #ifndef OPENSSL_NO_ZLIB
948 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
954 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
955 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
959 if (dcont == NULL && !check_content(cms))
962 cont = CMS_dataInit(cms, dcont);
965 r = cms_copy_content(out, cont, flags);
966 do_free_upto(cont, dcont);
970 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
972 CMS_ContentInfo *cms;
975 comp_nid = NID_zlib_compression;
976 cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL);
980 if (!(flags & CMS_DETACHED))
981 CMS_set_detached(cms, 0);
983 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
986 CMS_ContentInfo_free(cms);
992 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
995 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
999 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
1001 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);