Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Ntlm / Type3Message.cs
1 //
2 // Mono.Security.Protocol.Ntlm.Type3Message - Authentication
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // References
11 // a.   NTLM Authentication Scheme for HTTP, Ronald Tschalär
12 //      http://www.innovation.ch/java/ntlm.html
13 // b.   The NTLM Authentication Protocol, Copyright © 2003 Eric Glass
14 //      http://davenport.sourceforge.net/ntlm.html
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using System.Globalization;
38 using System.Text;
39
40 namespace Mono.Security.Protocol.Ntlm {
41
42         public class Type3Message : MessageBase {
43
44                 private NtlmAuthLevel _level;
45                 private byte[] _challenge;
46                 private string _host;
47                 private string _domain;
48                 private string _username;
49                 private string _password;
50                 private Type2Message _type2;
51                 private byte[] _lm;
52                 private byte[] _nt;
53                 
54                 internal const string LegacyAPIWarning = 
55                         "Use of this API is highly discouraged, " +
56                         "it selects legacy-mode LM/NTLM authentication, which sends " +
57                         "your password in very weak encryption over the wire even if " +
58                         "the server supports the more secure NTLMv2 / NTLMv2 Session. " +
59                         "You need to use the new `Type3Message (Type2Message)' constructor " +
60                         "to use the more secure NTLMv2 / NTLMv2 Session authentication modes. " +
61                         "These require the Type 2 message from the server to compute the response.";
62                 
63                 [Obsolete (LegacyAPIWarning)]
64                 public Type3Message () : base (3)
65                 {
66                         if (DefaultAuthLevel != NtlmAuthLevel.LM_and_NTLM)
67                                 throw new InvalidOperationException (
68                                         "Refusing to use legacy-mode LM/NTLM authentication " +
69                                         "unless explicitly enabled using DefaultAuthLevel.");
70
71                         // default values
72                         _domain = Environment.UserDomainName;
73                         _host = Environment.MachineName;
74                         _username = Environment.UserName;
75                         _level = NtlmAuthLevel.LM_and_NTLM;
76                         Flags = (NtlmFlags) 0x8201;
77                 }
78
79                 public Type3Message (byte[] message) : base (3)
80                 {
81                         Decode (message);
82                 }
83
84                 public Type3Message (Type2Message type2) : base (3)
85                 {
86                         _type2 = type2;
87                         _level = DefaultAuthLevel;
88                         _challenge = (byte[]) type2.Nonce.Clone ();
89
90                         _domain = type2.TargetName;
91                         _host = Environment.MachineName;
92                         _username = Environment.UserName;
93
94                         Flags = (NtlmFlags) 0x8200;
95                         if ((type2.Flags & NtlmFlags.NegotiateUnicode) != 0)
96                                 Flags |= NtlmFlags.NegotiateUnicode;
97                         else
98                                 Flags |= NtlmFlags.NegotiateOem;
99
100                         if ((type2.Flags & NtlmFlags.NegotiateNtlm2Key) != 0)
101                                 Flags |= NtlmFlags.NegotiateNtlm2Key;
102                 }
103
104                 ~Type3Message () 
105                 {
106                         if (_challenge != null)
107                                 Array.Clear (_challenge, 0, _challenge.Length);
108                         if (_lm != null)
109                                 Array.Clear (_lm, 0, _lm.Length);
110                         if (_nt != null)
111                                 Array.Clear (_nt, 0, _nt.Length);
112                 }
113
114                 // Default auth level
115
116                 static NtlmAuthLevel _default = NtlmAuthLevel.LM_and_NTLM_and_try_NTLMv2_Session;
117
118                 public static NtlmAuthLevel DefaultAuthLevel {
119                         get { return _default; }
120                         set { _default = value; }
121                 }
122
123                 public NtlmAuthLevel Level {
124                         get { return _level; }
125                         set { _level = value; }
126                 }
127                 
128                 // properties
129
130                 [Obsolete (LegacyAPIWarning)]
131                 public byte[] Challenge {
132                         get { 
133                                 if (_challenge == null)
134                                         return null;
135                                 return (byte[]) _challenge.Clone (); }
136                         set { 
137                                 if ((_type2 != null) || (_level != NtlmAuthLevel.LM_and_NTLM))
138                                         throw new InvalidOperationException (
139                                                 "Refusing to use legacy-mode LM/NTLM authentication " +
140                                                         "unless explicitly enabled using DefaultAuthLevel.");
141                                 
142                                 if (value == null)
143                                         throw new ArgumentNullException ("Challenge");
144                                 if (value.Length != 8) {
145                                         string msg = Locale.GetText ("Invalid Challenge Length (should be 8 bytes).");
146                                         throw new ArgumentException (msg, "Challenge");
147                                 }
148                                 _challenge = (byte[]) value.Clone (); 
149                         }
150                 }
151
152                 public string Domain {
153                         get { return _domain; }
154                         set {
155                                 if (_type2 != null)
156                                         throw new InvalidOperationException (
157                                                 "Domain is set automatically from Type2Message.TargetName");
158                                 if (value == null)
159                                         value = "";
160                                 if (value == "")
161                                         Flags &= ~NtlmFlags.NegotiateDomainSupplied;
162                                 else
163                                         Flags |= NtlmFlags.NegotiateDomainSupplied;
164
165                                 _domain = value;
166                         }
167                 }
168
169                 public string Host {
170                         get { return _host; }
171                         set {
172                                 if (value == null)
173                                         value = "";
174                                 if (value == "")
175                                         Flags &= ~NtlmFlags.NegotiateWorkstationSupplied;
176                                 else
177                                         Flags |= NtlmFlags.NegotiateWorkstationSupplied;
178
179                                 _host = value;
180                         }
181                 }
182
183                 public string Password {
184                         get { return _password; }
185                         set { _password = value; }
186                 }
187
188                 public string Username {
189                         get { return _username; }
190                         set { _username = value; }
191                 }
192
193                 public byte[] LM {
194                         get { return _lm; }
195                 }
196
197                 public byte[] NT {
198                         get { return _nt; }
199                         set { _nt = value; }
200                 }
201
202                 // methods
203
204                 protected override void Decode (byte[] message)
205                 {
206                         base.Decode (message);
207
208                         if (BitConverterLE.ToUInt16 (message, 56) != message.Length) {
209                                 string msg = Locale.GetText ("Invalid Type3 message length.");
210                                 throw new ArgumentException (msg, "message");
211                         }
212
213                         _password = null;
214
215                         if (message.Length >= 64)
216                                 Flags = (NtlmFlags)BitConverterLE.ToUInt32 (message, 60);
217                         else
218                                 Flags = (NtlmFlags)0x8201;
219                         
220                         int lm_len = BitConverterLE.ToUInt16 (message, 12);
221                         int lm_off = BitConverterLE.ToUInt16 (message, 16);
222                         _lm = new byte [lm_len];
223                         Buffer.BlockCopy (message, lm_off, _lm, 0, lm_len);
224
225                         int nt_len = BitConverterLE.ToUInt16 (message, 20);
226                         int nt_off = BitConverterLE.ToUInt16 (message, 24);
227                         _nt = new byte [nt_len];
228                         Buffer.BlockCopy (message, nt_off, _nt, 0, nt_len);
229                         
230                         int dom_len = BitConverterLE.ToUInt16 (message, 28);
231                         int dom_off = BitConverterLE.ToUInt16 (message, 32);
232                         _domain = DecodeString (message, dom_off, dom_len);
233
234                         int user_len = BitConverterLE.ToUInt16 (message, 36);
235                         int user_off = BitConverterLE.ToUInt16 (message, 40);
236                         _username = DecodeString (message, user_off, user_len);
237                         
238                         int host_len = BitConverterLE.ToUInt16 (message, 44);
239                         int host_off = BitConverterLE.ToUInt16 (message, 48);
240                         _host = DecodeString (message, host_off, host_len);
241                         
242                         // Session key.  We don't use it yet.
243                         // int skey_len = BitConverterLE.ToUInt16 (message, 52);
244                         // int skey_off = BitConverterLE.ToUInt16 (message, 56);
245                 }
246
247                 string DecodeString (byte[] buffer, int offset, int len)
248                 {
249                         if ((Flags & NtlmFlags.NegotiateUnicode) != 0)
250                                 return Encoding.Unicode.GetString (buffer, offset, len);
251                         else
252                                 return Encoding.ASCII.GetString (buffer, offset, len);
253                 }
254
255                 byte[] EncodeString (string text)
256                 {
257                         if (text == null)
258                                 return new byte [0];
259                         if ((Flags & NtlmFlags.NegotiateUnicode) != 0)
260                                 return Encoding.Unicode.GetBytes (text);
261                         else
262                                 return Encoding.ASCII.GetBytes (text);
263                 }
264
265                 public override byte[] GetBytes ()
266                 {
267                         byte[] target = EncodeString (_domain);
268                         byte[] user = EncodeString (_username);
269                         byte[] host = EncodeString (_host);
270
271                         byte[] lm, ntlm;
272                         if (_type2 == null) {
273                                 if (_level != NtlmAuthLevel.LM_and_NTLM)
274                                         throw new InvalidOperationException (
275                                                 "Refusing to use legacy-mode LM/NTLM authentication " +
276                                                         "unless explicitly enabled using DefaultAuthLevel.");
277                                 
278                                 using (var legacy = new ChallengeResponse (_password, _challenge)) {
279                                         lm = legacy.LM;
280                                         ntlm = legacy.NT;
281                                 }
282                         } else {
283                                 ChallengeResponse2.Compute (_type2, _level, _username, _password, out lm, out ntlm);
284                         }
285
286                         var lmresp_len = lm != null ? lm.Length : 0;
287                         var ntresp_len = ntlm != null ? ntlm.Length : 0;
288
289                         byte[] data = PrepareMessage (64 + target.Length + user.Length + host.Length + lmresp_len + ntresp_len);
290
291                         // LM response
292                         short lmresp_off = (short)(64 + target.Length + user.Length + host.Length);
293                         data [12] = (byte)lmresp_len;
294                         data [13] = (byte)0x00;
295                         data [14] = (byte)lmresp_len;
296                         data [15] = (byte)0x00;
297                         data [16] = (byte)lmresp_off;
298                         data [17] = (byte)(lmresp_off >> 8);
299
300                         // NT response
301                         short ntresp_off = (short)(lmresp_off + lmresp_len);
302                         data [20] = (byte)ntresp_len;
303                         data [21] = (byte)(ntresp_len >> 8);
304                         data [22] = (byte)ntresp_len;
305                         data [23] = (byte)(ntresp_len >> 8);
306                         data [24] = (byte)ntresp_off;
307                         data [25] = (byte)(ntresp_off >> 8);
308
309                         // target
310                         short dom_len = (short)target.Length;
311                         short dom_off = 64;
312                         data [28] = (byte)dom_len;
313                         data [29] = (byte)(dom_len >> 8);
314                         data [30] = data [28];
315                         data [31] = data [29];
316                         data [32] = (byte)dom_off;
317                         data [33] = (byte)(dom_off >> 8);
318
319                         // username
320                         short uname_len = (short)user.Length;
321                         short uname_off = (short)(dom_off + dom_len);
322                         data [36] = (byte)uname_len;
323                         data [37] = (byte)(uname_len >> 8);
324                         data [38] = data [36];
325                         data [39] = data [37];
326                         data [40] = (byte)uname_off;
327                         data [41] = (byte)(uname_off >> 8);
328
329                         // host
330                         short host_len = (short)host.Length;
331                         short host_off = (short)(uname_off + uname_len);
332                         data [44] = (byte)host_len;
333                         data [45] = (byte)(host_len >> 8);
334                         data [46] = data [44];
335                         data [47] = data [45];
336                         data [48] = (byte)host_off;
337                         data [49] = (byte)(host_off >> 8);
338
339                         // message length
340                         short msg_len = (short)data.Length;
341                         data [56] = (byte)msg_len;
342                         data [57] = (byte)(msg_len >> 8);
343
344                         int flags = (int)Flags;
345
346                         // options flags
347                         data [60] = (byte)flags;
348                         data [61] = (byte)((uint)flags >> 8);
349                         data [62] = (byte)((uint)flags >> 16);
350                         data [63] = (byte)((uint)flags >> 24);
351
352                         Buffer.BlockCopy (target, 0, data, dom_off, target.Length);
353                         Buffer.BlockCopy (user, 0, data, uname_off, user.Length);
354                         Buffer.BlockCopy (host, 0, data, host_off, host.Length);
355
356                         if (lm != null) {
357                                 Buffer.BlockCopy (lm, 0, data, lmresp_off, lm.Length);
358                                 Array.Clear (lm, 0, lm.Length);
359                         }
360                         Buffer.BlockCopy (ntlm, 0, data, ntresp_off, ntlm.Length);
361                         Array.Clear (ntlm, 0, ntlm.Length);
362
363                         return data;
364                 }
365         }
366 }