Search in sources :

Example 56 with POIFSFileSystem

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));
}
Also used : FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AgileEncryptionVerifier(org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) AgileDecryptor(org.apache.poi.poifs.crypt.agile.AgileDecryptor) File(java.io.File) Test(org.junit.Test)

Example 57 with POIFSFileSystem

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();
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Test(org.junit.Test)

Example 58 with POIFSFileSystem

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);
}
Also used : AgileDecryptor(org.apache.poi.poifs.crypt.agile.AgileDecryptor) ByteArrayInputStream(java.io.ByteArrayInputStream) BoundedInputStream(org.apache.poi.util.BoundedInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AgileEncryptionHeader(org.apache.poi.poifs.crypt.agile.AgileEncryptionHeader) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) BoundedInputStream(org.apache.poi.util.BoundedInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Example 59 with POIFSFileSystem

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();
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 60 with POIFSFileSystem

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();
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Test(org.junit.Test)

Aggregations

POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)121 Test (org.junit.Test)58 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)38 InputStream (java.io.InputStream)36 ByteArrayInputStream (java.io.ByteArrayInputStream)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 FileInputStream (java.io.FileInputStream)31 File (java.io.File)25 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)15 FileOutputStream (java.io.FileOutputStream)14 OutputStream (java.io.OutputStream)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)13 TempFile (org.apache.poi.util.TempFile)13 IOException (java.io.IOException)12 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)7 MutableSection (org.apache.poi.hpsf.MutableSection)7 HashMap (java.util.HashMap)6 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)6 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)6