/*******************************************************************************
* Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria
* PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a
* joint initiative of the Federal Chancellery Austria and Graz University of
* Technology.
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
* http://www.osor.eu/eupl/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* This product combines work with different licenses. See the "NOTICE" text
* file for details on the various modules and licenses.
* The "NOTICE" text file is part of the distribution. Any derivative works
* that you distribute must include a readable copy of the "NOTICE" text file.
******************************************************************************/
package at.gv.egiz.pdfas.lib.impl.signing.pdfbox2;
import at.gv.egiz.pdfas.common.exceptions.PDFASError;
import at.gv.egiz.pdfas.common.exceptions.PdfAsException;
import at.gv.egiz.pdfas.common.messages.MessageResolver;
import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings;
import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource;
import at.gv.egiz.pdfas.lib.api.IConfigurationConstants;
import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner;
import at.gv.egiz.pdfas.lib.api.sign.SignParameter;
import at.gv.egiz.pdfas.lib.impl.ErrorExtractor;
import at.gv.egiz.pdfas.lib.impl.SignaturePositionImpl;
import at.gv.egiz.pdfas.lib.impl.configuration.SignatureProfileConfiguration;
import at.gv.egiz.pdfas.lib.impl.pdfbox2.PDFBOXObject;
import at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignaturePlaceholderExtractor;
import at.gv.egiz.pdfas.lib.impl.pdfbox2.positioning.Positioning;
import at.gv.egiz.pdfas.lib.impl.pdfbox2.utils.PdfBoxUtils;
import at.gv.egiz.pdfas.lib.impl.placeholder.PlaceholderFilter;
import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData;
import at.gv.egiz.pdfas.lib.impl.signing.IPdfSigner;
import at.gv.egiz.pdfas.lib.impl.signing.PDFASSignatureExtractor;
import at.gv.egiz.pdfas.lib.impl.signing.PDFASSignatureInterface;
import at.gv.egiz.pdfas.lib.impl.stamping.IPDFStamper;
import at.gv.egiz.pdfas.lib.impl.stamping.IPDFVisualObject;
import at.gv.egiz.pdfas.lib.impl.stamping.TableFactory;
import at.gv.egiz.pdfas.lib.impl.stamping.ValueResolver;
import at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureProperties;
import at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PdfBoxVisualObject;
import at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.StamperFactory;
import at.gv.egiz.pdfas.lib.impl.status.OperationStatus;
import at.gv.egiz.pdfas.lib.impl.status.PDFObject;
import at.gv.egiz.pdfas.lib.impl.status.RequestedSignature;
import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction;
import at.knowcenter.wag.egov.egiz.pdf.TablePos;
import at.knowcenter.wag.egov.egiz.table.Table;
import iaik.x509.X509Certificate;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.cos.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.pdfbox.pdmodel.common.PDNumberTreeNode;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement;
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot;
import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureOptions;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField;
import org.apache.pdfbox.preflight.PreflightDocument;
import org.apache.pdfbox.preflight.ValidationResult;
import org.apache.pdfbox.preflight.exception.SyntaxValidationException;
import org.apache.pdfbox.preflight.exception.ValidationException;
import org.apache.pdfbox.preflight.parser.PreflightParser;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.schema.PDFAIdentificationSchema;
import org.apache.xmpbox.xml.DomXmpParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.activation.DataSource;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants {
private static final Logger logger = LoggerFactory.getLogger(PADESPDFBOXSigner.class);
private boolean isAdobeSigForm = false;
public void signPDF(PDFObject genericPdfObject, RequestedSignature requestedSignature,
PDFASSignatureInterface genericSigner) throws PdfAsException {
PDFAsVisualSignatureProperties properties = null;
String placeholder_id = "";
if (!(genericPdfObject instanceof PDFBOXObject)) {
// tODO:
throw new PdfAsException();
}
PDFBOXObject pdfObject = (PDFBOXObject) genericPdfObject;
if (!(genericSigner instanceof PDFASPDFBOXSignatureInterface)) {
// tODO:
throw new PdfAsException();
}
PDFASPDFBOXSignatureInterface signer = (PDFASPDFBOXSignatureInterface) genericSigner;
String pdfaVersion = null;
PDDocument doc = null;
SignatureOptions options = new SignatureOptions();
COSDocument visualSignatureDocumentGuard = null;
try {
doc = pdfObject.getDocument();
//if signature already exists dont create new page
List pdSignatureFieldList = doc.getSignatureFields();
PDSignature signature;
// sign a PDF with an existing empty signature, as created by the CreateEmptySignatureForm example.
String sigFieldName = pdfObject.getStatus().getSettings().getValue(SIGNATURE_FIELD_NAME);
signature = findExistingSignature(doc, sigFieldName);
if (signature == null) {
// create signature dictionary
signature = new PDSignature();
}
else {
isAdobeSigForm = true;
}
signature.setFilter(COSName.getPDFName(signer.getPDFFilter()));
signature.setSubFilter(COSName.getPDFName(signer.getPDFSubFilter()));
SignaturePlaceholderData signaturePlaceholderDataInit = PlaceholderFilter.checkPlaceholderSignatureLocation(pdfObject.getStatus(), pdfObject.getStatus().getSettings(), placeholder_id);
//gives a list of all placeholders
List placeholders = SignaturePlaceholderExtractor.listPlaceholders();
if(checkAvailablePlaceholders(placeholders,existingSignatureLocations(doc))!=null)
{
placeholder_id = (checkAvailablePlaceholders(placeholders, existingSignatureLocations(doc))).getId();
};
SignaturePlaceholderData signaturePlaceholderData = PlaceholderFilter
.checkPlaceholderSignatureLocation(pdfObject.getStatus(), pdfObject.getStatus().getSettings(),placeholder_id);
TablePos tablePos = null;
signature.setLocation(signaturePlaceholderData.getPlaceholderName());
if (signaturePlaceholderData != null) {
// Placeholder found!
logger.info("Placeholder data found.");
if (signaturePlaceholderData.getProfile() != null) {
logger.debug("Placeholder Profile set to: " + signaturePlaceholderData.getProfile());
requestedSignature.setSignatureProfileID(signaturePlaceholderData.getProfile());
}
tablePos = signaturePlaceholderData.getTablePos();
if (tablePos != null) {
SignatureProfileConfiguration signatureProfileConfiguration = pdfObject.getStatus()
.getSignatureProfileConfiguration(requestedSignature.getSignatureProfileID());
float minWidth = signatureProfileConfiguration.getMinWidth();
if(minWidth > 0) {
if (tablePos.getWidth() < minWidth) {
tablePos.width = minWidth;
logger.debug("Correcting placeholder with to minimum width {}", minWidth);
}
}
logger.debug("Placeholder Position set to: " + tablePos.toString());
}
}
SignatureProfileSettings signatureProfileSettings = TableFactory
.createProfile(requestedSignature.getSignatureProfileID(), pdfObject.getStatus().getSettings());
//Check if input document is PDF-A conform
if (signatureProfileSettings.isPDFA()) {
DataSource origDoc = pdfObject.getOriginalDocument();
InputStream stream = origDoc.getInputStream();
//Run PreflightParser for checking conformity//
//runPDFAPreflight(origDoc);
}
ValueResolver resolver = new ValueResolver(requestedSignature, pdfObject.getStatus());
String signerName = resolver.resolve("SIG_SUBJECT", signatureProfileSettings.getValue("SIG_SUBJECT"),
signatureProfileSettings);
signature.setName(signerName);
signature.setSignDate(Calendar.getInstance());
String signerReason = signatureProfileSettings.getSigningReason();
if (signerReason == null) {
signerReason = "PAdES Signature";
}
signature.setReason(signerReason);
logger.debug("Signing reason: " + signerReason);
logger.debug("Signing @ " + signer.getSigningDate().getTime().toString());
// the signing date, needed for valid signature
// signature.setSignDate(signer.getSigningDate());
signer.setPDSignature(signature);
int signatureSize = 0x1000;
try {
String reservedSignatureSizeString = signatureProfileSettings.getValue(SIG_RESERVED_SIZE);
if (reservedSignatureSizeString != null) {
signatureSize = Integer.parseInt(reservedSignatureSizeString);
}
logger.debug("Reserving {} bytes for signature", signatureSize);
} catch (NumberFormatException e) {
logger.warn("Invalid configuration value: {} should be a number using 0x1000", SIG_RESERVED_SIZE);
}
options.setPreferredSignatureSize(signatureSize);
if(signatureProfileSettings.isPDFA() || signatureProfileSettings.isPDFA3()) {
pdfaVersion = getPDFAVersion(doc);
signatureProfileSettings.setPDFAVersion(pdfaVersion);
}
// Is visible Signature
if (requestedSignature.isVisual()) {
logger.info("Creating visual siganture block");
SignatureProfileConfiguration signatureProfileConfiguration = pdfObject.getStatus()
.getSignatureProfileConfiguration(requestedSignature.getSignatureProfileID());
if (tablePos == null) {
// ================================================================
// PositioningStage (visual) -> find position or use
// fixed
// position
String posString = pdfObject.getStatus().getSignParamter().getSignaturePosition();
TablePos signaturePos = null;
String signaturePosString = signatureProfileConfiguration.getDefaultPositioning();
if (signaturePosString != null) {
logger.debug("using signature Positioning: " + signaturePos);
signaturePos = new TablePos(signaturePosString);
}
logger.debug("using Positioning: " + posString);
if (posString != null) {
// Merge Signature Position
tablePos = new TablePos(posString, signaturePos);
} else {
// Fallback to signature Position!
tablePos = signaturePos;
}
if (tablePos == null) {
// Last Fallback default position
tablePos = new TablePos();
}
}
//Legacy Modes not supported with pdfbox2 anymore
// boolean legacy32Position = signatureProfileConfiguration.getLegacy32Positioning();
// boolean legacy40Position = signatureProfileConfiguration.getLegacy40Positioning();
// create Table describtion
Table main = TableFactory.createSigTable(signatureProfileSettings, MAIN, pdfObject.getStatus(),
requestedSignature);
IPDFStamper stamper = StamperFactory.createDefaultStamper(pdfObject.getStatus().getSettings());
IPDFVisualObject visualObject = stamper.createVisualPDFObject(pdfObject, main);
/*
* PDDocument originalDocument = PDDocument .load(new
* ByteArrayInputStream(pdfObject.getStatus()
* .getPdfObject().getOriginalDocument()));
*/
PositioningInstruction positioningInstruction = Positioning.determineTablePositioning(tablePos, "",
doc, visualObject, pdfObject.getStatus().getSettings());
logger.debug("Positioning: {}", positioningInstruction.toString());
if(!isAdobeSigForm) {
if (positioningInstruction.isMakeNewPage()) {
int last = doc.getNumberOfPages() - 1;
PDDocumentCatalog root = doc.getDocumentCatalog();
PDPage lastPage = root.getPages().get(last);
root.getPages().getCOSObject().setNeedToBeUpdated(true);
PDPage p = new PDPage(lastPage.getMediaBox());
p.setResources(new PDResources());
p.setRotation(lastPage.getRotation());
doc.addPage(p);
}
// handle rotated page
int targetPageNumber = positioningInstruction.getPage();
logger.debug("Target Page: " + targetPageNumber);
PDPage targetPage = doc.getPages().get(targetPageNumber - 1);
int rot = targetPage.getRotation();
logger.debug("Page rotation: " + rot);
// positioningInstruction.setRotation(positioningInstruction.getRotation()
// // + rot);
logger.debug("resulting Sign rotation: " + positioningInstruction.getRotation());
SignaturePositionImpl position = new SignaturePositionImpl();
position.setX(positioningInstruction.getX());
position.setY(positioningInstruction.getY());
position.setPage(positioningInstruction.getPage());
position.setHeight(visualObject.getHeight());
position.setWidth(visualObject.getWidth());
requestedSignature.setSignaturePosition(position);
}
properties = new PDFAsVisualSignatureProperties(pdfObject.getStatus().getSettings(), pdfObject,
(PdfBoxVisualObject) visualObject, positioningInstruction, signatureProfileSettings);
properties.buildSignature();
/*
* ByteArrayOutputStream sigbos = new
* ByteArrayOutputStream();
* sigbos.write(StreamUtils.inputStreamToByteArray
* (properties .getVisibleSignature())); sigbos.close();
*/
/*if (signaturePlaceholderData != null) {
InputStream fis = PADESPDFBOXSigner.class.getResourceAsStream("/placeholder/empty.jpg");
PDImageXObject img = JPEGFactory.createFromStream(doc, fis);
img.getCOSObject().setNeedToBeUpdated(true);
// PDDocumentCatalog root = doc.getDocumentCatalog();
// PDPageNode rootPages = root.getPages();
// List kids = new ArrayList();
// rootPages.getAllKids(kids);
int pageNumber = positioningInstruction.getPage();
PDPage page = doc.getPages().get(pageNumber - 1);
logger.info("Placeholder name: " + signaturePlaceholderData.getPlaceholderName());
COSDictionary xobjectsDictionary = (COSDictionary) page.getResources().getCOSObject()
.getDictionaryObject(COSName.XOBJECT);
xobjectsDictionary.setItem(signaturePlaceholderData.getPlaceholderName(), img);
xobjectsDictionary.setNeedToBeUpdated(true);
page.getResources().getCOSObject().setNeedToBeUpdated(true);
logger.info("Placeholder name: " + signaturePlaceholderData.getPlaceholderName());
}*/
if (signatureProfileSettings.isPDFA() || signatureProfileSettings.isPDFA3()) {
PDDocumentCatalog root = doc.getDocumentCatalog();
COSBase base = root.getCOSObject().getItem(COSName.OUTPUT_INTENTS);
InputStream colorProfile = null;
//colorProfile = this.getClass().getResourceAsStream("/icm/sRGB.icm");
colorProfile = this.getClass().getResourceAsStream("/icm/sRGB Color Space Profile.icm");
//Set output intents for PDF/A conformity//
try {
PDOutputIntent intent = new PDOutputIntent(doc, colorProfile);
intent.setInfo("sRGB IEC61966-2.1");
intent.setOutputCondition("sRGB IEC61966-2.1");
intent.setOutputConditionIdentifier("sRGB IEC61966-2.1");
intent.setRegistryName("http://www.color.org");
List oi = new ArrayList();
oi.add(intent);
root.setOutputIntents(oi);
root.getCOSObject().setNeedToBeUpdated(true);
logger.info("added Output Intent");
} catch (Throwable e) {
e.printStackTrace();
throw new PdfAsException("Failed to add Output Intent", e);
}
finally {
IOUtils.closeQuietly(colorProfile);
}
}
options.setPage(positioningInstruction.getPage()-1);
options.setVisualSignature(properties.getVisibleSignature());
}
visualSignatureDocumentGuard = options.getVisualSignature();
doc.addSignature(signature, signer, options);
if (sigFieldName == null) {
sigFieldName = "PDF-AS Signatur";
}
int count = PdfBoxUtils.countSignatures(doc, sigFieldName);
sigFieldName = sigFieldName + count;
PDAcroForm acroFormm = doc.getDocumentCatalog().getAcroForm();
// PDStructureTreeRoot pdstRoot =
// doc.getDocumentCatalog().getStructureTreeRoot();
// COSDictionary dic =
// doc.getDocumentCatalog().getCOSDictionary();
// PDStructureElement el = new PDStructureElement("Widget",
// pdstRoot);
//this is not used for Adobe signature fields
if(!isAdobeSigForm) {
PDSignatureField signatureField = null;
if (acroFormm != null) {
@SuppressWarnings("unchecked")
List fields = acroFormm.getFields();
if (fields != null) {
for (PDField pdField : fields) {
if (pdField != null) {
if (pdField instanceof PDSignatureField) {
PDSignatureField tmpSigField = (PDSignatureField) pdField;
if (tmpSigField.getSignature() != null
&& tmpSigField.getSignature().getCOSObject() != null) {
if (tmpSigField.getSignature().getCOSObject()
.equals(signature.getCOSObject())) {
signatureField = (PDSignatureField) pdField;
}
}
}
}
}
} else {
logger.warn("Failed to name Signature Field! [Cannot find Field list in acroForm!]");
}
if (signatureField != null) {
signatureField.setPartialName(sigFieldName);
}
if (properties != null) {
signatureField.setAlternateFieldName(properties.getAlternativeTableCaption());
} else {
signatureField.setAlternateFieldName(sigFieldName);
}
} else {
logger.warn("Failed to name Signature Field! [Cannot find acroForm!]");
}
}
PDSignatureField signatureField = null;
PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
if (acroForm != null) {
signatureField = (PDSignatureField) acroForm.getField(sigFieldName);
}
// PDF-UA
logger.info("Adding pdf/ua content.");
try {
PDDocumentCatalog root = doc.getDocumentCatalog();
PDStructureTreeRoot structureTreeRoot = root.getStructureTreeRoot();
if (structureTreeRoot != null) {
logger.info("Tree Root: {}", structureTreeRoot.toString());
List