org.apache.mina.proxy.handlers.http.ntlm
Class NTLMUtilities

java.lang.Object
  extended by org.apache.mina.proxy.handlers.http.ntlm.NTLMUtilities
All Implemented Interfaces:
NTLMConstants

public class NTLMUtilities
extends Object
implements NTLMConstants

NTLMUtilities.java - NTLM functions used for authentication and unit testing.

Since:
MINA 2.0.0-M3
Author:
Apache MINA Project

Field Summary
 
Fields inherited from interface org.apache.mina.proxy.handlers.http.ntlm.NTLMConstants
DEFAULT_FLAGS, DEFAULT_OS_VERSION, FLAG_NEGOTIATE_128_BIT_ENCRYPTION, FLAG_NEGOTIATE_56_BIT_ENCRYPTION, FLAG_NEGOTIATE_ALWAYS_SIGN, FLAG_NEGOTIATE_ANONYMOUS, FLAG_NEGOTIATE_DATAGRAM_STYLE, FLAG_NEGOTIATE_DOMAIN_SUPPLIED, FLAG_NEGOTIATE_KEY_EXCHANGE, FLAG_NEGOTIATE_LAN_MANAGER_KEY, FLAG_NEGOTIATE_LOCAL_CALL, FLAG_NEGOTIATE_NTLM, FLAG_NEGOTIATE_NTLM2, FLAG_NEGOTIATE_OEM, FLAG_NEGOTIATE_SEAL, FLAG_NEGOTIATE_SIGN, FLAG_NEGOTIATE_TARGET_INFO, FLAG_NEGOTIATE_UNICODE, FLAG_NEGOTIATE_WORKSTATION_SUPPLIED, FLAG_REQUEST_SERVER_AUTH_REALM, FLAG_TARGET_TYPE_DOMAIN, FLAG_TARGET_TYPE_SERVER, FLAG_TARGET_TYPE_SHARE, FLAG_UNIDENTIFIED_1, FLAG_UNIDENTIFIED_10, FLAG_UNIDENTIFIED_11, FLAG_UNIDENTIFIED_2, FLAG_UNIDENTIFIED_3, FLAG_UNIDENTIFIED_4, FLAG_UNIDENTIFIED_5, FLAG_UNIDENTIFIED_6, FLAG_UNIDENTIFIED_7, FLAG_UNIDENTIFIED_8, FLAG_UNIDENTIFIED_9, MESSAGE_TYPE_1, MESSAGE_TYPE_2, MESSAGE_TYPE_3, NTLM_SIGNATURE, TARGET_INFORMATION_SUBBLOCK_DNS_DOMAIN_NAME_TYPE, TARGET_INFORMATION_SUBBLOCK_DOMAIN_TYPE, TARGET_INFORMATION_SUBBLOCK_FQDNS_HOSTNAME_TYPE, TARGET_INFORMATION_SUBBLOCK_PARENT_DNS_DOMAIN_NAME_TYPE, TARGET_INFORMATION_SUBBLOCK_SERVER_TYPE, TARGET_INFORMATION_SUBBLOCK_TERMINATOR_TYPE
 
Constructor Summary
NTLMUtilities()
           
 
Method Summary
static byte[] createType1Message(String workStation, String domain, Integer customFlags, byte[] osVersion)
          see http://davenport.sourceforge.net/ntlm.html#theType1Message
static byte[] createType3Message(String user, String password, byte[] challenge, String target, String workstation, Integer serverFlags, byte[] osVersion)
           
static byte[] extractChallengeFromType2Message(byte[] msg)
          Extracts the NTLM challenge from the type 2 message as an 8 byte array.
static int extractFlagsFromType2Message(byte[] msg)
          Extracts the NTLM flags from the type 2 message.
static byte[] extractTargetInfoFromType2Message(byte[] msg, Integer msgFlags)
          Extracts the target information block from the type 2 message.
static String extractTargetNameFromType2Message(byte[] msg, Integer msgFlags)
          Extracts the target name from the type 2 message.
static byte[] getOsVersion()
          Tries to return a valid OS version on Windows systems.
static void printTargetInformationBlockFromType2Message(byte[] msg, Integer msgFlags, PrintWriter out)
          Prints to the PrintWriter the target information block extracted from the type 2 message.
static byte[] readSecurityBufferTarget(byte[] msg, int securityBufferOffset)
          Reads the byte array described by the security buffer stored at the securityBufferOffset offset.
static void writeOSVersion(byte majorVersion, byte minorVersion, short buildNumber, byte[] b, int offset)
          Writes the Windows OS version passed in as three byte values (majorVersion.minorVersion.buildNumber) to the given byte array at offset.
static byte[] writeSecurityBuffer(short length, int bufferOffset)
           
static void writeSecurityBuffer(short length, short allocated, int bufferOffset, byte[] b, int offset)
          Writes a security buffer to the given array b at offset offset.
static int writeSecurityBufferAndUpdatePointer(ByteArrayOutputStream baos, short len, int pointer)
          Writes a security buffer and returns the pointer of the position where to write the next security buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NTLMUtilities

public NTLMUtilities()
Method Detail

writeSecurityBuffer

public static final byte[] writeSecurityBuffer(short length,
                                               int bufferOffset)
