From 05bb51dd0190c56f7ec59f6f0c759e00f1d72edc Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Wed, 11 Dec 2013 12:04:19 +0100 Subject: Signature profile implementations --- .../pdfas/lib/api/IConfigurationConstants.java | 4 + .../at/gv/egiz/pdfas/lib/api/PdfAsFactory.java | 117 ++++++++++++++++++--- .../java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java | 6 +- .../SignatureProfileConfiguration.java | 11 ++ .../pdfas/lib/impl/positioning/Positioning.java | 33 +----- .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 13 ++- .../impl/signing/pdfbox/PdfboxSignerWrapper.java | 4 +- 7 files changed, 134 insertions(+), 54 deletions(-) (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas') diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/IConfigurationConstants.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/IConfigurationConstants.java index 59515937..21f8c81e 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/IConfigurationConstants.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/IConfigurationConstants.java @@ -3,6 +3,7 @@ package at.gv.egiz.pdfas.lib.api; public interface IConfigurationConstants { public static final String TRUE = "true"; + public static final String FALSE = "false"; public static final String SIG_OBJECT = "sig_obj"; public static final String TYPE = "type"; @@ -11,6 +12,9 @@ public interface IConfigurationConstants { public static final String POS = "pos"; public static final String DEFAULT = "default"; public static final String SEPERATOR = "."; + + + public static final String LEGACY_POSITIONING = ".legacy.pos"; public static final String PLACEHOLDER_SEARCH_ENABLED = "enable_placeholder_search"; public static final String DEFAULT_SIGNATURE_PROFILE = SIG_OBJECT + SEPERATOR + TYPE + SEPERATOR + DEFAULT; diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java index b303a139..963e4098 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java @@ -1,12 +1,19 @@ package at.gv.egiz.pdfas.lib.api; -import iaik.security.ecc.provider.ECCProvider; +import iaik.security.ec.provider.ECCelerate; import iaik.security.provider.IAIK; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; -import org.apache.log4j.BasicConfigurator; import org.apache.log4j.PropertyConfigurator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; @@ -15,26 +22,108 @@ import at.gv.egiz.pdfas.lib.impl.SignParameterImpl; import at.gv.egiz.pdfas.lib.impl.VerifyParameterImpl; public class PdfAsFactory { - + + private static final Logger logger = LoggerFactory + .getLogger(PdfAsFactory.class); + + private static final String DEFAULT_CONFIG_RES = "config/config.zip"; + static { - PropertyConfigurator.configure(ClassLoader.getSystemResourceAsStream("resources/log4j.properties")); - //BasicConfigurator.configure(); - - IAIK.getInstance(); - ECCProvider.addAsProvider(); + PropertyConfigurator.configure(ClassLoader + .getSystemResourceAsStream("resources/log4j.properties")); + // BasicConfigurator.configure(); + + //iaik.security.ecc.provider.ECCProvider.addAsProvider(); + IAIK.addAsProvider();; + //ECCProvider.addAsProvider(); + // install security provider + ECCelerate.addAsProvider(); } - + public static PdfAs createPdfAs(File configuration) { return new PdfAsImpl(configuration); } - - public static SignParameter createSignParameter(Configuration configuration, DataSource dataSource) { + + public static SignParameter createSignParameter( + Configuration configuration, DataSource dataSource) { SignParameter param = new SignParameterImpl(configuration, dataSource); return param; } - - public static VerifyParameter createVerifyParameter(Configuration configuration, DataSource dataSource) { - VerifyParameter param = new VerifyParameterImpl(configuration, dataSource); + + public static VerifyParameter createVerifyParameter( + Configuration configuration, DataSource dataSource) { + VerifyParameter param = new VerifyParameterImpl(configuration, + dataSource); return param; } + + public static void deployDefaultConfiguration(File targetDirectory) + throws Exception { + if (targetDirectory.exists()) { + targetDirectory.delete(); + } + + if (!targetDirectory.exists()) { + targetDirectory.mkdir(); + } + InputStream is = ClassLoader + .getSystemResourceAsStream(DEFAULT_CONFIG_RES); + // URL zipUrl = ClassLoader.getSystemResource(DEFAULT_CONFIG_RES); + // logger.debug("CONFIG URI: " + zipUrl.toURI().toString()); + // File zipFile = new File(zipUrl.toURI()); + // ZipFile zip = null; + ZipInputStream zip = null; + try { + zip = new ZipInputStream(is); + + ZipEntry entry = zip.getNextEntry(); + while (entry != null) { + + File destinationPath = new File( + targetDirectory.getAbsolutePath(), entry.getName()); + + // create parent directories + destinationPath.getParentFile().mkdirs(); + + // if the entry is a file extract it + if (entry.isDirectory()) { + destinationPath.mkdir(); + zip.closeEntry(); + entry = zip.getNextEntry(); + continue; + } else { + + logger.debug("Extracting file: " + destinationPath); + + int b; + byte buffer[] = new byte[1024]; + + FileOutputStream fos = new FileOutputStream(destinationPath); + + BufferedOutputStream bos = new BufferedOutputStream(fos, + 1024); + + while ((b = zip.read(buffer, 0, 1024)) != -1) { + bos.write(buffer, 0, b); + } + + bos.close(); + zip.closeEntry(); + + } + entry = zip.getNextEntry(); + } + + } catch (IOException ioe) { + System.out.println("Error opening zip file" + ioe); + } finally { + try { + if (zip != null) { + zip.close(); + } + } catch (IOException ioe) { + System.out.println("Error while closing zip file" + ioe); + } + } + } } 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 ddfc79ce..fc245592 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 @@ -459,7 +459,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { PositioningInstruction positioningInstruction = Positioning .determineTablePositioning(tablePos, "", originalDocument, - visualObject); + visualObject, false); // ================================================================ // StampingStage (visual) -> stamp logical signature block to @@ -524,6 +524,8 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { .getDefaultPositioning(); } + boolean legacy32Position = signatureProfileConfiguration.getLegacy32Positioning(); + TablePos tablePos = null; if (posString == null) { @@ -538,7 +540,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { PositioningInstruction positioningInstruction = Positioning .determineTablePositioning(tablePos, "", originalDocument, - visualObject); + visualObject, legacy32Position); // ================================================================ // StampingStage (visual) -> stamp logical signature block to diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/SignatureProfileConfiguration.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/SignatureProfileConfiguration.java index 8f09b495..b8a0139a 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/SignatureProfileConfiguration.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/SignatureProfileConfiguration.java @@ -23,4 +23,15 @@ public class SignatureProfileConfiguration extends SpecificBaseConfiguration String key = SIG_OBJECT + SEPERATOR + profileID + SEPERATOR + TABLE + SEPERATOR + POS; return this.configuration.getValue(key); } + + public boolean getLegacy32Positioning() { + String key = SIG_OBJECT + SEPERATOR + profileID + LEGACY_POSITIONING; + String value = this.configuration.getValue(key); + if(value != null) { + if(value.equalsIgnoreCase(TRUE)) { + return true; + } + } + return false; + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/positioning/Positioning.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/positioning/Positioning.java index 340ba0bb..da7da772 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/positioning/Positioning.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/positioning/Positioning.java @@ -47,39 +47,8 @@ public class Positioning { * F.e. */ public static PositioningInstruction determineTablePositioning(TablePos pos, String signature_type, - PDDocument pdfDataSource, IPDFVisualObject pdf_table) throws PdfAsException + PDDocument pdfDataSource, IPDFVisualObject pdf_table, boolean legacy32) throws PdfAsException { - boolean legacy32 = false; - - //TODO: settings reader ... - - /* - if (pos == null) - { - String pos_string = SettingsReader.getInstance().getSetting(SignatureTypes.SIG_OBJ + signature_type + ".pos", null); - if (pos_string != null) - { - pos = PdfAS.parsePositionFromPosString(pos_string); - } - } - if (pos == null) - { - // The default algorithm. x,y,w =auto ,p=lastpage, f:ignored because - // y:auto - pos = new TablePos(); - } - - // afitzek - // Allow legacy positioning (3.2) for BRZ Templates ... - boolean legacy32 = false; - String leg = SettingsReader.getInstance().getSetting(SignatureTypes.SIG_OBJ + signature_type + ".legacy.pos", "false"); - if (leg != null) { - if ("true".equals(leg.trim())) { - legacy32 = true; - } - } - */ - // System.out.println("Tablepos="+pos); return adjustSignatureTableandCalculatePosition(pdfDataSource, pdf_table, pos, legacy32); } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index 7273b8a9..cc75ff69 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java @@ -65,11 +65,16 @@ public class PADESPDFBOXSigner implements IPdfSigner { String signerName = resolver.resolve("SIG_SUBJECT", signatureProfileSettings.getValue("SIG_SUBJECT"), signatureProfileSettings, requestedSignature); - signature.setName(signerName); - //signature.setLocation("signer location"); - signature.setReason("PDF-AS Signatur"); - + 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 diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PdfboxSignerWrapper.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PdfboxSignerWrapper.java index 554a5b98..718acc24 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PdfboxSignerWrapper.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PdfboxSignerWrapper.java @@ -42,10 +42,10 @@ public class PdfboxSignerWrapper implements PDFASSignatureInterface { try { byte[] signature = signer.sign(data, byteRange); /*logger.debug("Signature Data: " - + iaik.utils.Util.toBase64String(signature)); + + iaik.utils.Util.toBase64String(signature));*/ FileOutputStream fos = new FileOutputStream("/tmp/fos.bin"); fos.write(signature); - fos.close();*/ + fos.close(); return signature; } catch (PdfAsException e) { throw new PdfAsWrappedIOException(e); -- cgit v1.2.3