Skip to content

Commit 5151359

Browse files
committed
Update to .NET 9 Preview 7.
Signed-off-by: Bradley Grainger <[email protected]>
1 parent f913876 commit 5151359

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net.Security;
88
using System.Net.Sockets;
99
using System.Reflection;
10+
using System.Runtime.ConstrainedExecution;
1011
using System.Security.Authentication;
1112
using System.Security.Cryptography;
1213
using System.Security.Cryptography.X509Certificates;
@@ -1280,7 +1281,11 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
12801281
{
12811282
try
12821283
{
1284+
#if NET9_0_OR_GREATER
1285+
var certificate = X509CertificateLoader.LoadPkcs12FromFile(cs.CertificateFile, cs.CertificatePassword, X509KeyStorageFlags.MachineKeySet);
1286+
#else
12831287
var certificate = new X509Certificate2(cs.CertificateFile, cs.CertificatePassword, X509KeyStorageFlags.MachineKeySet);
1288+
#endif
12841289
if (!certificate.HasPrivateKey)
12851290
{
12861291
certificate.Dispose();
@@ -1352,7 +1357,9 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
13521357
{
13531358
// load the certificate at this index; note that 'new X509Certificate' stops at the end of the first certificate it loads
13541359
Log.LoadingCaCertificate(m_logger, Id, index);
1355-
#if NET5_0_OR_GREATER
1360+
#if NET9_0_OR_GREATER
1361+
var caCertificate = X509CertificateLoader.LoadCertificate(certificateBytes.AsSpan(index, (nextIndex == -1 ? certificateBytes.Length : nextIndex) - index));
1362+
#elif NET5_0_OR_GREATER
13561363
var caCertificate = new X509Certificate2(certificateBytes.AsSpan(index, (nextIndex == -1 ? certificateBytes.Length : nextIndex) - index), default(ReadOnlySpan<char>), X509KeyStorageFlags.MachineKeySet);
13571364
#else
13581365
var caCertificate = new X509Certificate2(Utility.ArraySlice(certificateBytes, index, (nextIndex == -1 ? certificateBytes.Length : nextIndex) - index), default(string), X509KeyStorageFlags.MachineKeySet);
@@ -1522,7 +1529,11 @@ X509CertificateCollection LoadCertificate(string sslKeyFile, string sslCertifica
15221529
// Schannel has a bug where ephemeral keys can't be loaded: https://github.com/dotnet/runtime/issues/23749#issuecomment-485947319
15231530
// The workaround is to export the key (which may make it "Perphemeral"): https://github.com/dotnet/runtime/issues/23749#issuecomment-739895373
15241531
var oldCertificate = m_clientCertificate;
1532+
#if NET9_0_OR_GREATER
1533+
m_clientCertificate = X509CertificateLoader.LoadPkcs12(m_clientCertificate.Export(X509ContentType.Pkcs12, default(string?)), null);
1534+
#else
15251535
m_clientCertificate = new X509Certificate2(m_clientCertificate.Export(X509ContentType.Pkcs12));
1536+
#endif
15261537
oldCertificate.Dispose();
15271538
}
15281539
return [m_clientCertificate];
@@ -1593,7 +1604,7 @@ X509CertificateCollection LoadCertificate(string sslKeyFile, string sslCertifica
15931604
throw new MySqlException("Could not load the client key from " + sslCertificateFile, ex);
15941605
}
15951606
#endif
1596-
}
1607+
}
15971608
}
15981609

15991610
#if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER

tests/IntegrationTests/SslTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ public async Task ConnectSslClientCertificateCallback(string certificateFile, st
6464
using var connection = new MySqlConnection(csb.ConnectionString);
6565
connection.ProvideClientCertificatesCallback = x =>
6666
{
67+
#if NET9_0_OR_GREATER
68+
x.Add(X509CertificateLoader.LoadPkcs12FromFile(certificateFilePath, certificateFilePassword));
69+
#else
6770
x.Add(new X509Certificate2(certificateFilePath, certificateFilePassword));
71+
#endif
6872
return default;
6973
};
7074

@@ -119,7 +123,11 @@ public async Task ConnectSslClientCertificateFromCertificateStore(string certFil
119123
// Create a mock of certificate store
120124
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
121125
store.Open(OpenFlags.ReadWrite);
126+
#if NET9_0_OR_GREATER
127+
var certificate = X509CertificateLoader.LoadPkcs12FromFile(Path.Combine(AppConfig.CertsPath, certFile), null);
128+
#else
122129
var certificate = new X509Certificate2(Path.Combine(AppConfig.CertsPath, certFile));
130+
#endif
123131
store.Add(certificate);
124132

125133
var csb = AppConfig.CreateConnectionStringBuilder();

0 commit comments

Comments
 (0)