Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove dead code from SslStream #32065

Merged
merged 2 commits into from
Feb 12, 2020
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 @@ -54,10 +54,6 @@ private enum FrameType : byte
AppData = 23
}

// This block is used by re-handshake code to buffer data decrypted with the old key.
private byte[] _queuedReadData;
private int _queuedReadCount;

//
// This block is used to rule the >>re-handshakes<< that are concurrent with read/write I/O requests.
//
Expand Down Expand Up @@ -202,35 +198,6 @@ private SecurityStatusPal PrivateDecryptData(byte[] buffer, ref int offset, ref
return _context.Decrypt(buffer, ref offset, ref count);
}

//
// When re-handshaking the "old" key decrypted data are queued until the handshake is done.
// When stream calls for decryption we will feed it queued data left from "old" encryption key.
//
// Must be called under the lock in case concurrent handshake is going.
//
private int CheckOldKeyDecryptedData(Memory<byte> buffer)
{
ThrowIfExceptionalOrNotAuthenticated();
if (_queuedReadData != null)
{
// This is inefficient yet simple and should be a REALLY rare case.
int toCopy = Math.Min(_queuedReadCount, buffer.Length);
new Span<byte>(_queuedReadData, 0, toCopy).CopyTo(buffer.Span);
_queuedReadCount -= toCopy;
if (_queuedReadCount == 0)
{
_queuedReadData = null;
}
else
{
Buffer.BlockCopy(_queuedReadData, toCopy, _queuedReadData, 0, _queuedReadCount);
}

return toCopy;
}
return -1;
}

//
// This method assumes that a SSPI context is already in a good shape.
// For example it is either a fresh context or already authenticated context that needs renegotiation.
Expand Down Expand Up @@ -602,23 +569,18 @@ private void FinishHandshakeRead(int newState)
// X - some bytes are ready, no need for IO
private int CheckEnqueueRead(Memory<byte> buffer)
{
int lockState = Interlocked.CompareExchange(ref _lockReadState, LockRead, LockNone);
ThrowIfExceptionalOrNotAuthenticated();

int lockState = Interlocked.CompareExchange(ref _lockReadState, LockRead, LockNone);
if (lockState != LockHandshake)
{
// Proceed, no concurrent handshake is ongoing so no need for a lock.
return CheckOldKeyDecryptedData(buffer);
return -1;
wfurt marked this conversation as resolved.
Show resolved Hide resolved
}

LazyAsyncResult lazyResult = null;
lock (SyncLock)
{
int result = CheckOldKeyDecryptedData(buffer);
if (result != -1)
{
return result;
}

// Check again under lock.
if (_lockReadState != LockHandshake)
{
Expand All @@ -631,30 +593,23 @@ private int CheckEnqueueRead(Memory<byte> buffer)
}
// Need to exit from lock before waiting.
lazyResult.InternalWaitForCompletion();
lock (SyncLock)
{
return CheckOldKeyDecryptedData(buffer);
}
ThrowIfExceptionalOrNotAuthenticated();
return -1;
}

private ValueTask<int> CheckEnqueueReadAsync(Memory<byte> buffer)
{
int lockState = Interlocked.CompareExchange(ref _lockReadState, LockRead, LockNone);
ThrowIfExceptionalOrNotAuthenticated();

int lockState = Interlocked.CompareExchange(ref _lockReadState, LockRead, LockNone);
if (lockState != LockHandshake)
{
// Proceed, no concurrent handshake is ongoing so no need for a lock.
return new ValueTask<int>(CheckOldKeyDecryptedData(buffer));
return new ValueTask<int>(-1);
}

lock (SyncLock)
{
int result = CheckOldKeyDecryptedData(buffer);
if (result != -1)
{
return new ValueTask<int>(result);
}

// Check again under lock.
if (_lockReadState != LockHandshake)
{
Expand All @@ -671,6 +626,8 @@ private ValueTask<int> CheckEnqueueReadAsync(Memory<byte> buffer)

private Task CheckEnqueueWriteAsync()
{
ThrowIfExceptionalOrNotAuthenticated();

// Clear previous request.
int lockState = Interlocked.CompareExchange(ref _lockWriteState, LockWrite, LockNone);
if (lockState != LockHandshake)
Expand Down