]>
Commit | Line | Data |
---|---|---|
9e200689 RS |
1 | /* |
2 | * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
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 | |
8 | */ | |
9 | ||
f3eba36c | 10 | /* Simple S/MIME decryption example */ |
3b28bc99 DSH |
11 | #include <openssl/pem.h> |
12 | #include <openssl/cms.h> | |
13 | #include <openssl/err.h> | |
14 | ||
15 | int main(int argc, char **argv) | |
0f113f3e MC |
16 | { |
17 | BIO *in = NULL, *out = NULL, *tbio = NULL; | |
18 | X509 *rcert = NULL; | |
19 | EVP_PKEY *rkey = NULL; | |
20 | CMS_ContentInfo *cms = NULL; | |
21 | int ret = 1; | |
3b28bc99 | 22 | |
0f113f3e MC |
23 | OpenSSL_add_all_algorithms(); |
24 | ERR_load_crypto_strings(); | |
3b28bc99 | 25 | |
0f113f3e MC |
26 | /* Read in recipient certificate and private key */ |
27 | tbio = BIO_new_file("signer.pem", "r"); | |
3b28bc99 | 28 | |
0f113f3e MC |
29 | if (!tbio) |
30 | goto err; | |
3b28bc99 | 31 | |
0f113f3e | 32 | rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); |
3b28bc99 | 33 | |
0f113f3e | 34 | BIO_reset(tbio); |
3b28bc99 | 35 | |
0f113f3e | 36 | rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); |
3b28bc99 | 37 | |
0f113f3e MC |
38 | if (!rcert || !rkey) |
39 | goto err; | |
3b28bc99 | 40 | |
0f113f3e | 41 | /* Open S/MIME message to decrypt */ |
3b28bc99 | 42 | |
0f113f3e | 43 | in = BIO_new_file("smencr.txt", "r"); |
3b28bc99 | 44 | |
0f113f3e MC |
45 | if (!in) |
46 | goto err; | |
3b28bc99 | 47 | |
0f113f3e MC |
48 | /* Parse message */ |
49 | cms = SMIME_read_CMS(in, NULL); | |
3b28bc99 | 50 | |
0f113f3e MC |
51 | if (!cms) |
52 | goto err; | |
3b28bc99 | 53 | |
0f113f3e MC |
54 | out = BIO_new_file("decout.txt", "w"); |
55 | if (!out) | |
56 | goto err; | |
3b28bc99 | 57 | |
0f113f3e MC |
58 | /* Decrypt S/MIME message */ |
59 | if (!CMS_decrypt(cms, rkey, rcert, NULL, out, 0)) | |
60 | goto err; | |
3b28bc99 | 61 | |
0f113f3e | 62 | ret = 0; |
3b28bc99 | 63 | |
0f113f3e | 64 | err: |
3b28bc99 | 65 | |
0f113f3e MC |
66 | if (ret) { |
67 | fprintf(stderr, "Error Decrypting Data\n"); | |
68 | ERR_print_errors_fp(stderr); | |
69 | } | |
3b28bc99 | 70 | |
25aaa98a | 71 | CMS_ContentInfo_free(cms); |
222561fe | 72 | X509_free(rcert); |
c5ba2d99 | 73 | EVP_PKEY_free(rkey); |
ca3a82c3 RS |
74 | BIO_free(in); |
75 | BIO_free(out); | |
76 | BIO_free(tbio); | |
0f113f3e | 77 | return ret; |
0f113f3e | 78 | } |