2728N/A/*
2728N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2728N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2728N/A *
2728N/A * This code is free software; you can redistribute it and/or modify it
2728N/A * under the terms of the GNU General Public License version 2 only, as
2728N/A * published by the Free Software Foundation. Oracle designates this
2728N/A * particular file as subject to the "Classpath" exception as provided
2728N/A * by Oracle in the LICENSE file that accompanied this code.
2728N/A *
2728N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2728N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2728N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2728N/A * version 2 for more details (a copy is included in the LICENSE file that
2728N/A * accompanied this code).
2728N/A *
2728N/A * You should have received a copy of the GNU General Public License version
2728N/A * 2 along with this work; if not, write to the Free Software Foundation,
2728N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2728N/A *
2728N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2728N/A * or visit www.oracle.com if you need additional information or have any
2728N/A * questions.
2728N/A */
2728N/A
2728N/Apackage com.sun.security.ntlm;
2728N/A
2728N/Aimport java.math.BigInteger;
2728N/Aimport java.util.Arrays;
2728N/Aimport java.util.Date;
2728N/Aimport java.util.Locale;
2728N/A
2728N/A/**
2728N/A * The NTLM client. Not multi-thread enabled.<p>
2728N/A * Example:
2728N/A * <pre>
2728N/A * Client client = new Client(null, "host", "dummy",
2728N/A * "REALM", "t0pSeCr3t".toCharArray());
2728N/A * byte[] type1 = client.type1();
2728N/A * // Send type1 to server and receive response as type2
2728N/A * byte[] type3 = client.type3(type2, nonce);
2728N/A * // Send type3 to server
2728N/A * </pre>
2728N/A */
2728N/Apublic final class Client extends NTLM {
2728N/A final private String hostname;
2728N/A final private String username;
2728N/A
2728N/A private String domain; // might be updated by Type 2 msg
2728N/A private byte[] pw1, pw2;
2728N/A
2728N/A /**
2728N/A * Creates an NTLM Client instance.
2728N/A * @param version the NTLM version to use, which can be:
2728N/A * <ul>
2728N/A * <li>LM/NTLM: Original NTLM v1
2728N/A * <li>LM: Original NTLM v1, LM only
2728N/A * <li>NTLM: Original NTLM v1, NTLM only
2728N/A * <li>NTLM2: NTLM v1 with Client Challenge
2728N/A * <li>LMv2/NTLMv2: NTLM v2
2728N/A * <li>LMv2: NTLM v2, LM only
2728N/A * <li>NTLMv2: NTLM v2, NTLM only
2728N/A * </ul>
2728N/A * If null, "LMv2/NTLMv2" will be used.
2728N/A * @param hostname hostname of the client, can be null
2728N/A * @param username username to be authenticated, must not be null
2728N/A * @param domain domain of {@code username}, can be null
2728N/A * @param password password for {@code username}, must not be not null.
2728N/A * This method does not make any modification to this parameter, it neither
2728N/A * needs to access the content of this parameter after this method call,
2728N/A * so you are free to modify or nullify this parameter after this call.
4357N/A * @throws NTLMException if {@code username} or {@code password} is null,
4357N/A * or {@code version} is illegal.
4357N/A *
2728N/A */
2728N/A public Client(String version, String hostname, String username,
2728N/A String domain, char[] password) throws NTLMException {
2728N/A super(version);
2728N/A if ((username == null || password == null)) {
4357N/A throw new NTLMException(NTLMException.PROTOCOL,
4357N/A "username/password cannot be null");
2728N/A }
2728N/A this.hostname = hostname;
2728N/A this.username = username;
2728N/A this.domain = domain;
2728N/A this.pw1 = getP1(password);
2728N/A this.pw2 = getP2(password);
2728N/A debug("NTLM Client: (h,u,t,version(v)) = (%s,%s,%s,%s(%s))\n",
2728N/A hostname, username, domain, version, v.toString());
2728N/A }
2728N/A
2728N/A /**
2728N/A * Generates the Type 1 message
2728N/A * @return the message generated
2728N/A */
2728N/A public byte[] type1() {
2728N/A Writer p = new Writer(1, 32);
2728N/A int flags = 0x8203;
2728N/A if (hostname != null) {
2728N/A flags |= 0x2000;
2728N/A }
2728N/A if (domain != null) {
2728N/A flags |= 0x1000;
2728N/A }
2728N/A if (v != Version.NTLM) {
2728N/A flags |= 0x80000;
2728N/A }
2728N/A p.writeInt(12, flags);
2728N/A p.writeSecurityBuffer(24, hostname, false);
2728N/A p.writeSecurityBuffer(16, domain, false);
2728N/A debug("NTLM Client: Type 1 created\n");
2728N/A debug(p.getBytes());
2728N/A return p.getBytes();
2728N/A }
2728N/A
2728N/A /**
2728N/A * Generates the Type 3 message
2728N/A * @param type2 the responding Type 2 message from server, must not be null
2728N/A * @param nonce random 8-byte array to be used in message generation,
2728N/A * must not be null except for original NTLM v1
2728N/A * @return the message generated
4357N/A * @throws NTLMException if the incoming message is invalid, or
4357N/A * {@code nonce} is null for NTLM v1.
2728N/A */
2728N/A public byte[] type3(byte[] type2, byte[] nonce) throws NTLMException {
2728N/A if (type2 == null || (v != Version.NTLM && nonce == null)) {
4357N/A throw new NTLMException(NTLMException.PROTOCOL,
4357N/A "type2 and nonce cannot be null");
2728N/A }
2728N/A debug("NTLM Client: Type 2 received\n");
2728N/A debug(type2);
2728N/A Reader r = new Reader(type2);
2728N/A byte[] challenge = r.readBytes(24, 8);
2728N/A int inputFlags = r.readInt(20);
2728N/A boolean unicode = (inputFlags & 1) == 1;
2728N/A String domainFromServer = r.readSecurityBuffer(12, unicode);
2728N/A if (domainFromServer != null) {
2728N/A domain = domainFromServer;
2728N/A }
2728N/A if (domain == null) {
5764N/A domain = "";
2728N/A }
2728N/A
2728N/A int flags = 0x88200 | (inputFlags & 3);
2728N/A Writer p = new Writer(3, 64);
2728N/A byte[] lm = null, ntlm = null;
2728N/A
2728N/A p.writeSecurityBuffer(28, domain, unicode);
2728N/A p.writeSecurityBuffer(36, username, unicode);
2728N/A p.writeSecurityBuffer(44, hostname, unicode);
2728N/A
2728N/A if (v == Version.NTLM) {
2728N/A byte[] lmhash = calcLMHash(pw1);
2728N/A byte[] nthash = calcNTHash(pw2);
2728N/A if (writeLM) lm = calcResponse (lmhash, challenge);
2728N/A if (writeNTLM) ntlm = calcResponse (nthash, challenge);
2728N/A } else if (v == Version.NTLM2) {
2728N/A byte[] nthash = calcNTHash(pw2);
2728N/A lm = ntlm2LM(nonce);
2728N/A ntlm = ntlm2NTLM(nthash, nonce, challenge);
2728N/A } else {
2728N/A byte[] nthash = calcNTHash(pw2);
2728N/A if (writeLM) lm = calcV2(nthash,
2728N/A username.toUpperCase(Locale.US)+domain, nonce, challenge);
2728N/A if (writeNTLM) {
2728N/A byte[] alist = type2.length > 48 ?
2728N/A r.readSecurityBuffer(40) : new byte[0];
2728N/A byte[] blob = new byte[32+alist.length];
2728N/A System.arraycopy(new byte[]{1,1,0,0,0,0,0,0}, 0, blob, 0, 8);
2728N/A // TS
2728N/A byte[] time = BigInteger.valueOf(new Date().getTime())
2728N/A .add(new BigInteger("11644473600000"))
2728N/A .multiply(BigInteger.valueOf(10000))
2728N/A .toByteArray();
2728N/A for (int i=0; i<time.length; i++) {
2728N/A blob[8+time.length-i-1] = time[i];
2728N/A }
2728N/A System.arraycopy(nonce, 0, blob, 16, 8);
2728N/A System.arraycopy(new byte[]{0,0,0,0}, 0, blob, 24, 4);
2728N/A System.arraycopy(alist, 0, blob, 28, alist.length);
2728N/A System.arraycopy(new byte[]{0,0,0,0}, 0,
2728N/A blob, 28+alist.length, 4);
2728N/A ntlm = calcV2(nthash, username.toUpperCase(Locale.US)+domain,
2728N/A blob, challenge);
2728N/A }
2728N/A }
2728N/A p.writeSecurityBuffer(12, lm);
2728N/A p.writeSecurityBuffer(20, ntlm);
2728N/A p.writeSecurityBuffer(52, new byte[0]);
2728N/A
2728N/A p.writeInt(60, flags);
2728N/A debug("NTLM Client: Type 3 created\n");
2728N/A debug(p.getBytes());
2728N/A return p.getBytes();
2728N/A }
2728N/A
2728N/A /**
2728N/A * Returns the domain value provided by server after the authentication
2728N/A * is complete, or the domain value provided by the client before it.
2728N/A * @return the domain
2728N/A */
2728N/A public String getDomain() {
2728N/A return domain;
2728N/A }
2728N/A
2728N/A /**
2728N/A * Disposes any password-derived information.
2728N/A */
2728N/A public void dispose() {
2728N/A Arrays.fill(pw1, (byte)0);
2728N/A Arrays.fill(pw2, (byte)0);
2728N/A }
2728N/A}