Search in sources :

Example 16 with LittleEndianByteArrayOutputStream

use of org.apache.poi.util.LittleEndianByteArrayOutputStream in project poi by apache.

the class AgileEncryptor method createEncryptionInfoEntry.

protected void createEncryptionInfoEntry(DirectoryNode dir, File tmpFile) throws IOException, GeneralSecurityException {
    DataSpaceMapUtils.addDefaultDataSpace(dir);
    final EncryptionInfo info = getEncryptionInfo();
    EncryptionRecord er = new EncryptionRecord() {

        @Override
        public void write(LittleEndianByteArrayOutputStream bos) {
            // EncryptionVersionInfo (4 bytes): A Version structure (section 2.1.4), where 
            // Version.vMajor MUST be 0x0004 and Version.vMinor MUST be 0x0004
            bos.writeShort(info.getVersionMajor());
            bos.writeShort(info.getVersionMinor());
            // Reserved (4 bytes): A value that MUST be 0x00000040
            bos.writeInt(info.getEncryptionFlags());
            EncryptionDocument ed = createEncryptionDocument();
            marshallEncryptionDocument(ed, bos);
        }
    };
    createEncryptionEntry(dir, "EncryptionInfo", er);
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) EncryptionDocument(com.microsoft.schemas.office.x2006.encryption.EncryptionDocument) EncryptionRecord(org.apache.poi.poifs.crypt.standard.EncryptionRecord)

Example 17 with LittleEndianByteArrayOutputStream

use of org.apache.poi.util.LittleEndianByteArrayOutputStream in project poi by apache.

the class AgileEncryptor method marshallEncryptionDocument.

protected void marshallEncryptionDocument(EncryptionDocument ed, LittleEndianByteArrayOutputStream os) {
    XmlOptions xo = new XmlOptions();
    xo.setCharacterEncoding("UTF-8");
    Map<String, String> nsMap = new HashMap<String, String>();
    nsMap.put(passwordUri.toString(), "p");
    nsMap.put(certificateUri.toString(), "c");
    xo.setUseDefaultNamespace();
    xo.setSaveSuggestedPrefixes(nsMap);
    xo.setSaveNamespacesFirst();
    xo.setSaveAggressiveNamespaces();
    // setting standalone doesn't work with xmlbeans-2.3 & 2.6
    // ed.documentProperties().setStandalone(true);
    xo.setSaveNoXmlDecl();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        bos.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n".getBytes("UTF-8"));
        ed.save(bos, xo);
        bos.writeTo(os);
    } catch (IOException e) {
        throw new EncryptedDocumentException("error marshalling encryption info document", e);
    }
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) HashMap(java.util.HashMap) XmlOptions(org.apache.xmlbeans.XmlOptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) IOException(java.io.IOException)

Example 18 with LittleEndianByteArrayOutputStream

use of org.apache.poi.util.LittleEndianByteArrayOutputStream 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)

Example 19 with LittleEndianByteArrayOutputStream

use of org.apache.poi.util.LittleEndianByteArrayOutputStream in project poi by apache.

the class TestConstantValueParser method testEncode.

@Test
public void testEncode() {
    int size = ConstantValueParser.getEncodedSize(SAMPLE_VALUES);
    byte[] data = new byte[size];
    ConstantValueParser.encode(new LittleEndianByteArrayOutputStream(data, 0), SAMPLE_VALUES);
    if (!Arrays.equals(data, SAMPLE_ENCODING)) {
        fail("Encoding differs");
    }
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) Test(org.junit.Test)

Example 20 with LittleEndianByteArrayOutputStream

use of org.apache.poi.util.LittleEndianByteArrayOutputStream in project poi by apache.

the class BinaryRC4Encryptor method createEncryptionInfoEntry.

protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException {
    DataSpaceMapUtils.addDefaultDataSpace(dir);
    final EncryptionInfo info = getEncryptionInfo();
    final BinaryRC4EncryptionHeader header = (BinaryRC4EncryptionHeader) info.getHeader();
    final BinaryRC4EncryptionVerifier verifier = (BinaryRC4EncryptionVerifier) info.getVerifier();
    EncryptionRecord er = new EncryptionRecord() {

        @Override
        public void write(LittleEndianByteArrayOutputStream bos) {
            bos.writeShort(info.getVersionMajor());
            bos.writeShort(info.getVersionMinor());
            header.write(bos);
            verifier.write(bos);
        }
    };
    DataSpaceMapUtils.createEncryptionEntry(dir, "EncryptionInfo", er);
}
Also used : LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) EncryptionRecord(org.apache.poi.poifs.crypt.standard.EncryptionRecord)

Aggregations

LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)20 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)5 IOException (java.io.IOException)4 EncryptionInfo (org.apache.poi.poifs.crypt.EncryptionInfo)4 EncryptionRecord (org.apache.poi.poifs.crypt.standard.EncryptionRecord)3 Test (org.junit.Test)3 ChunkedCipherOutputStream (org.apache.poi.poifs.crypt.ChunkedCipherOutputStream)2 CryptoAPIEncryptionHeader (org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader)2 CryptoAPIEncryptionVerifier (org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionVerifier)2 LittleEndianByteArrayInputStream (org.apache.poi.util.LittleEndianByteArrayInputStream)2 EncryptionDocument (com.microsoft.schemas.office.x2006.encryption.EncryptionDocument)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 AssertionFailedError (junit.framework.AssertionFailedError)1 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)1 EscherBlipRecord (org.apache.poi.ddf.EscherBlipRecord)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1