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 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 CMSerr(CMS_F_CMS_COPY_CONTENT, 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 CMSerr(CMS_F_CMS_COPY_CONTENT, 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 CMSerr(CMS_F_CHECK_CONTENT, 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 CMSerr(CMS_F_CMS_DATA, 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 OPENSSL_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 CMSerr(CMS_F_CMS_DIGEST_VERIFY, 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, OPENSSL_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 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
203 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
207 if (dcont == NULL && !check_content(cms))
210 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
212 cont = CMS_dataInit(cms, dcont);
215 r = cms_copy_content(out, cont, flags);
216 do_free_upto(cont, dcont);
220 CMS_ContentInfo *CMS_EncryptedData_encrypt_ex(BIO *in, const EVP_CIPHER *cipher,
221 const unsigned char *key,
222 size_t keylen, unsigned int flags,
226 CMS_ContentInfo *cms;
228 if (cipher == NULL) {
229 CMSerr(0, CMS_R_NO_CIPHER);
232 cms = CMS_ContentInfo_new_ex(libctx, propq);
235 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
238 if (!(flags & CMS_DETACHED))
239 CMS_set_detached(cms, 0);
241 if ((flags & (CMS_STREAM | CMS_PARTIAL))
242 || CMS_final(cms, in, NULL, flags))
245 CMS_ContentInfo_free(cms);
249 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
250 const unsigned char *key,
251 size_t keylen, unsigned int flags)
253 return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
257 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
259 STACK_OF(X509) *certs,
260 STACK_OF(X509_CRL) *crls,
261 STACK_OF(X509) **chain,
262 const CMS_CTX *cms_ctx)
268 ctx = X509_STORE_CTX_new_ex(cms_ctx->libctx, cms_ctx->propq);
270 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, 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 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, 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 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
286 CMS_R_CERTIFICATE_VERIFY_ERROR);
287 ERR_add_error_data(2, "Verify error:",
288 X509_verify_cert_error_string(j));
293 /* also send back the trust chain when required */
295 *chain = X509_STORE_CTX_get1_chain(ctx);
297 X509_STORE_CTX_free(ctx);
302 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
303 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
306 STACK_OF(CMS_SignerInfo) *sinfos;
307 STACK_OF(X509) *cms_certs = NULL;
308 STACK_OF(X509_CRL) *crls = NULL;
309 STACK_OF(X509) **si_chains = NULL;
311 int i, scount = 0, ret = 0;
312 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
313 int cadesVerify = (flags & CMS_CADES) != 0;
314 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
316 if (dcont == NULL && !check_content(cms))
318 if (dcont != NULL && !(flags & CMS_BINARY)) {
319 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
321 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
322 flags |= CMS_ASCIICRLF;
325 /* Attempt to find all signer certificates */
327 sinfos = CMS_get0_SignerInfos(cms);
329 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
330 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
334 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
335 si = sk_CMS_SignerInfo_value(sinfos, i);
336 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
341 if (scount != sk_CMS_SignerInfo_num(sinfos))
342 scount += CMS_set1_signers_certs(cms, certs, flags);
344 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
345 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
349 /* Attempt to verify all signers certs */
350 /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
352 if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
354 /* Certificate trust chain is required to check CAdES signature */
355 si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
356 if (si_chains == NULL) {
357 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
361 cms_certs = CMS_get1_certs(cms);
362 if (!(flags & CMS_NOCRL))
363 crls = CMS_get1_crls(cms);
364 for (i = 0; i < scount; i++) {
365 si = sk_CMS_SignerInfo_value(sinfos, i);
367 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
368 si_chains ? &si_chains[i] : NULL,
374 /* Attempt to verify all SignerInfo signed attribute signatures */
376 if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
377 for (i = 0; i < scount; i++) {
378 si = sk_CMS_SignerInfo_value(sinfos, i);
379 if (CMS_signed_get_attr_count(si) < 0)
381 if (CMS_SignerInfo_verify(si) <= 0)
384 STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
386 if (ess_check_signing_certs(si, si_chain) <= 0)
393 * Performance optimization: if the content is a memory BIO then store
394 * its contents in a temporary read only memory BIO. This avoids
395 * potentially large numbers of slow copies of data which will occur when
396 * reading from a read write memory BIO when signatures are calculated.
399 if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
403 len = BIO_get_mem_data(dcont, &ptr);
404 tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
406 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
413 * If not binary mode and detached generate digests by *writing* through
414 * the BIO. That makes it possible to canonicalise the input.
416 if (!(flags & SMIME_BINARY) && dcont) {
418 * Create output BIO so we can either handle text or to ensure
419 * included content doesn't override detached content.
421 tmpout = cms_get_text_bio(out, flags);
422 if (tmpout == NULL) {
423 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
426 cmsbio = CMS_dataInit(cms, tmpout);
430 * Don't use SMIME_TEXT for verify: it adds headers and we want to
433 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
435 if (flags & CMS_TEXT) {
436 if (!SMIME_text(tmpout, out)) {
437 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR);
442 cmsbio = CMS_dataInit(cms, tmpin);
446 if (!cms_copy_content(out, cmsbio, flags))
450 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
451 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
452 si = sk_CMS_SignerInfo_value(sinfos, i);
453 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
454 CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
462 if (!(flags & SMIME_BINARY) && dcont) {
463 do_free_upto(cmsbio, tmpout);
467 if (dcont && (tmpin == dcont))
468 do_free_upto(cmsbio, dcont);
470 BIO_free_all(cmsbio);
474 BIO_free_all(tmpout);
477 if (si_chains != NULL) {
478 for (i = 0; i < scount; ++i)
479 sk_X509_pop_free(si_chains[i], X509_free);
480 OPENSSL_free(si_chains);
482 sk_X509_pop_free(cms_certs, X509_free);
483 sk_X509_CRL_pop_free(crls, X509_CRL_free);
488 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
489 STACK_OF(X509) *certs,
490 X509_STORE *store, unsigned int flags)
494 flags &= ~(CMS_DETACHED | CMS_TEXT);
495 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
498 return cms_Receipt_verify(rcms, ocms);
501 CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
502 STACK_OF(X509) *certs, BIO *data,
503 unsigned int flags, OPENSSL_CTX *libctx,
506 CMS_ContentInfo *cms;
509 cms = CMS_ContentInfo_new_ex(libctx, propq);
510 if (cms == NULL || !CMS_SignedData_init(cms))
512 if (flags & CMS_ASCIICRLF
513 && !CMS_set1_eContentType(cms,
514 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
517 if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
518 CMSerr(0, CMS_R_ADD_SIGNER_ERROR);
522 for (i = 0; i < sk_X509_num(certs); i++) {
523 X509 *x = sk_X509_value(certs, i);
525 if (!CMS_add1_cert(cms, x))
529 if (!(flags & CMS_DETACHED))
530 CMS_set_detached(cms, 0);
532 if ((flags & (CMS_STREAM | CMS_PARTIAL))
533 || CMS_final(cms, data, NULL, flags))
539 CMSerr(0, ERR_R_MALLOC_FAILURE);
542 CMS_ContentInfo_free(cms);
546 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
547 BIO *data, unsigned int flags)
549 return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
552 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
553 X509 *signcert, EVP_PKEY *pkey,
554 STACK_OF(X509) *certs, unsigned int flags)
556 CMS_SignerInfo *rct_si;
557 CMS_ContentInfo *cms = NULL;
558 ASN1_OCTET_STRING **pos, *os;
559 BIO *rct_cont = NULL;
561 const CMS_CTX *ctx = si->cms_ctx;
563 flags &= ~(CMS_STREAM | CMS_TEXT);
564 /* Not really detached but avoids content being allocated */
565 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
566 if (pkey == NULL || signcert == NULL) {
567 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
571 /* Initialize signed data */
573 cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags, ctx->libctx, ctx->propq);
577 /* Set inner content type to signed receipt */
578 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
581 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
583 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
587 os = cms_encode_Receipt(si);
591 /* Set content to digest */
592 rct_cont = BIO_new_mem_buf(os->data, os->length);
593 if (rct_cont == NULL)
596 /* Add msgSigDigest attribute */
598 if (!cms_msgSigDigest_add1(rct_si, si))
601 /* Finalize structure */
602 if (!CMS_final(cms, rct_cont, NULL, flags))
605 /* Set embedded content */
606 pos = CMS_get0_content(cms);
615 CMS_ContentInfo_free(cms);
620 CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
621 const EVP_CIPHER *cipher, unsigned int flags,
622 OPENSSL_CTX *libctx, const char *propq)
624 CMS_ContentInfo *cms;
629 cms = (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
630 ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
631 : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
634 for (i = 0; i < sk_X509_num(certs); i++) {
635 recip = sk_X509_value(certs, i);
636 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
637 CMSerr(0, CMS_R_RECIPIENT_ERROR);
642 if (!(flags & CMS_DETACHED))
643 CMS_set_detached(cms, 0);
645 if ((flags & (CMS_STREAM | CMS_PARTIAL))
646 || CMS_final(cms, data, NULL, flags))
652 CMSerr(0, ERR_R_MALLOC_FAILURE);
654 CMS_ContentInfo_free(cms);
658 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
659 const EVP_CIPHER *cipher, unsigned int flags)
661 return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
664 static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
665 CMS_RecipientInfo *ri,
666 EVP_PKEY *pk, X509 *cert, X509 *peer)
669 STACK_OF(CMS_RecipientEncryptedKey) *reks;
670 CMS_RecipientEncryptedKey *rek;
672 reks = CMS_RecipientInfo_kari_get0_reks(ri);
673 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
676 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
677 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
679 CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
680 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
681 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
684 return cert == NULL ? 0 : -1;
689 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
691 return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
694 int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
695 X509 *cert, X509 *peer)
697 STACK_OF(CMS_RecipientInfo) *ris;
698 CMS_RecipientInfo *ri;
699 int i, r, cms_pkey_ri_type;
700 int debug = 0, match_ri = 0;
702 ris = CMS_get0_RecipientInfos(cms);
704 debug = cms_get0_env_enc_content(cms)->debug;
706 cms_pkey_ri_type = cms_pkey_get_ri_type(pk);
707 if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
708 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER,
709 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
713 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
716 ri = sk_CMS_RecipientInfo_value(ris, i);
717 ri_type = CMS_RecipientInfo_type(ri);
718 if (!cms_pkey_is_ri_type_supported(pk, ri_type))
721 if (ri_type == CMS_RECIPINFO_AGREE) {
722 r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
729 * If we have a cert try matching RecipientInfo otherwise try them
732 else if (cert == NULL|| !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
734 CMS_RecipientInfo_set0_pkey(ri, pk);
735 r = CMS_RecipientInfo_decrypt(cms, ri);
736 CMS_RecipientInfo_set0_pkey(ri, NULL);
739 * If not debugging clear any error and return success to
740 * avoid leaking of information useful to MMA
748 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER, CMS_R_DECRYPT_ERROR);
752 * If no cert and not debugging don't leave loop after first
753 * successful decrypt. Always attempt to decrypt all recipients
754 * to avoid leaking timing of a successful decrypt.
756 else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
760 /* If no cert, key transport and not debugging always return success */
762 && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
769 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER, CMS_R_NO_MATCHING_RECIPIENT);
774 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
775 unsigned char *key, size_t keylen,
776 const unsigned char *id, size_t idlen)
778 STACK_OF(CMS_RecipientInfo) *ris;
779 CMS_RecipientInfo *ri;
782 ris = CMS_get0_RecipientInfos(cms);
783 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
784 ri = sk_CMS_RecipientInfo_value(ris, i);
785 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
789 * If we have an id try matching RecipientInfo otherwise try them
792 if (id == NULL || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
793 CMS_RecipientInfo_set0_key(ri, key, keylen);
794 r = CMS_RecipientInfo_decrypt(cms, ri);
795 CMS_RecipientInfo_set0_key(ri, NULL, 0);
799 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
806 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
811 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
812 unsigned char *pass, ossl_ssize_t passlen)
814 STACK_OF(CMS_RecipientInfo) *ris;
815 CMS_RecipientInfo *ri;
818 ris = CMS_get0_RecipientInfos(cms);
819 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
820 ri = sk_CMS_RecipientInfo_value(ris, i);
821 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
823 CMS_RecipientInfo_set0_password(ri, pass, passlen);
824 r = CMS_RecipientInfo_decrypt(cms, ri);
825 CMS_RecipientInfo_set0_password(ri, NULL, 0);
830 CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
835 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
836 BIO *dcont, BIO *out, unsigned int flags)
841 int nid = OBJ_obj2nid(CMS_get0_type(cms));
843 if (nid != NID_pkcs7_enveloped
844 && nid != NID_id_smime_ct_authEnvelopedData) {
845 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
848 if (dcont == NULL && !check_content(cms))
850 if (flags & CMS_DEBUG_DECRYPT)
851 cms_get0_env_enc_content(cms)->debug = 1;
853 cms_get0_env_enc_content(cms)->debug = 0;
855 cms_get0_env_enc_content(cms)->havenocert = 1;
857 cms_get0_env_enc_content(cms)->havenocert = 0;
858 if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
860 if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
862 cont = CMS_dataInit(cms, dcont);
865 r = cms_copy_content(out, cont, flags);
866 do_free_upto(cont, dcont);
870 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
875 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
876 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);
880 ret = SMIME_crlf_copy(data, cmsbio, flags);
882 (void)BIO_flush(cmsbio);
884 if (!CMS_dataFinal(cms, cmsbio)) {
885 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
889 do_free_upto(cmsbio, dcont);
897 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
903 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
904 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
908 if (dcont == NULL && !check_content(cms))
911 cont = CMS_dataInit(cms, dcont);
914 r = cms_copy_content(out, cont, flags);
915 do_free_upto(cont, dcont);
919 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
921 CMS_ContentInfo *cms;
924 comp_nid = NID_zlib_compression;
925 cms = cms_CompressedData_create(comp_nid, NULL, NULL);
929 if (!(flags & CMS_DETACHED))
930 CMS_set_detached(cms, 0);
932 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
935 CMS_ContentInfo_free(cms);
941 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
944 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
948 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
950 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);