Merge pull request #637 from LogosBible/enetdown
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Ntlm / Type2Message.cs
1 //
2 // Mono.Security.Protocol.Ntlm.Type2Message - Challenge
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // References
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
15 //
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:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
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.
34 //
35
36 using System;
37 using System.Text;
38 using System.Security.Cryptography;
39
40 namespace Mono.Security.Protocol.Ntlm {
41
42 #if INSIDE_SYSTEM
43         internal
44 #else
45         public
46 #endif
47         class Type2Message : MessageBase {
48
49                 private byte[] _nonce;
50                 private string _targetName;
51                 private byte[] _targetInfo;
52
53                 public Type2Message () : base (2)
54                 {
55                         _nonce = new byte [8];
56                         RandomNumberGenerator rng = RandomNumberGenerator.Create ();
57                         rng.GetBytes (_nonce);
58                         // default values
59                         Flags = (NtlmFlags) 0x8201;
60                 }
61
62                 public Type2Message (byte[] message) : base (2)
63                 {
64                         _nonce = new byte [8];
65                         Decode (message);
66                 }
67
68                 ~Type2Message () 
69                 {
70                         if (_nonce != null)
71                                 Array.Clear (_nonce, 0, _nonce.Length);
72                 }
73
74                 // properties
75
76                 public byte[] Nonce {
77                         get { return (byte[]) _nonce.Clone (); }
78                         set { 
79                                 if (value == null)
80                                         throw new ArgumentNullException ("Nonce");
81                                 if (value.Length != 8) {
82                                         string msg = Locale.GetText ("Invalid Nonce Length (should be 8 bytes).");
83                                         throw new ArgumentException (msg, "Nonce");
84                                 }
85                                 _nonce = (byte[]) value.Clone (); 
86                         }
87                 }
88
89                 public string TargetName {
90                         get { return _targetName; }
91                 }
92
93                 public byte[] TargetInfo {
94                         get { return (byte[])_targetInfo.Clone (); }
95                 }
96
97                 // methods
98
99                 protected override void Decode (byte[] message)
100                 {
101                         base.Decode (message);
102
103                         Flags = (NtlmFlags)BitConverterLE.ToUInt32 (message, 20);
104
105                         Buffer.BlockCopy (message, 24, _nonce, 0, 8);
106
107                         var tname_len = BitConverterLE.ToUInt16 (message, 12);
108                         var tname_off = BitConverterLE.ToUInt16 (message, 16);
109                         if (tname_len > 0) {
110                                 if ((Flags & NtlmFlags.NegotiateOem) != 0)
111                                         _targetName = Encoding.ASCII.GetString (message, tname_off, tname_len);
112                                 else
113                                         _targetName = Encoding.Unicode.GetString (message, tname_off, tname_len);
114                         }
115                         
116                         // The Target Info block is optional.
117                         if (message.Length >= 48) {
118                                 var tinfo_len = BitConverterLE.ToUInt16 (message, 40);
119                                 var tinfo_off = BitConverterLE.ToUInt16 (message, 44);
120                                 if (tinfo_len > 0) {
121                                         _targetInfo = new byte [tinfo_len];
122                                         Buffer.BlockCopy (message, tinfo_off, _targetInfo, 0, tinfo_len);
123                                 }
124                         }
125                 }
126
127                 public override byte[] GetBytes ()
128                 {
129                         byte[] data = PrepareMessage (40);
130
131                         // message length
132                         short msg_len = (short)data.Length;
133                         data [16] = (byte) msg_len;
134                         data [17] = (byte)(msg_len >> 8);
135
136                         // flags
137                         data [20] = (byte) Flags;
138                         data [21] = (byte)((uint)Flags >> 8);
139                         data [22] = (byte)((uint)Flags >> 16);
140                         data [23] = (byte)((uint)Flags >> 24);
141
142                         Buffer.BlockCopy (_nonce, 0, data, 24, _nonce.Length);
143                         return data;
144                 }
145         }
146 }