Wrong RevisionNumber when disabling all permissions and using 128bit encryption
-------------------------------------------------------------------------------

                 Key: PDFBOX-837
                 URL: https://issues.apache.org/jira/browse/PDFBOX-837
             Project: PDFBox
          Issue Type: Bug
          Components: PDModel
    Affects Versions: 1.2.1
            Reporter: Bernd Engelhardt


When disabling all permissions and using a 128bit encryption the following 
exception is thrown when saving the PDF document:

org.apache.pdfbox.exceptions.COSVisitorException: Error: Expected length=5 
actual=16
        at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1022)
        at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:911)
        at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:892)
        at pdfbox.Main.main(Main.java:53)

This is reproducable with the following code:

public class Main {

    public static void main(String[] args) {
        try {
            AccessPermission ap = new AccessPermission();

            StandardProtectionPolicy spp = null;
            ap.setCanAssembleDocument(false);
            ap.setCanExtractContent(false);
            ap.setCanExtractForAccessibility(false);
            ap.setCanFillInForm(false);
            ap.setCanModify(false);
            ap.setCanModifyAnnotations(false);
            ap.setCanPrint(false);
            ap.setCanPrintDegraded(false);

            spp = new StandardProtectionPolicy(null, null, ap);
            spp.setEncryptionKeyLength(128);

            PDDocument document = null;
            FileInputStream sourceFile = new FileInputStream(new 
File("C:\\Web\\NetBeansProjects\\pdfBox\\test.pdf"));
            document = PDDocument.load(sourceFile);
            document.protect(spp);
            
document.save("C:\\Web\\NetBeansProjects\\pdfBox\\test_encrypted.pdf");
            document.close();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

The problem is based on "computeRevisionNumber" in 
"StandardSecurityHandler.java". If all flags are disabled, the routine returns 
a value of 2. But if the 128bit encryption is enabled, the revision should be 
3. If not, the method "computeUserPassword" will fail.

A solution would be to check the key length in "computeRevisionNumber".

   private int computeRevisionNumber()
    {
        if(version == 2
            && !policy.getPermissions().canFillInForm()
            && !policy.getPermissions().canExtractForAccessibility()
            && !policy.getPermissions().canPrintDegraded()
            && keyLength == 40 )
        {
            return 2;
        }
        return 3;
    } 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to