1 | /*
|
---|
2 | * Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (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 |
|
---|
10 | /* Simple S/MIME verification example */
|
---|
11 | #include <openssl/pem.h>
|
---|
12 | #include <openssl/pkcs7.h>
|
---|
13 | #include <openssl/err.h>
|
---|
14 |
|
---|
15 | int main(int argc, char **argv)
|
---|
16 | {
|
---|
17 | BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL;
|
---|
18 | X509_STORE *st = NULL;
|
---|
19 | X509 *cacert = NULL;
|
---|
20 | PKCS7 *p7 = NULL;
|
---|
21 |
|
---|
22 | int ret = 1;
|
---|
23 |
|
---|
24 | OpenSSL_add_all_algorithms();
|
---|
25 | ERR_load_crypto_strings();
|
---|
26 |
|
---|
27 | /* Set up trusted CA certificate store */
|
---|
28 |
|
---|
29 | st = X509_STORE_new();
|
---|
30 | if (st == NULL)
|
---|
31 | goto err;
|
---|
32 |
|
---|
33 | /* Read in signer certificate and private key */
|
---|
34 | tbio = BIO_new_file("cacert.pem", "r");
|
---|
35 |
|
---|
36 | if (tbio == NULL)
|
---|
37 | goto err;
|
---|
38 |
|
---|
39 | cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
|
---|
40 |
|
---|
41 | if (cacert == NULL)
|
---|
42 | goto err;
|
---|
43 |
|
---|
44 | if (!X509_STORE_add_cert(st, cacert))
|
---|
45 | goto err;
|
---|
46 |
|
---|
47 | /* Open content being signed */
|
---|
48 |
|
---|
49 | in = BIO_new_file("smout.txt", "r");
|
---|
50 |
|
---|
51 | if (in == NULL)
|
---|
52 | goto err;
|
---|
53 |
|
---|
54 | /* Sign content */
|
---|
55 | p7 = SMIME_read_PKCS7(in, &cont);
|
---|
56 |
|
---|
57 | if (p7 == NULL)
|
---|
58 | goto err;
|
---|
59 |
|
---|
60 | /* File to output verified content to */
|
---|
61 | out = BIO_new_file("smver.txt", "w");
|
---|
62 | if (out == NULL)
|
---|
63 | goto err;
|
---|
64 |
|
---|
65 | if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) {
|
---|
66 | fprintf(stderr, "Verification Failure\n");
|
---|
67 | goto err;
|
---|
68 | }
|
---|
69 |
|
---|
70 | fprintf(stderr, "Verification Successful\n");
|
---|
71 |
|
---|
72 | ret = 0;
|
---|
73 |
|
---|
74 | err:
|
---|
75 | if (ret) {
|
---|
76 | fprintf(stderr, "Error Verifying Data\n");
|
---|
77 | ERR_print_errors_fp(stderr);
|
---|
78 | }
|
---|
79 |
|
---|
80 | X509_STORE_free(st);
|
---|
81 | PKCS7_free(p7);
|
---|
82 | X509_free(cacert);
|
---|
83 | BIO_free(in);
|
---|
84 | BIO_free(out);
|
---|
85 | BIO_free(tbio);
|
---|
86 | return ret;
|
---|
87 | }
|
---|