Skip to content

Commit 2a73bcc

Browse files
committed
Dispose X509 certificates. Fixes #275
On .NET 4.6 and .NET Standard 1.0 (and later), X509Certificate implements IDisposable and should be disposed to free resources.
1 parent 95a1463 commit 2a73bcc

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/MySqlConnector/Serialization/MySqlSession.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
585585
try
586586
{
587587
var certificate = new X509Certificate2(cs.CertificateFile, cs.CertificatePassword);
588+
#if !NET451
589+
m_clientCertificate = certificate;
590+
#endif
588591
clientCertificates = new X509CertificateCollection {certificate};
589592
}
590593
catch (CryptographicException ex)
@@ -601,6 +604,9 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
601604
try
602605
{
603606
var caCertificate = new X509Certificate2(cs.CACertificateFile);
607+
#if !NET451
608+
m_serverCertificate = caCertificate;
609+
#endif
604610
caCertificateChain = new X509Chain
605611
{
606612
ChainPolicy =
@@ -619,9 +625,9 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
619625
}
620626
}
621627

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];
623629

624-
bool RemoteCertificateCb(object rcbSender, X509Certificate rcbCertificate, X509Chain rcbChain, SslPolicyErrors rcbPolicyErrors)
630+
bool ValidateRemoteCertificate(object rcbSender, X509Certificate rcbCertificate, X509Chain rcbChain, SslPolicyErrors rcbPolicyErrors)
625631
{
626632
if (cs.SslMode == MySqlSslMode.Preferred || cs.SslMode == MySqlSslMode.Required)
627633
return true;
@@ -644,12 +650,9 @@ bool RemoteCertificateCb(object rcbSender, X509Certificate rcbCertificate, X509C
644650

645651
SslStream sslStream;
646652
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);
649654
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);
653656

654657
// SslProtocols.Tls1.2 throws an exception in Windows, see https://github.com/mysql-net/MySqlConnector/pull/101
655658
var sslProtocols = SslProtocols.Tls | SslProtocols.Tls11;
@@ -700,6 +703,10 @@ private void ShutdownSocket()
700703
Utility.Dispose(ref m_networkStream);
701704
SafeDispose(ref m_tcpClient);
702705
SafeDispose(ref m_socket);
706+
#if !NET451
707+
Utility.Dispose(ref m_clientCertificate);
708+
Utility.Dispose(ref m_serverCertificate);
709+
#endif
703710
}
704711

705712
/// <summary>
@@ -798,6 +805,10 @@ private enum State
798805
TcpClient m_tcpClient;
799806
Socket m_socket;
800807
NetworkStream m_networkStream;
808+
#if !NET451
809+
IDisposable m_clientCertificate;
810+
IDisposable m_serverCertificate;
811+
#endif
801812
IPayloadHandler m_payloadHandler;
802813
MySqlCommand m_activeCommand;
803814
MySqlDataReader m_activeReader;

0 commit comments

Comments
 (0)