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