Skip to content

Commit 7c2964f

Browse files
committed
Minor code cleanup.
Signed-off-by: Bradley Grainger <[email protected]>
1 parent abee0c7 commit 7c2964f

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

src/MySqlConnector.Authentication.Ed25519/Ed25519AuthenticationPlugin.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ public byte[] CreatePasswordHash(string password, ReadOnlySpan<byte> authenticat
4545
return passwordHash;
4646
}
4747

48-
/// <summary>
49-
/// Creates the authentication response.
50-
/// </summary>
5148
private static void CreateResponseAndHash(string password, ReadOnlySpan<byte> authenticationData, out byte[] passwordHash, out byte[] authenticationResponse)
5249
{
5350
// Java reference: https://github.com/MariaDB/mariadb-connector-j/blob/master/src/main/java/org/mariadb/jdbc/internal/com/send/authentication/Ed25519PasswordPlugin.java

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,13 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
438438
var initialHandshake = InitialHandshakePayload.Create(payload.Span);
439439

440440
// if PluginAuth is supported, then use the specified auth plugin; else, fall back to protocol capabilities to determine the auth type to use
441-
var authPluginName = (initialHandshake.ProtocolCapabilities & ProtocolCapabilities.PluginAuth) != 0 ? initialHandshake.AuthPluginName! :
441+
m_currentAuthenticationMethod = (initialHandshake.ProtocolCapabilities & ProtocolCapabilities.PluginAuth) != 0 ? initialHandshake.AuthPluginName! :
442442
(initialHandshake.ProtocolCapabilities & ProtocolCapabilities.SecureConnection) == 0 ? "mysql_old_password" :
443443
"mysql_native_password";
444-
Log.ServerSentAuthPluginName(m_logger, Id, authPluginName);
445-
if (authPluginName is not "mysql_native_password" and not "sha256_password" and not "caching_sha2_password")
444+
Log.ServerSentAuthPluginName(m_logger, Id, m_currentAuthenticationMethod);
445+
if (m_currentAuthenticationMethod is not "mysql_native_password" and not "sha256_password" and not "caching_sha2_password")
446446
{
447-
Log.UnsupportedAuthenticationMethod(m_logger, Id, authPluginName);
447+
Log.UnsupportedAuthenticationMethod(m_logger, Id, m_currentAuthenticationMethod);
448448
throw new NotSupportedException($"Authentication method '{initialHandshake.AuthPluginName}' is not supported.");
449449
}
450450

@@ -608,30 +608,27 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
608608
}
609609

