NtlmMessage.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. namespace SharpCifs.Ntlmssp
  19. {
  20. /// <summary>Abstract superclass for all NTLMSSP messages.</summary>
  21. /// <remarks>Abstract superclass for all NTLMSSP messages.</remarks>
  22. public abstract class NtlmMessage : NtlmFlags
  23. {
  24. /// <summary>The NTLMSSP "preamble".</summary>
  25. /// <remarks>The NTLMSSP "preamble".</remarks>
  26. protected internal static readonly byte[] NtlmsspSignature =
  27. {
  28. unchecked((byte)('N')),
  29. unchecked((byte)('T')),
  30. unchecked((byte)('L')),
  31. unchecked((byte)('M')),
  32. unchecked((byte)('S')),
  33. unchecked((byte)('S')),
  34. unchecked((byte)('P')),
  35. unchecked(0)
  36. };
  37. private static readonly string OemEncoding = Config.DefaultOemEncoding;
  38. protected internal static readonly string UniEncoding = "UTF-16LE";
  39. private int _flags;
  40. /// <summary>Returns the flags currently in use for this message.</summary>
  41. /// <remarks>Returns the flags currently in use for this message.</remarks>
  42. /// <returns>
  43. /// An <code>int</code> containing the flags in use for this
  44. /// message.
  45. /// </returns>
  46. public virtual int GetFlags()
  47. {
  48. return _flags;
  49. }
  50. /// <summary>Sets the flags for this message.</summary>
  51. /// <remarks>Sets the flags for this message.</remarks>
  52. /// <param name="flags">The flags for this message.</param>
  53. public virtual void SetFlags(int flags)
  54. {
  55. this._flags = flags;
  56. }
  57. /// <summary>Returns the status of the specified flag.</summary>
  58. /// <remarks>Returns the status of the specified flag.</remarks>
  59. /// <param name="flag">The flag to test (i.e., <code>NTLMSSP_NEGOTIATE_OEM</code>).</param>
  60. /// <returns>A <code>boolean</code> indicating whether the flag is set.</returns>
  61. public virtual bool GetFlag(int flag)
  62. {
  63. return (GetFlags() & flag) != 0;
  64. }
  65. /// <summary>Sets or clears the specified flag.</summary>
  66. /// <remarks>Sets or clears the specified flag.</remarks>
  67. /// <param name="flag">
  68. /// The flag to set/clear (i.e.,
  69. /// <code>NTLMSSP_NEGOTIATE_OEM</code>).
  70. /// </param>
  71. /// <param name="value">
  72. /// Indicates whether to set (<code>true</code>) or
  73. /// clear (<code>false</code>) the specified flag.
  74. /// </param>
  75. public virtual void SetFlag(int flag, bool value)
  76. {
  77. SetFlags(value
  78. ? (GetFlags() | flag)
  79. : (GetFlags() & (unchecked((int)(0xffffffff)) ^ flag)));
  80. }
  81. internal static int ReadULong(byte[] src, int index)
  82. {
  83. return (src[index] & unchecked(0xff))
  84. | ((src[index + 1] & unchecked(0xff)) << 8)
  85. | ((src[index + 2] & unchecked(0xff)) << 16)
  86. | ((src[index + 3] & unchecked(0xff)) << 24);
  87. }
  88. internal static int ReadUShort(byte[] src, int index)
  89. {
  90. return (src[index] & unchecked(0xff)) | ((src[index + 1] & unchecked(0xff)) << 8);
  91. }
  92. internal static byte[] ReadSecurityBuffer(byte[] src, int index)
  93. {
  94. int length = ReadUShort(src, index);
  95. int offset = ReadULong(src, index + 4);
  96. byte[] buffer = new byte[length];
  97. Array.Copy(src, offset, buffer, 0, length);
  98. return buffer;
  99. }
  100. internal static void WriteULong(byte[] dest, int offset, int value)
  101. {
  102. dest[offset] = unchecked((byte)(value & unchecked(0xff)));
  103. dest[offset + 1] = unchecked((byte)(value >> 8 & unchecked(0xff)));
  104. dest[offset + 2] = unchecked((byte)(value >> 16 & unchecked(0xff)));
  105. dest[offset + 3] = unchecked((byte)(value >> 24 & unchecked(0xff)));
  106. }
  107. internal static void WriteUShort(byte[] dest, int offset, int value)
  108. {
  109. dest[offset] = unchecked((byte)(value & unchecked(0xff)));
  110. dest[offset + 1] = unchecked((byte)(value >> 8 & unchecked(0xff)));
  111. }
  112. internal static void WriteSecurityBuffer(byte[] dest,
  113. int offset,
  114. int bodyOffset,
  115. byte[] src)
  116. {
  117. int length = (src != null)
  118. ? src.Length
  119. : 0;
  120. if (length == 0)
  121. {
  122. return;
  123. }
  124. WriteUShort(dest, offset, length);
  125. WriteUShort(dest, offset + 2, length);
  126. WriteULong(dest, offset + 4, bodyOffset);
  127. Array.Copy(src, 0, dest, bodyOffset, length);
  128. }
  129. internal static string GetOemEncoding()
  130. {
  131. return OemEncoding;
  132. }
  133. /// <summary>Returns the raw byte representation of this message.</summary>
  134. /// <remarks>Returns the raw byte representation of this message.</remarks>
  135. /// <returns>A <code>byte[]</code> containing the raw message material.</returns>
  136. public abstract byte[] ToByteArray();
  137. }
  138. }