From cec0065b747d30c6a0a17d18f2c7c8962a9102ed Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 17 Dec 2013 11:38:33 +0100 Subject: Pdf AS validation --- .../exceptions/PdfAsValidationException.java | 23 ++++++++ .../at/gv/egiz/pdfas/common/settings/Settings.java | 6 +- .../resources/resources/messages/common.properties | 1 + .../java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java | 64 +++++++++++++++++----- 4 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsValidationException.java diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsValidationException.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsValidationException.java new file mode 100644 index 00000000..61bfffda --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsValidationException.java @@ -0,0 +1,23 @@ +package at.gv.egiz.pdfas.common.exceptions; + +import at.gv.egiz.pdfas.common.messages.MessageResolver; + +public class PdfAsValidationException extends PdfAsException { + + /** + * + */ + private static final long serialVersionUID = -2428540014894153122L; + + private String parameter; + + public PdfAsValidationException(String msgId, String parameter) { + super(msgId); + this.parameter = parameter; + } + + @Override + protected String localizeMessage(String msgId) { + return String.format(MessageResolver.resolveMessage(msgId), parameter); + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java index f3e17ab8..255c8ba8 100644 --- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java @@ -42,7 +42,11 @@ public class Settings implements ISettings, IProfileConstants{ while(includeIterator.hasNext()) { String includeFile = configDir + File.separator + includeIterator.next(); logger.debug("Loading included cfg file: " + includeFile); - properties.load(new FileInputStream(includeFile)); + try { + properties.load(new FileInputStream(includeFile)); + } catch(Throwable e) { + logger.error("Failed to load cfg file " + includeFile, e); + } } } diff --git a/pdf-as-common/src/main/resources/resources/messages/common.properties b/pdf-as-common/src/main/resources/resources/messages/common.properties index dadef742..ef3b4949 100644 --- a/pdf-as-common/src/main/resources/resources/messages/common.properties +++ b/pdf-as-common/src/main/resources/resources/messages/common.properties @@ -33,6 +33,7 @@ error.pdf.sig.05=Failed to continue signature process error.pdf.sig.06=Failed to finish signature process error.pdf.sig.07=Failed to determine Signature Profile error.pdf.sig.08=Signature created by the BKU is not valid +error.pdf.sig.09=Signature profile %s not available #Signature verification errors diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java index 48d15b4d..e9332bc2 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java @@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PDFIOException; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsException; +import at.gv.egiz.pdfas.common.exceptions.PdfAsValidationException; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.common.settings.Settings; import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings; @@ -69,11 +70,40 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { this.settings = new Settings(cfgFile); } + private void verifySignParameter(SignParameter parameter) + throws PdfAsException { + // Status initialization + if (!(parameter.getConfiguration() instanceof ISettings)) { + throw new PdfAsSettingsException("Invalid settings object!"); + } + + ISettings settings = (ISettings) parameter.getConfiguration(); + + String signatureProfile = parameter.getSignatureProfileId(); + if(signatureProfile != null) { + if(!settings.hasPrefix("sig_obj." + signatureProfile + ".key")) { + throw new PdfAsValidationException("error.pdf.sig.09", signatureProfile); + } + } + + // TODO: verify Sign Parameter + } + + private void verifyVerifyParameter(VerifyParameter parameter) + throws PdfAsException { + // Status initialization + if (!(parameter.getConfiguration() instanceof ISettings)) { + throw new PdfAsSettingsException("Invalid settings object!"); + } + + // TODO: verify Verify Parameter + } + public SignResult sign(SignParameter parameter) throws PdfAsException { logger.trace("sign started"); - // TODO: verify signParameter + verifySignParameter(parameter); try { // Status initialization @@ -130,7 +160,8 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { return result; } catch (Throwable e) { - logger.error("Failed to create signature [" + e.getMessage() + "]", e); + logger.error("Failed to create signature [" + e.getMessage() + "]", + e); throw new PdfAsException("error.pdf.sig.01", e); } finally { logger.trace("sign done"); @@ -139,6 +170,9 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { public List verify(VerifyParameter parameter) throws PdfAsException { + + verifyVerifyParameter(parameter); + PDDocument doc = null; try { List result = new ArrayList(); @@ -212,7 +246,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { logger.error("Failed to verify document", e); throw new PdfAsException("error.pdf.verify.02", e); } finally { - if(doc != null) { + if (doc != null) { try { doc.close(); } catch (IOException e) { @@ -228,7 +262,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { public StatusRequest startSign(SignParameter parameter) throws PdfAsException { - + // TODO: VERIFY PARAMETERS StatusRequestImpl request = new StatusRequestImpl(); @@ -245,7 +279,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { status); status.setRequestedSignature(requestedSignature); - + request.setStatus(status); request.setNeedCertificate(true); @@ -388,22 +422,23 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { .createDefaultStamper(settings); IPDFVisualObject visualObject = stamper.createVisualPDFObject( status.getPdfObject(), main); - + PDDocument originalDocument = PDDocument .load(new ByteArrayInputStream(status.getPdfObject() .getOriginalDocument())); PositioningInstruction positioningInstruction = Positioning - .determineTablePositioning(tablePos, "", originalDocument, - visualObject, false); + .determineTablePositioning(tablePos, "", + originalDocument, visualObject, false); // ================================================================ // StampingStage (visual) -> stamp logical signature block to // location (itext) - byte[] incrementalUpdate = stamper.writeVisualObject(visualObject, - positioningInstruction, status.getPdfObject() - .getOriginalDocument(), signaturePlaceholderData.getPlaceholderName()); + byte[] incrementalUpdate = stamper.writeVisualObject( + visualObject, positioningInstruction, status + .getPdfObject().getOriginalDocument(), + signaturePlaceholderData.getPlaceholderName()); SignaturePositionImpl position = new SignaturePositionImpl(); position.setX(positioningInstruction.getX()); @@ -461,9 +496,10 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { } logger.debug("using Positioning: " + posString); - - boolean legacy32Position = signatureProfileConfiguration.getLegacy32Positioning(); - + + boolean legacy32Position = signatureProfileConfiguration + .getLegacy32Positioning(); + TablePos tablePos = null; if (posString == null) { -- cgit v1.2.3