From af90012c848711a4c9010dbcf71694dbfbca0e86 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Thu, 10 Jul 2014 12:09:05 +0200 Subject: Integrity verification of Signature after Signature creation to ensure correct signed Document --- .../common/exceptions/PdfAsSignatureException.java | 4 ++ .../at/gv/egiz/pdfas/common/utils/PDFUtils.java | 27 +++++++++++++ .../at/gv/egiz/pdfas/common/utils/StreamUtils.java | 44 ++++++++++++++-------- 3 files changed, 59 insertions(+), 16 deletions(-) (limited to 'pdf-as-common/src/main/java/at/gv') diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsSignatureException.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsSignatureException.java index 759a9ec8..f701bffc 100644 --- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsSignatureException.java +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsSignatureException.java @@ -33,4 +33,8 @@ public class PdfAsSignatureException extends PdfAsException { public PdfAsSignatureException(String msgId, Throwable e) { super(msgId, e); } + + public PdfAsSignatureException(String msgId) { + super(msgId); + } } diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/PDFUtils.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/PDFUtils.java index 8eb2e8bb..68cfd80a 100644 --- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/PDFUtils.java +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/PDFUtils.java @@ -99,6 +99,33 @@ public class PDFUtils { return data; } + public static byte[] extractSignatureData(byte[] signatureData, int[] byteRange) throws PDFIOException { + if(byteRange.length % 2 != 0) { + throw new PDFIOException("error.pdf.io.09"); + } + + int lastOffset = byteRange[byteRange.length - 2]; + int lastSize = byteRange[byteRange.length - 1]; + + int dataSize = lastOffset + lastSize; + + byte[] data = new byte[dataSize]; + int currentdataOff = 0; + + Arrays.fill(data, (byte)0x0); + + for(int i = 0; i < byteRange.length; i = i + 2) { + int offset = byteRange[i]; + int size = byteRange[i+1]; + + for(int j = 0; j < size; j++) { + data[offset + j] = signatureData[currentdataOff]; + currentdataOff++; + } + } + return data; + } + private static int extractASCIIInteger(byte[] data, int offset) { int nextsepp = nextSeperator(data, offset); diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StreamUtils.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StreamUtils.java index 579099f0..50ec444c 100644 --- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StreamUtils.java +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/StreamUtils.java @@ -28,24 +28,36 @@ import java.io.IOException; import java.io.InputStream; /** - * Created with IntelliJ IDEA. - * User: afitzek - * Date: 8/29/13 - * Time: 9:54 AM - * To change this template use File | Settings | File Templates. + * Created with IntelliJ IDEA. User: afitzek Date: 8/29/13 Time: 9:54 AM To + * change this template use File | Settings | File Templates. */ public class StreamUtils { - public static byte[] inputStreamToByteArray(InputStream stream) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] buffer = new byte[4096]; - int readBytes = 0; + public static byte[] inputStreamToByteArray(InputStream stream) + throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int readBytes = 0; - while((readBytes = stream.read(buffer)) != -1) { - bos.write(buffer, 0, readBytes); - } - stream.close(); - bos.close(); - return bos.toByteArray(); - } + while ((readBytes = stream.read(buffer)) != -1) { + bos.write(buffer, 0, readBytes); + } + stream.close(); + bos.close(); + return bos.toByteArray(); + } + + public static boolean dataCompare(byte[] a, byte[] b) { + if (a != null && b != null) { + if (a.length == b.length) { + for(int i = 0; i < a.length; i++) { + if(a[i] != b[i]) { + return false; + } + } + return true; + } + } + return false; + } } -- cgit v1.2.3