NtlmMessage.cs 5.0 KB

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