// This code is derived from jcifs smb client library // Ported by J. Arturo // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA using System; namespace SharpCifs.Ntlmssp { /// Abstract superclass for all NTLMSSP messages. /// Abstract superclass for all NTLMSSP messages. public abstract class NtlmMessage : NtlmFlags { /// The NTLMSSP "preamble". /// The NTLMSSP "preamble". protected internal static readonly byte[] NtlmsspSignature = { unchecked( (byte)('N')), unchecked((byte)('T')), unchecked((byte)('L')), unchecked((byte)('M')), unchecked((byte)('S')), unchecked((byte )('S')), unchecked((byte)('P')), unchecked(0) }; private static readonly string OemEncoding = Config.DefaultOemEncoding; protected internal static readonly string UniEncoding = "UTF-16LE"; private int _flags; /// Returns the flags currently in use for this message. /// Returns the flags currently in use for this message. /// /// An int containing the flags in use for this /// message. /// public virtual int GetFlags() { return _flags; } /// Sets the flags for this message. /// Sets the flags for this message. /// The flags for this message. public virtual void SetFlags(int flags) { this._flags = flags; } /// Returns the status of the specified flag. /// Returns the status of the specified flag. /// The flag to test (i.e., NTLMSSP_NEGOTIATE_OEM). /// A boolean indicating whether the flag is set. public virtual bool GetFlag(int flag) { return (GetFlags() & flag) != 0; } /// Sets or clears the specified flag. /// Sets or clears the specified flag. /// /// The flag to set/clear (i.e., /// NTLMSSP_NEGOTIATE_OEM). /// /// /// Indicates whether to set (true) or /// clear (false) the specified flag. /// public virtual void SetFlag(int flag, bool value) { SetFlags(value ? (GetFlags() | flag) : (GetFlags() & (unchecked((int)(0xffffffff) ) ^ flag))); } internal static int ReadULong(byte[] src, int index) { return (src[index] & unchecked(0xff)) | ((src[index + 1] & unchecked(0xff)) << 8) | ((src[index + 2] & unchecked(0xff)) << 16) | ((src[index + 3] & unchecked(0xff)) << 24); } internal static int ReadUShort(byte[] src, int index) { return (src[index] & unchecked(0xff)) | ((src[index + 1] & unchecked(0xff)) << 8); } internal static byte[] ReadSecurityBuffer(byte[] src, int index) { int length = ReadUShort(src, index); int offset = ReadULong(src, index + 4); byte[] buffer = new byte[length]; Array.Copy(src, offset, buffer, 0, length); return buffer; } internal static void WriteULong(byte[] dest, int offset, int value) { dest[offset] = unchecked((byte)(value & unchecked(0xff))); dest[offset + 1] = unchecked((byte)(value >> 8 & unchecked(0xff))); dest[offset + 2] = unchecked((byte)(value >> 16 & unchecked(0xff))); dest[offset + 3] = unchecked((byte)(value >> 24 & unchecked(0xff))); } internal static void WriteUShort(byte[] dest, int offset, int value) { dest[offset] = unchecked((byte)(value & unchecked(0xff))); dest[offset + 1] = unchecked((byte)(value >> 8 & unchecked(0xff))); } internal static void WriteSecurityBuffer(byte[] dest, int offset, int bodyOffset, byte[] src) { int length = (src != null) ? src.Length : 0; if (length == 0) { return; } WriteUShort(dest, offset, length); WriteUShort(dest, offset + 2, length); WriteULong(dest, offset + 4, bodyOffset); Array.Copy(src, 0, dest, bodyOffset, length); } internal static string GetOemEncoding() { return OemEncoding; } /// Returns the raw byte representation of this message. /// Returns the raw byte representation of this message. /// A byte[] containing the raw message material. public abstract byte[] ToByteArray(); } }