@@ -28,7 +28,7 @@ internal sealed partial class SmtpConnection
28
28
private readonly EventHandler ? _onCloseHandler ;
29
29
internal SmtpTransport ? _parent ;
30
30
private readonly SmtpClient ? _client ;
31
- private NetworkStream ? _networkStream ;
31
+ private Stream ? _stream ;
32
32
internal TcpClient ? _tcpClient ;
33
33
private SmtpReplyReaderFactory ? _responseReader ;
34
34
@@ -82,7 +82,7 @@ internal X509CertificateCollection? ClientCertificates
82
82
internal void InitializeConnection ( string host , int port )
83
83
{
84
84
_tcpClient ! . Connect ( host , port ) ;
85
- _networkStream = _tcpClient . GetStream ( ) ;
85
+ _stream = _tcpClient . GetStream ( ) ;
86
86
}
87
87
88
88
internal IAsyncResult BeginInitializeConnection ( string host , int port , AsyncCallback ? callback , object ? state )
@@ -93,7 +93,7 @@ internal IAsyncResult BeginInitializeConnection(string host, int port, AsyncCall
93
93
internal void EndInitializeConnection ( IAsyncResult result )
94
94
{
95
95
_tcpClient ! . EndConnect ( result ) ;
96
- _networkStream = _tcpClient . GetStream ( ) ;
96
+ _stream = _tcpClient . GetStream ( ) ;
97
97
}
98
98
99
99
internal IAsyncResult BeginGetConnection ( ContextAwareResult outerResult , AsyncCallback ? callback , object ? state , string host , int port )
@@ -105,18 +105,18 @@ internal IAsyncResult BeginGetConnection(ContextAwareResult outerResult, AsyncCa
105
105
106
106
internal IAsyncResult BeginFlush ( AsyncCallback ? callback , object ? state )
107
107
{
108
- return _networkStream ! . BeginWrite ( _bufferBuilder . GetBuffer ( ) , 0 , _bufferBuilder . Length , callback , state ) ;
108
+ return _stream ! . BeginWrite ( _bufferBuilder . GetBuffer ( ) , 0 , _bufferBuilder . Length , callback , state ) ;
109
109
}
110
110
111
111
internal void EndFlush ( IAsyncResult result )
112
112
{
113
- _networkStream ! . EndWrite ( result ) ;
113
+ _stream ! . EndWrite ( result ) ;
114
114
_bufferBuilder . Reset ( ) ;
115
115
}
116
116
117
117
internal void Flush ( )
118
118
{
119
- _networkStream ! . Write ( _bufferBuilder . GetBuffer ( ) , 0 , _bufferBuilder . Length ) ;
119
+ _stream ! . Write ( _bufferBuilder . GetBuffer ( ) , 0 , _bufferBuilder . Length ) ;
120
120
_bufferBuilder . Reset ( ) ;
121
121
}
122
122
@@ -150,7 +150,7 @@ private void ShutdownConnection(bool isAbort)
150
150
finally
151
151
{
152
152
//free cbt buffer
153
- _networkStream ? . Close ( ) ;
153
+ _stream ? . Close ( ) ;
154
154
_tcpClient . Dispose ( ) ;
155
155
}
156
156
}
@@ -190,7 +190,7 @@ internal void GetConnection(string host, int port)
190
190
}
191
191
192
192
InitializeConnection ( host , port ) ;
193
- _responseReader = new SmtpReplyReaderFactory ( _networkStream ! ) ;
193
+ _responseReader = new SmtpReplyReaderFactory ( _stream ! ) ;
194
194
195
195
LineInfo info = _responseReader . GetNextReplyReader ( ) . ReadLine ( ) ;
196
196
@@ -225,17 +225,25 @@ internal void GetConnection(string host, int port)
225
225
if ( ! _serverSupportsStartTls )
226
226
{
227
227
// Either TLS is already established or server does not support TLS
228
- if ( ! ( _networkStream is TlsStream ) )
228
+ if ( ! ( _stream is SslStream ) )
229
229
{
230
230
throw new SmtpException ( SR . MailServerDoesNotSupportStartTls ) ;
231
231
}
232
232
}
233
233
234
234
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 ) ;
239
247
240
248
// According to RFC 3207: The client SHOULD send an EHLO command
241
249
// as the first command after a successful TLS negotiation.
@@ -362,7 +370,7 @@ internal static void EndGetConnection(IAsyncResult result)
362
370
363
371
internal Stream GetClosableStream ( )
364
372
{
365
- ClosableStream cs = new ClosableStream ( _networkStream ! , _onCloseHandler ) ;
373
+ ClosableStream cs = new ClosableStream ( _stream ! , _onCloseHandler ) ;
366
374
_isStreamOpen = true ;
367
375
return cs ;
368
376
}
@@ -460,7 +468,7 @@ private static void InitializeConnectionCallback(IAsyncResult result)
460
468
461
469
private void Handshake ( )
462
470
{
463
- _connection . _responseReader = new SmtpReplyReaderFactory ( _connection . _networkStream ! ) ;
471
+ _connection . _responseReader = new SmtpReplyReaderFactory ( _connection . _stream ! ) ;
464
472
465
473
SmtpReplyReader reader = _connection . Reader ! . GetNextReplyReader ( ) ;
466
474
IAsyncResult result = reader . BeginReadLine ( s_handshakeCallback , this ) ;
@@ -533,10 +541,10 @@ private bool SendEHello()
533
541
{
534
542
_connection . _extensions = EHelloCommand . EndSend ( result ) ;
535
543
_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
537
545
// that we sent after TLS handshake compelted. So skip TLS and
538
546
// continue with Authenticate.
539
- if ( _connection . _networkStream is TlsStream )
547
+ if ( _connection . _stream is SslStream )
540
548
{
541
549
Authenticate ( ) ;
542
550
return true ;
@@ -547,7 +555,7 @@ private bool SendEHello()
547
555
if ( ! _connection . _serverSupportsStartTls )
548
556
{
549
557
// Either TLS is already established or server does not support TLS
550
- if ( ! ( _connection . _networkStream is TlsStream ) )
558
+ if ( ! ( _connection . _stream is SslStream ) )
551
559
{
552
560
throw new SmtpException ( SR . MailServerDoesNotSupportStartTls ) ;
553
561
}
@@ -579,7 +587,7 @@ private static void SendEHelloCallback(IAsyncResult result)
579
587
// If we already have a SSlStream, this is the second EHLO cmd
580
588
// that we sent after TLS handshake compelted. So skip TLS and
581
589
// continue with Authenticate.
582
- if ( thisPtr . _connection . _networkStream is TlsStream )
590
+ if ( thisPtr . _connection . _stream is SslStream )
583
591
{
584
592
thisPtr . Authenticate ( ) ;
585
593
return ;
@@ -606,7 +614,7 @@ private static void SendEHelloCallback(IAsyncResult result)
606
614
if ( ! thisPtr . _connection . _serverSupportsStartTls )
607
615
{
608
616
// 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 ) )
610
618
{
611
619
throw new SmtpException ( SR . MailServerDoesNotSupportStartTls ) ;
612
620
}
@@ -663,7 +671,7 @@ private bool SendStartTls()
663
671
if ( result . CompletedSynchronously )
664
672
{
665
673
StartTlsCommand . EndSend ( result ) ;
666
- TlsStreamAuthenticate ( ) ;
674
+ SslStreamAuthenticate ( ) ;
667
675
return true ;
668
676
}
669
677
return false ;
@@ -677,7 +685,7 @@ private static void SendStartTlsCallback(IAsyncResult result)
677
685
try
678
686
{
679
687
StartTlsCommand . EndSend ( result ) ;
680
- thisPtr . TlsStreamAuthenticate ( ) ;
688
+ thisPtr . SslStreamAuthenticate ( ) ;
681
689
}
682
690
catch ( Exception e )
683
691
{
@@ -686,29 +694,39 @@ private static void SendStartTlsCallback(IAsyncResult result)
686
694
}
687
695
}
688
696
689
- private bool TlsStreamAuthenticate ( )
697
+ private bool SslStreamAuthenticate ( )
690
698
{
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
+
693
711
if ( result . CompletedSynchronously )
694
712
{
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 ) ;
697
715
SendEHello ( ) ;
698
716
return true ;
699
717
}
700
718
return false ;
701
719
}
702
720
703
- private static void TlsStreamAuthenticateCallback ( IAsyncResult result )
721
+ private static void SslStreamAuthenticateCallback ( IAsyncResult result )
704
722
{
705
723
if ( ! result . CompletedSynchronously )
706
724
{
707
725
ConnectAndHandshakeAsyncResult thisPtr = ( ConnectAndHandshakeAsyncResult ) result . AsyncState ! ;
708
726
try
709
727
{
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 ) ;
712
730
thisPtr . SendEHello ( ) ;
713
731
}
714
732
catch ( Exception e )
0 commit comments