add encryption support

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/hssf_cryptoapi@1756964 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-08-19 20:23:16 +00:00
parent 5a486ec7c8
commit 9521546156
25 changed files with 588 additions and 191 deletions

View File

@ -18,6 +18,7 @@
package org.apache.poi.hssf.model; package org.apache.poi.hssf.model;
import java.security.AccessControlException; import java.security.AccessControlException;
import java.security.GeneralSecurityException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -25,6 +26,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.crypto.SecretKey;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ddf.EscherBSERecord; import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.ddf.EscherBoolProperty; import org.apache.poi.ddf.EscherBoolProperty;
import org.apache.poi.ddf.EscherContainerRecord; import org.apache.poi.ddf.EscherContainerRecord;
@ -52,6 +56,7 @@ import org.apache.poi.hssf.record.EscherAggregate;
import org.apache.poi.hssf.record.ExtSSTRecord; import org.apache.poi.hssf.record.ExtSSTRecord;
import org.apache.poi.hssf.record.ExtendedFormatRecord; import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.ExternSheetRecord; import org.apache.poi.hssf.record.ExternSheetRecord;
import org.apache.poi.hssf.record.FilePassRecord;
import org.apache.poi.hssf.record.FileSharingRecord; import org.apache.poi.hssf.record.FileSharingRecord;
import org.apache.poi.hssf.record.FnGroupCountRecord; import org.apache.poi.hssf.record.FnGroupCountRecord;
import org.apache.poi.hssf.record.FontRecord; import org.apache.poi.hssf.record.FontRecord;
@ -82,8 +87,13 @@ import org.apache.poi.hssf.record.WindowProtectRecord;
import org.apache.poi.hssf.record.WriteAccessRecord; import org.apache.poi.hssf.record.WriteAccessRecord;
import org.apache.poi.hssf.record.WriteProtectRecord; import org.apache.poi.hssf.record.WriteProtectRecord;
import org.apache.poi.hssf.record.common.UnicodeString; import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.crypt.CryptoFunctions; import org.apache.poi.poifs.crypt.CryptoFunctions;
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.ss.formula.EvaluationWorkbook.ExternalName; import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalName;
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet; import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheetRange; import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheetRange;
@ -1082,10 +1092,8 @@ public final class InternalWorkbook {
SSTRecord sst = null; SSTRecord sst = null;
int sstPos = 0; int sstPos = 0;
boolean wroteBoundSheets = false; boolean wroteBoundSheets = false;
for ( int k = 0; k < records.size(); k++ ) for ( Record record : records ) {
{
Record record = records.get( k );
int len = 0; int len = 0;
if (record instanceof SSTRecord) if (record instanceof SSTRecord)
{ {
@ -1124,6 +1132,8 @@ public final class InternalWorkbook {
* Include in it ant code that modifies the workbook record stream and affects its size. * Include in it ant code that modifies the workbook record stream and affects its size.
*/ */
public void preSerialize(){ public void preSerialize(){
updateEncryptionRecord();
// Ensure we have enough tab IDs // Ensure we have enough tab IDs
// Can be a few short if new sheets were added // Can be a few short if new sheets were added
if(records.getTabpos() > 0) { if(records.getTabpos() > 0) {
@ -1134,6 +1144,49 @@ public final class InternalWorkbook {
} }
} }
private void updateEncryptionRecord() {
FilePassRecord fpr = null;
int fprPos = -1;
for (Record r : records.getRecords()) {
fprPos++;
if (r instanceof FilePassRecord) {
fpr = (FilePassRecord)r;
break;
}
}
String password = Biff8EncryptionKey.getCurrentUserPassword();
if (password == null) {
if (fpr != null) {
// need to remove password data
records.remove(fprPos);
}
return;
} else {
// create password record
if (fpr == null) {
fpr = new FilePassRecord(EncryptionMode.binaryRC4);
records.add(1, fpr);
}
// check if the password has been changed
EncryptionInfo ei = fpr.getEncryptionInfo();
byte encVer[] = ei.getVerifier().getEncryptedVerifier();
try {
Decryptor dec = ei.getDecryptor();
Encryptor enc = ei.getEncryptor();
if (encVer == null || !dec.verifyPassword(password)) {
enc.confirmPassword(password);
} else {
SecretKey sk = dec.getSecretKey();
ei.getEncryptor().setSecretKey(sk);
}
} catch (GeneralSecurityException e) {
throw new EncryptedDocumentException("can't validate/update encryption setting", e);
}
}
}
public int getSize() public int getSize()
{ {
int retval = 0; int retval = 0;

View File

@ -24,6 +24,8 @@ import java.util.List;
import org.apache.poi.util.BitField; import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory; import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.HexDump; import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
import org.apache.poi.util.LittleEndianOutput; import org.apache.poi.util.LittleEndianOutput;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
import org.apache.poi.ss.util.WorkbookUtil; import org.apache.poi.ss.util.WorkbookUtil;
@ -60,7 +62,9 @@ public final class BoundSheetRecord extends StandardRecord {
* @param in the record stream to read from * @param in the record stream to read from
*/ */
public BoundSheetRecord(RecordInputStream in) { public BoundSheetRecord(RecordInputStream in) {
field_1_position_of_BOF = in.readInt(); byte buf[] = new byte[LittleEndianConsts.INT_SIZE];
in.readPlain(buf, 0, buf.length);
field_1_position_of_BOF = LittleEndian.getInt(buf);
field_2_option_flags = in.readUShort(); field_2_option_flags = in.readUShort();
int field_3_sheetname_length = in.readUByte(); int field_3_sheetname_length = in.readUByte();
field_4_isMultibyteUnicode = in.readByte(); field_4_isMultibyteUnicode = in.readByte();

View File

@ -56,6 +56,11 @@ public final class FilePassRecord extends StandardRecord implements Cloneable {
} }
} }
public FilePassRecord(EncryptionMode encryptionMode) {
encryptionType = (encryptionMode == EncryptionMode.xor) ? ENCRYPTION_XOR : ENCRYPTION_OTHER;
encryptionInfo = new EncryptionInfo(encryptionMode);
}
public FilePassRecord(RecordInputStream in) { public FilePassRecord(RecordInputStream in) {
encryptionType = in.readUShort(); encryptionType = in.readUShort();

View File

@ -78,20 +78,16 @@ public final class RecordFactoryInputStream {
outputRecs.add(rec); outputRecs.add(rec);
} }
// If it's a FILEPASS, track it specifically but // If it's a FILEPASS, track it specifically
// don't include it in the main stream
if (rec instanceof FilePassRecord) { if (rec instanceof FilePassRecord) {
fpr = (FilePassRecord) rec; fpr = (FilePassRecord) rec;
outputRecs.remove(outputRecs.size()-1); }
// TODO - add fpr not added to outputRecs
rec = outputRecs.get(0); // workbook not encrypted (typical case)
} else { if (rec instanceof EOFRecord) {
// workbook not encrypted (typical case) // A workbook stream is never empty, so crash instead
if (rec instanceof EOFRecord) { // of trying to keep track of nesting level
// A workbook stream is never empty, so crash instead throw new IllegalStateException("Nothing between BOF and EOF");
// of trying to keep track of nesting level
throw new IllegalStateException("Nothing between BOF and EOF");
}
} }
} }
} else { } else {

View File

@ -309,13 +309,22 @@ public final class RecordInputStream implements LittleEndianInput {
} }
return result; return result;
} }
public void readPlain(byte[] buf, int off, int len) {
readFully(buf, 0, buf.length, true);
}
@Override @Override
public void readFully(byte[] buf) { public void readFully(byte[] buf) {
readFully(buf, 0, buf.length); readFully(buf, 0, buf.length, false);
} }
@Override @Override
public void readFully(byte[] buf, int off, int len) { public void readFully(byte[] buf, int off, int len) {
readFully(buf, off, len, false);
}
protected void readFully(byte[] buf, int off, int len, boolean isPlain) {
int origLen = len; int origLen = len;
if (buf == null) { if (buf == null) {
throw new NullPointerException(); throw new NullPointerException();
@ -335,7 +344,11 @@ public final class RecordInputStream implements LittleEndianInput {
} }
} }
checkRecordPosition(nextChunk); checkRecordPosition(nextChunk);
_dataInput.readFully(buf, off, nextChunk); if (isPlain) {
_dataInput.readPlain(buf, off, nextChunk);
} else {
_dataInput.readFully(buf, off, nextChunk);
}
_currentDataOffset+=nextChunk; _currentDataOffset+=nextChunk;
off += nextChunk; off += nextChunk;
len -= nextChunk; len -= nextChunk;

View File

@ -54,28 +54,34 @@ public class ContinuableRecordInput implements LittleEndianInput {
public ContinuableRecordInput(RecordInputStream in){ public ContinuableRecordInput(RecordInputStream in){
_in = in; _in = in;
} }
@Override
public int available(){ public int available(){
return _in.available(); return _in.available();
} }
@Override
public byte readByte(){ public byte readByte(){
return _in.readByte(); return _in.readByte();
} }
@Override
public int readUByte(){ public int readUByte(){
return _in.readUByte(); return _in.readUByte();
} }
@Override
public short readShort(){ public short readShort(){
return _in.readShort(); return _in.readShort();
} }
@Override
public int readUShort(){ public int readUShort(){
int ch1 = readUByte(); int ch1 = readUByte();
int ch2 = readUByte(); int ch2 = readUByte();
return (ch2 << 8) + (ch1 << 0); return (ch2 << 8) + (ch1 << 0);
} }
@Override
public int readInt(){ public int readInt(){
int ch1 = _in.readUByte(); int ch1 = _in.readUByte();
int ch2 = _in.readUByte(); int ch2 = _in.readUByte();
@ -84,6 +90,7 @@ public class ContinuableRecordInput implements LittleEndianInput {
return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0); return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0);
} }
@Override
public long readLong(){ public long readLong(){
int b0 = _in.readUByte(); int b0 = _in.readUByte();
int b1 = _in.readUByte(); int b1 = _in.readUByte();
@ -103,14 +110,23 @@ public class ContinuableRecordInput implements LittleEndianInput {
(b0 << 0)); (b0 << 0));
} }
@Override
public double readDouble(){ public double readDouble(){
return _in.readDouble(); return _in.readDouble();
} }
@Override
public void readFully(byte[] buf){ public void readFully(byte[] buf){
_in.readFully(buf); _in.readFully(buf);
} }
@Override
public void readFully(byte[] buf, int off, int len){ public void readFully(byte[] buf, int off, int len){
_in.readFully(buf, off, len); _in.readFully(buf, off, len);
} }
@Override
public void readPlain(byte[] buf, int off, int len) {
readFully(buf, off, len);
}
} }

View File

@ -17,7 +17,6 @@
package org.apache.poi.hssf.record.crypto; package org.apache.poi.hssf.record.crypto;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.PushbackInputStream; import java.io.PushbackInputStream;
@ -35,7 +34,7 @@ import org.apache.poi.util.LittleEndianInput;
public final class Biff8DecryptingStream implements BiffHeaderInput, LittleEndianInput { public final class Biff8DecryptingStream implements BiffHeaderInput, LittleEndianInput {
private static final int RC4_REKEYING_INTERVAL = 1024; public static final int RC4_REKEYING_INTERVAL = 1024;
private final EncryptionInfo info; private final EncryptionInfo info;
private ChunkedCipherInputStream ccis; private ChunkedCipherInputStream ccis;
@ -180,7 +179,7 @@ public final class Biff8DecryptingStream implements BiffHeaderInput, LittleEndia
* *
* @return <code>true</code> if record type specified by <tt>sid</tt> is never encrypted * @return <code>true</code> if record type specified by <tt>sid</tt> is never encrypted
*/ */
private static boolean isNeverEncryptedRecord(int sid) { public static boolean isNeverEncryptedRecord(int sid) {
switch (sid) { switch (sid) {
case BOFRecord.sid: case BOFRecord.sid:
// sheet BOFs for sure // sheet BOFs for sure
@ -204,15 +203,9 @@ public final class Biff8DecryptingStream implements BiffHeaderInput, LittleEndia
} }
} }
private void readPlain(byte b[], int off, int len) { @Override
try { public void readPlain(byte b[], int off, int len) {
int readBytes = ccis.readPlain(b, off, len); ccis.readPlain(b, off, len);
if (readBytes < len) {
throw new RecordFormatException("buffer underrun");
}
} catch (IOException e) {
throw new RecordFormatException(e);
}
} }
} }

View File

@ -23,6 +23,7 @@ import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAME
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -61,8 +62,10 @@ import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.model.RecordStream; import org.apache.poi.hssf.model.RecordStream;
import org.apache.poi.hssf.record.AbstractEscherHolderRecord; import org.apache.poi.hssf.record.AbstractEscherHolderRecord;
import org.apache.poi.hssf.record.BackupRecord; import org.apache.poi.hssf.record.BackupRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.DrawingGroupRecord; import org.apache.poi.hssf.record.DrawingGroupRecord;
import org.apache.poi.hssf.record.ExtendedFormatRecord; import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.FilePassRecord;
import org.apache.poi.hssf.record.FontRecord; import org.apache.poi.hssf.record.FontRecord;
import org.apache.poi.hssf.record.LabelRecord; import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.LabelSSTRecord;
@ -74,8 +77,11 @@ import org.apache.poi.hssf.record.SSTRecord;
import org.apache.poi.hssf.record.UnknownRecord; 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.record.crypto.Biff8DecryptingStream;
import org.apache.poi.hssf.util.CellReference; import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.poifs.crypt.ChunkedCipherOutputStream;
import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.Encryptor;
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.DocumentNode; import org.apache.poi.poifs.filesystem.DocumentNode;
@ -99,8 +105,11 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil; import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.util.Configurator; import org.apache.poi.util.Configurator;
import org.apache.poi.util.HexDump; import org.apache.poi.util.HexDump;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianByteArrayInputStream;
import org.apache.poi.util.LittleEndianByteArrayOutputStream;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
@ -1443,7 +1452,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
if (log.check( POILogger.DEBUG )) { if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "HSSFWorkbook.getBytes()"); log.log(DEBUG, "HSSFWorkbook.getBytes()");
} }
HSSFSheet[] sheets = getSheets(); HSSFSheet[] sheets = getSheets();
int nSheets = sheets.length; int nSheets = sheets.length;
@ -1485,9 +1494,70 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
} }
pos += serializedSize; pos += serializedSize;
} }
encryptBytes(retval);
return retval; return retval;
} }
@SuppressWarnings("resource")
protected void encryptBytes(byte buf[]) {
int initialOffset = 0;
FilePassRecord fpr = null;
for (Record r : workbook.getRecords()) {
initialOffset += r.getRecordSize();
if (r instanceof FilePassRecord) {
fpr = (FilePassRecord)r;
break;
}
}
if (fpr == null) {
return;
}
LittleEndianByteArrayInputStream plain = new LittleEndianByteArrayInputStream(buf, 0);
LittleEndianByteArrayOutputStream leos = new LittleEndianByteArrayOutputStream(buf, 0);
Encryptor enc = fpr.getEncryptionInfo().getEncryptor();
enc.setChunkSize(Biff8DecryptingStream.RC4_REKEYING_INTERVAL);
byte tmp[] = new byte[1024];
try {
ChunkedCipherOutputStream os = enc.getDataStream(leos, initialOffset);
int totalBytes = 0;
while (totalBytes < buf.length) {
plain.read(tmp, 0, 4);
final int sid = LittleEndian.getUShort(tmp, 0);
final int len = LittleEndian.getUShort(tmp, 2);
boolean isPlain = Biff8DecryptingStream.isNeverEncryptedRecord(sid);
os.setNextRecordSize(len, isPlain);
os.writePlain(tmp, 0, 4);
if (sid == BoundSheetRecord.sid) {
// special case for the field_1_position_of_BOF (=lbPlyPos) field of
// the BoundSheet8 record which must be unencrypted
byte bsrBuf[] = new byte[len];
plain.readFully(bsrBuf);
os.writePlain(bsrBuf, 0, 4);
os.write(bsrBuf, 4, len-4);
} else {
int todo = len;
while (todo > 0) {
int nextLen = Math.min(todo, tmp.length);
plain.readFully(tmp, 0, nextLen);
if (isPlain) {
os.writePlain(tmp, 0, nextLen);
} else {
os.write(tmp, 0, nextLen);
}
todo -= nextLen;
}
}
totalBytes += 4 + len;
}
os.close();
} catch (Exception e) {
throw new EncryptedDocumentException(e);
}
}
/*package*/ InternalWorkbook getWorkbook() { /*package*/ InternalWorkbook getWorkbook() {
return workbook; return workbook;
} }

