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/10/10 09:58:54 UTC

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

Author: tilman
Date: Sat Oct 10 09:58:54 2020
New Revision: 1882385

URL: http://svn.apache.org/viewvc?rev=1882385&view=rev
Log:
PDFBOX-4297: use the new method to get the signature contents

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

Modified: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java?rev=1882385&r1=1882384&r2=1882385&view=diff
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java (original)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java Sat Oct 10 09:58:54 2020
@@ -134,7 +134,7 @@ public final class ShowSignature
                 for (PDSignature sig : document.getSignatureDictionaries())
                 {
                     COSDictionary sigDict = sig.getCOSObject();
-                    COSString contents = (COSString) sigDict.getDictionaryObject(COSName.CONTENTS);
+                    byte[] contents = sig.getContents();
 
                     // download the signed content
                     FileInputStream fis = new FileInputStream(infile);
@@ -169,9 +169,8 @@ public final class ShowSignature
                         else if (subFilter.equals("adbe.pkcs7.sha1"))
                         {
                             // example: PDFBOX-1452.pdf
-                            byte[] certData = contents.getBytes();
                             CertificateFactory factory = CertificateFactory.getInstance("X.509");
-                            ByteArrayInputStream certStream = new ByteArrayInputStream(certData);
+                            ByteArrayInputStream certStream = new ByteArrayInputStream(contents);
                             Collection<? extends Certificate> certs = factory.generateCertificates(certStream);
                             System.out.println("certs=" + certs);
 
@@ -260,7 +259,7 @@ public final class ShowSignature
                         long fileLen = infile.length();
                         long rangeMax = byteRange[2] + (long) byteRange[3];
                         // multiply content length with 2 (because it is in hex in the PDF) and add 2 for < and >
-                        int contentLen = contents.getString().length() * 2 + 2;
+                        int contentLen = contents.length * 2 + 2;
                         if (fileLen != rangeMax || byteRange[0] != 0 || byteRange[1] + contentLen != byteRange[2])
                         {
                             // a false result doesn't necessarily mean that the PDF is a fake
@@ -296,7 +295,7 @@ public final class ShowSignature
         }
     }
 
-    private void checkContentValueWithFile(File file, int[] byteRange, COSString contents) throws IOException
+    private void checkContentValueWithFile(File file, int[] byteRange, byte[] contents) throws IOException
     {
         // https://stackoverflow.com/questions/55049270
         // comment by mkl: check whether gap contains a hex value equal
@@ -310,7 +309,7 @@ public final class ShowSignature
             System.err.println("'<' expected at offset " + byteRange[1] + ", but got " + (char) c);
         }
         byte[] contentFromFile = raf.readFully(byteRange[2] - byteRange[1] - 2);
-        byte[] contentAsHex = Hex.getString(contents.getBytes()).getBytes(Charsets.US_ASCII);
+        byte[] contentAsHex = Hex.getString(contents).getBytes(Charsets.US_ASCII);
         if (contentFromFile.length != contentAsHex.length)
         {
             System.err.println("Raw content length from file is " +
@@ -348,16 +347,16 @@ public final class ShowSignature
         raf.close();
     }
 
-    private void verifyETSIdotRFC3161(byte[] buf, COSString contents)
+    private void verifyETSIdotRFC3161(byte[] buf, byte[] contents)
             throws CertificateException, CMSException, IOException, OperatorCreationException,
             TSPException, NoSuchAlgorithmException, CertificateVerificationException
     {
-        TimeStampToken timeStampToken = new TimeStampToken(new CMSSignedData(contents.getBytes()));
+        TimeStampToken timeStampToken = new TimeStampToken(new CMSSignedData(contents));
         System.out.println("Time stamp gen time: " + timeStampToken.getTimeStampInfo().getGenTime());
         System.out.println("Time stamp tsa name: " + timeStampToken.getTimeStampInfo().getTsa().getName());
         
         CertificateFactory factory = CertificateFactory.getInstance("X.509");
-        ByteArrayInputStream certStream = new ByteArrayInputStream(contents.getBytes());
+        ByteArrayInputStream certStream = new ByteArrayInputStream(contents);
         Collection<? extends Certificate> certs = factory.generateCertificates(certStream);
         System.out.println("certs=" + certs);
         
@@ -393,7 +392,7 @@ public final class ShowSignature
      * @throws GeneralSecurityException
      * @throws TSPException
      */
-    private void verifyPKCS7(byte[] byteArray, COSString contents, PDSignature sig)
+    private void verifyPKCS7(byte[] byteArray, byte[] contents, PDSignature sig)
             throws CMSException, OperatorCreationException,
                    IOException, GeneralSecurityException, TSPException, CertificateVerificationException
     {
@@ -401,7 +400,7 @@ public final class ShowSignature
         // http://stackoverflow.com/a/26702631/535646
         // http://stackoverflow.com/a/9261365/535646
         CMSProcessable signedContent = new CMSProcessableByteArray(byteArray);
-        CMSSignedData signedData = new CMSSignedData(signedContent, contents.getBytes());
+        CMSSignedData signedData = new CMSSignedData(signedContent, contents);
         @SuppressWarnings("unchecked")
         Store<X509CertificateHolder> certificatesStore = signedData.getCertificates();
         if (certificatesStore.getMatches(null).isEmpty())