Author: coheigea
Date: Mon Dec 22 04:31:02 2008
New Revision: 728664

URL: http://svn.apache.org/viewvc?rev=728664&view=rev
Log:
[WSS-60] - A fix for "Problems when SOAP envelope namespace prefix is null"
 - The first problem with "createElementInSameNamespace" was already fixed 
since the JIRA was raised
 - Added a fix for the second problem
 - Added a test as well

Added:
    webservices/wss4j/trunk/test/wssec/TestWSSecurityWSS60.java   (with props)
Modified:
    webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java
    webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncrypt.java
    webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecHeader.java
    webservices/wss4j/trunk/src/org/apache/ws/security/util/WSSecurityUtil.java
    webservices/wss4j/trunk/test/wssec/PackageTests.java
    webservices/wss4j/trunk/test/wssec/TestWSSecuritySignatureParts.java
    webservices/wss4j/trunk/test/wssec/TestWSSecurityUTSignature.java

Modified: webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java?rev=728664&r1=728663&r2=728664&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java 
(original)
+++ webservices/wss4j/trunk/src/org/apache/ws/security/WSConstants.java Mon Dec 
22 04:31:02 2008
@@ -78,6 +78,7 @@
     public static final String WSSE_PREFIX = "wsse";
     public static final String WSSE11_PREFIX = "wsse11";
     public static final String WSU_PREFIX = "wsu";
+    public static final String DEFAULT_SOAP_PREFIX = "soapenv";
     
     /*
      * Now the namespaces, local names, and prefixes of XML-SIG and XML-ENC

Modified: 
webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncrypt.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncrypt.java?rev=728664&r1=728663&r2=728664&view=diff
==============================================================================
--- 
webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncrypt.java 
(original)
+++ 
webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecEncrypt.java 
Mon Dec 22 04:31:02 2008
@@ -225,7 +225,7 @@
          */
         
         
