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 2012/03/16 11:17:57 UTC

svn commit: r1301416 - in /santuario/xml-security-java/branches/1.4.x-fixes: CHANGELOG.txt src/org/apache/xml/security/encryption/XMLCipher.java src_unitTests/org/apache/xml/security/test/encryption/EncryptContentTest.java

Author: coheigea
Date: Fri Mar 16 10:17:56 2012
New Revision: 1301416

URL: http://svn.apache.org/viewvc?rev=1301416&view=rev
Log:
[SANTUARIO-301] - Missing KeyInfo element when encrypting multiple elements

Modified:
    santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt
    santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/encryption/XMLCipher.java
    santuario/xml-security-java/branches/1.4.x-fixes/src_unitTests/org/apache/xml/security/test/encryption/EncryptContentTest.java

Modified: santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt?rev=1301416&r1=1301415&r2=1301416&view=diff
==============================================================================
--- santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt (original)
+++ santuario/xml-security-java/branches/1.4.x-fixes/CHANGELOG.txt Fri Mar 16 10:17:56 2012
@@ -1,6 +1,7 @@
 Changelog for "Apache xml-security" <http://santuario.apache.org/>
 
 New in v.1.4.7-SNAPSHOT:
+    Fixed SANTUARIO-301 - Missing KeyInfo element when encrypting multiple elements.
     Fixed SANTUARIO-299: StringIndexOutOfBoundsException is thrown during reference verification (if URI = "#")
     Fixed SANTUARIO-295: XMLDSig XPathFilter2Transform bug involving intersect filter
     Fixed SANTUARIO-286: Test case Santuario273.testC14n11Base() tests for a side-effect of canonicalization, rather than the desired effect.

Modified: santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/encryption/XMLCipher.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/encryption/XMLCipher.java?rev=1301416&r1=1301415&r2=1301416&view=diff
==============================================================================
--- santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/encryption/XMLCipher.java (original)
+++ santuario/xml-security-java/branches/1.4.x-fixes/src/org/apache/xml/security/encryption/XMLCipher.java Fri Mar 16 10:17:56 2012
@@ -3048,7 +3048,7 @@ public class XMLCipher {
                         super.getEncryptionMethod()).toElement());
                 }
                 if (null != super.getKeyInfo()) {
-                    result.appendChild(super.getKeyInfo().getElement());
+                    result.appendChild(super.getKeyInfo().getElement().cloneNode(true));
                 }
 
                 result.appendChild(
@@ -3185,7 +3185,7 @@ public class XMLCipher {
                         super.getEncryptionMethod()).toElement());
                 }
                 if (null != super.getKeyInfo()) {
-                    result.appendChild(super.getKeyInfo().getElement());
+                    result.appendChild(super.getKeyInfo().getElement().cloneNode(true));
                 }
                 result.appendChild(
                     ((CipherDataImpl) super.getCipherData()).toElement());

Modified: santuario/xml-security-java/branches/1.4.x-fixes/src_unitTests/org/apache/xml/security/test/encryption/EncryptContentTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/branches/1.4.x-fixes/src_unitTests/org/apache/xml/security/test/encryption/EncryptContentTest.java?rev=1301416&r1=1301415&r2=1301416&view=diff
==============================================================================
--- santuario/xml-security-java/branches/1.4.x-fixes/src_unitTests/org/apache/xml/security/test/encryption/EncryptContentTest.java (original)
+++ santuario/xml-security-java/branches/1.4.x-fixes/src_unitTests/org/apache/xml/security/test/encryption/EncryptContentTest.java Fri Mar 16 10:17:56 2012
@@ -33,7 +33,10 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.encryption.EncryptedData;
 import org.apache.xml.security.encryption.XMLCipher;
+import org.apache.xml.security.keys.KeyInfo;
+import org.apache.xml.security.utils.Constants;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -54,6 +57,18 @@ public class EncryptContentTest extends 
         "    <serial>Y10</serial>\n" +
 	"  </user>\n" +
 	"</users>\n";
+    
+    private static final String MULTIPLE_USER_DATA =
+            "<users>\n" +
+            "  <user>\n" +
+            "    <firstname>Bugs</firstname>\n" +
+            "    <lastname>Bunny</lastname>\n" +
+            "  </user>\n" +
+            "  <user>\n" +
+            "    <firstname>Daffy</firstname>\n" +
+            "    <lastname>Duck</lastname>\n" +
+            "  </user>\n" +
+            "</users>\n";
 
     private DocumentBuilder db;
     private SecretKey secretKey;
@@ -141,4 +156,34 @@ public class EncryptContentTest extends 
 
 	// t.transform(new DOMSource(doc), new StreamResult(System.out));
     }
+    
+    /**
+     * See SANTUARIO-301:
+     * https://issues.apache.org/jira/browse/SANTUARIO-301
+     */
+    public void testMultipleKeyInfoElements() throws Exception {
+        if (!haveISOPadding) {
+            log.warn("Test testMultipleKeyInfoElements skipped as necessary algorithms not available");
+            return;
+        }
+
+        Document doc = db.parse(new ByteArrayInputStream(MULTIPLE_USER_DATA.getBytes("UTF8")));
+        NodeList dataToEncrypt = doc.getElementsByTagName("user");
+
+        XMLCipher dataCipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
+        dataCipher.init(XMLCipher.ENCRYPT_MODE, secretKey);
+
+        KeyInfo keyInfo = new KeyInfo(doc);
+        keyInfo.addKeyName("mykey");
+
+        EncryptedData encryptedData = dataCipher.getEncryptedData();
+        encryptedData.setKeyInfo(keyInfo);
+        
+        for (int i = 0; i < dataToEncrypt.getLength(); i++) {
+            dataCipher.doFinal(doc,(Element) dataToEncrypt.item(i), true);
+        }
+
+        NodeList keyInfoList = doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "KeyInfo");
+        assertEquals(keyInfoList.getLength(), 2);
+    }
 }