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