From 952154615616326dbff2d1e5843bcd8309e76fbf Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Fri, 19 Aug 2016 20:23:16 +0000 Subject: [PATCH] add encryption support git-svn-id: https://svn.apache.org/repos/asf/poi/branches/hssf_cryptoapi@1756964 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/model/InternalWorkbook.java | 59 ++++++- .../poi/hssf/record/BoundSheetRecord.java | 6 +- .../poi/hssf/record/FilePassRecord.java | 5 + .../hssf/record/RecordFactoryInputStream.java | 20 +-- .../poi/hssf/record/RecordInputStream.java | 19 ++- .../record/cont/ContinuableRecordInput.java | 18 ++- .../record/crypto/Biff8DecryptingStream.java | 17 +- .../poi/hssf/usermodel/HSSFWorkbook.java | 72 ++++++++- .../poifs/crypt/ChunkedCipherInputStream.java | 29 ++-- .../crypt/ChunkedCipherOutputStream.java | 79 +++++++-- .../poi/poifs/crypt/CryptoFunctions.java | 4 +- .../org/apache/poi/poifs/crypt/Encryptor.java | 18 ++- .../crypt/binaryrc4/BinaryRC4Encryptor.java | 32 +++- .../crypt/cryptoapi/CryptoAPIEncryptor.java | 5 +- .../poi/poifs/crypt/xor/XORDecryptor.java | 150 +++++++++--------- .../poifs/crypt/xor/XOREncryptionHeader.java | 2 +- .../crypt/xor/XOREncryptionVerifier.java | 10 ++ .../poi/poifs/crypt/xor/XOREncryptor.java | 117 ++++++++++++-- .../poifs/filesystem/DocumentInputStream.java | 5 + .../LittleEndianByteArrayInputStream.java | 5 + .../apache/poi/util/LittleEndianInput.java | 15 +- .../poi/util/LittleEndianInputStream.java | 35 ++-- .../usermodel/HSLFSlideShowEncrypted.java | 4 +- .../record/TestRecordFactoryInputStream.java | 5 +- .../poi/hssf/usermodel/TestCryptoAPI.java | 48 +++--- 25 files changed, 588 insertions(+), 191 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index 84c6073c9..3a5fc1ef5 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -18,6 +18,7 @@ package org.apache.poi.hssf.model; import java.security.AccessControlException; +import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; @@ -25,6 +26,9 @@ import java.util.List; import java.util.Map; 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.EscherBoolProperty; 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.ExtendedFormatRecord; 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.FnGroupCountRecord; 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.WriteProtectRecord; 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.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.ExternalSheet; import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheetRange; @@ -1082,10 +1092,8 @@ public final class InternalWorkbook { SSTRecord sst = null; int sstPos = 0; boolean wroteBoundSheets = false; - for ( int k = 0; k < records.size(); k++ ) - { + for ( Record record : records ) { - Record record = records.get( k ); int len = 0; 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. */ public void preSerialize(){ + updateEncryptionRecord(); + // Ensure we have enough tab IDs // Can be a few short if new sheets were added 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() { int retval = 0; diff --git a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java index 5aa756d32..61b92831c 100644 --- a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java +++ b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java @@ -24,6 +24,8 @@ import java.util.List; import org.apache.poi.util.BitField; import org.apache.poi.util.BitFieldFactory; 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.StringUtil; 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 */ 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(); int field_3_sheetname_length = in.readUByte(); field_4_isMultibyteUnicode = in.readByte(); diff --git a/src/java/org/apache/poi/hssf/record/FilePassRecord.java b/src/java/org/apache/poi/hssf/record/FilePassRecord.java index 344096e59..acb5c8da4 100644 --- a/src/java/org/apache/poi/hssf/record/FilePassRecord.java +++ b/src/java/org/apache/poi/hssf/record/FilePassRecord.java @@ -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) { encryptionType = in.readUShort(); diff --git a/src/java/org/apache/poi/hssf/record/RecordFactoryInputStream.java b/src/java/org/apache/poi/hssf/record/RecordFactoryInputStream.java index 6338f9a69..2c5ba94c0 100644 --- a/src/java/org/apache/poi/hssf/record/RecordFactoryInputStream.java +++ b/src/java/org/apache/poi/hssf/record/RecordFactoryInputStream.java @@ -78,20 +78,16 @@ public final class RecordFactoryInputStream { outputRecs.add(rec); } - // If it's a FILEPASS, track it specifically but - // don't include it in the main stream + // If it's a FILEPASS, track it specifically if (rec instanceof FilePassRecord) { fpr = (FilePassRecord) rec; - outputRecs.remove(outputRecs.size()-1); - // TODO - add fpr not added to outputRecs - rec = outputRecs.get(0); - } else { - // workbook not encrypted (typical case) - if (rec instanceof EOFRecord) { - // A workbook stream is never empty, so crash instead - // of trying to keep track of nesting level - throw new IllegalStateException("Nothing between BOF and EOF"); - } + } + + // workbook not encrypted (typical case) + if (rec instanceof EOFRecord) { + // A workbook stream is never empty, so crash instead + // of trying to keep track of nesting level + throw new IllegalStateException("Nothing between BOF and EOF"); } } } else { diff --git a/src/java/org/apache/poi/hssf/record/RecordInputStream.java b/src/java/org/apache/poi/hssf/record/RecordInputStream.java index 929f0f2bf..a3d84863e 100644 --- a/src/java/org/apache/poi/hssf/record/RecordInputStream.java +++ b/src/java/org/apache/poi/hssf/record/RecordInputStream.java @@ -309,13 +309,22 @@ public final class RecordInputStream implements LittleEndianInput { } return result; } + + public void readPlain(byte[] buf, int off, int len) { + readFully(buf, 0, buf.length, true); + } + @Override 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) { + readFully(buf, off, len, false); + } + + protected void readFully(byte[] buf, int off, int len, boolean isPlain) { int origLen = len; if (buf == null) { throw new NullPointerException(); @@ -335,7 +344,11 @@ public final class RecordInputStream implements LittleEndianInput { } } checkRecordPosition(nextChunk); - _dataInput.readFully(buf, off, nextChunk); + if (isPlain) { + _dataInput.readPlain(buf, off, nextChunk); + } else { + _dataInput.readFully(buf, off, nextChunk); + } _currentDataOffset+=nextChunk; off += nextChunk; len -= nextChunk; diff --git a/src/java/org/apache/poi/hssf/record/cont/ContinuableRecordInput.java b/src/java/org/apache/poi/hssf/record/cont/ContinuableRecordInput.java index 739aeacb8..ecd8d4b59 100644 --- a/src/java/org/apache/poi/hssf/record/cont/ContinuableRecordInput.java +++ b/src/java/org/apache/poi/hssf/record/cont/ContinuableRecordInput.java @@ -54,28 +54,34 @@ public class ContinuableRecordInput implements LittleEndianInput { public ContinuableRecordInput(RecordInputStream in){ _in = in; } + @Override public int available(){ return _in.available(); } + @Override public byte readByte(){ return _in.readByte(); } + @Override public int readUByte(){ return _in.readUByte(); } + @Override public short readShort(){ return _in.readShort(); } + @Override public int readUShort(){ int ch1 = readUByte(); int ch2 = readUByte(); return (ch2 << 8) + (ch1 << 0); } + @Override public int readInt(){ int ch1 = _in.readUByte(); int ch2 = _in.readUByte(); @@ -84,6 +90,7 @@ public class ContinuableRecordInput implements LittleEndianInput { return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0); } + @Override public long readLong(){ int b0 = _in.readUByte(); int b1 = _in.readUByte(); @@ -103,14 +110,23 @@ public class ContinuableRecordInput implements LittleEndianInput { (b0 << 0)); } + @Override public double readDouble(){ return _in.readDouble(); } + + @Override public void readFully(byte[] buf){ _in.readFully(buf); } + + @Override public void readFully(byte[] buf, int off, int len){ _in.readFully(buf, off, len); } - + + @Override + public void readPlain(byte[] buf, int off, int len) { + readFully(buf, off, len); + } } diff --git a/src/java/org/apache/poi/hssf/record/crypto/Biff8DecryptingStream.java b/src/java/org/apache/poi/hssf/record/crypto/Biff8DecryptingStream.java index ef574beea..cef3102ff 100644 --- a/src/java/org/apache/poi/hssf/record/crypto/Biff8DecryptingStream.java +++ b/src/java/org/apache/poi/hssf/record/crypto/Biff8DecryptingStream.java @@ -17,7 +17,6 @@ package org.apache.poi.hssf.record.crypto; -import java.io.IOException; import java.io.InputStream; import java.io.PushbackInputStream; @@ -35,7 +34,7 @@ import org.apache.poi.util.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 ChunkedCipherInputStream ccis; @@ -180,7 +179,7 @@ public final class Biff8DecryptingStream implements BiffHeaderInput, LittleEndia * * @return true if record type specified by sid is never encrypted */ - private static boolean isNeverEncryptedRecord(int sid) { + public static boolean isNeverEncryptedRecord(int sid) { switch (sid) { case BOFRecord.sid: // sheet BOFs for sure @@ -204,15 +203,9 @@ public final class Biff8DecryptingStream implements BiffHeaderInput, LittleEndia } } - private void readPlain(byte b[], int off, int len) { - try { - int readBytes = ccis.readPlain(b, off, len); - if (readBytes < len) { - throw new RecordFormatException("buffer underrun"); - } - } catch (IOException e) { - throw new RecordFormatException(e); - } + @Override + public void readPlain(byte b[], int off, int len) { + ccis.readPlain(b, off, len); } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index ba015a9f1..2800904af 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -23,6 +23,7 @@ import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAME import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; 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.record.AbstractEscherHolderRecord; 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.ExtendedFormatRecord; +import org.apache.poi.hssf.record.FilePassRecord; import org.apache.poi.hssf.record.FontRecord; import org.apache.poi.hssf.record.LabelRecord; 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.aggregates.RecordAggregate.RecordVisitor; 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.poifs.crypt.ChunkedCipherOutputStream; 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.DirectoryNode; 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.util.Configurator; import org.apache.poi.util.HexDump; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; 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.POILogger; @@ -1443,7 +1452,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss if (log.check( POILogger.DEBUG )) { log.log(DEBUG, "HSSFWorkbook.getBytes()"); } - + HSSFSheet[] sheets = getSheets(); int nSheets = sheets.length; @@ -1485,9 +1494,70 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } pos += serializedSize; } + + encryptBytes(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() { return workbook; } diff --git a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java index 7b5632dea..e59ceb369 100644 --- a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java +++ b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java @@ -222,24 +222,33 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream { /** * Used when BIFF header fields (sid, size) are being read. The internal * {@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) { - return len; + return; } - int readBytes, total = 0; - do { - readBytes = read(b, off, len, true); - total += Math.max(0, readBytes); - } while (readBytes > -1 && total < len); - - return total; + try { + int readBytes, total = 0; + do { + readBytes = read(b, off, len, true); + total += Math.max(0, readBytes); + } while (readBytes > -1 && total < len); + + 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, - * which needs to be set before encryption + * which needs to be set before decryption * * @param recordSize the size of the next record */ diff --git a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java index 08ca4c3be..b734dc3fb 100644 --- a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java +++ b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java @@ -25,6 +25,7 @@ import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import java.security.GeneralSecurityException; +import java.util.BitSet; import javax.crypto.BadPaddingException; 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 int STREAMING = -1; - protected final int _chunkSize; - protected final int _chunkBits; + private final int _chunkSize; + private final int _chunkBits; private final byte[] _chunk; + private final BitSet _plainByteFlags; private final File _fileOut; private final DirectoryNode _dir; private long _pos = 0; + private long _totalPos = 0; + private long _written = 0; private Cipher _cipher; public ChunkedCipherOutputStream(DirectoryNode dir, int chunkSize) throws IOException, GeneralSecurityException { @@ -63,6 +67,7 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { this._chunkSize = chunkSize; int cs = chunkSize == STREAMING ? 4096 : chunkSize; _chunk = new byte[cs]; + _plainByteFlags = new BitSet(cs); _chunkBits = Integer.bitCount(cs-1); _fileOut = TempFile.createTempFile("encrypted_package", "crypt"); _fileOut.deleteOnExit(); @@ -76,6 +81,7 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { this._chunkSize = chunkSize; int cs = chunkSize == STREAMING ? 4096 : chunkSize; _chunk = new byte[cs]; + _plainByteFlags = new BitSet(cs); _chunkBits = Integer.bitCount(cs-1); _fileOut = null; _dir = null; @@ -106,8 +112,15 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { } @Override - public void write(byte[] b, int off, int len) - throws IOException { + public void write(byte[] b, int off, int len) 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) { return; } @@ -121,7 +134,11 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { int posInChunk = (int)(_pos & chunkMask); int nextLen = Math.min(_chunk.length-posInChunk, len); System.arraycopy(b, off, _chunk, posInChunk, nextLen); + if (writePlain) { + _plainByteFlags.set(posInChunk, posInChunk+nextLen); + } _pos += nextLen; + _totalPos += nextLen; off += nextLen; len -= nextLen; if ((_pos & chunkMask) == 0) { @@ -130,12 +147,12 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { } } - private int getChunkMask() { + protected int getChunkMask() { return _chunk.length-1; } protected void writeChunk(boolean continued) throws IOException { - if (_pos == 0) { + if (_pos == 0 || _totalPos == _written) { return; } @@ -157,14 +174,18 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { int ciLen; try { 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 (continued) { doFinal = false; } - // reset stream (not only) in case we were interrupted by plain stream parts - _pos = 0; } else { _cipher = initCipherForBlock(_cipher, index, lastChunk); + // restore pos - only streaming chunks will be reset + _pos = oldPos; } ciLen = invokeCipher(posInChunk, doFinal); } catch (GeneralSecurityException e) { @@ -172,6 +193,8 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { } out.write(_chunk, 0, ciLen); + _plainByteFlags.clear(); + _written += ciLen; } /** @@ -184,11 +207,17 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { * @throws ShortBufferException */ protected int invokeCipher(int posInChunk, boolean doFinal) throws GeneralSecurityException { - if (doFinal) { - return _cipher.doFinal(_chunk, 0, posInChunk, _chunk); - } else { - return _cipher.update(_chunk, 0, posInChunk, _chunk); + byte plain[] = (_plainByteFlags.isEmpty()) ? null : _chunk.clone(); + + int ciLen = (doFinal) + ? _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 @@ -208,7 +237,33 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { 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 { @Override public void processPOIFSWriterEvent(POIFSWriterEvent event) { diff --git a/src/java/org/apache/poi/poifs/crypt/CryptoFunctions.java b/src/java/org/apache/poi/poifs/crypt/CryptoFunctions.java index 69f8d6768..b35d2c59c 100644 --- a/src/java/org/apache/poi/poifs/crypt/CryptoFunctions.java +++ b/src/java/org/apache/poi/poifs/crypt/CryptoFunctions.java @@ -498,7 +498,9 @@ public class CryptoFunctions { * @return the byte array for xor obfuscation */ 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")); // this code is based on the libre office implementation. diff --git a/src/java/org/apache/poi/poifs/crypt/Encryptor.java b/src/java/org/apache/poi/poifs/crypt/Encryptor.java index d92fc3097..546c96628 100644 --- a/src/java/org/apache/poi/poifs/crypt/Encryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/Encryptor.java @@ -61,11 +61,16 @@ public abstract class Encryptor implements Cloneable { 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() { return secretKey; } - protected void setSecretKey(SecretKey secretKey) { + public void setSecretKey(SecretKey secretKey) { this.secretKey = secretKey; } @@ -77,6 +82,17 @@ public abstract class Encryptor implements Cloneable { 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 public Encryptor clone() throws CloneNotSupportedException { Encryptor other = (Encryptor)super.clone(); diff --git a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Encryptor.java b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Encryptor.java index ef49c9dc7..9545cbab0 100644 --- a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Encryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Encryptor.java @@ -41,6 +41,8 @@ import org.apache.poi.util.LittleEndianByteArrayOutputStream; public class BinaryRC4Encryptor extends Encryptor implements Cloneable { + private int _chunkSize = 512; + protected BinaryRC4Encryptor() { } @@ -84,6 +86,12 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable { return countStream; } + @Override + public BinaryRC4CipherOutputStream getDataStream(OutputStream stream, int initialOffset) + throws IOException, GeneralSecurityException { + return new BinaryRC4CipherOutputStream(stream); + } + protected int getKeySizeInBytes() { return getEncryptionInfo().getHeader().getKeySize() / 8; } @@ -105,6 +113,11 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable { DataSpaceMapUtils.createEncryptionEntry(dir, "EncryptionInfo", er); } + @Override + public void setChunkSize(int chunkSize) { + _chunkSize = chunkSize; + } + @Override public BinaryRC4Encryptor clone() throws CloneNotSupportedException { return (BinaryRC4Encryptor)super.clone(); @@ -112,12 +125,22 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable { 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 protected Cipher initCipherForBlock(Cipher cipher, int block, boolean lastChunk) throws GeneralSecurityException { return BinaryRC4Decryptor.initCipherForBlock(cipher, block, getEncryptionInfo(), getSecretKey(), Cipher.ENCRYPT_MODE); } - + @Override protected void calculateChecksum(File file, int i) { } @@ -128,9 +151,10 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable { BinaryRC4Encryptor.this.createEncryptionInfoEntry(dir); } - public BinaryRC4CipherOutputStream(DirectoryNode dir) - throws IOException, GeneralSecurityException { - super(dir, 512); + @Override + public void flush() throws IOException { + writeChunk(false); + super.flush(); } } } diff --git a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java index 02d28761a..e15558405 100644 --- a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java @@ -111,7 +111,8 @@ public class CryptoAPIEncryptor extends Encryptor implements Cloneable { throw new IOException("not supported"); } - public CryptoAPICipherOutputStream getDataStream(OutputStream stream) + @Override + public CryptoAPICipherOutputStream getDataStream(OutputStream stream, int initialOffset) throws IOException, GeneralSecurityException { return new CryptoAPICipherOutputStream(stream); } @@ -212,6 +213,7 @@ public class CryptoAPIEncryptor extends Encryptor implements Cloneable { return getEncryptionInfo().getHeader().getKeySize() / 8; } + @Override public void setChunkSize(int chunkSize) { _chunkSize = chunkSize; } @@ -268,6 +270,7 @@ public class CryptoAPIEncryptor extends Encryptor implements Cloneable { @Override public void flush() throws IOException { writeChunk(false); + super.flush(); } } diff --git a/src/java/org/apache/poi/poifs/crypt/xor/XORDecryptor.java b/src/java/org/apache/poi/poifs/crypt/xor/XORDecryptor.java index cb50b4782..01725f48b 100644 --- a/src/java/org/apache/poi/poifs/crypt/xor/XORDecryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/xor/XORDecryptor.java @@ -36,80 +36,6 @@ public class XORDecryptor extends Decryptor implements Cloneable { private long _length = -1L; 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 2.3.7.3 Binary Document XOR Data Transformation Method 1 - */ - @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() { } @@ -166,9 +92,83 @@ public class XORDecryptor extends Decryptor implements Cloneable { public void setChunkSize(int chunkSize) { _chunkSize = chunkSize; } - + @Override public XORDecryptor clone() throws CloneNotSupportedException { 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 2.3.7.3 Binary Document XOR Data Transformation Method 1 + */ + @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); + } + } } diff --git a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionHeader.java b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionHeader.java index cc5068f6b..873c1abde 100644 --- a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionHeader.java +++ b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionHeader.java @@ -27,7 +27,7 @@ public class XOREncryptionHeader extends EncryptionHeader implements EncryptionR } @Override - public void write(LittleEndianByteArrayOutputStream littleendianbytearrayoutputstream) { + public void write(LittleEndianByteArrayOutputStream leos) { } @Override diff --git a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionVerifier.java b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionVerifier.java index 1dcfb941c..388e3d872 100644 --- a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionVerifier.java +++ b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionVerifier.java @@ -58,4 +58,14 @@ public class XOREncryptionVerifier extends EncryptionVerifier implements Encrypt public XOREncryptionVerifier clone() throws CloneNotSupportedException { return (XOREncryptionVerifier)super.clone(); } + + @Override + protected void setEncryptedVerifier(byte[] encryptedVerifier) { + super.setEncryptedVerifier(encryptedVerifier); + } + + @Override + protected void setEncryptedKey(byte[] encryptedKey) { + super.setEncryptedKey(encryptedKey); + } } diff --git a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptor.java b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptor.java index 054b5e0e4..4a0b48801 100644 --- a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptor.java @@ -21,37 +21,41 @@ import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.security.GeneralSecurityException; -import java.security.MessageDigest; -import java.security.SecureRandom; -import java.util.Random; +import java.util.BitSet; 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.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.HashAlgorithm; -import org.apache.poi.poifs.crypt.standard.EncryptionRecord; 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 { - protected XOREncryptor() { } @Override 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 public void confirmPassword(String password, byte keySpec[], byte keySalt[], byte verifier[], byte verifierSalt[], byte integritySalt[]) { + confirmPassword(password); } @Override @@ -61,10 +65,21 @@ public class XOREncryptor extends Encryptor implements Cloneable { return countStream; } + @Override + public XORCipherOutputStream getDataStream(OutputStream stream, int initialOffset) + throws IOException, GeneralSecurityException { + return new XORCipherOutputStream(stream, initialOffset); + } + protected int getKeySizeInBytes() { return -1; } + @Override + public void setChunkSize(int chunkSize) { + // chunkSize is irrelevant + } + protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException { } @@ -73,7 +88,21 @@ public class XOREncryptor extends Encryptor implements Cloneable { 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 protected Cipher initCipherForBlock(Cipher cipher, int block, boolean lastChunk) @@ -91,9 +120,67 @@ public class XOREncryptor extends Encryptor implements Cloneable { XOREncryptor.this.createEncryptionInfoEntry(dir); } - public XORCipherOutputStream(DirectoryNode dir) - throws IOException, GeneralSecurityException { - super(dir, 512); + @Override + public void setNextRecordSize(int recordSize, boolean isPlain) { + 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))); + } + + } } diff --git a/src/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java b/src/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java index 672a4aa11..ffd6a1a85 100644 --- a/src/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java +++ b/src/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java @@ -189,4 +189,9 @@ public class DocumentInputStream extends InputStream implements LittleEndianInpu int i = readInt(); return i & 0xFFFFFFFFL; } + + @Override + public void readPlain(byte[] buf, int off, int len) { + readFully(buf, off, len); + } } diff --git a/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java b/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java index 4594343bf..2c5fe70b7 100644 --- a/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java +++ b/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java @@ -104,4 +104,9 @@ public final class LittleEndianByteArrayInputStream extends ByteArrayInputStream checkPosition(buffer.length); read(buffer, 0, buffer.length); } + + @Override + public void readPlain(byte[] buf, int off, int len) { + readFully(buf, off, len); + } } diff --git a/src/java/org/apache/poi/util/LittleEndianInput.java b/src/java/org/apache/poi/util/LittleEndianInput.java index d8db24797..c140c275d 100644 --- a/src/java/org/apache/poi/util/LittleEndianInput.java +++ b/src/java/org/apache/poi/util/LittleEndianInput.java @@ -16,10 +16,7 @@ ==================================================================== */ package org.apache.poi.util; -/** - * - * @author Josh Micich - */ + public interface LittleEndianInput { int available(); byte readByte(); @@ -31,4 +28,14 @@ public interface LittleEndianInput { double readDouble(); void readFully(byte[] buf); 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); } diff --git a/src/java/org/apache/poi/util/LittleEndianInputStream.java b/src/java/org/apache/poi/util/LittleEndianInputStream.java index da42caf3d..3062c50ff 100644 --- a/src/java/org/apache/poi/util/LittleEndianInputStream.java +++ b/src/java/org/apache/poi/util/LittleEndianInputStream.java @@ -34,7 +34,8 @@ public class LittleEndianInputStream extends FilterInputStream implements Little super(is); } - public int available() { + @Override + public int available() { try { return super.available(); } catch (IOException e) { @@ -42,11 +43,13 @@ public class LittleEndianInputStream extends FilterInputStream implements Little } } - public byte readByte() { + @Override + public byte readByte() { return (byte)readUByte(); } - public int readUByte() { + @Override + public int readUByte() { byte buf[] = new byte[1]; try { checkEOF(read(buf), 1); @@ -56,11 +59,13 @@ public class LittleEndianInputStream extends FilterInputStream implements Little return LittleEndian.getUByte(buf); } - public double readDouble() { + @Override + public double readDouble() { return Double.longBitsToDouble(readLong()); } - public int readInt() { + @Override + public int readInt() { byte buf[] = new byte[LittleEndianConsts.INT_SIZE]; try { checkEOF(read(buf), buf.length); @@ -82,7 +87,8 @@ public class LittleEndianInputStream extends FilterInputStream implements Little return retNum & 0x00FFFFFFFFL; } - public long readLong() { + @Override + public long readLong() { byte buf[] = new byte[LittleEndianConsts.LONG_SIZE]; try { checkEOF(read(buf), LittleEndianConsts.LONG_SIZE); @@ -92,11 +98,13 @@ public class LittleEndianInputStream extends FilterInputStream implements Little return LittleEndian.getLong(buf); } - public short readShort() { + @Override + public short readShort() { return (short)readUShort(); } - public int readUShort() { + @Override + public int readUShort() { byte buf[] = new byte[LittleEndianConsts.SHORT_SIZE]; try { 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); } - public void readFully(byte[] buf, int off, int len) { + @Override + public void readFully(byte[] buf, int off, int len) { try { checkEOF(read(buf, off, len), len); } catch (IOException e) { throw new RuntimeException(e); } } + + @Override + public void readPlain(byte[] buf, int off, int len) { + readFully(buf, off, len); + } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java index ece68eef7..c2dd42098 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java @@ -169,7 +169,7 @@ public class HSLFSlideShowEncrypted implements Closeable { if (cyos == null) { enc.setChunkSize(-1); - cyos = enc.getDataStream(plainStream); + cyos = enc.getDataStream(plainStream, 0); } cyos.initCipherForBlock(persistId, false); } catch (Exception e) { @@ -314,7 +314,7 @@ public class HSLFSlideShowEncrypted implements Closeable { try { enc.setChunkSize(-1); - ccos = enc.getDataStream(los); + ccos = enc.getDataStream(los, 0); int recInst = fieldRecInst.getValue(LittleEndian.getUShort(pictstream, offset)); int recType = LittleEndian.getUShort(pictstream, offset+2); final int rlen = (int)LittleEndian.getUInt(pictstream, offset+4); diff --git a/src/testcases/org/apache/poi/hssf/record/TestRecordFactoryInputStream.java b/src/testcases/org/apache/poi/hssf/record/TestRecordFactoryInputStream.java index 0e441e195..4cfdfcf24 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestRecordFactoryInputStream.java +++ b/src/testcases/org/apache/poi/hssf/record/TestRecordFactoryInputStream.java @@ -151,11 +151,12 @@ public final class TestRecordFactoryInputStream { /** - * makes sure the record stream starts with {@link BOFRecord} and then {@link WindowOneRecord} - * The second record is gets decrypted so this method also checks its content. + * makes sure the record stream starts with {@link BOFRecord}, {@link FilePassRecord} and then {@link WindowOneRecord} + * The third record is decrypted so this method also checks its content. */ private void confirmReadInitialRecords(RecordFactoryInputStream rfis) { assertEquals(BOFRecord.class, rfis.nextRecord().getClass()); + FilePassRecord recFP = (FilePassRecord) rfis.nextRecord(); WindowOneRecord rec1 = (WindowOneRecord) rfis.nextRecord(); assertArrayEquals(HexRead.readFromString(SAMPLE_WINDOW1),rec1.serialize()); } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestCryptoAPI.java b/src/testcases/org/apache/poi/hssf/usermodel/TestCryptoAPI.java index e7618073b..3baafa8b3 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestCryptoAPI.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestCryptoAPI.java @@ -17,8 +17,7 @@ package org.apache.poi.hssf.usermodel; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.apache.poi.POITestCase.assertContains; import java.io.IOException; @@ -38,25 +37,34 @@ public class TestCryptoAPI { @Test public void bug59857() throws IOException { - Biff8EncryptionKey.setCurrentUserPassword("abc"); - HSSFWorkbook wb1 = ssTests.openSampleWorkbook("xor-encryption-abc.xls"); - String textExpected = "Sheet1\n1\n2\n3\n"; - String textActual = new ExcelExtractor(wb1).getText(); - assertEquals(textExpected, textActual); - wb1.close(); + // XOR-Obfuscation + // TODO: XOR-Obfuscation is currently flawed - although the de-/obfuscation initially works, + // it suddenly differs from the result of encrypted files via Office ... + // and only very small files can be opened without file validation errors + validateContent("xor-encryption-abc.xls", "abc", "Sheet1\n1\n2\n3\n"); - Biff8EncryptionKey.setCurrentUserPassword("password"); - HSSFWorkbook wb2 = ssTests.openSampleWorkbook("password.xls"); - 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(); + // BinaryRC4 + 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."); - Biff8EncryptionKey.setCurrentUserPassword("freedom"); - HSSFWorkbook wb3 = ssTests.openSampleWorkbook("35897-type4.xls"); - textExpected = "Sheet1\nhello there!\n"; - textActual = new ExcelExtractor(wb3).getText(); - assertEquals(textExpected, textActual); - wb3.close(); + // CryptoAPI + validateContent("35897-type4.xls", "freedom", "Sheet1\nhello there!\n"); + } + + private void validateContent(String wbFile, String password, String textExpected) throws IOException { + 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(); } }