Search in sources :

Example 1 with EncryptionDocument

use of com.microsoft.schemas.office.x2006.encryption.EncryptionDocument in project poi by apache.

the class AgileEncryptionInfoBuilder method initialize.

@Override
public void initialize(EncryptionInfo info, LittleEndianInput dis) throws IOException {
    EncryptionDocument ed = parseDescriptor((InputStream) dis);
    info.setHeader(new AgileEncryptionHeader(ed));
    info.setVerifier(new AgileEncryptionVerifier(ed));
    if (info.getVersionMajor() == EncryptionMode.agile.versionMajor && info.getVersionMinor() == EncryptionMode.agile.versionMinor) {
        AgileDecryptor dec = new AgileDecryptor();
        dec.setEncryptionInfo(info);
        info.setDecryptor(dec);
        AgileEncryptor enc = new AgileEncryptor();
        enc.setEncryptionInfo(info);
        info.setEncryptor(enc);
    }
}
Also used : EncryptionDocument(com.microsoft.schemas.office.x2006.encryption.EncryptionDocument)

Example 2 with EncryptionDocument

use of com.microsoft.schemas.office.x2006.encryption.EncryptionDocument 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 3 with EncryptionDocument

use of com.microsoft.schemas.office.x2006.encryption.EncryptionDocument in project poi by apache.

the class AgileEncryptor method createEncryptionDocument.

