author | weijun |
Fri, 02 Nov 2012 10:48:04 +0800 | |
changeset 14340 | e150cbaf584e |
parent 10348 | 7d1a82029332 |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
6517 | 1 |
/* |
2 |
* Copyright (c) 2010, 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 it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* 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 WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 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 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.security.ntlm; |
|
27 |
||
28 |
import java.util.Arrays; |
|
29 |
import java.util.Locale; |
|
30 |
||
31 |
/** |
|
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 type1 |
|
45 |
* byte[] type2 = server.type2(type1, nonce); |
|
46 |
* // Send type2 to client and receive type3 |
|
47 |
* verify(type3, nonce); |
|
48 |
* </pre> |
|
49 |
*/ |
|
50 |
public abstract class Server extends NTLM { |
|
51 |
final private String domain; |
|
52 |
final 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 v1 |
|
58 |
* <li>NTLM2: NTLM v1 with Client Challenge |
|
59 |
* <li>NTLMv2: NTLM v2 |
|
60 |
* </ul> |
|
61 |
* If null, all versions will be supported. Please note that unless NTLM2 |
|
62 |
* is selected, authentication succeeds if one of LM (or LMv2) or |
|
63 |
* NTLM (or NTLMv2) is verified. |
|
64 |
* @param domain the domain, must not be null |
|
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
65 |
* @throws NTLMException if {@code domain} is null. |
6517 | 66 |
*/ |
67 |
public Server(String version, String domain) throws NTLMException { |
|
68 |
super(version); |
|
69 |
if (domain == null) { |
|
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
70 |
throw new NTLMException(NTLMException.PROTOCOL, |
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
71 |
"domain cannot be null"); |
6517 | 72 |
} |
73 |
this.allVersion = (version == null); |
|
74 |
this.domain = domain; |
|
75 |
debug("NTLM Server: (t,version) = (%s,%s)\n", domain, version); |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Generates the Type 2 message |
|
80 |
* @param type1 the Type1 message received, must not be null |
|
81 |
* @param nonce the random 8-byte array to be used in message generation, |
|
82 |
* must not be null |
|
83 |
* @return the message generated |
|
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
84 |
* @throws NTLMException if the incoming message is invalid, or |
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
85 |
* {@code nonce} is null. |
6517 | 86 |
*/ |
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
87 |
public byte[] type2(byte[] type1, byte[] nonce) throws NTLMException { |
6517 | 88 |
if (nonce == null) { |
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
89 |
throw new NTLMException(NTLMException.PROTOCOL, |
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
90 |
"nonce cannot be null"); |
6517 | 91 |
} |
92 |
debug("NTLM Server: Type 1 received\n"); |
|
93 |
if (type1 != null) debug(type1); |
|
94 |
Writer p = new Writer(2, 32); |
|
95 |
int flags = 0x80205; |
|
96 |
p.writeSecurityBuffer(12, domain, true); |
|
97 |
p.writeInt(20, flags); |
|
98 |
p.writeBytes(24, nonce); |
|
99 |
debug("NTLM Server: Type 2 created\n"); |
|
100 |
debug(p.getBytes()); |
|
101 |
return p.getBytes(); |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Verifies the Type3 message received from client and returns |
|
106 |
* various negotiated information. |
|
107 |
* @param type3 the incoming Type3 message from client, must not be null |
|
108 |
* @param nonce the same nonce provided in {@link #type2}, must not be null |
|
14340 | 109 |
* @return client username, client hostname, and the request target |
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
110 |
* @throws NTLMException if the incoming message is invalid, or |
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
111 |
* {@code nonce} is null. |
6517 | 112 |
*/ |
113 |
public String[] verify(byte[] type3, byte[] nonce) |
|
114 |
throws NTLMException { |
|
115 |
if (type3 == null || nonce == null) { |
|
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
116 |
throw new NTLMException(NTLMException.PROTOCOL, |
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
6517
diff
changeset
|
117 |
"type1 or nonce cannot be null"); |
6517 | 118 |
} |
119 |
debug("NTLM Server: Type 3 received\n"); |
|
120 |
if (type3 != null) debug(type3); |
|
121 |
Reader r = new Reader(type3); |
|
122 |
String username = r.readSecurityBuffer(36, true); |
|
123 |
String hostname = r.readSecurityBuffer(44, true); |
|
124 |
String incomingDomain = r.readSecurityBuffer(28, true); |
|
125 |
/*if (incomingDomain != null && !incomingDomain.equals(domain)) { |
|
126 |
throw new NTLMException(NTLMException.DOMAIN_UNMATCH, |
|
127 |
"Wrong domain: " + incomingDomain + |
|
128 |
" vs " + domain); // Needed? |
|
129 |
}*/ |
|
130 |
boolean verified = false; |
|
131 |
char[] password = getPassword(domain, username); |
|
132 |
if (password == null) { |
|
133 |
throw new NTLMException(NTLMException.USER_UNKNOWN, |
|
134 |
"Unknown user"); |
|
135 |
} |
|
136 |
byte[] incomingLM = r.readSecurityBuffer(12); |
|
137 |
byte[] incomingNTLM = r.readSecurityBuffer(20); |
|
138 |
||
139 |
if (!verified && (allVersion || v == Version.NTLM)) { |
|
140 |
if (incomingLM.length > 0) { |
|
141 |
byte[] pw1 = getP1(password); |
|
142 |
byte[] lmhash = calcLMHash(pw1); |
|
143 |
byte[] lmresponse = calcResponse (lmhash, nonce); |
|
144 |
if (Arrays.equals(lmresponse, incomingLM)) { |
|
145 |
verified = true; |
|
146 |
} |
|
147 |
} |
|
148 |
if (incomingNTLM.length > 0) { |
|
149 |
byte[] pw2 = getP2(password); |
|
150 |
byte[] nthash = calcNTHash(pw2); |
|
151 |
byte[] ntresponse = calcResponse (nthash, nonce); |
|
152 |
if (Arrays.equals(ntresponse, incomingNTLM)) { |
|
153 |
verified = true; |
|
154 |
} |
|
155 |
} |
|
156 |
debug("NTLM Server: verify using NTLM: " + verified + "\n"); |
|
157 |
} |
|
158 |
if (!verified && (allVersion || v == Version.NTLM2)) { |
|
159 |
byte[] pw2 = getP2(password); |
|
160 |
byte[] nthash = calcNTHash(pw2); |
|
161 |
byte[] clientNonce = Arrays.copyOf(incomingLM, 8); |
|
162 |
byte[] ntlmresponse = ntlm2NTLM(nthash, clientNonce, nonce); |
|
163 |
if (Arrays.equals(incomingNTLM, ntlmresponse)) { |
|
164 |
verified = true; |
|
165 |
} |
|
166 |
debug("NTLM Server: verify using NTLM2: " + verified + "\n"); |
|
167 |
} |
|
168 |
if (!verified && (allVersion || v == Version.NTLMv2)) { |
|
169 |
byte[] pw2 = getP2(password); |
|
170 |
byte[] nthash = calcNTHash(pw2); |
|
171 |
if (incomingLM.length > 0) { |
|
172 |
byte[] clientNonce = Arrays.copyOfRange( |
|
173 |
incomingLM, 16, incomingLM.length); |
|
174 |
byte[] lmresponse = calcV2(nthash, |
|
175 |
username.toUpperCase(Locale.US)+incomingDomain, |
|
176 |
clientNonce, nonce); |
|
177 |
if (Arrays.equals(lmresponse, incomingLM)) { |
|
178 |
verified = true; |
|
179 |
} |
|
180 |
} |
|
181 |
if (incomingNTLM.length > 0) { |
|
182 |
byte[] clientBlob = Arrays.copyOfRange( |
|
183 |
incomingNTLM, 16, incomingNTLM.length); |
|
184 |
byte[] ntlmresponse = calcV2(nthash, |
|
185 |
username.toUpperCase(Locale.US)+incomingDomain, |
|
186 |
clientBlob, nonce); |
|
187 |
if (Arrays.equals(ntlmresponse, incomingNTLM)) { |
|
188 |
verified = true; |
|
189 |
} |
|
190 |
} |
|
191 |
debug("NTLM Server: verify using NTLMv2: " + verified + "\n"); |
|
192 |
} |
|
193 |
if (!verified) { |
|
194 |
throw new NTLMException(NTLMException.AUTH_FAILED, |
|
195 |
"None of LM and NTLM verified"); |
|
196 |
} |
|
14340 | 197 |
return new String[] {username, hostname, incomingDomain}; |
6517 | 198 |
} |
199 |
||
200 |
/** |
|
201 |
* Retrieves the password for a given user. This method should be |
|
202 |
* overridden in a concrete class. |
|
203 |
* @param domain can be null |
|
204 |
* @param username must not be null |
|
205 |
* @return the password for the user, or null if unknown |
|
206 |
*/ |
|
207 |
public abstract char[] getPassword(String domain, String username); |
|
208 |
} |