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/26 17:00:43 UTC
svn commit: r1063768 [2/3] - in /santuario/xml-security-java/trunk: ./ data/
samples/data/ samples/data/at/ samples/data/at/iaik/
samples/data/at/iaik/ixsil/ samples/data/com/ samples/data/com/pothole/
samples/data/com/pureedge/ samples/data/ie/ sample...
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateMerlinsExampleSixteen.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateMerlinsExampleSixteen.java?rev=1063768&r1=1063767&r2=1063768&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateMerlinsExampleSixteen.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateMerlinsExampleSixteen.java Wed Jan 26 16:00:40 2011
@@ -16,8 +16,6 @@
*/
package org.apache.xml.security.samples.signature;
-
-
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
@@ -49,485 +47,432 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
-
/**
*
* @author $Author$
*/
public class CreateMerlinsExampleSixteen {
-
- /** {@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(CreateMerlinsExampleSixteen.class.getName());
- /**
- * Method main
- *
- * @param unused
- * @throws Exception
- */
- public static void main(String unused[]) throws Exception {
- Constants.setSignatureSpecNSprefix("ds");
- //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("merlinsSixteenRecreatedNoRetrievalMethod.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());
-
- if (privateKey == null) {
- throw new RuntimeException("Private key is null");
- }
-
- X509Certificate cert =
- (X509Certificate) ks.getCertificate(certificateAlias);
- 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();
-
- //////////////////////////////////////////////////
- Element envelope = doc.createElementNS("http://www.usps.gov/",
- "Envelope");
-
- envelope.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "http://www.usps.gov/");
- envelope.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:foo", "http://www.usps.gov/foo");
- envelope.appendChild(doc.createTextNode("\n"));
- doc.appendChild(doc.createComment(" Preamble "));
- doc.appendChild(envelope);
- doc.appendChild(doc.createComment(" Postamble "));
-
- Element dearSir = doc.createElementNS("http://www.usps.gov/", "DearSir");
-
- dearSir.appendChild(doc.createTextNode("foo"));
- envelope.appendChild(dearSir);
- envelope.appendChild(doc.createTextNode("\n"));
-
- Element body = doc.createElementNS("http://www.usps.gov/", "Body");
-
- body.appendChild(doc.createTextNode("bar"));
- envelope.appendChild(body);
- envelope.appendChild(doc.createTextNode("\n"));
-
- Element YoursSincerely = doc.createElementNS("http://www.usps.gov/",
- "YoursSincerely");
- YoursSincerely.appendChild(doc.createTextNode("\n"));
-
- envelope.appendChild(YoursSincerely);
-
- Element PostScript = doc.createElementNS("http://www.usps.gov/",
- "PostScript");
-
- PostScript.appendChild(doc.createTextNode("bar"));
- envelope.appendChild(PostScript);
-
- Element Notaries = doc.createElementNS(null, "Notaries");
-
- Notaries.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
- Notaries.setAttributeNS(null, "Id", "notaries");
- IdResolver.registerElementById(Notaries, "Id");
-
- {
- Element Notary = doc.createElementNS(null, "Notary");
-
- Notary.setAttributeNS(null, "name", "Great, A. T.");
- Notaries.appendChild(Notary);
- }
-
- {
- Element Notary = doc.createElementNS(null, "Notary");
-
- Notary.setAttributeNS(null, "name", "Hun, A. T.");
- Notaries.appendChild(Notary);
- }
-
- envelope.appendChild(Notaries);
- envelope.appendChild(doc.createComment(" Commentary "));
-
- //////////////////////////////////////////////////
- String BaseURI = signatureFile.toURL().toString();
- XMLSignature sig = new XMLSignature(doc, BaseURI,
- XMLSignature.ALGO_ID_SIGNATURE_DSA);
-
- YoursSincerely.appendChild(sig.getElement());
- sig.setId("signature");
-
- /*
- * Add the Objects
- */
-
- // object-1
- {
- ObjectContainer object1 = new ObjectContainer(doc);
-
- object1.setId("object-1");
- object1.setMimeType("text/plain");
- object1.appendChild(doc.createTextNode("I am the text."));
- sig.appendObject(object1);
- }
-
- // object-2
- {
- ObjectContainer object2 = new ObjectContainer(doc);
-
- object2.setId("object-2");
- object2.setMimeType("text/plain");
- object2.setEncoding("http://www.w3.org/2000/09/xmldsig#base64");
- object2.appendChild(doc.createTextNode("SSBhbSB0aGUgdGV4dC4="));
- sig.appendObject(object2);
- }
-
- // object-3
- {
- ObjectContainer object = new ObjectContainer(doc);
-
- object.setId("object-3");
-
- Element nonc = doc.createElementNS(null, "NonCommentandus");
-
- nonc.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
- nonc.appendChild(doc.createComment(" Commentandum "));
- object.appendChild(doc.createTextNode("\n "));
- object.appendChild(nonc);
- object.appendChild(doc.createTextNode("\n "));
- sig.appendObject(object);
- }
-
- // object number 4
- {
- ObjectContainer object = new ObjectContainer(doc);
-
- object.appendChild(createObject4(sig));
- sig.appendObject(object);
- }
-
- // object number 4
- {
- ObjectContainer object = new ObjectContainer(doc);
- SignatureProperties sps = new SignatureProperties(doc);
-
- sps.setId("signature-properties-1");
-
- SignatureProperty sp = new SignatureProperty(doc, "#signature");
- Element signedAdress = doc.createElementNS("urn:demo",
- "SignedAddress");
-
- signedAdress.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "urn:demo");
-
- Element IP = doc.createElementNS("urn:demo", "IP");
-
- IP.appendChild(doc.createTextNode("192.168.21.138"));
- signedAdress.appendChild(IP);
- sp.appendChild(signedAdress);
- sps.addSignatureProperty(sp);
- object.appendChild(sps.getElement());
- sig.appendObject(object);
- }
-
- {
- ObjectContainer object = new ObjectContainer(doc);
-
- object.setId("object-4");
-
- X509Data x509data = new X509Data(doc);
-
- x509data.add(new XMLX509SubjectName(doc, cert));
- x509data.add(new XMLX509IssuerSerial(doc, cert));
- x509data.add(new XMLX509Certificate(doc, cert));
- object.appendChild(x509data.getElement());
- sig.appendObject(object);
- }
-
- /*
- * Add References
- */
- sig.getSignedInfo()
- .addResourceResolver(new org.apache.xml.security.samples.utils.resolver
- .OfflineResolver());
- sig.addDocument("http://www.w3.org/TR/xml-stylesheet");
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_BASE64_DECODE);
- sig.addDocument("http://xmldsig.pothole.com/xml-stylesheet.txt",
- transforms, Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- Transforms transforms = new Transforms(doc);
- XPathContainer xpathC = new XPathContainer(doc);
-
- xpathC.setXPath("self::text()");
- transforms.addTransform(Transforms.TRANSFORM_XPATH,
- xpathC.getElementPlusReturns());
- sig.addDocument("#object-1", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
- /*
- {
- Transforms transforms = new Transforms(doc);
- XPathContainer xpathC = new XPathContainer(doc);
-
- //J-
- xpathC.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
- xpathC.setXPath("\n"
- + " ancestor-or-self::ds:SignedInfo " + "\n"
- + " and " + "\n"
- + " count(ancestor-or-self::ds:Reference | " + "\n"
- + " here()/ancestor::ds:Reference[1]) > " + "\n"
- + " count(ancestor-or-self::ds:Reference) " + "\n"
- + " or " + "\n"
- + " count(ancestor-or-self::node() | " + "\n"
- + " id('notaries')) = " + "\n"
- + " count(ancestor-or-self::node()) " + "\n");
- //J+
- transforms.addTransform(Transforms.TRANSFORM_XPATH,
- xpathC.getElementPlusReturns());
- sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
- */
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_BASE64_DECODE);
- sig.addDocument("#object-2", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- sig.addDocument("#manifest-1", null, Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Manifest");
- sig.addDocument("#signature-properties-1", null,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#SignatureProperties");
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- sig.addDocument("#xpointer(/)", transforms,
- Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("#xpointer(/)", transforms,
- Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- sig.addDocument("#object-3", null, Constants.ALGO_ID_DIGEST_SHA1,
- null, "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("#object-3", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- {
- sig.addDocument("#xpointer(id('object-3'))", null,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("#xpointer(id('object-3'))", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- {
- sig.addDocument("#manifest-reference-1", null,
- Constants.ALGO_ID_DIGEST_SHA1, "reference-1",
- "http://www.w3.org/2000/09/xmldsig#Reference");
- }
-
- {
- sig.addDocument("#reference-1", null,
- Constants.ALGO_ID_DIGEST_SHA1, "reference-2",
- "http://www.w3.org/2000/09/xmldsig#Reference");
- }
-
- {
- sig.addDocument("#reference-2", null,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Reference");
- }
-
- /*
- * Add KeyInfo and sign()
- */
- {
- Transforms retrievalTransforms = new Transforms(doc);
- XPathContainer xpathC = new XPathContainer(doc);
-
- xpathC.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
- xpathC.setXPath("ancestor-or-self::ds:X509Data");
- retrievalTransforms.addTransform(Transforms.TRANSFORM_XPATH,
- xpathC.getElement());
- sig.getKeyInfo().add(
- new RetrievalMethod(
- doc, "#object-4", retrievalTransforms,
- "http://www.w3.org/2000/09/xmldsig#X509Data"));
-
- /*
- X509Data x509data = new X509Data(doc);
-
- x509data.add(new XMLX509SubjectName(doc, cert));
- x509data.add(new XMLX509IssuerSerial(doc, cert));
- x509data.add(new XMLX509Certificate(doc, cert));
- sig.getKeyInfo().add(x509data);
+ static {
+ org.apache.xml.security.Init.init();
+ }
+
+ /**
+ * Method main
+ *
+ * @param unused
+ * @throws Exception
+ */
+ public static void main(String unused[]) throws Exception {
+ Constants.setSignatureSpecNSprefix("ds");
+ 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("merlinsSixteenRecreatedNoRetrievalMethod.xml");
+
+ KeyStore ks = KeyStore.getInstance(keystoreType);
+ FileInputStream fis = new FileInputStream(keystoreFile);
+
+ ks.load(fis, keystorePass.toCharArray());
+
+ PrivateKey privateKey =
+ (PrivateKey) ks.getKey(privateKeyAlias, privateKeyPass.toCharArray());
+
+ if (privateKey == null) {
+ throw new RuntimeException("Private key is null");
+ }
+
+ X509Certificate cert =
+ (X509Certificate) ks.getCertificate(certificateAlias);
+ 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();
+
+ //////////////////////////////////////////////////
+ Element envelope = doc.createElementNS("http://www.usps.gov/", "Envelope");
+
+ envelope.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "http://www.usps.gov/");
+ envelope.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:foo", "http://www.usps.gov/foo");
+ envelope.appendChild(doc.createTextNode("\n"));
+ doc.appendChild(doc.createComment(" Preamble "));
+ doc.appendChild(envelope);
+ doc.appendChild(doc.createComment(" Postamble "));
+
+ Element dearSir = doc.createElementNS("http://www.usps.gov/", "DearSir");
+
+ dearSir.appendChild(doc.createTextNode("foo"));
+ envelope.appendChild(dearSir);
+ envelope.appendChild(doc.createTextNode("\n"));
+
+ Element body = doc.createElementNS("http://www.usps.gov/", "Body");
+
+ body.appendChild(doc.createTextNode("bar"));
+ envelope.appendChild(body);
+ envelope.appendChild(doc.createTextNode("\n"));
+
+ Element YoursSincerely = doc.createElementNS("http://www.usps.gov/", "YoursSincerely");
+ YoursSincerely.appendChild(doc.createTextNode("\n"));
+
+ envelope.appendChild(YoursSincerely);
+
+ Element PostScript = doc.createElementNS("http://www.usps.gov/", "PostScript");
+
+ PostScript.appendChild(doc.createTextNode("bar"));
+ envelope.appendChild(PostScript);
+
+ Element Notaries = doc.createElementNS(null, "Notaries");
+
+ Notaries.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
+ Notaries.setAttributeNS(null, "Id", "notaries");
+ IdResolver.registerElementById(Notaries, "Id");
+
+ {
+ Element Notary = doc.createElementNS(null, "Notary");
+
+ Notary.setAttributeNS(null, "name", "Great, A. T.");
+ Notaries.appendChild(Notary);
+ }
+
+ {
+ Element Notary = doc.createElementNS(null, "Notary");
+
+ Notary.setAttributeNS(null, "name", "Hun, A. T.");
+ Notaries.appendChild(Notary);
+ }
+
+ envelope.appendChild(Notaries);
+ envelope.appendChild(doc.createComment(" Commentary "));
+
+ //////////////////////////////////////////////////
+ String BaseURI = signatureFile.toURI().toURL().toString();
+ XMLSignature sig =
+ new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_SIGNATURE_DSA);
+
+ YoursSincerely.appendChild(sig.getElement());
+ sig.setId("signature");
+
+ /*
+ * Add the Objects
*/
- System.out.println("Start signing");
- sig.sign(privateKey);
- System.out.println("Finished signing");
- }
-
- FileOutputStream f = new FileOutputStream(signatureFile);
-
- XMLUtils.outputDOMc14nWithComments(doc, f);
- f.close();
- System.out.println("Wrote signature to " + BaseURI);
-
- SignedInfo s = sig.getSignedInfo();
- for (int i=0; i<s.getLength(); i++) {
- Reference r = s.item(i);
- String fn = "merlin16_"+i+".html";
- System.out.println("Wrote Reference " + i + " to file " + fn);
- JavaUtils.writeBytesToFilename(fn, r.getHTMLRepresentation().getBytes());
- }
-
-
- /*
- for (int i=0; i<s.getSignedContentLength(); i++) {
- if (s.item(i).getType().equals(Reference.MANIFEST_URI)) {
- System.out.println("################ Signed Manifest " + i + " ################");
- } else {
- System.out.println("################ Signed Resource " + i + " ################");
- }
- System.out.println(new String(s.getSignedContentItem(i)));
- System.out.println();
- }
- */
- }
-
- /**
- * Method createObject4
- *
- * @param sig
- *
- * @throws Exception
- */
- public static Element createObject4(XMLSignature sig) throws Exception {
-
- Document doc = sig.getElement().getOwnerDocument();
- String BaseURI = sig.getBaseURI();
- Manifest manifest = new Manifest(doc);
- manifest.addResourceResolver(new OfflineResolver());
-
- manifest.setId("manifest-1");
- manifest.addDocument(BaseURI, "http://www.w3.org/TR/xml-stylesheet",
- null, Constants.ALGO_ID_DIGEST_SHA1,
- "manifest-reference-1", null);
- manifest.addDocument(BaseURI, "#reference-1", null,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Reference");
-
- //J-
- String xslt = ""
- + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n"
- + " xmlns='http://www.w3.org/TR/xhtml1/strict' \n"
- + " exclude-result-prefixes='foo' \n"
- + " version='1.0'>\n"
- + " <xsl:output encoding='UTF-8' \n"
- + " indent='no' \n"
- + " method='xml' />\n"
- + " <xsl:template match='/'>\n"
- + " <html>\n"
- + " <head>\n"
- + " <title>Notaries</title>\n"
- + " </head>\n"
- + " <body>\n"
- + " <table>\n"
- + " <xsl:for-each select='Notaries/Notary'>\n"
- + " <tr>\n"
- + " <th>\n"
- + " <xsl:value-of select='@name' />\n"
- + " </th>\n"
- + " </tr>\n"
- + " </xsl:for-each>\n"
- + " </table>\n"
- + " </body>\n"
- + " </html>\n"
- + " </xsl:template>\n"
- + "</xsl:stylesheet>\n"
- ;
- //J+
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- dbf.setNamespaceAware(true);
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
- org.w3c.dom.Document docxslt =
- db.parse(new ByteArrayInputStream(xslt.getBytes()));
- Node xslElem = docxslt.getDocumentElement();
- Node xslElemImported = doc.importNode(xslElem, true);
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_XSLT,
- (Element) xslElemImported);
- manifest.addDocument(BaseURI, "#notaries", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null, null);
-
- return manifest.getElement();
- }
-
- static {
- org.apache.xml.security.Init.init();
- }
+ // object-1
+ {
+ ObjectContainer object1 = new ObjectContainer(doc);
+
+ object1.setId("object-1");
+ object1.setMimeType("text/plain");
+ object1.appendChild(doc.createTextNode("I am the text."));
+ sig.appendObject(object1);
+ }
+
+ // object-2
+ {
+ ObjectContainer object2 = new ObjectContainer(doc);
+
+ object2.setId("object-2");
+ object2.setMimeType("text/plain");
+ object2.setEncoding("http://www.w3.org/2000/09/xmldsig#base64");
+ object2.appendChild(doc.createTextNode("SSBhbSB0aGUgdGV4dC4="));
+ sig.appendObject(object2);
+ }
+
+ // object-3
+ {
+ ObjectContainer object = new ObjectContainer(doc);
+
+ object.setId("object-3");
+
+ Element nonc = doc.createElementNS(null, "NonCommentandus");
+
+ nonc.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
+ nonc.appendChild(doc.createComment(" Commentandum "));
+ object.appendChild(doc.createTextNode("\n "));
+ object.appendChild(nonc);
+ object.appendChild(doc.createTextNode("\n "));
+ sig.appendObject(object);
+ }
+
+ // object number 4
+ {
+ ObjectContainer object = new ObjectContainer(doc);
+
+ object.appendChild(createObject4(sig));
+ sig.appendObject(object);
+ }
+
+ // object number 4
+ {
+ ObjectContainer object = new ObjectContainer(doc);
+ SignatureProperties sps = new SignatureProperties(doc);
+
+ sps.setId("signature-properties-1");
+
+ SignatureProperty sp = new SignatureProperty(doc, "#signature");
+ Element signedAdress = doc.createElementNS("urn:demo",
+ "SignedAddress");
+
+ signedAdress.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "urn:demo");
+
+ Element IP = doc.createElementNS("urn:demo", "IP");
+
+ IP.appendChild(doc.createTextNode("192.168.21.138"));
+ signedAdress.appendChild(IP);
+ sp.appendChild(signedAdress);
+ sps.addSignatureProperty(sp);
+ object.appendChild(sps.getElement());
+ sig.appendObject(object);
+ }
+
+ {
+ ObjectContainer object = new ObjectContainer(doc);
+
+ object.setId("object-4");
+
+ X509Data x509data = new X509Data(doc);
+
+ x509data.add(new XMLX509SubjectName(doc, cert));
+ x509data.add(new XMLX509IssuerSerial(doc, cert));
+ x509data.add(new XMLX509Certificate(doc, cert));
+ object.appendChild(x509data.getElement());
+ sig.appendObject(object);
+ }
+
+ /*
+ * Add References
+ */
+ sig.getSignedInfo().addResourceResolver(
+ new org.apache.xml.security.samples.utils.resolver.OfflineResolver());
+ sig.addDocument("http://www.w3.org/TR/xml-stylesheet");
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_BASE64_DECODE);
+ sig.addDocument("http://xmldsig.pothole.com/xml-stylesheet.txt",
+ transforms, Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+ XPathContainer xpathC = new XPathContainer(doc);
+
+ xpathC.setXPath("self::text()");
+ transforms.addTransform(Transforms.TRANSFORM_XPATH,
+ xpathC.getElementPlusReturns());
+ sig.addDocument("#object-1", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_BASE64_DECODE);
+ sig.addDocument("#object-2", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ sig.addDocument("#manifest-1", null, Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Manifest");
+ sig.addDocument("#signature-properties-1", null,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#SignatureProperties");
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ sig.addDocument("#xpointer(/)", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("#xpointer(/)", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ sig.addDocument("#object-3", null, Constants.ALGO_ID_DIGEST_SHA1,
+ null, "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("#object-3", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ sig.addDocument("#xpointer(id('object-3'))", null,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("#xpointer(id('object-3'))", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ sig.addDocument("#manifest-reference-1", null,
+ Constants.ALGO_ID_DIGEST_SHA1, "reference-1",
+ "http://www.w3.org/2000/09/xmldsig#Reference");
+ }
+
+ {
+ sig.addDocument("#reference-1", null,
+ Constants.ALGO_ID_DIGEST_SHA1, "reference-2",
+ "http://www.w3.org/2000/09/xmldsig#Reference");
+ }
+
+ {
+ sig.addDocument("#reference-2", null,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Reference");
+ }
+
+ /*
+ * Add KeyInfo and sign()
+ */
+ {
+ Transforms retrievalTransforms = new Transforms(doc);
+ XPathContainer xpathC = new XPathContainer(doc);
+
+ xpathC.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
+ xpathC.setXPath("ancestor-or-self::ds:X509Data");
+ retrievalTransforms.addTransform(Transforms.TRANSFORM_XPATH, xpathC.getElement());
+ sig.getKeyInfo().add(
+ new RetrievalMethod(
+ doc, "#object-4", retrievalTransforms, "http://www.w3.org/2000/09/xmldsig#X509Data"));
+
+ System.out.println("Start signing");
+ sig.sign(privateKey);
+ System.out.println("Finished signing");
+ }
+
+ FileOutputStream f = new FileOutputStream(signatureFile);
+
+ XMLUtils.outputDOMc14nWithComments(doc, f);
+ f.close();
+ System.out.println("Wrote signature to " + BaseURI);
+
+ SignedInfo s = sig.getSignedInfo();
+ for (int i = 0; i < s.getLength(); i++) {
+ Reference r = s.item(i);
+ String fn = "merlin16_"+i+".html";
+ System.out.println("Wrote Reference " + i + " to file " + fn);
+ JavaUtils.writeBytesToFilename(fn, r.getHTMLRepresentation().getBytes());
+ }
+
+ }
+
+ /**
+ * Method createObject4
+ *
+ * @param sig
+ *
+ * @throws Exception
+ */
+ public static Element createObject4(XMLSignature sig) throws Exception {
+
+ Document doc = sig.getElement().getOwnerDocument();
+ String BaseURI = sig.getBaseURI();
+ Manifest manifest = new Manifest(doc);
+ manifest.addResourceResolver(new OfflineResolver());
+
+ manifest.setId("manifest-1");
+ manifest.addDocument(BaseURI, "http://www.w3.org/TR/xml-stylesheet",
+ null, Constants.ALGO_ID_DIGEST_SHA1,
+ "manifest-reference-1", null);
+ manifest.addDocument(BaseURI, "#reference-1", null,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Reference");
+
+ String xslt = ""
+ + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n"
+ + " xmlns='http://www.w3.org/TR/xhtml1/strict' \n"
+ + " exclude-result-prefixes='foo' \n"
+ + " version='1.0'>\n"
+ + " <xsl:output encoding='UTF-8' \n"
+ + " indent='no' \n"
+ + " method='xml' />\n"
+ + " <xsl:template match='/'>\n"
+ + " <html>\n"
+ + " <head>\n"
+ + " <title>Notaries</title>\n"
+ + " </head>\n"
+ + " <body>\n"
+ + " <table>\n"
+ + " <xsl:for-each select='Notaries/Notary'>\n"
+ + " <tr>\n"
+ + " <th>\n"
+ + " <xsl:value-of select='@name' />\n"
+ + " </th>\n"
+ + " </tr>\n"
+ + " </xsl:for-each>\n"
+ + " </table>\n"
+ + " </body>\n"
+ + " </html>\n"
+ + " </xsl:template>\n"
+ + "</xsl:stylesheet>\n"
+ ;
+
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ dbf.setNamespaceAware(true);
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+ org.w3c.dom.Document docxslt =
+ db.parse(new ByteArrayInputStream(xslt.getBytes()));
+ Node xslElem = docxslt.getDocumentElement();
+ Node xslElemImported = doc.importNode(xslElem, true);
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_XSLT,
+ (Element) xslElemImported);
+ manifest.addDocument(BaseURI, "#notaries", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null, null);
+
+ return manifest.getElement();
+ }
+
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateMerlinsExampleTwentyThree.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateMerlinsExampleTwentyThree.java?rev=1063768&r1=1063767&r2=1063768&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateMerlinsExampleTwentyThree.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateMerlinsExampleTwentyThree.java Wed Jan 26 16:00:40 2011
@@ -16,8 +16,6 @@
*/
package org.apache.xml.security.samples.signature;
-
-
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
@@ -49,485 +47,432 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
-
/**
*
* @author $Author$
*/
public class CreateMerlinsExampleTwentyThree {
-
- /** {@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(CreateMerlinsExampleTwentyThree.class.getName());
- /**
- * Method main
- *
- * @param unused
- * @throws Exception
- */
- public static void main(String unused[]) throws Exception {
- Constants.setSignatureSpecNSprefix("ds");
- //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("merlinsTwentyThreeRecreatedNoRetrievalMethod.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());
-
- if (privateKey == null) {
- throw new RuntimeException("Private key is null");
- }
-
- X509Certificate cert =
- (X509Certificate) ks.getCertificate(certificateAlias);
- 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();
-
- //////////////////////////////////////////////////
- Element envelope = doc.createElementNS("http://www.usps.gov/",
- "Envelope");
-
- envelope.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "http://www.usps.gov/");
- envelope.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:foo", "http://www.usps.gov/foo");
- envelope.appendChild(doc.createTextNode("\n"));
- doc.appendChild(doc.createComment(" Preamble "));
- doc.appendChild(envelope);
- doc.appendChild(doc.createComment(" Postamble "));
-
- Element dearSir = doc.createElementNS("http://www.usps.gov/", "DearSir");
-
- dearSir.appendChild(doc.createTextNode("foo"));
- envelope.appendChild(dearSir);
- envelope.appendChild(doc.createTextNode("\n"));
-
- Element body = doc.createElementNS("http://www.usps.gov/", "Body");
-
- body.appendChild(doc.createTextNode("bar"));
- envelope.appendChild(body);
- envelope.appendChild(doc.createTextNode("\n"));
-
- Element YoursSincerely = doc.createElementNS("http://www.usps.gov/",
- "YoursSincerely");
- YoursSincerely.appendChild(doc.createTextNode("\n"));
-
- envelope.appendChild(YoursSincerely);
-
- Element PostScript = doc.createElementNS("http://www.usps.gov/",
- "PostScript");
-
- PostScript.appendChild(doc.createTextNode("bar"));
- envelope.appendChild(PostScript);
-
- Element Notaries = doc.createElementNS(null, "Notaries");
-
- Notaries.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
- Notaries.setAttributeNS(null, "Id", "notaries");
- IdResolver.registerElementById(Notaries, "Id");
-
- {
- Element Notary = doc.createElementNS(null, "Notary");
-
- Notary.setAttributeNS(null, "name", "Great, A. T.");
- Notaries.appendChild(Notary);
- }
-
- {
- Element Notary = doc.createElementNS(null, "Notary");
-
- Notary.setAttributeNS(null, "name", "Hun, A. T.");
- Notaries.appendChild(Notary);
- }
-
- envelope.appendChild(Notaries);
- envelope.appendChild(doc.createComment(" Commentary "));
-
- //////////////////////////////////////////////////
- String BaseURI = signatureFile.toURL().toString();
- XMLSignature sig = new XMLSignature(doc, BaseURI,
- XMLSignature.ALGO_ID_SIGNATURE_DSA);
-
- YoursSincerely.appendChild(sig.getElement());
- sig.setId("signature");
-
- /*
- * Add the Objects
- */
-
- // object-1
- {
- ObjectContainer object1 = new ObjectContainer(doc);
-
- object1.setId("object-1");
- object1.setMimeType("text/plain");
- object1.appendChild(doc.createTextNode("I am the text."));
- sig.appendObject(object1);
- }
-
- // object-2
- {
- ObjectContainer object2 = new ObjectContainer(doc);
-
- object2.setId("object-2");
- object2.setMimeType("text/plain");
- object2.setEncoding("http://www.w3.org/2000/09/xmldsig#base64");
- object2.appendChild(doc.createTextNode("SSBhbSB0aGUgdGV4dC4="));
- sig.appendObject(object2);
- }
-
- // object-3
- {
- ObjectContainer object = new ObjectContainer(doc);
-
- object.setId("object-3");
-
- Element nonc = doc.createElementNS(null, "NonCommentandus");
-
- nonc.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
- nonc.appendChild(doc.createComment(" Commentandum "));
- object.appendChild(doc.createTextNode("\n "));
- object.appendChild(nonc);
- object.appendChild(doc.createTextNode("\n "));
- sig.appendObject(object);
- }
-
- // object number 4
- {
- ObjectContainer object = new ObjectContainer(doc);
-
- object.appendChild(createObject4(sig));
- sig.appendObject(object);
- }
-
- // object number 4
- {
- ObjectContainer object = new ObjectContainer(doc);
- SignatureProperties sps = new SignatureProperties(doc);
-
- sps.setId("signature-properties-1");
-
- SignatureProperty sp = new SignatureProperty(doc, "#signature");
- Element signedAdress = doc.createElementNS("urn:demo",
- "SignedAddress");
-
- signedAdress.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "urn:demo");
-
- Element IP = doc.createElementNS("urn:demo", "IP");
-
- IP.appendChild(doc.createTextNode("192.168.21.138"));
- signedAdress.appendChild(IP);
- sp.appendChild(signedAdress);
- sps.addSignatureProperty(sp);
- object.appendChild(sps.getElement());
- sig.appendObject(object);
- }
-
- {
- ObjectContainer object = new ObjectContainer(doc);
-
- object.setId("object-4");
-
- X509Data x509data = new X509Data(doc);
-
- x509data.add(new XMLX509SubjectName(doc, cert));
- x509data.add(new XMLX509IssuerSerial(doc, cert));
- x509data.add(new XMLX509Certificate(doc, cert));
- object.appendChild(x509data.getElement());
- sig.appendObject(object);
- }
-
- /*
- * Add References
- */
- sig.getSignedInfo()
- .addResourceResolver(new org.apache.xml.security.samples.utils.resolver
- .OfflineResolver());
- sig.addDocument("http://www.w3.org/TR/xml-stylesheet");
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_BASE64_DECODE);
- sig.addDocument("http://xmldsig.pothole.com/xml-stylesheet.txt",
- transforms, Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- Transforms transforms = new Transforms(doc);
- XPathContainer xpathC = new XPathContainer(doc);
-
- xpathC.setXPath("self::text()");
- transforms.addTransform(Transforms.TRANSFORM_XPATH,
- xpathC.getElementPlusReturns());
- sig.addDocument("#object-1", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
- /*
- {
- Transforms transforms = new Transforms(doc);
- XPathContainer xpathC = new XPathContainer(doc);
-
- //J-
- xpathC.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
- xpathC.setXPath("\n"
- + " ancestor-or-self::ds:SignedInfo " + "\n"
- + " and " + "\n"
- + " count(ancestor-or-self::ds:Reference | " + "\n"
- + " here()/ancestor::ds:Reference[1]) > " + "\n"
- + " count(ancestor-or-self::ds:Reference) " + "\n"
- + " or " + "\n"
- + " count(ancestor-or-self::node() | " + "\n"
- + " id('notaries')) = " + "\n"
- + " count(ancestor-or-self::node()) " + "\n");
- //J+
- transforms.addTransform(Transforms.TRANSFORM_XPATH,
- xpathC.getElementPlusReturns());
- sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
- */
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_BASE64_DECODE);
- sig.addDocument("#object-2", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- sig.addDocument("#manifest-1", null, Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Manifest");
- sig.addDocument("#signature-properties-1", null,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#SignatureProperties");
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- sig.addDocument("#xpointer(/)", transforms,
- Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("#xpointer(/)", transforms,
- Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- sig.addDocument("#object-3", null, Constants.ALGO_ID_DIGEST_SHA1,
- null, "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("#object-3", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- {
- sig.addDocument("#xpointer(id('object-3'))", null,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- {
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("#xpointer(id('object-3'))", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
- }
-
- {
- sig.addDocument("#manifest-reference-1", null,
- Constants.ALGO_ID_DIGEST_SHA1, "reference-1",
- "http://www.w3.org/2000/09/xmldsig#Reference");
- }
-
- {
- sig.addDocument("#reference-1", null,
- Constants.ALGO_ID_DIGEST_SHA1, "reference-2",
- "http://www.w3.org/2000/09/xmldsig#Reference");
- }
-
- {
- sig.addDocument("#reference-2", null,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Reference");
- }
-
- /*
- * Add KeyInfo and sign()
- */
- {
- Transforms retrievalTransforms = new Transforms(doc);
- XPathContainer xpathC = new XPathContainer(doc);
-
- xpathC.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
- xpathC.setXPath("ancestor-or-self::ds:X509Data");
- retrievalTransforms.addTransform(Transforms.TRANSFORM_XPATH,
- xpathC.getElement());
- sig.getKeyInfo().add(
- new RetrievalMethod(
- doc, "#object-4", retrievalTransforms,
- "http://www.w3.org/2000/09/xmldsig#X509Data"));
-
- /*
- X509Data x509data = new X509Data(doc);
-
- x509data.add(new XMLX509SubjectName(doc, cert));
- x509data.add(new XMLX509IssuerSerial(doc, cert));
- x509data.add(new XMLX509Certificate(doc, cert));
- sig.getKeyInfo().add(x509data);
+ static {
+ org.apache.xml.security.Init.init();
+ }
+
+ /**
+ * Method main
+ *
+ * @param unused
+ * @throws Exception
+ */
+ public static void main(String unused[]) throws Exception {
+ Constants.setSignatureSpecNSprefix("ds");
+
+ 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("merlinsTwentyThreeRecreatedNoRetrievalMethod.xml");
+
+ KeyStore ks = KeyStore.getInstance(keystoreType);
+ FileInputStream fis = new FileInputStream(keystoreFile);
+
+ ks.load(fis, keystorePass.toCharArray());
+
+ PrivateKey privateKey =
+ (PrivateKey) ks.getKey(privateKeyAlias, privateKeyPass.toCharArray());
+
+ if (privateKey == null) {
+ throw new RuntimeException("Private key is null");
+ }
+
+ X509Certificate cert =
+ (X509Certificate) ks.getCertificate(certificateAlias);
+ 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();
+
+ //////////////////////////////////////////////////
+ Element envelope = doc.createElementNS("http://www.usps.gov/", "Envelope");
+
+ envelope.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "http://www.usps.gov/");
+ envelope.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:foo", "http://www.usps.gov/foo");
+ envelope.appendChild(doc.createTextNode("\n"));
+ doc.appendChild(doc.createComment(" Preamble "));
+ doc.appendChild(envelope);
+ doc.appendChild(doc.createComment(" Postamble "));
+
+ Element dearSir = doc.createElementNS("http://www.usps.gov/", "DearSir");
+
+ dearSir.appendChild(doc.createTextNode("foo"));
+ envelope.appendChild(dearSir);
+ envelope.appendChild(doc.createTextNode("\n"));
+
+ Element body = doc.createElementNS("http://www.usps.gov/", "Body");
+
+ body.appendChild(doc.createTextNode("bar"));
+ envelope.appendChild(body);
+ envelope.appendChild(doc.createTextNode("\n"));
+
+ Element YoursSincerely = doc.createElementNS("http://www.usps.gov/", "YoursSincerely");
+ YoursSincerely.appendChild(doc.createTextNode("\n"));
+
+ envelope.appendChild(YoursSincerely);
+
+ Element PostScript = doc.createElementNS("http://www.usps.gov/", "PostScript");
+
+ PostScript.appendChild(doc.createTextNode("bar"));
+ envelope.appendChild(PostScript);
+
+ Element Notaries = doc.createElementNS(null, "Notaries");
+
+ Notaries.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
+ Notaries.setAttributeNS(null, "Id", "notaries");
+ IdResolver.registerElementById(Notaries, "Id");
+
+ {
+ Element Notary = doc.createElementNS(null, "Notary");
+
+ Notary.setAttributeNS(null, "name", "Great, A. T.");
+ Notaries.appendChild(Notary);
+ }
+
+ {
+ Element Notary = doc.createElementNS(null, "Notary");
+
+ Notary.setAttributeNS(null, "name", "Hun, A. T.");
+ Notaries.appendChild(Notary);
+ }
+
+ envelope.appendChild(Notaries);
+ envelope.appendChild(doc.createComment(" Commentary "));
+
+ //////////////////////////////////////////////////
+ String BaseURI = signatureFile.toURI().toURL().toString();
+ XMLSignature sig = new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_SIGNATURE_DSA);
+
+ YoursSincerely.appendChild(sig.getElement());
+ sig.setId("signature");
+
+ /*
+ * Add the Objects
+ */
+
+ // object-1
+ {
+ ObjectContainer object1 = new ObjectContainer(doc);
+
+ object1.setId("object-1");
+ object1.setMimeType("text/plain");
+ object1.appendChild(doc.createTextNode("I am the text."));
+ sig.appendObject(object1);
+ }
+
+ // object-2
+ {
+ ObjectContainer object2 = new ObjectContainer(doc);
+
+ object2.setId("object-2");
+ object2.setMimeType("text/plain");
+ object2.setEncoding("http://www.w3.org/2000/09/xmldsig#base64");
+ object2.appendChild(doc.createTextNode("SSBhbSB0aGUgdGV4dC4="));
+ sig.appendObject(object2);
+ }
+
+ // object-3
+ {
+ ObjectContainer object = new ObjectContainer(doc);
+
+ object.setId("object-3");
+
+ Element nonc = doc.createElementNS(null, "NonCommentandus");
+
+ nonc.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
+ nonc.appendChild(doc.createComment(" Commentandum "));
+ object.appendChild(doc.createTextNode("\n "));
+ object.appendChild(nonc);
+ object.appendChild(doc.createTextNode("\n "));
+ sig.appendObject(object);
+ }
+
+ // object number 4
+ {
+ ObjectContainer object = new ObjectContainer(doc);
+
+ object.appendChild(createObject4(sig));
+ sig.appendObject(object);
+ }
+
+ // object number 4
+ {
+ ObjectContainer object = new ObjectContainer(doc);
+ SignatureProperties sps = new SignatureProperties(doc);
+
+ sps.setId("signature-properties-1");
+
+ SignatureProperty sp = new SignatureProperty(doc, "#signature");
+ Element signedAdress = doc.createElementNS("urn:demo", "SignedAddress");
+
+ signedAdress.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "urn:demo");
+
+ Element IP = doc.createElementNS("urn:demo", "IP");
+
+ IP.appendChild(doc.createTextNode("192.168.21.138"));
+ signedAdress.appendChild(IP);
+ sp.appendChild(signedAdress);
+ sps.addSignatureProperty(sp);
+ object.appendChild(sps.getElement());
+ sig.appendObject(object);
+ }
+
+ {
+ ObjectContainer object = new ObjectContainer(doc);
+
+ object.setId("object-4");
+
+ X509Data x509data = new X509Data(doc);
+
+ x509data.add(new XMLX509SubjectName(doc, cert));
+ x509data.add(new XMLX509IssuerSerial(doc, cert));
+ x509data.add(new XMLX509Certificate(doc, cert));
+ object.appendChild(x509data.getElement());
+ sig.appendObject(object);
+ }
+
+ /*
+ * Add References
*/
+ sig.getSignedInfo().addResourceResolver(
+ new org.apache.xml.security.samples.utils.resolver.OfflineResolver());
+ sig.addDocument("http://www.w3.org/TR/xml-stylesheet");
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_BASE64_DECODE);
+ sig.addDocument("http://xmldsig.pothole.com/xml-stylesheet.txt",
+ transforms, Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+ XPathContainer xpathC = new XPathContainer(doc);
+
+ xpathC.setXPath("self::text()");
+ transforms.addTransform(Transforms.TRANSFORM_XPATH,
+ xpathC.getElementPlusReturns());
+ sig.addDocument("#object-1", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_BASE64_DECODE);
+ sig.addDocument("#object-2", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ sig.addDocument("#manifest-1", null, Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Manifest");
+ sig.addDocument("#signature-properties-1", null,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#SignatureProperties");
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ sig.addDocument("#xpointer(/)", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("#xpointer(/)", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ sig.addDocument("#object-3", null, Constants.ALGO_ID_DIGEST_SHA1,
+ null, "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("#object-3", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ sig.addDocument("#xpointer(id('object-3'))", null,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("#xpointer(id('object-3'))", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object");
+ }
+
+ {
+ sig.addDocument("#manifest-reference-1", null,
+ Constants.ALGO_ID_DIGEST_SHA1, "reference-1",
+ "http://www.w3.org/2000/09/xmldsig#Reference");
+ }
+
+ {
+ sig.addDocument("#reference-1", null,
+ Constants.ALGO_ID_DIGEST_SHA1, "reference-2",
+ "http://www.w3.org/2000/09/xmldsig#Reference");
+ }
+
+ {
+ sig.addDocument("#reference-2", null,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Reference");
+ }
+
+ /*
+ * Add KeyInfo and sign()
+ */
+ {
+ Transforms retrievalTransforms = new Transforms(doc);
+ XPathContainer xpathC = new XPathContainer(doc);
+
+ xpathC.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
+ xpathC.setXPath("ancestor-or-self::ds:X509Data");
+ retrievalTransforms.addTransform(Transforms.TRANSFORM_XPATH,
+ xpathC.getElement());
+ sig.getKeyInfo().add(
+ new RetrievalMethod(
+ doc, "#object-4", retrievalTransforms, "http://www.w3.org/2000/09/xmldsig#X509Data"));
+
+ System.out.println("Start signing");
+ sig.sign(privateKey);
+ System.out.println("Finished signing");
+ }
+
+ FileOutputStream f = new FileOutputStream(signatureFile);
+
+ XMLUtils.outputDOMc14nWithComments(doc, f);
+ f.close();
+ System.out.println("Wrote signature to " + BaseURI);
+
+ SignedInfo s = sig.getSignedInfo();
+ for (int i = 0; i < s.getLength(); i++) {
+ Reference r = s.item(i);
+ String fn = "merlin16_"+i+".html";
+ System.out.println("Wrote Reference " + i + " to file " + fn);
+ JavaUtils.writeBytesToFilename(fn, r.getHTMLRepresentation().getBytes());
+ }
+
+ }
+
+ /**
+ * Method createObject4
+ *
+ * @param sig
+ *
+ * @throws Exception
+ */
+ public static Element createObject4(XMLSignature sig) throws Exception {
+
+ Document doc = sig.getElement().getOwnerDocument();
+ String BaseURI = sig.getBaseURI();
+ Manifest manifest = new Manifest(doc);
+ manifest.addResourceResolver(new OfflineResolver());
+
+ manifest.setId("manifest-1");
+ manifest.addDocument(BaseURI, "http://www.w3.org/TR/xml-stylesheet",
+ null, Constants.ALGO_ID_DIGEST_SHA1,
+ "manifest-reference-1", null);
+ manifest.addDocument(BaseURI, "#reference-1", null,
+ Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Reference");
+
+ String xslt = ""
+ + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n"
+ + " xmlns='http://www.w3.org/TR/xhtml1/strict' \n"
+ + " exclude-result-prefixes='foo' \n"
+ + " version='1.0'>\n"
+ + " <xsl:output encoding='UTF-8' \n"
+ + " indent='no' \n"
+ + " method='xml' />\n"
+ + " <xsl:template match='/'>\n"
+ + " <html>\n"
+ + " <head>\n"
+ + " <title>Notaries</title>\n"
+ + " </head>\n"
+ + " <body>\n"
+ + " <table>\n"
+ + " <xsl:for-each select='Notaries/Notary'>\n"
+ + " <tr>\n"
+ + " <th>\n"
+ + " <xsl:value-of select='@name' />\n"
+ + " </th>\n"
+ + " </tr>\n"
+ + " </xsl:for-each>\n"
+ + " </table>\n"
+ + " </body>\n"
+ + " </html>\n"
+ + " </xsl:template>\n"
+ + "</xsl:stylesheet>\n"
+ ;
+
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ dbf.setNamespaceAware(true);
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+ org.w3c.dom.Document docxslt =
+ db.parse(new ByteArrayInputStream(xslt.getBytes()));
+ Node xslElem = docxslt.getDocumentElement();
+ Node xslElemImported = doc.importNode(xslElem, true);
+ Transforms transforms = new Transforms(doc);
+
+ transforms.addTransform(Transforms.TRANSFORM_XSLT,
+ (Element) xslElemImported);
+ manifest.addDocument(BaseURI, "#notaries", transforms,
+ Constants.ALGO_ID_DIGEST_SHA1, null, null);
+
+ return manifest.getElement();
+ }
- System.out.println("Start signing");
- sig.sign(privateKey);
- System.out.println("Finished signing");
- }
-
- FileOutputStream f = new FileOutputStream(signatureFile);
-
- XMLUtils.outputDOMc14nWithComments(doc, f);
- f.close();
- System.out.println("Wrote signature to " + BaseURI);
-
- SignedInfo s = sig.getSignedInfo();
- for (int i=0; i<s.getLength(); i++) {
- Reference r = s.item(i);
- String fn = "merlin16_"+i+".html";
- System.out.println("Wrote Reference " + i + " to file " + fn);
- JavaUtils.writeBytesToFilename(fn, r.getHTMLRepresentation().getBytes());
- }
-
-
- /*
- for (int i=0; i<s.getSignedContentLength(); i++) {
- if (s.item(i).getType().equals(Reference.MANIFEST_URI)) {
- System.out.println("################ Signed Manifest " + i + " ################");
- } else {
- System.out.println("################ Signed Resource " + i + " ################");
- }
- System.out.println(new String(s.getSignedContentItem(i)));
- System.out.println();
- }
- */
- }
-
- /**
- * Method createObject4
- *
- * @param sig
- *
- * @throws Exception
- */
- public static Element createObject4(XMLSignature sig) throws Exception {
-
- Document doc = sig.getElement().getOwnerDocument();
- String BaseURI = sig.getBaseURI();
- Manifest manifest = new Manifest(doc);
- manifest.addResourceResolver(new OfflineResolver());
-
- manifest.setId("manifest-1");
- manifest.addDocument(BaseURI, "http://www.w3.org/TR/xml-stylesheet",
- null, Constants.ALGO_ID_DIGEST_SHA1,
- "manifest-reference-1", null);
- manifest.addDocument(BaseURI, "#reference-1", null,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Reference");
-
- //J-
- String xslt = ""
- + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n"
- + " xmlns='http://www.w3.org/TR/xhtml1/strict' \n"
- + " exclude-result-prefixes='foo' \n"
- + " version='1.0'>\n"
- + " <xsl:output encoding='UTF-8' \n"
- + " indent='no' \n"
- + " method='xml' />\n"
- + " <xsl:template match='/'>\n"
- + " <html>\n"
- + " <head>\n"
- + " <title>Notaries</title>\n"
- + " </head>\n"
- + " <body>\n"
- + " <table>\n"
- + " <xsl:for-each select='Notaries/Notary'>\n"
- + " <tr>\n"
- + " <th>\n"
- + " <xsl:value-of select='@name' />\n"
- + " </th>\n"
- + " </tr>\n"
- + " </xsl:for-each>\n"
- + " </table>\n"
- + " </body>\n"
- + " </html>\n"
- + " </xsl:template>\n"
- + "</xsl:stylesheet>\n"
- ;
- //J+
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- dbf.setNamespaceAware(true);
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
- org.w3c.dom.Document docxslt =
- db.parse(new ByteArrayInputStream(xslt.getBytes()));
- Node xslElem = docxslt.getDocumentElement();
- Node xslElemImported = doc.importNode(xslElem, true);
- Transforms transforms = new Transforms(doc);
-
- transforms.addTransform(Transforms.TRANSFORM_XSLT,
- (Element) xslElemImported);
- manifest.addDocument(BaseURI, "#notaries", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null, null);
-
- return manifest.getElement();
- }
-
- static {
- org.apache.xml.security.Init.init();
- }
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateNullURIReference.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateNullURIReference.java?rev=1063768&r1=1063767&r2=1063768&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateNullURIReference.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateNullURIReference.java Wed Jan 26 16:00:40 2011
@@ -16,8 +16,6 @@
*/
package org.apache.xml.security.samples.signature;
-
-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -34,82 +32,79 @@ import org.apache.xml.security.utils.XML
* Class CreateNullURIReference
*
* @author $Author$
- * @version $Revision$
*/
public class CreateNullURIReference {
- /** {@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(
- CreateNullURIReference.class.getName());
+ org.apache.commons.logging.LogFactory.getLog(CreateNullURIReference.class.getName());
+
+ 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("signature.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.toURL().toString();
+
+ Constants.setSignatureSpecNSprefix(null);
+
+ XMLSignature sig =
+ new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_SIGNATURE_DSA);
+ byte[][] memoryData = {
+ "The secret data".getBytes(), "dataset 2".getBytes(),
+ };
+
+ sig.addResourceResolver(new NullURIReferenceResolver(memoryData));
+ doc.appendChild(sig.getElement());
+
+ {
+ sig.addDocument(null, null, Constants.ALGO_ID_DIGEST_SHA1);
+ sig.addDocument(null, null, 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");
+ }
+
+ FileOutputStream f = new FileOutputStream(signatureFile);
+
+ XMLUtils.outputDOMc14nWithComments(doc, f);
+ f.close();
+ System.out.println("Wrote signature to " + BaseURI);
+ }
- /**
- * 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("signature.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(null);
-
- XMLSignature sig = new XMLSignature(doc, BaseURI,
- XMLSignature.ALGO_ID_SIGNATURE_DSA);
- byte[][] memoryData = {
- "The secret data".getBytes(), "dataset 2".getBytes(),
- };
-
- sig.addResourceResolver(new NullURIReferenceResolver(memoryData));
- doc.appendChild(sig.getElement());
-
- {
- sig.addDocument(null, null, Constants.ALGO_ID_DIGEST_SHA1);
- sig.addDocument(null, null, 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");
- }
-
- 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();
- }
}
Modified: santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateSignature.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateSignature.java?rev=1063768&r1=1063767&r2=1063768&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateSignature.java (original)
+++ santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/CreateSignature.java Wed Jan 26 16:00:40 2011
@@ -16,8 +16,6 @@
*/
package org.apache.xml.security.samples.signature;
-
-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -33,142 +31,140 @@ import org.w3c.dom.Element;
/**
- *
- *
* @author $Author$
*/
public class CreateSignature {
- /** {@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(CreateSignature.class.getName());
- /**
- * Method main
- *
- * @param unused
- * @throws Exception
- */
- public static void main(String unused[]) throws Exception {
- Constants.setSignatureSpecNSprefix("ds");
-
- //J-
- //All the parameters for the keystore
- 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("signature.xml");
- //J+
- KeyStore ks = KeyStore.getInstance(keystoreType);
- FileInputStream fis = new FileInputStream(keystoreFile);
-
- //load the keystore
- ks.load(fis, keystorePass.toCharArray());
-
- //get the private key for signing.
- PrivateKey privateKey = (PrivateKey) ks.getKey(privateKeyAlias,
- privateKeyPass.toCharArray());
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- //XML Signature needs to be namespace aware
- dbf.setNamespaceAware(true);
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
- org.w3c.dom.Document doc = db.newDocument();
-
- //Build a sample document. It will look something like:
- //<!-- Comment before -->
- //<apache:RootElement xmlns:apache="http://www.apache.org/ns/#app1">Some simple text
- //</apache:RootElement>
- //<!-- Comment after -->
- doc.appendChild(doc.createComment(" Comment before "));
-
- Element root = doc.createElementNS("http://www.apache.org/ns/#app1",
- "apache:RootElement");
-
- root.setAttributeNS(null, "attr1", "test1");
- root.setAttributeNS(null, "attr2", "test2");
- root.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:foo", "http://example.org/#foo");
- root.setAttributeNS("http://example.org/#foo", "foo:attr1", "foo's test");
-
-
-
- root.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:apache", "http://www.apache.org/ns/#app1");
- doc.appendChild(root);
- root.appendChild(doc.createTextNode("Some simple text\n"));
-
- //The BaseURI is the URI that's used to prepend to relative URIs
- String BaseURI = signatureFile.toURL().toString();
- //Create an XML Signature object from the document, BaseURI and
- //signature algorithm (in this case DSA)
- XMLSignature sig = new XMLSignature(doc, BaseURI,
- XMLSignature.ALGO_ID_SIGNATURE_DSA);
-
-
- //Append the signature element to the root element before signing because
- //this is going to be an enveloped signature.
- //This means the signature is going to be enveloped by the document.
- //Two other possible forms are enveloping where the document is inside the
- //signature and detached where they are seperate.
- //Note that they can be mixed in 1 signature with seperate references as
- //shown below.
- root.appendChild(sig.getElement());
- doc.appendChild(doc.createComment(" Comment after "));
- sig.getSignedInfo()
- .addResourceResolver(new org.apache.xml.security.samples.utils.resolver
- .OfflineResolver());
-
- {
- //create the transforms object for the Document/Reference
- Transforms transforms = new Transforms(doc);
-
- //First we have to strip away the signature element (it's not part of the
- //signature calculations). The enveloped transform can be used for this.
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- //Part of the signature element needs to be canonicalized. It is a kind
- //of normalizing algorithm for XML. For more information please take a
- //look at the W3C XML Digital Signature webpage.
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- //Add the above Document/Reference
- sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
- }
-
- {
- //Add in 2 external URIs. This is a detached Reference.
- //
- // When sign() is called, two network connections are made. -- well,
- // not really, as we use the OfflineResolver which acts as a proxy for
- // these two resouces ;-))
- //
- sig.addDocument("http://www.w3.org/TR/xml-stylesheet");
- sig.addDocument("http://www.nue.et-inf.uni-siegen.de/index.html");
- }
-
- {
- //Add in the KeyInfo for the certificate that we used the private key of
- 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");
- }
-
- 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 {
+ Constants.setSignatureSpecNSprefix("ds");
+
+ //All the parameters for the keystore
+ 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("signature.xml");
+
+ KeyStore ks = KeyStore.getInstance(keystoreType);
+ FileInputStream fis = new FileInputStream(keystoreFile);
+
+ //load the keystore
+ ks.load(fis, keystorePass.toCharArray());
+
+ //get the private key for signing.
+ PrivateKey privateKey =
+ (PrivateKey) ks.getKey(privateKeyAlias, privateKeyPass.toCharArray());
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ //XML Signature needs to be namespace aware
+ dbf.setNamespaceAware(true);
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+ org.w3c.dom.Document doc = db.newDocument();
+
+ //Build a sample document. It will look something like:
+ //<!-- Comment before -->
+ //<apache:RootElement xmlns:apache="http://www.apache.org/ns/#app1">Some simple text
+ //</apache:RootElement>
+ //<!-- Comment after -->
+ doc.appendChild(doc.createComment(" Comment before "));
+
+ Element root =
+ doc.createElementNS("http://www.apache.org/ns/#app1", "apache:RootElement");
+
+ root.setAttributeNS(null, "attr1", "test1");
+ root.setAttributeNS(null, "attr2", "test2");
+ root.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:foo", "http://example.org/#foo");
+ root.setAttributeNS("http://example.org/#foo", "foo:attr1", "foo's test");
+
+
+
+ root.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:apache", "http://www.apache.org/ns/#app1");
+ doc.appendChild(root);
+ root.appendChild(doc.createTextNode("Some simple text\n"));
+
+ //The BaseURI is the URI that's used to prepend to relative URIs
+ String BaseURI = signatureFile.toURI().toURL().toString();
+ //Create an XML Signature object from the document, BaseURI and
+ //signature algorithm (in this case DSA)
+ XMLSignature sig =
+ new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_SIGNATURE_DSA);
+
+
+ //Append the signature element to the root element before signing because
+ //this is going to be an enveloped signature.
+ //This means the signature is going to be enveloped by the document.
+ //Two other possible forms are enveloping where the document is inside the
+ //signature and detached where they are seperate.
+ //Note that they can be mixed in 1 signature with seperate references as
+ //shown below.
+ root.appendChild(sig.getElement());
+ doc.appendChild(doc.createComment(" Comment after "));
+ sig.getSignedInfo().addResourceResolver(
+ new org.apache.xml.security.samples.utils.resolver.OfflineResolver()
+ );
+
+ {
+ //create the transforms object for the Document/Reference
+ Transforms transforms = new Transforms(doc);
+
+ //First we have to strip away the signature element (it's not part of the
+ //signature calculations). The enveloped transform can be used for this.
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ //Part of the signature element needs to be canonicalized. It is a kind
+ //of normalizing algorithm for XML. For more information please take a
+ //look at the W3C XML Digital Signature webpage.
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ //Add the above Document/Reference
+ sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
+ }
+
+ {
+ //Add in 2 external URIs. This is a detached Reference.
+ //
+ // When sign() is called, two network connections are made. -- well,
+ // not really, as we use the OfflineResolver which acts as a proxy for
+ // these two resouces ;-))
+ //
+ sig.addDocument("http://www.w3.org/TR/xml-stylesheet");
+ sig.addDocument("http://www.nue.et-inf.uni-siegen.de/index.html");
+ }
+
+ {
+ //Add in the KeyInfo for the certificate that we used the private key of
+ 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");
+ }
+
+ 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/TestSignature.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/samples/org/apache/xml/security/samples/signature/TestSignature.java?rev=1063768&r1=1063767&r2=1063768&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 Wed Jan 26 16:00:40 2011
@@ -56,9 +56,9 @@ public class TestSignature {
//J-
String merlinsDir =
- "data/ie/baltimore/merlin-examples/merlin-xmldsig-eighteen/";
+ "samples/data/ie/baltimore/merlin-examples/merlin-xmldsig-eighteen/";
String ourDir =
- "data/org/apache/xml/security/temp/key/";
+ "samples/data/org/apache/xml/security/temp/key/";
String filenames[] = { merlinsDir +
/* 0 */ "signature-keyname.xml",
merlinsDir +