Package eu.europa.esig.dss.pdf
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 Summary
Constructors Modifier Constructor Description protected
AbstractPDFSignatureService(PDFServiceMode serviceMode, SignatureDrawerFactory signatureDrawerFactory)
Constructor for the PDFSignatureService -
Method Summary
Modifier and Type Method Description DSSDocument
addDssDictionary(DSSDocument document, List<DSSDictionaryCallback> callbacks)
This method adds the DSS dictionary (Baseline-LT)DSSDocument
addNewSignatureField(DSSDocument document, SignatureFieldParameters parameters)
This method allows to add a new signature field to an existing pdf documentprotected Map<String,Long>
buildKnownObjects(List<DSSDictionaryCallback> callbacks)
This method builds a Map of known Objects (extracted from previous DSS Dictionaries).protected AnnotationBox
buildSignatureFieldBox(SignatureDrawer signatureDrawer)
Returns a SignatureFieldBox.protected abstract void
checkDocumentPermissions(DSSDocument toSignDocument, String pwd)
This method checks if the document is not encrypted or with limited edition rightsprotected AnnotationBox
checkVisibleSignatureFieldBoxPosition(PdfDocumentReader reader, SignatureFieldParameters parameters)
Checks if the signatureFieldBox overlaps with any existing annotations on the given pageprotected void
checkVisibleSignatureFieldBoxPosition(SignatureDrawer signatureDrawer, PdfDocumentReader documentReader, SignatureFieldParameters fieldParameters)
Checks validity of the SignatureField positionprotected byte[]
extractBeforeSignatureValue(ByteRange byteRange, byte[] signedContent)
Extract the content before the signature valueList<String>
getAvailableSignatureFields(DSSDocument document)
This method returns not signed signature-fieldsprotected PdfModificationDetection
getPdfModificationDetection(PdfDocumentReader finalRevisionReader, byte[] signedContent, String pwd)
Proceeds PDF modification detectionList<PdfRevision>
getRevisions(DSSDocument document, String pwd)
Retrieves revisions from a PDF documentprotected byte[]
getSignatureValue(DSSDocument dssDocument, ByteRange byteRange)
Gets the SignatureValue from thedssDocument
according to thebyteRange
Example: extracts bytes from 841 to 959. [0, 840, 960, 1200]protected String
getTokenDigest(Token token)
Gets SHA-256 digest of the tokenprotected String
getType()
Gets the type of the signature dictionaryprotected List<PdfModification>
getVisualDifferences(PdfDocumentReader signedRevisionReader, PdfDocumentReader finalRevisionReader)
Returns a list of visual differences between the provided PDF and the signed contentprotected 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 thebyteRange
NOTE: used for SIWA detectionprotected boolean
isDocTimestamp(PdfSignatureDictionary pdfSigDict)
Checks if the given signature dictionary represents a DocTimeStampprotected boolean
isDocumentTimestampLayer()
Checks if a DocumentTimestamp has to be added in the current modeprotected boolean
isSignature(PdfSignatureDictionary pdfSigDict)
Checks if the given signature dictionary represents a Signatureprotected abstract PdfDocumentReader
loadPdfDocumentReader(byte[] binaries, String passwordProtection)
LoadsPdfDocumentReader
instanceprotected abstract PdfDocumentReader
loadPdfDocumentReader(DSSDocument dssDocument, String passwordProtection)
LoadsPdfDocumentReader
instanceprotected SignatureDrawer
loadSignatureDrawer(SignatureImageParameters imageParameters)
Returns a SignatureDrawer initialized from a providedsignatureDrawerFactory
void
setAlertOnSignatureFieldOverlap(StatusAlert alertOnSignatureFieldOverlap)
Sets alert on a signature field overlap with existing fields or/and annotations Default : ExceptionOnStatusAlert - throw the exceptionvoid
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 pagesMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface eu.europa.esig.dss.pdf.PDFSignatureService
addDssDictionary, addNewSignatureField, digest, getAvailableSignatureFields, sign
-
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 layersignatureDrawerFactory
- the factory ofSignatureDrawer
-
-
Method Details
-
setAlertOnSignatureFieldOverlap
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
Returns a SignatureDrawer initialized from a providedsignatureDrawerFactory
- 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
Gets the type of the signature dictionary- Returns:
String
-
checkDocumentPermissions
This method checks if the document is not encrypted or with limited edition rights- Parameters:
toSignDocument
-DSSDocument
the document which will be modifiedpwd
-String
password protection phrase used to encrypt the document
-
getRevisions
Description copied from interface:PDFSignatureService
Retrieves revisions from a PDF document- Specified by:
getRevisions
in interfacePDFSignatureService
- Parameters:
document
- the document to extract revisions frompwd
- the password protection phrase used to encrypt the PDF document use 'null' value for not an encrypted document- Returns:
- list of extracted
PdfRevision
s
-
addDssDictionary
Description copied from interface:PDFSignatureService
This method adds the DSS dictionary (Baseline-LT)- Specified by:
addDssDictionary
in interfacePDFSignatureService
- Parameters:
document
- the document to be extendedcallbacks
- the callbacks to retrieve the revocation data,...- Returns:
- the pdf document with the added dss dictionary
-
getAvailableSignatureFields
Description copied from interface:PDFSignatureService
This method returns not signed signature-fields- Specified by:
getAvailableSignatureFields
in interfacePDFSignatureService
- Parameters:
document
- the pdf document- Returns:
- the list of empty signature fields
-
addNewSignatureField
Description copied from interface:PDFSignatureService
This method allows to add a new signature field to an existing pdf document- Specified by:
addNewSignatureField
in interfacePDFSignatureService
- Parameters:
document
- the pdf documentparameters
- 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, InvalidPasswordExceptionLoadsPdfDocumentReader
instance- Parameters:
dssDocument
-DSSDocument
to readpasswordProtection
-String
the password used to protect the document- Returns:
PdfDocumentReader
- Throws:
IOException
- in case of loading errorInvalidPasswordException
- if the password is not provided or invalid for a protected document
-
loadPdfDocumentReader
protected abstract PdfDocumentReader loadPdfDocumentReader(byte[] binaries, String passwordProtection) throws IOException, InvalidPasswordExceptionLoadsPdfDocumentReader
instance- Parameters:
binaries
- a byte arraypasswordProtection
-String
the password used to protect the document- Returns:
PdfDocumentReader
- Throws:
IOException
- in case of loading errorInvalidPasswordException
- 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 thebyteRange
NOTE: used for SIWA detection- Parameters:
document
-DSSDocument
to be validatedbyteRange
-ByteRange
cms
- binaries of the CMSSignedDatasignatureFieldNames
- a list ofString
- Returns:
- TRUE if the content value equals the byte range extraction, FALSE otherwise
-
getSignatureValue
protected byte[] getSignatureValue(DSSDocument dssDocument, ByteRange byteRange) throws IOExceptionGets the SignatureValue from thedssDocument
according to thebyteRange
Example: extracts bytes from 841 to 959. [0, 840, 960, 1200]- Parameters:
dssDocument
-DSSDocument
to processbyteRange
-ByteRange
specifying the signatureValue- Returns:
- signatureValue binaries
- Throws:
IOException
- if an exception occurs
-
extractBeforeSignatureValue
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
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
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
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 ofDSSDictionaryCallback
s- Returns:
- a map of built objects and their ids
-
getTokenDigest
Gets SHA-256 digest of the token -
checkVisibleSignatureFieldBoxPosition
protected void checkVisibleSignatureFieldBoxPosition(SignatureDrawer signatureDrawer, PdfDocumentReader documentReader, SignatureFieldParameters fieldParameters) throws IOExceptionChecks validity of the SignatureField position- Parameters:
signatureDrawer
-SignatureDrawer
documentReader
-PdfDocumentReader
fieldParameters
-SignatureFieldParameters
- Throws:
IOException
- if an exception occurs
-
buildSignatureFieldBox
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 IOExceptionChecks if the signatureFieldBox overlaps with any existing annotations on the given page- Parameters:
reader
-PdfDocumentReader
to be validatedparameters
-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 contentsignedContent
- a byte array representing a signed revision contentpwd
-String
password protection- Returns:
PdfModificationDetection
-
getVisualDifferences
protected List<PdfModification> getVisualDifferences(PdfDocumentReader signedRevisionReader, PdfDocumentReader finalRevisionReader) throws IOExceptionReturns a list of visual differences between the provided PDF and the signed content- Parameters:
signedRevisionReader
-PdfDocumentReader
for the signed revision contentfinalRevisionReader
-PdfDocumentReader
for the input PDF document- Returns:
- a list of
PdfModification
s - Throws:
IOException
- if an exception occurs
-