use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.
the class TestEncryptor method agileEncryption.
@Test
public void agileEncryption() throws Exception {
int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);
File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");
String pass = "pass";
NPOIFSFileSystem nfs = new NPOIFSFileSystem(file);
// Check the encryption details
EncryptionInfo infoExpected = new EncryptionInfo(nfs);
Decryptor decExpected = Decryptor.getInstance(infoExpected);
boolean passed = decExpected.verifyPassword(pass);
assertTrue("Unable to process: document is encrypted", passed);
// extract the payload
InputStream is = decExpected.getDataStream(nfs);
byte[] payloadExpected = IOUtils.toByteArray(is);
is.close();
long decPackLenExpected = decExpected.getLength();
assertEquals(decPackLenExpected, payloadExpected.length);
is = nfs.getRoot().createDocumentInputStream(Decryptor.DEFAULT_POIFS_ENTRY);
// ignore padding block
is = new BoundedInputStream(is, is.available() - 16);
byte[] encPackExpected = IOUtils.toByteArray(is);
is.close();
// listDir(nfs.getRoot(), "orig", "");
nfs.close();
// check that same verifier/salt lead to same hashes
byte[] verifierSaltExpected = infoExpected.getVerifier().getSalt();
byte[] verifierExpected = decExpected.getVerifier();
byte[] keySalt = infoExpected.getHeader().getKeySalt();
byte[] keySpec = decExpected.getSecretKey().getEncoded();
byte[] integritySalt = decExpected.getIntegrityHmacKey();
// the hmacs of the file always differ, as we use PKCS5-padding to pad the bytes
// whereas office just uses random bytes
// byte integrityHash[] = d.getIntegrityHmacValue();
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo infoActual = new EncryptionInfo(EncryptionMode.agile, infoExpected.getVerifier().getCipherAlgorithm(), infoExpected.getVerifier().getHashAlgorithm(), infoExpected.getHeader().getKeySize(), infoExpected.getHeader().getBlockSize(), infoExpected.getVerifier().getChainingMode());
Encryptor e = Encryptor.getInstance(infoActual);
e.confirmPassword(pass, keySpec, keySalt, verifierExpected, verifierSaltExpected, integritySalt);
OutputStream os = e.getDataStream(fs);
IOUtils.copy(new ByteArrayInputStream(payloadExpected), os);
os.close();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
fs.writeFilesystem(bos);
fs.close();
nfs = new NPOIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
infoActual = new EncryptionInfo(nfs.getRoot());
Decryptor decActual = Decryptor.getInstance(infoActual);
passed = decActual.verifyPassword(pass);
assertTrue("Unable to process: document is encrypted", passed);
// extract the payload
is = decActual.getDataStream(nfs);
byte[] payloadActual = IOUtils.toByteArray(is);
is.close();
long decPackLenActual = decActual.getLength();
is = nfs.getRoot().createDocumentInputStream(Decryptor.DEFAULT_POIFS_ENTRY);
// ignore padding block
is = new BoundedInputStream(is, is.available() - 16);
byte[] encPackActual = IOUtils.toByteArray(is);
is.close();
// listDir(nfs.getRoot(), "copy", "");
nfs.close();
AgileEncryptionHeader aehExpected = (AgileEncryptionHeader) infoExpected.getHeader();
AgileEncryptionHeader aehActual = (AgileEncryptionHeader) infoActual.getHeader();
assertArrayEquals(aehExpected.getEncryptedHmacKey(), aehActual.getEncryptedHmacKey());
assertEquals(decPackLenExpected, decPackLenActual);
assertArrayEquals(payloadExpected, payloadActual);
assertArrayEquals(encPackExpected, encPackActual);
}
use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.
the class TestEncryptor method encryptPackageWithoutCoreProperties.
/**
* Ensure we can encrypt a package that is missing the Core
* Properties, eg one from dodgy versions of Jasper Reports
* See https://github.com/nestoru/xlsxenc/ and
* http://stackoverflow.com/questions/28593223
*/
@Test
public void encryptPackageWithoutCoreProperties() throws Exception {
// Open our file without core properties
File inp = POIDataSamples.getOpenXML4JInstance().getFile("OPCCompliance_NoCoreProperties.xlsx");
OPCPackage pkg = OPCPackage.open(inp.getPath());
// It doesn't have any core properties yet
assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
// Encrypt it
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
NPOIFSFileSystem fs = new NPOIFSFileSystem();
Encryptor enc = info.getEncryptor();
enc.confirmPassword("password");
OutputStream os = enc.getDataStream(fs);
pkg.save(os);
os.close();
pkg.revert();
// Save the resulting OLE2 document, and re-open it
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fs.writeFilesystem(baos);
fs.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
NPOIFSFileSystem inpFS = new NPOIFSFileSystem(bais);
// Check we can decrypt it
info = new EncryptionInfo(inpFS);
Decryptor d = Decryptor.getInstance(info);
assertEquals(true, d.verifyPassword("password"));
OPCPackage inpPkg = OPCPackage.open(d.getDataStream(inpFS));
// Check it now has empty core properties
assertEquals(1, inpPkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(inpPkg.getPackageProperties());
assertNotNull(inpPkg.getPackageProperties().getLanguageProperty());
assertNull(inpPkg.getPackageProperties().getLanguageProperty().getValue());
inpPkg.close();
inpFS.close();
}
use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project tika by apache.
the class RTFObjDataParser method handleEmbeddedPOIFS.
//will throw IOException if not actually POIFS
//can return null byte[]
private byte[] handleEmbeddedPOIFS(InputStream is, Metadata metadata, AtomicInteger unknownFilenameCount) throws IOException {
byte[] ret = null;
try (NPOIFSFileSystem fs = new NPOIFSFileSystem(is)) {
DirectoryNode root = fs.getRoot();
if (root == null) {
return ret;
}
if (root.hasEntry("Package")) {
Entry ooxml = root.getEntry("Package");
TikaInputStream stream = TikaInputStream.get(new DocumentInputStream((DocumentEntry) ooxml));
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(stream, out);
ret = out.toByteArray();
} else {
//try poifs
POIFSDocumentType type = POIFSDocumentType.detectType(root);
if (type == POIFSDocumentType.OLE10_NATIVE) {
try {
// Try to un-wrap the OLE10Native record:
Ole10Native ole = Ole10Native.createFromEmbeddedOleObject(root);
ret = ole.getDataBuffer();
} catch (Ole10NativeException ex) {
// Not a valid OLE10Native record, skip it
}
} else if (type == POIFSDocumentType.COMP_OBJ) {
DocumentEntry contentsEntry;
try {
contentsEntry = (DocumentEntry) root.getEntry("CONTENTS");
} catch (FileNotFoundException ioe) {
contentsEntry = (DocumentEntry) root.getEntry("Contents");
}
try (DocumentInputStream inp = new DocumentInputStream(contentsEntry)) {
ret = new byte[contentsEntry.getSize()];
inp.readFully(ret);
}
} else {
ByteArrayOutputStream out = new ByteArrayOutputStream();
is.reset();
IOUtils.copy(is, out);
ret = out.toByteArray();
metadata.set(Metadata.RESOURCE_NAME_KEY, "file_" + unknownFilenameCount.getAndIncrement() + "." + type.getExtension());
metadata.set(Metadata.CONTENT_TYPE, type.getType().toString());
}
}
}
return ret;
}
use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project tika by apache.
the class POIFSContainerDetector method detect.
public MediaType detect(InputStream input, Metadata metadata) throws IOException {
// Check if we have access to the document
if (input == null) {
return MediaType.OCTET_STREAM;
}
// If this is a TikaInputStream wrapping an already
// parsed NPOIFileSystem/DirectoryNode, just get the
// names from the root:
TikaInputStream tis = TikaInputStream.cast(input);
Set<String> names = null;
if (tis != null) {
Object container = tis.getOpenContainer();
if (container instanceof NPOIFSFileSystem) {
names = getTopLevelNames(((NPOIFSFileSystem) container).getRoot());
} else if (container instanceof DirectoryNode) {
names = getTopLevelNames((DirectoryNode) container);
}
}
if (names == null) {
// Check if the document starts with the OLE header
input.mark(8);
try {
if (input.read() != 0xd0 || input.read() != 0xcf || input.read() != 0x11 || input.read() != 0xe0 || input.read() != 0xa1 || input.read() != 0xb1 || input.read() != 0x1a || input.read() != 0xe1) {
return MediaType.OCTET_STREAM;
}
} finally {
input.reset();
}
}
// We can only detect the exact type when given a TikaInputStream
if (names == null && tis != null) {
// Look for known top level entry names to detect the document type
names = getTopLevelNames(tis);
}
// Detect based on the names (as available)
if (tis != null && tis.getOpenContainer() != null && tis.getOpenContainer() instanceof NPOIFSFileSystem) {
return detect(names, ((NPOIFSFileSystem) tis.getOpenContainer()).getRoot());
} else {
return detect(names, null);
}
}
use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem in project poi by apache.
the class BiffViewer method main.
/**
* Method main with 1 argument just run straight biffview against given
* file<p>
*
* <b>Usage</b>:<p>
*
* BiffViewer [--biffhex] [--noint] [--noescher] [--out] <fileName><p>
* BiffViewer --rawhex [--out] <fileName>
*
* <table summary="BiffViewer options">
* <tr><td>--biffhex</td><td>show hex dump of each BIFF record</td></tr>
* <tr><td>--noint</td><td>do not output interpretation of BIFF records</td></tr>
* <tr><td>--out</td><td>send output to <fileName>.out</td></tr>
* <tr><td>--rawhex</td><td>output raw hex dump of whole workbook stream</td></tr>
* <tr><td>--escher</td><td>turn on deserialization of escher records (default is off)</td></tr>
* <tr><td>--noheader</td><td>do not print record header (default is on)</td></tr>
* </table>
*
* @param args the command line arguments
*
* @throws IOException if the file doesn't exist or contained errors
* @throws CommandParseException if the command line contained errors
*/
public static void main(String[] args) throws IOException, CommandParseException {
// args = new String[] { "--out", "", };
CommandArgs cmdArgs = CommandArgs.parse(args);
PrintWriter pw;
if (cmdArgs.shouldOutputToFile()) {
OutputStream os = new FileOutputStream(cmdArgs.getFile().getAbsolutePath() + ".out");
pw = new PrintWriter(new OutputStreamWriter(os, StringUtil.UTF8));
} else {
// Use the system default encoding when sending to System Out
pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()));
}
NPOIFSFileSystem fs = null;
InputStream is = null;
try {
fs = new NPOIFSFileSystem(cmdArgs.getFile(), true);
is = getPOIFSInputStream(fs);
if (cmdArgs.shouldOutputRawHexOnly()) {
byte[] data = IOUtils.toByteArray(is);
HexDump.dump(data, 0, System.out, 0);
} else {
boolean dumpInterpretedRecords = cmdArgs.shouldDumpRecordInterpretations();
boolean dumpHex = cmdArgs.shouldDumpBiffHex();
// TODO - fix non-zeroAlign
boolean zeroAlignHexDump = dumpInterpretedRecords;
runBiffViewer(pw, is, dumpInterpretedRecords, dumpHex, zeroAlignHexDump, cmdArgs.suppressHeader());
}
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(fs);
IOUtils.closeQuietly(pw);
}
}
Aggregations