2
0

SspiSession.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using Mono.Security;
  8. using Mono.Security.Protocol.Ntlm;
  9. namespace System.ServiceModel.Security
  10. {
  11. internal abstract class SspiSession
  12. {
  13. internal static readonly byte [] NtlmSSP = new byte [] {
  14. 0x4E, 0x54, 0x4C, 0x4D, 0x53, 0x53, 0x50, 0x00};
  15. public long Challenge, Context, ClientOSVersion, ServerOSVersion;
  16. public string ServerName, DomainName, DnsHostName, DnsDomainName;
  17. public bool Verify (byte [] expected, byte [] actual, int offset, int length)
  18. {
  19. if (expected.Length != length)
  20. return false;
  21. for (int i = 0; i < length; i++)
  22. if (expected [i] != actual [i + offset])
  23. return false;
  24. return true;
  25. }
  26. public SspiSecurityBufferStruct ReadSecurityBuffer (BinaryReader reader)
  27. {
  28. return new SspiSecurityBufferStruct (
  29. reader.ReadInt16 (),
  30. reader.ReadInt16 (),
  31. reader.ReadInt32 ());
  32. }
  33. }
  34. internal struct SspiSecurityBufferStruct
  35. {
  36. public SspiSecurityBufferStruct (short length, short allocatedSpace, int offset)
  37. {
  38. Length = length;
  39. AllocatedSpace = allocatedSpace;
  40. Offset = offset;
  41. }
  42. public readonly short Length;
  43. public readonly short AllocatedSpace;
  44. public readonly int Offset;
  45. }
  46. internal class SspiClientSession : SspiSession
  47. {
  48. Type2Message type2;
  49. Type3Message type3;
  50. // Class(60) {
  51. // OID(spnego),
  52. // Class(A0) {
  53. // Class(30) {
  54. // Class(A0) {
  55. // Class(30) { OID,OID,OID} },
  56. // Class(A2) { OctetStream } } } }
  57. public byte [] ProcessSpnegoInitialContextTokenRequest ()
  58. {
  59. Type1Message type1 = new Type1Message (NtlmVersion.Version3);
  60. type1.Flags = unchecked ((NtlmFlags) 0xE21882B7);
  61. type1.Domain = "WORKGROUP"; // FIXME: remove it
  62. ASN1 asn = new ASN1 (0x60);
  63. ASN1 asn2 = new ASN1 (0xA0);
  64. ASN1 asn21 = new ASN1 (0x30);
  65. ASN1 asn211 = new ASN1 (0xA0);
  66. ASN1 asn2111 = new ASN1 (0x30);
  67. asn211.Add (asn2111);
  68. asn2111.Add (ASN1Convert.FromOid (Constants.OidNtlmSsp));
  69. asn2111.Add (ASN1Convert.FromOid (Constants.OidKerberos5));
  70. asn2111.Add (ASN1Convert.FromOid (Constants.OidMIT));
  71. ASN1 asn212 = new ASN1 (0xA2);
  72. ASN1 asn2121 = new ASN1 (0x4);
  73. asn2121.Value = type1.GetBytes ();
  74. asn212.Add (asn2121);
  75. asn21.Add (asn211);
  76. asn21.Add (asn212);
  77. asn2.Add (asn21);
  78. asn.Add (ASN1Convert.FromOid (Constants.OidSpnego));
  79. asn.Add (asn2);
  80. return asn.GetBytes ();
  81. }
  82. // Example buffer:
  83. // A18181 307F A003
  84. // 0A0101
  85. // A10C 060A2B06010401823702020A
  86. // A26A 0468 NTLM
  87. // NTLM = 4E544C4D53535000 0200000004000400 3800000035829AE2
  88. // 0D1A7FF0F171F339 0000000000000000 2C002C003C000000
  89. // 0501280A0000000F 5000430002000400 5000430001000400
  90. // 5000430004000400 5000430003000400 5000430006000400
  91. // 0100000000000000
  92. public void ProcessSpnegoInitialContextTokenResponse (byte [] raw)
  93. {
  94. ASN1 asn1 = new ASN1 (raw);
  95. // FIXME: check OIDs and structure
  96. ProcessMessageType2 (asn1 [0] [2] [0].Value);
  97. }
  98. // Class { Class { Class { OctetStream } } }
  99. public byte [] ProcessSpnegoProcessContextToken (string user, string pass)
  100. {
  101. ASN1 asn = new ASN1 (0xA1);
  102. ASN1 asn2 = new ASN1 (0x30);
  103. ASN1 asn3 = new ASN1 (0xA2);
  104. asn3.Add (new ASN1 (0x04, ProcessMessageType3 (user, pass)));
  105. asn2.Add (asn3);
  106. asn.Add (asn2);
  107. return asn.GetBytes ();
  108. }
  109. public byte [] ProcessMessageType1 ()
  110. {
  111. Type1Message type1 = new Type1Message (NtlmVersion.Version3);
  112. type1.Flags = unchecked ((NtlmFlags) 0xE21882B7);
  113. return type1.GetBytes ();
  114. }
  115. string TargetName;
  116. public void ProcessMessageType2 (byte [] raw)
  117. {
  118. type2 = new Type2Message (raw);
  119. }
  120. public byte [] ProcessMessageType3 (string user, string password)
  121. {
  122. TargetName = Environment.MachineName;
  123. ServerName = Environment.MachineName;
  124. // FIXME
  125. DomainName = ServerName;// IPGlobalProperties.GetIPGlobalProperties ().DomainName;
  126. DnsHostName = Dns.GetHostName ();
  127. DnsDomainName = DnsHostName; // FIXME
  128. type3 = new Type3Message (NtlmVersion.Version3);
  129. type3.Flags = (NtlmFlags) (unchecked ((int) 0xE2188235));
  130. type3.Domain = DomainName;
  131. type3.Host = DnsHostName;
  132. type3.Challenge = type2.Nonce;
  133. type3.Username = user;
  134. type3.Password = password;
  135. return type3.GetBytes ();
  136. }
  137. }
  138. internal class SspiServerSession : SspiSession
  139. {
  140. public string TargetName;
  141. public long SuppliedDomain, SuppliedWorkstation;
  142. Type1Message type1;
  143. Type2Message type2;
  144. Type3Message type3;
  145. // Example buffer:
  146. // 6069 0606 2B0601050502 A05F 305D A024 3022
  147. // 060A 2B06010401823702020A
  148. // 0609 2A864882F712010202
  149. // 0609 2A864886F712010202
  150. // A235 0433 NTLM
  151. // NTLM = 4E544C4D53535000 01000000 B7B218E2 090009002A000000
  152. // 0200020028000000 0501280A0000000F 5043 574F524B47524F5550
  153. public void ProcessSpnegoInitialContextTokenRequest (byte [] raw)
  154. {
  155. ASN1 asn1 = new ASN1 (raw);
  156. // FIXME: check OIDs
  157. ProcessMessageType1 (asn1 [1] [0] [1] [0].Value);
  158. }
  159. // Class {
  160. // Class {
  161. // Class { Enum },
  162. // Class { OID(NTLMSSP) },
  163. // Class { OctetStream } } }
  164. public byte [] ProcessSpnegoInitialContextTokenResponse ()
  165. {
  166. ASN1 top = new ASN1 (0xA1);
  167. ASN1 asn = new ASN1 (0x30);
  168. ASN1 asn1 = new ASN1 (0xA0);
  169. // FIXME: what is this enum?
  170. asn1.Add (new ASN1 (0x0A, new byte [] {1})); // Enum whatever
  171. ASN1 asn2 = new ASN1 (0xA1);
  172. asn2.Add (ASN1Convert.FromOid (Constants.OidNtlmSsp));
  173. ASN1 asn3 = new ASN1 (0xA2);
  174. asn3.Add (new ASN1 (0x04, ProcessMessageType2 ()));
  175. asn.Add (asn1);
  176. asn.Add (asn2);
  177. asn.Add (asn3);
  178. top.Add (asn);
  179. return top.GetBytes ();
  180. }
  181. // Example buffer:
  182. // A181A7
  183. // 3081A4
  184. // A281A1
  185. // 04819E
  186. // 4E544C4D53535000 03000000
  187. // 180018005E000000 1800180076000000 0400040048000000
  188. // 0E000E004C000000 040004005A000000 100010008E000000
  189. // 358218E2 0501280A0000000F
  190. // 50004300 6100740073007500730068006900 50004300
  191. // [8 bytes LM] [16 bytes of 0s]
  192. // [24 bytes of NTLM]
  193. // C94EE2ADE7E32244 BD60D3B33609C167
  194. public void ProcessSpnegoProcessContextToken (byte [] raw)
  195. {
  196. ASN1 asn1 = new ASN1 (raw);
  197. // FIXME: check structure
  198. ProcessMessageType3 (asn1 [0] [0] [0].Value);
  199. }
  200. public void ProcessMessageType1 (byte [] raw)
  201. {
  202. type1 = new Type1Message (raw, NtlmVersion.Version3);
  203. }
  204. public byte [] ProcessMessageType2 ()
  205. {
  206. byte [] bytes = new byte [8];
  207. RandomNumberGenerator.Create ().GetNonZeroBytes (bytes);
  208. Challenge = bytes [0] << 24 + bytes [1] << 16 + bytes [2] << 8 + bytes [3];
  209. Context = 0; // FIXME
  210. ServerOSVersion = 0x0F00000A28010500; // FIXME
  211. TargetName = Environment.MachineName;
  212. ServerName = Environment.MachineName;
  213. // FIXME
  214. DomainName = ServerName;// IPGlobalProperties.GetIPGlobalProperties ().DomainName;
  215. DnsHostName = Dns.GetHostName ();
  216. DnsDomainName = DnsHostName; // FIXME
  217. type2 = new Type2Message (NtlmVersion.Version3);
  218. type2.Flags = (NtlmFlags) (unchecked ((int) 0xE21882B7));
  219. type2.TargetName = TargetName;
  220. type2.Target.ServerName = ServerName;
  221. type2.Target.DomainName = DomainName;
  222. type2.Target.DnsHostName = DnsHostName;
  223. type2.Target.DnsDomainName = DnsDomainName;
  224. return type2.GetBytes ();
  225. }
  226. public void ProcessMessageType3 (byte [] raw)
  227. {
  228. /*
  229. MemoryStream ms = new MemoryStream (raw);
  230. if (!Verify (NtlmSSP, raw, 0, 8))
  231. throw new SecurityNegotiationException ("Expected NTLM SSPI header not found");
  232. BinaryReader reader = new BinaryReader (ms);
  233. reader.ReadInt64 (); // skip 8 bytes
  234. if (reader.ReadInt32 () != 3)
  235. throw new SecurityNegotiationException ("SSPI type 3 message is expected");
  236. SspiSecurityBufferStruct lmResInfo = ReadSecurityBuffer (reader);
  237. SspiSecurityBufferStruct ntlmResInfo = ReadSecurityBuffer (reader);
  238. SspiSecurityBufferStruct targetNameInfo = ReadSecurityBuffer (reader);
  239. SspiSecurityBufferStruct userNameInfo = ReadSecurityBuffer (reader);
  240. SspiSecurityBufferStruct wsNameInfo = ReadSecurityBuffer (reader);
  241. SspiSecurityBufferStruct sessionKeyInfo = ReadSecurityBuffer (reader);
  242. int flags = reader.ReadInt32 ();
  243. ServerOSVersion = reader.ReadInt64 ();
  244. */
  245. type3 = new Type3Message (raw, NtlmVersion.Version3);
  246. }
  247. }
  248. }