Path: blob/main/crypto/openssl/demos/smime/smver.c
34889 views
/*1* Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/* Simple S/MIME verification example */10#include <openssl/pem.h>11#include <openssl/pkcs7.h>12#include <openssl/err.h>1314int main(int argc, char **argv)15{16BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL;17X509_STORE *st = NULL;18X509 *cacert = NULL;19PKCS7 *p7 = NULL;20int ret = EXIT_FAILURE;2122OpenSSL_add_all_algorithms();23ERR_load_crypto_strings();2425/* Set up trusted CA certificate store */2627st = X509_STORE_new();28if (st == NULL)29goto err;3031/* Read in signer certificate and private key */32tbio = BIO_new_file("cacert.pem", "r");3334if (tbio == NULL)35goto err;3637cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);3839if (cacert == NULL)40goto err;4142if (!X509_STORE_add_cert(st, cacert))43goto err;4445/* Open content being signed */4647in = BIO_new_file("smout.txt", "r");4849if (in == NULL)50goto err;5152/* Sign content */53p7 = SMIME_read_PKCS7(in, &cont);5455if (p7 == NULL)56goto err;5758/* File to output verified content to */59out = BIO_new_file("smver.txt", "w");60if (out == NULL)61goto err;6263if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) {64fprintf(stderr, "Verification Failure\n");65goto err;66}6768printf("Verification Successful\n");6970ret = EXIT_SUCCESS;71err:72if (ret != EXIT_SUCCESS) {73fprintf(stderr, "Error Verifying Data\n");74ERR_print_errors_fp(stderr);75}7677X509_STORE_free(st);78PKCS7_free(p7);79X509_free(cacert);80BIO_free(in);81BIO_free(out);82BIO_free(tbio);83return ret;84}858687