protected EncryptionDocument createEncryptionDocument() {
    AgileEncryptionVerifier ver = (AgileEncryptionVerifier) getEncryptionInfo().getVerifier();
    AgileEncryptionHeader header = (AgileEncryptionHeader) getEncryptionInfo().getHeader();
    EncryptionDocument ed = EncryptionDocument.Factory.newInstance();
    CTEncryption edRoot = ed.addNewEncryption();
    CTKeyData keyData = edRoot.addNewKeyData();
    CTKeyEncryptors keyEncList = edRoot.addNewKeyEncryptors();
    CTKeyEncryptor keyEnc = keyEncList.addNewKeyEncryptor();
    keyEnc.setUri(passwordUri);
    CTPasswordKeyEncryptor keyPass = keyEnc.addNewEncryptedPasswordKey();
    keyPass.setSpinCount(ver.getSpinCount());
    keyData.setSaltSize(header.getBlockSize());
    keyPass.setSaltSize(ver.getBlockSize());
    keyData.setBlockSize(header.getBlockSize());
    keyPass.setBlockSize(ver.getBlockSize());
    keyData.setKeyBits(header.getKeySize());
    keyPass.setKeyBits(ver.getKeySize());
    keyData.setHashSize(header.getHashAlgorithm().hashSize);
    keyPass.setHashSize(ver.getHashAlgorithm().hashSize);
    // header and verifier have to have the same cipher algorithm
    if (!header.getCipherAlgorithm().xmlId.equals(ver.getCipherAlgorithm().xmlId)) {
        throw new EncryptedDocumentException("Cipher algorithm of header and verifier have to match");
    }
    STCipherAlgorithm.Enum xmlCipherAlgo = STCipherAlgorithm.Enum.forString(header.getCipherAlgorithm().xmlId);
    if (xmlCipherAlgo == null) {
        throw new EncryptedDocumentException("CipherAlgorithm " + header.getCipherAlgorithm() + " not supported.");
    }
    keyData.setCipherAlgorithm(xmlCipherAlgo);
    keyPass.setCipherAlgorithm(xmlCipherAlgo);
    switch(header.getChainingMode()) {
        case cbc:
            keyData.setCipherChaining(STCipherChaining.CHAINING_MODE_CBC);
            keyPass.setCipherChaining(STCipherChaining.CHAINING_MODE_CBC);
            break;
        case cfb:
            keyData.setCipherChaining(STCipherChaining.CHAINING_MODE_CFB);
            keyPass.setCipherChaining(STCipherChaining.CHAINING_MODE_CFB);
            break;
        default:
            throw new EncryptedDocumentException("ChainingMode " + header.getChainingMode() + " not supported.");
    }
    keyData.setHashAlgorithm(mapHashAlgorithm(header.getHashAlgorithm()));
    keyPass.setHashAlgorithm(mapHashAlgorithm(ver.getHashAlgorithm()));
    keyData.setSaltValue(header.getKeySalt());
    keyPass.setSaltValue(ver.getSalt());
    keyPass.setEncryptedVerifierHashInput(ver.getEncryptedVerifier());
    keyPass.setEncryptedVerifierHashValue(ver.getEncryptedVerifierHash());
    keyPass.setEncryptedKeyValue(ver.getEncryptedKey());
    CTDataIntegrity hmacData = edRoot.addNewDataIntegrity();
    hmacData.setEncryptedHmacKey(header.getEncryptedHmacKey());
    hmacData.setEncryptedHmacValue(header.getEncryptedHmacValue());
    for (AgileCertificateEntry ace : ver.getCertificates()) {
        keyEnc = keyEncList.addNewKeyEncryptor();
        keyEnc.setUri(certificateUri);
        CTCertificateKeyEncryptor certData = keyEnc.addNewEncryptedCertificateKey();
        try {
            certData.setX509Certificate(ace.x509.getEncoded());
        } catch (CertificateEncodingException e) {
            throw new EncryptedDocumentException(e);
        }
        certData.setEncryptedKeyValue(ace.encryptedKey);
        certData.setCertVerifier(ace.certVerifier);
    }
    return ed;
}
Also used : AgileCertificateEntry(org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier.AgileCertificateEntry) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) CTEncryption(com.microsoft.schemas.office.x2006.encryption.CTEncryption) CTDataIntegrity(com.microsoft.schemas.office.x2006.encryption.CTDataIntegrity) STCipherAlgorithm(com.microsoft.schemas.office.x2006.encryption.STCipherAlgorithm) EncryptionDocument(com.microsoft.schemas.office.x2006.encryption.EncryptionDocument) CertificateEncodingException(java.security.cert.CertificateEncodingException) CTCertificateKeyEncryptor(com.microsoft.schemas.office.x2006.keyEncryptor.certificate.CTCertificateKeyEncryptor) CTKeyEncryptor(com.microsoft.schemas.office.x2006.encryption.CTKeyEncryptor) CTKeyEncryptors(com.microsoft.schemas.office.x2006.encryption.CTKeyEncryptors) CTPasswordKeyEncryptor(com.microsoft.schemas.office.x2006.keyEncryptor.password.CTPasswordKeyEncryptor) CTKeyData(com.microsoft.schemas.office.x2006.encryption.CTKeyData)

Aggregations

EncryptionDocument (com.microsoft.schemas.office.x2006.encryption.EncryptionDocument)3 CTDataIntegrity (com.microsoft.schemas.office.x2006.encryption.CTDataIntegrity)1 CTEncryption (com.microsoft.schemas.office.x2006.encryption.CTEncryption)1 CTKeyData (com.microsoft.schemas.office.x2006.encryption.CTKeyData)1 CTKeyEncryptor (com.microsoft.schemas.office.x2006.encryption.CTKeyEncryptor)1 CTKeyEncryptors (com.microsoft.schemas.office.x2006.encryption.CTKeyEncryptors)1 STCipherAlgorithm (com.microsoft.schemas.office.x2006.encryption.STCipherAlgorithm)1 CTCertificateKeyEncryptor (com.microsoft.schemas.office.x2006.keyEncryptor.certificate.CTCertificateKeyEncryptor)1 CTPasswordKeyEncryptor (com.microsoft.schemas.office.x2006.keyEncryptor.password.CTPasswordKeyEncryptor)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 EncryptionInfo (org.apache.poi.poifs.crypt.EncryptionInfo)1 AgileCertificateEntry (org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier.AgileCertificateEntry)1 EncryptionRecord (org.apache.poi.poifs.crypt.standard.EncryptionRecord)1 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)1