Created
August 1, 2020 03:06
-
-
Save kohnakagawa/2b80462c4f5853969b3407b1a449f00c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/stat.h> | |
#include "openssl/err.h" | |
#include "openssl/objects.h" | |
#include "openssl/evp.h" | |
#include "openssl/x509.h" | |
#include "openssl/pkcs7.h" | |
#include "openssl/pem.h" | |
int get_file_size(const char* fname) { | |
struct stat st; | |
if (stat(fname, &st) != 0) { | |
fprintf(stderr, "cannot open file"); | |
return -1; | |
} | |
return st.st_size; | |
} | |
int main() { | |
const char* in_file = "test.der"; | |
FILE* fin = fopen(in_file, "rb"); | |
if (!fin) { | |
fprintf(stderr, "cannot open file"); | |
return 1; | |
} | |
BIO* out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT); | |
const int fsize = get_file_size(in_file); | |
char* buffer = (char*) malloc(fsize); | |
fread(buffer, sizeof(char), fsize, fin); | |
BIO* mem = BIO_new_mem_buf(buffer, fsize); | |
PKCS7* p7 = d2i_PKCS7_bio(mem, NULL); | |
PKCS7_print_ctx(out, p7, 0, NULL); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't free any resources in this sample program. If you use this snippet, please call free function to release the allocated resources.