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
10 /* Simple S/MIME decryption example */
11 #include <openssl/pem.h>
12 #include <openssl/cms.h>
13 #include <openssl/err.h>
15 int main(int argc, char **argv)
17 BIO *in = NULL, *out = NULL, *tbio = NULL;
19 EVP_PKEY *rkey = NULL;
20 CMS_ContentInfo *cms = NULL;
23 OpenSSL_add_all_algorithms();
24 ERR_load_crypto_strings();
26 /* Read in recipient certificate and private key */
27 tbio = BIO_new_file("signer.pem", "r");
32 rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
36 rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
41 /* Open S/MIME message to decrypt */
43 in = BIO_new_file("smencr.txt", "r");
49 cms = SMIME_read_CMS(in, NULL);
54 out = BIO_new_file("decout.txt", "w");
58 /* Decrypt S/MIME message */
59 if (!CMS_decrypt(cms, rkey, rcert, NULL, out, 0))
67 fprintf(stderr, "Error Decrypting Data\n");
68 ERR_print_errors_fp(stderr);
71 CMS_ContentInfo_free(cms);