1*0a6a1f1dSLionel Sambuc /*
2*0a6a1f1dSLionel Sambuc * S/MIME detached data decrypt example: rarely done but should the need
3*0a6a1f1dSLionel Sambuc * arise this is an example....
4ebfedea0SLionel Sambuc */
5ebfedea0SLionel Sambuc #include <openssl/pem.h>
6ebfedea0SLionel Sambuc #include <openssl/cms.h>
7ebfedea0SLionel Sambuc #include <openssl/err.h>
8ebfedea0SLionel Sambuc
main(int argc,char ** argv)9ebfedea0SLionel Sambuc int main(int argc, char **argv)
10ebfedea0SLionel Sambuc {
11ebfedea0SLionel Sambuc BIO *in = NULL, *out = NULL, *tbio = NULL, *dcont = NULL;
12ebfedea0SLionel Sambuc X509 *rcert = NULL;
13ebfedea0SLionel Sambuc EVP_PKEY *rkey = NULL;
14ebfedea0SLionel Sambuc CMS_ContentInfo *cms = NULL;
15ebfedea0SLionel Sambuc int ret = 1;
16ebfedea0SLionel Sambuc
17ebfedea0SLionel Sambuc OpenSSL_add_all_algorithms();
18ebfedea0SLionel Sambuc ERR_load_crypto_strings();
19ebfedea0SLionel Sambuc
20ebfedea0SLionel Sambuc /* Read in recipient certificate and private key */
21ebfedea0SLionel Sambuc tbio = BIO_new_file("signer.pem", "r");
22ebfedea0SLionel Sambuc
23ebfedea0SLionel Sambuc if (!tbio)
24ebfedea0SLionel Sambuc goto err;
25ebfedea0SLionel Sambuc
26ebfedea0SLionel Sambuc rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
27ebfedea0SLionel Sambuc
28ebfedea0SLionel Sambuc BIO_reset(tbio);
29ebfedea0SLionel Sambuc
30ebfedea0SLionel Sambuc rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
31ebfedea0SLionel Sambuc
32ebfedea0SLionel Sambuc if (!rcert || !rkey)
33ebfedea0SLionel Sambuc goto err;
34ebfedea0SLionel Sambuc
35ebfedea0SLionel Sambuc /* Open PEM file containing enveloped data */
36ebfedea0SLionel Sambuc
37ebfedea0SLionel Sambuc in = BIO_new_file("smencr.pem", "r");
38ebfedea0SLionel Sambuc
39ebfedea0SLionel Sambuc if (!in)
40ebfedea0SLionel Sambuc goto err;
41ebfedea0SLionel Sambuc
42ebfedea0SLionel Sambuc /* Parse PEM content */
43ebfedea0SLionel Sambuc cms = PEM_read_bio_CMS(in, NULL, 0, NULL);
44ebfedea0SLionel Sambuc
45ebfedea0SLionel Sambuc if (!cms)
46ebfedea0SLionel Sambuc goto err;
47ebfedea0SLionel Sambuc
48ebfedea0SLionel Sambuc /* Open file containing detached content */
49ebfedea0SLionel Sambuc dcont = BIO_new_file("smencr.out", "rb");
50ebfedea0SLionel Sambuc
51ebfedea0SLionel Sambuc if (!in)
52ebfedea0SLionel Sambuc goto err;
53ebfedea0SLionel Sambuc
54ebfedea0SLionel Sambuc out = BIO_new_file("encrout.txt", "w");
55ebfedea0SLionel Sambuc if (!out)
56ebfedea0SLionel Sambuc goto err;
57ebfedea0SLionel Sambuc
58ebfedea0SLionel Sambuc /* Decrypt S/MIME message */
59ebfedea0SLionel Sambuc if (!CMS_decrypt(cms, rkey, rcert, dcont, out, 0))
60ebfedea0SLionel Sambuc goto err;
61ebfedea0SLionel Sambuc
62ebfedea0SLionel Sambuc ret = 0;
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc err:
65ebfedea0SLionel Sambuc
66*0a6a1f1dSLionel Sambuc if (ret) {
67ebfedea0SLionel Sambuc fprintf(stderr, "Error Decrypting Data\n");
68ebfedea0SLionel Sambuc ERR_print_errors_fp(stderr);
69ebfedea0SLionel Sambuc }
70ebfedea0SLionel Sambuc
71ebfedea0SLionel Sambuc if (cms)
72ebfedea0SLionel Sambuc CMS_ContentInfo_free(cms);
73ebfedea0SLionel Sambuc if (rcert)
74ebfedea0SLionel Sambuc X509_free(rcert);
75ebfedea0SLionel Sambuc if (rkey)
76ebfedea0SLionel Sambuc EVP_PKEY_free(rkey);
77ebfedea0SLionel Sambuc
78ebfedea0SLionel Sambuc if (in)
79ebfedea0SLionel Sambuc BIO_free(in);
80ebfedea0SLionel Sambuc if (out)
81ebfedea0SLionel Sambuc BIO_free(out);
82ebfedea0SLionel Sambuc if (tbio)
83ebfedea0SLionel Sambuc BIO_free(tbio);
84ebfedea0SLionel Sambuc if (dcont)
85ebfedea0SLionel Sambuc BIO_free(dcont);
86ebfedea0SLionel Sambuc
87ebfedea0SLionel Sambuc return ret;
88ebfedea0SLionel Sambuc
89ebfedea0SLionel Sambuc }
90