1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2007-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 encrypt example */
11*e0c4386eSCy Schubert #include <openssl/pem.h>
12*e0c4386eSCy Schubert #include <openssl/pkcs7.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, *tbio = NULL;
18*e0c4386eSCy Schubert X509 *rcert = NULL;
19*e0c4386eSCy Schubert STACK_OF(X509) *recips = NULL;
20*e0c4386eSCy Schubert PKCS7 *p7 = NULL;
21*e0c4386eSCy Schubert int ret = 1;
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubert /*
24*e0c4386eSCy Schubert * On OpenSSL 0.9.9 only:
25*e0c4386eSCy Schubert * for streaming set PKCS7_STREAM
26*e0c4386eSCy Schubert */
27*e0c4386eSCy Schubert int flags = PKCS7_STREAM;
28*e0c4386eSCy Schubert
29*e0c4386eSCy Schubert OpenSSL_add_all_algorithms();
30*e0c4386eSCy Schubert ERR_load_crypto_strings();
31*e0c4386eSCy Schubert
32*e0c4386eSCy Schubert /* Read in recipient certificate */
33*e0c4386eSCy Schubert tbio = BIO_new_file("signer.pem", "r");
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert if (!tbio)
36*e0c4386eSCy Schubert goto err;
37*e0c4386eSCy Schubert
38*e0c4386eSCy Schubert rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubert if (!rcert)
41*e0c4386eSCy Schubert goto err;
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert /* Create recipient STACK and add recipient cert to it */
44*e0c4386eSCy Schubert recips = sk_X509_new_null();
45*e0c4386eSCy Schubert
46*e0c4386eSCy Schubert if (!recips || !sk_X509_push(recips, rcert))
47*e0c4386eSCy Schubert goto err;
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert /*
50*e0c4386eSCy Schubert * sk_X509_pop_free will free up recipient STACK and its contents so set
51*e0c4386eSCy Schubert * rcert to NULL so it isn't freed up twice.
52*e0c4386eSCy Schubert */
53*e0c4386eSCy Schubert rcert = NULL;
54*e0c4386eSCy Schubert
55*e0c4386eSCy Schubert /* Open content being encrypted */
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubert in = BIO_new_file("encr.txt", "r");
58*e0c4386eSCy Schubert
59*e0c4386eSCy Schubert if (!in)
60*e0c4386eSCy Schubert goto err;
61*e0c4386eSCy Schubert
62*e0c4386eSCy Schubert /* encrypt content */
63*e0c4386eSCy Schubert p7 = PKCS7_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
64*e0c4386eSCy Schubert
65*e0c4386eSCy Schubert if (!p7)
66*e0c4386eSCy Schubert goto err;
67*e0c4386eSCy Schubert
68*e0c4386eSCy Schubert out = BIO_new_file("smencr.txt", "w");
69*e0c4386eSCy Schubert if (!out)
70*e0c4386eSCy Schubert goto err;
71*e0c4386eSCy Schubert
72*e0c4386eSCy Schubert /* Write out S/MIME message */
73*e0c4386eSCy Schubert if (!SMIME_write_PKCS7(out, p7, in, flags))
74*e0c4386eSCy Schubert goto err;
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubert ret = 0;
77*e0c4386eSCy Schubert
78*e0c4386eSCy Schubert err:
79*e0c4386eSCy Schubert if (ret) {
80*e0c4386eSCy Schubert fprintf(stderr, "Error Encrypting Data\n");
81*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
82*e0c4386eSCy Schubert }
83*e0c4386eSCy Schubert PKCS7_free(p7);
84*e0c4386eSCy Schubert X509_free(rcert);
85*e0c4386eSCy Schubert sk_X509_pop_free(recips, X509_free);
86*e0c4386eSCy Schubert BIO_free(in);
87*e0c4386eSCy Schubert BIO_free(out);
88*e0c4386eSCy Schubert BIO_free(tbio);
89*e0c4386eSCy Schubert return ret;
90*e0c4386eSCy Schubert
91*e0c4386eSCy Schubert }
92