On Fri, Feb 28, 2003, Christian Weber wrote:

> Dear OpenSSL users,
> 
> we want to want to operate on the Contents of a memory BIO
> - eg digest - and tried out following implementation:
> 
> -------------
> BIO *mb = BIO_new(BIO_s_mem());
> // filling it with some data
> char* message="This is some data to digest";
> BIO_write(mb, message, sizeof(message));
> 
> // after the memory bio has been filled
> BIO *dg = BIO_new(BIO_f_md()), *nul=BIO_new(BIO_s_null());
> EVP_MD *md = EVP_md5();
> BIO_set_md(dg,md);
> 
> // get all out of the mb and diget it
> BIO_push(mb,dg);  // append digesting to mem-BIO
> BIO_push(dg,nul); // get all out of digest-BIO
> // BIO chain should be: mem-dgst-nul
> 
> // get the digest now
> char mdbuf[EVP_MAX_MD_SIZE];
> BIO_gets(dg, mdbuf, EVP_MAX_MD_SIZE);
> -------------
> 
> but the memory BIO stays filled, even after performing
> BIO_flush(mb) and the digest we get out of dg is the 
> digesting start value.
> 
> So what am I missing? How can the propagation of 
> the memory data through the filter BIO be triggered?
> 

It doesn't work like that. You can't just join a load of BIOs together and
expect their contents to proceed along the chain. The data must be explicitly
read from the top BIO of the chain, then it will originate from the final
source/sink BIO (memory BIO in this case) and pass through any filter BIOs
along the way. As such a BIO chain must contain only one source/sink BIO and
this must be at the end.

So if you arrange it so you have:

dgst-mem

then read everything from dgst until you get EOF (using BIO_read) it will pull
the data from mem and it will pass through dgst. Then you can retrieve the
digest from dgst.

The best way to handle this depends on how the memory BIO is created and
whether you want its contents after the digesting process.

For example if you will need the memory BIO and have control over its creation
then you could use dgst-mem while data is being written to it. The data will
tnen be written correctly to the memory BIO and also be digested. You can then
read the digest and discard the md BIO.

Alternatively if you don't want to keep the data you can do dgst-null.

If you don't have control over the creation of the memory BIO (i.e. you
receive it in a finalized state) then a dgst BIO may not be the best solution.
You could instead call BIO_get_mem_data() to retrieve the memory BIOs buffer
and call EVP_Digest() on it.

Steve.
--
Dr Stephen N. Henson.
Core developer of the   OpenSSL project: http://www.openssl.org/
Freelance consultant see: http://www.drh-consultancy.demon.co.uk/
Email: [EMAIL PROTECTED], PGP key: via homepage.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to