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)
29 rbio = BIO_new(BIO_s_null());
30 else if (flags & CMS_TEXT) {
31 rbio = BIO_new(BIO_s_mem());
32 BIO_set_mem_eof_return(rbio, 0);
38 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
40 unsigned char buf[4096];
44 tmpout = cms_get_text_bio(out, flags);
47 CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
51 /* Read all content through chain to process digest, decrypt etc */
53 i = BIO_read(in, buf, sizeof(buf));
55 if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
56 if (!BIO_get_cipher_status(in))
64 if (tmpout && (BIO_write(tmpout, buf, i) != i))
68 if (flags & CMS_TEXT) {
69 if (!SMIME_text(tmpout, out)) {
70 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)
103 } while (f != NULL && f != upto);
108 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
112 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
113 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
116 cont = CMS_dataInit(cms, NULL);
119 r = cms_copy_content(out, cont, flags);
124 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
126 CMS_ContentInfo *cms;
127 cms = cms_Data_create();
131 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
134 CMS_ContentInfo_free(cms);
139 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
144 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
145 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
149 if (!dcont && !check_content(cms))
152 cont = CMS_dataInit(cms, dcont);
155 r = cms_copy_content(out, cont, flags);
157 r = cms_DigestedData_do_final(cms, cont, 1);
158 do_free_upto(cont, dcont);
162 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
165 CMS_ContentInfo *cms;
168 cms = cms_DigestedData_create(md);
172 if (!(flags & CMS_DETACHED))
173 CMS_set_detached(cms, 0);
175 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
178 CMS_ContentInfo_free(cms);
182 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
183 const unsigned char *key, size_t keylen,
184 BIO *dcont, BIO *out, unsigned int flags)
188 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
189 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
190 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
194 if (!dcont && !check_content(cms))
197 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
199 cont = CMS_dataInit(cms, dcont);
202 r = cms_copy_content(out, cont, flags);
203 do_free_upto(cont, dcont);
207 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
208 const unsigned char *key,
209 size_t keylen, unsigned int flags)
211 CMS_ContentInfo *cms;
213 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
216 cms = CMS_ContentInfo_new();
219 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
222 if (!(flags & CMS_DETACHED))
223 CMS_set_detached(cms, 0);
225 if ((flags & (CMS_STREAM | CMS_PARTIAL))
226 || CMS_final(cms, in, NULL, flags))
229 CMS_ContentInfo_free(cms);
233 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
235 STACK_OF(X509) *certs,
236 STACK_OF(X509_CRL) *crls,
237 STACK_OF(X509) **chain)
239 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
244 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
247 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
248 if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
249 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR);
252 X509_STORE_CTX_set_default(ctx, "smime_sign");
254 X509_STORE_CTX_set0_crls(ctx, crls);
256 i = X509_verify_cert(ctx);
258 j = X509_STORE_CTX_get_error(ctx);
259 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
260 CMS_R_CERTIFICATE_VERIFY_ERROR);
261 ERR_add_error_data(2, "Verify error:",
262 X509_verify_cert_error_string(j));
267 /* also send back the trust chain when required */
269 *chain = X509_STORE_CTX_get1_chain(ctx);
271 X509_STORE_CTX_free(ctx);
276 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
277 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
280 STACK_OF(CMS_SignerInfo) *sinfos;
281 STACK_OF(X509) *cms_certs = NULL;
282 STACK_OF(X509_CRL) *crls = NULL;
283 STACK_OF(X509) **si_chains = NULL;
285 int i, scount = 0, ret = 0;
286 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
287 int cadesVerify = (flags & CMS_CADES) != 0;
289 if (!dcont && !check_content(cms))
291 if (dcont && !(flags & CMS_BINARY)) {
292 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
293 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
294 flags |= CMS_ASCIICRLF;
297 /* Attempt to find all signer certificates */
299 sinfos = CMS_get0_SignerInfos(cms);
301 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
302 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
306 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
307 si = sk_CMS_SignerInfo_value(sinfos, i);
308 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
313 if (scount != sk_CMS_SignerInfo_num(sinfos))
314 scount += CMS_set1_signers_certs(cms, certs, flags);
316 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
317 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
321 /* Attempt to verify all signers certs */
322 /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
324 if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
326 /* Certificate trust chain is required to check CAdES signature */
327 si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
328 if (si_chains == NULL) {
329 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
333 cms_certs = CMS_get1_certs(cms);
334 if (!(flags & CMS_NOCRL))
335 crls = CMS_get1_crls(cms);
336 for (i = 0; i < scount; i++) {
337 si = sk_CMS_SignerInfo_value(sinfos, i);
339 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
340 si_chains ? &si_chains[i] : NULL))
345 /* Attempt to verify all SignerInfo signed attribute signatures */
347 if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
348 for (i = 0; i < scount; i++) {
349 si = sk_CMS_SignerInfo_value(sinfos, i);
350 if (CMS_signed_get_attr_count(si) < 0)
352 if (CMS_SignerInfo_verify(si) <= 0)
355 STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
357 if (ess_check_signing_certs(si, si_chain) <= 0)
364 * Performance optimization: if the content is a memory BIO then store
365 * its contents in a temporary read only memory BIO. This avoids
366 * potentially large numbers of slow copies of data which will occur when
367 * reading from a read write memory BIO when signatures are calculated.
370 if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
373 len = BIO_get_mem_data(dcont, &ptr);
374 tmpin = BIO_new_mem_buf(ptr, len);
376 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
382 * If not binary mode and detached generate digests by *writing* through
383 * the BIO. That makes it possible to canonicalise the input.
385 if (!(flags & SMIME_BINARY) && dcont) {
387 * Create output BIO so we can either handle text or to ensure
388 * included content doesn't override detached content.
390 tmpout = cms_get_text_bio(out, flags);
392 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
395 cmsbio = CMS_dataInit(cms, tmpout);
399 * Don't use SMIME_TEXT for verify: it adds headers and we want to
402 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
404 if (flags & CMS_TEXT) {
405 if (!SMIME_text(tmpout, out)) {
406 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR);
411 cmsbio = CMS_dataInit(cms, tmpin);
415 if (!cms_copy_content(out, cmsbio, flags))
419 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
420 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
421 si = sk_CMS_SignerInfo_value(sinfos, i);
422 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
423 CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
432 if (!(flags & SMIME_BINARY) && dcont) {
433 do_free_upto(cmsbio, tmpout);
437 if (dcont && (tmpin == dcont))
438 do_free_upto(cmsbio, dcont);
440 BIO_free_all(cmsbio);
444 BIO_free_all(tmpout);
447 if (si_chains != NULL) {
448 for (i = 0; i < scount; ++i)
449 sk_X509_pop_free(si_chains[i], X509_free);
450 OPENSSL_free(si_chains);
452 sk_X509_pop_free(cms_certs, X509_free);
453 sk_X509_CRL_pop_free(crls, X509_CRL_free);
458 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
459 STACK_OF(X509) *certs,
460 X509_STORE *store, unsigned int flags)
463 flags &= ~(CMS_DETACHED | CMS_TEXT);
464 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
467 return cms_Receipt_verify(rcms, ocms);
470 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
471 STACK_OF(X509) *certs, BIO *data,
474 CMS_ContentInfo *cms;
477 cms = CMS_ContentInfo_new();
478 if (cms == NULL || !CMS_SignedData_init(cms))
480 if (flags & CMS_ASCIICRLF
481 && !CMS_set1_eContentType(cms,
482 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
485 if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
486 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
490 for (i = 0; i < sk_X509_num(certs); i++) {
491 X509 *x = sk_X509_value(certs, i);
492 if (!CMS_add1_cert(cms, x))
496 if (!(flags & CMS_DETACHED))
497 CMS_set_detached(cms, 0);
499 if ((flags & (CMS_STREAM | CMS_PARTIAL))
500 || CMS_final(cms, data, NULL, flags))
506 CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
509 CMS_ContentInfo_free(cms);
513 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
514 X509 *signcert, EVP_PKEY *pkey,
515 STACK_OF(X509) *certs, unsigned int flags)
517 CMS_SignerInfo *rct_si;
518 CMS_ContentInfo *cms = NULL;
519 ASN1_OCTET_STRING **pos, *os;
520 BIO *rct_cont = NULL;
523 flags &= ~(CMS_STREAM | CMS_TEXT);
524 /* Not really detached but avoids content being allocated */
525 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
526 if (pkey == NULL || signcert == NULL) {
527 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
531 /* Initialize signed data */
533 cms = CMS_sign(NULL, NULL, certs, NULL, flags);
537 /* Set inner content type to signed receipt */
538 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
541 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
543 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
547 os = cms_encode_Receipt(si);
552 /* Set content to digest */
553 rct_cont = BIO_new_mem_buf(os->data, os->length);
557 /* Add msgSigDigest attribute */
559 if (!cms_msgSigDigest_add1(rct_si, si))
562 /* Finalize structure */
563 if (!CMS_final(cms, rct_cont, NULL, flags))
566 /* Set embedded content */
567 pos = CMS_get0_content(cms);
576 CMS_ContentInfo_free(cms);
581 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
582 const EVP_CIPHER *cipher, unsigned int flags)
584 CMS_ContentInfo *cms;
587 cms = CMS_EnvelopedData_create(cipher);
590 for (i = 0; i < sk_X509_num(certs); i++) {
591 recip = sk_X509_value(certs, i);
592 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
593 CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
598 if (!(flags & CMS_DETACHED))
599 CMS_set_detached(cms, 0);
601 if ((flags & (CMS_STREAM | CMS_PARTIAL))
602 || CMS_final(cms, data, NULL, flags))
608 CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
610 CMS_ContentInfo_free(cms);
614 static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
615 EVP_PKEY *pk, X509 *cert, X509 *peer)
618 STACK_OF(CMS_RecipientEncryptedKey) *reks;
619 CMS_RecipientEncryptedKey *rek;
621 reks = CMS_RecipientInfo_kari_get0_reks(ri);
622 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
624 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
625 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
627 CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
628 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
629 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
632 return cert == NULL ? 0 : -1;
637 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
639 return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
642 int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert, X509 *peer)
644 STACK_OF(CMS_RecipientInfo) *ris;
645 CMS_RecipientInfo *ri;
646 int i, r, cms_pkey_ri_type;
647 int debug = 0, match_ri = 0;
648 ris = CMS_get0_RecipientInfos(cms);
650 debug = cms->d.envelopedData->encryptedContentInfo->debug;
652 cms_pkey_ri_type = cms_pkey_get_ri_type(pk);
653 if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
654 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER,
655 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
659 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
662 ri = sk_CMS_RecipientInfo_value(ris, i);
663 ri_type = CMS_RecipientInfo_type(ri);
664 if (!cms_pkey_is_ri_type_supported(pk, ri_type))
667 if (ri_type == CMS_RECIPINFO_AGREE) {
668 r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
675 * If we have a cert try matching RecipientInfo otherwise try them
678 else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
680 CMS_RecipientInfo_set0_pkey(ri, pk);
681 r = CMS_RecipientInfo_decrypt(cms, ri);
682 CMS_RecipientInfo_set0_pkey(ri, NULL);
685 * If not debugging clear any error and return success to
686 * avoid leaking of information useful to MMA
694 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER, CMS_R_DECRYPT_ERROR);
698 * If no cert and not debugging don't leave loop after first
699 * successful decrypt. Always attempt to decrypt all recipients
700 * to avoid leaking timing of a successful decrypt.
702 else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
706 /* If no cert, key transport and not debugging always return success */
707 if (cert == NULL && cms_pkey_ri_type == CMS_RECIPINFO_TRANS && match_ri && !debug) {
712 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY_AND_PEER, CMS_R_NO_MATCHING_RECIPIENT);
717 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
718 unsigned char *key, size_t keylen,
719 const unsigned char *id, size_t idlen)
721 STACK_OF(CMS_RecipientInfo) *ris;
722 CMS_RecipientInfo *ri;
724 ris = CMS_get0_RecipientInfos(cms);
725 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
726 ri = sk_CMS_RecipientInfo_value(ris, i);
727 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
731 * If we have an id try matching RecipientInfo otherwise try them
734 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
735 CMS_RecipientInfo_set0_key(ri, key, keylen);
736 r = CMS_RecipientInfo_decrypt(cms, ri);
737 CMS_RecipientInfo_set0_key(ri, NULL, 0);
741 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
748 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
753 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
754 unsigned char *pass, ossl_ssize_t passlen)
756 STACK_OF(CMS_RecipientInfo) *ris;
757 CMS_RecipientInfo *ri;
759 ris = CMS_get0_RecipientInfos(cms);
760 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
761 ri = sk_CMS_RecipientInfo_value(ris, i);
762 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
764 CMS_RecipientInfo_set0_password(ri, pass, passlen);
765 r = CMS_RecipientInfo_decrypt(cms, ri);
766 CMS_RecipientInfo_set0_password(ri, NULL, 0);
771 CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
776 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
777 BIO *dcont, BIO *out, unsigned int flags)
782 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
783 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
786 if (!dcont && !check_content(cms))
788 if (flags & CMS_DEBUG_DECRYPT)
789 cms->d.envelopedData->encryptedContentInfo->debug = 1;
791 cms->d.envelopedData->encryptedContentInfo->debug = 0;
793 cms->d.envelopedData->encryptedContentInfo->havenocert = 1;
795 cms->d.envelopedData->encryptedContentInfo->havenocert = 0;
796 if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
798 if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
800 cont = CMS_dataInit(cms, dcont);
803 r = cms_copy_content(out, cont, flags);
804 do_free_upto(cont, dcont);
808 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
813 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
814 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);
818 SMIME_crlf_copy(data, cmsbio, flags);
820 (void)BIO_flush(cmsbio);
822 if (!CMS_dataFinal(cms, cmsbio)) {
823 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
830 do_free_upto(cmsbio, dcont);
838 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
843 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
844 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
848 if (!dcont && !check_content(cms))
851 cont = CMS_dataInit(cms, dcont);
854 r = cms_copy_content(out, cont, flags);
855 do_free_upto(cont, dcont);
859 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
861 CMS_ContentInfo *cms;
863 comp_nid = NID_zlib_compression;
864 cms = cms_CompressedData_create(comp_nid);
868 if (!(flags & CMS_DETACHED))
869 CMS_set_detached(cms, 0);
871 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
874 CMS_ContentInfo_free(cms);
880 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
883 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
887 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
889 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);