I needed to have a routine to convert the data stored in an ASN1_TYPE structure to an ASCII string. After finding that no one on the net seemed to have the answer I went and did some research. This is what I came up with for an answer. If any one sees anything wrong with it I would love to know about it and will make suitable adjustments. I will probably submit the change to the OpenSSL project for hopeful inclusion.
Peace, Chuck Wegrzyn
#include <openssl/pem.h> #include <openssl/conf.h> #include <openssl/x509v3.h> #include <openssl/engine.h> #include <openssl/asn1.h> static void error(char *msg) { // print out message... printf(msg); exit(1); } void ASN1_TYPE_print(BIO *bio,ASN1_TYPE *ptr) { // memory bio to collect the output. char objbuf[80]; const char *ln; // Collect the things from the ASN1_TYPE structure switch(ptr->type) { case V_ASN1_BOOLEAN: BIO_puts(bio, ptr->value.boolean ? "true" : "false"); break; case V_ASN1_INTEGER: BIO_puts(bio,i2s_ASN1_INTEGER(NULL,ptr->value.integer)); break; case V_ASN1_ENUMERATED: BIO_puts(bio,i2s_ASN1_INTEGER(NULL,ptr->value.enumerated)); break; case V_ASN1_NULL: BIO_puts(bio,"NULL"); break; case V_ASN1_UTCTIME: ASN1_UTCTIME_print(bio,ptr->value.utctime); break; case V_ASN1_GENERALIZEDTIME: ASN1_GENERALIZEDTIME_print(bio,ptr->value.generalizedtime); break; case V_ASN1_OBJECT: ln = OBJ_nid2ln(OBJ_obj2nid(ptr->value.object)); if( !ln ) ln = ""; OBJ_obj2txt(objbuf,sizeof(objbuf),ptr->value.object,1); BIO_puts(bio,objbuf); break; default : ASN1_STRING_print_ex(bio, ptr->value.visiblestring, ASN1_STRFLGS_DUMP_UNKNOWN|ASN1_STRFLGS_SHOW_TYPE); break; } }