BIO_get_mem_ptr 返回 0 作为长度和输出

BIO_get_mem_ptr is returnig 0 as length and output

我在这里尝试获取 bio 文件的大小,

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

我从一些 Whosebug 问题中得到了这个示例代码。我已经尝试了很多次,但是 BIO_get_mem_ptrbptr 中给出了一个 null 指针,return 值为 0。我在任何参考站点中都找不到与此问题相关的任何解决方案。

这是源代码,

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;
}

这是什么问题,

正如您在 The Man 上看到的 bio_get_mem_ptr 想要一个 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.

您正在尝试使用文件,因此您应该使用 bio_s_file

The Man 上的示例代码