You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.9 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright ?2012, 2018, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.Security.Cryptography;
  25. namespace Externals.MySql.Data.MySqlClient.Authentication
  26. {
  27. /// <summary>
  28. /// Allows connections to a user account set with the mysql_native_password authentication plugin.
  29. /// </summary>
  30. internal class MySqlNativePasswordPlugin : MySqlAuthenticationPlugin
  31. {
  32. public override string PluginName => "mysql_native_password";
  33. protected override void SetAuthData(byte[] data)
  34. {
  35. // if the data given to us is a null terminated string, we need to trim off the trailing zero
  36. if (data[data.Length - 1] == 0)
  37. {
  38. byte[] b = new byte[data.Length - 1];
  39. Buffer.BlockCopy(data, 0, b, 0, data.Length - 1);
  40. base.SetAuthData(b);
  41. }
  42. else
  43. base.SetAuthData(data);
  44. }
  45. protected override byte[] MoreData(byte[] data)
  46. {
  47. byte[] passBytes = (GetPassword() ?? new byte[1]) as byte[];
  48. byte[] buffer = new byte[passBytes.Length - 1];
  49. Array.Copy(passBytes, 1, buffer, 0, passBytes.Length - 1);
  50. return buffer;
  51. }
  52. public override object GetPassword()
  53. {
  54. byte[] bytes = Get411Password(Settings.Password, AuthenticationData);
  55. if (bytes != null && bytes.Length == 1 && bytes[0] == 0) return null;
  56. return bytes;
  57. }
  58. /// <summary>
  59. /// Returns a byte array containing the proper encryption of the
  60. /// given password/seed according to the new 4.1.1 authentication scheme.
  61. /// </summary>
  62. /// <param name="password"></param>
  63. /// <param name="seedBytes"></param>
  64. /// <returns></returns>
  65. protected byte[] Get411Password(string password, byte[] seedBytes)
  66. {
  67. // if we have no password, then we just return 1 zero byte
  68. if (password.Length == 0) return new byte[1];
  69. //SHA1 sha = new SHA1CryptoServiceProvider();
  70. SHA1 sha = SHA1.Create();
  71. byte[] firstHash = sha.ComputeHash(this.Encoding.GetBytes(password));
  72. byte[] secondHash = sha.ComputeHash(firstHash);
  73. byte[] input = new byte[seedBytes.Length + secondHash.Length];
  74. Array.Copy(seedBytes, 0, input, 0, seedBytes.Length);
  75. Array.Copy(secondHash, 0, input, seedBytes.Length, secondHash.Length);
  76. byte[] thirdHash = sha.ComputeHash(input);
  77. byte[] finalHash = new byte[thirdHash.Length + 1];
  78. finalHash[0] = 0x14;
  79. Array.Copy(thirdHash, 0, finalHash, 1, thirdHash.Length);
  80. for (int i = 1; i < finalHash.Length; i++)
  81. finalHash[i] = (byte)(finalHash[i] ^ firstHash[i - 1]);
  82. return finalHash;
  83. }
  84. }
  85. }
  86. #endif