Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Fix threading issue when reading and writing from SslStream at the same time #37736

Merged
merged 1 commit into from
May 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,26 @@ internal static int Encrypt(SafeSslHandle context, ReadOnlyMemory<byte> input, r
errorCode = Ssl.SslErrorCode.SSL_ERROR_NONE;

int retVal;
unsafe
Exception innerError = null;

lock (context)
{
using (MemoryHandle handle = input.Pin())
unsafe
{
using (MemoryHandle handle = input.Pin())
{
retVal = Ssl.SslWrite(context, (byte*)handle.Pointer, input.Length);
}
}

if (retVal != input.Length)
{
retVal = Ssl.SslWrite(context, (byte*)handle.Pointer, input.Length);
errorCode = GetSslError(context, retVal, out innerError);
}
}

if (retVal != input.Length)
{
errorCode = GetSslError(context, retVal, out Exception innerError);
retVal = 0;

switch (errorCode)
Expand All @@ -351,6 +360,7 @@ internal static int Encrypt(SafeSslHandle context, ReadOnlyMemory<byte> input, r
}

retVal = BioRead(context.OutputBio, output, capacityNeeded);

if (retVal <= 0)
{
// Make sure we clear out the error that is stored in the queue
Expand All @@ -370,27 +380,34 @@ internal static int Decrypt(SafeSslHandle context, byte[] outBuffer, int offset,
errorCode = Ssl.SslErrorCode.SSL_ERROR_NONE;

int retVal = BioWrite(context.InputBio, outBuffer, offset, count);
Exception innerError = null;

if (retVal == count)
lock (context)
{
unsafe
if (retVal == count)
{
fixed (byte* fixedBuffer = outBuffer)
unsafe
{
fixed (byte* fixedBuffer = outBuffer)
{
retVal = Ssl.SslRead(context, fixedBuffer + offset, outBuffer.Length);
}
}

if (retVal > 0)
{
retVal = Ssl.SslRead(context, fixedBuffer + offset, outBuffer.Length);
count = retVal;
}
}

if (retVal > 0)
if (retVal != count)
{
count = retVal;
errorCode = GetSslError(context, retVal, out innerError);
}
}

if (retVal != count)
{
Exception innerError;
errorCode = GetSslError(context, retVal, out innerError);
retVal = 0;

switch (errorCode)
Expand Down