Skip to content

Commit f5874b0

Browse files
authored
fix buffer handling in Tls handshake (#32267)
* fix buffer handling in Tls handshake * feedback from review
1 parent 0b32c1a commit f5874b0

File tree

4 files changed

+118
-72
lines changed

4 files changed

+118
-72
lines changed

src/libraries/System.Net.Security/src/System.Net.Security.csproj

+7-4
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,21 @@
6666
<Link>Common\System\Net\DebugCriticalHandleZeroOrMinusOneIsInvalid.cs</Link>
6767
</Compile>
6868
<!-- System.Net common -->
69+
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs">
70+
<Link>Common\System\Net\ArrayBuffer.cs</Link>
71+
</Compile>
6972
<Compile Include="$(CommonPath)System\Net\ExceptionCheck.cs">
7073
<Link>Common\System\Net\ExceptionCheck.cs</Link>
7174
</Compile>
7275
<Compile Include="$(CommonPath)System\Net\LazyAsyncResult.cs">
7376
<Link>Common\System\Net\LazyAsyncResult.cs</Link>
7477
</Compile>
75-
<Compile Include="$(CommonPath)System\Net\UriScheme.cs">
76-
<Link>Common\System\Net\UriScheme.cs</Link>
77-
</Compile>
7878
<Compile Include="$(CommonPath)System\Net\SecurityProtocol.cs">
7979
<Link>Common\System\Net\SecurityProtocol.cs</Link>
8080
</Compile>
81+
<Compile Include="$(CommonPath)System\Net\UriScheme.cs">
82+
<Link>Common\System\Net\UriScheme.cs</Link>
83+
</Compile>
8184
<!-- Common -->
8285
<Compile Include="$(CommonPath)System\NotImplemented.cs">
8386
<Link>Common\System\NotImplemented.cs</Link>
@@ -471,4 +474,4 @@
471474
<Reference Include="System.Security.Cryptography.OpenSsl" />
472475
<Reference Include="System.Security.Cryptography.Primitives" />
473476
</ItemGroup>
474-
</Project>
477+
</Project>

src/libraries/System.Net.Security/src/System/Net/Security/SecureChannel.cs

+5-18
Original file line numberDiff line numberDiff line change
@@ -717,21 +717,21 @@ private bool AcquireServerCredentials(ref byte[] thumbPrint, ReadOnlySpan<byte>
717717
}
718718

719719
//
720-
internal ProtocolToken NextMessage(byte[] incoming, int offset, int count)
720+
internal ProtocolToken NextMessage(ReadOnlySpan<byte> incomingBuffer)
721721
{
722722
if (NetEventSource.IsEnabled)
723723
NetEventSource.Enter(this);
724724

725725
byte[] nextmsg = null;
726-
SecurityStatusPal status = GenerateToken(incoming, offset, count, ref nextmsg);
726+
SecurityStatusPal status = GenerateToken(incomingBuffer, ref nextmsg);
727727

728728
if (!_sslAuthenticationOptions.IsServer && status.ErrorCode == SecurityStatusPalErrorCode.CredentialsNeeded)
729729
{
730730
if (NetEventSource.IsEnabled)
731731
NetEventSource.Info(this, "NextMessage() returned SecurityStatusPal.CredentialsNeeded");
732732

733733
SetRefreshCredentialNeeded();
734-
status = GenerateToken(incoming, offset, count, ref nextmsg);
734+
status = GenerateToken(incomingBuffer, ref nextmsg);
735735
}
736736

737737
ProtocolToken token = new ProtocolToken(nextmsg, status);
@@ -763,27 +763,14 @@ server in response
763763
Return:
764764
status - error information
765765
--*/
766-
private SecurityStatusPal GenerateToken(byte[] input, int offset, int count, ref byte[] output)
766+
private SecurityStatusPal GenerateToken(ReadOnlySpan<byte> inputBuffer, ref byte[] output)
767767
{
768768
if (NetEventSource.IsEnabled) NetEventSource.Enter(this, $"_refreshCredentialNeeded = {_refreshCredentialNeeded}");
769769

770-
if (offset < 0 || offset > (input == null ? 0 : input.Length))
771-
{
772-
NetEventSource.Fail(this, "Argument 'offset' out of range.");
773-
throw new ArgumentOutOfRangeException(nameof(offset));
774-
}
775-
776-
if (count < 0 || count > (input == null ? 0 : input.Length - offset))
777-
{
778-
NetEventSource.Fail(this, "Argument 'count' out of range.");
779-
throw new ArgumentOutOfRangeException(nameof(count));
780-
}
781-
782770
byte[] result = Array.Empty<byte>();
783771
SecurityStatusPal status = default;
784772
bool cachedCreds = false;
785773
byte[] thumbPrint = null;
786-
ReadOnlySpan<byte> inputBuffer = new ReadOnlySpan<byte>(input, offset, count);
787774

788775
//
789776
// Looping through ASC or ISC with potentially cached credential that could have been
@@ -1155,7 +1142,7 @@ private ProtocolToken GenerateAlertToken()
11551142
byte[] nextmsg = null;
11561143

11571144
SecurityStatusPal status;
1158-
status = GenerateToken(null, 0, 0, ref nextmsg);
1145+
status = GenerateToken(default, ref nextmsg);
11591146

11601147
ProtocolToken token = new ProtocolToken(nextmsg, status);
11611148

src/libraries/System.Net.Security/src/System/Net/Security/SslStream.Implementation.Adapters.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public partial class SslStream
1111
{
1212
private interface ISslIOAdapter
1313
{
14-
ValueTask<int> ReadAsync(byte[] buffer, int offset, int count);
14+
ValueTask<int> ReadAsync(Memory<byte> buffer);
1515
ValueTask<int> ReadLockAsync(Memory<byte> buffer);
1616
Task WriteLockAsync();
1717
ValueTask WriteAsync(byte[] buffer, int offset, int count);
@@ -29,7 +29,7 @@ public AsyncSslIOAdapter(SslStream sslStream, CancellationToken cancellationToke
2929
_sslStream = sslStream;
3030
}
3131

32-
public ValueTask<int> ReadAsync(byte[] buffer, int offset, int count) => _sslStream.InnerStream.ReadAsync(new Memory<byte>(buffer, offset, count), _cancellationToken);
32+
public ValueTask<int> ReadAsync(Memory<byte> buffer) => _sslStream.InnerStream.ReadAsync(buffer, _cancellationToken);
3333

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

@@ -46,7 +46,7 @@ public AsyncSslIOAdapter(SslStream sslStream, CancellationToken cancellationToke
4646

4747
public SyncSslIOAdapter(SslStream sslStream) => _sslStream = sslStream;
4848

49-
public ValueTask<int> ReadAsync(byte[] buffer, int offset, int count) => new ValueTask<int>(_sslStream.InnerStream.Read(buffer, offset, count));
49+
public ValueTask<int> ReadAsync(Memory<byte> buffer) => new ValueTask<int>(_sslStream.InnerStream.Read(buffer.Span));
5050

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

0 commit comments

Comments
 (0)