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