1 /* Simple S/MIME encrypt example */
2 #include <openssl/pem.h>
3 #include <openssl/cms.h>
4 #include <openssl/err.h>
6 int main(int argc, char **argv)
8 BIO *in = NULL, *out = NULL, *tbio = NULL;
10 STACK_OF(X509) *recips = NULL;
11 CMS_ContentInfo *cms = NULL;
15 * On OpenSSL 1.0.0 and later only:
16 * for streaming set CMS_STREAM
18 int flags = CMS_STREAM;
20 OpenSSL_add_all_algorithms();
21 ERR_load_crypto_strings();
23 /* Read in recipient certificate */
24 tbio = BIO_new_file("signer.pem", "r");
29 rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
34 /* Create recipient STACK and add recipient cert to it */
35 recips = sk_X509_new_null();
37 if (!recips || !sk_X509_push(recips, rcert))
40 /* sk_X509_pop_free will free up recipient STACK and its contents
41 * so set rcert to NULL so it isn't freed up twice.
45 /* Open content being encrypted */
47 in = BIO_new_file("encr.txt", "r");
53 cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
58 out = BIO_new_file("smencr.txt", "w");
62 /* Write out S/MIME message */
63 if (!SMIME_write_CMS(out, cms, in, flags))
72 fprintf(stderr, "Error Encrypting Data\n");
73 ERR_print_errors_fp(stderr);
77 CMS_ContentInfo_free(cms);
81 sk_X509_pop_free(recips, X509_free);