From fb4bf25259289aa72cf05d8f665a99012b82329d Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 29 Apr 2015 19:47:35 +0000 Subject: [PATCH] Use a constant for the name of the OOXML encrypted package node git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1676838 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/stress/POIXMLDocumentHandler.java | 3 ++- src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java | 3 ++- .../org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java | 4 +++- src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java | 2 +- src/java/org/apache/poi/poifs/crypt/Decryptor.java | 1 + src/java/org/apache/poi/poifs/crypt/Encryptor.java | 1 + .../apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java | 4 ++-- .../apache/poi/poifs/crypt/standard/StandardDecryptor.java | 2 +- .../apache/poi/poifs/crypt/standard/StandardEncryptor.java | 2 +- .../java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java | 2 +- .../testcases/org/apache/poi/poifs/crypt/TestEncryptor.java | 4 ++-- 11 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java b/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java index c720272352..7b0821dcc0 100644 --- a/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java +++ b/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java @@ -27,6 +27,7 @@ import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackagePart; +import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.junit.Ignore; import org.junit.Test; @@ -43,7 +44,7 @@ public final class POIXMLDocumentHandler { protected static boolean isEncrypted(InputStream stream) throws IOException { if (POIFSFileSystem.hasPOIFSHeader(stream)) { POIFSFileSystem poifs = new POIFSFileSystem(stream); - if (poifs.getRoot().hasEntry("EncryptedPackage")) { + if (poifs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { return true; } throw new IOException("wrong file format or file extension for OO XML file"); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 503ed64d6c..aee2d5444a 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -65,6 +65,7 @@ import org.apache.poi.hssf.record.UnknownRecord; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; import org.apache.poi.hssf.record.common.UnicodeString; import org.apache.poi.hssf.util.CellReference; +import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.EntryUtils; @@ -248,7 +249,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // check for an encrypted .xlsx file - they get OLE2 wrapped try { - directory.getEntry("EncryptedPackage"); + directory.getEntry(Decryptor.DEFAULT_POIFS_ENTRY); throw new EncryptedDocumentException("The supplied spreadsheet seems to be an Encrypted .xlsx file. " + "It must be decrypted before use by XSSF, it cannot be used by HSSF"); } catch (FileNotFoundException e) { diff --git a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java index 8a2bf00454..f663c7e089 100644 --- a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java +++ b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java @@ -16,6 +16,8 @@ ==================================================================== */ package org.apache.poi.poifs.crypt; +import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY; + import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -133,7 +135,7 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE); calculateChecksum(fileOut, oleStreamSize); - dir.createDocument("EncryptedPackage", oleStreamSize, new EncryptedPackageWriter()); + dir.createDocument(DEFAULT_POIFS_ENTRY, oleStreamSize, new EncryptedPackageWriter()); createEncryptionInfoEntry(dir, fileOut); } catch (GeneralSecurityException e) { throw new IOException(e); diff --git a/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java b/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java index 0c80c6c2b3..f6477fb9b5 100644 --- a/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java +++ b/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java @@ -36,7 +36,7 @@ public class DataSpaceMapUtils { public static void addDefaultDataSpace(DirectoryEntry dir) throws IOException { DataSpaceMapEntry dsme = new DataSpaceMapEntry( new int[]{ 0 } - , new String[]{ "EncryptedPackage" } + , new String[]{ Decryptor.DEFAULT_POIFS_ENTRY } , "StrongEncryptionDataSpace" ); DataSpaceMap dsm = new DataSpaceMap(new DataSpaceMapEntry[]{dsme}); diff --git a/src/java/org/apache/poi/poifs/crypt/Decryptor.java b/src/java/org/apache/poi/poifs/crypt/Decryptor.java index af449290e8..d584346f9c 100644 --- a/src/java/org/apache/poi/poifs/crypt/Decryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/Decryptor.java @@ -29,6 +29,7 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem; public abstract class Decryptor { public static final String DEFAULT_PASSWORD="VelvetSweatshop"; + public static final String DEFAULT_POIFS_ENTRY="EncryptedPackage"; protected final EncryptionInfoBuilder builder; private SecretKey secretKey; diff --git a/src/java/org/apache/poi/poifs/crypt/Encryptor.java b/src/java/org/apache/poi/poifs/crypt/Encryptor.java index abfd693306..4c1b51258f 100644 --- a/src/java/org/apache/poi/poifs/crypt/Encryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/Encryptor.java @@ -27,6 +27,7 @@ import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public abstract class Encryptor { + protected static final String DEFAULT_POIFS_ENTRY = Decryptor.DEFAULT_POIFS_ENTRY; private SecretKey secretKey; /** diff --git a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java index 40eab54e3a..a2d3d6f0e1 100644 --- a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java @@ -118,7 +118,7 @@ public class BinaryRC4Decryptor extends Decryptor { public InputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { - DocumentInputStream dis = dir.createDocumentInputStream("EncryptedPackage"); + DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY); _length = dis.readLong(); BinaryRC4CipherInputStream cipherStream = new BinaryRC4CipherInputStream(dis, _length); return cipherStream; @@ -131,4 +131,4 @@ public class BinaryRC4Decryptor extends Decryptor { return _length; } -} \ No newline at end of file +} diff --git a/src/java/org/apache/poi/poifs/crypt/standard/StandardDecryptor.java b/src/java/org/apache/poi/poifs/crypt/standard/StandardDecryptor.java index 2b2c75b520..1d6ddd398e 100644 --- a/src/java/org/apache/poi/poifs/crypt/standard/StandardDecryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/standard/StandardDecryptor.java @@ -123,7 +123,7 @@ public class StandardDecryptor extends Decryptor { } public InputStream getDataStream(DirectoryNode dir) throws IOException { - DocumentInputStream dis = dir.createDocumentInputStream("EncryptedPackage"); + DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY); _length = dis.readLong(); diff --git a/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java b/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java index 7049c715de..ae6304fbb5 100644 --- a/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java @@ -166,7 +166,7 @@ public class StandardEncryptor extends Encryptor { void writeToPOIFS() throws IOException { int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE); - dir.createDocument("EncryptedPackage", oleStreamSize, this); + dir.createDocument(DEFAULT_POIFS_ENTRY, oleStreamSize, this); // TODO: any properties??? } diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java b/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java index 05499685f5..3af78128c8 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java @@ -279,7 +279,7 @@ public class AgileDecryptor extends Decryptor { } public InputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { - DocumentInputStream dis = dir.createDocumentInputStream("EncryptedPackage"); + DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY); _length = dis.readLong(); ChunkedCipherInputStream cipherStream = new AgileCipherInputStream(dis, _length); diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java index 2cd9889bd2..fd494ba267 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java @@ -111,7 +111,7 @@ public class TestEncryptor { long decPackLenExpected = decExpected.getLength(); assertEquals(decPackLenExpected, payloadExpected.length); - is = nfs.getRoot().createDocumentInputStream("EncryptedPackage"); + is = nfs.getRoot().createDocumentInputStream(Decryptor.DEFAULT_POIFS_ENTRY); is = new BoundedInputStream(is, is.available()-16); // ignore padding block byte encPackExpected[] = IOUtils.toByteArray(is); is.close(); @@ -163,7 +163,7 @@ public class TestEncryptor { long decPackLenActual = decActual.getLength(); - is = nfs.getRoot().createDocumentInputStream("EncryptedPackage"); + is = nfs.getRoot().createDocumentInputStream(Decryptor.DEFAULT_POIFS_ENTRY); is = new BoundedInputStream(is, is.available()-16); // ignore padding block byte encPackActual[] = IOUtils.toByteArray(is); is.close(); -- 2.39.5