Module jcifs

Class NtlmPasswordAuthenticator

java.lang.Object
org.codelibs.jcifs.smb.impl.NtlmPasswordAuthenticator
All Implemented Interfaces:
Serializable, Cloneable, Principal, Credentials, CredentialsInternal
Direct Known Subclasses:
Kerb5Authenticator, NtlmNtHashAuthenticator, NtlmPasswordAuthentication

public class NtlmPasswordAuthenticator extends Object implements Principal, CredentialsInternal, Serializable
This class stores and encrypts NTLM user credentials. Contrary to NtlmPasswordAuthentication this does not cause guest authentication when the "guest" username is supplied. Use NtlmPasswordAuthenticator.AuthenticationType instead.
Author:
mbechler
See Also:
  • Constructor Details

    • NtlmPasswordAuthenticator

      public NtlmPasswordAuthenticator()
      Construct anonymous credentials
    • NtlmPasswordAuthenticator

      public NtlmPasswordAuthenticator(NtlmPasswordAuthenticator.AuthenticationType type)
      Create an NtlmPasswordAuthenticator with the specified authentication type.
      Parameters:
      type - the authentication type to use
    • NtlmPasswordAuthenticator

      public NtlmPasswordAuthenticator(String username, String password)
      Create username/password credentials
      Parameters:
      username - the username for authentication
      password - the password for authentication
    • NtlmPasswordAuthenticator

      public NtlmPasswordAuthenticator(String domain, String username, String password)
      Create username/password credentials with specified domain
      Parameters:
      domain - the domain for authentication
      username - the username for authentication
      password - the password for authentication
    • NtlmPasswordAuthenticator

      public NtlmPasswordAuthenticator(String domain, String username, String password, NtlmPasswordAuthenticator.AuthenticationType type)
      Create username/password credentials with specified domain
      Parameters:
      domain - the authentication domain
      username - the username for authentication
      password - the password for authentication
      type - authentication type
    • NtlmPasswordAuthenticator

      protected NtlmPasswordAuthenticator(String userInfo, String defDomain, String defUser, String defPassword)
      Create an NtlmPasswordAuthenticator from user info with defaults.
      Parameters:
      userInfo - the user information string
      defDomain - the default domain if not specified
      defUser - the default username if not specified
      defPassword - the default password if not specified
    • NtlmPasswordAuthenticator

      protected NtlmPasswordAuthenticator(String userInfo, String defDomain, String defUser, String defPassword, NtlmPasswordAuthenticator.AuthenticationType type)
      Create authenticator from URL userInfo string
      Parameters:
      userInfo - the userInfo string from URL
      defDomain - the default domain to use if not specified
      defUser - the default username to use if not specified
      defPassword - the default password to use if not specified
      type - the authentication type to use
  • Method Details

    • guessAuthenticationType

      protected NtlmPasswordAuthenticator.AuthenticationType guessAuthenticationType()
      Guess the authentication type based on the username format
      Returns:
      the guessed authentication type
    • unwrap

      public <T extends Credentials> T unwrap(Class<T> t)
      Description copied from interface: Credentials
      Unwrap credentials to a specific type.
      Specified by:
      unwrap in interface Credentials
      Type Parameters:
      T - the credential type to unwrap to
      Parameters:
      t - the class of the credential type to unwrap to
      Returns:
      instance for type, null if the type cannot be unwrapped
    • getSubject

      public Subject getSubject()
      Description copied from interface: CredentialsInternal
      Get the security subject associated with these credentials.
      Specified by:
      getSubject in interface CredentialsInternal
      Returns:
      subject associated with the credentials
    • refresh

      public void refresh() throws CIFSException
      Description copied from interface: CredentialsInternal
      Refresh the credentials.
      Specified by:
      refresh in interface CredentialsInternal
      Throws:
      CIFSException - if refresh fails
    • createContext

      public SSPContext createContext(CIFSContext tc, String targetDomain, String host, byte[] initialToken, boolean doSigning) throws SmbException
      Create an SSP context for authentication.
      Specified by:
      createContext in interface CredentialsInternal
      Parameters:
      tc - the CIFS context
      targetDomain - the target domain for authentication
      host - the target host
      initialToken - initial authentication token, if any
      doSigning - whether message signing should be enabled
      Returns:
      a new SSP authentication context
      Throws:
      SmbException - if context creation fails
      See Also:
    • clone

      public NtlmPasswordAuthenticator clone()
      Description copied from interface: CredentialsInternal
      Create a copy of the credentials.
      Specified by:
      clone in interface CredentialsInternal
      Overrides:
      clone in class Object
      Returns:
      a copy of the credentials
    • cloneInternal

      protected static void cloneInternal(NtlmPasswordAuthenticator cloned, NtlmPasswordAuthenticator toClone)
      Clone internal fields from one authenticator to another.
      Parameters:
      cloned - the target authenticator to copy to
      toClone - the source authenticator to copy from
    • getUserDomain

      public String getUserDomain()
      Returns the domain.
      Specified by:
      getUserDomain in interface Credentials
      Returns:
      the domain the user account is in
    • getSpecifiedUserDomain

      public String getSpecifiedUserDomain()
      Get the original specified user domain
      Returns:
      the original specified user domain
    • getUsername

      public String getUsername()
      Returns the username.
      Returns:
      the username
    • getPassword

      public String getPassword()
      Returns the password in plain text or null if the raw password hashes were used to construct this NtlmPasswordAuthentication object which will be the case when NTLM HTTP Authentication is used. There is no way to retrieve a users password in plain text unless it is supplied by the user at runtime.
      Returns:
      the password
    • getName

      public String getName()
      Return the domain and username in the format: domain\\username. This is equivalent to toString().
      Specified by:
      getName in interface Principal
    • equals

      public boolean equals(Object obj)
      Compares two NtlmPasswordAuthentication objects for equality. Two NtlmPasswordAuthentication objects are equal if their caseless domain and username fields are equal
      Specified by:
      equals in interface Principal
      Overrides:
      equals in class Object
      See Also:
    • hashCode

      public int hashCode()
      Return the upcased username hash code.
      Specified by:
      hashCode in interface Principal
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Return the domain and username in the format: domain\\username. This is equivalent to getName().
      Specified by:
      toString in interface Principal
      Overrides:
      toString in class Object
    • isAnonymous

      public boolean isAnonymous()
      Description copied from interface: Credentials
      Check if these are anonymous credentials.
      Specified by:
      isAnonymous in interface Credentials
      Returns:
      whether these are anonymous credentials
    • isGuest

      public boolean isGuest()
      Description copied from interface: Credentials
      Check if these are guest credentials.
      Specified by:
      isGuest in interface Credentials
      Returns:
      whether these are guest credentials
    • isPreferredMech

      public boolean isPreferredMech(org.bouncycastle.asn1.ASN1ObjectIdentifier mechanism)
      Check if the given mechanism is preferred for this credential
      Parameters:
      mechanism - the mechanism to check
      Returns:
      whether the given mechanism is the preferred one for this credential
    • getAnsiHash

      public byte[] getAnsiHash(CIFSContext tc, byte[] chlng) throws GeneralSecurityException
      Computes the 24 byte ANSI password hash given the 8 byte server challenge.
      Parameters:
      tc - the CIFS context
      chlng - the server challenge
      Returns:
      the hash for the given challenge
      Throws:
      GeneralSecurityException - if a security error occurs
    • getUnicodeHash

      public byte[] getUnicodeHash(CIFSContext tc, byte[] chlng) throws GeneralSecurityException
      Computes the 24 byte Unicode password hash given the 8 byte server challenge.
      Parameters:
      tc - the CIFS context
      chlng - the server challenge
      Returns:
      the hash for the given challenge
      Throws:
      GeneralSecurityException - if a security error occurs
    • getSigningKey

      public byte[] getSigningKey(CIFSContext tc, byte[] chlng) throws SmbException, GeneralSecurityException
      Get the signing key for this authentication
      Parameters:
      tc - the CIFS context
      chlng - the server challenge
      Returns:
      the signing key
      Throws:
      SmbException - if an SMB error occurs
      GeneralSecurityException - if a security error occurs
    • getUserSessionKey

      public byte[] getUserSessionKey(CIFSContext tc, byte[] chlng)
      Returns the effective user session key.
      Parameters:
      tc - the CIFS context
      chlng - The server challenge.
      Returns:
      A byte[] containing the effective user session key, used in SMB MAC signing and NTLMSSP signing and sealing.
    • getUserSessionKey

      public void getUserSessionKey(CIFSContext tc, byte[] chlng, byte[] dest, int offset) throws SmbException
      Calculates the effective user session key.
      Parameters:
      tc - context to use
      chlng - The server challenge.
      dest - The destination array in which the user session key will be placed.
      offset - The offset in the destination array at which the session key will start.
      Throws:
      SmbException - if an SMB error occurs
    • getNTHash

      protected byte[] getNTHash()
      Get the NT hash of the password
      Returns:
      the NT hash