2 * Copyright 2008-2021 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_MALLOC_FAILURE);
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))
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 = 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 = 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;
174 cms = cms_DigestedData_create(md, ctx, propq);
178 if (!(flags & CMS_DETACHED))
179 CMS_set_detached(cms, 0);
181 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
184 CMS_ContentInfo_free(cms);
188 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
191 return CMS_digest_create_ex(in, md, flags, NULL, NULL);
194 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
195 const unsigned char *key, size_t keylen,
196 BIO *dcont, BIO *out, unsigned int flags)
201 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
202 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENCRYPTED_DATA);
206 if (dcont == NULL && !check_content(cms))
209 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
211 cont = CMS_dataInit(cms, dcont);
214 r = cms_copy_content(out, cont, flags);
215 do_free_upto(cont, dcont);
219 CMS_ContentInfo *CMS_EncryptedData_encrypt_ex(BIO *in, const EVP_CIPHER *cipher,
220 const unsigned char *key,
221 size_t keylen, unsigned int flags,
222 OSSL_LIB_CTX *libctx,
225 CMS_ContentInfo *cms;
227 if (cipher == NULL) {
228 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER);
231 cms = CMS_ContentInfo_new_ex(libctx, propq);
234 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
237 if (!(flags & CMS_DETACHED))
238 CMS_set_detached(cms, 0);
240 if ((flags & (CMS_STREAM | CMS_PARTIAL))
241 || CMS_final(cms, in, NULL, flags))
244 CMS_ContentInfo_free(cms);
248 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
249 const unsigned char *key,
250 size_t keylen, unsigned int flags)
252 return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
256 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
258 STACK_OF(X509) *certs,
259 STACK_OF(X509_CRL) *crls,
260 STACK_OF(X509) **chain,
261 const CMS_CTX *cms_ctx)
267 ctx = X509_STORE_CTX_new_ex(cms_ctx_get0_libctx(cms_ctx),
268 cms_ctx_get0_propq(cms_ctx));
270 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
273 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
274 if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
275 ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR);
278 X509_STORE_CTX_set_default(ctx, "smime_sign");
280 X509_STORE_CTX_set0_crls(ctx, crls);
282 i = X509_verify_cert(ctx);
284 j = X509_STORE_CTX_get_error(ctx);
285 ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR,
286 "Verify error: %s", X509_verify_cert_error_string(j));
291 /* also send back the trust chain when required */
293 *chain = X509_STORE_CTX_get1_chain(ctx);
295 X509_STORE_CTX_free(ctx);
300 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
301 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
304 STACK_OF(CMS_SignerInfo) *sinfos;
305 STACK_OF(X509) *cms_certs = NULL;
306 STACK_OF(X509_CRL) *crls = NULL;
307 STACK_OF(X509) **si_chains = NULL;
309 int i, scount = 0, ret = 0;
310 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
311 int cadesVerify = (flags & CMS_CADES) != 0;
312 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
314 if (dcont == NULL && !check_content(cms))
316 if (dcont != NULL && !(flags & CMS_BINARY)) {
317 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
319 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
320 flags |= CMS_ASCIICRLF;
323 /* Attempt to find all signer certificates */
325 sinfos = CMS_get0_SignerInfos(cms);
327 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
328 ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
332 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
333 si = sk_CMS_SignerInfo_value(sinfos, i);
334 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
339 if (scount != sk_CMS_SignerInfo_num(sinfos))
340 scount += CMS_set1_signers_certs(cms, certs, flags);
342 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
343 ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
347 /* Attempt to verify all signers certs */
348 /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
350 if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
352 /* Certificate trust chain is required to check CAdES signature */
353 si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
354 if (si_chains == NULL) {
355 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
359 cms_certs = CMS_get1_certs(cms);
360 if (!(flags & CMS_NOCRL))
361 crls = CMS_get1_crls(cms);
362 for (i = 0; i < scount; i++) {
363 si = sk_CMS_SignerInfo_value(sinfos, i);
365 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
366 si_chains ? &si_chains[i] : NULL,
372 /* Attempt to verify all SignerInfo signed attribute signatures */
374 if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
375 for (i = 0; i < scount; i++) {
376 si = sk_CMS_SignerInfo_value(sinfos, i);
377 if (CMS_signed_get_attr_count(si) < 0)
379 if (CMS_SignerInfo_verify(si) <= 0)
382 STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
384 if (ess_check_signing_certs(si, si_chain) <= 0)
391 * Performance optimization: if the content is a memory BIO then store
392 * its contents in a temporary read only memory BIO. This avoids
393 * potentially large numbers of slow copies of data which will occur when
394 * reading from a read write memory BIO when signatures are calculated.
397 if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
401 len = BIO_get_mem_data(dcont, &ptr);
402 tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
404 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
411 * If not binary mode and detached generate digests by *writing* through
412 * the BIO. That makes it possible to canonicalise the input.
414 if (!(flags & SMIME_BINARY) && dcont) {
416 * Create output BIO so we can either handle text or to ensure
417 * included content doesn't override detached content.
419 tmpout = cms_get_text_bio(out, flags);
420 if (tmpout == NULL) {
421 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
424 cmsbio = CMS_dataInit(cms, tmpout);
428 * Don't use SMIME_TEXT for verify: it adds headers and we want to
431 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
433 if (flags & CMS_TEXT) {
434 if (!SMIME_text(tmpout, out)) {
435 ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
440 cmsbio = CMS_dataInit(cms, tmpin);
444 if (!cms_copy_content(out, cmsbio, flags))
448 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
449 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
450 si = sk_CMS_SignerInfo_value(sinfos, i);
451 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
452 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR);
460 if (!(flags & SMIME_BINARY) && dcont) {
461 do_free_upto(cmsbio, tmpout);
465 if (dcont && (tmpin == dcont))
466 do_free_upto(cmsbio, dcont);
468 BIO_free_all(cmsbio);
472 BIO_free_all(tmpout);
475 if (si_chains != NULL) {
476 for (i = 0; i < scount; ++i)
477 sk_X509_pop_free(si_chains[i], X509_free);
478 OPENSSL_free(si_chains);
480 sk_X509_pop_free(cms_certs, X509_free);
481 sk_X509_CRL_pop_free(crls, X509_CRL_free);
486 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
487 STACK_OF(X509) *certs,
488 X509_STORE *store, unsigned int flags)
492 flags &= ~(CMS_DETACHED | CMS_TEXT);
493 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
496 return cms_Receipt_verify(rcms, ocms);
499 CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
500 STACK_OF(X509) *certs, BIO *data,
501 unsigned int flags, OSSL_LIB_CTX *libctx,
504 CMS_ContentInfo *cms;
507 cms = CMS_ContentInfo_new_ex(libctx, propq);
508 if (cms == NULL || !CMS_SignedData_init(cms))
510 if (flags & CMS_ASCIICRLF
511 && !CMS_set1_eContentType(cms,
512 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
515 if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
516 ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
520 for (i = 0; i < sk_X509_num(certs); i++) {
521 X509 *x = sk_X509_value(certs, i);
523 if (!CMS_add1_cert(cms, x))
527 if (!(flags & CMS_DETACHED))
528 CMS_set_detached(cms, 0);
530 if ((flags & (CMS_STREAM | CMS_PARTIAL))
531 || CMS_final(cms, data, NULL, flags))
537 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
540 CMS_ContentInfo_free(cms);
544 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
545 BIO *data, unsigned int flags)
547 return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
550 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
551 X509 *signcert, EVP_PKEY *pkey,
552 STACK_OF(X509) *certs, unsigned int flags)
554 CMS_SignerInfo *rct_si;
555 CMS_ContentInfo *cms = NULL;
556 ASN1_OCTET_STRING **pos, *os;
557 BIO *rct_cont = NULL;
559 const CMS_CTX *ctx = si->cms_ctx;
561 flags &= ~(CMS_STREAM | CMS_TEXT);
562 /* Not really detached but avoids content being allocated */
563 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
564 if (pkey == NULL || signcert == NULL) {
565 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
569 /* Initialize signed data */
571 cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags, cms_ctx_get0_libctx(ctx),
572 cms_ctx_get0_propq(ctx));
576 /* Set inner content type to signed receipt */
577 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
580 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
582 ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
586 os = cms_encode_Receipt(si);
590 /* Set content to digest */
591 rct_cont = BIO_new_mem_buf(os->data, os->length);
592 if (rct_cont == NULL)
595 /* Add msgSigDigest attribute */
597 if (!cms_msgSigDigest_add1(rct_si, si))
600 /* Finalize structure */
601 if (!CMS_final(cms, rct_cont, NULL, flags))
604 /* Set embedded content */
605 pos = CMS_get0_content(cms);
614 CMS_ContentInfo_free(cms);
619 CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
620 const EVP_CIPHER *cipher, unsigned int flags,
621 OSSL_LIB_CTX *libctx, const char *propq)
623 CMS_ContentInfo *cms;
628 cms = (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
629 ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
630 : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
633 for (i = 0; i < sk_X509_num(certs); i++) {
634 recip = sk_X509_value(certs, i);
635 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
636 ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
641 if (!(flags & CMS_DETACHED))
642 CMS_set_detached(cms, 0);
644 if ((flags & (CMS_STREAM | CMS_PARTIAL))
645 || CMS_final(cms, data, NULL, flags))
651 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
653 CMS_ContentInfo_free(cms);
657 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
658 const EVP_CIPHER *cipher, unsigned int flags)
660 return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
663 static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
664 CMS_RecipientInfo *ri,
665 EVP_PKEY *pk, X509 *cert, X509 *peer)
668 STACK_OF(CMS_RecipientEncryptedKey) *reks;
669 CMS_RecipientEncryptedKey *rek;
671 reks = CMS_RecipientInfo_kari_get0_reks(ri);
672 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
675 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
676 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
678 CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
679 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
680 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
683 return cert == NULL ? 0 : -1;
688 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
690 return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
693 int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
694 X509 *cert, X509 *peer)
696 STACK_OF(CMS_RecipientInfo) *ris;
697 CMS_RecipientInfo *ri;
698 int i, r, cms_pkey_ri_type;
699 int debug = 0, match_ri = 0;
701 ris = CMS_get0_RecipientInfos(cms);
703 debug = cms_get0_env_enc_content(cms)->debug;
705 cms_pkey_ri_type = cms_pkey_get_ri_type(pk);
706 if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
707 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
711 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
714 ri = sk_CMS_RecipientInfo_value(ris, i);
715 ri_type = CMS_RecipientInfo_type(ri);
716 if (!cms_pkey_is_ri_type_supported(pk, ri_type))
719 if (ri_type == CMS_RECIPINFO_AGREE) {
720 r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
727 * If we have a cert try matching RecipientInfo otherwise try them
730 else if (cert == NULL|| !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
732 CMS_RecipientInfo_set0_pkey(ri, pk);
733 r = CMS_RecipientInfo_decrypt(cms, ri);
734 CMS_RecipientInfo_set0_pkey(ri, NULL);
737 * If not debugging clear any error and return success to
738 * avoid leaking of information useful to MMA
746 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
750 * If no cert and not debugging don't leave loop after first
751 * successful decrypt. Always attempt to decrypt all recipients
752 * to avoid leaking timing of a successful decrypt.
754 else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
758 /* If no cert, key transport and not debugging always return success */
760 && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
767 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
772 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
773 unsigned char *key, size_t keylen,
774 const unsigned char *id, size_t idlen)
776 STACK_OF(CMS_RecipientInfo) *ris;
777 CMS_RecipientInfo *ri;
780 ris = CMS_get0_RecipientInfos(cms);
781 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
782 ri = sk_CMS_RecipientInfo_value(ris, i);
783 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
787 * If we have an id try matching RecipientInfo otherwise try them
790 if (id == NULL || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
791 CMS_RecipientInfo_set0_key(ri, key, keylen);
792 r = CMS_RecipientInfo_decrypt(cms, ri);
793 CMS_RecipientInfo_set0_key(ri, NULL, 0);
797 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
804 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
809 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
810 unsigned char *pass, ossl_ssize_t passlen)
812 STACK_OF(CMS_RecipientInfo) *ris;
813 CMS_RecipientInfo *ri;
816 ris = CMS_get0_RecipientInfos(cms);
817 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
818 ri = sk_CMS_RecipientInfo_value(ris, i);
819 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
821 CMS_RecipientInfo_set0_password(ri, pass, passlen);
822 r = CMS_RecipientInfo_decrypt(cms, ri);
823 CMS_RecipientInfo_set0_password(ri, NULL, 0);
828 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
833 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
834 BIO *dcont, BIO *out, unsigned int flags)
839 int nid = OBJ_obj2nid(CMS_get0_type(cms));
841 if (nid != NID_pkcs7_enveloped
842 && nid != NID_id_smime_ct_authEnvelopedData) {
843 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA);
846 if (dcont == NULL && !check_content(cms))
848 if (flags & CMS_DEBUG_DECRYPT)
849 cms_get0_env_enc_content(cms)->debug = 1;
851 cms_get0_env_enc_content(cms)->debug = 0;
853 cms_get0_env_enc_content(cms)->havenocert = 1;
855 cms_get0_env_enc_content(cms)->havenocert = 0;
856 if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
858 if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
860 cont = CMS_dataInit(cms, dcont);
863 r = cms_copy_content(out, cont, flags);
864 do_free_upto(cont, dcont);
868 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
873 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
874 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
878 ret = SMIME_crlf_copy(data, cmsbio, flags);
880 (void)BIO_flush(cmsbio);
882 if (!CMS_dataFinal(cms, cmsbio)) {
883 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
887 do_free_upto(cmsbio, dcont);
895 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
901 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
902 ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
906 if (dcont == NULL && !check_content(cms))
909 cont = CMS_dataInit(cms, dcont);
912 r = cms_copy_content(out, cont, flags);
913 do_free_upto(cont, dcont);
917 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
919 CMS_ContentInfo *cms;
922 comp_nid = NID_zlib_compression;
923 cms = cms_CompressedData_create(comp_nid, NULL, NULL);
927 if (!(flags & CMS_DETACHED))
928 CMS_set_detached(cms, 0);
930 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
933 CMS_ContentInfo_free(cms);
939 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
942 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
946 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
948 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);