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
The abstract implementation of a PDF signature service
-
Field Summary
Modifier and TypeFieldDescriptionprotected PdfDifferencesFinder
Used to find differences occurred between PDF revisions (e.g. visible changes).protected PdfObjectModificationsFinder
Used to find differences within internal PDF objects occurred between PDF revisions .protected PdfPermissionsChecker
Used to verify PDF document permissions regarding a new signature creationprotected PdfSignatureFieldPositionChecker
Used to verify the signature field position placement validityprotected DSSResourcesHandlerBuilder
The builder to be used to create a newDSSResourcesHandler
for each internal call, defining a way working with internal resources (e.g. in memory or by using temporary files). -
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractPDFSignatureService
(PDFServiceMode serviceMode, SignatureDrawerFactory signatureDrawerFactory) Constructor for the PDFSignatureService -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addDeveloperExtension
(PdfDocumentReader documentReader, String prefix, PdfDict developerExtension) Adds a new developer extension defined indeveloperExtension
dictionaryaddDssDictionary
(DSSDocument document, PdfValidationDataContainer validationDataForInclusion) This method adds the DSS dictionary (Baseline-LT) to a document without password-protection and without VRI dictionary.addDssDictionary
(DSSDocument document, PdfValidationDataContainer validationDataForInclusion, char[] pwd) This method adds the DSS dictionary (Baseline-LT) to a password-protected document without inclusion of VRI dictionary.addNewSignatureField
(DSSDocument document, SignatureFieldParameters parameters) This method allows to add a new signature field to an existing pdf documentprotected void
analyzePdfModifications
(DSSDocument document, PdfCMSRevision pdfRevision, PdfDocumentReader finalRevisionReader, char[] pwd) This method performs a modification analysis for a single givenpdfRevision
void
analyzePdfModifications
(DSSDocument document, List<AdvancedSignature> signatures, char[] pwd) Analyze the PDF revision and try to detect any modification (shadow attacks) for signaturesvoid
analyzeTimestampPdfModifications
(DSSDocument document, List<TimestampToken> timestamps, char[] pwd) Analyze the PDF revision and try to detect any modification (shadow attacks) for PDf document timestampsprotected void
assertSignatureFieldPositionValid
(PdfDocumentReader documentReader, AnnotationBox annotationBox, int pageNumber) This method verifies validity of the signature field box configuration calling the providedpdfSignatureFieldPositionChecker
protected AnnotationBox
buildSignatureFieldBox
(SignatureDrawer signatureDrawer) Returns a SignatureFieldBox.protected void
checkPdfPermissions
(PdfDocumentReader documentReader, SignatureFieldParameters fieldParameters) This method verifies the PDF permissions dictionariesprotected abstract DSSMessageDigest
computeDigest
(DSSDocument toSignDocument, PAdESCommonParameters parameters) Computes digest on to be signed data computed on thetoSignDocument
respectively to the givenparameters
protected PdfDict
createDeveloperExtensionDict
(PdfDocumentReader documentReader, String baseVersion, Integer extensionLevel, String extensionRevision, String type, String url) Creates a new developer extension dictionary with the given configurationprotected void
digitalSignatureEnhancement
(PdfDocumentReader documentReader, PAdESCommonParameters parameters) This method ensures the PDF document structure is correct for inclusion of specific digital signature's functionalitiesprotected void
ensureESICDeveloperExtension1
(PdfDocumentReader documentReader) This method verifies presence of the ESIC developer extension with level 1 in the PDF document.protected void
ensureESICDeveloperExtension2
(PdfDocumentReader documentReader) This method verifies presence of the ESIC developer extension with level 2 in the PDF document.protected void
ensureISO_32001DeveloperExtension
(PdfDocumentReader documentReader) This method verifies presence of the ISO 32001 developer extension in the PDF document.protected void
ensureISO_32002DeveloperExtension
(PdfDocumentReader documentReader) This method verifies presence of the ISO 32002 developer extension in the PDF document.getAvailableSignatureFields
(DSSDocument document) This method returns not signed signature-fieldsgetRevisions
(DSSDocument document, char[] pwd) Retrieves revisions from a PDF documentprotected String
getType()
Gets the type of the signature dictionaryprotected AnnotationBox
getVisibleSignatureFieldBoxPosition
(PdfDocumentReader reader, SignatureFieldParameters parameters) Checks if the signatureFieldBox overlaps with any existing annotations on the given page and returns the respectful signature field boxprotected AnnotationBox
getVisibleSignatureFieldBoxPosition
(SignatureDrawer signatureDrawer, PdfDocumentReader documentReader, SignatureFieldParameters fieldParameters) Checks validity of the SignatureField position and returns the calculated signature field boxprotected List
<PdfModification> getVisualDifferences
(PdfDocumentReader signedRevisionReader, PdfDocumentReader finalRevisionReader) Returns a list of visual differences between the provided PDF and the signed contentprotected DSSResourcesHandler
This method instantiates a newDSSResourcesFactory
protected boolean
isCAdESDetached
(PAdESCommonParameters parameters) Verifies if the signature is created with a use of "ETSI.CAdES.detached" SubFilterprotected boolean
isDeveloperExtensionPresent
(PdfDocumentReader documentReader, String prefix, PdfDict developerExtension) Verifies whether the specified developer extension is present in the document's catalog.protected boolean
isDocTimestamp
(PdfSignatureDictionary pdfSigDict) Checks if the given signature dictionary represents a DocTimeStampprotected boolean
Checks if a DocumentTimestamp has to be added in the current modeprotected boolean
isISO_32001
(PAdESCommonParameters parameters) Verifies if the ISO_ profile for 32001 shall be activatedprotected boolean
isISO_32002
(PAdESCommonParameters parameters) Verifies if the ISO_ profile for 32002 shall be activatedprotected boolean
isSignature
(PdfSignatureDictionary pdfSigDict) Checks if the given signature dictionary represents a Signatureprotected abstract PdfDocumentReader
loadPdfDocumentReader
(DSSDocument dssDocument, char[] passwordProtection) LoadsPdfDocumentReader
instanceprotected SignatureDrawer
loadSignatureDrawer
(SignatureImageParameters imageParameters) Returns a SignatureDrawer initialized from a providedsignatureDrawerFactory
messageDigest
(DSSDocument toSignDocument, PAdESCommonParameters parameters) Returns the message-digest computed on PDF signature revision's ByteRangevoid
setPdfDifferencesFinder
(PdfDifferencesFinder pdfDifferencesFinder) Sets thePdfDifferencesFinder
used to find the differences on pages between given PDF revisions.void
setPdfObjectModificationsFinder
(PdfObjectModificationsFinder pdfObjectModificationsFinder) Sets thePdfObjectModificationsFinder
used to find the differences between internal PDF objects occurred between given PDF revisions.void
setPdfPermissionsChecker
(PdfPermissionsChecker pdfPermissionsChecker) Sets thePdfPermissionsChecker
used to verify the PDF document rules for a new signature creationvoid
setPdfSignatureFieldPositionChecker
(PdfSignatureFieldPositionChecker pdfSignatureFieldPositionChecker) Sets thePdfSignatureFieldPositionChecker
used to verify the validity of new signature field placement.void
setResourcesHandlerBuilder
(DSSResourcesHandlerBuilder resourcesHandlerBuilder) SetsDSSResourcesFactoryBuilder
to be used for aDSSResourcesHandler
creation in internal methods.sign
(DSSDocument toSignDocument, byte[] cmsSignedData, PAdESCommonParameters parameters) Signs a PDF documentprotected abstract DSSDocument
signDocument
(DSSDocument toSignDocument, byte[] cmsSignedData, PAdESCommonParameters parameters) This method creates a signed document from the originaltoSignDocument
, incorporating a new revision, enveloping the providedcmsSignedData
protected AnnotationBox
toPdfPageCoordinates
(AnnotationBox fieldAnnotationBox, AnnotationBox pageBox) This method transforms afieldAnnotationBox
's positions and dimensions according to the given pageprotected boolean
validateByteRange
(ByteRange byteRange, DSSDocument document, byte[] cms) This method verifies the validity of /ByteRange field against the extracted from /Contents fieldcms
and the current pdfdocument
Methods 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, getAvailableSignatureFields, previewPageWithVisualSignature, previewSignatureField
-
Field Details
-
resourcesHandlerBuilder
The builder to be used to create a newDSSResourcesHandler
for each internal call, defining a way working with internal resources (e.g. in memory or by using temporary files). The resources are used on a document creation Default :eu.europa.esig.dss.signature.resources.InMemoryResourcesHandler
, working with data in memory -
pdfDifferencesFinder
Used to find differences occurred between PDF revisions (e.g. visible changes). Default :DefaultPdfDifferencesFinder
-
pdfObjectModificationsFinder
Used to find differences within internal PDF objects occurred between PDF revisions . Default :DefaultPdfModificationsFinder
-
pdfPermissionsChecker
Used to verify PDF document permissions regarding a new signature creation -
pdfSignatureFieldPositionChecker
Used to verify the signature field position placement validity
-
-
Constructor Details
-
AbstractPDFSignatureService
protected AbstractPDFSignatureService(PDFServiceMode serviceMode, SignatureDrawerFactory signatureDrawerFactory) Constructor for the PDFSignatureService- Parameters:
serviceMode
- current instance is used to generate Signature or DocumentTimeStamp revisionsignatureDrawerFactory
- the factory ofSignatureDrawer
-
-
Method Details
-
setResourcesHandlerBuilder
Description copied from interface:PDFSignatureService
SetsDSSResourcesFactoryBuilder
to be used for aDSSResourcesHandler
creation in internal methods.DSSResourcesHandler
defines a way to operate with OutputStreams and createDSSDocument
s. Default :eu.europa.esig.dss.signature.resources.InMemoryResourcesHandler
. Works with data in memory.- Specified by:
setResourcesHandlerBuilder
in interfacePDFSignatureService
- Parameters:
resourcesHandlerBuilder
-DSSResourcesHandlerBuilder
-
setPdfDifferencesFinder
Description copied from interface:PDFSignatureService
Sets thePdfDifferencesFinder
used to find the differences on pages between given PDF revisions. Default :eu.europa.esig.dss.pdf.modifications.DefaultPdfDifferencesFinder
- Specified by:
setPdfDifferencesFinder
in interfacePDFSignatureService
- Parameters:
pdfDifferencesFinder
-PdfDifferencesFinder
-
setPdfObjectModificationsFinder
public void setPdfObjectModificationsFinder(PdfObjectModificationsFinder pdfObjectModificationsFinder) Description copied from interface:PDFSignatureService
Sets thePdfObjectModificationsFinder
used to find the differences between internal PDF objects occurred between given PDF revisions. Default :eu.europa.esig.dss.pdf.modifications.DefaultPdfObjectModificationsFinder
- Specified by:
setPdfObjectModificationsFinder
in interfacePDFSignatureService
- Parameters:
pdfObjectModificationsFinder
-PdfObjectModificationsFinder
-
setPdfPermissionsChecker
Description copied from interface:PDFSignatureService
Sets thePdfPermissionsChecker
used to verify the PDF document rules for a new signature creation- Specified by:
setPdfPermissionsChecker
in interfacePDFSignatureService
- Parameters:
pdfPermissionsChecker
-PdfPermissionsChecker
-
setPdfSignatureFieldPositionChecker
public void setPdfSignatureFieldPositionChecker(PdfSignatureFieldPositionChecker pdfSignatureFieldPositionChecker) Description copied from interface:PDFSignatureService
Sets thePdfSignatureFieldPositionChecker
used to verify the validity of new signature field placement. For example to ensure the new signature field lies within PDF page borders and/or it does not overlap with existing signature fields.- Specified by:
setPdfSignatureFieldPositionChecker
in interfacePDFSignatureService
- Parameters:
pdfSignatureFieldPositionChecker
-PdfPermissionsChecker
-
loadSignatureDrawer
Returns a SignatureDrawer initialized from a providedsignatureDrawerFactory
- Parameters:
imageParameters
-SignatureImageParameters
to use- Returns:
SignatureDrawer
-
instantiateResourcesHandler
This method instantiates a newDSSResourcesFactory
- Returns:
DSSResourcesHandler
- Throws:
IOException
- if an error occurs on DSSResourcesHandler instantiation
-
messageDigest
Description copied from interface:PDFSignatureService
Returns the message-digest computed on PDF signature revision's ByteRange- Specified by:
messageDigest
in interfacePDFSignatureService
- Parameters:
toSignDocument
- the document to be signedparameters
- the signature/timestamp parameters- Returns:
DSSMessageDigest
-
computeDigest
protected abstract DSSMessageDigest computeDigest(DSSDocument toSignDocument, PAdESCommonParameters parameters) Computes digest on to be signed data computed on thetoSignDocument
respectively to the givenparameters
- Parameters:
toSignDocument
-DSSDocument
to be signedparameters
-PAdESCommonParameters
- Returns:
DSSMessageDigest
-
sign
public DSSDocument sign(DSSDocument toSignDocument, byte[] cmsSignedData, PAdESCommonParameters parameters) Description copied from interface:PDFSignatureService
Signs a PDF document- Specified by:
sign
in interfacePDFSignatureService
- Parameters:
toSignDocument
- the pdf document to be signedcmsSignedData
- the encoded CMS Signed dataparameters
- the signature/timestamp parameters- Returns:
DSSDocument
-
signDocument
protected abstract DSSDocument signDocument(DSSDocument toSignDocument, byte[] cmsSignedData, PAdESCommonParameters parameters) This method creates a signed document from the originaltoSignDocument
, incorporating a new revision, enveloping the providedcmsSignedData
- Parameters:
toSignDocument
-DSSDocument
to be signedcmsSignedData
- byte array representing the encoded CMS signed data's binariesparameters
-PAdESCommonParameters
- Returns:
DSSDocument
-
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
-
digitalSignatureEnhancement
protected void digitalSignatureEnhancement(PdfDocumentReader documentReader, PAdESCommonParameters parameters) This method ensures the PDF document structure is correct for inclusion of specific digital signature's functionalities- Parameters:
documentReader
-PdfDocumentReader
to be extendedparameters
-PAdESCommonParameters
-
ensureESICDeveloperExtension1
This method verifies presence of the ESIC developer extension with level 1 in the PDF document. Creates one, when not present.- Parameters:
documentReader
-PdfDocumentReader
-
ensureESICDeveloperExtension2
This method verifies presence of the ESIC developer extension with level 2 in the PDF document. Creates one, when not present.- Parameters:
documentReader
-PdfDocumentReader
-
ensureISO_32001DeveloperExtension
This method verifies presence of the ISO 32001 developer extension in the PDF document. Creates one, when not present.- Parameters:
documentReader
-PdfDocumentReader
-
ensureISO_32002DeveloperExtension
This method verifies presence of the ISO 32002 developer extension in the PDF document. Creates one, when not present.- Parameters:
documentReader
-PdfDocumentReader
-
createDeveloperExtensionDict
-
isCAdESDetached
Verifies if the signature is created with a use of "ETSI.CAdES.detached" SubFilter- Parameters:
parameters
-PAdESCommonParameters
- Returns:
- TRUE if the "ETSI.CAdES.detached" SubFilter is used, FALSE otherwise
-
isISO_32001
Verifies if the ISO_ profile for 32001 shall be activated- Parameters:
parameters
-PAdESCommonParameters
- Returns:
- TRUE if the ISO_ developer extension shall be included, FALSE otherwise
-
isISO_32002
Verifies if the ISO_ profile for 32002 shall be activated- Parameters:
parameters
-PAdESCommonParameters
- Returns:
- TRUE if the ISO_ developer extension shall be included, FALSE otherwise
-
isDeveloperExtensionPresent
protected boolean isDeveloperExtensionPresent(PdfDocumentReader documentReader, String prefix, PdfDict developerExtension) Verifies whether the specified developer extension is present in the document's catalog. The extension shall fully match the defined parameters.- Parameters:
documentReader
-PdfDocumentReader
prefix
-String
developerExtension
-PdfDict
- Returns:
- TRUE if the extension is present, FALSE otherwise
-
addDeveloperExtension
protected void addDeveloperExtension(PdfDocumentReader documentReader, String prefix, PdfDict developerExtension) Adds a new developer extension defined indeveloperExtension
dictionary- Parameters:
documentReader
-PdfDocumentReader
prefix
-String
developerExtension
-PdfDict
-
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
public DSSDocument addDssDictionary(DSSDocument document, PdfValidationDataContainer validationDataForInclusion) Description copied from interface:PDFSignatureService
This method adds the DSS dictionary (Baseline-LT) to a document without password-protection and without VRI dictionary.- Specified by:
addDssDictionary
in interfacePDFSignatureService
- Parameters:
document
- the document to be extendedvalidationDataForInclusion
-PdfValidationDataContainer
- Returns:
- the pdf document with the added dss dictionary
-
addDssDictionary
public DSSDocument addDssDictionary(DSSDocument document, PdfValidationDataContainer validationDataForInclusion, char[] pwd) Description copied from interface:PDFSignatureService
This method adds the DSS dictionary (Baseline-LT) to a password-protected document without inclusion of VRI dictionary.- Specified by:
addDssDictionary
in interfacePDFSignatureService
- Parameters:
document
- the document to be extendedvalidationDataForInclusion
-PdfValidationDataContainer
pwd
- the password protection used to create the encrypted document (optional)- 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, char[] passwordProtection) throws IOException, InvalidPasswordException LoadsPdfDocumentReader
instance- Parameters:
dssDocument
-DSSDocument
to readpasswordProtection
- 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
-
validateByteRange
This method verifies the validity of /ByteRange field against the extracted from /Contents fieldcms
and the current pdfdocument
- Parameters:
byteRange
-ByteRange
to be validateddocument
-DSSDocument
current PDF documentcms
- byte array representing the binaries extracted from /Contents field- Returns:
- TRUE if the /ByteRange is valid, FALSE otherwise
-
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
-
getVisibleSignatureFieldBoxPosition
protected AnnotationBox getVisibleSignatureFieldBoxPosition(SignatureDrawer signatureDrawer, PdfDocumentReader documentReader, SignatureFieldParameters fieldParameters) throws IOException Checks validity of the SignatureField position and returns the calculated signature field box- Parameters:
signatureDrawer
-SignatureDrawer
documentReader
-PdfDocumentReader
fieldParameters
-SignatureFieldParameters
- Returns:
AnnotationBox
- 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
-
getVisibleSignatureFieldBoxPosition
protected AnnotationBox getVisibleSignatureFieldBoxPosition(PdfDocumentReader reader, SignatureFieldParameters parameters) throws IOException Checks if the signatureFieldBox overlaps with any existing annotations on the given page and returns the respectful signature field box- Parameters:
reader
-PdfDocumentReader
to be validatedparameters
-SignatureFieldParameters
- Returns:
AnnotationBox
computed signature field box- Throws:
IOException
- if an exception occurs
-
assertSignatureFieldPositionValid
protected void assertSignatureFieldPositionValid(PdfDocumentReader documentReader, AnnotationBox annotationBox, int pageNumber) This method verifies validity of the signature field box configuration calling the providedpdfSignatureFieldPositionChecker
- Parameters:
documentReader
-PdfDocumentReader
document where the new signature field should be createdannotationBox
-AnnotationBox
defining position and dimensions of the new signature fieldpageNumber
- the number of a page where the new signature should be created
-
toPdfPageCoordinates
protected AnnotationBox toPdfPageCoordinates(AnnotationBox fieldAnnotationBox, AnnotationBox pageBox) This method transforms afieldAnnotationBox
's positions and dimensions according to the given page- Parameters:
fieldAnnotationBox
-AnnotationBox
computed field of a signaturepageBox
-AnnotationBox
page's box- Returns:
AnnotationBox
-
analyzePdfModifications
public void analyzePdfModifications(DSSDocument document, List<AdvancedSignature> signatures, char[] pwd) Description copied from interface:PDFSignatureService
Analyze the PDF revision and try to detect any modification (shadow attacks) for signatures- Specified by:
analyzePdfModifications
in interfacePDFSignatureService
- Parameters:
document
-DSSDocument
the documentsignatures
- the different signatures to be analysedpwd
-String
password protection
-
analyzeTimestampPdfModifications
public void analyzeTimestampPdfModifications(DSSDocument document, List<TimestampToken> timestamps, char[] pwd) Description copied from interface:PDFSignatureService
Analyze the PDF revision and try to detect any modification (shadow attacks) for PDf document timestamps- Specified by:
analyzeTimestampPdfModifications
in interfacePDFSignatureService
- Parameters:
document
-DSSDocument
the documenttimestamps
- the detached document timestamps to be analysedpwd
-String
password protection
-
analyzePdfModifications
protected void analyzePdfModifications(DSSDocument document, PdfCMSRevision pdfRevision, PdfDocumentReader finalRevisionReader, char[] pwd) throws IOException This method performs a modification analysis for a single givenpdfRevision
- Parameters:
document
-DSSDocument
the validating documentpdfRevision
-PdfCMSRevision
signature revision to be validatedfinalRevisionReader
-PdfDocumentReader
final document revisionpwd
- char array representing the password string- Throws:
IOException
- if an exception occurs while reading the PDF document
-
getVisualDifferences
protected List<PdfModification> getVisualDifferences(PdfDocumentReader signedRevisionReader, PdfDocumentReader finalRevisionReader) Returns 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
-
checkPdfPermissions
protected void checkPdfPermissions(PdfDocumentReader documentReader, SignatureFieldParameters fieldParameters) This method verifies the PDF permissions dictionaries- Parameters:
documentReader
-PdfDocumentReader
document to be checkedfieldParameters
-SignatureFieldParameters
identifying a new signature field configuration
-