Search in sources :

Example 1 with DocumentEncryptionAtom

use of org.apache.poi.hslf.record.DocumentEncryptionAtom in project poi by apache.

the class HSLFSlideShowEncrypted method removeEncryptionRecord.

protected static Record[] removeEncryptionRecord(Record[] records) {
    int deaSlideId = -1;
    int deaOffset = -1;
    PersistPtrHolder ptr = null;
    UserEditAtom uea = null;
    List<Record> recordList = new ArrayList<Record>();
    for (Record r : records) {
        if (r instanceof DocumentEncryptionAtom) {
            deaOffset = ((DocumentEncryptionAtom) r).getLastOnDiskOffset();
            continue;
        } else if (r instanceof UserEditAtom) {
            uea = (UserEditAtom) r;
            deaSlideId = uea.getEncryptSessionPersistIdRef();
            uea.setEncryptSessionPersistIdRef(-1);
        } else if (r instanceof PersistPtrHolder) {
            ptr = (PersistPtrHolder) r;
        }
        recordList.add(r);
    }
    if (ptr == null || uea == null) {
        throw new EncryptedDocumentException("UserEditAtom or PersistPtrholder not found.");
    }
    if (deaSlideId == -1 && deaOffset == -1) {
        return records;
    }
    TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>(ptr.getSlideLocationsLookup());
    ptr.clear();
    int maxSlideId = -1;
    for (Map.Entry<Integer, Integer> me : tm.entrySet()) {
        if (me.getKey() == deaSlideId || me.getValue() == deaOffset) {
            continue;
        }
        ptr.addSlideLookup(me.getKey(), me.getValue());
        maxSlideId = Math.max(me.getKey(), maxSlideId);
    }
    uea.setMaxPersistWritten(maxSlideId);
    records = recordList.toArray(new Record[recordList.size()]);
    return records;
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) PersistPtrHolder(org.apache.poi.hslf.record.PersistPtrHolder) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) DocumentEncryptionAtom(org.apache.poi.hslf.record.DocumentEncryptionAtom) Record(org.apache.poi.hslf.record.Record) PositionDependentRecord(org.apache.poi.hslf.record.PositionDependentRecord) UserEditAtom(org.apache.poi.hslf.record.UserEditAtom) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap)

Example 2 with DocumentEncryptionAtom

use of org.apache.poi.hslf.record.DocumentEncryptionAtom 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 3 with DocumentEncryptionAtom

use of org.apache.poi.hslf.record.DocumentEncryptionAtom in project poi by apache.

the class HSLFSlideShowEncrypted method addEncryptionRecord.

protected static Record[] addEncryptionRecord(Record[] records, DocumentEncryptionAtom dea) {
    assert (dea != null);
    int ueaIdx = -1, ptrIdx = -1, deaIdx = -1, idx = -1;
    for (Record r : records) {
        idx++;
        if (r instanceof UserEditAtom) {
            ueaIdx = idx;
        } else if (r instanceof PersistPtrHolder) {
            ptrIdx = idx;
        } else if (r instanceof DocumentEncryptionAtom) {
            deaIdx = idx;
        }
    }
    assert (ueaIdx != -1 && ptrIdx != -1 && ptrIdx < ueaIdx);
    if (deaIdx != -1) {
        DocumentEncryptionAtom deaOld = (DocumentEncryptionAtom) records[deaIdx];
        dea.setLastOnDiskOffset(deaOld.getLastOnDiskOffset());
        records[deaIdx] = dea;
        return records;
    } else {
        PersistPtrHolder ptr = (PersistPtrHolder) records[ptrIdx];
        UserEditAtom uea = ((UserEditAtom) records[ueaIdx]);
        dea.setLastOnDiskOffset(ptr.getLastOnDiskOffset() - 1);
        int nextSlideId = uea.getMaxPersistWritten() + 1;
        ptr.addSlideLookup(nextSlideId, ptr.getLastOnDiskOffset() - 1);
        uea.setEncryptSessionPersistIdRef(nextSlideId);
        uea.setMaxPersistWritten(nextSlideId);
        Record[] newRecords = new Record[records.length + 1];
        if (ptrIdx > 0) {
            System.arraycopy(records, 0, newRecords, 0, ptrIdx);
        }
        if (ptrIdx < records.length - 1) {
            System.arraycopy(records, ptrIdx, newRecords, ptrIdx + 1, records.length - ptrIdx);
        }
        newRecords[ptrIdx] = dea;
        return newRecords;
    }
}
Also used : DocumentEncryptionAtom(org.apache.poi.hslf.record.DocumentEncryptionAtom) PersistPtrHolder(org.apache.poi.hslf.record.PersistPtrHolder) Record(org.apache.poi.hslf.record.Record) PositionDependentRecord(org.apache.poi.hslf.record.PositionDependentRecord) UserEditAtom(org.apache.poi.hslf.record.UserEditAtom)

