Skip to content

Commit 8224292

Browse files
koushikthirupatturAnthony Scarpino
authored andcommitted
8365069: Refactor tests to use PEM API (Phase 1)
Reviewed-by: ascarpino
1 parent 4c6af03 commit 8224292

File tree

18 files changed

+324
-442
lines changed

18 files changed

+324
-442
lines changed

test/jdk/java/security/KeyStore/PKCS12/WriteP12Test.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 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,12 +30,7 @@
3030
import java.io.IOException;
3131
import java.nio.file.Files;
3232
import java.nio.file.Paths;
33-
import java.security.Key;
34-
import java.security.KeyStore;
35-
import java.security.KeyStoreException;
36-
import java.security.NoSuchAlgorithmException;
37-
import java.security.NoSuchProviderException;
38-
import java.security.UnrecoverableKeyException;
33+
import java.security.*;
3934
import java.security.cert.Certificate;
4035
import java.security.cert.CertificateException;
4136
import java.security.cert.CertificateFactory;
@@ -50,6 +45,7 @@
5045
* @summary Write different types p12 key store to Check the write related
5146
* APIs.
5247
* @run main WriteP12Test
48+
* @enablePreview
5349
*/
5450

5551
public class WriteP12Test {
@@ -128,19 +124,16 @@ public class WriteP12Test {
128124
private final Certificate testLeadCert;
129125
private final Certificate caCert;
130126

131-
WriteP12Test() throws CertificateException {
132-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
133-
caCert = cf.generateCertificate(new ByteArrayInputStream(CA_CERT_STR
134-
.getBytes()));
135-
testLeadCert = cf.generateCertificate(new ByteArrayInputStream(
136-
LEAD_CERT.getBytes()));
137-
testerCert = cf.generateCertificate(new ByteArrayInputStream(END_CERT
138-
.getBytes()));
127+
WriteP12Test() {
128+
PEMDecoder pemDecoder = PEMDecoder.of();
129+
caCert = pemDecoder.decode(CA_CERT_STR, X509Certificate.class);
130+
testLeadCert = pemDecoder.decode(LEAD_CERT, X509Certificate.class);
131+
testerCert = pemDecoder.decode(END_CERT, X509Certificate.class);
139132
}
140133

141-
public static void main(String[] args) throws CertificateException,
142-
UnrecoverableKeyException, KeyStoreException,
143-
NoSuchProviderException, NoSuchAlgorithmException, IOException {
134+
public static void main(String[] args) throws UnrecoverableKeyException,
135+
KeyStoreException, NoSuchProviderException,
136+
NoSuchAlgorithmException, IOException {
144137
WriteP12Test jstest = new WriteP12Test();
145138
out.println("test WriteP12CertChain");
146139
/*

test/jdk/java/security/KeyStore/TestKeyStoreBasic.java

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 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,54 +21,48 @@
2121
* questions.
2222
*/
2323

24-
import java.io.BufferedInputStream;
2524
import java.io.ByteArrayInputStream;
2625
import java.io.ByteArrayOutputStream;
2726
import java.io.IOException;
28-
import java.io.InputStream;
29-
import java.security.KeyFactory;
30-
import java.security.KeyStore;
31-
import java.security.KeyStoreException;
32-
import java.security.NoSuchProviderException;
33-
import java.security.PrivateKey;
34-
import java.security.UnrecoverableKeyException;
27+
import java.security.*;
3528
import java.security.cert.Certificate;
36-
import java.security.cert.CertificateFactory;
37-
import java.security.spec.KeySpec;
38-
import java.security.spec.PKCS8EncodedKeySpec;
39-
import java.util.Base64;
29+
import java.security.cert.X509Certificate;
4030

4131
/*
4232
* @test
4333
* @bug 8048621 8133090 8167371 8236671
34+
* @enablePreview
4435
* @summary Test basic operations with keystores (jks, jceks, pkcs12)
4536
* @author Yu-Ching Valerie PENG
4637
*/
4738
public class TestKeyStoreBasic {
4839

49-
private static final String PRIVATE_KEY_PKCS8_BASE64 = ""
50-
+ "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpyz97liuWPDYcLH9TX8BiT78o"
51-
+ "lCmAfmevvch6ncXUVuCzbdaKuKXwn4EVbDszsVJLoK5zdtP+X3iDhutj+IgKmLhuczF3M9VIcWr+"
52-
+ "JJUyTH4+3h/RT8cjCDZOmk9iXkb5ifruVsLqzb9g+Vp140Oz7leikne7KmclHvTfvFd0WDI7Gb9v"
53-
+ "o4f5rT717BXJ/n+M6pNk8DLpLiEu6eziYvXRv5x+t5Go3x0eCXdaxEQUf2j876Wfr2qHRJK7lDfF"
54-
+ "e1DDsMg/KpKGiILYZ+g2qtVMZSxtp5BZEtfB5qV/IE5kWO+mCIAGpXSZIdbERR6pZUq8GLEe1T9e"
55-
+ "+sO6H24w2F19AgMBAAECggEBAId/12187dO6wUPCjumuJA1QrrBnbKdKONyai36uoc1Od4s5QFj7"
56-
+ "+hEIeS7rbGNYQuBvnkgusAbzkW0FIpxpHce3EJez/emux6pEOKoP77BwMt9gy+txyu0+BHi91FQg"
57-
+ "AGvrnQDO5EYVY4Cz/WjOsJzKu8zVLg+DS0Toa2qRFwmUe9mVAXPNOCZ3Oae/Q6tCDsaINNw0fmjj"
58-
+ "jn6uohPbS+n6xENG3FkQXB36getXy310xTGED2J27cmAQH6gLR6Kl2iROzNPbbpBqbuemI9kbcld"
59-
+ "EwBS1jRfZWeaPstYA1niVrE9UgUBzemnoh4TDkG076sYthHMr5QFGjPswnwtJ4ECgYEA0sURQ5+v"
60-
+ "baH4tdaemI3qpnknXTlzSpuZZmAoyvY0Id0mlduwKwmZ3Y5989wHfnnhFfyNO4IkTKjI2Wp97qP5"
61-
+ "4eqUNpA7FtNU7KUzMcFDTtwtNZuRYMrKlqo2lLbA+gVrAYpYZFL4b7tcwtX4DnYorDsmude6W8sG"
62-
+ "4Mx2VdFJC9UCgYEAzjsdXCYH5doWUHb0dvn9ID7IikffEMRM720MRjrnnnVbpzx6ACntkPDNZg7p"
63-
+ "TRE/mx7iBz81ZaUWE+V0wd0JvCHEdpAz3mksyvDFhU4Bgs6xzf2pSul5muhsx3hHcvvPezz5Bnxs"
64-
+ "faJlzkxfwotyGmvWN15GA/pyfsZjsbbTpwkCgYAO6NnbysQCIV8SnegCKqfatt9N/O5m7LLhRxQb"
65-
+ "p2bwrlA4cZ34rWkw/w9x3LK7A6wkfgUPnJkswxPSLXJTG05l6M4rPfCwIKr1Qopojp9QSMr569NQ"
66-
+ "4YeLOOc7heIIzbFQHpU6I5Rncv2Q2sn9W+ZsqJKIuvX34FjQNiZ406EzMQKBgHSxOGS61D84DuZK"
67-
+ "2Ps1awhC3kB4eHzJRms3vflDPWoJJ+pSKwpKrzUTPHXiPBqyhtYkPGszVeiE6CAr9sv3YZnFVaBs"
68-
+ "6hyQUJsob+uE/w/gGvXe8VsFDx0bJOodYfhrCbTHBHWqE81nBcocpxayxsayfAzqWB3KKd0YLrMR"
69-
+ "K2PZAoGAcZa8915R2m0KZ6HVJUt/JDR85jCbN71kcVDFY2XSFkOJvOdFoHNfRckfLzjq9Y2MSSTV"
70-
+ "+QDWbDo2doUQCejJUTaN8nP79tfyir24X5uVPvQaeVoGTKYb+LfUqK0F60lStmjuddIGSZH55y3v"
71-
+ "+9XjmxbVERtd1lqgQg3VlmKlEXY=";
40+
private static final String PRIVATE_KEY_PKCS8_BASE64 = """
41+
-----BEGIN PRIVATE KEY-----
42+
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpyz97liuWPDYcLH9TX8BiT78o
43+
lCmAfmevvch6ncXUVuCzbdaKuKXwn4EVbDszsVJLoK5zdtP+X3iDhutj+IgKmLhuczF3M9VIcWr+
44+
JJUyTH4+3h/RT8cjCDZOmk9iXkb5ifruVsLqzb9g+Vp140Oz7leikne7KmclHvTfvFd0WDI7Gb9v
45+
o4f5rT717BXJ/n+M6pNk8DLpLiEu6eziYvXRv5x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C3kB4eHzJRms3vflDPWoJJ+pSKwpKrzUTPHXiPBqyhtYkPGszVeiE6CAr9sv3YZnFVaBs
60+
6hyQUJsob+uE/w/gGvXe8VsFDx0bJOodYfhrCbTHBHWqE81nBcocpxayxsayfAzqWB3KKd0YLrMR
61+
K2PZAoGAcZa8915R2m0KZ6HVJUt/JDR85jCbN71kcVDFY2XSFkOJvOdFoHNfRckfLzjq9Y2MSSTV
62+
+QDWbDo2doUQCejJUTaN8nP79tfyir24X5uVPvQaeVoGTKYb+LfUqK0F60lStmjuddIGSZH55y3v
63+
+9XjmxbVERtd1lqgQg3VlmKlEXY=
64+
-----END PRIVATE KEY-----
65+
""";
7266

7367
/*
7468
* Certificate:
@@ -132,20 +126,9 @@ public void run() throws Exception {
132126

133127
public void runTest(String provider) throws Exception {
134128

135-
// load private key
136-
// all keystore types should support private keys
137-
KeySpec spec = new PKCS8EncodedKeySpec(
138-
Base64.getMimeDecoder().decode(PRIVATE_KEY_PKCS8_BASE64));
139-
PrivateKey privateKey = KeyFactory.getInstance("RSA")
140-
.generatePrivate(spec);
141-
142-
// load x509 certificate
143-
Certificate cert;
144-
try (InputStream is = new BufferedInputStream(
145-
new ByteArrayInputStream(CERTIFICATE.getBytes()))) {
146-
cert = CertificateFactory.getInstance("X.509")
147-
.generateCertificate(is);
148-
}
129+
PEMDecoder pemDecoder = PEMDecoder.of();
130+
PrivateKey privateKey = pemDecoder.decode(PRIVATE_KEY_PKCS8_BASE64, PrivateKey.class);
131+
Certificate cert = pemDecoder.decode(CERTIFICATE, X509Certificate.class);
149132

150133
int numEntries = 5;
151134
String type = null;

test/jdk/java/security/cert/CertPathBuilder/selfIssued/DisableRevocation.java

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 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
@@ -36,11 +36,14 @@
3636
* @run main/othervm DisableRevocation subca
3737
* @run main/othervm DisableRevocation subci
3838
* @run main/othervm DisableRevocation alice
39+
* @enablePreview
3940
* @author Xuelei Fan
4041
*/
4142

4243
import java.io.*;
4344
import java.net.SocketException;
45+
import java.security.DEREncodable;
46+
import java.security.PEMDecoder;
4447
import java.util.*;
4548
import java.security.Security;
4649
import java.security.cert.*;
@@ -143,49 +146,29 @@ public final class DisableRevocation {
143146
"G93Dcf0U1JRO77juc61Br5paAy8Bok18Y/MeG7uKgB2MAEJYKhGKbCrfMw==\n" +
144147
"-----END CERTIFICATE-----";
145148

149+
private static final PEMDecoder PEM_DECODER = PEMDecoder.of();
150+
146151
private static Set<TrustAnchor> generateTrustAnchors()
147152
throws CertificateException {
148153
// generate certificate from cert string
149-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
150-
151-
ByteArrayInputStream is =
152-
new ByteArrayInputStream(selfSignedCertStr.getBytes());
153-
Certificate selfSignedCert = cf.generateCertificate(is);
154+
X509Certificate selfSignedCert = PEM_DECODER.decode(selfSignedCertStr, X509Certificate.class);
154155

155156
// generate a trust anchor
156157
TrustAnchor anchor =
157-
new TrustAnchor((X509Certificate)selfSignedCert, null);
158+
new TrustAnchor(selfSignedCert, null);
158159

159160
return Collections.singleton(anchor);
160161
}
161162

162163
private static CertStore generateCertificateStore() throws Exception {
163-
Collection entries = new HashSet();
164+
Collection<DEREncodable> entries = new HashSet<>();
164165

165166
// generate certificate from certificate string
166-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
167-
168-
ByteArrayInputStream is;
169-
170-
is = new ByteArrayInputStream(targetCertStr.getBytes());
171-
Certificate cert = cf.generateCertificate(is);
172-
entries.add(cert);
173-
174-
is = new ByteArrayInputStream(subCaCertStr.getBytes());
175-
cert = cf.generateCertificate(is);
176-
entries.add(cert);
177-
178-
is = new ByteArrayInputStream(selfSignedCertStr.getBytes());
179-
cert = cf.generateCertificate(is);
180-
entries.add(cert);
181-
182-
is = new ByteArrayInputStream(topCrlIssuerCertStr.getBytes());
183-
cert = cf.generateCertificate(is);
184-
entries.add(cert);
185-
186-
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
187-
cert = cf.generateCertificate(is);
188-
entries.add(cert);
167+
entries.add(PEM_DECODER.decode(targetCertStr, X509Certificate.class));
168+
entries.add(PEM_DECODER.decode(subCaCertStr, X509Certificate.class));
169+
entries.add(PEM_DECODER.decode(selfSignedCertStr, X509Certificate.class));
170+
entries.add(PEM_DECODER.decode(topCrlIssuerCertStr, X509Certificate.class));
171+
entries.add(PEM_DECODER.decode(subCrlIssuerCertStr, X509Certificate.class));
189172

190173
return CertStore.getInstance("Collection",
191174
new CollectionCertStoreParameters(entries));
@@ -198,15 +181,16 @@ private static X509CertSelector generateSelector(String name)
198181
// generate certificate from certificate string
199182
CertificateFactory cf = CertificateFactory.getInstance("X.509");
200183
ByteArrayInputStream is = null;
184+
String cert;
201185
if (name.equals("subca")) {
202-
is = new ByteArrayInputStream(subCaCertStr.getBytes());
186+
cert = subCaCertStr;
203187
} else if (name.equals("subci")) {
204-
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
188+
cert = subCrlIssuerCertStr;
205189
} else {
206-
is = new ByteArrayInputStream(targetCertStr.getBytes());
190+
cert = targetCertStr;
207191
}
208192

209-
X509Certificate target = (X509Certificate)cf.generateCertificate(is);
193+
X509Certificate target = PEM_DECODER.decode(cert, X509Certificate.class);
210194
byte[] extVal = target.getExtensionValue("2.5.29.14");
211195
if (extVal != null) {
212196
DerInputStream in = new DerInputStream(extVal);
@@ -222,19 +206,17 @@ private static X509CertSelector generateSelector(String name)
222206

223207
private static boolean match(String name, Certificate cert)
224208
throws Exception {
225-
X509CertSelector selector = new X509CertSelector();
226209

227210
// generate certificate from certificate string
228-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
229-
ByteArrayInputStream is = null;
211+
String newCert;
230212
if (name.equals("subca")) {
231-
is = new ByteArrayInputStream(subCaCertStr.getBytes());
213+
newCert = subCaCertStr;
232214
} else if (name.equals("subci")) {
233-
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
215+
newCert = subCrlIssuerCertStr;
234216
} else {
235-
is = new ByteArrayInputStream(targetCertStr.getBytes());
217+
newCert = targetCertStr;
236218
}
237-
X509Certificate target = (X509Certificate)cf.generateCertificate(is);
219+
X509Certificate target = PEM_DECODER.decode(newCert, X509Certificate.class);
238220

239221
return target.equals(cert);
240222
}

0 commit comments

Comments
 (0)