net.sourceforge.jtds.jdbc

Class NtlmAuth


public class NtlmAuth
extends java.lang.Object

This class calculates the two "responses" to the nonce supplied by the server as a part of NTLM authentication. Much gratitude to the authors of this page, esp. for NTLMv2 info: http://davenport.sourceforge.net/ntlm.html
Version:
$Id: NtlmAuth.java,v 1.7 2006/06/23 18:00:56 matt_brinkley Exp $
Author:
Matt Brinkley

Method Summary

static byte[]
answerLmChallenge(String pwd, byte[] nonce)
static byte[]
answerLmv2Challenge(String domain, String user, String password, byte[] nonce, byte[] clientNonce)
static byte[]
answerNtChallenge(String password, byte[] nonce)
static byte[]
answerNtlmv2Challenge(String domain, String user, String password, byte[] nonce, byte[] targetInfo, byte[] clientNonce)
static byte[]
answerNtlmv2Challenge(String domain, String user, String password, byte[] nonce, byte[] targetInfo, byte[] clientNonce, byte[] timestamp)
static byte[]
answerNtlmv2Challenge(String domain, String user, String password, byte[] nonce, byte[] targetInfo, byte[] clientNonce, long now)
private static byte[]
convertPassword(String password)
Used by answerNtlmChallenge.
private static byte[]
createBlob(byte[] targetInformation, byte[] clientChallenge, byte[] timestamp)
Creates the NTLMv2 blob from the given target information block and client challenge.
static byte[]
createTimestamp(long time)
Creates a timestamp in the format used in NTLMv2 responses.
private static byte[]
encryptNonce(byte[] key, byte[] nonce)
private static byte[]
hmacMD5(byte[] data, byte[] key)
Calculates the HMAC-MD5 hash of the given data using the specified hashing key.
private static byte[]
lmv2Response(byte[] hash, byte[] clientData, byte[] challenge)
Creates the LMv2 Response from the given hash, client data, and Type 2 challenge.
private static byte[]
makeDESkey(byte[] buf, int off)
Turns a 7-byte DES key into an 8-byte one by adding parity bits.
private static byte[]
md5(byte[] data)
private static byte[]
ntHash(String password)
Creates the md4 hash of the unicode password.
private static byte[]
ntv2Hash(String domain, String user, String password)

Method Details

answerLmChallenge

public static byte[] answerLmChallenge(String pwd,
                                       byte[] nonce)
            throws UnsupportedEncodingException

answerLmv2Challenge

public static byte[] answerLmv2Challenge(String domain,
                                         String user,
                                         String password,
                                         byte[] nonce,
                                         byte[] clientNonce)
            throws UnsupportedEncodingException

answerNtChallenge

public static byte[] answerNtChallenge(String password,
                                       byte[] nonce)
            throws UnsupportedEncodingException

answerNtlmv2Challenge

public static byte[] answerNtlmv2Challenge(String domain,
                                           String user,
                                           String password,
                                           byte[] nonce,
                                           byte[] targetInfo,
                                           byte[] clientNonce)
            throws UnsupportedEncodingException

answerNtlmv2Challenge

public static byte[] answerNtlmv2Challenge(String domain,
                                           String user,
                                           String password,
                                           byte[] nonce,
                                           byte[] targetInfo,
                                           byte[] clientNonce,
                                           byte[] timestamp)
            throws UnsupportedEncodingException

answerNtlmv2Challenge

public static byte[] answerNtlmv2Challenge(String domain,
                                           String user,
                                           String password,
                                           byte[] nonce,
                                           byte[] targetInfo,
                                           byte[] clientNonce,
                                           long now)
            throws UnsupportedEncodingException

convertPassword

private static byte[] convertPassword(String password)
            throws UnsupportedEncodingException
Used by answerNtlmChallenge. We need the password converted to caps, narrowed and padded/truncated to 14 chars...

createBlob

private static byte[] createBlob(byte[] targetInformation,
                                 byte[] clientChallenge,
                                 byte[] timestamp)
Creates the NTLMv2 blob from the given target information block and client challenge.
Parameters:
targetInformation - The target information block from the Type 2 message.
clientChallenge - The random 8-byte client challenge.
Returns:
The blob, used in the calculation of the NTLMv2 Response.

createTimestamp

public static byte[] createTimestamp(long time)
Creates a timestamp in the format used in NTLMv2 responses. Public so it could be unit tested.
Parameters:
time - current time, as returned from System.currentTimeMillis
Returns:
little-endian byte array of number of tenths of microseconds since Jan 1, 1601

encryptNonce

private static byte[] encryptNonce(byte[] key,
                                   byte[] nonce)

hmacMD5

private static byte[] hmacMD5(byte[] data,
                              byte[] key)
Calculates the HMAC-MD5 hash of the given data using the specified hashing key.
Parameters:
data - The data for which the hash will be calculated.
key - The hashing key.
Returns:
The HMAC-MD5 hash of the given data.

lmv2Response

private static byte[] lmv2Response(byte[] hash,
                                   byte[] clientData,
                                   byte[] challenge)
Creates the LMv2 Response from the given hash, client data, and Type 2 challenge.
Parameters:
hash - The NTLMv2 Hash.
clientData - The client data (blob or client challenge).
challenge - The server challenge from the Type 2 message.
Returns:
The response (either NTLMv2 or LMv2, depending on the client data).

makeDESkey

private static byte[] makeDESkey(byte[] buf,
                                 int off)
Turns a 7-byte DES key into an 8-byte one by adding parity bits. All implementations of DES seem to want an 8-byte key.

md5

private static byte[] md5(byte[] data)

ntHash

private static byte[] ntHash(String password)
            throws UnsupportedEncodingException
Creates the md4 hash of the unicode password. This is used as the DES key when encrypting the nonce for NTLM challenge-response

ntv2Hash

private static byte[] ntv2Hash(String domain,
                               String user,
                               String password)
            throws UnsupportedEncodingException

Generated on September 18 2009