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

robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-java-rampart.git


The following commit(s) were added to refs/heads/master by this push:
     new 201d23f9 RAMPART-421: handle sp:ContentEncryptedElements on its own
201d23f9 is described below

commit 201d23f93b695ee19da6d0f97d46aba4243654d2
Author: Robert Lazarski <[email protected]>
AuthorDate: Tue Nov 17 04:40:44 2026 -1000

    RAMPART-421: handle sp:ContentEncryptedElements on its own
    
    ContentEncryptedElements serialized its XPath expressions without the
    namespace declarations they rely on, unlike SignedEncryptedElements,
    and a policy whose only protection is content encryption counted as
    needing no security header, so the client sent the message in the
    clear. Both now match the EncryptedElements behaviour.
    
    Co-Authored-By: Claude Fable 5.1 <[email protected]>
---
 .../java/org/apache/rampart/util/RampartUtil.java  |   6 +-
 .../secpolicy/model/ContentEncryptedElements.java  |   8 ++
 .../rampart/ContentEncryptedElementsTest.java      | 145 +++++++++++++++++++++
 .../policy/rampart-content-encrypted-only.xml      |  60 +++++++++
 4 files changed, 217 insertions(+), 2 deletions(-)

diff --git 
a/modules/rampart-core/src/main/java/org/apache/rampart/util/RampartUtil.java 
b/modules/rampart-core/src/main/java/org/apache/rampart/util/RampartUtil.java
index 374583de..56822844 100644
--- 
a/modules/rampart-core/src/main/java/org/apache/rampart/util/RampartUtil.java
+++ 
b/modules/rampart-core/src/main/java/org/apache/rampart/util/RampartUtil.java
@@ -1743,9 +1743,11 @@ public class RampartUtil {
 
         // Checking for encrypted parts and elements
         if ((rpd.isEncryptBody() && (countOptional || 
!rpd.isEncryptBodyOptional()))
-                || rpd.getEncryptedParts().size() != 0 || 
rpd.getEncryptedElements().size() != 0 ) {
+                || rpd.getEncryptedParts().size() != 0 || 
rpd.getEncryptedElements().size() != 0
+            || rpd.getContentEncryptedElements().size() != 0) { // RAMPART-421
             log.debug("RampartUtil.isSecHeaderRequired: TRUE - encryption 
required (encryptBody=" + rpd.isEncryptBody() +
-                      ", encryptedParts=" + rpd.getEncryptedParts().size() + 
", encryptedElements=" + rpd.getEncryptedElements().size() + ")");
+                      ", encryptedParts=" + rpd.getEncryptedParts().size() + 
", encryptedElements=" + rpd.getEncryptedElements().size()
+                      + ", contentEncryptedElements=" + 
rpd.getContentEncryptedElements().size() + ")");
             return true;
         } else {
             log.debug("RampartUtil.isSecHeaderRequired: No encryption 
required");
diff --git 
a/modules/rampart-policy/src/main/java/org/apache/ws/secpolicy/model/ContentEncryptedElements.java
 
b/modules/rampart-policy/src/main/java/org/apache/ws/secpolicy/model/ContentEncryptedElements.java
index ae4cfbf5..70a62cb2 100644
--- 
a/modules/rampart-policy/src/main/java/org/apache/ws/secpolicy/model/ContentEncryptedElements.java
+++ 
b/modules/rampart-policy/src/main/java/org/apache/ws/secpolicy/model/ContentEncryptedElements.java
@@ -19,6 +19,7 @@ package org.apache.ws.secpolicy.model;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.HashMap;
+import java.util.Map;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
@@ -94,6 +95,13 @@ public class ContentEncryptedElements extends 
AbstractSecurityAssertion {
             xpathExpression = iterator.next();
             // <sp:XPath ..>
             writeStartElement(writer, prefix, SPConstants.XPATH_EXPR, 
namespaceURI);
+
+            // RAMPART-421: the expression is only meaningful with the 
prefixes it
+            // was declared with, as SignedEncryptedElements already 
serializes.
+            for (Map.Entry<String, String> declared : 
declaredNamespaces.entrySet()) {
+                writer.writeNamespace(declared.getKey(), declared.getValue());
+            }
+
             writer.writeCharacters(xpathExpression);
             writer.writeEndElement();
         }
diff --git 
a/modules/rampart-tests/src/test/java/org/apache/rampart/ContentEncryptedElementsTest.java
 
b/modules/rampart-tests/src/test/java/org/apache/rampart/ContentEncryptedElementsTest.java
new file mode 100644
index 00000000..8390c26a
--- /dev/null
+++ 
b/modules/rampart-tests/src/test/java/org/apache/rampart/ContentEncryptedElementsTest.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.rampart;
+
+import java.io.ByteArrayInputStream;
+import java.io.StringWriter;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axis2.builder.SOAPBuilder;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.AxisService;
+import org.apache.axis2.description.Parameter;
+import org.apache.neethi.Assertion;
+import org.apache.neethi.Policy;
+import org.apache.neethi.PolicyEngine;
+import org.apache.rampart.policy.RampartPolicyBuilder;
+import org.apache.rampart.policy.RampartPolicyData;
+import org.apache.rampart.util.RampartUtil;
+import org.apache.ws.secpolicy.model.ContentEncryptedElements;
+import org.apache.wss4j.dom.WSConstants;
+import org.apache.wss4j.dom.handler.WSHandlerConstants;
+
+/**
+ * RAMPART-421: sp:ContentEncryptedElements lost the namespace declarations of 
its
+ * XPath expressions when serialized, and a policy whose only protection is 
content
+ * encryption was treated as needing no security header at all, so the client 
sent
+ * the message in the clear and the service refused it.
+ */
+public class ContentEncryptedElementsTest extends MessageBuilderTestBase {
+
+    private static final String POLICY = 
"test-resources/policy/rampart-content-encrypted-only.xml";
+    private static final String WSA_NS = 
"http://schemas.xmlsoap.org/ws/2004/08/addressing";;
+
+    private static ContentEncryptedElements contentEncryptedElements(Policy 
policy) {
+        List<Assertion> alternative = policy.getAlternatives().next();
+        for (Assertion assertion : alternative) {
+            if (assertion instanceof ContentEncryptedElements) {
+                return (ContentEncryptedElements) assertion;
+            }
+        }
+        fail("no sp:ContentEncryptedElements in the policy");
+        return null;
+    }
+
+    private static String serialize(Policy policy) throws Exception {
+        StringWriter out = new StringWriter();
+        XMLStreamWriter writer = 
XMLOutputFactory.newInstance().createXMLStreamWriter(out);
+        policy.serialize(writer);
+        writer.flush();
+        return out.toString();
+    }
+
+    /** Serialize, parse again, and the XPath prefixes must still resolve. */
+    public void testSerializedXPathKeepsItsNamespaceDeclarations() throws 
Exception {
+        Policy original = loadPolicy(POLICY);
+        assertEquals(WSA_NS, 
contentEncryptedElements(original).getDeclaredNamespaces().get("wsa"));
+
+        String xml = serialize(original);
+        assertTrue("sp:XPath must carry xmlns:wsa, was: " + xml,
+                xml.contains("xmlns:wsa=\"" + WSA_NS + "\""));
+
+        Policy reparsed = PolicyEngine.getPolicy(AXIOMUtil.stringToOM(xml));
+        ContentEncryptedElements roundTripped = 
contentEncryptedElements(reparsed);
+        assertEquals(2, roundTripped.getXPathExpressions().size());
+        assertEquals(WSA_NS, roundTripped.getDeclaredNamespaces().get("wsa"));
+    }
+
+    /** Content encryption alone is a reason to build, and to expect, a 
security header. */
+    public void testContentOnlyPolicyRequiresASecurityHeader() throws 
Exception {
+        RampartPolicyData rpd = 
RampartPolicyBuilder.build(loadPolicy(POLICY).getAlternatives().next());
+        assertEquals(2, rpd.getContentEncryptedElements().size());
+        assertTrue("client must build a header", 
RampartUtil.isSecHeaderRequired(rpd, true, false));
+        assertTrue("service must expect a header", 
RampartUtil.isSecHeaderRequired(rpd, false, true));
+    }
+
+    /** The client encrypts the header contents and the service decrypts and 
accepts them. */
+    public void testHeaderContentIsEncryptedAndAcceptedEndToEnd() throws 
Exception {
+        MessageContext ctx = getMsgCtx();
+        Policy policy = loadPolicy(POLICY);
+        ctx.setProperty(RampartMessageData.KEY_RAMPART_POLICY, policy);
+        String originalTo = header(ctx.getEnvelope(), "To").getText();
+
+        new MessageBuilder().build(ctx);
+
+        OMElement security = 
ctx.getEnvelope().getHeader().getFirstChildWithName(
+                new QName(WSConstants.WSSE_NS, WSConstants.WSSE_LN));
+        assertNotNull("no security header was built", security);
+        for (String name : new String[] {"To", "Action"}) {
+            OMElement encrypted = header(ctx.getEnvelope(), 
name).getFirstChildWithName(
+                    new QName(WSConstants.ENC_NS, WSConstants.ENC_DATA_LN));
+            assertNotNull("wsa:" + name + " content must be encrypted", 
encrypted);
+            assertEquals("content, not element, encryption",
+                    WSConstants.ENC_NS + "Content", 
encrypted.getAttributeValue(new QName("Type")));
+        }
+
+        processAsService(ctx, policy);
+        assertEquals("wsa:To must be back in the clear after processing",
+                originalTo, header(ctx.getEnvelope(), "To").getText());
+    }
+
+    private static OMElement header(SOAPEnvelope env, String localName) {
+        OMElement h = env.getHeader().getFirstChildWithName(new QName(WSA_NS, 
localName));
+        assertNotNull("wsa:" + localName + " header missing", h);
+        return h;
+    }
+
+    private static void processAsService(MessageContext ctx, Policy policy) 
throws Exception {
+        SOAPEnvelope env = ctx.getEnvelope();
+        ByteArrayInputStream in = new 
ByteArrayInputStream(env.toString().getBytes("UTF-8"));
+        env = (SOAPEnvelope) new SOAPBuilder().processDocument(in, "text/xml", 
ctx);
+        ctx.setEnvelope(env);
+
+        ctx.setServerSide(true);
+        AxisService axisService = ctx.getAxisService();
+        Parameter clientSide = 
axisService.getParameter(RampartMessageData.PARAM_CLIENT_SIDE);
+        if (clientSide != null) {
+            axisService.removeParameter(clientSide);
+        }
+        ctx.setProperty(RampartMessageData.KEY_RAMPART_POLICY, policy);
+        ctx.setProperty(WSHandlerConstants.PW_CALLBACK_REF, new 
TestCBHandler());
+
+        new RampartEngine().process(ctx);
+    }
+}
diff --git 
a/modules/rampart-tests/test-resources/policy/rampart-content-encrypted-only.xml
 
b/modules/rampart-tests/test-resources/policy/rampart-content-encrypted-only.xml
new file mode 100644
index 00000000..26433606
--- /dev/null
+++ 
b/modules/rampart-tests/test-resources/policy/rampart-content-encrypted-only.xml
@@ -0,0 +1,60 @@
+<wsp:Policy wsu:Id="ContentEncryptedOnly" 
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
 xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy";>
+       <wsp:ExactlyOne>
+               <wsp:All>
+                       <!-- RAMPART-421: the only protection asked for is 
content encryption of two
+                            headers. No timestamp, no signed parts, no 
encrypted parts. -->
+                       <sp:AsymmetricBinding 
xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702";>
+                               <wsp:Policy>
+                                       <sp:InitiatorToken>
+                                               <wsp:Policy>
+                                                       <sp:X509Token 
sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient";>
+                                                               <wsp:Policy>
+                                                                       
<sp:WssX509V3Token10/>
+                                                               </wsp:Policy>
+                                                       </sp:X509Token>
+                                               </wsp:Policy>
+                                       </sp:InitiatorToken>
+                                       <sp:RecipientToken>
+                                               <wsp:Policy>
+                                                       <sp:X509Token 
sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/Never";>
+                                                               <wsp:Policy>
+                                                                       
<sp:WssX509V3Token10/>
+                                                               </wsp:Policy>
+                                                       </sp:X509Token>
+                                               </wsp:Policy>
+                                       </sp:RecipientToken>
+                                       <sp:AlgorithmSuite>
+                                               <wsp:Policy>
+                                                       <sp:Basic128/>
+                                               </wsp:Policy>
+                                       </sp:AlgorithmSuite>
+                               </wsp:Policy>
+                       </sp:AsymmetricBinding>
+                       <sp:ContentEncryptedElements 
xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702";>
+                               <sp:XPath 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"; 
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing";>/soapenv:Envelope/soapenv:Header/wsa:To</sp:XPath>
+                               <sp:XPath 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"; 
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing";>/soapenv:Envelope/soapenv:Header/wsa:Action</sp:XPath>
+                       </sp:ContentEncryptedElements>
+
+                       <ramp:RampartConfig 
xmlns:ramp="http://ws.apache.org/rampart/policy";>
+                               <ramp:user>alice</ramp:user>
+                               <ramp:encryptionUser>bob</ramp:encryptionUser>
+                               
<ramp:passwordCallbackClass>org.apache.rampart.TestCBHandler</ramp:passwordCallbackClass>
+
+                               <ramp:signatureCrypto>
+                                       <ramp:crypto 
provider="org.apache.ws.security.components.crypto.Merlin">
+                                               <ramp:property 
name="org.apache.ws.security.crypto.merlin.keystore.type">pkcs12</ramp:property>
+                                               <ramp:property 
name="org.apache.ws.security.crypto.merlin.file">test-resources/keys/interop2024.pkcs12</ramp:property>
+                                               <ramp:property 
name="org.apache.ws.security.crypto.merlin.keystore.password">password</ramp:property>
+                                       </ramp:crypto>
+                               </ramp:signatureCrypto>
+                               <ramp:encryptionCypto>
+                                       <ramp:crypto 
provider="org.apache.ws.security.components.crypto.Merlin">
+                                               <ramp:property 
name="org.apache.ws.security.crypto.merlin.keystore.type">pkcs12</ramp:property>
+                                               <ramp:property 
name="org.apache.ws.security.crypto.merlin.file">test-resources/keys/interop2024.pkcs12</ramp:property>
+                                               <ramp:property 
name="org.apache.ws.security.crypto.merlin.keystore.password">password</ramp:property>
+                                       </ramp:crypto>
+                               </ramp:encryptionCypto>
+                       </ramp:RampartConfig>
+               </wsp:All>
+       </wsp:ExactlyOne>
+</wsp:Policy>

Reply via email to