Skip to content

Commit 34524ef

Browse files
committed
Restore AuthenticationException as inner exception.
Signed-off-by: Bradley Grainger <[email protected]>
1 parent b58af51 commit 34524ef

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
528528
}
529529

530530
var ok = OkPayload.Create(payload.Span, this);
531-
if (m_rcbPolicyErrors != SslPolicyErrors.None)
531+
if (m_sslPolicyErrors != SslPolicyErrors.None)
532532
{
533533
// SSL would normally have thrown error, so connector need to ensure server certificates
534534
// pass only if :
@@ -538,29 +538,15 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
538538
{
539539
if (string.IsNullOrEmpty(password) || !ValidateFingerprint(ok.StatusInfo, initialHandshake.AuthPluginData.AsSpan(0, 20), password!))
540540
{
541-
// fingerprint validation fail.
542-
// now throwing SSL exception depending on m_rcbPolicyErrors
543541
ShutdownSocket();
544542
HostName = "";
545-
lock (m_lock) m_state = State.Failed;
546-
MySqlException ex;
547-
switch (m_rcbPolicyErrors)
548-
{
549-
case SslPolicyErrors.RemoteCertificateNotAvailable:
550-
// impossible
551-
ex = new MySqlException(MySqlErrorCode.UnableToConnectToHost, "SSL not validated, no remote certificate available");
552-
break;
553-
554-
case SslPolicyErrors.RemoteCertificateNameMismatch:
555-
ex = new MySqlException(MySqlErrorCode.UnableToConnectToHost, "SSL not validated, certificate name mismatch");
556-
break;
557-
558-
default:
559-
ex = new MySqlException(MySqlErrorCode.UnableToConnectToHost, "SSL not validated, certificate chain validation fail");
560-
break;
561-
}
562-
Log.CouldNotInitializeTlsConnection(m_logger, ex, Id);
563-
throw ex;
543+
lock (m_lock)
544+
m_state = State.Failed;
545+
546+
// throw a MySqlException with an AuthenticationException InnerException to mimic what would have happened if ValidateRemoteCertificate returned false
547+
var innerException = new AuthenticationException($"The remote certificate was rejected due to the following error: {m_sslPolicyErrors}");
548+
Log.CouldNotInitializeTlsConnection(m_logger, innerException, Id);
549+
throw new MySqlException(MySqlErrorCode.UnableToConnectToHost, "SSL Authentication Error", innerException);
564550
}
565551
}
566552
}
@@ -1601,7 +1587,7 @@ caCertificateChain is not null &&
16011587
using var sha256 = SHA256.Create();
16021588
m_remoteCertificateSha2Thumbprint = sha256.ComputeHash(cert2.RawData);
16031589
#endif
1604-
m_rcbPolicyErrors = rcbPolicyErrors;
1590+
m_sslPolicyErrors = rcbPolicyErrors;
16051591
return true;
16061592
}
16071593

@@ -2128,5 +2114,5 @@ protected override void OnStatementBegin(int index)
21282114
private Dictionary<string, PreparedStatements>? m_preparedStatements;
21292115
private string m_pluginName = "mysql_native_password";
21302116
private byte[]? m_remoteCertificateSha2Thumbprint;
2131-
private SslPolicyErrors m_rcbPolicyErrors;
2117+
private SslPolicyErrors m_sslPolicyErrors;
21322118
}

0 commit comments

Comments
 (0)