Skip to content

Commit c9e1d15

Browse files
rzikmMihaZupan
andauthored
Replace TlsStream type by using SslStream directly (#106451)
* Remove TlsStream from System.Net.Mail * Remove TlsStream from System.Net.Requests * Delete TlsStream.cs * Update src/libraries/System.Net.Requests/src/System/Net/FtpControlStream.cs Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com> * Update src/libraries/System.Net.Requests/src/System/Net/FtpDataStream.cs --------- Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
1 parent 9230f2b commit c9e1d15

File tree

8 files changed

+179
-236
lines changed

8 files changed

+179
-236
lines changed

src/libraries/Common/src/System/Net/TlsStream.cs

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/libraries/System.Net.Mail/src/System.Net.Mail.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@
110110
Link="Common\System\Net\DebugSafeHandleZeroOrMinusOneIsInvalid.cs" />
111111
<Compile Include="$(CommonPath)System\Net\DebugSafeHandle.cs"
112112
Link="Common\System\Net\DebugSafeHandle.cs" />
113-
<Compile Include="$(CommonPath)System\Net\TlsStream.cs"
114-
Link="Common\System\Net\TlsStream.cs" />
115113
<Compile Include="$(CommonPath)System\Net\InternalException.cs"
116114
Link="Common\System\Net\InternalException.cs" />
117115
<Compile Include="$(CommonPath)System\Net\ExceptionCheck.cs"

src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpConnection.cs

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal sealed partial class SmtpConnection
2828
private readonly EventHandler? _onCloseHandler;
2929
internal SmtpTransport? _parent;
3030
private readonly SmtpClient? _client;
31-
private NetworkStream? _networkStream;
31+
private Stream? _stream;
3232
internal TcpClient? _tcpClient;
3333
private SmtpReplyReaderFactory? _responseReader;
3434

@@ -82,7 +82,7 @@ internal X509CertificateCollection? ClientCertificates
8282
internal void InitializeConnection(string host, int port)
8383
{
8484
_tcpClient!.Connect(host, port);
85-
_networkStream = _tcpClient.GetStream();
85+
_stream = _tcpClient.GetStream();
8686
}
8787

8888
internal IAsyncResult BeginInitializeConnection(string host, int port, AsyncCallback? callback, object? state)
@@ -93,7 +93,7 @@ internal IAsyncResult BeginInitializeConnection(string host, int port, AsyncCall
9393
internal void EndInitializeConnection(IAsyncResult result)
9494
{
9595
_tcpClient!.EndConnect(result);
96-
_networkStream = _tcpClient.GetStream();
96+
_stream = _tcpClient.GetStream();
9797
}
9898

9999
internal IAsyncResult BeginGetConnection(ContextAwareResult outerResult, AsyncCallback? callback, object? state, string host, int port)
@@ -105,18 +105,18 @@ internal IAsyncResult BeginGetConnection(ContextAwareResult outerResult, AsyncCa
105105

106106
internal IAsyncResult BeginFlush(AsyncCallback? callback, object? state)
107107
{
108-
return _networkStream!.BeginWrite(_bufferBuilder.GetBuffer(), 0, _bufferBuilder.Length, callback, state);
108+
return _stream!.BeginWrite(_bufferBuilder.GetBuffer(), 0, _bufferBuilder.Length, callback, state);
109109
}
110110

111111
internal void EndFlush(IAsyncResult result)
112112
{
113-
_networkStream!.EndWrite(result);
113+
_stream!.EndWrite(result);
114114
_bufferBuilder.Reset();
115115
}
116116

117117
internal void Flush()
118118
{
119-
_networkStream!.Write(_bufferBuilder.GetBuffer(), 0, _bufferBuilder.Length);
119+
_stream!.Write(_bufferBuilder.GetBuffer(), 0, _bufferBuilder.Length);
120120
_bufferBuilder.Reset();
121121
}
122122

@@ -150,7 +150,7 @@ private void ShutdownConnection(bool isAbort)
150150
finally
151151
{
152152
//free cbt buffer
153-
_networkStream?.Close();
153+
_stream?.Close();
154154
_tcpClient.Dispose();
155155
}
156156
}
@@ -190,7 +190,7 @@ internal void GetConnection(string host, int port)
190190
}
191191

192192
InitializeConnection(host, port);
193-
_responseReader = new SmtpReplyReaderFactory(_networkStream!);
193+
_responseReader = new SmtpReplyReaderFactory(_stream!);
194194

195195
LineInfo info = _responseReader.GetNextReplyReader().ReadLine();
196196

@@ -225,17 +225,25 @@ internal void GetConnection(string host, int port)
225225
if (!_serverSupportsStartTls)
226226
{
227227
// Either TLS is already established or server does not support TLS
228-
if (!(_networkStream is TlsStream))
228+
if (!(_stream is SslStream))
229229
{
230230
throw new SmtpException(SR.MailServerDoesNotSupportStartTls);
231231
}
232232
}
233233

234234
StartTlsCommand.Send(this);
235-
TlsStream tlsStream = new TlsStream(_networkStream!, _tcpClient!.Client, host, _clientCertificates);
236-
tlsStream.AuthenticateAsClient();
237-
_networkStream = tlsStream;
238-
_responseReader = new SmtpReplyReaderFactory(_networkStream);
235+
#pragma warning disable SYSLIB0014 // ServicePointManager is obsolete
236+
SslStream sslStream = new SslStream(_stream!, false, ServicePointManager.ServerCertificateValidationCallback);
237+
238+
sslStream.AuthenticateAsClient(
239+
host,
240+
_clientCertificates,
241+
(SslProtocols)ServicePointManager.SecurityProtocol, // enums use same values
242+
ServicePointManager.CheckCertificateRevocationList);
243+
#pragma warning restore SYSLIB0014 // ServicePointManager is obsolete
244+
245+
_stream = sslStream;
246+
_responseReader = new SmtpReplyReaderFactory(_stream);
239247

240248
// According to RFC 3207: The client SHOULD send an EHLO command
241249
// as the first command after a successful TLS negotiation.
@@ -362,7 +370,7 @@ internal static void EndGetConnection(IAsyncResult result)
362370

363371
internal Stream GetClosableStream()
364372
{
365-
ClosableStream cs = new ClosableStream(_networkStream!, _onCloseHandler);
373+
ClosableStream cs = new ClosableStream(_stream!, _onCloseHandler);
366374
_isStreamOpen = true;
367375
return cs;
368376
}
@@ -460,7 +468,7 @@ private static void InitializeConnectionCallback(IAsyncResult result)
460468

461469
private void Handshake()
462470
{
463-
_connection._responseReader = new SmtpReplyReaderFactory(_connection._networkStream!);
471+
_connection._responseReader = new SmtpReplyReaderFactory(_connection._stream!);
464472

465473
SmtpReplyReader reader = _connection.Reader!.GetNextReplyReader();
466474
IAsyncResult result = reader.BeginReadLine(s_handshakeCallback, this);
@@ -533,10 +541,10 @@ private bool SendEHello()
533541
{
534542
_connection._extensions = EHelloCommand.EndSend(result);
535543
_connection.ParseExtensions(_connection._extensions);
536-
// If we already have a TlsStream, this is the second EHLO cmd
544+
// If we already have a SslStream, this is the second EHLO cmd
537545
// that we sent after TLS handshake compelted. So skip TLS and
538546
// continue with Authenticate.
539-
if (_connection._networkStream is TlsStream)
547+
if (_connection._stream is SslStream)
540548
{
541549
Authenticate();
542550
return true;
@@ -547,7 +555,7 @@ private bool SendEHello()
547555
if (!_connection._serverSupportsStartTls)
548556
{
549557
// Either TLS is already established or server does not support TLS
550-
if (!(_connection._networkStream is TlsStream))
558+
if (!(_connection._stream is SslStream))
551559
{
552560
throw new SmtpException(SR.MailServerDoesNotSupportStartTls);
553561
}
@@ -579,7 +587,7 @@ private static void SendEHelloCallback(IAsyncResult result)
579587
// If we already have a SSlStream, this is the second EHLO cmd
580588
// that we sent after TLS handshake compelted. So skip TLS and
581589
// continue with Authenticate.
582-
if (thisPtr._connection._networkStream is TlsStream)
590+
if (thisPtr._connection._stream is SslStream)
583591
{
584592
thisPtr.Authenticate();
585593
return;
@@ -606,7 +614,7 @@ private static void SendEHelloCallback(IAsyncResult result)
606614
if (!thisPtr._connection._serverSupportsStartTls)
607615
{
608616
// Either TLS is already established or server does not support TLS
609-
if (!(thisPtr._connection._networkStream is TlsStream))
617+
if (!(thisPtr._connection._stream is SslStream))
610618
{
611619
throw new SmtpException(SR.MailServerDoesNotSupportStartTls);
612620
}
@@ -663,7 +671,7 @@ private bool SendStartTls()
663671
if (result.CompletedSynchronously)
664672
{
665673
StartTlsCommand.EndSend(result);
666-
TlsStreamAuthenticate();
674+
SslStreamAuthenticate();
667675
return true;
668676
}
669677
return false;
@@ -677,7 +685,7 @@ private static void SendStartTlsCallback(IAsyncResult result)
677685
try
678686
{
679687
StartTlsCommand.EndSend(result);
680-
thisPtr.TlsStreamAuthenticate();
688+
thisPtr.SslStreamAuthenticate();
681689
}
682690
catch (Exception e)
683691
{
@@ -686,29 +694,39 @@ private static void SendStartTlsCallback(IAsyncResult result)
686694
}
687695
}
688696

689-
private bool TlsStreamAuthenticate()
697+
private bool SslStreamAuthenticate()
690698
{
691-
_connection._networkStream = new TlsStream(_connection._networkStream!, _connection._tcpClient!.Client, _host, _connection._clientCertificates);
692-
IAsyncResult result = ((TlsStream)_connection._networkStream).BeginAuthenticateAsClient(TlsStreamAuthenticateCallback, this);
699+
#pragma warning disable SYSLIB0014 // ServicePointManager is obsolete
700+
_connection._stream = new SslStream(_connection._stream!, false, ServicePointManager.ServerCertificateValidationCallback);
701+
702+
IAsyncResult result = ((SslStream)_connection._stream).BeginAuthenticateAsClient(
703+
_host,
704+
_connection._clientCertificates,
705+
(SslProtocols)ServicePointManager.SecurityProtocol, // enums use same values
706+
ServicePointManager.CheckCertificateRevocationList,
707+
SslStreamAuthenticateCallback,
708+
this);
709+
#pragma warning restore SYSLIB0014 // ServicePointManager is obsolete
710+
693711
if (result.CompletedSynchronously)
694712
{
695-
((TlsStream)_connection._networkStream).EndAuthenticateAsClient(result);
696-
_connection._responseReader = new SmtpReplyReaderFactory(_connection._networkStream);
713+
((SslStream)_connection._stream).EndAuthenticateAsClient(result);
714+
_connection._responseReader = new SmtpReplyReaderFactory(_connection._stream);
697715
SendEHello();
698716
return true;
699717
}
700718
return false;
701719
}
702720

703-
private static void TlsStreamAuthenticateCallback(IAsyncResult result)
721+
private static void SslStreamAuthenticateCallback(IAsyncResult result)
704722
{
705723
if (!result.CompletedSynchronously)
706724
{
707725
ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState!;
708726
try
709727
{
710-
(thisPtr._connection._networkStream as TlsStream)!.EndAuthenticateAsClient(result);
711-
thisPtr._connection._responseReader = new SmtpReplyReaderFactory(thisPtr._connection._networkStream);
728+
(thisPtr._connection._stream as SslStream)!.EndAuthenticateAsClient(result);
729+
thisPtr._connection._responseReader = new SmtpReplyReaderFactory(thisPtr._connection._stream);
712730
thisPtr.SendEHello();
713731
}
714732
catch (Exception e)

src/libraries/System.Net.Mail/tests/Unit/System.Net.Mail.Unit.Tests.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@
112112
Link="ProductionCode\BufferBuilder.cs" />
113113
<Compile Include="$(CommonPath)DisableRuntimeMarshalling.cs"
114114
Link="Common\DisableRuntimeMarshalling.cs" />
115-
<Compile Include="$(CommonPath)System\Net\TlsStream.cs"
116-
Link="Common\System\Net\TlsStream.cs" />
117115
<Compile Include="$(CommonPath)System\Net\InternalException.cs"
118116
Link="Common\System\Net\InternalException.cs" />
119117
<Compile Include="$(CommonPath)System\Net\LazyAsyncResult.cs"
@@ -140,8 +138,8 @@
140138
Link="Common\System\HexConverter.cs" />
141139
<Compile Include="$(CommonPath)System\Obsoletions.cs"
142140
Link="Common\System\Obsoletions.cs" />
143-
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
144-
Link="Common\System\Text\ValueStringBuilder.cs" />
141+
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
142+
Link="Common\System\Text\ValueStringBuilder.cs" />
145143
</ItemGroup>
146144
<!-- Unix specific files -->
147145
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'unix'">

src/libraries/System.Net.Requests/src/System.Net.Requests.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@
8181
Link="Common\System\Net\ContextAwareResult.cs" />
8282
<Compile Include="$(CommonPath)System\Net\ExceptionCheck.cs"
8383
Link="Common\System\Net\ExceptionCheck.cs" />
84-
<Compile Include="$(CommonPath)System\Net\TlsStream.cs"
85-
Link="Common\System\Net\TlsStream.cs" />
8684
<Compile Include="$(CommonPath)System\Net\SecurityProtocol.cs"
8785
Link="Common\System\Net\SecurityProtocol.cs" />
8886
<Compile Include="$(CommonPath)System\NotImplemented.cs"

0 commit comments

Comments
 (0)