Skip to content

Commit f0818fa

Browse files
committed
JDK-8365072: Refactor tests to use PEM API (Phase 2)
10. test/jdk/sun/security/ssl/X509TrustManagerImpl/ComodoHacker.java 11. test/jdk/javax/net/ssl/interop/ClientHelloInterOp.java * test/jdk/javax/net/ssl/interop/ClientHelloBufferUnderflowException.java * test/jdk/javax/net/ssl/interop/ClientHelloChromeInterOp.java 12. test/jdk/sun/security/rsa/InvalidBitString.java
1 parent 113cc9b commit f0818fa

File tree

5 files changed

+55
-95
lines changed

5 files changed

+55
-95
lines changed

test/jdk/javax/net/ssl/interop/ClientHelloBufferUnderflowException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
3030
* @test
3131
* @bug 8215790 8219389
3232
* @summary Verify exception
33+
* @enablePreview
3334
* @library /test/lib
3435
* @modules java.base/sun.security.util
3536
* @run main/othervm ClientHelloBufferUnderflowException

test/jdk/javax/net/ssl/interop/ClientHelloChromeInterOp.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
3030
* @test
3131
* @bug 8169362
3232
* @summary Interop automated testing with Chrome
33+
* @enablePreview
3334
* @library /test/lib
3435
* @modules jdk.crypto.ec
3536
* java.base/sun.security.util

test/jdk/javax/net/ssl/interop/ClientHelloInterOp.java

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,17 +21,22 @@
2121
* questions.
2222
*/
2323

24-
import javax.net.ssl.*;
25-
import javax.net.ssl.SSLEngineResult.*;
26-
import java.io.*;
27-
import java.nio.*;
24+
import javax.net.ssl.KeyManagerFactory;
25+
import javax.net.ssl.SSLContext;
26+
import javax.net.ssl.SSLEngine;
27+
import javax.net.ssl.SSLEngineResult;
28+
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
29+
import javax.net.ssl.SSLSession;
30+
import javax.net.ssl.TrustManagerFactory;
31+
import java.nio.ByteBuffer;
2832
import java.security.KeyStore;
33+
import java.security.PEMDecoder;
34+
import java.security.PEMRecord;
2935
import java.security.PrivateKey;
30-
import java.security.KeyFactory;
3136
import java.security.cert.Certificate;
32-
import java.security.cert.CertificateFactory;
33-
import java.security.spec.*;
34-
import java.util.Base64;
37+
import java.security.cert.X509Certificate;
38+
import java.security.interfaces.ECPrivateKey;
39+
import java.security.interfaces.RSAPrivateKey;
3540

