Search in sources :

Example 1 with CTKeyData

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

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 EncryptionDocument (com.microsoft.schemas.office.x2006.encryption.EncryptionDocument)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 AgileCertificateEntry (org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier.AgileCertificateEntry)1