Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/ReverseNameLookup.java
41152 views
1
/*
2
* Copyright (c) 2002, 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 4748292
27
* @library /test/lib
28
* @summary Prevent/Disable reverse name lookups with JSSE SSL sockets
29
* @run main/othervm ReverseNameLookup -Djava.net.preferIPv4Stack
30
*
31
* SunJSSE does not support dynamic system properties, no way to re-use
32
* system properties in samevm/agentvm mode.
33
*/
34
35
import jdk.test.lib.net.IPSupport;
36
37
import java.io.*;
38
import java.net.*;
39
import javax.net.ssl.*;
40
41
public class ReverseNameLookup {
42
43
/*
44
* =============================================================
45
* Set the various variables needed for the tests, then
46
* specify what tests to run on each side.
47
*/
48
49
/*
50
* Should we run the client or server in a separate thread?
51
* Both sides can throw exceptions, but do you have a preference
52
* as to which side should be the main thread.
53
*/
54
static boolean separateServerThread = true;
55
56
/*
57
* Where do we find the keystores?
58
*/
59
static String pathToStores = "../../../../javax/net/ssl/etc";
60
static String keyStoreFile = "keystore";
61
static String trustStoreFile = "truststore";
62
static String passwd = "passphrase";
63
64
/*
65
* Is the server ready to serve?
66
*/
67
volatile static boolean serverReady = false;
68
69
/*
70
* Turn on SSL debugging?
71
*/
72
static boolean debug = false;
73
74
/*
75
* If the client or server is doing some kind of object creation
76
* that the other side depends on, and that thread prematurely
77
* exits, you may experience a hang. The test harness will
78
* terminate all hung threads after its timeout has expired,
79
* currently 3 minutes by default, but you might try to be
80
* smart about it....
81
*/
82
83
/*
84
* Define the server side of the test.
85
*
86
* If the server prematurely exits, serverReady will be set to true
87
* to avoid infinite hangs.
88
*/
89
void doServerSide() throws Exception {
90
SSLServerSocketFactory sslssf =
91
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
92
InetSocketAddress socketAddress =
93
new InetSocketAddress(InetAddress.getLoopbackAddress(), serverPort);
94
SSLServerSocket sslServerSocket =
95
(SSLServerSocket) sslssf.createServerSocket();
96
sslServerSocket.bind(socketAddress);
97
98
serverPort = sslServerSocket.getLocalPort();
99
100
/*
101
* Signal Client, we're ready for his connect.
102
*/
103
serverReady = true;
104
105
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
106
InputStream sslIS = sslSocket.getInputStream();
107
OutputStream sslOS = sslSocket.getOutputStream();
108
109
sslIS.read();
110
sslOS.write(85);
111
sslOS.flush();
112
113
sslSocket.close();
114
}
115
116
/*
117
* Define the client side of the test.
118
*
119
* If the server prematurely exits, serverReady will be set to true
120
* to avoid infinite hangs.
121
*/
122
void doClientSide() throws Exception {
123
124
/*
125
* Wait for server to get started.
126
*/
127
while (!serverReady) {
128
Thread.sleep(50);
129
}
130
131
SSLSocketFactory sslsf =
132
(SSLSocketFactory) SSLSocketFactory.getDefault();
133
SSLSocket sslSocket = (SSLSocket)
134
sslsf.createSocket("127.0.0.1", serverPort);
135
136
InputStream sslIS = sslSocket.getInputStream();
137
OutputStream sslOS = sslSocket.getOutputStream();
138
139
sslOS.write(280);
140
sslOS.flush();
141
sslIS.read();
142
SSLSession session = sslSocket.getSession();
143
if (!session.getPeerHost().equals("127.0.0.1")) {
144
throw new RuntimeException("we shouldn't do reverse name lookup");
145
}
146
sslSocket.close();
147
}
148
149
/*
150
* =============================================================
151
* The remainder is just support stuff
152
*/
153
154
// use any free port by default
155
volatile int serverPort = 0;
156
157
volatile Exception serverException = null;
158
volatile Exception clientException = null;
159
160
public static void main(String[] args) throws Exception {
161
IPSupport.throwSkippedExceptionIfNonOperational();
162
String keyFilename =
163
System.getProperty("test.src", "./") + "/" + pathToStores +
164
"/" + keyStoreFile;
165
String trustFilename =
166
System.getProperty("test.src", "./") + "/" + pathToStores +
167
"/" + trustStoreFile;
168
169
System.setProperty("javax.net.ssl.keyStore", keyFilename);
170
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
171
System.setProperty("javax.net.ssl.trustStore", trustFilename);
172
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
173
174
if (debug)
175
System.setProperty("javax.net.debug", "all");
176
177
/*
178
* Start the tests.
179
*/
180
new ReverseNameLookup();
181
}
182
183
Thread clientThread = null;
184
Thread serverThread = null;
185
186
/*
187
* Primary constructor, used to drive remainder of the test.
188
*
189
* Fork off the other side, then do your work.
190
*/
191
ReverseNameLookup() throws Exception {
192
if (separateServerThread) {
193
startServer(true);
194
startClient(false);
195
} else {
196
startClient(true);
197
startServer(false);
198
}
199
200
/*
201
* Wait for other side to close down.
202
*/
203
if (separateServerThread) {
204
serverThread.join();
205
} else {
206
clientThread.join();
207
}
208
209
/*
210
* When we get here, the test is pretty much over.
211
*
212
* If the main thread excepted, that propagates back
213
* immediately. If the other thread threw an exception, we
214
* should report back.
215
*/
216
if (serverException != null)
217
throw serverException;
218
if (clientException != null)
219
throw clientException;
220
}
221
222
void startServer(boolean newThread) throws Exception {
223
if (newThread) {
224
serverThread = new Thread() {
225
public void run() {
226
try {
227
doServerSide();
228
} catch (Exception e) {
229
/*
230
* Our server thread just died.
231
*
232
* Release the client, if not active already...
233
*/
234
System.err.println("Server died...");
235
serverReady = true;
236
serverException = e;
237
}
238
}
239
};
240
serverThread.start();
241
} else {
242
try {
243
doServerSide();
244
} finally {
245
serverReady = true;
246
}
247
}
248
}
249
250
void startClient(boolean newThread) throws Exception {
251
if (newThread) {
252
clientThread = new Thread() {
253
public void run() {
254
try {
255
doClientSide();
256
} catch (Exception e) {
257
/*
258
* Our client thread just died.
259
*/
260
System.err.println("Client died...");
261
clientException = e;
262
}
263
}
264
};
265
clientThread.start();
266
} else {
267
doClientSide();
268
}
269
}
270
}
271
272