aboutsummaryrefslogtreecommitdiff
path: root/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java')
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java218
1 files changed, 109 insertions, 109 deletions
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java
index 3f0f482..0e0da78 100644
--- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java
@@ -80,7 +80,7 @@ public class VerificationFilterImpl implements VerificationFilter
public static final String SUPRESS_EXCEPTION_WHEN_LAST_UIBLOCK_IS_NO_SIGNATURE = "supress_exception_when_last_iublock_is_no_signature";
public static final String BINARY_ONLY = "binary_only";
public static final String ASSUME_ONLY_SIGNATURE_BLOCKS = "assume_only_signature_blocks";
-
+
/**
* @see at.gv.egiz.pdfas.framework.vfilter.VerificationFilter#extractSignatureHolders(at.gv.egiz.pdfas.framework.input.PdfDataSource,
* java.util.List,
@@ -97,7 +97,7 @@ public class VerificationFilterImpl implements VerificationFilter
log.debug("Original IU blocks: " + blocks.size());
debugIUBlocks(blocks);
}
-
+
unrollLinearization(blocks);
if (log.isDebugEnabled())
@@ -106,7 +106,7 @@ public class VerificationFilterImpl implements VerificationFilter
debugIUBlocks(blocks);
}
-
+
SettingsReader settings;
try {
settings = SettingsReader.getInstance();
@@ -115,7 +115,7 @@ public class VerificationFilterImpl implements VerificationFilter
}
String check_doc = settings.getSetting(CHECK_DOCUMENT, "false");
- // check document for textual sigs here here if binary_only is set
+ // check document for textual sigs here here if binary_only is set
if ("true".equalsIgnoreCase(check_doc) &&
parameters.extractBinarySignaturesOnly()) {
@@ -125,7 +125,7 @@ public class VerificationFilterImpl implements VerificationFilter
log.debug("Skipping checkDocument for textual sigs.");
}
// end add
-
+
List signatureHolderChain = null;
if (parameters.extractBinarySignaturesOnly())
@@ -156,11 +156,11 @@ public class VerificationFilterImpl implements VerificationFilter
}
}
-
+
log.trace("extractSignaturHolders finished (" + (signatureHolderChain != null ? signatureHolderChain.size() : 0) + " elements).");
sw.stop();
log.debug("extractSignatureHolders: " + sw.getTime() + "ms.");
-
+
return signatureHolderChain;
}
@@ -194,12 +194,12 @@ public class VerificationFilterImpl implements VerificationFilter
foundSignatures = extractNewSignaturesFromText(normalizedText);
}
-
+
List textOnlySignatures = filterOutBinarySignatures(foundSignatures);
-
+
return textOnlySignatures;
}
-
+
protected String normalizeText(String freetext) throws VerificationFilterException
{
try
@@ -214,7 +214,7 @@ public class VerificationFilterImpl implements VerificationFilter
/**
* Removes the linearization footer from the list of update blocks.
- *
+ *
* @param blocks
* The list of FooterParseResult objects in \prev order.
*/
@@ -263,15 +263,15 @@ public class VerificationFilterImpl implements VerificationFilter
if (sao.oldSignature != null)
{
extractedSignatures.add(0, sao.oldSignature);
- }
+ }
}
else
{
log.debug("extracting signatures from last partition...");
extractedSignatures = extractSignaturesFromPartition(pdf, lastTextPartition);
}
-
-
+
+
List signatureHolderChain = intermingleSignatures(binarySignatures, extractedSignatures);
return signatureHolderChain;
@@ -287,15 +287,15 @@ public class VerificationFilterImpl implements VerificationFilter
Set binarySigValues = new HashSet();
Iterator iterator = binarySignatures.iterator();
while(iterator.hasNext()) {
-
+
SignatureHolder sh = (SignatureHolder)iterator.next();
-
+
String sigVal = sh.getSignatureObject().getSignationValue();
binarySigValues.add(sigVal);
}
-
+
SignatureHolder oldSignature = null;
-
+
//List originalPartitions = partitions;
// This gives every IU block an own text partition
// This allows text signatures to be found correctly if there are
@@ -313,36 +313,36 @@ public class VerificationFilterImpl implements VerificationFilter
}
String check_doc = settings.getSetting(CHECK_DOCUMENT, "false");
boolean supressException = "true".equalsIgnoreCase(settings.getSetting(SUPRESS_EXCEPTION_WHEN_LAST_UIBLOCK_IS_NO_SIGNATURE, "false"));
-
+
// flag indicating that the last IU-block of the document is a non-signature IU-block
boolean lastBlockWasModified = false;
-
+
// counter of all signatures (textual and binary) of this document
int signatureCounter = 0;
-
+
// counter of all textual signatures in this document
int txtSigsSoFar = 0;
-
+
// counter of all textual signatures in the current partition
int txtSigsThisPartition = 0;
-
+
List partitionResults = new ArrayList(partitions.size());
- List nshList = new ArrayList();
-
+ List nshList = new ArrayList();
+
boolean sigFound = false;
-
+
for (int i = 0; i < partitions.size(); i++)
{
Partition p = (Partition) partitions.get(i);
-
+
// updating flag and counter
boolean partitionContainsNewTextSignatures = true;
txtSigsSoFar = txtSigsThisPartition;
-
+
if (p instanceof TextPartition)
{
TextPartition tp = (TextPartition) p;
-
+
List partitionResult = null;
boolean scanThisPartitionForOldSignature = (i == 0) && scanForOldSignatures;
@@ -356,34 +356,34 @@ public class VerificationFilterImpl implements VerificationFilter
{
partitionResult = extractSignaturesFromPartition(pdf, tp);
}
-
- // binary signature blocks that have been detected as well are identified by comparing their signature values
+
+ // binary signature blocks that have been detected as well are identified by comparing their signature values
// with those stored in our Set above and are not considered for our IU-check
List onlyTextSignatures = new ArrayList();
Iterator iter = partitionResult.iterator();
while(iter.hasNext()) {
-
+
SignatureHolder sh = (SignatureHolder)iter.next();
if(!binarySigValues.contains(sh.getSignatureObject().getSignationValue())) {
-
+
onlyTextSignatures.add(sh);
}
}
-
+
// update signature counters
txtSigsThisPartition = onlyTextSignatures.size();
int newTextSignatures = txtSigsThisPartition - txtSigsSoFar;
signatureCounter = signatureCounter + newTextSignatures;
-
+
// update sigFound flag
if(txtSigsThisPartition > 0) {
-
+
sigFound = true;
}
-
+
// TextPartition is only valid, if at least one more text signature has been found than in the previous text partition
if(!(newTextSignatures > 0)) {
-
+
partitionContainsNewTextSignatures = false;
}
@@ -391,39 +391,39 @@ public class VerificationFilterImpl implements VerificationFilter
} else {
// should be binary partition
if(p instanceof BinaryPartition) {
-
+
BinaryPartition binpart = (BinaryPartition)p;
// updating counter and flag
signatureCounter = signatureCounter + binpart.blocks.size();
sigFound = true;
-
+
}
}
-
+
// if document checking is enabled, at least one signature has been found so far, we are dealing with a
// non-signature IU-block
if ((check_doc.equalsIgnoreCase("true"))&& (sigFound && !partitionContainsNewTextSignatures)) {
-
- nshList.add(new NoSignatureHolder(signatureCounter));
+
+ nshList.add(new NoSignatureHolder(signatureCounter));
lastBlockWasModified = true;
-
+
} else {
-
+
lastBlockWasModified = false;
}
-
+
}
// throw an exception if the last update block does not contain a signature and signatures have been found in this document
if (lastBlockWasModified) {
if (!supressException) {
- throw new VerificationFilterException(ErrorCode.MODIFIED_AFTER_SIGNATION, "The document has been modified after being signed.");
+ throw new VerificationFilterException(ErrorCode.MODIFIED_AFTER_SIGNATION, "The document has been modified after being signed.");
} else {
log.debug("The document has been modified after being signed. According to the configuration, no exception is thrown.");
}
}
-
+
List extractedSignatures = new ArrayList();
Iterator it = partitionResults.iterator();
List prevPartitionResult = null;
@@ -451,54 +451,54 @@ public class VerificationFilterImpl implements VerificationFilter
}
List signatureHolderChain = intermingleSignatures(binarySignatures, extractedSignatures);
-
+
if (oldSignature != null)
{
signatureHolderChain.add(0, oldSignature);
}
-
+
// add the created NoSignatureHolders
signatureHolderChain.addAll(nshList);
-
+
return signatureHolderChain;
}
-
+
private void mergeSignatures(List oldList, List newList, List result) {
-
+
for(int i=0; i < newList.size(); i++) {
-
+
SignatureHolder currentNewSh = (SignatureHolder)newList.get(i);
-
+
boolean shAlreadyPresentInOldList = false;
int pos = -1;
-
+
for(int j=0; j<oldList.size(); j++) {
-
+
SignatureHolder currentOldSh = (SignatureHolder)oldList.get(j);
-
+
if(currentNewSh.getSignatureObject().getSignationValue().equals(currentOldSh.getSignatureObject().getSignationValue())) {
-
+
shAlreadyPresentInOldList = true;
pos = j;
}
}
-
+
if(!shAlreadyPresentInOldList) {
-
+
// signature holder has not been found earlier -> add
result.add(currentNewSh);
}
-
+
}
-
-
+
+
return;
}
-
-
+
+
protected List flattenOutTextPartitions (List partitions, List blocks)
{
-
+
List blockPartitions = new ArrayList(blocks.size());
Iterator it = partitions.iterator();
while (it.hasNext())
@@ -523,10 +523,10 @@ public class VerificationFilterImpl implements VerificationFilter
blockPartitions.add(p);
}
}
-
+
// note: successive binary blocks are still combined to one binary partition
assert blockPartitions.size() <= blocks.size();
-
+
return blockPartitions;
}
@@ -536,15 +536,15 @@ public class VerificationFilterImpl implements VerificationFilter
protected String extractText(PdfDataSource pdf, int endOfDocument, String encoding) throws PresentableException
{
-
+
log.debug("EXTRACTING TEXT (" + encoding + ")... end index = " + endOfDocument);
-
+
DelimitedPdfDataSource dds = new DelimitedPdfDataSource(pdf, endOfDocument);
//DelimitedInputStream dis = new DelimitedInputStream(pdf.createInputStream(), endOfDocument);
return PdfAS.extractNormalizedTextTextual(dds, encoding);
}
-
-
+
+
protected List extractNewSignaturesFromText(String text) throws VerificationFilterException
{
try
@@ -556,7 +556,7 @@ public class VerificationFilterImpl implements VerificationFilter
throw new VerificationFilterException(e);
}
}
-
+
protected List extractNewAndOldSignaturesFromText(String text) throws VerificationFilterException
{
SignaturesAndOld sao = extractSignaturesAndOld(text);
@@ -651,11 +651,11 @@ public class VerificationFilterImpl implements VerificationFilter
/**
* Extracts the binary singatures from the given PDF.
- *
+ *
* <p>
* IU blocks without an egiz dict are not considered.
* </p>
- *
+ *
* @param pdf
* @param blocks
* @return Returns the List of signature holders.
@@ -673,7 +673,7 @@ public class VerificationFilterImpl implements VerificationFilter
String binary_only = settings.getSetting(BINARY_ONLY, "false");
String assume_sigs_only = settings.getSetting(ASSUME_ONLY_SIGNATURE_BLOCKS, "false");
boolean supressException = "true".equalsIgnoreCase(settings.getSetting(SUPRESS_EXCEPTION_WHEN_LAST_UIBLOCK_IS_NO_SIGNATURE, "false"));
-
+
try
{
// PERF: extract binary signatures needs byte array
@@ -689,15 +689,15 @@ public class VerificationFilterImpl implements VerificationFilter
FooterParseResult fpr = (FooterParseResult) it.next();
assert fpr.next_index > prev_end;
- if (VerificationFilterBinaryHelper.containsEGIZDict(data, fpr))
+ PdfASID kz = null;
+ if (VerificationFilterBinaryHelper.containsEGIZDict(data, fpr) &&
+ (kz = VerificationFilterBinaryHelper.extractKZFromEGIZBlock(data, fpr)) != null)
{
- PdfASID kz = VerificationFilterBinaryHelper.extractKZFromEGIZBlock(data, fpr);
-
// TODO dferbas hack baik test
//kz = new PdfASID("urn:pdfsigfilter:bka.gv.at:binaer:v1.1.0");
-
+
Verificator verificator = VerificatorFactory.createBinaryVerificator(kz);
- List binary_holders = verificator.parseBlock(pdf, data, fpr, prev_end);
+ List binary_holders = verificator.parseBlock(pdf, data, fpr, prev_end);
binarySignatures.addAll(binary_holders);
if(binary_holders.size() > 0) {
@@ -707,21 +707,21 @@ public class VerificationFilterImpl implements VerificationFilter
// an Exception is thrown here if:
// 1) check_document is activated
// 2) assume_only_signature_blocks is false - otherwise we permit updates
- // 3) binary_only is true - otherwise updates are handled in method performFullConservative().
- // when binary-only is true, we can be sure that a block that contains no egiz-dict is no textual
+ // 3) binary_only is true - otherwise updates are handled in method performFullConservative().
+ // when binary-only is true, we can be sure that a block that contains no egiz-dict is no textual
// signature either but an illegal update, otherwise an Exception (doc contains textual sig) would have been thrown before
// 4) a binary signature has been detected in a previous block
- if(check_doc.equalsIgnoreCase("true") &&
+ if(check_doc.equalsIgnoreCase("true") &&
binary_only.equalsIgnoreCase("true") &&
assume_sigs_only.equalsIgnoreCase("false") &&
- sig_detected) {
-
+ sig_detected) {
+
if (!supressException) {
- throw new VerificationFilterException(ErrorCode.MODIFIED_AFTER_SIGNATION, "The document has been modified after being signed.");
+ throw new VerificationFilterException(ErrorCode.MODIFIED_AFTER_SIGNATION, "The document has been modified after being signed.");
} else {
log.debug("The document has been modified after being signed. According to the configuration, no exception is thrown.");
}
-
+
}
}
@@ -749,7 +749,7 @@ public class VerificationFilterImpl implements VerificationFilter
List extractedSignatures = extractNewSignaturesFromText(extractedText);
log.debug("Extracting signatures finished.");
log.debug("Number of found signatures: " + extractedSignatures.size());
-
+
if (extractedSignatures.size() > 0) {
List cp1252SignaturesPositions = new ArrayList();
//boolean iscp1252Sig = false;
@@ -760,19 +760,19 @@ public class VerificationFilterImpl implements VerificationFilter
log.debug("found cp1252 signature");
cp1252SignaturesPositions.add(new Integer(i));
//iscp1252Sig = true;
- //break;
+ //break;
}
- }
+ }
if (cp1252SignaturesPositions.size() > 0) {
log.debug("redo text and signature extraction with cp1252 encoding");
extractedText = extractText(pdf, endOfDocument, "cp1252");
log.debug("Extracting text finished.");
-
+
log.debug("Extracting signatures:");
List cp1252ExtractedSignatures = extractNewSignaturesFromText(extractedText);
log.debug("Extracting signatures finished.");
log.debug("Number of found signatures: " + extractedSignatures.size());
-
+
if (cp1252ExtractedSignatures.size() != extractedSignatures.size()) {
log.error("Invalid cp1252 signatures found. Skipping cp1252 compatibility.");
}
@@ -780,12 +780,12 @@ public class VerificationFilterImpl implements VerificationFilter
for (int i = 0; i < cp1252SignaturesPositions.size(); i++) {
int replaceIndex = ((Integer)cp1252SignaturesPositions.get(i)).intValue();
extractedSignatures.remove(replaceIndex);
- extractedSignatures.add(replaceIndex, cp1252ExtractedSignatures.get(replaceIndex));
+ extractedSignatures.add(replaceIndex, cp1252ExtractedSignatures.get(replaceIndex));
}
}
-
+
}
-
+
if (log.isDebugEnabled())
{
log.debug("extracted signatures:");
@@ -856,7 +856,7 @@ public class VerificationFilterImpl implements VerificationFilter
}
protected void checkBinaryOnly(PdfDataSource pdf, boolean considerOldSigs) throws VerificationFilterException {
-
+
DelimitedPdfDataSource dds = new DelimitedPdfDataSource(pdf, pdf.getLength());
String text = null;
try {
@@ -864,10 +864,10 @@ public class VerificationFilterImpl implements VerificationFilter
} catch (PresentableException e) {
throw new VerificationFilterException(e);
}
-
+
List sigs = new ArrayList();
-
- if(considerOldSigs) {
+
+ if(considerOldSigs) {
SignaturesAndOld sao = extractSignaturesAndOld(text);
if(sao != null) {
if(sao.newSignatures != null) {
@@ -877,30 +877,30 @@ public class VerificationFilterImpl implements VerificationFilter
sigs.add(sao.oldSignature);
}
}
- } else {
+ } else {
List signatures = extractSignatures(pdf, pdf.getLength());
if(signatures != null) {
sigs.addAll(signatures);
}
}
-
+
Iterator it = sigs.iterator();
while(it.hasNext()) {
SignatureHolder current = (SignatureHolder)it.next();
if((current != null)&&(!current.getSignatureObject().isBinary())) {
throw new VerificationFilterException(ErrorCode.NON_BINARY_SIGNATURES_PRESENT, "The document contains non-binary signatures.");
}
- }
+ }
}
-
-
+
+
protected static class SignaturesAndOld
{
public List newSignatures = null;
public SignatureHolder oldSignature = null;
}
-
+
protected SignaturesAndOld extractSignaturesAndOld(String text) throws VerificationFilterException
{
try
@@ -908,7 +908,7 @@ public class VerificationFilterImpl implements VerificationFilter
log.debug("Extracting signatures:");
List extractedSignatures = extractNewSignaturesFromText(text);
log.debug("Extracting signatures finished.");
-
+
log.debug("Extracting old signatures:");
SignatureHolder oldSignature = extractOldSignature(text, extractedSignatures);
log.debug("Extracting old signatures finished.");
@@ -929,7 +929,7 @@ public class VerificationFilterImpl implements VerificationFilter
/**
* Extracts the old signature from the text, but only if it is older than the
* oldest signature of the new signatueres.
- *
+ *
* @param extractedText
* @param newSignatures
* @return