You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2014/11/23 14:53:39 UTC

svn commit: r1641180 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: pdfparser/NonSequentialPDFParser.java pdmodel/PDDocument.java

Author: lehmi
Date: Sun Nov 23 13:53:38 2014
New Revision: 1641180

URL: http://svn.apache.org/r1641180
Log:
PDFBOX-2430: handle the decryption of pdfs using public key security while parsing

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/NonSequentialPDFParser.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/NonSequentialPDFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/NonSequentialPDFParser.java?rev=1641180&r1=1641179&r2=1641180&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/NonSequentialPDFParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/NonSequentialPDFParser.java Sun Nov 23 13:53:38 2014
@@ -145,8 +145,8 @@ public class NonSequentialPDFParser exte
     protected SecurityHandler securityHandler = null;
 
     private AccessPermission accessPermission;
-    private final String keyStoreFilename = null;
-    private final String alias = null;
+    private InputStream keyStoreInputStream = null;
+    private String keyAlias = null;
     private String password = "";
     private int readTrailBytes = DEFAULT_TRAIL_BYTECOUNT; // how many trailing
                                                           // bytes to read for
@@ -254,13 +254,49 @@ public class NonSequentialPDFParser exte
     public NonSequentialPDFParser(File file, String decryptionPassword, boolean useScratchFiles)
             throws IOException
     {
+        this(file, decryptionPassword, null, null, useScratchFiles);
+    }
+
+    /**
+     * Constructs parser for given file using given buffer for temporary storage.
+     * 
+     * @param file the pdf to be parsed.
+     * @param decryptionPassword password to be used for decryption.
+     * @param keyStore key store to be used for decryption when using public key security 
+     * @param alias alias to be used for decryption when using public key security
+     * 
+     * @throws IOException If something went wrong.
+     */
+    public NonSequentialPDFParser(File file, String decryptionPassword, InputStream keyStore, String alias)
+            throws IOException
+    {
+        this(file, decryptionPassword, keyStore, alias, false);
+    }
+
+    /**
+     * Constructs parser for given file using given buffer for temporary storage.
+     * 
+     * @param file the pdf to be parsed.
+     * @param decryptionPassword password to be used for decryption.
+     * @param keyStore key store to be used for decryption when using public key security 
+     * @param alias alias to be used for decryption when using public key security
+     * @param useScratchFiles use a buffer for temporary storage.
+     * 
+     * @throws IOException If something went wrong.
+     */
+    public NonSequentialPDFParser(File file, String decryptionPassword, InputStream keyStore, 
+            String alias, boolean useScratchFiles) throws IOException
+    {
         super(EMPTY_INPUT_STREAM, false);
         pdfFile = file;
         raStream = new RandomAccessBufferedFileInputStream(pdfFile);
-        init(file, decryptionPassword, useScratchFiles);
+        password = decryptionPassword;
+        keyStoreInputStream = keyStore;
+        keyAlias = alias;
+        init(file, useScratchFiles);
     }
 
-    private void init(File file, String decryptionPassword, boolean useScratchFiles) throws IOException
+    private void init(File file, boolean useScratchFiles) throws IOException
     {
         String eofLookupRangeStr = System.getProperty(SYSPROP_EOFLOOKUPRANGE);
         if (eofLookupRangeStr != null)
@@ -277,7 +313,6 @@ public class NonSequentialPDFParser exte
         }
         setDocument(new COSDocument(false, useScratchFiles));
         pdfSource = new PushBackInputStream(raStream, 4096);
