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