Skip to content

Commit 3638c11

Browse files
committed
Work around ephemeral PEM bug on Windows. Fixes #1278
1 parent ddcd2cb commit 3638c11

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,14 @@ X509CertificateCollection LoadCertificate(string sslKeyFile, string sslCertifica
15851585
throw new NotSupportedException("SslCert and SslKey connection string options are not supported in netstandard2.0.");
15861586
#elif NET5_0_OR_GREATER
15871587
m_clientCertificate = X509Certificate2.CreateFromPemFile(sslCertificateFile, sslKeyFile);
1588+
if (Utility.IsWindows())
1589+
{
1590+
// Schannel has a bug where ephemeral keys can't be loaded: https://github.com/dotnet/runtime/issues/23749#issuecomment-485947319
1591+
// The workaround is to export the key (which may make it "Perphemeral"): https://github.com/dotnet/runtime/issues/23749#issuecomment-739895373
1592+
var oldCertificate = m_clientCertificate;
1593+
m_clientCertificate = new X509Certificate2(m_clientCertificate.Export(X509ContentType.Pkcs12));
1594+
oldCertificate.Dispose();
1595+
}
15881596
return new() { m_clientCertificate };
15891597
#else
15901598
m_logArguments[1] = sslKeyFile;
@@ -1616,7 +1624,6 @@ X509CertificateCollection LoadCertificate(string sslKeyFile, string sslCertifica
16161624
RSA rsa;
16171625
try
16181626
{
1619-
#pragma warning disable CA1416
16201627
// SslStream on Windows needs a KeyContainerName to be set
16211628
var csp = new CspParameters
16221629
{
@@ -1626,7 +1633,6 @@ X509CertificateCollection LoadCertificate(string sslKeyFile, string sslCertifica
16261633
{
16271634
PersistKeyInCsp = true,
16281635
};
1629-
#pragma warning restore
16301636
}
16311637
catch (PlatformNotSupportedException)
16321638
{

0 commit comments

Comments
 (0)