Title: Message
Hello, I am trying to add an XSL transformation to a signature.
However, I keep getting a NullPointerException in the Xalan File ElemTemplateElement.java in the function:
 
  public StylesheetRoot getStylesheetRoot()
  {
    return m_parentNode.getStylesheetRoot();
  }
For testing purposes, I just modifed the CreateSignature.java example. The code is below. If someone could point out what I am doing wrong, I would really appreciate it.
 
 
   public static void main(String unused[]) throws Exception {
      Constants.setSignatureSpecNSprefix("");
 
   String eForm = "<formData version=\"2\">"
      + "<shana>"
      + "<template id=\"1137938496\" name=\"\" rev=\"1\"></template>"
      + "</shana>"
      + "<model>"
      + "<string name=\"Cell1\"></string>"
        + "<string name=\"Cell2\"></string>"
      + "<string name=\"Cell3\"></string>"
      + "<binary name=\"Cell4\" type=\"signature\"></binary>"
      + "</model>"
      + "<instance index=\"1\">"
      + "<Cell4>4000</Cell4>"
      + "<Cell3></Cell3>"
      + "<Cell2>3000</Cell2>"
      + "<Cell1>1000</Cell1>"
      + "</instance>"
      + "<properties>"
      + "</properties>"
      + "</formData>";
 
   String xslt = "<?xml version=\"1.0\"?>\n"
   + "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>\n"
   + "<xsl:output encoding='UTF-8' method='xml' indent='no' />\n"
   + "<xsl:template match='/'>\n"
   + "<xsl:for-each select='formData/instance/child::*'>\n"
      + "<xsl:sort select='local-name()' />\n"
   + "<xsl:value-of select='local-name()' /> = <xsl:value-of select='text()' />;\n"
   + "</xsl:for-each>\n"
   + "</xsl:template>\n"
   + "</xsl:stylesheet>\n"
   ;
 
      //All the parameters for the keystore
      String keystoreType = "JKS";
      String keystoreFile = "c:/xml/my_keys/tomcat.jks";
   String keystorePass = "tomcat";
   String privateKeyAlias = "myprivatekey";
   String privateKeyPass = "mypassword";
   String certificateAlias = "mycert";
      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());
      String keyFormat = privateKey.getFormat();
 
      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();
 
      //The BaseURI is the URI that's used to prepend to relative URIs
   InputStream istream = new StringBufferInputStream(eForm);
   org.w3c.dom.Document doc = db.parse(istream);
      String BaseURI = signatureFile.toURL().toString();
      XMLSignature sig = new XMLSignature(doc, BaseURI,
                                          XMLSignature.ALGO_ID_SIGNATURE_RSA);
 
      //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.
   Element root = doc.getDocumentElement();
      root.appendChild(sig.getElement());
   {
         //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);
 
   // xslt transform
   org.w3c.dom.Document docxslt = db.parse(new ByteArrayInputStream(xslt.getBytes()));
   Node xslElem = docxslt.getDocumentElement();
   Node xslElemImported = doc.importNode(xslElem, true);
   transforms.addTransform(Transforms.TRANSFORM_XSLT, (Element) xslElemImported);
 
         //Add the above Document/Reference
         sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
      }
 
   {
         //Add in the KeyInfo for the certificate that we used the private key of
         X509Certificate cert =
            (X509Certificate) ks.getCertificate(certificateAlias);
 
         sig.addKeyInfo(cert);
         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();
   }
}
Thanks,
 
-- Sanjay

Reply via email to