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

avoid ArgumentOutOfRangeException while processing invalid or incomplete TLS frame #63184

Merged
merged 2 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -460,11 +460,6 @@ private async ValueTask<ProtocolToken> ReceiveBlobAsync<TIOAdapter>(TIOAdapter a
where TIOAdapter : IReadWriteAdapter
{
int readBytes = await FillHandshakeBufferAsync(adapter, SecureChannel.ReadHeaderSize).ConfigureAwait(false);
if (readBytes == 0)
{
throw new IOException(SR.net_io_eof);
}

if (_framing == Framing.Unified || _framing == Framing.Unknown)
{
_framing = DetectFraming(_handshakeBuffer.ActiveReadOnlySpan);
Expand Down Expand Up @@ -1083,7 +1078,7 @@ private ValueTask<int> FillHandshakeBufferAsync<TIOAdapter>(TIOAdapter adapter,
int bytesRead = t.Result;
if (bytesRead == 0)
{
return new ValueTask<int>(0);
throw new IOException(SR.net_io_eof);
wfurt marked this conversation as resolved.
Show resolved Hide resolved
}

_handshakeBuffer.Commit(bytesRead);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,49 @@ public async Task ServerAsyncAuthenticate_NoCertificate_Throws(bool useAsync)
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task ServerAsyncAuthenticate_InvalidHello_Throws(bool close)
{
(NetworkStream client, NetworkStream server) = TestHelper.GetConnectedTcpStreams();
using (client)
using (SslStream ssl = new SslStream(server))
{
byte[] buffer = new byte[182];
buffer[0] = 178;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 1;
buffer[4] = 133;
buffer[5] = 166;

Task t1 = ssl.AuthenticateAsServerAsync(_serverCertificate, false, false);
Task t2 = client.WriteAsync(buffer).AsTask();
if (close)
{
await t2.WaitAsync(TestConfiguration.PassingTestTimeout);
client.Socket.Shutdown(SocketShutdown.Send);
}
else
{
// Write enough data to full frame size
buffer = new byte[13000];
t2 = client.WriteAsync(buffer).AsTask();
await t2.WaitAsync(TestConfiguration.PassingTestTimeout);
}

if (close)
{
await Assert.ThrowsAsync<IOException>(() => t1);
}
else
{
await Assert.ThrowsAsync<AuthenticationException>(() => t1);
}
}
}

public static IEnumerable<object[]> ProtocolMismatchData()
{
if (PlatformDetection.SupportsSsl3)
Expand Down