Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/HandshakeOutStream/NullCerts.java
41152 views
1
/*
2
* Copyright (c) 2001, 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 4453053
27
* @summary If a server shuts down correctly during handshaking, the client
28
* doesn't see it.
29
* @run main/othervm NullCerts
30
*
31
* SunJSSE does not support dynamic system properties, no way to re-use
32
* system properties in samevm/agentvm mode.
33
* @author Brad Wetmore
34
*/
35
36
import java.io.*;
37
import java.net.*;
38
import java.security.*;
39
import javax.net.ssl.*;
40
41
public class NullCerts {
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
private static boolean separateServerThread = true;
55
56
/*
57
* Where do we find the keystores?
58
*/
59
private final static String pathToStores = "../../../../javax/net/ssl/etc";
60
private final static String keyStoreFile = "keystore";
61
private final static String trustStoreFile = "truststore";
62
private final static String passwd = "passphrase";
63
private final static char[] cpasswd = "passphrase".toCharArray();
64
65
/*
66
* Is the server ready to serve?
67
*/
68
volatile static boolean serverReady = false;
69
70
/*
71
* Turn on SSL debugging?
72
*/
73
private final static boolean DEBUG = false;
74
75
/*
76
* If the client or server is doing some kind of object creation
77
* that the other side depends on, and that thread prematurely
78
* exits, you may experience a hang. The test harness will
79
* terminate all hung threads after its timeout has expired,
80
* currently 3 minutes by default, but you might try to be
81
* smart about it....
82
*/
83
84
/*
85
* Define the server side of the test.
86
*
87
* If the server prematurely exits, serverReady will be set to true
88
* to avoid infinite hangs.
89
*/
90
private void doServerSide() throws Exception {
91
SSLServerSocketFactory sslssf =
92
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
93
SSLServerSocket sslServerSocket =
94
(SSLServerSocket) sslssf.createServerSocket(serverPort, 3);
95
sslServerSocket.setNeedClientAuth(true);
96
97
serverPort = sslServerSocket.getLocalPort();
98
99
/*
100
* Signal Client, we're ready for his connect.
101
*/
102
serverReady = true;
103
104
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
105
InputStream sslIS = sslSocket.getInputStream();
106
OutputStream sslOS = sslSocket.getOutputStream();
107
108
try {
109
sslIS.read();
110
sslOS.write(85);
111
sslOS.flush();
112
} catch (SSLHandshakeException e) {
113
System.out.println(
114
"Should see a null cert chain exception for server: "
115
+ e.toString());
116
}
117
118
sslSocket.close();
119
System.out.println("Server done and exiting!");
120
}
121
122
/*
123
* Define the client side of the test.
124
*
125
* If the server prematurely exits, serverReady will be set to true
126
* to avoid infinite hangs.
127
*/
128
private void doClientSide() throws Exception {
129
130
/*
131
* Wait for server to get started.
132
*/
133
while (!serverReady) {
134
Thread.sleep(50);
135
}
136
137
System.out.println("Starting test");
138
139
KeyStore ks = KeyStore.getInstance("JKS");
140
KeyStore uks = KeyStore.getInstance("JKS");
141
SSLContext ctx = SSLContext.getInstance("TLS");
142
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
143
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
144
145
uks.load(new FileInputStream(unknownFilename), cpasswd);
146
kmf.init(uks, cpasswd);
147
148
ks.load(new FileInputStream(trustFilename), cpasswd);
149
tmf.init(ks);
150
151
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
152
153
SSLSocketFactory sslsf =
154
(SSLSocketFactory) ctx.getSocketFactory();
155
SSLSocket sslSocket = (SSLSocket)
156
sslsf.createSocket("localhost", serverPort);
157
158
InputStream sslIS = sslSocket.getInputStream();
159
OutputStream sslOS = sslSocket.getOutputStream();
160
161
try {
162
sslOS.write(280);
163
sslOS.flush();
164
sslIS.read();
165
166
sslSocket.close();
167
} catch (IOException e) {
168
String str =
169
"\nYou will either see a bad_certificate SSLException\n" +
170
"or an IOException if the server shutdown while the\n" +
171
"client was still sending the remainder of its \n" +
172
"handshake data.";
173
System.out.println(str + e.toString());
174
}
175
}
176
177
/*
178
* =============================================================
179
* The remainder is just support stuff
180
*/
181
182
// use any free port by default
183
volatile int serverPort = 0;
184
185
private volatile Exception serverException = null;
186
private volatile Exception clientException = null;
187
188
private final static String keyFilename =
189
System.getProperty("test.src", ".") + "/" + pathToStores +
190
"/" + keyStoreFile;
191
private final static String trustFilename =
192
System.getProperty("test.src", ".") + "/" + pathToStores +
193
"/" + trustStoreFile;
194
private final static String unknownFilename =
195
System.getProperty("test.src", ".") + "/" + pathToStores +
196
"/" + "unknown_keystore";
197
198
// Used for running test standalone
199
public static void main(String[] args) throws Exception {
200
201
String testRoot = System.getProperty("test.src", ".");
202
System.setProperty("javax.net.ssl.keyStore", keyFilename);
203
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
204
System.setProperty("javax.net.ssl.trustStore", trustFilename);
205
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
206
207
if (DEBUG)
208
System.setProperty("javax.net.debug", "all");
209
210
/*
211
* Start the tests.
212
*/
213
new NullCerts();
214
}
215
216
private Thread clientThread = null;
217
private Thread serverThread = null;
218
219
/*
220
* Primary constructor, used to drive remainder of the test.
221
*
222
* Fork off the other side, then do your work.
223
*/
224
NullCerts() throws Exception {
225
226
if (separateServerThread) {
227
startServer(true);
228
startClient(false);
229
} else {
230
startClient(true);
231
startServer(false);
232
}
233
234
/*
235
* Wait for other side to close down.
236
*/
237
if (separateServerThread) {
238
serverThread.join();
239
} else {
240
clientThread.join();
241
}
242
243
/*
244
* When we get here, the test is pretty much over.
245
*
246
* If the main thread excepted, that propagates back
247
* immediately. If the other thread threw an exception, we
248
* should report back.
249
*/
250
if (serverException != null) {
251
System.err.print("Server Exception:");
252
throw serverException;
253
}
254
if (clientException != null) {
255
System.err.print("Client Exception:");
256
throw clientException;
257
}
258
}
259
260
private void startServer(boolean newThread) throws Exception {
261
if (newThread) {
262
serverThread = new Thread() {
263
public void run() {
264
try {
265
doServerSide();
266
} catch (Exception e) {
267
/*
268
* Our server thread just died.
269
*
270
* Release the client, if not active already...
271
*/
272
System.err.println("Server died...");
273
serverReady = true;
274
serverException = e;
275
}
276
}
277
};
278
serverThread.start();
279
} else {
280
doServerSide();
281
}
282
}
283
284
private void startClient(boolean newThread) throws Exception {
285
if (newThread) {
286
clientThread = new Thread() {
287
public void run() {
288
try {
289
doClientSide();
290
} catch (Exception e) {
291
/*
292
* Our client thread just died.
293
*/
294
System.err.println("Client died...");
295
clientException = e;
296
}
297
}
298
};
299
clientThread.start();
300
} else {
301
doClientSide();
302
}
303
}
304
}
305
306