diff options
Diffstat (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl')
3 files changed, 77 insertions, 48 deletions
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<VerifyResult> verify(VerifyParameter parameter) throws PdfAsException { - + verifyVerifyParameter(parameter); - + + int signatureToVerify = parameter.getWhichSignature(); + int currentSignature = 0; PDDocument doc = null; try { List<VerifyResult> result = new ArrayList<VerifyResult>(); @@ -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<VerifyResult> 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<VerifyResult> 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<VerifyResult> verify(byte[] contentData, byte[] signatureContent) throws PdfAsException; + public List<VerifyResult> verify(byte[] contentData, byte[] signatureContent, Date verificationTime) throws PdfAsException; public List<FilterEntry> getFiters(); } |