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