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