1 /* S/MIME detached data decrypt example: rarely done but
2 * should the need arise this is an example....
4 #include <openssl/pem.h>
5 #include <openssl/cms.h>
6 #include <openssl/err.h>
8 int main(int argc, char **argv)
10 BIO *in = NULL, *out = NULL, *tbio = NULL, *dcont = NULL;
12 EVP_PKEY *rkey = NULL;
13 CMS_ContentInfo *cms = NULL;
16 OpenSSL_add_all_algorithms();
17 ERR_load_crypto_strings();
19 /* Read in recipient certificate and private key */
20 tbio = BIO_new_file("signer.pem", "r");
25 rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
29 rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
34 /* Open PEM file containing enveloped data */
36 in = BIO_new_file("smencr.pem", "r");
41 /* Parse PEM content */
42 cms = PEM_read_bio_CMS(in, NULL, 0, NULL);
47 /* Open file containing detached content */
48 dcont = BIO_new_file("smencr.out", "rb");
53 out = BIO_new_file("encrout.txt", "w");
57 /* Decrypt S/MIME message */
58 if (!CMS_decrypt(cms, rkey, rcert, dcont, out, 0))
67 fprintf(stderr, "Error Decrypting Data\n");
68 ERR_print_errors_fp(stderr);
72 CMS_ContentInfo_free(cms);