-        password = decryptionPassword;
     }
 
     /**
@@ -329,10 +364,46 @@ public class NonSequentialPDFParser exte
     public NonSequentialPDFParser(InputStream input, String decryptionPassword, boolean useScratchFiles)
             throws IOException
     {
+        this(input, decryptionPassword, null, null, useScratchFiles);
+    }
+
+    /**
+     * Constructor.
+     * 
+     * @param input input stream representing the pdf.
+     * @param decryptionPassword password to be used for decryption.
+     * @param keyStore key store to be used for decryption when using public key security 
+     * @param alias alias to be used for decryption when using public key security
+     *
+     * @throws IOException If something went wrong.
+     */
+    public NonSequentialPDFParser(InputStream input, String decryptionPassword, InputStream keyStore, String alias)
+            throws IOException
+    {
+        this(input, decryptionPassword, keyStore, alias, false);
+    }
+
+    /**
+     * Constructor.
+     * 
+     * @param input input stream representing the pdf.
+     * @param decryptionPassword password to be used for decryption.
+     * @param keyStore key store to be used for decryption when using public key security 
+     * @param alias alias to be used for decryption when using public key security
+     * @param useScratchFiles use a buffer for temporary storage.
+     *
+     * @throws IOException If something went wrong.
+     */
+    public NonSequentialPDFParser(InputStream input, String decryptionPassword, InputStream keyStore,
+            String alias, boolean useScratchFiles) throws IOException
+    {
         super(EMPTY_INPUT_STREAM, false);
         pdfFile = createTmpFile(input);
         raStream = new RandomAccessBufferedFileInputStream(pdfFile);
-        init(pdfFile, decryptionPassword, useScratchFiles);
+        password = decryptionPassword;
+        keyStoreInputStream = keyStore;
+        keyAlias = alias;
+        init(pdfFile, useScratchFiles);
     }
 
     /**
@@ -526,12 +597,12 @@ public class NonSequentialPDFParser exte
                 PDEncryption encryption = new PDEncryption(document.getEncryptionDictionary());
 
                 DecryptionMaterial decryptionMaterial;
-                if (keyStoreFilename != null)
+                if (keyStoreInputStream != null)
                 {
                     KeyStore ks = KeyStore.getInstance("PKCS12");
-                    ks.load(new FileInputStream(keyStoreFilename), password.toCharArray());
+                    ks.load(keyStoreInputStream, password.toCharArray());
 
-                    decryptionMaterial = new PublicKeyDecryptionMaterial(ks, alias, password);
+                    decryptionMaterial = new PublicKeyDecryptionMaterial(ks, keyAlias, password);
                 }
                 else
                 {
@@ -964,6 +1035,10 @@ public class NonSequentialPDFParser exte
             try
             {
                 closeFileStream();
+                if (keyStoreInputStream != null)
+                {
+                    keyStoreInputStream.close();
+                }
             }
             catch (IOException ioe)
             {

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1641180&r1=1641179&r2=1641180&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Sun Nov 23 13:53:38 2014
@@ -25,7 +25,6 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.URL;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
@@ -743,8 +742,8 @@ public class PDDocument implements Close
     /**
      * This will <b>mark</b> a document to be encrypted. The actual encryption will occur when the document is saved.
      *
-     * @deprecated This method is provided for compatibility reasons only. User should use the new security layer instead and the
-     * openProtection method especially.
+     * @deprecated This method is provided for compatibility reasons only. User should use the new security layer 
+     * instead and the openProtection method especially.
      * 
      * @param ownerPassword The owner password to encrypt the document.
      * @param userPassword The user password to encrypt the document.
@@ -916,7 +915,7 @@ public class PDDocument implements Close
      */
     public static PDDocument load(File file, boolean useScratchFiles) throws IOException
     {
-        return load(file, "", useScratchFiles);
+        return load(file, "", null, null, useScratchFiles);
     }
 
     /**
@@ -931,7 +930,7 @@ public class PDDocument implements Close
      */
     public static PDDocument load(File file, String password) throws IOException
     {
-        return load(file, password, false);
+        return load(file, password, null, null, false);
     }
 
     /**
@@ -947,7 +946,44 @@ public class PDDocument implements Close
      */
     public static PDDocument load(File file, String password, boolean useScratchFiles) throws IOException
     {
-        NonSequentialPDFParser parser = new NonSequentialPDFParser(file, password, useScratchFiles);
+        return load(file, password, null, null, useScratchFiles);
+    }
+
+    /**
+     * Parses PDF with non sequential parser.
+     * 
+     * @param file file to be loaded
+     * @param password password to be used for decryption
+     * @param keyStore key store to be used for decryption when using public key security 
+     * @param alias alias to be used for decryption when using public key security
+     * 
+     * @return loaded document
+     * 
+     * @throws IOException in case of a file reading or parsing error
+     */
+    public static PDDocument load(File file, String password, InputStream keyStore, String alias)
+            throws IOException
+    {
+        return load(file, password, keyStore, alias, false);
+    }
+
+    /**
+     * Parses PDF with non sequential parser.
+     * 
+     * @param file file to be loaded
+     * @param password password to be used for decryption
+     * @param keyStore key store to be used for decryption when using public key security 
+     * @param alias alias to be used for decryption when using public key security
+     * @param useScratchFiles enables the usage of a scratch file if set to true
+     * 
+     * @return loaded document
+     * 
+     * @throws IOException in case of a file reading or parsing error
+     */
+    public static PDDocument load(File file, String password, InputStream keyStore, String alias,
+            boolean useScratchFiles) throws IOException
+    {
+        NonSequentialPDFParser parser = new NonSequentialPDFParser(file, password, keyStore, alias, useScratchFiles);
         parser.parse();
         return parser.getPDDocument();
     }
