author | chegar |
Mon, 10 Jan 2011 18:12:43 +0000 | |
changeset 8815 | 9a3f042d307e |
parent 6517 | 151856936fd8 |
child 9035 | 1255eb81cc2f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
26 |
package sun.net.www.protocol.http.ntlm; |
2 | 27 |
|
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
28 |
import com.sun.security.ntlm.Client; |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
29 |
import com.sun.security.ntlm.NTLMException; |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
30 |
import java.io.IOException; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
31 |
import java.net.InetAddress; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
32 |
import java.net.PasswordAuthentication; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
33 |
import java.net.UnknownHostException; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
34 |
import java.net.URL; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
35 |
import java.security.GeneralSecurityException; |
2 | 36 |
|
37 |
import sun.net.www.HeaderParser; |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
38 |
import sun.net.www.protocol.http.AuthenticationInfo; |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
39 |
import sun.net.www.protocol.http.AuthScheme; |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
40 |
import sun.net.www.protocol.http.HttpURLConnection; |
2 | 41 |
|
42 |
/** |
|
43 |
* NTLMAuthentication: |
|
44 |
* |
|
45 |
* @author Michael McMahon |
|
46 |
*/ |
|
47 |
||
48 |
/* |
|
49 |
* NTLM authentication is nominally based on the framework defined in RFC2617, |
|
50 |
* but differs from the standard (Basic & Digest) schemes as follows: |
|
51 |
* |
|
52 |
* 1. A complete authentication requires three request/response transactions |
|
53 |
* as shown below: |
|
54 |
* REQ -------------------------------> |
|
55 |
* <---- 401 (signalling NTLM) -------- |
|
56 |
* |
|
57 |
* REQ (with type1 NTLM msg) ---------> |
|
58 |
* <---- 401 (with type 2 NTLM msg) --- |
|
59 |
* |
|
60 |
* REQ (with type3 NTLM msg) ---------> |
|
61 |
* <---- OK --------------------------- |
|
62 |
* |
|
63 |
* 2. The scope of the authentication is the TCP connection (which must be kept-alive) |
|
64 |
* after the type2 response is received. This means that NTLM does not work end-to-end |
|
65 |
* through a proxy, rather between client and proxy, or between client and server (with no proxy) |
|
66 |
*/ |
|
67 |
||
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
68 |
public class NTLMAuthentication extends AuthenticationInfo { |
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
69 |
private static final long serialVersionUID = 170L; |
2 | 70 |
|
8815
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
71 |
private static final NTLMAuthenticationCallback NTLMAuthCallback = |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
72 |
NTLMAuthenticationCallback.getNTLMAuthenticationCallback(); |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
73 |
|
2 | 74 |
private String hostname; |
75 |
private static String defaultDomain; /* Domain to use if not specified by user */ |
|
76 |
||
77 |
static { |
|
78 |
defaultDomain = java.security.AccessController.doPrivileged( |
|
79 |
new sun.security.action.GetPropertyAction("http.auth.ntlm.domain", |
|
80 |
"domain")); |
|
81 |
}; |
|
82 |
||
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
83 |
public static boolean supportsTransparentAuth () { |
2 | 84 |
return false; |
85 |
} |
|
86 |
||
8815
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
87 |
/** |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
88 |
* Returns true if the given site is trusted, i.e. we can try |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
89 |
* transparent Authentication. |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
90 |
*/ |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
91 |
public static boolean isTrustedSite(URL url) { |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
92 |
return NTLMAuthCallback.isTrustedSite(url); |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
93 |
} |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
6517
diff
changeset
|
94 |
|
2 | 95 |
private void init0() { |
96 |
||
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
97 |
hostname = java.security.AccessController.doPrivileged( |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
98 |
new java.security.PrivilegedAction<String>() { |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
99 |
public String run() { |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
100 |
String localhost; |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
101 |
try { |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
102 |
localhost = InetAddress.getLocalHost().getHostName().toUpperCase(); |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
103 |
} catch (UnknownHostException e) { |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
104 |
localhost = "localhost"; |
2 | 105 |
} |
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
106 |
return localhost; |
2 | 107 |
} |
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
108 |
}); |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
109 |
int x = hostname.indexOf ('.'); |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
110 |
if (x != -1) { |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
111 |
hostname = hostname.substring (0, x); |
2 | 112 |
} |
113 |
}; |
|
114 |
||
115 |
PasswordAuthentication pw; |
|
116 |
||
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
117 |
Client client; |
2 | 118 |
/** |
119 |
* Create a NTLMAuthentication: |
|
120 |
* Username may be specified as domain<BACKSLASH>username in the application Authenticator. |
|
121 |
* If this notation is not used, then the domain will be taken |
|
122 |
* from a system property: "http.auth.ntlm.domain". |
|
123 |
*/ |
|
124 |
public NTLMAuthentication(boolean isProxy, URL url, PasswordAuthentication pw) { |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
125 |
super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
126 |
AuthScheme.NTLM, |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
127 |
url, |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
128 |
""); |
2 | 129 |
init (pw); |
130 |
} |
|
131 |
||
132 |
private void init (PasswordAuthentication pw) { |
|
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
133 |
String username; |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
134 |
String ntdomain; |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
135 |
char[] password; |
2 | 136 |
this.pw = pw; |
137 |
String s = pw.getUserName(); |
|
138 |
int i = s.indexOf ('\\'); |
|
139 |
if (i == -1) { |
|
140 |
username = s; |
|
141 |
ntdomain = defaultDomain; |
|
142 |
} else { |
|
143 |
ntdomain = s.substring (0, i).toUpperCase(); |
|
144 |
username = s.substring (i+1); |
|
145 |
} |
|
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
146 |
password = pw.getPassword(); |
2 | 147 |
init0(); |
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
148 |
try { |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
149 |
client = new Client(System.getProperty("ntlm.version"), hostname, |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
150 |
username, ntdomain, password); |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
151 |
} catch (NTLMException ne) { |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
152 |
try { |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
153 |
client = new Client(null, hostname, username, ntdomain, password); |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
154 |
} catch (NTLMException ne2) { |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
155 |
// Will never happen |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
156 |
throw new AssertionError("Really?"); |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
157 |
} |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
158 |
} |
2 | 159 |
} |
160 |
||
161 |
/** |
|
162 |
* Constructor used for proxy entries |
|
163 |
*/ |
|
164 |
public NTLMAuthentication(boolean isProxy, String host, int port, |
|
165 |
PasswordAuthentication pw) { |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
166 |
super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
167 |
AuthScheme.NTLM, |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
168 |
host, |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
169 |
port, |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
1639
diff
changeset
|
170 |
""); |
2 | 171 |
init (pw); |
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* @return true if this authentication supports preemptive authorization |
|
176 |
*/ |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
177 |
@Override |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
178 |
public boolean supportsPreemptiveAuthorization() { |
2 | 179 |
return false; |
180 |
} |
|
181 |
||
182 |
/** |
|
183 |
* Not supported. Must use the setHeaders() method |
|
184 |
*/ |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
185 |
@Override |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
186 |
public String getHeaderValue(URL url, String method) { |
2 | 187 |
throw new RuntimeException ("getHeaderValue not supported"); |
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
* Check if the header indicates that the current auth. parameters are stale. |
|
192 |
* If so, then replace the relevant field with the new value |
|
193 |
* and return true. Otherwise return false. |
|
194 |
* returning true means the request can be retried with the same userid/password |
|
195 |
* returning false means we have to go back to the user to ask for a new |
|
196 |
* username password. |
|
197 |
*/ |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
198 |
@Override |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
199 |
public boolean isAuthorizationStale (String header) { |
2 | 200 |
return false; /* should not be called for ntlm */ |
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Set header(s) on the given connection. |
|
205 |
* @param conn The connection to apply the header(s) to |
|
206 |
* @param p A source of header values for this connection, not used because |
|
207 |
* HeaderParser converts the fields to lower case, use raw instead |
|
208 |
* @param raw The raw header field. |
|
209 |
* @return true if all goes well, false if no headers were set. |
|
210 |
*/ |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
211 |
@Override |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
212 |
public synchronized boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) { |
2 | 213 |
|
214 |
try { |
|
215 |
String response; |
|
216 |
if (raw.length() < 6) { /* NTLM<sp> */ |
|
217 |
response = buildType1Msg (); |
|
218 |
} else { |
|
219 |
String msg = raw.substring (5); /* skip NTLM<sp> */ |
|
220 |
response = buildType3Msg (msg); |
|
221 |
} |
|
222 |
conn.setAuthenticationProperty(getHeaderName(), response); |
|
223 |
return true; |
|
224 |
} catch (IOException e) { |
|
225 |
return false; |
|
226 |
} catch (GeneralSecurityException e) { |
|
227 |
return false; |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
private String buildType1Msg () { |
|
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
232 |
byte[] msg = client.type1(); |
2 | 233 |
String result = "NTLM " + (new B64Encoder()).encode (msg); |
234 |
return result; |
|
235 |
} |
|
236 |
||
237 |
private String buildType3Msg (String challenge) throws GeneralSecurityException, |
|
238 |
IOException { |
|
239 |
/* First decode the type2 message to get the server nonce */ |
|
240 |
/* nonce is located at type2[24] for 8 bytes */ |
|
241 |
||
242 |
byte[] type2 = (new sun.misc.BASE64Decoder()).decodeBuffer (challenge); |
|
6517
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
243 |
byte[] nonce = new byte[8]; |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
244 |
new java.util.Random().nextBytes(nonce); |
151856936fd8
6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
5506
diff
changeset
|
245 |
byte[] msg = client.type3(type2, nonce); |
2 | 246 |
String result = "NTLM " + (new B64Encoder()).encode (msg); |
247 |
return result; |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
class B64Encoder extends sun.misc.BASE64Encoder { |
|
252 |
/* to force it to to the entire encoding in one line */ |
|
253 |
protected int bytesPerLine () { |
|
254 |
return 1024; |
|
255 |
} |
|
256 |
} |