2 * S/MIME detached data decrypt example: rarely done but should the need
3 * arise this is an example....
5 #include <openssl/pem.h>
6 #include <openssl/cms.h>
7 #include <openssl/err.h>
9 int main(int argc, char **argv)
11 BIO *in = NULL, *out = NULL, *tbio = NULL, *dcont = NULL;
13 EVP_PKEY *rkey = NULL;
14 CMS_ContentInfo *cms = NULL;
17 OpenSSL_add_all_algorithms();
18 ERR_load_crypto_strings();
20 /* Read in recipient certificate and private key */
21 tbio = BIO_new_file("signer.pem", "r");
26 rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
30 rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
35 /* Open PEM file containing enveloped data */
37 in = BIO_new_file("smencr.pem", "r");
42 /* Parse PEM content */
43 cms = PEM_read_bio_CMS(in, NULL, 0, NULL);
48 /* Open file containing detached content */
49 dcont = BIO_new_file("smencr.out", "rb");
54 out = BIO_new_file("encrout.txt", "w");
58 /* Decrypt S/MIME message */
59 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);