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