NTLMAuthenticationProviderBase.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Copyright (C) 2014-2017 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.GSSAPI;
  10. namespace SMBLibrary.Authentication.NTLM
  11. {
  12. public abstract class NTLMAuthenticationProviderBase : IGSSMechanism
  13. {
  14. public static readonly byte[] NTLMSSPIdentifier = new byte[] { 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x02, 0x0a };
  15. public NTStatus AcceptSecurityContext(ref object context, byte[] inputToken, out byte[] outputToken)
  16. {
  17. outputToken = null;
  18. if (!AuthenticationMessageUtils.IsSignatureValid(inputToken))
  19. {
  20. return NTStatus.SEC_E_INVALID_TOKEN;
  21. }
  22. MessageTypeName messageType = AuthenticationMessageUtils.GetMessageType(inputToken);
  23. if (messageType == MessageTypeName.Negotiate)
  24. {
  25. NegotiateMessage input = new NegotiateMessage(inputToken);
  26. ChallengeMessage output;
  27. NTStatus status = GetChallengeMessage(out context, input, out output);
  28. outputToken = output.GetBytes();
  29. return status;
  30. }
  31. else if (messageType == MessageTypeName.Authenticate)
  32. {
  33. AuthenticateMessage message = new AuthenticateMessage(inputToken);
  34. return Authenticate(context, message);
  35. }
  36. else
  37. {
  38. return NTStatus.SEC_E_INVALID_TOKEN;
  39. }
  40. }
  41. public abstract NTStatus GetChallengeMessage(out object context, NegotiateMessage negotiateMessage, out ChallengeMessage challengeMessage);
  42. public abstract NTStatus Authenticate(object context, AuthenticateMessage authenticateMessage);
  43. public abstract void DeleteSecurityContext(ref object context);
  44. public abstract object GetContextAttribute(object context, GSSAttributeName attributeName);
  45. public byte[] Identifier
  46. {
  47. get
  48. {
  49. return NTLMSSPIdentifier;
  50. }
  51. }
  52. }
  53. }