Merge pull request #498 from Unroll-Me/master
[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         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 #if MOONLIGHT
93                                 DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
94 #else
95                                 DES des = DES.Create ();
96 #endif
97                                 des.Mode = CipherMode.ECB;
98                                 ICryptoTransform ct = null;
99                                 
100                                 // Note: In .NET DES cannot accept a weak key
101                                 // this can happen for a null password
102                                 if ((value == null) || (value.Length < 1)) {
103                                         Buffer.BlockCopy (nullEncMagic, 0, _lmpwd, 0, 8);
104                                 }
105                                 else {
106                                         des.Key = PasswordToKey (value, 0);
107                                         ct = des.CreateEncryptor ();
108                                         ct.TransformBlock (magic, 0, 8, _lmpwd, 0);
109                                 }
110
111                                 // and if a password has less than 8 characters
112                                 if ((value == null) || (value.Length < 8)) {
113                                         Buffer.BlockCopy (nullEncMagic, 0, _lmpwd, 8, 8);
114                                 }
115                                 else {
116                                         des.Key = PasswordToKey (value, 7);
117                                         ct = des.CreateEncryptor ();
118                                         ct.TransformBlock (magic, 0, 8, _lmpwd, 8);
119                                 }
120
121                                 // create NT password
122 #if MOONLIGHT
123                                 MD4Managed md4 = new MD4Managed ();
124 #else
125                                 MD4 md4 = MD4.Create ();
126 #endif
127                                 byte[] data = ((value == null) ? (new byte [0]) : (Encoding.Unicode.GetBytes (value)));
128                                 byte[] hash = md4.ComputeHash (data);
129                                 Buffer.BlockCopy (hash, 0, _ntpwd, 0, 16);
130
131                                 // clean up
132                                 Array.Clear (data, 0, data.Length);
133                                 Array.Clear (hash, 0, hash.Length);
134                                 des.Clear ();
135                         }
136                 }
137
138                 public byte[] Challenge {
139                         get { return null; }
140                         set {
141                                 if (value == null)
142                                         throw new ArgumentNullException ("Challenge");
143                                 if (_disposed)
144                                         throw new ObjectDisposedException ("too late");
145                                 // we don't want the caller to modify the value afterward
146                                 _challenge = (byte[]) value.Clone ();
147                         }
148                 }
149
150                 public byte[] LM {
151                         get { 
152                                 if (_disposed)
153                                         throw new ObjectDisposedException ("too late");
154
155                                 return GetResponse (_lmpwd);
156                         }
157                 }
158
159                 public byte[] NT {
160                         get { 
161                                 if (_disposed)
162                                         throw new ObjectDisposedException ("too late");
163
164                                 return GetResponse (_ntpwd);
165                         }
166                 }
167
168                 // IDisposable method
169
170                 public void Dispose () 
171                 {
172                         Dispose (true);
173                         GC.SuppressFinalize (this);
174                 }
175
176                 private void Dispose (bool disposing) 
177                 {
178                         if (!_disposed) {
179                                 // cleanup our stuff
180                                 Array.Clear (_lmpwd, 0, _lmpwd.Length);
181                                 Array.Clear (_ntpwd, 0, _ntpwd.Length);
182                                 if (_challenge != null)
183                                         Array.Clear (_challenge, 0, _challenge.Length);
184                                 _disposed = true;
185                         }
186                 }
187
188                 // private methods
189
190                 private byte[] GetResponse (byte[] pwd) 
191                 {
192                         byte[] response = new byte [24];
193 #if MOONLIGHT
194                         DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
195 #else
196                         DES des = DES.Create ();
197 #endif
198                         des.Mode = CipherMode.ECB;
199                         des.Key = PrepareDESKey (pwd, 0);
200                         ICryptoTransform ct = des.CreateEncryptor ();
201                         ct.TransformBlock (_challenge, 0, 8, response, 0);
202                         des.Key = PrepareDESKey (pwd, 7);
203                         ct = des.CreateEncryptor ();
204                         ct.TransformBlock (_challenge, 0, 8, response, 8);
205                         des.Key = PrepareDESKey (pwd, 14);
206                         ct = des.CreateEncryptor ();
207                         ct.TransformBlock (_challenge, 0, 8, response, 16);
208                         return response;
209                 }
210
211                 private byte[] PrepareDESKey (byte[] key56bits, int position) 
212                 {
213                         // convert to 8 bytes
214                         byte[] key = new byte [8];
215                         key [0] = key56bits [position];
216                         key [1] = (byte) ((key56bits [position] << 7)     | (key56bits [position + 1] >> 1));
217                         key [2] = (byte) ((key56bits [position + 1] << 6) | (key56bits [position + 2] >> 2));
218                         key [3] = (byte) ((key56bits [position + 2] << 5) | (key56bits [position + 3] >> 3));
219                         key [4] = (byte) ((key56bits [position + 3] << 4) | (key56bits [position + 4] >> 4));
220                         key [5] = (byte) ((key56bits [position + 4] << 3) | (key56bits [position + 5] >> 5));
221                         key [6] = (byte) ((key56bits [position + 5] << 2) | (key56bits [position + 6] >> 6));
222                         key [7] = (byte)  (key56bits [position + 6] << 1);
223                         return key;
224                 }
225
226                 private byte[] PasswordToKey (string password, int position) 
227                 {
228                         byte[] key7 = new byte [7];
229                         int len = System.Math.Min (password.Length - position, 7);
230                         Encoding.ASCII.GetBytes (password.ToUpper (CultureInfo.CurrentCulture), position, len, key7, 0);
231                         byte[] key8 = PrepareDESKey (key7, 0);
232                         // cleanup intermediate key material
233                         Array.Clear (key7, 0, key7.Length);
234                         return key8;
235                 }
236         }
237 }