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:41:56 UTC

svn commit: r1876846 - /pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java

Author: tilman
Date: Wed Apr 22 18:41:55 2020
New Revision: 1876846

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

Modified:
    pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java

Modified: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java?rev=1876846&r1=1876845&r2=1876846&view=diff
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java (original)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmbeddedTimeStamp.java Wed Apr 22 18:41:55 2020
@@ -25,11 +25,6 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
-import java.util.SortedMap;
-import java.util.TreeMap;
-
-import org.apache.pdfbox.cos.COSBase;
-import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.io.IOUtils;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
@@ -150,64 +145,37 @@ public class CreateEmbeddedTimeStamp
     private void processRelevantSignatures(byte[] documentBytes)
             throws IOException, CMSException, NoSuchAlgorithmException
     {
-        getRelevantSignature(document);
-        if (signature != null)
+        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
-     * @throws IOException
-     */
-    private void getRelevantSignature(PDDocument document) throws IOException
-    {
-        // 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.
-        SortedMap<Integer, PDSignature> sortedMap = new TreeMap<Integer, PDSignature>();
-        for (PDSignature sig : document.getSignatureDictionaries())
-        {
-            int sigOffset = sig.getByteRange()[1];
-            sortedMap.put(sigOffset, sig);
-        }
-        if (sortedMap.size() > 0)
-        {
-            PDSignature lastSignature = sortedMap.get(sortedMap.lastKey());
-            COSBase type = lastSignature.getCOSObject().getItem(COSName.TYPE);
-            if (type.equals(COSName.SIG))
-            {
-                signature = lastSignature;
-            }
+        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 (newEncoded.length > maxSize - 2)
+        {
+            throw new IOException(
+                    "New Signature is too big for existing Signature-Placeholder. Max Place: "
+                    + maxSize);
+        }
+        else
+        {
+            changedEncodedSignature = newEncoded;
         }
     }