2006-12-22 15:56:04 -05:00
|
|
|
/* ====================================================================
|
2006-12-22 16:00:02 -05:00
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
2006-12-22 15:56:04 -05:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
package org.apache.poi.hssf.record;
|
|
|
|
|
2016-08-07 20:10:44 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-01-10 18:14:03 -05:00
|
|
|
import org.apache.poi.EncryptedDocumentException;
|
2016-08-07 20:10:44 -04:00
|
|
|
import org.apache.poi.poifs.crypt.EncryptionInfo;
|
|
|
|
import org.apache.poi.poifs.crypt.EncryptionMode;
|
|
|
|
import org.apache.poi.poifs.crypt.binaryrc4.BinaryRC4EncryptionHeader;
|
|
|
|
import org.apache.poi.poifs.crypt.binaryrc4.BinaryRC4EncryptionVerifier;
|
|
|
|
import org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader;
|
|
|
|
import org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionVerifier;
|
|
|
|
import org.apache.poi.poifs.crypt.xor.XOREncryptionHeader;
|
|
|
|
import org.apache.poi.poifs.crypt.xor.XOREncryptionVerifier;
|
2009-08-07 02:03:31 -04:00
|
|
|
import org.apache.poi.util.HexDump;
|
2016-08-07 20:10:44 -04:00
|
|
|
import org.apache.poi.util.LittleEndianByteArrayOutputStream;
|
2008-11-18 13:49:19 -05:00
|
|
|
import org.apache.poi.util.LittleEndianOutput;
|
2006-12-22 15:56:04 -05:00
|
|
|
|
|
|
|
/**
|
2016-06-12 17:28:13 -04:00
|
|
|
* Title: File Pass Record (0x002F) <p>
|
2009-08-07 02:03:31 -04:00
|
|
|
*
|
|
|
|
* Description: Indicates that the record after this record are encrypted.
|
2006-12-22 15:56:04 -05:00
|
|
|
*/
|
2015-09-28 19:16:58 -04:00
|
|
|
public final class FilePassRecord extends StandardRecord implements Cloneable {
|
|
|
|
public static final short sid = 0x002F;
|
|
|
|
private static final int ENCRYPTION_XOR = 0;
|
|
|
|
private static final int ENCRYPTION_OTHER = 1;
|
2014-05-05 17:41:31 -04:00
|
|
|
|
2016-08-07 20:10:44 -04:00
|
|
|
private int encryptionType;
|
|
|
|
private EncryptionInfo encryptionInfo;
|
|
|
|
private int dataLength;
|
2014-05-05 17:41:31 -04:00
|
|
|
|
2015-09-28 19:16:58 -04:00
|
|
|
private FilePassRecord(FilePassRecord other) {
|
2016-08-07 20:10:44 -04:00
|
|
|
dataLength = other.dataLength;
|
|
|
|
encryptionType = other.encryptionType;
|
|
|
|
try {
|
|
|
|
encryptionInfo = other.encryptionInfo.clone();
|
|
|
|
} catch (CloneNotSupportedException e) {
|
|
|
|
throw new EncryptedDocumentException(e);
|
|
|
|
}
|
2015-09-28 19:16:58 -04:00
|
|
|
}
|
|
|
|
|
2009-08-07 02:03:31 -04:00
|
|
|
public FilePassRecord(RecordInputStream in) {
|
2016-08-07 20:10:44 -04:00
|
|
|
dataLength = in.remaining();
|
|
|
|
encryptionType = in.readUShort();
|
|
|
|
|
|
|
|
EncryptionMode preferredMode;
|
|
|
|
switch (encryptionType) {
|
|
|
|
case ENCRYPTION_XOR:
|
|
|
|
preferredMode = EncryptionMode.xor;
|
|
|
|
break;
|
|
|
|
case ENCRYPTION_OTHER:
|
|
|
|
preferredMode = EncryptionMode.cryptoAPI;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new EncryptedDocumentException("invalid encryption type");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
encryptionInfo = new EncryptionInfo(in, preferredMode);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new EncryptedDocumentException(e);
|
|
|
|
}
|
2009-08-07 02:03:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void serialize(LittleEndianOutput out) {
|
2016-08-07 20:10:44 -04:00
|
|
|
out.writeShort(encryptionType);
|
|
|
|
|
|
|
|
byte data[] = new byte[1024];
|
|
|
|
LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(data, 0);
|
|
|
|
|
|
|
|
switch (encryptionInfo.getEncryptionMode()) {
|
|
|
|
case xor:
|
|
|
|
((XOREncryptionHeader)encryptionInfo.getHeader()).write(bos);
|
|
|
|
((XOREncryptionVerifier)encryptionInfo.getVerifier()).write(bos);
|
|
|
|
break;
|
|
|
|
case binaryRC4:
|
|
|
|
out.writeShort(encryptionInfo.getVersionMajor());
|
|
|
|
out.writeShort(encryptionInfo.getVersionMinor());
|
|
|
|
((BinaryRC4EncryptionHeader)encryptionInfo.getHeader()).write(bos);
|
|
|
|
((BinaryRC4EncryptionVerifier)encryptionInfo.getVerifier()).write(bos);
|
|
|
|
break;
|
|
|
|
case cryptoAPI:
|
|
|
|
out.writeShort(encryptionInfo.getVersionMajor());
|
|
|
|
out.writeShort(encryptionInfo.getVersionMinor());
|
|
|
|
((CryptoAPIEncryptionHeader)encryptionInfo.getHeader()).write(bos);
|
|
|
|
((CryptoAPIEncryptionVerifier)encryptionInfo.getVerifier()).write(bos);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new RuntimeException("not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
out.write(data, 0, bos.getWriteIndex());
|
2009-08-07 02:03:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected int getDataSize() {
|
2016-08-07 20:10:44 -04:00
|
|
|
return dataLength;
|
2009-08-07 02:03:31 -04:00
|
|
|
}
|
|
|
|
|
2016-08-07 20:10:44 -04:00
|
|
|
public EncryptionInfo getEncryptionInfo() {
|
|
|
|
return encryptionInfo;
|
2014-05-05 17:41:31 -04:00
|
|
|
}
|
2009-08-07 02:03:31 -04:00
|
|
|
|
2016-08-07 20:10:44 -04:00
|
|
|
public short getSid() {
|
2009-08-07 02:03:31 -04:00
|
|
|
return sid;
|
|
|
|
}
|
2014-05-05 17:41:31 -04:00
|
|
|
|
2015-09-28 19:16:58 -04:00
|
|
|
@Override
|
|
|
|
public FilePassRecord clone() {
|
|
|
|
return new FilePassRecord(this);
|
2009-08-07 02:03:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
StringBuffer buffer = new StringBuffer();
|
2006-12-22 15:56:04 -05:00
|
|
|
|
2009-08-07 02:03:31 -04:00
|
|
|
buffer.append("[FILEPASS]\n");
|
2016-08-07 20:10:44 -04:00
|
|
|
buffer.append(" .type = ").append(HexDump.shortToHex(encryptionType)).append("\n");
|
|
|
|
String prefix = " ."+encryptionInfo.getEncryptionMode();
|
|
|
|
buffer.append(prefix+".info = ").append(HexDump.shortToHex(encryptionInfo.getVersionMajor())).append("\n");
|
|
|
|
buffer.append(prefix+".ver = ").append(HexDump.shortToHex(encryptionInfo.getVersionMinor())).append("\n");
|
|
|
|
buffer.append(prefix+".salt = ").append(HexDump.toHex(encryptionInfo.getVerifier().getSalt())).append("\n");
|
|
|
|
buffer.append(prefix+".verifier = ").append(HexDump.toHex(encryptionInfo.getVerifier().getEncryptedVerifier())).append("\n");
|
|
|
|
buffer.append(prefix+".verifierHash = ").append(HexDump.toHex(encryptionInfo.getVerifier().getEncryptedVerifierHash())).append("\n");
|
2009-08-07 02:03:31 -04:00
|
|
|
buffer.append("[/FILEPASS]\n");
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
2006-12-22 15:56:04 -05:00
|
|
|
}
|