Skip to content

Commit 9c58ff9

Browse files
authored
Merge pull request #121 from caleblloyd/f_cert_allocation
reduce certificate allocations
2 parents d537c5a + 85197ff commit 9c58ff9

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/MySqlConnector/Serialization/ConnectionSettings.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.IO;
43
using System.Runtime.InteropServices;
5-
using System.Security.Cryptography;
6-
using System.Security.Cryptography.X509Certificates;
74
using MySql.Data.MySqlClient;
85

96
namespace MySql.Data.Serialization
@@ -34,19 +31,8 @@ public ConnectionSettings(MySqlConnectionStringBuilder csb)
3431

3532
// SSL/TLS Options
3633
SslMode = csb.SslMode;
37-
if (SslMode != MySqlSslMode.None)
38-
{
39-
try
40-
{
41-
Certificate = new X509Certificate2(csb.CertificateFile, csb.CertificatePassword);
42-
}
43-
catch (CryptographicException ex)
44-
{
45-
if (!File.Exists(csb.CertificateFile))
46-
throw new MySqlException("Cannot find SSL Certificate File", ex);
47-
throw new MySqlException("Either the SSL Certificate Password is incorrect or the SSL Certificate File is invalid", ex);
48-
}
49-
}
34+
CertificateFile = csb.CertificateFile;
35+
CertificatePassword = csb.CertificatePassword;
5036

5137
// Connection Pooling Options
5238
Pooling = csb.Pooling;
@@ -83,7 +69,8 @@ private ConnectionSettings(ConnectionSettings other, bool? useCompression)
8369

8470
// SSL/TLS Options
8571
SslMode = other.SslMode;
86-
Certificate = other.Certificate;
72+
CertificateFile = other.CertificateFile;
73+
CertificatePassword = other.CertificatePassword;
8774

8875
// Connection Pooling Options
8976
Pooling = other.Pooling;
@@ -114,7 +101,8 @@ private ConnectionSettings(ConnectionSettings other, bool? useCompression)
114101

115102
// SSL/TLS Options
116103
internal readonly MySqlSslMode SslMode;
117-
internal readonly X509Certificate2 Certificate;
104+
internal readonly string CertificateFile;
105+
internal readonly string CertificatePassword;
118106

119107
// Connection Pooling Options
120108
internal readonly bool Pooling;
@@ -134,3 +122,4 @@ private ConnectionSettings(ConnectionSettings other, bool? useCompression)
134122
}
135123

136124
}
125+

src/MySqlConnector/Serialization/MySqlSession.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Net.Sockets;
66
using System.Runtime.InteropServices;
77
using System.Security.Authentication;
8+
using System.Security.Cryptography;
89
using System.Security.Cryptography.X509Certificates;
910
using System.Text;
1011
using System.Threading;
@@ -299,6 +300,18 @@ private async Task<bool> OpenUnixSocketAsync(ConnectionSettings cs, IOBehavior i
299300

300301
private async Task InitSslAsync(ConnectionSettings cs, IOBehavior ioBehavior, CancellationToken cancellationToken)
301302
{
303+
X509Certificate2 certificate;
304+
try
305+
{
306+
certificate = new X509Certificate2(cs.CertificateFile, cs.CertificatePassword);
307+
}
308+
catch (CryptographicException ex)
309+
{
310+
if (!File.Exists(cs.CertificateFile))
311+
throw new MySqlException("Cannot find SSL Certificate File", ex);
312+
throw new MySqlException("Either the SSL Certificate Password is incorrect or the SSL Certificate File is invalid", ex);
313+
}
314+
302315
Func<object, string, X509CertificateCollection, X509Certificate, string[], X509Certificate> localCertificateCb =
303316
(lcbSender, lcbTargetHost, lcbLocalCertificates, lcbRemoteCertificate, lcbAcceptableIssuers) => lcbLocalCertificates[0];
304317

@@ -319,7 +332,7 @@ private async Task InitSslAsync(ConnectionSettings cs, IOBehavior ioBehavior, Ca
319332
var sslStream = new SslStream(m_networkStream, false,
320333
new RemoteCertificateValidationCallback(remoteCertificateCb),
321334
new LocalCertificateSelectionCallback(localCertificateCb));
322-
var clientCertificates = new X509CertificateCollection { cs.Certificate };
335+
var clientCertificates = new X509CertificateCollection { certificate };
323336

324337
// SslProtocols.Tls1.2 throws an exception in Windows, see https://github.com/mysql-net/MySqlConnector/pull/101
325338
var sslProtocols = SslProtocols.Tls | SslProtocols.Tls11;

0 commit comments

Comments
 (0)