I have a socket application that reads and writes data. I'm using OpenSSL to do the encryption/decryption. My question is whether the "BIO_write" method can buffer data internally or if I have to append to a growing buffer as I read more from the socket. Here's what I'm doing.
I read from the socket and I use the class method below to write all the bytes that were read into the BIO:
int CSslConnectionContext::Store(BYTE* pbData, DWORD dwDataLength)
{
int bytes = 0;
if (dwDataLength > 0)
{
bytes = BIO_write(bio[BIO_RECV], pbData, dwDataLength);
}
return bytes;
}
I then immediately call the SSL_read method to get decrypted data:
int CSslConnectionContext::Read(BYTE* pbBuffer, DWORD dwBufferSize)
{
int bytes = SSL_read(ssl, pbBuffer, dwBufferSize);
return bytes;
}
If SSL_read returns a positive number then I've got decrypted data available for my application.
What I'm not sure of is what happens when my socket read doesn't capture all of the data required for decryption in a single read.
So if I need 100 bytes to be able to decrypted the data and the first read only gets 80, can I call BIO_write() with those 80, do another socket read to get the next 20, and then call BIO_write() with just those 20 bytes?
Or do I need to write my code so when I read 80 I do something like this:
- call BIO_write() with the 80 bytes.
- if that returns a failure indicator - hold onto that 80 bytes.
- read the next 20 bytes from the socket and append it to the buffered 80 bytes.
- call BIO_write() with 100 bytes