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/SSLSession/RenegotiateTLS13.java
38853 views
1
/*
2
* Copyright (c) 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
* @run main/othervm -Djavax.net.debug=ssl RenegotiateTLS13
27
*/
28
29
import javax.net.ssl.KeyManagerFactory;
30
import javax.net.ssl.SSLContext;
31
import javax.net.ssl.SSLServerSocket;
32
import javax.net.ssl.SSLServerSocketFactory;
33
import javax.net.ssl.SSLSocket;
34
import javax.net.ssl.SSLSocketFactory;
35
import javax.net.ssl.TrustManagerFactory;
36
import java.io.DataInputStream;
37
import java.io.DataOutputStream;
38
import java.io.File;
39
import java.io.FileInputStream;
40
import java.io.IOException;
41
import java.security.KeyStore;
42
import java.security.SecureRandom;
43
44
public class RenegotiateTLS13 {
45
46
static final String dataString = "This is a test";
47
48
// Run the server as a thread instead of the client
49
static boolean separateServerThread = false;
50
51
static String pathToStores = "../etc";
52
static String keyStoreFile = "keystore";
53
static String trustStoreFile = "truststore";
54
static String passwd = "passphrase";
55
56
// Server ready flag
57
volatile static boolean serverReady = false;
58
// Turn on SSL debugging
59
static boolean debug = false;
60
// Server done flag
61
static boolean done = false;
62
63
// Main server code
64
65
void doServerSide() throws Exception {
66
SSLServerSocketFactory sslssf;
67
sslssf = initContext().getServerSocketFactory();
68
SSLServerSocket sslServerSocket =
69
(SSLServerSocket) sslssf.createServerSocket(serverPort);
70
serverPort = sslServerSocket.getLocalPort();
71
72
serverReady = true;
73
74
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
75
76
DataInputStream sslIS =
77
new DataInputStream(sslSocket.getInputStream());
78
String s = "";
79
while (s.compareTo("done") != 0) {
80
try {
81
s = sslIS.readUTF();
82
System.out.println("Received: " + s);
83
} catch (IOException e) {
84
throw e;
85
}
86
}
87
done = true;
88
sslSocket.close();
89
}
90
91
// Main client code
92
void doClientSide() throws Exception {
93
94
while (!serverReady) {
95
Thread.sleep(5);
96
}
97
98
SSLSocketFactory sslsf;
99
sslsf = initContext().getSocketFactory();
100
101
SSLSocket sslSocket = (SSLSocket)
102
sslsf.createSocket("localhost", serverPort);
103
104
DataOutputStream sslOS =
105
new DataOutputStream(sslSocket.getOutputStream());
106
107
sslOS.writeUTF("With " + dataString);
108
sslOS.writeUTF("With " + dataString);
109
sslOS.writeUTF("With " + dataString);
110
111
sslSocket.startHandshake();
112
113
sslOS.writeUTF("With " + dataString);
114
sslOS.writeUTF("With " + dataString);
115
sslOS.writeUTF("With " + dataString);
116
117
sslSocket.startHandshake();
118
119
sslOS.writeUTF("With " + dataString);
120
sslOS.writeUTF("With " + dataString);
121
sslOS.writeUTF("With " + dataString);
122
sslOS.writeUTF("done");
123
124
while (!done) {
125
Thread.sleep(5);
126
}
127
sslSocket.close();
128
}
129
130
volatile int serverPort = 0;
131
132
volatile Exception serverException = null;
133
volatile Exception clientException = null;
134
135
public static void main(String[] args) throws Exception {
136
String keyFilename =
137
System.getProperty("test.src", "./") + "/" + pathToStores +
138
"/" + keyStoreFile;
139
String trustFilename =
140
System.getProperty("test.src", "./") + "/" + pathToStores +
141
"/" + trustStoreFile;
142
143
System.setProperty("javax.net.ssl.keyStore", keyFilename);
144
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
145
System.setProperty("javax.net.ssl.trustStore", trustFilename);
146
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
147
148
if (debug)
149
System.setProperty("javax.net.debug", "ssl");
150
151
new RenegotiateTLS13();
152
}
153
154
Thread clientThread = null;
155
Thread serverThread = null;
156
157
/*
158
* Primary constructor, used to drive remainder of the test.
159
*
160
* Fork off the other side, then do your work.
161
*/
162
RenegotiateTLS13() throws Exception {
163
try {
164
if (separateServerThread) {
165
startServer(true);
166
startClient(false);
167
} else {
168
startClient(true);
169
startServer(false);
170
}
171
} catch (Exception e) {
172
// swallow for now. Show later
173
}
174
175
/*
176
* Wait for other side to close down.
177
*/
178
if (separateServerThread) {
179
serverThread.join();
180
} else {
181
clientThread.join();
182
}
183
184
/*
185
* When we get here, the test is pretty much over.
186
* Which side threw the error?
187
*/
188
Exception local;
189
Exception remote;
190
String whichRemote;
191
192
if (separateServerThread) {
193
remote = serverException;
194
local = clientException;
195
whichRemote = "server";
196
} else {
197
remote = clientException;
198
local = serverException;
199
whichRemote = "client";
200
}
201
202
/*
203
* If both failed, return the curthread's exception, but also
204
* print the remote side Exception
205
*/
206
if ((local != null) && (remote != null)) {
207
System.out.println(whichRemote + " also threw:");
208
remote.printStackTrace();
209
System.out.println();
210
throw local;
211
}
212
213
if (remote != null) {
214
throw remote;
215
}
216
217
if (local != null) {
218
throw local;
219
}
220
}
221
222
void startServer(boolean newThread) throws Exception {
223
if (newThread) {
224
serverThread = new Thread() {
225
public void run() {
226
try {
227
doServerSide();
228
} catch (Exception e) {
229
/*
230
* Our server thread just died.
231
*
232
* Release the client, if not active already...
233
*/
234
System.err.println("Server died...");
235
serverReady = true;
236
serverException = e;
237
}
238
}
239
};
240
serverThread.start();
241
} else {
242
try {
243
doServerSide();
244
} catch (Exception e) {
245
serverException = e;
246
} finally {
247
serverReady = true;
248
}
249
}
250
}
251
252
void startClient(boolean newThread) throws Exception {
253
if (newThread) {
254
clientThread = new Thread() {
255
public void run() {
256
try {
257
doClientSide();
258
} catch (Exception e) {
259
/*
260
* Our client thread just died.
261
*/
262
System.err.println("Client died...");
263
clientException = e;
264
}
265
}
266
};
267
clientThread.start();
268
} else {
269
try {
270
doClientSide();
271
} catch (Exception e) {
272
clientException = e;
273
}
274
}
275
}
276
277
// Initialize context for TLS 1.3
278
SSLContext initContext() throws Exception {
279
System.out.println("Using TLS13");
280
SSLContext sc = SSLContext.getInstance("TLSv1.3");
281
KeyStore ks = KeyStore.getInstance("jks");
282
ks.load(new FileInputStream(new File(System.getProperty("javax.net.ssl.keyStore"))), passwd.toCharArray());
283
KeyManagerFactory kmf = KeyManagerFactory.getInstance(
284
KeyManagerFactory.getDefaultAlgorithm());
285
kmf.init(ks, passwd.toCharArray());
286
TrustManagerFactory tmf = TrustManagerFactory.getInstance(
287
TrustManagerFactory.getDefaultAlgorithm());
288
tmf.init(ks);
289
sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
290
return sc;
291
}
292
}
293
294