Skip to content

Commit 5284e88

Browse files
committed
Persist the fallback TLS version only on success. Fixes #1349
If an exception happens during TLS negotiation, we only want to set the fallback TLS version on the connection pool if connecting succeeded. Otherwise, we could get into a permanent state of trying to connect with a TLS version that will fail. Signed-off-by: Bradley Grainger <[email protected]>
1 parent 5a7c78c commit 5284e88

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,14 +424,15 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
424424
// (which is SslProtocols.None; see https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls),
425425
// then fall back to SslProtocols.Tls11 if that fails and it's possible that the cause is a yaSSL server.
426426
bool shouldRetrySsl;
427+
var shouldUpdatePoolSslProtocols = false;
427428
var sslProtocols = Pool?.SslProtocols ?? cs.TlsVersions;
428429
PayloadData payload;
429430
InitialHandshakePayload initialHandshake;
430431
do
431432
{
432-
bool tls11or10Supported = (sslProtocols & (SslProtocols.Tls | SslProtocols.Tls11)) != SslProtocols.None;
433-
bool tls12Supported = (sslProtocols & SslProtocols.Tls12) == SslProtocols.Tls12;
434-
shouldRetrySsl = (sslProtocols == SslProtocols.None || (tls12Supported && tls11or10Supported)) && Utility.IsWindows();
433+
var isTls11or10Supported = (sslProtocols & (SslProtocols.Tls | SslProtocols.Tls11)) != SslProtocols.None;
434+
var isTls12Supported = (sslProtocols & SslProtocols.Tls12) == SslProtocols.Tls12;
435+
shouldRetrySsl = (sslProtocols == SslProtocols.None || (isTls12Supported && isTls11or10Supported)) && Utility.IsWindows();
435436

436437
var connected = false;
437438
if (cs.ConnectionProtocol == MySqlConnectionProtocol.Sockets)
@@ -538,19 +539,20 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
538539
{
539540
await InitSslAsync(initialHandshake.ProtocolCapabilities, cs, connection, sslProtocols, ioBehavior, cancellationToken).ConfigureAwait(false);
540541
shouldRetrySsl = false;
542+
if (shouldUpdatePoolSslProtocols && Pool is not null)
543+
Pool.SslProtocols = sslProtocols;
541544
}
542545
catch (ArgumentException ex) when (ex.ParamName == "sslProtocolType" && sslProtocols == SslProtocols.None)
543546
{
544547
Log.Debug(ex, "Session{0} doesn't support SslProtocols.None; falling back to explicitly specifying SslProtocols", m_logArguments);
545548
sslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
546549
}
547-
catch (Exception ex) when (shouldRetrySsl && ((ex is MySqlException && ex.InnerException is IOException) || ex is IOException))
550+
catch (Exception ex) when (shouldRetrySsl && ((ex is MySqlException && ex.InnerException is AuthenticationException or IOException) || ex is AuthenticationException or IOException))
548551
{
549552
// negotiating TLS 1.2 with a yaSSL-based server throws an exception on Windows, see comment at top of method
550-
Log.Debug(ex, "Session{0} failed negotiating TLS; falling back to TLS 1.1", m_logArguments);
553+
Log.Warn(ex, "Session{0} failed negotiating TLS; falling back to TLS 1.1", m_logArguments);
551554
sslProtocols = sslProtocols == SslProtocols.None ? SslProtocols.Tls | SslProtocols.Tls11 : (SslProtocols.Tls | SslProtocols.Tls11) & sslProtocols;
552-
if (Pool is not null)
553-
Pool.SslProtocols = sslProtocols;
555+
shouldUpdatePoolSslProtocols = true;
554556
}
555557
}
556558
else

0 commit comments

Comments
 (0)