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