Skip to content

Commit 24d5043

Browse files
wfurtstephentoub
andauthored
improve reliability of SslStream tests with failing certificate validation (#43570)
* improve reliability of SslStream tests with failing certificate validation * Update src/libraries/System.Net.Security/src/System/Net/Security/SslStream.Implementation.cs Co-authored-by: Stephen Toub <stoub@microsoft.com> * fix failing ALPN test on old OpenSSL Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent bae74dc commit 24d5043

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ private async Task ForceAuthenticationAsync<TIOAdapter>(TIOAdapter adapter, bool
362362
payload = message.Payload;
363363
size = message.Size;
364364
}
365-
else if (message.Failed && _lastFrame.Header.Type == TlsContentType.Handshake)
365+
else if (message.Failed && (_lastFrame.Header.Type == TlsContentType.Handshake || _lastFrame.Header.Type == TlsContentType.ChangeCipherSpec))
366366
{
367367
// If we failed without OS sending out alert, inject one here to be consistent across platforms.
368368
payload = TlsFrameHelper.CreateAlertFrame(_lastFrame.Header.Version, TlsAlertDescription.ProtocolVersion);
@@ -562,7 +562,8 @@ private ProtocolToken ProcessBlob(int frameSize)
562562
}
563563

564564
frameSize = nextHeader.Length + TlsFrameHelper.HeaderSize;
565-
if (nextHeader.Type == TlsContentType.AppData || frameSize > _handshakeBuffer.ActiveLength)
565+
// Can process more handshake frames in single step, but we should avoid processing too much so as to preserve API boundary between handshake and I/O.
566+
if ((nextHeader.Type != TlsContentType.Handshake && nextHeader.Type != TlsContentType.ChangeCipherSpec) || frameSize > _handshakeBuffer.ActiveLength)
566567
{
567568
// We don't have full frame left or we already have app data which needs to be processed by decrypt.
568569
break;

src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamAlertsTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ public async Task SslStream_StreamToStream_HandshakeAlert_Ok()
2929
using (var server = new SslStream(stream2, true, FailClientCertificate))
3030
using (X509Certificate2 certificate = Configuration.Certificates.GetServerCertificate())
3131
{
32+
int timeout = TestConfiguration.PassingTestTimeoutMilliseconds;
33+
3234
Task serverAuth = server.AuthenticateAsServerAsync(certificate);
33-
await client.AuthenticateAsClientAsync(certificate.GetNameInfo(X509NameType.SimpleName, false));
35+
await client.AuthenticateAsClientAsync(certificate.GetNameInfo(X509NameType.SimpleName, false)).TimeoutAfter(timeout);
3436

3537
byte[] buffer = new byte[1024];
3638

3739
// Schannel semantics require that Decrypt is called to receive an alert.
3840
await client.WriteAsync(buffer, 0, buffer.Length);
39-
var exception = await Assert.ThrowsAsync<IOException>(() => client.ReadAsync(buffer, 0, buffer.Length));
41+
var exception = await Assert.ThrowsAsync<IOException>(() => client.ReadAsync(buffer, 0, buffer.Length)).TimeoutAfter(timeout);
4042

4143
Assert.IsType<Win32Exception>(exception.InnerException);
4244
var win32ex = (Win32Exception)exception.InnerException;
@@ -45,7 +47,7 @@ public async Task SslStream_StreamToStream_HandshakeAlert_Ok()
4547
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd721886(v=vs.85).aspx
4648
Assert.Equal(SEC_E_CERT_UNKNOWN, unchecked((uint)win32ex.NativeErrorCode));
4749

48-
await Assert.ThrowsAsync<AuthenticationException>(() => serverAuth);
50+
await Assert.ThrowsAsync<AuthenticationException>(() => serverAuth).TimeoutAfter(timeout);
4951

5052
await Assert.ThrowsAsync<AuthenticationException>(() => server.WriteAsync(buffer, 0, buffer.Length));
5153
await Assert.ThrowsAsync<AuthenticationException>(() => server.ReadAsync(buffer, 0, buffer.Length));

0 commit comments

Comments
 (0)