NTLMAuthenticationProviderBase.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 negotiateMessage;
  26. try
  27. {
  28. negotiateMessage = new NegotiateMessage(inputToken);
  29. }
  30. catch
  31. {
  32. return NTStatus.SEC_E_INVALID_TOKEN;
  33. }
  34. ChallengeMessage challengeMessage;
  35. NTStatus status = GetChallengeMessage(out context, negotiateMessage, out challengeMessage);
  36. outputToken = challengeMessage.GetBytes();
  37. return status;
  38. }
  39. else if (messageType == MessageTypeName.Authenticate)
  40. {
  41. AuthenticateMessage authenticateMessage;
  42. try
  43. {
  44. authenticateMessage = new AuthenticateMessage(inputToken);
  45. }
  46. catch
  47. {
  48. return NTStatus.SEC_E_INVALID_TOKEN;
  49. }
  50. return Authenticate(context, authenticateMessage);
  51. }
  52. else
  53. {
  54. return NTStatus.SEC_E_INVALID_TOKEN;
  55. }
  56. }
  57. public abstract NTStatus GetChallengeMessage(out object context, NegotiateMessage negotiateMessage, out ChallengeMessage challengeMessage);
  58. public abstract NTStatus Authenticate(object context, AuthenticateMessage authenticateMessage);
  59. public abstract bool DeleteSecurityContext(ref object context);
  60. public abstract object GetContextAttribute(object context, GSSAttributeName attributeName);
  61. public byte[] Identifier
  62. {
  63. get
  64. {
  65. return NTLMSSPIdentifier;
  66. }
  67. }
  68. }
  69. }