OpenSSL: update to 1.0.1m
[tomato.git] / release / src / router / openssl / demos / smime / smdec.c
blob6814d5199b791d417dde96eb01b801c06df3a82e
1 /* Simple S/MIME signing example */
2 #include <openssl/pem.h>
3 #include <openssl/pkcs7.h>
4 #include <openssl/err.h>
6 int main(int argc, char **argv)
8 BIO *in = NULL, *out = NULL, *tbio = NULL;
9 X509 *rcert = NULL;
10 EVP_PKEY *rkey = NULL;
11 PKCS7 *p7 = NULL;
12 int ret = 1;
14 OpenSSL_add_all_algorithms();
15 ERR_load_crypto_strings();
17 /* Read in recipient certificate and private key */
18 tbio = BIO_new_file("signer.pem", "r");
20 if (!tbio)
21 goto err;
23 rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
25 BIO_reset(tbio);
27 rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
29 if (!rcert || !rkey)
30 goto err;
32 /* Open content being signed */
34 in = BIO_new_file("smencr.txt", "r");
36 if (!in)
37 goto err;
39 /* Sign content */
40 p7 = SMIME_read_PKCS7(in, NULL);
42 if (!p7)
43 goto err;
45 out = BIO_new_file("encrout.txt", "w");
46 if (!out)
47 goto err;
49 /* Decrypt S/MIME message */
50 if (!PKCS7_decrypt(p7, rkey, rcert, out, 0))
51 goto err;
53 ret = 0;
55 err:
57 if (ret) {
58 fprintf(stderr, "Error Signing Data\n");
59 ERR_print_errors_fp(stderr);
62 if (p7)
63 PKCS7_free(p7);
64 if (rcert)
65 X509_free(rcert);
66 if (rkey)
67 EVP_PKEY_free(rkey);
69 if (in)
70 BIO_free(in);
71 if (out)
72 BIO_free(out);
73 if (tbio)
74 BIO_free(tbio);
76 return ret;