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 2011/01/27 13:19:06 UTC
svn commit: r1064102 -
/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/
Author: coheigea
Date: Thu Jan 27 12:19:05 2011
New Revision: 1064102
URL: http://svn.apache.org/viewvc?rev=1064102&view=rev
Log:
[SANTUARIO-257] - Finished with sample indentation.
Modified:
santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/HereSigner.java
santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/NullURIReferenceResolver.java
santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/SignaturePropertiesSample.java
santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/TestSignature.java
santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyCollectableSignature.java
santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesFifteen.java
santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesSixteen.java
santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesTwentyThree.java
santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifySignature.java
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/HereSigner.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/HereSigner.java?rev=1064102&r1=1064101&r2=1064102&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/HereSigner.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/HereSigner.java Thu Jan 27 12:19:05 2011
@@ -16,8 +16,6 @@
*/
package org.apache.xml.security.samples.signature;
-
-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -35,10 +33,7 @@ import org.apache.xml.security.utils.Hel
import org.apache.xml.security.utils.XMLUtils;
import org.w3c.dom.Element;
-
/**
- *
- *
* @author $Author$
*/
public class HereSigner {
@@ -47,108 +42,104 @@ public class HereSigner {
static org.apache.commons.logging.Log log =
org.apache.commons.logging.LogFactory.getLog(HereSigner.class.getName());
- /**
- * Method main
- *
- * @param unused
- * @throws Exception
- */
- public static void main(String unused[]) throws Exception {
- //J-
- String keystoreType = "JKS";
- String keystoreFile = "data/org/apache/xml/security/samples/input/keystore.jks";
- String keystorePass = "xmlsecurity";
- String privateKeyAlias = "test";
- String privateKeyPass = "xmlsecurity";
- String certificateAlias = "test";
- File signatureFile = new File("hereSignature.xml");
- //J+
- KeyStore ks = KeyStore.getInstance(keystoreType);
- FileInputStream fis = new FileInputStream(keystoreFile);
-
- ks.load(fis, keystorePass.toCharArray());
-
- PrivateKey privateKey = (PrivateKey) ks.getKey(privateKeyAlias,
- privateKeyPass.toCharArray());
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- dbf.setNamespaceAware(true);
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
- org.w3c.dom.Document doc = db.newDocument();
-
-
- String BaseURI = signatureFile.toURL().toString();
- Constants.setSignatureSpecNSprefix("prof");
- XMLSignature sig = new XMLSignature(doc, BaseURI,
- XMLSignature.ALGO_ID_SIGNATURE_DSA);
-
- doc.appendChild(sig.getElement());
- sig.getSignedInfo()
- .addResourceResolver(new org.apache.xml.security.samples.utils.resolver
- .OfflineResolver());
-
- {
- ObjectContainer ob1 = new ObjectContainer(doc);
- ob1.setId("object-1");
- ob1.appendChild(doc.createTextNode("\nSigned Text\n"));
- Element c = doc.createElementNS(null, "element");
- c.setAttributeNS(null, "name", "val");
- ob1.appendChild(c);
- sig.appendObject(ob1);
-
- Transforms transforms = new Transforms(doc);
- XPathContainer xc = new XPathContainer(doc);
- xc.setXPathNamespaceContext("prof", Constants.SignatureSpecNS);
-
- //J-
- String xpath = "\n"
- + "count(" + "\n"
- + " ancestor-or-self::prof:Object " + "\n"
- + " | " + "\n"
- + " here()/ancestor::prof:Signature[1]/child::prof:Object[@Id='object-1']" + "\n"
- + ") <= count(" + "\n"
- + " ancestor-or-self::prof:Object" + "\n"
- + ") " + "\n";
- //J+
-
- xc.setXPath(xpath);
- HelperNodeList nl = new HelperNodeList();
- nl.appendChild(doc.createTextNode("\n"));
- nl.appendChild(xc.getElement());
- nl.appendChild(doc.createTextNode("\n"));
-
- transforms.addTransform(Transforms.TRANSFORM_XPATH, nl);
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- X509Certificate cert =
- (X509Certificate) ks.getCertificate(certificateAlias);
-
- sig.addKeyInfo(cert);
- sig.addKeyInfo(cert.getPublicKey());
- System.out.println("Start signing");
- sig.sign(privateKey);
- System.out.println("Finished signing");
- }
-
- SignedInfo s = sig.getSignedInfo();
- for (int i=0; i<s.getSignedContentLength(); i++) {
- System.out.println(new String(s.getSignedContentItem(i)));
- }
-
- FileOutputStream f = new FileOutputStream(signatureFile);
-
- XMLUtils.outputDOMc14nWithComments(doc, f);
-
- f.close();
- System.out.println("Wrote signature to " + BaseURI);
- }
-
- static {
- org.apache.xml.security.Init.init();
- }
+ static {
+ org.apache.xml.security.Init.init();
+ }
+
+ /**
+ * Method main
+ *
+ * @param unused
+ * @throws Exception
+ */
+ public static void main(String unused[]) throws Exception {
+ String keystoreType = "JKS";
+ String keystoreFile = "samples/data/keystore.jks";
+ String keystorePass = "xmlsecurity";
+ String privateKeyAlias = "test";
+ String privateKeyPass = "xmlsecurity";
+ String certificateAlias = "test";
+ File signatureFile = new File("hereSignature.xml");
+
+ KeyStore ks = KeyStore.getInstance(keystoreType);
+ FileInputStream fis = new FileInputStream(keystoreFile);
+
+ ks.load(fis, keystorePass.toCharArray());
+
+ PrivateKey privateKey =
+ (PrivateKey) ks.getKey(privateKeyAlias, privateKeyPass.toCharArray());
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ dbf.setNamespaceAware(true);
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+ org.w3c.dom.Document doc = db.newDocument();
+
+ String BaseURI = signatureFile.toURI().toURL().toString();
+ Constants.setSignatureSpecNSprefix("prof");
+ XMLSignature sig =
+ new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_SIGNATURE_DSA);
+
+ doc.appendChild(sig.getElement());
+ sig.getSignedInfo()
+ .addResourceResolver(new org.apache.xml.security.samples.utils.resolver.OfflineResolver());
+
+ {
+ ObjectContainer ob1 = new ObjectContainer(doc);
+ ob1.setId("object-1");
+ ob1.appendChild(doc.createTextNode("\nSigned Text\n"));
+ Element c = doc.createElementNS(null, "element");
+ c.setAttributeNS(null, "name", "val");
+ ob1.appendChild(c);
+ sig.appendObject(ob1);
+
+ Transforms transforms = new Transforms(doc);
+ XPathContainer xc = new XPathContainer(doc);
+ xc.setXPathNamespaceContext("prof", Constants.SignatureSpecNS);
+
+ String xpath = "\n"
+ + "count(" + "\n"
+ + " ancestor-or-self::prof:Object " + "\n"
+ + " | " + "\n"
+ + " here()/ancestor::prof:Signature[1]/child::prof:Object[@Id='object-1']" + "\n"
+ + ") <= count(" + "\n"
+ + " ancestor-or-self::prof:Object" + "\n"
+ + ") " + "\n";
+
+ xc.setXPath(xpath);
+ HelperNodeList nl = new HelperNodeList();
+ nl.appendChild(doc.createTextNode("\n"));
+ nl.appendChild(xc.getElement());
+ nl.appendChild(doc.createTextNode("\n"));
+
+ transforms.addTransform(Transforms.TRANSFORM_XPATH, nl);
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ X509Certificate cert =
+ (X509Certificate) ks.getCertificate(certificateAlias);
+
+ sig.addKeyInfo(cert);
+ sig.addKeyInfo(cert.getPublicKey());
+ System.out.println("Start signing");
+ sig.sign(privateKey);
+ System.out.println("Finished signing");
+ }
+
+ SignedInfo s = sig.getSignedInfo();
+ for (int i = 0; i < s.getSignedContentLength(); i++) {
+ System.out.println(new String(s.getSignedContentItem(i)));
+ }
+
+ FileOutputStream f = new FileOutputStream(signatureFile);
+
+ XMLUtils.outputDOMc14nWithComments(doc, f);
+
+ f.close();
+ System.out.println("Wrote signature to " + BaseURI);
+ }
+
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/NullURIReferenceResolver.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/NullURIReferenceResolver.java?rev=1064102&r1=1064101&r2=1064102&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/NullURIReferenceResolver.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/NullURIReferenceResolver.java Thu Jan 27 12:19:05 2011
@@ -16,14 +16,11 @@
*/
package org.apache.xml.security.samples.signature;
-
-
import org.apache.xml.security.signature.XMLSignatureInput;
import org.apache.xml.security.utils.resolver.ResourceResolverException;
import org.apache.xml.security.utils.resolver.ResourceResolverSpi;
import org.w3c.dom.Attr;
-
/**
* This is a sample ResourceResolver who demonstrated how References without
* URI attribuet could be handled.
@@ -32,111 +29,110 @@ import org.w3c.dom.Attr;
*/
public class NullURIReferenceResolver extends ResourceResolverSpi {
- /** {@link org.apache.commons.logging} logging facility */
+ /** {@link org.apache.commons.logging} logging facility */
static org.apache.commons.logging.Log log =
- org.apache.commons.logging.LogFactory.getLog(
- NullURIReferenceResolver.class.getName());
+ org.apache.commons.logging.LogFactory.getLog(NullURIReferenceResolver.class.getName());
- /** Field _data[] */
- byte _data[] = null;
+ /** Field _data[] */
+ byte _data[] = null;
- /** Field _data2[][] */
- byte _data2[][] = null;
+ /** Field _data2[][] */
+ byte _data2[][] = null;
- /** Field _count */
- int _count = -1;
-
- /**
- * Constructor NullURIReferenceResolver
- *
- * @param data
- */
- public NullURIReferenceResolver(byte[] data) {
- _data = data;
- _count = -1;
- }
-
- /**
- * Constructor NullURIReferenceResolver
- *
- * @param data
- */
- public NullURIReferenceResolver(byte[][] data) {
- _data2 = data;
- _count = 0;
- }
-
- /**
- * Method engineResolve
- *
- * @param uri
- * @param BaseURI
- *
- * @throws ResourceResolverException
- */
- public XMLSignatureInput engineResolve(Attr uri, String BaseURI)
- throws ResourceResolverException {
-
- XMLSignatureInput result = null;
-
- if ((this._data != null) && (this._count == -1)) {
-
- // we always return the same stuff;
- result = new XMLSignatureInput(this._data);
-
- result.setSourceURI("memory://null");
- result.setMIMEType("text/txt");
- } else if ((this._data == null) && (this._count != -1)) {
- if (this._count < this._data2.length) {
- result = new XMLSignatureInput(this._data2[this._count]);
+ /** Field _count */
+ int _count = -1;
+
+ /**
+ * Constructor NullURIReferenceResolver
+ *
+ * @param data
+ */
+ public NullURIReferenceResolver(byte[] data) {
+ _data = data;
+ _count = -1;
+ }
+
+ /**
+ * Constructor NullURIReferenceResolver
+ *
+ * @param data
+ */
+ public NullURIReferenceResolver(byte[][] data) {
+ _data2 = data;
+ _count = 0;
+ }
+
+ /**
+ * Method engineResolve
+ *
+ * @param uri
+ * @param BaseURI
+ *
+ * @throws ResourceResolverException
+ */
+ public XMLSignatureInput engineResolve(Attr uri, String BaseURI)
+ throws ResourceResolverException {
- result.setSourceURI("memory://" + this._count);
+ XMLSignatureInput result = null;
- this._count++;
+ if ((this._data != null) && (this._count == -1)) {
+ // we always return the same stuff;
+ result = new XMLSignatureInput(this._data);
+
+ result.setSourceURI("memory://null");
result.setMIMEType("text/txt");
- } else {
- String errMsg = "You did not supply enough data!!! There are only "
- + (this._data2.length) + " byte[] arrays";
- Object exArgs[] = { errMsg };
+ } else if ((this._data == null) && (this._count != -1)) {
+ if (this._count < this._data2.length) {
+ result = new XMLSignatureInput(this._data2[this._count]);
+
+ result.setSourceURI("memory://" + this._count);
+
+ this._count++;
+
+ result.setMIMEType("text/txt");
+ } else {
+ String errMsg = "You did not supply enough data!!! There are only "
+ + (this._data2.length) + " byte[] arrays";
+ Object exArgs[] = { errMsg };
+
+ throw new ResourceResolverException("empty", exArgs, uri, BaseURI);
+ }
+ } else {
+ Object exArgs[] = { "You did not supply data !!!" };
throw new ResourceResolverException("empty", exArgs, uri, BaseURI);
- }
- } else {
- Object exArgs[] = { "You did not supply data !!!" };
-
- throw new ResourceResolverException("empty", exArgs, uri, BaseURI);
- }
-
- return result;
- }
-
- /**
- * Method engineCanResolve
- *
- * @param uri
- * @param BaseURI
- *
- */
- public boolean engineCanResolve(Attr uri, String BaseURI) {
-
- if (uri == null) {
- if ((this._data != null) && (this._count == -1)) {
- return true;
- } else if ((this._data == null) && (this._count != -1)) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Method engineGetPropertyKeys
- *
- *
- */
- public String[] engineGetPropertyKeys() {
- return null;
- }
+ }
+
+ return result;
+ }
+
+ /**
+ * Method engineCanResolve
+ *
+ * @param uri
+ * @param BaseURI
+ *
+ */
+ public boolean engineCanResolve(Attr uri, String BaseURI) {
+
+ if (uri == null) {
+ if ((this._data != null) && (this._count == -1)) {
+ return true;
+ } else if ((this._data == null) && (this._count != -1)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Method engineGetPropertyKeys
+ *
+ *
+ */
+ public String[] engineGetPropertyKeys() {
+ return null;
+ }
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/SignaturePropertiesSample.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/SignaturePropertiesSample.java?rev=1064102&r1=1064101&r2=1064102&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/SignaturePropertiesSample.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/SignaturePropertiesSample.java Thu Jan 27 12:19:05 2011
@@ -17,7 +17,6 @@
package org.apache.xml.security.samples.signature;
-
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -28,71 +27,65 @@ import org.apache.xml.security.signature
import org.apache.xml.security.signature.XMLSignature;
import org.w3c.dom.Document;
-
/**
* Class SignaturePropertiesSample
*
* @author $Author$
- * @version $Revision$
*/
public class SignaturePropertiesSample {
- /**
- * Method main
- *
- * @param args
- * @throws Exception
- */
- public static void main(String args[]) throws Exception {
+ /**
+ * Method main
+ *
+ * @param args
+ * @throws Exception
+ */
+ public static void main(String args[]) throws Exception {
- org.apache.xml.security.Init.init();
+ org.apache.xml.security.Init.init();
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
+ dbf.setNamespaceAware(true);
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.newDocument();
- XMLSignature sig = new XMLSignature(doc, null,
- XMLSignature.ALGO_ID_MAC_HMAC_SHA1);
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ Document doc = db.newDocument();
+ XMLSignature sig = new XMLSignature(doc, null, XMLSignature.ALGO_ID_MAC_HMAC_SHA1);
- doc.appendChild(sig.getElement());
+ doc.appendChild(sig.getElement());
- SignatureProperty prop1 = new SignatureProperty(doc,
- "http://www.xmlsecurity.org/#target",
- "prop1");
+ SignatureProperty prop1 =
+ new SignatureProperty(doc, "http://www.xmlsecurity.org/#target", "prop1");
- prop1.getElement()
- .appendChild(doc.createTextNode("\n some data for this property\n"));
+ prop1.getElement().appendChild(doc.createTextNode("\n some data for this property\n"));
- SignatureProperties props = new SignatureProperties(doc);
+ SignatureProperties props = new SignatureProperties(doc);
- props.addSignatureProperty(prop1);
+ props.addSignatureProperty(prop1);
- ObjectContainer object = new ObjectContainer(doc);
+ ObjectContainer object = new ObjectContainer(doc);
- object.appendChild(doc.createTextNode("\n"));
- object.appendChild(props.getElement());
- object.appendChild(doc.createTextNode("\n"));
- sig.appendObject(object);
- sig.addDocument("#prop1");
+ object.appendChild(doc.createTextNode("\n"));
+ object.appendChild(props.getElement());
+ object.appendChild(doc.createTextNode("\n"));
+ sig.appendObject(object);
+ sig.addDocument("#prop1");
- String secretKey = "secret";
+ String secretKey = "secret";
- sig.getKeyInfo().addKeyName("The UTF-8 octets of \"" + secretKey
- + "\" are used for signing ("
- + secretKey.length() + " octets)");
- sig.sign(sig.createSecretKey(secretKey.getBytes()));
+ sig.getKeyInfo().addKeyName("The UTF-8 octets of \"" + secretKey
+ + "\" are used for signing ("
+ + secretKey.length() + " octets)");
+ sig.sign(sig.createSecretKey(secretKey.getBytes()));
- Canonicalizer c14n =
- Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
+ Canonicalizer c14n =
+ Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
- System.out.println("---------------------------------------");
- System.out.println(new String(c14n.canonicalizeSubtree(doc)));
- System.out.println("---------------------------------------");
- System.out
- .println(new String(sig.getSignedInfo().item(0).getTransformsOutput()
- .getBytes()));
- System.out.println("---------------------------------------");
- }
+ System.out.println("---------------------------------------");
+ System.out.println(new String(c14n.canonicalizeSubtree(doc)));
+ System.out.println("---------------------------------------");
+ System.out.println(new String(sig.getSignedInfo().item(0).getTransformsOutput().getBytes()));
+ System.out.println("---------------------------------------");
+ }
+
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/TestSignature.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/TestSignature.java?rev=1064102&r1=1064101&r2=1064102&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/TestSignature.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/TestSignature.java Thu Jan 27 12:19:05 2011
@@ -38,124 +38,110 @@ import org.w3c.dom.Element;
* Class TestSignature
*
* @author $Author$
- * @version $Revision$
*/
public class TestSignature {
- /**
- * Method main
- *
- * @param unused
- */
- public static void main(String unused[]) {
-
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- dbf.setNamespaceAware(true);
-
- //J-
- String merlinsDir =
- "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-eighteen/";
- String ourDir =
- "samples/data/org/apache/xml/security/temp/key/";
- String filenames[] = { merlinsDir +
- /* 0 */ "signature-keyname.xml",
- merlinsDir +
- /* 1 */ "signature-retrievalmethod-rawx509crt.xml",
- merlinsDir +
- /* 2 */ "signature-x509-crt-crl.xml",
- merlinsDir +
- /* 3 */ "signature-x509-crt.xml",
- merlinsDir +
- /* 4 */ "signature-x509-is.xml",
- merlinsDir +
- /* 5 */ "signature-x509-ski.xml",
- merlinsDir +
- /* 6 */ "signature-x509-sn.xml",
- ourDir +
- /* 7 */ "signature-retrievalmethod-x509data.xml"
- };
- //J+
- int start = 0;
- int end = filenames.length;
-
- // int end = filenames.length;
- for (int file_to_verify = start; file_to_verify < end; file_to_verify++) {
- try {
- String filename = filenames[file_to_verify];
- File f = new File(filename);
-
- System.out.println("");
- System.out.println(
- "#########################################################");
- System.out.println("Try to verify " + f.toURL().toString());
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
- org.w3c.dom.Document doc =
- db.parse(new java.io.FileInputStream(filename));
-
- XPathFactory xpf = XPathFactory.newInstance();
- XPath xpath = xpf.newXPath();
- xpath.setNamespaceContext(new DSNamespaceContext());
-
- String expression = "//ds:Signature[1]";
- Element sigElement =
- (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
-
- //Creates a XMLSignature from the element and uses the filename as
- //the baseURI. That URI is prepended to all relative URIs.
- XMLSignature signature =
- new XMLSignature(sigElement,
- (new File(filename)).toURL().toString());
-
- signature.addResourceResolver(new OfflineResolver());
-
- //Get the KeyInfo object, which might contain some clues as to what
- //key was used to create the signature. It might also contain the
- //full cert.
- KeyInfo ki = signature.getKeyInfo();
-
- ki.addStorageResolver(new StorageResolver(new org.apache.xml
- .security.keys.storage.implementations
- .CertsInFilesystemDirectoryResolver(merlinsDir + "certs")));
-
- if (ki != null) {
-
- //First try to see if it is an X509Cert
- X509Certificate cert =
- signature.getKeyInfo().getX509Certificate();
-
- if (cert != null) {
-
- //check if the signature is valid using the cert
- System.out.println("Check: "
- + signature.checkSignatureValue(cert));
- } else {
-
- //Maybe it's a public key
- PublicKey pk = signature.getKeyInfo().getPublicKey();
-
- if (pk != null) {
-
- //check if the signature is valid using the public key
- System.out.println("Check: "
- + signature.checkSignatureValue(pk));
- } else {
-
- //No X509Cert or PublicKey could be found.
- System.out
- .println("Could not find Certificate or PublicKey");
- }
- }
- } else {
-
- //If the signature did not contain any KeyInfo element
- System.out.println("Could not find ds:KeyInfo");
+ /**
+ * Method main
+ *
+ * @param unused
+ */
+ public static void main(String unused[]) {
+
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ dbf.setNamespaceAware(true);
+
+ String merlinsDir =
+ "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-eighteen/";
+ String ourDir =
+ "samples/data/org/apache/xml/security/temp/key/";
+ String filenames[] = { merlinsDir +
+ /* 0 */ "signature-keyname.xml",
+ merlinsDir +
+ /* 1 */ "signature-retrievalmethod-rawx509crt.xml",
+ merlinsDir +
+ /* 2 */ "signature-x509-crt-crl.xml",
+ merlinsDir +
+ /* 3 */ "signature-x509-crt.xml",
+ merlinsDir +
+ /* 4 */ "signature-x509-is.xml",
+ merlinsDir +
+ /* 5 */ "signature-x509-ski.xml",
+ merlinsDir +
+ /* 6 */ "signature-x509-sn.xml",
+ ourDir +
+ /* 7 */ "signature-retrievalmethod-x509data.xml"
+ };
+ int start = 0;
+ int end = filenames.length;
+
+ // int end = filenames.length;
+ for (int file_to_verify = start; file_to_verify < end; file_to_verify++) {
+ try {
+ String filename = filenames[file_to_verify];
+ File f = new File(filename);
+
+ System.out.println("");
+ System.out.println("#########################################################");
+ System.out.println("Try to verify " + f.toURL().toString());
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+ org.w3c.dom.Document doc =
+ db.parse(new java.io.FileInputStream(filename));
+
+ XPathFactory xpf = XPathFactory.newInstance();
+ XPath xpath = xpf.newXPath();
+ xpath.setNamespaceContext(new DSNamespaceContext());
+
+ String expression = "//ds:Signature[1]";
+ Element sigElement =
+ (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
+
+ //Creates a XMLSignature from the element and uses the filename as
+ //the baseURI. That URI is prepended to all relative URIs.
+ XMLSignature signature =
+ new XMLSignature(sigElement, (new File(filename)).toURI().toURL().toString());
+
+ signature.addResourceResolver(new OfflineResolver());
+
+ //Get the KeyInfo object, which might contain some clues as to what
+ //key was used to create the signature. It might also contain the
+ //full cert.
+ KeyInfo ki = signature.getKeyInfo();
+
+ ki.addStorageResolver(
+ new StorageResolver(
+ new org.apache.xml.security.keys.storage.implementations
+ .CertsInFilesystemDirectoryResolver(merlinsDir + "certs")));
+
+ if (ki != null) {
+ //First try to see if it is an X509Cert
+ X509Certificate cert =
+ signature.getKeyInfo().getX509Certificate();
+
+ if (cert != null) {
+ //check if the signature is valid using the cert
+ System.out.println("Check: " + signature.checkSignatureValue(cert));
+ } else {
+ //Maybe it's a public key
+ PublicKey pk = signature.getKeyInfo().getPublicKey();
+ if (pk != null) {
+ //check if the signature is valid using the public key
+ System.out.println("Check: " + signature.checkSignatureValue(pk));
+ } else {
+ //No X509Cert or PublicKey could be found.
+ System.out.println("Could not find Certificate or PublicKey");
+ }
+ }
+ } else {
+ //If the signature did not contain any KeyInfo element
+ System.out.println("Could not find ds:KeyInfo");
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
+ }
+ }
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyCollectableSignature.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyCollectableSignature.java?rev=1064102&r1=1064101&r2=1064102&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyCollectableSignature.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyCollectableSignature.java Thu Jan 27 12:19:05 2011
@@ -17,7 +17,6 @@
package org.apache.xml.security.samples.signature;
-
import java.io.File;
import javax.xml.xpath.XPath;
@@ -33,83 +32,81 @@ import org.apache.xml.security.utils.XML
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
-
/**
*
* @author $Author$
*/
public class VerifyCollectableSignature {
- /** {@link org.apache.commons.logging} logging facility */
+ /** {@link org.apache.commons.logging} logging facility */
static org.apache.commons.logging.Log log =
org.apache.commons.logging.LogFactory.getLog(VerifyCollectableSignature.class.getName());
- /**
- * Method main
- *
- * @param unused
- * @throws Exception
- */
- public static void main(String unused[]) throws Exception {
-
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- dbf.setNamespaceAware(true);
- dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
-
- try {
- File signatureFile = new File("collectableSignature.xml");
- String BaseURI = signatureFile.toURL().toString();
-
- System.out.println("Try to verify "
- + signatureFile.toURL().toString());
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
-
- db.setErrorHandler(new org.apache.xml.security.utils
- .IgnoreAllErrorHandler());
-
- org.w3c.dom.Document doc =
- db.parse(new java.io.FileInputStream(signatureFile));
-
- XPathFactory xpf = XPathFactory.newInstance();
- XPath xpath = xpf.newXPath();
- xpath.setNamespaceContext(new DSNamespaceContext());
-
- String expression = "//ds:Signature[1]";
- NodeList signatureElems =
- (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
-
- for (int i = 0; i < signatureElems.getLength(); i++) {
- Element sigElement = (Element) signatureElems.item(i);
- XMLSignature signature = new XMLSignature(sigElement, BaseURI);
- byte[] secretKey = "secretValue".getBytes();
-
- System.out
- .println("The XML signature number " + i + " in file " + BaseURI + " is "
- + (signature
- .checkSignatureValue(signature
- .createSecretKey(CreateCollectableSignature
- .passphrase.getBytes()))
- ? "valid (good)"
- : "invalid !!!!! (bad)"));
-
- SignedInfo s = signature.getSignedInfo();
-
- for (int j = 0; j < s.getSignedContentLength(); j++) {
- System.out.println("################ Signed Resource " + i + "/" + j
- + " ################");
- System.out.println(new String(s.getSignedContentItem(j)));
- System.out.println();
+ static {
+ org.apache.xml.security.Init.init();
+ }
+
+ /**
+ * Method main
+ *
+ * @param unused
+ * @throws Exception
+ */
+ public static void main(String unused[]) throws Exception {
+
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ dbf.setNamespaceAware(true);
+ dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
+
+ try {
+ File signatureFile = new File("collectableSignature.xml");
+ String BaseURI = signatureFile.toURI().toURL().toString();
+
+ System.out.println("Try to verify "
+ + signatureFile.toURI().toURL().toString());
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+
+ db.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
+
+ org.w3c.dom.Document doc =
+ db.parse(new java.io.FileInputStream(signatureFile));
+
+ XPathFactory xpf = XPathFactory.newInstance();
+ XPath xpath = xpf.newXPath();
+ xpath.setNamespaceContext(new DSNamespaceContext());
+
+ String expression = "//ds:Signature[1]";
+ NodeList signatureElems =
+ (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
+
+ for (int i = 0; i < signatureElems.getLength(); i++) {
+ Element sigElement = (Element) signatureElems.item(i);
+ XMLSignature signature = new XMLSignature(sigElement, BaseURI);
+ byte[] secretKey = "secretValue".getBytes();
+
+ System.out.println(
+ "The XML signature number " + i + " in file " + BaseURI + " is "
+ + (signature.checkSignatureValue(
+ signature.createSecretKey(
+ CreateCollectableSignature.passphrase.getBytes()
+ )
+ ) ? "valid (good)" : "invalid !!!!! (bad)"));
+
+ SignedInfo s = signature.getSignedInfo();
+
+ for (int j = 0; j < s.getSignedContentLength(); j++) {
+ System.out.println("################ Signed Resource " + i + "/" + j
+ + " ################");
+ System.out.println(new String(s.getSignedContentItem(j)));
+ System.out.println();
+ }
}
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- static {
- org.apache.xml.security.Init.init();
- }
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesFifteen.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesFifteen.java?rev=1064102&r1=1064101&r2=1064102&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesFifteen.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesFifteen.java Thu Jan 27 12:19:05 2011
@@ -17,7 +17,6 @@
package org.apache.xml.security.samples.signature;
-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -34,160 +33,152 @@ import org.apache.xml.security.utils.Con
import org.apache.xml.security.utils.XMLUtils;
import org.w3c.dom.Element;
-
/**
*
* @author $Author$
*/
public class VerifyMerlinsExamplesFifteen {
- /** {@link org.apache.commons.logging} logging facility */
+ /** {@link org.apache.commons.logging} logging facility */
static org.apache.commons.logging.Log log =
- org.apache.commons.logging.LogFactory.getLog(
- VerifyMerlinsExamplesFifteen.class.getName());
+ org.apache.commons.logging.LogFactory.getLog(VerifyMerlinsExamplesFifteen.class.getName());
- /** Field schemaValidate */
- static final boolean schemaValidate = false;
+ /** Field schemaValidate */
+ static final boolean schemaValidate = false;
+
+ /** Field signatureSchemaFile */
+ static final String signatureSchemaFile = "samples/data/xmldsig-core-schema.xsd";
+
+ static {
+ org.apache.xml.security.Init.init();
+ }
+
+ /**
+ * Method main
+ *
+ * @param unused
+ */
+ public static void main(String unused[]) {
+
+ if (schemaValidate) {
+ System.out.println("We do schema-validation");
+ } else {
+ System.out.println("We do not schema-validation");
+ }
+
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ if (VerifyMerlinsExamplesSixteen.schemaValidate) {
+ dbf.setAttribute("http://apache.org/xml/features/validation/schema",
+ Boolean.TRUE);
+ dbf.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion",
+ Boolean.TRUE);
+ dbf.setValidating(true);
+ dbf.setAttribute("http://xml.org/sax/features/validation",
+ Boolean.TRUE);
+ dbf.setAttribute("http://apache.org/xml/properties/schema/external-schemaLocation",
+ Constants.SignatureSpecNS + " "
+ + VerifyMerlinsExamplesSixteen.signatureSchemaFile);
+ }
+
+ dbf.setNamespaceAware(true);
+ dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
+
+ String merlinsDir =
+ "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-fifteen/";
+ String filenames[] = { merlinsDir + "signature-enveloping-hmac-sha1.xml",
+ merlinsDir + "signature-enveloping-hmac-sha1-40.xml",
+ merlinsDir + "signature-enveloped-dsa.xml",
+ merlinsDir + "signature-enveloping-b64-dsa.xml",
+ merlinsDir + "signature-enveloping-dsa.xml",
+ merlinsDir + "signature-enveloping-rsa.xml",
+ merlinsDir + "signature-external-b64-dsa.xml",
+ merlinsDir + "signature-external-dsa.xml"
+ };
+ int start = 0;
+ // int end = filenames.length;
+ int end = 2;
+
+ for (int i = start; i < 2; i++) {
+ String signatureFileName = filenames[i];
+
+ try {
+ verifyHMAC(dbf, signatureFileName);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
- /** Field signatureSchemaFile */
- static final String signatureSchemaFile = "samples/data/xmldsig-core-schema.xsd";
+ for (int i = 2; i < filenames.length; i++) {
+ String signatureFileName = filenames[i];
- /**
- * Method main
- *
- * @param unused
- */
- public static void main(String unused[]) {
-
- if (schemaValidate) {
- System.out.println("We do schema-validation");
- } else {
- System.out.println("We do not schema-validation");
- }
-
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- if (VerifyMerlinsExamplesSixteen.schemaValidate) {
- dbf.setAttribute("http://apache.org/xml/features/validation/schema",
- Boolean.TRUE);
- dbf.setAttribute(
- "http://apache.org/xml/features/dom/defer-node-expansion",
- Boolean.TRUE);
- dbf.setValidating(true);
- dbf.setAttribute("http://xml.org/sax/features/validation",
- Boolean.TRUE);
- dbf.setAttribute(
- "http://apache.org/xml/properties/schema/external-schemaLocation",
- Constants.SignatureSpecNS + " "
- + VerifyMerlinsExamplesSixteen.signatureSchemaFile);
- }
-
- dbf.setNamespaceAware(true);
- dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
-
- //J-
- String merlinsDir =
- "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-fifteen/";
- String filenames[] = { merlinsDir + "signature-enveloping-hmac-sha1.xml",
- merlinsDir + "signature-enveloping-hmac-sha1-40.xml",
- merlinsDir + "signature-enveloped-dsa.xml",
- merlinsDir + "signature-enveloping-b64-dsa.xml",
- merlinsDir + "signature-enveloping-dsa.xml",
- merlinsDir + "signature-enveloping-rsa.xml",
- merlinsDir + "signature-external-b64-dsa.xml",
- merlinsDir + "signature-external-dsa.xml"
- };
- //J+
- int start = 0;
- // int end = filenames.length;
- int end = 2;
-
- for (int i = start; i < 2; i++) {
- String signatureFileName = filenames[i];
-
- try {
- verifyHMAC(dbf, signatureFileName);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- for (int i = 2; i < filenames.length; i++) {
- String signatureFileName = filenames[i];
-
- try {
- VerifyMerlinsExamplesSixteen.verify(dbf, signatureFileName);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
-
- /**
- * Method verify
- *
- * @param dbf
- * @param filename
- * @throws Exception
- */
- public static void verifyHMAC(DocumentBuilderFactory dbf, String filename)
- throws Exception {
-
- File f = new File(filename);
-
- System.out.println("Try to verify " + f.toURL().toString());
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
-
- if (VerifyMerlinsExamplesSixteen.schemaValidate) {
- db.setErrorHandler(new org.apache.xml.security.utils
- .IgnoreAllErrorHandler());
- db.setEntityResolver(new org.xml.sax.EntityResolver() {
-
- public org.xml.sax
- .InputSource resolveEntity(String publicId, String systemId)
- throws org.xml.sax.SAXException {
-
- if (systemId.endsWith("xmldsig-core-schema.xsd")) {
- try {
- return new org.xml.sax
- .InputSource(new FileInputStream(signatureSchemaFile));
- } catch (FileNotFoundException ex) {
- throw new org.xml.sax.SAXException(ex);
- }
- } else {
- return null;
- }
+ try {
+ VerifyMerlinsExamplesSixteen.verify(dbf, signatureFileName);
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- });
- }
+ }
+ }
- org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
+ /**
+ * Method verify
+ *
+ * @param dbf
+ * @param filename
+ * @throws Exception
+ */
+ public static void verifyHMAC(DocumentBuilderFactory dbf, String filename)
+ throws Exception {
+
+ File f = new File(filename);
+
+ System.out.println("Try to verify " + f.toURI().toURL().toString());
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+
+ if (VerifyMerlinsExamplesSixteen.schemaValidate) {
+ db.setErrorHandler(new org.apache.xml.security.utils
+ .IgnoreAllErrorHandler());
+ db.setEntityResolver(new org.xml.sax.EntityResolver() {
+
+ public org.xml.sax.InputSource resolveEntity(String publicId, String systemId)
+ throws org.xml.sax.SAXException {
+
+ if (systemId.endsWith("xmldsig-core-schema.xsd")) {
+ try {
+ return new org.xml.sax.InputSource(new FileInputStream(signatureSchemaFile));
+ } catch (FileNotFoundException ex) {
+ throw new org.xml.sax.SAXException(ex);
+ }
+ } else {
+ return null;
+ }
+ }
+ });
+ }
+
+ org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
+
+ XPathFactory xpf = XPathFactory.newInstance();
+ XPath xpath = xpf.newXPath();
+ xpath.setNamespaceContext(new DSNamespaceContext());
+
+ String expression = "//ds:Signature[1]";
+ Element sigElement =
+ (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
+ XMLSignature signature =
+ new XMLSignature(sigElement, f.toURI().toURL().toString());
+
+ // signature.addResourceResolver(new OfflineResolver());
+
+ byte keybytes[] = "secret".getBytes("ASCII");
+ javax.crypto.SecretKey sk = signature.createSecretKey(keybytes);
+
+ System.out.println("The XML signature in file "
+ + f.toURI().toURL().toString() + " is "
+ + (signature.checkSignatureValue(sk)
+ ? "valid (good)" : "invalid !!!!! (bad)"));
+ }
- XPathFactory xpf = XPathFactory.newInstance();
- XPath xpath = xpf.newXPath();
- xpath.setNamespaceContext(new DSNamespaceContext());
-
- String expression = "//ds:Signature[1]";
- Element sigElement =
- (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
- XMLSignature signature = new XMLSignature(sigElement,
- f.toURL().toString());
-
- // signature.addResourceResolver(new OfflineResolver());
-
- byte keybytes[] = "secret".getBytes("ASCII");
- javax.crypto.SecretKey sk = signature.createSecretKey(keybytes);
-
- System.out.println("The XML signature in file "
- + f.toURL().toString() + " is "
- + (signature.checkSignatureValue(sk)
- ? "valid (good)"
- : "invalid !!!!! (bad)"));
- }
-
- static {
- org.apache.xml.security.Init.init();
- }
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesSixteen.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesSixteen.java?rev=1064102&r1=1064101&r2=1064102&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesSixteen.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesSixteen.java Thu Jan 27 12:19:05 2011
@@ -44,175 +44,151 @@ import org.w3c.dom.Element;
*/
public class VerifyMerlinsExamplesSixteen {
- /** {@link org.apache.commons.logging} logging facility */
+ /** {@link org.apache.commons.logging} logging facility */
static org.apache.commons.logging.Log log =
- org.apache.commons.logging.LogFactory.getLog(
- VerifyMerlinsExamplesSixteen.class.getName());
+ org.apache.commons.logging.LogFactory.getLog(VerifyMerlinsExamplesSixteen.class.getName());
- /** Field schemaValidate */
- static final boolean schemaValidate = false;
+ /** Field schemaValidate */
+ static final boolean schemaValidate = false;
- /** Field signatureSchemaFile */
- static final String signatureSchemaFile = "samples/data/xmldsig-core-schema.xsd";
-
- /**
- * Method main
- *
- * @param unused
- */
- public static void main(String unused[]) {
-
- if (schemaValidate) {
- System.out.println("We do schema-validation");
- } else {
- System.out.println("We do not schema-validation");
- }
-
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- if (VerifyMerlinsExamplesSixteen.schemaValidate) {
- dbf.setAttribute("http://apache.org/xml/features/validation/schema",
- Boolean.TRUE);
- dbf.setAttribute(
- "http://apache.org/xml/features/dom/defer-node-expansion",
- Boolean.TRUE);
- dbf.setValidating(true);
- dbf.setAttribute("http://xml.org/sax/features/validation",
- Boolean.TRUE);
- dbf.setAttribute(
- "http://apache.org/xml/properties/schema/external-schemaLocation",
- Constants.SignatureSpecNS + " "
- + VerifyMerlinsExamplesSixteen.signatureSchemaFile);
- }
-
- dbf.setNamespaceAware(true);
- dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
-
- //J-
- String filenames[] = {
- "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-sixteen/signature.xml"
- };
- //J+
- int start = 0;
- int end = filenames.length;
-
- for (int i = start; i < end; i++) {
- String signatureFileName = filenames[i];
-
- try {
- verify(dbf, signatureFileName);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
-
- /**
- * Method verify
- *
- * @param dbf
- * @param filename
- * @throws Exception
- */
- public static void verify(DocumentBuilderFactory dbf, String filename)
- throws Exception {
-
- File f = new File(filename);
-
- System.out.println("Try to verify " + f.toURL().toString());
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
-
- if (VerifyMerlinsExamplesSixteen.schemaValidate) {
- db.setErrorHandler(new org.apache.xml.security.utils
- .IgnoreAllErrorHandler());
- db.setEntityResolver(new org.xml.sax.EntityResolver() {
-
- public org.xml.sax
- .InputSource resolveEntity(String publicId, String systemId)
- throws org.xml.sax.SAXException {
-
- if (systemId.endsWith("xmldsig-core-schema.xsd")) {
- try {
- return new org.xml.sax
- .InputSource(new FileInputStream(signatureSchemaFile));
- } catch (FileNotFoundException ex) {
- throw new org.xml.sax.SAXException(ex);
- }
- } else {
- return null;
- }
+ /** Field signatureSchemaFile */
+ static final String signatureSchemaFile = "samples/data/xmldsig-core-schema.xsd";
+
+ static {
+ org.apache.xml.security.Init.init();
+ }
+
+ /**
+ * Method main
+ *
+ * @param unused
+ */
+ public static void main(String unused[]) {
+
+ if (schemaValidate) {
+ System.out.println("We do schema-validation");
+ } else {
+ System.out.println("We do not schema-validation");
+ }
+
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ if (VerifyMerlinsExamplesSixteen.schemaValidate) {
+ dbf.setAttribute("http://apache.org/xml/features/validation/schema",
+ Boolean.TRUE);
+ dbf.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion",
+ Boolean.TRUE);
+ dbf.setValidating(true);
+ dbf.setAttribute("http://xml.org/sax/features/validation",
+ Boolean.TRUE);
+ dbf.setAttribute("http://apache.org/xml/properties/schema/external-schemaLocation",
+ Constants.SignatureSpecNS + " "
+ + VerifyMerlinsExamplesSixteen.signatureSchemaFile);
+ }
+
+ dbf.setNamespaceAware(true);
+ dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
+
+ String filenames[] = {
+ "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-sixteen/signature.xml"
+ };
+ int start = 0;
+ int end = filenames.length;
+
+ for (int i = start; i < end; i++) {
+ String signatureFileName = filenames[i];
+
+ try {
+ verify(dbf, signatureFileName);
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- });
- }
-
- org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
+ }
+ }
- XPathFactory xpf = XPathFactory.newInstance();
- XPath xpath = xpf.newXPath();
- xpath.setNamespaceContext(new DSNamespaceContext());
-
- String expression = "//ds:Signature[1]";
- Element sigElement =
- (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
- XMLSignature signature = new XMLSignature(sigElement,
- f.toURL().toString());
-
- signature.getSignedInfo()
- .addResourceResolver(new org.apache.xml.security.samples.utils.resolver
- .OfflineResolver());
-
- signature.setFollowNestedManifests(false);
-
- // signature.addResourceResolver(new OfflineResolver());
-
- // XMLUtils.outputDOMc14nWithComments(signature.getElement(), System.out);
- KeyInfo ki = signature.getKeyInfo();
-
- if (ki != null) {
- /*
- if (ki.containsX509Data()) {
- System.out.println("Could find a X509Data element in the KeyInfo");
- }
- */
-
- X509Certificate cert = signature.getKeyInfo().getX509Certificate();
-
- if (cert != null) {
- /*
- System.out.println(
- "I try to verify the signature using the X509 Certificate: "
- + cert);
- */
- System.out.println("The XML signature in file "
- + f.toURL().toString() + " is "
- + (signature.checkSignatureValue(cert)
- ? "valid (good)"
- : "invalid !!!!! (bad)"));
- } else {
- // System.out.println("Did not find a Certificate");
-
- PublicKey pk = signature.getKeyInfo().getPublicKey();
-
- if (pk != null) {
- // System.out.println("I try to verify the signature using the public key: " + pk);
- System.out.println("The XML signature in file "
- + f.toURL().toString() + " is "
- + (signature.checkSignatureValue(pk)
- ? "valid (good)"
- : "invalid !!!!! (bad)"));
+ /**
+ * Method verify
+ *
+ * @param dbf
+ * @param filename
+ * @throws Exception
+ */
+ public static void verify(DocumentBuilderFactory dbf, String filename)
+ throws Exception {
+
+ File f = new File(filename);
+
+ System.out.println("Try to verify " + f.toURL().toString());
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+
+ if (VerifyMerlinsExamplesSixteen.schemaValidate) {
+ db.setErrorHandler(new org.apache.xml.security.utils
+ .IgnoreAllErrorHandler());
+ db.setEntityResolver(new org.xml.sax.EntityResolver() {
+
+ public org.xml.sax.InputSource resolveEntity(String publicId, String systemId)
+ throws org.xml.sax.SAXException {
+
+ if (systemId.endsWith("xmldsig-core-schema.xsd")) {
+ try {
+ return new org.xml.sax
+ .InputSource(new FileInputStream(signatureSchemaFile));
+ } catch (FileNotFoundException ex) {
+ throw new org.xml.sax.SAXException(ex);
+ }
+ } else {
+ return null;
+ }
+ }
+ });
+ }
+
+ org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
+
+ XPathFactory xpf = XPathFactory.newInstance();
+ XPath xpath = xpf.newXPath();
+ xpath.setNamespaceContext(new DSNamespaceContext());
+
+ String expression = "//ds:Signature[1]";
+ Element sigElement =
+ (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
+ XMLSignature signature =
+ new XMLSignature(sigElement, f.toURI().toURL().toString());
+
+ signature.getSignedInfo().addResourceResolver(
+ new org.apache.xml.security.samples.utils.resolver.OfflineResolver()
+ );
+
+ signature.setFollowNestedManifests(false);
+
+ KeyInfo ki = signature.getKeyInfo();
+
+ if (ki != null) {
+ X509Certificate cert = signature.getKeyInfo().getX509Certificate();
+
+ if (cert != null) {
+ System.out.println("The XML signature in file "
+ + f.toURI().toURL().toString() + " is "
+ + (signature.checkSignatureValue(cert)
+ ? "valid (good)" : "invalid !!!!! (bad)"));
} else {
- System.out.println(
- "Did not find a public key, so I can't check the signature");
+ PublicKey pk = signature.getKeyInfo().getPublicKey();
+
+ if (pk != null) {
+ System.out.println("The XML signature in file "
+ + f.toURI().toURL().toString() + " is "
+ + (signature.checkSignatureValue(pk)
+ ? "valid (good)" : "invalid !!!!! (bad)"));
+ } else {
+ System.out.println(
+ "Did not find a public key, so I can't check the signature");
+ }
}
- }
- } else {
- System.out.println("Did not find a KeyInfo");
- }
- }
-
- static {
- org.apache.xml.security.Init.init();
- }
+ } else {
+ System.out.println("Did not find a KeyInfo");
+ }
+ }
+
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesTwentyThree.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesTwentyThree.java?rev=1064102&r1=1064101&r2=1064102&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesTwentyThree.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifyMerlinsExamplesTwentyThree.java Thu Jan 27 12:19:05 2011
@@ -16,8 +16,6 @@
*/
package org.apache.xml.security.samples.signature;
-
-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -37,194 +35,166 @@ import org.apache.xml.security.utils.Con
import org.apache.xml.security.utils.XMLUtils;
import org.w3c.dom.Element;
-
/**
*
* @author $Author$
*/
public class VerifyMerlinsExamplesTwentyThree {
- /** {@link org.apache.commons.logging} logging facility */
+ /** {@link org.apache.commons.logging} logging facility */
static org.apache.commons.logging.Log log =
- org.apache.commons.logging.LogFactory.getLog(
- VerifyMerlinsExamplesTwentyThree.class.getName());
-
- /** Field schemaValidate */
- static final boolean schemaValidate = false;
+ org.apache.commons.logging.LogFactory.getLog(VerifyMerlinsExamplesTwentyThree.class.getName());
- /** Field signatureSchemaFile */
- static final String signatureSchemaFile = "samples/data/xmldsig-core-schema.xsd";
+ /** Field schemaValidate */
+ static final boolean schemaValidate = false;
- /**
- * Method main
- *
- * @param unused
- */
- public static void main(String unused[]) {
-
- if (schemaValidate) {
- System.out.println("We do schema-validation");
- } else {
- System.out.println("We do not schema-validation");
- }
-
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- if (VerifyMerlinsExamplesTwentyThree.schemaValidate) {
- dbf.setAttribute("http://apache.org/xml/features/validation/schema",
- Boolean.TRUE);
- dbf.setAttribute(
- "http://apache.org/xml/features/dom/defer-node-expansion",
- Boolean.TRUE);
- dbf.setValidating(true);
- dbf.setAttribute("http://xml.org/sax/features/validation",
- Boolean.TRUE);
- dbf.setAttribute(
- "http://apache.org/xml/properties/schema/external-schemaLocation",
- Constants.SignatureSpecNS + " "
- + VerifyMerlinsExamplesTwentyThree.signatureSchemaFile);
- }
-
- dbf.setNamespaceAware(true);
- dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
-
- //J-
- String merlinsDir =
- "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/";
- String filenames[] = { // "23signature.xml"
- // "merlinsTwentyThreeRecreated.xml"
- merlinsDir + "signature.xml",
- merlinsDir + "signature-enveloped-dsa.xml",
- merlinsDir + "signature-enveloping-b64-dsa.xml",
- merlinsDir + "signature-enveloping-dsa.xml",
- merlinsDir + "signature-enveloping-hmac-sha1-40.xml",
- merlinsDir + "signature-enveloping-hmac-sha1.xml",
- merlinsDir + "signature-enveloping-rsa.xml",
- merlinsDir + "signature-external-b64-dsa.xml",
- merlinsDir + "signature-external-dsa.xml"
- };
- //J+
- int start = 0;
- int end = filenames.length;
-
- for (int i = start; i < end; i++) {
- String signatureFileName = filenames[i];
-
- try {
- verify(dbf, signatureFileName);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
-
- /**
- * Method verify
- *
- * @param dbf
- * @param filename
- * @throws Exception
- */
- public static void verify(DocumentBuilderFactory dbf, String filename)
- throws Exception {
-
- File f = new File(filename);
-
- System.out.println("Try to verify " + f.toURL().toString());
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
-
- if (VerifyMerlinsExamplesTwentyThree.schemaValidate) {
- db.setErrorHandler(new org.apache.xml.security.utils
- .IgnoreAllErrorHandler());
- db.setEntityResolver(new org.xml.sax.EntityResolver() {
-
- public org.xml.sax
- .InputSource resolveEntity(String publicId, String systemId)
- throws org.xml.sax.SAXException {
-
- if (systemId.endsWith("xmldsig-core-schema.xsd")) {
- try {
- return new org.xml.sax
- .InputSource(new FileInputStream(signatureSchemaFile));
- } catch (FileNotFoundException ex) {
- throw new org.xml.sax.SAXException(ex);
- }
- } else {
- return null;
- }
+ /** Field signatureSchemaFile */
+ static final String signatureSchemaFile = "samples/data/xmldsig-core-schema.xsd";
+
+ static {
+ org.apache.xml.security.Init.init();
+ }
+
+ /**
+ * Method main
+ *
+ * @param unused
+ */
+ public static void main(String unused[]) {
+
+ if (schemaValidate) {
+ System.out.println("We do schema-validation");
+ } else {
+ System.out.println("We do not schema-validation");
+ }
+
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ if (VerifyMerlinsExamplesTwentyThree.schemaValidate) {
+ dbf.setAttribute("http://apache.org/xml/features/validation/schema",
+ Boolean.TRUE);
+ dbf.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion",
+ Boolean.TRUE);
+ dbf.setValidating(true);
+ dbf.setAttribute("http://xml.org/sax/features/validation",
+ Boolean.TRUE);
+ dbf.setAttribute("http://apache.org/xml/properties/schema/external-schemaLocation",
+ Constants.SignatureSpecNS + " "
+ + VerifyMerlinsExamplesTwentyThree.signatureSchemaFile);
+ }
+
+ dbf.setNamespaceAware(true);
+ dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
+
+ String merlinsDir =
+ "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/";
+ String filenames[] = { // "23signature.xml"
+ // "merlinsTwentyThreeRecreated.xml"
+ merlinsDir + "signature.xml",
+ merlinsDir + "signature-enveloped-dsa.xml",
+ merlinsDir + "signature-enveloping-b64-dsa.xml",
+ merlinsDir + "signature-enveloping-dsa.xml",
+ merlinsDir + "signature-enveloping-hmac-sha1-40.xml",
+ merlinsDir + "signature-enveloping-hmac-sha1.xml",
+ merlinsDir + "signature-enveloping-rsa.xml",
+ merlinsDir + "signature-external-b64-dsa.xml",
+ merlinsDir + "signature-external-dsa.xml"
+ };
+ int start = 0;
+ int end = filenames.length;
+
+ for (int i = start; i < end; i++) {
+ String signatureFileName = filenames[i];
+
+ try {
+ verify(dbf, signatureFileName);
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- });
- }
+ }
+ }
- org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
-
- XPathFactory xpf = XPathFactory.newInstance();
- XPath xpath = xpf.newXPath();
- xpath.setNamespaceContext(new DSNamespaceContext());
-
- String expression = "//ds:Signature[1]";
- Element sigElement =
- (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
- XMLSignature signature = new XMLSignature(sigElement,
- f.toURL().toString());
-
- signature.getSignedInfo()
- .addResourceResolver(new org.apache.xml.security.samples.utils.resolver
- .OfflineResolver());
-
- signature.setFollowNestedManifests(false);
-
- // signature.addResourceResolver(new OfflineResolver());
-
- // XMLUtils.outputDOMc14nWithComments(signature.getElement(), System.out);
- KeyInfo ki = signature.getKeyInfo();
-
- if (ki != null) {
- /*
- if (ki.containsX509Data()) {
- System.out.println("Could find a X509Data element in the KeyInfo");
- }
- */
-
- X509Certificate cert = signature.getKeyInfo().getX509Certificate();
-
- if (cert != null) {
- /*
- System.out.println(
- "I try to verify the signature using the X509 Certificate: "
- + cert);
- */
- System.out.println("The XML signature in file "
- + f.toURL().toString() + " is "
- + (signature.checkSignatureValue(cert)
- ? "valid (good)"
- : "invalid !!!!! (bad)"));
- } else {
- // System.out.println("Did not find a Certificate");
-
- PublicKey pk = signature.getKeyInfo().getPublicKey();
-
- if (pk != null) {
- // System.out.println("I try to verify the signature using the public key: " + pk);
- System.out.println("The XML signature in file "
- + f.toURL().toString() + " is "
- + (signature.checkSignatureValue(pk)
- ? "valid (good)"
- : "invalid !!!!! (bad)"));
+ /**
+ * Method verify
+ *
+ * @param dbf
+ * @param filename
+ * @throws Exception
+ */
+ public static void verify(DocumentBuilderFactory dbf, String filename)
+ throws Exception {
+
+ File f = new File(filename);
+
+ System.out.println("Try to verify " + f.toURI().toURL().toString());
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+
+ if (VerifyMerlinsExamplesTwentyThree.schemaValidate) {
+ db.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
+ db.setEntityResolver(new org.xml.sax.EntityResolver() {
+
+ public org.xml.sax.InputSource resolveEntity(String publicId, String systemId)
+ throws org.xml.sax.SAXException {
+
+ if (systemId.endsWith("xmldsig-core-schema.xsd")) {
+ try {
+ return new org.xml.sax.InputSource(new FileInputStream(signatureSchemaFile));
+ } catch (FileNotFoundException ex) {
+ throw new org.xml.sax.SAXException(ex);
+ }
+ } else {
+ return null;
+ }
+ }
+ });
+ }
+
+ org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
+
+ XPathFactory xpf = XPathFactory.newInstance();
+ XPath xpath = xpf.newXPath();
+ xpath.setNamespaceContext(new DSNamespaceContext());
+
+ String expression = "//ds:Signature[1]";
+ Element sigElement =
+ (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
+ XMLSignature signature =
+ new XMLSignature(sigElement, f.toURI().toURL().toString());
+
+ signature.getSignedInfo()
+ .addResourceResolver(new org.apache.xml.security.samples.utils.resolver.OfflineResolver());
+
+ signature.setFollowNestedManifests(false);
+
+ KeyInfo ki = signature.getKeyInfo();
+
+ if (ki != null) {
+ X509Certificate cert = signature.getKeyInfo().getX509Certificate();
+
+ if (cert != null) {
+ System.out.println("The XML signature in file "
+ + f.toURI().toURL().toString() + " is "
+ + (signature.checkSignatureValue(cert)
+ ? "valid (good)" : "invalid !!!!! (bad)"));
} else {
- System.out.println(
- "Did not find a public key, so I can't check the signature");
+ PublicKey pk = signature.getKeyInfo().getPublicKey();
+
+ if (pk != null) {
+ System.out.println("The XML signature in file "
+ + f.toURI().toURL().toString() + " is "
+ + (signature.checkSignatureValue(pk)
+ ? "valid (good)" : "invalid !!!!! (bad)"));
+ } else {
+ System.out.println(
+ "Did not find a public key, so I can't check the signature");
+ }
}
- }
- } else {
- System.out.println("Did not find a KeyInfo");
- }
-
- }
-
- static {
- org.apache.xml.security.Init.init();
- }
+ } else {
+ System.out.println("Did not find a KeyInfo");
+ }
+
+ }
+
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifySignature.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifySignature.java?rev=1064102&r1=1064101&r2=1064102&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifySignature.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/VerifySignature.java Thu Jan 27 12:19:05 2011
@@ -39,145 +39,127 @@ import org.w3c.dom.Element;
* @author $Author$
*/
public class VerifySignature {
-
- /**
- * Method main
- *
- * @param unused
- */
- public static void main(String unused[]) {
-
- boolean schemaValidate = false;
- final String signatureSchemaFile = "samples/data/xmldsig-core-schema.xsd";
- // String signatureFileName = "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-fifteen/signature-enveloping-rsa.xml";
- String signatureFileName = "signature.xml";
-
- if (schemaValidate) {
- System.out.println("We do schema-validation");
- }
-
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- if (schemaValidate) {
- dbf.setAttribute("http://apache.org/xml/features/validation/schema",
- Boolean.TRUE);
- dbf.setAttribute(
- "http://apache.org/xml/features/dom/defer-node-expansion",
- Boolean.TRUE);
- dbf.setValidating(true);
- dbf.setAttribute("http://xml.org/sax/features/validation",
- Boolean.TRUE);
- }
-
- dbf.setNamespaceAware(true);
- dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
-
- if (schemaValidate) {
- dbf.setAttribute(
- "http://apache.org/xml/properties/schema/external-schemaLocation",
- Constants.SignatureSpecNS + " " + signatureSchemaFile);
- }
-
- try {
-
- // File f = new File("signature.xml");
- File f = new File(signatureFileName);
-
- System.out.println("Try to verify " + f.toURL().toString());
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
-
- db.setErrorHandler(new org.apache.xml.security.utils
- .IgnoreAllErrorHandler());
-
- if (schemaValidate) {
- db.setEntityResolver(new org.xml.sax.EntityResolver() {
-
- public org.xml.sax.InputSource resolveEntity(
- String publicId, String systemId)
- throws org.xml.sax.SAXException {
-
- if (systemId.endsWith("xmldsig-core-schema.xsd")) {
- try {
- return new org.xml.sax.InputSource(
- new FileInputStream(signatureSchemaFile));
- } catch (FileNotFoundException ex) {
- throw new org.xml.sax.SAXException(ex);
- }
- } else {
- return null;
- }
- }
- });
- }
-
- org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
-
- XPathFactory xpf = XPathFactory.newInstance();
- XPath xpath = xpf.newXPath();
- xpath.setNamespaceContext(new DSNamespaceContext());
-
- String expression = "//ds:Signature[1]";
- Element sigElement =
- (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
- XMLSignature signature = new XMLSignature(sigElement,
- f.toURI().toURL().toString());
-
- signature.addResourceResolver(new OfflineResolver());
-
- // XMLUtils.outputDOMc14nWithComments(signature.getElement(), System.out);
- KeyInfo ki = signature.getKeyInfo();
-
- if (ki != null) {
- if (ki.containsX509Data()) {
- System.out
- .println("Could find a X509Data element in the KeyInfo");
+
+ static {
+ org.apache.xml.security.Init.init();
+ }
+
+ /**
+ * Method main
+ *
+ * @param unused
+ */
+ public static void main(String unused[]) {
+
+ boolean schemaValidate = false;
+ final String signatureSchemaFile = "samples/data/xmldsig-core-schema.xsd";
+ String signatureFileName = "signature.xml";
+
+ if (schemaValidate) {
+ System.out.println("We do schema-validation");
+ }
+
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ if (schemaValidate) {
+ dbf.setAttribute("http://apache.org/xml/features/validation/schema",
+ Boolean.TRUE);
+ dbf.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion",
+ Boolean.TRUE);
+ dbf.setValidating(true);
+ dbf.setAttribute("http://xml.org/sax/features/validation",
+ Boolean.TRUE);
+ }
+
+ dbf.setNamespaceAware(true);
+ dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
+
+ if (schemaValidate) {
+ dbf.setAttribute("http://apache.org/xml/properties/schema/external-schemaLocation",
+ Constants.SignatureSpecNS + " " + signatureSchemaFile);
+ }
+
+ try {
+
+ File f = new File(signatureFileName);
+
+ System.out.println("Try to verify " + f.toURI().toURL().toString());
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+
+ db.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
+
+ if (schemaValidate) {
+ db.setEntityResolver(new org.xml.sax.EntityResolver() {
+
+ public org.xml.sax.InputSource resolveEntity(
+ String publicId, String systemId
+ ) throws org.xml.sax.SAXException {
+
+ if (systemId.endsWith("xmldsig-core-schema.xsd")) {
+ try {
+ return new org.xml.sax.InputSource(
+ new FileInputStream(signatureSchemaFile));
+ } catch (FileNotFoundException ex) {
+ throw new org.xml.sax.SAXException(ex);
+ }
+ } else {
+ return null;
+ }
+ }
+ });
}
- X509Certificate cert = signature.getKeyInfo().getX509Certificate();
+ org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
- if (cert != null) {
- /*
- System.out.println(
- "I try to verify the signature using the X509 Certificate: "
- + cert);
- */
- System.out.println("The XML signature in file "
- + f.toURL().toString() + " is "
- + (signature.checkSignatureValue(cert)
- ? "valid (good)"
- : "invalid !!!!! (bad)"));
+ XPathFactory xpf = XPathFactory.newInstance();
+ XPath xpath = xpf.newXPath();
+ xpath.setNamespaceContext(new DSNamespaceContext());
+
+ String expression = "//ds:Signature[1]";
+ Element sigElement =
+ (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
+ XMLSignature signature =
+ new XMLSignature(sigElement, f.toURI().toURL().toString());
+
+ signature.addResourceResolver(new OfflineResolver());
+
+ KeyInfo ki = signature.getKeyInfo();
+
+ if (ki != null) {
+ if (ki.containsX509Data()) {
+ System.out.println("Could find a X509Data element in the KeyInfo");
+ }
+
+ X509Certificate cert = signature.getKeyInfo().getX509Certificate();
+
+ if (cert != null) {
+ System.out.println("The XML signature in file "
+ + f.toURI().toURL().toString() + " is "
+ + (signature.checkSignatureValue(cert)
+ ? "valid (good)" : "invalid !!!!! (bad)"));
+ } else {
+ System.out.println("Did not find a Certificate");
+
+ PublicKey pk = signature.getKeyInfo().getPublicKey();
+
+ if (pk != null) {
+ System.out.println("The XML signature in file "
+ + f.toURI().toURL().toString() + " is "
+ + (signature.checkSignatureValue(pk)
+ ? "valid (good)" : "invalid !!!!! (bad)"));
+ } else {
+ System.out.println(
+ "Did not find a public key, so I can't check the signature");
+ }
+ }
} else {
- System.out.println("Did not find a Certificate");
-
- PublicKey pk = signature.getKeyInfo().getPublicKey();
-
- if (pk != null) {
- /*
- System.out.println(
- "I try to verify the signature using the public key: "
- + pk);
- */
- System.out.println("The XML signature in file "
- + f.toURL().toString() + " is "
- + (signature.checkSignatureValue(pk)
- ? "valid (good)"
- : "invalid !!!!! (bad)"));
- } else {
- System.out.println(
- "Did not find a public key, so I can't check the signature");
- }
+ System.out.println("Did not find a KeyInfo");
}
- } else {
- System.out.println("Did not find a KeyInfo");
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- static {
- org.apache.xml.security.Init.init();
- }
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
}