Skip to content

Commit d40c944

Browse files
committed
Extract LoadCertificate to a local method.
1 parent c561ddb commit d40c944

File tree

1 file changed

+79
-74
lines changed

1 file changed

+79
-74
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,81 +1125,9 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
11251125
}
11261126
}
11271127

1128-
if (cs.SslCertificateFile.Length != 0 && cs.SslKeyFile.Length != 0)
1128+
if (cs.SslKeyFile.Length != 0 && cs.SslCertificateFile.Length != 0)
11291129
{
1130-
#if !NETSTANDARD1_3 && !NETSTANDARD2_0
1131-
m_logArguments[1] = cs.SslKeyFile;
1132-
Log.Debug("Session{0} loading client key from KeyFile '{1}'", m_logArguments);
1133-
string keyPem;
1134-
try
1135-
{
1136-
keyPem = File.ReadAllText(cs.SslKeyFile);
1137-
}
1138-
catch (Exception ex)
1139-
{
1140-
Log.Error(ex, "Session{0} couldn't load client key from KeyFile '{1}'", m_logArguments);
1141-
throw new MySqlException("Could not load client key file: " + cs.SslKeyFile, ex);
1142-
}
1143-
1144-
RSAParameters rsaParameters;
1145-
try
1146-
{
1147-
rsaParameters = Utility.GetRsaParameters(keyPem);
1148-
}
1149-
catch (FormatException ex)
1150-
{
1151-
Log.Error(ex, "Session{0} couldn't load client key from KeyFile '{1}'", m_logArguments);
1152-
throw new MySqlException("Could not load the client key from " + cs.SslKeyFile, ex);
1153-
}
1154-
1155-
try
1156-
{
1157-
RSA rsa;
1158-
try
1159-
{
1160-
#pragma warning disable CA1416
1161-
// SslStream on Windows needs a KeyContainerName to be set
1162-
var csp = new CspParameters
1163-
{
1164-
KeyContainerName = Guid.NewGuid().ToString(),
1165-
};
1166-
rsa = new RSACryptoServiceProvider(csp)
1167-
{
1168-
PersistKeyInCsp = true,
1169-
};
1170-
#pragma warning restore
1171-
}
1172-
catch (PlatformNotSupportedException)
1173-
{
1174-
rsa = RSA.Create();
1175-
}
1176-
rsa.ImportParameters(rsaParameters);
1177-
1178-
#if NET45 || NET461 || NET471
1179-
var certificate = new X509Certificate2(cs.SslCertificateFile, "", X509KeyStorageFlags.MachineKeySet)
1180-
{
1181-
PrivateKey = rsa,
1182-
};
1183-
#else
1184-
X509Certificate2 certificate;
1185-
using (var publicCertificate = new X509Certificate2(cs.SslCertificateFile))
1186-
certificate = publicCertificate.CopyWithPrivateKey(rsa);
1187-
#endif
1188-
1189-
m_clientCertificate = certificate;
1190-
clientCertificates = new() { certificate };
1191-
}
1192-
1193-
catch (CryptographicException ex)
1194-
{
1195-
Log.Error(ex, "Session{0} couldn't load client key from KeyFile '{1}'", m_logArguments);
1196-
if (!File.Exists(cs.SslCertificateFile))
1197-
throw new MySqlException("Cannot find client certificate file: " + cs.SslCertificateFile, ex);
1198-
throw new MySqlException("Could not load the client key from " + cs.SslKeyFile, ex);
1199-
}
1200-
#else
1201-
throw new NotSupportedException("SslCert and SslKey connection string options are not supported in netstandard1.3 or netstandard2.0.");
1202-
#endif
1130+
clientCertificates = LoadCertificate(cs.SslKeyFile, cs.SslCertificateFile);
12031131
}
12041132
else if (cs.CertificateFile.Length != 0)
12051133
{
@@ -1398,6 +1326,83 @@ await sslStream.AuthenticateAsClientAsync(clientAuthenticationOptions.TargetHost
13981326
caCertificateChain?.Reset();
13991327
#else
14001328
caCertificateChain?.Dispose();
1329+
#endif
1330+
}
1331+
1332+
// Returns a X509CertificateCollection containing the single certificate contained in 'sslKeyFile' (PEM private key) and 'sslCertificateFile' (PEM certificate).
1333+
X509CertificateCollection LoadCertificate(string sslKeyFile, string sslCertificateFile)
1334+
{
1335+
#if !NETSTANDARD1_3 && !NETSTANDARD2_0
1336+
m_logArguments[1] = sslKeyFile;
1337+
Log.Debug("Session{0} loading client key from KeyFile '{1}'", m_logArguments);
1338+
string keyPem;
1339+
try
1340+
{
1341+
keyPem = File.ReadAllText(sslKeyFile);
1342+
}
1343+
catch (Exception ex)
1344+
{
1345+
Log.Error(ex, "Session{0} couldn't load client key from KeyFile '{1}'", m_logArguments);
1346+
throw new MySqlException("Could not load client key file: " + sslKeyFile, ex);
1347+
}
1348+
1349+
RSAParameters rsaParameters;
1350+
try
1351+
{
1352+
rsaParameters = Utility.GetRsaParameters(keyPem);
1353+
}
1354+
catch (FormatException ex)
1355+
{
1356+
Log.Error(ex, "Session{0} couldn't load client key from KeyFile '{1}'", m_logArguments);
1357+
throw new MySqlException("Could not load the client key from " + sslKeyFile, ex);
1358+
}
1359+
1360+
try
1361+
{
1362+
RSA rsa;
1363+
try
1364+
{
1365+
#pragma warning disable CA1416
1366+
// SslStream on Windows needs a KeyContainerName to be set
1367+
var csp = new CspParameters
1368+
{
1369+
KeyContainerName = Guid.NewGuid().ToString(),
1370+
};
1371+
rsa = new RSACryptoServiceProvider(csp)
1372+
{
1373+
PersistKeyInCsp = true,
1374+
};
1375+
#pragma warning restore
1376+
}
1377+
catch (PlatformNotSupportedException)
1378+
{
1379+
rsa = RSA.Create();
1380+
}
1381+
rsa.ImportParameters(rsaParameters);
1382+
1383+
#if NET45 || NET461 || NET471
1384+
var certificate = new X509Certificate2(sslCertificateFile, "", X509KeyStorageFlags.MachineKeySet)
1385+
{
1386+
PrivateKey = rsa,
1387+
};
1388+
#else
1389+
X509Certificate2 certificate;
1390+
using (var publicCertificate = new X509Certificate2(sslCertificateFile))
1391+
certificate = publicCertificate.CopyWithPrivateKey(rsa);
1392+
#endif
1393+
1394+
m_clientCertificate = certificate;
1395+
return new() { certificate };
1396+
}
1397+
catch (CryptographicException ex)
1398+
{
1399+
Log.Error(ex, "Session{0} couldn't load client key from KeyFile '{1}'", m_logArguments);
1400+
if (!File.Exists(sslCertificateFile))
1401+
throw new MySqlException("Cannot find client certificate file: " + sslCertificateFile, ex);
1402+
throw new MySqlException("Could not load the client key from " + sslKeyFile, ex);
1403+
}
1404+
#else
1405+
throw new NotSupportedException("SslCert and SslKey connection string options are not supported in netstandard1.3 or netstandard2.0.");
14011406
#endif
14021407
}
14031408
}

0 commit comments

Comments
 (0)