OpenSSL - access a file-BIO via memory

chris01 Source

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?




answered 6 months ago Matt Caswell #1

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.

comments powered by Disqus