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