View File

@ -222,24 +222,33 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream {
/** /**
* Used when BIFF header fields (sid, size) are being read. The internal * Used when BIFF header fields (sid, size) are being read. The internal
* {@link Cipher} instance must step even when unencrypted bytes are read * {@link Cipher} instance must step even when unencrypted bytes are read
*
*/ */
public int readPlain(byte b[], int off, int len) throws IOException { @Override
public void readPlain(byte b[], int off, int len) {
if (len <= 0) { if (len <= 0) {
return len; return;
} }
int readBytes, total = 0; try {
do { int readBytes, total = 0;
readBytes = read(b, off, len, true); do {
total += Math.max(0, readBytes); readBytes = read(b, off, len, true);
} while (readBytes > -1 && total < len); total += Math.max(0, readBytes);
} while (readBytes > -1 && total < len);
return total;
if (total < len) {
throw new EOFException("buffer underrun");
}
} catch (IOException e) {
// need to wrap checked exception, because of LittleEndianInput interface :(
throw new RuntimeException(e);
}
} }
/** /**
* Some ciphers (actually just XOR) are based on the record size, * Some ciphers (actually just XOR) are based on the record size,
* which needs to be set before encryption * which needs to be set before decryption
* *
* @param recordSize the size of the next record * @param recordSize the size of the next record
*/ */

View File

@ -25,6 +25,7 @@ import java.io.FilterOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.util.BitSet;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher; import javax.crypto.Cipher;
@ -48,14 +49,17 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
private static final POILogger LOG = POILogFactory.getLogger(ChunkedCipherOutputStream.class); private static final POILogger LOG = POILogFactory.getLogger(ChunkedCipherOutputStream.class);
private static final int STREAMING = -1; private static final int STREAMING = -1;
protected final int _chunkSize; private final int _chunkSize;
protected final int _chunkBits; private final int _chunkBits;
private final byte[] _chunk; private final byte[] _chunk;
private final BitSet _plainByteFlags;
private final File _fileOut; private final File _fileOut;
private final DirectoryNode _dir; private final DirectoryNode _dir;
private long _pos = 0; private long _pos = 0;
private long _totalPos = 0;
private long _written = 0;
private Cipher _cipher; private Cipher _cipher;
public ChunkedCipherOutputStream(DirectoryNode dir, int chunkSize) throws IOException, GeneralSecurityException { public ChunkedCipherOutputStream(DirectoryNode dir, int chunkSize) throws IOException, GeneralSecurityException {
@ -63,6 +67,7 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
this._chunkSize = chunkSize; this._chunkSize = chunkSize;
int cs = chunkSize == STREAMING ? 4096 : chunkSize; int cs = chunkSize == STREAMING ? 4096 : chunkSize;
_chunk = new byte[cs]; _chunk = new byte[cs];
_plainByteFlags = new BitSet(cs);
_chunkBits = Integer.bitCount(cs-1); _chunkBits = Integer.bitCount(cs-1);
_fileOut = TempFile.createTempFile("encrypted_package", "crypt"); _fileOut = TempFile.createTempFile("encrypted_package", "crypt");
_fileOut.deleteOnExit(); _fileOut.deleteOnExit();
@ -76,6 +81,7 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
this._chunkSize = chunkSize; this._chunkSize = chunkSize;
int cs = chunkSize == STREAMING ? 4096 : chunkSize; int cs = chunkSize == STREAMING ? 4096 : chunkSize;
_chunk = new byte[cs]; _chunk = new byte[cs];
_plainByteFlags = new BitSet(cs);
_chunkBits = Integer.bitCount(cs-1); _chunkBits = Integer.bitCount(cs-1);
_fileOut = null; _fileOut = null;
_dir = null; _dir = null;
@ -106,8 +112,15 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
} }
@Override @Override
public void write(byte[] b, int off, int len) public void write(byte[] b, int off, int len) throws IOException {
throws IOException { write(b, off, len, false);
}
public void writePlain(byte[] b, int off, int len) throws IOException {
write(b, off, len, true);
}
protected void write(byte[] b, int off, int len, boolean writePlain) throws IOException {
if (len == 0) { if (len == 0) {
return; return;
} }
@ -121,7 +134,11 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
int posInChunk = (int)(_pos & chunkMask); int posInChunk = (int)(_pos & chunkMask);
int nextLen = Math.min(_chunk.length-posInChunk, len); int nextLen = Math.min(_chunk.length-posInChunk, len);
System.arraycopy(b, off, _chunk, posInChunk, nextLen); System.arraycopy(b, off, _chunk, posInChunk, nextLen);
if (writePlain) {
_plainByteFlags.set(posInChunk, posInChunk+nextLen);
}
_pos += nextLen; _pos += nextLen;
_totalPos += nextLen;
off += nextLen; off += nextLen;
len -= nextLen; len -= nextLen;
if ((_pos & chunkMask) == 0) { if ((_pos & chunkMask) == 0) {
@ -130,12 +147,12 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
} }
} }
private int getChunkMask() { protected int getChunkMask() {
return _chunk.length-1; return _chunk.length-1;
} }
protected void writeChunk(boolean continued) throws IOException { protected void writeChunk(boolean continued) throws IOException {
if (_pos == 0) { if (_pos == 0 || _totalPos == _written) {
return; return;
} }
@ -157,14 +174,18 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
int ciLen; int ciLen;
try { try {
boolean doFinal = true; boolean doFinal = true;
long oldPos = _pos;
// reset stream (not only) in case we were interrupted by plain stream parts
// this also needs to be set to prevent an endless loop
_pos = 0;
if (_chunkSize == STREAMING) { if (_chunkSize == STREAMING) {
if (continued) { if (continued) {
doFinal = false; doFinal = false;
} }
// reset stream (not only) in case we were interrupted by plain stream parts
_pos = 0;
} else { } else {
_cipher = initCipherForBlock(_cipher, index, lastChunk); _cipher = initCipherForBlock(_cipher, index, lastChunk);
// restore pos - only streaming chunks will be reset
_pos = oldPos;
} }
ciLen = invokeCipher(posInChunk, doFinal); ciLen = invokeCipher(posInChunk, doFinal);
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
@ -172,6 +193,8 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
} }
out.write(_chunk, 0, ciLen); out.write(_chunk, 0, ciLen);
_plainByteFlags.clear();
_written += ciLen;
} }
/** /**
@ -184,11 +207,17 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
* @throws ShortBufferException * @throws ShortBufferException
*/ */
protected int invokeCipher(int posInChunk, boolean doFinal) throws GeneralSecurityException { protected int invokeCipher(int posInChunk, boolean doFinal) throws GeneralSecurityException {
if (doFinal) { byte plain[] = (_plainByteFlags.isEmpty()) ? null : _chunk.clone();
return _cipher.doFinal(_chunk, 0, posInChunk, _chunk);
} else { int ciLen = (doFinal)
return _cipher.update(_chunk, 0, posInChunk, _chunk); ? _cipher.doFinal(_chunk, 0, posInChunk, _chunk)
: _cipher.update(_chunk, 0, posInChunk, _chunk);
for (int i = _plainByteFlags.nextSetBit(0); i >= 0 && i < posInChunk; i = _plainByteFlags.nextSetBit(i+1)) {
_chunk[i] = plain[i];
} }
return ciLen;
} }
@Override @Override
@ -208,7 +237,33 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
throw new IOException(e); throw new IOException(e);
} }
} }
protected byte[] getChunk() {
return _chunk;
}
protected BitSet getPlainByteFlags() {
return _plainByteFlags;
}
protected long getPos() {
return _pos;
}
protected long getTotalPos() {
return _totalPos;
}
/**
* Some ciphers (actually just XOR) are based on the record size,
* which needs to be set before encryption
*
* @param recordSize the size of the next record
* @param isPlain {@code true} if the record is unencrypted
*/
public void setNextRecordSize(int recordSize, boolean isPlain) {
}
private class EncryptedPackageWriter implements POIFSWriterListener { private class EncryptedPackageWriter implements POIFSWriterListener {
@Override @Override
public void processPOIFSWriterEvent(POIFSWriterEvent event) { public void processPOIFSWriterEvent(POIFSWriterEvent event) {

View File

@ -498,7 +498,9 @@ public class CryptoFunctions {
* @return the byte array for xor obfuscation * @return the byte array for xor obfuscation
*/ */
public static byte[] createXorArray1(String password) { public static byte[] createXorArray1(String password) {
if (password.length() > 15) password = password.substring(0, 15); if (password.length() > 15) {
password = password.substring(0, 15);
}
byte passBytes[] = password.getBytes(Charset.forName("ASCII")); byte passBytes[] = password.getBytes(Charset.forName("ASCII"));
// this code is based on the libre office implementation. // this code is based on the libre office implementation.

View File

@ -61,11 +61,16 @@ public abstract class Encryptor implements Cloneable {
return getDataStream(fs.getRoot()); return getDataStream(fs.getRoot());
} }
public ChunkedCipherOutputStream getDataStream(OutputStream stream, int initialOffset)
throws IOException, GeneralSecurityException {
throw new RuntimeException("this decryptor doesn't support writing directly to a stream");
}
public SecretKey getSecretKey() { public SecretKey getSecretKey() {
return secretKey; return secretKey;
} }
protected void setSecretKey(SecretKey secretKey) { public void setSecretKey(SecretKey secretKey) {
this.secretKey = secretKey; this.secretKey = secretKey;
} }
@ -77,6 +82,17 @@ public abstract class Encryptor implements Cloneable {
this.encryptionInfo = encryptionInfo; this.encryptionInfo = encryptionInfo;
} }
/**
* Sets the chunk size of the data stream.
* Needs to be set before the data stream is requested.
* When not set, the implementation uses method specific default values
*
* @param chunkSize the chunk size, i.e. the block size with the same encryption key
*/
public void setChunkSize(int chunkSize) {
throw new RuntimeException("this decryptor doesn't support changing the chunk size");
}
@Override @Override
public Encryptor clone() throws CloneNotSupportedException { public Encryptor clone() throws CloneNotSupportedException {
Encryptor other = (Encryptor)super.clone(); Encryptor other = (Encryptor)super.clone();

View File

@ -41,6 +41,8 @@ import org.apache.poi.util.LittleEndianByteArrayOutputStream;
public class BinaryRC4Encryptor extends Encryptor implements Cloneable { public class BinaryRC4Encryptor extends Encryptor implements Cloneable {
private int _chunkSize = 512;
protected BinaryRC4Encryptor() { protected BinaryRC4Encryptor() {
} }
@ -84,6 +86,12 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable {
return countStream; return countStream;
} }
@Override
public BinaryRC4CipherOutputStream getDataStream(OutputStream stream, int initialOffset)
throws IOException, GeneralSecurityException {
return new BinaryRC4CipherOutputStream(stream);
}
protected int getKeySizeInBytes() { protected int getKeySizeInBytes() {
return getEncryptionInfo().getHeader().getKeySize() / 8; return getEncryptionInfo().getHeader().getKeySize() / 8;
} }
@ -105,6 +113,11 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable {
DataSpaceMapUtils.createEncryptionEntry(dir, "EncryptionInfo", er); DataSpaceMapUtils.createEncryptionEntry(dir, "EncryptionInfo", er);
} }
@Override
public void setChunkSize(int chunkSize) {
_chunkSize = chunkSize;
}
@Override @Override
public BinaryRC4Encryptor clone() throws CloneNotSupportedException { public BinaryRC4Encryptor clone() throws CloneNotSupportedException {
return (BinaryRC4Encryptor)super.clone(); return (BinaryRC4Encryptor)super.clone();
@ -112,12 +125,22 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable {
protected class BinaryRC4CipherOutputStream extends ChunkedCipherOutputStream { protected class BinaryRC4CipherOutputStream extends ChunkedCipherOutputStream {
public BinaryRC4CipherOutputStream(OutputStream stream)
throws IOException, GeneralSecurityException {
super(stream, BinaryRC4Encryptor.this._chunkSize);
}
public BinaryRC4CipherOutputStream(DirectoryNode dir)
throws IOException, GeneralSecurityException {
super(dir, BinaryRC4Encryptor.this._chunkSize);
}
@Override @Override
protected Cipher initCipherForBlock(Cipher cipher, int block, boolean lastChunk) protected Cipher initCipherForBlock(Cipher cipher, int block, boolean lastChunk)
throws GeneralSecurityException { throws GeneralSecurityException {
return BinaryRC4Decryptor.initCipherForBlock(cipher, block, getEncryptionInfo(), getSecretKey(), Cipher.ENCRYPT_MODE); return BinaryRC4Decryptor.initCipherForBlock(cipher, block, getEncryptionInfo(), getSecretKey(), Cipher.ENCRYPT_MODE);
} }
@Override @Override
protected void calculateChecksum(File file, int i) { protected void calculateChecksum(File file, int i) {
} }
@ -128,9 +151,10 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable {
BinaryRC4Encryptor.this.createEncryptionInfoEntry(dir); BinaryRC4Encryptor.this.createEncryptionInfoEntry(dir);
} }
public BinaryRC4CipherOutputStream(DirectoryNode dir) @Override
throws IOException, GeneralSecurityException { public void flush() throws IOException {
super(dir, 512); writeChunk(false);
super.flush();
} }
} }
} }

View File

@ -111,7 +111,8 @@ public class CryptoAPIEncryptor extends Encryptor implements Cloneable {
throw new IOException("not supported"); throw new IOException("not supported");
} }
public CryptoAPICipherOutputStream getDataStream(OutputStream stream) @Override
public CryptoAPICipherOutputStream getDataStream(OutputStream stream, int initialOffset)
throws IOException, GeneralSecurityException { throws IOException, GeneralSecurityException {
return new CryptoAPICipherOutputStream(stream); return new CryptoAPICipherOutputStream(stream);
} }
@ -212,6 +213,7 @@ public class CryptoAPIEncryptor extends Encryptor implements Cloneable {
return getEncryptionInfo().getHeader().getKeySize() / 8; return getEncryptionInfo().getHeader().getKeySize() / 8;
} }
@Override
public void setChunkSize(int chunkSize) { public void setChunkSize(int chunkSize) {
_chunkSize = chunkSize; _chunkSize = chunkSize;
} }
@ -268,6 +270,7 @@ public class CryptoAPIEncryptor extends Encryptor implements Cloneable {
@Override @Override
public void flush() throws IOException { public void flush() throws IOException {
writeChunk(false); writeChunk(false);
super.flush();
} }
} }

View File

@ -36,80 +36,6 @@ public class XORDecryptor extends Decryptor implements Cloneable {
private long _length = -1L; private long _length = -1L;
private int _chunkSize = 512; private int _chunkSize = 512;
private class XORCipherInputStream extends ChunkedCipherInputStream {
private final int _initialOffset;
private int _recordStart = 0;
private int _recordEnd = 0;
@Override
protected Cipher initCipherForBlock(Cipher existing, int block)
throws GeneralSecurityException {
return XORDecryptor.this.initCipherForBlock(existing, block);
}
public XORCipherInputStream(InputStream stream, int initialPos)
throws GeneralSecurityException {
super(stream, Integer.MAX_VALUE, _chunkSize);
_initialOffset = initialPos;
}
@Override
protected int invokeCipher(int totalBytes, boolean doFinal) {
final int pos = (int)getPos();
final byte xorArray[] = getEncryptionInfo().getDecryptor().getSecretKey().getEncoded();
final byte chunk[] = getChunk();
final byte plain[] = getPlain();
final int posInChunk = pos & getChunkMask();
/*
* From: http://social.msdn.microsoft.com/Forums/en-US/3dadbed3-0e68-4f11-8b43-3a2328d9ebd5
*
* The initial value for XorArrayIndex is as follows:
* XorArrayIndex = (FileOffset + Data.Length) % 16
*
* The FileOffset variable in this context is the stream offset into the Workbook stream at
* the time we are about to write each of the bytes of the record data.
* This (the value) is then incremented after each byte is written.
*/
final int xorArrayIndex = _initialOffset+_recordEnd+(pos-_recordStart);
for (int i=0; pos+i < _recordEnd && i < totalBytes; i++) {
// The following is taken from the Libre Office implementation
// It seems that the encrypt and decrypt method is mixed up
// in the MS-OFFCRYPTO docs
byte value = plain[posInChunk+i];
value = rotateLeft(value, 3);
value ^= xorArray[(xorArrayIndex+i) & 0x0F];
chunk[posInChunk+i] = value;
}
// the other bytes will be encoded, when setNextRecordSize is called the next time
return totalBytes;
}
private byte rotateLeft(byte bits, int shift) {
return (byte)(((bits & 0xff) << shift) | ((bits & 0xff) >>> (8 - shift)));
}
/**
* Decrypts a xor obfuscated byte array.
* The data is decrypted in-place
*
* @see <a href="http://msdn.microsoft.com/en-us/library/dd908506.aspx">2.3.7.3 Binary Document XOR Data Transformation Method 1</a>
*/
@Override
public void setNextRecordSize(int recordSize) {
_recordStart = (int)getPos();
_recordEnd = _recordStart+recordSize;
int pos = (int)getPos();
byte chunk[] = getChunk();
int chunkMask = getChunkMask();
int nextBytes = Math.min(recordSize, chunk.length-(pos & chunkMask));
invokeCipher(nextBytes, true);
}
}
protected XORDecryptor() { protected XORDecryptor() {
} }
@ -166,9 +92,83 @@ public class XORDecryptor extends Decryptor implements Cloneable {
public void setChunkSize(int chunkSize) { public void setChunkSize(int chunkSize) {
_chunkSize = chunkSize; _chunkSize = chunkSize;
} }
@Override @Override
public XORDecryptor clone() throws CloneNotSupportedException { public XORDecryptor clone() throws CloneNotSupportedException {
return (XORDecryptor)super.clone(); return (XORDecryptor)super.clone();
} }
private class XORCipherInputStream extends ChunkedCipherInputStream {
private final int _initialOffset;
private int _recordStart = 0;
private int _recordEnd = 0;
public XORCipherInputStream(InputStream stream, int initialPos)
throws GeneralSecurityException {
super(stream, Integer.MAX_VALUE, _chunkSize);
_initialOffset = initialPos;
}
@Override
protected Cipher initCipherForBlock(Cipher existing, int block)
throws GeneralSecurityException {
return XORDecryptor.this.initCipherForBlock(existing, block);
}
@Override
protected int invokeCipher(int totalBytes, boolean doFinal) {
final int pos = (int)getPos();
final byte xorArray[] = getEncryptionInfo().getDecryptor().getSecretKey().getEncoded();
final byte chunk[] = getChunk();
final byte plain[] = getPlain();
final int posInChunk = pos & getChunkMask();
/*
* From: http://social.msdn.microsoft.com/Forums/en-US/3dadbed3-0e68-4f11-8b43-3a2328d9ebd5
*
* The initial value for XorArrayIndex is as follows:
* XorArrayIndex = (FileOffset + Data.Length) % 16
*
* The FileOffset variable in this context is the stream offset into the Workbook stream at
* the time we are about to write each of the bytes of the record data.
* This (the value) is then incremented after each byte is written.
*/
final int xorArrayIndex = _initialOffset+_recordEnd+(pos-_recordStart);
for (int i=0; pos+i < _recordEnd && i < totalBytes; i++) {
// The following is taken from the Libre Office implementation
// It seems that the encrypt and decrypt method is mixed up
// in the MS-OFFCRYPTO docs
byte value = plain[posInChunk+i];
value = rotateLeft(value, 3);
value ^= xorArray[(xorArrayIndex+i) & 0x0F];
chunk[posInChunk+i] = value;
}
// the other bytes will be encoded, when setNextRecordSize is called the next time
return totalBytes;
}
private byte rotateLeft(byte bits, int shift) {
return (byte)(((bits & 0xff) << shift) | ((bits & 0xff) >>> (8 - shift)));
}
/**
* Decrypts a xor obfuscated byte array.
* The data is decrypted in-place
*
* @see <a href="http://msdn.microsoft.com/en-us/library/dd908506.aspx">2.3.7.3 Binary Document XOR Data Transformation Method 1</a>
*/
@Override
public void setNextRecordSize(int recordSize) {
final int pos = (int)getPos();
final byte chunk[] = getChunk();
final int chunkMask = getChunkMask();
_recordStart = pos;
_recordEnd = _recordStart+recordSize;
int nextBytes = Math.min(recordSize, chunk.length-(pos & chunkMask));
invokeCipher(nextBytes, true);
}
}
} }

View File

@ -27,7 +27,7 @@ public class XOREncryptionHeader extends EncryptionHeader implements EncryptionR
} }
@Override @Override
public void write(LittleEndianByteArrayOutputStream littleendianbytearrayoutputstream) { public void write(LittleEndianByteArrayOutputStream leos) {
} }
@Override @Override

View File

@ -58,4 +58,14 @@ public class XOREncryptionVerifier extends EncryptionVerifier implements Encrypt
public XOREncryptionVerifier clone() throws CloneNotSupportedException { public XOREncryptionVerifier clone() throws CloneNotSupportedException {
return (XOREncryptionVerifier)super.clone(); return (XOREncryptionVerifier)super.clone();
} }
@Override
protected void setEncryptedVerifier(byte[] encryptedVerifier) {
super.setEncryptedVerifier(encryptedVerifier);
}
@Override
protected void setEncryptedKey(byte[] encryptedKey) {
super.setEncryptedKey(encryptedKey);
}
} }

