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