From 2436aa273f00dafb465c2342ea8e7297898915d2 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 21 Jan 2014 09:57:16 +0100 Subject: Implementations for pdf-as-web and pdf-as-legacy --- .../at/gv/egiz/pdfas/lib/api/PdfAsFactory.java | 60 +++++++++-- .../egiz/pdfas/lib/api/verify/VerifyParameter.java | 6 ++ .../java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java | 110 ++++++++++++--------- .../egiz/pdfas/lib/impl/VerifyParameterImpl.java | 12 +++ .../egiz/pdfas/lib/impl/verify/IVerifyFilter.java | 3 +- 5 files changed, 137 insertions(+), 54 deletions(-) (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib') 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 b73cc9af..284573e0 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 @@ -10,6 +10,10 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URL; +import java.util.Enumeration; +import java.util.jar.Attributes; +import java.util.jar.Manifest; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -30,9 +34,20 @@ public class PdfAsFactory { private static final String DEFAULT_CONFIG_RES = "config/config.zip"; + private static final String MAN_ATTRIBUTE = "JARMANIFEST"; + private static final String PDF_AS_LIB = "PDF-AS-LIB"; + private static final String IMPL_VERSION = "Implementation-Version"; + + static { - /*PropertyConfigurator.configure(ClassLoader - .getSystemResourceAsStream("resources/log4j.properties"));*/ + System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + System.out.println("+ PDF-AS: " + getVersion()); + System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + + /* + * PropertyConfigurator.configure(ClassLoader + * .getSystemResourceAsStream("resources/log4j.properties")); + */ IAIK.addAsProvider(); ECCelerate.addAsProvider(); } @@ -53,10 +68,12 @@ public class PdfAsFactory { File log4j = new File(configuration.getAbsolutePath() + File.separator + "cfg" + File.separator + "log4j.properties"); - logger.info("Loading log4j configuration: " + log4j.getAbsolutePath()); + logger.info("Loading log4j configuration: " + + log4j.getAbsolutePath()); if (log4j.exists()) { try { - System.setProperty("pdf-as.work-dir", configuration.getAbsolutePath()); + System.setProperty("pdf-as.work-dir", + configuration.getAbsolutePath()); PropertyConfigurator.configure(new FileInputStream( log4j)); } catch (FileNotFoundException e) { @@ -87,9 +104,9 @@ public class PdfAsFactory { /** * Deploy default configuration to targetDirectory * - * The targetDirectory will be deleted and + * The targetDirectory will be deleted and * - * @param targetDirectory + * @param targetDirectory * @throws Exception */ public static void deployDefaultConfiguration(File targetDirectory) @@ -157,4 +174,35 @@ public class PdfAsFactory { } } } + + public static String getVersion() { + Package pack = PdfAsFactory.class.getPackage(); + return pack.getImplementationVersion(); + /* + try { + + + Enumeration resources = PdfAsFactory.class.getClassLoader() + .getResources("META-INF/MANIFEST.MF"); + while (resources.hasMoreElements()) { + Manifest manifest = new Manifest(resources.nextElement() + .openStream()); + Attributes attributes = manifest.getAttributes(MAN_ATTRIBUTE); + if (attributes != null) { + if(attributes.isEmpty()) { + String value = attributes.getValue(new Attributes.Name(MAN_ATTRIBUTE)); + if(value != null && value.equals(PDF_AS_LIB)) { + // Got my manifest + return manifest.getAttributes(IMPL_VERSION).getValue(IMPL_VERSION); + } + } + } + } + } catch (Throwable e) { + e.printStackTrace(); + logger.error("Failed to read Version!"); + return "0.0.0"; + } + return "0.0.0";*/ + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/verify/VerifyParameter.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/verify/VerifyParameter.java index d319e373..da777343 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/verify/VerifyParameter.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/verify/VerifyParameter.java @@ -1,5 +1,7 @@ package at.gv.egiz.pdfas.lib.api.verify; +import java.util.Date; + import at.gv.egiz.pdfas.lib.api.PdfAsParameter; public interface VerifyParameter extends PdfAsParameter { @@ -7,4 +9,8 @@ public interface VerifyParameter extends PdfAsParameter { public int getWhichSignature(); public void setWhichSignature(int which); + + public Date getVerificationTime(); + + public void setVerificationTime(Date verificationTime); } 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 e9332bc2..04b385f7 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 @@ -76,26 +76,27 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { 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); + 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 } @@ -170,9 +171,11 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { public List verify(VerifyParameter parameter) throws PdfAsException { - + verifyVerifyParameter(parameter); - + + int signatureToVerify = parameter.getWhichSignature(); + int currentSignature = 0; PDDocument doc = null; try { List result = new ArrayList(); @@ -192,50 +195,63 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { COSDictionary field = (COSDictionary) fields.getObject(i); String type = field.getNameAsString("FT"); if ("Sig".equals(type)) { - logger.trace("Found Signature: "); - COSBase base = field.getDictionaryObject("V"); - COSDictionary dict = (COSDictionary) base; - - logger.debug("Signer: " + dict.getNameAsString("Name")); - logger.debug("SubFilter: " - + dict.getNameAsString("SubFilter")); - logger.debug("Filter: " + dict.getNameAsString("Filter")); - logger.debug("Modified: " + dict.getNameAsString("M")); - COSArray byteRange = (COSArray) dict - .getDictionaryObject("ByteRange"); - - StringBuilder sb = new StringBuilder(); - int[] bytes = new int[byteRange.size()]; - for (int j = 0; j < byteRange.size(); j++) { - bytes[j] = byteRange.getInt(j); - sb.append(" " + bytes[j]); - } + boolean verifyThis = true; - logger.debug("ByteRange" + sb.toString()); + if (signatureToVerify >= 0) { + // verify only specific siganture! + verifyThis = signatureToVerify == currentSignature; + } - COSString content = (COSString) dict - .getDictionaryObject("Contents"); + if (verifyThis) { + logger.trace("Found Signature: "); + COSBase base = field.getDictionaryObject("V"); + COSDictionary dict = (COSDictionary) base; + + logger.debug("Signer: " + dict.getNameAsString("Name")); + logger.debug("SubFilter: " + + dict.getNameAsString("SubFilter")); + logger.debug("Filter: " + + dict.getNameAsString("Filter")); + logger.debug("Modified: " + dict.getNameAsString("M")); + COSArray byteRange = (COSArray) dict + .getDictionaryObject("ByteRange"); + + StringBuilder sb = new StringBuilder(); + int[] bytes = new int[byteRange.size()]; + for (int j = 0; j < byteRange.size(); j++) { + bytes[j] = byteRange.getInt(j); + sb.append(" " + bytes[j]); + } - ByteArrayOutputStream contentData = new ByteArrayOutputStream(); - for (int j = 0; j < bytes.length; j = j + 2) { - int offset = bytes[j]; - int length = bytes[j + 1]; - contentData.write(parameter.getDataSource() - .getByteData(), offset, length); - } - contentData.close(); + logger.debug("ByteRange" + sb.toString()); - IVerifyFilter verifyFilter = verifier.getVerifier( - dict.getNameAsString("Filter"), - dict.getNameAsString("SubFilter")); + COSString content = (COSString) dict + .getDictionaryObject("Contents"); - if (verifyFilter != null) { - List results = verifyFilter.verify( - contentData.toByteArray(), content.getBytes()); - if (results != null && !results.isEmpty()) { - result.addAll(results); + ByteArrayOutputStream contentData = new ByteArrayOutputStream(); + for (int j = 0; j < bytes.length; j = j + 2) { + int offset = bytes[j]; + int length = bytes[j + 1]; + contentData.write(parameter.getDataSource() + .getByteData(), offset, length); + } + contentData.close(); + + IVerifyFilter verifyFilter = verifier.getVerifier( + dict.getNameAsString("Filter"), + dict.getNameAsString("SubFilter")); + + if (verifyFilter != null) { + List results = verifyFilter.verify( + contentData.toByteArray(), + content.getBytes(), + parameter.getVerificationTime()); + if (results != null && !results.isEmpty()) { + result.addAll(results); + } } } + currentSignature++; } } return result; diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/VerifyParameterImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/VerifyParameterImpl.java index 3b264938..d754c032 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/VerifyParameterImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/VerifyParameterImpl.java @@ -1,5 +1,7 @@ package at.gv.egiz.pdfas.lib.impl; +import java.util.Date; + import at.gv.egiz.pdfas.lib.api.Configuration; import at.gv.egiz.pdfas.lib.api.DataSource; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; @@ -8,6 +10,8 @@ public class VerifyParameterImpl extends PdfAsParameterImpl implements VerifyPar protected int which = - 1; + protected Date verificationTime = null; + public VerifyParameterImpl(Configuration configuration, DataSource dataSource) { super(configuration, dataSource); @@ -20,4 +24,12 @@ public class VerifyParameterImpl extends PdfAsParameterImpl implements VerifyPar public void setWhichSignature(int which) { this.which = which; } + + public Date getVerificationTime() { + return verificationTime; + } + + public void setVerificationTime(Date verificationTime) { + this.verificationTime = verificationTime; + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/IVerifyFilter.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/IVerifyFilter.java index 53c2e342..e83b6132 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/IVerifyFilter.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/IVerifyFilter.java @@ -1,5 +1,6 @@ package at.gv.egiz.pdfas.lib.impl.verify; +import java.util.Date; import java.util.List; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; @@ -8,6 +9,6 @@ import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; public interface IVerifyFilter { public void setConfiguration(Configuration config); - public List verify(byte[] contentData, byte[] signatureContent) throws PdfAsException; + public List verify(byte[] contentData, byte[] signatureContent, Date verificationTime) throws PdfAsException; public List getFiters(); } -- cgit v1.2.3