Move openssl-0.9/ to openssl/.
[dragonfly.git] / crypto / openssl / crypto / cms / cms_smime.c
blobb35d28d411a3f77d6fb22626262b03cb33c7e769
1 /* crypto/cms/cms_smime.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
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
10 * are met:
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
18 * distribution.
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
35 * acknowledgment:
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 "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>
60 #include "cms_lcl.h"
62 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
64 unsigned char buf[4096];
65 int r = 0, i;
66 BIO *tmpout = NULL;
68 if (out == NULL)
69 tmpout = BIO_new(BIO_s_null());
70 else if (flags & CMS_TEXT)
71 tmpout = BIO_new(BIO_s_mem());
72 else
73 tmpout = out;
75 if(!tmpout)
77 CMSerr(CMS_F_CMS_COPY_CONTENT,ERR_R_MALLOC_FAILURE);
78 goto err;
81 /* Read all content through chain to process digest, decrypt etc */
82 for (;;)
84 i=BIO_read(in,buf,sizeof(buf));
85 if (i <= 0)
87 if (BIO_method_type(in) == BIO_TYPE_CIPHER)
89 if (!BIO_get_cipher_status(in))
90 goto err;
92 if (i < 0)
93 goto err;
94 break;
97 if (tmpout && (BIO_write(tmpout, buf, i) != i))
98 goto err;
101 if(flags & CMS_TEXT)
103 if(!SMIME_text(tmpout, out))
105 CMSerr(CMS_F_CMS_COPY_CONTENT,CMS_R_SMIME_TEXT_ERROR);
106 goto err;
110 r = 1;
112 err:
113 if (tmpout && (tmpout != out))
114 BIO_free(tmpout);
115 return r;
119 static int check_content(CMS_ContentInfo *cms)
121 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
122 if (!pos || !*pos)
124 CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
125 return 0;
127 return 1;
130 static void do_free_upto(BIO *f, BIO *upto)
132 if (upto)
134 BIO *tbio;
137 tbio = BIO_pop(f);
138 BIO_free(f);
139 f = tbio;
141 while (f != upto);
143 else
144 BIO_free_all(f);
147 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
149 BIO *cont;
150 int r;
151 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data)
153 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
154 return 0;
156 cont = CMS_dataInit(cms, NULL);
157 if (!cont)
158 return 0;
159 r = cms_copy_content(out, cont, flags);
160 BIO_free_all(cont);
161 return r;
164 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
166 CMS_ContentInfo *cms;
167 cms = cms_Data_create();
168 if (!cms)
169 return NULL;
171 if (CMS_final(cms, in, NULL, flags))
172 return cms;
174 CMS_ContentInfo_free(cms);
176 return NULL;
179 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
180 unsigned int flags)
182 BIO *cont;
183 int r;
184 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest)
186 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
187 return 0;
190 if (!dcont && !check_content(cms))
191 return 0;
193 cont = CMS_dataInit(cms, dcont);
194 if (!cont)
195 return 0;
196 r = cms_copy_content(out, cont, flags);
197 if (r)
198 r = cms_DigestedData_do_final(cms, cont, 1);
199 do_free_upto(cont, dcont);
200 return r;
203 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
204 unsigned int flags)
206 CMS_ContentInfo *cms;
207 if (!md)
208 md = EVP_sha1();
209 cms = cms_DigestedData_create(md);
210 if (!cms)
211 return NULL;
213 if(!(flags & CMS_DETACHED))
215 flags &= ~CMS_STREAM;
216 CMS_set_detached(cms, 0);
219 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
220 return cms;
222 CMS_ContentInfo_free(cms);
223 return NULL;
226 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
227 const unsigned char *key, size_t keylen,
228 BIO *dcont, BIO *out, unsigned int flags)
230 BIO *cont;
231 int r;
232 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted)
234 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
235 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
236 return 0;
239 if (!dcont && !check_content(cms))
240 return 0;
242 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
243 return 0;
244 cont = CMS_dataInit(cms, dcont);
245 if (!cont)
246 return 0;
247 r = cms_copy_content(out, cont, flags);
248 do_free_upto(cont, dcont);
249 return r;
252 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
253 const unsigned char *key, size_t keylen,
254 unsigned int flags)
256 CMS_ContentInfo *cms;
257 if (!cipher)
259 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
260 return NULL;
262 cms = CMS_ContentInfo_new();
263 if (!cms)
264 return NULL;
265 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
266 return NULL;
268 if(!(flags & CMS_DETACHED))
270 flags &= ~CMS_STREAM;
271 CMS_set_detached(cms, 0);
274 if ((flags & (CMS_STREAM|CMS_PARTIAL))
275 || CMS_final(cms, in, NULL, flags))
276 return cms;
278 CMS_ContentInfo_free(cms);
279 return NULL;
282 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
283 X509_STORE *store,
284 STACK_OF(X509) *certs,
285 STACK_OF(X509_CRL) *crls,
286 unsigned int flags)
288 X509_STORE_CTX ctx;
289 X509 *signer;
290 int i, j, r = 0;
291 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
292 if (!X509_STORE_CTX_init(&ctx, store, signer, certs))
294 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
295 CMS_R_STORE_INIT_ERROR);
296 goto err;
298 X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_SMIME_SIGN);
299 if (crls)
300 X509_STORE_CTX_set0_crls(&ctx, crls);
302 i = X509_verify_cert(&ctx);
303 if (i <= 0)
305 j = X509_STORE_CTX_get_error(&ctx);
306 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
307 CMS_R_CERTIFICATE_VERIFY_ERROR);
308 ERR_add_error_data(2, "Verify error:",
309 X509_verify_cert_error_string(j));
310 goto err;
312 r = 1;
313 err:
314 X509_STORE_CTX_cleanup(&ctx);
315 return r;
319 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
320 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
322 CMS_SignerInfo *si;
323 STACK_OF(CMS_SignerInfo) *sinfos;
324 STACK_OF(X509) *cms_certs = NULL;
325 STACK_OF(X509_CRL) *crls = NULL;
326 X509 *signer;
327 int i, scount = 0, ret = 0;
328 BIO *cmsbio = NULL, *tmpin = NULL;
330 if (!dcont && !check_content(cms))
331 return 0;
333 /* Attempt to find all signer certificates */
335 sinfos = CMS_get0_SignerInfos(cms);
337 if (sk_CMS_SignerInfo_num(sinfos) <= 0)
339 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
340 goto err;
343 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
345 si = sk_CMS_SignerInfo_value(sinfos, i);
346 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
347 if (signer)
348 scount++;
351 if (scount != sk_CMS_SignerInfo_num(sinfos))
352 scount += CMS_set1_signers_certs(cms, certs, flags);
354 if (scount != sk_CMS_SignerInfo_num(sinfos))
356 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
357 goto err;
360 /* Attempt to verify all signers certs */
362 if (!(flags & CMS_NO_SIGNER_CERT_VERIFY))
364 cms_certs = CMS_get1_certs(cms);
365 if (!(flags & CMS_NOCRL))
366 crls = CMS_get1_crls(cms);
367 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
369 si = sk_CMS_SignerInfo_value(sinfos, i);
370 if (!cms_signerinfo_verify_cert(si, store,
371 cms_certs, crls, flags))
372 goto err;
376 /* Attempt to verify all SignerInfo signed attribute signatures */
378 if (!(flags & CMS_NO_ATTR_VERIFY))
380 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
382 si = sk_CMS_SignerInfo_value(sinfos, i);
383 if (CMS_signed_get_attr_count(si) < 0)
384 continue;
385 if (CMS_SignerInfo_verify(si) <= 0)
386 goto err;
390 /* Performance optimization: if the content is a memory BIO then
391 * store its contents in a temporary read only memory BIO. This
392 * avoids potentially large numbers of slow copies of data which will
393 * occur when reading from a read write memory BIO when signatures
394 * are calculated.
397 if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM))
399 char *ptr;
400 long len;
401 len = BIO_get_mem_data(dcont, &ptr);
402 tmpin = BIO_new_mem_buf(ptr, len);
403 if (tmpin == NULL)
405 CMSerr(CMS_F_CMS_VERIFY,ERR_R_MALLOC_FAILURE);
406 return 0;
409 else
410 tmpin = dcont;
413 cmsbio=CMS_dataInit(cms, tmpin);
414 if (!cmsbio)
415 goto err;
417 if (!cms_copy_content(out, cmsbio, flags))
418 goto err;
420 if (!(flags & CMS_NO_CONTENT_VERIFY))
422 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
424 si = sk_CMS_SignerInfo_value(sinfos, i);
425 if (!CMS_SignerInfo_verify_content(si, cmsbio))
427 CMSerr(CMS_F_CMS_VERIFY,
428 CMS_R_CONTENT_VERIFY_ERROR);
429 goto err;
434 ret = 1;
436 err:
438 if (dcont && (tmpin == dcont))
439 do_free_upto(cmsbio, dcont);
440 else
441 BIO_free_all(cmsbio);
443 if (cms_certs)
444 sk_X509_pop_free(cms_certs, X509_free);
445 if (crls)
446 sk_X509_CRL_pop_free(crls, X509_CRL_free);
448 return ret;
451 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
452 STACK_OF(X509) *certs,
453 X509_STORE *store, unsigned int flags)
455 int r;
456 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
457 if (r <= 0)
458 return r;
459 return cms_Receipt_verify(rcms, ocms);
462 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
463 BIO *data, unsigned int flags)
465 CMS_ContentInfo *cms;
466 int i;
468 cms = CMS_ContentInfo_new();
469 if (!cms || !CMS_SignedData_init(cms))
470 goto merr;
472 if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags))
474 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
475 goto err;
478 for (i = 0; i < sk_X509_num(certs); i++)
480 X509 *x = sk_X509_value(certs, i);
481 if (!CMS_add1_cert(cms, x))
482 goto merr;
485 if(!(flags & CMS_DETACHED))
487 flags &= ~CMS_STREAM;
488 CMS_set_detached(cms, 0);
491 if ((flags & (CMS_STREAM|CMS_PARTIAL))
492 || CMS_final(cms, data, NULL, flags))
493 return cms;
494 else
495 goto err;
497 merr:
498 CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
500 err:
501 if (cms)
502 CMS_ContentInfo_free(cms);
503 return NULL;
506 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
507 X509 *signcert, EVP_PKEY *pkey,
508 STACK_OF(X509) *certs,
509 unsigned int flags)
511 CMS_SignerInfo *rct_si;
512 CMS_ContentInfo *cms = NULL;
513 ASN1_OCTET_STRING **pos, *os;
514 BIO *rct_cont = NULL;
515 int r = 0;
517 flags &= ~CMS_STREAM;
518 /* Not really detached but avoids content being allocated */
519 flags |= CMS_PARTIAL|CMS_BINARY|CMS_DETACHED;
520 if (!pkey || !signcert)
522 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
523 return NULL;
526 /* Initialize signed data */
528 cms = CMS_sign(NULL, NULL, certs, NULL, flags);
529 if (!cms)
530 goto err;
532 /* Set inner content type to signed receipt */
533 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
534 goto err;
536 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
537 if (!rct_si)
539 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
540 goto err;
543 os = cms_encode_Receipt(si);
545 if (!os)
546 goto err;
548 /* Set content to digest */
549 rct_cont = BIO_new_mem_buf(os->data, os->length);
550 if (!rct_cont)
551 goto err;
553 /* Add msgSigDigest attribute */
555 if (!cms_msgSigDigest_add1(rct_si, si))
556 goto err;
558 /* Finalize structure */
559 if (!CMS_final(cms, rct_cont, NULL, flags))
560 goto err;
562 /* Set embedded content */
563 pos = CMS_get0_content(cms);
564 *pos = os;
566 r = 1;
568 err:
569 if (rct_cont)
570 BIO_free(rct_cont);
571 if (r)
572 return cms;
573 CMS_ContentInfo_free(cms);
574 return NULL;
578 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
579 const EVP_CIPHER *cipher, unsigned int flags)
581 CMS_ContentInfo *cms;
582 int i;
583 X509 *recip;
584 cms = CMS_EnvelopedData_create(cipher);
585 if (!cms)
586 goto merr;
587 for (i = 0; i < sk_X509_num(certs); i++)
589 recip = sk_X509_value(certs, i);
590 if (!CMS_add1_recipient_cert(cms, recip, flags))
592 CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
593 goto err;
597 if(!(flags & CMS_DETACHED))
599 flags &= ~CMS_STREAM;
600 CMS_set_detached(cms, 0);
603 if ((flags & (CMS_STREAM|CMS_PARTIAL))
604 || CMS_final(cms, data, NULL, flags))
605 return cms;
606 else
607 goto err;
609 merr:
610 CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
611 err:
612 if (cms)
613 CMS_ContentInfo_free(cms);
614 return NULL;
617 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
619 STACK_OF(CMS_RecipientInfo) *ris;
620 CMS_RecipientInfo *ri;
621 int i, r;
622 ris = CMS_get0_RecipientInfos(cms);
623 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
625 ri = sk_CMS_RecipientInfo_value(ris, i);
626 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_TRANS)
627 continue;
628 /* If we have a cert try matching RecipientInfo
629 * otherwise try them all.
631 if (!cert || (CMS_RecipientInfo_ktri_cert_cmp(ri, cert) == 0))
633 CMS_RecipientInfo_set0_pkey(ri, pk);
634 r = CMS_RecipientInfo_decrypt(cms, ri);
635 CMS_RecipientInfo_set0_pkey(ri, NULL);
636 if (r > 0)
637 return 1;
638 if (cert)
640 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
641 CMS_R_DECRYPT_ERROR);
642 return 0;
644 ERR_clear_error();
648 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
649 return 0;
653 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
654 unsigned char *key, size_t keylen,
655 unsigned char *id, size_t idlen)
657 STACK_OF(CMS_RecipientInfo) *ris;
658 CMS_RecipientInfo *ri;
659 int i, r;
660 ris = CMS_get0_RecipientInfos(cms);
661 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
663 ri = sk_CMS_RecipientInfo_value(ris, i);
664 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
665 continue;
667 /* If we have an id try matching RecipientInfo
668 * otherwise try them all.
670 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0))
672 CMS_RecipientInfo_set0_key(ri, key, keylen);
673 r = CMS_RecipientInfo_decrypt(cms, ri);
674 CMS_RecipientInfo_set0_key(ri, NULL, 0);
675 if (r > 0)
676 return 1;
677 if (id)
679 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY,
680 CMS_R_DECRYPT_ERROR);
681 return 0;
683 ERR_clear_error();
687 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
688 return 0;
692 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
693 BIO *dcont, BIO *out,
694 unsigned int flags)
696 int r;
697 BIO *cont;
698 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped)
700 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
701 return 0;
703 if (!dcont && !check_content(cms))
704 return 0;
705 if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
706 return 0;
708 cont = CMS_dataInit(cms, dcont);
709 if (!cont)
710 return 0;
711 r = cms_copy_content(out, cont, flags);
712 do_free_upto(cont, dcont);
713 return r;
716 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
718 BIO *cmsbio;
719 int ret = 0;
720 if (!(cmsbio = CMS_dataInit(cms, dcont)))
722 CMSerr(CMS_F_CMS_FINAL,ERR_R_MALLOC_FAILURE);
723 return 0;
726 SMIME_crlf_copy(data, cmsbio, flags);
728 (void)BIO_flush(cmsbio);
731 if (!CMS_dataFinal(cms, cmsbio))
733 CMSerr(CMS_F_CMS_FINAL,CMS_R_CMS_DATAFINAL_ERROR);
734 goto err;
737 ret = 1;
739 err:
740 do_free_upto(cmsbio, dcont);
742 return ret;
746 #ifdef ZLIB
748 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
749 unsigned int flags)
751 BIO *cont;
752 int r;
753 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData)
755 CMSerr(CMS_F_CMS_UNCOMPRESS,
756 CMS_R_TYPE_NOT_COMPRESSED_DATA);
757 return 0;
760 if (!dcont && !check_content(cms))
761 return 0;
763 cont = CMS_dataInit(cms, dcont);
764 if (!cont)
765 return 0;
766 r = cms_copy_content(out, cont, flags);
767 do_free_upto(cont, dcont);
768 return r;
771 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
773 CMS_ContentInfo *cms;
774 if (comp_nid <= 0)
775 comp_nid = NID_zlib_compression;
776 cms = cms_CompressedData_create(comp_nid);
777 if (!cms)
778 return NULL;
780 if(!(flags & CMS_DETACHED))
782 flags &= ~CMS_STREAM;
783 CMS_set_detached(cms, 0);
786 if (CMS_final(cms, in, NULL, flags))
787 return cms;
789 CMS_ContentInfo_free(cms);
790 return NULL;
793 #else
795 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
796 unsigned int flags)
798 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
799 return 0;
802 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
804 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
805 return NULL;
808 #endif