1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-21 02:33:24 +00:00

Updated TLS buffering, to fix issue associated with OpenSSL/1.1.1c

Still needs additional work
This commit is contained in:
Bryan Roe
2019-06-20 13:56:33 -07:00
parent 13ad575bb5
commit 6de8178723

View File

@@ -1065,13 +1065,14 @@ ILibAsyncSocket_SendStatus ILibAsyncSocket_ProcessEncryptedBuffer(ILibAsyncSocke
if (j < (int)(Reader->writeBioBuffer->length)) if (j < (int)(Reader->writeBioBuffer->length))
{ {
// Not all data was sent // Not all data was sent
Reader->writeBioBuffer->data += j; data = (ILibAsyncSocket_SendData*)ILibMemory_Allocate(sizeof(ILibAsyncSocket_SendData), Reader->writeBioBuffer->length - j, NULL, NULL);
Reader->writeBioBuffer->length -= j; data->buffer = ILibMemory_GetExtraMemory(data, sizeof(ILibAsyncSocket_SendData));
data->bufferSize = Reader->writeBioBuffer->length - j;
data = (ILibAsyncSocket_SendData*)ILibMemory_Allocate(sizeof(ILibAsyncSocket_SendData), 0, NULL, NULL);
data->UserFree = ILibAsyncSocket_MemoryOwnership_BIO; data->UserFree = ILibAsyncSocket_MemoryOwnership_BIO;
memcpy_s(data->buffer, data->bufferSize, Reader->writeBioBuffer->data + j, data->bufferSize);
Reader->PendingSend_Head = Reader->PendingSend_Tail = data; Reader->PendingSend_Head = Reader->PendingSend_Tail = data;
retVal = ILibAsyncSocket_NOT_ALL_DATA_SENT_YET; retVal = ILibAsyncSocket_NOT_ALL_DATA_SENT_YET;
BIO_reset(Reader->writeBio);
} }
else if (j == (int)(Reader->writeBioBuffer->length)) else if (j == (int)(Reader->writeBioBuffer->length))
{ {
@@ -1845,9 +1846,25 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
#ifndef MICROSTACK_NOTLS #ifndef MICROSTACK_NOTLS
if (module->ssl != NULL) if (module->ssl != NULL)
{ {
if (module->PendingSend_Head->UserFree == ILibAsyncSocket_MemoryOwnership_BIO) // First check to see if there is a buffer for us to send
if (module->PendingSend_Head->buffer != NULL && module->PendingSend_Head->bytesSent != module->PendingSend_Head->bufferSize)
{ {
if (module->writeBioBuffer->length > 0) bytesSent = (int)send(module->internalSocket, module->PendingSend_Head->buffer + module->PendingSend_Head->bytesSent, module->PendingSend_Head->bufferSize - module->PendingSend_Head->bytesSent, MSG_NOSIGNAL); // Klocwork reports that this could block while holding a lock... This socket has been set to O_NONBLOCK, so that will never happen
if (bytesSent > 0)
{
module->PendingSend_Head->bytesSent += bytesSent;
if (module->PendingSend_Head->bytesSent == module->PendingSend_Head->bufferSize && module->PendingSend_Head->UserFree == ILibAsyncSocket_MemoryOwnership_CHAIN)
{
free(module->PendingSend_Head->buffer);
module->PendingSend_Head->buffer = NULL;
}
}
if (bytesSent <= 0 || module->PendingSend_Head->bytesSent < module->PendingSend_Head->bufferSize)
{
TRY_TO_SEND = 0;
}
}
if (module->writeBioBuffer->length > 0 && TRY_TO_SEND != 0)
{ {
BIO_clear_retry_flags(module->writeBio); BIO_clear_retry_flags(module->writeBio);
bytesSent = (int)send(module->internalSocket, module->writeBioBuffer->data, (int)(module->writeBioBuffer->length), MSG_NOSIGNAL); // Klocwork reports that this could block while holding a lock... This socket has been set to O_NONBLOCK, so that will never happen bytesSent = (int)send(module->internalSocket, module->writeBioBuffer->data, (int)(module->writeBioBuffer->length), MSG_NOSIGNAL); // Klocwork reports that this could block while holding a lock... This socket has been set to O_NONBLOCK, so that will never happen
@@ -1857,13 +1874,22 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
if ((bytesSent > 0 && bytesSent < (int)(module->writeBioBuffer->length)) || (bytesSent < 0 && errno == EWOULDBLOCK)) if ((bytesSent > 0 && bytesSent < (int)(module->writeBioBuffer->length)) || (bytesSent < 0 && errno == EWOULDBLOCK))
#endif #endif
{ {
// Not all data was sent
if (bytesSent > 0) if (bytesSent > 0)
{ {
module->writeBioBuffer->data += bytesSent; // Some Data was sent, so we need to grab all the data from SSL and buffer it
module->writeBioBuffer->length -= bytesSent; module->PendingSend_Head->UserFree = ILibAsyncSocket_MemoryOwnership_CHAIN;
module->PendingSend_Head->buffer = ILibMemory_Allocate(module->writeBioBuffer->length - bytesSent, 0, NULL, NULL);
module->PendingSend_Head->bufferSize = module->writeBioBuffer->length - bytesSent;
module->PendingSend_Head->bytesSent = 0;
memcpy_s(module->PendingSend_Head->buffer, module->PendingSend_Head->bufferSize, module->writeBioBuffer + bytesSent, module->PendingSend_Head->bufferSize);
module->TotalBytesSent += bytesSent; module->TotalBytesSent += bytesSent;
module->PendingBytesToSend = (unsigned int)(module->writeBioBuffer->length); module->PendingBytesToSend = (unsigned int)(module->PendingSend_Head->bufferSize);
BIO_reset(module->writeBio);
}
else
{
// No Data was sent, so we can just leave the data in the writeBio, and fetch it later
} }
TRY_TO_SEND = 0; TRY_TO_SEND = 0;
} }
@@ -1873,6 +1899,7 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
BIO_reset(module->writeBio); BIO_reset(module->writeBio);
module->TotalBytesSent += bytesSent; module->TotalBytesSent += bytesSent;
module->PendingBytesToSend = (unsigned int)(module->writeBioBuffer->length); module->PendingBytesToSend = (unsigned int)(module->writeBioBuffer->length);
if (module->PendingSend_Head->buffer != NULL && module->PendingSend_Head->UserFree == ILibAsyncSocket_MemoryOwnership_CHAIN) { free(module->PendingSend_Head->buffer); }
free(module->PendingSend_Head); free(module->PendingSend_Head);
module->PendingSend_Head = module->PendingSend_Tail = NULL; module->PendingSend_Head = module->PendingSend_Tail = NULL;
@@ -1881,14 +1908,14 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
else else
{ {
// Something went wrong // Something went wrong
module->writeBioBuffer->length = 0; BIO_reset(module->writeBio);
if (module->PendingSend_Head->buffer != NULL && module->PendingSend_Head->UserFree == ILibAsyncSocket_MemoryOwnership_CHAIN) { free(module->PendingSend_Head->buffer); }
free(module->PendingSend_Head); free(module->PendingSend_Head);
module->PendingSend_Head = module->PendingSend_Tail = NULL; module->PendingSend_Head = module->PendingSend_Tail = NULL;
TRY_TO_SEND = 0; TRY_TO_SEND = 0;
} }
} }
}
} }
else else
#endif #endif