You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2019/02/02 11:40:48 UTC

svn commit: r1852790 - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java

Author: tilman
Date: Sat Feb  2 11:40:47 2019
New Revision: 1852790

URL: http://svn.apache.org/viewvc?rev=1852790&view=rev
Log:
PDFBOX-4453: fix bug that caused encrypted strings that were identical to decrypted strings to be skipped; enable test

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java
    pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java?rev=1852790&r1=1852789&r2=1852790&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java Sat Feb  2 11:40:47 2019
@@ -28,10 +28,11 @@ import java.security.InvalidKeyException
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
+import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashSet;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
@@ -83,7 +84,7 @@ public abstract class SecurityHandler
     /** indicates if the Metadata have to be decrypted of not. */
     private boolean decryptMetadata;
 
-    private final Set<COSBase> objects = new HashSet<COSBase>();
+    private final Map<COSBase, List<COSBase>> objects = new HashMap<COSBase, List<COSBase>>();
 
     private boolean useAES;
 
@@ -387,27 +388,47 @@ public abstract class SecurityHandler
      */
     public void decrypt(COSBase obj, long objNum, long genNum) throws IOException
     {
-        if (!objects.contains(obj))
+        // PDFBOX-4453: Originally this was just a Set. This failed in rare cases
+        // when a decrypted string was identical to an encrypted string.
+        // Because COSString.equals() checks the contents, decryption was then skipped.
+        // This solution keeps all different "equal" objects.
+        // Please do not replace this with a solution using System.identityHashCode(),
+        // because this is still a hash code and is NOT guaranteed to be unique.
+        List<COSBase> list = objects.get(obj);
+        if (list == null)
         {
-            objects.add(obj);
-
-            if (obj instanceof COSString)
-            {
-                decryptString((COSString) obj, objNum, genNum);
-            }
-            else if (obj instanceof COSStream)
-            {
-                decryptStream((COSStream) obj, objNum, genNum);
-            }
-            else if (obj instanceof COSDictionary)
-            {
-                decryptDictionary((COSDictionary) obj, objNum, genNum);
-            }
-            else if (obj instanceof COSArray)
+            list = new ArrayList<COSBase>();
+            objects.put(obj, list);
+        }
+        else
+        {
+            // check whether the exact object is in the list
+            for (COSBase base : list)
             {
-                decryptArray((COSArray) obj, objNum, genNum);
+                if (base == obj)
+                {
+                    return;
+                }
             }
         }
+        list.add(obj);
+
+        if (obj instanceof COSString)
+        {
+            decryptString((COSString) obj, objNum, genNum);
+        }
+        else if (obj instanceof COSStream)
+        {
+            decryptStream((COSStream) obj, objNum, genNum);
+        }
+        else if (obj instanceof COSDictionary)
+        {
+            decryptDictionary((COSDictionary) obj, objNum, genNum);
+        }
+        else if (obj instanceof COSArray)
+        {
+            decryptArray((COSArray) obj, objNum, genNum);
+        }
     }
 
     /**

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java?rev=1852790&r1=1852789&r2=1852790&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java Sat Feb  2 11:40:47 2019
@@ -268,7 +268,7 @@ public class TestSymmetricKeyEncryption
      * 
      * @throws IOException 
      */
-    public void t_estPDFBox4453() throws IOException
+    public void testPDFBox4453() throws IOException
     {
         final int TESTCOUNT = 1000;
         File file = new File(testResultsDir,"PDFBOX-4453.pdf");