Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/TLSv11/EmptyCertificateAuthorities.java
38853 views
1
/*
2
* Copyright (c) 2010, 2015, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
//
27
// SunJSSE does not support dynamic system properties, no way to re-use
28
// system properties in samevm/agentvm mode.
29
//
30
31
/*
32
* @test
33
* @bug 4873188
34
* @summary Support TLS 1.1
35
* @run main/othervm EmptyCertificateAuthorities
36
* @author Xuelei Fan
37
*/
38
39
import java.io.*;
40
import java.net.*;
41
import java.security.*;
42
import java.security.cert.*;
43
import javax.net.ssl.*;
44
45
public class EmptyCertificateAuthorities {
46
47
/*
48
* =============================================================
49
* Set the various variables needed for the tests, then
50
* specify what tests to run on each side.
51
*/
52
53
/*
54
* Should we run the client or server in a separate thread?
55
* Both sides can throw exceptions, but do you have a preference
56
* as to which side should be the main thread.
57
*/
58
static boolean separateServerThread = false;
59
60
/*
61
* Where do we find the keystores?
62
*/
63
static String pathToStores = "../etc";
64
static String keyStoreFile = "keystore";
65
static String trustStoreFile = "truststore";
66
static String passwd = "passphrase";
67
68
/*
69
* Is the server ready to serve?
70
*/
71
volatile static boolean serverReady = false;
72
73
/*
74
* Turn on SSL debugging?
75
*/
76
static boolean debug = false;
77
78
/*
79
* If the client or server is doing some kind of object creation
80
* that the other side depends on, and that thread prematurely
81
* exits, you may experience a hang. The test harness will
82
* terminate all hung threads after its timeout has expired,
83
* currently 3 minutes by default, but you might try to be
84
* smart about it....
85
*/
86
87
/*
88
* Define the server side of the test.
89
*
90
* If the server prematurely exits, serverReady will be set to true
91
* to avoid infinite hangs.
92
*/
93
void doServerSide() throws Exception {
94
SSLServerSocketFactory sslssf = getSSLServerSF();
95
SSLServerSocket sslServerSocket =
96
(SSLServerSocket) sslssf.createServerSocket(serverPort);
97
98
// require client authentication.
99
sslServerSocket.setNeedClientAuth(true);
100
101
serverPort = sslServerSocket.getLocalPort();
102
103
/*
104
* Signal Client, we're ready for his connect.
105
*/
106
serverReady = true;
107
108
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
109
InputStream sslIS = sslSocket.getInputStream();
110
OutputStream sslOS = sslSocket.getOutputStream();
111
112
sslIS.read();
113
sslOS.write('A');
114
sslOS.flush();
115
116
sslSocket.close();
117
}
118
119
/*
120
* Define the client side of the test.
121
*
122
* If the server prematurely exits, serverReady will be set to true
123
* to avoid infinite hangs.
124
*/
125
void doClientSide() throws Exception {
126
127
/*
128
* Wait for server to get started.
129
*/
130
while (!serverReady) {
131
Thread.sleep(50);
132
}
133
134
SSLSocketFactory sslsf =
135
(SSLSocketFactory) SSLSocketFactory.getDefault();
136
SSLSocket sslSocket = (SSLSocket)
137
sslsf.createSocket("localhost", serverPort);
138
139
// enable TLSv1.1 only
140
sslSocket.setEnabledProtocols(new String[] {"TLSv1.1"});
141
142
InputStream sslIS = sslSocket.getInputStream();
143
OutputStream sslOS = sslSocket.getOutputStream();
144
145
sslOS.write('B');
146
sslOS.flush();
147
sslIS.read();
148
149
sslSocket.close();
150
}
151
152
private SSLServerSocketFactory getSSLServerSF() throws Exception {
153
154
char [] password =
155
System.getProperty("javax.net.ssl.keyStorePassword").toCharArray();
156
String keyFilename = System.getProperty("javax.net.ssl.keyStore");
157
158
KeyStore ks = KeyStore.getInstance("JKS");
159
ks.load(new FileInputStream(keyFilename), password);
160
161
KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");
162
kmf.init(ks, password);
163
164
KeyManager[] kms = kmf.getKeyManagers();
165
TrustManager[] tms = new MyX509TM[] {new MyX509TM()};
166
167
SSLContext ctx = SSLContext.getInstance("TLS");
168
ctx.init(kms, tms, null);
169
170
return ctx.getServerSocketFactory();
171
}
172
173
174
static class MyX509TM implements X509TrustManager {
175
X509TrustManager tm;
176
177
public void checkClientTrusted(X509Certificate[] chain,
178
String authType) throws CertificateException {
179
if (tm == null) {
180
initialize();
181
}
182
tm.checkClientTrusted(chain, authType);
183
}
184
185
public void checkServerTrusted(X509Certificate[] chain,
186
String authType) throws CertificateException {
187
if (tm == null) {
188
initialize();
189
}
190
tm.checkServerTrusted(chain, authType);
191
}
192
193
public X509Certificate[] getAcceptedIssuers() {
194
// always return empty array
195
return new X509Certificate[0];
196
}
197
198
private void initialize() throws CertificateException {
199
String passwd =
200
System.getProperty("javax.net.ssl.trustStorePassword");
201
char [] password = passwd.toCharArray();
202
String trustFilename =
203
System.getProperty("javax.net.ssl.trustStore");
204
205
try {
206
KeyStore ks = KeyStore.getInstance("JKS");
207
ks.load(new FileInputStream(trustFilename), password);
208
209
TrustManagerFactory tmf =
210
TrustManagerFactory.getInstance("PKIX");
211
tmf.init(ks);
212
tm = (X509TrustManager)tmf.getTrustManagers()[0];
213
} catch (Exception e) {
214
throw new CertificateException("Unable to initialize TM");
215
}
216
217
}
218
}
219
220
/*
221
* =============================================================
222
* The remainder is just support stuff
223
*/
224
225
// use any free port by default
226
volatile int serverPort = 0;
227
228
volatile Exception serverException = null;
229
volatile Exception clientException = null;
230
231
public static void main(String[] args) throws Exception {
232
// MD5 is used in this test case, don't disable MD5 algorithm.
233
Security.setProperty("jdk.certpath.disabledAlgorithms",
234
"MD2, RSA keySize < 1024");
235
Security.setProperty("jdk.tls.disabledAlgorithms",
236
"SSLv3, RC4, DH keySize < 768");
237
238
String keyFilename =
239
System.getProperty("test.src", ".") + "/" + pathToStores +
240
"/" + keyStoreFile;
241
String trustFilename =
242
System.getProperty("test.src", ".") + "/" + pathToStores +
243
"/" + trustStoreFile;
244
245
System.setProperty("javax.net.ssl.keyStore", keyFilename);
246
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
247
System.setProperty("javax.net.ssl.trustStore", trustFilename);
248
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
249
250
if (debug)
251
System.setProperty("javax.net.debug", "all");
252
253
/*
254
* Start the tests.
255
*/
256
new EmptyCertificateAuthorities();
257
}
258
259
Thread clientThread = null;
260
Thread serverThread = null;
261
262
/*
263
* Primary constructor, used to drive remainder of the test.
264
*
265
* Fork off the other side, then do your work.
266
*/
267
EmptyCertificateAuthorities() throws Exception {
268
try {
269
if (separateServerThread) {
270
startServer(true);
271
startClient(false);
272
} else {
273
startClient(true);
274
startServer(false);
275
}
276
} catch (Exception e) {
277
// swallow for now. Show later
278
}
279
280
/*
281
* Wait for other side to close down.
282
*/
283
if (separateServerThread) {
284
serverThread.join();
285
} else {
286
clientThread.join();
287
}
288
289
/*
290
* When we get here, the test is pretty much over.
291
* Which side threw the error?
292
*/
293
Exception local;
294
Exception remote;
295
String whichRemote;
296
297
if (separateServerThread) {
298
remote = serverException;
299
local = clientException;
300
whichRemote = "server";
301
} else {
302
remote = clientException;
303
local = serverException;
304
whichRemote = "client";
305
}
306
307
/*
308
* If both failed, return the curthread's exception, but also
309
* print the remote side Exception
310
*/
311
if ((local != null) && (remote != null)) {
312
System.out.println(whichRemote + " also threw:");
313
remote.printStackTrace();
314
System.out.println();
315
throw local;
316
}
317
318
if (remote != null) {
319
throw remote;
320
}
321
322
if (local != null) {
323
throw local;
324
}
325
}
326
327
void startServer(boolean newThread) throws Exception {
328
if (newThread) {
329
serverThread = new Thread() {
330
public void run() {
331
try {
332
doServerSide();
333
} catch (Exception e) {
334
/*
335
* Our server thread just died.
336
*
337
* Release the client, if not active already...
338
*/
339
System.err.println("Server died...");
340
serverReady = true;
341
serverException = e;
342
}
343
}
344
};
345
serverThread.start();
346
} else {
347
try {
348
doServerSide();
349
} catch (Exception e) {
350
serverException = e;
351
} finally {
352
serverReady = true;
353
}
354
}
355
}
356
357
void startClient(boolean newThread) throws Exception {
358
if (newThread) {
359
clientThread = new Thread() {
360
public void run() {
361
try {
362
doClientSide();
363
} catch (Exception e) {
364
/*
365
* Our client thread just died.
366
*/
367
System.err.println("Client died...");
368
clientException = e;
369
}
370
}
371
};
372
clientThread.start();
373
} else {
374
try {
375
doClientSide();
376
} catch (Exception e) {
377
clientException = e;
378
}
379
}
380
}
381
}
382
383