3641
public abstract class ClientHelloInterOp {
3742

@@ -179,6 +184,8 @@ public abstract class ClientHelloInterOp {
179184
"RSA"
180185
};
181186

187+
private static final PEMDecoder pemDecoder = PEMDecoder.of();
188+
182189
/*
183190
* Run the test case.
184191
*/
@@ -251,13 +258,9 @@ protected SSLContext createSSLContext(
251258

252259
KeyStore ts = null; // trust store
253260
KeyStore ks = null; // key store
254-
char passphrase[] = "passphrase".toCharArray();
255-
256-
// Generate certificate from cert string.
257-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
261+
char[] passphrase = "passphrase".toCharArray();
258262

259263
// Import the trused certs.
260-
ByteArrayInputStream is;
261264
if (trustedMaterials != null && trustedMaterials.length != 0) {
262265
ts = KeyStore.getInstance("JKS");
263266
ts.load(null, null);
@@ -266,13 +269,8 @@ protected SSLContext createSSLContext(
266269
new Certificate[trustedMaterials.length];
267270
for (int i = 0; i < trustedMaterials.length; i++) {
268271
String trustedCertStr = trustedMaterials[i];
269-
270-
is = new ByteArrayInputStream(trustedCertStr.getBytes());
271-
try {
272-
trustedCert[i] = cf.generateCertificate(is);
273-
} finally {
274-
is.close();
275-
}
272+
// Generate certificate from cert string.
273+
trustedCert[i] = pemDecoder.decode(trustedCertStr, X509Certificate.class);
276274

277275
ts.setCertificateEntry("trusted-cert-" + i, trustedCert[i]);
278276
}
@@ -295,21 +293,16 @@ protected SSLContext createSSLContext(
295293
String keyCertStr = keyMaterialCerts[i];
296294

297295
// generate the private key.
298-
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(
299-
Base64.getMimeDecoder().decode(keyMaterialKeys[i]));
300-
KeyFactory kf =
301-
KeyFactory.getInstance(keyMaterialKeyAlgs[i]);
302-
PrivateKey priKey = kf.generatePrivate(priKeySpec);
296+
String keyMaterialStrPEMFormat = new PEMRecord("PRIVATE KEY", keyMaterialKeys[i]).toString();
303297

304-
// generate certificate chain
305-
is = new ByteArrayInputStream(keyCertStr.getBytes());
306-
Certificate keyCert = null;
307-
try {
308-
keyCert = cf.generateCertificate(is);
309-
} finally {
310-
is.close();
311-
}
298+
PrivateKey priKey = switch (keyMaterialKeyAlgs[i]) {
299+
case "RSA" -> pemDecoder.decode(keyMaterialStrPEMFormat, RSAPrivateKey.class);
300+
case "EC" -> pemDecoder.decode(keyMaterialStrPEMFormat, ECPrivateKey.class);
301+
default -> pemDecoder.decode(keyMaterialStrPEMFormat, PrivateKey.class);
302+
};
312303

304+
// generate certificate chain
305+
Certificate keyCert = pemDecoder.decode(keyCertStr, X509Certificate.class);
313306
Certificate[] chain = new Certificate[] { keyCert };
314307

315308
// import the key entry.

test/jdk/sun/security/rsa/InvalidBitString.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,15 +23,15 @@
2323

2424
/* @test
2525
* @summary Validation of signatures succeed when it should fail
26+
* @enablePreview
2627
* @bug 6896700
2728
*/
2829

29-
import java.io.InputStream;
30-
import java.io.ByteArrayInputStream;
30+
import java.security.PEMDecoder;
3131
import java.security.cert.Certificate;
32-
import java.security.cert.CertificateFactory;
3332
import java.security.PublicKey;
3433
import java.security.SignatureException;
34+
import java.security.cert.X509Certificate;
3535

3636
public class InvalidBitString {
3737

@@ -87,16 +87,16 @@ public class InvalidBitString {
8787
"ZAM6mgkuSY7/vdnsiJtU\n" +
8888
"-----END CERTIFICATE-----\n";
8989

90-
public static void main(String args[]) throws Exception {
91-
92-
Certificate signer = generate(signerCertStr);
90+
public static void main(String[] args) throws Exception {
91+
final PEMDecoder pemDecoder = PEMDecoder.of();
92+
Certificate signer = pemDecoder.decode(signerCertStr, X509Certificate.class);
9393

9494
// the valid certificate
95-
Certificate normal = generate(normalCertStr);
95+
Certificate normal = pemDecoder.decode(normalCertStr, X509Certificate.class);
9696
// the invalid certificate with extra signature bits
97-
Certificate longer = generate(longerCertStr);
97+
Certificate longer = pemDecoder.decode(longerCertStr, X509Certificate.class);
9898
// the invalid certificate without enough signature bits
99-
Certificate shorter = generate(shorterCertStr);
99+
Certificate shorter = pemDecoder.decode(shorterCertStr, X509Certificate.class);
100100

101101
if (!test(normal, signer, " normal", true) ||
102102
!test(longer, signer, " longer", false) ||
@@ -105,19 +105,6 @@ public static void main(String args[]) throws Exception {
105105
}
106106
}
107107

108-
private static Certificate generate(String certStr) throws Exception {
109-
InputStream is = null;
110-
try {
111-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
112-
is = new ByteArrayInputStream(certStr.getBytes());
113-
return cf.generateCertificate(is);
114-
} finally {
115-
if (is != null) {
116-
is.close();
117-
}
118-
}
119-
}
120-
121108
private static boolean test(Certificate target, Certificate signer,
122109
String title, boolean expected) throws Exception {
123110
System.out.print("Checking " + title + ": expected: " +

test/jdk/sun/security/ssl/X509TrustManagerImpl/ComodoHacker.java

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,21 +25,18 @@
2525
* @test
2626
* @bug 7123519
2727
* @summary Problem with java/classes_security
28+
* @enablePreview
2829
* @run main/othervm ComodoHacker PKIX
2930
* @run main/othervm ComodoHacker SunX509
3031
*/
3132

32-
import java.net.*;
33-
import java.util.*;
34-
import java.io.*;
35-
import javax.net.ssl.*;
33+
import javax.net.ssl.TrustManagerFactory;
34+
import javax.net.ssl.X509TrustManager;
3635
import java.security.KeyStore;
36+
import java.security.PEMDecoder;
3737
import java.security.cert.Certificate;
38-
import java.security.cert.CertificateFactory;
3938
import java.security.cert.X509Certificate;
4039
import java.security.cert.CertificateException;
41-
import java.security.spec.*;
42-
import java.security.interfaces.*;
4340

4441
public class ComodoHacker {
4542
// DigiNotar Root CA, untrusted root certificate
@@ -213,6 +210,8 @@ public class ComodoHacker {
213210
"baB2sVGcVNBkK55bT8gPqnx8JypubyUvayzZGg==\n" +
214211
"-----END CERTIFICATE-----";
215212

213+
private static final PEMDecoder pemDecoder = PEMDecoder.of();
214+
216215
private static String tmAlgorithm; // trust manager
217216

218217
public static void main(String args[]) throws Exception {
@@ -253,19 +252,15 @@ private static void parseArguments(String[] args) {
253252
}
254253

255254
private static X509TrustManager getTrustManager() throws Exception {
256-
// generate certificate from cert string
257-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
258255

259256
// create a key store
260257
KeyStore ks = KeyStore.getInstance("JKS");
261258
ks.load(null, null);
262259

260+
// generate certificate from cert string
261+
Certificate trustedCert = pemDecoder.decode(trustedCertStr, X509Certificate.class);
263262
// import the trusted cert
264-
try (ByteArrayInputStream is =
265-
new ByteArrayInputStream(trustedCertStr.getBytes())) {
266-
Certificate trustedCert = cf.generateCertificate(is);
267-
ks.setCertificateEntry("RSA Export Signer", trustedCert);
268-
}
263+
ks.setCertificateEntry("RSA Export Signer", trustedCert);
269264

270265
// create the trust manager
271266
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlgorithm);
@@ -276,28 +271,11 @@ private static X509TrustManager getTrustManager() throws Exception {
276271

277272
private static X509Certificate[] getFraudulentChain() throws Exception {
278273
// generate certificate from cert string
279-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
280-
281274
X509Certificate[] chain = new X509Certificate[4];
282-
try (ByteArrayInputStream is =
283-
new ByteArrayInputStream(targetCertStr.getBytes())) {
284-
chain[0] = (X509Certificate)cf.generateCertificate(is);
285-
}
286-
287-
try (ByteArrayInputStream is =
288-
new ByteArrayInputStream(intermediateCertStr.getBytes())) {
289-
chain[1] = (X509Certificate)cf.generateCertificate(is);
290-
}
291-
292-
try (ByteArrayInputStream is =
293-
new ByteArrayInputStream(compromisedCertStr.getBytes())) {
294-
chain[2] = (X509Certificate)cf.generateCertificate(is);
295-
}
296-
297-
try (ByteArrayInputStream is =
298-
new ByteArrayInputStream(untrustedCrossCertStr.getBytes())) {
299-
chain[3] = (X509Certificate)cf.generateCertificate(is);
300-
}
275+
chain[0] = pemDecoder.decode(targetCertStr, X509Certificate.class);
276+
chain[1] = pemDecoder.decode(intermediateCertStr, X509Certificate.class);
277+
chain[2] = pemDecoder.decode(compromisedCertStr, X509Certificate.class);
278+
chain[3] = pemDecoder.decode(untrustedCrossCertStr, X509Certificate.class);
301279

302280
return chain;
303281
}

0 commit comments

Comments
 (0)