Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/security/ntlm/Server.java
38924 views
1/*2* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation. Oracle designates this8* particular file as subject to the "Classpath" exception as provided9* by Oracle in the LICENSE file that accompanied this code.10*11* This code is distributed in the hope that it will be useful, but WITHOUT12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14* version 2 for more details (a copy is included in the LICENSE file that15* accompanied this code).16*17* You should have received a copy of the GNU General Public License version18* 2 along with this work; if not, write to the Free Software Foundation,19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.20*21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA22* or visit www.oracle.com if you need additional information or have any23* questions.24*/2526package com.sun.security.ntlm;2728import java.util.Arrays;29import java.util.Locale;3031/**32* The NTLM server, not multi-thread enabled.<p>33* Example:34* <pre>35* Server server = new Server(null, "REALM") {36* public char[] getPassword(String ntdomain, String username) {37* switch (username) {38* case "dummy": return "t0pSeCr3t".toCharArray();39* case "guest": return "".toCharArray();40* default: return null;41* }42* }43* };44* // Receive client request as type145* byte[] type2 = server.type2(type1, nonce);46* // Send type2 to client and receive type347* verify(type3, nonce);48* </pre>49*/50public abstract class Server extends NTLM {51final private String domain;52final private boolean allVersion;53/**54* Creates a Server instance.55* @param version the NTLM version to use, which can be:56* <ul>57* <li>NTLM: Original NTLM v158* <li>NTLM2: NTLM v1 with Client Challenge59* <li>NTLMv2: NTLM v260* </ul>61* If null, all versions will be supported. Please note that unless NTLM262* is selected, authentication succeeds if one of LM (or LMv2) or63* NTLM (or NTLMv2) is verified.64* @param domain the domain, must not be null65* @throws NTLMException if {@code domain} is null.66*/67public Server(String version, String domain) throws NTLMException {68super(version);69if (domain == null) {70throw new NTLMException(NTLMException.PROTOCOL,71"domain cannot be null");72}73this.allVersion = (version == null);74this.domain = domain;75debug("NTLM Server: (t,version) = (%s,%s)\n", domain, version);76}7778/**79* Generates the Type 2 message80* @param type1 the Type1 message received, must not be null81* @param nonce the random 8-byte array to be used in message generation,82* must not be null83* @return the message generated84* @throws NTLMException if the incoming message is invalid, or85* {@code nonce} is null.86*/87public byte[] type2(byte[] type1, byte[] nonce) throws NTLMException {88if (nonce == null) {89throw new NTLMException(NTLMException.PROTOCOL,90"nonce cannot be null");91}92debug("NTLM Server: Type 1 received\n");93if (type1 != null) debug(type1);94Writer p = new Writer(2, 32);95// Negotiate NTLM2 Key, Target Type Domain,96// Negotiate NTLM, Request Target, Negotiate unicode97int flags = 0x90205;98p.writeSecurityBuffer(12, domain, true);99p.writeInt(20, flags);100p.writeBytes(24, nonce);101debug("NTLM Server: Type 2 created\n");102debug(p.getBytes());103return p.getBytes();104}105106/**107* Verifies the Type3 message received from client and returns108* various negotiated information.109* @param type3 the incoming Type3 message from client, must not be null110* @param nonce the same nonce provided in {@link #type2}, must not be null111* @return client username, client hostname, and the request target112* @throws NTLMException if the incoming message is invalid, or113* {@code nonce} is null.114*/115public String[] verify(byte[] type3, byte[] nonce)116throws NTLMException {117if (type3 == null || nonce == null) {118throw new NTLMException(NTLMException.PROTOCOL,119"type1 or nonce cannot be null");120}121debug("NTLM Server: Type 3 received\n");122if (type3 != null) debug(type3);123Reader r = new Reader(type3);124String username = r.readSecurityBuffer(36, true);125String hostname = r.readSecurityBuffer(44, true);126String incomingDomain = r.readSecurityBuffer(28, true);127/*if (incomingDomain != null && !incomingDomain.equals(domain)) {128throw new NTLMException(NTLMException.DOMAIN_UNMATCH,129"Wrong domain: " + incomingDomain +130" vs " + domain); // Needed?131}*/132133boolean verified = false;134char[] password = getPassword(incomingDomain, username);135if (password == null) {136throw new NTLMException(NTLMException.USER_UNKNOWN,137"Unknown user");138}139byte[] incomingLM = r.readSecurityBuffer(12);140byte[] incomingNTLM = r.readSecurityBuffer(20);141142if (!verified && (allVersion || v == Version.NTLM)) {143if (incomingLM.length > 0) {144byte[] pw1 = getP1(password);145byte[] lmhash = calcLMHash(pw1);146byte[] lmresponse = calcResponse (lmhash, nonce);147if (Arrays.equals(lmresponse, incomingLM)) {148verified = true;149}150}151if (incomingNTLM.length > 0) {152byte[] pw2 = getP2(password);153byte[] nthash = calcNTHash(pw2);154byte[] ntresponse = calcResponse (nthash, nonce);155if (Arrays.equals(ntresponse, incomingNTLM)) {156verified = true;157}158}159debug("NTLM Server: verify using NTLM: " + verified + "\n");160}161if (!verified && (allVersion || v == Version.NTLM2)) {162byte[] pw2 = getP2(password);163byte[] nthash = calcNTHash(pw2);164byte[] clientNonce = Arrays.copyOf(incomingLM, 8);165byte[] ntlmresponse = ntlm2NTLM(nthash, clientNonce, nonce);166if (Arrays.equals(incomingNTLM, ntlmresponse)) {167verified = true;168}169debug("NTLM Server: verify using NTLM2: " + verified + "\n");170}171if (!verified && (allVersion || v == Version.NTLMv2)) {172byte[] pw2 = getP2(password);173byte[] nthash = calcNTHash(pw2);174if (incomingLM.length > 0) {175byte[] clientNonce = Arrays.copyOfRange(176incomingLM, 16, incomingLM.length);177byte[] lmresponse = calcV2(nthash,178username.toUpperCase(Locale.US)+incomingDomain,179clientNonce, nonce);180if (Arrays.equals(lmresponse, incomingLM)) {181verified = true;182}183}184if (incomingNTLM.length > 0) {185// We didn't sent alist in type2(), so there186// is nothing to check here.187byte[] clientBlob = Arrays.copyOfRange(188incomingNTLM, 16, incomingNTLM.length);189byte[] ntlmresponse = calcV2(nthash,190username.toUpperCase(Locale.US)+incomingDomain,191clientBlob, nonce);192if (Arrays.equals(ntlmresponse, incomingNTLM)) {193verified = true;194}195}196debug("NTLM Server: verify using NTLMv2: " + verified + "\n");197}198if (!verified) {199throw new NTLMException(NTLMException.AUTH_FAILED,200"None of LM and NTLM verified");201}202return new String[] {username, hostname, incomingDomain};203}204205/**206* Retrieves the password for a given user. This method should be207* overridden in a concrete class.208* @param domain can be null209* @param username must not be null210* @return the password for the user, or null if unknown211*/212public abstract char[] getPassword(String domain, String username);213}214215216