2 // Mono.Security.Protocol.Ntlm.ChallengeResponse
3 // Implements Challenge Response for NTLM v1
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // (C) 2004 Novell (http://www.novell.com)
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
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:
27 // The above copyright notice and this permission notice shall be
28 // included in all copies or substantial portions of the Software.
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.
40 using System
.Globalization
;
41 using System
.Security
.Cryptography
;
44 using Mono
.Security
.Cryptography
;
46 namespace Mono
.Security
.Protocol
.Ntlm
{
48 public class ChallengeResponse
: IDisposable
{
50 static private byte[] magic
= { 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 }
;
52 // This is the pre-encrypted magic value with a null DES key (0xAAD3B435B51404EE)
53 // Ref: http://packetstormsecurity.nl/Crackers/NT/l0phtcrack/l0phtcrack2.5-readme.html
54 static private byte[] nullEncMagic
= { 0xAA, 0xD3, 0xB4, 0x35, 0xB5, 0x14, 0x04, 0xEE }
;
56 private bool _disposed
;
57 private byte[] _challenge
;
58 private byte[] _lmpwd
;
59 private byte[] _ntpwd
;
63 public ChallengeResponse ()
66 _lmpwd
= new byte [21];
67 _ntpwd
= new byte [21];
70 public ChallengeResponse (string password
, byte[] challenge
) : this ()
73 Challenge
= challenge
;
84 public string Password
{
88 throw new ObjectDisposedException ("too late");
90 // create Lan Manager password
91 DES des
= DES
.Create ();
92 des
.Mode
= CipherMode
.ECB
;
93 ICryptoTransform ct
= null;
95 // Note: In .NET DES cannot accept a weak key
96 // this can happen for a null password
97 if ((value == null) || (value.Length
< 1)) {
98 Buffer
.BlockCopy (nullEncMagic
, 0, _lmpwd
, 0, 8);
101 des
.Key
= PasswordToKey (value, 0);
102 ct
= des
.CreateEncryptor ();
103 ct
.TransformBlock (magic
, 0, 8, _lmpwd
, 0);
106 // and if a password has less than 8 characters
107 if ((value == null) || (value.Length
< 8)) {
108 Buffer
.BlockCopy (nullEncMagic
, 0, _lmpwd
, 8, 8);
111 des
.Key
= PasswordToKey (value, 7);
112 ct
= des
.CreateEncryptor ();
113 ct
.TransformBlock (magic
, 0, 8, _lmpwd
, 8);
116 // create NT password
117 MD4 md4
= MD4
.Create ();
118 byte[] data
= ((value == null) ? (new byte [0]) : (Encoding
.Unicode
.GetBytes (value)));
119 byte[] hash
= md4
.ComputeHash (data
);
120 Buffer
.BlockCopy (hash
, 0, _ntpwd
, 0, 16);
123 Array
.Clear (data
, 0, data
.Length
);
124 Array
.Clear (hash
, 0, hash
.Length
);
129 public byte[] Challenge
{
133 throw new ArgumentNullException ("Challenge");
135 throw new ObjectDisposedException ("too late");
136 // we don't want the caller to modify the value afterward
137 _challenge
= (byte[]) value.Clone ();
144 throw new ObjectDisposedException ("too late");
146 return GetResponse (_lmpwd
);
153 throw new ObjectDisposedException ("too late");
155 return GetResponse (_ntpwd
);
159 // IDisposable method
161 public void Dispose ()
164 GC
.SuppressFinalize (this);
167 private void Dispose (bool disposing
)
171 Array
.Clear (_lmpwd
, 0, _lmpwd
.Length
);
172 Array
.Clear (_ntpwd
, 0, _ntpwd
.Length
);
173 if (_challenge
!= null)
174 Array
.Clear (_challenge
, 0, _challenge
.Length
);
181 private byte[] GetResponse (byte[] pwd
)
183 byte[] response
= new byte [24];
184 DES des
= DES
.Create ();
185 des
.Mode
= CipherMode
.ECB
;
186 des
.Key
= PrepareDESKey (pwd
, 0);
187 ICryptoTransform ct
= des
.CreateEncryptor ();
188 ct
.TransformBlock (_challenge
, 0, 8, response
, 0);
189 des
.Key
= PrepareDESKey (pwd
, 7);
190 ct
= des
.CreateEncryptor ();
191 ct
.TransformBlock (_challenge
, 0, 8, response
, 8);
192 des
.Key
= PrepareDESKey (pwd
, 14);
193 ct
= des
.CreateEncryptor ();
194 ct
.TransformBlock (_challenge
, 0, 8, response
, 16);
198 private byte[] PrepareDESKey (byte[] key56bits
, int position
)
200 // convert to 8 bytes
201 byte[] key
= new byte [8];
202 key
[0] = key56bits
[position
];
203 key
[1] = (byte) ((key56bits
[position
] << 7) | (key56bits
[position
+ 1] >> 1));
204 key
[2] = (byte) ((key56bits
[position
+ 1] << 6) | (key56bits
[position
+ 2] >> 2));
205 key
[3] = (byte) ((key56bits
[position
+ 2] << 5) | (key56bits
[position
+ 3] >> 3));
206 key
[4] = (byte) ((key56bits
[position
+ 3] << 4) | (key56bits
[position
+ 4] >> 4));
207 key
[5] = (byte) ((key56bits
[position
+ 4] << 3) | (key56bits
[position
+ 5] >> 5));
208 key
[6] = (byte) ((key56bits
[position
+ 5] << 2) | (key56bits
[position
+ 6] >> 6));
209 key
[7] = (byte) (key56bits
[position
+ 6] << 1);
213 private byte[] PasswordToKey (string password
, int position
)
215 byte[] key7
= new byte [7];
216 int len
= System
.Math
.Min (password
.Length
- position
, 7);
217 Encoding
.ASCII
.GetBytes (password
.ToUpper (CultureInfo
.CurrentCulture
), position
, len
, key7
, 0);
218 byte[] key8
= PrepareDESKey (key7
, 0);
219 // cleanup intermediate key material
220 Array
.Clear (key7
, 0, key7
.Length
);