diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2014-12-05 20:20:23 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2014-12-05 20:20:23 +0000 |
commit | cce7a4180728963b772f1f5b4271923d1dcf3307 (patch) | |
tree | e2fe524ab8868f3fb9a85871279d8633e0d6dc0d | |
parent | 42bb3f2194c23bffd60f7598d8055065e7c5754e (diff) | |
download | poi-cce7a4180728963b772f1f5b4271923d1dcf3307.tar.gz poi-cce7a4180728963b772f1f5b4271923d1dcf3307.zip |
XML signatures - ignore line breaks in Office 2007 .rels files
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1643415 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | src/ooxml/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java | 23 | ||||
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java | 25 | ||||
-rw-r--r-- | test-data/xmldsign/office2007prettyPrintedRels.docx | bin | 0 -> 15390 bytes |
3 files changed, 36 insertions, 12 deletions
diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java index dad12d2e8f..64eb53d6f8 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java @@ -24,7 +24,10 @@ package org.apache.poi.poifs.crypt.dsig;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
@@ -81,12 +84,28 @@ public class OOXMLURIDereferencer implements URIDereferencer, SignatureConfigura LOG.log(POILogger.DEBUG, "cannot resolve, delegating to base DOM URI dereferencer", uri);
return this.baseUriDereferencer.dereference(uriReference, context);
}
-
+
+ InputStream dataStream;
try {
- return new OctetStreamData(part.getInputStream(), uri.toString(), null);
+ dataStream = part.getInputStream();
+
+ // workaround for office 2007 pretty-printed .rels files
+ if (part.getPartName().toString().endsWith(".rels")) {
+ // although xmlsec has an option to ignore line breaks, currently this
+ // only affects .rels files, so we only modify these
+ // http://stackoverflow.com/questions/4728300
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ for (int ch; (ch = dataStream.read()) != -1; ) {
+ if (ch == 10 || ch == 13) continue;
+ bos.write(ch);
+ }
+ dataStream = new ByteArrayInputStream(bos.toByteArray());
+ }
} catch (IOException e) {
throw new URIReferenceException("I/O error: " + e.getMessage(), e);
}
+
+ return new OctetStreamData(dataStream, uri.toString(), null);
}
private PackagePart findPart(URI uri) {
diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java index 609a26d6b6..fe7a91bc02 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java @@ -23,7 +23,10 @@ ================================================================= */
package org.apache.poi.poifs.crypt;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
@@ -109,6 +112,17 @@ public class TestSignatureInfo { }
@Test
+ public void office2007prettyPrintedRels() throws Exception {
+ OPCPackage pkg = OPCPackage.open(testdata.getFile("office2007prettyPrintedRels.docx"), PackageAccess.READ);
+ SignatureConfig sic = new SignatureConfig();
+ sic.setOpcPackage(pkg);
+ SignatureInfo si = new SignatureInfo();
+ si.setSignatureConfig(sic);
+ boolean isValid = si.verifySignature();
+ assertTrue(isValid);
+ }
+
+ @Test
public void getSignerUnsigned() throws Exception {
String testFiles[] = {
"hello-world-unsigned.docx",
@@ -222,7 +236,6 @@ public class TestSignatureInfo { public void testManipulation() throws Exception {
// sign & validate
String testFile = "hello-world-unsigned.xlsx";
- @SuppressWarnings("resource") // closed via XSSFWorkbook.close() below ?!
OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);
sign(pkg, "Test", "CN=Test", 1);
@@ -523,14 +536,6 @@ public class TestSignatureInfo { si.confirmSignature();
boolean b = si.verifySignature();
assertTrue("Signature not correctly calculated for " + ha, b);
-// } catch (EncryptedDocumentException e) {
-// // see http://apache-poi.1045710.n5.nabble.com/org-apache-poi-poifs-crypt-TestSignatureInfo-failing-on-trunk-on-Java-6-tp5717032.html
-// Throwable cause = e.getCause();
-// if (cause instanceof ArrayIndexOutOfBoundsException) {
-// LOG.log(POILogger.ERROR, "ignoring AIOOBE - hopefully a SHA2 bug ...", e);
-// } else {
-// throw e;
-// }
} finally {
if (pkg != null) pkg.close();
}
diff --git a/test-data/xmldsign/office2007prettyPrintedRels.docx b/test-data/xmldsign/office2007prettyPrintedRels.docx Binary files differnew file mode 100644 index 0000000000..ce8b940944 --- /dev/null +++ b/test-data/xmldsign/office2007prettyPrintedRels.docx |