Added tests for Task.WhenAll w/ empty list
[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 (value == null)
156                                         value = "";
157                                 if (value == "")
158                                         Flags &= ~NtlmFlags.NegotiateDomainSupplied;
159                                 else
160                                         Flags |= NtlmFlags.NegotiateDomainSupplied;
161
162                                 _domain = value;
163                         }
164                 }
165
166                 public string Host {
167                         get { return _host; }
168                         set {
169                                 if (value == null)
170                                         value = "";
171                                 if (value == "")
172                                         Flags &= ~NtlmFlags.NegotiateWorkstationSupplied;
173                                 else
174                                         Flags |= NtlmFlags.NegotiateWorkstationSupplied;
175
176                                 _host = value;
177                         }
178                 }
179
180                 public string Password {
181                         get { return _password; }
182                         set { _password = value; }
183                 }
184
185                 public string Username {
186                         get { return _username; }
187                         set { _username = value; }
188                 }
189
190                 public byte[] LM {
191                         get { return _lm; }
192                 }
193
194                 public byte[] NT {
195                         get { return _nt; }
196                         set { _nt = value; }
197                 }
198
199                 // methods
200
201                 protected override void Decode (byte[] message)
202                 {
203                         base.Decode (message);
204
205                         _password = null;
206
207                         if (message.Length >= 64)
208                                 Flags = (NtlmFlags)BitConverterLE.ToUInt32 (message, 60);
209                         else
210                                 Flags = (NtlmFlags)0x8201;
211                         
212                         int lm_len = BitConverterLE.ToUInt16 (message, 12);
213                         int lm_off = BitConverterLE.ToUInt16 (message, 16);
214                         _lm = new byte [lm_len];
215                         Buffer.BlockCopy (message, lm_off, _lm, 0, lm_len);
216
217                         int nt_len = BitConverterLE.ToUInt16 (message, 20);
218                         int nt_off = BitConverterLE.ToUInt16 (message, 24);
219                         _nt = new byte [nt_len];
220                         Buffer.BlockCopy (message, nt_off, _nt, 0, nt_len);
221                         
222                         int dom_len = BitConverterLE.ToUInt16 (message, 28);
223                         int dom_off = BitConverterLE.ToUInt16 (message, 32);
224                         _domain = DecodeString (message, dom_off, dom_len);
225
226                         int user_len = BitConverterLE.ToUInt16 (message, 36);
227                         int user_off = BitConverterLE.ToUInt16 (message, 40);
228                         _username = DecodeString (message, user_off, user_len);
229                         
230                         int host_len = BitConverterLE.ToUInt16 (message, 44);
231                         int host_off = BitConverterLE.ToUInt16 (message, 48);
232                         _host = DecodeString (message, host_off, host_len);
233                         
234                         // Session key.  We don't use it yet.
235                         // int skey_len = BitConverterLE.ToUInt16 (message, 52);
236                         // int skey_off = BitConverterLE.ToUInt16 (message, 56);
237                 }
238
239                 string DecodeString (byte[] buffer, int offset, int len)
240                 {
241                         if ((Flags & NtlmFlags.NegotiateUnicode) != 0)
242                                 return Encoding.Unicode.GetString (buffer, offset, len);
243                         else
244                                 return Encoding.ASCII.GetString (buffer, offset, len);
245                 }
246
247                 byte[] EncodeString (string text)
248                 {
249                         if (text == null)
250                                 return new byte [0];
251                         if ((Flags & NtlmFlags.NegotiateUnicode) != 0)
252                                 return Encoding.Unicode.GetBytes (text);
253                         else
254                                 return Encoding.ASCII.GetBytes (text);
255                 }
256
257                 public override byte[] GetBytes ()
258                 {
259                         byte[] target = EncodeString (_domain);
260                         byte[] user = EncodeString (_username);
261                         byte[] host = EncodeString (_host);
262
263                         byte[] lm, ntlm;
264                         if (_type2 == null) {
265                                 if (_level != NtlmAuthLevel.LM_and_NTLM)
266                                         throw new InvalidOperationException (
267                                                 "Refusing to use legacy-mode LM/NTLM authentication " +
268                                                         "unless explicitly enabled using DefaultAuthLevel.");
269                                 
270                                 using (var legacy = new ChallengeResponse (_password, _challenge)) {
271                                         lm = legacy.LM;
272                                         ntlm = legacy.NT;
273                                 }
274                         } else {
275                                 ChallengeResponse2.Compute (_type2, _level, _username, _password, _domain, out lm, out ntlm);
276                         }
277
278                         var lmresp_len = lm != null ? lm.Length : 0;
279                         var ntresp_len = ntlm != null ? ntlm.Length : 0;
280
281                         byte[] data = PrepareMessage (64 + target.Length + user.Length + host.Length + lmresp_len + ntresp_len);
282
283                         // LM response
284                         short lmresp_off = (short)(64 + target.Length + user.Length + host.Length);
285                         data [12] = (byte)lmresp_len;
286                         data [13] = (byte)0x00;
287                         data [14] = (byte)lmresp_len;
288                         data [15] = (byte)0x00;
289                         data [16] = (byte)lmresp_off;
290                         data [17] = (byte)(lmresp_off >> 8);
291
292                         // NT response
293                         short ntresp_off = (short)(lmresp_off + lmresp_len);
294                         data [20] = (byte)ntresp_len;
295                         data [21] = (byte)(ntresp_len >> 8);
296                         data [22] = (byte)ntresp_len;
297                         data [23] = (byte)(ntresp_len >> 8);
298                         data [24] = (byte)ntresp_off;
299                         data [25] = (byte)(ntresp_off >> 8);
300
301                         // target
302                         short dom_len = (short)target.Length;
303                         short dom_off = 64;
304                         data [28] = (byte)dom_len;
305                         data [29] = (byte)(dom_len >> 8);
306                         data [30] = data [28];
307                         data [31] = data [29];
308                         data [32] = (byte)dom_off;
309                         data [33] = (byte)(dom_off >> 8);
310
311                         // username
312                         short uname_len = (short)user.Length;
313                         short uname_off = (short)(dom_off + dom_len);
314                         data [36] = (byte)uname_len;
315                         data [37] = (byte)(uname_len >> 8);
316                         data [38] = data [36];
317                         data [39] = data [37];
318                         data [40] = (byte)uname_off;
319                         data [41] = (byte)(uname_off >> 8);
320
321                         // host
322                         short host_len = (short)host.Length;
323                         short host_off = (short)(uname_off + uname_len);
324                         data [44] = (byte)host_len;
325                         data [45] = (byte)(host_len >> 8);
326                         data [46] = data [44];
327                         data [47] = data [45];
328                         data [48] = (byte)host_off;
329                         data [49] = (byte)(host_off >> 8);
330
331                         // message length
332                         short msg_len = (short)data.Length;
333                         data [56] = (byte)msg_len;
334                         data [57] = (byte)(msg_len >> 8);
335
336                         int flags = (int)Flags;
337
338                         // options flags
339                         data [60] = (byte)flags;
340                         data [61] = (byte)((uint)flags >> 8);
341                         data [62] = (byte)((uint)flags >> 16);
342                         data [63] = (byte)((uint)flags >> 24);
343
344                         Buffer.BlockCopy (target, 0, data, dom_off, target.Length);
345                         Buffer.BlockCopy (user, 0, data, uname_off, user.Length);
346                         Buffer.BlockCopy (host, 0, data, host_off, host.Length);
347
348                         if (lm != null) {
349                                 Buffer.BlockCopy (lm, 0, data, lmresp_off, lm.Length);
350                                 Array.Clear (lm, 0, lm.Length);
351                         }
352                         Buffer.BlockCopy (ntlm, 0, data, ntresp_off, ntlm.Length);
353                         Array.Clear (ntlm, 0, ntlm.Length);
354
355                         return data;
356                 }
357         }
358 }