2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * S/MIME detached data encrypt example: rarely done but should the need
12 * arise this is an example....
14 #include <openssl/pem.h>
15 #include <openssl/cms.h>
16 #include <openssl/err.h>
18 int main(int argc, char **argv)
20 BIO *in = NULL, *out = NULL, *tbio = NULL, *dout = NULL;
22 STACK_OF(X509) *recips = NULL;
23 CMS_ContentInfo *cms = NULL;
26 int flags = CMS_STREAM | CMS_DETACHED;
28 OpenSSL_add_all_algorithms();
29 ERR_load_crypto_strings();
31 /* Read in recipient certificate */
32 tbio = BIO_new_file("signer.pem", "r");
37 rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
42 /* Create recipient STACK and add recipient cert to it */
43 recips = sk_X509_new_null();
45 if (!recips || !sk_X509_push(recips, rcert))
49 * sk_X509_pop_free will free up recipient STACK and its contents so set
50 * rcert to NULL so it isn't freed up twice.
54 /* Open content being encrypted */
56 in = BIO_new_file("encr.txt", "r");
58 dout = BIO_new_file("smencr.out", "wb");
64 cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
69 out = BIO_new_file("smencr.pem", "w");
73 if (!CMS_final(cms, in, dout, flags))
76 /* Write out CMS structure without content */
77 if (!PEM_write_bio_CMS(out, cms))
85 fprintf(stderr, "Error Encrypting Data\n");
86 ERR_print_errors_fp(stderr);
89 CMS_ContentInfo_free(cms);
91 sk_X509_pop_free(recips, X509_free);