smver.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2007-2023 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. /* 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 = EXIT_FAILURE;
  20. OpenSSL_add_all_algorithms();
  21. ERR_load_crypto_strings();
  22. /* Set up trusted CA certificate store */
  23. st = X509_STORE_new();
  24. if (st == NULL)
  25. goto err;
  26. /* Read in signer certificate and private key */
  27. tbio = BIO_new_file("cacert.pem", "r");
  28. if (tbio == NULL)
  29. goto err;
  30. cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
  31. if (cacert == NULL)
  32. goto err;
  33. if (!X509_STORE_add_cert(st, cacert))
  34. goto err;
  35. /* Open content being signed */
  36. in = BIO_new_file("smout.txt", "r");
  37. if (in == NULL)
  38. goto err;
  39. /* Sign content */
  40. p7 = SMIME_read_PKCS7(in, &cont);
  41. if (p7 == NULL)
  42. goto err;
  43. /* File to output verified content to */
  44. out = BIO_new_file("smver.txt", "w");
  45. if (out == NULL)
  46. goto err;
  47. if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) {
  48. fprintf(stderr, "Verification Failure\n");
  49. goto err;
  50. }
  51. printf("Verification Successful\n");
  52. ret = EXIT_SUCCESS;
  53. err:
  54. if (ret != EXIT_SUCCESS) {
  55. fprintf(stderr, "Error Verifying Data\n");
  56. ERR_print_errors_fp(stderr);
  57. }
  58. X509_STORE_free(st);
  59. PKCS7_free(p7);
  60. X509_free(cacert);
  61. BIO_free(in);
  62. BIO_free(out);
  63. BIO_free(tbio);
  64. return ret;
  65. }