author | weijun |
Wed, 09 Jul 2014 15:10:42 +0800 | |
changeset 25396 | 5e73c95f95db |
parent 10348 | 7d1a82029332 |
permissions | -rw-r--r-- |
6517 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
2 |
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
6517 | 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.sasl.ntlm; |
|
27 |
||
28 |
import com.sun.security.ntlm.Client; |
|
29 |
import com.sun.security.ntlm.NTLMException; |
|
30 |
import java.io.IOException; |
|
31 |
import java.net.InetAddress; |
|
32 |
import java.net.UnknownHostException; |
|
33 |
import java.util.Map; |
|
34 |
import java.util.Random; |
|
35 |
import javax.security.auth.callback.Callback; |
|
36 |
||
37 |
||
38 |
import javax.security.sasl.*; |
|
39 |
import javax.security.auth.callback.CallbackHandler; |
|
40 |
import javax.security.auth.callback.NameCallback; |
|
41 |
import javax.security.auth.callback.PasswordCallback; |
|
42 |
import javax.security.auth.callback.UnsupportedCallbackException; |
|
43 |
||
44 |
/** |
|
45 |
* Required callbacks: |
|
46 |
* - RealmCallback |
|
47 |
* handle can provide domain info for authentication, optional |
|
48 |
* - NameCallback |
|
49 |
* handler must enter username to use for authentication |
|
50 |
* - PasswordCallback |
|
51 |
* handler must enter password for username to use for authentication |
|
52 |
* |
|
53 |
* Environment properties that affect behavior of implementation: |
|
54 |
* |
|
55 |
* javax.security.sasl.qop |
|
56 |
* String, quality of protection; only "auth" is accepted, default "auth" |
|
57 |
* |
|
58 |
* com.sun.security.sasl.ntlm.version |
|
59 |
* String, name a specific version to use; can be: |
|
60 |
* LM/NTLM: Original NTLM v1 |
|
61 |
* LM: Original NTLM v1, LM only |
|
62 |
* NTLM: Original NTLM v1, NTLM only |
|
63 |
* NTLM2: NTLM v1 with Client Challenge |
|
64 |
* LMv2/NTLMv2: NTLM v2 |
|
65 |
* LMv2: NTLM v2, LM only |
|
66 |
* NTLMv2: NTLM v2, NTLM only |
|
67 |
* If not specified, use system property "ntlm.version". If |
|
68 |
* still not specified, use default value "LMv2/NTLMv2". |
|
69 |
* |
|
70 |
* com.sun.security.sasl.ntlm.random |
|
71 |
* java.util.Random, the nonce source to be used in NTLM v2 or NTLM v1 with |
|
72 |
* Client Challenge. Default null, an internal java.util.Random object |
|
73 |
* will be used |
|
74 |
* |
|
75 |
* Negotiated Properties: |
|
76 |
* |
|
77 |
* javax.security.sasl.qop |
|
78 |
* Always "auth" |
|
79 |
* |
|
80 |
* com.sun.security.sasl.html.domain |
|
81 |
* The domain for the user, provided by the server |
|
82 |
* |
|
83 |
* @see <a href="http://www.ietf.org/rfc/rfc2222.txt">RFC 2222</a> |
|
84 |
* - Simple Authentication and Security Layer (SASL) |
|
85 |
* |
|
86 |
*/ |
|
87 |
final class NTLMClient implements SaslClient { |
|
88 |
||
89 |
private static final String NTLM_VERSION = |
|
90 |
"com.sun.security.sasl.ntlm.version"; |
|
91 |
private static final String NTLM_RANDOM = |
|
92 |
"com.sun.security.sasl.ntlm.random"; |
|
93 |
private final static String NTLM_DOMAIN = |
|
94 |
"com.sun.security.sasl.ntlm.domain"; |
|
95 |
private final static String NTLM_HOSTNAME = |
|
96 |
"com.sun.security.sasl.ntlm.hostname"; |
|
97 |
||
98 |
private final Client client; |
|
99 |
private final String mech; |
|
100 |
private final Random random; |
|
101 |
||
102 |
private int step = 0; // 0-start,1-nego,2-auth,3-done |
|
103 |
||
104 |
/** |
|
105 |
* @param mech non-null |
|
106 |
* @param authorizationId can be null or empty and ignored |
|
107 |
* @param protocol non-null for Sasl, useless for NTLM |
|
108 |
* @param serverName non-null for Sasl, but can be null for NTLM |
|
109 |
* @param props can be null |
|
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
10336
diff
changeset
|
110 |
* @param cbh can be null for Sasl, already null-checked in factory |
6517 | 111 |
* @throws SaslException |
112 |
*/ |
|
113 |
NTLMClient(String mech, String authzid, String protocol, String serverName, |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
114 |
Map<String, ?> props, CallbackHandler cbh) throws SaslException { |
6517 | 115 |
|
116 |
this.mech = mech; |
|
117 |
String version = null; |
|
118 |
Random rtmp = null; |
|
119 |
String hostname = null; |
|
120 |
||
121 |
if (props != null) { |
|
122 |
String qop = (String)props.get(Sasl.QOP); |
|
123 |
if (qop != null && !qop.equals("auth")) { |
|
124 |
throw new SaslException("NTLM only support auth"); |
|
125 |
} |
|
126 |
version = (String)props.get(NTLM_VERSION); |
|
127 |
rtmp = (Random)props.get(NTLM_RANDOM); |
|
128 |
hostname = (String)props.get(NTLM_HOSTNAME); |
|
129 |
} |
|
130 |
this.random = rtmp != null ? rtmp : new Random(); |
|
131 |
||
132 |
if (version == null) { |
|
133 |
version = System.getProperty("ntlm.version"); |
|
134 |
} |
|
135 |
||
136 |
RealmCallback dcb = (serverName != null && !serverName.isEmpty())? |
|
137 |
new RealmCallback("Realm: ", serverName) : |
|
138 |
new RealmCallback("Realm: "); |
|
139 |
NameCallback ncb = (authzid != null && !authzid.isEmpty()) ? |
|
140 |
new NameCallback("User name: ", authzid) : |
|
141 |
new NameCallback("User name: "); |
|
142 |
PasswordCallback pcb = |
|
143 |
new PasswordCallback("Password: ", false); |
|
144 |
||
145 |
try { |
|
146 |
cbh.handle(new Callback[] {dcb, ncb, pcb}); |
|
147 |
} catch (UnsupportedCallbackException e) { |
|
148 |
throw new SaslException("NTLM: Cannot perform callback to " + |
|
149 |
"acquire realm, username or password", e); |
|
150 |
} catch (IOException e) { |
|
151 |
throw new SaslException( |
|
152 |
"NTLM: Error acquiring realm, username or password", e); |
|
153 |
} |
|
154 |
||
155 |
if (hostname == null) { |
|
156 |
try { |
|
157 |
hostname = InetAddress.getLocalHost().getCanonicalHostName(); |
|
158 |
} catch (UnknownHostException e) { |
|
159 |
hostname = "localhost"; |
|
160 |
} |
|
161 |
} |
|
162 |
try { |
|
25396
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
163 |
String name = ncb.getName(); |
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
164 |
if (name == null) { |
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
165 |
name = authzid; |
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
166 |
} |
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
167 |
String domain = dcb.getText(); |
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
168 |
if (domain == null) { |
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
169 |
domain = serverName; |
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
170 |
} |
6517 | 171 |
client = new Client(version, hostname, |
25396
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
172 |
name, |
5e73c95f95db
7150092: NTLM authentication fail if user specified a different realm
weijun
parents:
10348
diff
changeset
|
173 |
domain, |
6517 | 174 |
pcb.getPassword()); |
175 |
} catch (NTLMException ne) { |
|
176 |
throw new SaslException( |
|
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
10336
diff
changeset
|
177 |
"NTLM: client creation failure", ne); |
6517 | 178 |
} |
179 |
} |
|
180 |
||
181 |
@Override |
|
182 |
public String getMechanismName() { |
|
183 |
return mech; |
|
184 |
} |
|
185 |
||
186 |
@Override |
|
187 |
public boolean isComplete() { |
|
188 |
return step >= 2; |
|
189 |
} |
|
190 |
||
191 |
@Override |
|
192 |
public byte[] unwrap(byte[] incoming, int offset, int len) |
|
193 |
throws SaslException { |
|
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
10336
diff
changeset
|
194 |
throw new IllegalStateException("Not supported."); |
6517 | 195 |
} |
196 |
||
197 |
@Override |
|
198 |
public byte[] wrap(byte[] outgoing, int offset, int len) |
|
199 |
throws SaslException { |
|
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
10336
diff
changeset
|
200 |
throw new IllegalStateException("Not supported."); |
6517 | 201 |
} |
202 |
||
203 |
@Override |
|
204 |
public Object getNegotiatedProperty(String propName) { |
|
10348
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
10336
diff
changeset
|
205 |
if (!isComplete()) { |
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
10336
diff
changeset
|
206 |
throw new IllegalStateException("authentication not complete"); |
7d1a82029332
7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents:
10336
diff
changeset
|
207 |
} |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
208 |
switch (propName) { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
209 |
case Sasl.QOP: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
210 |
return "auth"; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
211 |
case NTLM_DOMAIN: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
212 |
return client.getDomain(); |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
213 |
default: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
214 |
return null; |
6517 | 215 |
} |
216 |
} |
|
217 |
||
218 |
@Override |
|
219 |
public void dispose() throws SaslException { |
|
220 |
client.dispose(); |
|
221 |
} |
|
222 |
||
223 |
@Override |
|
224 |
public boolean hasInitialResponse() { |
|
225 |
return true; |
|
226 |
} |
|
227 |
||
228 |
@Override |
|
229 |
public byte[] evaluateChallenge(byte[] challenge) throws SaslException { |
|
230 |
step++; |
|
231 |
if (step == 1) { |
|
232 |
return client.type1(); |
|
233 |
} else { |
|
234 |
try { |
|
235 |
byte[] nonce = new byte[8]; |
|
236 |
random.nextBytes(nonce); |
|
237 |
return client.type3(challenge, nonce); |
|
238 |
} catch (NTLMException ex) { |
|
239 |
throw new SaslException("Type3 creation failed", ex); |
|
240 |
} |
|
241 |
} |
|
242 |
} |
|
243 |
} |