FileDocCategorySizeDatePackage
NtlmContext.javaAPI DocJCIFS 1.3.17 API6193Tue Oct 18 15:26:24 BST 2011jcifs.smb

NtlmContext

public class NtlmContext extends Object
For initiating NTLM authentication (including NTLMv2). If you want to add NTLMv2 authentication support to something this is what you want to use. See the code for details. Note that JCIFS does not implement the acceptor side of NTLM authentication.

Fields Summary
NtlmPasswordAuthentication
auth
int
ntlmsspFlags
String
workstation
boolean
isEstablished
byte[]
serverChallenge
byte[]
signingKey
String
netbiosName
int
state
jcifs.util.LogStream
log
Constructors Summary
public NtlmContext(NtlmPasswordAuthentication auth, boolean doSigning)


         
        this.auth = auth;
        this.ntlmsspFlags = ntlmsspFlags |
                NtlmFlags.NTLMSSP_REQUEST_TARGET |
                NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 |
                NtlmFlags.NTLMSSP_NEGOTIATE_128;
        if (doSigning) {
            this.ntlmsspFlags |= NtlmFlags.NTLMSSP_NEGOTIATE_SIGN |
                NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
                NtlmFlags.NTLMSSP_NEGOTIATE_KEY_EXCH;
        }
        this.workstation = Type1Message.getDefaultWorkstation();
        log = LogStream.getInstance();
    
Methods Summary
public java.lang.StringgetNetbiosName()

        return netbiosName;
    
private java.lang.StringgetNtlmsspListItem(byte[] type2token, int id0)

        int ri = 58;

        for ( ;; ) {
            int id = Encdec.dec_uint16le(type2token, ri);
            int len = Encdec.dec_uint16le(type2token, ri + 2);
            ri += 4;
            if (id == 0 || (ri + len) > type2token.length) {
                break;
            } else if (id == id0) {
                try {
                    return new String(type2token, ri, len, SmbConstants.UNI_ENCODING);
                } catch (java.io.UnsupportedEncodingException uee) {
                    break;
                }
            }
            ri += len;
        }

        return null;
    
public byte[]getServerChallenge()

        return serverChallenge;
    
public byte[]getSigningKey()

        return signingKey;
    
public byte[]initSecContext(byte[] token, int offset, int len)

        switch (state) {
            case 1:
                Type1Message msg1 = new Type1Message(ntlmsspFlags, auth.getDomain(), workstation);
                token = msg1.toByteArray();

                if (log.level >= 4) {
                    log.println(msg1);
                    if (log.level >= 6)
                        Hexdump.hexdump(log, token, 0, token.length);
                }

                state++;
                break;
            case 2:
                try {
                    Type2Message msg2 = new Type2Message(token);

                    if (log.level >= 4) {
                        log.println(msg2);
                        if (log.level >= 6)
                            Hexdump.hexdump(log, token, 0, token.length);
                    }

                    serverChallenge = msg2.getChallenge();
                    ntlmsspFlags &= msg2.getFlags();

//                  netbiosName = getNtlmsspListItem(token, 0x0001);

                    Type3Message msg3 = new Type3Message(msg2,
                                auth.getPassword(),
                                auth.getDomain(),
                                auth.getUsername(),
                                workstation,
                                ntlmsspFlags);
                    token = msg3.toByteArray();

                    if (log.level >= 4) {
                        log.println(msg3);
                        if (log.level >= 6)
                            Hexdump.hexdump(log, token, 0, token.length);
                    }

                    if ((ntlmsspFlags & NtlmFlags.NTLMSSP_NEGOTIATE_SIGN) != 0)
                        signingKey = msg3.getMasterKey();

                    isEstablished = true;
                    state++;
                    break;
                } catch (Exception e) {
                    throw new SmbException(e.getMessage(), e);
                }
            default:
                throw new SmbException("Invalid state");
        }
        return token;
    
public booleanisEstablished()

        return isEstablished;
    
public java.lang.StringtoString()

        String ret = "NtlmContext[auth=" + auth +
            ",ntlmsspFlags=0x" + Hexdump.toHexString(ntlmsspFlags, 8) +
            ",workstation=" + workstation +
            ",isEstablished=" + isEstablished +
            ",state=" + state +
            ",serverChallenge=";
        if (serverChallenge == null) {
            ret += "null";
        } else {
            ret += Hexdump.toHexString(serverChallenge, 0, serverChallenge.length * 2);
        }
        ret += ",signingKey=";
        if (signingKey == null) {
            ret += "null";
        } else {
            ret += Hexdump.toHexString(signingKey, 0, signingKey.length * 2);
        }
        ret += "]";
        return ret;