You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@santuario.apache.org by "Charlesworth, Chico" <Ch...@softwareag.co.uk> on 2003/02/11 17:27:42 UTC
Problem with signing XML document in two steps - Prepare XML docu
ment with SignedInfo, then sign document
Hi,
I've got a business requirement whereby I cannot sign a XML document in one
go. First I must "prepare" the XML document where the SignedInfo part of the
XML signature needs to be added to the document, which I can do no problem.
Then using the returned "prepared" document, I later need to sign it, but
this step fails with a nullpointer?? Is this a bug?
I can add the SignedInfo XML part to my XML document using the following
code:
public org.w3c.dom.Document prepare(org.w3c.dom.Document doc) throws
Exception {
XMLSignature sig = new XMLSignature(doc, "",
XMLSignature.ALGO_ID_SIGNATURE_DSA);
if ((doc.getParentNode() == null) && (doc.getChildNodes().getLength()
> 0)) {
// Append signature to root element
Element rootElmnt = (Element) doc.getChildNodes().item(0);
rootElmnt.appendChild(sig.getElement());
} else {
// Append signature to document
doc.appendChild(sig.getElement());
}
// Specify the transforms
Transforms transforms = new Transforms(doc);
transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
sig.addDocument("", transforms,
org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1);
// Generate Disgest Values
sig.getSignedInfo().generateDigestValues();
// Normally would now addKeyInfo(certificate) and
addKeyInfo(certificatePublicKey) and sign(privateKey),
// But want to do this in a separate step!!
return doc;
}
Then I try to sign the returned document from the method above with the
following code but this throws a nullpointer???
public org.w3c.dom.Document sign(org.w3c.dom.Document doc) throws Exception
{
// Get Signature Node
Node signatureNode =
XPathAPI.selectSingleNode(doc.getDocumentElement(),
Constants.XMLSignature.SIGNATURE_XPATH);
XMLSignature sig = new XMLSignature((Element) signatureNode, ""); //
Throws Nullpointer exception at this point
// Add certificate to signature
sig.addKeyInfo(cert);
sig.addKeyInfo(cert.getPublicKey());
// Sign Document
sig.sign(privateKey);
return doc;
}
Any ideas?
Chico.
ps. this is what the packages/classes I'm importing:
import java.io.*;
import java.security.*;
import java.security.cert.*;
import javax.xml.transform.TransformerException;
import javax.xml.parsers.*;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.apache.xml.security.algorithms.MessageDigestAlgorithm;
import org.apache.xml.security.c14n.*;
// import org.apache.xml.security.exceptions.XMLSecurityException;
import org.apache.xml.security.signature.*;
import org.apache.xml.security.keys.*;
import org.apache.xml.security.keys.content.*;
import org.apache.xml.security.keys.content.x509.*;
import org.apache.xml.security.keys.keyresolver.*;
import org.apache.xml.security.keys.storage.*;
import org.apache.xml.security.keys.storage.implementations.*;
import org.apache.xml.security.utils.XMLUtils;
import org.apache.xml.security.transforms.*;
import org.apache.xml.security.Init;
import org.apache.xml.serialize.*;
--
The content of this e-mail is confidential, may contain privileged material
and is intended solely for the recipient(s) named above. If you receive this
in error, please notify Software AG immediately and delete this e-mail.
Software AG (UK) Limited
Registered in England & Wales 1310740
Registered Office: Hudson House, Hudson Way,
Pride Park, Derby DE24 8HS