]> source.dussan.org Git - poi.git/commitdiff
clean ups
authorAndreas Beeker <kiwiwings@apache.org>
Thu, 25 Sep 2014 23:59:36 +0000 (23:59 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Thu, 25 Sep 2014 23:59:36 +0000 (23:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/xml_signature@1627682 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java
src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/XAdESSignatureFacet.java

index 6ebb78439252a23ff127df368c0ce883dbdf2cbb..d4b5ecd1d36f0f8e12dba4a0a269a4ff6e61fd32 100644 (file)
@@ -71,6 +71,9 @@ import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
 import javax.xml.parsers.ParserConfigurationException;\r
 import javax.xml.transform.TransformerException;\r
 import javax.xml.transform.TransformerFactoryConfigurationError;\r
+import javax.xml.xpath.XPath;\r
+import javax.xml.xpath.XPathConstants;\r
+import javax.xml.xpath.XPathFactory;\r
 \r
 import org.apache.jcp.xml.dsig.internal.dom.DOMReference;\r
 import org.apache.jcp.xml.dsig.internal.dom.DOMSignedInfo;\r
@@ -175,7 +178,11 @@ public class SignatureInfo implements SignatureConfigurable {
             KeyInfoKeySelector keySelector = new KeyInfoKeySelector();\r
             try {\r
                 Document doc = DocumentHelper.readDocument(signaturePart.getInputStream());\r
-                registerIds(doc);\r
+                XPath xpath = XPathFactory.newInstance().newXPath();\r
+                NodeList nl = (NodeList)xpath.compile("//*[@Id]").evaluate(doc, XPathConstants.NODESET);\r
+                for (int i=0; i<nl.getLength(); i++) {\r
+                    ((Element)nl.item(i)).setIdAttribute("Id", true);\r
+                }\r
                 \r
                 DOMValidateContext domValidateContext = new DOMValidateContext(keySelector, doc);\r
                 domValidateContext.setProperty("org.jcp.xml.dsig.validateManifests", Boolean.TRUE);\r
@@ -206,12 +213,18 @@ public class SignatureInfo implements SignatureConfigurable {
         public void handleEvent(Event e) {\r
             if (e instanceof MutationEvent) {\r
                 MutationEvent mutEvt = (MutationEvent)e;\r
-                if (mutEvt.getTarget() instanceof Element) {\r
+                EventTarget et = mutEvt.getTarget();\r
+                if (et instanceof Element) {\r
                     Element el = (Element)mutEvt.getTarget();\r
                     String packageId = signatureConfig.getPackageSignatureId();\r
-                    if (packageId.equals(el.getAttribute("Id"))) {\r
-                        target.get().removeEventListener("DOMSubtreeModified", this, false);\r
-                        el.setAttributeNS(XmlNS, "xmlns:mdssi", PackageNamespaces.DIGITAL_SIGNATURE);\r
+                    if (el.hasAttribute("Id")) {\r
+                        el.setIdAttribute("Id", true);\r
+                        \r
+                        if (packageId.equals(el.getAttribute("Id"))) {\r
+                            target.get().removeEventListener("DOMSubtreeModified", this, false);\r
+                            el.setAttributeNS(XmlNS, "xmlns:mdssi", PackageNamespaces.DIGITAL_SIGNATURE);\r
+                            target.get().addEventListener("DOMSubtreeModified", this, false);\r
+                        }\r
                     }\r
                 }\r
             }\r
@@ -274,6 +287,7 @@ public class SignatureInfo implements SignatureConfigurable {
     }\r
     \r
     public Iterable<SignaturePart> getSignatureParts() {\r
+        signatureConfig.init(true);\r
         return new Iterable<SignaturePart>() {\r
             public Iterator<SignaturePart> iterator() {\r
                 return new Iterator<SignaturePart>() {\r
@@ -378,10 +392,8 @@ public class SignatureInfo implements SignatureConfigurable {
         default: throw new EncryptedDocumentException("Hash algorithm "+signatureConfig.getDigestAlgo()+" not supported for signing.");\r
         }\r
     }\r
-\r
     \r
-    \r
-    public static synchronized void initXmlProvider() {\r
+    protected static synchronized void initXmlProvider() {\r
         if (isInitialized) return;\r
         isInitialized = true;\r
         \r
@@ -394,6 +406,10 @@ public class SignatureInfo implements SignatureConfigurable {
         }\r
     }\r
     \r
+    /**\r
+     * Helper method for adding informations before the signing.\r
+     * Normally {@link #confirmSignature()} is sufficient to be used.\r
+     */\r
     @SuppressWarnings("unchecked")\r
     public DigestInfo preSign(Document document, List<DigestInfo> digestInfos)\r
         throws ParserConfigurationException, NoSuchAlgorithmException,\r
@@ -401,7 +417,6 @@ public class SignatureInfo implements SignatureConfigurable {
         javax.xml.crypto.dsig.XMLSignatureException,\r
         TransformerFactoryConfigurationError, TransformerException,\r
         IOException, SAXException, NoSuchProviderException, XmlException, URISyntaxException {\r
-        SignatureInfo.initXmlProvider();\r
         signatureConfig.init(false);\r
         \r
         // it's necessary to explicitly set the mdssi namespace, but the sign() method has no\r
@@ -489,8 +504,6 @@ public class SignatureInfo implements SignatureConfigurable {
         // xmlSignContext.putNamespacePrefix(PackageNamespaces.DIGITAL_SIGNATURE, "mdssi");\r
         xmlSignature.sign(xmlSignContext);\r
 \r
-        registerIds(document);\r
-        \r
         /*\r
          * Completion of undigested ds:References in the ds:Manifests.\r
          */\r
@@ -545,10 +558,13 @@ public class SignatureInfo implements SignatureConfigurable {
         return new DigestInfo(digestValue, signatureConfig.getDigestAlgo(), description);\r
     }\r
 \r
+    /**\r
+     * Helper method for adding informations after the signing.\r
+     * Normally {@link #confirmSignature()} is sufficient to be used.\r
+     */\r
     public void postSign(Document document, byte[] signatureValue)\r
     throws IOException, MarshalException, ParserConfigurationException, XmlException {\r
         LOG.log(POILogger.DEBUG, "postSign");\r
-        SignatureInfo.initXmlProvider();\r
 \r
         /*\r
          * Check ds:Signature node.\r
@@ -574,7 +590,6 @@ public class SignatureInfo implements SignatureConfigurable {
             signatureFacet.postSign(document, signatureConfig.getSigningCertificateChain());\r
         }\r
 \r
-        registerIds(document);\r
         writeDocument(document);\r
     }\r
 \r
@@ -635,28 +650,6 @@ public class SignatureInfo implements SignatureConfigurable {
         sigsPart.addRelationship(sigPartName, TargetMode.INTERNAL, PackageRelationshipTypes.DIGITAL_SIGNATURE);\r
     }\r
     \r
-    /**\r
-     * the resulting document needs to be tweaked before it can be digested -\r
-     * this applies to the verification and signing step\r
-     *\r
-     * @param doc\r
-     */\r
-    private static void registerIds(Document doc) {\r
-        NodeList nl = doc.getElementsByTagNameNS(XmlDSigNS, "Object");\r
-        registerIdAttribute(nl);\r
-        nl = doc.getElementsByTagNameNS("http://uri.etsi.org/01903/v1.3.2#", "SignedProperties");\r
-        registerIdAttribute(nl);\r
-    }\r
-    \r
-    public static void registerIdAttribute(NodeList nl) {\r
-        for (int i=0; i<nl.getLength(); i++) {\r
-            Element el = (Element)nl.item(i);\r
-            if (el.hasAttribute("Id")) {\r
-                el.setIdAttribute("Id", true);\r
-            }\r
-        }\r
-    }\r
-    \r
     @SuppressWarnings("unchecked")\r
     public static <T> List<T> safe(List<T> other) {\r
         return other == null ? Collections.EMPTY_LIST : other;\r
index c4b1e8f8c0beca1815cf02919d4c75f38a6aefc0..a1c6acf75b21c1a736f8fb204b9a701adcdf6b82 100644 (file)
@@ -52,7 +52,6 @@ import javax.xml.crypto.dsig.spec.TransformParameterSpec;
 import org.apache.poi.poifs.crypt.CryptoFunctions;\r
 import org.apache.poi.poifs.crypt.HashAlgorithm;\r
 import org.apache.poi.poifs.crypt.dsig.SignatureConfig;\r
-import org.apache.poi.poifs.crypt.dsig.SignatureInfo;\r
 import org.apache.poi.poifs.crypt.dsig.services.SignaturePolicyService;\r
 import org.apache.poi.util.POILogFactory;\r
 import org.apache.poi.util.POILogger;\r
@@ -214,7 +213,6 @@ public class XAdESSignatureFacet implements SignatureFacet {
         // add XAdES ds:Object\r
         List<XMLStructure> xadesObjectContent = new ArrayList<XMLStructure>();\r
         Element qualDocEl = (Element)document.importNode(qualifyingProperties.getDomNode(), true);\r
-        SignatureInfo.registerIdAttribute(qualDocEl.getElementsByTagName("SignedProperties"));\r
         qualDocEl.setAttributeNS(XmlNS, "xmlns:xd", "http://uri.etsi.org/01903/v1.3.2#");\r
         setPrefix(qualDocEl, "http://uri.etsi.org/01903/v1.3.2#", "xd");\r
         xadesObjectContent.add(new DOMStructure(qualDocEl));\r