Merge pull request #799 from kebby/master
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Ntlm / MessageBase.cs
1 //
2 // Mono.Security.Protocol.Ntlm.MessageBase
3 //      abstract class for all NTLM messages
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // References
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
16 //
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:
24 // 
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 // 
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.
35 //
36
37 using System;
38 using System.Globalization;
39
40 namespace Mono.Security.Protocol.Ntlm {
41
42 #if INSIDE_SYSTEM
43         internal
44 #else
45         public
46 #endif
47         abstract class MessageBase {
48
49                 static private byte[] header = { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00 };
50                 
51                 private int _type;
52                 private NtlmFlags _flags;
53
54                 protected MessageBase (int messageType) 
55                 {
56                         _type = messageType;
57                 }
58                 
59                 public NtlmFlags Flags {
60                         get { return _flags; }
61                         set { _flags = value; }
62                 }
63
64                 public int Type { 
65                         get { return _type; }
66                 }
67
68                 protected byte[] PrepareMessage (int messageSize) 
69                 {
70                         byte[] message = new byte [messageSize];
71                         Buffer.BlockCopy (header, 0, message, 0, 8);
72                         
73                         message [ 8] = (byte) _type;
74                         message [ 9] = (byte)(_type >> 8);
75                         message [10] = (byte)(_type >> 16);
76                         message [11] = (byte)(_type >> 24);
77
78                         return message;
79                 }
80
81                 protected virtual void Decode (byte[] message) 
82                 {
83                         if (message == null)
84                                 throw new ArgumentNullException ("message");
85
86                         if (message.Length < 12) {
87                                 string msg = Locale.GetText ("Minimum message length is 12 bytes.");
88                                 throw new ArgumentOutOfRangeException ("message", message.Length, msg);
89                         }
90
91                         if (!CheckHeader (message)) {
92                                 string msg = String.Format (Locale.GetText ("Invalid Type{0} message."), _type);
93                                 throw new ArgumentException (msg, "message");
94                         }
95                 }
96
97
98                 protected bool CheckHeader (byte[] message) 
99                 {
100                         for (int i=0; i < header.Length; i++) {
101                                 if (message [i] != header [i])
102                                         return false;
103                         }
104                         return (BitConverterLE.ToUInt32 (message, 8) == _type);
105                 }
106
107                 public abstract byte[] GetBytes ();
108         }
109 }