Skip to content

Commit 08e0fdb

Browse files
committed
JDK-836732: Refactor PEM API tests requiring PEMRecord usage
1 parent 812add2 commit 08e0fdb

File tree

3 files changed

+110
-81
lines changed

3 files changed

+110
-81
lines changed

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

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2024, 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
@@ -26,14 +26,28 @@
2626
* @bug 4853305 8023980 8254717
2727
* @summary Test KeyFactory of the new RSA provider
2828
* @author Andreas Sterbenz
29+
* @enablePreview
2930
*/
3031

31-
import java.io.*;
32-
import java.util.*;
32+
import java.io.File;
33+
import java.io.FileInputStream;
34+
import java.io.InputStream;
35+
import java.security.Key;
36+
import java.security.KeyFactory;
37+
import java.security.KeyStore;
38+
import java.security.PEM;
39+
import java.security.PrivateKey;
40+
import java.security.PublicKey;
41+
import java.security.interfaces.RSAPrivateCrtKey;
42+
import java.security.spec.KeySpec;
43+
import java.security.spec.PKCS8EncodedKeySpec;
44+
import java.security.spec.RSAPrivateCrtKeySpec;
45+
import java.security.spec.RSAPrivateKeySpec;
46+
import java.security.spec.RSAPublicKeySpec;
47+
import java.security.spec.X509EncodedKeySpec;
3348

34-
import java.security.*;
35-
import java.security.interfaces.*;
36-
import java.security.spec.*;
49+
import java.util.Arrays;
50+
import java.util.Enumeration;
3751

