@@ -326,35 +326,19 @@ private async Task<PayloadData> SwitchAuthenticationAsync(ConnectionSettings cs,
326326 payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
327327
328328 var cachingSha2ServerResponsePayload = CachingSha2ServerResponsePayload . Create ( payload ) ;
329-
330329 if ( cachingSha2ServerResponsePayload . Succeeded )
331- {
332330 return await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
333- }
334331
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" ;
349333
350334 case "sha256_password" :
351335 if ( ! m_isSecureConnection && cs . Password . Length > 1 )
352336 {
353337#if NET45
354338 throw new MySqlException ( "Authentication method '{0}' requires a secure connection (prior to .NET 4.6)." . FormatInvariant ( switchRequest . Name ) ) ;
355339#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 ) ;
358342#endif
359343 }
360344 else
@@ -384,11 +368,10 @@ private async Task<PayloadData> SendClearPasswordAsync(ConnectionSettings cs, IO
384368
385369#if ! NET45
386370 private async Task < PayloadData > SendEncryptedPasswordAsync (
371+ AuthenticationMethodSwitchRequestPayload switchRequest ,
387372 string rsaPublicKey ,
388- RSAEncryptionPadding rsaEncryptionPadding ,
389373 ConnectionSettings cs ,
390374 IOBehavior ioBehavior ,
391- AuthenticationMethodSwitchRequestPayload switchRequest ,
392375 CancellationToken cancellationToken )
393376 {
394377 // load the RSA public key
@@ -414,53 +397,16 @@ private async Task<PayloadData> SendEncryptedPasswordAsync(
414397 passwordBytes [ i ] ^= AuthPluginData [ i % AuthPluginData . Length ] ;
415398
416399 // 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 ) ;
418402 var payload = new PayloadData ( new ArraySegment < byte > ( encryptedPassword ) ) ;
419403 await SendReplyAsync ( payload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
420404 return await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
421405 }
422406 }
423407#endif
424408
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 )
464410 {
465411 if ( ! string . IsNullOrEmpty ( cs . ServerRsaPublicKeyFile ) )
466412 {
@@ -470,24 +416,21 @@ private async Task<string> GetRsaPublicKeyForCachingSha2PasswordAsync(
470416 }
471417 catch ( IOException ex )
472418 {
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 ) ;
475420 }
476421 }
477422
478423 if ( cs . AllowPublicKeyRetrieval )
479424 {
480425 // 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 ) ;
483428 var payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
484429 var publicKeyPayload = AuthenticationMoreDataPayload . Create ( payload ) ;
485430 return Encoding . ASCII . GetString ( publicKeyPayload . Data ) ;
486431 }
487432
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 ) ) ;
491434 }
492435
493436 public async Task < bool > TryPingAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
0 commit comments