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