Akhil V Suku
Akhil V Suku

Reputation: 912

BIO_get_mem_ptr is returnig 0 as length and output

Here I am trying to get the size of bio file as ,

long res = BIO_get_mem_ptr(certBio, &bptr);
length = bptr->length; // is the length

I got this sample code from some stackoverflow question. I have tried many time but the BIO_get_mem_ptr is giving a null pointer in bptr with return value 0. I can't find any solutions related to this problem in any reference sites.

Here is the source code,

int pass_cb(char *buf, int size, int rwflag, void *u)
{
    int len;
    char *tmp;  
    tmp = "123456";
    len = strlen(tmp);

    if (len <= 0) 
        return 0; 
    if (len > size) 
        len = size;
    memcpy(buf, tmp, len);
    return len;
}

int main(void)
{  
    X509 *x509;    
    int length = 0;

    unsigned char data[1000];
    unsigned char *buffer = NULL;
    buffer = data; 

    BIO *certBio = BIO_new(BIO_s_file()); 
    // BIO *certBio = BIO_new(BIO_s_mem());  -> have tried this one too gives the same result
    BUF_MEM *bptr = 0;  

    BIO_read_filename(certBio, "E:\\share\\Folder\\TempCert.pem");

    x509 = PEM_read_bio_X509_AUX(certBio, NULL, pass_cb, NULL); 

    long res = BIO_get_mem_ptr(certBio, &bptr);
    length = bptr->length;

    memset(&buffer, 0, 1000);

    int  ret = BIO_read(certBio, &buffer, length);   

    BIO_free_all(certBio);
    CertFreeCertificateContext(pContext);  
    CertCloseStore(hStore, 0); 
    return 0;
}

What is the problem causing here,

Upvotes: 1

Views: 1173

Answers (1)

LPs
LPs

Reputation: 16223

As you can see on The Man bio_get_mem_ptr wants a memory BIO

A memory BIO is a source/sink BIO which uses memory for its I/O. Data written to a memory BIO is stored in a BUF_MEM structure which is extended as appropriate to accommodate the stored data.

You are trying to use a file, so you should use the example code on The Man of bio_s_file

Upvotes: 1

Related Questions