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 2024/04/11 13:10:43 UTC

(santuario-xml-security-java) branch 3.0.x-fixes updated: [SANTUARIO-617] remove mgf element in case of rsa-oaep-mgf1p (#306)

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

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


The following commit(s) were added to refs/heads/3.0.x-fixes by this push:
     new 319e7750 [SANTUARIO-617] remove mgf element in case of rsa-oaep-mgf1p (#306)
319e7750 is described below

commit 319e7750515c628f22dcda946ac6d0e778276391
Author: jrihtarsic <jr...@gmail.com>
AuthorDate: Thu Apr 11 14:25:52 2024 +0200

    [SANTUARIO-617] remove mgf element in case of rsa-oaep-mgf1p (#306)
    
    Co-authored-by: RIHTARSIC Joze <jo...@ext.ec.europa.eu>
---
 .../apache/xml/security/encryption/XMLCipher.java  |  3 +-
 .../output/XMLEncryptOutputProcessor.java          |  8 ++-
 .../test/dom/encryption/XMLCipherTest.java         | 56 ++++++++++++++++++
 .../test/stax/encryption/XMLEncryption11Test.java  | 67 +++++++++++++++++++++-
 4 files changed, 130 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/xml/security/encryption/XMLCipher.java b/src/main/java/org/apache/xml/security/encryption/XMLCipher.java
index 24160835..e841e342 100644
--- a/src/main/java/org/apache/xml/security/encryption/XMLCipher.java
+++ b/src/main/java/org/apache/xml/security/encryption/XMLCipher.java
@@ -3200,7 +3200,8 @@ public class XMLCipher {
                     );
                     result.appendChild(digestElement);
                 }
-                if (mgfAlgorithm != null) {
+
+                if (mgfAlgorithm != null && !XMLCipher.RSA_OAEP.equals(algorithm)) {
                     Element mgfElement =
                         XMLUtils.createElementInEncryption11Space(
                             contextDocument, EncryptionConstants._TAG_MGF
diff --git a/src/main/java/org/apache/xml/security/stax/impl/processor/output/XMLEncryptOutputProcessor.java b/src/main/java/org/apache/xml/security/stax/impl/processor/output/XMLEncryptOutputProcessor.java
index 1e23a8b1..4f3d1696 100644
--- a/src/main/java/org/apache/xml/security/stax/impl/processor/output/XMLEncryptOutputProcessor.java
+++ b/src/main/java/org/apache/xml/security/stax/impl/processor/output/XMLEncryptOutputProcessor.java
@@ -154,7 +154,9 @@ public class XMLEncryptOutputProcessor extends AbstractEncryptOutputProcessor {
                         createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_EncryptionMethod, false, attributes);
 
                         final String encryptionKeyTransportDigestAlgorithm = getSecurityProperties().getEncryptionKeyTransportDigestAlgorithm();
-                        final String encryptionKeyTransportMGFAlgorithm = getSecurityProperties().getEncryptionKeyTransportMGFAlgorithm();
+                        // Check for rsa-oaep-mgf1p then MGF1 is set by default and encryptionKeyTransportMGFAlgorithm must not be set!
+                        final String encryptionKeyTransportMGFAlgorithm = XMLSecurityConstants.NS_XENC_RSAOAEPMGF1P.equals(encryptionKeyTransportAlgorithm)?
+                                null : getSecurityProperties().getEncryptionKeyTransportMGFAlgorithm();
 
                         if (XMLSecurityConstants.NS_XENC11_RSAOAEP.equals(encryptionKeyTransportAlgorithm) ||
                                 XMLSecurityConstants.NS_XENC_RSAOAEPMGF1P.equals(encryptionKeyTransportAlgorithm)) {
@@ -214,7 +216,9 @@ public class XMLEncryptOutputProcessor extends AbstractEncryptOutputProcessor {
                                 }
 
                                 MGF1ParameterSpec mgfParameterSpec = new MGF1ParameterSpec("SHA-1");
-                                if (encryptionKeyTransportMGFAlgorithm != null) {
+                                // Check for RSA-OAEP then MGF1 is set by default and value must not be set!
+                                if (encryptionKeyTransportMGFAlgorithm != null
+                                    && !XMLSecurityConstants.NS_XENC_RSAOAEPMGF1P.equals(encryptionKeyTransportAlgorithm)) {
                                     String jceMGFAlgorithm = JCEMapper.translateURItoJCEID(encryptionKeyTransportMGFAlgorithm);
                                     mgfParameterSpec = new MGF1ParameterSpec(jceMGFAlgorithm);
                                 }
diff --git a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
index d53d809e..d8e96578 100644
--- a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
+++ b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
@@ -55,6 +55,7 @@ import org.apache.xml.security.encryption.params.KeyAgreementParameters;
 import org.apache.xml.security.encryption.params.KeyDerivationParameters;
 import org.apache.xml.security.keys.KeyInfo;
 import org.apache.xml.security.parser.XMLParserException;
+import org.apache.xml.security.stax.ext.XMLSecurityConstants;
 import org.apache.xml.security.test.dom.TestUtils;
 import org.apache.xml.security.testutils.JDKTestUtils;
 import org.apache.xml.security.testutils.KeyTestUtils;
@@ -66,6 +67,7 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assumptions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 import org.junit.jupiter.params.provider.EnumSource;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
@@ -370,6 +372,60 @@ public class XMLCipherTest {
         assertEquals(source, target);
     }
 
+    /**
+     * The http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p identifier defines the
+     * mask generation function as the fixed value of MGF1 with SHA1. In this case
+     * the optional xenc11:MGF element of the xenc:EncryptionMethod element
+     * MUST NOT be provided. For the http://www.w3.org/2009/xmlenc11#rsa-oaep
+     * identifier, the mask generation function must be defined by the xenc11:MGF
+     * element.
+     */
+    @ParameterizedTest
+    @CsvSource({
+            "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p,,0",
+            "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p,'',0",
+            "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p, http://www.w3.org/2009/xmlenc11#mgf1sha1,0",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha1,1",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha256,1",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha224,1",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha384,1",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha512,1",
+    })
+    void testAES128ElementRsaOaepKWCipher(String keyWrapAlgorithmURI, String mgf1URI, int mgfElementCount) throws Exception {
+        // Skip test for IBM JDK
+        Assumptions.assumeTrue(haveISOPadding,
+                "Test testAES128ElementRsaOaepKWCipher was skipped as necessary algorithms not available!" );
+        // init parameters encrypted key object
+        int transportKeyBitLength = 128;
+
+        // prepare the test document
+        Document d = TestUtils.newDocument(); // source
+
+        // Generate test recipient key pair
+        KeyPairGenerator rsaKeygen = KeyPairGenerator.getInstance("RSA");
+        KeyPair kp = rsaKeygen.generateKeyPair();
+        PublicKey pub = kp.getPublic();
+
+        // Generate a traffic key
+        KeyGenerator keygen = KeyGenerator.getInstance("AES");
+        keygen.init(transportKeyBitLength);
+        Key ephemeralSymmetricKey = keygen.generateKey();
+
+        XMLCipher cipherEncKey = XMLCipher.getInstance(keyWrapAlgorithmURI);
+        cipherEncKey.init(XMLCipher.WRAP_MODE, pub);
+        cipherEncKey.setSecureValidation(true);
+        // encrypt transport key with KeyAgreement
+        EncryptedKey encryptedKey =  cipherEncKey.encryptKey(d, ephemeralSymmetricKey,mgf1URI,null);
+        Element enckeyDoc = cipherEncKey.martial(encryptedKey);
+
+        NodeList mfgElements = enckeyDoc.getElementsByTagNameNS(XMLSecurityConstants.NS_XMLENC11, EncryptionConstants._TAG_MGF);
+        assertEquals(mgfElementCount, mfgElements.getLength());
+        assertEquals(keyWrapAlgorithmURI, encryptedKey.getEncryptionMethod().getAlgorithm());
+        if (mgfElementCount > 0) {
+            assertEquals(mgf1URI, encryptedKey.getEncryptionMethod().getMGFAlgorithm());
+        }
+    }
+
     /**
      * Test encryption using a generated AES 192 bit key that is
      * encrypted using a 3DES key.  Then reverse by decrypting
diff --git a/src/test/java/org/apache/xml/security/test/stax/encryption/XMLEncryption11Test.java b/src/test/java/org/apache/xml/security/test/stax/encryption/XMLEncryption11Test.java
index df1b6df1..6aa75c22 100644
--- a/src/test/java/org/apache/xml/security/test/stax/encryption/XMLEncryption11Test.java
+++ b/src/test/java/org/apache/xml/security/test/stax/encryption/XMLEncryption11Test.java
@@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.security.GeneralSecurityException;
 import java.security.Key;
 import java.security.KeyStore;
 import java.security.PrivateKey;
@@ -66,6 +67,8 @@ import org.apache.xml.security.test.stax.utils.XmlReaderToWriter;
 import org.apache.xml.security.utils.XMLUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -164,6 +167,60 @@ public class XMLEncryption11Test {
         checkDecryptedDoc(dd, true);
     }
 
+    /**
+     * The http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p identifier defines the
+     * mask generation function as the fixed value of MGF1 with SHA1. In this case
+     * the optional xenc11:MGF element of the xenc:EncryptionMethod element
+     * MUST NOT be provided. For the http://www.w3.org/2009/xmlenc11#rsa-oaep
+     * identifier, the mask generation function must be defined by the xenc11:MGF
+     * element.
+     */
+    @ParameterizedTest
+    @CsvSource({
+            "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p,,0",
+            "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p,'',0",
+            "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p, http://www.w3.org/2009/xmlenc11#mgf1sha1,0",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha1,1",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha256,1",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha224,1",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha384,1",
+            "http://www.w3.org/2009/xmlenc11#rsa-oaep, http://www.w3.org/2009/xmlenc11#mgf1sha512,1",
+    })
+    void testRsaOaepMgfElement(String rsaOaepURI, String mgfAlgURI, int prfCount) throws Exception {
+        assumeFalse(isIBMJdK);
+
+        KeyStore keyStore = loadKeyStore("org/w3c/www/interop/xmlenc-core-11/RSA-2048_SHA256WithRSA.jks");
+        KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
+                keyStore.getEntry("importkey", new KeyStore.PasswordProtection("passwd".toCharArray()));
+        PrivateKey rsaKey = pkEntry.getPrivateKey();
+        X509Certificate x509Certificate = (X509Certificate) pkEntry.getCertificate();
+
+        // Perform encryption
+        String filename = "org/w3c/www/interop/xmlenc-core-11/plaintext.xml";
+
+        KeyGenerator keygen = KeyGenerator.getInstance("AES");
+        keygen.init(256);
+        SecretKey sessionKey = keygen.generateKey();
+
+        SecurePart securePart =
+                new SecurePart(new QName("urn:example:po", "PurchaseOrder"), SecurePart.Modifier.Element);
+
+        Document ed = encryptDocument(filename, securePart, x509Certificate.getPublicKey(),
+                rsaOaepURI, null, mgfAlgURI,
+                sessionKey, "http://www.w3.org/2009/xmlenc11#aes128-gcm",
+                null);
+
+        NodeList mfgElements = ed.getElementsByTagNameNS(XMLSecurityConstants.NS_XMLENC11, "MGF");
+        assertEquals(prfCount, mfgElements.getLength());
+
+        if (prfCount == 1) {
+            assertEquals(mgfAlgURI, ((Element) mfgElements.item(0)).getAttribute("Algorithm"));
+        }
+        // Perform decryption
+        Document dd = decryptElement(ed, rsaKey);
+        checkDecryptedDoc(dd, true);
+    }
+
     /**
      * rsa-oaep-mgf1p, Digest:SHA256, MGF:SHA1, PSource: None
      */
@@ -585,7 +642,7 @@ public class XMLEncryption11Test {
             assertEquals(1, digestMethodElements.getLength());
             assertEquals(digestMethodAlgo, ((Element) digestMethodElements.item(0)).getAttribute("Algorithm"));
         }
-        if (mgfAlgo != null) {
+        if (mgfAlgo != null && !XMLSecurityConstants.NS_XENC_RSAOAEPMGF1P.equals(encryptedKeyAlgo)) {
             NodeList mfgElements = document.getElementsByTagNameNS(XMLSecurityConstants.NS_XMLENC11, "MGF");
             assertEquals(1, mfgElements.getLength());
             assertEquals(mgfAlgo, ((Element) mfgElements.item(0)).getAttribute("Algorithm"));
@@ -674,4 +731,12 @@ public class XMLEncryption11Test {
             );
         }
     }
+
+    private KeyStore loadKeyStore(final String ksFile) throws IOException, GeneralSecurityException {
+        KeyStore keyStore = KeyStore.getInstance("jks");
+        try (InputStream stream = getClass().getClassLoader().getResourceAsStream(ksFile)) {
+            keyStore.load(stream, "passwd".toCharArray());
+        }
+        return keyStore;
+    }
 }