-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
/*
* Add a ds:KeyInfo entry.
*/
- KeyInfoFactory keyInfoFactory = KeyInfoFactory.getInstance();
+ KeyInfoFactory keyInfoFactory = CryptoFactoryFactory.getKeyInfoFactory();
List<Object> x509DataObjects = new LinkedList<Object>();
X509Certificate signingCertificate = signingCertificateChain.get(0);
try {
outputSignedOfficeOpenXMLDocument(this.toByteArray());
} catch (Exception e) {
- throw new IOException("generic error: " + e.getMessage(), e);
+ throw new IOException(e.getMessage());
}
}
}
/**
- * The output stream to which to write the signed Office OpenXML file.
- *
- * @return
+ * @return The output stream to which to write the signed Office OpenXML file.
*/
abstract protected OutputStream getSignedOfficeOpenXMLDocumentOutputStream();
/**
- * Gives back the URL of the OOXML to be signed.
- *
- * @return
+ * @return the URL of the OOXML to be signed.
*/
abstract protected URL getOfficeOpenXMLDocumentURL();
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ooxml.signature.service.signer.ooxml;
+
+import java.security.Provider;
+
+import javax.xml.crypto.dsig.XMLSignatureFactory;
+import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;
+
+/**
+ * Creates {@link XMLSignatureFactory} and {@link KeyInfoFactory} instances
+ * as used by the ooxml signature service.
+ */
+final class CryptoFactoryFactory {
+
+ private static final Provider _provider = new org.jcp.xml.dsig.internal.dom.XMLDSigRI();
+
+ private CryptoFactoryFactory() {
+ // no instances of this class
+ }
+
+ public static XMLSignatureFactory getSignatureFactory() {
+ return XMLSignatureFactory.getInstance("DOM", _provider);
+ }
+
+ public static KeyInfoFactory getKeyInfoFactory() {
+ return KeyInfoFactory.getInstance("DOM", _provider);
+ }
+}
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
/**
* Signature verifier util class for Office Open XML file format.
*/
-public class OOXMLSignatureVerifier {
+public final class OOXMLSignatureVerifier {
private static final Log LOG = LogFactory.getLog(OOXMLSignatureVerifier.class);
private OOXMLSignatureVerifier() {
- super();
+ // no instances of this class;
}
/**
- * Checks whether the file referred by the given URL is an OOXML document.
- *
- * @param url
- * @return
- * @throws IOException
+ * @return <code>true</code> if the file referred by the given URL is an OOXML document.
*/
public static boolean isOOXML(URL url) throws IOException {
ZipInputStream zipInputStream = new ZipInputStream(url.openStream());
OOXMLURIDereferencer dereferencer = new OOXMLURIDereferencer(url);
domValidateContext.setURIDereferencer(dereferencer);
- XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance();
+ XMLSignatureFactory xmlSignatureFactory = CryptoFactoryFactory.getSignatureFactory();
XMLSignature xmlSignature = xmlSignatureFactory.unmarshalXMLSignature(domValidateContext);
boolean validity = xmlSignature.validate(domValidateContext);
OOXMLURIDereferencer dereferencer = new OOXMLURIDereferencer(url);
domValidateContext.setURIDereferencer(dereferencer);
- XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance();
+ XMLSignatureFactory xmlSignatureFactory = CryptoFactoryFactory.getSignatureFactory();
XMLSignature xmlSignature = xmlSignatureFactory.unmarshalXMLSignature(domValidateContext);
return xmlSignature.validate(domValidateContext);
}
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
throw new IllegalArgumentException("ooxmlUrl is null");
}
this.ooxmlUrl = ooxmlUrl;
- XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance();
+ XMLSignatureFactory xmlSignatureFactory = CryptoFactoryFactory.getSignatureFactory();
this.baseUriDereferencer = xmlSignatureFactory.getURIDereferencer();
}
return part.getInputStream();
}
}
- LOG.info("No part found for URI: " + uri);
+ LOG.debug("No part found for URI: " + uri);
return null;
}
}
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
-public class TestAbstractXmlSignatureService extends TestCase {
+public final class TestAbstractXmlSignatureService extends TestCase {
private static final Log LOG = LogFactory.getLog(TestAbstractXmlSignatureService.class);
}
}
+ private XMLSignatureFactory getXMLSignatureFactory() {
+ return XMLSignatureFactory.getInstance("DOM", new org.jcp.xml.dsig.internal.dom.XMLDSigRI());
+ }
+
public void testSignEnvelopingDocument() throws Exception {
// setup
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
assertNotNull(digestValueNode);
String digestValueTextContent = digestValueNode.getTextContent();
LOG.debug("digest value text content: " + digestValueTextContent);
- assertFalse(digestValueTextContent.isEmpty());
+ assertTrue(digestValueTextContent.length() > 0);
/*
* Sign the received XML signature digest value.
Node signatureNode = signatureNodeList.item(0);
DOMValidateContext domValidateContext = new DOMValidateContext(KeySelector.singletonKeySelector(keyPair.getPublic()), signatureNode);
- XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance();
+ XMLSignatureFactory xmlSignatureFactory = getXMLSignatureFactory();
XMLSignature xmlSignature = xmlSignatureFactory.unmarshalXMLSignature(domValidateContext);
boolean validity = xmlSignature.validate(domValidateContext);
assertTrue(validity);
assertNotNull(digestValueNode);
String digestValueTextContent = digestValueNode.getTextContent();
LOG.debug("digest value text content: " + digestValueTextContent);
- assertFalse(digestValueTextContent.isEmpty());
+ assertTrue(digestValueTextContent.length() > 0);
/*
* Sign the received XML signature digest value.
DOMValidateContext domValidateContext = new DOMValidateContext(KeySelector.singletonKeySelector(keyPair.getPublic()), signatureNode);
domValidateContext.setURIDereferencer(uriDereferencer);
- XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance();
+ XMLSignatureFactory xmlSignatureFactory = getXMLSignatureFactory();
XMLSignature xmlSignature = xmlSignatureFactory.unmarshalXMLSignature(domValidateContext);
boolean validity = xmlSignature.validate(domValidateContext);
assertTrue(validity);
assertNotNull(digestValueNode);
String digestValueTextContent = digestValueNode.getTextContent();
LOG.debug("digest value text content: " + digestValueTextContent);
- assertFalse(digestValueTextContent.isEmpty());
+ assertTrue(digestValueTextContent.length() > 0);
/*
* Sign the received XML signature digest value.
DOMValidateContext domValidateContext = new DOMValidateContext(KeySelector.singletonKeySelector(keyPair.getPublic()), signatureNode);
URIDereferencer dereferencer = new URITest2Dereferencer();
domValidateContext.setURIDereferencer(dereferencer);
- XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance();
+ XMLSignatureFactory xmlSignatureFactory = getXMLSignatureFactory();
XMLSignature xmlSignature = xmlSignatureFactory.unmarshalXMLSignature(domValidateContext);
boolean validity = xmlSignature.validate(domValidateContext);
assertTrue(validity);
assertNotNull(digestValueNode);
String digestValueTextContent = digestValueNode.getTextContent();
LOG.debug("digest value text content: " + digestValueTextContent);
- assertFalse(digestValueTextContent.isEmpty());
+ assertTrue(digestValueTextContent.length() > 0);
/*
* Sign the received XML signature digest value.
DOMValidateContext domValidateContext = new DOMValidateContext(KeySelector.singletonKeySelector(keyPair.getPublic()), signatureNode);
URIDereferencer dereferencer = new URITest2Dereferencer();
domValidateContext.setURIDereferencer(dereferencer);
- XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance();
+ XMLSignatureFactory xmlSignatureFactory = getXMLSignatureFactory();
XMLSignature xmlSignature = xmlSignatureFactory.unmarshalXMLSignature(domValidateContext);
boolean validity = xmlSignature.validate(domValidateContext);
assertTrue(validity);
assertNotNull(digestValueNode);
String digestValueTextContent = digestValueNode.getTextContent();
LOG.debug("digest value text content: " + digestValueTextContent);
- assertFalse(digestValueTextContent.isEmpty());
+ assertTrue(digestValueTextContent.length() > 0);
}
}