]> source.dussan.org Git - poi.git/commitdiff
#58630 - Signing failed after deletion of first sheet
authorAndreas Beeker <kiwiwings@apache.org>
Fri, 20 Nov 2015 22:38:27 +0000 (22:38 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Fri, 20 Nov 2015 22:38:27 +0000 (22:38 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1715438 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/OOXMLSignatureFacet.java
src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java
test-data/xmldsign/bug58630.xlsx [new file with mode: 0644]

index 8ba8bf2395324d1025603da92aa1eb5ffcf1bed3..9e094a6415f8269923d5f1aa0cee2436421fcef1 100644 (file)
@@ -33,7 +33,6 @@ import java.util.HashSet;
 import java.util.List;\r
 import java.util.Locale;\r
 import java.util.Set;\r
-import java.util.TimeZone;\r
 \r
 import javax.xml.XMLConstants;\r
 import javax.xml.crypto.XMLStructure;\r
@@ -113,6 +112,7 @@ public class OOXMLSignatureFacet extends SignatureFacet {
         references.add(reference);\r
     }\r
 \r
+    @SuppressWarnings("resource")\r
     protected void addManifestReferences(List<Reference> manifestReferences)\r
     throws XMLSignatureException {\r
 \r
@@ -149,7 +149,10 @@ public class OOXMLSignatureFacet extends SignatureFacet {
                 parameterSpec.addRelationshipReference(relationship.getId());\r
 \r
                 // TODO: find a better way ...\r
-                String partName = baseUri + relationship.getTargetURI().toString();\r
+                String partName = relationship.getTargetURI().toString();\r
+                if (!partName.startsWith(baseUri)) {\r
+                    partName = baseUri + partName;\r
+                }\r
                 try {\r
                     partName = new URI(partName).normalize().getPath().replace('\\', '/');\r
                     LOG.log(POILogger.DEBUG, "part name: " + partName);\r
index 6f43fdd1f1843bcaeef8b67fa1f3c7e4e9ceb237..d2fead012d3bea87410fc0fcdd0027c033a85554 100644 (file)
@@ -28,6 +28,8 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;\r
 import static org.junit.Assert.assertTrue;\r
 \r
+import java.io.ByteArrayInputStream;\r
+import java.io.ByteArrayOutputStream;\r
 import java.io.File;\r
 import java.io.FileInputStream;\r
 import java.io.FileOutputStream;\r
@@ -68,11 +70,13 @@ import org.apache.poi.poifs.crypt.dsig.services.RevocationData;
 import org.apache.poi.poifs.crypt.dsig.services.RevocationDataService;\r
 import org.apache.poi.poifs.crypt.dsig.services.TimeStampService;\r
 import org.apache.poi.poifs.crypt.dsig.services.TimeStampServiceValidator;\r
+import org.apache.poi.ss.usermodel.WorkbookFactory;\r
 import org.apache.poi.util.DocumentHelper;\r
 import org.apache.poi.util.IOUtils;\r
 import org.apache.poi.util.LocaleUtil;\r
 import org.apache.poi.util.POILogFactory;\r
 import org.apache.poi.util.POILogger;\r
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;\r
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
 import org.apache.xmlbeans.XmlObject;\r
 import org.bouncycastle.asn1.x509.KeyUsage;\r
@@ -240,6 +244,7 @@ public class TestSignatureInfo {
     public void testManipulation() throws Exception {\r
         // sign & validate\r
         String testFile = "hello-world-unsigned.xlsx";\r
+        @SuppressWarnings("resource")\r
         OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);\r
         sign(pkg, "Test", "CN=Test", 1);\r
         \r
@@ -379,6 +384,7 @@ public class TestSignatureInfo {
         try {\r
             si.confirmSignature();\r
         } catch (RuntimeException e) {\r
+            pkg.close();\r
             // only allow a ConnectException because of timeout, we see this in Jenkins from time to time...\r
             if(e.getCause() == null) {\r
                 throw e;\r
@@ -546,6 +552,32 @@ public class TestSignatureInfo {
             }\r
         }\r
     }\r
+\r
+    @Test\r
+    public void bug58630() throws Exception {\r
+        // test deletion of sheet 0 and signing\r
+        File tpl = copy(testdata.getFile("bug58630.xlsx"));\r
+        SXSSFWorkbook wb1 = new SXSSFWorkbook((XSSFWorkbook)WorkbookFactory.create(tpl), 10);\r
+        wb1.setCompressTempFiles(true);\r
+        wb1.removeSheetAt(0);\r
+        ByteArrayOutputStream os = new ByteArrayOutputStream();\r
+        wb1.write(os);\r
+        wb1.close();\r
+        OPCPackage pkg = OPCPackage.open(new ByteArrayInputStream(os.toByteArray()));\r
+        \r
+        initKeyPair("Test", "CN=Test");\r
+        SignatureConfig signatureConfig = new SignatureConfig();\r
+        signatureConfig.setKey(keyPair.getPrivate());\r
+        signatureConfig.setSigningCertificateChain(Collections.singletonList(x509));\r
+        signatureConfig.setOpcPackage(pkg);\r
+        \r
+        SignatureInfo si = new SignatureInfo();\r
+        si.setSignatureConfig(signatureConfig);\r
+        si.confirmSignature();\r
+        assertTrue("invalid signature", si.verifySignature());\r
+        \r
+        pkg.close();\r
+    }\r
     \r
     \r
     private void sign(OPCPackage pkgCopy, String alias, String signerDn, int signerCount) throws Exception {\r
diff --git a/test-data/xmldsign/bug58630.xlsx b/test-data/xmldsign/bug58630.xlsx
new file mode 100644 (file)
index 0000000..cf7b1cc
Binary files /dev/null and b/test-data/xmldsign/bug58630.xlsx differ