Skip to content

Instantly share code, notes, and snippets.

@ykon
Created July 21, 2018 11:04
Show Gist options
  • Save ykon/13e2bf39423a0f8dd654cb0eb5b46ded to your computer and use it in GitHub Desktop.
Save ykon/13e2bf39423a0f8dd654cb0eb5b46ded to your computer and use it in GitHub Desktop.
CMS Verify
// original: openssl/demos/cms/cms_ver.c
// g++ test_verify.cpp -lcrypto -o test_verify
#include <iostream>
#include <string>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/pkcs7.h>
#include <openssl/safestack.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h> /* X509_PURPOSE_ANY */
#include <openssl/x509_vfy.h>
#include <openssl/cms.h>
#include <openssl/pem.h>
std::string get_bio_str(BIO* bio) {
char* data;
BIO_get_mem_data(bio, &data);
return data;
}
void print_openssl_errors() {
ERR_print_errors_fp(stderr);
}
void exit_openssl_error() {
print_openssl_errors();
exit(1);
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << argv[0] << " target-file" << std::endl;
return 1;
}
const char* target_file_path = argv[1];
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
X509_STORE *st = X509_STORE_new();
if (!X509_STORE_set_default_paths(st))
exit_openssl_error();
BIO *in = BIO_new_file(target_file_path, "r");
if (!in)
exit_openssl_error();
BIO *cont = NULL;
CMS_ContentInfo *cms = SMIME_read_CMS(in, &cont);
if (!cms)
exit_openssl_error();
BIO *out = BIO_new(BIO_s_mem());
if (!out)
exit_openssl_error();
if (!CMS_verify(cms, NULL, st, cont, out, 0))
exit_openssl_error();
std::cout << "Verify OK" << std::endl;
std::cout << "---" << std::endl;
std::cout << get_bio_str(out) << std::endl;
CMS_ContentInfo_free(cms);
BIO_free(in);
BIO_free(out);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment