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
2121 * questions.
2222 */
2323
24- import java .io .ByteArrayInputStream ;
2524import java .security .Key ;
2625import java .security .KeyFactory ;
2726import java .security .NoSuchAlgorithmException ;
2827import java .security .NoSuchProviderException ;
28+ import java .security .PEM ;
29+ import java .security .PEMDecoder ;
30+ import java .security .PEMEncoder ;
2931import java .security .PrivateKey ;
3032import java .security .PublicKey ;
33+ import java .security .Security ;
3134import java .security .cert .Certificate ;
32- import java .security .cert .CertificateException ;
33- import java .security .cert .CertificateFactory ;
35+ import java .security .cert .X509Certificate ;
3436import java .security .interfaces .RSAPrivateCrtKey ;
3537import java .security .interfaces .RSAPublicKey ;
3638import java .security .spec .InvalidKeySpecException ;
37- import java .security .spec .PKCS8EncodedKeySpec ;
3839import java .security .spec .RSAPrivateCrtKeySpec ;
3940import java .security .spec .RSAPublicKeySpec ;
4041import java .security .spec .X509EncodedKeySpec ;
4142import 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+
5052public 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 }
0 commit comments