cms_ver.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2008-2016 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/cms.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. CMS_ContentInfo *cms = 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 CA certificate */
  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 message being verified */
  34. in = BIO_new_file("smout.txt", "r");
  35. if (!in)
  36. goto err;
  37. /* parse message */
  38. cms = SMIME_read_CMS(in, &cont);
  39. if (!cms)
  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 (!CMS_verify(cms, 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. CMS_ContentInfo_free(cms);
  57. X509_free(cacert);
  58. BIO_free(in);
  59. BIO_free(out);
  60. BIO_free(tbio);
  61. return ret;
  62. }