ChallengeResponse.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // Mono.Security.Protocol.Ntlm.ChallengeResponse
  3. // Implements Challenge Response for NTLM v1
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. // Atsushi Enomoto <[email protected]>
  8. //
  9. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  10. // (C) 2004, 2007 Novell (http://www.novell.com)
  11. //
  12. // References
  13. // a. NTLM Authentication Scheme for HTTP, Ronald Tschalär
  14. // http://www.innovation.ch/java/ntlm.html
  15. // b. The NTLM Authentication Protocol, Copyright © 2003 Eric Glass
  16. // http://davenport.sourceforge.net/ntlm.html
  17. //
  18. //
  19. // Permission is hereby granted, free of charge, to any person obtaining
  20. // a copy of this software and associated documentation files (the
  21. // "Software"), to deal in the Software without restriction, including
  22. // without limitation the rights to use, copy, modify, merge, publish,
  23. // distribute, sublicense, and/or sell copies of the Software, and to
  24. // permit persons to whom the Software is furnished to do so, subject to
  25. // the following conditions:
  26. //
  27. // The above copyright notice and this permission notice shall be
  28. // included in all copies or substantial portions of the Software.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  34. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  35. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. //
  38. using System;
  39. using System.Globalization;
  40. using System.Security.Cryptography;
  41. using System.Text;
  42. using Mono.Security.Cryptography;
  43. namespace Mono.Security.Protocol.Ntlm {
  44. public class ChallengeResponse : IDisposable {
  45. static private byte[] magic = { 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
  46. // This is the pre-encrypted magic value with a null DES key (0xAAD3B435B51404EE)
  47. // Ref: http://packetstormsecurity.nl/Crackers/NT/l0phtcrack/l0phtcrack2.5-readme.html
  48. static private byte[] nullEncMagic = { 0xAA, 0xD3, 0xB4, 0x35, 0xB5, 0x14, 0x04, 0xEE };
  49. private bool _disposed;
  50. private byte[] _challenge;
  51. private byte[] _lmpwd;
  52. private byte[] _ntpwd;
  53. // constructors
  54. public ChallengeResponse ()
  55. {
  56. _disposed = false;
  57. _lmpwd = new byte [21];
  58. _ntpwd = new byte [21];
  59. }
  60. public ChallengeResponse (string password, byte[] challenge) : this ()
  61. {
  62. Password = password;
  63. Challenge = challenge;
  64. }
  65. ~ChallengeResponse ()
  66. {
  67. if (!_disposed)
  68. Dispose ();
  69. }
  70. // properties
  71. public string Password {
  72. get { return null; }
  73. set {
  74. if (_disposed)
  75. throw new ObjectDisposedException ("too late");
  76. // create Lan Manager password
  77. DES des = DES.Create ();
  78. des.Mode = CipherMode.ECB;
  79. ICryptoTransform ct = null;
  80. // Note: In .NET DES cannot accept a weak key
  81. // this can happen for a null password
  82. if ((value == null) || (value.Length < 1)) {
  83. Buffer.BlockCopy (nullEncMagic, 0, _lmpwd, 0, 8);
  84. }
  85. else {
  86. des.Key = PasswordToKey (value, 0);
  87. ct = des.CreateEncryptor ();
  88. ct.TransformBlock (magic, 0, 8, _lmpwd, 0);
  89. }
  90. // and if a password has less than 8 characters
  91. if ((value == null) || (value.Length < 8)) {
  92. Buffer.BlockCopy (nullEncMagic, 0, _lmpwd, 8, 8);
  93. }
  94. else {
  95. des.Key = PasswordToKey (value, 7);
  96. ct = des.CreateEncryptor ();
  97. ct.TransformBlock (magic, 0, 8, _lmpwd, 8);
  98. }
  99. // create NT password
  100. MD4 md4 = MD4.Create ();
  101. byte[] data = ((value == null) ? (new byte [0]) : (Encoding.Unicode.GetBytes (value)));
  102. byte[] hash = md4.ComputeHash (data);
  103. Buffer.BlockCopy (hash, 0, _ntpwd, 0, 16);
  104. // clean up
  105. Array.Clear (data, 0, data.Length);
  106. Array.Clear (hash, 0, hash.Length);
  107. des.Clear ();
  108. }
  109. }
  110. public byte[] Challenge {
  111. get { return null; }
  112. set {
  113. if (value == null)
  114. throw new ArgumentNullException ("Challenge");
  115. if (_disposed)
  116. throw new ObjectDisposedException ("too late");
  117. // we don't want the caller to modify the value afterward
  118. _challenge = (byte[]) value.Clone ();
  119. }
  120. }
  121. public byte[] LM {
  122. get {
  123. if (_disposed)
  124. throw new ObjectDisposedException ("too late");
  125. return GetResponse (_lmpwd);
  126. }
  127. }
  128. public byte[] NT {
  129. get {
  130. if (_disposed)
  131. throw new ObjectDisposedException ("too late");
  132. return GetResponse (_ntpwd);
  133. }
  134. }
  135. public byte [] LMSessionKey {
  136. get {
  137. if (_disposed)
  138. throw new ObjectDisposedException ("too late");
  139. byte[] lm = LM;
  140. byte[] pwd = new byte [14];
  141. Buffer.BlockCopy (lm, 0, pwd, 0, 8);
  142. for (int i = 8; i < 14; i++)
  143. pwd [i] = 0xBD;
  144. byte[] response = new byte [16];
  145. DES des = DES.Create ();
  146. des.Mode = CipherMode.ECB;
  147. des.Key = PrepareDESKey (pwd, 0);
  148. ICryptoTransform ct = des.CreateEncryptor ();
  149. ct.TransformBlock (lm, 0, 8, response, 0);
  150. des.Key = PrepareDESKey (pwd, 7);
  151. ct = des.CreateEncryptor ();
  152. ct.TransformBlock (lm, 0, 8, response, 8);
  153. return response;
  154. }
  155. }
  156. // IDisposable method
  157. public void Dispose ()
  158. {
  159. Dispose (true);
  160. GC.SuppressFinalize (this);
  161. }
  162. private void Dispose (bool disposing)
  163. {
  164. if (!_disposed) {
  165. // cleanup our stuff
  166. Array.Clear (_lmpwd, 0, _lmpwd.Length);
  167. Array.Clear (_ntpwd, 0, _ntpwd.Length);
  168. if (_challenge != null)
  169. Array.Clear (_challenge, 0, _challenge.Length);
  170. _disposed = true;
  171. }
  172. }
  173. // private methods
  174. private byte[] GetResponse (byte[] pwd)
  175. {
  176. byte[] response = new byte [24];
  177. DES des = DES.Create ();
  178. des.Mode = CipherMode.ECB;
  179. des.Key = PrepareDESKey (pwd, 0);
  180. ICryptoTransform ct = des.CreateEncryptor ();
  181. ct.TransformBlock (_challenge, 0, 8, response, 0);
  182. des.Key = PrepareDESKey (pwd, 7);
  183. ct = des.CreateEncryptor ();
  184. ct.TransformBlock (_challenge, 0, 8, response, 8);
  185. des.Key = PrepareDESKey (pwd, 14);
  186. ct = des.CreateEncryptor ();
  187. ct.TransformBlock (_challenge, 0, 8, response, 16);
  188. return response;
  189. }
  190. private byte[] PrepareDESKey (byte[] key56bits, int position)
  191. {
  192. // convert to 8 bytes
  193. byte[] key = new byte [8];
  194. key [0] = key56bits [position];
  195. key [1] = (byte) ((key56bits [position] << 7) | (key56bits [position + 1] >> 1));
  196. key [2] = (byte) ((key56bits [position + 1] << 6) | (key56bits [position + 2] >> 2));
  197. key [3] = (byte) ((key56bits [position + 2] << 5) | (key56bits [position + 3] >> 3));
  198. key [4] = (byte) ((key56bits [position + 3] << 4) | (key56bits [position + 4] >> 4));
  199. key [5] = (byte) ((key56bits [position + 4] << 3) | (key56bits [position + 5] >> 5));
  200. key [6] = (byte) ((key56bits [position + 5] << 2) | (key56bits [position + 6] >> 6));
  201. key [7] = (byte) (key56bits [position + 6] << 1);
  202. return key;
  203. }
  204. private byte[] PasswordToKey (string password, int position)
  205. {
  206. byte[] key7 = new byte [7];
  207. int len = System.Math.Min (password.Length - position, 7);
  208. Encoding.ASCII.GetBytes (password.ToUpper (CultureInfo.CurrentCulture), position, len, key7, 0);
  209. byte[] key8 = PrepareDESKey (key7, 0);
  210. // cleanup intermediate key material
  211. Array.Clear (key7, 0, key7.Length);
  212. return key8;
  213. }
  214. }
  215. }