Skip to content

Commit cde351b

Browse files
authored
Merge pull request #913 from lauxjpn/fix/sslkey
Extract key from between PEM header/footer, instead of just removing the PEM header/footer.
2 parents 6b49ba4 + 8568f6f commit cde351b

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed
3.17 KB
Binary file not shown.

src/MySqlConnector/Utilities/Utility.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,39 @@ public static unsafe void Convert(this Encoder encoder, ReadOnlySpan<char> chars
8585
/// <returns>An RSA key.</returns>
8686
public static RSAParameters GetRsaParameters(string key)
8787
{
88+
const string beginRsaPrivateKey = "-----BEGIN RSA PRIVATE KEY-----";
89+
const string endRsaPrivateKey = "-----END RSA PRIVATE KEY-----";
90+
const string beginPublicKey = "-----BEGIN PUBLIC KEY-----";
91+
const string endPublicKey = "-----END PUBLIC KEY-----";
92+
93+
int keyStartIndex;
94+
string pemFooter;
8895
bool isPrivate;
89-
if (key.StartsWith("-----BEGIN RSA PRIVATE KEY-----", StringComparison.Ordinal))
96+
97+
if ((keyStartIndex = key.IndexOf(beginRsaPrivateKey, StringComparison.Ordinal)) > -1)
9098
{
91-
key = key.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "");
99+
keyStartIndex += beginRsaPrivateKey.Length;
100+
pemFooter = endRsaPrivateKey;
92101
isPrivate = true;
93102
}
94-
else if (key.StartsWith("-----BEGIN PUBLIC KEY-----", StringComparison.Ordinal))
103+
else if ((keyStartIndex = key.IndexOf(beginPublicKey, StringComparison.Ordinal)) > -1)
95104
{
96-
key = key.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "");
105+
keyStartIndex += beginPublicKey.Length;
106+
pemFooter = endPublicKey;
97107
isPrivate = false;
98108
}
99109
else
100110
{
101111
throw new FormatException("Unrecognized PEM header: " + key.Substring(0, Math.Min(key.Length, 80)));
102112
}
103113

114+
var keyEndIndex = key.IndexOf(pemFooter, keyStartIndex, StringComparison.Ordinal);
115+
116+
if (keyEndIndex <= -1)
117+
throw new FormatException($"Missing expected '{pemFooter}' PEM footer: " + key.Substring(Math.Max(key.Length - 80, 0)));
118+
119+
key = key.Substring(keyStartIndex, keyEndIndex - keyStartIndex);
120+
104121
return GetRsaParameters(System.Convert.FromBase64String(key), isPrivate);
105122
}
106123

tests/SideBySide/SslTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ public async Task ConnectSslClientCertificate(string certFile, string certFilePa
6464
#if !NETCOREAPP1_1_2
6565
[SkippableTheory(ConfigSettings.RequiresSsl | ConfigSettings.KnownClientCertificate)]
6666
[InlineData("ssl-client-cert.pem", "ssl-client-key.pem", null)]
67+
[InlineData("ssl-client-cert.pem", "ssl-client-key-null.pem", null)]
6768
#if !BASELINE
6869
[InlineData("ssl-client-cert.pem", "ssl-client-key.pem", "ssl-ca-cert.pem")] // https://bugs.mysql.com/bug.php?id=95436
70+
[InlineData("ssl-client-cert.pem", "ssl-client-key-null.pem", "ssl-ca-cert.pem")] // https://bugs.mysql.com/bug.php?id=95436
6971
#endif
7072
public async Task ConnectSslClientCertificatePem(string certFile, string keyFile, string caCertFile)
7173
{

0 commit comments

Comments
 (0)