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 87da6b67 RAMPART-362: reference issued SAML tokens with a 
wsse:KeyIdentifier
87da6b67 is described below

commit 87da6b675b724d1b49da182511631e56af73eea3
Author: Robert Lazarski <[email protected]>
AuthorDate: Tue Nov 17 16:09:31 2026 -1000

    RAMPART-362: reference issued SAML tokens with a wsse:KeyIdentifier
    
    Signatures and encryption keyed by an issued SAML token referenced the
    assertion with a wsse:Reference URI. That is a local reference, so a
    recipient such as a WCF client cannot resolve it when the assertion is
    not in the message, as in a response to an AlwaysToRecipient token. The
    builders now emit the SAML Token Profile KeyIdentifier, with the SAMLID
    value type for SAML 2.0 assertions instead of the SAML 1.x one always.
    
    Co-Authored-By: Claude Fable 5.1 <[email protected]>
---
 .../org/apache/rampart/builder/BindingBuilder.java |   7 +-
 .../rampart/builder/SymmetricBindingBuilder.java   |  14 ++-
 .../rampart/builder/TransportBindingBuilder.java   |   6 +-
 .../java/org/apache/rampart/util/RampartUtil.java  |  20 ++++
 .../apache/rampart/IssuedTokenReferenceTest.java   | 133 +++++++++++++++++++++
 .../policy/rampart-symm-binding-issued-token.xml   |  79 ++++++++++++
 6 files changed, 251 insertions(+), 8 deletions(-)

diff --git 
a/modules/rampart-core/src/main/java/org/apache/rampart/builder/BindingBuilder.java
 
b/modules/rampart-core/src/main/java/org/apache/rampart/builder/BindingBuilder.java
index c1a459dc..83281aa5 100644
--- 
a/modules/rampart-core/src/main/java/org/apache/rampart/builder/BindingBuilder.java
+++ 
b/modules/rampart-core/src/main/java/org/apache/rampart/builder/BindingBuilder.java
@@ -696,9 +696,10 @@ public abstract class BindingBuilder {
                     }
 
                 } else if (policyToken instanceof IssuedToken) {
-
-                    
sig.setCustomTokenValueType(RampartUtil.getSAML10AssertionNamespace());
-                    sig.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
+                    // RAMPART-362: reference the SAML assertion with a 
wsse:KeyIdentifier,
+                    // see RampartUtil.getSAMLKeyIdentifierValueType
+                    
sig.setCustomTokenValueType(RampartUtil.getSAMLKeyIdentifierValueType(tok));
+                    
sig.setKeyIdentifierType(WSConstants.CUSTOM_KEY_IDENTIFIER);
                 }
                 
                 String sigTokId; 
diff --git 
a/modules/rampart-core/src/main/java/org/apache/rampart/builder/SymmetricBindingBuilder.java
 
