KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > ntlmssp > NtlmMessage


1 /* jcifs smb client library in Java
2  * Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
3  * "Eric Glass" <jcifs at samba dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package jcifs.ntlmssp;
21
22 import jcifs.Config;
23
24 /**
25  * Abstract superclass for all NTLMSSP messages.
26  */

27 public abstract class NtlmMessage implements NtlmFlags {
28
29     /**
30      * The NTLMSSP "preamble".
31      */

32     protected static final byte[] NTLMSSP_SIGNATURE = new byte[] {
33         (byte) 'N', (byte) 'T', (byte) 'L', (byte) 'M',
34         (byte) 'S', (byte) 'S', (byte) 'P', (byte) 0
35     };
36
37     private static final String JavaDoc OEM_ENCODING = Config.getProperty("jcifs.encoding", "Cp850");
38
39     private int flags;
40
41     /**
42      * Returns the flags currently in use for this message.
43      *
44      * @return An <code>int</code> containing the flags in use for this
45      * message.
46      */

47     public int getFlags() {
48         return flags;
49     }
50
51     /**
52      * Sets the flags for this message.
53      *
54      * @param flags The flags for this message.
55      */

56     public void setFlags(int flags) {
57         this.flags = flags;
58     }
59
60     /**
61      * Returns the status of the specified flag.
62      *
63      * @param flag The flag to test (i.e., <code>NTLMSSP_NEGOTIATE_OEM</code>).
64      * @return A <code>boolean</code> indicating whether the flag is set.
65      */

66     public boolean getFlag(int flag) {
67         return (getFlags() & flag) != 0;
68     }
69
70     /**
71      * Sets or clears the specified flag.
72      *
73      * @param flag The flag to set/clear (i.e.,
74      * <code>NTLMSSP_NEGOTIATE_OEM</code>).
75      * @param value Indicates whether to set (<code>true</code>) or
76      * clear (<code>false</code>) the specified flag.
77      */

78     public void setFlag(int flag, boolean value) {
79         setFlags(value ? (getFlags() | flag) :
80                 (getFlags() & (0xffffffff ^ flag)));
81     }
82
83     static int readULong(byte[] src, int index) {
84         return (src[index] & 0xff) |
85                 ((src[index + 1] & 0xff) << 8) |
86                 ((src[index + 2] & 0xff) << 16) |
87                 ((src[index + 3] & 0xff) << 24);
88     }
89
90     static int readUShort(byte[] src, int index) {
91         return (src[index] & 0xff) | ((src[index + 1] & 0xff) << 8);
92     }
93
94     static byte[] readSecurityBuffer(byte[] src, int index) {
95         int length = readUShort(src, index);
96         int offset = readULong(src, index + 4);
97         byte[] buffer = new byte[length];
98         System.arraycopy(src, offset, buffer, 0, length);
99         return buffer;
100     }
101
102     static void writeULong(byte[] dest, int offset, int ulong) {
103         dest[offset] = (byte) (ulong & 0xff);
104         dest[offset + 1] = (byte) (ulong >> 8 & 0xff);
105         dest[offset + 2] = (byte) (ulong >> 16 & 0xff);
106         dest[offset + 3] = (byte) (ulong >> 24 & 0xff);
107     }
108
109     static void writeUShort(byte[] dest, int offset, int ushort) {
110         dest[offset] = (byte) (ushort & 0xff);
111         dest[offset + 1] = (byte) (ushort >> 8 & 0xff);
112     }
113
114     static void writeSecurityBuffer(byte[] dest, int offset, int bodyOffset,
115             byte[] src) {
116         int length = (src != null) ? src.length : 0;
117         if (length == 0) return;
118         writeUShort(dest, offset, length);
119         writeUShort(dest, offset + 2, length);
120         writeULong(dest, offset + 4, bodyOffset);
121         System.arraycopy(src, 0, dest, bodyOffset, length);
122     }
123
124     static String JavaDoc getOEMEncoding() {
125         return OEM_ENCODING;
126     }
127
128     /**
129      * Returns the raw byte representation of this message.
130      *
131      * @return A <code>byte[]</code> containing the raw message material.
132      */

133     public abstract byte[] toByteArray();
134
135 }
136
Popular Tags