Search in sources :

Example 1 with FilePassRecord

use of org.apache.poi.hssf.record.FilePassRecord in project poi by apache.

the class InternalWorkbook method updateEncryptionRecord.

private void updateEncryptionRecord() {
    FilePassRecord fpr = (FilePassRecord) findFirstRecordBySid(FilePassRecord.sid);
    String password = Biff8EncryptionKey.getCurrentUserPassword();
    if (password == null) {
        if (fpr != null) {
            // need to remove password data
            records.remove(fpr);
        }
    } else {
        // create password record
        if (fpr == null) {
            fpr = new FilePassRecord(EncryptionMode.binaryRC4);
            records.add(1, fpr);
        }
        // check if the password has been changed
        EncryptionInfo ei = fpr.getEncryptionInfo();
        byte[] encVer = ei.getVerifier().getEncryptedVerifier();
        try {
            Decryptor dec = ei.getDecryptor();
            Encryptor enc = ei.getEncryptor();
            if (encVer == null || !dec.verifyPassword(password)) {
                enc.confirmPassword(password);
            } else {
                SecretKey sk = dec.getSecretKey();
                ei.getEncryptor().setSecretKey(sk);
            }
        } catch (GeneralSecurityException e) {
            throw new EncryptedDocumentException("can't validate/update encryption setting", e);
        }
    }
}
Also used : FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) SecretKey(javax.crypto.SecretKey) Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) GeneralSecurityException(java.security.GeneralSecurityException) Encryptor(org.apache.poi.poifs.crypt.Encryptor) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString)

Example 2 with FilePassRecord

use of org.apache.poi.hssf.record.FilePassRecord in project poi by apache.

the class HSSFWorkbook method encryptBytes.

@SuppressWarnings("resource")
protected void encryptBytes(byte[] buf) {
    int initialOffset = 0;
    FilePassRecord fpr = null;
    for (Record r : workbook.getRecords()) {
        initialOffset += r.getRecordSize();
        if (r instanceof FilePassRecord) {
            fpr = (FilePassRecord) r;
            break;
        }
    }
    if (fpr == null) {
        return;
    }
    // NOSONAR
    LittleEndianByteArrayInputStream plain = new LittleEndianByteArrayInputStream(buf, 0);
    // NOSONAR
    LittleEndianByteArrayOutputStream leos = new LittleEndianByteArrayOutputStream(buf, 0);
    Encryptor enc = fpr.getEncryptionInfo().getEncryptor();
    enc.setChunkSize(Biff8DecryptingStream.RC4_REKEYING_INTERVAL);
    byte[] tmp = new byte[1024];
    try {
        ChunkedCipherOutputStream os = enc.getDataStream(leos, initialOffset);
        int totalBytes = 0;
        while (totalBytes < buf.length) {
            plain.read(tmp, 0, 4);
            final int sid = LittleEndian.getUShort(tmp, 0);
            final int len = LittleEndian.getUShort(tmp, 2);
            boolean isPlain = Biff8DecryptingStream.isNeverEncryptedRecord(sid);
            os.setNextRecordSize(len, isPlain);
            os.writePlain(tmp, 0, 4);
            if (sid == BoundSheetRecord.sid) {
                // special case for the field_1_position_of_BOF (=lbPlyPos) field of
                // the BoundSheet8 record which must be unencrypted
                byte[] bsrBuf = new byte[len];
                plain.readFully(bsrBuf);
                os.writePlain(bsrBuf, 0, 4);
                os.write(bsrBuf, 4, len - 4);
            } else {
                int todo = len;
                while (todo > 0) {
                    int nextLen = Math.min(todo, tmp.length);
                    plain.readFully(tmp, 0, nextLen);
                    if (isPlain) {
                        os.writePlain(tmp, 0, nextLen);
                    } else {
                        os.write(tmp, 0, nextLen);
                    }
                    todo -= nextLen;
                }
            }
            totalBytes += 4 + len;
        }
        os.close();
    } catch (Exception e) {
        throw new EncryptedDocumentException(e);
    }
}
Also used : FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) ChunkedCipherOutputStream(org.apache.poi.poifs.crypt.ChunkedCipherOutputStream) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) RecalcIdRecord(org.apache.poi.hssf.record.RecalcIdRecord) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord) Record(org.apache.poi.hssf.record.Record) AbstractEscherHolderRecord(org.apache.poi.hssf.record.AbstractEscherHolderRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) EscherBlipRecord(org.apache.poi.ddf.EscherBlipRecord) DrawingGroupRecord(org.apache.poi.hssf.record.DrawingGroupRecord) BackupRecord(org.apache.poi.hssf.record.BackupRecord) EscherRecord(org.apache.poi.ddf.EscherRecord) NameRecord(org.apache.poi.hssf.record.NameRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) LabelRecord(org.apache.poi.hssf.record.LabelRecord) FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) FontRecord(org.apache.poi.hssf.record.FontRecord) SSTRecord(org.apache.poi.hssf.record.SSTRecord) ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) Encryptor(org.apache.poi.poifs.crypt.Encryptor) FileNotFoundException(java.io.FileNotFoundException) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException)

Aggregations

EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 FilePassRecord (org.apache.poi.hssf.record.FilePassRecord)2 Encryptor (org.apache.poi.poifs.crypt.Encryptor)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 NoSuchElementException (java.util.NoSuchElementException)1 SecretKey (javax.crypto.SecretKey)1 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)1 EscherBlipRecord (org.apache.poi.ddf.EscherBlipRecord)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1 OldExcelFormatException (org.apache.poi.hssf.OldExcelFormatException)1 AbstractEscherHolderRecord (org.apache.poi.hssf.record.AbstractEscherHolderRecord)1 BackupRecord (org.apache.poi.hssf.record.BackupRecord)1 BoundSheetRecord (org.apache.poi.hssf.record.BoundSheetRecord)1 DrawingGroupRecord (org.apache.poi.hssf.record.DrawingGroupRecord)1 ExtendedFormatRecord (org.apache.poi.hssf.record.ExtendedFormatRecord)1 FontRecord (org.apache.poi.hssf.record.FontRecord)1 LabelRecord (org.apache.poi.hssf.record.LabelRecord)1 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)1