use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestCertificateEncryption method testCertificateEncryption.
@Test
public void testCertificateEncryption() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes128, HashAlgorithm.sha1, -1, -1, ChainingMode.cbc);
AgileEncryptionVerifier aev = (AgileEncryptionVerifier) info.getVerifier();
CertData certData = loadKeystore();
aev.addCertificate(certData.x509);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("foobaa");
File file = POIDataSamples.getDocumentInstance().getFile("VariousPictures.docx");
InputStream fis = new FileInputStream(file);
byte[] byteExpected = IOUtils.toByteArray(fis);
fis.close();
OutputStream os = enc.getDataStream(fs);
IOUtils.copy(new ByteArrayInputStream(byteExpected), os);
os.close();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
fs.writeFilesystem(bos);
bos.close();
fs = new POIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
info = new EncryptionInfo(fs);
AgileDecryptor agDec = (AgileDecryptor) info.getDecryptor();
boolean passed = agDec.verifyPassword(certData.keypair, certData.x509);
assertTrue("certificate verification failed", passed);
fis = agDec.getDataStream(fs);
byte[] byteActual = IOUtils.toByteArray(fis);
fis.close();
assertThat(byteExpected, equalTo(byteActual));
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestDecryptor method passwordVerification.
@Test
public void passwordVerification() throws IOException, GeneralSecurityException {
POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.getInstance(info);
assertTrue(d.verifyPassword(Decryptor.DEFAULT_PASSWORD));
fs.close();
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem 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.POIFSFileSystem in project poi by apache.
the class TestDecryptor method test58616.
@Test
public void test58616() throws IOException, GeneralSecurityException {
FileInputStream fis = new FileInputStream(XSSFTestDataSamples.getSampleFile("58616.xlsx"));
POIFSFileSystem pfs = new POIFSFileSystem(fis);
EncryptionInfo info = new EncryptionInfo(pfs);
Decryptor dec = Decryptor.getInstance(info);
//dec.verifyPassword(null);
dec.getDataStream(pfs);
pfs.close();
fis.close();
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestDecryptor method decrypt.
@Test
public void decrypt() throws IOException, GeneralSecurityException {
POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.getInstance(info);
d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
zipOk(fs.getRoot(), d);
fs.close();
}
Aggregations