xref: /minix3/crypto/external/bsd/openssl/dist/demos/cms/cms_enc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* Simple S/MIME encrypt example */
2ebfedea0SLionel Sambuc #include <openssl/pem.h>
3ebfedea0SLionel Sambuc #include <openssl/cms.h>
4ebfedea0SLionel Sambuc #include <openssl/err.h>
5ebfedea0SLionel Sambuc 
main(int argc,char ** argv)6ebfedea0SLionel Sambuc int main(int argc, char **argv)
7ebfedea0SLionel Sambuc {
8ebfedea0SLionel Sambuc     BIO *in = NULL, *out = NULL, *tbio = NULL;
9ebfedea0SLionel Sambuc     X509 *rcert = NULL;
10ebfedea0SLionel Sambuc     STACK_OF(X509) *recips = NULL;
11ebfedea0SLionel Sambuc     CMS_ContentInfo *cms = NULL;
12ebfedea0SLionel Sambuc     int ret = 1;
13ebfedea0SLionel Sambuc 
14ebfedea0SLionel Sambuc     /*
15ebfedea0SLionel Sambuc      * On OpenSSL 1.0.0 and later only:
16ebfedea0SLionel Sambuc      * for streaming set CMS_STREAM
17ebfedea0SLionel Sambuc      */
18ebfedea0SLionel Sambuc     int flags = CMS_STREAM;
19ebfedea0SLionel Sambuc 
20ebfedea0SLionel Sambuc     OpenSSL_add_all_algorithms();
21ebfedea0SLionel Sambuc     ERR_load_crypto_strings();
22ebfedea0SLionel Sambuc 
23ebfedea0SLionel Sambuc     /* Read in recipient certificate */
24ebfedea0SLionel Sambuc     tbio = BIO_new_file("signer.pem", "r");
25ebfedea0SLionel Sambuc 
26ebfedea0SLionel Sambuc     if (!tbio)
27ebfedea0SLionel Sambuc         goto err;
28ebfedea0SLionel Sambuc 
29ebfedea0SLionel Sambuc     rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
30ebfedea0SLionel Sambuc 
31ebfedea0SLionel Sambuc     if (!rcert)
32ebfedea0SLionel Sambuc         goto err;
33ebfedea0SLionel Sambuc 
34ebfedea0SLionel Sambuc     /* Create recipient STACK and add recipient cert to it */
35ebfedea0SLionel Sambuc     recips = sk_X509_new_null();
36ebfedea0SLionel Sambuc 
37ebfedea0SLionel Sambuc     if (!recips || !sk_X509_push(recips, rcert))
38ebfedea0SLionel Sambuc         goto err;
39ebfedea0SLionel Sambuc 
40*0a6a1f1dSLionel Sambuc     /*
41*0a6a1f1dSLionel Sambuc      * sk_X509_pop_free will free up recipient STACK and its contents so set
42*0a6a1f1dSLionel Sambuc      * rcert to NULL so it isn't freed up twice.
43ebfedea0SLionel Sambuc      */
44ebfedea0SLionel Sambuc     rcert = NULL;
45ebfedea0SLionel Sambuc 
46ebfedea0SLionel Sambuc     /* Open content being encrypted */
47ebfedea0SLionel Sambuc 
48ebfedea0SLionel Sambuc     in = BIO_new_file("encr.txt", "r");
49ebfedea0SLionel Sambuc 
50ebfedea0SLionel Sambuc     if (!in)
51ebfedea0SLionel Sambuc         goto err;
52ebfedea0SLionel Sambuc 
53ebfedea0SLionel Sambuc     /* encrypt content */
54ebfedea0SLionel Sambuc     cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
55ebfedea0SLionel Sambuc 
56ebfedea0SLionel Sambuc     if (!cms)
57ebfedea0SLionel Sambuc         goto err;
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc     out = BIO_new_file("smencr.txt", "w");
60ebfedea0SLionel Sambuc     if (!out)
61ebfedea0SLionel Sambuc         goto err;
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc     /* Write out S/MIME message */
64ebfedea0SLionel Sambuc     if (!SMIME_write_CMS(out, cms, in, flags))
65ebfedea0SLionel Sambuc         goto err;
66ebfedea0SLionel Sambuc 
67ebfedea0SLionel Sambuc     ret = 0;
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc  err:
70ebfedea0SLionel Sambuc 
71*0a6a1f1dSLionel Sambuc     if (ret) {
72ebfedea0SLionel Sambuc         fprintf(stderr, "Error Encrypting Data\n");
73ebfedea0SLionel Sambuc         ERR_print_errors_fp(stderr);
74ebfedea0SLionel Sambuc     }
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc     if (cms)
77ebfedea0SLionel Sambuc         CMS_ContentInfo_free(cms);
78ebfedea0SLionel Sambuc     if (rcert)
79ebfedea0SLionel Sambuc         X509_free(rcert);
80ebfedea0SLionel Sambuc     if (recips)
81ebfedea0SLionel Sambuc         sk_X509_pop_free(recips, X509_free);
82ebfedea0SLionel Sambuc 
83ebfedea0SLionel Sambuc     if (in)
84ebfedea0SLionel Sambuc         BIO_free(in);
85ebfedea0SLionel Sambuc     if (out)
86ebfedea0SLionel Sambuc         BIO_free(out);
87ebfedea0SLionel Sambuc     if (tbio)
88ebfedea0SLionel Sambuc         BIO_free(tbio);
89ebfedea0SLionel Sambuc 
90ebfedea0SLionel Sambuc     return ret;
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc }
93