Search code examples
c++opensslcsr

How to parse a CSR object using openssl API


I have an std::string which contains the CSR file's content. The problem is that I can't find a way to parse this string into an object. I've already tried the answers to this post but they're only for a FILE.

I'm currently using this method but it always returns NULL.

const unsigned char * certChar = reinterpret_cast<const unsigned char*>(certStr.c_str());

X509* csr = d2i_X509(NULL, &certChar, certStr.size());
if (csr == NULL)
{
        std::cout << "Return value was NULL" << std::endl;
}

The loaded file in memory is a .CSR with the content put into the certStr std::string.

Using the ERR_get_error() method, i got the following error: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag


Solution

  • Thanks to Botje, I figured how to make it work.

    BIO* bio = BIO_new_mem_buf(reinterpret_cast<const void*>(certStr.c_str()),certStr.size());
    X509_REQ* csr;
    PEM_read_bio_X509_REQ(bio, &csr, NULL, NULL);