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/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 DEFINE_STACK_OF(CMS_SignerInfo)
20 DEFINE_STACK_OF(CMS_RecipientEncryptedKey)
21 DEFINE_STACK_OF(CMS_RecipientInfo)
23 static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
28 rbio = BIO_new(BIO_s_null());
29 else if (flags & CMS_TEXT) {
30 rbio = BIO_new(BIO_s_mem());
31 BIO_set_mem_eof_return(rbio, 0);
37 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
39 unsigned char buf[4096];
43 tmpout = cms_get_text_bio(out, flags);
46 CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
50 /* Read all content through chain to process digest, decrypt etc */
52 i = BIO_read(in, buf, sizeof(buf));
54 if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
55 if (!BIO_get_cipher_status(in))
63 if (tmpout != NULL && (BIO_write(tmpout, buf, i) != i))
67 if (flags & CMS_TEXT) {
68 if (!SMIME_text(tmpout, out)) {
69 CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR);
82 static int check_content(CMS_ContentInfo *cms)
84 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
86 if (pos == NULL || *pos == NULL) {
87 CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
93 static void do_free_upto(BIO *f, BIO *upto)
102 } while (f != NULL && f != upto);
108 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
113 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
114 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
117 cont = CMS_dataInit(cms, NULL);
120 r = cms_copy_content(out, cont, flags);
125 CMS_ContentInfo *CMS_data_create_with_libctx(BIO *in, unsigned int flags,
129 CMS_ContentInfo *cms = cms_Data_create(libctx, propq);
134 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
137 CMS_ContentInfo_free(cms);
141 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
143 return CMS_data_create_with_libctx(in, flags, NULL, NULL);
146 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
152 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
153 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
157 if (dcont == NULL && !check_content(cms))
160 cont = CMS_dataInit(cms, dcont);
164 r = cms_copy_content(out, cont, flags);
166 r = cms_DigestedData_do_final(cms, cont, 1);
167 do_free_upto(cont, dcont);
171 CMS_ContentInfo *CMS_digest_create_with_libctx(BIO *in,
177 CMS_ContentInfo *cms;
181 cms = cms_DigestedData_create(md, ctx, propq);
185 if (!(flags & CMS_DETACHED))
186 CMS_set_detached(cms, 0);
188 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
191 CMS_ContentInfo_free(cms);
195 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
198 return CMS_digest_create_with_libctx(in, md, flags, NULL, NULL);
201 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
202 const unsigned char *key, size_t keylen,
203 BIO *dcont, BIO *out, unsigned int flags)
208 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
209 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
210 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
214 if (dcont == NULL && !check_content(cms))
217 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
219 cont = CMS_dataInit(cms, dcont);
222 r = cms_copy_content(out, cont, flags);
223 do_free_upto(cont, dcont);
227 CMS_ContentInfo *CMS_EncryptedData_encrypt_with_libctx(BIO *in,
228 const EVP_CIPHER *cipher,
229 const unsigned char *key,
235 CMS_ContentInfo *cms;
237 if (cipher == NULL) {
238 CMSerr(0, CMS_R_NO_CIPHER);
241 cms = CMS_ContentInfo_new_with_libctx(libctx, propq);
244 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
247 if (!(flags & CMS_DETACHED))
248 CMS_set_detached(cms, 0);
250 if ((flags & (CMS_STREAM | CMS_PARTIAL))
251 || CMS_final(cms, in, NULL, flags))
254 CMS_ContentInfo_free(cms);
258 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
259 const unsigned char *key,
260 size_t keylen, unsigned int flags)
262 return CMS_EncryptedData_encrypt_with_libctx(in, cipher, key, keylen, flags,
266 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
268 STACK_OF(X509) *certs,
269 STACK_OF(X509_CRL) *crls,
270 STACK_OF(X509) **chain,
271 const CMS_CTX *cms_ctx)
277 ctx = X509_STORE_CTX_new_with_libctx(cms_ctx->libctx, cms_ctx->propq);
279 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
282 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
283 if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
284 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR);
287 X509_STORE_CTX_set_default(ctx, "smime_sign");
289 X509_STORE_CTX_set0_crls(ctx, crls);
291 i = X509_verify_cert(ctx);
293 j = X509_STORE_CTX_get_error(ctx);
294 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
295 CMS_R_CERTIFICATE_VERIFY_ERROR);
296 ERR_add_error_data(2, "Verify error:",
297 X509_verify_cert_error_string(j));
302 /* also send back the trust chain when required */
304 *chain = X509_STORE_CTX_get1_chain(ctx);
306 X509_STORE_CTX_free(ctx);
311 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
312 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
315 STACK_OF(CMS_SignerInfo) *sinfos;
316 STACK_OF(X509) *cms_certs = NULL;
317 STACK_OF(X509_CRL) *crls = NULL;
318 STACK_OF(X509) **si_chains = NULL;
320 int i, scount = 0, ret = 0;
321 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
322 int cadesVerify = (flags & CMS_CADES) != 0;
323 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
325 if (dcont == NULL && !check_content(cms))
327 if (dcont != NULL && !(flags & CMS_BINARY)) {
328 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
330 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
331 flags |= CMS_ASCIICRLF;
334 /* Attempt to find all signer certificates */
336 sinfos = CMS_get0_SignerInfos(cms);
338 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
339 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
343 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
344 si = sk_CMS_SignerInfo_value(sinfos, i);
345 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
350 if (scount != sk_CMS_SignerInfo_num(sinfos))
351 scount += CMS_set1_signers_certs(cms, certs, flags);
353 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
354 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
358 /* Attempt to verify all signers certs */
359 /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
361 if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
363 /* Certificate trust chain is required to check CAdES signature */
364 si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
365 if (si_chains == NULL) {
366 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
370 cms_certs = CMS_get1_certs(cms);
371 if (!(flags & CMS_NOCRL))
372 crls = CMS_get1_crls(cms);
373 for (i = 0; i < scount; i++) {
374 si = sk_CMS_SignerInfo_value(sinfos, i);
376 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
377 si_chains ? &si_chains[i] : NULL,
383 /* Attempt to verify all SignerInfo signed attribute signatures */
385 if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
386 for (i = 0; i < scount; i++) {
387 si = sk_CMS_SignerInfo_value(sinfos, i);
388 if (CMS_signed_get_attr_count(si) < 0)
390 if (CMS_SignerInfo_verify(si) <= 0)
393 STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
395 if (ess_check_signing_certs(si, si_chain) <= 0)
402 * Performance optimization: if the content is a memory BIO then store
403 * its contents in a temporary read only memory BIO. This avoids
404 * potentially large numbers of slow copies of data which will occur when
405 * reading from a read write memory BIO when signatures are calculated.
408 if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
412 len = BIO_get_mem_data(dcont, &ptr);
413 tmpin = BIO_new_mem_buf(ptr, len);
415 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
422 * If not binary mode and detached generate digests by *writing* through
423 * the BIO. That makes it possible to canonicalise the input.
425 if (!(flags & SMIME_BINARY) && dcont) {
427 * Create output BIO so we can either handle text or to ensure
428 * included content doesn't override detached content.
430 tmpout = cms_get_text_bio(out, flags);
431 if (tmpout == NULL) {
432 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
435 cmsbio = CMS_dataInit(cms, tmpout);
439 * Don't use SMIME_TEXT for verify: it adds headers and we want to
442 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
444 if (flags & CMS_TEXT) {
445 if (!SMIME_text(tmpout, out)) {
446 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR);
451 cmsbio = CMS_dataInit(cms, tmpin);
455 if (!cms_copy_content(out, cmsbio, flags))
459 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
460 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
461 si = sk_CMS_SignerInfo_value(sinfos, i);
462 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
463 CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
471 if (!(flags & SMIME_BINARY) && dcont) {
472 do_free_upto(cmsbio, tmpout);
476 if (dcont && (tmpin == dcont))
477 do_free_upto(cmsbio, dcont);
479 BIO_free_all(cmsbio);
483 BIO_free_all(tmpout);
486 if (si_chains != NULL) {
487 for (i = 0; i < scount; ++i)
488 sk_X509_pop_free(si_chains[i], X509_free);
489 OPENSSL_free(si_chains);
491 sk_X509_pop_free(cms_certs, X509_free);
492 sk_X509_CRL_pop_free(crls, X509_CRL_free);
497 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
498 STACK_OF(X509) *certs,
499 X509_STORE *store, unsigned int flags)
503 flags &= ~(CMS_DETACHED | CMS_TEXT);
504 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
507 return cms_Receipt_verify(rcms, ocms);
510 CMS_ContentInfo *CMS_sign_with_libctx(X509 *signcert, EVP_PKEY *pkey,
511 STACK_OF(X509) *certs, BIO *data,
513 OPENSSL_CTX *libctx, const char *propq)
515 CMS_ContentInfo *cms;
518 cms = CMS_ContentInfo_new_with_libctx(libctx, propq);
519 if (cms == NULL || !CMS_SignedData_init(cms))
521 if (flags & CMS_ASCIICRLF
522 && !CMS_set1_eContentType(cms,
523 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
526 if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
527 CMSerr(0, CMS_R_ADD_SIGNER_ERROR);
531 for (i = 0; i < sk_X509_num(certs); i++) {
532 X509 *x = sk_X509_value(certs, i);
534 if (!CMS_add1_cert(cms, x))
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 CMSerr(0, ERR_R_MALLOC_FAILURE);
551 CMS_ContentInfo_free(cms);
555 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
556 BIO *data, unsigned int flags)
558 return CMS_sign_with_libctx(signcert, pkey, certs, data, flags, NULL, NULL);
561 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
562 X509 *signcert, EVP_PKEY *pkey,
563 STACK_OF(X509) *certs, unsigned int flags)
565 CMS_SignerInfo *rct_si;
566 CMS_ContentInfo *cms = NULL;
567 ASN1_OCTET_STRING **pos, *os;
568 BIO *rct_cont = NULL;
570 const CMS_CTX *ctx = si->cms_ctx;
572 flags &= ~(CMS_STREAM | CMS_TEXT);
573 /* Not really detached but avoids content being allocated */
574 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
575 if (pkey == NULL || signcert == NULL) {
576 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
580 /* Initialize signed data */
582 cms = CMS_sign_with_libctx(NULL, NULL, certs, NULL, flags,
583 ctx->libctx, ctx->propq);
587 /* Set inner content type to signed receipt */
588 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
591 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
593 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
597 os = cms_encode_Receipt(si);
601 /* Set content to digest */
602 rct_cont = BIO_new_mem_buf(os->data, os->length);
603 if (rct_cont == NULL)
606 /* Add msgSigDigest attribute */
608 if (!cms_msgSigDigest_add1(rct_si, si))
611 /* Finalize structure */
612 if (!CMS_final(cms, rct_cont, NULL, flags))
615 /* Set embedded content */
616 pos = CMS_get0_content(cms);
625 CMS_ContentInfo_free(cms);
630 CMS_ContentInfo *CMS_encrypt_with_libctx(STACK_OF(X509) *certs,
631 BIO *data, const EVP_CIPHER *cipher,
633 OPENSSL_CTX *libctx, const char *propq)
635 CMS_ContentInfo *cms;
640 cms = (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
641 ? CMS_AuthEnvelopedData_create_with_libctx(cipher, libctx, propq)
642 : CMS_EnvelopedData_create_with_libctx(cipher, libctx, propq);
645 for (i = 0; i < sk_X509_num(certs); i++) {
646 recip = sk_X509_value(certs, i);
647 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
648 CMSerr(0, CMS_R_RECIPIENT_ERROR);
653 if (!(flags & CMS_DETACHED))
654 CMS_set_detached(cms, 0);
656 if ((flags & (CMS_STREAM | CMS_PARTIAL))
657 || CMS_final(cms, data, NULL, flags))
663 CMSerr(0, ERR_R_MALLOC_FAILURE);
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_with_libctx(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;
709 CMS_RecipientInfo *ri;
710 int i, r, cms_pkey_ri_type;
711 int debug = 0, match_ri = 0;
713 ris = CMS_get0_RecipientInfos(cms);
715 debug = cms_get0_env_enc_content(cms)->debug;
717 cms_pkey_ri_type = cms_pkey_get_ri_type(pk);
718 if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
719 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER,
720 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
724 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
727 ri = sk_CMS_RecipientInfo_value(ris, i);
728 ri_type = CMS_RecipientInfo_type(ri);
729 if (!cms_pkey_is_ri_type_supported(pk, ri_type))
732 if (ri_type == CMS_RECIPINFO_AGREE) {
733 r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
740 * If we have a cert try matching RecipientInfo otherwise try them
743 else if (cert == NULL|| !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
745 CMS_RecipientInfo_set0_pkey(ri, pk);
746 r = CMS_RecipientInfo_decrypt(cms, ri);
747 CMS_RecipientInfo_set0_pkey(ri, NULL);
750 * If not debugging clear any error and return success to
751 * avoid leaking of information useful to MMA
759 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER, CMS_R_DECRYPT_ERROR);
763 * If no cert and not debugging don't leave loop after first
764 * successful decrypt. Always attempt to decrypt all recipients
765 * to avoid leaking timing of a successful decrypt.
767 else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
771 /* If no cert, key transport and not debugging always return success */
773 && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
780 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER, CMS_R_NO_MATCHING_RECIPIENT);
785 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
786 unsigned char *key, size_t keylen,
787 const unsigned char *id, size_t idlen)
789 STACK_OF(CMS_RecipientInfo) *ris;
790 CMS_RecipientInfo *ri;
793 ris = CMS_get0_RecipientInfos(cms);
794 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
795 ri = sk_CMS_RecipientInfo_value(ris, i);
796 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
800 * If we have an id try matching RecipientInfo otherwise try them
803 if (id == NULL || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
804 CMS_RecipientInfo_set0_key(ri, key, keylen);
805 r = CMS_RecipientInfo_decrypt(cms, ri);
806 CMS_RecipientInfo_set0_key(ri, NULL, 0);
810 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
817 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
822 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
823 unsigned char *pass, ossl_ssize_t passlen)
825 STACK_OF(CMS_RecipientInfo) *ris;
826 CMS_RecipientInfo *ri;
829 ris = CMS_get0_RecipientInfos(cms);
830 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
831 ri = sk_CMS_RecipientInfo_value(ris, i);
832 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
834 CMS_RecipientInfo_set0_password(ri, pass, passlen);
835 r = CMS_RecipientInfo_decrypt(cms, ri);
836 CMS_RecipientInfo_set0_password(ri, NULL, 0);
841 CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
846 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
847 BIO *dcont, BIO *out, unsigned int flags)
852 int nid = OBJ_obj2nid(CMS_get0_type(cms));
854 if (nid != NID_pkcs7_enveloped
855 && nid != NID_id_smime_ct_authEnvelopedData) {
856 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
859 if (dcont == NULL && !check_content(cms))
861 if (flags & CMS_DEBUG_DECRYPT)
862 cms_get0_env_enc_content(cms)->debug = 1;
864 cms_get0_env_enc_content(cms)->debug = 0;
866 cms_get0_env_enc_content(cms)->havenocert = 1;
868 cms_get0_env_enc_content(cms)->havenocert = 0;
869 if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
871 if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
873 cont = CMS_dataInit(cms, dcont);
876 r = cms_copy_content(out, cont, flags);
877 do_free_upto(cont, dcont);
881 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
886 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
887 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);
891 ret = SMIME_crlf_copy(data, cmsbio, flags);
893 (void)BIO_flush(cmsbio);
895 if (!CMS_dataFinal(cms, cmsbio)) {
896 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
900 do_free_upto(cmsbio, dcont);
908 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
914 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
915 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
919 if (dcont == NULL && !check_content(cms))
922 cont = CMS_dataInit(cms, dcont);
925 r = cms_copy_content(out, cont, flags);
926 do_free_upto(cont, dcont);
930 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
932 CMS_ContentInfo *cms;
935 comp_nid = NID_zlib_compression;
936 cms = cms_CompressedData_create(comp_nid, NULL, NULL);
940 if (!(flags & CMS_DETACHED))
941 CMS_set_detached(cms, 0);
943 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
946 CMS_ContentInfo_free(cms);
952 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
955 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
959 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
961 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);