Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Npgsql/Internal/NpgsqlConnector.Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,11 @@ internal async ValueTask AuthenticateGSS(bool async, CancellationToken cancellat

using var authContext = new NegotiateAuthentication(clientOptions);
var data = authContext.GetOutgoingBlob(ReadOnlySpan<byte>.Empty, out var statusCode)!;
Debug.Assert(statusCode == NegotiateAuthenticationStatusCode.ContinueNeeded);
if (statusCode != NegotiateAuthenticationStatusCode.ContinueNeeded)
{
// Unable to retrieve credentials or some other issue
throw new NpgsqlException($"Unable to authenticate with GSS: received {statusCode} instead of the expected ContinueNeeded");
}
await WritePassword(data, 0, data.Length, async, cancellationToken).ConfigureAwait(false);
await Flush(async, cancellationToken).ConfigureAwait(false);
while (true)
Expand Down
168 changes: 102 additions & 66 deletions src/Npgsql/Internal/NpgsqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,17 +595,27 @@ static async Task OpenCore(
bool async,
CancellationToken cancellationToken)
{
await conn.RawOpen(sslMode, gssEncMode, timeout, async, cancellationToken).ConfigureAwait(false);
timeout.CheckAndApply(conn);
conn.WriteStartupMessage(username);
await conn.Flush(async, cancellationToken).ConfigureAwait(false);
// If we fail to connect to the socket, there is no reason to retry even if SslMode/GssEncryption allows it
await conn.RawOpen(timeout, async, cancellationToken).ConfigureAwait(false);

using var cancellationRegistration = conn.StartCancellableOperation(cancellationToken, attemptPgCancellation: false);
try
{
await conn.SetupEncryption(sslMode, gssEncMode, timeout, async, cancellationToken).ConfigureAwait(false);
timeout.CheckAndApply(conn);
conn.WriteStartupMessage(username);
await conn.Flush(async, cancellationToken).ConfigureAwait(false);

using var cancellationRegistration = conn.StartCancellableOperation(cancellationToken, attemptPgCancellation: false);
await conn.Authenticate(username, timeout, async, cancellationToken).ConfigureAwait(false);
}
// We handle any exception here because on Windows while receiving a response from Postgres
// We might hit connection reset, in which case the actual error will be lost
// And we only read some IO error
// In addition, this behavior mimics libpq, where it retries as long as GssEncryptionMode and SslMode allows it
catch (Exception e) when
// We might also get here OperationCancelledException/TimeoutException
// But it's fine to fall down and retry because we'll immediately exit with the exact same exception
//
// Any error after trying with GSS encryption
(gssEncMode == GssEncryptionMode.Prefer ||
// Auth error with/without SSL
Expand All @@ -619,9 +629,6 @@ static async Task OpenCore(
else
sslMode = sslMode == SslMode.Prefer ? SslMode.Disable : SslMode.Require;

cancellationRegistration.Dispose();
Debug.Assert(!conn.IsBroken);

conn.Cleanup();

// If Prefer was specified and we failed (with SSL), retry without SSL.
Expand Down Expand Up @@ -695,6 +702,8 @@ internal async ValueTask<GssEncryptionResult> GSSEncrypt(bool async, bool isRequ
default:
throw new NpgsqlException($"Received unknown response {response} for GSSEncRequest (expecting G or N)");
case 'N':
if (isRequired)
throw new NpgsqlException("GGS encryption requested. No GSS encryption enabled connection from this host is configured.");
return GssEncryptionResult.NegotiateFailure;
case 'G':
break;
Expand Down Expand Up @@ -906,7 +915,7 @@ async ValueTask<string> GetUsernameAsyncInternal()
}
}

async Task RawOpen(SslMode sslMode, GssEncryptionMode gssEncryptionMode, NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken)
async Task RawOpen(NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken)
{
try
{
Expand Down Expand Up @@ -938,59 +947,6 @@ async Task RawOpen(SslMode sslMode, GssEncryptionMode gssEncryptionMode, NpgsqlT

IsSslEncrypted = false;
IsGssEncrypted = false;

var gssEncryptResult = await TryNegotiateGssEncryption(gssEncryptionMode, async, cancellationToken).ConfigureAwait(false);
if (gssEncryptResult == GssEncryptionResult.Success)
return;

timeout.CheckAndApply(this);

if (GetSslNegotiation(Settings) == SslNegotiation.Direct)
{
// We already check that in NpgsqlConnectionStringBuilder.PostProcessAndValidate, but since we also allow environment variables...
if (Settings.SslMode is not SslMode.Require and not SslMode.VerifyCA and not SslMode.VerifyFull)
throw new ArgumentException("SSL Mode has to be Require or higher to be used with direct SSL Negotiation");
if (gssEncryptResult == GssEncryptionResult.NegotiateFailure)
{
// We can be here only if it's fallback from preferred (but failed) gss encryption
// In this case, direct encryption isn't going to work anymore, so we throw a bogus exception to retry again without gss
// Alternatively, we can instead just go with the usual route of writing SslRequest, ignoring direct ssl
// But this is how libpq works
Debug.Assert(gssEncryptionMode == GssEncryptionMode.Prefer);
// The exception message doesn't matter since we're going to retry again
throw new NpgsqlException();
}

await DataSource.TransportSecurityHandler.NegotiateEncryption(async, this, sslMode, timeout, cancellationToken).ConfigureAwait(false);
if (ReadBuffer.ReadBytesLeft > 0)
throw new NpgsqlException("Additional unencrypted data received after SSL negotiation - this should never happen, and may be an indication of a man-in-the-middle attack.");
}
else if ((sslMode is SslMode.Prefer && DataSource.TransportSecurityHandler.SupportEncryption) ||
sslMode is SslMode.Require or SslMode.VerifyCA or SslMode.VerifyFull)
{
WriteSslRequest();
await Flush(async, cancellationToken).ConfigureAwait(false);

await ReadBuffer.Ensure(1, async).ConfigureAwait(false);
var response = (char)ReadBuffer.ReadByte();
timeout.CheckAndApply(this);

switch (response)
{
default:
throw new NpgsqlException($"Received unknown response {response} for SSLRequest (expecting S or N)");
case 'N':
if (sslMode != SslMode.Prefer)
throw new NpgsqlException("SSL connection requested. No SSL enabled connection from this host is configured.");
break;
case 'S':
await DataSource.TransportSecurityHandler.NegotiateEncryption(async, this, sslMode, timeout, cancellationToken).ConfigureAwait(false);
break;
}

if (ReadBuffer.ReadBytesLeft > 0)
throw new NpgsqlException("Additional unencrypted data received after SSL negotiation - this should never happen, and may be an indication of a man-in-the-middle attack.");
}
}
catch
{
Expand All @@ -1007,12 +963,80 @@ async Task RawOpen(SslMode sslMode, GssEncryptionMode gssEncryptionMode, NpgsqlT
}
}

async Task SetupEncryption(SslMode sslMode, GssEncryptionMode gssEncryptionMode, NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken)
{
var gssEncryptResult = await TryNegotiateGssEncryption(gssEncryptionMode, async, cancellationToken).ConfigureAwait(false);
if (gssEncryptResult == GssEncryptionResult.Success)
return;

// TryNegotiateGssEncryption should already throw a much more meaningful exception
// if GSS encryption is required but for some reason we can't negotiate it.
// But since we have to return a specific result instead of generic true/false
// To make absolutely sure we didn't miss anything, recheck again
if (gssEncryptionMode == GssEncryptionMode.Require)
throw new NpgsqlException($"Unable to negotiate GSS encryption: {gssEncryptResult}");

timeout.CheckAndApply(this);

if (GetSslNegotiation(Settings) == SslNegotiation.Direct)
{
// We already check that in NpgsqlConnectionStringBuilder.PostProcessAndValidate, but since we also allow environment variables...
if (Settings.SslMode is not SslMode.Require and not SslMode.VerifyCA and not SslMode.VerifyFull)
throw new ArgumentException("SSL Mode has to be Require or higher to be used with direct SSL Negotiation");
if (gssEncryptResult == GssEncryptionResult.NegotiateFailure)
{
// We can be here only if it's fallback from preferred (but failed) gss encryption
// In this case, direct encryption isn't going to work anymore, so we throw a bogus exception to retry again without gss
// Alternatively, we can instead just go with the usual route of writing SslRequest, ignoring direct ssl
// But this is how libpq works
Debug.Assert(gssEncryptionMode == GssEncryptionMode.Prefer);
// The exception message doesn't matter since we're going to retry again
throw new NpgsqlException();
}

await DataSource.TransportSecurityHandler.NegotiateEncryption(async, this, sslMode, timeout, cancellationToken).ConfigureAwait(false);
if (ReadBuffer.ReadBytesLeft > 0)
throw new NpgsqlException("Additional unencrypted data received after SSL negotiation - this should never happen, and may be an indication of a man-in-the-middle attack.");
}
else if ((sslMode is SslMode.Prefer && DataSource.TransportSecurityHandler.SupportEncryption) ||
sslMode is SslMode.Require or SslMode.VerifyCA or SslMode.VerifyFull)
{
WriteSslRequest();
await Flush(async, cancellationToken).ConfigureAwait(false);

await ReadBuffer.Ensure(1, async).ConfigureAwait(false);
var response = (char)ReadBuffer.ReadByte();
timeout.CheckAndApply(this);

switch (response)
{
default:
throw new NpgsqlException($"Received unknown response {response} for SSLRequest (expecting S or N)");
case 'N':
if (sslMode != SslMode.Prefer)
throw new NpgsqlException("SSL connection requested. No SSL enabled connection from this host is configured.");
break;
case 'S':
await DataSource.TransportSecurityHandler.NegotiateEncryption(async, this, sslMode, timeout, cancellationToken).ConfigureAwait(false);
break;
}

if (ReadBuffer.ReadBytesLeft > 0)
throw new NpgsqlException("Additional unencrypted data received after SSL negotiation - this should never happen, and may be an indication of a man-in-the-middle attack.");
}
}

async ValueTask<GssEncryptionResult> TryNegotiateGssEncryption(GssEncryptionMode gssEncryptionMode, bool async, CancellationToken cancellationToken)
{
// GetCredentialFailure is essentially a nop (since we didn't send anything over the wire)
// So we can proceed further as if gss encryption wasn't even attempted
if (gssEncryptionMode == GssEncryptionMode.Disable) return GssEncryptionResult.GetCredentialFailure;

// Same thing as above, though in this case user doesn't require GSS encryption but didn't enable encryption
// Most of the time they're using the default value, in which case also exit without throwing an error
if (gssEncryptionMode == GssEncryptionMode.Prefer && !DataSource.TransportSecurityHandler.SupportEncryption)
return GssEncryptionResult.GetCredentialFailure;

if (ConnectedEndPoint!.AddressFamily == AddressFamily.Unix)
{
if (gssEncryptionMode == GssEncryptionMode.Prefer)
Expand All @@ -1037,7 +1061,9 @@ static SslNegotiation GetSslNegotiation(NpgsqlConnectionStringBuilder settings)
return sslNegotiation;
}

return SslNegotiation.Postgres;
// If user hasn't provided the value via connection string or environment variable
// Retrieve the default value from property
return settings.SslNegotiation;
}

static GssEncryptionMode GetGssEncMode(NpgsqlConnectionStringBuilder settings)
Expand All @@ -1051,7 +1077,9 @@ static GssEncryptionMode GetGssEncMode(NpgsqlConnectionStringBuilder settings)
return gssEncMode;
}

return GssEncryptionMode.Disable;
// If user hasn't provided the value via connection string or environment variable
// Retrieve the default value from property
return settings.GssEncryptionMode;
}

internal async Task NegotiateEncryption(SslMode sslMode, NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken)
Expand Down Expand Up @@ -2186,9 +2214,13 @@ void DoCancelRequest(int backendProcessId, int backendSecretKey)
{
try
{
RawOpen(Settings.SslMode, gssEncMode, new NpgsqlTimeout(TimeSpan.FromSeconds(ConnectionTimeout)), false,
var timeout = new NpgsqlTimeout(TimeSpan.FromSeconds(ConnectionTimeout));
RawOpen(timeout, false,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's slightly problematic to have to duplicate the RawOpen/Encrypt here as well; also the retry logic catching the exception etc. It feels like there's some sort of refactor possible, where both regular Open and DoCancelRequest would use the same thing which connects and sets up authentication.

But I don't think we should hold this PR up for it - just future cleanup possibility.

CancellationToken.None)
.GetAwaiter().GetResult();
SetupEncryption(Settings.SslMode, gssEncMode, timeout, false,
CancellationToken.None).
GetAwaiter().GetResult();
}
catch (Exception e) when (gssEncMode == GssEncryptionMode.Prefer)
{
Expand All @@ -2197,9 +2229,13 @@ void DoCancelRequest(int backendProcessId, int backendSecretKey)

// If we hit an error with gss encryption
// Retry again without it
RawOpen(Settings.SslMode, GssEncryptionMode.Disable, new NpgsqlTimeout(TimeSpan.FromSeconds(ConnectionTimeout)), false,
var timeout = new NpgsqlTimeout(TimeSpan.FromSeconds(ConnectionTimeout));
RawOpen(timeout, false,
CancellationToken.None)
.GetAwaiter().GetResult();
SetupEncryption(Settings.SslMode, GssEncryptionMode.Disable, timeout, false,
CancellationToken.None).
GetAwaiter().GetResult();
}

WriteCancelRequest(backendProcessId, backendSecretKey);
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql/NpgsqlConnectionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public SslNegotiation SslNegotiation
[NpgsqlConnectionStringProperty]
public GssEncryptionMode GssEncryptionMode
{
get => UserProvidedGssEncMode ?? GssEncryptionMode.Disable;
get => UserProvidedGssEncMode ?? GssEncryptionMode.Prefer;
set
{
UserProvidedGssEncMode = value;
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql/Util/GSSStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override void Write(ReadOnlySpan<byte> buffer)
Unsafe.WriteUnaligned(ref _writeLengthBuffer[0], BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(written.Length) : written.Length);

_stream.Write(_writeLengthBuffer);
_stream.Write(buffer.Slice(start, lengthToWrite));
_stream.Write(_writeBuffer.WrittenMemory.Span);

_writeBuffer.ResetWrittenCount();
start += lengthToWrite;
Expand Down