I think the easiest way is to use a memory BIO:
...
X509 *lcert = NULL;
BUF_MEM *bptr = NULL;
char *buf = NULL;
int loc;
FILE *f = fopen("your cert goes here", "rb");
if( (lcert = PEM_read_X509(f, &lcert, NULL, NULL)) == NULL){
// error handling...
}
loc = X509_get_ext_by_NID( lcert, NID_key_usage, -1);
X509_EXTENSION *ex = X509_get_ext(lcert, loc);
BIO *bio = BIO_new(BIO_s_mem());
if(!X509V3_EXT_print(bio, ex, 0, 0)){
// error handling...
}
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bptr);
// now bptr contains the strings of the key_usage, take
// care that bptr->data is NOT NULL terminated, so
// to print it well, let's do something..
buf = (char *)malloc( (bptr->length + 1)*sizeof(char) );
memcpy(buf, bptr->data, bptr->length);
buf[bptr->length] = '';
// Now you can printf it or parse it, the way you want...
printf ("%s
", buf);
...
In my case, for a teste certificate, it has printed "Digital Signature, Non Repudiation, Key Encipherment"
There are other ways, like using an ASN1_BIT_STRING *. I can show you if the above doesn't fit your needs.
Regards.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…