From 0042a4717239718390a3e23e9bb1e38592f07186 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 16 Oct 2021 10:30:43 +0000 Subject: [PATCH] record size of encrypted temp data git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894303 13f79535-47bb-0310-9956-ffa450edef68 --- .../EncryptedTempFilePackagePart.java | 5 +---- .../poifs/crypt/temp/EncryptedTempData.java | 14 ++++++++++++-- .../opc/TestEncryptedTempFilePackagePart.java | 5 +++-- .../opc/TestTempFilePackagePart.java | 2 +- poi/src/test/java9/module-info.class | Bin 4198 -> 4145 bytes 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java index a9119eaf42..a64326c32a 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java @@ -98,12 +98,9 @@ public final class EncryptedTempFilePackagePart extends PackagePart { return tempFile.getOutputStream(); } - /** - * @return EncryptedTempData.getSize() always returns -1 - */ @Override public long getSize() { - return -1; + return tempFile.getByteCount(); } @Override diff --git a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java index 9f219f1a80..d0fe43a9c8 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java +++ b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java @@ -32,6 +32,7 @@ import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.spec.SecretKeySpec; +import org.apache.commons.io.output.CountingOutputStream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.poi.poifs.crypt.ChainingMode; @@ -52,7 +53,8 @@ public class EncryptedTempData { private final SecretKeySpec skeySpec; private final byte[] ivBytes; private final File tempFile; - + private CountingOutputStream outputStream; + public EncryptedTempData() throws IOException { SecureRandom sr = new SecureRandom(); ivBytes = new byte[16]; @@ -72,7 +74,8 @@ public class EncryptedTempData { */ public OutputStream getOutputStream() throws IOException { Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING); - return new CipherOutputStream(new FileOutputStream(tempFile), ciEnc); + outputStream = new CountingOutputStream(new CipherOutputStream(new FileOutputStream(tempFile), ciEnc)); + return outputStream; } /** @@ -86,6 +89,13 @@ public class EncryptedTempData { return new CipherInputStream(new FileInputStream(tempFile), ciDec); } + /** + * @return number of bytes stored in the temp data file (the number you should expect after you decrypt the data) + */ + public long getByteCount() { + return outputStream == null ? 0 : outputStream.getByteCount(); + } + /** * Removes the temporarily backing file */ diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java index 4f0524b2ea..1d9f3d411e 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java +++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java @@ -33,15 +33,16 @@ public class TestEncryptedTempFilePackagePart { @Test void testRoundTrip() throws Exception { String text = UUID.randomUUID().toString(); + byte[] bytes = text.getBytes(StandardCharsets.UTF_8); String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); try (OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ)) { PackagePartName name = new PackagePartName("/test.txt", true); EncryptedTempFilePackagePart part = new EncryptedTempFilePackagePart(p, name, "text/plain"); try (OutputStream os = part.getOutputStream()) { - os.write(text.getBytes(StandardCharsets.UTF_8)); + os.write(bytes); } - assertEquals(-1, part.getSize()); + assertEquals(bytes.length, part.getSize()); try (InputStream is = part.getInputStream()) { assertEquals(text, new String(IOUtils.toByteArray(is), StandardCharsets.UTF_8)); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java index c30dbcf18b..768054acec 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java +++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java @@ -40,7 +40,7 @@ public class TestTempFilePackagePart { PackagePartName name = new PackagePartName("/test.txt", true); TempFilePackagePart part = new TempFilePackagePart(p, name, "text/plain"); try (OutputStream os = part.getOutputStream()) { - os.write(text.getBytes(StandardCharsets.UTF_8)); + os.write(bytes); } assertEquals(bytes.length, part.getSize()); try (InputStream is = part.getInputStream()) { diff --git a/poi/src/test/java9/module-info.class b/poi/src/test/java9/module-info.class index 438e778ab515b0d480b1fb79ccaffeeafa4e55c6..028b943d0ae7577c19f08d4b20ce352134f4f685 100644 GIT binary patch literal 4145 zcmai1`Fj*a5PgH_iXbRkf)@fR9w<9RgZF``2r7z1#ak!4lg%W%Gvmx`LOk)l@B6;* z`}Xq({{a6eKkL=*Cd2Fy^L=?Q(_P(NU9Y;j^UuG3{S9Cf{;c3G2ZxMAouuw>4O+f~ z!`e}!)ABb2;aF6g@TNQm2ko$?D!eL29aM&fY8z`q8>^^S(Wt_&Vyud$+MQ7w8m00%V!_>lM34$HK}Rqpm=^32%n0rg+$*?G zaKGRI!GnUmf`zBx!?=Imx8YZUkknwd@J}) z@O>3O2!5>KrwV?q;Fk)1t>Cu`ey`vU2a9u2bg(>1nl&%>>f?SbjspHVSj6DgM!i&n zzB*@6kJ{}hOl$S1)$;3EpnnfLII`5D;ny7;+8@#Ovhgz=EE$QtB=xrh*@&O*JF*ou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