author | chegar |
Wed, 16 Apr 2008 14:17:54 +0100 | |
changeset 479 | c6ddcfc7ff4d |
parent 2 | 90ce3da70b43 |
child 715 | f16baef3a20e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 1997-2005 Sun Microsystems, Inc. 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.net.www.protocol.http; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.net.URL; |
|
30 |
import java.net.ProtocolException; |
|
31 |
import java.net.PasswordAuthentication; |
|
32 |
import java.util.Arrays; |
|
33 |
import java.util.StringTokenizer; |
|
34 |
import java.util.Random; |
|
35 |
||
36 |
import sun.net.www.HeaderParser; |
|
37 |
import java.security.MessageDigest; |
|
38 |
import java.security.NoSuchAlgorithmException; |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
39 |
import static sun.net.www.protocol.http.HttpURLConnection.HTTP_CONNECT; |
2 | 40 |
|
41 |
||
42 |
/** |
|
43 |
* DigestAuthentication: Encapsulate an http server authentication using |
|
44 |
* the "Digest" scheme, as described in RFC2069 and updated in RFC2617 |
|
45 |
* |
|
46 |
* @author Bill Foote |
|
47 |
*/ |
|
48 |
||
49 |
class DigestAuthentication extends AuthenticationInfo { |
|
50 |
||
51 |
private static final long serialVersionUID = 100L; |
|
52 |
||
53 |
static final char DIGEST_AUTH = 'D'; |
|
54 |
||
55 |
private String authMethod; |
|
56 |
||
57 |
// Authentication parameters defined in RFC2617. |
|
58 |
// One instance of these may be shared among several DigestAuthentication |
|
59 |
// instances as a result of a single authorization (for multiple domains) |
|
60 |
||
61 |
static class Parameters implements java.io.Serializable { |
|
62 |
private boolean serverQop; // server proposed qop=auth |
|
63 |
private String opaque; |
|
64 |
private String cnonce; |
|
65 |
private String nonce; |
|
66 |
private String algorithm; |
|
67 |
private int NCcount=0; |
|
68 |
||
69 |
// The H(A1) string used for MD5-sess |
|
70 |
private String cachedHA1; |
|
71 |
||
72 |
// Force the HA1 value to be recalculated because the nonce has changed |
|
73 |
private boolean redoCachedHA1 = true; |
|
74 |
||
75 |
private static final int cnonceRepeat = 5; |
|
76 |
||
77 |
private static final int cnoncelen = 40; /* number of characters in cnonce */ |
|
78 |
||
79 |
private static Random random; |
|
80 |
||
81 |
static { |
|
82 |
random = new Random(); |
|
83 |
} |
|
84 |
||
85 |
Parameters () { |
|
86 |
serverQop = false; |
|
87 |
opaque = null; |
|
88 |
algorithm = null; |
|
89 |
cachedHA1 = null; |
|
90 |
nonce = null; |
|
91 |
setNewCnonce(); |
|
92 |
} |
|
93 |
||
94 |
boolean authQop () { |
|
95 |
return serverQop; |
|
96 |
} |
|
97 |
synchronized void incrementNC() { |
|
98 |
NCcount ++; |
|
99 |
} |
|
100 |
synchronized int getNCCount () { |
|
101 |
return NCcount; |
|
102 |
} |
|
103 |
||
104 |
int cnonce_count = 0; |
|
105 |
||
106 |
/* each call increments the counter */ |
|
107 |
synchronized String getCnonce () { |
|
108 |
if (cnonce_count >= cnonceRepeat) { |
|
109 |
setNewCnonce(); |
|
110 |
} |
|
111 |
cnonce_count++; |
|
112 |
return cnonce; |
|
113 |
} |
|
114 |
synchronized void setNewCnonce () { |
|
115 |
byte bb[] = new byte [cnoncelen/2]; |
|
116 |
char cc[] = new char [cnoncelen]; |
|
117 |
random.nextBytes (bb); |
|
118 |
for (int i=0; i<(cnoncelen/2); i++) { |
|
119 |
int x = bb[i] + 128; |
|
120 |
cc[i*2]= (char) ('A'+ x/16); |
|
121 |
cc[i*2+1]= (char) ('A'+ x%16); |
|
122 |
} |
|
123 |
cnonce = new String (cc, 0, cnoncelen); |
|
124 |
cnonce_count = 0; |
|
125 |
redoCachedHA1 = true; |
|
126 |
} |
|
127 |
||
128 |
synchronized void setQop (String qop) { |
|
129 |
if (qop != null) { |
|
130 |
StringTokenizer st = new StringTokenizer (qop, " "); |
|
131 |
while (st.hasMoreTokens()) { |
|
132 |
if (st.nextToken().equalsIgnoreCase ("auth")) { |
|
133 |
serverQop = true; |
|
134 |
return; |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
serverQop = false; |
|
139 |
} |
|
140 |
||
141 |
synchronized String getOpaque () { return opaque;} |
|
142 |
synchronized void setOpaque (String s) { opaque=s;} |
|
143 |
||
144 |
synchronized String getNonce () { return nonce;} |
|
145 |
||
146 |
synchronized void setNonce (String s) { |
|
147 |
if (!s.equals(nonce)) { |
|
148 |
nonce=s; |
|
149 |
NCcount = 0; |
|
150 |
redoCachedHA1 = true; |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
synchronized String getCachedHA1 () { |
|
155 |
if (redoCachedHA1) { |
|
156 |
return null; |
|
157 |
} else { |
|
158 |
return cachedHA1; |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
synchronized void setCachedHA1 (String s) { |
|
163 |
cachedHA1=s; |
|
164 |
redoCachedHA1=false; |
|
165 |
} |
|
166 |
||
167 |
synchronized String getAlgorithm () { return algorithm;} |
|
168 |
synchronized void setAlgorithm (String s) { algorithm=s;} |
|
169 |
} |
|
170 |
||
171 |
Parameters params; |
|
172 |
||
173 |
/** |
|
174 |
* Create a DigestAuthentication |
|
175 |
*/ |
|
176 |
public DigestAuthentication(boolean isProxy, URL url, String realm, |
|
177 |
String authMethod, PasswordAuthentication pw, |
|
178 |
Parameters params) { |
|
179 |
super(isProxy?PROXY_AUTHENTICATION:SERVER_AUTHENTICATION, DIGEST_AUTH,url, realm); |
|
180 |
this.authMethod = authMethod; |
|
181 |
this.pw = pw; |
|
182 |
this.params = params; |
|
183 |
} |
|
184 |
||
185 |
public DigestAuthentication(boolean isProxy, String host, int port, String realm, |
|
186 |
String authMethod, PasswordAuthentication pw, |
|
187 |
Parameters params) { |
|
188 |
super(isProxy?PROXY_AUTHENTICATION:SERVER_AUTHENTICATION, DIGEST_AUTH,host, port, realm); |
|
189 |
this.authMethod = authMethod; |
|
190 |
this.pw = pw; |
|
191 |
this.params = params; |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* @return true if this authentication supports preemptive authorization |
|
196 |
*/ |
|
197 |
boolean supportsPreemptiveAuthorization() { |
|
198 |
return true; |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* @return the name of the HTTP header this authentication wants set |
|
203 |
*/ |
|
204 |
String getHeaderName() { |
|
205 |
if (type == SERVER_AUTHENTICATION) { |
|
206 |
return "Authorization"; |
|
207 |
} else { |
|
208 |
return "Proxy-Authorization"; |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
* Reclaculates the request-digest and returns it. |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
214 |
* |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
215 |
* <P> Used in the common case where the requestURI is simply the |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
216 |
* abs_path. |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
217 |
* |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
218 |
* @param url |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
219 |
* the URL |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
220 |
* |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
221 |
* @param method |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
222 |
* the HTTP method |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
223 |
* |
2 | 224 |
* @return the value of the HTTP header this authentication wants set |
225 |
*/ |
|
226 |
String getHeaderValue(URL url, String method) { |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
227 |
return getHeaderValueImpl(url.getFile(), method); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
228 |
} |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
229 |
|
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
230 |
/** |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
231 |
* Reclaculates the request-digest and returns it. |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
232 |
* |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
233 |
* <P> Used when the requestURI is not the abs_path. The exact |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
234 |
* requestURI can be passed as a String. |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
235 |
* |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
236 |
* @param requestURI |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
237 |
* the Request-URI from the HTTP request line |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
238 |
* |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
239 |
* @param method |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
240 |
* the HTTP method |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
241 |
* |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
242 |
* @return the value of the HTTP header this authentication wants set |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
243 |
*/ |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
244 |
String getHeaderValue(String requestURI, String method) { |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
245 |
return getHeaderValueImpl(requestURI, method); |
2 | 246 |
} |
247 |
||
248 |
/** |
|
249 |
* Check if the header indicates that the current auth. parameters are stale. |
|
250 |
* If so, then replace the relevant field with the new value |
|
251 |
* and return true. Otherwise return false. |
|
252 |
* returning true means the request can be retried with the same userid/password |
|
253 |
* returning false means we have to go back to the user to ask for a new |
|
254 |
* username password. |
|
255 |
*/ |
|
256 |
boolean isAuthorizationStale (String header) { |
|
257 |
HeaderParser p = new HeaderParser (header); |
|
258 |
String s = p.findValue ("stale"); |
|
259 |
if (s == null || !s.equals("true")) |
|
260 |
return false; |
|
261 |
String newNonce = p.findValue ("nonce"); |
|
262 |
if (newNonce == null || "".equals(newNonce)) { |
|
263 |
return false; |
|
264 |
} |
|
265 |
params.setNonce (newNonce); |
|
266 |
return true; |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Set header(s) on the given connection. |
|
271 |
* @param conn The connection to apply the header(s) to |
|
272 |
* @param p A source of header values for this connection, if needed. |
|
273 |
* @param raw Raw header values for this connection, if needed. |
|
274 |
* @return true if all goes well, false if no headers were set. |
|
275 |
*/ |
|
276 |
boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) { |
|
277 |
params.setNonce (p.findValue("nonce")); |
|
278 |
params.setOpaque (p.findValue("opaque")); |
|
279 |
params.setQop (p.findValue("qop")); |
|
280 |
||
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
281 |
String uri; |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
282 |
String method; |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
283 |
if (type == PROXY_AUTHENTICATION && |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
284 |
conn.tunnelState() == HttpURLConnection.TunnelState.SETUP) { |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
285 |
uri = HttpURLConnection.connectRequestURI(conn.getURL()); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
286 |
method = HTTP_CONNECT; |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
287 |
} else { |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
288 |
uri = conn.getURL().getFile(); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
289 |
method = conn.getMethod(); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
290 |
} |
2 | 291 |
|
292 |
if (params.nonce == null || authMethod == null || pw == null || realm == null) { |
|
293 |
return false; |
|
294 |
} |
|
295 |
if (authMethod.length() >= 1) { |
|
296 |
// Method seems to get converted to all lower case elsewhere. |
|
297 |
// It really does need to start with an upper case letter |
|
298 |
// here. |
|
299 |
authMethod = Character.toUpperCase(authMethod.charAt(0)) |
|
300 |
+ authMethod.substring(1).toLowerCase(); |
|
301 |
} |
|
302 |
String algorithm = p.findValue("algorithm"); |
|
303 |
if (algorithm == null || "".equals(algorithm)) { |
|
304 |
algorithm = "MD5"; // The default, accoriding to rfc2069 |
|
305 |
} |
|
306 |
params.setAlgorithm (algorithm); |
|
307 |
||
308 |
// If authQop is true, then the server is doing RFC2617 and |
|
309 |
// has offered qop=auth. We do not support any other modes |
|
310 |
// and if auth is not offered we fallback to the RFC2069 behavior |
|
311 |
||
312 |
if (params.authQop()) { |
|
313 |
params.setNewCnonce(); |
|
314 |
} |
|
315 |
||
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
2
diff
changeset
|
316 |
String value = getHeaderValueImpl (uri, method); |
2 | 317 |
if (value != null) { |
318 |
conn.setAuthenticationProperty(getHeaderName(), value); |
|
319 |
return true; |
|
320 |
} else { |
|
321 |
return false; |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
/* Calculate the Authorization header field given the request URI |
|
326 |
* and based on the authorization information in params |
|
327 |
*/ |
|
328 |
private String getHeaderValueImpl (String uri, String method) { |
|
329 |
String response; |
|
330 |
char[] passwd = pw.getPassword(); |
|
331 |
boolean qop = params.authQop(); |
|
332 |
String opaque = params.getOpaque(); |
|
333 |
String cnonce = params.getCnonce (); |
|
334 |
String nonce = params.getNonce (); |
|
335 |
String algorithm = params.getAlgorithm (); |
|
336 |
params.incrementNC (); |
|
337 |
int nccount = params.getNCCount (); |
|
338 |
String ncstring=null; |
|
339 |
||
340 |
if (nccount != -1) { |
|
341 |
ncstring = Integer.toHexString (nccount).toLowerCase(); |
|
342 |
int len = ncstring.length(); |
|
343 |
if (len < 8) |
|
344 |
ncstring = zeroPad [len] + ncstring; |
|
345 |
} |
|
346 |
||
347 |
try { |
|
348 |
response = computeDigest(true, pw.getUserName(),passwd,realm, |
|
349 |
method, uri, nonce, cnonce, ncstring); |
|
350 |
} catch (NoSuchAlgorithmException ex) { |
|
351 |
return null; |
|
352 |
} |
|
353 |
||
354 |
String ncfield = "\""; |
|
355 |
if (qop) { |
|
356 |
ncfield = "\", nc=" + ncstring; |
|
357 |
} |
|
358 |
||
359 |
String value = authMethod |
|
360 |
+ " username=\"" + pw.getUserName() |
|
361 |
+ "\", realm=\"" + realm |
|
362 |
+ "\", nonce=\"" + nonce |
|
363 |
+ ncfield |
|
364 |
+ ", uri=\"" + uri |
|
365 |
+ "\", response=\"" + response |
|
366 |
+ "\", algorithm=\"" + algorithm; |
|
367 |
if (opaque != null) { |
|
368 |
value = value + "\", opaque=\"" + opaque; |
|
369 |
} |
|
370 |
if (cnonce != null) { |
|
371 |
value = value + "\", cnonce=\"" + cnonce; |
|
372 |
} |
|
373 |
if (qop) { |
|
374 |
value = value + "\", qop=\"auth"; |
|
375 |
} |
|
376 |
value = value + "\""; |
|
377 |
return value; |
|
378 |
} |
|
379 |
||
380 |
public void checkResponse (String header, String method, URL url) |
|
381 |
throws IOException { |
|
382 |
String uri = url.getFile(); |
|
383 |
char[] passwd = pw.getPassword(); |
|
384 |
String username = pw.getUserName(); |
|
385 |
boolean qop = params.authQop(); |
|
386 |
String opaque = params.getOpaque(); |
|
387 |
String cnonce = params.cnonce; |
|
388 |
String nonce = params.getNonce (); |
|
389 |
String algorithm = params.getAlgorithm (); |
|
390 |
int nccount = params.getNCCount (); |
|
391 |
String ncstring=null; |
|
392 |
||
393 |
if (header == null) { |
|
394 |
throw new ProtocolException ("No authentication information in response"); |
|
395 |
} |
|
396 |
||
397 |
if (nccount != -1) { |
|
398 |
ncstring = Integer.toHexString (nccount).toUpperCase(); |
|
399 |
int len = ncstring.length(); |
|
400 |
if (len < 8) |
|
401 |
ncstring = zeroPad [len] + ncstring; |
|
402 |
} |
|
403 |
try { |
|
404 |
String expected = computeDigest(false, username,passwd,realm, |
|
405 |
method, uri, nonce, cnonce, ncstring); |
|
406 |
HeaderParser p = new HeaderParser (header); |
|
407 |
String rspauth = p.findValue ("rspauth"); |
|
408 |
if (rspauth == null) { |
|
409 |
throw new ProtocolException ("No digest in response"); |
|
410 |
} |
|
411 |
if (!rspauth.equals (expected)) { |
|
412 |
throw new ProtocolException ("Response digest invalid"); |
|
413 |
} |
|
414 |
/* Check if there is a nextnonce field */ |
|
415 |
String nextnonce = p.findValue ("nextnonce"); |
|
416 |
if (nextnonce != null && ! "".equals(nextnonce)) { |
|
417 |
params.setNonce (nextnonce); |
|
418 |
} |
|
419 |
||
420 |
} catch (NoSuchAlgorithmException ex) { |
|
421 |
throw new ProtocolException ("Unsupported algorithm in response"); |
|
422 |
} |
|
423 |
} |
|
424 |
||
425 |
private String computeDigest( |
|
426 |
boolean isRequest, String userName, char[] password, |
|
427 |
String realm, String connMethod, |
|
428 |
String requestURI, String nonceString, |
|
429 |
String cnonce, String ncValue |
|
430 |
) throws NoSuchAlgorithmException |
|
431 |
{ |
|
432 |
||
433 |
String A1, HashA1; |
|
434 |
String algorithm = params.getAlgorithm (); |
|
435 |
boolean md5sess = algorithm.equalsIgnoreCase ("MD5-sess"); |
|
436 |
||
437 |
MessageDigest md = MessageDigest.getInstance(md5sess?"MD5":algorithm); |
|
438 |
||
439 |
if (md5sess) { |
|
440 |
if ((HashA1 = params.getCachedHA1 ()) == null) { |
|
441 |
String s = userName + ":" + realm + ":"; |
|
442 |
String s1 = encode (s, password, md); |
|
443 |
A1 = s1 + ":" + nonceString + ":" + cnonce; |
|
444 |
HashA1 = encode(A1, null, md); |
|
445 |
params.setCachedHA1 (HashA1); |
|
446 |
} |
|
447 |
} else { |
|
448 |
A1 = userName + ":" + realm + ":"; |
|
449 |
HashA1 = encode(A1, password, md); |
|
450 |
} |
|
451 |
||
452 |
String A2; |
|
453 |
if (isRequest) { |
|
454 |
A2 = connMethod + ":" + requestURI; |
|
455 |
} else { |
|
456 |
A2 = ":" + requestURI; |
|
457 |
} |
|
458 |
String HashA2 = encode(A2, null, md); |
|
459 |
String combo, finalHash; |
|
460 |
||
461 |
if (params.authQop()) { /* RRC2617 when qop=auth */ |
|
462 |
combo = HashA1+ ":" + nonceString + ":" + ncValue + ":" + |
|
463 |
cnonce + ":auth:" +HashA2; |
|
464 |
||
465 |
} else { /* for compatibility with RFC2069 */ |
|
466 |
combo = HashA1 + ":" + |
|
467 |
nonceString + ":" + |
|
468 |
HashA2; |
|
469 |
} |
|
470 |
finalHash = encode(combo, null, md); |
|
471 |
return finalHash; |
|
472 |
} |
|
473 |
||
474 |
private final static char charArray[] = { |
|
475 |
'0', '1', '2', '3', '4', '5', '6', '7', |
|
476 |
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
|
477 |
}; |
|
478 |
||
479 |
private final static String zeroPad[] = { |
|
480 |
// 0 1 2 3 4 5 6 7 |
|
481 |
"00000000", "0000000", "000000", "00000", "0000", "000", "00", "0" |
|
482 |
}; |
|
483 |
||
484 |
private String encode(String src, char[] passwd, MessageDigest md) { |
|
485 |
try { |
|
486 |
md.update(src.getBytes("ISO-8859-1")); |
|
487 |
} catch (java.io.UnsupportedEncodingException uee) { |
|
488 |
assert false; |
|
489 |
} |
|
490 |
if (passwd != null) { |
|
491 |
byte[] passwdBytes = new byte[passwd.length]; |
|
492 |
for (int i=0; i<passwd.length; i++) |
|
493 |
passwdBytes[i] = (byte)passwd[i]; |
|
494 |
md.update(passwdBytes); |
|
495 |
Arrays.fill(passwdBytes, (byte)0x00); |
|
496 |
} |
|
497 |
byte[] digest = md.digest(); |
|
498 |
||
499 |
StringBuffer res = new StringBuffer(digest.length * 2); |
|
500 |
for (int i = 0; i < digest.length; i++) { |
|
501 |
int hashchar = ((digest[i] >>> 4) & 0xf); |
|
502 |
res.append(charArray[hashchar]); |
|
503 |
hashchar = (digest[i] & 0xf); |
|
504 |
res.append(charArray[hashchar]); |
|
505 |
} |
|
506 |
return res.toString(); |
|
507 |
} |
|
508 |
} |