Path: blob/master/src/java.base/share/classes/com/sun/security/ntlm/Server.java
41161 views
/*1* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.security.ntlm;2627import java.util.Arrays;28import java.util.Locale;2930/**31* The NTLM server, not multi-thread enabled.<p>32* Example:33* <pre>34* Server server = new Server(null, "REALM") {35* public char[] getPassword(String ntdomain, String username) {36* switch (username) {37* case "dummy": return "t0pSeCr3t".toCharArray();38* case "guest": return "".toCharArray();39* default: return null;40* }41* }42* };43* // Receive client request as type144* byte[] type2 = server.type2(type1, nonce);45* // Send type2 to client and receive type346* verify(type3, nonce);47* </pre>48*/49public abstract class Server extends NTLM {50private final String domain;51private final boolean allVersion;52/**53* Creates a Server instance.54* @param version the NTLM version to use, which can be:55* <ul>56* <li>NTLM: Original NTLM v157* <li>NTLM2: NTLM v1 with Client Challenge58* <li>NTLMv2: NTLM v259* </ul>60* If null, all versions will be supported. Please note that unless NTLM261* is selected, authentication succeeds if one of LM (or LMv2) or62* NTLM (or NTLMv2) is verified.63* @param domain the domain, must not be null64* @throws NTLMException if {@code domain} is null.65*/66public Server(String version, String domain) throws NTLMException {67super(version);68if (domain == null) {69throw new NTLMException(NTLMException.PROTOCOL,70"domain cannot be null");71}72this.allVersion = (version == null);73this.domain = domain;74debug("NTLM Server: (t,version) = (%s,%s)\n", domain, version);75}7677/**78* Generates the Type 2 message79* @param type1 the Type1 message received, must not be null80* @param nonce the random 8-byte array to be used in message generation,81* must not be null82* @return the message generated83* @throws NTLMException if the incoming message is invalid, or84* {@code nonce} is null.85*/86public byte[] type2(byte[] type1, byte[] nonce) throws NTLMException {87if (nonce == null) {88throw new NTLMException(NTLMException.PROTOCOL,89"nonce cannot be null");90}91debug("NTLM Server: Type 1 received\n");92if (type1 != null) debug(type1);93Writer p = new Writer(2, 32);94// Negotiate NTLM2 Key, Target Type Domain,95// Negotiate NTLM, Request Target, Negotiate unicode96int flags = 0x90205;97p.writeSecurityBuffer(12, domain, true);98p.writeInt(20, flags);99p.writeBytes(24, nonce);100debug("NTLM Server: Type 2 created\n");101debug(p.getBytes());102return p.getBytes();103}104105/**106* Verifies the Type3 message received from client and returns107* various negotiated information.108* @param type3 the incoming Type3 message from client, must not be null109* @param nonce the same nonce provided in {@link #type2}, must not be null110* @return client username, client hostname, and the request target111* @throws NTLMException if the incoming message is invalid, or112* {@code nonce} is null.113*/114public String[] verify(byte[] type3, byte[] nonce)115throws NTLMException {116if (type3 == null || nonce == null) {117throw new NTLMException(NTLMException.PROTOCOL,118"type1 or nonce cannot be null");119}120debug("NTLM Server: Type 3 received\n");121if (type3 != null) debug(type3);122Reader r = new Reader(type3);123String username = r.readSecurityBuffer(36, true);124String hostname = r.readSecurityBuffer(44, true);125String incomingDomain = r.readSecurityBuffer(28, true);126/*if (incomingDomain != null && !incomingDomain.equals(domain)) {127throw new NTLMException(NTLMException.DOMAIN_UNMATCH,128"Wrong domain: " + incomingDomain +129" vs " + domain); // Needed?130}*/131132boolean verified = false;133char[] password = getPassword(incomingDomain, username);134if (password == null) {135throw new NTLMException(NTLMException.USER_UNKNOWN,136"Unknown user");137}138byte[] incomingLM = r.readSecurityBuffer(12);139byte[] incomingNTLM = r.readSecurityBuffer(20);140141if (!verified && (allVersion || v == Version.NTLM)) {142if (incomingLM.length > 0) {143byte[] pw1 = getP1(password);144byte[] lmhash = calcLMHash(pw1);145byte[] lmresponse = calcResponse (lmhash, nonce);146if (Arrays.equals(lmresponse, incomingLM)) {147verified = true;148}149}150if (incomingNTLM.length > 0) {151byte[] pw2 = getP2(password);152byte[] nthash = calcNTHash(pw2);153byte[] ntresponse = calcResponse (nthash, nonce);154if (Arrays.equals(ntresponse, incomingNTLM)) {155verified = true;156}157}158debug("NTLM Server: verify using NTLM: " + verified + "\n");159}160if (!verified && (allVersion || v == Version.NTLM2)) {161byte[] pw2 = getP2(password);162byte[] nthash = calcNTHash(pw2);163byte[] clientNonce = Arrays.copyOf(incomingLM, 8);164byte[] ntlmresponse = ntlm2NTLM(nthash, clientNonce, nonce);165if (Arrays.equals(incomingNTLM, ntlmresponse)) {166verified = true;167}168debug("NTLM Server: verify using NTLM2: " + verified + "\n");169}170if (!verified && (allVersion || v == Version.NTLMv2)) {171byte[] pw2 = getP2(password);172byte[] nthash = calcNTHash(pw2);173if (incomingLM.length > 0) {174byte[] clientNonce = Arrays.copyOfRange(175incomingLM, 16, incomingLM.length);176byte[] lmresponse = calcV2(nthash,177username.toUpperCase(Locale.US)+incomingDomain,178clientNonce, nonce);179if (Arrays.equals(lmresponse, incomingLM)) {180verified = true;181}182}183if (incomingNTLM.length > 0) {184// We didn't sent alist in type2(), so there185// is nothing to check here.186byte[] clientBlob = Arrays.copyOfRange(187incomingNTLM, 16, incomingNTLM.length);188byte[] ntlmresponse = calcV2(nthash,189username.toUpperCase(Locale.US)+incomingDomain,190clientBlob, nonce);191if (Arrays.equals(ntlmresponse, incomingNTLM)) {192verified = true;193}194}195debug("NTLM Server: verify using NTLMv2: " + verified + "\n");196}197if (!verified) {198throw new NTLMException(NTLMException.AUTH_FAILED,199"None of LM and NTLM verified");200}201return new String[] {username, hostname, incomingDomain};202}203204/**205* Retrieves the password for a given user. This method should be206* overridden in a concrete class.207* @param domain can be null208* @param username must not be null209* @return the password for the user, or null if unknown210*/211public abstract char[] getPassword(String domain, String username);212}213214215