2010-05-27 13:23:27 +00: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
|
|
|
|
|
|
|
|
|
|
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.poifs.crypt;
|
|
|
|
|
|
2013-12-24 23:13:21 +00:00
|
|
|
import static org.apache.poi.poifs.crypt.EncryptionMode.agile;
|
|
|
|
|
import static org.apache.poi.poifs.crypt.EncryptionMode.standard;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.EncryptedDocumentException;
|
2011-03-25 16:24:34 +00:00
|
|
|
import org.apache.poi.poifs.filesystem.DirectoryNode;
|
2010-05-27 13:23:27 +00:00
|
|
|
import org.apache.poi.poifs.filesystem.DocumentInputStream;
|
2011-05-19 11:59:27 +00:00
|
|
|
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
|
2010-05-27 13:23:27 +00:00
|
|
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
public class EncryptionInfo {
|
|
|
|
|
private final int versionMajor;
|
|
|
|
|
private final int versionMinor;
|
|
|
|
|
private final int encryptionFlags;
|
2013-12-24 23:13:21 +00:00
|
|
|
|
2010-05-27 13:23:27 +00:00
|
|
|
private final EncryptionHeader header;
|
|
|
|
|
private final EncryptionVerifier verifier;
|
2013-12-24 23:13:21 +00:00
|
|
|
private final Decryptor decryptor;
|
|
|
|
|
private final Encryptor encryptor;
|
2010-05-27 13:23:27 +00:00
|
|
|
|
|
|
|
|
public EncryptionInfo(POIFSFileSystem fs) throws IOException {
|
2011-03-25 16:24:34 +00:00
|
|
|
this(fs.getRoot());
|
|
|
|
|
}
|
2013-12-24 23:13:21 +00:00
|
|
|
|
2011-05-19 11:59:27 +00:00
|
|
|
public EncryptionInfo(NPOIFSFileSystem fs) throws IOException {
|
|
|
|
|
this(fs.getRoot());
|
|
|
|
|
}
|
2013-12-24 23:13:21 +00:00
|
|
|
|
2011-03-25 16:24:34 +00:00
|
|
|
public EncryptionInfo(DirectoryNode dir) throws IOException {
|
|
|
|
|
DocumentInputStream dis = dir.createDocumentInputStream("EncryptionInfo");
|
2010-05-27 13:23:27 +00:00
|
|
|
versionMajor = dis.readShort();
|
|
|
|
|
versionMinor = dis.readShort();
|
2011-05-10 10:38:17 +00:00
|
|
|
encryptionFlags = dis.readInt();
|
2013-12-24 23:13:21 +00:00
|
|
|
|
|
|
|
|
EncryptionMode encryptionMode;
|
|
|
|
|
if (versionMajor == agile.versionMajor
|
|
|
|
|
&& versionMinor == agile.versionMinor
|
|
|
|
|
&& encryptionFlags == agile.encryptionFlags) {
|
|
|
|
|
encryptionMode = agile;
|
2010-05-27 13:23:27 +00:00
|
|
|
} else {
|
2013-12-24 23:13:21 +00:00
|
|
|
encryptionMode = standard;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EncryptionInfoBuilder eib;
|
|
|
|
|
try {
|
|
|
|
|
eib = getBuilder(encryptionMode);
|
2013-12-24 23:39:09 +00:00
|
|
|
} catch (Exception e) {
|
2013-12-24 23:49:10 +00:00
|
|
|
throw (IOException)new IOException().initCause(e);
|
2010-05-27 13:23:27 +00:00
|
|
|
}
|
2013-12-24 23:13:21 +00:00
|
|
|
|
|
|
|
|
eib.initialize(this, dis);
|
|
|
|
|
header = eib.getHeader();
|
|
|
|
|
verifier = eib.getVerifier();
|
|
|
|
|
decryptor = eib.getDecryptor();
|
|
|
|
|
encryptor = eib.getEncryptor();
|
2010-05-27 13:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
2013-12-24 23:13:21 +00:00
|
|
|
public EncryptionInfo(POIFSFileSystem fs, EncryptionMode encryptionMode) throws IOException {
|
|
|
|
|
this(fs.getRoot(), encryptionMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EncryptionInfo(NPOIFSFileSystem fs, EncryptionMode encryptionMode) throws IOException {
|
|
|
|
|
this(fs.getRoot(), encryptionMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EncryptionInfo(
|
|
|
|
|
DirectoryNode dir
|
|
|
|
|
, EncryptionMode encryptionMode
|
|
|
|
|
) throws EncryptedDocumentException {
|
|
|
|
|
this(dir, encryptionMode, null, null, -1, -1, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EncryptionInfo(
|
|
|
|
|
POIFSFileSystem fs
|
|
|
|
|
, EncryptionMode encryptionMode
|
|
|
|
|
, CipherAlgorithm cipherAlgorithm
|
|
|
|
|
, HashAlgorithm hashAlgorithm
|
|
|
|
|
, int keyBits
|
|
|
|
|
, int blockSize
|
|
|
|
|
, ChainingMode chainingMode
|
|
|
|
|
) throws EncryptedDocumentException {
|
|
|
|
|
this(fs.getRoot(), encryptionMode, cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EncryptionInfo(
|
|
|
|
|
NPOIFSFileSystem fs
|
|
|
|
|
, EncryptionMode encryptionMode
|
|
|
|
|
, CipherAlgorithm cipherAlgorithm
|
|
|
|
|
, HashAlgorithm hashAlgorithm
|
|
|
|
|
, int keyBits
|
|
|
|
|
, int blockSize
|
|
|
|
|
, ChainingMode chainingMode
|
|
|
|
|
) throws EncryptedDocumentException {
|
|
|
|
|
this(fs.getRoot(), encryptionMode, cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EncryptionInfo(
|
|
|
|
|
DirectoryNode dir
|
|
|
|
|
, EncryptionMode encryptionMode
|
|
|
|
|
, CipherAlgorithm cipherAlgorithm
|
|
|
|
|
, HashAlgorithm hashAlgorithm
|
|
|
|
|
, int keyBits
|
|
|
|
|
, int blockSize
|
|
|
|
|
, ChainingMode chainingMode
|
|
|
|
|
) throws EncryptedDocumentException {
|
|
|
|
|
versionMajor = encryptionMode.versionMajor;
|
|
|
|
|
versionMinor = encryptionMode.versionMinor;
|
|
|
|
|
encryptionFlags = encryptionMode.encryptionFlags;
|
|
|
|
|
|
|
|
|
|
EncryptionInfoBuilder eib;
|
|
|
|
|
try {
|
|
|
|
|
eib = getBuilder(encryptionMode);
|
2013-12-24 23:39:09 +00:00
|
|
|
} catch (Exception e) {
|
2013-12-24 23:13:21 +00:00
|
|
|
throw new EncryptedDocumentException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eib.initialize(this, cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
|
|
|
|
|
|
|
|
|
|
header = eib.getHeader();
|
|
|
|
|
verifier = eib.getVerifier();
|
|
|
|
|
decryptor = eib.getDecryptor();
|
|
|
|
|
encryptor = eib.getEncryptor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static EncryptionInfoBuilder getBuilder(EncryptionMode encryptionMode)
|
2013-12-24 23:39:09 +00:00
|
|
|
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
|
2013-12-24 23:13:21 +00:00
|
|
|
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
|
|
|
|
EncryptionInfoBuilder eib;
|
|
|
|
|
eib = (EncryptionInfoBuilder)cl.loadClass(encryptionMode.builder).newInstance();
|
|
|
|
|
return eib;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-27 13:23:27 +00:00
|
|
|
public int getVersionMajor() {
|
|
|
|
|
return versionMajor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getVersionMinor() {
|
|
|
|
|
return versionMinor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getEncryptionFlags() {
|
|
|
|
|
return encryptionFlags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EncryptionHeader getHeader() {
|
|
|
|
|
return header;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EncryptionVerifier getVerifier() {
|
|
|
|
|
return verifier;
|
|
|
|
|
}
|
2013-12-24 23:13:21 +00:00
|
|
|
|
|
|
|
|
public Decryptor getDecryptor() {
|
|
|
|
|
return decryptor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Encryptor getEncryptor() {
|
|
|
|
|
return encryptor;
|
|
|
|
|
}
|
2010-05-27 13:23:27 +00:00
|
|
|
}
|