View File

@ -21,37 +21,41 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.MessageDigest; import java.util.BitSet;
import java.security.SecureRandom;
import java.util.Random;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.poifs.crypt.ChunkedCipherOutputStream; import org.apache.poi.poifs.crypt.ChunkedCipherOutputStream;
import org.apache.poi.poifs.crypt.CryptoFunctions; import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.DataSpaceMapUtils;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.poifs.crypt.standard.EncryptionRecord;
import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.util.LittleEndianByteArrayOutputStream; import org.apache.poi.util.LittleEndian;
public class XOREncryptor extends Encryptor implements Cloneable { public class XOREncryptor extends Encryptor implements Cloneable {
protected XOREncryptor() { protected XOREncryptor() {
} }
@Override @Override
public void confirmPassword(String password) { public void confirmPassword(String password) {
int keyComp = CryptoFunctions.createXorKey1(password);
int verifierComp = CryptoFunctions.createXorVerifier1(password);
byte xorArray[] = CryptoFunctions.createXorArray1(password);
byte shortBuf[] = new byte[2];
XOREncryptionVerifier ver = (XOREncryptionVerifier)getEncryptionInfo().getVerifier();
LittleEndian.putUShort(shortBuf, 0, keyComp);
ver.setEncryptedKey(shortBuf);
LittleEndian.putUShort(shortBuf, 0, verifierComp);
ver.setEncryptedVerifier(shortBuf);
setSecretKey(new SecretKeySpec(xorArray, "XOR"));
} }
@Override @Override
public void confirmPassword(String password, byte keySpec[], public void confirmPassword(String password, byte keySpec[],
byte keySalt[], byte verifier[], byte verifierSalt[], byte keySalt[], byte verifier[], byte verifierSalt[],
byte integritySalt[]) { byte integritySalt[]) {
confirmPassword(password);
} }
@Override @Override
@ -61,10 +65,21 @@ public class XOREncryptor extends Encryptor implements Cloneable {
return countStream; return countStream;
} }
@Override
public XORCipherOutputStream getDataStream(OutputStream stream, int initialOffset)
throws IOException, GeneralSecurityException {
return new XORCipherOutputStream(stream, initialOffset);
}
protected int getKeySizeInBytes() { protected int getKeySizeInBytes() {
return -1; return -1;
} }
@Override
public void setChunkSize(int chunkSize) {
// chunkSize is irrelevant
}
protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException { protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException {
} }
@ -73,7 +88,21 @@ public class XOREncryptor extends Encryptor implements Cloneable {
return (XOREncryptor)super.clone(); return (XOREncryptor)super.clone();
} }
protected class XORCipherOutputStream extends ChunkedCipherOutputStream { private class XORCipherOutputStream extends ChunkedCipherOutputStream {
private final int _initialOffset;
private int _recordStart = 0;
private int _recordEnd = 0;
private boolean _isPlain = false;
public XORCipherOutputStream(OutputStream stream, int initialPos) throws IOException, GeneralSecurityException {
super(stream, -1);
_initialOffset = initialPos;
}
public XORCipherOutputStream(DirectoryNode dir) throws IOException, GeneralSecurityException {
super(dir, -1);
_initialOffset = 0;
}
@Override @Override
protected Cipher initCipherForBlock(Cipher cipher, int block, boolean lastChunk) protected Cipher initCipherForBlock(Cipher cipher, int block, boolean lastChunk)
@ -91,9 +120,67 @@ public class XOREncryptor extends Encryptor implements Cloneable {
XOREncryptor.this.createEncryptionInfoEntry(dir); XOREncryptor.this.createEncryptionInfoEntry(dir);
} }
public XORCipherOutputStream(DirectoryNode dir) @Override
throws IOException, GeneralSecurityException { public void setNextRecordSize(int recordSize, boolean isPlain) {
super(dir, 512); if (_recordEnd > 0 && !_isPlain) {
// encrypt last record
invokeCipher((int)getPos(), true);
}
_recordStart = (int)getTotalPos()+4;
_recordEnd = _recordStart+recordSize;
_isPlain = isPlain;
} }
@Override
public void flush() throws IOException {
setNextRecordSize(0, true);
super.flush();
}
@Override
protected int invokeCipher(int posInChunk, boolean doFinal) {
if (posInChunk == 0) {
return 0;
}
final int start = Math.max(posInChunk-(_recordEnd-_recordStart), 0);
final BitSet plainBytes = getPlainByteFlags();
final byte xorArray[] = getEncryptionInfo().getEncryptor().getSecretKey().getEncoded();
final byte chunk[] = getChunk();
final byte plain[] = (plainBytes.isEmpty()) ? null : chunk.clone();
/*
* From: http://social.msdn.microsoft.com/Forums/en-US/3dadbed3-0e68-4f11-8b43-3a2328d9ebd5
*
* The initial value for XorArrayIndex is as follows:
* XorArrayIndex = (FileOffset + Data.Length) % 16
*
* The FileOffset variable in this context is the stream offset into the Workbook stream at
* the time we are about to write each of the bytes of the record data.
* This (the value) is then incremented after each byte is written.
*/
// ... also need to handle invocation in case of a filled chunk
int xorArrayIndex = _recordEnd+(start-_recordStart);
for (int i=start; i < posInChunk; i++) {
byte value = chunk[i];
value ^= xorArray[(xorArrayIndex++) & 0x0F];
value = rotateLeft(value, 8-3);
chunk[i] = value;
}
for (int i = plainBytes.nextSetBit(start); i >= 0 && i < posInChunk; i = plainBytes.nextSetBit(i+1)) {
chunk[i] = plain[i];
}
return posInChunk;
}
private byte rotateLeft(byte bits, int shift) {
return (byte)(((bits & 0xff) << shift) | ((bits & 0xff) >>> (8 - shift)));
}
} }
} }

