2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
54 #include "internal/cryptlib.h"
55 #include <openssl/asn1t.h>
56 #include <openssl/x509.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/err.h>
59 #include <openssl/cms.h>
61 #include "internal/asn1_int.h"
63 static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
67 rbio = BIO_new(BIO_s_null());
68 else if (flags & CMS_TEXT) {
69 rbio = BIO_new(BIO_s_mem());
70 BIO_set_mem_eof_return(rbio, 0);
76 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
78 unsigned char buf[4096];
82 tmpout = cms_get_text_bio(out, flags);
85 CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
89 /* Read all content through chain to process digest, decrypt etc */
91 i = BIO_read(in, buf, sizeof(buf));
93 if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
94 if (!BIO_get_cipher_status(in))
102 if (tmpout && (BIO_write(tmpout, buf, i) != i))
106 if (flags & CMS_TEXT) {
107 if (!SMIME_text(tmpout, out)) {
108 CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR);
122 static int check_content(CMS_ContentInfo *cms)
124 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
126 CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
132 static void do_free_upto(BIO *f, BIO *upto)
141 while (f && f != upto);
146 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
150 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
151 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
154 cont = CMS_dataInit(cms, NULL);
157 r = cms_copy_content(out, cont, flags);
162 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
164 CMS_ContentInfo *cms;
165 cms = cms_Data_create();
169 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
172 CMS_ContentInfo_free(cms);
177 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
182 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
183 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
187 if (!dcont && !check_content(cms))
190 cont = CMS_dataInit(cms, dcont);
193 r = cms_copy_content(out, cont, flags);
195 r = cms_DigestedData_do_final(cms, cont, 1);
196 do_free_upto(cont, dcont);
200 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
203 CMS_ContentInfo *cms;
206 cms = cms_DigestedData_create(md);
210 if (!(flags & CMS_DETACHED))
211 CMS_set_detached(cms, 0);
213 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
216 CMS_ContentInfo_free(cms);
220 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
221 const unsigned char *key, size_t keylen,
222 BIO *dcont, BIO *out, unsigned int flags)
226 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
227 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
228 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
232 if (!dcont && !check_content(cms))
235 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
237 cont = CMS_dataInit(cms, dcont);
240 r = cms_copy_content(out, cont, flags);
241 do_free_upto(cont, dcont);
245 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
246 const unsigned char *key,
247 size_t keylen, unsigned int flags)
249 CMS_ContentInfo *cms;
251 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
254 cms = CMS_ContentInfo_new();
257 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
260 if (!(flags & CMS_DETACHED))
261 CMS_set_detached(cms, 0);
263 if ((flags & (CMS_STREAM | CMS_PARTIAL))
264 || CMS_final(cms, in, NULL, flags))
267 CMS_ContentInfo_free(cms);
271 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
273 STACK_OF(X509) *certs,
274 STACK_OF(X509_CRL) *crls)
276 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
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 X509_STORE_CTX_free(ctx);
309 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
310 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
313 STACK_OF(CMS_SignerInfo) *sinfos;
314 STACK_OF(X509) *cms_certs = NULL;
315 STACK_OF(X509_CRL) *crls = NULL;
317 int i, scount = 0, ret = 0;
318 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
320 if (!dcont && !check_content(cms))
322 if (dcont && !(flags & CMS_BINARY)) {
323 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
324 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
325 flags |= CMS_ASCIICRLF;
328 /* Attempt to find all signer certificates */
330 sinfos = CMS_get0_SignerInfos(cms);
332 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
333 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
337 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
338 si = sk_CMS_SignerInfo_value(sinfos, i);
339 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
344 if (scount != sk_CMS_SignerInfo_num(sinfos))
345 scount += CMS_set1_signers_certs(cms, certs, flags);
347 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
348 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
352 /* Attempt to verify all signers certs */
354 if (!(flags & CMS_NO_SIGNER_CERT_VERIFY)) {
355 cms_certs = CMS_get1_certs(cms);
356 if (!(flags & CMS_NOCRL))
357 crls = CMS_get1_crls(cms);
358 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
359 si = sk_CMS_SignerInfo_value(sinfos, i);
360 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls))
365 /* Attempt to verify all SignerInfo signed attribute signatures */
367 if (!(flags & CMS_NO_ATTR_VERIFY)) {
368 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
369 si = sk_CMS_SignerInfo_value(sinfos, i);
370 if (CMS_signed_get_attr_count(si) < 0)
372 if (CMS_SignerInfo_verify(si) <= 0)
378 * Performance optimization: if the content is a memory BIO then store
379 * its contents in a temporary read only memory BIO. This avoids
380 * potentially large numbers of slow copies of data which will occur when
381 * reading from a read write memory BIO when signatures are calculated.
384 if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
387 len = BIO_get_mem_data(dcont, &ptr);
388 tmpin = BIO_new_mem_buf(ptr, len);
390 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
396 * If not binary mode and detached generate digests by *writing* through
397 * the BIO. That makes it possible to canonicalise the input.
399 if (!(flags & SMIME_BINARY) && dcont) {
401 * Create output BIO so we can either handle text or to ensure
402 * included content doesn't override detached content.
404 tmpout = cms_get_text_bio(out, flags);
406 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
409 cmsbio = CMS_dataInit(cms, tmpout);
413 * Don't use SMIME_TEXT for verify: it adds headers and we want to
416 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
418 if (flags & CMS_TEXT) {
419 if (!SMIME_text(tmpout, out)) {
420 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR);
425 cmsbio = CMS_dataInit(cms, tmpin);
429 if (!cms_copy_content(out, cmsbio, flags))
433 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
434 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
435 si = sk_CMS_SignerInfo_value(sinfos, i);
436 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
437 CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
446 if (!(flags & SMIME_BINARY) && dcont) {
447 do_free_upto(cmsbio, tmpout);
451 if (dcont && (tmpin == dcont))
452 do_free_upto(cmsbio, dcont);
454 BIO_free_all(cmsbio);
458 BIO_free_all(tmpout);
461 sk_X509_pop_free(cms_certs, X509_free);
462 sk_X509_CRL_pop_free(crls, X509_CRL_free);
467 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
468 STACK_OF(X509) *certs,
469 X509_STORE *store, unsigned int flags)
472 flags &= ~(CMS_DETACHED | CMS_TEXT);
473 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
476 return cms_Receipt_verify(rcms, ocms);
479 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
480 STACK_OF(X509) *certs, BIO *data,
483 CMS_ContentInfo *cms;
486 cms = CMS_ContentInfo_new();
487 if (cms == NULL || !CMS_SignedData_init(cms))
489 if (flags & CMS_ASCIICRLF
490 && !CMS_set1_eContentType(cms,
491 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
494 if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
495 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
499 for (i = 0; i < sk_X509_num(certs); i++) {
500 X509 *x = sk_X509_value(certs, i);
501 if (!CMS_add1_cert(cms, x))
505 if (!(flags & CMS_DETACHED))
506 CMS_set_detached(cms, 0);
508 if ((flags & (CMS_STREAM | CMS_PARTIAL))
509 || CMS_final(cms, data, NULL, flags))
515 CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
518 CMS_ContentInfo_free(cms);
522 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
523 X509 *signcert, EVP_PKEY *pkey,
524 STACK_OF(X509) *certs, unsigned int flags)
526 CMS_SignerInfo *rct_si;
527 CMS_ContentInfo *cms = NULL;
528 ASN1_OCTET_STRING **pos, *os;
529 BIO *rct_cont = NULL;
532 flags &= ~(CMS_STREAM | CMS_TEXT);
533 /* Not really detached but avoids content being allocated */
534 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
535 if (!pkey || !signcert) {
536 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
540 /* Initialize signed data */
542 cms = CMS_sign(NULL, NULL, certs, NULL, flags);
546 /* Set inner content type to signed receipt */
547 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
550 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
552 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
556 os = cms_encode_Receipt(si);
561 /* Set content to digest */
562 rct_cont = BIO_new_mem_buf(os->data, os->length);
566 /* Add msgSigDigest attribute */
568 if (!cms_msgSigDigest_add1(rct_si, si))
571 /* Finalize structure */
572 if (!CMS_final(cms, rct_cont, NULL, flags))
575 /* Set embedded content */
576 pos = CMS_get0_content(cms);
585 CMS_ContentInfo_free(cms);
590 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
591 const EVP_CIPHER *cipher, unsigned int flags)
593 CMS_ContentInfo *cms;
596 cms = CMS_EnvelopedData_create(cipher);
599 for (i = 0; i < sk_X509_num(certs); i++) {
600 recip = sk_X509_value(certs, i);
601 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
602 CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
607 if (!(flags & CMS_DETACHED))
608 CMS_set_detached(cms, 0);
610 if ((flags & (CMS_STREAM | CMS_PARTIAL))
611 || CMS_final(cms, data, NULL, flags))
617 CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
619 CMS_ContentInfo_free(cms);
623 static int cms_kari_set1_pkey(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
624 EVP_PKEY *pk, X509 *cert)
627 STACK_OF(CMS_RecipientEncryptedKey) *reks;
628 CMS_RecipientEncryptedKey *rek;
629 reks = CMS_RecipientInfo_kari_get0_reks(ri);
632 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
634 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
635 if (CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
637 CMS_RecipientInfo_kari_set0_pkey(ri, pk);
638 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
639 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
647 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
649 STACK_OF(CMS_RecipientInfo) *ris;
650 CMS_RecipientInfo *ri;
652 int debug = 0, match_ri = 0;
653 ris = CMS_get0_RecipientInfos(cms);
655 debug = cms->d.envelopedData->encryptedContentInfo->debug;
656 ri_type = cms_pkey_get_ri_type(pk);
657 if (ri_type == CMS_RECIPINFO_NONE) {
658 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
659 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
663 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
664 ri = sk_CMS_RecipientInfo_value(ris, i);
665 if (CMS_RecipientInfo_type(ri) != ri_type)
668 if (ri_type == CMS_RECIPINFO_AGREE) {
669 r = cms_kari_set1_pkey(cms, ri, pk, cert);
676 * If we have a cert try matching RecipientInfo otherwise try them
679 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, 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)
706 /* If no cert and not debugging always return success */
707 if (match_ri && !cert && !debug) {
712 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
717 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
718 unsigned char *key, size_t keylen,
719 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)
781 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
782 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
785 if (!dcont && !check_content(cms))
787 if (flags & CMS_DEBUG_DECRYPT)
788 cms->d.envelopedData->encryptedContentInfo->debug = 1;
790 cms->d.envelopedData->encryptedContentInfo->debug = 0;
791 if (!pk && !cert && !dcont && !out)
793 if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
795 cont = CMS_dataInit(cms, dcont);
798 r = cms_copy_content(out, cont, flags);
799 do_free_upto(cont, dcont);
803 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
808 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
809 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);
813 SMIME_crlf_copy(data, cmsbio, flags);
815 (void)BIO_flush(cmsbio);
817 if (!CMS_dataFinal(cms, cmsbio)) {
818 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
825 do_free_upto(cmsbio, dcont);
833 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
838 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
839 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
843 if (!dcont && !check_content(cms))
846 cont = CMS_dataInit(cms, dcont);
849 r = cms_copy_content(out, cont, flags);
850 do_free_upto(cont, dcont);
854 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
856 CMS_ContentInfo *cms;
858 comp_nid = NID_zlib_compression;
859 cms = cms_CompressedData_create(comp_nid);
863 if (!(flags & CMS_DETACHED))
864 CMS_set_detached(cms, 0);
866 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
869 CMS_ContentInfo_free(cms);
875 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
878 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
882 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
884 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);