GSSProvider.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright (C) 2017-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using SMBLibrary.Authentication.NTLM;
  10. using Utilities;
  11. namespace SMBLibrary.Authentication.GSSAPI
  12. {
  13. public class GSSContext
  14. {
  15. internal IGSSMechanism Mechanism;
  16. internal object MechanismContext;
  17. internal GSSContext(IGSSMechanism mechanism, object mechanismContext)
  18. {
  19. Mechanism = mechanism;
  20. MechanismContext = mechanismContext;
  21. }
  22. }
  23. public class GSSProvider
  24. {
  25. public static readonly byte[] NTLMSSPIdentifier = new byte[] { 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x02, 0x0a };
  26. private List<IGSSMechanism> m_mechanisms;
  27. public GSSProvider(IGSSMechanism mechanism)
  28. {
  29. m_mechanisms = new List<IGSSMechanism>();
  30. m_mechanisms.Add(mechanism);
  31. }
  32. public GSSProvider(List<IGSSMechanism> mechanisms)
  33. {
  34. m_mechanisms = mechanisms;
  35. }
  36. public byte[] GetSPNEGOTokenInitBytes()
  37. {
  38. SimpleProtectedNegotiationTokenInit token = new SimpleProtectedNegotiationTokenInit();
  39. token.MechanismTypeList = new List<byte[]>();
  40. foreach (IGSSMechanism mechanism in m_mechanisms)
  41. {
  42. token.MechanismTypeList.Add(mechanism.Identifier);
  43. }
  44. return token.GetBytes(true);
  45. }
  46. public virtual NTStatus AcceptSecurityContext(ref GSSContext context, byte[] inputToken, out byte[] outputToken)
  47. {
  48. outputToken = null;
  49. SimpleProtectedNegotiationToken spnegoToken = null;
  50. try
  51. {
  52. spnegoToken = SimpleProtectedNegotiationToken.ReadToken(inputToken, 0, false);
  53. }
  54. catch
  55. {
  56. }
  57. if (spnegoToken != null)
  58. {
  59. if (spnegoToken is SimpleProtectedNegotiationTokenInit)
  60. {
  61. SimpleProtectedNegotiationTokenInit tokenInit = (SimpleProtectedNegotiationTokenInit)spnegoToken;
  62. if (tokenInit.MechanismTypeList.Count == 0)
  63. {
  64. return NTStatus.SEC_E_INVALID_TOKEN;
  65. }
  66. // RFC 4178: Note that in order to avoid an extra round trip, the first context establishment token
  67. // of the initiator's preferred mechanism SHOULD be embedded in the initial negotiation message.
  68. byte[] preferredMechanism = tokenInit.MechanismTypeList[0];
  69. IGSSMechanism mechanism = FindMechanism(preferredMechanism);
  70. bool isPreferredMechanism = (mechanism != null);
  71. if (!isPreferredMechanism)
  72. {
  73. mechanism = FindMechanism(tokenInit.MechanismTypeList);
  74. }
  75. if (mechanism != null)
  76. {
  77. NTStatus status;
  78. context = new GSSContext(mechanism, null);
  79. if (isPreferredMechanism)
  80. {
  81. byte[] mechanismOutput;
  82. status = mechanism.AcceptSecurityContext(ref context.MechanismContext, tokenInit.MechanismToken, out mechanismOutput);
  83. outputToken = GetSPNEGOTokenResponseBytes(mechanismOutput, status, mechanism.Identifier);
  84. }
  85. else
  86. {
  87. status = NTStatus.SEC_I_CONTINUE_NEEDED;
  88. outputToken = GetSPNEGOTokenResponseBytes(null, status, mechanism.Identifier);
  89. }
  90. return status;
  91. }
  92. return NTStatus.SEC_E_SECPKG_NOT_FOUND;
  93. }
  94. else // SimpleProtectedNegotiationTokenResponse
  95. {
  96. if (context == null)
  97. {
  98. return NTStatus.SEC_E_INVALID_TOKEN;
  99. }
  100. IGSSMechanism mechanism = context.Mechanism;
  101. SimpleProtectedNegotiationTokenResponse tokenResponse = (SimpleProtectedNegotiationTokenResponse)spnegoToken;
  102. byte[] mechanismOutput;
  103. NTStatus status = mechanism.AcceptSecurityContext(ref context.MechanismContext, tokenResponse.ResponseToken, out mechanismOutput);
  104. outputToken = GetSPNEGOTokenResponseBytes(mechanismOutput, status, null);
  105. return status;
  106. }
  107. }
  108. else
  109. {
  110. // [MS-SMB] The Windows GSS implementation supports raw Kerberos / NTLM messages in the SecurityBlob.
  111. // [MS-SMB2] Windows [..] will also accept raw Kerberos messages and implicit NTLM messages as part of GSS authentication.
  112. if (AuthenticationMessageUtils.IsSignatureValid(inputToken))
  113. {
  114. MessageTypeName messageType = AuthenticationMessageUtils.GetMessageType(inputToken);
  115. IGSSMechanism ntlmAuthenticationProvider = FindMechanism(NTLMSSPIdentifier);
  116. if (ntlmAuthenticationProvider != null)
  117. {
  118. if (messageType == MessageTypeName.Negotiate)
  119. {
  120. context = new GSSContext(ntlmAuthenticationProvider, null);
  121. }
  122. if (context == null)
  123. {
  124. return NTStatus.SEC_E_INVALID_TOKEN;
  125. }
  126. NTStatus status = ntlmAuthenticationProvider.AcceptSecurityContext(ref context.MechanismContext, inputToken, out outputToken);
  127. return status;
  128. }
  129. else
  130. {
  131. return NTStatus.SEC_E_SECPKG_NOT_FOUND;
  132. }
  133. }
  134. }
  135. return NTStatus.SEC_E_INVALID_TOKEN;
  136. }
  137. public virtual object GetContextAttribute(GSSContext context, GSSAttributeName attributeName)
  138. {
  139. if (context == null)
  140. {
  141. return null;
  142. }
  143. IGSSMechanism mechanism = context.Mechanism;
  144. return mechanism.GetContextAttribute(context.MechanismContext, attributeName);
  145. }
  146. public virtual bool DeleteSecurityContext(ref GSSContext context)
  147. {
  148. if (context != null)
  149. {
  150. IGSSMechanism mechanism = context.Mechanism;
  151. return mechanism.DeleteSecurityContext(ref context.MechanismContext);
  152. }
  153. return false;
  154. }
  155. /// <summary>
  156. /// Helper method for legacy implementation.
  157. /// </summary>
  158. public virtual NTStatus GetNTLMChallengeMessage(out GSSContext context, NegotiateMessage negotiateMessage, out ChallengeMessage challengeMessage)
  159. {
  160. IGSSMechanism ntlmAuthenticationProvider = FindMechanism(NTLMSSPIdentifier);
  161. if (ntlmAuthenticationProvider != null)
  162. {
  163. context = new GSSContext(ntlmAuthenticationProvider, null);
  164. byte[] outputToken;
  165. NTStatus result = ntlmAuthenticationProvider.AcceptSecurityContext(ref context.MechanismContext, negotiateMessage.GetBytes(), out outputToken);
  166. challengeMessage = new ChallengeMessage(outputToken);
  167. return result;
  168. }
  169. else
  170. {
  171. context = null;
  172. challengeMessage = null;
  173. return NTStatus.SEC_E_SECPKG_NOT_FOUND;
  174. }
  175. }
  176. /// <summary>
  177. /// Helper method for legacy implementation.
  178. /// </summary>
  179. public virtual NTStatus NTLMAuthenticate(GSSContext context, AuthenticateMessage authenticateMessage)
  180. {
  181. if (context != null && ByteUtils.AreByteArraysEqual(context.Mechanism.Identifier, NTLMSSPIdentifier))
  182. {
  183. IGSSMechanism mechanism = context.Mechanism;
  184. byte[] outputToken;
  185. NTStatus result = mechanism.AcceptSecurityContext(ref context.MechanismContext, authenticateMessage.GetBytes(), out outputToken);
  186. return result;
  187. }
  188. else
  189. {
  190. return NTStatus.SEC_E_SECPKG_NOT_FOUND;
  191. }
  192. }
  193. public IGSSMechanism FindMechanism(List<byte[]> mechanismIdentifiers)
  194. {
  195. foreach (byte[] identifier in mechanismIdentifiers)
  196. {
  197. IGSSMechanism mechanism = FindMechanism(identifier);
  198. if (mechanism != null)
  199. {
  200. return mechanism;
  201. }
  202. }
  203. return null;
  204. }
  205. public IGSSMechanism FindMechanism(byte[] mechanismIdentifier)
  206. {
  207. foreach (IGSSMechanism mechanism in m_mechanisms)
  208. {
  209. if (ByteUtils.AreByteArraysEqual(mechanism.Identifier, mechanismIdentifier))
  210. {
  211. return mechanism;
  212. }
  213. }
  214. return null;
  215. }
  216. private static byte[] GetSPNEGOTokenResponseBytes(byte[] mechanismOutput, NTStatus status, byte[] mechanismIdentifier)
  217. {
  218. SimpleProtectedNegotiationTokenResponse tokenResponse = new SimpleProtectedNegotiationTokenResponse();
  219. if (status == NTStatus.STATUS_SUCCESS)
  220. {
  221. tokenResponse.NegState = NegState.AcceptCompleted;
  222. }
  223. else if (status == NTStatus.SEC_I_CONTINUE_NEEDED)
  224. {
  225. tokenResponse.NegState = NegState.AcceptIncomplete;
  226. }
  227. else
  228. {
  229. tokenResponse.NegState = NegState.Reject;
  230. }
  231. tokenResponse.SupportedMechanism = mechanismIdentifier;
  232. tokenResponse.ResponseToken = mechanismOutput;
  233. return tokenResponse.GetBytes();
  234. }
  235. }
  236. }