Skip to content

Instantly share code, notes, and snippets.

@kohnakagawa
Created August 1, 2020 03:06
Show Gist options
  • Save kohnakagawa/2b80462c4f5853969b3407b1a449f00c to your computer and use it in GitHub Desktop.
Save kohnakagawa/2b80462c4f5853969b3407b1a449f00c to your computer and use it in GitHub Desktop.
#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;
}
@kohnakagawa
Copy link
Author

I don't free any resources in this sample program. If you use this snippet, please call free function to release the allocated resources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment