title: PDF Signature Validation
body: <p>I am trying to validate a PDF Signature using PDFBox and BouncyCastle. My code works for most PDF:s, however there is one file, the cryptographic validation using BouncyCastle fails. I'm using pdfbox 1.8, BouncyCastle 1.52. The test input pdf file is randomly got from somewhere, it seems that it is generated using iText. <a href="https://drive.google.com/file/d/0BzEmZ9pRWLhPOUJSYUdlRjg2eEU/view?usp=sharing" rel="nofollow noreferrer">Test pdf file</a></p>
  
  <pre><code>public void testValidateSignature() throws Exception
  {
      byte[] pdfByte;
      PDDocument pdfDoc = null;
      SignerInformationVerifier verifier = null;
      try
      {
          pdfByte = IOUtils.toByteArray( this.getClass().getResourceAsStream( "SignatureVlidationTest.pdf" ) );
          pdfDoc = PDDocument.load( new ByteArrayInputStream( pdfByte ));
          PDSignature signature = pdfDoc.getSignatureDictionaries().get( 0 );
  
          byte[] signatureAsBytes = signature.getContents( pdfByte );
          byte[] signedContentAsBytes = signature.getSignedContent( pdfByte );
          CMSSignedData cms = new CMSSignedData( new CMSProcessableByteArray( signedContentAsBytes ), signatureAsBytes);
          SignerInformation signerInfo = (SignerInformation)cms.getSignerInfos().getSigners().iterator().next();
          X509CertificateHolder cert = (X509CertificateHolder)cms.getCertificates().getMatches( signerInfo.getSID() ).iterator().next();
          verifier = new JcaSimpleSignerInfoVerifierBuilder( ).setProvider( new BouncyCastleProvider() ).build( cert );
  
          // result if false
          boolean verifyRt = signerInfo.verify( verifier );
  
      }
      finally
      {
          if( pdfDoc != null )
          {
              pdfDoc.close();
          }
      }
  
  }
  </code></pre>