See Also:
writeSecurityBuffer(short, short, int, byte[], int)

writeSecurityBuffer

public static final void writeSecurityBuffer(short length,
                                             short allocated,
                                             int bufferOffset,
                                             byte[] b,
                                             int offset)
Writes a security buffer to the given array b at offset offset. A security buffer defines a pointer to an area in the data that defines some data with a variable length. This allows to have a semi-fixed length header thus making a little bit easier the decoding process in the NTLM protocol.

Parameters:
length - the length of the security buffer
allocated - the allocated space for the security buffer (should be greater or equal to length
bufferOffset - the offset from the main array where the currently defined security buffer will be written
b - the buffer in which we write the security buffer
offset - the offset at which to write to the b buffer

writeOSVersion

public static final void writeOSVersion(byte majorVersion,
                                        byte minorVersion,
                                        short buildNumber,
                                        byte[] b,
                                        int offset)
Writes the Windows OS version passed in as three byte values (majorVersion.minorVersion.buildNumber) to the given byte array at offset.

Parameters:
majorVersion - the major version number
minorVersion - the minor version number
buildNumber - the build number
b - the target byte array
offset - the offset at which to write in the array

getOsVersion

public static final byte[] getOsVersion()
Tries to return a valid OS version on Windows systems. If it fails to do so or if we're running on another OS then a fake Windows XP OS version is returned because the protocol uses it.

Returns:
a NTLM OS version byte buffer

createType1Message

public static final byte[] createType1Message(String workStation,
                                              String domain,
                                              Integer customFlags,
                                              byte[] osVersion)
see http://davenport.sourceforge.net/ntlm.html#theType1Message

Parameters:
workStation - the workstation name
domain - the domain name
customFlags - custom flags, if null then NTLMConstants.DEFAULT_CONSTANTS is used
osVersion - the os version of the client, if null then NTLMConstants.DEFAULT_OS_VERSION is used
Returns:
the type 1 message

writeSecurityBufferAndUpdatePointer

public static final int writeSecurityBufferAndUpdatePointer(ByteArrayOutputStream baos,
                                                            short len,
                                                            int pointer)
                                                     throws IOException
Writes a security buffer and returns the pointer of the position where to write the next security buffer.

Parameters:
baos - the stream where the security buffer is written
len - the length of the security buffer
pointer - the position where the security buffer can be written
Returns:
the position where the next security buffer will be written
Throws:
IOException - if writing to the ByteArrayOutputStream fails

extractChallengeFromType2Message

public static final byte[] extractChallengeFromType2Message(byte[] msg)
Extracts the NTLM challenge from the type 2 message as an 8 byte array.

Parameters:
msg - the type 2 message byte array
Returns:
the challenge

extractFlagsFromType2Message

public static final int extractFlagsFromType2Message(byte[] msg)
Extracts the NTLM flags from the type 2 message.

Parameters:
msg - the type 2 message byte array
Returns:
the proxy flags as an int

readSecurityBufferTarget

public static final byte[] readSecurityBufferTarget(byte[] msg,
                                                    int securityBufferOffset)
Reads the byte array described by the security buffer stored at the securityBufferOffset offset.

Parameters:
msg - the message where to read the security buffer and it's value
securityBufferOffset - the offset at which to read the security buffer
Returns:
a new byte array holding the data pointed by the security buffer

extractTargetNameFromType2Message

public static final String extractTargetNameFromType2Message(byte[] msg,
                                                             Integer msgFlags)
                                                      throws UnsupportedEncodingException
Extracts the target name from the type 2 message.

Parameters:
msg - the type 2 message byte array
msgFlags - the flags if null then flags are extracted from the type 2 message
Returns:
the target name
Throws:
UnsupportedEncodingException - if unable to use the needed UTF-16LE or ASCII charsets

extractTargetInfoFromType2Message

public static final byte[] extractTargetInfoFromType2Message(byte[] msg,
                                                             Integer msgFlags)
Extracts the target information block from the type 2 message.

Parameters:
msg - the type 2 message byte array
msgFlags - the flags if null then flags are extracted from the type 2 message
Returns:
the target info

printTargetInformationBlockFromType2Message

public static final void printTargetInformationBlockFromType2Message(byte[] msg,
                                                                     Integer msgFlags,
                                                                     PrintWriter out)
                                                              throws UnsupportedEncodingException
Prints to the PrintWriter the target information block extracted from the type 2 message.

Parameters:
msg - the type 2 message
msgFlags - the flags if null then flags are extracted from the type 2 message
out - the output target for the information
Throws:
UnsupportedEncodingException - if unable to use the needed UTF-16LE or ASCII charsets

createType3Message

public static final byte[] createType3Message(String user,
                                              String password,
                                              byte[] challenge,
                                              String target,
                                              String workstation,
                                              Integer serverFlags,
                                              byte[] osVersion)
Parameters:
user - the user name
password - the user password
challenge - the challenge response
target - the target name
workstation - the client workstation's name
serverFlags - the flags set by the client
osVersion - the os version of the client
Returns:
the type 3 message
See Also:
http://davenport.sourceforge.net/ntlm.html#theType3Message


Copyright © 2004-2011 Apache MINA Project. All Rights Reserved.