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