@@ -222,33 +222,31 @@ public async Task ConnectAsync(ConnectionSettings cs, IOBehavior ioBehavior, Can
222
222
ServerVersion = new ServerVersion ( Encoding . ASCII . GetString ( initialHandshake . ServerVersion ) ) ;
223
223
ConnectionId = initialHandshake . ConnectionId ;
224
224
AuthPluginData = initialHandshake . AuthPluginData ;
225
- if ( cs . UseCompression && ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . Compress ) == 0 )
226
- cs = cs . WithUseCompression ( false ) ;
225
+ m_useCompression = cs . UseCompression && ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . Compress ) != 0 ;
227
226
228
227
var serverSupportsSsl = ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . Ssl ) != 0 ;
229
228
if ( cs . SslMode != MySqlSslMode . None && ( cs . SslMode != MySqlSslMode . Preferred || serverSupportsSsl ) )
230
229
{
231
230
if ( ! serverSupportsSsl )
232
231
throw new MySqlException ( "Server does not support SSL" ) ;
233
232
await InitSslAsync ( initialHandshake . ProtocolCapabilities , cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
234
- cs = cs . WithSecureConnection ( true ) ;
235
233
}
236
234
237
- var response = HandshakeResponse41Packet . Create ( initialHandshake , cs ) ;
235
+ var response = HandshakeResponse41Packet . Create ( initialHandshake , cs , m_useCompression ) ;
238
236
payload = new PayloadData ( new ArraySegment < byte > ( response ) ) ;
239
237
await SendReplyAsync ( payload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
240
238
payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
241
239
242
240
// if server doesn't support the authentication fast path, it will send a new challenge
243
241
if ( payload . HeaderByte == AuthenticationMethodSwitchRequestPayload . Signature )
244
242
{
245
- await SwitchAuthenticationAsync ( payload , cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
243
+ await SwitchAuthenticationAsync ( payload , cs . Password , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
246
244
payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
247
245
}
248
246
249
247
OkPayload . Create ( payload ) ;
250
248
251
- if ( cs . UseCompression )
249
+ if ( m_useCompression )
252
250
m_payloadHandler = new CompressedPayloadHandler ( m_payloadHandler . ByteHandler ) ;
253
251
}
254
252
@@ -276,30 +274,30 @@ public async Task ResetConnectionAsync(ConnectionSettings cs, IOBehavior ioBehav
276
274
payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
277
275
if ( payload . HeaderByte == AuthenticationMethodSwitchRequestPayload . Signature )
278
276
{
279
- await SwitchAuthenticationAsync ( payload , cs , ioBehavior , cancellationToken ) ;
277
+ await SwitchAuthenticationAsync ( payload , cs . Password , ioBehavior , cancellationToken ) ;
280
278
payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
281
279
}
282
280
OkPayload . Create ( payload ) ;
283
281
}
284
282
}
285
283
286
- private async Task SwitchAuthenticationAsync ( PayloadData payload , ConnectionSettings cs , IOBehavior ioBehavior , CancellationToken cancellationToken )
284
+ private async Task SwitchAuthenticationAsync ( PayloadData payload , string password , IOBehavior ioBehavior , CancellationToken cancellationToken )
287
285
{
288
286
// if the server didn't support the hashed password; rehash with the new challenge
289
287
var switchRequest = AuthenticationMethodSwitchRequestPayload . Create ( payload ) ;
290
288
switch ( switchRequest . Name )
291
289
{
292
290
case "mysql_native_password" :
293
291
AuthPluginData = switchRequest . Data ;
294
- var hashedPassword = AuthenticationUtility . CreateAuthenticationResponse ( AuthPluginData , 0 , cs . Password ) ;
292
+ var hashedPassword = AuthenticationUtility . CreateAuthenticationResponse ( AuthPluginData , 0 , password ) ;
295
293
payload = new PayloadData ( new ArraySegment < byte > ( hashedPassword ) ) ;
296
294
await SendReplyAsync ( payload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
297
295
break ;
298
296
299
297
case "mysql_clear_password" :
300
- if ( ! cs . IsSecureConnection )
298
+ if ( ! m_isSecureConnection )
301
299
throw new MySqlException ( "Authentication method '{0}' requires a secure connection." . FormatInvariant ( switchRequest . Name ) ) ;
302
- payload = new PayloadData ( new ArraySegment < byte > ( Encoding . UTF8 . GetBytes ( cs . Password ) ) ) ;
300
+ payload = new PayloadData ( new ArraySegment < byte > ( Encoding . UTF8 . GetBytes ( password ) ) ) ;
303
301
await SendReplyAsync ( payload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
304
302
break ;
305
303
@@ -577,7 +575,7 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
577
575
578
576
var checkCertificateRevocation = cs . SslMode == MySqlSslMode . VerifyFull ;
579
577
580
- var initSsl = new PayloadData ( new ArraySegment < byte > ( HandshakeResponse41Packet . InitSsl ( serverCapabilities , cs ) ) ) ;
578
+ var initSsl = new PayloadData ( new ArraySegment < byte > ( HandshakeResponse41Packet . InitSsl ( serverCapabilities , cs , m_useCompression ) ) ) ;
581
579
await SendReplyAsync ( initSsl , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
582
580
583
581
try
@@ -596,6 +594,7 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
596
594
}
597
595
var sslByteHandler = new StreamByteHandler ( sslStream ) ;
598
596
m_payloadHandler . ByteHandler = sslByteHandler ;
597
+ m_isSecureConnection = true ;
599
598
}
600
599
catch ( Exception ex )
601
600
{
@@ -730,5 +729,7 @@ private enum State
730
729
IPayloadHandler m_payloadHandler ;
731
730
MySqlCommand m_activeCommand ;
732
731
MySqlDataReader m_activeReader ;
732
+ bool m_useCompression ;
733
+ bool m_isSecureConnection ;
733
734
}
734
735
}
0 commit comments