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))
248 CMS_ContentInfo_free(cms);
252 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
253 const unsigned char *key,
254 size_t keylen, unsigned int flags)
256 return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
260 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
262 STACK_OF(X509) *untrusted,
263 STACK_OF(X509_CRL) *crls,
264 STACK_OF(X509) **chain,
265 const CMS_CTX *cms_ctx)
271 ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx),
272 ossl_cms_ctx_get0_propq(cms_ctx));
274 ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
277 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
278 if (!X509_STORE_CTX_init(ctx, store, signer, untrusted)) {
279 ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR);
282 X509_STORE_CTX_set_default(ctx, "smime_sign");
284 X509_STORE_CTX_set0_crls(ctx, crls);
286 i = X509_verify_cert(ctx);
288 j = X509_STORE_CTX_get_error(ctx);
289 ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR,
290 "Verify error: %s", X509_verify_cert_error_string(j));
295 /* also send back the trust chain when required */
297 *chain = X509_STORE_CTX_get1_chain(ctx);
299 X509_STORE_CTX_free(ctx);
304 /* This strongly overlaps with PKCS7_verify() */
305 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
306 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
309 STACK_OF(CMS_SignerInfo) *sinfos;
310 STACK_OF(X509) *cms_certs = NULL;
311 STACK_OF(X509_CRL) *crls = NULL;
312 STACK_OF(X509) **si_chains = NULL;
314 int i, scount = 0, ret = 0;
315 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
316 int cadesVerify = (flags & CMS_CADES) != 0;
317 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
319 if (dcont == NULL && !check_content(cms))
321 if (dcont != NULL && !(flags & CMS_BINARY)) {
322 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
324 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
325 flags |= CMS_ASCIICRLF;
328 /* Attempt to find all signer certificates */
330 sinfos = CMS_get0_SignerInfos(cms);
332 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
333 ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
337 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
338 si = sk_CMS_SignerInfo_value(sinfos, i);
339 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
344 if (scount != sk_CMS_SignerInfo_num(sinfos))
345 scount += CMS_set1_signers_certs(cms, certs, flags);
347 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
348 ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
352 /* Attempt to verify all signers certs */
353 /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
355 if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
357 /* Certificate trust chain is required to check CAdES signature */
358 si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
359 if (si_chains == NULL)
362 cms_certs = CMS_get1_certs(cms);
363 if (!(flags & CMS_NOCRL))
364 crls = CMS_get1_crls(cms);
365 for (i = 0; i < scount; i++) {
366 si = sk_CMS_SignerInfo_value(sinfos, i);
368 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
369 si_chains ? &si_chains[i] : NULL,
375 /* Attempt to verify all SignerInfo signed attribute signatures */
377 if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
378 for (i = 0; i < scount; i++) {
379 si = sk_CMS_SignerInfo_value(sinfos, i);
380 if (CMS_signed_get_attr_count(si) < 0)
382 if (CMS_SignerInfo_verify(si) <= 0)
385 STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
387 if (ossl_cms_check_signing_certs(si, si_chain) <= 0)
394 * Performance optimization: if the content is a memory BIO then store
395 * its contents in a temporary read only memory BIO. This avoids
396 * potentially large numbers of slow copies of data which will occur when
397 * reading from a read write memory BIO when signatures are calculated.
400 if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
404 len = BIO_get_mem_data(dcont, &ptr);
405 tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
407 ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB);
414 * If not binary mode and detached generate digests by *writing* through
415 * the BIO. That makes it possible to canonicalise the input.
417 if (!(flags & SMIME_BINARY) && dcont) {
419 * Create output BIO so we can either handle text or to ensure
420 * included content doesn't override detached content.
422 tmpout = cms_get_text_bio(out, flags);
423 if (tmpout == NULL) {
424 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
427 cmsbio = CMS_dataInit(cms, tmpout);
431 * Don't use SMIME_TEXT for verify: it adds headers and we want to
434 if (!SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT))
437 if (flags & CMS_TEXT) {
438 if (!SMIME_text(tmpout, out)) {
439 ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
444 cmsbio = CMS_dataInit(cms, tmpin);
448 if (!cms_copy_content(out, cmsbio, flags))
452 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
453 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
454 si = sk_CMS_SignerInfo_value(sinfos, i);
455 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
456 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR);
464 if (!(flags & SMIME_BINARY) && dcont) {
465 do_free_upto(cmsbio, tmpout);
469 if (dcont && (tmpin == dcont))
470 do_free_upto(cmsbio, dcont);
472 BIO_free_all(cmsbio);
476 BIO_free_all(tmpout);
479 if (si_chains != NULL) {
480 for (i = 0; i < scount; ++i)
481 OSSL_STACK_OF_X509_free(si_chains[i]);
482 OPENSSL_free(si_chains);
484 OSSL_STACK_OF_X509_free(cms_certs);
485 sk_X509_CRL_pop_free(crls, X509_CRL_free);
490 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
491 STACK_OF(X509) *certs,
492 X509_STORE *store, unsigned int flags)
496 flags &= ~(CMS_DETACHED | CMS_TEXT);
497 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
500 return ossl_cms_Receipt_verify(rcms, ocms);
503 CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
504 STACK_OF(X509) *certs, BIO *data,
505 unsigned int flags, OSSL_LIB_CTX *libctx,
508 CMS_ContentInfo *cms;
511 cms = CMS_ContentInfo_new_ex(libctx, propq);
512 if (cms == NULL || !CMS_SignedData_init(cms)) {
513 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
516 if (flags & CMS_ASCIICRLF
517 && !CMS_set1_eContentType(cms,
518 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF))) {
519 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
523 if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
524 ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
528 for (i = 0; i < sk_X509_num(certs); i++) {
529 X509 *x = sk_X509_value(certs, i);
531 if (!CMS_add1_cert(cms, x)) {
532 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
537 if (!(flags & CMS_DETACHED))
538 CMS_set_detached(cms, 0);
540 if ((flags & (CMS_STREAM | CMS_PARTIAL))
541 || CMS_final(cms, data, NULL, flags))
547 CMS_ContentInfo_free(cms);
551 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
552 BIO *data, unsigned int flags)
554 return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
557 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
558 X509 *signcert, EVP_PKEY *pkey,
559 STACK_OF(X509) *certs, unsigned int flags)
561 CMS_SignerInfo *rct_si;
562 CMS_ContentInfo *cms = NULL;
563 ASN1_OCTET_STRING **pos, *os = NULL;
564 BIO *rct_cont = NULL;
566 const CMS_CTX *ctx = si->cms_ctx;
568 flags &= ~(CMS_STREAM | CMS_TEXT);
569 /* Not really detached but avoids content being allocated */
570 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
571 if (pkey == NULL || signcert == NULL) {
572 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
576 /* Initialize signed data */
578 cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags,
579 ossl_cms_ctx_get0_libctx(ctx),
580 ossl_cms_ctx_get0_propq(ctx));
584 /* Set inner content type to signed receipt */
585 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
588 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
590 ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
594 os = ossl_cms_encode_Receipt(si);
598 /* Set content to digest */
599 rct_cont = BIO_new_mem_buf(os->data, os->length);
600 if (rct_cont == NULL)
603 /* Add msgSigDigest attribute */
605 if (!ossl_cms_msgSigDigest_add1(rct_si, si))
608 /* Finalize structure */
609 if (!CMS_final(cms, rct_cont, NULL, flags))
612 /* Set embedded content */
613 pos = CMS_get0_content(cms);
624 CMS_ContentInfo_free(cms);
625 ASN1_OCTET_STRING_free(os);
630 CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
631 const EVP_CIPHER *cipher, unsigned int flags,
632 OSSL_LIB_CTX *libctx, const char *propq)
634 CMS_ContentInfo *cms;
639 cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
640 ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
641 : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
643 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
646 for (i = 0; i < sk_X509_num(certs); i++) {
647 recip = sk_X509_value(certs, i);
648 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
649 ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
654 if (!(flags & CMS_DETACHED))
655 CMS_set_detached(cms, 0);
657 if ((flags & (CMS_STREAM | CMS_PARTIAL))
658 || CMS_final(cms, data, NULL, flags))
661 ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
664 CMS_ContentInfo_free(cms);
668 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
669 const EVP_CIPHER *cipher, unsigned int flags)
671 return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
674 static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
675 CMS_RecipientInfo *ri,
676 EVP_PKEY *pk, X509 *cert, X509 *peer)
679 STACK_OF(CMS_RecipientEncryptedKey) *reks;
680 CMS_RecipientEncryptedKey *rek;
682 reks = CMS_RecipientInfo_kari_get0_reks(ri);
683 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
686 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
687 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
689 CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
690 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
691 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
694 return cert == NULL ? 0 : -1;
699 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
701 return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
704 int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
705 X509 *cert, X509 *peer)
707 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
708 CMS_RecipientInfo *ri;
709 int i, r, cms_pkey_ri_type;
710 int debug = 0, match_ri = 0;
711 CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
713 /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
715 OPENSSL_clear_free(ec->key, ec->keylen);
720 if (ris != NULL && ec != NULL)
723 cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk);
724 if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
725 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
729 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
732 ri = sk_CMS_RecipientInfo_value(ris, i);
733 ri_type = CMS_RecipientInfo_type(ri);
734 if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type))
737 if (ri_type == CMS_RECIPINFO_AGREE) {
738 r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
744 /* If we have a cert, try matching RecipientInfo, else try them all */
745 else if (cert == NULL || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
747 CMS_RecipientInfo_set0_pkey(ri, pk);
748 r = CMS_RecipientInfo_decrypt(cms, ri);
749 CMS_RecipientInfo_set0_pkey(ri, NULL);
752 * If not debugging clear any error and return success to
753 * avoid leaking of information useful to MMA
761 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
765 * If no cert and not debugging don't leave loop after first
766 * successful decrypt. Always attempt to decrypt all recipients
767 * to avoid leaking timing of a successful decrypt.
769 else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
773 /* If no cert, key transport and not debugging always return success */
775 && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
783 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
788 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
789 unsigned char *key, size_t keylen,
790 const unsigned char *id, size_t idlen)
792 STACK_OF(CMS_RecipientInfo) *ris;
793 CMS_RecipientInfo *ri;
794 int i, r, match_ri = 0;
796 ris = CMS_get0_RecipientInfos(cms);
797 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
798 ri = sk_CMS_RecipientInfo_value(ris, i);
799 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
802 /* If we have an id, try matching RecipientInfo, else try them all */
804 || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
806 CMS_RecipientInfo_set0_key(ri, key, keylen);
807 r = CMS_RecipientInfo_decrypt(cms, ri);
808 CMS_RecipientInfo_set0_key(ri, NULL, 0);
812 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
820 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
825 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
826 unsigned char *pass, ossl_ssize_t passlen)
828 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
829 CMS_RecipientInfo *ri;
830 int i, r, match_ri = 0;
831 CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
833 /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
835 OPENSSL_clear_free(ec->key, ec->keylen);
840 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
841 ri = sk_CMS_RecipientInfo_value(ris, i);
842 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
845 /* Must try each PasswordRecipientInfo */
847 CMS_RecipientInfo_set0_password(ri, pass, passlen);
848 r = CMS_RecipientInfo_decrypt(cms, ri);
849 CMS_RecipientInfo_set0_password(ri, NULL, 0);
855 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
860 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
861 BIO *dcont, BIO *out, unsigned int flags)
865 CMS_EncryptedContentInfo *ec;
866 int nid = OBJ_obj2nid(CMS_get0_type(cms));
868 if (nid != NID_pkcs7_enveloped
869 && nid != NID_id_smime_ct_authEnvelopedData) {
870 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA);
873 if (dcont == NULL && !check_content(cms))
875 ec = ossl_cms_get0_env_enc_content(cms);
876 ec->debug = (flags & CMS_DEBUG_DECRYPT) != 0;
877 ec->havenocert = cert == NULL;
878 if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
880 if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
882 cont = CMS_dataInit(cms, dcont);
885 r = cms_copy_content(out, cont, flags);
886 do_free_upto(cont, dcont);
890 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
895 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
896 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
900 if (!SMIME_crlf_copy(data, cmsbio, flags)) {
904 (void)BIO_flush(cmsbio);
906 if (!CMS_dataFinal(cms, cmsbio)) {
907 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
914 do_free_upto(cmsbio, dcont);
920 int CMS_final_digest(CMS_ContentInfo *cms,
921 const unsigned char *md, unsigned int mdlen,
922 BIO *dcont, unsigned int flags)
927 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
928 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
932 (void)BIO_flush(cmsbio);
934 if (!ossl_cms_DataFinal(cms, cmsbio, md, mdlen)) {
935 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
941 do_free_upto(cmsbio, dcont);
945 #ifndef OPENSSL_NO_ZLIB
947 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
953 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
954 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
958 if (dcont == NULL && !check_content(cms))
961 cont = CMS_dataInit(cms, dcont);
964 r = cms_copy_content(out, cont, flags);
965 do_free_upto(cont, dcont);
969 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
971 CMS_ContentInfo *cms;
974 comp_nid = NID_zlib_compression;
975 cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL);
979 if (!(flags & CMS_DETACHED))
980 CMS_set_detached(cms, 0);
982 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
985 CMS_ContentInfo_free(cms);
991 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
994 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
998 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
1000 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);