xref: /freebsd-src/crypto/openssl/demos/cms/cms_comp.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert /* Simple S/MIME compress example */
11*e0c4386eSCy Schubert #include <openssl/pem.h>
12*e0c4386eSCy Schubert #include <openssl/cms.h>
13*e0c4386eSCy Schubert #include <openssl/err.h>
14*e0c4386eSCy Schubert 
main(int argc,char ** argv)15*e0c4386eSCy Schubert int main(int argc, char **argv)
16*e0c4386eSCy Schubert {
17*e0c4386eSCy Schubert     BIO *in = NULL, *out = NULL;
18*e0c4386eSCy Schubert     CMS_ContentInfo *cms = NULL;
19*e0c4386eSCy Schubert     int ret = 1;
20*e0c4386eSCy Schubert 
21*e0c4386eSCy Schubert     /*
22*e0c4386eSCy Schubert      * On OpenSSL 1.0.0+ only:
23*e0c4386eSCy Schubert      * for streaming set CMS_STREAM
24*e0c4386eSCy Schubert      */
25*e0c4386eSCy Schubert     int flags = CMS_STREAM;
26*e0c4386eSCy Schubert 
27*e0c4386eSCy Schubert     OpenSSL_add_all_algorithms();
28*e0c4386eSCy Schubert     ERR_load_crypto_strings();
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert     /* Open content being compressed */
31*e0c4386eSCy Schubert 
32*e0c4386eSCy Schubert     in = BIO_new_file("comp.txt", "r");
33*e0c4386eSCy Schubert 
34*e0c4386eSCy Schubert     if (!in)
35*e0c4386eSCy Schubert         goto err;
36*e0c4386eSCy Schubert 
37*e0c4386eSCy Schubert     /* compress content */
38*e0c4386eSCy Schubert     cms = CMS_compress(in, NID_zlib_compression, flags);
39*e0c4386eSCy Schubert 
40*e0c4386eSCy Schubert     if (!cms)
41*e0c4386eSCy Schubert         goto err;
42*e0c4386eSCy Schubert 
43*e0c4386eSCy Schubert     out = BIO_new_file("smcomp.txt", "w");
44*e0c4386eSCy Schubert     if (!out)
45*e0c4386eSCy Schubert         goto err;
46*e0c4386eSCy Schubert 
47*e0c4386eSCy Schubert     /* Write out S/MIME message */
48*e0c4386eSCy Schubert     if (!SMIME_write_CMS(out, cms, in, flags))
49*e0c4386eSCy Schubert         goto err;
50*e0c4386eSCy Schubert 
51*e0c4386eSCy Schubert     ret = 0;
52*e0c4386eSCy Schubert 
53*e0c4386eSCy Schubert  err:
54*e0c4386eSCy Schubert 
55*e0c4386eSCy Schubert     if (ret) {
56*e0c4386eSCy Schubert         fprintf(stderr, "Error Compressing Data\n");
57*e0c4386eSCy Schubert         ERR_print_errors_fp(stderr);
58*e0c4386eSCy Schubert     }
59*e0c4386eSCy Schubert 
60*e0c4386eSCy Schubert     CMS_ContentInfo_free(cms);
61*e0c4386eSCy Schubert     BIO_free(in);
62*e0c4386eSCy Schubert     BIO_free(out);
63*e0c4386eSCy Schubert     return ret;
64*e0c4386eSCy Schubert }
65