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/ExportableStreamCipher.java
38853 views
1
/*
2
* Copyright (c) 2010, 2016, 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 ExportableStreamCipher
36
* @author Xuelei Fan
37
*/
38
39
import java.io.IOException;
40
import java.io.InputStream;
41
import java.io.OutputStream;
42
import javax.net.ssl.SSLException;
43
import javax.net.ssl.SSLHandshakeException;
44
import javax.net.ssl.SSLServerSocket;
45
import javax.net.ssl.SSLServerSocketFactory;
46
import javax.net.ssl.SSLSocket;
47
import javax.net.ssl.SSLSocketFactory;
48
49
public class ExportableStreamCipher {
50
51
/*
52
* =============================================================
53
* Set the various variables needed for the tests, then
54
* specify what tests to run on each side.
55
*/
56
57
/*
58
* Should we run the client or server in a separate thread?
59
* Both sides can throw exceptions, but do you have a preference
60
* as to which side should be the main thread.
61
*/
62
static boolean separateServerThread = false;
63
64
/*
65
* Where do we find the keystores?
66
*/
67
static String pathToStores = "../etc";
68
static String keyStoreFile = "keystore";
69
static String trustStoreFile = "truststore";
70
static String passwd = "passphrase";
71
72
/*
73
* Is the server ready to serve?
74
*/
75
volatile static boolean serverReady = false;
76
77
/*
78
* Turn on SSL debugging?
79
*/
80
static boolean debug = false;
81
82
/*
83
* If the client or server is doing some kind of object creation
84
* that the other side depends on, and that thread prematurely
85
* exits, you may experience a hang. The test harness will
86
* terminate all hung threads after its timeout has expired,
87
* currently 3 minutes by default, but you might try to be
88
* smart about it....
89
*/
90
91
/*
92
* Define the server side of the test.
93
*
94
* If the server prematurely exits, serverReady will be set to true
95
* to avoid infinite hangs.
96
*/
97
void doServerSide() throws Exception {
98
SSLServerSocketFactory sslssf =
99
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
100
SSLServerSocket sslServerSocket =
101
(SSLServerSocket) sslssf.createServerSocket(serverPort);
102
103
serverPort = sslServerSocket.getLocalPort();
104
105
/*
106
* Signal Client, we're ready for his connect.
107
*/
108
serverReady = true;
109
110
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
111
InputStream sslIS = sslSocket.getInputStream();
112
OutputStream sslOS = sslSocket.getOutputStream();
113
114
boolean interrupted = false;
115
try {
116
sslIS.read();
117
sslOS.write('A');
118
sslOS.flush();
119
} catch (IOException ioe) {
120
// get the expected exception
121
interrupted = true;
122
} finally {
123
sslSocket.close();
124
}
125
126
if (!interrupted) {
127
throw new SSLHandshakeException(
128
"A weak cipher suite is negotiated, " +
129
"TLSv1.1 must not negotiate the exportable cipher suites.");
130
}
131
}
132
133
/*
134
* Define the client side of the test.
135
*
136
* If the server prematurely exits, serverReady will be set to true
137
* to avoid infinite hangs.
138
*/
139
void doClientSide() throws Exception {
140
141
/*
142
* Wait for server to get started.
143
*/
144
while (!serverReady) {
145
Thread.sleep(50);
146
}
147
148
SSLSocketFactory sslsf =
149
(SSLSocketFactory) SSLSocketFactory.getDefault();
150
SSLSocket sslSocket = (SSLSocket)
151
sslsf.createSocket("localhost", serverPort);
152
153
// enable TLSv1.1 only
154
sslSocket.setEnabledProtocols(new String[] {"TLSv1.1"});
155
156
// enable a exportable stream cipher
157
sslSocket.setEnabledCipherSuites(
158
new String[] {"SSL_RSA_EXPORT_WITH_RC4_40_MD5"});
159
160
InputStream sslIS = sslSocket.getInputStream();
161
OutputStream sslOS = sslSocket.getOutputStream();
162
163
boolean interrupted = false;
164
try {
165
sslOS.write('B');
166
sslOS.flush();
167
sslIS.read();
168
} catch (SSLException ssle) {
169
// get the expected exception
170
interrupted = true;
171
} finally {
172
sslSocket.close();
173
}
174
175
if (!interrupted) {
176
throw new SSLHandshakeException(
177
"A weak cipher suite is negotiated, " +
178
"TLSv1.1 must not negotiate the exportable cipher suites.");
179
}
180
}
181
182
/*
183
* =============================================================
184
* The remainder is just support stuff
185
*/
186
187
// use any free port by default
188
volatile int serverPort = 0;
189
190
volatile Exception serverException = null;
191
volatile Exception clientException = null;
192
193
public static void main(String[] args) throws Exception {
194
String keyFilename =
195
System.getProperty("test.src", ".") + "/" + pathToStores +
196
"/" + keyStoreFile;
197
String trustFilename =
198
System.getProperty("test.src", ".") + "/" + pathToStores +
199
"/" + trustStoreFile;
200
201
System.setProperty("javax.net.ssl.keyStore", keyFilename);
202
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
203
System.setProperty("javax.net.ssl.trustStore", trustFilename);
204
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
205
206
if (debug)
207
System.setProperty("javax.net.debug", "all");
208
209
/*
210
* Start the tests.
211
*/
212
new ExportableStreamCipher();
213
}
214
215
Thread clientThread = null;
216
Thread serverThread = null;
217
218
/*
219
* Primary constructor, used to drive remainder of the test.
220
*
221
* Fork off the other side, then do your work.
222
*/
223
ExportableStreamCipher() throws Exception {
224
try {
225
if (separateServerThread) {
226
startServer(true);
227
startClient(false);
228
} else {
229
startClient(true);
230
startServer(false);
231
}
232
} catch (Exception e) {
233
// swallow for now. Show later
234
}
235
236
/*
237
* Wait for other side to close down.
238
*/
239
if (separateServerThread) {
240
serverThread.join();
241
} else {
242
clientThread.join();
243
}
244
245
/*
246
* When we get here, the test is pretty much over.
247
* Which side threw the error?
248
*/
249
Exception local;
250
Exception remote;
251
String whichRemote;
252
253
if (separateServerThread) {
254
remote = serverException;
255
local = clientException;
256
whichRemote = "server";
257
} else {
258
remote = clientException;
259
local = serverException;
260
whichRemote = "client";
261
}
262
263
/*
264
* If both failed, return the curthread's exception, but also
265
* print the remote side Exception
266
*/
267
if ((local != null) && (remote != null)) {
268
System.out.println(whichRemote + " also threw:");
269
remote.printStackTrace();
270
System.out.println();
271
throw local;
272
}
273
274
if (remote != null) {
275
throw remote;
276
}
277
278
if (local != null) {
279
throw local;
280
}
281
}
282
283
void startServer(boolean newThread) throws Exception {
284
if (newThread) {
285
serverThread = new Thread() {
286
public void run() {
287
try {
288
doServerSide();
289
} catch (Exception e) {
290
/*
291
* Our server thread just died.
292
*
293
* Release the client, if not active already...
294
*/
295
System.err.println("Server died...");
296
serverReady = true;
297
serverException = e;
298
}
299
}
300
};
301
serverThread.start();
302
} else {
303
try {
304
doServerSide();
305
} catch (Exception e) {
306
serverException = e;
307
} finally {
308
serverReady = true;
309
}
310
}
311
}
312
313
void startClient(boolean newThread) throws Exception {
314
if (newThread) {
315
clientThread = new Thread() {
316
public void run() {
317
try {
318
doClientSide();
319
} catch (Exception e) {
320
/*
321
* Our client thread just died.
322
*/
323
System.err.println("Client died...");
324
clientException = e;
325
}
326
}
327
};
328
clientThread.start();
329
} else {
330
try {
331
doClientSide();
332
} catch (Exception e) {
333
clientException = e;
334
}
335
}
336
}
337
}
338
339