001: /* jcifs smb client library in Java
002: * Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
003: * "Eric Glass" <jcifs at samba dot org>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package jcifs.ntlmssp;
021:
022: import jcifs.Config;
023:
024: /**
025: * Abstract superclass for all NTLMSSP messages.
026: */
027: public abstract class NtlmMessage implements NtlmFlags {
028:
029: /**
030: * The NTLMSSP "preamble".
031: */
032: protected static final byte[] NTLMSSP_SIGNATURE = new byte[] {
033: (byte) 'N', (byte) 'T', (byte) 'L', (byte) 'M', (byte) 'S',
034: (byte) 'S', (byte) 'P', (byte) 0 };
035:
036: private static final String OEM_ENCODING = Config.getProperty(
037: "jcifs.encoding", System.getProperty("file.encoding"));
038:
039: private int flags;
040:
041: /**
042: * Returns the flags currently in use for this message.
043: *
044: * @return An <code>int</code> containing the flags in use for this
045: * message.
046: */
047: public int getFlags() {
048: return flags;
049: }
050:
051: /**
052: * Sets the flags for this message.
053: *
054: * @param flags The flags for this message.
055: */
056: public void setFlags(int flags) {
057: this .flags = flags;
058: }
059:
060: /**
061: * Returns the status of the specified flag.
062: *
063: * @param flag The flag to test (i.e., <code>NTLMSSP_NEGOTIATE_OEM</code>).
064: * @return A <code>boolean</code> indicating whether the flag is set.
065: */
066: public boolean getFlag(int flag) {
067: return (getFlags() & flag) != 0;
068: }
069:
070: /**
071: * Sets or clears the specified flag.
072: *
073: * @param flag The flag to set/clear (i.e.,
074: * <code>NTLMSSP_NEGOTIATE_OEM</code>).
075: * @param value Indicates whether to set (<code>true</code>) or
076: * clear (<code>false</code>) the specified flag.
077: */
078: public void setFlag(int flag, boolean value) {
079: setFlags(value ? (getFlags() | flag)
080: : (getFlags() & (0xffffffff ^ flag)));
081: }
082:
083: static int readULong(byte[] src, int index) {
084: return (src[index] & 0xff) | ((src[index + 1] & 0xff) << 8)
085: | ((src[index + 2] & 0xff) << 16)
086: | ((src[index + 3] & 0xff) << 24);
087: }
088:
089: static int readUShort(byte[] src, int index) {
090: return (src[index] & 0xff) | ((src[index + 1] & 0xff) << 8);
091: }
092:
093: static byte[] readSecurityBuffer(byte[] src, int index) {
094: int length = readUShort(src, index);
095: int offset = readULong(src, index + 4);
096: byte[] buffer = new byte[length];
097: System.arraycopy(src, offset, buffer, 0, length);
098: return buffer;
099: }
100:
101: static void writeULong(byte[] dest, int offset, int ulong) {
102: dest[offset] = (byte) (ulong & 0xff);
103: dest[offset + 1] = (byte) (ulong >> 8 & 0xff);
104: dest[offset + 2] = (byte) (ulong >> 16 & 0xff);
105: dest[offset + 3] = (byte) (ulong >> 24 & 0xff);
106: }
107:
108: static void writeUShort(byte[] dest, int offset, int ushort) {
109: dest[offset] = (byte) (ushort & 0xff);
110: dest[offset + 1] = (byte) (ushort >> 8 & 0xff);
111: }
112:
113: static void writeSecurityBuffer(byte[] dest, int offset,
114: int bodyOffset, byte[] src) {
115: int length = (src != null) ? src.length : 0;
116: if (length == 0)
117: 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 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: }
|