Class AbstractPDFSignatureService

java.lang.Object
eu.europa.esig.dss.pdf.AbstractPDFSignatureService
All Implemented Interfaces:
PDFSignatureService
Direct Known Subclasses:
ITextPDFSignatureService, PdfBoxSignatureService

public abstract class AbstractPDFSignatureService
extends Object
implements PDFSignatureService
The abstract implementation of a PDF signature service
  • Constructor Details

    • AbstractPDFSignatureService

      protected AbstractPDFSignatureService​(PDFServiceMode serviceMode, SignatureDrawerFactory signatureDrawerFactory)
      Constructor for the PDFSignatureService
      Parameters:
      serviceMode - current instance is used to generate DocumentTypestamp or Signature signature layer
      signatureDrawerFactory - the factory of SignatureDrawer
  • Method Details

    • setAlertOnSignatureFieldOverlap

      public void setAlertOnSignatureFieldOverlap​(StatusAlert alertOnSignatureFieldOverlap)
      Sets alert on a signature field overlap with existing fields or/and annotations Default : ExceptionOnStatusAlert - throw the exception
      Parameters:
      alertOnSignatureFieldOverlap - StatusAlert to execute
    • setMaximalPagesAmountForVisualComparison

      public void setMaximalPagesAmountForVisualComparison​(int pagesAmount)
      Sets a maximal pages amount in a PDF to process a visual screenshot comparison Example: for value 10, the visual comparison will be executed for a PDF containing 10 and less pages NOTE: In order to disable visual comparison check set the pages amount to 0 (zero) Default : 10 pages
      Parameters:
      pagesAmount - the amount of the pages to execute visual comparison for
    • loadSignatureDrawer

      protected SignatureDrawer loadSignatureDrawer​(SignatureImageParameters imageParameters)
      Returns a SignatureDrawer initialized from a provided signatureDrawerFactory
      Parameters:
      imageParameters - SignatureImageParameters to use
      Returns:
      SignatureDrawer
    • isDocumentTimestampLayer

      protected boolean isDocumentTimestampLayer()
      Checks if a DocumentTimestamp has to be added in the current mode
      Returns:
      TRUE if it is a DocumentTimestamp layer, FALSE otherwise
    • getType

      protected String getType()
      Gets the type of the signature dictionary
      Returns:
      String
    • checkDocumentPermissions

      protected abstract void checkDocumentPermissions​(DSSDocument toSignDocument, String pwd)
      This method checks if the document is not encrypted or with limited edition rights
      Parameters:
      toSignDocument - DSSDocument the document which will be modified
      pwd - String password protection phrase used to encrypt the document
    • getRevisions

      public List<PdfRevision> getRevisions​(DSSDocument document, String pwd)
      Description copied from interface: PDFSignatureService
      Retrieves revisions from a PDF document
      Specified by:
      getRevisions in interface PDFSignatureService
      Parameters:
      document - the document to extract revisions from
      pwd - the password protection phrase used to encrypt the PDF document use 'null' value for not an encrypted document
      Returns:
      list of extracted PdfRevisions
    • addDssDictionary

      public DSSDocument addDssDictionary​(DSSDocument document, List<DSSDictionaryCallback> callbacks)
      Description copied from interface: PDFSignatureService
      This method adds the DSS dictionary (Baseline-LT)
      Specified by:
      addDssDictionary in interface PDFSignatureService
      Parameters:
      document - the document to be extended
      callbacks - the callbacks to retrieve the revocation data,...
      Returns:
      the pdf document with the added dss dictionary
    • getAvailableSignatureFields

      public List<String> getAvailableSignatureFields​(DSSDocument document)
      Description copied from interface: PDFSignatureService
      This method returns not signed signature-fields
      Specified by:
      getAvailableSignatureFields in interface PDFSignatureService
      Parameters:
      document - the pdf document
      Returns:
      the list of empty signature fields
    • addNewSignatureField

      public DSSDocument addNewSignatureField​(DSSDocument document, SignatureFieldParameters parameters)
      Description copied from interface: PDFSignatureService
      This method allows to add a new signature field to an existing pdf document
      Specified by:
      addNewSignatureField in interface PDFSignatureService
      Parameters:
      document - the pdf document
      parameters - the parameters with the coordinates,... of the signature field
      Returns:
      the pdf document with the new added signature field
    • loadPdfDocumentReader

      protected abstract PdfDocumentReader loadPdfDocumentReader​(DSSDocument dssDocument, String passwordProtection) throws IOException, InvalidPasswordException
      Loads PdfDocumentReader instance
      Parameters:
      dssDocument - DSSDocument to read
      passwordProtection - String the password used to protect the document
      Returns:
      PdfDocumentReader
      Throws:
      IOException - in case of loading error
      InvalidPasswordException - if the password is not provided or invalid for a protected document
    • loadPdfDocumentReader

      protected abstract PdfDocumentReader loadPdfDocumentReader​(byte[] binaries, String passwordProtection) throws IOException, InvalidPasswordException
      Loads PdfDocumentReader instance
      Parameters:
      binaries - a byte array
      passwordProtection - String the password used to protect the document
      Returns:
      PdfDocumentReader
      Throws:
      IOException - in case of loading error
      InvalidPasswordException - if the password is not provided or invalid for a protected document
    • isContentValueEqualsByteRangeExtraction

      protected boolean isContentValueEqualsByteRangeExtraction​(DSSDocument document, ByteRange byteRange, byte[] cms, List<String> signatureFieldNames)
      Checks if the of the value incorporated into /Contents matches the range defined in the byteRange NOTE: used for SIWA detection
      Parameters:
      document - DSSDocument to be validated
      byteRange - ByteRange
      cms - binaries of the CMSSignedData
      signatureFieldNames - a list of String
      Returns:
      TRUE if the content value equals the byte range extraction, FALSE otherwise
    • getSignatureValue

      protected byte[] getSignatureValue​(DSSDocument dssDocument, ByteRange byteRange) throws IOException
      Gets the SignatureValue from the dssDocument according to the byteRange Example: extracts bytes from 841 to 959. [0, 840, 960, 1200]
      Parameters:
      dssDocument - DSSDocument to process
      byteRange - ByteRange specifying the signatureValue
      Returns:
      signatureValue binaries
      Throws:
      IOException - if an exception occurs
    • extractBeforeSignatureValue

      protected byte[] extractBeforeSignatureValue​(ByteRange byteRange, byte[] signedContent)
      Extract the content before the signature value
      Parameters:
      byteRange - ByteRange
      signedContent - byte array representing the signed content
      Returns:
      the first part of the byte range
    • isDocTimestamp

      protected boolean isDocTimestamp​(PdfSignatureDictionary pdfSigDict)
      Checks if the given signature dictionary represents a DocTimeStamp
      Parameters:
      pdfSigDict - PdfSignatureDictionary to check
      Returns:
      TRUE if the signature dictionary represents a DocTimeStamp, FALSE otherwise
    • isSignature

      protected boolean isSignature​(PdfSignatureDictionary pdfSigDict)
      Checks if the given signature dictionary represents a Signature
      Parameters:
      pdfSigDict - PdfSignatureDictionary to check
      Returns:
      TRUE if the signature dictionary represents a Signature, FALSE otherwise
    • buildKnownObjects

      protected Map<String,​Long> buildKnownObjects​(List<DSSDictionaryCallback> callbacks)
      This method builds a Map of known Objects (extracted from previous DSS Dictionaries). This map will be used to avoid duplicate the same objects between layers.
      Parameters:
      callbacks - a list of DSSDictionaryCallbacks
      Returns:
      a map of built objects and their ids
    • getTokenDigest

      protected String getTokenDigest​(Token token)
      Gets SHA-256 digest of the token
      Parameters:
      token - Token
      Returns:
      String base64 encoded SHA-256 digest
    • checkVisibleSignatureFieldBoxPosition

      protected void checkVisibleSignatureFieldBoxPosition​(SignatureDrawer signatureDrawer, PdfDocumentReader documentReader, SignatureFieldParameters fieldParameters) throws IOException
      Checks validity of the SignatureField position
      Parameters:
      signatureDrawer - SignatureDrawer
      documentReader - PdfDocumentReader
      fieldParameters - SignatureFieldParameters
      Throws:
      IOException - if an exception occurs
    • buildSignatureFieldBox

      protected AnnotationBox buildSignatureFieldBox​(SignatureDrawer signatureDrawer) throws IOException
      Returns a SignatureFieldBox. Used for a SignatureField position validation.
      Parameters:
      signatureDrawer - SignatureDrawer
      Returns:
      AnnotationBox
      Throws:
      IOException - if an exception occurs
    • checkVisibleSignatureFieldBoxPosition

      protected AnnotationBox checkVisibleSignatureFieldBoxPosition​(PdfDocumentReader reader, SignatureFieldParameters parameters) throws IOException
      Checks if the signatureFieldBox overlaps with any existing annotations on the given page
      Parameters:
      reader - PdfDocumentReader to be validated
      parameters - SignatureFieldParameters
      Returns:
      AnnotationBox computed signature field box
      Throws:
      IOException - if an exception occurs
    • getPdfModificationDetection

      protected PdfModificationDetection getPdfModificationDetection​(PdfDocumentReader finalRevisionReader, byte[] signedContent, String pwd)
      Proceeds PDF modification detection
      Parameters:
      finalRevisionReader - PdfDocumentReader the reader for the final PDF content
      signedContent - a byte array representing a signed revision content
      pwd - String password protection
      Returns:
      PdfModificationDetection
    • getVisualDifferences

      protected List<PdfModification> getVisualDifferences​(PdfDocumentReader signedRevisionReader, PdfDocumentReader finalRevisionReader) throws IOException
      Returns a list of visual differences between the provided PDF and the signed content
      Parameters:
      signedRevisionReader - PdfDocumentReader for the signed revision content
      finalRevisionReader - PdfDocumentReader for the input PDF document
      Returns:
      a list of PdfModifications
      Throws:
      IOException - if an exception occurs