xref: /third_party/openssl/demos/cms/cms_ver.c (revision e1051a39)
1/*
2 * Copyright 2008-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/cms.h>
13#include <openssl/err.h>
14
15int 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    CMS_ContentInfo *cms = 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 CA certificate */
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 message being verified */
48
49    in = BIO_new_file("smout.txt", "r");
50
51    if (in == NULL)
52        goto err;
53
54    /* parse message */
55    cms = SMIME_read_CMS(in, &cont);
56
57    if (cms == 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 (!CMS_verify(cms, 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
76    if (ret) {
77        fprintf(stderr, "Error Verifying Data\n");
78        ERR_print_errors_fp(stderr);
79    }
80
81    X509_STORE_free(st);
82    CMS_ContentInfo_free(cms);
83    X509_free(cacert);
84    BIO_free(in);
85    BIO_free(out);
86    BIO_free(tbio);
87    return ret;
88}
89