-        if(this.ephemeralKey == null) {
+        if (this.ephemeralKey == null) {
             if (symmetricKey == null) {
                 KeyGenerator keyGen = getKeyGenerator();
                 this.symmetricKey = keyGen.generateKey();
@@ -234,7 +234,6 @@
         }
         
         if (this.symmetricKey == null) {
-
             this.symmetricKey = WSSecurityUtil.prepareSecretKey(symEncAlgo,
                     this.ephemeralKey);
         }
@@ -243,7 +242,7 @@
          * Get the certificate that contains the public key for the public key
          * algorithm that will encrypt the generated symmetric (session) key.
          */
-        if(this.encryptSymmKey) {
+        if (this.encryptSymmKey) {
             X509Certificate remoteCert = null;
             if (useThisCert != null) {
                 remoteCert = useThisCert;
@@ -445,7 +444,7 @@
             keyIdentifierType == WSConstants.ENCRYPTED_KEY_SHA1_IDENTIFIER) {
             keyInfo = new KeyInfo(document);
             SecurityTokenReference secToken = new 
SecurityTokenReference(document);
-            if(this.customReferenceValue != null) {
+            if (this.customReferenceValue != null) {
                 secToken.setKeyIdentifierEncKeySHA1(this.customReferenceValue);
             } else {
                 
secToken.setKeyIdentifierEncKeySHA1(getSHA1(encryptedEphemeralKey));
@@ -510,7 +509,7 @@
 
             cloneKeyInfo = true;
             
-            if(keyInfo == null) {
+            if (keyInfo == null) {
                 keyInfo = new KeyInfo(document);
                 SecurityTokenReference secToken = new 
SecurityTokenReference(document);
                 Reference ref = new Reference(document);
@@ -522,7 +521,7 @@
                         + WSConstants.SIG_PREFIX, WSConstants.SIG_NS);
             }
             /*
-             * Forth step: encrypt data, and set necessary attributes in
+             * Fourth step: encrypt data, and set necessary attributes in
              * xenc:EncryptedData
              */
             try {
@@ -543,7 +542,7 @@
                         if 
(attr.getNamespaceURI().equals(WSConstants.URI_SOAP11_ENV)
                                 || 
attr.getNamespaceURI().equals(WSConstants.URI_SOAP12_ENV)) {                    
     
                             String soapEnvPrefix = 
WSSecurityUtil.setNamespace(elem,
-                                    attr.getNamespaceURI(), "soapevn");
+                                    attr.getNamespaceURI(), 
WSConstants.DEFAULT_SOAP_PREFIX);
                             elem.setAttributeNS(attr.getNamespaceURI(), 
soapEnvPrefix +":"+attr.getLocalName(), attr.getValue());
                         }
                     }
@@ -568,7 +567,7 @@
                     encData.setKeyInfo(keyInfo);
                     xmlCipher.doFinal(doc, body, content);          
                 }
-                if(cloneKeyInfo) {
+                if (cloneKeyInfo) {
                     keyInfo = new KeyInfo((Element) keyInfo.getElement()
                             .cloneNode(true), null);
                 }

Modified: 
webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecHeader.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecHeader.java?rev=728664&r1=728663&r2=728664&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecHeader.java 
(original)
+++ webservices/wss4j/trunk/src/org/apache/ws/security/message/WSSecHeader.java 
Mon Dec 22 04:31:02 2008
@@ -115,18 +115,16 @@
         if (securityHeader == null) {            
             securityHeader = WSSecurityUtil.findWsseSecurityHeaderBlock(doc, 
doc
                     .getDocumentElement(), actor, false);
-            if ( securityHeader == null ) {
+            if (securityHeader == null) {
                 return true;
             }
         }
         
-        if ( securityHeader.getChildNodes().getLength() == 0 ) {
+        if (securityHeader.getChildNodes().getLength() == 0) {
             return true;
         } else {
             return false;
         }
-
-        
     }
 
     /**
@@ -156,8 +154,13 @@
         securityHeader = WSSecurityUtil.findWsseSecurityHeaderBlock(doc, doc
                 .getDocumentElement(), actor, true);
 
-        String soapPrefix = WSSecurityUtil.getPrefixNS(soapConstants
-                .getEnvelopeURI(), securityHeader);
+        String soapPrefix = 
+            WSSecurityUtil.setNamespace(
+                securityHeader, 
+                soapConstants.getEnvelopeURI(),
+                WSConstants.DEFAULT_SOAP_PREFIX
+            );
+        
         if (actor != null && actor.length() > 0) {
             securityHeader.setAttributeNS(soapConstants.getEnvelopeURI(),
                     soapPrefix
@@ -178,7 +181,7 @@
         if (securityHeader == null) {            
             securityHeader = WSSecurityUtil.findWsseSecurityHeaderBlock(doc, 
doc
                     .getDocumentElement(), actor, false);
-            if ( securityHeader == null ) {
+            if (securityHeader == null) {
                 return;
             }
         }

Modified: 
webservices/wss4j/trunk/src/org/apache/ws/security/util/WSSecurityUtil.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/util/WSSecurityUtil.java?rev=728664&r1=728663&r2=728664&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/org/apache/ws/security/util/WSSecurityUtil.java 
(original)
+++ webservices/wss4j/trunk/src/org/apache/ws/security/util/WSSecurityUtil.java 
Mon Dec 22 04:31:02 2008
@@ -302,12 +302,15 @@
     }
 
     /**
-     * set the namespace if it is not set already. <p/>
+     * Set a namespace/prefix on an element if it is not set already. First 
off, it
+     * searches for the element for the prefix associated with the specified
+     * namespace. If the prefix isn't null, then this is returned. Otherwise, 
it
+     * creates a new attribute using the namespace/prefix passed as parameters.
      * 
      * @param element
      * @param namespace
      * @param prefix
-     * @return TODO
+     * @return the prefix associated with the set namespace
      */
     public static String setNamespace(Element element, String namespace,
             String prefix) {
@@ -315,8 +318,7 @@
         if (pre != null) {
             return pre;
         }
-        element.setAttributeNS(WSConstants.XMLNS_NS, "xmlns:" + prefix,
-                namespace);
+        element.setAttributeNS(WSConstants.XMLNS_NS, "xmlns:" + prefix, 
namespace);
         return prefix;
     }
 

Modified: webservices/wss4j/trunk/test/wssec/PackageTests.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/test/wssec/PackageTests.java?rev=728664&r1=728663&r2=728664&view=diff
==============================================================================
--- webservices/wss4j/trunk/test/wssec/PackageTests.java (original)
+++ webservices/wss4j/trunk/test/wssec/PackageTests.java Mon Dec 22 04:31:02 
2008
@@ -81,6 +81,7 @@
         suite.addTestSuite(TestWSSecurityCertError.class);
         suite.addTestSuite(TestWSSecuritySignatureParts.class);
         suite.addTestSuite(TestWSSecurityUTSignature.class);
+        suite.addTestSuite(TestWSSecurityWSS60.class);
         
         return suite;
     }

Modified: webservices/wss4j/trunk/test/wssec/TestWSSecuritySignatureParts.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/test/wssec/TestWSSecuritySignatureParts.java?rev=728664&r1=728663&r2=728664&view=diff
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecuritySignatureParts.java 
(original)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecuritySignatureParts.java Mon 
Dec 22 04:31:02 2008
@@ -55,7 +55,7 @@
  * has a custom header added.
  */
 public class TestWSSecuritySignatureParts extends TestCase implements 
CallbackHandler {
-    private static Log log = LogFactory.getLog(TestWSSecurityNew8.class);
+    private static Log log = 
LogFactory.getLog(TestWSSecuritySignatureParts.class);
     static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
+
             "<soapenv:Envelope xmlns:foo=\"urn:foo.bar\" 
xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"; 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"; 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>" +
             "   <soapenv:Header>" +

Modified: webservices/wss4j/trunk/test/wssec/TestWSSecurityUTSignature.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/test/wssec/TestWSSecurityUTSignature.java?rev=728664&r1=728663&r2=728664&view=diff
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityUTSignature.java (original)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityUTSignature.java Mon Dec 
22 04:31:02 2008
@@ -60,7 +60,7 @@
  * non-standard implementation.
  */
 public class TestWSSecurityUTSignature extends TestCase implements 
CallbackHandler {
-    private static Log log = LogFactory.getLog(TestWSSecurityUTDK.class);
+    private static Log log = 
LogFactory.getLog(TestWSSecurityUTSignature.class);
     static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
+
             "<soapenv:Envelope 
xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"; 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"; 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>" +
             "   <soapenv:Body>" +

Added: webservices/wss4j/trunk/test/wssec/TestWSSecurityWSS60.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/test/wssec/TestWSSecurityWSS60.java?rev=728664&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/wssec/TestWSSecurityWSS60.java (added)
+++ webservices/wss4j/trunk/test/wssec/TestWSSecurityWSS60.java Mon Dec 22 
04:31:02 2008
@@ -0,0 +1,180 @@
+/*
+ * Copyright  2003-2004 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 wssec;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.axis.Message;
+import org.apache.axis.MessageContext;
+import org.apache.axis.client.AxisClient;
+import org.apache.axis.configuration.NullProvider;
+import org.apache.axis.message.SOAPEnvelope;
+import org.apache.axis.utils.XMLUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ws.security.WSPasswordCallback;
+import org.apache.ws.security.WSSecurityEngine;
+import org.apache.ws.security.WSConstants;
+import org.apache.ws.security.components.crypto.Crypto;
+import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.message.WSSecSignature;
+import org.apache.ws.security.message.WSSecHeader;
+import org.w3c.dom.Document;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+/**
+ * This is a test for WSS-60 - "Problems when SOAP envelope namespace prefix 
is null"
+ * http://issues.apache.org/jira/browse/WSS-60
+ */
+public class TestWSSecurityWSS60 extends TestCase implements CallbackHandler {
+    private static Log log = LogFactory.getLog(TestWSSecurityWSS60.class);
+    static final String soapMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
+
+            "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"; 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"; 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>" +
+            "   <Body>" +
+            "      <ns1:testMethod 
xmlns:ns1=\"http://axis/service/security/test6/LogTestService8\";></ns1:testMethod>"
 +
+            "   </Body>" +
+            "</Envelope>";
+
+    static final WSSecurityEngine secEngine = new WSSecurityEngine();
+    static final Crypto crypto = CryptoFactory.getInstance();
+    MessageContext msgContext;
+    Message message;
+
+    /**
+     * TestWSSecurity constructor
+     * <p/>
+     * 
+     * @param name name of the test
+     */
+    public TestWSSecurityWSS60(String name) {
+        super(name);
+    }
+
+    /**
+     * JUnit suite
+     * <p/>
+     * 
+     * @return a junit test suite
+     */
+    public static Test suite() {
+        return new TestSuite(TestWSSecurityWSS60.class);
+    }
+
+    /**
+     * Main method
+     * <p/>
+     * 
+     * @param args command line args
+     */
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(suite());
+    }
+
+    /**
+     * Setup method
+     * <p/>
+     * 
+     * @throws Exception Thrown when there is a problem in setup
+     */
+    protected void setUp() throws Exception {
+        AxisClient tmpEngine = new AxisClient(new NullProvider());
+        msgContext = new MessageContext(tmpEngine);
+        message = getSOAPMessage();
+    }
+
+    /**
+     * Constructs a soap envelope
+     * <p/>
+     * 
+     * @return soap envelope
+     * @throws Exception if there is any problem constructing the soap envelope
+     */
+    protected Message getSOAPMessage() throws Exception {
+        InputStream in = new ByteArrayInputStream(soapMsg.getBytes());
+        Message msg = new Message(in);
+        msg.setMessageContext(msgContext);
+        return msg;
+    }
+
+    /**
+     * Test signing a SOAP message that has no SOAP namespace prefix
+     */
+    public void testNoSOAPNamespacePrefix() throws Exception {
+        SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
+        WSSecSignature sign = new WSSecSignature();
+        sign.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
+        sign.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
+
+        Document doc = unsignedEnvelope.getAsDocument();
+
+        WSSecHeader secHeader = new WSSecHeader();
+        secHeader.setActor("bob");
+        secHeader.insertSecurityHeader(doc);
+        Document signedDoc = sign.build(doc, crypto, secHeader);
+        
+        Message signedMsg = SOAPUtil.toAxisMessage(signedDoc);
+        if (log.isDebugEnabled()) {
+            
XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(), new 
PrintWriter(System.out));
+        }
+        signedDoc = signedMsg.getSOAPEnvelope().getAsDocument();
+        
+        verify(signedDoc);
+    }
+    
+    /**
+     * Verifies the soap envelope
+     * <p/>
+     * 
+     * @param doc 
+     * @throws Exception Thrown when there is a problem in verification
+     */
+    private void verify(Document doc) throws Exception {
+        secEngine.processSecurityHeader(doc, null, this, crypto);
+        SOAPUtil.updateSOAPMessage(doc, message);
+        if (log.isDebugEnabled()) {
+            log.debug("Verfied and decrypted message:");
+            
XMLUtils.PrettyElementToWriter(message.getSOAPEnvelope().getAsDOM(), new 
PrintWriter(System.out));
+        }
+    }
+
+    public void handle(Callback[] callbacks)
+            throws IOException, UnsupportedCallbackException {
+        for (int i = 0; i < callbacks.length; i++) {
+            if (callbacks[i] instanceof WSPasswordCallback) {
+                WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
+                /*
+                 * here call a function/method to lookup the password for
+                 * the given identifier (e.g. a user name or keystore alias)
+                 * e.g.: 
pc.setPassword(passStore.getPassword(pc.getIdentfifier))
+                 * for Testing we supply a fixed name here.
+                 */
+                pc.setPassword("security");
+            } else {
+                throw new UnsupportedCallbackException(callbacks[i], 
"Unrecognized Callback");
+            }
+        }
+    }
+}

Propchange: webservices/wss4j/trunk/test/wssec/TestWSSecurityWSS60.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/wss4j/trunk/test/wssec/TestWSSecurityWSS60.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to