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/ciphersuites/DisabledAlgorithms.java
38853 views
1
/*
2
* Copyright (c) 2015, 2018, 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 8076221 8211883
27
* @summary Check if weak cipher suites are disabled
28
* @run main/othervm DisabledAlgorithms default
29
* @run main/othervm DisabledAlgorithms empty
30
*/
31
32
import java.io.BufferedInputStream;
33
import java.io.BufferedOutputStream;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.OutputStream;
37
import java.security.NoSuchAlgorithmException;
38
import java.security.Security;
39
import java.util.concurrent.TimeUnit;
40
import javax.net.ssl.SSLContext;
41
import javax.net.ssl.SSLHandshakeException;
42
import javax.net.ssl.SSLServerSocket;
43
import javax.net.ssl.SSLServerSocketFactory;
44
import javax.net.ssl.SSLSocket;
45
import javax.net.ssl.SSLSocketFactory;
46
47
public class DisabledAlgorithms {
48
49
private static final String pathToStores = "../etc";
50
private static final String keyStoreFile = "keystore";
51
private static final String trustStoreFile = "truststore";
52
private static final String passwd = "passphrase";
53
54
private static final String keyFilename =
55
System.getProperty("test.src", "./") + "/" + pathToStores +
56
"/" + keyStoreFile;
57
58
private static final String trustFilename =
59
System.getProperty("test.src", "./") + "/" + pathToStores +
60
"/" + trustStoreFile;
61
62
// supported RC4, NULL, and anon cipher suites
63
// it does not contain KRB5 cipher suites because they need a KDC
64
private static final String[] rc4_null_anon_ciphersuites = new String[] {
65
"TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",
66
"TLS_ECDHE_RSA_WITH_RC4_128_SHA",
67
"SSL_RSA_WITH_RC4_128_SHA",
68
"TLS_ECDH_ECDSA_WITH_RC4_128_SHA",
69
"TLS_ECDH_RSA_WITH_RC4_128_SHA",
70
"SSL_RSA_WITH_RC4_128_MD5",
71
"TLS_ECDH_anon_WITH_RC4_128_SHA",
72
"SSL_DH_anon_WITH_RC4_128_MD5",
73
"SSL_RSA_WITH_NULL_MD5",
74
"SSL_RSA_WITH_NULL_SHA",
75
"TLS_RSA_WITH_NULL_SHA256",
76
"TLS_ECDH_ECDSA_WITH_NULL_SHA",
77
"TLS_ECDHE_ECDSA_WITH_NULL_SHA",
78
"TLS_ECDH_RSA_WITH_NULL_SHA",
79
"TLS_ECDHE_RSA_WITH_NULL_SHA",
80
"TLS_ECDH_anon_WITH_NULL_SHA",
81
"SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
82
"SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",
83
"SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",
84
"SSL_DH_anon_WITH_DES_CBC_SHA",
85
"SSL_DH_anon_WITH_RC4_128_MD5",
86
"TLS_DH_anon_WITH_AES_128_CBC_SHA",
87
"TLS_DH_anon_WITH_AES_128_CBC_SHA256",
88
"TLS_DH_anon_WITH_AES_128_GCM_SHA256",
89
"TLS_DH_anon_WITH_AES_256_CBC_SHA",
90
"TLS_DH_anon_WITH_AES_256_CBC_SHA256",
91
"TLS_DH_anon_WITH_AES_256_GCM_SHA384",
92
"TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA",
93
"TLS_ECDH_anon_WITH_AES_128_CBC_SHA",
94
"TLS_ECDH_anon_WITH_AES_256_CBC_SHA",
95
"TLS_ECDH_anon_WITH_NULL_SHA",
96
"TLS_ECDH_anon_WITH_RC4_128_SHA"
97
};
98
99
public static void main(String[] args) throws Exception {
100
if (args.length < 1) {
101
throw new RuntimeException("No parameters specified");
102
}
103
104
System.setProperty("javax.net.ssl.keyStore", keyFilename);
105
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
106
System.setProperty("javax.net.ssl.trustStore", trustFilename);
107
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
108
109
switch (args[0]) {
110
case "default":
111
// use default jdk.tls.disabledAlgorithms
112
System.out.println("jdk.tls.disabledAlgorithms = "
113
+ Security.getProperty("jdk.tls.disabledAlgorithms"));
114
115
// check if RC4, NULL, and anon cipher suites
116
// can't be used by default
117
checkFailure(rc4_null_anon_ciphersuites);
118
break;
119
case "empty":
120
// reset jdk.tls.disabledAlgorithms
121
Security.setProperty("jdk.tls.disabledAlgorithms", "");
122
System.out.println("jdk.tls.disabledAlgorithms = "
123
+ Security.getProperty("jdk.tls.disabledAlgorithms"));
124
125
// check if RC4, NULL, and anon cipher suites can be used
126
// if jdk.tls.disabledAlgorithms is empty
127
checkSuccess(rc4_null_anon_ciphersuites);
128
break;
129
default:
130
throw new RuntimeException("Wrong parameter: " + args[0]);
131
}
132
133
System.out.println("Test passed");
134
}
135
136
/*
137
* Checks if that specified cipher suites cannot be used.
138
*/
139
private static void checkFailure(String[] ciphersuites) throws Exception {
140
try (SSLServer server = SSLServer.init(ciphersuites)) {
141
startNewThread(server);
142
while (!server.isRunning()) {
143
sleep();
144
}
145
146
int port = server.getPort();
147
for (String ciphersuite : ciphersuites) {
148
try (SSLClient client = SSLClient.init(port, ciphersuite)) {
149
client.connect();
150
throw new RuntimeException("Expected SSLHandshakeException "
151
+ "not thrown");
152
} catch (SSLHandshakeException e) {
153
System.out.println("Expected exception on client side: "
154
+ e);
155
}
156
}
157
158
while (server.isRunning()) {
159
sleep();
160
}
161
162
if (!server.sslError()) {
163
throw new RuntimeException("Expected SSL exception "
164
+ "not thrown on server side");
165
}
166
}
167
168
}
169
170
/*
171
* Checks if specified cipher suites can be used.
172
*/
173
private static void checkSuccess(String[] ciphersuites) throws Exception {
174
try (SSLServer server = SSLServer.init(ciphersuites)) {
175
startNewThread(server);
176
while (!server.isRunning()) {
177
sleep();
178
}
179
180
int port = server.getPort();
181
for (String ciphersuite : ciphersuites) {
182
try (SSLClient client = SSLClient.init(port, ciphersuite)) {
183
client.connect();
184
String negotiated = client.getNegotiatedCipherSuite();
185
System.out.println("Negotiated cipher suite: "
186
+ negotiated);
187
if (!negotiated.equals(ciphersuite)) {
188
throw new RuntimeException("Unexpected cipher suite: "
189
+ negotiated);
190
}
191
}
192
}
193
194
server.stop();
195
while (server.isRunning()) {
196
sleep();
197
}
198
199
if (server.error()) {
200
throw new RuntimeException("Unexpected error on server side");
201
}
202
}
203
204
}
205
206
private static Thread startNewThread(SSLServer server) {
207
Thread serverThread = new Thread(server, "SSL server thread");
208
serverThread.setDaemon(true);
209
serverThread.start();
210
return serverThread;
211
}
212
213
private static void sleep() {
214
try {
215
TimeUnit.MILLISECONDS.sleep(50);
216
} catch (InterruptedException e) {
217
// do nothing
218
}
219
}
220
221
static class SSLServer implements Runnable, AutoCloseable {
222
223
private final SSLServerSocket ssocket;
224
private volatile boolean stopped = false;
225
private volatile boolean running = false;
226
private volatile boolean sslError = false;
227
private volatile boolean otherError = false;
228
229
private SSLServer(SSLServerSocket ssocket) {
230
this.ssocket = ssocket;
231
}
232
233
@Override
234
public void run() {
235
System.out.println("Server: started");
236
running = true;
237
while (!stopped) {
238
try (SSLSocket socket = (SSLSocket) ssocket.accept()) {
239
System.out.println("Server: accepted client connection");
240
InputStream in = socket.getInputStream();
241
OutputStream out = socket.getOutputStream();
242
int b = in.read();
243
if (b < 0) {
244
throw new IOException("Unexpected EOF");
245
}
246
System.out.println("Server: send data: " + b);
247
out.write(b);
248
out.flush();
249
socket.getSession().invalidate();
250
} catch (SSLHandshakeException e) {
251
System.out.println("Server: run: " + e);
252
sslError = true;
253
stopped = true;
254
} catch (IOException e) {
255
if (!stopped) {
256
System.out.println("Server: run: unexpected exception: "
257
+ e);
258
e.printStackTrace();
259
otherError = true;
260
stopped = true;
261
} else {
262
System.out.println("Server: run: " + e);
263
System.out.println("The exception above occurred "
264
+ "because socket was closed, "
265
+ "please ignore it");
266
}
267
}
268
}
269
270
System.out.println("Server: finished");
271
running = false;
272
}
273
274
int getPort() {
275
return ssocket.getLocalPort();
276
}
277
278
String[] getEnabledCiperSuites() {
279
return ssocket.getEnabledCipherSuites();
280
}
281
282
boolean isRunning() {
283
return running;
284
}
285
286
boolean sslError() {
287
return sslError;
288
}
289
290
boolean error() {
291
return sslError || otherError;
292
}
293
294
void stop() {
295
stopped = true;
296
if (!ssocket.isClosed()) {
297
try {
298
System.out.println("Server: close socket");
299
ssocket.close();
300
} catch (IOException e) {
301
System.out.println("Server: close: " + e);
302
}
303
}
304
}
305
306
@Override
307
public void close() {
308
stop();
309
}
310
311
static SSLServer init(String[] ciphersuites)
312
throws IOException {
313
SSLServerSocketFactory ssf = (SSLServerSocketFactory)
314
SSLServerSocketFactory.getDefault();
315
SSLServerSocket ssocket = (SSLServerSocket)
316
ssf.createServerSocket(0);
317
318
if (ciphersuites != null) {
319
System.out.println("Server: enable cipher suites: "
320
+ java.util.Arrays.toString(ciphersuites));
321
ssocket.setEnabledCipherSuites(ciphersuites);
322
}
323
324
return new SSLServer(ssocket);
325
}
326
}
327
328
static class SSLClient implements AutoCloseable {
329
330
private final SSLSocket socket;
331
332
private SSLClient(SSLSocket socket) {
333
this.socket = socket;
334
}
335
336
void connect() throws IOException {
337
System.out.println("Client: connect to server");
338
try (
339
BufferedInputStream bis = new BufferedInputStream(
340
socket.getInputStream());
341
BufferedOutputStream bos = new BufferedOutputStream(
342
socket.getOutputStream())) {
343
bos.write('x');
344
bos.flush();
345
346
int read = bis.read();
347
if (read < 0) {
348
throw new IOException("Client: couldn't read a response");
349
}
350
socket.getSession().invalidate();
351
}
352
}
353
354
String[] getEnabledCiperSuites() {
355
return socket.getEnabledCipherSuites();
356
}
357
358
String getNegotiatedCipherSuite() {
359
return socket.getSession().getCipherSuite();
360
}
361
362
@Override
363
public void close() throws Exception {
364
if (!socket.isClosed()) {
365
try {
366
socket.close();
367
} catch (IOException e) {
368
System.out.println("Client: close: " + e);
369
}
370
}
371
}
372
373
static SSLClient init(int port)
374
throws NoSuchAlgorithmException, IOException {
375
return init(port, null);
376
}
377
378
static SSLClient init(int port, String ciphersuite)
379
throws NoSuchAlgorithmException, IOException {
380
SSLContext context = SSLContext.getDefault();
381
SSLSocketFactory ssf = (SSLSocketFactory)
382
context.getSocketFactory();
383
SSLSocket socket = (SSLSocket) ssf.createSocket("localhost", port);
384
385
if (ciphersuite != null) {
386
System.out.println("Client: enable cipher suite: "
387
+ ciphersuite);
388
socket.setEnabledCipherSuites(new String[] { ciphersuite });
389
}
390
391
return new SSLClient(socket);
392
}
393
394
}
395
396
397
}
398
399