610610
/// <summary>
611-
/// Validate SSL validation has
611+
/// Validate SSL validation hash (from OK packet).
612612
/// </summary>
613-
/// <param name="validationHash">received validation hash</param>
614-
/// <param name="challenge">initial seed</param>
615-
/// <param name="password">password</param>
616-
/// <returns>true if validated</returns>
613+
/// <param name="validationHash">The validation hash received from the server.</param>
614+
/// <param name="challenge">The auth plugin data from the initial handshake.</param>
615+
/// <param name="password">The user's password.</param>
616+
/// <returns><c>true</c> if the validation hash matches the locally-computed value; otherwise, <c>false</c>.</returns>
617617
private bool ValidateFingerprint(byte[]? validationHash, ReadOnlySpan<byte> challenge, string password)
618618
{
619-
if (validationHash?.Length != 65)
619+
// expect 0x01 followed by 64 hex characters giving a SHA2 hash
620+
if (validationHash?.Length != 65 || validationHash[0] != 1)
620621
return false;
621622

622-
// ensure using SHA256 encryption
623-
if (validationHash[0] != 0x01)
624-
throw new FormatException($"Unexpected validation hash format. expected 0x01 but got 0x{validationHash[0]:X2}");
625-
626623
byte[]? passwordHashResult = null;
627-
switch (m_pluginName)
624+
switch (m_currentAuthenticationMethod)
628625
{
629626
case "mysql_native_password":
630627
passwordHashResult = AuthenticationUtility.HashPassword([], password, onlyHashPassword: true);
631628
break;
632629

633630
case "client_ed25519":
634-
AuthenticationPlugins.TryGetPlugin(m_pluginName, out var ed25519Plugin);
631+
AuthenticationPlugins.TryGetPlugin(m_currentAuthenticationMethod, out var ed25519Plugin);
635632
if (ed25519Plugin is IAuthenticationPlugin2 plugin2)
636633
passwordHashResult = plugin2.CreatePasswordHash(password, challenge);
637634
break;
@@ -836,7 +833,7 @@ private async Task<PayloadData> SwitchAuthenticationAsync(ConnectionSettings cs,
836833
// if the server didn't support the hashed password; rehash with the new challenge
837834
var switchRequest = AuthenticationMethodSwitchRequestPayload.Create(payload.Span);
838835
Log.SwitchingToAuthenticationMethod(m_logger, Id, switchRequest.Name);
839-
m_pluginName = switchRequest.Name;
836+
m_currentAuthenticationMethod = switchRequest.Name;
840837
switch (switchRequest.Name)
841838
{
842839
case "mysql_native_password":
@@ -2140,7 +2137,7 @@ protected override void OnStatementBegin(int index)
21402137
private PayloadData m_setNamesPayload;
21412138
private byte[]? m_pipelinedResetConnectionBytes;
21422139
private Dictionary<string, PreparedStatements>? m_preparedStatements;
2143-
private string m_pluginName = "mysql_native_password";
2140+
private string? m_currentAuthenticationMethod;
21442141
private byte[]? m_remoteCertificateSha2Thumbprint;
21452142
private SslPolicyErrors m_sslPolicyErrors;
21462143
}

src/MySqlConnector/Protocol/Serialization/AuthenticationUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static byte[] GetNullTerminatedPasswordBytes(string password)
2525
}
2626

2727
public static byte[] CreateAuthenticationResponse(ReadOnlySpan<byte> challenge, string password) =>
28-
string.IsNullOrEmpty(password) ? [] : HashPassword(challenge, password, false);
28+
string.IsNullOrEmpty(password) ? [] : HashPassword(challenge, password, onlyHashPassword: false);
2929

3030
/// <summary>
3131
/// Hashes a password with the "Secure Password Authentication" method.

tests/IntegrationTests/ServerFeatures.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public enum ServerFeatures
3737
CancelSleepSuccessfully = 0x40_0000,
3838

3939
/// <summary>
40-
/// Server permit redirection, available on first OK_Packet
40+
/// Server permits redirection (sent as a server variable in first OK packet).
4141
/// </summary>
4242
Redirection = 0x80_0000,
4343

4444
/// <summary>
45-
/// Server permit redirection, available on first OK_Packet
45+
/// Server provides hash of TLS certificate in first OK packet.
4646
/// </summary>
4747
TlsFingerprintValidation = 0x100_0000,
4848
}

tests/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Otherwise, set the following options appropriately:
2727
* `ErrorCodes`: server returns error codes in error packet (some MySQL proxies do not)
2828
* `Json`: the `JSON` data type (MySQL 5.7 and later)
2929
* `LargePackets`: large packets (over 4MB)
30+
* `Redirection`: server supports sending redirection information in a server variable in the first OK packet
3031
* `RoundDateTime`: server rounds `datetime` values to the specified precision (not implemented in MariaDB)
3132
* `RsaEncryption`: server supports RSA public key encryption (for `sha256_password` and `caching_sha2_password`)
3233
* `SessionTrack`: server supports `CLIENT_SESSION_TRACK` capability (MySQL 5.7 and later)
@@ -36,9 +37,9 @@ Otherwise, set the following options appropriately:
3637
* `Tls11`: server supports TLS 1.1
3738
* `Tls12`: server supports TLS 1.2
3839
* `Tls13`: server supports TLS 1.3
40+
* `TlsFingerprintValidation`: server provides a hash of the TLS certificate fingerprint in the first OK packet
3941
* `UnixDomainSocket`: server is accessible via a Unix domain socket
4042
* `UuidToBin`: server supports `UUID_TO_BIN` (MySQL 8.0 and later)
41-
* `UnsupportedFeatures`: server supports
4243

4344
## Running Tests
4445

0 commit comments

Comments
 (0)