|
7 | 7 | using System.Net.Security;
|
8 | 8 | using System.Net.Sockets;
|
9 | 9 | using System.Reflection;
|
| 10 | +using System.Runtime.ConstrainedExecution; |
10 | 11 | using System.Security.Authentication;
|
11 | 12 | using System.Security.Cryptography;
|
12 | 13 | using System.Security.Cryptography.X509Certificates;
|
@@ -1280,7 +1281,11 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
|
1280 | 1281 | {
|
1281 | 1282 | try
|
1282 | 1283 | {
|
| 1284 | +#if NET9_0_OR_GREATER |
| 1285 | + var certificate = X509CertificateLoader.LoadPkcs12FromFile(cs.CertificateFile, cs.CertificatePassword, X509KeyStorageFlags.MachineKeySet); |
| 1286 | +#else |
1283 | 1287 | var certificate = new X509Certificate2(cs.CertificateFile, cs.CertificatePassword, X509KeyStorageFlags.MachineKeySet);
|
| 1288 | +#endif |
1284 | 1289 | if (!certificate.HasPrivateKey)
|
1285 | 1290 | {
|
1286 | 1291 | certificate.Dispose();
|
@@ -1352,7 +1357,9 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
|
1352 | 1357 | {
|
1353 | 1358 | // load the certificate at this index; note that 'new X509Certificate' stops at the end of the first certificate it loads
|
1354 | 1359 | 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 |
1356 | 1363 | var caCertificate = new X509Certificate2(certificateBytes.AsSpan(index, (nextIndex == -1 ? certificateBytes.Length : nextIndex) - index), default(ReadOnlySpan<char>), X509KeyStorageFlags.MachineKeySet);
|
1357 | 1364 | #else
|
1358 | 1365 | 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
|
1522 | 1529 | // Schannel has a bug where ephemeral keys can't be loaded: https://github.com/dotnet/runtime/issues/23749#issuecomment-485947319
|
1523 | 1530 | // The workaround is to export the key (which may make it "Perphemeral"): https://github.com/dotnet/runtime/issues/23749#issuecomment-739895373
|
1524 | 1531 | 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 |
1525 | 1535 | m_clientCertificate = new X509Certificate2(m_clientCertificate.Export(X509ContentType.Pkcs12));
|
| 1536 | +#endif |
1526 | 1537 | oldCertificate.Dispose();
|
1527 | 1538 | }
|
1528 | 1539 | return [m_clientCertificate];
|
@@ -1593,7 +1604,7 @@ X509CertificateCollection LoadCertificate(string sslKeyFile, string sslCertifica
|
1593 | 1604 | throw new MySqlException("Could not load the client key from " + sslCertificateFile, ex);
|
1594 | 1605 | }
|
1595 | 1606 | #endif
|
1596 |
| - } |
| 1607 | + } |
1597 | 1608 | }
|
1598 | 1609 |
|
1599 | 1610 | #if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
|
|
0 commit comments