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 decrypt 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, *dcont = NULL;
22 EVP_PKEY *rkey = NULL;
23 CMS_ContentInfo *cms = NULL;
26 OpenSSL_add_all_algorithms();
27 ERR_load_crypto_strings();
29 /* Read in recipient certificate and private key */
30 tbio = BIO_new_file("signer.pem", "r");
35 rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
39 rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
44 /* Open PEM file containing enveloped data */
46 in = BIO_new_file("smencr.pem", "r");
51 /* Parse PEM content */
52 cms = PEM_read_bio_CMS(in, NULL, 0, NULL);
57 /* Open file containing detached content */
58 dcont = BIO_new_file("smencr.out", "rb");
63 out = BIO_new_file("encrout.txt", "w");
67 /* Decrypt S/MIME message */
68 if (!CMS_decrypt(cms, rkey, rcert, dcont, out, 0))
76 fprintf(stderr, "Error Decrypting Data\n");
77 ERR_print_errors_fp(stderr);
80 CMS_ContentInfo_free(cms);