cms_ver.c 1.4 KB

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