Skip to content

Commit

Permalink
rework locking in SslStream to support TLS1.3 (#32925)
Browse files Browse the repository at this point in the history
* initial locking

* feedback from review

* update _handshakeWaiter

* feedback from review

* feedback from review

* feedback from review
  • Loading branch information
wfurt committed Mar 20, 2020
1 parent 7ae0add commit 4bdf468
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,11 @@ internal static int Encrypt(SafeSslHandle context, ReadOnlySpan<byte> input, ref
int retVal;
Exception? innerError = null;

lock (context)
{
retVal = Ssl.SslWrite(context, ref MemoryMarshal.GetReference(input), input.Length);
retVal = Ssl.SslWrite(context, ref MemoryMarshal.GetReference(input), input.Length);

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

if (retVal != input.Length)
Expand Down Expand Up @@ -390,30 +387,27 @@ internal static int Decrypt(SafeSslHandle context, byte[] outBuffer, int offset,
int retVal = BioWrite(context.InputBio!, outBuffer, offset, count);
Exception? innerError = null;

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

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

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

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

if (retVal != count)
{
retVal = 0;
Expand Down
1 change: 1 addition & 0 deletions src/libraries/Common/src/System/Net/SecurityStatusPal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal enum SecurityStatusPalErrorCode
ContextExpired,
CredentialsNeeded,
Renegotiate,
TryAgain,

// Errors
OutOfMemory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ public partial class SslStream
private interface ISslIOAdapter
{
ValueTask<int> ReadAsync(Memory<byte> buffer);
ValueTask<int> ReadLockAsync(Memory<byte> buffer);
Task WriteLockAsync();
ValueTask WriteAsync(byte[] buffer, int offset, int count);
Task WaitAsync(TaskCompletionSource<bool> waiter);
CancellationToken CancellationToken { get; }
}

Expand All @@ -31,12 +30,10 @@ public AsyncSslIOAdapter(SslStream sslStream, CancellationToken cancellationToke

public ValueTask<int> ReadAsync(Memory<byte> buffer) => _sslStream.InnerStream.ReadAsync(buffer, _cancellationToken);

public ValueTask<int> ReadLockAsync(Memory<byte> buffer) => _sslStream.CheckEnqueueReadAsync(buffer);

public Task WriteLockAsync() => _sslStream.CheckEnqueueWriteAsync();

public ValueTask WriteAsync(byte[] buffer, int offset, int count) => _sslStream.InnerStream.WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), _cancellationToken);

public Task WaitAsync(TaskCompletionSource<bool> waiter) => waiter.Task;

public CancellationToken CancellationToken => _cancellationToken;
}

Expand All @@ -48,17 +45,15 @@ public AsyncSslIOAdapter(SslStream sslStream, CancellationToken cancellationToke

public ValueTask<int> ReadAsync(Memory<byte> buffer) => new ValueTask<int>(_sslStream.InnerStream.Read(buffer.Span));

public ValueTask<int> ReadLockAsync(Memory<byte> buffer) => new ValueTask<int>(_sslStream.CheckEnqueueRead(buffer));

public ValueTask WriteAsync(byte[] buffer, int offset, int count)
{
_sslStream.InnerStream.Write(buffer, offset, count);
return default;
}

public Task WriteLockAsync()
public Task WaitAsync(TaskCompletionSource<bool> waiter)
{
_sslStream.CheckEnqueueWrite();
waiter.Task.Wait();
return Task.CompletedTask;
}

Expand Down
Loading

0 comments on commit 4bdf468

Please sign in to comment.