KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > ntlm > Type3NTLMMessage


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth.ntlm;
18
19 import org.alfresco.filesys.util.HexDump;
20
21 /**
22  * Type 3 NTLM Message Class
23  *
24  * @author GKSpencer
25  */

26 public class Type3NTLMMessage extends NTLMMessage
27 {
28     // Minimal type 3 message length
29

30     public static final int MinimalMessageLength = 52;
31     
32     // Type 2 field offsets
33

34     public static final int OffsetLMResponse = 12;
35     public static final int OffsetNTLMResponse = 20;
36     public static final int OffsetDomain = 28;
37     public static final int OffsetUserName = 36;
38     public static final int OffsetWorkstationName = 44;
39     public static final int OffsetDataMinimum = 52;
40     public static final int OffsetSessionKey = 52; // optional
41
public static final int OffsetFlags = 60; // optional
42
public static final int OffsetData = 64;
43     
44     // Flag to indicate if Unicode strings have been negotiated
45

46     private boolean m_unicode;
47     
48     // Data block offset, used to indicate if session key and flags have been specified
49

50     private int m_dataOffset = -1;
51     
52     /**
53      * Default constructor
54      */

55     public Type3NTLMMessage()
56     {
57         super();
58     }
59     
60     /**
61      * Class constructor
62      *
63      * @param buf byte[]
64      */

65     public Type3NTLMMessage(byte[] buf)
66     {
67         super(buf, 0, buf.length);
68     }
69     
70     /**
71      * Class constructor
72      *
73      * @param buf byte[]
74      * @param offset int
75      * @param len int
76      * @param unicode boolean
77      */

78     public Type3NTLMMessage(byte[] buf, int offset, int len, boolean unicode)
79     {
80         super(buf, offset, len);
81         
82         m_unicode = unicode;
83     }
84
85     /**
86      * Return the flags value
87      *
88      * @return int
89      */

90     public int getFlags()
91     {
92         return getIntValue(OffsetFlags);
93     }
94     
95     /**
96      * Return the LM password hash
97      *
98      * @return byte[]
99      */

100     public final byte[] getLMHash()
101     {
102         return getByteValue(OffsetLMResponse);
103     }
104     
105     /**
106      * Return the NTLM password hash
107      *
108      * @return byte[]
109      */

110     public final byte[] getNTLMHash()
111     {
112         return getByteValue(OffsetNTLMResponse);
113     }
114     
115     /**
116      * Return the domain name
117      *
118      * @return String
119      */

120     public final String JavaDoc getDomain()
121     {
122         return getStringValue(OffsetDomain, hasFlag(NTLM.FlagNegotiateUnicode));
123     }
124     
125     /**
126      * Return the user name
127      *
128      * @return String
129      */

130     public final String JavaDoc getUserName()
131     {
132         return getStringValue(OffsetUserName, hasFlag(NTLM.FlagNegotiateUnicode));
133     }
134     
135     /**
136      * Return the workstation name
137      *
138      * @return String
139      */

140     public final String JavaDoc getWorkstation()
141     {
142         return getStringValue(OffsetWorkstationName, hasFlag(NTLM.FlagNegotiateUnicode));
143     }
144
145     /**
146      * Determine if the session key has been specified
147      *
148      * @return boolean
149      */

150     public final boolean hasSessionKey()
151     {
152         return getShortValue(OffsetSessionKey) > 0;
153     }
154     
155     /**
156      * Return the session key, or null if the session key is not present
157      *
158      * @return byte[]
159      */

160     public final byte[] getSessionKey()
161     {
162         if ( hasSessionKey() == false)
163             return null;
164         
165         // Get the session key bytes
166

167         return getByteValue(OffsetSessionKey);
168     }
169     
170     /**
171      * Build a type 3 message
172      *
173      * @param lmHash byte[]
174      * @param ntlmHash byte[]
175      * @param domain String
176      * @param username String
177      * @param wksname String
178      * @param sessKey byte[]
179      * @param flags int
180      */

181     public final void buildType3(byte[] lmHash, byte[] ntlmHash, String JavaDoc domain, String JavaDoc username, String JavaDoc wksname,
182             byte[] sessKey, int flags)
183     {
184         initializeHeader(NTLM.Type3, 0);
185         
186         // Set the data offset
187

188         int dataOff = OffsetData;
189         
190         // Pack the domain, user and workstation names
191

192         dataOff = setStringValue(OffsetDomain, domain, dataOff, m_unicode);
193         dataOff = setStringValue(OffsetUserName, username, dataOff, m_unicode);
194         dataOff = setStringValue(OffsetWorkstationName, wksname, dataOff, m_unicode);
195         
196         // Pack the LM and NTLM password hashes
197

198         dataOff = setByteValue(OffsetLMResponse, lmHash, dataOff);
199         dataOff = setByteValue(OffsetNTLMResponse, ntlmHash, dataOff);
200         
201         // Pack the session key
202

203         dataOff = setByteValue(OffsetSessionKey, sessKey, dataOff);
204         
205         // Make sure various flags are set
206

207         int typ3flags = NTLM.FlagNegotiateNTLM + NTLM.FlagRequestTarget;
208         if ( m_unicode)
209             flags += NTLM.FlagNegotiateUnicode;
210  
211         // Pack the flags
212

213         setIntValue(OffsetFlags, typ3flags);
214         
215         // Set the message length
216

217         setLength(dataOff);
218     }
219     
220     /**
221      * Set the message flags
222      *
223      * @param flags int
224      */

225     protected void setFlags(int flags)
226     {
227         setIntValue(OffsetFlags, flags);
228     }
229     
230     /**
231      * Find the data block offset
232      *
233      * @return int
234      */

235     private final int findDataBlockOffset()
236     {
237         // Find the lowest data offset
238
//
239
// Check the LM hash
240

241         int offset = getByteOffset(OffsetLMResponse);
242         
243         if ( m_dataOffset == -1 || offset < m_dataOffset)
244             m_dataOffset = offset;
245         
246         // Check the NTLM hash
247

248         offset = getByteOffset(OffsetNTLMResponse);
249         if ( offset < m_dataOffset)
250             m_dataOffset = offset;
251         
252         // Check the domain name
253

254         offset = getStringOffset(OffsetDomain);
255         if ( offset < m_dataOffset)
256             m_dataOffset = offset;
257         
258         // Check the user name
259

260         offset = getStringOffset(OffsetUserName);
261         if ( offset < m_dataOffset)
262             m_dataOffset = offset;
263         
264         // Check the workstation
265

266         offset = getStringOffset(OffsetWorkstationName);
267         if ( offset < m_dataOffset)
268             m_dataOffset = offset;
269         
270         // Return the new data offset
271

272         return m_dataOffset;
273     }
274     
275     
276     /**
277      * Return the type 3 message as a string
278      *
279      * @return String
280      */

281     public String JavaDoc toString()
282     {
283         StringBuilder JavaDoc str = new StringBuilder JavaDoc();
284         
285         str.append("[Type3:");
286         
287         str.append(",LM:");
288         if ( getLMHash() != null)
289             str.append(HexDump.hexString(getLMHash()));
290         else
291             str.append("<Null>");
292         
293         str.append(",NTLM:");
294         if ( getNTLMHash() != null)
295             str.append(HexDump.hexString(getNTLMHash()));
296         else
297             str.append("<Null>");
298         
299         str.append(",Dom:");
300         str.append(getDomain());
301         str.append(",User:");
302         str.append(getUserName());
303         str.append(",Wks:");
304         str.append(getWorkstation());
305         
306         if ( hasSessionKey())
307         {
308             str.append(",SessKey:");
309             str.append(HexDump.hexString(getSessionKey()));
310             str.append(",Flags:0x");
311             str.append(Integer.toHexString(getFlags()));
312         }
313         str.append("]");
314         
315         return str.toString();
316     }
317 }
318
Popular Tags