chris01
chris01

Reputation: 12431

OpenSSL - access a file-BIO via memory

I try to access the bytes of a BIO with BUF_MEM. But it is not working if it is a file-BIO.

    BUF_MEM *bmmem = NULL, bmfil = NULL;
    int ret = -1;

    // OK for memory
    BIO *biomem = BIO_new (BIO_s_mem ());       
    ret = BIO_get_mem_ptr (biomem, &bmmem);    // ret = 1
    printf ("\nbiomem - %d %d", ret, bmmem);   // OK

    // NOK for file
    BIO *biofil = BIO_new (BIO_s_file ());
    BIO_read_filename (biofil, "myfile.pem");   // ok
    ret = BIO_get_mem_ptr (biofil, &bmfil);     // ret = 0
    printf ("\nbiofil - %d %d, ret, bmfil);     // NOK

Do I miss something?

Thanks!

Upvotes: 2

Views: 1066

Answers (1)

Matt Caswell
Matt Caswell

Reputation: 9502

BIO_get_mem_ptr gives you a pointer into the underlying memory buffer of a mem bio. It only works with a mem BIO. In a file BIO there is no underlying memory buffer!

Use BIO_read to read data out of a BIO. That works on both a mem BIO and a file BIO.

https://www.openssl.org/docs/man1.1.0/crypto/BIO_read.html

Upvotes: 3

Related Questions