Use XPathNavigator output in WCF diagnostics. Got sorta-valid message trace.
[mono.git] / mcs / class / System.ServiceModel / Mono.Security.Protocol.Ntlm / ChallengeResponse.cs
1 //
2 // Mono.Security.Protocol.Ntlm.ChallengeResponse
3 //      Implements Challenge Response for NTLM v1
4 //
5 // Author:
6 //      Sebastien Pouliot <sebastien@ximian.com>
7 //      Atsushi Enomoto <atsushi@ximian.com>
8 //
9 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // (C) 2004, 2007 Novell (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
19 //
20 // Permission is hereby granted, free of charge, to any person obtaining
21 // a copy of this software and associated documentation files (the
22 // "Software"), to deal in the Software without restriction, including
23 // without limitation the rights to use, copy, modify, merge, publish,
24 // distribute, sublicense, and/or sell copies of the Software, and to
25 // permit persons to whom the Software is furnished to do so, subject to
26 // the following conditions:
27 // 
28 // The above copyright notice and this permission notice shall be
29 // included in all copies or substantial portions of the Software.
30 // 
31 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 //
39
40 using System;
41 using System.Globalization;
42 using System.Security.Cryptography;
43 using System.Text;
44
45 using Mono.Security.Cryptography;
46
47 namespace Mono.Security.Protocol.Ntlm {
48
49         public class ChallengeResponse : IDisposable {
50
51                 static private byte[] magic = { 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
52
53                 // This is the pre-encrypted magic value with a null DES key (0xAAD3B435B51404EE)
54                 // Ref: http://packetstormsecurity.nl/Crackers/NT/l0phtcrack/l0phtcrack2.5-readme.html
55                 static private byte[] nullEncMagic = { 0xAA, 0xD3, 0xB4, 0x35, 0xB5, 0x14, 0x04, 0xEE };
56
57                 private bool _disposed;
58                 private byte[] _challenge;
59                 private byte[] _lmpwd;
60                 private byte[] _ntpwd;
61
62                 // constructors
63
64                 public ChallengeResponse () 
65                 {
66                         _disposed = false;
67                         _lmpwd = new byte [21];
68                         _ntpwd = new byte [21];
69                 }
70                 
71                 public ChallengeResponse (string password, byte[] challenge) : this ()
72                 {
73                         Password = password;
74                         Challenge = challenge;
75                 }
76
77                 ~ChallengeResponse () 
78                 {
79                         if (!_disposed)
80                                 Dispose ();
81                 }
82
83                 // properties
84
85                 public string Password {
86                         get { return null; }
87                         set { 
88                                 if (_disposed)
89                                         throw new ObjectDisposedException ("too late");
90
91                                 // create Lan Manager password
92                                 DES des = DES.Create ();
93                                 des.Mode = CipherMode.ECB;
94                                 ICryptoTransform ct = null;
95                                 
96                                 // Note: In .NET DES cannot accept a weak key
97                                 // this can happen for a null password
98                                 if ((value == null) || (value.Length < 1)) {
99                                         Buffer.BlockCopy (nullEncMagic, 0, _lmpwd, 0, 8);
100                                 }
101                                 else {
102                                         des.Key = PasswordToKey (value, 0);
103                                         ct = des.CreateEncryptor ();
104                                         ct.TransformBlock (magic, 0, 8, _lmpwd, 0);
105                                 }
106
107                                 // and if a password has less than 8 characters
108                                 if ((value == null) || (value.Length < 8)) {
109                                         Buffer.BlockCopy (nullEncMagic, 0, _lmpwd, 8, 8);
110                                 }
111                                 else {
112                                         des.Key = PasswordToKey (value, 7);
113                                         ct = des.CreateEncryptor ();
114                                         ct.TransformBlock (magic, 0, 8, _lmpwd, 8);
115                                 }
116
117                                 // create NT password
118                                 MD4 md4 = MD4.Create ();
119                                 byte[] data = ((value == null) ? (new byte [0]) : (Encoding.Unicode.GetBytes (value)));
120                                 byte[] hash = md4.ComputeHash (data);
121                                 Buffer.BlockCopy (hash, 0, _ntpwd, 0, 16);
122
123                                 // clean up
124                                 Array.Clear (data, 0, data.Length);
125                                 Array.Clear (hash, 0, hash.Length);
126                                 des.Clear ();
127                         }
128                 }
129
130                 public byte[] Challenge {
131                         get { return null; }
132                         set {
133                                 if (value == null)
134                                         throw new ArgumentNullException ("Challenge");
135                                 if (_disposed)
136                                         throw new ObjectDisposedException ("too late");
137                                 // we don't want the caller to modify the value afterward
138                                 _challenge = (byte[]) value.Clone ();
139                         }
140                 }
141
142                 public byte[] LM {
143                         get { 
144                                 if (_disposed)
145                                         throw new ObjectDisposedException ("too late");
146
147                                 return GetResponse (_lmpwd);
148                         }
149                 }
150
151                 public byte[] NT {
152                         get { 
153                                 if (_disposed)
154                                         throw new ObjectDisposedException ("too late");
155
156                                 return GetResponse (_ntpwd);
157                         }
158                 }
159
160                 public byte [] LMSessionKey {
161                         get {
162                                 if (_disposed)
163                                         throw new ObjectDisposedException ("too late");
164
165                                 byte[] lm = LM;
166                                 byte[] pwd = new byte [14];
167                                 Buffer.BlockCopy (lm, 0, pwd, 0, 8);
168                                 for (int i = 8; i < 14; i++)
169                                         pwd [i] = 0xBD;
170                                 byte[] response = new byte [16];
171                                 DES des = DES.Create ();
172                                 des.Mode = CipherMode.ECB;
173                                 des.Key = PrepareDESKey (pwd, 0);
174                                 ICryptoTransform ct = des.CreateEncryptor ();
175                                 ct.TransformBlock (lm, 0, 8, response, 0);
176                                 des.Key = PrepareDESKey (pwd, 7);
177                                 ct = des.CreateEncryptor ();
178                                 ct.TransformBlock (lm, 0, 8, response, 8);
179                                 return response;
180                         }
181                 }
182
183                 // IDisposable method
184
185                 public void Dispose () 
186                 {
187                         Dispose (true);
188                         GC.SuppressFinalize (this);
189                 }
190
191                 private void Dispose (bool disposing) 
192                 {
193                         if (!_disposed) {
194                                 // cleanup our stuff
195                                 Array.Clear (_lmpwd, 0, _lmpwd.Length);
196                                 Array.Clear (_ntpwd, 0, _ntpwd.Length);
197                                 if (_challenge != null)
198                                         Array.Clear (_challenge, 0, _challenge.Length);
199                                 _disposed = true;
200                         }
201                 }
202
203                 // private methods
204
205                 private byte[] GetResponse (byte[] pwd) 
206                 {
207                         byte[] response = new byte [24];
208                         DES des = DES.Create ();
209                         des.Mode = CipherMode.ECB;
210                         des.Key = PrepareDESKey (pwd, 0);
211                         ICryptoTransform ct = des.CreateEncryptor ();
212                         ct.TransformBlock (_challenge, 0, 8, response, 0);
213                         des.Key = PrepareDESKey (pwd, 7);
214                         ct = des.CreateEncryptor ();
215                         ct.TransformBlock (_challenge, 0, 8, response, 8);
216                         des.Key = PrepareDESKey (pwd, 14);
217                         ct = des.CreateEncryptor ();
218                         ct.TransformBlock (_challenge, 0, 8, response, 16);
219                         return response;
220                 }
221
222                 private byte[] PrepareDESKey (byte[] key56bits, int position) 
223                 {
224                         // convert to 8 bytes
225                         byte[] key = new byte [8];
226                         key [0] = key56bits [position];
227                         key [1] = (byte) ((key56bits [position] << 7)     | (key56bits [position + 1] >> 1));
228                         key [2] = (byte) ((key56bits [position + 1] << 6) | (key56bits [position + 2] >> 2));
229                         key [3] = (byte) ((key56bits [position + 2] << 5) | (key56bits [position + 3] >> 3));
230                         key [4] = (byte) ((key56bits [position + 3] << 4) | (key56bits [position + 4] >> 4));
231                         key [5] = (byte) ((key56bits [position + 4] << 3) | (key56bits [position + 5] >> 5));
232                         key [6] = (byte) ((key56bits [position + 5] << 2) | (key56bits [position + 6] >> 6));
233                         key [7] = (byte)  (key56bits [position + 6] << 1);
234                         return key;
235                 }
236
237                 private byte[] PasswordToKey (string password, int position) 
238                 {
239                         byte[] key7 = new byte [7];
240                         int len = System.Math.Min (password.Length - position, 7);
241                         Encoding.ASCII.GetBytes (password.ToUpper (CultureInfo.CurrentCulture), position, len, key7, 0);
242                         byte[] key8 = PrepareDESKey (key7, 0);
243                         // cleanup intermediate key material
244                         Array.Clear (key7, 0, key7.Length);
245                         return key8;
246                 }
247         }
248 }