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