b/modules/rampart-core/src/main/java/org/apache/rampart/builder/SymmetricBindingBuilder.java
index 602cc40e..6e071725 100644
--- 
a/modules/rampart-core/src/main/java/org/apache/rampart/builder/SymmetricBindingBuilder.java
+++ 
b/modules/rampart-core/src/main/java/org/apache/rampart/builder/SymmetricBindingBuilder.java
@@ -294,7 +294,11 @@ public class SymmetricBindingBuilder extends 
BindingBuilder {
                 encr.setSymmetricEncAlgorithm(algorithmSuite.getEncryption());
                 // SymmKey is already encrypted, no need to do it again
                 encr.setEncryptSymmKey(false);
-                if (!rmd.isInitiator() && tok instanceof EncryptedKeyToken) {
+                if (encryptionToken instanceof IssuedToken) {
+                    // RAMPART-362: reference the SAML assertion with a 
wsse:KeyIdentifier,
+                    // see RampartUtil.getSAMLKeyIdentifierValueType
+                    
encr.setCustomReferenceValue(RampartUtil.getSAMLKeyIdentifierValueType(tok));
+                } else if (!rmd.isInitiator() && tok instanceof 
EncryptedKeyToken) {
                     // TODO was encr.setUseKeyIdentifier(true); - verify
                     encr.setEncKeyIdDirectId(true);
                     
encr.setCustomReferenceValue(((EncryptedKeyToken)tok).getSHA1());
@@ -726,8 +730,12 @@ public class SymmetricBindingBuilder extends 
BindingBuilder {
                     RampartUtil.setEncryptionUser(rmd, encr);
                     encr.setEncryptSymmKey(false);
                     
encr.setSymmetricEncAlgorithm(rpd.getAlgorithmSuite().getEncryption());
-                    // Use key identifier in the KeyInfo in server side
-                    if (!rmd.isInitiator()) {
+                    if (encrToken instanceof IssuedToken) {
+                        // RAMPART-362: reference the SAML assertion with a 
wsse:KeyIdentifier,
+                        // see RampartUtil.getSAMLKeyIdentifierValueType
+                        
encr.setCustomReferenceValue(RampartUtil.getSAMLKeyIdentifierValueType(encrTok));
+                    } else if (!rmd.isInitiator()) {
+                        // Use key identifier in the KeyInfo in server side
                         if(encrTok instanceof EncryptedKeyToken) {
                             // TODO was encr.setUseKeyIdentifier(true); verify
                             encr.setEncKeyIdDirectId(true);
diff --git 
a/modules/rampart-core/src/main/java/org/apache/rampart/builder/TransportBindingBuilder.java
 
b/modules/rampart-core/src/main/java/org/apache/rampart/builder/TransportBindingBuilder.java
index 3d642893..27fd3a83 100644
--- 
a/modules/rampart-core/src/main/java/org/apache/rampart/builder/TransportBindingBuilder.java
+++ 
b/modules/rampart-core/src/main/java/org/apache/rampart/builder/TransportBindingBuilder.java
@@ -552,11 +552,13 @@ public class TransportBindingBuilder extends 
BindingBuilder {
                    tokId = tokId.substring(1);
                 }
                 sig.setCustomTokenId(tokId);
-                
sig.setCustomTokenValueType(RampartUtil.getSAML10AssertionNamespace());
+                // RAMPART-362: reference the SAML assertion with a 
wsse:KeyIdentifier,
+                // see RampartUtil.getSAMLKeyIdentifierValueType
+                
sig.setCustomTokenValueType(RampartUtil.getSAMLKeyIdentifierValueType(tok));
                 sig.setSecretKey(tok.getSecret());
                 
sig.setSignatureAlgorithm(algorithmSuite.getAsymmetricSignature());
                 
sig.setSignatureAlgorithm(algorithmSuite.getSymmetricSignature());
-                sig.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
+                sig.setKeyIdentifierType(WSConstants.CUSTOM_KEY_IDENTIFIER);
                 
sig.prepare(RampartUtil.getSignatureCrypto(rpd.getRampartConfig(), 
rmd.getCustomClassLoader()));
 
                 sig.getParts().addAll(sigParts);
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 56822844..28099363 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
@@ -2052,6 +2052,26 @@ public class RampartUtil {
 
     }
 
+    /**
+     * Returns the wsse:KeyIdentifier ValueType that references the given SAML 
assertion
+     * token: SAMLAssertionID for a SAML 1.x assertion, SAMLID for a SAML 2.0 
assertion.
+     * <p>
+     * RAMPART-362: the SAML Token Profile references an assertion with a 
KeyIdentifier,
+     * not with a wsse:Reference URI. A wsse:Reference is a local reference, 
and a
+     * recipient cannot resolve it when the assertion is not in the message, 
which is the
+     * case for a response secured with an AlwaysToRecipient issued token.
+     *
+     * @param token the issued SAML token
+     * @return the KeyIdentifier ValueType for the assertion's SAML version
+     */
+    public static String getSAMLKeyIdentifierValueType(Token token) {
+        if (token != null && token.getToken() != null
+                && 
WSConstants.SAML2_NS.equals(token.getToken().getQName().getNamespaceURI())) {
+            return WSConstants.WSS_SAML2_KI_VALUE_TYPE;
+        }
+        return WSConstants.WSS_SAML_KI_VALUE_TYPE;
+    }
+
     /**
      * Sets encryption crypto file or crypto reference key to signature crypto 
file or signature
      * crypto reference.
diff --git 
a/modules/rampart-tests/src/test/java/org/apache/rampart/IssuedTokenReferenceTest.java
 
b/modules/rampart-tests/src/test/java/org/apache/rampart/IssuedTokenReferenceTest.java
new file mode 100644
index 00000000..22e9636b
--- /dev/null
+++ 
b/modules/rampart-tests/src/test/java/org/apache/rampart/IssuedTokenReferenceTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axis2.context.MessageContext;
+import org.apache.neethi.Policy;
+import org.apache.rahas.Token;
+import org.apache.rahas.TrustUtil;
+import org.apache.wss4j.dom.WSConstants;
+
+/**
+ * RAMPART-362: a message protected with an issued SAML token references the
+ * assertion from ds:KeyInfo with a wsse:KeyIdentifier, as the SAML Token 
Profile
+ * requires, and not with a wsse:Reference URI. The URI form is a local 
reference
+ * that a recipient cannot resolve when the assertion is not in the message.
+ */
+public class IssuedTokenReferenceTest extends MessageBuilderTestBase {
+
+    private static final String SAML1_NS = 
"urn:oasis:names:tc:SAML:1.0:assertion";
+    private static final String SAML2_NS = 
"urn:oasis:names:tc:SAML:2.0:assertion";
+    private static final QName STR = new QName(WSConstants.WSSE_NS, 
"SecurityTokenReference");
+    private static final QName KEY_IDENTIFIER = new QName(WSConstants.WSSE_NS, 
"KeyIdentifier");
+    private static final QName REFERENCE = new QName(WSConstants.WSSE_NS, 
"Reference");
+    private static final QName TOKEN_TYPE = new QName(WSConstants.WSSE11_NS, 
"TokenType");
+
+    /** A stand-in for an issued holder-of-key assertion: only its id and 
namespace matter here. */
+    private static OMElement assertion(String ns, String idAttribute, String 
id) {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        OMNamespace saml = fac.createOMNamespace(ns, "saml");
+        OMElement assertion = fac.createOMElement("Assertion", saml);
+        assertion.addAttribute(idAttribute, id, null);
+        return assertion;
+    }
+
+    private SOAPEnvelope build(OMElement assertion, String id) throws 
Exception {
+        MessageContext ctx = getMsgCtx();
+        Policy policy = 
loadPolicy("test-resources/policy/rampart-symm-binding-issued-token.xml");
+        ctx.setProperty(RampartMessageData.KEY_RAMPART_POLICY, policy);
+
+        Token token = new Token(id, assertion, new Date(),
+                new Date(System.currentTimeMillis() + 300000));
+        token.setSecret(new byte[16]);
+        TrustUtil.getTokenStore(ctx).add(token);
+        // The token is already issued: no STS round trip
+        ctx.setProperty(RampartMessageData.KEY_CUSTOM_ISSUED_TOKEN, id);
+
+        new MessageBuilder().build(ctx);
+
+        List<QName> expected = new ArrayList<QName>();
+        expected.add(new QName(WSConstants.WSU_NS, 
WSConstants.TIMESTAMP_TOKEN_LN));
+        expected.add(assertion.getQName());
+        expected.add(new QName(WSConstants.ENC_NS, WSConstants.REF_LIST_LN));
+        expected.add(new QName(WSConstants.SIG_NS, WSConstants.SIG_LN));
+        verifySecHeader(expected.iterator(), ctx.getEnvelope());
+        return ctx.getEnvelope();
+    }
+
+    private static OMElement securityHeader(SOAPEnvelope env) {
+        return env.getHeader().getFirstChildWithName(
+                new QName(WSConstants.WSSE_NS, WSConstants.WSSE_LN));
+    }
+
+    private static OMElement keyInfoStr(OMElement parent) {
+        OMElement keyInfo = parent.getFirstChildWithName(
+                new QName(WSConstants.SIG_NS, WSConstants.KEYINFO_LN));
+        assertNotNull("ds:KeyInfo missing under " + parent.getQName(), 
keyInfo);
+        OMElement str = keyInfo.getFirstChildWithName(STR);
+        assertNotNull("wsse:SecurityTokenReference missing under " + 
parent.getQName(), str);
+        return str;
+    }
+
+    private static void assertSamlKeyIdentifier(OMElement str, String 
valueType,
+                                                String tokenType, String id) {
+        assertNull("the assertion must not be referenced by URI", 
str.getFirstChildWithName(REFERENCE));
+        OMElement ki = str.getFirstChildWithName(KEY_IDENTIFIER);
+        assertNotNull("wsse:KeyIdentifier missing", ki);
+        assertEquals(valueType, ki.getAttributeValue(new QName("ValueType")));
+        assertEquals(id, ki.getText());
+        assertEquals(tokenType, str.getAttributeValue(TOKEN_TYPE));
+    }
+
+    private static void assertReferences(SOAPEnvelope env, String valueType, 
String tokenType,
+                                         String id) {
+        OMElement secHeader = securityHeader(env);
+        OMElement signature = secHeader.getFirstChildWithName(
+                new QName(WSConstants.SIG_NS, WSConstants.SIG_LN));
+        assertSamlKeyIdentifier(keyInfoStr(signature), valueType, tokenType, 
id);
+
+        Iterator<OMElement> bodyChildren = env.getBody().getChildElements();
+        assertTrue("body must be encrypted", bodyChildren.hasNext());
+        OMElement encryptedData = bodyChildren.next();
+        assertEquals(new QName(WSConstants.ENC_NS, WSConstants.ENC_DATA_LN), 
encryptedData.getQName());
+        assertSamlKeyIdentifier(keyInfoStr(encryptedData), valueType, 
tokenType, id);
+    }
+
+    public void testSaml11AssertionReferencedByKeyIdentifier() throws 
Exception {
+        String id = "_saml1-assertion-id";
+        SOAPEnvelope env = build(assertion(SAML1_NS, "AssertionID", id), id);
+        assertReferences(env, WSConstants.WSS_SAML_KI_VALUE_TYPE, 
WSConstants.WSS_SAML_TOKEN_TYPE, id);
+    }
+
+    public void testSaml20AssertionReferencedByKeyIdentifier() throws 
Exception {
+        String id = "_saml2-assertion-id";
+        SOAPEnvelope env = build(assertion(SAML2_NS, "ID", id), id);
+        assertReferences(env, WSConstants.WSS_SAML2_KI_VALUE_TYPE, 
WSConstants.WSS_SAML2_TOKEN_TYPE, id);
+    }
+}
diff --git 
a/modules/rampart-tests/test-resources/policy/rampart-symm-binding-issued-token.xml
 
b/modules/rampart-tests/test-resources/policy/rampart-symm-binding-issued-token.xml
new file mode 100644
index 00000000..cf313d9e
--- /dev/null
+++ 
b/modules/rampart-tests/test-resources/policy/rampart-symm-binding-issued-token.xml
@@ -0,0 +1,79 @@
+<wsp:Policy wsu:Id="issuedTokenPolicy" 
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>
+                       <sp:SymmetricBinding 
xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy";>
+                               <wsp:Policy>
+                                       <sp:ProtectionToken>
+                                               <wsp:Policy>
+                                                       <sp:IssuedToken 
sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient";>
+                                                               <sp:Issuer>
+                                                                       
<wsa:Address 
xmlns:wsa="http://www.w3.org/2005/08/addressing";>http://localhost/sts</wsa:Address>
+                                                               </sp:Issuer>
+                                                               
<sp:RequestSecurityTokenTemplate>
+                                                                       
<t:TokenType 
xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust";>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>
+                                                                       
<t:KeyType 
xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust";>http://schemas.xmlsoap.org/ws/2005/02/trust/SymmetricKey</t:KeyType>
+                                                                       
<t:KeySize xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust";>128</t:KeySize>
+                                                               
</sp:RequestSecurityTokenTemplate>
+                                                               <wsp:Policy>
+                                                                       
<sp:RequireInternalReference/>
+                                                               </wsp:Policy>
+                                                       </sp:IssuedToken>
+                                               </wsp:Policy>
+                                       </sp:ProtectionToken>
+                                       <sp:AlgorithmSuite>
+                                               <wsp:Policy>
+                                                       <sp:Basic128/>
+                                               </wsp:Policy>
+                                       </sp:AlgorithmSuite>
+                                       <sp:Layout>
+                                               <wsp:Policy>
+                                                       <sp:Lax/>
+                                               </wsp:Policy>
+                                       </sp:Layout>
+                                       <sp:IncludeTimestamp/>
+                                       <sp:OnlySignEntireHeadersAndBody/>
+                               </wsp:Policy>
+                       </sp:SymmetricBinding>
+                       <sp:Wss10 
xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy";>
+                               <wsp:Policy>
+                                       <sp:MustSupportRefKeyIdentifier/>
+                                       <sp:MustSupportRefIssuerSerial/>
+                               </wsp:Policy>
+                       </sp:Wss10>
+                       <sp:Trust10 
xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy";>
+                               <wsp:Policy>
+                                       <sp:MustSupportIssuedTokens/>
+                                       <sp:RequireClientEntropy/>
+                                       <sp:RequireServerEntropy/>
+                               </wsp:Policy>
+                       </sp:Trust10>
+                       <sp:SignedParts 
xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy";>
+                               <sp:Body/>
+                       </sp:SignedParts>
+                       <sp:EncryptedParts 
xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy";>
+                               <sp:Body/>
+                       </sp:EncryptedParts>
+
+                       <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