changeset 14650:fdcd822abdcf

8242556: Cannot load RSASSA-PSS public key with non-null params from byte array Summary: Update AlgorithmId to use alg name before oid str when parsing DER bytes Reviewed-by: mullan
author valeriep
date Tue, 14 Apr 2020 22:12:13 +0000
parents e350c3661d4a
children ba9b355d6057
files src/share/classes/sun/security/x509/AlgorithmId.java test/sun/security/rsa/pss/PSSParametersTest.java test/sun/security/rsa/pss/TestPSSKeySupport.java
diffstat 3 files changed, 33 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/security/x509/AlgorithmId.java	Wed Nov 07 01:04:26 2018 +0000
+++ b/src/share/classes/sun/security/x509/AlgorithmId.java	Tue Apr 14 22:12:13 2020 +0000
@@ -121,9 +121,9 @@
     }
 
     protected void decodeParams() throws IOException {
-        String algidString = algid.toString();
+        String algidName = getName();
         try {
-            algParams = AlgorithmParameters.getInstance(algidString);
+            algParams = AlgorithmParameters.getInstance(algidName);
         } catch (NoSuchAlgorithmException e) {
             /*
              * This algorithm parameter type is not supported, so we cannot
--- a/test/sun/security/rsa/pss/PSSParametersTest.java	Wed Nov 07 01:04:26 2018 +0000
+++ b/test/sun/security/rsa/pss/PSSParametersTest.java	Tue Apr 14 22:12:13 2020 +0000
@@ -31,7 +31,7 @@
 
 /**
  * @test
- * @bug 8146293
+ * @bug 8146293 8242556
  * @summary Test RSASSA-PSS AlgorithmParameters impl of SunRsaSign provider.
  * @run main PSSParametersTest
  */
@@ -41,34 +41,41 @@
      */
     private static final String PROVIDER = "SunRsaSign";
 
-    private static final String ALGO = "RSASSA-PSS";
+    private static final String PSS_ALGO = "RSASSA-PSS";
+    private static final String PSS_OID = "1.2.840.113549.1.1.10";
 
     public static void main(String[] args) throws Exception {
         System.out.println("Testing against DEFAULT parameters");
         test(PSSParameterSpec.DEFAULT);
         System.out.println("Testing against custom parameters");
-        test(new PSSParameterSpec("SHA-512/224", "MGF1", MGF1ParameterSpec.SHA384,
-            100, 1));
+        test(new PSSParameterSpec("SHA-512/224", "MGF1",
+                MGF1ParameterSpec.SHA384, 100, 1));
         System.out.println("Test Passed");
     }
 
-    // test against the given spec by initializing w/ it, generate the DER bytes,
-    // then initialize another instance w/ the DER bytes, retrieve the spec.
-    // compare both spec for equality and throw exception if the comparison failed.
+    // test the given spec by first initializing w/ it, generate the DER
+    // bytes, then initialize w/ the DER bytes, retrieve the spec.
+    // compare both spec for equality and throw exception if the check failed.
     private static void test(PSSParameterSpec spec) throws Exception {
-        AlgorithmParameters params = AlgorithmParameters.getInstance(ALGO, PROVIDER);
-        params.init(spec);
-        byte[] encoded = params.getEncoded();
-        AlgorithmParameters params2 = AlgorithmParameters.getInstance(ALGO, PROVIDER);
-        params2.init(encoded);
-        PSSParameterSpec spec2 = params2.getParameterSpec(PSSParameterSpec.class);
-        if (!isEqual(spec, spec2)) {
-            throw new RuntimeException("Spec check Failed");
+        String ALGORITHMS[] = { PSS_ALGO, PSS_OID };
+        for (String alg : ALGORITHMS) {
+            AlgorithmParameters params = AlgorithmParameters.getInstance
+                    (alg, PROVIDER);
+            params.init(spec);
+            byte[] encoded = params.getEncoded();
+            AlgorithmParameters params2 = AlgorithmParameters.getInstance
+                    (alg, PROVIDER);
+            params2.init(encoded);
+            PSSParameterSpec spec2 = params2.getParameterSpec
+                (PSSParameterSpec.class);
+            if (!isEqual(spec, spec2)) {
+                throw new RuntimeException("Spec check Failed for " +  alg);
+            }
         }
     }
 
-    private static boolean isEqual(PSSParameterSpec spec, PSSParameterSpec spec2)
-        throws Exception {
+    private static boolean isEqual(PSSParameterSpec spec,
+            PSSParameterSpec spec2) throws Exception {
         if (spec == spec2) return true;
         if (spec == null || spec2 == null) return false;
 
@@ -107,8 +114,9 @@
                     ((MGF1ParameterSpec)mgfParams).getDigestAlgorithm().equals
                          (((MGF1ParameterSpec)mgfParams2).getDigestAlgorithm());
                 if (!result) {
-                    System.out.println("Different Digest algo in MGF Parameters: " +
-                        ((MGF1ParameterSpec)mgfParams).getDigestAlgorithm() + " vs " +
+                    System.out.println("Different MGF1 digest algorithms: " +
+                        ((MGF1ParameterSpec)mgfParams).getDigestAlgorithm() +
+                        " vs " +
                         ((MGF1ParameterSpec)mgfParams2).getDigestAlgorithm());
                 }
                 return result;
--- a/test/sun/security/rsa/pss/TestPSSKeySupport.java	Wed Nov 07 01:04:26 2018 +0000
+++ b/test/sun/security/rsa/pss/TestPSSKeySupport.java	Tue Apr 14 22:12:13 2020 +0000
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8146293
+ * @bug 8146293 8242556
  * @summary Test RSASSA-PSS Key related support such as KeyPairGenerator
  * and KeyFactory of the SunRsaSign provider
  */
@@ -148,5 +148,8 @@
         KeyFactory kf = KeyFactory.getInstance(ALGO, "SunRsaSign");
         test(kf, kp.getPublic());
         test(kf, kp.getPrivate());
+        test(kf, kp2.getPublic());
+        test(kf, kp2.getPrivate());
+
     }
 }