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
FieldsModifier and TypeFieldDescriptionprotected PdfDifferencesFinderUsed to find differences occurred between PDF revisions (e.g.protected PdfObjectModificationsFinderUsed to find differences within internal PDF objects occurred between PDF revisions .protected DSSResourcesHandlerBuilderThe builder to be used to create a newDSSResourcesHandlerfor each internal call, defining a way working with internal resources (e.g. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractPDFSignatureService(PDFServiceMode serviceMode, SignatureDrawerFactory signatureDrawerFactory) Constructor for the PDFSignatureService -
Method Summary
Modifier and TypeMethodDescriptionaddDssDictionary(DSSDocument document, PdfValidationDataContainer validationDataForInclusion) This method adds the DSS dictionary (Baseline-LT)addNewSignatureField(DSSDocument document, SignatureFieldParameters parameters) This method allows to add a new signature field to an existing pdf documentvoidanalyzePdfModifications(DSSDocument document, List<AdvancedSignature> signatures, String pwd) Analyze the PDF revision and try to detect any modification (shadow attacks)protected AnnotationBoxbuildSignatureFieldBox(SignatureDrawer signatureDrawer) Returns a SignatureFieldBox.protected voidcheckDocumentPermissions(PdfDocumentReader pdfDocumentReader) This method checks if the document is not encrypted or with limited edition rightsprotected voidcheckNewSignatureIsPermitted(PdfDocumentReader reader, SignatureFieldParameters fieldParameters) This method verifies whether a new signature is permittedprotected voidcheckSignatureFieldAgainstPageDimensions(AnnotationBox signatureFieldBox, AnnotationBox pageBox, int pageRotation) This method verifies whether thesignatureFieldBoxis withinpageBoxprotected voidcheckSignatureFieldBoxOverlap(AnnotationBox signatureFieldBox, List<PdfAnnotation> pdfAnnotations) This method verifies whether thesignatureFieldBoxoverlaps with one of the extractedpdfAnnotationsprotected abstract byte[]computeDigest(DSSDocument toSignDocument, PAdESCommonParameters parameters) Computes digest on to be signed data computed on thetoSignDocumentrespectively to the givenparametersbyte[]digest(DSSDocument toSignDocument, PAdESCommonParameters parameters) Returns the digest value of a PDF document.protected byte[]extractBeforeSignatureValue(ByteRange byteRange, byte[] signedContent) Extract the content before the signature valuegetAvailableSignatureFields(DSSDocument document) This method returns not signed signature-fieldsgetRevisions(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.protected StringgetType()Gets the type of the signature dictionaryprotected AnnotationBoxgetVisibleSignatureFieldBoxPosition(PdfDocumentReader reader, SignatureFieldParameters parameters) Checks if the signatureFieldBox overlaps with any existing annotations on the given page and returns the respectful signature field boxprotected AnnotationBoxgetVisibleSignatureFieldBoxPosition(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 DSSResourcesHandlerThis method instantiates a newDSSResourcesFactoryprotected 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 booleanChecks 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 providedsignatureDrawerFactoryvoidsetAlertOnForbiddenSignatureCreation(StatusAlert alertOnForbiddenSignatureCreation) Sets a behavior to follow when creating a new signature in a document that forbids creation of new signatures Default : ExceptionOnStatusAlert - throw the exceptionvoidsetAlertOnSignatureFieldOutsidePageDimensions(StatusAlert alertOnSignatureFieldOutsidePageDimensions) Sets a behavior to follow when a new signature field is created outside the page's dimensions Default : ExceptionOnStatusAlert - throw the exceptionvoidsetAlertOnSignatureFieldOverlap(StatusAlert alertOnSignatureFieldOverlap) Sets alert on a signature field overlap with existing fields or/and annotations Default : ExceptionOnStatusAlert - throw the exceptionvoidsetMaximalPagesAmountForVisualComparison(int pagesAmount) Deprecated.since 5.11.voidsetPdfDifferencesFinder(PdfDifferencesFinder pdfDifferencesFinder) Sets thePdfDifferencesFinderused to find the differences on pages between given PDF revisions.voidsetPdfObjectModificationsFinder(PdfObjectModificationsFinder pdfObjectModificationsFinder) Sets thePdfObjectModificationsFinderused to find the differences between internal PDF objects occurred between given PDF revisions.voidsetResourcesHandlerBuilder(DSSResourcesHandlerBuilder resourcesHandlerBuilder) SetsDSSResourcesFactoryBuilderto be used for aDSSResourcesHandlercreation in internal methods.sign(DSSDocument toSignDocument, byte[] cmsSignedData, PAdESCommonParameters parameters) Signs a PDF documentprotected abstract DSSDocumentsignDocument(DSSDocument toSignDocument, byte[] cmsSignedData, PAdESCommonParameters parameters) This method creates a signed document from the originaltoSignDocument, incorporating a new revision, enveloping the providedcmsSignedDataprotected AnnotationBoxtoPdfPageCoordinates(AnnotationBox fieldAnnotationBox, AnnotationBox pageBox, int pageRotation) This method transforms afieldAnnotationBox's positions and dimensions according to the given pageMethods 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, getAvailableSignatureFields, previewPageWithVisualSignature, previewSignatureField
-
Field Details
-
resourcesHandlerBuilder
The builder to be used to create a newDSSResourcesHandlerfor 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
-
-
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:PDFSignatureServiceSetsDSSResourcesFactoryBuilderto be used for aDSSResourcesHandlercreation in internal methods.DSSResourcesHandlerdefines a way to operate with OutputStreams and createDSSDocuments. Default :eu.europa.esig.dss.signature.resources.InMemoryResourcesHandler. Works with data in memory.- Specified by:
setResourcesHandlerBuilderin interfacePDFSignatureService- Parameters:
resourcesHandlerBuilder-DSSResourcesHandlerBuilder
-
setPdfDifferencesFinder
Description copied from interface:PDFSignatureServiceSets thePdfDifferencesFinderused to find the differences on pages between given PDF revisions. Default :eu.europa.esig.dss.pdf.modifications.DefaultPdfDifferencesFinder- Specified by:
setPdfDifferencesFinderin interfacePDFSignatureService- Parameters:
pdfDifferencesFinder-PdfDifferencesFinder
-
setPdfObjectModificationsFinder
public void setPdfObjectModificationsFinder(PdfObjectModificationsFinder pdfObjectModificationsFinder) Description copied from interface:PDFSignatureServiceSets thePdfObjectModificationsFinderused to find the differences between internal PDF objects occurred between given PDF revisions. Default :eu.europa.esig.dss.pdf.modifications.DefaultPdfObjectModificationsFinder- Specified by:
setPdfObjectModificationsFinderin interfacePDFSignatureService- Parameters:
pdfObjectModificationsFinder-PdfObjectModificationsFinder
-
setAlertOnSignatureFieldOverlap
Sets alert on a signature field overlap with existing fields or/and annotations Default : ExceptionOnStatusAlert - throw the exception- Parameters:
alertOnSignatureFieldOverlap-StatusAlertto execute
-
setAlertOnSignatureFieldOutsidePageDimensions
public void setAlertOnSignatureFieldOutsidePageDimensions(StatusAlert alertOnSignatureFieldOutsidePageDimensions) Sets a behavior to follow when a new signature field is created outside the page's dimensions Default : ExceptionOnStatusAlert - throw the exception- Parameters:
alertOnSignatureFieldOutsidePageDimensions-StatusAlertto execute
-
setAlertOnForbiddenSignatureCreation
Sets a behavior to follow when creating a new signature in a document that forbids creation of new signatures Default : ExceptionOnStatusAlert - throw the exception- Parameters:
alertOnForbiddenSignatureCreation-StatusAlertto execute
-
setMaximalPagesAmountForVisualComparison
Deprecated.since 5.11. UsePDFDocumentValidator validator = new PDFDocumentValidator(signedDocument); ... IPdfObjFactory pdfObjFactory = new ServiceLoaderPdfObjFactory(); DefaultPdfDifferencesFinder pdfDifferencesFinder = new DefaultPdfDifferencesFinder(); pdfDifferencesFinder.setMaximalPagesAmountForVisualComparison(0); pdfObjFactory.setPdfDifferencesFinder(pdfDifferencesFinder); validator.setPdfObjFactory(pdfObjFactory);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 fewer 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
-
instantiateResourcesHandler
This method instantiates a newDSSResourcesFactory- Returns:
DSSResourcesHandler- Throws:
IOException- if an error occurs on DSSResourcesHandler instantiation
-
digest
Description copied from interface:PDFSignatureServiceReturns the digest value of a PDF document.- Specified by:
digestin interfacePDFSignatureService- Parameters:
toSignDocument- the document to be signedparameters- the signature/timestamp parameters- Returns:
- the digest value
-
computeDigest
protected abstract byte[] computeDigest(DSSDocument toSignDocument, PAdESCommonParameters parameters) Computes digest on to be signed data computed on thetoSignDocumentrespectively to the givenparameters- Parameters:
toSignDocument-DSSDocumentto be signedparameters-PAdESCommonParameters- Returns:
- byte array
-
sign
public DSSDocument sign(DSSDocument toSignDocument, byte[] cmsSignedData, PAdESCommonParameters parameters) Description copied from interface:PDFSignatureServiceSigns a PDF document- Specified by:
signin 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-DSSDocumentto 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
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:
pdfDocumentReader-PdfDocumentReaderto check
-
checkNewSignatureIsPermitted
protected void checkNewSignatureIsPermitted(PdfDocumentReader reader, SignatureFieldParameters fieldParameters) This method verifies whether a new signature is permitted- Parameters:
reader-PdfDocumentReaderfieldParameters-SignatureFieldParameters
-
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
public DSSDocument addDssDictionary(DSSDocument document, PdfValidationDataContainer validationDataForInclusion) Description copied from interface:PDFSignatureServiceThis method adds the DSS dictionary (Baseline-LT)- Specified by:
addDssDictionaryin interfacePDFSignatureService- Parameters:
document- the document to be extendedvalidationDataForInclusion-PdfValidationDataContainer- 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, InvalidPasswordException LoadsPdfDocumentReaderinstance- 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, InvalidPasswordException LoadsPdfDocumentReaderinstance- 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 of signature fieldStringnames- Returns:
- TRUE if the content value equals the byte range extraction, FALSE otherwise
-
getSignatureValue
Gets 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
-
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-SignatureDrawerdocumentReader-PdfDocumentReaderfieldParameters-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-PdfDocumentReaderto be validatedparameters-SignatureFieldParameters- Returns:
AnnotationBoxcomputed signature field box- Throws:
IOException- if an exception occurs
-
toPdfPageCoordinates
protected AnnotationBox toPdfPageCoordinates(AnnotationBox fieldAnnotationBox, AnnotationBox pageBox, int pageRotation) This method transforms afieldAnnotationBox's positions and dimensions according to the given page- Parameters:
fieldAnnotationBox-AnnotationBoxcomputed field of a signaturepageBox-AnnotationBoxpage's boxpageRotation- defines the page's rotation- Returns:
AnnotationBox
-
checkSignatureFieldBoxOverlap
protected void checkSignatureFieldBoxOverlap(AnnotationBox signatureFieldBox, List<PdfAnnotation> pdfAnnotations) This method verifies whether thesignatureFieldBoxoverlaps with one of the extractedpdfAnnotations- Parameters:
signatureFieldBox-AnnotationBoxto verifypdfAnnotations- a list ofAnnotationBoxto verify against
-
checkSignatureFieldAgainstPageDimensions
protected void checkSignatureFieldAgainstPageDimensions(AnnotationBox signatureFieldBox, AnnotationBox pageBox, int pageRotation) This method verifies whether thesignatureFieldBoxis withinpageBox- Parameters:
signatureFieldBox-AnnotationBoxto checkpageBox-AnnotationBoxrepresenting the page's boxpageRotation- defines the page's rotation
-
analyzePdfModifications
public void analyzePdfModifications(DSSDocument document, List<AdvancedSignature> signatures, String pwd) Description copied from interface:PDFSignatureServiceAnalyze the PDF revision and try to detect any modification (shadow attacks)- Specified by:
analyzePdfModificationsin interfacePDFSignatureService- Parameters:
document-DSSDocumentthe documentsignatures- the different signatures to analysepwd-Stringpassword protection
-
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-PdfDocumentReaderfor the signed revision contentfinalRevisionReader-PdfDocumentReaderfor the input PDF document- Returns:
- a list of
PdfModifications
-