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;
}
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;
}
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;
}
}
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);
}
}
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);
}
}
}
Aggregations