2 // Mono.Security.Protocol.Ntlm.Type2Message - Challenge
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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
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:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
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.
37 using System.Security.Cryptography;
39 namespace Mono.Security.Protocol.Ntlm {
41 public class Type2Message : MessageBase {
43 private byte[] _nonce;
45 public Type2Message () : base (2)
47 _nonce = new byte [8];
48 RandomNumberGenerator rng = RandomNumberGenerator.Create ();
49 rng.GetBytes (_nonce);
51 Flags = (NtlmFlags) 0x8201;
54 public Type2Message (byte[] message) : base (2)
56 _nonce = new byte [8];
63 Array.Clear (_nonce, 0, _nonce.Length);
69 get { return (byte[]) _nonce.Clone (); }
72 throw new ArgumentNullException ("Nonce");
73 if (value.Length != 8) {
74 string msg = Locale.GetText ("Invalid Nonce Length (should be 8 bytes).");
75 throw new ArgumentException (msg, "Nonce");
77 _nonce = (byte[]) value.Clone ();
83 protected override void Decode (byte[] message)
85 base.Decode (message);
87 Flags = (NtlmFlags) BitConverterLE.ToUInt32 (message, 20);
89 Buffer.BlockCopy (message, 24, _nonce, 0, 8);
92 public override byte[] GetBytes ()
94 byte[] data = PrepareMessage (40);
97 short msg_len = (short)data.Length;
98 data [16] = (byte) msg_len;
99 data [17] = (byte)(msg_len >> 8);
102 data [20] = (byte) Flags;
103 data [21] = (byte)((uint)Flags >> 8);
104 data [22] = (byte)((uint)Flags >> 16);
105 data [23] = (byte)((uint)Flags >> 24);
107 Buffer.BlockCopy (_nonce, 0, data, 24, _nonce.Length);