Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/B6870935.java
41149 views
1
/*
2
* Copyright (c) 2001, 2021, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 6870935
27
* @modules java.base/sun.net.www
28
* @run main/othervm -Dhttp.nonProxyHosts="" -Dhttp.auth.digest.validateProxy=true B6870935
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true
30
* -Dhttp.nonProxyHosts="" -Dhttp.auth.digest.validateProxy=true B6870935
31
*/
32
33
import java.io.*;
34
import java.util.*;
35
import java.net.*;
36
import java.security.*;
37
import sun.net.www.*;
38
39
/* This is one simple test of the RFC2617 digest authentication behavior
40
* It specifically tests that the client correctly checks the returned
41
* Authentication-Info header field from the server and throws an exception
42
* if the password is wrong
43
*/
44
45
public class B6870935 {
46
47
static char[] passwd = "password".toCharArray();
48
static String username = "user";
49
static String nonce = "abcdefghijklmnopqrstuvwxyz";
50
static String realm = "wallyworld";
51
static String uri = "http://www.ibm.com";
52
static volatile boolean error = false;
53
54
static class DigestServer extends Thread {
55
56
ServerSocket s;
57
InputStream is;
58
OutputStream os;
59
int port;
60
61
String reply1 = "HTTP/1.1 407 Proxy Authentication Required\r\n"+
62
"Proxy-Authenticate: Digest realm=\""+realm+"\" domain=/ "+
63
"nonce=\""+nonce+"\" qop=\"auth\"\r\n\r\n";
64
65
String reply2 = "HTTP/1.1 200 OK\r\n" +
66
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
67
"Server: Apache/1.3.14 (Unix)\r\n" +
68
"Content-Type: text/html; charset=iso-8859-1\r\n" +
69
"Transfer-encoding: chunked\r\n\r\n"+
70
"B\r\nHelloWorld1\r\n"+
71
"B\r\nHelloWorld2\r\n"+
72
"B\r\nHelloWorld3\r\n"+
73
"B\r\nHelloWorld4\r\n"+
74
"B\r\nHelloWorld5\r\n"+
75
"0\r\n"+
76
"Proxy-Authentication-Info: ";
77
78
DigestServer (ServerSocket y) {
79
s = y;
80
port = s.getLocalPort();
81
}
82
83
public void run () {
84
try {
85
System.out.println("Server started");
86
Socket s1 = s.accept ();
87
is = s1.getInputStream ();
88
os = s1.getOutputStream ();
89
is.read ();
90
os.write (reply1.getBytes());
91
System.out.println("First response sent");
92
Thread.sleep (2000);
93
s1.close ();
94
System.out.println("First connection closed");
95
96
s1 = s.accept ();
97
is = s1.getInputStream ();
98
os = s1.getOutputStream ();
99
// is.read ();
100
// need to get the cnonce out of the response
101
MessageHeader header = new MessageHeader (is);
102
String raw = header.findValue ("Proxy-Authorization");
103
HeaderParser parser = new HeaderParser (raw);
104
String cnonce = parser.findValue ("cnonce");
105
String cnstring = parser.findValue ("nc");
106
String clientrsp = parser.findValue ("response");
107
String expected = computeDigest(
108
true, username,passwd,realm,
109
"GET", uri, nonce, cnonce, cnstring
110
);
111
if (!expected.equals(clientrsp)) {
112
s1.close ();
113
s.close ();
114
error = true;
115
return;
116
}
117
118
String reply = reply2 + getAuthorization (
119
realm, false, uri, "GET", cnonce,
120
cnstring, passwd, username
121
) +"\r\n";
122
os.write (reply.getBytes());
123
System.out.println("Second response sent");
124
Thread.sleep (2000);
125
s1.close ();
126
System.out.println("Second connection closed");
127
}
128
catch (Exception e) {
129
System.out.println (e);
130
e.printStackTrace();
131
} finally {
132
System.out.println("Server finished");
133
}
134
}
135
136
private String getAuthorization (String realm, boolean isRequest, String uri, String method, String cnonce, String cnstring, char[] password, String username) {
137
String response;
138
139
try {
140
response = computeDigest(isRequest, username,passwd,realm,
141
method, uri, nonce, cnonce, cnstring);
142
} catch (NoSuchAlgorithmException ex) {
143
return null;
144
}
145
146
String value = "Digest"
147
+ " qop=\"auth"
148
+ "\", cnonce=\"" + cnonce
149
+ "\", rspauth=\"" + response
150
+ "\", nc=\"" + cnstring + "\"";
151
return (value+ "\r\n");
152
}
153
154
private String computeDigest(
155
boolean isRequest, String userName, char[] password,
156
String realm, String connMethod,
157
String requestURI, String nonceString,
158
String cnonce, String ncValue
159
) throws NoSuchAlgorithmException
160
{
161
162
String A1, HashA1;
163
164
MessageDigest md = MessageDigest.getInstance("MD5");
165
166
{
167
A1 = userName + ":" + realm + ":";
168
HashA1 = encode(A1, password, md);
169
}
170
171
String A2;
172
if (isRequest) {
173
A2 = connMethod + ":" + requestURI;
174
} else {
175
A2 = ":" + requestURI;
176
}
177
String HashA2 = encode(A2, null, md);
178
String combo, finalHash;
179
180
{ /* RRC2617 when qop=auth */
181
combo = HashA1+ ":" + nonceString + ":" + ncValue + ":" +
182
cnonce + ":auth:" +HashA2;
183
184
}
185
finalHash = encode(combo, null, md);
186
return finalHash;
187
}
188
189
private String encode(String src, char[] passwd, MessageDigest md) {
190
md.update(src.getBytes());
191
if (passwd != null) {
192
byte[] passwdBytes = new byte[passwd.length];
193
for (int i=0; i<passwd.length; i++)
194
passwdBytes[i] = (byte)passwd[i];
195
md.update(passwdBytes);
196
Arrays.fill(passwdBytes, (byte)0x00);
197
}
198
byte[] digest = md.digest();
199
return HexFormat.of().formatHex(digest);
200
}
201
}
202
203
204
static class MyAuthenticator extends Authenticator {
205
public MyAuthenticator () {
206
super ();
207
}
208
209
public PasswordAuthentication getPasswordAuthentication ()
210
{
211
return (new PasswordAuthentication (username, passwd));
212
}
213
}
214
215
216
public static void main(String[] args) throws Exception {
217
int nLoops = 1;
218
int nSize = 10;
219
int port, n =0;
220
byte b[] = new byte[nSize];
221
DigestServer server;
222
ServerSocket sock;
223
224
InetAddress address = InetAddress.getLoopbackAddress();
225
InetAddress resolved = InetAddress.getByName(address.getHostName());
226
System.out.println("Lookup: "
227
+ address + " -> \"" + address.getHostName() + "\" -> "
228
+ resolved);
229
String proxyHost = address.equals(resolved)
230
? address.getHostName()
231
: address.getHostAddress();
232
try {
233
sock = new ServerSocket();
234
sock.bind(new InetSocketAddress(address, 0));
235
port = sock.getLocalPort ();
236
}
237
catch (Exception e) {
238
System.out.println ("Exception: " + e);
239
return;
240
}
241
242
server = new DigestServer(sock);
243
server.start ();
244
245
try {
246
Authenticator.setDefault (new MyAuthenticator ());
247
SocketAddress addr = InetSocketAddress.createUnresolved(proxyHost, port);
248
Proxy proxy = new Proxy (Proxy.Type.HTTP, addr);
249
String s = "http://www.ibm.com";
250
URL url = new URL(s);
251
System.out.println("opening connection through proxy: " + addr);
252
java.net.URLConnection conURL = url.openConnection(proxy);
253
254
InputStream in = conURL.getInputStream();
255
int c;
256
while ((c = in.read ()) != -1) {
257
}
258
in.close ();
259
}
260
catch(IOException e) {
261
e.printStackTrace();
262
error = true;
263
sock.close();
264
} finally {
265
server.join();
266
}
267
if (error) {
268
throw new RuntimeException ("Error in test");
269
}
270
}
271
}
272
273