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/ciphersuites/ECCurvesconstraints.java
38853 views
1
/*
2
* Copyright (c) 2016, 2017, 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 8148516
34
* @summary Improve the default strength of EC in JDK
35
* @run main/othervm ECCurvesconstraints PKIX
36
* @run main/othervm ECCurvesconstraints SunX509
37
*/
38
39
import java.io.ByteArrayInputStream;
40
import java.io.InputStream;
41
import java.io.OutputStream;
42
import java.io.IOException;
43
import java.security.KeyStore;
44
import java.security.KeyFactory;
45
import java.security.cert.Certificate;
46
import java.security.cert.CertificateFactory;
47
import java.security.interfaces.ECPrivateKey;
48
import java.security.spec.PKCS8EncodedKeySpec;
49
import java.util.Base64;
50
import javax.net.ssl.KeyManagerFactory;
51
import javax.net.ssl.SSLContext;
52
import javax.net.ssl.SSLServerSocket;
53
import javax.net.ssl.SSLServerSocketFactory;
54
import javax.net.ssl.SSLSocket;
55
import javax.net.ssl.SSLSocketFactory;
56
import javax.net.ssl.TrustManagerFactory;
57
58
public class ECCurvesconstraints {
59
60
/*
61
* =============================================================
62
* Set the various variables needed for the tests, then
63
* specify what tests to run on each side.
64
*/
65
66
/*
67
* Should we run the client or server in a separate thread?
68
* Both sides can throw exceptions, but do you have a preference
69
* as to which side should be the main thread.
70
*/
71
static boolean separateServerThread = false;
72
73
/*
74
* Where do we find the keystores?
75
*/
76
// Certificates and key used in the test.
77
//
78
// EC curve: secp224k1
79
static String trustedCertStr =
80
"-----BEGIN CERTIFICATE-----\n" +
81
"MIIBCzCBugIEVz2lcjAKBggqhkjOPQQDAjAaMRgwFgYDVQQDDA93d3cuZXhhbXBs\n" +
82
"ZS5vcmcwHhcNMTYwNTE5MTEzNzM5WhcNMTcwNTE5MTEzNzM5WjAaMRgwFgYDVQQD\n" +
83
"DA93d3cuZXhhbXBsZS5vcmcwTjAQBgcqhkjOPQIBBgUrgQQAIAM6AAT68uovMZ8f\n" +
84
"KARn5NOjvieJaq6h8zHYkM9w5DuN0kkOo4KBhke06EkQj0nvQQcSvppTV6RoDLY4\n" +
85
"djAKBggqhkjOPQQDAgNAADA9AhwMNIujM0R0llpPH6d89d1S3VRGH/78ovc+zw51\n" +
86
"Ah0AuZ1YlQkUbrJIzkuPSICxz5UfCWPe+7w4as+wiA==\n" +
87
"-----END CERTIFICATE-----";
88
89
// Private key in the format of PKCS#8
90
static String targetPrivateKey =
91
"MIGCAgEAMBAGByqGSM49AgEGBSuBBAAgBGswaQIBAQQdAPbckc86mgW/zexB1Ajq\n" +
92
"38HntWOjdxL6XSoiAsWgBwYFK4EEACChPAM6AAT68uovMZ8fKARn5NOjvieJaq6h\n" +
93
"8zHYkM9w5DuN0kkOo4KBhke06EkQj0nvQQcSvppTV6RoDLY4dg==";
94
95
static String[] serverCerts = {trustedCertStr};
96
static String[] serverKeys = {targetPrivateKey};
97
static String[] clientCerts = {trustedCertStr};
98
static String[] clientKeys = {targetPrivateKey};
99
100
static char passphrase[] = "passphrase".toCharArray();
101
102
/*
103
* Is the server ready to serve?
104
*/
105
volatile static boolean serverReady = false;
106
107
/*
108
* Turn on SSL debugging?
109
*/
110
static boolean debug = false;
111
112
/*
113
* Define the server 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 doServerSide() throws Exception {
119
SSLContext context = generateSSLContext(false);
120
SSLServerSocketFactory sslssf = context.getServerSocketFactory();
121
SSLServerSocket sslServerSocket =
122
(SSLServerSocket)sslssf.createServerSocket(serverPort);
123
serverPort = sslServerSocket.getLocalPort();
124
125
/*
126
* Signal Client, we're ready for his connect.
127
*/
128
serverReady = true;
129
130
SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept();
131
try {
132
sslSocket.setSoTimeout(5000);
133
sslSocket.setSoLinger(true, 5);
134
135
InputStream sslIS = sslSocket.getInputStream();
136
OutputStream sslOS = sslSocket.getOutputStream();
137
138
sslIS.read();
139
sslOS.write('A');
140
sslOS.flush();
141
142
throw new Exception("EC curve secp224k1 should be disabled");
143
} catch (IOException she) {
144
// expected exception: no cipher suites in common
145
System.out.println("Expected exception: " + she);
146
} finally {
147
sslSocket.close();
148
sslServerSocket.close();
149
}
150
}
151
152
/*
153
* Define the client side of the test.
154
*
155
* If the server prematurely exits, serverReady will be set to true
156
* to avoid infinite hangs.
157
*/
158
void doClientSide() throws Exception {
159
160
/*
161
* Wait for server to get started.
162
*/
163
while (!serverReady) {
164
Thread.sleep(50);
165
}
166
167
SSLContext context = generateSSLContext(true);
168
SSLSocketFactory sslsf = context.getSocketFactory();
169
170
SSLSocket sslSocket =
171
(SSLSocket)sslsf.createSocket("localhost", serverPort);
172
173
try {
174
sslSocket.setSoTimeout(5000);
175
sslSocket.setSoLinger(true, 5);
176
177
InputStream sslIS = sslSocket.getInputStream();
178
OutputStream sslOS = sslSocket.getOutputStream();
179
180
sslOS.write('B');
181
sslOS.flush();
182
sslIS.read();
183
184
throw new Exception("EC curve secp224k1 should be disabled");
185
} catch (IOException she) {
186
// expected exception: Received fatal alert
187
System.out.println("Expected exception: " + she);
188
} finally {
189
sslSocket.close();
190
}
191
}
192
193
/*
194
* =============================================================
195
* The remainder is just support stuff
196
*/
197
private static String tmAlgorithm; // trust manager
198
199
private static void parseArguments(String[] args) {
200
tmAlgorithm = args[0];
201
}
202
203
private static SSLContext generateSSLContext(boolean isClient)
204
throws Exception {
205
206
// generate certificate from cert string
207
CertificateFactory cf = CertificateFactory.getInstance("X.509");
208
209
// create a key store
210
KeyStore ks = KeyStore.getInstance("JKS");
211
ks.load(null, null);
212
213
// import the trused cert
214
ByteArrayInputStream is =
215
new ByteArrayInputStream(trustedCertStr.getBytes());
216
Certificate trusedCert = cf.generateCertificate(is);
217
is.close();
218
219
ks.setCertificateEntry("Export Signer", trusedCert);
220
221
String[] certStrs = null;
222
String[] keyStrs = null;
223
if (isClient) {
224
certStrs = clientCerts;
225
keyStrs = clientKeys;
226
} else {
227
certStrs = serverCerts;
228
keyStrs = serverKeys;
229
}
230
231
for (int i = 0; i < certStrs.length; i++) {
232
// generate the private key.
233
String keySpecStr = keyStrs[i];
234
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(
235
Base64.getMimeDecoder().decode(keySpecStr));
236
KeyFactory kf = KeyFactory.getInstance("EC");
237
ECPrivateKey priKey =
238
(ECPrivateKey)kf.generatePrivate(priKeySpec);
239
240
// generate certificate chain
241
String keyCertStr = certStrs[i];
242
is = new ByteArrayInputStream(keyCertStr.getBytes());
243
Certificate keyCert = cf.generateCertificate(is);
244
is.close();
245
246
Certificate[] chain = new Certificate[2];
247
chain[0] = keyCert;
248
chain[1] = trusedCert;
249
250
// import the key entry.
251
ks.setKeyEntry("key-entry-" + i, priKey, passphrase, chain);
252
}
253
254
// create SSL context
255
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlgorithm);
256
tmf.init(ks);
257
258
SSLContext ctx = SSLContext.getInstance("TLS");
259
KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");
260
kmf.init(ks, passphrase);
261
262
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
263
ks = null;
264
265
return ctx;
266
}
267
268
// use any free port by default
269
volatile int serverPort = 0;
270
271
volatile Exception serverException = null;
272
volatile Exception clientException = null;
273
274
public static void main(String[] args) throws Exception {
275
if (debug) {
276
System.setProperty("javax.net.debug", "all");
277
}
278
279
/*
280
* Get the customized arguments.
281
*/
282
parseArguments(args);
283
284
/*
285
* Start the tests.
286
*/
287
new ECCurvesconstraints();
288
}
289
290
Thread clientThread = null;
291
Thread serverThread = null;
292
293
/*
294
* Primary constructor, used to drive remainder of the test.
295
*
296
* Fork off the other side, then do your work.
297
*/
298
ECCurvesconstraints() throws Exception {
299
try {
300
if (separateServerThread) {
301
startServer(true);
302
startClient(false);
303
} else {
304
startClient(true);
305
startServer(false);
306
}
307
} catch (Exception e) {
308
// swallow for now. Show later
309
}
310
311
/*
312
* Wait for other side to close down.
313
*/
314
if (separateServerThread) {
315
serverThread.join();
316
} else {
317
clientThread.join();
318
}
319
320
/*
321
* When we get here, the test is pretty much over.
322
* Which side threw the error?
323
*/
324
Exception local;
325
Exception remote;
326
String whichRemote;
327
328
if (separateServerThread) {
329
remote = serverException;
330
local = clientException;
331
whichRemote = "server";
332
} else {
333
remote = clientException;
334
local = serverException;
335
whichRemote = "client";
336
}
337
338
/*
339
* If both failed, return the curthread's exception, but also
340
* print the remote side Exception
341
*/
342
if ((local != null) && (remote != null)) {
343
System.out.println(whichRemote + " also threw:");
344
remote.printStackTrace();
345
System.out.println();
346
throw local;
347
}
348
349
if (remote != null) {
350
throw remote;
351
}
352
353
if (local != null) {
354
throw local;
355
}
356
}
357
358
void startServer(boolean newThread) throws Exception {
359
if (newThread) {
360
serverThread = new Thread() {
361
public void run() {
362
try {
363
doServerSide();
364
} catch (Exception e) {
365
/*
366
* Our server thread just died.
367
*
368
* Release the client, if not active already...
369
*/
370
System.err.println("Server died, because of " + e);
371
serverReady = true;
372
serverException = e;
373
}
374
}
375
};
376
serverThread.start();
377
} else {
378
try {
379
doServerSide();
380
} catch (Exception e) {
381
serverException = e;
382
} finally {
383
serverReady = true;
384
}
385
}
386
}
387
388
void startClient(boolean newThread) throws Exception {
389
if (newThread) {
390
clientThread = new Thread() {
391
public void run() {
392
try {
393
doClientSide();
394
} catch (Exception e) {
395
/*
396
* Our client thread just died.
397
*/
398
System.err.println("Client died, because of " + e);
399
clientException = e;
400
}
401
}
402
};
403
clientThread.start();
404
} else {
405
try {
406
doClientSide();
407
} catch (Exception e) {
408
clientException = e;
409
}
410
}
411
}
412
}
413
414