You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@santuario.apache.org by "Kenworthy, Edward (EDS)" <Ed...@ird.eds.com> on 2003/09/18 10:47:16 UTC

RE: Signing DOM trees

I need to sign a big message (25megs). I can sign 12meg messages with no
problems. However 25megs just don't work. Does anyone have any suggestions,
optimisations etc they could suggest that would help me push the envelope ?

This is the code I'm using

/**
     * Creates a signature
     *
     * @param xml2BeSigned	DOM tree of the XML to be signed
     * @param keystore	Path to the keystore-file
     * @param keyalias	Alias within given keystore for private key
     * @param keystorepwd	Password that protects the given keystore
     * @param privkeypwd	Password that protects the private key of
the user
     * @param certificatealias (Ignored) Alias of the certificate to include
in the KeyInfo-Element (can be null)
     * @param signset (Ignored) XPath to identifie the nodes to be signed
within an xml-document (can be null)
     */
    private org.w3c.dom.Document signDocument(Document xml2BeSigned, String
keystore,String keyalias,String keystorepwd,
        String privkeypwd,String certificatealias,String signset)
    {
        assert keystore != null;
        assert keyalias != null;
        assert keystorepwd != null;
        assert privkeypwd != null;
        assert xml2BeSigned != null;

        org.w3c.dom.Document result=null;
        try
        {
/*
*	First get the private key you're going to use for signing.
*/
            KeyStore ks = KeyStore.getInstance("JKS");
            FileInputStream fis = new FileInputStream(keystore);

            //load the keystore
            ks.load(fis, keystorepwd.toCharArray());

            //get the private key for signing.
            PrivateKey privateKey = (PrivateKey)
ks.getKey(keyalias,privkeypwd.toCharArray());

/*
*	Create your XMLSignature object. We don't care about URI's (which
are an unecessary 
*	complication for most usages.
*	In this case the DOM tree being passed in is both the document to be
signed AND
*	the container for the signature.
*/
            XMLSignature sig = new
XMLSignature(xml2BeSigned,"",XMLSignature.ALGO_ID_SIGNATURE_DSA);

/*
*	Get a (blank) signature element from the XMLSignature object and
attach it to the end of the 
*	document to be signed.
*/
// Attach the (blank) signature element to the document to be signed
            xml2BeSigned.getDocumentElement().appendChild(sig.getElement());

/*
*	All we actually want here is to tell it to use the w3c standard c14n
algorithm, but 
*	unfortunately you have to work around some of the flexibility
provided by the api.
*/
// Setup the transformation
            Transforms transforms = new Transforms(xml2BeSigned);
            XPathContainer xpath = new XPathContainer(xml2BeSigned);
            xpath.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
/*
*	As we attached the signature element to the document to be signed we
have to tell the 
*	XMLsignature object to sign everything but the signature element.
*/
            xpath.setXPath("not(ancestor-or-self::ds:Signature)\n");
            transforms.addTransform(Transforms.TRANSFORM_XPATH,
xpath.getElementPlusReturns());
            sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);

/*
*	Finally! We actually get to sign the document.
*/

            sig.sign(privateKey);
    }