aboutsummaryrefslogtreecommitdiffstats
path: root/poi/src
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2023-02-05 21:11:03 +0000
committerDominik Stadler <centic@apache.org>2023-02-05 21:11:03 +0000
commit1ca2e638ed1de3438cf660f1006d6cd42e265e3c (patch)
treea5af456ce62ca4f6c5449920b4f97acc46dde0bc /poi/src
parent43551babf1cf628aa184a2f9445412815781dc9e (diff)
downloadpoi-1ca2e638ed1de3438cf660f1006d6cd42e265e3c.tar.gz
poi-1ca2e638ed1de3438cf660f1006d6cd42e265e3c.zip
Bug 66436: Fix invalid handling of padded encrypted bytes
The encrypted data is padded to 16 bytes, but these additional bytes were not read from the stream Make the reading of the additional bytes "lenient" to not introduce breaking change if some existing functionality did produce non-aligned data for some reason. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1907444 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src')
-rw-r--r--poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java11
-rw-r--r--poi/src/test/java/org/apache/poi/poifs/crypt/agile/TestAgileDecryptor.java102
2 files changed, 113 insertions, 0 deletions
diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java
index ef1622c40f..baa2a000df 100644
--- a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java
+++ b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java
@@ -196,6 +196,17 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream {
throw new EOFException("buffer underrun");
}
+ // encrypted data is processed in chunks of 16 bytes,
+ // so try to read some more data if the current data is not a
+ // multiple of 16 bytes
+ if (totalBytes % 16 != 0) {
+ int toRead = 16 - totalBytes % 16;
+ int read = super.read(plain, totalBytes, toRead);
+ if (read > 0) {
+ totalBytes += read;
+ }
+ }
+
System.arraycopy(plain, 0, chunk, 0, totalBytes);
invokeCipher(totalBytes, totalBytes == chunkSize);
diff --git a/poi/src/test/java/org/apache/poi/poifs/crypt/agile/TestAgileDecryptor.java b/poi/src/test/java/org/apache/poi/poifs/crypt/agile/TestAgileDecryptor.java
new file mode 100644
index 0000000000..0d08b232bf
--- /dev/null
+++ b/poi/src/test/java/org/apache/poi/poifs/crypt/agile/TestAgileDecryptor.java
@@ -0,0 +1,102 @@
+/* ====================================================================
+ 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.poifs.crypt.agile;
+
+import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+
+import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
+import org.apache.poi.poifs.crypt.Decryptor;
+import org.apache.poi.poifs.crypt.EncryptionInfo;
+import org.apache.poi.poifs.crypt.EncryptionMode;
+import org.apache.poi.poifs.crypt.Encryptor;
+import org.apache.poi.poifs.filesystem.DocumentInputStream;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.util.HexDump;
+import org.apache.poi.util.IOUtils;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+class TestAgileDecryptor {
+ @SuppressWarnings("PrimitiveArrayArgumentToVarargsMethod")
+ public static Stream<Arguments> data() {
+ List<Arguments> data = new ArrayList<>();
+ data.add(Arguments.of(new byte[15]));
+ data.add(Arguments.of(new byte[16]));
+ data.add(Arguments.of(new byte[17]));
+ data.add(Arguments.of(new byte[3292]));
+ data.add(Arguments.of(new byte[3293]));
+ data.add(Arguments.of(new byte[4000]));
+
+ return data.stream();
+ }
+
+ @ParameterizedTest
+ @MethodSource("data")
+ void testAgileDecryptor(byte[] testData) throws Exception {
+ EncryptionInfo infoEnc = new EncryptionInfo(EncryptionMode.agile);
+ Encryptor enc = infoEnc.getEncryptor();
+ enc.confirmPassword("f");
+
+ byte[] encData;
+ byte[] encDocument;
+ try (POIFSFileSystem fsEnc = new POIFSFileSystem()) {
+ try (OutputStream os = enc.getDataStream(fsEnc)) {
+ os.write(testData);
+ }
+
+ UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
+ fsEnc.writeFilesystem(bos);
+
+ bos.close();
+ encData = bos.toByteArray();
+
+ DocumentInputStream dis = fsEnc.getRoot().createDocumentInputStream(DEFAULT_POIFS_ENTRY);
+ /*long _length =*/ dis.readLong();
+ encDocument = IOUtils.toByteArray(dis);
+ }
+
+ byte[] actualData;
+ try (POIFSFileSystem fsDec = new POIFSFileSystem(new ByteArrayInputStream(encData))) {
+ EncryptionInfo infoDec = new EncryptionInfo(fsDec);
+ Decryptor dec = infoDec.getDecryptor();
+ assertTrue(dec.verifyPassword("f"));
+ InputStream is = dec.getDataStream(fsDec);
+
+ actualData = IOUtils.toByteArray(is);
+ is.close();
+ }
+
+ // input-data and resulting decrypted data should be equal
+ assertArrayEquals(testData, actualData,
+ "Having " + testData.length + " bytes, had expected \n" +
+ HexDump.dump(testData, 0, 0) + " and actual \n" +
+ HexDump.dump(actualData, 0, 0) + " encrypted \n" +
+ HexDump.dump(encDocument, 0, 0) + " full encrypted \n" +
+ HexDump.dump(encData, 0, 0));
+ }
+}