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