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

coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-wss4j.git


The following commit(s) were added to refs/heads/master by this push:
     new 10d62c8ba Check an EncryptedKey embedded in an EncryptedData's KeyInfo 
(StAX) (#777)
10d62c8ba is described below

commit 10d62c8ba351f74b05e32df0b875aae0edb5d6a2
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Mon Nov 9 11:03:13 2026 +0000

    Check an EncryptedKey embedded in an EncryptedData's KeyInfo (StAX) (#777)
---
 .../processor/input/DecryptInputProcessor.java     | 23 ++++++++
 .../input/WSSEncryptedKeyInputHandler.java         | 25 ++++++--
 .../wss4j/stax/test/VulnerabliltyVectorsTest.java  | 67 +++++++++++++++++++++-
 3 files changed, 109 insertions(+), 6 deletions(-)

diff --git 
a/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/DecryptInputProcessor.java
 
b/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/DecryptInputProcessor.java
index 7cc1a532d..447ae5797 100644
--- 
a/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/DecryptInputProcessor.java
+++ 
b/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/DecryptInputProcessor.java
@@ -55,6 +55,7 @@ import org.apache.xml.security.binding.xmldsig.TransformType;
 import org.apache.xml.security.binding.xmldsig.TransformsType;
 import org.apache.xml.security.binding.xmlenc.CipherReferenceType;
 import org.apache.xml.security.binding.xmlenc.EncryptedDataType;
+import org.apache.xml.security.binding.xmlenc.EncryptedKeyType;
 import org.apache.xml.security.binding.xmlenc.ReferenceList;
 import org.apache.xml.security.binding.xmlenc.ReferenceType;
 import org.apache.xml.security.exceptions.XMLSecurityException;
@@ -88,11 +89,19 @@ public class DecryptInputProcessor extends 
AbstractDecryptInputProcessor {
 
     private List<DeferredAttachment> attachmentReferences = new ArrayList<>();
 
+    /**
+     * Whether the key is taken from the KeyInfo of each EncryptedData, rather 
than from a KeyInfo
+     * given to this processor. Santuario then processes an EncryptedKey in 
that KeyInfo itself,
+     * without WSSEncryptedKeyInputHandler, so its checks are applied in 
handleSecurityToken.
+     */
+    private final boolean keyInfoFromEncryptedData;
+
     public DecryptInputProcessor(KeyInfoType keyInfoType, ReferenceList 
referenceList,
                                  WSSSecurityProperties securityProperties, 
WSInboundSecurityContext securityContext)
             throws XMLSecurityException {
 
         super(keyInfoType, referenceList, securityProperties);
+        keyInfoFromEncryptedData = keyInfoType == null;
         checkBSPCompliance(keyInfoType, referenceList, securityContext, 
BSPRule.R3006);
     }
 
@@ -303,6 +312,20 @@ public class DecryptInputProcessor extends 
AbstractDecryptInputProcessor {
     @Override
     protected void handleSecurityToken(InboundSecurityToken 
inboundSecurityToken, InboundSecurityContext inboundSecurityContext,
                                        EncryptedDataType encryptedDataType) 
throws XMLSecurityException {
+        // An EncryptedKey embedded in the KeyInfo of the EncryptedData has 
been resolved by Santuario
+        // without WSSEncryptedKeyInputHandler, so it has not been checked 
yet. The key is only
+        // unwrapped once it is used, which is after this method, so check it 
here.
+        if (keyInfoFromEncryptedData && encryptedDataType.getKeyInfo() != 
null) {
+            EncryptedKeyType encryptedKeyType = XMLSecurityUtils.getQNameType(
+                encryptedDataType.getKeyInfo().getContent(), 
XMLSecurityConstants.TAG_xenc_EncryptedKey);
+            if (encryptedKeyType != null) {
+                WSInboundSecurityContext wsInboundSecurityContext = 
(WSInboundSecurityContext) inboundSecurityContext;
+                
WSSEncryptedKeyInputHandler.checkBSPCompliance(wsInboundSecurityContext, 
encryptedKeyType);
+                WSSEncryptedKeyInputHandler.checkKeyTransportAlgorithm(
+                    encryptedKeyType, getSecurityProperties(), 
wsInboundSecurityContext);
+            }
+        }
+
         
inboundSecurityToken.addTokenUsage(WSSecurityTokenConstants.TokenUsage_Encryption);
         TokenSecurityEvent<? extends SecurityToken> tokenSecurityEvent =
             WSSUtils.createTokenSecurityEvent(inboundSecurityToken, 
encryptedDataType.getId());
diff --git 
a/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/WSSEncryptedKeyInputHandler.java
 
b/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/WSSEncryptedKeyInputHandler.java
index 9716181a0..f7550ee28 100644
--- 
a/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/WSSEncryptedKeyInputHandler.java
+++ 
b/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/input/WSSEncryptedKeyInputHandler.java
@@ -49,7 +49,22 @@ public class WSSEncryptedKeyInputHandler extends 
XMLEncryptedKeyInputHandler {
                        XMLSecEvent responsibleXMLSecStartXMLEvent, 
XMLSecurityProperties securityProperties)
         throws XMLSecurityException {
         checkBSPCompliance(inputProcessorChain, encryptedKeyType);
+        checkKeyTransportAlgorithm(encryptedKeyType, securityProperties,
+                                   (WSInboundSecurityContext) 
inputProcessorChain.getSecurityContext());
 
+        super.handle(inputProcessorChain, encryptedKeyType, 
responsibleXMLSecStartXMLEvent, securityProperties);
+    }
+
+    /**
+     * Check the key transport algorithm of the EncryptedKey: it must match 
the required algorithm,
+     * if one is defined, and RSA v1.5 is rejected unless it has been allowed. 
This must be done
+     * before the key is unwrapped. It is also called by DecryptInputProcessor 
for an EncryptedKey
+     * embedded in the KeyInfo of an EncryptedData, which Santuario processes 
without this handler.
+     */
+    static void checkKeyTransportAlgorithm(EncryptedKeyType encryptedKeyType,
+                                           XMLSecurityProperties 
securityProperties,
+                                           WSInboundSecurityContext 
wsInboundSecurityContext)
+        throws WSSecurityException {
         // Check encryption algorithm against the required algorithm, if 
defined
         EncryptionMethodType encryptionMethodType = 
encryptedKeyType.getEncryptionMethod();
         if (securityProperties.getEncryptionKeyTransportAlgorithm() != null
@@ -67,8 +82,6 @@ public class WSSEncryptedKeyInputHandler extends 
XMLEncryptedKeyInputHandler {
         if (encryptionMethodType != null
             && 
WSSConstants.NS_XENC_RSA15.equals(encryptionMethodType.getAlgorithm())
             && !((WSSSecurityProperties) 
securityProperties).isAllowRSA15KeyTransportAlgorithm()) {
-            final WSInboundSecurityContext wsInboundSecurityContext =
-                (WSInboundSecurityContext) 
inputProcessorChain.getSecurityContext();
             Boolean allowRSA15 =
                 
wsInboundSecurityContext.get(WSSConstants.PROP_ALLOW_RSA15_KEYTRANSPORT_ALGORITHM);
             if (allowRSA15 == null || !allowRSA15) {
@@ -76,8 +89,6 @@ public class WSSEncryptedKeyInputHandler extends 
XMLEncryptedKeyInputHandler {
                                               
WSSConstants.PROP_ALLOW_RSA15_KEYTRANSPORT_ALGORITHM);
             }
         }
-
-        super.handle(inputProcessorChain, encryptedKeyType, 
responsibleXMLSecStartXMLEvent, securityProperties);
     }
 
     //if this EncryptedKey structure contains a reference list, instantiate a 
new DecryptInputProcessor
@@ -102,7 +113,11 @@ public class WSSEncryptedKeyInputHandler extends 
XMLEncryptedKeyInputHandler {
 
     protected void checkBSPCompliance(InputProcessorChain inputProcessorChain, 
EncryptedKeyType encryptedKeyType)
             throws XMLSecurityException {
-        final WSInboundSecurityContext securityContext = 
(WSInboundSecurityContext) inputProcessorChain.getSecurityContext();
+        checkBSPCompliance((WSInboundSecurityContext) 
inputProcessorChain.getSecurityContext(), encryptedKeyType);
+    }
+
+    static void checkBSPCompliance(WSInboundSecurityContext securityContext, 
EncryptedKeyType encryptedKeyType)
+            throws WSSecurityException {
         if (encryptedKeyType.getType() != null) {
             securityContext.handleBSPRule(BSPRule.R3209);
         }
diff --git 
a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/VulnerabliltyVectorsTest.java
 
b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/VulnerabliltyVectorsTest.java
index 4011953b1..d792ab90b 100644
--- 
a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/VulnerabliltyVectorsTest.java
+++ 
b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/VulnerabliltyVectorsTest.java
@@ -24,15 +24,25 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
+
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpression;
 
+import org.apache.wss4j.common.WSEncryptionPart;
 import org.apache.wss4j.common.bsp.BSPRule;
+import org.apache.wss4j.common.crypto.CryptoFactory;
 import org.apache.wss4j.common.ext.WSSecurityException;
+import org.apache.wss4j.common.util.KeyUtils;
+import org.apache.wss4j.common.util.SOAPUtil;
+import org.apache.wss4j.dom.WSConstants;
 import org.apache.wss4j.dom.handler.WSHandlerConstants;
+import org.apache.wss4j.dom.message.WSSecEncrypt;
+import org.apache.wss4j.dom.message.WSSecHeader;
 import org.apache.wss4j.stax.ext.WSSConstants;
 import org.apache.wss4j.stax.ext.WSSSecurityProperties;
 import org.junit.jupiter.api.Test;
@@ -411,4 +421,59 @@ public class VulnerabliltyVectorsTest extends 
AbstractTestBase {
             assertEquals(((WSSecurityException) e.getCause()).getFaultCode(), 
WSSecurityException.FAILED_CHECK);
         }
     }
-}
\ No newline at end of file
+
+    /**
+     * RSA v1.5 key transport must also be rejected by default for an 
EncryptedKey embedded in the
+     * KeyInfo of an EncryptedData, rather than placed in the security header. 
Santuario processes
+     * such an EncryptedKey itself, so the check used to be skipped.
+     */
+    @Test
+    public void testDisallowRSA15AlgorithmEmbeddedEncryptedKey() throws 
Exception {
+        Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
+        WSSecHeader secHeader = new WSSecHeader(doc);
+        secHeader.insertSecurityHeader();
+
+        WSSecEncrypt encrypt = new WSSecEncrypt(secHeader);
+        encrypt.setUserInfo("receiver", "default");
+        encrypt.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
+        encrypt.setKeyEncAlgo(WSConstants.KEYTRANSPORT_RSA15);
+        encrypt.setSymmetricEncAlgorithm(WSConstants.AES_128);
+        encrypt.setEmbedEncryptedKey(true);
+        encrypt.getParts().add(new WSEncryptionPart("Body", 
"http://schemas.xmlsoap.org/soap/envelope/";, "Content"));
+
+        KeyGenerator keyGen = KeyUtils.getKeyGenerator(WSConstants.AES_128);
+        SecretKey symmetricKey = keyGen.generateKey();
+        
encrypt.prepare(CryptoFactory.getInstance("transmitter-crypto.properties"), 
symmetricKey);
+        // Only the ReferenceList goes in the security header: the 
EncryptedKey is in the KeyInfo of
+        // the EncryptedData
+        encrypt.addExternalRefElement(encrypt.encrypt(symmetricKey));
+
+        NodeList encryptedKeys = 
doc.getElementsByTagNameNS(WSConstants.ENC_NS, "EncryptedKey");
+        assertEquals(1, encryptedKeys.getLength());
+        assertEquals("KeyInfo", 
encryptedKeys.item(0).getParentNode().getLocalName());
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        TRANSFORMER_FACTORY.newTransformer().transform(new DOMSource(doc), new 
StreamResult(baos));
+
+        // Rejected by default...
+        WSSSecurityProperties inboundsecurityProperties = new 
WSSSecurityProperties();
+        inboundsecurityProperties.setCallbackHandler(new 
CallbackHandlerImpl());
+        
inboundsecurityProperties.loadDecryptionKeystore(this.getClass().getClassLoader().getResource("receiver.jks"),
 "default".toCharArray());
+        try {
+            doInboundSecurity(inboundsecurityProperties,
+                    xmlInputFactory.createXMLStreamReader(
+                            new ByteArrayInputStream(baos.toByteArray())));
+            fail("Expected XMLStreamException");
+        } catch (XMLStreamException e) {
+            assertTrue(e.getCause() instanceof WSSecurityException);
+            assertEquals(((WSSecurityException) e.getCause()).getFaultCode(), 
WSSecurityException.FAILED_CHECK);
+        }
+
+        // ...but the message decrypts once RSA v1.5 is allowed, so the 
rejection is the RSA v1.5 check
+        inboundsecurityProperties.setAllowRSA15KeyTransportAlgorithm(true);
+        Document document = doInboundSecurity(inboundsecurityProperties,
+                xmlInputFactory.createXMLStreamReader(
+                        new ByteArrayInputStream(baos.toByteArray())));
+        assertEquals(0, document.getElementsByTagNameNS(WSConstants.ENC_NS, 
"EncryptedData").getLength());
+    }
+}

Reply via email to