3852
public class TestKeyFactory {
3953

@@ -83,9 +97,9 @@ public class TestKeyFactory {
8397

8498
private static final PrivateKey P1_PRIV;
8599
private static final PublicKey P1_PUB;
86-
87100
static {
88-
byte[] encodedPriv = Base64.getDecoder().decode(PKCS1_PRIV_STR);
101+
byte[] encodedPriv =
102+
new PEM("PRIVATE KEY", PKCS1_PRIV_STR).decode();
89103
P1_PRIV = new PrivateKey() {
90104
@Override
91105
public String getAlgorithm() {
@@ -100,7 +114,7 @@ public byte[] getEncoded() {
100114
return encodedPriv.clone();
101115
}
102116
};
103-
byte[] encodedPub = Base64.getDecoder().decode(PKCS1_PUB_STR);
117+
byte[] encodedPub = new PEM("PRIVATE KEY", PKCS1_PUB_STR).decode();
104118
P1_PUB = new PublicKey() {
105119
@Override
106120
public String getAlgorithm() {
@@ -130,15 +144,15 @@ static KeyStore getKeyStore() throws Exception {
130144
* equivalent
131145
*/
132146
private static void testKey(Key key1, Key key2) throws Exception {
133-
if (key2.getAlgorithm().equals("RSA") == false) {
147+
if (!key2.getAlgorithm().equals("RSA")) {
134148
throw new Exception("Algorithm not RSA");
135149
}
136150
if (key1 instanceof PublicKey) {
137-
if (key2.getFormat().equals("X.509") == false) {
151+
if (!key2.getFormat().equals("X.509")) {
138152
throw new Exception("Format not X.509");
139153
}
140154
} else if (key1 instanceof PrivateKey) {
141-
if (key2.getFormat().equals("PKCS#8") == false) {
155+
if (!key2.getFormat().equals("PKCS#8")) {
142156
throw new Exception("Format not PKCS#8: " + key2.getFormat());
143157
}
144158
}
@@ -204,18 +218,23 @@ private static void testPrivate(KeyFactory kf, PrivateKey key)
204218
KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);
205219
PrivateKey key6 = kf.generatePrivate(rsaSpec2);
206220
testKey(key6, key6);
207-
if (key instanceof RSAPrivateCrtKey) {
208-
RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
221+
if (key instanceof RSAPrivateCrtKey rsaKey) {
209222
KeySpec rsaSpec3 = new RSAPrivateCrtKeySpec(rsaKey.getModulus(),
210-
rsaKey.getPublicExponent(), rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(),
211-
rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(), rsaKey.getCrtCoefficient(), rsaKey.getParams());
223+
rsaKey.getPublicExponent(),
224+
rsaKey.getPrivateExponent(),
225+
rsaKey.getPrimeP(),
226+
rsaKey.getPrimeQ(),
227+
rsaKey.getPrimeExponentP(),
228+
rsaKey.getPrimeExponentQ(),
229+
rsaKey.getCrtCoefficient(),
230+
rsaKey.getParams());
212231
PrivateKey key7 = kf.generatePrivate(rsaSpec3);
213232
testKey(key6, key7);
214233
}
215234
}
216235

217236
private static void test(KeyFactory kf, Key key) throws Exception {
218-
if (key.getAlgorithm().equals("RSA") == false) {
237+
if (!key.getAlgorithm().equals("RSA")) {
219238
System.out.println("Not an RSA key, ignoring");
220239
}
221240
if (key instanceof PublicKey) {

test/jdk/sun/security/rsa/pss/PSSKeyCompatibility.java

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 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,32 +21,34 @@
2121
* questions.
2222
*/
2323

24-
import java.io.ByteArrayInputStream;
2524
import java.security.Key;
2625
import java.security.KeyFactory;
2726
import java.security.NoSuchAlgorithmException;
2827
import java.security.NoSuchProviderException;
28+
import java.security.PEM;
29+
import java.security.PEMDecoder;
30+
import java.security.PEMEncoder;
2931
import java.security.PrivateKey;
3032
import java.security.PublicKey;
33+
import java.security.Security;
3134
import java.security.cert.Certificate;
32-
import java.security.cert.CertificateException;
33-
import java.security.cert.CertificateFactory;
35+
import java.security.cert.X509Certificate;
3436
import java.security.interfaces.RSAPrivateCrtKey;
3537
import java.security.interfaces.RSAPublicKey;
3638
import java.security.spec.InvalidKeySpecException;
37-
import java.security.spec.PKCS8EncodedKeySpec;
3839
import java.security.spec.RSAPrivateCrtKeySpec;
3940
import java.security.spec.RSAPublicKeySpec;
4041
import java.security.spec.X509EncodedKeySpec;
4142
import java.util.Arrays;
42-
import java.util.Base64;
4343

4444
/**
4545
* @test
4646
* @bug 8242335
4747
* @summary OpenSSL generated compatibility test with RSASSA-PSS Java.
48+
* @enablePreview
4849
* @run main PSSKeyCompatibility
4950
*/
51+
5052
public class PSSKeyCompatibility {
5153

5254
private static final String ALGO = "RSASSA-PSS";
@@ -74,26 +76,34 @@ private static boolean validatePrivate(String algorithm, String provider,
7476
String type) {
7577

7678
try {
77-
KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
78-
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(
79-
Base64.getMimeDecoder().decode(type));
80-
PrivateKey priv = kf.generatePrivate(privSpec);
79+
final PEMDecoder decoder = PEMDecoder.of()
80+
.withFactory(Security.getProvider(provider));
81+
final PrivateKey priv = decoder.decode(
82+
new PEM("PRIVATE KEY", type).toString(),
83+
PrivateKey.class
84+
);
8185

82-
RSAPrivateCrtKey crtKey = (RSAPrivateCrtKey) priv;
83-
PrivateKey priv1 = kf.generatePrivate(new RSAPrivateCrtKeySpec(
84-
crtKey.getModulus(),
85-
crtKey.getPublicExponent(),
86-
crtKey.getPrivateExponent(),
87-
crtKey.getPrimeP(),
88-
crtKey.getPrimeQ(),
89-
crtKey.getPrimeExponentP(),
90-
crtKey.getPrimeExponentQ(),
91-
crtKey.getCrtCoefficient(),
92-
crtKey.getParams()
93-
));
94-
equals(priv, priv1);
86+
if (priv instanceof RSAPrivateCrtKey crtKey) {
87+
final KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
88+
final PrivateKey priv1 =
89+
kf.generatePrivate(new RSAPrivateCrtKeySpec(
90+
crtKey.getModulus(),
91+
crtKey.getPublicExponent(),
92+
crtKey.getPrivateExponent(),
93+
crtKey.getPrimeP(),
94+
crtKey.getPrimeQ(),
95+
crtKey.getPrimeExponentP(),
96+
crtKey.getPrimeExponentQ(),
97+
crtKey.getCrtCoefficient(),
98+
crtKey.getParams()
99+
));
100+
equals(priv, priv1);
101+
} else {
102+
throw new RuntimeException(
103+
"Private key is not RSAPrivateCrtKey");
104+
}
95105
} catch (NoSuchAlgorithmException | InvalidKeySpecException
96-
| NoSuchProviderException e) {
106+
| NoSuchProviderException e) {
97107
e.printStackTrace(System.out);
98108
return false;
99109
}
@@ -105,22 +115,28 @@ private static boolean validateCert(String algorithm, String provider,
105115
String type) {
106116

107117
try {
108-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
109-
Certificate cert = cf.generateCertificate(
110-
new ByteArrayInputStream(type.getBytes()));
118+
final Certificate cert = PEMDecoder.of()
119+
.decode(type, X509Certificate.class);
111120
System.out.println(cert);
112-
KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
113-
X509EncodedKeySpec pubSpec = kf.getKeySpec(
114-
cert.getPublicKey(), X509EncodedKeySpec.class);
115-
PublicKey pub = kf.generatePublic(pubSpec);
116-
PublicKey pub1 = kf.generatePublic(new RSAPublicKeySpec(
117-
((RSAPublicKey) pub).getModulus(),
118-
((RSAPublicKey) pub).getPublicExponent(),
119-
((RSAPublicKey) pub).getParams()));
121+
122+
final PEMDecoder decoder = PEMDecoder.of()
123+
.withFactory(Security.getProvider(provider));
124+
final RSAPublicKey pub = decoder.decode(
125+
PEMEncoder.of().encodeToString(
126+
new X509EncodedKeySpec(
127+
cert.getPublicKey().getEncoded())
128+
),
129+
RSAPublicKey.class);
130+
131+
final KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
132+
final PublicKey pub1 = kf.generatePublic(new RSAPublicKeySpec(
133+
pub.getModulus(),
134+
pub.getPublicExponent(),
135+
pub.getParams()));
120136
equals(cert.getPublicKey(), pub);
121137
equals(pub, pub1);
122-
} catch (CertificateException | NoSuchAlgorithmException
123-
| InvalidKeySpecException | NoSuchProviderException e) {
138+
} catch (NoSuchAlgorithmException
139+
| InvalidKeySpecException | NoSuchProviderException e) {
124140
e.printStackTrace(System.out);
125141
return false;
126142
}

test/jdk/sun/security/validator/PKIXValAndRevCheckTests.java

Lines changed: 21 additions & 27 deletions
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
@@ -27,13 +27,14 @@
2727
* @summary Stapled OCSPResponses should be added to PKIXRevocationChecker
2828
* irrespective of revocationEnabled flag
2929
* @library /test/lib
30+
* @enablePreview
3031
* @modules java.base/sun.security.validator
3132
* @build jdk.test.lib.Convert
3233
* @run main PKIXValAndRevCheckTests
3334
*/
3435

35-
import java.io.ByteArrayInputStream;
36-
import java.io.UnsupportedEncodingException;
36+
import java.security.PEM;
37+
import java.security.PEMDecoder;
3738
import java.security.cert.CertPathValidator;
3839
import java.security.cert.CertPathValidatorException;
3940
import java.security.cert.CertPathValidatorException.BasicReason;
@@ -43,7 +44,6 @@
4344
import java.security.cert.PKIXRevocationChecker;
4445
import java.security.cert.TrustAnchor;
4546
import java.security.cert.X509Certificate;
46-
import java.util.Base64;
4747
import java.util.Collections;
4848
import java.util.Date;
4949
import java.util.List;
@@ -349,15 +349,24 @@ public class PKIXValAndRevCheckTests {
349349
static final Date VALID_DATE = new Date(1566007200000L);
350350

351351
public static void main(String[] args) throws Exception {
352-
CertificateFactory certFac = CertificateFactory.getInstance("X.509");
352+
CertificateFactory certFac =
353+
CertificateFactory.getInstance("X.509");
353354
CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
354-
X509Certificate goodGuyCert = getCert(certFac, GOOD_SERVER_PEM);
355-
X509Certificate badGuyCert = getCert(certFac, BAD_SERVER_PEM);
356-
X509Certificate intCACert = getCert(certFac, INT_CA_PEM);
357-
X509Certificate rootCACert = getCert(certFac, ROOT_CA_PEM);
358-
byte[] goodOcspDer = pemToDer(GOOD_GUY_OCSP_PEM);
359-
byte[] badOcspDer = pemToDer(BAD_GUY_OCSP_PEM);
360-
byte[] intCAOcspDer = pemToDer(INT_CA_OCSP_PEM);
355+
final PEMDecoder decoder = PEMDecoder.of();
356+
X509Certificate goodGuyCert =
357+
decoder.decode(GOOD_SERVER_PEM, X509Certificate.class);
358+
X509Certificate badGuyCert =
359+
decoder.decode(BAD_SERVER_PEM, X509Certificate.class);
360+
X509Certificate intCACert =
361+
decoder.decode(INT_CA_PEM, X509Certificate.class);
362+
X509Certificate rootCACert =
363+
decoder.decode(ROOT_CA_PEM, X509Certificate.class);
364+
byte[] goodOcspDer =
365+
new PEM("OCSP RESPONSE", GOOD_GUY_OCSP_PEM).decode();
366+
byte[] badOcspDer =
367+
new PEM("OCSP RESPONSE", BAD_GUY_OCSP_PEM).decode();
368+
byte[] intCAOcspDer =
369+
new PEM("OCSP RESPONSE",INT_CA_OCSP_PEM).decode();
361370
Set<TrustAnchor> trustAnchors =
362371
Set.of(new TrustAnchor(rootCACert, null));
363372
PKIXRevocationChecker pkrc;
@@ -479,19 +488,4 @@ static void verifyCause(Throwable cause, Throwable expectedExc) {
479488
}
480489
}
481490
}
482-
483-
static X509Certificate getCert(CertificateFactory fac, String pemCert) {
484-
try {
485-
ByteArrayInputStream bais =
486-
new ByteArrayInputStream(pemCert.getBytes("UTF-8"));
487-
return (X509Certificate)fac.generateCertificate(bais);
488-
} catch (UnsupportedEncodingException | CertificateException exc) {
489-
throw new RuntimeException(exc);
490-
}
491-
}
492-
493-
static byte[] pemToDer(String pemData) {
494-
Base64.Decoder b64Dec = Base64.getMimeDecoder();
495-
return b64Dec.decode(pemData);
496-
}
497491
}

0 commit comments

Comments
 (0)