Openssl编程获取证书common name

Talk is cheap, show me the code!

#include 
#include 
#include 
#include 
#include 

int main(int argc, char **argv)
{
    char  cn[256] = "";
    BIO *bio = NULL;

    bio = BIO_new_file(argv[1], "r");
    assert(bio);

    X509 *x = NULL;
    x = PEM_read_bio_X509(bio, NULL, NULL, NULL);
    assert(x);

    X509_NAME *name = X509_get_subject_name(x);

    X509_NAME_get_text_by_NID(name,NID_commonName, cn, sizeof(cn));

    printf("%s\n", cn);
}
gcc -lssl a.c
./a.out 1.crt

你可能感兴趣的:(linux系统编程,openssl,编程)