Skip to content

Commit 3146bcd

Browse files
vonzshikroji
andauthored
Add SHA3 hash algorithms for SASL authentication (npgsql#6028)
Closes npgsql#6027 --------- Co-authored-by: Shay Rojansky <[email protected]>
1 parent 01155b6 commit 3146bcd

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

src/Npgsql/Internal/NpgsqlConnector.Auth.cs

+35-39
Original file line numberDiff line numberDiff line change
@@ -200,51 +200,47 @@ internal void AuthenticateSASLSha256Plus(ref string mechanism, ref string cbindF
200200
// But to be on the safe side we'll just create a new instance of it
201201
using var remoteCertificate = new X509Certificate2(sslStream.RemoteCertificate);
202202
// Checking for hashing algorithms
203-
HashAlgorithm? hashAlgorithm = null;
204203
var algorithmName = remoteCertificate.SignatureAlgorithm.FriendlyName;
205-
if (algorithmName is null)
206-
{
207-
ConnectionLogger.LogWarning("Signature algorithm was null, falling back to SCRAM-SHA-256");
208-
}
209-
else if (algorithmName.StartsWith("sha1", StringComparison.OrdinalIgnoreCase) ||
210-
algorithmName.StartsWith("md5", StringComparison.OrdinalIgnoreCase) ||
211-
algorithmName.StartsWith("sha256", StringComparison.OrdinalIgnoreCase))
212-
{
213-
hashAlgorithm = SHA256.Create();
214-
}
215-
else if (algorithmName.StartsWith("sha384", StringComparison.OrdinalIgnoreCase))
216-
{
217-
hashAlgorithm = SHA384.Create();
218-
}
219-
else if (algorithmName.StartsWith("sha512", StringComparison.OrdinalIgnoreCase))
204+
205+
HashAlgorithm? hashAlgorithm = algorithmName switch
220206
{
221-
hashAlgorithm = SHA512.Create();
222-
}
223-
else
207+
not null when algorithmName.StartsWith("sha1", StringComparison.OrdinalIgnoreCase) => SHA256.Create(),
208+
not null when algorithmName.StartsWith("md5", StringComparison.OrdinalIgnoreCase) => SHA256.Create(),
209+
not null when algorithmName.StartsWith("sha256", StringComparison.OrdinalIgnoreCase) => SHA256.Create(),
210+
not null when algorithmName.StartsWith("sha384", StringComparison.OrdinalIgnoreCase) => SHA384.Create(),
211+
not null when algorithmName.StartsWith("sha512", StringComparison.OrdinalIgnoreCase) => SHA512.Create(),
212+
not null when algorithmName.StartsWith("sha3-256", StringComparison.OrdinalIgnoreCase) => SHA3_256.Create(),
213+
not null when algorithmName.StartsWith("sha3-384", StringComparison.OrdinalIgnoreCase) => SHA3_384.Create(),
214+
not null when algorithmName.StartsWith("sha3-512", StringComparison.OrdinalIgnoreCase) => SHA3_512.Create(),
215+
216+
_ => null
217+
};
218+
219+
if (hashAlgorithm is null)
224220
{
225221
ConnectionLogger.LogWarning(
226-
$"Support for signature algorithm {algorithmName} is not yet implemented, falling back to SCRAM-SHA-256");
222+
algorithmName is null
223+
? "Signature algorithm was null, falling back to SCRAM-SHA-256"
224+
: $"Support for signature algorithm {algorithmName} is not yet implemented, falling back to SCRAM-SHA-256");
225+
return;
227226
}
228227

229-
if (hashAlgorithm != null)
230-
{
231-
using var _ = hashAlgorithm;
232-
233-
// RFC 5929
234-
mechanism = "SCRAM-SHA-256-PLUS";
235-
// PostgreSQL only supports tls-server-end-point binding
236-
cbindFlag = "p=tls-server-end-point";
237-
// SCRAM-SHA-256-PLUS depends on using ssl stream, so it's fine
238-
var cbindFlagBytes = Encoding.UTF8.GetBytes($"{cbindFlag},,");
239-
240-
var certificateHash = hashAlgorithm.ComputeHash(remoteCertificate.GetRawCertData());
241-
var cbindBytes = new byte[cbindFlagBytes.Length + certificateHash.Length];
242-
cbindFlagBytes.CopyTo(cbindBytes, 0);
243-
certificateHash.CopyTo(cbindBytes, cbindFlagBytes.Length);
244-
cbind = Convert.ToBase64String(cbindBytes);
245-
successfulBind = true;
246-
IsScramPlus = true;
247-
}
228+
using var _ = hashAlgorithm;
229+
230+
// RFC 5929
231+
mechanism = "SCRAM-SHA-256-PLUS";
232+
// PostgreSQL only supports tls-server-end-point binding
233+
cbindFlag = "p=tls-server-end-point";
234+
// SCRAM-SHA-256-PLUS depends on using ssl stream, so it's fine
235+
var cbindFlagBytes = Encoding.UTF8.GetBytes($"{cbindFlag},,");
236+
237+
var certificateHash = hashAlgorithm.ComputeHash(remoteCertificate.GetRawCertData());
238+
var cbindBytes = new byte[cbindFlagBytes.Length + certificateHash.Length];
239+
cbindFlagBytes.CopyTo(cbindBytes, 0);
240+
certificateHash.CopyTo(cbindBytes, cbindFlagBytes.Length);
241+
cbind = Convert.ToBase64String(cbindBytes);
242+
successfulBind = true;
243+
IsScramPlus = true;
248244
}
249245

250246
static byte[] Hi(string str, byte[] salt, int count)

0 commit comments

Comments
 (0)