2
0

smver.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /* Simple S/MIME verification example */
  10. #include <openssl/pem.h>
  11. #include <openssl/pkcs7.h>
  12. #include <openssl/err.h>
  13. int main(int argc, char **argv)
  14. {
  15. BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL;
  16. X509_STORE *st = NULL;
  17. X509 *cacert = NULL;
  18. PKCS7 *p7 = NULL;
  19. int ret = 1;
  20. OpenSSL_add_all_algorithms();
  21. ERR_load_crypto_strings();
  22. /* Set up trusted CA certificate store */
  23. st = X509_STORE_new();
  24. /* Read in signer certificate and private key */
  25. tbio = BIO_new_file("cacert.pem", "r");
  26. if (!tbio)
  27. goto err;
  28. cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
  29. if (!cacert)
  30. goto err;
  31. if (!X509_STORE_add_cert(st, cacert))
  32. goto err;
  33. /* Open content being signed */
  34. in = BIO_new_file("smout.txt", "r");
  35. if (!in)
  36. goto err;
  37. /* Sign content */
  38. p7 = SMIME_read_PKCS7(in, &cont);
  39. if (!p7)
  40. goto err;
  41. /* File to output verified content to */
  42. out = BIO_new_file("smver.txt", "w");
  43. if (!out)
  44. goto err;
  45. if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) {
  46. fprintf(stderr, "Verification Failure\n");
  47. goto err;
  48. }
  49. fprintf(stderr, "Verification Successful\n");
  50. ret = 0;
  51. err:
  52. if (ret) {
  53. fprintf(stderr, "Error Verifying Data\n");
  54. ERR_print_errors_fp(stderr);
  55. }
  56. PKCS7_free(p7);
  57. X509_free(cacert);
  58. BIO_free(in);
  59. BIO_free(out);
  60. BIO_free(tbio);
  61. return ret;
  62. }