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