1635165faSspz /*
2c7da899bSchristos * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
3c7da899bSchristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5c7da899bSchristos * this file except in compliance with the License. You can obtain a copy
6c7da899bSchristos * in the file LICENSE in the source distribution or at
7c7da899bSchristos * https://www.openssl.org/source/license.html
8c7da899bSchristos */
9c7da899bSchristos
10c7da899bSchristos /*
11635165faSspz * S/MIME detached data encrypt example: rarely done but should the need
12635165faSspz * arise this is an example....
13a89c9211Schristos */
14a89c9211Schristos #include <openssl/pem.h>
15a89c9211Schristos #include <openssl/cms.h>
16a89c9211Schristos #include <openssl/err.h>
17a89c9211Schristos
main(int argc,char ** argv)18a89c9211Schristos int main(int argc, char **argv)
19a89c9211Schristos {
20a89c9211Schristos BIO *in = NULL, *out = NULL, *tbio = NULL, *dout = NULL;
21a89c9211Schristos X509 *rcert = NULL;
22a89c9211Schristos STACK_OF(X509) *recips = NULL;
23a89c9211Schristos CMS_ContentInfo *cms = NULL;
24a89c9211Schristos int ret = 1;
25a89c9211Schristos
26a89c9211Schristos int flags = CMS_STREAM | CMS_DETACHED;
27a89c9211Schristos
28a89c9211Schristos OpenSSL_add_all_algorithms();
29a89c9211Schristos ERR_load_crypto_strings();
30a89c9211Schristos
31a89c9211Schristos /* Read in recipient certificate */
32a89c9211Schristos tbio = BIO_new_file("signer.pem", "r");
33a89c9211Schristos
34a89c9211Schristos if (!tbio)
35a89c9211Schristos goto err;
36a89c9211Schristos
37a89c9211Schristos rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
38a89c9211Schristos
39a89c9211Schristos if (!rcert)
40a89c9211Schristos goto err;
41a89c9211Schristos
42a89c9211Schristos /* Create recipient STACK and add recipient cert to it */
43a89c9211Schristos recips = sk_X509_new_null();
44a89c9211Schristos
45a89c9211Schristos if (!recips || !sk_X509_push(recips, rcert))
46a89c9211Schristos goto err;
47a89c9211Schristos
48635165faSspz /*
49635165faSspz * sk_X509_pop_free will free up recipient STACK and its contents so set
50635165faSspz * rcert to NULL so it isn't freed up twice.
51a89c9211Schristos */
52a89c9211Schristos rcert = NULL;
53a89c9211Schristos
54a89c9211Schristos /* Open content being encrypted */
55a89c9211Schristos
56a89c9211Schristos in = BIO_new_file("encr.txt", "r");
57a89c9211Schristos
58a89c9211Schristos dout = BIO_new_file("smencr.out", "wb");
59a89c9211Schristos
60a89c9211Schristos if (!in)
61a89c9211Schristos goto err;
62a89c9211Schristos
63a89c9211Schristos /* encrypt content */
64a89c9211Schristos cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
65a89c9211Schristos
66a89c9211Schristos if (!cms)
67a89c9211Schristos goto err;
68a89c9211Schristos
69a89c9211Schristos out = BIO_new_file("smencr.pem", "w");
70a89c9211Schristos if (!out)
71a89c9211Schristos goto err;
72a89c9211Schristos
73a89c9211Schristos if (!CMS_final(cms, in, dout, flags))
74a89c9211Schristos goto err;
75a89c9211Schristos
76a89c9211Schristos /* Write out CMS structure without content */
77a89c9211Schristos if (!PEM_write_bio_CMS(out, cms))
78a89c9211Schristos goto err;
79a89c9211Schristos
80a89c9211Schristos ret = 0;
81a89c9211Schristos
82a89c9211Schristos err:
83a89c9211Schristos
84635165faSspz if (ret) {
85a89c9211Schristos fprintf(stderr, "Error Encrypting Data\n");
86a89c9211Schristos ERR_print_errors_fp(stderr);
87a89c9211Schristos }
88a89c9211Schristos
89a89c9211Schristos CMS_ContentInfo_free(cms);
90a89c9211Schristos X509_free(rcert);
91a89c9211Schristos sk_X509_pop_free(recips, X509_free);
92a89c9211Schristos BIO_free(in);
93a89c9211Schristos BIO_free(out);
94a89c9211Schristos BIO_free(dout);
95a89c9211Schristos BIO_free(tbio);
96a89c9211Schristos return ret;
97a89c9211Schristos }
98