Search in sources :

Example 1 with PDEncryption

use of com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption in project PdfBox-Android by TomRoush.

the class TestSymmetricKeyEncryption method encrypt.

// encrypt with keylength and permission, save, check sizes before and after encryption
// reopen, decrypt and return document
private PDDocument encrypt(int keyLength, boolean preferAES, int sizePriorToEncr, PDDocument doc, String prefix, AccessPermission permission, String userpassword, String ownerpassword) throws IOException {
    StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerpassword, userpassword, permission);
    spp.setEncryptionKeyLength(keyLength);
    spp.setPreferAES(preferAES);
    // This must have no effect and should only log a warning.
    doc.setAllSecurityToBeRemoved(true);
    doc.protect(spp);
    File pdfFile = new File(testResultsDir, prefix + keyLength + "-bit-" + (preferAES ? "AES" : "RC4") + "-encrypted.pdf");
    doc.save(pdfFile);
    doc.close();
    long sizeEncrypted = pdfFile.length();
    Assert.assertTrue(keyLength + "-bit " + (preferAES ? "AES" : "RC4") + " encrypted pdf should not have same size as plain one", sizeEncrypted != sizePriorToEncr);
    // test with owner password => full permissions
    PDDocument encryptedDoc = PDDocument.load(pdfFile, ownerpassword);
    Assert.assertTrue(encryptedDoc.isEncrypted());
    Assert.assertTrue(encryptedDoc.getCurrentAccessPermission().isOwnerPermission());
    // Older encryption allows to get the user password when the owner password is known
    PDEncryption encryption = encryptedDoc.getEncryption();
    int revision = encryption.getRevision();
    if (revision < 5) {
        StandardSecurityHandler standardSecurityHandler = new StandardSecurityHandler();
        int keyLengthInBytes = encryption.getVersion() == 1 ? 5 : encryption.getLength() / 8;
        byte[] computedUserPassword = standardSecurityHandler.getUserPassword(ownerpassword.getBytes(Charsets.ISO_8859_1), encryption.getOwnerKey(), revision, keyLengthInBytes);
        assertEquals(userpassword.substring(0, 32), new String(computedUserPassword, Charsets.ISO_8859_1));
    }
    encryptedDoc.close();
    // test with user password => restricted permissions
    encryptedDoc = PDDocument.load(pdfFile, userpassword);
    Assert.assertTrue(encryptedDoc.isEncrypted());
    Assert.assertFalse(encryptedDoc.getCurrentAccessPermission().isOwnerPermission());
    assertEquals(permission.getPermissionBytes(), encryptedDoc.getCurrentAccessPermission().getPermissionBytes());
    return encryptedDoc;
}
Also used : StandardSecurityHandler(com.tom_roush.pdfbox.pdmodel.encryption.StandardSecurityHandler) StandardProtectionPolicy(com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDEncryption(com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) File(java.io.File)

Example 2 with PDEncryption

use of com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption in project PdfBox-Android by TomRoush.

the class PDDocument method protect.

/**
 * Protects the document with a protection policy. The document content will be really
 * encrypted when it will be saved. This method only marks the document for encryption. It also
 * calls {@link #setAllSecurityToBeRemoved(boolean)} with a false argument if it was set to true
 * previously and logs a warning.
 *
 * @see com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy
 * @see com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy
 *
 * @param policy The protection policy.
 * @throws IOException if there isn't any suitable security handler.
 */
public void protect(ProtectionPolicy policy) throws IOException {
    if (isAllSecurityToBeRemoved()) {
        Log.w("PdfBox-Android", "do not call setAllSecurityToBeRemoved(true) before calling protect(), " + "as protect() implies setAllSecurityToBeRemoved(false)");
        setAllSecurityToBeRemoved(false);
    }
    if (!isEncrypted()) {
        encryption = new PDEncryption();
    }
    SecurityHandler securityHandler = SecurityHandlerFactory.INSTANCE.newSecurityHandlerForPolicy(policy);
    if (securityHandler == null) {
        throw new IOException("No security handler for policy " + policy);
    }
    getEncryption().setSecurityHandler(securityHandler);
}
Also used : SecurityHandler(com.tom_roush.pdfbox.pdmodel.encryption.SecurityHandler) PDEncryption(com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption) IOException(java.io.IOException)

Example 3 with PDEncryption

use of com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption in project PdfBox-Android by TomRoush.

the class COSParser method prepareDecryption.

/**
 * Prepare for decryption.
 *
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException if something went wrong
 */
private void prepareDecryption() throws IOException {
    if (encryption != null) {
        return;
    }
    COSBase trailerEncryptItem = document.getTrailer().getItem(COSName.ENCRYPT);
    if (trailerEncryptItem == null || trailerEncryptItem instanceof COSNull) {
        return;
    }
    if (trailerEncryptItem instanceof COSObject) {
        COSObject trailerEncryptObj = (COSObject) trailerEncryptItem;
        parseDictionaryRecursive(trailerEncryptObj);
    }
    try {
        encryption = new PDEncryption(document.getEncryptionDictionary());
        DecryptionMaterial decryptionMaterial;
        if (keyStoreInputStream != null) {
            KeyStore ks = KeyStore.getInstance("PKCS12");
            ks.load(keyStoreInputStream, password.toCharArray());
            decryptionMaterial = new PublicKeyDecryptionMaterial(ks, keyAlias, password);
        } else {
            decryptionMaterial = new StandardDecryptionMaterial(password);
        }
        securityHandler = encryption.getSecurityHandler();
        securityHandler.prepareForDecryption(encryption, document.getDocumentID(), decryptionMaterial);
        accessPermission = securityHandler.getCurrentAccessPermission();
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("Error (" + e.getClass().getSimpleName() + ") while creating security handler for decryption", e);
    } finally {
        if (keyStoreInputStream != null) {
            IOUtils.closeQuietly(keyStoreInputStream);
        }
    }
}
Also used : DecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.DecryptionMaterial) PublicKeyDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial) StandardDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSNull(com.tom_roush.pdfbox.cos.COSNull) PublicKeyDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial) StandardDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) PDEncryption(com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption) IOException(java.io.IOException) KeyStore(java.security.KeyStore) InvalidPasswordException(com.tom_roush.pdfbox.pdmodel.encryption.InvalidPasswordException) IOException(java.io.IOException)

Aggregations

PDEncryption (com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption)3 IOException (java.io.IOException)2 COSBase (com.tom_roush.pdfbox.cos.COSBase)1 COSNull (com.tom_roush.pdfbox.cos.COSNull)1 COSObject (com.tom_roush.pdfbox.cos.COSObject)1 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)1 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)1 DecryptionMaterial (com.tom_roush.pdfbox.pdmodel.encryption.DecryptionMaterial)1 InvalidPasswordException (com.tom_roush.pdfbox.pdmodel.encryption.InvalidPasswordException)1 PublicKeyDecryptionMaterial (com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial)1 SecurityHandler (com.tom_roush.pdfbox.pdmodel.encryption.SecurityHandler)1 StandardDecryptionMaterial (com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial)1 StandardProtectionPolicy (com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy)1 StandardSecurityHandler (com.tom_roush.pdfbox.pdmodel.encryption.StandardSecurityHandler)1 File (java.io.File)1 KeyStore (java.security.KeyStore)1