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.util.Arrays;
2728N/Aimport java.util.Locale;
2728N/A
2728N/A/**
2728N/A * The NTLM server, not multi-thread enabled.<p>
2728N/A * Example:
2728N/A * <pre>
2728N/A * Server server = new Server(null, "REALM") {
2728N/A * public char[] getPassword(String ntdomain, String username) {
2728N/A * switch (username) {
2728N/A * case "dummy": return "t0pSeCr3t".toCharArray();
2728N/A * case "guest": return "".toCharArray();
2728N/A * default: return null;
2728N/A * }
2728N/A * }
2728N/A * };
2728N/A * // Receive client request as type1
2728N/A * byte[] type2 = server.type2(type1, nonce);
2728N/A * // Send type2 to client and receive type3
2728N/A * verify(type3, nonce);
2728N/A * </pre>
2728N/A */
2728N/Apublic abstract class Server extends NTLM {
2728N/A final private String domain;
2728N/A final private boolean allVersion;
2728N/A /**
2728N/A * Creates a Server instance.
2728N/A * @param version the NTLM version to use, which can be:
2728N/A * <ul>
2728N/A * <li>NTLM: Original NTLM v1
2728N/A * <li>NTLM2: NTLM v1 with Client Challenge
2728N/A * <li>NTLMv2: NTLM v2
2728N/A * </ul>
2728N/A * If null, all versions will be supported. Please note that unless NTLM2
2728N/A * is selected, authentication succeeds if one of LM (or LMv2) or
2728N/A * NTLM (or NTLMv2) is verified.
2728N/A * @param domain the domain, must not be null
4357N/A * @throws NTLMException if {@code domain} is null.
2728N/A */
2728N/A public Server(String version, String domain) throws NTLMException {
2728N/A super(version);
2728N/A if (domain == null) {
4357N/A throw new NTLMException(NTLMException.PROTOCOL,
4357N/A "domain cannot be null");
2728N/A }
2728N/A this.allVersion = (version == null);
2728N/A this.domain = domain;
2728N/A debug("NTLM Server: (t,version) = (%s,%s)\n", domain, version);
2728N/A }
2728N/A
2728N/A /**
2728N/A * Generates the Type 2 message
2728N/A * @param type1 the Type1 message received, must not be null
2728N/A * @param nonce the random 8-byte array to be used in message generation,
2728N/A * must not be null
2728N/A * @return the message generated
4357N/A * @throws NTLMException if the incoming message is invalid, or
4357N/A * {@code nonce} is null.
2728N/A */
4357N/A public byte[] type2(byte[] type1, byte[] nonce) throws NTLMException {
2728N/A if (nonce == null) {
4357N/A throw new NTLMException(NTLMException.PROTOCOL,
4357N/A "nonce cannot be null");
2728N/A }
2728N/A debug("NTLM Server: Type 1 received\n");
2728N/A if (type1 != null) debug(type1);
2728N/A Writer p = new Writer(2, 32);
2728N/A int flags = 0x80205;
2728N/A p.writeSecurityBuffer(12, domain, true);
2728N/A p.writeInt(20, flags);
2728N/A p.writeBytes(24, nonce);
2728N/A debug("NTLM Server: Type 2 created\n");
2728N/A debug(p.getBytes());
2728N/A return p.getBytes();
2728N/A }
2728N/A
2728N/A /**
2728N/A * Verifies the Type3 message received from client and returns
2728N/A * various negotiated information.
2728N/A * @param type3 the incoming Type3 message from client, must not be null
2728N/A * @param nonce the same nonce provided in {@link #type2}, must not be null
2728N/A * @return username and hostname of the client in a byte array
4357N/A * @throws NTLMException if the incoming message is invalid, or
4357N/A * {@code nonce} is null.
2728N/A */
2728N/A public String[] verify(byte[] type3, byte[] nonce)
2728N/A throws NTLMException {
2728N/A if (type3 == null || nonce == null) {
4357N/A throw new NTLMException(NTLMException.PROTOCOL,
4357N/A "type1 or nonce cannot be null");
2728N/A }
2728N/A debug("NTLM Server: Type 3 received\n");
2728N/A if (type3 != null) debug(type3);
2728N/A Reader r = new Reader(type3);
2728N/A String username = r.readSecurityBuffer(36, true);
2728N/A String hostname = r.readSecurityBuffer(44, true);
2728N/A String incomingDomain = r.readSecurityBuffer(28, true);
2728N/A /*if (incomingDomain != null && !incomingDomain.equals(domain)) {
2728N/A throw new NTLMException(NTLMException.DOMAIN_UNMATCH,
2728N/A "Wrong domain: " + incomingDomain +
2728N/A " vs " + domain); // Needed?
2728N/A }*/
2728N/A boolean verified = false;
2728N/A char[] password = getPassword(domain, username);
2728N/A if (password == null) {
2728N/A throw new NTLMException(NTLMException.USER_UNKNOWN,
2728N/A "Unknown user");
2728N/A }
2728N/A byte[] incomingLM = r.readSecurityBuffer(12);
2728N/A byte[] incomingNTLM = r.readSecurityBuffer(20);
2728N/A
2728N/A if (!verified && (allVersion || v == Version.NTLM)) {
2728N/A if (incomingLM.length > 0) {
2728N/A byte[] pw1 = getP1(password);
2728N/A byte[] lmhash = calcLMHash(pw1);
2728N/A byte[] lmresponse = calcResponse (lmhash, nonce);
2728N/A if (Arrays.equals(lmresponse, incomingLM)) {
2728N/A verified = true;
2728N/A }
2728N/A }
2728N/A if (incomingNTLM.length > 0) {
2728N/A byte[] pw2 = getP2(password);
2728N/A byte[] nthash = calcNTHash(pw2);
2728N/A byte[] ntresponse = calcResponse (nthash, nonce);
2728N/A if (Arrays.equals(ntresponse, incomingNTLM)) {
2728N/A verified = true;
2728N/A }
2728N/A }
2728N/A debug("NTLM Server: verify using NTLM: " + verified + "\n");
2728N/A }
2728N/A if (!verified && (allVersion || v == Version.NTLM2)) {
2728N/A byte[] pw2 = getP2(password);
2728N/A byte[] nthash = calcNTHash(pw2);
2728N/A byte[] clientNonce = Arrays.copyOf(incomingLM, 8);
2728N/A byte[] ntlmresponse = ntlm2NTLM(nthash, clientNonce, nonce);
2728N/A if (Arrays.equals(incomingNTLM, ntlmresponse)) {
2728N/A verified = true;
2728N/A }
2728N/A debug("NTLM Server: verify using NTLM2: " + verified + "\n");
2728N/A }
2728N/A if (!verified && (allVersion || v == Version.NTLMv2)) {
2728N/A byte[] pw2 = getP2(password);
2728N/A byte[] nthash = calcNTHash(pw2);
2728N/A if (incomingLM.length > 0) {
2728N/A byte[] clientNonce = Arrays.copyOfRange(
2728N/A incomingLM, 16, incomingLM.length);
2728N/A byte[] lmresponse = calcV2(nthash,
2728N/A username.toUpperCase(Locale.US)+incomingDomain,
2728N/A clientNonce, nonce);
2728N/A if (Arrays.equals(lmresponse, incomingLM)) {
2728N/A verified = true;
2728N/A }
2728N/A }
2728N/A if (incomingNTLM.length > 0) {
2728N/A byte[] clientBlob = Arrays.copyOfRange(
2728N/A incomingNTLM, 16, incomingNTLM.length);
2728N/A byte[] ntlmresponse = calcV2(nthash,
2728N/A username.toUpperCase(Locale.US)+incomingDomain,
2728N/A clientBlob, nonce);
2728N/A if (Arrays.equals(ntlmresponse, incomingNTLM)) {
2728N/A verified = true;
2728N/A }
2728N/A }
2728N/A debug("NTLM Server: verify using NTLMv2: " + verified + "\n");
2728N/A }
2728N/A if (!verified) {
2728N/A throw new NTLMException(NTLMException.AUTH_FAILED,
2728N/A "None of LM and NTLM verified");
2728N/A }
2728N/A return new String[] {username, hostname};
2728N/A }
2728N/A
2728N/A /**
2728N/A * Retrieves the password for a given user. This method should be
2728N/A * overridden in a concrete class.
2728N/A * @param domain can be null
2728N/A * @param username must not be null
2728N/A * @return the password for the user, or null if unknown
2728N/A */
2728N/A public abstract char[] getPassword(String domain, String username);
2728N/A}