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