2010-06-21 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / System.ServiceModel / 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 //      Atsushi Enomoto <atsushi@ximian.com>
8 //
9 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004, 2007 Novell, Inc (http://www.novell.com)
11 //
12 // References
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
17 //
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:
25 // 
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
28 // 
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.
36 //
37
38 using System;
39 using System.Globalization;
40
41 namespace Mono.Security.Protocol.Ntlm {
42
43         public abstract class MessageBase {
44                 static byte [] _current_os_version = GetOSVersion ();
45
46                 static byte [] GetOSVersion ()
47                 {
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);
54                         bytes [7] = 0xF;
55                         return bytes;
56                 }
57
58                 static private byte[] header = { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00 };
59                 
60                 private int _type;
61                 private NtlmFlags _flags;
62                 private NtlmVersion _version;
63                 private byte [] _osversion = _current_os_version;
64
65                 protected MessageBase (int messageType) : this (messageType, NtlmVersion.Version1)
66                 {
67                 }
68                 protected MessageBase (int messageType, NtlmVersion version) 
69                 {
70                         _type = messageType;
71                         _version = version;
72                 }
73                 
74                 public NtlmFlags Flags {
75                         get { return _flags; }
76                         set { _flags = value; }
77                 }
78
79                 public byte [] OSVersion {
80                         get { return (byte []) _osversion.Clone (); }
81                         set { _osversion = (byte []) value.Clone (); }
82                 }
83
84                 public int Type { 
85                         get { return _type; }
86                 }
87
88                 public NtlmVersion Version {
89                         get { return _version; }
90                 }
91
92                 protected byte[] PrepareMessage (int messageSize) 
93                 {
94                         byte[] message = new byte [messageSize];
95                         Buffer.BlockCopy (header, 0, message, 0, 8);
96                         
97                         message [ 8] = (byte) _type;
98                         message [ 9] = (byte)(_type >> 8);
99                         message [10] = (byte)(_type >> 16);
100                         message [11] = (byte)(_type >> 24);
101
102                         return message;
103                 }
104
105                 protected virtual void Decode (byte[] message) 
106                 {
107                         if (message == null)
108                                 throw new ArgumentNullException ("message");
109
110                         if (message.Length < 12) {
111                                 string msg = Locale.GetText ("Minimum message length is 12 bytes.");
112                                 throw new ArgumentOutOfRangeException ("message", message.Length, msg);
113                         }
114
115                         if (!CheckHeader (message)) {
116                                 string msg = String.Format (Locale.GetText ("Invalid Type{0} message."), _type);
117                                 throw new ArgumentException (msg, "message");
118                         }
119                 }
120
121
122                 protected bool CheckHeader (byte[] message) 
123                 {
124                         for (int i=0; i < header.Length; i++) {
125                                 if (message [i] != header [i])
126                                         return false;
127                         }
128                         return (BitConverterLE.ToUInt32 (message, 8) == _type);
129                 }
130
131                 public abstract byte[] GetBytes ();
132
133                 internal byte [] CreateSubArray (byte [] source, int offset, int length)
134                 {
135                         byte [] ret = new byte [length];
136                         Array.Copy (source, offset, ret, 0, length);
137                         return ret;
138                 }
139         }
140 }