pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1813188 - in /pdfbox/trunk/examples/src: main/java/org/apache/pdfbox/examples/signature/CreateEmptySignatureForm.java test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java
Date Tue, 24 Oct 2017 15:54:43 GMT
Author: tilman
Date: Tue Oct 24 15:54:42 2017
New Revision: 1813188

URL: http://svn.apache.org/viewvc?rev=1813188&view=rev
Log:
PDFBOX-3978: add test with empty signature before invisible signature; allow argument for
empty signature example; don't use confusing method

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmptySignatureForm.java
    pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmptySignatureForm.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmptySignatureForm.java?rev=1813188&r1=1813187&r2=1813188&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmptySignatureForm.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateEmptySignatureForm.java
Tue Oct 24 15:54:42 2017
@@ -78,7 +78,7 @@ public final class CreateEmptySignatureF
 
             acroForm.getFields().add(signatureField);
 
-            document.save("target/EmptySignatureForm.pdf");
+            document.save(args[0]);
         }
     }
 }

Modified: pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java?rev=1813188&r1=1813187&r2=1813188&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java
(original)
+++ pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java
Tue Oct 24 15:54:42 2017
@@ -24,9 +24,12 @@ import java.io.InputStream;
 import java.net.URL;
 import java.security.GeneralSecurityException;
 import java.security.KeyStore;
+import java.security.KeyStoreException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
 import java.security.cert.Certificate;
+import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
 import java.text.MessageFormat;
 import java.util.Arrays;
@@ -36,6 +39,7 @@ import javax.xml.bind.DatatypeConverter;
 
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSString;
+import org.apache.pdfbox.examples.signature.CreateEmptySignatureForm;
 import org.apache.pdfbox.examples.signature.CreateSignature;
 import org.apache.pdfbox.examples.signature.CreateVisibleSignature;
 import org.apache.pdfbox.examples.signature.TSAClient;
@@ -217,6 +221,72 @@ public class TestCreateSignature
         checkSignature(destFile);
     }
 
+    /**
+     * Test when visually signing externally on an existing signature field on a file which
has
+     * been signed before.
+     * 
+     * @throws IOException
+     * @throws NoSuchAlgorithmException
+     * @throws KeyStoreException
+     * @throws CertificateException
+     * @throws UnrecoverableKeyException
+     * @throws CMSException
+     * @throws OperatorCreationException
+     * @throws GeneralSecurityException 
+     */
+    @Test
+    public void testPDFBox3978() throws IOException, NoSuchAlgorithmException, KeyStoreException,

+                                        CertificateException, UnrecoverableKeyException,

+                                        CMSException, OperatorCreationException, GeneralSecurityException
+    {
+        String filename        = outDir + "EmptySignatureForm.pdf";
+        String filenameSigned1 = outDir + "EmptySignatureForm-signed1.pdf";
+        String filenameSigned2 = outDir + "EmptySignatureForm-signed2.pdf";
+
+        if (!externallySign)
+        {
+            return;
+        }
+
+        // load the keystore
+        KeyStore keystore = KeyStore.getInstance("PKCS12");
+        keystore.load(new FileInputStream(keystorePath), password.toCharArray());
+
+        // create file with empty signature
+        CreateEmptySignatureForm.main(new String[]{filename});
+
+        // sign PDF
+        CreateSignature signing1 = new CreateSignature(keystore, password.toCharArray());
+        signing1.setExternalSigning(false);
+        signing1.signDetached(new File(filename), new File(filenameSigned1));
+
+        checkSignature(new File(filenameSigned1));
+
+        try (PDDocument doc1 = PDDocument.load(new File(filenameSigned1)))
+        {
+            List<PDSignature> signatureDictionaries = doc1.getSignatureDictionaries();
+            Assert.assertEquals(1, signatureDictionaries.size());
+        }
+
+        // do visual signing in the field
+        try (FileInputStream fis = new FileInputStream(jpegPath))
+        {
+            CreateVisibleSignature signing2 = new CreateVisibleSignature(keystore, password.toCharArray());
+            signing2.setVisibleSignDesigner(filenameSigned1, 0, 0, -50, fis, 1);
+            signing2.setVisibleSignatureProperties("name", "location", "Security", 0, 1,
true);
+            signing2.setExternalSigning(externallySign);
+            signing2.signPDF(new File(filenameSigned1), new File(filenameSigned2), null,
"Signature1");
+        }
+
+        checkSignature(new File(filenameSigned2));
+
+        try (PDDocument doc2 = PDDocument.load(new File(filenameSigned2)))
+        {
+            List<PDSignature> signatureDictionaries = doc2.getSignatureDictionaries();
+            Assert.assertEquals(2, signatureDictionaries.size());
+        }
+    }
+
     private String getOutputFileName(String filePattern)
     {
         return MessageFormat.format(filePattern,(externallySign ? "_ext" : ""));
@@ -313,7 +383,7 @@ public class TestCreateSignature
             caught = true;
         }
         Assert.assertTrue("IllegalStateException should have been thrown", caught);
-        document.getLastSignatureDictionary().setByteRange(reserveByteRange);
+        signature.setByteRange(reserveByteRange);
         Assert.assertEquals(digestString, calculateDigestString(document.saveIncrementalForExternalSigning(new
ByteArrayOutputStream()).getContent()));
     }
 }



Mime
View raw message