@@ -528,26 +528,43 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
528
528
}
529
529
530
530
var ok = OkPayload . Create ( payload . Span , this ) ;
531
+
531
532
if ( m_sslPolicyErrors != SslPolicyErrors . None )
532
533
{
533
- // SSL would normally have thrown error, so connector need to ensure server certificates
534
+ // SSL would normally have thrown error, but this was suppressed in ValidateRemoteCertificate; now we need to verify the server certificate
534
535
// pass only if :
535
536
// * connection method is MitM-proof (e.g. unix socket)
536
537
// * auth plugin is MitM-proof and check SHA2(user's hashed password, scramble, certificate fingerprint)
537
- if ( cs . ConnectionProtocol != MySqlConnectionProtocol . UnixSocket )
538
+ // see https://mariadb.org/mission-impossible-zero-configuration-ssl/
539
+ var ignoreCertificateError = false ;
540
+
541
+ if ( cs . ConnectionProtocol == MySqlConnectionProtocol . UnixSocket )
538
542
{
539
- if ( string . IsNullOrEmpty ( password ) || ! ValidateFingerprint ( ok . StatusInfo , initialHandshake . AuthPluginData . AsSpan ( 0 , 20 ) , password ! ) )
540
- {
541
- ShutdownSocket ( ) ;
542
- HostName = "" ;
543
- lock ( m_lock )
544
- m_state = State . Failed ;
543
+ Log . CertificateErrorUnixSocket ( m_logger , Id , m_sslPolicyErrors ) ;
544
+ ignoreCertificateError = true ;
545
+ }
546
+ else if ( string . IsNullOrEmpty ( password ) )
547
+ {
548
+ // there is no shared secret that can be used to validate the certificate
549
+ Log . CertificateErrorNoPassword ( m_logger , Id , m_sslPolicyErrors ) ;
550
+ }
551
+ else if ( ValidateFingerprint ( ok . StatusInfo , initialHandshake . AuthPluginData . AsSpan ( 0 , 20 ) , password ) )
552
+ {
553
+ Log . CertificateErrorValidThumbprint ( m_logger , Id , m_sslPolicyErrors ) ;
554
+ ignoreCertificateError = true ;
555
+ }
545
556
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 ) ;
550
- }
557
+ if ( ! ignoreCertificateError )
558
+ {
559
+ ShutdownSocket ( ) ;
560
+ HostName = "" ;
561
+ lock ( m_lock )
562
+ m_state = State . Failed ;
563
+
564
+ // throw a MySqlException with an AuthenticationException InnerException to mimic what would have happened if ValidateRemoteCertificate returned false
565
+ var innerException = new AuthenticationException ( $ "The remote certificate was rejected due to the following error: { m_sslPolicyErrors } ") ;
566
+ Log . CouldNotInitializeTlsConnection ( m_logger , innerException , Id ) ;
567
+ throw new MySqlException ( MySqlErrorCode . UnableToConnectToHost , "SSL Authentication Error" , innerException ) ;
551
568
}
552
569
}
553
570
@@ -1664,11 +1681,22 @@ await sslStream.AuthenticateAsClientAsync(clientAuthenticationOptions.TargetHost
1664
1681
m_payloadHandler ! . ByteHandler = sslByteHandler ;
1665
1682
m_isSecureConnection = true ;
1666
1683
m_sslStream = sslStream ;
1684
+ if ( m_sslPolicyErrors != SslPolicyErrors . None )
1685
+ {
1686
+ #if NETCOREAPP3_0_OR_GREATER
1687
+ Log . ConnectedTlsBasicPreliminary ( m_logger , Id , m_sslPolicyErrors , sslStream . SslProtocol , sslStream . NegotiatedCipherSuite ) ;
1688
+ #else
1689
+ Log . ConnectedTlsDetailedPreliminary ( m_logger , Id , m_sslPolicyErrors , sslStream . SslProtocol , sslStream . CipherAlgorithm , sslStream . HashAlgorithm , sslStream . KeyExchangeAlgorithm , sslStream . KeyExchangeStrength ) ;
1690
+ #endif
1691
+ }
1692
+ else
1693
+ {
1667
1694
#if NETCOREAPP3_0_OR_GREATER
1668
- Log . ConnectedTlsBasic ( m_logger , Id , sslStream . SslProtocol , sslStream . NegotiatedCipherSuite ) ;
1695
+ Log . ConnectedTlsBasic ( m_logger , Id , sslStream . SslProtocol , sslStream . NegotiatedCipherSuite ) ;
1669
1696
#else
1670
- Log . ConnectedTlsDetailed ( m_logger , Id , sslStream . SslProtocol , sslStream . CipherAlgorithm , sslStream . HashAlgorithm , sslStream . KeyExchangeAlgorithm , sslStream . KeyExchangeStrength ) ;
1697
+ Log . ConnectedTlsDetailed ( m_logger , Id , sslStream . SslProtocol , sslStream . CipherAlgorithm , sslStream . HashAlgorithm , sslStream . KeyExchangeAlgorithm , sslStream . KeyExchangeStrength ) ;
1671
1698
#endif
1699
+ }
1672
1700
}
1673
1701
catch ( Exception ex )
1674
1702
{
0 commit comments