@@ -585,6 +585,9 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
585
585
try
586
586
{
587
587
var certificate = new X509Certificate2 ( cs . CertificateFile , cs . CertificatePassword ) ;
588
+ #if ! NET451
589
+ m_clientCertificate = certificate ;
590
+ #endif
588
591
clientCertificates = new X509CertificateCollection { certificate } ;
589
592
}
590
593
catch ( CryptographicException ex )
@@ -601,6 +604,9 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
601
604
try
602
605
{
603
606
var caCertificate = new X509Certificate2 ( cs . CACertificateFile ) ;
607
+ #if ! NET451
608
+ m_serverCertificate = caCertificate ;
609
+ #endif
604
610
caCertificateChain = new X509Chain
605
611
{
606
612
ChainPolicy =
@@ -619,9 +625,9 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
619
625
}
620
626
}
621
627
622
- X509Certificate LocalCertificateCb ( object lcbSender , string lcbTargetHost , X509CertificateCollection lcbLocalCertificates , X509Certificate lcbRemoteCertificate , string [ ] lcbAcceptableIssuers ) => lcbLocalCertificates [ 0 ] ;
628
+ X509Certificate ValidateLocalCertificate ( object lcbSender , string lcbTargetHost , X509CertificateCollection lcbLocalCertificates , X509Certificate lcbRemoteCertificate , string [ ] lcbAcceptableIssuers ) => lcbLocalCertificates [ 0 ] ;
623
629
624
- bool RemoteCertificateCb ( object rcbSender , X509Certificate rcbCertificate , X509Chain rcbChain , SslPolicyErrors rcbPolicyErrors )
630
+ bool ValidateRemoteCertificate ( object rcbSender , X509Certificate rcbCertificate , X509Chain rcbChain , SslPolicyErrors rcbPolicyErrors )
625
631
{
626
632
if ( cs . SslMode == MySqlSslMode . Preferred || cs . SslMode == MySqlSslMode . Required )
627
633
return true ;
@@ -644,12 +650,9 @@ bool RemoteCertificateCb(object rcbSender, X509Certificate rcbCertificate, X509C
644
650
645
651
SslStream sslStream ;
646
652
if ( clientCertificates == null )
647
- sslStream = new SslStream ( m_networkStream , false ,
648
- new RemoteCertificateValidationCallback ( ( Func < object , X509Certificate , X509Chain , SslPolicyErrors , bool > ) RemoteCertificateCb ) ) ;
653
+ sslStream = new SslStream ( m_networkStream , false , ValidateRemoteCertificate ) ;
649
654
else
650
- sslStream = new SslStream ( m_networkStream , false ,
651
- new RemoteCertificateValidationCallback ( ( Func < object , X509Certificate , X509Chain , SslPolicyErrors , bool > ) RemoteCertificateCb ) ,
652
- new LocalCertificateSelectionCallback ( ( Func < object , string , X509CertificateCollection , X509Certificate , string [ ] , X509Certificate > ) LocalCertificateCb ) ) ;
655
+ sslStream = new SslStream ( m_networkStream , false , ValidateRemoteCertificate , ValidateLocalCertificate ) ;
653
656
654
657
// SslProtocols.Tls1.2 throws an exception in Windows, see https://github.com/mysql-net/MySqlConnector/pull/101
655
658
var sslProtocols = SslProtocols . Tls | SslProtocols . Tls11 ;
@@ -700,6 +703,10 @@ private void ShutdownSocket()
700
703
Utility . Dispose ( ref m_networkStream ) ;
701
704
SafeDispose ( ref m_tcpClient ) ;
702
705
SafeDispose ( ref m_socket ) ;
706
+ #if ! NET451
707
+ Utility . Dispose ( ref m_clientCertificate ) ;
708
+ Utility . Dispose ( ref m_serverCertificate ) ;
709
+ #endif
703
710
}
704
711
705
712
/// <summary>
@@ -798,6 +805,10 @@ private enum State
798
805
TcpClient m_tcpClient ;
799
806
Socket m_socket ;
800
807
NetworkStream m_networkStream ;
808
+ #if ! NET451
809
+ IDisposable m_clientCertificate ;
810
+ IDisposable m_serverCertificate ;
811
+ #endif
801
812
IPayloadHandler m_payloadHandler ;
802
813
MySqlCommand m_activeCommand ;
803
814
MySqlDataReader m_activeReader ;
0 commit comments