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.

188 lines
6.9 KiB

4 years ago
  1. // Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc., 2013, 2014 Oracle and/or its affiliates. All rights reserved.
  2. //
  3. // MySQL Connector/NET is licensed under the terms of the GPLv2
  4. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  5. // MySQL Connectors. There are special exceptions to the terms and
  6. // conditions of the GPLv2 as it is applied to this software, see the
  7. // FLOSS License Exception
  8. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  9. //
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published
  12. // by the Free Software Foundation; version 2 of the License.
  13. //
  14. // This program is distributed in the hope that it will be useful, but
  15. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. // for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License along
  20. // with this program; if not, write to the Free Software Foundation, Inc.,
  21. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. #if MYSQL_6_9
  23. using System;
  24. using System.Globalization;
  25. using Externals.MySql.Data.Common;
  26. using Externals.MySql.Data.MySqlClient.Properties;
  27. using System.Security.Cryptography;
  28. using System.Text;
  29. namespace Externals.MySql.Data.MySqlClient
  30. {
  31. /// <summary>
  32. /// Summary description for Crypt.
  33. /// </summary>
  34. internal class Crypt
  35. {
  36. /// <summary>
  37. /// Simple XOR scramble
  38. /// </summary>
  39. /// <param name="from">Source array</param>
  40. /// <param name="fromIndex">Index inside source array</param>
  41. /// <param name="to">Destination array</param>
  42. /// <param name="toIndex">Index inside destination array</param>
  43. /// <param name="password">Password used to xor the bits</param>
  44. /// <param name="length">Number of bytes to scramble</param>
  45. private static void XorScramble(byte[] from, int fromIndex, byte[] to, int toIndex,
  46. byte[] password, int length)
  47. {
  48. // make sure we were called properly
  49. if (fromIndex < 0 || fromIndex >= from.Length)
  50. throw new ArgumentException(Resources.IndexMustBeValid, "fromIndex");
  51. if ((fromIndex + length) > from.Length)
  52. throw new ArgumentException(Resources.FromAndLengthTooBig, "fromIndex");
  53. if (from == null)
  54. throw new ArgumentException(Resources.BufferCannotBeNull, "from");
  55. if (to == null)
  56. throw new ArgumentException(Resources.BufferCannotBeNull, "to");
  57. if (toIndex < 0 || toIndex >= to.Length)
  58. throw new ArgumentException(Resources.IndexMustBeValid, "toIndex");
  59. if ((toIndex + length) > to.Length)
  60. throw new ArgumentException(Resources.IndexAndLengthTooBig, "toIndex");
  61. if (password == null || password.Length < length)
  62. throw new ArgumentException(Resources.PasswordMustHaveLegalChars, "password");
  63. if (length < 0)
  64. throw new ArgumentException(Resources.ParameterCannotBeNegative, "count");
  65. // now perform the work
  66. for (int i = 0; i < length; i++)
  67. to[toIndex++] = (byte)(from[fromIndex++] ^ password[i]);
  68. }
  69. /// <summary>
  70. /// Returns a byte array containing the proper encryption of the
  71. /// given password/seed according to the new 4.1.1 authentication scheme.
  72. /// </summary>
  73. /// <param name="password"></param>
  74. /// <param name="seed"></param>
  75. /// <returns></returns>
  76. public static byte[] Get411Password(string password, string seed)
  77. {
  78. // if we have no password, then we just return 2 zero bytes
  79. if (password.Length == 0) return new byte[1];
  80. SHA1 sha = new SHA1CryptoServiceProvider();
  81. byte[] firstHash = sha.ComputeHash(Encoding.Default.GetBytes(password));
  82. byte[] secondHash = sha.ComputeHash(firstHash);
  83. byte[] seedBytes = Encoding.Default.GetBytes(seed);
  84. byte[] input = new byte[seedBytes.Length + secondHash.Length];
  85. Array.Copy(seedBytes, 0, input, 0, seedBytes.Length);
  86. Array.Copy(secondHash, 0, input, seedBytes.Length, secondHash.Length);
  87. byte[] thirdHash = sha.ComputeHash(input);
  88. byte[] finalHash = new byte[thirdHash.Length + 1];
  89. finalHash[0] = 0x14;
  90. Array.Copy(thirdHash, 0, finalHash, 1, thirdHash.Length);
  91. for (int i = 1; i < finalHash.Length; i++)
  92. finalHash[i] = (byte)(finalHash[i] ^ firstHash[i - 1]);
  93. return finalHash;
  94. //byte[] buffer = new byte[finalHash.Length - 1];
  95. //Array.Copy(finalHash, 1, buffer, 0, finalHash.Length - 1);
  96. //return buffer;
  97. }
  98. private static double rand(ref long seed1, ref long seed2, long max)
  99. {
  100. seed1 = (seed1 * 3) + seed2;
  101. seed1 %= max;
  102. seed2 = (seed1 + seed2 + 33) % max;
  103. return (seed1 / (double)max);
  104. }
  105. /// <summary>
  106. /// Encrypts a password using the MySql encryption scheme
  107. /// </summary>
  108. /// <param name="password">The password to encrypt</param>
  109. /// <param name="seed">The encryption seed the server gave us</param>
  110. /// <param name="new_ver">Indicates if we should use the old or new encryption scheme</param>
  111. /// <returns></returns>
  112. public static String EncryptPassword(String password, String seed, bool new_ver)
  113. {
  114. long max = 0x3fffffff;
  115. if (!new_ver)
  116. max = 0x01FFFFFF;
  117. if (password == null || password.Length == 0)
  118. return password;
  119. long[] hash_seed = Hash(seed);
  120. long[] hash_pass = Hash(password);
  121. long seed1 = (hash_seed[0] ^ hash_pass[0]) % max;
  122. long seed2 = (hash_seed[1] ^ hash_pass[1]) % max;
  123. if (!new_ver)
  124. seed2 = seed1 / 2;
  125. char[] scrambled = new char[seed.Length];
  126. for (int x = 0; x < seed.Length; x++)
  127. {
  128. double r = rand(ref seed1, ref seed2, max);
  129. scrambled[x] = (char)(Math.Floor(r * 31) + 64);
  130. }
  131. if (new_ver)
  132. {
  133. /* Make it harder to break */
  134. char extra = (char)Math.Floor(rand(ref seed1, ref seed2, max) * 31);
  135. for (int x = 0; x < scrambled.Length; x++)
  136. scrambled[x] ^= extra;
  137. }
  138. return new string(scrambled);
  139. }
  140. /// <summary>
  141. /// Hashes a password using the algorithm from Monty's code.
  142. /// The first element in the return is the result of the "old" hash.
  143. /// The second element is the rest of the "new" hash.
  144. /// </summary>
  145. /// <param name="P">Password to be hashed</param>
  146. /// <returns>Two element array containing the hashed values</returns>
  147. private static long[] Hash(String P)
  148. {
  149. long val1 = 1345345333;
  150. long val2 = 0x12345671;
  151. long inc = 7;
  152. for (int i = 0; i < P.Length; i++)
  153. {
  154. if (P[i] == ' ' || P[i] == '\t') continue;
  155. long temp = (0xff & P[i]);
  156. val1 ^= (((val1 & 63) + inc) * temp) + (val1 << 8);
  157. val2 += (val2 << 8) ^ val1;
  158. inc += temp;
  159. }
  160. long[] hash = new long[2];
  161. hash[0] = val1 & 0x7fffffff;
  162. hash[1] = val2 & 0x7fffffff;
  163. return hash;
  164. }
  165. }
  166. }
  167. #endif