@@ -963,7 +999,7 @@ public class PDDocument implements Close
      */
     public static PDDocument load(InputStream input) throws IOException
     {
-        return load(input, "", false);
+        return load(input, "", null, null, false);
     }
 
     /**
@@ -978,7 +1014,7 @@ public class PDDocument implements Close
      */
     public static PDDocument load(InputStream input, boolean useScratchFiles) throws IOException
     {
-        return load(input, "", useScratchFiles);
+        return load(input, "", null, null, useScratchFiles);
     }
 
     /**
@@ -994,7 +1030,25 @@ public class PDDocument implements Close
     public static PDDocument load(InputStream input, String password)
             throws IOException
     {
-        return load(input, password, false);
+        return load(input, password, null, null, false);
+    }
+
+    /**
+     * Parses PDF with non sequential parser.
+     * 
+     * @param input stream that contains the document.
+     * @param password password to be used for decryption
+     * @param keyStore key store to be used for decryption when using public key security 
+     * @param alias alias to be used for decryption when using public key security
+     * 
+     * @return loaded document
+     * 
+     * @throws IOException in case of a file reading or parsing error
+     */
+    public static PDDocument load(InputStream input, String password, InputStream keyStore, String alias)
+            throws IOException
+    {
+        return load(input, password, keyStore, alias, false);
     }
 
     /**
@@ -1002,16 +1056,18 @@ public class PDDocument implements Close
      * 
      * @param input stream that contains the document.
      * @param password password to be used for decryption
+     * @param keyStore key store to be used for decryption when using public key security 
+     * @param alias alias to be used for decryption when using public key security
      * @param useScratchFiles enables the usage of a scratch file if set to true
      * 
      * @return loaded document
      * 
      * @throws IOException in case of a file reading or parsing error
      */
-    public static PDDocument load(InputStream input, String password, boolean useScratchFiles)
+    public static PDDocument load(InputStream input, String password, InputStream keyStore, String alias, boolean useScratchFiles)
             throws IOException
     {
-        NonSequentialPDFParser parser = new NonSequentialPDFParser(input, password, useScratchFiles);
+        NonSequentialPDFParser parser = new NonSequentialPDFParser(input, password, keyStore, alias, useScratchFiles);
         parser.parse();
         return parser.getPDDocument();
     }
@@ -1317,11 +1373,21 @@ public class PDDocument implements Close
         allSecurityToBeRemoved = removeAllSecurity;
     }
 
+    /**
+     * Provides the document ID.
+     *
+     * @return the dcoument ID
+     */
     public Long getDocumentId()
     {
         return documentId;
     }
 
+    /**
+     * Sets the document ID to the given value.
+     * 
+     * @param docId the new document ID
+     */
     public void setDocumentId(Long docId)
     {
         documentId = docId;