Search in sources :

Example 1 with StreamDescriptorEntry

use of org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor.StreamDescriptorEntry in project poi by apache.

the class CryptoAPIEncryptor method getSummaryEntries.

/**
     * Encrypt the Document-/SummaryInformation and other optionally streams.
     * Opposed to other crypto modes, cryptoapi is record based and can't be used
     * to stream-encrypt a whole file
     * 
     * @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream</a>
     */
public OutputStream getSummaryEntries(DirectoryNode dir) throws IOException, GeneralSecurityException {
    // NOSONAR
    CryptoAPIDocumentOutputStream bos = new CryptoAPIDocumentOutputStream(this);
    byte[] buf = new byte[8];
    // skip header
    bos.write(buf, 0, 8);
    String[] entryNames = { SummaryInformation.DEFAULT_STREAM_NAME, DocumentSummaryInformation.DEFAULT_STREAM_NAME };
    List<StreamDescriptorEntry> descList = new ArrayList<StreamDescriptorEntry>();
    int block = 0;
    for (String entryName : entryNames) {
        if (!dir.hasEntry(entryName)) {
            continue;
        }
        StreamDescriptorEntry descEntry = new StreamDescriptorEntry();
        descEntry.block = block;
        descEntry.streamOffset = bos.size();
        descEntry.streamName = entryName;
        descEntry.flags = StreamDescriptorEntry.flagStream.setValue(0, 1);
        descEntry.reserved2 = 0;
        bos.setBlock(block);
        DocumentInputStream dis = dir.createDocumentInputStream(entryName);
        IOUtils.copy(dis, bos);
        dis.close();
        descEntry.streamSize = bos.size() - descEntry.streamOffset;
        descList.add(descEntry);
        dir.getEntry(entryName).delete();
        block++;
    }
    int streamDescriptorArrayOffset = bos.size();
    bos.setBlock(0);
    LittleEndian.putUInt(buf, 0, descList.size());
    bos.write(buf, 0, 4);
    for (StreamDescriptorEntry sde : descList) {
        LittleEndian.putUInt(buf, 0, sde.streamOffset);
        bos.write(buf, 0, 4);
        LittleEndian.putUInt(buf, 0, sde.streamSize);
        bos.write(buf, 0, 4);
        LittleEndian.putUShort(buf, 0, sde.block);
        bos.write(buf, 0, 2);
        LittleEndian.putUByte(buf, 0, (short) sde.streamName.length());
        bos.write(buf, 0, 1);
        LittleEndian.putUByte(buf, 0, (short) sde.flags);
        bos.write(buf, 0, 1);
        LittleEndian.putUInt(buf, 0, sde.reserved2);
        bos.write(buf, 0, 4);
        byte[] nameBytes = StringUtil.getToUnicodeLE(sde.streamName);
        bos.write(nameBytes, 0, nameBytes.length);
        // null-termination
        LittleEndian.putShort(buf, 0, (short) 0);
        bos.write(buf, 0, 2);
    }
    int savedSize = bos.size();
    int streamDescriptorArraySize = savedSize - streamDescriptorArrayOffset;
    LittleEndian.putUInt(buf, 0, streamDescriptorArrayOffset);
    LittleEndian.putUInt(buf, 4, streamDescriptorArraySize);
    bos.reset();
    bos.setBlock(0);
    bos.write(buf, 0, 8);
    bos.setSize(savedSize);
    dir.createDocument("EncryptedSummary", new ByteArrayInputStream(bos.getBuf(), 0, savedSize));
    DocumentSummaryInformation dsi = PropertySetFactory.newDocumentSummaryInformation();
    try {
        dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    } catch (WritingNotSupportedException e) {
        throw new IOException(e);
    }
    return bos;
}
Also used : ArrayList(java.util.ArrayList) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) WritingNotSupportedException(org.apache.poi.hpsf.WritingNotSupportedException) IOException(java.io.IOException) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) StreamDescriptorEntry(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor.StreamDescriptorEntry) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)1 WritingNotSupportedException (org.apache.poi.hpsf.WritingNotSupportedException)1 StreamDescriptorEntry (org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor.StreamDescriptorEntry)1 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)1