NtlmContext.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using SharpCifs.Ntlmssp;
  19. using SharpCifs.Util;
  20. using SharpCifs.Util.Sharpen;
  21. namespace SharpCifs.Smb
  22. {
  23. /// <summary>For initiating NTLM authentication (including NTLMv2).</summary>
  24. /// <remarks>For initiating NTLM authentication (including NTLMv2). If you want to add NTLMv2 authentication support to something this is what you want to use. See the code for details. Note that JCIFS does not implement the acceptor side of NTLM authentication.
  25. /// </remarks>
  26. public class NtlmContext
  27. {
  28. internal NtlmPasswordAuthentication Auth;
  29. internal int NtlmsspFlags;
  30. internal string Workstation;
  31. internal bool isEstablished;
  32. internal byte[] ServerChallenge;
  33. internal byte[] SigningKey;
  34. internal string NetbiosName = null;
  35. internal int State = 1;
  36. internal LogStream Log;
  37. public NtlmContext(NtlmPasswordAuthentication auth, bool doSigning)
  38. {
  39. this.Auth = auth;
  40. NtlmsspFlags = NtlmsspFlags | NtlmFlags.NtlmsspRequestTarget | NtlmFlags.NtlmsspNegotiateNtlm2
  41. | NtlmFlags.NtlmsspNegotiate128;
  42. if (doSigning)
  43. {
  44. NtlmsspFlags |= NtlmFlags.NtlmsspNegotiateSign | NtlmFlags.NtlmsspNegotiateAlwaysSign
  45. | NtlmFlags.NtlmsspNegotiateKeyExch;
  46. }
  47. Workstation = Type1Message.GetDefaultWorkstation();
  48. Log = LogStream.GetInstance();
  49. }
  50. public override string ToString()
  51. {
  52. string ret = "NtlmContext[auth=" + Auth + ",ntlmsspFlags=0x" + Hexdump.ToHexString
  53. (NtlmsspFlags, 8) + ",workstation=" + Workstation + ",isEstablished=" + isEstablished
  54. + ",state=" + State + ",serverChallenge=";
  55. if (ServerChallenge == null)
  56. {
  57. ret += "null";
  58. }
  59. else
  60. {
  61. ret += Hexdump.ToHexString(ServerChallenge, 0, ServerChallenge.Length * 2);
  62. }
  63. ret += ",signingKey=";
  64. if (SigningKey == null)
  65. {
  66. ret += "null";
  67. }
  68. else
  69. {
  70. ret += Hexdump.ToHexString(SigningKey, 0, SigningKey.Length * 2);
  71. }
  72. ret += "]";
  73. return ret;
  74. }
  75. public virtual bool IsEstablished()
  76. {
  77. return isEstablished;
  78. }
  79. public virtual byte[] GetServerChallenge()
  80. {
  81. return ServerChallenge;
  82. }
  83. public virtual byte[] GetSigningKey()
  84. {
  85. return SigningKey;
  86. }
  87. public virtual string GetNetbiosName()
  88. {
  89. return NetbiosName;
  90. }
  91. private string GetNtlmsspListItem(byte[] type2Token, int id0)
  92. {
  93. int ri = 58;
  94. for (; ; )
  95. {
  96. int id = Encdec.Dec_uint16le(type2Token, ri);
  97. int len = Encdec.Dec_uint16le(type2Token, ri + 2);
  98. ri += 4;
  99. if (id == 0 || (ri + len) > type2Token.Length)
  100. {
  101. break;
  102. }
  103. if (id == id0)
  104. {
  105. try
  106. {
  107. return Runtime.GetStringForBytes(type2Token, ri, len, SmbConstants.UniEncoding
  108. );
  109. }
  110. catch (UnsupportedEncodingException)
  111. {
  112. break;
  113. }
  114. }
  115. ri += len;
  116. }
  117. return null;
  118. }
  119. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  120. public virtual byte[] InitSecContext(byte[] token, int offset, int len)
  121. {
  122. switch (State)
  123. {
  124. case 1:
  125. {
  126. Type1Message msg1 = new Type1Message(NtlmsspFlags, Auth.GetDomain(), Workstation);
  127. token = msg1.ToByteArray();
  128. if (Log.Level >= 4)
  129. {
  130. Log.WriteLine(msg1);
  131. if (Log.Level >= 6)
  132. {
  133. Hexdump.ToHexdump(Log, token, 0, token.Length);
  134. }
  135. }
  136. State++;
  137. break;
  138. }
  139. case 2:
  140. {
  141. try
  142. {
  143. Type2Message msg2 = new Type2Message(token);
  144. if (Log.Level >= 4)
  145. {
  146. Log.WriteLine(msg2);
  147. if (Log.Level >= 6)
  148. {
  149. Hexdump.ToHexdump(Log, token, 0, token.Length);
  150. }
  151. }
  152. ServerChallenge = msg2.GetChallenge();
  153. NtlmsspFlags &= msg2.GetFlags();
  154. // netbiosName = getNtlmsspListItem(token, 0x0001);
  155. Type3Message msg3 = new Type3Message(msg2, Auth.GetPassword(), Auth.GetDomain(),
  156. Auth.GetUsername(), Workstation, NtlmsspFlags);
  157. token = msg3.ToByteArray();
  158. if (Log.Level >= 4)
  159. {
  160. Log.WriteLine(msg3);
  161. if (Log.Level >= 6)
  162. {
  163. Hexdump.ToHexdump(Log, token, 0, token.Length);
  164. }
  165. }
  166. if ((NtlmsspFlags & NtlmFlags.NtlmsspNegotiateSign) != 0)
  167. {
  168. SigningKey = msg3.GetMasterKey();
  169. }
  170. isEstablished = true;
  171. State++;
  172. break;
  173. }
  174. catch (Exception e)
  175. {
  176. throw new SmbException(e.Message, e);
  177. }
  178. }
  179. default:
  180. {
  181. throw new SmbException("Invalid state");
  182. }
  183. }
  184. return token;
  185. }
  186. }
  187. }