From 9496bb87c0789d819689a8750385079e44f515ee Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 18 Feb 2014 11:00:11 +0100 Subject: Moved signature data creation to PDFUtils --- .../at/gv/egiz/pdfas/common/utils/PDFUtils.java | 48 ++++++++++++++++++++++ .../resources/resources/messages/common.properties | 1 + .../java/at/gv/egiz/sl/util/BaseSLConnector.java | 35 +++------------- .../main/java/at/gv/egiz/sl/util/ISLConnector.java | 3 +- 4 files changed, 57 insertions(+), 30 deletions(-) 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 8b649034..2161c05a 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 @@ -1,6 +1,7 @@ package at.gv.egiz.pdfas.common.utils; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.pdfbox.pdmodel.PDDocument; @@ -8,6 +9,7 @@ import org.apache.pdfbox.pdmodel.encryption.AccessPermission; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.gv.egiz.pdfas.common.exceptions.PDFIOException; import at.gv.egiz.pdfas.common.exceptions.PdfAsValidationException; public class PDFUtils { @@ -28,6 +30,52 @@ public class PDFUtils { private static final byte range_seperation = (byte) 0x20; private static final byte range_end = (byte) 0x5D; + public static int[] buildExcludeRange(int[] byteRange) throws PDFIOException { + + if(byteRange.length % 2 != 0) { + throw new PDFIOException("error.pdf.io.09"); + } + + int[] exclude_range = new int[byteRange.length-2]; + + for(int i = 0; i < byteRange.length; i = i + 2) { + int offset = byteRange[i]; + int size = byteRange[i+1]; + if(i + 2 < byteRange.length) { + exclude_range[i] = offset + size; // exclude start + exclude_range[i+1] = byteRange[i+2] - 1; // exclude end + } + } + return exclude_range; + } + + public static byte[] blackOutSignature(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/resources/resources/messages/common.properties b/pdf-as-common/src/main/resources/resources/messages/common.properties index bdae59ca..3e7f3d55 100644 --- a/pdf-as-common/src/main/resources/resources/messages/common.properties +++ b/pdf-as-common/src/main/resources/resources/messages/common.properties @@ -10,6 +10,7 @@ error.pdf.io.05=Security Layer element not available error.pdf.io.06=MOA-SS Error %s [%s] [%s [%s]] error.pdf.io.07=No CMS Signature in MOA SS Response error.pdf.io.08=MOA-SS Error communication error +error.pdf.io.09=Invalid PDF byteRange #PDF Stamper Errors error.pdf.stamp.01=Failed to load true type font diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BaseSLConnector.java b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BaseSLConnector.java index 59e5d7cf..8772239c 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BaseSLConnector.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BaseSLConnector.java @@ -6,6 +6,8 @@ import java.util.Arrays; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.gv.egiz.pdfas.common.exceptions.PDFIOException; +import at.gv.egiz.pdfas.common.utils.PDFUtils; import at.gv.egiz.sl.Base64OptRefContentType; import at.gv.egiz.sl.CMSDataObjectRequiredMetaType; import at.gv.egiz.sl.CreateCMSSignatureRequestType; @@ -44,35 +46,10 @@ public abstract class BaseSLConnector implements ISLConnector { return request; } - public CreateCMSSignatureRequestType createCMSRequest(byte[] signatureData, int[] byteRange) { - // TODO build byte[] from signatureData and fill 0 bytes in byteRanged - if(byteRange.length % 2 != 0) { - // TODO: error - } - - 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); - int[] exclude_range = new int[byteRange.length-2]; - 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++; - } - if(i + 2 < byteRange.length) { - exclude_range[i] = offset + size; // exclude start - exclude_range[i+1] = byteRange[i+2] - 1; // exclude end - } - } + public CreateCMSSignatureRequestType createCMSRequest(byte[] signatureData, int[] byteRange) throws PDFIOException { + byte[] data = PDFUtils.blackOutSignature(signatureData, byteRange); + + int[] exclude_range = PDFUtils.buildExcludeRange(byteRange); logger.info("Exclude Byte Range: " + exclude_range[0] + " " + exclude_range[1]); // == MetaInfoType diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/ISLConnector.java b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/ISLConnector.java index 82571720..3c0c1b84 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/ISLConnector.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/ISLConnector.java @@ -1,5 +1,6 @@ package at.gv.egiz.sl.util; +import at.gv.egiz.pdfas.common.exceptions.PDFIOException; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.sl.CreateCMSSignatureRequestType; import at.gv.egiz.sl.CreateCMSSignatureResponseType; @@ -10,6 +11,6 @@ public interface ISLConnector { public InfoboxReadRequestType createInfoboxReadRequest(); public InfoboxReadResponseType sendInfoboxReadRequest(InfoboxReadRequestType request) throws PdfAsException; - public CreateCMSSignatureRequestType createCMSRequest(byte[] signatureData, int[] byteRange); + public CreateCMSSignatureRequestType createCMSRequest(byte[] signatureData, int[] byteRange) throws PDFIOException; public CreateCMSSignatureResponseType sendCMSRequest(CreateCMSSignatureRequestType request) throws PdfAsException; } -- cgit v1.2.3