lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (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/pkcs7.h> |
| 13 | #include <openssl/err.h> |
| 14 | |
| 15 | int 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 | PKCS7 *p7 = 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 | |
| 31 | /* Read in signer certificate and private key */ |
| 32 | tbio = BIO_new_file("cacert.pem", "r"); |
| 33 | |
| 34 | if (!tbio) |
| 35 | goto err; |
| 36 | |
| 37 | cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL); |
| 38 | |
| 39 | if (!cacert) |
| 40 | goto err; |
| 41 | |
| 42 | if (!X509_STORE_add_cert(st, cacert)) |
| 43 | goto err; |
| 44 | |
| 45 | /* Open content being signed */ |
| 46 | |
| 47 | in = BIO_new_file("smout.txt", "r"); |
| 48 | |
| 49 | if (!in) |
| 50 | goto err; |
| 51 | |
| 52 | /* Sign content */ |
| 53 | p7 = SMIME_read_PKCS7(in, &cont); |
| 54 | |
| 55 | if (!p7) |
| 56 | goto err; |
| 57 | |
| 58 | /* File to output verified content to */ |
| 59 | out = BIO_new_file("smver.txt", "w"); |
| 60 | if (!out) |
| 61 | goto err; |
| 62 | |
| 63 | if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) { |
| 64 | fprintf(stderr, "Verification Failure\n"); |
| 65 | goto err; |
| 66 | } |
| 67 | |
| 68 | fprintf(stderr, "Verification Successful\n"); |
| 69 | |
| 70 | ret = 0; |
| 71 | |
| 72 | err: |
| 73 | if (ret) { |
| 74 | fprintf(stderr, "Error Verifying Data\n"); |
| 75 | ERR_print_errors_fp(stderr); |
| 76 | } |
| 77 | PKCS7_free(p7); |
| 78 | X509_free(cacert); |
| 79 | BIO_free(in); |
| 80 | BIO_free(out); |
| 81 | BIO_free(tbio); |
| 82 | return ret; |
| 83 | } |