2 // Mono.Security.Protocol.Ntlm.MessageBase
3 // abstract class for all NTLM messages
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // a. NTLM Authentication Scheme for HTTP, Ronald Tschalär
13 // http://www.innovation.ch/java/ntlm.html
14 // b. The NTLM Authentication Protocol, Copyright © 2003 Eric Glass
15 // http://davenport.sourceforge.net/ntlm.html
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.Globalization;
40 namespace Mono.Security.Protocol.Ntlm {
47 abstract class MessageBase {
49 static private byte[] header = { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00 };
52 private NtlmFlags _flags;
54 protected MessageBase (int messageType)
59 public NtlmFlags Flags {
60 get { return _flags; }
61 set { _flags = value; }
68 protected byte[] PrepareMessage (int messageSize)
70 byte[] message = new byte [messageSize];
71 Buffer.BlockCopy (header, 0, message, 0, 8);
73 message [ 8] = (byte) _type;
74 message [ 9] = (byte)(_type >> 8);
75 message [10] = (byte)(_type >> 16);
76 message [11] = (byte)(_type >> 24);
81 protected virtual void Decode (byte[] message)
84 throw new ArgumentNullException ("message");
86 if (message.Length < 12) {
87 string msg = Locale.GetText ("Minimum message length is 12 bytes.");
88 throw new ArgumentOutOfRangeException ("message", message.Length, msg);
91 if (!CheckHeader (message)) {
92 string msg = String.Format (Locale.GetText ("Invalid Type{0} message."), _type);
93 throw new ArgumentException (msg, "message");
98 protected bool CheckHeader (byte[] message)
100 for (int i=0; i < header.Length; i++) {
101 if (message [i] != header [i])
104 return (BitConverterLE.ToUInt32 (message, 8) == _type);
107 public abstract byte[] GetBytes ();