You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by mu...@apache.org on 2008/10/14 17:12:04 UTC

svn commit: r704560 - in /xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations: Bug45961Test.java MockCanonicalizationMethod.java

Author: mullan
Date: Tue Oct 14 08:12:03 2008
New Revision: 704560

URL: http://svn.apache.org/viewvc?rev=704560&view=rev
Log:
Fixed bug 45961: verify with own canonicalization method

Added:
    xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/Bug45961Test.java
    xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/MockCanonicalizationMethod.java

Added: xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/Bug45961Test.java
URL: http://svn.apache.org/viewvc/xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/Bug45961Test.java?rev=704560&view=auto
==============================================================================
--- xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/Bug45961Test.java (added)
+++ xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/Bug45961Test.java Tue Oct 14 08:12:03 2008
@@ -0,0 +1,92 @@
+package org.apache.xml.security.test.c14n.implementations;
+
+import java.io.FileInputStream;
+
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.security.cert.X509Certificate;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.apache.xml.security.Init;
+import org.apache.xml.security.c14n.Canonicalizer;
+import org.apache.xml.security.signature.ObjectContainer;
+import org.apache.xml.security.signature.XMLSignature;
+import org.apache.xml.security.signature.XMLSignatureException;
+import org.apache.xml.security.utils.Constants;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import junit.framework.TestCase;
+
+public class Bug45961Test extends TestCase {
+
+	private static final String OBJECT_ID = "Object";
+	private static final String MOCK_CANONICALIZATION_METHOD = MockCanonicalizationMethod.MOCK_CANONICALIZATION_METHOD;
+	private static final char[] PASSWORD = "changeit".toCharArray();
+	private static final String ALIAS = "mullan";
+	private DocumentBuilder _builder;
+
+	@Override
+	protected void setUp() throws Exception {
+		Init.init();
+		Canonicalizer.register(MOCK_CANONICALIZATION_METHOD,
+				MockCanonicalizationMethod.class.getName());
+		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+		factory.setNamespaceAware(true);
+		_builder = factory.newDocumentBuilder();
+	}
+
+	public void testBug() throws Exception {
+		Document document = getSignedDocument();
+		try {
+			Element element = (Element) document.getFirstChild();
+			assertEquals(Constants.getSignatureSpecNSprefix() + ":"
+					+ Constants._TAG_SIGNATURE, element.getNodeName());
+			new XMLSignature(element, null);
+		} catch (XMLSignatureException e) {
+			fail(e.getMessage());
+		}
+	}
+
+	private Document getSignedDocument() throws Exception {
+		KeyStore ks = KeyStore.getInstance("JKS");
+		FileInputStream fis = new FileInputStream(getAbsolutePath("data/test.jks"));
+		ks.load(fis, PASSWORD);
+		fis.close();
+		PrivateKey privateKey = (PrivateKey) ks.getKey(ALIAS, PASSWORD);
+		X509Certificate signingCert = (X509Certificate) ks
+				.getCertificate(ALIAS);
+
+		Document document = _builder.newDocument();
+
+		XMLSignature signature = new XMLSignature(document, null,
+				XMLSignature.ALGO_ID_SIGNATURE_DSA,
+				MOCK_CANONICALIZATION_METHOD);
+
+		document.appendChild(signature.getElement());
+
+		Element root = document.createElementNS("", "RootElement");
+		root.appendChild(document.createTextNode("Some simple test\n"));
+		ObjectContainer object = new ObjectContainer(document);
+		object.appendChild(root);
+		object.setId(OBJECT_ID);
+
+		signature.addDocument("#" + OBJECT_ID);
+
+		signature.addKeyInfo(signingCert);
+		signature.sign(privateKey);
+		return document;
+	}
+
+   private String getAbsolutePath(String path)
+   {
+          String basedir = System.getProperty("basedir");
+          if(basedir != null && !"".equals(basedir)) {
+                path = basedir + "/" + path;
+          }
+          return path;
+   }
+}

Added: xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/MockCanonicalizationMethod.java
URL: http://svn.apache.org/viewvc/xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/MockCanonicalizationMethod.java?rev=704560&view=auto
==============================================================================
--- xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/MockCanonicalizationMethod.java (added)
+++ xml/security/trunk/src_unitTests/org/apache/xml/security/test/c14n/implementations/MockCanonicalizationMethod.java Tue Oct 14 08:12:03 2008
@@ -0,0 +1,98 @@
+package org.apache.xml.security.test.c14n.implementations;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Set;
+
+import org.apache.xml.security.c14n.CanonicalizationException;
+import org.apache.xml.security.c14n.CanonicalizerSpi;
+import org.apache.xml.security.utils.UnsyncByteArrayOutputStream;
+import org.apache.xml.security.utils.XMLUtils;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import com.sun.org.apache.xml.internal.serialize.OutputFormat;
+import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
+
+@SuppressWarnings("unchecked")
+public class MockCanonicalizationMethod extends CanonicalizerSpi {
+
+	public static final String MOCK_CANONICALIZATION_METHOD = "mock.canonicalization.method";
+	private OutputStream _os;
+
+	@Override
+	public byte[] engineCanonicalizeSubTree(Node rootNode)
+			throws CanonicalizationException {
+		return canonicalize(rootNode);
+	}
+
+	private byte[] canonicalize(Node rootNode) throws CanonicalizationException {
+		try {
+			XMLSerializer serializer = new XMLSerializer(getOutputStream(),
+					new OutputFormat());
+			serializer.setNamespaces(true);
+			serializer.serialize((Element) rootNode);
+			return toByteArray();
+		} catch (IOException e) {
+			throw new CanonicalizationException(e.getMessage(), e);
+		}
+	}
+
+	private byte[] toByteArray() {
+		OutputStream os = getOutputStream();
+		if (os instanceof ByteArrayOutputStream) {
+			ByteArrayOutputStream os2 = (ByteArrayOutputStream) os;
+			byte[] result = os2.toByteArray();
+			os2.reset();
+			return result;
+		}
+		if (os instanceof UnsyncByteArrayOutputStream) {
+			UnsyncByteArrayOutputStream os2 = (UnsyncByteArrayOutputStream) os;
+			byte[] result = os2.toByteArray();
+			os2.reset();
+			return result;
+		}
+		return null;
+	}
+
+	private OutputStream getOutputStream() {
+		if (_os == null) {
+			_os = new ByteArrayOutputStream();
+		}
+		return _os;
+	}
+
+	@Override
+	public byte[] engineCanonicalizeSubTree(Node rootNode,
+			String inclusiveNamespaces) throws CanonicalizationException {
+		return canonicalize(rootNode);
+	}
+
+	@Override
+	public byte[] engineCanonicalizeXPathNodeSet(Set xpathNodeSet)
+			throws CanonicalizationException {
+		return canonicalize(XMLUtils.getOwnerDocument(xpathNodeSet));
+	}
+
+	@Override
+	public byte[] engineCanonicalizeXPathNodeSet(Set xpathNodeSet,
+			String inclusiveNamespaces) throws CanonicalizationException {
+		return canonicalize(XMLUtils.getOwnerDocument(xpathNodeSet));
+	}
+
+	@Override
+	public boolean engineGetIncludeComments() {
+		return false;
+	}
+
+	@Override
+	public String engineGetURI() {
+		return MOCK_CANONICALIZATION_METHOD;
+	}
+
+	@Override
+	public void setWriter(OutputStream os) {
+		_os = os;
+	}
+}