Example 4 with DocumentEncryptionAtom

use of org.apache.poi.hslf.record.DocumentEncryptionAtom in project poi by apache.

the class HSLFSlideShowEncrypted method updateEncryptionRecord.

protected Record[] updateEncryptionRecord(Record[] records) {
    String password = Biff8EncryptionKey.getCurrentUserPassword();
    if (password == null) {
        if (dea == null) {
            // no password given, no encryption record exits -> done
            return records;
        } else {
            // need to remove password data
            dea = null;
            return removeEncryptionRecord(records);
        }
    } else {
        // create password record
        if (dea == null) {
            dea = new DocumentEncryptionAtom();
            enc = null;
        }
        encryptInit();
        EncryptionInfo ei = dea.getEncryptionInfo();
        byte[] salt = ei.getVerifier().getSalt();
        if (salt == null) {
            enc.confirmPassword(password);
        } else {
            byte[] verifier = ei.getDecryptor().getVerifier();
            enc.confirmPassword(password, null, null, verifier, salt, null);
        }
        // move EncryptionRecord to last slide position
        records = normalizeRecords(records);
        return addEncryptionRecord(records, dea);
    }
}
Also used : DocumentEncryptionAtom(org.apache.poi.hslf.record.DocumentEncryptionAtom) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo)

Example 5 with DocumentEncryptionAtom

use of org.apache.poi.hslf.record.DocumentEncryptionAtom in project poi by apache.

the class HSLFSlideShowImpl method writeProperties.

/**
     * Writes out the standard Documment Information Properties (HPSF)
     *
     * @param outFS          the POIFSFileSystem to write the properties into
     * @param writtenEntries a list of POIFS entries to add the property names too
     * @throws IOException if an error when writing to the
     *                     {@link POIFSFileSystem} occurs
     */
@Override
protected void writeProperties(NPOIFSFileSystem outFS, List<String> writtenEntries) throws IOException {
    super.writeProperties(outFS, writtenEntries);
    DocumentEncryptionAtom dea = getDocumentEncryptionAtom();
    if (dea != null) {
        CryptoAPIEncryptor enc = (CryptoAPIEncryptor) dea.getEncryptionInfo().getEncryptor();
        try {
            // ignore OutputStream
            enc.getSummaryEntries(outFS.getRoot());
        } catch (IOException e) {
            throw e;
        } catch (GeneralSecurityException e) {
            throw new IOException(e);
        }
    }
}
Also used : DocumentEncryptionAtom(org.apache.poi.hslf.record.DocumentEncryptionAtom) CryptoAPIEncryptor(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptor) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Aggregations

DocumentEncryptionAtom (org.apache.poi.hslf.record.DocumentEncryptionAtom)5 PersistPtrHolder (org.apache.poi.hslf.record.PersistPtrHolder)3 UserEditAtom (org.apache.poi.hslf.record.UserEditAtom)3 IOException (java.io.IOException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 PositionDependentRecord (org.apache.poi.hslf.record.PositionDependentRecord)2 Record (org.apache.poi.hslf.record.Record)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 TreeMap (java.util.TreeMap)1 CorruptPowerPointFileException (org.apache.poi.hslf.exceptions.CorruptPowerPointFileException)1 EncryptedPowerPointFileException (org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException)1 EncryptionInfo (org.apache.poi.poifs.crypt.EncryptionInfo)1 CryptoAPIEncryptor (org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptor)1