2 // Mono.Security.Protocol.Ntlm.MessageBase
3 // abstract class for all NTLM messages
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Atsushi Enomoto <atsushi@ximian.com>
9 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004, 2007 Novell, Inc (http://www.novell.com)
13 // a. NTLM Authentication Scheme for HTTP, Ronald Tschalär
14 // http://www.innovation.ch/java/ntlm.html
15 // b. The NTLM Authentication Protocol, Copyright © 2003 Eric Glass
16 // http://davenport.sourceforge.net/ntlm.html
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System.Globalization;
41 namespace Mono.Security.Protocol.Ntlm {
43 public abstract class MessageBase {
44 static byte [] _current_os_version = GetOSVersion ();
46 static byte [] GetOSVersion ()
48 Version v = Environment.OSVersion.Version;
49 byte [] bytes = new byte [8];
50 bytes [0] = (byte) v.Major;
51 bytes [1] = (byte) v.Minor;
52 bytes [2] = (byte) v.Build;
53 bytes [3] = (byte) (v.Build >> 8);
58 static private byte[] header = { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00 };
61 private NtlmFlags _flags;
62 private NtlmVersion _version;
63 private byte [] _osversion = _current_os_version;
65 protected MessageBase (int messageType) : this (messageType, NtlmVersion.Version1)
68 protected MessageBase (int messageType, NtlmVersion version)
74 public NtlmFlags Flags {
75 get { return _flags; }
76 set { _flags = value; }
79 public byte [] OSVersion {
80 get { return (byte []) _osversion.Clone (); }
81 set { _osversion = (byte []) value.Clone (); }
88 public NtlmVersion Version {
89 get { return _version; }
92 protected byte[] PrepareMessage (int messageSize)
94 byte[] message = new byte [messageSize];
95 Buffer.BlockCopy (header, 0, message, 0, 8);
97 message [ 8] = (byte) _type;
98 message [ 9] = (byte)(_type >> 8);
99 message [10] = (byte)(_type >> 16);
100 message [11] = (byte)(_type >> 24);
105 protected virtual void Decode (byte[] message)
108 throw new ArgumentNullException ("message");
110 if (message.Length < 12) {
111 string msg = Locale.GetText ("Minimum message length is 12 bytes.");
112 throw new ArgumentOutOfRangeException ("message", message.Length, msg);
115 if (!CheckHeader (message)) {
116 string msg = String.Format (Locale.GetText ("Invalid Type{0} message."), _type);
117 throw new ArgumentException (msg, "message");
122 protected bool CheckHeader (byte[] message)
124 for (int i=0; i < header.Length; i++) {
125 if (message [i] != header [i])
128 return (BitConverterLE.ToUInt32 (message, 8) == _type);
131 public abstract byte[] GetBytes ();
133 internal byte [] CreateSubArray (byte [] source, int offset, int length)
135 byte [] ret = new byte [length];
136 Array.Copy (source, offset, ret, 0, length);