Class NTLMEngineImpl
- java.lang.Object
-
- org.apache.hc.client5.http.impl.auth.NTLMEngineImpl
-
- All Implemented Interfaces:
NTLMEngine
final class NTLMEngineImpl extends java.lang.Object implements NTLMEngine
Provides an implementation for NTLMv1, NTLMv2, and NTLM2 Session forms of the NTLM authentication protocol.- Since:
- 4.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classNTLMEngineImpl.CipherGen(package private) static classNTLMEngineImpl.Handle(package private) static classNTLMEngineImpl.HMACMD5Cryptography support - HMACMD5 - algorithmically based on various web resources by Karl Wright(package private) static classNTLMEngineImpl.MD4Cryptography support - MD4.(package private) static classNTLMEngineImpl.Mode(package private) static classNTLMEngineImpl.NTLMMessageNTLM message generation, base class(package private) static classNTLMEngineImpl.Type1MessageType 1 message assembly class(package private) static classNTLMEngineImpl.Type2MessageType 2 message class(package private) static classNTLMEngineImpl.Type3MessageType 3 message assembly class
-
Field Summary
Fields Modifier and Type Field Description private static java.nio.charset.CharsetDEFAULT_CHARSETCharacter encoding(package private) static intFLAG_DOMAIN_PRESENT(package private) static intFLAG_REQUEST_128BIT_KEY_EXCH(package private) static intFLAG_REQUEST_56BIT_ENCRYPTION(package private) static intFLAG_REQUEST_ALWAYS_SIGN(package private) static intFLAG_REQUEST_EXPLICIT_KEY_EXCH(package private) static intFLAG_REQUEST_LAN_MANAGER_KEY(package private) static intFLAG_REQUEST_NTLM2_SESSION(package private) static intFLAG_REQUEST_NTLMv1(package private) static intFLAG_REQUEST_OEM_ENCODING(package private) static intFLAG_REQUEST_SEAL(package private) static intFLAG_REQUEST_SIGN(package private) static intFLAG_REQUEST_TARGET(package private) static intFLAG_REQUEST_UNICODE_ENCODING(package private) static intFLAG_REQUEST_VERSION(package private) static intFLAG_TARGETINFO_PRESENT(package private) static intFLAG_WORKSTATION_PRESENTprivate static byte[]MAGIC_TLS_SERVER_ENDPOINT(package private) static intMSV_AV_CHANNEL_BINDINGS(package private) static intMSV_AV_DNS_COMPUTER_NAME(package private) static intMSV_AV_DNS_DOMAIN_NAME(package private) static intMSV_AV_DNS_TREE_NAME(package private) static intMSV_AV_EOL(package private) static intMSV_AV_FLAGS(package private) static intMSV_AV_FLAGS_ACCOUNT_AUTH_CONSTAINED(package private) static intMSV_AV_FLAGS_MIC(package private) static intMSV_AV_FLAGS_UNTRUSTED_TARGET_SPN(package private) static intMSV_AV_NB_COMPUTER_NAME(package private) static intMSV_AV_NB_DOMAIN_NAME(package private) static intMSV_AV_SINGLE_HOST(package private) static intMSV_AV_TARGET_NAME(package private) static intMSV_AV_TIMESTAMPprivate static java.security.SecureRandomRND_GENSecure random generatorprivate static byte[]SEAL_MAGIC_CLIENTprivate static byte[]SEAL_MAGIC_SERVERprivate static byte[]SIGN_MAGIC_CLIENTprivate static byte[]SIGN_MAGIC_SERVERprivate static byte[]SIGNATUREThe signature string as bytes in the default encodingprivate static java.lang.StringTYPE_1_MESSAGEprivate static java.nio.charset.CharsetUNICODE_LITTLE_UNMARKEDUnicode encoding
-
Constructor Summary
Constructors Constructor Description NTLMEngineImpl()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static byte[]createBlob(byte[] clientChallenge, byte[] targetInformation, byte[] timestamp)Creates the NTLMv2 blob from the given target information block and client challenge.private static java.security.KeycreateDESKey(byte[] bytes, int offset)Creates a DES encryption key from the given key material.private static voidencodeLong(byte[] buf, int offset, int value)private static byte[]encodeLong(int value)(package private) static intF(int x, int y, int z)(package private) static intG(int x, int y, int z)java.lang.StringgenerateType1Msg(java.lang.String domain, java.lang.String workstation)Generates a Type1 message given the domain and workstation.java.lang.StringgenerateType3Msg(java.lang.String username, char[] password, java.lang.String domain, java.lang.String workstation, java.lang.String challenge)Generates a Type3 message given the user credentials and the authentication challenge.private static java.nio.charset.CharsetgetCharset(int flags)Find the character set based on the flags.(package private) static java.security.MessageDigestgetMD5()private static byte[]getNullTerminatedAsciiString(java.lang.String source)(package private) static java.lang.StringgetResponseFor(java.lang.String message, java.lang.String username, char[] password, java.lang.String host, java.lang.String domain)Returns the response for the given message.(package private) static java.lang.StringgetResponseFor(java.lang.String message, java.lang.String username, char[] password, java.lang.String host, java.lang.String domain, java.security.cert.Certificate peerServerCertificate)Returns the response for the given message.(package private) static java.lang.StringgetType1Message(java.lang.String host, java.lang.String domain)Creates the first message (type 1 message) in the NTLM authentication sequence.(package private) static java.lang.StringgetType3Message(java.lang.String user, char[] password, java.lang.String host, java.lang.String domain, byte[] nonce, int type2Flags, java.lang.String target, byte[] targetInformation)Creates the type 3 message using the given server nonce.(package private) static java.lang.StringgetType3Message(java.lang.String user, char[] password, java.lang.String host, java.lang.String domain, byte[] nonce, int type2Flags, java.lang.String target, byte[] targetInformation, java.security.cert.Certificate peerServerCertificate, byte[] type1Message, byte[] type2Message)Creates the type 3 message using the given server nonce.(package private) static intH(int x, int y, int z)(package private) static byte[]hmacMD5(byte[] value, byte[] key)Calculates HMAC-MD5private static byte[]lmHash(char[] password)Creates the LM Hash of the user's password.private static byte[]lmResponse(byte[] hash, byte[] challenge)Creates the LM Response from the given hash and Type 2 challenge.private static byte[]lmv2Hash(java.lang.String domain, java.lang.String user, byte[] ntlmHash)Creates the LMv2 Hash of the user's password.private static byte[]lmv2Response(byte[] hash, byte[] challenge, byte[] clientData)Creates the LMv2 Response from the given hash, client data, and Type 2 challenge.private static byte[]makeRandomChallenge(java.util.Random random)Calculate a challenge blockprivate static byte[]makeSecondaryKey(java.util.Random random)Calculate a 16-byte secondary key(package private) static byte[]ntlm2SessionResponse(byte[] ntlmHash, byte[] challenge, byte[] clientChallenge)Calculates the NTLM2 Session Response for the given challenge, using the specified password and client challenge.private static byte[]ntlmHash(char[] password)Creates the NTLM Hash of the user's password.private static byte[]ntlmv2Hash(java.lang.String domain, java.lang.String user, byte[] ntlmHash)Creates the NTLMv2 Hash of the user's password.private static voidoddParity(byte[] bytes)Applies odd parity to the given byte array.(package private) static byte[]RC4(byte[] value, byte[] key)Calculates RC4private static byte[]readSecurityBuffer(byte[] src, int index)private static intreadULong(byte[] src, int index)private static intreadUShort(byte[] src, int index)(package private) static introtintlft(int val, int numbits)(package private) static voidwriteULong(byte[] buffer, int value, int offset)(package private) static voidwriteUShort(byte[] buffer, int value, int offset)
-
-
-
Field Detail
-
UNICODE_LITTLE_UNMARKED
private static final java.nio.charset.Charset UNICODE_LITTLE_UNMARKED
Unicode encoding
-
DEFAULT_CHARSET
private static final java.nio.charset.Charset DEFAULT_CHARSET
Character encoding
-
FLAG_REQUEST_UNICODE_ENCODING
static final int FLAG_REQUEST_UNICODE_ENCODING
- See Also:
- Constant Field Values
-
FLAG_REQUEST_OEM_ENCODING
static final int FLAG_REQUEST_OEM_ENCODING
- See Also:
- Constant Field Values
-
FLAG_REQUEST_TARGET
static final int FLAG_REQUEST_TARGET
- See Also:
- Constant Field Values
-
FLAG_REQUEST_SIGN
static final int FLAG_REQUEST_SIGN
- See Also:
- Constant Field Values
-
FLAG_REQUEST_SEAL
static final int FLAG_REQUEST_SEAL
- See Also:
- Constant Field Values
-
FLAG_REQUEST_LAN_MANAGER_KEY
static final int FLAG_REQUEST_LAN_MANAGER_KEY
- See Also:
- Constant Field Values
-
FLAG_REQUEST_NTLMv1
static final int FLAG_REQUEST_NTLMv1
- See Also:
- Constant Field Values
-
FLAG_DOMAIN_PRESENT
static final int FLAG_DOMAIN_PRESENT
- See Also:
- Constant Field Values
-
FLAG_WORKSTATION_PRESENT
static final int FLAG_WORKSTATION_PRESENT
- See Also:
- Constant Field Values
-
FLAG_REQUEST_ALWAYS_SIGN
static final int FLAG_REQUEST_ALWAYS_SIGN
- See Also:
- Constant Field Values
-
FLAG_REQUEST_NTLM2_SESSION
static final int FLAG_REQUEST_NTLM2_SESSION
- See Also:
- Constant Field Values
-
FLAG_REQUEST_VERSION
static final int FLAG_REQUEST_VERSION
- See Also:
- Constant Field Values
-
FLAG_TARGETINFO_PRESENT
static final int FLAG_TARGETINFO_PRESENT
- See Also:
- Constant Field Values
-
FLAG_REQUEST_128BIT_KEY_EXCH
static final int FLAG_REQUEST_128BIT_KEY_EXCH
- See Also:
- Constant Field Values
-
FLAG_REQUEST_EXPLICIT_KEY_EXCH
static final int FLAG_REQUEST_EXPLICIT_KEY_EXCH
- See Also:
- Constant Field Values
-
FLAG_REQUEST_56BIT_ENCRYPTION
static final int FLAG_REQUEST_56BIT_ENCRYPTION
- See Also:
- Constant Field Values
-
MSV_AV_EOL
static final int MSV_AV_EOL
- See Also:
- Constant Field Values
-
MSV_AV_NB_COMPUTER_NAME
static final int MSV_AV_NB_COMPUTER_NAME
- See Also:
- Constant Field Values
-
MSV_AV_NB_DOMAIN_NAME
static final int MSV_AV_NB_DOMAIN_NAME
- See Also:
- Constant Field Values
-
MSV_AV_DNS_COMPUTER_NAME
static final int MSV_AV_DNS_COMPUTER_NAME
- See Also:
- Constant Field Values
-
MSV_AV_DNS_DOMAIN_NAME
static final int MSV_AV_DNS_DOMAIN_NAME
- See Also:
- Constant Field Values
-
MSV_AV_DNS_TREE_NAME
static final int MSV_AV_DNS_TREE_NAME
- See Also:
- Constant Field Values
-
MSV_AV_FLAGS
static final int MSV_AV_FLAGS
- See Also:
- Constant Field Values
-
MSV_AV_TIMESTAMP
static final int MSV_AV_TIMESTAMP
- See Also:
- Constant Field Values
-
MSV_AV_SINGLE_HOST
static final int MSV_AV_SINGLE_HOST
- See Also:
- Constant Field Values
-
MSV_AV_TARGET_NAME
static final int MSV_AV_TARGET_NAME
- See Also:
- Constant Field Values
-
MSV_AV_CHANNEL_BINDINGS
static final int MSV_AV_CHANNEL_BINDINGS
- See Also:
- Constant Field Values
-
MSV_AV_FLAGS_ACCOUNT_AUTH_CONSTAINED
static final int MSV_AV_FLAGS_ACCOUNT_AUTH_CONSTAINED
- See Also:
- Constant Field Values
-
MSV_AV_FLAGS_MIC
static final int MSV_AV_FLAGS_MIC
- See Also:
- Constant Field Values
-
MSV_AV_FLAGS_UNTRUSTED_TARGET_SPN
static final int MSV_AV_FLAGS_UNTRUSTED_TARGET_SPN
- See Also:
- Constant Field Values
-
RND_GEN
private static final java.security.SecureRandom RND_GEN
Secure random generator
-
SIGNATURE
private static final byte[] SIGNATURE
The signature string as bytes in the default encoding
-
SIGN_MAGIC_SERVER
private static final byte[] SIGN_MAGIC_SERVER
-
SIGN_MAGIC_CLIENT
private static final byte[] SIGN_MAGIC_CLIENT
-
SEAL_MAGIC_SERVER
private static final byte[] SEAL_MAGIC_SERVER
-
SEAL_MAGIC_CLIENT
private static final byte[] SEAL_MAGIC_CLIENT
-
MAGIC_TLS_SERVER_ENDPOINT
private static final byte[] MAGIC_TLS_SERVER_ENDPOINT
-
TYPE_1_MESSAGE
private static final java.lang.String TYPE_1_MESSAGE
-
-
Method Detail
-
getNullTerminatedAsciiString
private static byte[] getNullTerminatedAsciiString(java.lang.String source)
-
getResponseFor
static java.lang.String getResponseFor(java.lang.String message, java.lang.String username, char[] password, java.lang.String host, java.lang.String domain) throws NTLMEngineExceptionReturns the response for the given message.- Parameters:
message- the message that was received from the server.username- the username to authenticate with.password- the password to authenticate with.host- The host.domain- the NT domain to authenticate in.- Returns:
- The response.
- Throws:
NTLMEngineException
-
getResponseFor
static java.lang.String getResponseFor(java.lang.String message, java.lang.String username, char[] password, java.lang.String host, java.lang.String domain, java.security.cert.Certificate peerServerCertificate) throws NTLMEngineExceptionReturns the response for the given message.- Parameters:
message- the message that was received from the server.username- the username to authenticate with.password- the password to authenticate with.host- The host.domain- the NT domain to authenticate in.- Returns:
- The response.
- Throws:
NTLMEngineException
-
getType1Message
static java.lang.String getType1Message(java.lang.String host, java.lang.String domain)Creates the first message (type 1 message) in the NTLM authentication sequence. This message includes the user name, domain and host for the authentication session.- Parameters:
host- the computer name of the host requesting authentication.domain- The domain to authenticate with.- Returns:
- String the message to add to the HTTP request header.
-
getType3Message
static java.lang.String getType3Message(java.lang.String user, char[] password, java.lang.String host, java.lang.String domain, byte[] nonce, int type2Flags, java.lang.String target, byte[] targetInformation) throws NTLMEngineExceptionCreates the type 3 message using the given server nonce. The type 3 message includes all the information for authentication, host, domain, username and the result of encrypting the nonce sent by the server using the user's password as the key.- Parameters:
user- The user name. This should not include the domain name.password- The password.host- The host that is originating the authentication request.domain- The domain to authenticate within.nonce- the 8 byte array the server sent.- Returns:
- The type 3 message.
- Throws:
NTLMEngineException- IfType3Message(String, String, String, char[], byte[], int, String, byte[])fails.
-
getType3Message
static java.lang.String getType3Message(java.lang.String user, char[] password, java.lang.String host, java.lang.String domain, byte[] nonce, int type2Flags, java.lang.String target, byte[] targetInformation, java.security.cert.Certificate peerServerCertificate, byte[] type1Message, byte[] type2Message) throws NTLMEngineExceptionCreates the type 3 message using the given server nonce. The type 3 message includes all the information for authentication, host, domain, username and the result of encrypting the nonce sent by the server using the user's password as the key.- Parameters:
user- The user name. This should not include the domain name.password- The password.host- The host that is originating the authentication request.domain- The domain to authenticate within.nonce- the 8 byte array the server sent.- Returns:
- The type 3 message.
- Throws:
NTLMEngineException
-
readULong
private static int readULong(byte[] src, int index)
-
readUShort
private static int readUShort(byte[] src, int index)
-
readSecurityBuffer
private static byte[] readSecurityBuffer(byte[] src, int index)
-
makeRandomChallenge
private static byte[] makeRandomChallenge(java.util.Random random)
Calculate a challenge block
-
makeSecondaryKey
private static byte[] makeSecondaryKey(java.util.Random random)
Calculate a 16-byte secondary key
-
hmacMD5
static byte[] hmacMD5(byte[] value, byte[] key)Calculates HMAC-MD5
-
RC4
static byte[] RC4(byte[] value, byte[] key) throws NTLMEngineExceptionCalculates RC4- Throws:
NTLMEngineException
-
ntlm2SessionResponse
static byte[] ntlm2SessionResponse(byte[] ntlmHash, byte[] challenge, byte[] clientChallenge) throws NTLMEngineExceptionCalculates the NTLM2 Session Response for the given challenge, using the specified password and client challenge.- Returns:
- The NTLM2 Session Response. This is placed in the NTLM response field of the Type 3 message; the LM response field contains the client challenge, null-padded to 24 bytes.
- Throws:
NTLMEngineException
-
lmHash
private static byte[] lmHash(char[] password) throws NTLMEngineExceptionCreates the LM Hash of the user's password.- Parameters:
password- The password.- Returns:
- The LM Hash of the given password, used in the calculation of the LM Response.
- Throws:
NTLMEngineException
-
ntlmHash
private static byte[] ntlmHash(char[] password) throws NTLMEngineExceptionCreates the NTLM Hash of the user's password.- Parameters:
password- The password.- Returns:
- The NTLM Hash of the given password, used in the calculation of the NTLM Response and the NTLMv2 and LMv2 Hashes.
- Throws:
NTLMEngineException
-
lmv2Hash
private static byte[] lmv2Hash(java.lang.String domain, java.lang.String user, byte[] ntlmHash) throws NTLMEngineExceptionCreates the LMv2 Hash of the user's password.- Returns:
- The LMv2 Hash, used in the calculation of the NTLMv2 and LMv2 Responses.
- Throws:
NTLMEngineException
-
ntlmv2Hash
private static byte[] ntlmv2Hash(java.lang.String domain, java.lang.String user, byte[] ntlmHash) throws NTLMEngineExceptionCreates the NTLMv2 Hash of the user's password.- Returns:
- The NTLMv2 Hash, used in the calculation of the NTLMv2 and LMv2 Responses.
- Throws:
NTLMEngineException
-
lmResponse
private static byte[] lmResponse(byte[] hash, byte[] challenge) throws NTLMEngineExceptionCreates the LM Response from the given hash and Type 2 challenge.- Parameters:
hash- The LM or NTLM Hash.challenge- The server challenge from the Type 2 message.- Returns:
- The response (either LM or NTLM, depending on the provided hash).
- Throws:
NTLMEngineException
-
lmv2Response
private static byte[] lmv2Response(byte[] hash, byte[] challenge, byte[] clientData)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).
-
encodeLong
private static byte[] encodeLong(int value)
-
encodeLong
private static void encodeLong(byte[] buf, int offset, int value)
-
createBlob
private static byte[] createBlob(byte[] clientChallenge, byte[] targetInformation, 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.
-
createDESKey
private static java.security.Key createDESKey(byte[] bytes, int offset)Creates a DES encryption key from the given key material.- Parameters:
bytes- A byte array containing the DES key material.offset- The offset in the given byte array at which the 7-byte key material starts.- Returns:
- A DES encryption key created from the key material starting at the specified offset in the given byte array.
-
oddParity
private static void oddParity(byte[] bytes)
Applies odd parity to the given byte array.- Parameters:
bytes- The data whose parity bits are to be adjusted for odd parity.
-
getCharset
private static java.nio.charset.Charset getCharset(int flags) throws NTLMEngineExceptionFind the character set based on the flags.- Parameters:
flags- is the flags.- Returns:
- the character set.
- Throws:
NTLMEngineException
-
writeUShort
static void writeUShort(byte[] buffer, int value, int offset)
-
writeULong
static void writeULong(byte[] buffer, int value, int offset)
-
F
static int F(int x, int y, int z)
-
G
static int G(int x, int y, int z)
-
H
static int H(int x, int y, int z)
-
rotintlft
static int rotintlft(int val, int numbits)
-
getMD5
static java.security.MessageDigest getMD5()
-
generateType1Msg
public java.lang.String generateType1Msg(java.lang.String domain, java.lang.String workstation) throws NTLMEngineExceptionDescription copied from interface:NTLMEngineGenerates a Type1 message given the domain and workstation.- Specified by:
generateType1Msgin interfaceNTLMEngine- Parameters:
domain- Optional Windows domain name. Can benull.workstation- Optional Windows workstation name. Can benull.- Returns:
- Type1 message
- Throws:
NTLMEngineException
-
generateType3Msg
public java.lang.String generateType3Msg(java.lang.String username, char[] password, java.lang.String domain, java.lang.String workstation, java.lang.String challenge) throws NTLMEngineExceptionDescription copied from interface:NTLMEngineGenerates a Type3 message given the user credentials and the authentication challenge.- Specified by:
generateType3Msgin interfaceNTLMEngine- Parameters:
username- Windows user namepassword- Passworddomain- Windows domain nameworkstation- Windows workstation namechallenge- Type2 challenge.- Returns:
- Type3 response.
- Throws:
NTLMEngineException
-
-