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 2020/04/22 18:39:39 UTC

svn commit: r1876844 - in /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature: CreateEmbeddedTimeStamp.java SigUtils.java

Author: tilman
Date: Wed Apr 22 18:39:39 2020
New Revision: 1876844

URL: http://svn.apache.org/viewvc?rev=1876844&view=rev
Log:
PDFBOX-3017: DRY refactoring

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java?rev=1876844&r1=1876843&r2=1876844&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java Wed Apr 22 18:39:39 2020
@@ -25,12 +25,8 @@ import java.io.OutputStream;
 import java.nio.file.Files;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
-import java.util.Comparator;
-import java.util.Optional;
 
 import org.apache.pdfbox.Loader;
-import org.apache.pdfbox.cos.COSBase;
-import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
 import org.apache.pdfbox.util.Hex;
@@ -142,66 +138,37 @@ public class CreateEmbeddedTimeStamp
     private void processRelevantSignatures(byte[] documentBytes)
             throws IOException, CMSException, NoSuchAlgorithmException
     {
-        getRelevantSignature(document);
-        if (signature != null)
+        signature = SigUtils.getLastRelevantSignature(document);
+        if (signature == null)
         {
-            byte[] sigBlock = signature.getContents(documentBytes);
-            CMSSignedData signedData = new CMSSignedData(sigBlock);
-
-            System.out.println("INFO: Byte Range: " + Arrays.toString(signature.getByteRange()));
+            return;
+        }
 
-            if (tsaUrl != null && tsaUrl.length() > 0)
-            {
-                ValidationTimeStamp validation = new ValidationTimeStamp(tsaUrl);
-                signedData = validation.addSignedTimeStamp(signedData);
-            }
+        byte[] sigBlock = signature.getContents(documentBytes);
+        CMSSignedData signedData = new CMSSignedData(sigBlock);
 
-            byte[] newEncoded = Hex.getBytes(signedData.getEncoded());
-            int maxSize = signature.getByteRange()[2] - signature.getByteRange()[1];
-            System.out.println(
-                    "INFO: New Signature has Size: " + newEncoded.length + " maxSize: " + maxSize);
+        System.out.println("INFO: Byte Range: " + Arrays.toString(signature.getByteRange()));
 
-            if (newEncoded.length > maxSize - 2)
-            {
-                throw new IOException(
-                        "New Signature is too big for existing Signature-Placeholder. Max Place: "
-                                + maxSize);
-            }
-            else
-            {
-                changedEncodedSignature = newEncoded;
-            }
+        if (tsaUrl != null && tsaUrl.length() > 0)
+        {
+            ValidationTimeStamp validation = new ValidationTimeStamp(tsaUrl);
+            signedData = validation.addSignedTimeStamp(signedData);
         }
-    }
-
-    /**
-     * Extracts last Document-Signature from the document. The signature will be set on the signature-field.
-     *
-     * @param document to get the Signature from
-     */
-    private void getRelevantSignature(PDDocument document)
-    {
-        Comparator<PDSignature> comparatorByOffset =
-                Comparator.comparing(sig -> sig.getByteRange()[1]);
 
-        // we can't use getLastSignatureDictionary() because this will fail (see PDFBOX-3978) 
-        // if a signature is assigned to a pre-defined empty signature field that isn't the last.
-        // we get the last in time by looking at the offset in the PDF file.
-        Optional<PDSignature> optLastSignature =
-                document.getSignatureDictionaries().stream().
-                sorted(comparatorByOffset.reversed()).
-                findFirst();
+        byte[] newEncoded = Hex.getBytes(signedData.getEncoded());
+        int maxSize = signature.getByteRange()[2] - signature.getByteRange()[1];
+        System.out.println(
+                "INFO: New Signature has Size: " + newEncoded.length + " maxSize: " + maxSize);
 
-        if (!optLastSignature.isPresent())
+        if (newEncoded.length > maxSize - 2)
         {
-            return;
+            throw new IOException(
+                    "New Signature is too big for existing Signature-Placeholder. Max Place: "
+                    + maxSize);
         }
-
-        PDSignature lastSignature = optLastSignature.get();
-        COSBase type = lastSignature.getCOSObject().getItem(COSName.TYPE);
-        if (COSName.SIG.equals(type))
+        else
         {
-            signature = lastSignature;
+            changedEncodedSignature = newEncoded;
         }
     }
 

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java?rev=1876844&r1=1876843&r2=1876844&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/SigUtils.java Wed Apr 22 18:39:39 2020
@@ -21,9 +21,9 @@ import java.security.cert.CertificateExc
 import java.security.cert.CertificateParsingException;
 import java.security.cert.X509Certificate;
 import java.util.Collection;
+import java.util.Comparator;
 import java.util.List;
-import java.util.SortedMap;
-import java.util.TreeMap;
+import java.util.Optional;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.cos.COSArray;
@@ -235,17 +235,21 @@ public class SigUtils
      */
     public static PDSignature getLastRelevantSignature(PDDocument document)
     {
-        SortedMap<Integer, PDSignature> sortedMap = new TreeMap<>();
-        for (PDSignature signature : document.getSignatureDictionaries())
-        {
-            int sigOffset = signature.getByteRange()[1];
-            sortedMap.put(sigOffset, signature);
-        }
-        if (sortedMap.size() > 0)
+        Comparator<PDSignature> comparatorByOffset =
+                Comparator.comparing(sig -> sig.getByteRange()[1]);
+
+        // we can't use getLastSignatureDictionary() because this will fail (see PDFBOX-3978) 
+        // if a signature is assigned to a pre-defined empty signature field that isn't the last.
+        // we get the last in time by looking at the offset in the PDF file.
+        Optional<PDSignature> optLastSignature =
+                document.getSignatureDictionaries().stream().
+                sorted(comparatorByOffset.reversed()).
+                findFirst();
+        if (optLastSignature.isPresent())
         {
-            PDSignature lastSignature = sortedMap.get(sortedMap.lastKey());
+            PDSignature lastSignature = optLastSignature.get();
             COSBase type = lastSignature.getCOSObject().getItem(COSName.TYPE);
-            if (type.equals(COSName.SIG) || type.equals(COSName.DOC_TIME_STAMP))
+            if (COSName.SIG.equals(type) || COSName.DOC_TIME_STAMP.equals(type))
             {
                 return lastSignature;
             }