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.

103 lines
3.6 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright © 2012, 2017 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.IO;
  24. using System;
  25. using Externals.MySql.Data.MySqlClient.Properties;
  26. using Externals.MySql.Data.Common;
  27. using System.Security.Cryptography;
  28. using System.Text;
  29. namespace Externals.MySql.Data.MySqlClient.Authentication
  30. {
  31. internal class MySqlNativePasswordPlugin : MySqlAuthenticationPlugin
  32. {
  33. public override string PluginName
  34. {
  35. get { return "mysql_native_password"; }
  36. }
  37. protected override void SetAuthData(byte[] data)
  38. {
  39. // if the data given to us is a null terminated string, we need to trim off the trailing zero
  40. if (data[data.Length - 1] == 0)
  41. {
  42. byte[] b = new byte[data.Length - 1];
  43. Buffer.BlockCopy(data, 0, b, 0, data.Length - 1);
  44. base.SetAuthData(b);
  45. }
  46. else
  47. base.SetAuthData(data);
  48. }
  49. protected override byte[] MoreData(byte[] data)
  50. {
  51. byte[] passBytes = GetPassword() as byte[];
  52. byte[] buffer = new byte[passBytes.Length - 1];
  53. Array.Copy(passBytes, 1, buffer, 0, passBytes.Length - 1);
  54. return buffer;
  55. }
  56. public override object GetPassword()
  57. {
  58. byte[] bytes = Get411Password(Settings.Password, AuthenticationData);
  59. if (bytes != null && bytes.Length == 1 && bytes[0] == 0) return null;
  60. return bytes;
  61. }
  62. /// <summary>
  63. /// Returns a byte array containing the proper encryption of the
  64. /// given password/seed according to the new 4.1.1 authentication scheme.
  65. /// </summary>
  66. /// <param name="password"></param>
  67. /// <param name="seedBytes"></param>
  68. /// <returns></returns>
  69. private byte[] Get411Password(string password, byte[] seedBytes)
  70. {
  71. // if we have no password, then we just return 1 zero byte
  72. if (password.Length == 0) return new byte[1];
  73. SHA1 sha = new SHA1CryptoServiceProvider();
  74. byte[] firstHash = sha.ComputeHash(this.Encoding.GetBytes(password));
  75. byte[] secondHash = sha.ComputeHash(firstHash);
  76. byte[] input = new byte[seedBytes.Length + secondHash.Length];
  77. Array.Copy(seedBytes, 0, input, 0, seedBytes.Length);
  78. Array.Copy(secondHash, 0, input, seedBytes.Length, secondHash.Length);
  79. byte[] thirdHash = sha.ComputeHash(input);
  80. byte[] finalHash = new byte[thirdHash.Length + 1];
  81. finalHash[0] = 0x14;
  82. Array.Copy(thirdHash, 0, finalHash, 1, thirdHash.Length);
  83. for (int i = 1; i < finalHash.Length; i++)
  84. finalHash[i] = (byte)(finalHash[i] ^ firstHash[i - 1]);
  85. return finalHash;
  86. }
  87. }
  88. }
  89. #endif