]> git.ipfire.org Git - thirdparty/openssl.git/blame - demos/smime/smver.c
Run util/openssl-format-source -v -c .
[thirdparty/openssl.git] / demos / smime / smver.c
CommitLineData
b2b2dafc
DSH
1/* Simple S/MIME verification example */
2#include <openssl/pem.h>
3#include <openssl/pkcs7.h>
4#include <openssl/err.h>
5
6int main(int argc, char **argv)
0f113f3e
MC
7{
8 BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL;
9 X509_STORE *st = NULL;
10 X509 *cacert = NULL;
11 PKCS7 *p7 = NULL;
b2b2dafc 12
0f113f3e 13 int ret = 1;
b2b2dafc 14
0f113f3e
MC
15 OpenSSL_add_all_algorithms();
16 ERR_load_crypto_strings();
b2b2dafc 17
0f113f3e 18 /* Set up trusted CA certificate store */
b2b2dafc 19
0f113f3e 20 st = X509_STORE_new();
b2b2dafc 21
0f113f3e
MC
22 /* Read in signer certificate and private key */
23 tbio = BIO_new_file("cacert.pem", "r");
b2b2dafc 24
0f113f3e
MC
25 if (!tbio)
26 goto err;
b2b2dafc 27
0f113f3e 28 cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
b2b2dafc 29
0f113f3e
MC
30 if (!cacert)
31 goto err;
b2b2dafc 32
0f113f3e
MC
33 if (!X509_STORE_add_cert(st, cacert))
34 goto err;
b2b2dafc 35
0f113f3e 36 /* Open content being signed */
b2b2dafc 37
0f113f3e 38 in = BIO_new_file("smout.txt", "r");
b2b2dafc 39
0f113f3e
MC
40 if (!in)
41 goto err;
b2b2dafc 42
0f113f3e
MC
43 /* Sign content */
44 p7 = SMIME_read_PKCS7(in, &cont);
b2b2dafc 45
0f113f3e
MC
46 if (!p7)
47 goto err;
b2b2dafc 48
0f113f3e
MC
49 /* File to output verified content to */
50 out = BIO_new_file("smver.txt", "w");
51 if (!out)
52 goto err;
b2b2dafc 53
0f113f3e
MC
54 if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) {
55 fprintf(stderr, "Verification Failure\n");
56 goto err;
57 }
b2b2dafc 58
0f113f3e 59 fprintf(stderr, "Verification Successful\n");
b2b2dafc 60
0f113f3e 61 ret = 0;
b2b2dafc 62
0f113f3e 63 err:
b2b2dafc 64
0f113f3e
MC
65 if (ret) {
66 fprintf(stderr, "Error Verifying Data\n");
67 ERR_print_errors_fp(stderr);
68 }
b2b2dafc 69
0f113f3e
MC
70 if (p7)
71 PKCS7_free(p7);
b2b2dafc 72
0f113f3e
MC
73 if (cacert)
74 X509_free(cacert);
b2b2dafc 75
0f113f3e
MC
76 if (in)
77 BIO_free(in);
78 if (out)
79 BIO_free(out);
80 if (tbio)
81 BIO_free(tbio);
b2b2dafc 82
0f113f3e 83 return ret;
b2b2dafc 84
0f113f3e 85}