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