Search in sources :

Example 1 with EncryptedPowerPointFileException

use of org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException in project poi by apache.

the class HSLFSlideShowEncrypted method encryptRecord.

protected OutputStream encryptRecord(OutputStream plainStream, int persistId, Record record) {
    boolean isPlain = (dea == null || record instanceof UserEditAtom || record instanceof PersistPtrHolder || record instanceof DocumentEncryptionAtom);
    try {
        if (isPlain) {
            if (cyos != null) {
                // write cached data to stream
                cyos.flush();
            }
            return plainStream;
        }
        encryptInit();
        if (cyos == null) {
            enc.setChunkSize(-1);
            cyos = enc.getDataStream(plainStream, 0);
        }
        cyos.initCipherForBlock(persistId, false);
    } catch (Exception e) {
        throw new EncryptedPowerPointFileException(e);
    }
    return cyos;
}
Also used : DocumentEncryptionAtom(org.apache.poi.hslf.record.DocumentEncryptionAtom) PersistPtrHolder(org.apache.poi.hslf.record.PersistPtrHolder) UserEditAtom(org.apache.poi.hslf.record.UserEditAtom) EncryptedPowerPointFileException(org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException) EncryptedPowerPointFileException(org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CorruptPowerPointFileException(org.apache.poi.hslf.exceptions.CorruptPowerPointFileException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException)

Example 2 with EncryptedPowerPointFileException

use of org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException in project poi by apache.

the class HSLFSlideShowEncrypted method encryptPicture.

protected void encryptPicture(byte[] pictstream, int offset) {
    if (dea == null) {
        return;
    }
    encryptInit();
    // NOSONAR
    LittleEndianByteArrayOutputStream los = new LittleEndianByteArrayOutputStream(pictstream, offset);
    ChunkedCipherOutputStream ccos = null;
    try {
        enc.setChunkSize(-1);
        ccos = enc.getDataStream(los, 0);
        int recInst = fieldRecInst.getValue(LittleEndian.getUShort(pictstream, offset));
        int recType = LittleEndian.getUShort(pictstream, offset + 2);
        final int rlen = (int) LittleEndian.getUInt(pictstream, offset + 4);
        ccos.write(pictstream, offset, 8);
        ccos.flush();
        offset += 8;
        int endOffset = offset + rlen;
        if (recType == 0xF007) {
            // TOOD: get a real example file ... to actual test the FBSE entry
            // not sure where the foDelay block is
            // File BLIP Store Entry (FBSE)
            int cbName = LittleEndian.getUShort(pictstream, offset + 33);
            for (int part : BLIB_STORE_ENTRY_PARTS) {
                ccos.write(pictstream, offset, part);
                ccos.flush();
                offset += part;
            }
            if (cbName > 0) {
                ccos.write(pictstream, offset, cbName);
                ccos.flush();
                offset += cbName;
            }
            if (offset == endOffset) {
                // no embedded blip
                return;
            }
            // fall through, read embedded blip now
            // update header data
            recInst = fieldRecInst.getValue(LittleEndian.getUShort(pictstream, offset));
            recType = LittleEndian.getUShort(pictstream, offset + 2);
            ccos.write(pictstream, offset, 8);
            ccos.flush();
            offset += 8;
        }
        int rgbUidCnt = (recInst == 0x217 || recInst == 0x3D5 || recInst == 0x46B || recInst == 0x543 || recInst == 0x6E1 || recInst == 0x6E3 || recInst == 0x6E5 || recInst == 0x7A9) ? 2 : 1;
        for (int i = 0; i < rgbUidCnt; i++) {
            // rgbUid 1/2
            ccos.write(pictstream, offset, 16);
            ccos.flush();
            offset += 16;
        }
        if (recType == 0xF01A || recType == 0XF01B || recType == 0XF01C) {
            // metafileHeader
            ccos.write(pictstream, offset, 34);
            offset += 34;
            ccos.flush();
        } else {
            // tag
            ccos.write(pictstream, offset, 1);
            offset += 1;
            ccos.flush();
        }
        int blipLen = endOffset - offset;
        ccos.write(pictstream, offset, blipLen);
        ccos.flush();
    } catch (Exception e) {
        throw new EncryptedPowerPointFileException(e);
    } finally {
        IOUtils.closeQuietly(ccos);
        IOUtils.closeQuietly(los);
    }
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) ChunkedCipherOutputStream(org.apache.poi.poifs.crypt.ChunkedCipherOutputStream) EncryptedPowerPointFileException(org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException) EncryptedPowerPointFileException(org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CorruptPowerPointFileException(org.apache.poi.hslf.exceptions.CorruptPowerPointFileException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException)

Example 3 with EncryptedPowerPointFileException

use of org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException in project poi by apache.

the class HSLFSlideShowEncrypted method decryptRecord.

protected void decryptRecord(byte[] docstream, int persistId, int offset) {
    if (dea == null) {
        return;
    }
    decryptInit();
    dec.setChunkSize(-1);
    // NOSONAR
    LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(docstream, offset);
    ChunkedCipherInputStream ccis = null;
    try {
        ccis = dec.getDataStream(lei, docstream.length - offset, 0);
        ccis.initCipherForBlock(persistId);
        // decrypt header and read length to be decrypted
        readFully(ccis, docstream, offset, 8);
        // decrypt the rest of the record
        int rlen = (int) LittleEndian.getUInt(docstream, offset + 4);
        readFully(ccis, docstream, offset + 8, rlen);
    } catch (Exception e) {
        throw new EncryptedPowerPointFileException(e);
    } finally {
        IOUtils.closeQuietly(ccis);
        IOUtils.closeQuietly(lei);
    }
}
Also used : LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ChunkedCipherInputStream(org.apache.poi.poifs.crypt.ChunkedCipherInputStream) EncryptedPowerPointFileException(org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException) EncryptedPowerPointFileException(org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CorruptPowerPointFileException(org.apache.poi.hslf.exceptions.CorruptPowerPointFileException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException)

Example 4 with EncryptedPowerPointFileException

use of org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException in project poi by apache.

the class TestDocumentEncryption method cryptoAPIDecryptionOther.

@Test
public void cryptoAPIDecryptionOther() throws Exception {
    Biff8EncryptionKey.setCurrentUserPassword("hello");
    String[] encPpts = { "Password_Protected-56-hello.ppt", "Password_Protected-hello.ppt", "Password_Protected-np-hello.ppt" };
    for (String pptFile : encPpts) {
        try {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true);
            HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs);
            new HSLFSlideShow(hss).close();
            fs.close();
        } catch (EncryptedPowerPointFileException e) {
            fail(pptFile + " can't be decrypted");
        }
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) EncryptedPowerPointFileException(org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) Test(org.junit.Test)

Aggregations

EncryptedPowerPointFileException (org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException)4 IOException (java.io.IOException)3 GeneralSecurityException (java.security.GeneralSecurityException)3 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)3 CorruptPowerPointFileException (org.apache.poi.hslf.exceptions.CorruptPowerPointFileException)3 DocumentEncryptionAtom (org.apache.poi.hslf.record.DocumentEncryptionAtom)1 PersistPtrHolder (org.apache.poi.hslf.record.PersistPtrHolder)1 UserEditAtom (org.apache.poi.hslf.record.UserEditAtom)1 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)1 HSLFSlideShowImpl (org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)1 ChunkedCipherInputStream (org.apache.poi.poifs.crypt.ChunkedCipherInputStream)1 ChunkedCipherOutputStream (org.apache.poi.poifs.crypt.ChunkedCipherOutputStream)1 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)1 LittleEndianByteArrayInputStream (org.apache.poi.util.LittleEndianByteArrayInputStream)1 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)1 Test (org.junit.Test)1