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
This commit is contained in:
Nick Burch 2015-04-29 19:47:35 +00:00
parent b3bd87d3c2
commit fb4bf25259
11 changed files with 17 additions and 11 deletions

View File

@ -27,6 +27,7 @@ import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -43,7 +44,7 @@ public final class POIXMLDocumentHandler {
protected static boolean isEncrypted(InputStream stream) throws IOException { protected static boolean isEncrypted(InputStream stream) throws IOException {
if (POIFSFileSystem.hasPOIFSHeader(stream)) { if (POIFSFileSystem.hasPOIFSHeader(stream)) {
POIFSFileSystem poifs = new POIFSFileSystem(stream); POIFSFileSystem poifs = new POIFSFileSystem(stream);
if (poifs.getRoot().hasEntry("EncryptedPackage")) { if (poifs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
return true; return true;
} }
throw new IOException("wrong file format or file extension for OO XML file"); throw new IOException("wrong file format or file extension for OO XML file");

View File

@ -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.aggregates.RecordAggregate.RecordVisitor;
import org.apache.poi.hssf.record.common.UnicodeString; import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.util.CellReference; 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.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.EntryUtils; 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 // check for an encrypted .xlsx file - they get OLE2 wrapped
try { try {
directory.getEntry("EncryptedPackage"); directory.getEntry(Decryptor.DEFAULT_POIFS_ENTRY);
throw new EncryptedDocumentException("The supplied spreadsheet seems to be an Encrypted .xlsx file. " + 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"); "It must be decrypted before use by XSSF, it cannot be used by HSSF");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {

View File

@ -16,6 +16,8 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.poifs.crypt; package org.apache.poi.poifs.crypt;
import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -133,7 +135,7 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE); int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE);
calculateChecksum(fileOut, oleStreamSize); calculateChecksum(fileOut, oleStreamSize);
dir.createDocument("EncryptedPackage", oleStreamSize, new EncryptedPackageWriter()); dir.createDocument(DEFAULT_POIFS_ENTRY, oleStreamSize, new EncryptedPackageWriter());
createEncryptionInfoEntry(dir, fileOut); createEncryptionInfoEntry(dir, fileOut);
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
throw new IOException(e); throw new IOException(e);

View File

@ -36,7 +36,7 @@ public class DataSpaceMapUtils {
public static void addDefaultDataSpace(DirectoryEntry dir) throws IOException { public static void addDefaultDataSpace(DirectoryEntry dir) throws IOException {
DataSpaceMapEntry dsme = new DataSpaceMapEntry( DataSpaceMapEntry dsme = new DataSpaceMapEntry(
new int[]{ 0 } new int[]{ 0 }
, new String[]{ "EncryptedPackage" } , new String[]{ Decryptor.DEFAULT_POIFS_ENTRY }
, "StrongEncryptionDataSpace" , "StrongEncryptionDataSpace"
); );
DataSpaceMap dsm = new DataSpaceMap(new DataSpaceMapEntry[]{dsme}); DataSpaceMap dsm = new DataSpaceMap(new DataSpaceMapEntry[]{dsme});

View File

@ -29,6 +29,7 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public abstract class Decryptor { public abstract class Decryptor {
public static final String DEFAULT_PASSWORD="VelvetSweatshop"; public static final String DEFAULT_PASSWORD="VelvetSweatshop";
public static final String DEFAULT_POIFS_ENTRY="EncryptedPackage";
protected final EncryptionInfoBuilder builder; protected final EncryptionInfoBuilder builder;
private SecretKey secretKey; private SecretKey secretKey;

View File

@ -27,6 +27,7 @@ import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public abstract class Encryptor { public abstract class Encryptor {
protected static final String DEFAULT_POIFS_ENTRY = Decryptor.DEFAULT_POIFS_ENTRY;
private SecretKey secretKey; private SecretKey secretKey;
/** /**

View File

@ -118,7 +118,7 @@ public class BinaryRC4Decryptor extends Decryptor {
public InputStream getDataStream(DirectoryNode dir) throws IOException, public InputStream getDataStream(DirectoryNode dir) throws IOException,
GeneralSecurityException { GeneralSecurityException {
DocumentInputStream dis = dir.createDocumentInputStream("EncryptedPackage"); DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY);
_length = dis.readLong(); _length = dis.readLong();
BinaryRC4CipherInputStream cipherStream = new BinaryRC4CipherInputStream(dis, _length); BinaryRC4CipherInputStream cipherStream = new BinaryRC4CipherInputStream(dis, _length);
return cipherStream; return cipherStream;

View File

@ -123,7 +123,7 @@ public class StandardDecryptor extends Decryptor {
} }
public InputStream getDataStream(DirectoryNode dir) throws IOException { public InputStream getDataStream(DirectoryNode dir) throws IOException {
DocumentInputStream dis = dir.createDocumentInputStream("EncryptedPackage"); DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY);
_length = dis.readLong(); _length = dis.readLong();

View File

@ -166,7 +166,7 @@ public class StandardEncryptor extends Encryptor {
void writeToPOIFS() throws IOException { void writeToPOIFS() throws IOException {
int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE); int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE);
dir.createDocument("EncryptedPackage", oleStreamSize, this); dir.createDocument(DEFAULT_POIFS_ENTRY, oleStreamSize, this);
// TODO: any properties??? // TODO: any properties???
} }

View File

@ -279,7 +279,7 @@ public class AgileDecryptor extends Decryptor {
} }
public InputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { public InputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException {
DocumentInputStream dis = dir.createDocumentInputStream("EncryptedPackage"); DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY);
_length = dis.readLong(); _length = dis.readLong();
ChunkedCipherInputStream cipherStream = new AgileCipherInputStream(dis, _length); ChunkedCipherInputStream cipherStream = new AgileCipherInputStream(dis, _length);

View File

@ -111,7 +111,7 @@ public class TestEncryptor {
long decPackLenExpected = decExpected.getLength(); long decPackLenExpected = decExpected.getLength();
assertEquals(decPackLenExpected, payloadExpected.length); 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 is = new BoundedInputStream(is, is.available()-16); // ignore padding block
byte encPackExpected[] = IOUtils.toByteArray(is); byte encPackExpected[] = IOUtils.toByteArray(is);
is.close(); is.close();
@ -163,7 +163,7 @@ public class TestEncryptor {
long decPackLenActual = decActual.getLength(); 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 is = new BoundedInputStream(is, is.available()-16); // ignore padding block
byte encPackActual[] = IOUtils.toByteArray(is); byte encPackActual[] = IOUtils.toByteArray(is);
is.close(); is.close();