Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract pem certificate information programmatically using openssl

Using the openssl command line is possible to extract, in a human readable mode, all the information contained in a .pem certificate; that is:

openssl x509 -noout -in <MyCertificate>.pem  -text

What are the suitable steps in order to extract this information using the openssl API?

Regards,

like image 626
Genar Avatar asked Jun 28 '11 15:06

Genar


1 Answers

The X509_print_ex family of functions is your answer.

#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/bio.h>

int main(int argc, char **argv)
{
    X509 *x509;
    BIO *i = BIO_new(BIO_s_file());
    BIO *o = BIO_new_fp(stdout,BIO_NOCLOSE);

    if((argc < 2) ||
       (BIO_read_filename(i, argv[1]) <= 0) ||
       ((x509 = PEM_read_bio_X509_AUX(i, NULL, NULL, NULL)) == NULL)) {
        return -1;
    }

    X509_print_ex(o, x509, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
}
like image 128
Mathias Brossard Avatar answered Oct 14 '22 07:10

Mathias Brossard