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.

190 lines
7.6 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright ?2004, 2016 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. #if NETSTANDARD1_3
  26. using Externals.MySql.Data.MySqlClient.Framework.NetStandard1_3;
  27. #else
  28. using System.Text;
  29. #endif
  30. namespace Externals.MySql.Data.MySqlClient
  31. {
  32. /// <summary>
  33. /// Summary description for Crypt.
  34. /// </summary>
  35. internal class Crypt
  36. {
  37. /// <summary>
  38. /// Simple XOR scramble
  39. /// </summary>
  40. /// <param name="from">Source array</param>
  41. /// <param name="fromIndex">Index inside source array</param>
  42. /// <param name="to">Destination array</param>
  43. /// <param name="toIndex">Index inside destination array</param>
  44. /// <param name="password">Password used to xor the bits</param>
  45. /// <param name="length">Number of bytes to scramble</param>
  46. private static void XorScramble(byte[] from, int fromIndex, byte[] to, int toIndex,
  47. byte[] password, int length)
  48. {
  49. // make sure we were called properly
  50. if (fromIndex < 0 || fromIndex >= from.Length)
  51. throw new ArgumentException(Resources.IndexMustBeValid, nameof(fromIndex));
  52. if ((fromIndex + length) > from.Length)
  53. throw new ArgumentException(Resources.FromAndLengthTooBig, nameof(fromIndex));
  54. if (from == null)
  55. throw new ArgumentException(Resources.BufferCannotBeNull, nameof(@from));
  56. if (to == null)
  57. throw new ArgumentException(Resources.BufferCannotBeNull, nameof(to));
  58. if (toIndex < 0 || toIndex >= to.Length)
  59. throw new ArgumentException(Resources.IndexMustBeValid, nameof(toIndex));
  60. if ((toIndex + length) > to.Length)
  61. throw new ArgumentException(Resources.IndexAndLengthTooBig, nameof(toIndex));
  62. if (password == null || password.Length < length)
  63. throw new ArgumentException(Resources.PasswordMustHaveLegalChars, nameof(password));
  64. if (length < 0)
  65. throw new ArgumentException(Resources.ParameterCannotBeNegative, nameof(length));
  66. // now perform the work
  67. for (int i = 0; i < length; i++)
  68. to[toIndex++] = (byte)(from[fromIndex++] ^ password[i]);
  69. }
  70. /// <summary>
  71. /// Returns a byte array containing the proper encryption of the
  72. /// given password/seed according to the new 4.1.1 authentication scheme.
  73. /// </summary>
  74. /// <param name="password"></param>
  75. /// <param name="seed"></param>
  76. /// <returns></returns>
  77. public static byte[] Get411Password(string password, string seed)
  78. {
  79. // if we have no password, then we just return 2 zero bytes
  80. if (password.Length == 0) return new byte[1];
  81. SHA1 sha = SHA1.Create();
  82. byte[] firstHash = sha.ComputeHash(Encoding.Default.GetBytes(password));
  83. byte[] secondHash = sha.ComputeHash(firstHash);
  84. byte[] seedBytes = Encoding.Default.GetBytes(seed);
  85. byte[] input = new byte[seedBytes.Length + secondHash.Length];
  86. Array.Copy(seedBytes, 0, input, 0, seedBytes.Length);
  87. Array.Copy(secondHash, 0, input, seedBytes.Length, secondHash.Length);
  88. byte[] thirdHash = sha.ComputeHash(input);
  89. byte[] finalHash = new byte[thirdHash.Length + 1];
  90. finalHash[0] = 0x14;
  91. Array.Copy(thirdHash, 0, finalHash, 1, thirdHash.Length);
  92. for (int i = 1; i < finalHash.Length; i++)
  93. finalHash[i] = (byte)(finalHash[i] ^ firstHash[i - 1]);
  94. return finalHash;
  95. //byte[] buffer = new byte[finalHash.Length - 1];
  96. //Array.Copy(finalHash, 1, buffer, 0, finalHash.Length - 1);
  97. //return buffer;
  98. }
  99. private static double rand(ref long seed1, ref long seed2, long max)
  100. {
  101. seed1 = (seed1 * 3) + seed2;
  102. seed1 %= max;
  103. seed2 = (seed1 + seed2 + 33) % max;
  104. return (seed1 / (double)max);
  105. }
  106. /// <summary>
  107. /// Encrypts a password using the MySql encryption scheme
  108. /// </summary>
  109. /// <param name="password">The password to encrypt</param>
  110. /// <param name="seed">The encryption seed the server gave us</param>
  111. /// <param name="new_ver">Indicates if we should use the old or new encryption scheme</param>
  112. /// <returns></returns>
  113. public static String EncryptPassword(String password, String seed, bool new_ver)
  114. {
  115. long max = 0x3fffffff;
  116. if (!new_ver)
  117. max = 0x01FFFFFF;
  118. if (string.IsNullOrEmpty(password))
  119. return password;
  120. long[] hash_seed = Hash(seed);
  121. long[] hash_pass = Hash(password);
  122. long seed1 = (hash_seed[0] ^ hash_pass[0]) % max;
  123. long seed2 = (hash_seed[1] ^ hash_pass[1]) % max;
  124. if (!new_ver)
  125. seed2 = seed1 / 2;
  126. char[] scrambled = new char[seed.Length];
  127. for (int x = 0; x < seed.Length; x++)
  128. {
  129. double r = rand(ref seed1, ref seed2, max);
  130. scrambled[x] = (char)(Math.Floor(r * 31) + 64);
  131. }
  132. if (new_ver)
  133. {
  134. /* Make it harder to break */
  135. char extra = (char)Math.Floor(rand(ref seed1, ref seed2, max) * 31);
  136. for (int x = 0; x < scrambled.Length; x++)
  137. scrambled[x] ^= extra;
  138. }
  139. return new string(scrambled);
  140. }
  141. /// <summary>
  142. /// Hashes a password using the algorithm from Monty's code.
  143. /// The first element in the return is the result of the "old" hash.
  144. /// The second element is the rest of the "new" hash.
  145. /// </summary>
  146. /// <param name="P">Password to be hashed</param>
  147. /// <returns>Two element array containing the hashed values</returns>
  148. private static long[] Hash(String P)
  149. {
  150. long val1 = 1345345333;
  151. long val2 = 0x12345671;
  152. long inc = 7;
  153. for (int i = 0; i < P.Length; i++)
  154. {
  155. if (P[i] == ' ' || P[i] == '\t') continue;
  156. long temp = (0xff & P[i]);
  157. val1 ^= (((val1 & 63) + inc) * temp) + (val1 << 8);
  158. val2 += (val2 << 8) ^ val1;
  159. inc += temp;
  160. }
  161. long[] hash = new long[2];
  162. hash[0] = val1 & 0x7fffffff;
  163. hash[1] = val2 & 0x7fffffff;
  164. return hash;
  165. }
  166. }
  167. }
  168. #endif