You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by mu...@apache.org on 2024/03/12 17:33:34 UTC

(santuario-xml-security-java) branch main updated: Fixed SANTUARIO-612: Support RSASSA-PSS keys in RSAKeyValue elements Changed DOMKeyInfoFactory.newKeyValue() to accept RSASSA-PSS keys. Overrode DOMRSAKeyValue.equals() to allow RSA/RSASSA-PSS keys to be equal. Update PKSignatureAlgorithmTest to use RSASSA-PSS keys.

This is an automated email from the ASF dual-hosted git repository.

mullan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/santuario-xml-security-java.git


The following commit(s) were added to refs/heads/main by this push:
     new bbee40cb Fixed SANTUARIO-612: Support RSASSA-PSS keys in RSAKeyValue elements Changed DOMKeyInfoFactory.newKeyValue() to accept RSASSA-PSS keys. Overrode DOMRSAKeyValue.equals() to allow RSA/RSASSA-PSS keys to be equal. Update PKSignatureAlgorithmTest to use RSASSA-PSS keys.
bbee40cb is described below

commit bbee40cbcfc4a416d8bf6d6a6cfdee50d6413c68
Author: Sean Mullan <se...@oracle.com>
AuthorDate: Tue Mar 12 12:12:31 2024 -0400

    Fixed SANTUARIO-612: Support RSASSA-PSS keys in RSAKeyValue elements
    Changed DOMKeyInfoFactory.newKeyValue() to accept RSASSA-PSS keys.
    Overrode DOMRSAKeyValue.equals() to allow RSA/RSASSA-PSS keys to be equal.
    Update PKSignatureAlgorithmTest to use RSASSA-PSS keys.
---
 .../xml/dsig/internal/dom/DOMKeyInfoFactory.java   |  2 +-
 .../jcp/xml/dsig/internal/dom/DOMKeyValue.java     | 27 ++++++++++++++++++++++
 .../xml/crypto/dsig/PKSignatureAlgorithmTest.java  | 19 +++++++++------
 3 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyInfoFactory.java b/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyInfoFactory.java
index efe01b8b..1e3c3fed 100644
--- a/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyInfoFactory.java
+++ b/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyInfoFactory.java
@@ -77,7 +77,7 @@ public final class DOMKeyInfoFactory extends KeyInfoFactory {
         String algorithm = key.getAlgorithm();
         if ("DSA".equals(algorithm)) {
             return new DOMKeyValue.DSA((DSAPublicKey) key);
-        } else if ("RSA".equals(algorithm)) {
+        } else if ("RSA".equals(algorithm) || "RSASSA-PSS".equals(algorithm)) {
             return new DOMKeyValue.RSA((RSAPublicKey) key);
         } else if ("EC".equals(algorithm)) {
             return new DOMKeyValue.EC((ECPublicKey) key);
diff --git a/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyValue.java b/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyValue.java
index 8a911ef7..3549c780 100644
--- a/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyValue.java
+++ b/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyValue.java
@@ -237,6 +237,33 @@ public abstract class DOMKeyValue<K extends PublicKey> extends DOMStructure impl
             RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
             return (RSAPublicKey) generatePublicKey(rsakf, spec);
         }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (!(obj instanceof KeyValue)) {
+                return false;
+            }
+            // This equality test allows RSA keys that have different
+            // algorithms (ex: RSA and RSASSA-PSS) to be equal as long
+            // as the key is the same.
+            try {
+                PublicKey otherKey = ((KeyValue)obj).getPublicKey();
+                if (!(otherKey instanceof RSAPublicKey)) {
+                    return false;
+                }
+                RSAPublicKey otherRSAKey = (RSAPublicKey)otherKey;
+                RSAPublicKey rsaKey = (RSAPublicKey)getPublicKey();
+                return rsaKey.getPublicExponent().equals(
+                            otherRSAKey.getPublicExponent())
+                        && rsaKey.getModulus().equals(otherRSAKey.getModulus());
+            } catch (KeyException ke) {
+                // no practical way to determine if the keys are equal
+                return false;
+            }
+        }
     }
 
     static final class DSA extends DOMKeyValue<DSAPublicKey> {
diff --git a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/PKSignatureAlgorithmTest.java b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/PKSignatureAlgorithmTest.java
index 2c8cad68..077b939d 100644
--- a/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/PKSignatureAlgorithmTest.java
+++ b/src/test/java/org/apache/xml/security/test/javax/xml/crypto/dsig/PKSignatureAlgorithmTest.java
@@ -75,8 +75,8 @@ class PKSignatureAlgorithmTest {
     private final SignatureMethod ecdsaSha1, ecdsaSha224, ecdsaSha256,
             ecdsaSha384, ecdsaSha512, ecdsaRipemd160;
     private final XMLSignatureFactory fac;
-    private final KeyPair rsaKeyPair, ecKeyPair;
-    private final KeyInfo rsaki;
+    private final KeyPair rsaKeyPair, rsaPssKeyPair, ecKeyPair;
+    private final KeyInfo rsaki, rsaPsski;
     private KeyInfo ecki;
     private boolean ecAlgParamsSupport = true;
     private static int javaVersion;
@@ -168,6 +168,10 @@ class PKSignatureAlgorithmTest {
         rsaKpg.initialize(2048);
         rsaKeyPair = rsaKpg.genKeyPair();
 
+        KeyPairGenerator rsaPssKpg = KeyPairGenerator.getInstance("RSASSA-PSS");
+        rsaPssKpg.initialize(2048);
+        rsaPssKeyPair = rsaPssKpg.genKeyPair();
+
         KeyPairGenerator ecKpg = KeyPairGenerator.getInstance("EC");
         ecKpg.initialize(256);
         ecKeyPair = ecKpg.genKeyPair();
@@ -175,6 +179,8 @@ class PKSignatureAlgorithmTest {
         KeyInfoFactory kifac = fac.getKeyInfoFactory();
         rsaki = kifac.newKeyInfo(Collections.singletonList
                                  (kifac.newKeyValue(rsaKeyPair.getPublic())), "DSig.KeyInfo_1");
+        rsaPsski = kifac.newKeyInfo(Collections.singletonList
+                                 (kifac.newKeyValue(rsaPssKeyPair.getPublic())), "DSig.KeyInfo_1");
 
         boolean isIBM = "IBM Corporation".equals(System.getProperty("java.vendor"));
         if (!isIBM) {
@@ -285,14 +291,14 @@ class PKSignatureAlgorithmTest {
 
     @Test
     void testRSA_PSS() throws Exception {
-        test_create_signature_enveloping(rsaPss, sha1, rsaki,
-                rsaKeyPair.getPrivate(), kvks);
+        test_create_signature_enveloping(rsaPss, sha1, rsaPsski,
+                rsaPssKeyPair.getPrivate(), kvks);
     }
 
     @Test
     void testRSA_PSS_SHA512() throws Exception {
-        test_create_signature_enveloping(rsaPssSha512, sha1, rsaki,
-                rsaKeyPair.getPrivate(), kvks);
+        test_create_signature_enveloping(rsaPssSha512, sha1, rsaPsski,
+                rsaPssKeyPair.getPrivate(), kvks);
     }
 
     @Test
@@ -379,5 +385,4 @@ class PKSignatureAlgorithmTest {
         assertEquals(sig, sig2);
         assertTrue(sig2.validate(dvc));
     }
-
 }