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
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
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
}\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
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
}\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
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
// 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
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
signatureFacet.postSign(document, signatureConfig.getSigningCertificateChain());\r
}\r
\r
- registerIds(document);\r
writeDocument(document);\r
}\r
\r
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