Guest User

Untitled

a guest
Jul 21st, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. // original: openssl/demos/cms/cms_ver.c
  2. // g++ test_verify.cpp -lcrypto -o test_verify
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. #include <openssl/bio.h>
  8. #include <openssl/err.h>
  9. #include <openssl/ssl.h>
  10. #include <openssl/pkcs7.h>
  11. #include <openssl/safestack.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/x509v3.h> /* X509_PURPOSE_ANY */
  14. #include <openssl/x509_vfy.h>
  15. #include <openssl/cms.h>
  16. #include <openssl/pem.h>
  17.  
  18. std::string get_bio_str(BIO* bio) {
  19. char* data;
  20. BIO_get_mem_data(bio, &data);
  21.  
  22. return data;
  23. }
  24.  
  25. void print_openssl_errors() {
  26. ERR_print_errors_fp(stderr);
  27. }
  28.  
  29. void exit_openssl_error() {
  30. print_openssl_errors();
  31. exit(1);
  32. }
  33.  
  34. int main(int argc, char* argv[]) {
  35. if (argc < 2) {
  36. std::cerr << argv[0] << " target-file" << std::endl;
  37. return 1;
  38. }
  39.  
  40. const char* target_file_path = argv[1];
  41.  
  42. OpenSSL_add_all_algorithms();
  43. ERR_load_crypto_strings();
  44.  
  45. X509_STORE *st = X509_STORE_new();
  46.  
  47. if (!X509_STORE_set_default_paths(st))
  48. exit_openssl_error();
  49.  
  50. BIO *in = BIO_new_file(target_file_path, "r");
  51. if (!in)
  52. exit_openssl_error();
  53.  
  54. BIO *cont = NULL;
  55. CMS_ContentInfo *cms = SMIME_read_CMS(in, &cont);
  56. if (!cms)
  57. exit_openssl_error();
  58.  
  59. BIO *out = BIO_new(BIO_s_mem());
  60. if (!out)
  61. exit_openssl_error();
  62.  
  63. if (!CMS_verify(cms, NULL, st, cont, out, 0))
  64. exit_openssl_error();
  65.  
  66. std::cout << "Verify OK" << std::endl;
  67. std::cout << "---" << std::endl;
  68. std::cout << get_bio_str(out) << std::endl;
  69.  
  70. CMS_ContentInfo_free(cms);
  71. BIO_free(in);
  72. BIO_free(out);
  73.  
  74. return 0;
  75. }
Add Comment
Please, Sign In to add comment