@@ -326,35 +326,19 @@ private async Task<PayloadData> SwitchAuthenticationAsync(ConnectionSettings cs,
326
326
payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
327
327
328
328
var cachingSha2ServerResponsePayload = CachingSha2ServerResponsePayload . Create ( payload ) ;
329
-
330
329
if ( cachingSha2ServerResponsePayload . Succeeded )
331
- {
332
330
return await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
333
- }
334
331
335
- if ( ! m_isSecureConnection && cs . Password . Length > 1 )
336
- {
337
- #if NET45
338
- throw new MySqlException ( "Authentication method '{0}' requires a secure connection (prior to .NET 4.6)." . FormatInvariant ( switchRequest . Name ) ) ;
339
- #else
340
-
341
- var rsaPublicKey = await GetRsaPublicKeyForCachingSha2PasswordAsync ( switchRequest . Name , cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
342
- return await SendEncryptedPasswordAsync ( rsaPublicKey , RSAEncryptionPadding . Pkcs1 , cs , ioBehavior , switchRequest , cancellationToken ) . ConfigureAwait ( false ) ;
343
- #endif
344
- }
345
- else
346
- {
347
- return await SendClearPasswordAsync ( cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
348
- }
332
+ goto case "sha256_password" ;
349
333
350
334
case "sha256_password" :
351
335
if ( ! m_isSecureConnection && cs . Password . Length > 1 )
352
336
{
353
337
#if NET45
354
338
throw new MySqlException ( "Authentication method '{0}' requires a secure connection (prior to .NET 4.6)." . FormatInvariant ( switchRequest . Name ) ) ;
355
339
#else
356
- var publicKey = await GetRsaPublicKeyForSha256PasswordAsync ( switchRequest . Name , cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
357
- return await SendEncryptedPasswordAsync ( publicKey , RSAEncryptionPadding . OaepSHA1 , cs , ioBehavior , switchRequest , cancellationToken ) . ConfigureAwait ( false ) ;
340
+ var publicKey = await GetRsaPublicKeyAsync ( switchRequest . Name , cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
341
+ return await SendEncryptedPasswordAsync ( switchRequest , publicKey , cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
358
342
#endif
359
343
}
360
344
else
@@ -384,11 +368,10 @@ private async Task<PayloadData> SendClearPasswordAsync(ConnectionSettings cs, IO
384
368
385
369
#if ! NET45
386
370
private async Task < PayloadData > SendEncryptedPasswordAsync (
371
+ AuthenticationMethodSwitchRequestPayload switchRequest ,
387
372
string rsaPublicKey ,
388
- RSAEncryptionPadding rsaEncryptionPadding ,
389
373
ConnectionSettings cs ,
390
374
IOBehavior ioBehavior ,
391
- AuthenticationMethodSwitchRequestPayload switchRequest ,
392
375
CancellationToken cancellationToken )
393
376
{
394
377
// load the RSA public key
@@ -414,53 +397,16 @@ private async Task<PayloadData> SendEncryptedPasswordAsync(
414
397
passwordBytes [ i ] ^= AuthPluginData [ i % AuthPluginData . Length ] ;
415
398
416
399
// encrypt with RSA public key
417
- var encryptedPassword = rsa . Encrypt ( passwordBytes , rsaEncryptionPadding ) ;
400
+ var padding = switchRequest . Name == "caching_sha2_password" ? RSAEncryptionPadding . Pkcs1 : RSAEncryptionPadding . OaepSHA1 ;
401
+ var encryptedPassword = rsa . Encrypt ( passwordBytes , padding ) ;
418
402
var payload = new PayloadData ( new ArraySegment < byte > ( encryptedPassword ) ) ;
419
403
await SendReplyAsync ( payload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
420
404
return await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
421
405
}
422
406
}
423
407
#endif
424
408
425
- private async Task < string > GetRsaPublicKeyForSha256PasswordAsync (
426
- string switchRequestName ,
427
- ConnectionSettings cs ,
428
- IOBehavior ioBehavior ,
429
- CancellationToken cancellationToken )
430
- {
431
- if ( ! string . IsNullOrEmpty ( cs . ServerRsaPublicKeyFile ) )
432
- {
433
- try
434
- {
435
- return File . ReadAllText ( cs . ServerRsaPublicKeyFile ) ;
436
- }
437
- catch ( IOException ex )
438
- {
439
- throw new MySqlException (
440
- "Couldn't load server's RSA public key from '{0}'" . FormatInvariant ( cs . ServerRsaPublicKeyFile ) , ex ) ;
441
- }
442
- }
443
-
444
- if ( cs . AllowPublicKeyRetrieval )
445
- {
446
- // request the RSA public key
447
- await SendReplyAsync ( new PayloadData ( new ArraySegment < byte > ( new byte [ ] { 0x01 } , 0 , 1 ) ) , ioBehavior ,
448
- cancellationToken ) . ConfigureAwait ( false ) ;
449
- var payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
450
- var publicKeyPayload = AuthenticationMoreDataPayload . Create ( payload ) ;
451
- return Encoding . ASCII . GetString ( publicKeyPayload . Data ) ;
452
- }
453
-
454
- throw new MySqlException (
455
- "Authentication method '{0}' failed. Either use a secure connection, specify the server's RSA public key with ServerRSAPublicKeyFile, or set AllowPublicKeyRetrieval=True."
456
- . FormatInvariant ( switchRequestName ) ) ;
457
- }
458
-
459
- private async Task < string > GetRsaPublicKeyForCachingSha2PasswordAsync (
460
- string switchRequestName ,
461
- ConnectionSettings cs ,
462
- IOBehavior ioBehavior ,
463
- CancellationToken cancellationToken )
409
+ private async Task < string > GetRsaPublicKeyAsync ( string switchRequestName , ConnectionSettings cs , IOBehavior ioBehavior , CancellationToken cancellationToken )
464
410
{
465
411
if ( ! string . IsNullOrEmpty ( cs . ServerRsaPublicKeyFile ) )
466
412
{
@@ -470,24 +416,21 @@ private async Task<string> GetRsaPublicKeyForCachingSha2PasswordAsync(
470
416
}
471
417
catch ( IOException ex )
472
418
{
473
- throw new MySqlException (
474
- "Couldn't load server's RSA public key from '{0}'" . FormatInvariant ( cs . ServerRsaPublicKeyFile ) , ex ) ;
419
+ throw new MySqlException ( "Couldn't load server's RSA public key from '{0}'" . FormatInvariant ( cs . ServerRsaPublicKeyFile ) , ex ) ;
475
420
}
476
421
}
477
422
478
423
if ( cs . AllowPublicKeyRetrieval )
479
424
{
480
425
// request the RSA public key
481
- await SendReplyAsync ( new PayloadData ( new ArraySegment < byte > ( new byte [ ] { 0x02 } , 0 , 1 ) ) , ioBehavior ,
482
- cancellationToken ) . ConfigureAwait ( false ) ;
426
+ var payloadContent = switchRequestName == "caching_sha2_password" ? ( byte ) 0x02 : ( byte ) 0x01 ;
427
+ await SendReplyAsync ( new PayloadData ( new ArraySegment < byte > ( new byte [ ] { payloadContent } , 0 , 1 ) ) , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
483
428
var payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
484
429
var publicKeyPayload = AuthenticationMoreDataPayload . Create ( payload ) ;
485
430
return Encoding . ASCII . GetString ( publicKeyPayload . Data ) ;
486
431
}
487
432
488
- throw new MySqlException (
489
- "Authentication method '{0}' failed. Either use a secure connection, specify the server's RSA public key with ServerRSAPublicKeyFile, or set AllowPublicKeyRetrieval=True."
490
- . FormatInvariant ( switchRequestName ) ) ;
433
+ throw new MySqlException ( "Authentication method '{0}' failed. Either use a secure connection, specify the server's RSA public key with ServerRSAPublicKeyFile, or set AllowPublicKeyRetrieval=True." . FormatInvariant ( switchRequestName ) ) ;
491
434
}
492
435
493
436
public async Task < bool > TryPingAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
0 commit comments