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)
21 DEFINE_STACK_OF(X509_CRL)
22 DEFINE_STACK_OF(CMS_RecipientEncryptedKey)
23 DEFINE_STACK_OF(CMS_RecipientInfo)
25 static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
30 rbio = BIO_new(BIO_s_null());
31 else if (flags & CMS_TEXT) {
32 rbio = BIO_new(BIO_s_mem());
33 BIO_set_mem_eof_return(rbio, 0);
39 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
41 unsigned char buf[4096];
45 tmpout = cms_get_text_bio(out, flags);
48 CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
52 /* Read all content through chain to process digest, decrypt etc */
54 i = BIO_read(in, buf, sizeof(buf));
56 if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
57 if (!BIO_get_cipher_status(in))
65 if (tmpout != NULL && (BIO_write(tmpout, buf, i) != i))
69 if (flags & CMS_TEXT) {
70 if (!SMIME_text(tmpout, out)) {
71 CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR);
84 static int check_content(CMS_ContentInfo *cms)
86 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
88 if (pos == NULL || *pos == NULL) {
89 CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
95 static void do_free_upto(BIO *f, BIO *upto)
104 } while (f != NULL && f != upto);
110 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
115 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
116 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
119 cont = CMS_dataInit(cms, NULL);
122 r = cms_copy_content(out, cont, flags);
127 CMS_ContentInfo *CMS_data_create_with_libctx(BIO *in, unsigned int flags,
131 CMS_ContentInfo *cms = cms_Data_create(libctx, propq);
136 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
139 CMS_ContentInfo_free(cms);
143 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
145 return CMS_data_create_with_libctx(in, flags, NULL, NULL);
148 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
154 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
155 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
159 if (dcont == NULL && !check_content(cms))
162 cont = CMS_dataInit(cms, dcont);
166 r = cms_copy_content(out, cont, flags);
168 r = cms_DigestedData_do_final(cms, cont, 1);
169 do_free_upto(cont, dcont);
173 CMS_ContentInfo *CMS_digest_create_with_libctx(BIO *in,
179 CMS_ContentInfo *cms;
183 cms = cms_DigestedData_create(md, ctx, propq);
187 if (!(flags & CMS_DETACHED))
188 CMS_set_detached(cms, 0);
190 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
193 CMS_ContentInfo_free(cms);
197 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
200 return CMS_digest_create_with_libctx(in, md, flags, NULL, NULL);
203 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
204 const unsigned char *key, size_t keylen,
205 BIO *dcont, BIO *out, unsigned int flags)
210 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
211 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
212 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
216 if (dcont == NULL && !check_content(cms))
219 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
221 cont = CMS_dataInit(cms, dcont);
224 r = cms_copy_content(out, cont, flags);
225 do_free_upto(cont, dcont);
229 CMS_ContentInfo *CMS_EncryptedData_encrypt_with_libctx(BIO *in,
230 const EVP_CIPHER *cipher,
231 const unsigned char *key,
237 CMS_ContentInfo *cms;
239 if (cipher == NULL) {
240 CMSerr(0, CMS_R_NO_CIPHER);
243 cms = CMS_ContentInfo_new_with_libctx(libctx, propq);
246 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
249 if (!(flags & CMS_DETACHED))
250 CMS_set_detached(cms, 0);
252 if ((flags & (CMS_STREAM | CMS_PARTIAL))
253 || CMS_final(cms, in, NULL, flags))
256 CMS_ContentInfo_free(cms);
260 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
261 const unsigned char *key,
262 size_t keylen, unsigned int flags)
264 return CMS_EncryptedData_encrypt_with_libctx(in, cipher, key, keylen, flags,
268 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
270 STACK_OF(X509) *certs,
271 STACK_OF(X509_CRL) *crls,
272 STACK_OF(X509) **chain,
273 const CMS_CTX *cms_ctx)
279 ctx = X509_STORE_CTX_new_with_libctx(cms_ctx->libctx, cms_ctx->propq);
281 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
284 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
285 if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
286 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR);
289 X509_STORE_CTX_set_default(ctx, "smime_sign");
291 X509_STORE_CTX_set0_crls(ctx, crls);
293 i = X509_verify_cert(ctx);
295 j = X509_STORE_CTX_get_error(ctx);
296 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
297 CMS_R_CERTIFICATE_VERIFY_ERROR);
298 ERR_add_error_data(2, "Verify error:",
299 X509_verify_cert_error_string(j));
304 /* also send back the trust chain when required */
306 *chain = X509_STORE_CTX_get1_chain(ctx);
308 X509_STORE_CTX_free(ctx);
313 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
314 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
317 STACK_OF(CMS_SignerInfo) *sinfos;
318 STACK_OF(X509) *cms_certs = NULL;
319 STACK_OF(X509_CRL) *crls = NULL;
320 STACK_OF(X509) **si_chains = NULL;
322 int i, scount = 0, ret = 0;
323 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
324 int cadesVerify = (flags & CMS_CADES) != 0;
325 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
327 if (dcont == NULL && !check_content(cms))
329 if (dcont != NULL && !(flags & CMS_BINARY)) {
330 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
332 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
333 flags |= CMS_ASCIICRLF;
336 /* Attempt to find all signer certificates */
338 sinfos = CMS_get0_SignerInfos(cms);
340 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
341 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
345 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
346 si = sk_CMS_SignerInfo_value(sinfos, i);
347 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
352 if (scount != sk_CMS_SignerInfo_num(sinfos))
353 scount += CMS_set1_signers_certs(cms, certs, flags);
355 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
356 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
360 /* Attempt to verify all signers certs */
361 /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
363 if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
365 /* Certificate trust chain is required to check CAdES signature */
366 si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
367 if (si_chains == NULL) {
368 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
372 cms_certs = CMS_get1_certs(cms);
373 if (!(flags & CMS_NOCRL))
374 crls = CMS_get1_crls(cms);
375 for (i = 0; i < scount; i++) {
376 si = sk_CMS_SignerInfo_value(sinfos, i);
378 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
379 si_chains ? &si_chains[i] : NULL,
385 /* Attempt to verify all SignerInfo signed attribute signatures */
387 if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
388 for (i = 0; i < scount; i++) {
389 si = sk_CMS_SignerInfo_value(sinfos, i);
390 if (CMS_signed_get_attr_count(si) < 0)
392 if (CMS_SignerInfo_verify(si) <= 0)
395 STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
397 if (ess_check_signing_certs(si, si_chain) <= 0)
404 * Performance optimization: if the content is a memory BIO then store
405 * its contents in a temporary read only memory BIO. This avoids
406 * potentially large numbers of slow copies of data which will occur when
407 * reading from a read write memory BIO when signatures are calculated.
410 if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
414 len = BIO_get_mem_data(dcont, &ptr);
415 tmpin = BIO_new_mem_buf(ptr, len);
417 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
424 * If not binary mode and detached generate digests by *writing* through
425 * the BIO. That makes it possible to canonicalise the input.
427 if (!(flags & SMIME_BINARY) && dcont) {
429 * Create output BIO so we can either handle text or to ensure
430 * included content doesn't override detached content.
432 tmpout = cms_get_text_bio(out, flags);
433 if (tmpout == NULL) {
434 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
437 cmsbio = CMS_dataInit(cms, tmpout);
441 * Don't use SMIME_TEXT for verify: it adds headers and we want to
444 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
446 if (flags & CMS_TEXT) {
447 if (!SMIME_text(tmpout, out)) {
448 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR);
453 cmsbio = CMS_dataInit(cms, tmpin);
457 if (!cms_copy_content(out, cmsbio, flags))
461 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
462 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
463 si = sk_CMS_SignerInfo_value(sinfos, i);
464 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
465 CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
473 if (!(flags & SMIME_BINARY) && dcont) {
474 do_free_upto(cmsbio, tmpout);
478 if (dcont && (tmpin == dcont))
479 do_free_upto(cmsbio, dcont);
481 BIO_free_all(cmsbio);
485 BIO_free_all(tmpout);
488 if (si_chains != NULL) {
489 for (i = 0; i < scount; ++i)
490 sk_X509_pop_free(si_chains[i], X509_free);
491 OPENSSL_free(si_chains);
493 sk_X509_pop_free(cms_certs, X509_free);
494 sk_X509_CRL_pop_free(crls, X509_CRL_free);
499 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
500 STACK_OF(X509) *certs,
501 X509_STORE *store, unsigned int flags)
505 flags &= ~(CMS_DETACHED | CMS_TEXT);
506 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
509 return cms_Receipt_verify(rcms, ocms);
512 CMS_ContentInfo *CMS_sign_with_libctx(X509 *signcert, EVP_PKEY *pkey,
513 STACK_OF(X509) *certs, BIO *data,
515 OPENSSL_CTX *libctx, const char *propq)
517 CMS_ContentInfo *cms;
520 cms = CMS_ContentInfo_new_with_libctx(libctx, propq);
521 if (cms == NULL || !CMS_SignedData_init(cms))
523 if (flags & CMS_ASCIICRLF
524 && !CMS_set1_eContentType(cms,
525 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
528 if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
529 CMSerr(0, CMS_R_ADD_SIGNER_ERROR);
533 for (i = 0; i < sk_X509_num(certs); i++) {
534 X509 *x = sk_X509_value(certs, i);
536 if (!CMS_add1_cert(cms, x))
540 if (!(flags & CMS_DETACHED))
541 CMS_set_detached(cms, 0);
543 if ((flags & (CMS_STREAM | CMS_PARTIAL))
544 || CMS_final(cms, data, NULL, flags))
550 CMSerr(0, ERR_R_MALLOC_FAILURE);
553 CMS_ContentInfo_free(cms);
557 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
558 BIO *data, unsigned int flags)
560 return CMS_sign_with_libctx(signcert, pkey, certs, data, flags, NULL, NULL);
563 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
564 X509 *signcert, EVP_PKEY *pkey,
565 STACK_OF(X509) *certs, unsigned int flags)
567 CMS_SignerInfo *rct_si;
568 CMS_ContentInfo *cms = NULL;
569 ASN1_OCTET_STRING **pos, *os;
570 BIO *rct_cont = NULL;
572 const CMS_CTX *ctx = si->cms_ctx;
574 flags &= ~(CMS_STREAM | CMS_TEXT);
575 /* Not really detached but avoids content being allocated */
576 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
577 if (pkey == NULL || signcert == NULL) {
578 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
582 /* Initialize signed data */
584 cms = CMS_sign_with_libctx(NULL, NULL, certs, NULL, flags,
585 ctx->libctx, ctx->propq);
589 /* Set inner content type to signed receipt */
590 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
593 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
595 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
599 os = cms_encode_Receipt(si);
603 /* Set content to digest */
604 rct_cont = BIO_new_mem_buf(os->data, os->length);
605 if (rct_cont == NULL)
608 /* Add msgSigDigest attribute */
610 if (!cms_msgSigDigest_add1(rct_si, si))
613 /* Finalize structure */
614 if (!CMS_final(cms, rct_cont, NULL, flags))
617 /* Set embedded content */
618 pos = CMS_get0_content(cms);
627 CMS_ContentInfo_free(cms);
632 CMS_ContentInfo *CMS_encrypt_with_libctx(STACK_OF(X509) *certs,
633 BIO *data, const EVP_CIPHER *cipher,
635 OPENSSL_CTX *libctx, const char *propq)
637 CMS_ContentInfo *cms;
641 cms = CMS_EnvelopedData_create_with_libctx(cipher, libctx, propq);
644 for (i = 0; i < sk_X509_num(certs); i++) {
645 recip = sk_X509_value(certs, i);
646 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
647 CMSerr(0, CMS_R_RECIPIENT_ERROR);
652 if (!(flags & CMS_DETACHED))
653 CMS_set_detached(cms, 0);
655 if ((flags & (CMS_STREAM | CMS_PARTIAL))
656 || CMS_final(cms, data, NULL, flags))
662 CMSerr(0, ERR_R_MALLOC_FAILURE);
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_with_libctx(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;
708 CMS_RecipientInfo *ri;
709 int i, r, cms_pkey_ri_type;
710 int debug = 0, match_ri = 0;
712 ris = CMS_get0_RecipientInfos(cms);
714 debug = cms->d.envelopedData->encryptedContentInfo->debug;
716 cms_pkey_ri_type = cms_pkey_get_ri_type(pk);
717 if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
718 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER,
719 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
723 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
726 ri = sk_CMS_RecipientInfo_value(ris, i);
727 ri_type = CMS_RecipientInfo_type(ri);
728 if (!cms_pkey_is_ri_type_supported(pk, ri_type))
731 if (ri_type == CMS_RECIPINFO_AGREE) {
732 r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
739 * If we have a cert try matching RecipientInfo otherwise try them
742 else if (cert == NULL|| !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
744 CMS_RecipientInfo_set0_pkey(ri, pk);
745 r = CMS_RecipientInfo_decrypt(cms, ri);
746 CMS_RecipientInfo_set0_pkey(ri, NULL);
749 * If not debugging clear any error and return success to
750 * avoid leaking of information useful to MMA
758 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER, CMS_R_DECRYPT_ERROR);
762 * If no cert and not debugging don't leave loop after first
763 * successful decrypt. Always attempt to decrypt all recipients
764 * to avoid leaking timing of a successful decrypt.
766 else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
770 /* If no cert, key transport and not debugging always return success */
772 && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
779 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER, CMS_R_NO_MATCHING_RECIPIENT);
784 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
785 unsigned char *key, size_t keylen,
786 const unsigned char *id, size_t idlen)
788 STACK_OF(CMS_RecipientInfo) *ris;
789 CMS_RecipientInfo *ri;
792 ris = CMS_get0_RecipientInfos(cms);
793 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
794 ri = sk_CMS_RecipientInfo_value(ris, i);
795 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
799 * If we have an id try matching RecipientInfo otherwise try them
802 if (id == NULL || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
803 CMS_RecipientInfo_set0_key(ri, key, keylen);
804 r = CMS_RecipientInfo_decrypt(cms, ri);
805 CMS_RecipientInfo_set0_key(ri, NULL, 0);
809 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
816 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
821 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
822 unsigned char *pass, ossl_ssize_t passlen)
824 STACK_OF(CMS_RecipientInfo) *ris;
825 CMS_RecipientInfo *ri;
828 ris = CMS_get0_RecipientInfos(cms);
829 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
830 ri = sk_CMS_RecipientInfo_value(ris, i);
831 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
833 CMS_RecipientInfo_set0_password(ri, pass, passlen);
834 r = CMS_RecipientInfo_decrypt(cms, ri);
835 CMS_RecipientInfo_set0_password(ri, NULL, 0);
840 CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
845 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
846 BIO *dcont, BIO *out, unsigned int flags)
851 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
852 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
855 if (dcont == NULL && !check_content(cms))
857 if (flags & CMS_DEBUG_DECRYPT)
858 cms->d.envelopedData->encryptedContentInfo->debug = 1;
860 cms->d.envelopedData->encryptedContentInfo->debug = 0;
862 cms->d.envelopedData->encryptedContentInfo->havenocert = 1;
864 cms->d.envelopedData->encryptedContentInfo->havenocert = 0;
865 if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
867 if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
869 cont = CMS_dataInit(cms, dcont);
872 r = cms_copy_content(out, cont, flags);
873 do_free_upto(cont, dcont);
877 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
882 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
883 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);
887 ret = SMIME_crlf_copy(data, cmsbio, flags);
889 (void)BIO_flush(cmsbio);
891 if (!CMS_dataFinal(cms, cmsbio)) {
892 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
896 do_free_upto(cmsbio, dcont);
904 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
910 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
911 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
915 if (dcont == NULL && !check_content(cms))
918 cont = CMS_dataInit(cms, dcont);
921 r = cms_copy_content(out, cont, flags);
922 do_free_upto(cont, dcont);
926 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
928 CMS_ContentInfo *cms;
931 comp_nid = NID_zlib_compression;
932 cms = cms_CompressedData_create(comp_nid, NULL, NULL);
936 if (!(flags & CMS_DETACHED))
937 CMS_set_detached(cms, 0);
939 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
942 CMS_ContentInfo_free(cms);
948 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
951 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
955 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
957 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);