mirror of https://github.com/apache/poi.git
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:
parent
d00da9fe03
commit
25cf50433d
|
@ -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");
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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???
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue