You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2023/11/03 15:33:51 UTC

(santuario-xml-security-java) branch 2.3.x-fixes updated: Remove call to Signature.getProvider() in debug log (#240)

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

coheigea pushed a commit to branch 2.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/santuario-xml-security-java.git


The following commit(s) were added to refs/heads/2.3.x-fixes by this push:
     new 31e39019 Remove call to Signature.getProvider() in debug log (#240)
31e39019 is described below

commit 31e3901971d91c18a87a58dc7b4bbcac3654b6a7
Author: narras-oss <69...@users.noreply.github.com>
AuthorDate: Fri Nov 3 07:44:00 2023 -0700

    Remove call to Signature.getProvider() in debug log (#240)
    
    The debug log message makes a call to Signature.getProvider() too early.
    This causes Signature.chooseFirstProvider() to be called which matches
     the first provider always rather than the correct provider based on
     PrivateKey.getAlgorithm() when there are multiple providers.
    This debug log was changed in this commit and introduced the issue: https://github.com/apache/santuario-xml-security-java/commit/1f4891867a938b29f2f1904be35b07d113d3eda9
    Added unit test cases with fake provider
---
 .../implementations/SignatureBaseRSA.java          |  3 +-
 .../dom/algorithms/PKSignatureAlgorithmTest.java   | 83 ++++++++++++++++++-
 .../test/dom/providers/TestCustomSignatureSpi.java | 95 ++++++++++++++++++++++
 3 files changed, 176 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/xml/security/algorithms/implementations/SignatureBaseRSA.java b/src/main/java/org/apache/xml/security/algorithms/implementations/SignatureBaseRSA.java
index bd11220d..1f59bc0c 100644
--- a/src/main/java/org/apache/xml/security/algorithms/implementations/SignatureBaseRSA.java
+++ b/src/main/java/org/apache/xml/security/algorithms/implementations/SignatureBaseRSA.java
@@ -60,8 +60,7 @@ public abstract class SignatureBaseRSA extends SignatureAlgorithmSpi {
     public SignatureBaseRSA(Provider provider) throws XMLSignatureException {
         String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
         this.signatureAlgorithm = getSignature(provider, algorithmID);
-        LOG.debug("Created SignatureRSA using {0} and provider {1}",
-            algorithmID, signatureAlgorithm.getProvider());
+        LOG.debug("Created SignatureRSA using {0}", algorithmID);
     }
 
     Signature getSignature(Provider provider, String algorithmID)
diff --git a/src/test/java/org/apache/xml/security/test/dom/algorithms/PKSignatureAlgorithmTest.java b/src/test/java/org/apache/xml/security/test/dom/algorithms/PKSignatureAlgorithmTest.java
index db1e8002..c66874e4 100644
--- a/src/test/java/org/apache/xml/security/test/dom/algorithms/PKSignatureAlgorithmTest.java
+++ b/src/test/java/org/apache/xml/security/test/dom/algorithms/PKSignatureAlgorithmTest.java
@@ -23,7 +23,9 @@ import java.lang.reflect.Constructor;
 import java.security.Key;
 import java.security.KeyPair;
 import java.security.KeyPairGenerator;
+import java.security.PrivateKey;
 import java.security.Provider;
+import java.security.PublicKey;
 import java.security.Security;
 import java.security.spec.AlgorithmParameterSpec;
 import java.security.spec.MGF1ParameterSpec;
@@ -31,14 +33,12 @@ import java.security.spec.PSSParameterSpec;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
-
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathFactory;
-
 import org.apache.xml.security.signature.XMLSignature;
 import org.apache.xml.security.test.dom.DSNamespaceContext;
-import org.apache.xml.security.test.dom.TestUtils;
+import org.apache.xml.security.test.dom.providers.TestCustomSignatureSpi;
 import org.apache.xml.security.transforms.Transforms;
 import org.apache.xml.security.utils.XMLUtils;
 import org.junit.jupiter.api.AfterAll;
@@ -484,6 +484,83 @@ public class PKSignatureAlgorithmTest {
         verify(document, ecKeyPair.getPublic(), localNames);
     }
 
+    @Test
+    void testRSA_SHA1WithCustomSecurityProvider() throws Exception {
+        TestCustomSignatureSpi.reset();
+        CustomFakeProvider.register();
+        try {
+            // Read in plaintext document
+            Document document = XMLUtils.readResource("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml",
+                getClass().getClassLoader(), false);
+
+            List<String> localNames = new ArrayList<>();
+            localNames.add("PaymentInfo");
+
+            sign(XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1, document, localNames, new CustomFakePrivateKey());
+            verify(document, new CustomFakePublicKey(), localNames);
+            TestCustomSignatureSpi.verifyCalls();
+        } finally {
+            CustomFakeProvider.deRegister();
+        }
+    }
+
+    private static class CustomFakeProvider extends Provider {
+
+        public static final String PROVIDER_NAME = "TestCustomProvider";
+
+        protected CustomFakeProvider() {
+            super(PROVIDER_NAME, "1.0", "Custom security provider for unit tests");
+            put("Signature.SHA1withRSA", TestCustomSignatureSpi.class.getName());
+        }
+
+        private static void register() {
+            Security.addProvider(new CustomFakeProvider());
+        }
+
+        private static void deRegister() {
+            final Provider provider = Security.getProvider(PROVIDER_NAME);
+            if (provider instanceof CustomFakeProvider) {
+                Security.removeProvider(PROVIDER_NAME);
+            }
+        }
+    }
+
+    private static class CustomFakePublicKey implements PublicKey {
+
+        @Override
+        public String getAlgorithm() {
+            return "Custom-ALG"; //return an algorithm that will not be supported by other providers
+        }
+
+        @Override
+        public String getFormat() {
+            return null;
+        }
+
+        @Override
+        public byte[] getEncoded() {
+            return new byte[0];
+        }
+    }
+
+    private static class CustomFakePrivateKey implements PrivateKey {
+
+        @Override
+        public String getAlgorithm() {
+            return "Custom-ALG"; //return an algorithm that will not be supported by other providers
+        }
+
+        @Override
+        public String getFormat() {
+            return null;
+        }
+
+        @Override
+        public byte[] getEncoded() {
+            return new byte[0];
+        }
+    }
+
     private XMLSignature sign(
         String algorithm,
         Document document,
diff --git a/src/test/java/org/apache/xml/security/test/dom/providers/TestCustomSignatureSpi.java b/src/test/java/org/apache/xml/security/test/dom/providers/TestCustomSignatureSpi.java
new file mode 100644
index 00000000..78d683e4
--- /dev/null
+++ b/src/test/java/org/apache/xml/security/test/dom/providers/TestCustomSignatureSpi.java
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the
+ * License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing permissions and limitations under the License.
+ */
+package org.apache.xml.security.test.dom.providers;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import java.security.InvalidKeyException;
+import java.security.InvalidParameterException;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.SignatureException;
+import java.security.SignatureSpi;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * A fake custom SignatureSpi implementation to make sure Custom SignatureSpis are honored
+ */
+public class TestCustomSignatureSpi extends SignatureSpi {
+
+    private static AtomicInteger signCallCount = new AtomicInteger(0);
+    private static AtomicInteger verifyCallCount = new AtomicInteger(0);
+
+    private static PrivateKey privateKeyCaptured = null;
+
+    public static void reset() {
+        signCallCount.set(0);
+        verifyCallCount.set(0);
+        privateKeyCaptured = null;
+    }
+
+    public static void verifyCalls() {
+        assertNotNull(privateKeyCaptured, "engineInitSign not invoked");
+        if (signCallCount.get() == 0) {
+            fail("sign was not invoked");
+        }
+        if (verifyCallCount.get() == 0) {
+            fail("verify was not invoked");
+        }
+    }
+
+    @Override
+    protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
+
+    }
+
+    @Override
+    protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
+        if (privateKeyCaptured == null) {
+            privateKeyCaptured = privateKey;
+        }
+    }
+
+    @Override
+    protected void engineUpdate(byte b) throws SignatureException {
+
+    }
+
+    @Override
+    protected void engineUpdate(byte[] b, int off, int len) throws SignatureException {
+
+    }
+
+    @Override
+    protected byte[] engineSign() throws SignatureException {
+        if (signCallCount.incrementAndGet() != 1) {
+            fail("engineSign called multiple times");
+        }
+        return new byte[0];
+    }
+
+    @Override
+    protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
+        verifyCallCount.incrementAndGet();
+        return true;
+    }
+
+    @Override
+    protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
+
+    }
+
+    @Override
+    protected Object engineGetParameter(String param) throws InvalidParameterException {
+        return null;
+    }
+}