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 protectedAbstractPDFSignatureService(PDFServiceMode serviceMode, SignatureDrawerFactory signatureDrawerFactory)Constructor for the PDFSignatureService -
Method Summary
Modifier and Type Method Description DSSDocumentaddDssDictionary(DSSDocument document, List<DSSDictionaryCallback> callbacks)This method adds the DSS dictionary (Baseline-LT)DSSDocumentaddNewSignatureField(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 AnnotationBoxbuildSignatureFieldBox(SignatureDrawer signatureDrawer)Returns a SignatureFieldBox.protected abstract voidcheckDocumentPermissions(DSSDocument toSignDocument, String pwd)This method checks if the document is not encrypted or with limited edition rightsprotected AnnotationBoxcheckVisibleSignatureFieldBoxPosition(PdfDocumentReader reader, SignatureFieldParameters parameters)Checks if the signatureFieldBox overlaps with any existing annotations on the given pageprotected voidcheckVisibleSignatureFieldBoxPosition(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 PdfModificationDetectiongetPdfModificationDetection(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 thedssDocumentaccording to thebyteRangeExample: extracts bytes from 841 to 959. [0, 840, 960, 1200]protected StringgetTokenDigest(Token token)Gets SHA-256 digest of the tokenprotected StringgetType()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 booleanisContentValueEqualsByteRangeExtraction(DSSDocument document, ByteRange byteRange, byte[] cms, List<String> signatureFieldNames)Checks if the of the value incorporated into /Contents matches the range defined in thebyteRangeNOTE: used for SIWA detectionprotected booleanisDocTimestamp(PdfSignatureDictionary pdfSigDict)Checks if the given signature dictionary represents a DocTimeStampprotected booleanisDocumentTimestampLayer()Checks if a DocumentTimestamp has to be added in the current modeprotected booleanisSignature(PdfSignatureDictionary pdfSigDict)Checks if the given signature dictionary represents a Signatureprotected abstract PdfDocumentReaderloadPdfDocumentReader(byte[] binaries, String passwordProtection)LoadsPdfDocumentReaderinstanceprotected abstract PdfDocumentReaderloadPdfDocumentReader(DSSDocument dssDocument, String passwordProtection)LoadsPdfDocumentReaderinstanceprotected SignatureDrawerloadSignatureDrawer(SignatureImageParameters imageParameters)Returns a SignatureDrawer initialized from a providedsignatureDrawerFactoryvoidsetAlertOnSignatureFieldOverlap(StatusAlert alertOnSignatureFieldOverlap)Sets alert on a signature field overlap with existing fields or/and annotations Default : ExceptionOnStatusAlert - throw the exceptionvoidsetMaximalPagesAmountForVisualComparison(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, waitMethods 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-StatusAlertto 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-SignatureImageParametersto 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-DSSDocumentthe document which will be modifiedpwd-Stringpassword protection phrase used to encrypt the document
-
getRevisions
Description copied from interface:PDFSignatureServiceRetrieves revisions from a PDF document- Specified by:
getRevisionsin 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
PdfRevisions
-
addDssDictionary
Description copied from interface:PDFSignatureServiceThis method adds the DSS dictionary (Baseline-LT)- Specified by:
addDssDictionaryin 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:PDFSignatureServiceThis method returns not signed signature-fields- Specified by:
getAvailableSignatureFieldsin interfacePDFSignatureService- Parameters:
document- the pdf document- Returns:
- the list of empty signature fields
-
addNewSignatureField
Description copied from interface:PDFSignatureServiceThis method allows to add a new signature field to an existing pdf document- Specified by:
addNewSignatureFieldin 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, InvalidPasswordExceptionLoadsPdfDocumentReaderinstance- Parameters:
dssDocument-DSSDocumentto readpasswordProtection-Stringthe 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, InvalidPasswordExceptionLoadsPdfDocumentReaderinstance- Parameters:
binaries- a byte arraypasswordProtection-Stringthe 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 thebyteRangeNOTE: used for SIWA detection- Parameters:
document-DSSDocumentto be validatedbyteRange-ByteRangecms- 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 thedssDocumentaccording to thebyteRangeExample: extracts bytes from 841 to 959. [0, 840, 960, 1200]- Parameters:
dssDocument-DSSDocumentto processbyteRange-ByteRangespecifying the signatureValue- Returns:
- signatureValue binaries
- Throws:
IOException- if an exception occurs
-
extractBeforeSignatureValue
Extract the content before the signature value- Parameters:
byteRange-ByteRangesignedContent- 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-PdfSignatureDictionaryto check- Returns:
- TRUE if the signature dictionary represents a DocTimeStamp, FALSE otherwise
-
isSignature
Checks if the given signature dictionary represents a Signature- Parameters:
pdfSigDict-PdfSignatureDictionaryto 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 ofDSSDictionaryCallbacks- 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-SignatureDrawerdocumentReader-PdfDocumentReaderfieldParameters-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-PdfDocumentReaderto be validatedparameters-SignatureFieldParameters- Returns:
AnnotationBoxcomputed 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-PdfDocumentReaderthe reader for the final PDF contentsignedContent- a byte array representing a signed revision contentpwd-Stringpassword 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-PdfDocumentReaderfor the signed revision contentfinalRevisionReader-PdfDocumentReaderfor the input PDF document- Returns:
- a list of
PdfModifications - Throws:
IOException- if an exception occurs
-