View File

@ -189,4 +189,9 @@ public class DocumentInputStream extends InputStream implements LittleEndianInpu
int i = readInt(); int i = readInt();
return i & 0xFFFFFFFFL; return i & 0xFFFFFFFFL;
} }
@Override
public void readPlain(byte[] buf, int off, int len) {
readFully(buf, off, len);
}
} }

View File

@ -104,4 +104,9 @@ public final class LittleEndianByteArrayInputStream extends ByteArrayInputStream
checkPosition(buffer.length); checkPosition(buffer.length);
read(buffer, 0, buffer.length); read(buffer, 0, buffer.length);
} }
@Override
public void readPlain(byte[] buf, int off, int len) {
readFully(buf, off, len);
}
} }

View File

@ -16,10 +16,7 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.util; package org.apache.poi.util;
/**
*
* @author Josh Micich
*/
public interface LittleEndianInput { public interface LittleEndianInput {
int available(); int available();
byte readByte(); byte readByte();
@ -31,4 +28,14 @@ public interface LittleEndianInput {
double readDouble(); double readDouble();
void readFully(byte[] buf); void readFully(byte[] buf);
void readFully(byte[] buf, int off, int len); void readFully(byte[] buf, int off, int len);
/**
* Usually acts the same as {@link #readFully(byte[], int, int)}, but
* for an encrypted stream the raw (unencrypted) data is filled
*
* @param buf the byte array to receive the bytes
* @param off the start offset into the byte array
* @param len the amount of bytes to fill
*/
void readPlain(byte[] buf, int off, int len);
} }

View File

@ -34,7 +34,8 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
super(is); super(is);
} }
public int available() { @Override
public int available() {
try { try {
return super.available(); return super.available();
} catch (IOException e) { } catch (IOException e) {
@ -42,11 +43,13 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
} }
} }
public byte readByte() { @Override
public byte readByte() {
return (byte)readUByte(); return (byte)readUByte();
} }
public int readUByte() { @Override
public int readUByte() {
byte buf[] = new byte[1]; byte buf[] = new byte[1];
try { try {
checkEOF(read(buf), 1); checkEOF(read(buf), 1);
@ -56,11 +59,13 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
return LittleEndian.getUByte(buf); return LittleEndian.getUByte(buf);
} }
public double readDouble() { @Override
public double readDouble() {
return Double.longBitsToDouble(readLong()); return Double.longBitsToDouble(readLong());
} }
public int readInt() { @Override
public int readInt() {
byte buf[] = new byte[LittleEndianConsts.INT_SIZE]; byte buf[] = new byte[LittleEndianConsts.INT_SIZE];
try { try {
checkEOF(read(buf), buf.length); checkEOF(read(buf), buf.length);
@ -82,7 +87,8 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
return retNum & 0x00FFFFFFFFL; return retNum & 0x00FFFFFFFFL;
} }
public long readLong() { @Override
public long readLong() {
byte buf[] = new byte[LittleEndianConsts.LONG_SIZE]; byte buf[] = new byte[LittleEndianConsts.LONG_SIZE];
try { try {
checkEOF(read(buf), LittleEndianConsts.LONG_SIZE); checkEOF(read(buf), LittleEndianConsts.LONG_SIZE);
@ -92,11 +98,13 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
return LittleEndian.getLong(buf); return LittleEndian.getLong(buf);
} }
public short readShort() { @Override
public short readShort() {
return (short)readUShort(); return (short)readUShort();
} }
public int readUShort() { @Override
public int readUShort() {
byte buf[] = new byte[LittleEndianConsts.SHORT_SIZE]; byte buf[] = new byte[LittleEndianConsts.SHORT_SIZE];
try { try {
checkEOF(read(buf), LittleEndianConsts.SHORT_SIZE); checkEOF(read(buf), LittleEndianConsts.SHORT_SIZE);
@ -112,15 +120,22 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
} }
} }
public void readFully(byte[] buf) { @Override
public void readFully(byte[] buf) {
readFully(buf, 0, buf.length); readFully(buf, 0, buf.length);
} }
public void readFully(byte[] buf, int off, int len) { @Override
public void readFully(byte[] buf, int off, int len) {
try { try {
checkEOF(read(buf, off, len), len); checkEOF(read(buf, off, len), len);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override
public void readPlain(byte[] buf, int off, int len) {
readFully(buf, off, len);
}
} }

View File

@ -169,7 +169,7 @@ public class HSLFSlideShowEncrypted implements Closeable {
if (cyos == null) { if (cyos == null) {
enc.setChunkSize(-1); enc.setChunkSize(-1);
cyos = enc.getDataStream(plainStream); cyos = enc.getDataStream(plainStream, 0);
} }
cyos.initCipherForBlock(persistId, false); cyos.initCipherForBlock(persistId, false);
} catch (Exception e) { } catch (Exception e) {
@ -314,7 +314,7 @@ public class HSLFSlideShowEncrypted implements Closeable {
try { try {
enc.setChunkSize(-1); enc.setChunkSize(-1);
ccos = enc.getDataStream(los); ccos = enc.getDataStream(los, 0);
int recInst = fieldRecInst.getValue(LittleEndian.getUShort(pictstream, offset)); int recInst = fieldRecInst.getValue(LittleEndian.getUShort(pictstream, offset));
int recType = LittleEndian.getUShort(pictstream, offset+2); int recType = LittleEndian.getUShort(pictstream, offset+2);
final int rlen = (int)LittleEndian.getUInt(pictstream, offset+4); final int rlen = (int)LittleEndian.getUInt(pictstream, offset+4);

View File

@ -151,11 +151,12 @@ public final class TestRecordFactoryInputStream {
/** /**
* makes sure the record stream starts with {@link BOFRecord} and then {@link WindowOneRecord} * makes sure the record stream starts with {@link BOFRecord}, {@link FilePassRecord} and then {@link WindowOneRecord}
* The second record is gets decrypted so this method also checks its content. * The third record is decrypted so this method also checks its content.
*/ */
private void confirmReadInitialRecords(RecordFactoryInputStream rfis) { private void confirmReadInitialRecords(RecordFactoryInputStream rfis) {
assertEquals(BOFRecord.class, rfis.nextRecord().getClass()); assertEquals(BOFRecord.class, rfis.nextRecord().getClass());
FilePassRecord recFP = (FilePassRecord) rfis.nextRecord();
WindowOneRecord rec1 = (WindowOneRecord) rfis.nextRecord(); WindowOneRecord rec1 = (WindowOneRecord) rfis.nextRecord();
assertArrayEquals(HexRead.readFromString(SAMPLE_WINDOW1),rec1.serialize()); assertArrayEquals(HexRead.readFromString(SAMPLE_WINDOW1),rec1.serialize());
} }

View File

@ -17,8 +17,7 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import static org.junit.Assert.assertEquals; import static org.apache.poi.POITestCase.assertContains;
import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
@ -38,25 +37,34 @@ public class TestCryptoAPI {
@Test @Test
public void bug59857() throws IOException { public void bug59857() throws IOException {
Biff8EncryptionKey.setCurrentUserPassword("abc"); // XOR-Obfuscation
HSSFWorkbook wb1 = ssTests.openSampleWorkbook("xor-encryption-abc.xls"); // TODO: XOR-Obfuscation is currently flawed - although the de-/obfuscation initially works,
String textExpected = "Sheet1\n1\n2\n3\n"; // it suddenly differs from the result of encrypted files via Office ...
String textActual = new ExcelExtractor(wb1).getText(); // and only very small files can be opened without file validation errors
assertEquals(textExpected, textActual); validateContent("xor-encryption-abc.xls", "abc", "Sheet1\n1\n2\n3\n");
wb1.close();
Biff8EncryptionKey.setCurrentUserPassword("password"); // BinaryRC4
HSSFWorkbook wb2 = ssTests.openSampleWorkbook("password.xls"); validateContent("password.xls", "password", "A ZIP bomb is a variant of mail-bombing. After most commercial mail servers began checking mail with anti-virus software and filtering certain malicious file types, trojan horse viruses tried to send themselves compressed into archives, such as ZIP, RAR or 7-Zip. Mail server software was then configured to unpack archives and check their contents as well. That gave black hats the idea to compose a \"bomb\" consisting of an enormous text file, containing, for example, only the letter z repeated millions of times. Such a file compresses into a relatively small archive, but its unpacking (especially by early versions of mail servers) would use a high amount of processing power, RAM and swap space, which could result in denial of service. Modern mail server computers usually have sufficient intelligence to recognize such attacks as well as sufficient processing power and memory space to process malicious attachments without interruption of service, though some are still susceptible to this technique if the ZIP bomb is mass-mailed.");
textExpected = "A ZIP bomb is a variant of mail-bombing. After most commercial mail servers began checking mail with anti-virus software and filtering certain malicious file types, trojan horse viruses tried to send themselves compressed into archives, such as ZIP, RAR or 7-Zip. Mail server software was then configured to unpack archives and check their contents as well. That gave black hats the idea to compose a \"bomb\" consisting of an enormous text file, containing, for example, only the letter z repeated millions of times. Such a file compresses into a relatively small archive, but its unpacking (especially by early versions of mail servers) would use a high amount of processing power, RAM and swap space, which could result in denial of service. Modern mail server computers usually have sufficient intelligence to recognize such attacks as well as sufficient processing power and memory space to process malicious attachments without interruption of service, though some are still susceptible to this technique if the ZIP bomb is mass-mailed.";
textActual = new ExcelExtractor(wb2).getText();
assertTrue(textActual.contains(textExpected));
wb2.close();
Biff8EncryptionKey.setCurrentUserPassword("freedom"); // CryptoAPI
HSSFWorkbook wb3 = ssTests.openSampleWorkbook("35897-type4.xls"); validateContent("35897-type4.xls", "freedom", "Sheet1\nhello there!\n");
textExpected = "Sheet1\nhello there!\n"; }
textActual = new ExcelExtractor(wb3).getText();
assertEquals(textExpected, textActual); private void validateContent(String wbFile, String password, String textExpected) throws IOException {
wb3.close(); Biff8EncryptionKey.setCurrentUserPassword(password);
HSSFWorkbook wb = ssTests.openSampleWorkbook(wbFile);
ExcelExtractor ee1 = new ExcelExtractor(wb);
String textActual = ee1.getText();
assertContains(textActual, textExpected);
Biff8EncryptionKey.setCurrentUserPassword("bla");
HSSFWorkbook wbBla = ssTests.writeOutAndReadBack(wb);
ExcelExtractor ee2 = new ExcelExtractor(wbBla);
textActual = ee2.getText();
assertContains(textActual, textExpected);
ee2.close();
ee1.close();
wbBla.close();
wb.close();
} }
} }