KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.UnsupportedEncodingException JavaDoc;
20
21 import org.alfresco.filesys.util.DataPacker;
22
23 /**
24  * NTLM Message Types Base Class
25  *
26  * @author GKSpencer
27  */

28 public abstract class NTLMMessage
29 {
30     // Default buffer size to allocate
31

32     private static final int DefaultBlobSize = 256;
33
34     // Field offsets
35

36     public static final int OffsetSignature = 0;
37     public static final int OffsetType = 8;
38     
39     // Buffer header length
40

41     public static final int BufferHeaderLen = 8;
42     
43     // Buffer, offset and lenght of the NTLM blob
44

45     private byte[] m_buf;
46     private int m_offset;
47     
48     private int m_len;
49     
50     /**
51      * Default constructor
52      */

53     protected NTLMMessage()
54     {
55         // Allocate a buffer
56

57         m_buf = new byte[DefaultBlobSize];
58         m_len = DefaultBlobSize;
59     }
60     
61     /**
62      * Class constructor
63      *
64      * @param buf byte[]
65      * @param offset int
66      * @param len int
67      */

68     protected NTLMMessage(byte[] buf, int offset, int len)
69     {
70         m_buf = buf;
71         m_offset = offset;
72         
73         m_len = len;
74     }
75     
76     /**
77      * Return the message type
78      *
79      * @return int
80      */

81     public final int isMessageType()
82     {
83         return DataPacker.getIntelInt(m_buf, m_offset + OffsetType);
84     }
85     
86     /**
87      * Return the message flags
88      *
89      * @return int
90      */

91     public abstract int getFlags();
92     
93     /**
94      * Return the state of the specified flag
95      *
96      * @param flag int
97      * @return boolean
98      */

99     public final boolean hasFlag(int flag)
100     {
101         return (getFlags() & flag) != 0 ? true : false;
102     }
103     /**
104      * Return the message length
105      *
106      * @return int
107      */

108     public int getLength()
109     {
110         return m_len;
111     }
112     
113     /**
114      * Set the message type
115      *
116      * @param typ int
117      */

118     public final void setMessageType(int typ)
119     {
120         DataPacker.putIntelInt(typ, m_buf, m_offset + OffsetType);
121     }
122     
123     /**
124      * Copy the NTLM blob data from the specified buffer
125      *
126      * @param buf byte[]
127      * @param offset int
128      * @param len int
129      */

130     public final void copyFrom(byte[] buf, int offset, int len)
131     {
132         // Allocate a new buffer, if required
133

134         if ( m_buf == null || m_offset != 0 || m_buf.length < len)
135             m_buf = new byte[len];
136         
137         // Copy the security blob data
138

139         System.arraycopy(buf, offset, m_buf, 0, len);
140     }
141     
142     /**
143      * Return the NTLM message as a byte array
144      *
145      * @return byte[]
146      */

147     public final byte[] getBytes()
148     {
149        byte[] byts = new byte[getLength()];
150        System.arraycopy(m_buf, m_offset, byts, 0, getLength());
151        return byts;
152     }
153     
154     /**
155      * Set the message flags
156      *
157      * @param flags int
158      */

159     protected abstract void setFlags(int flags);
160     
161     /**
162      * Initialize the blob
163      *
164      * @param typ int
165      * @param flags int
166      */

167     protected void initializeHeader(int typ, int flags)
168     {
169         // Set the signature
170

171         System.arraycopy( NTLM.Signature, 0, m_buf, m_offset, NTLM.Signature.length);
172         
173         setMessageType(typ);
174         setFlags(flags);
175     }
176     
177     /**
178      * Return a short/16bit value
179      *
180      * @param offset int
181      * @return int
182      */

183     protected final int getShortValue(int offset)
184     {
185         return DataPacker.getIntelShort(m_buf, m_offset + offset);
186     }
187     
188     /**
189      * Return an int/32bit value
190      *
191      * @param offset int
192      * @return int
193      */

194     protected final int getIntValue(int offset)
195     {
196         return DataPacker.getIntelInt(m_buf, m_offset + offset);
197     }
198
199     /**
200      * Return the offset for a byte value
201      *
202      * @param offset int
203      * @return int
204      */

205     protected final int getByteOffset(int offset)
206     {
207         return getIntValue(m_offset + offset + 4);
208     }
209     
210     /**
211      * Return a byte value that has a header
212      *
213      * @param offset int
214      * @return byte[]
215      */

216     protected final byte[] getByteValue(int offset)
217     {
218         // Get the byte block length
219

220         int bLen = getShortValue(m_offset + offset);
221         if ( bLen == 0)
222             return null;
223         
224         int bOff = getIntValue(m_offset + offset + 4);
225         return getRawBytes(bOff, bLen);
226     }
227     
228     /**
229      * Return a block of byte data
230      *
231      * @param offset int
232      * @param len int
233      * @return byte[]
234      */

235     protected final byte[] getRawBytes(int offset, int len)
236     {
237         byte[] byts = new byte[len];
238         System.arraycopy(m_buf, m_offset + offset, byts, 0, len);
239         
240         return byts;
241     }
242     
243     /**
244      * Return the length of a string
245      *
246      * @param offset int
247      * @return int
248      */

249     protected final int getStringLength(int offset)
250     {
251         int bufpos = m_offset + offset;
252         
253         if ( bufpos + 2 > m_len)
254             return -1;
255         return DataPacker.getIntelShort(m_buf, bufpos);
256     }
257     
258     /**
259      * Return the allocated length of a string
260      *
261      * @param offset int
262      * @return int
263      */

264     protected final int getStringAllocatedLength(int offset)
265     {
266         int bufpos = m_offset + offset;
267         
268         if ( bufpos + 8 > m_len)
269             return -1;
270         return DataPacker.getIntelShort(m_buf, bufpos + 2);
271     }
272     
273     /**
274      * Return the string data offset
275      *
276      * @param offset int
277      * @return int
278      */

279     protected final int getStringOffset(int offset)
280     {
281         int bufpos = m_offset + offset;
282         
283         if ( bufpos + 8 > m_len)
284             return -1;
285         return DataPacker.getIntelInt(m_buf, bufpos + 4);
286     }
287     
288     /**
289      * Return a string value
290      *
291      * @param offset int
292      * @param isuni boolean
293      * @return String
294      */

295     protected final String JavaDoc getStringValue(int offset, boolean isuni)
296     {
297         int bufpos = m_offset + offset;
298         
299         if ( offset + 8 > m_len)
300             return null;
301         
302         // Get the offset to the string
303

304         int len = DataPacker.getIntelShort(m_buf, bufpos);
305         int pos = DataPacker.getIntelInt(m_buf, bufpos + 4);
306         
307         // Get the string value
308

309         if ( pos + len > m_len)
310             return null;
311 // if ( isuni)
312
// len = len / 2;
313

314         // Unpack the string
315

316         String JavaDoc str = null;
317         try
318         {
319             str = new String JavaDoc(m_buf, m_offset + pos, len, isuni ? "UnicodeLittle" : "US-ASCII");
320         }
321         catch (UnsupportedEncodingException JavaDoc ex)
322         {
323             ex.printStackTrace();
324         }
325         
326         return str;
327     }
328     
329     /**
330      * Return a raw string
331      *
332      * @param offset int
333      * @param len int
334      * @param isuni boolean
335      * @return String
336      */

337     protected final String JavaDoc getRawString(int offset, int len, boolean isuni)
338     {
339         return DataPacker.getString(m_buf, m_offset + offset, len, isuni);
340     }
341     
342     /**
343      * Set a short/16bit value
344      *
345      * @param offset int
346      * @param sval int
347      */

348     protected final void setShortValue(int offset, int sval)
349     {
350         DataPacker.putIntelShort(sval, m_buf, m_offset + offset);
351     }
352     
353     /**
354      * Set an int/32bit value
355      *
356      * @param offset int
357      * @param val int
358      */

359     protected final void setIntValue(int offset, int val)
360     {
361         DataPacker.putIntelInt(val, m_buf, m_offset + offset);
362     }
363     
364     /**
365      * Set a raw byte value
366      *
367      * @param offset int
368      * @param byts byte[]
369      */

370     protected final void setRawBytes(int offset, byte[] byts)
371     {
372         System.arraycopy(byts, 0, m_buf, m_offset + offset, byts.length);
373     }
374     
375     /**
376      * Set raw int values
377      *
378      * @param offset int
379      * @param ints int[]
380      */

381     protected final void setRawInts(int offset, int[] ints)
382     {
383         int bufpos = m_offset + offset;
384         
385         for ( int i = 0; i < ints.length; i++)
386         {
387             DataPacker.putIntelInt( ints[i], m_buf, bufpos);
388             bufpos += 4;
389         }
390     }
391     
392     /**
393      * Pack a raw string
394      *
395      * @param offset int
396      * @param str String
397      * @param isuni boolean
398      * @return int
399      */

400     protected final int setRawString(int offset, String JavaDoc str, boolean isuni)
401     {
402         return DataPacker.putString(str, m_buf, m_offset + offset, false, isuni);
403     }
404     
405     /**
406      * Zero out an area of bytes
407      *
408      * @param offset int
409      * @param len int
410      */

411     protected final void zeroBytes(int offset, int len)
412     {
413         int bufpos = m_offset + offset;
414         for ( int i = 0; i < len; i++)
415             m_buf[bufpos++] = (byte) 0;
416     }
417     
418     /**
419      * Set a byte array value
420      *
421      * @param offset int
422      * @param byts byte[]
423      * @param dataOffset int
424      * @return int
425      */

426     protected final int setByteValue(int offset, byte[] byts, int dataOffset)
427     {
428         int bytsLen = byts != null ? byts.length : 0;
429         
430         if ( m_offset + offset + 12 > m_buf.length ||
431                 m_offset + dataOffset + bytsLen > m_buf.length)
432             throw new ArrayIndexOutOfBoundsException JavaDoc();
433         
434         // Pack the byte pointer block
435

436         DataPacker.putIntelShort(bytsLen, m_buf, m_offset + offset);
437         DataPacker.putIntelShort(bytsLen, m_buf, m_offset + offset + 2);
438         DataPacker.putIntelInt(dataOffset, m_buf, m_offset + offset + 4);
439
440         // Pack the bytes
441

442         if ( bytsLen > 0)
443             System.arraycopy(byts, 0, m_buf, m_offset + dataOffset, bytsLen);
444         
445         // Return the new data buffer offset
446

447         return dataOffset + DataPacker.wordAlign(bytsLen);
448     }
449     
450     /**
451      * Set a string value
452      *
453      * @param offset int
454      * @param val String
455      * @param strOffset int
456      * @param isuni boolean
457      * @return int
458      */

459     protected final int setStringValue(int offset, String JavaDoc val, int strOffset, boolean isuni)
460     {
461         // Get the length in bytes
462

463         int len = val.length();
464         if ( isuni)
465             len *= 2;
466         
467         if ( m_offset + offset + 8 > m_buf.length ||
468                 m_offset + strOffset + len > m_buf.length)
469             throw new ArrayIndexOutOfBoundsException JavaDoc();
470         
471         // Pack the string pointer block
472

473         
474         DataPacker.putIntelShort(len, m_buf, m_offset + offset);
475         DataPacker.putIntelShort(len, m_buf, m_offset + offset + 2);
476         DataPacker.putIntelInt(strOffset, m_buf, m_offset + offset + 4);
477         
478         // Pack the string
479

480         return DataPacker.putString(val, m_buf, m_offset + strOffset, false, isuni) - m_offset;
481     }
482     
483     /**
484      * Set the message length
485      *
486      * @param len int
487      */

488     protected final void setLength(int len)
489     {
490         m_len = len;
491     }
492     
493     /**
494      * Validate and determine the NTLM message type
495      *
496      * @param buf byte[]
497      * @return int
498      */

499     public final static int isNTLMType(byte[] buf)
500     {
501         return isNTLMType(buf, 0);
502     }
503     
504     /**
505      * Validate and determine the NTLM message type
506      *
507      * @param buf byte[]
508      * @param offset int
509      * @return int
510      */

511     public final static int isNTLMType(byte[] buf, int offset)
512     {
513         // Validate the buffer
514

515         if ( buf == null || buf.length < BufferHeaderLen)
516             return -1;
517         
518         for ( int i = 0; i < NTLM.Signature.length; i++)
519         {
520             if ( buf[offset + i] != NTLM.Signature[i])
521                 return -1;
522         }
523         
524         // Get the NTLM message type
525

526         return DataPacker.getIntelInt(buf, offset + OffsetType);
527     }
528 }
529
Popular Tags