diff options
Diffstat (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas')
4 files changed, 65 insertions, 35 deletions
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/IPlainSigner.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/IPlainSigner.java index 6155a245..0ad40cc4 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/IPlainSigner.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/IPlainSigner.java @@ -7,9 +7,11 @@ import java.io.IOException; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.exceptions.SignatureException; +import at.gv.egiz.pdfas.common.exceptions.PdfAsException; + public interface IPlainSigner { - public X509Certificate getCertificate(); - public byte[] sign(byte[] input) throws SignatureException, IOException; + public X509Certificate getCertificate() throws PdfAsException; + public byte[] sign(byte[] input, int[] byteRange) throws PdfAsException; public String getPDFSubFilter(); public String getPDFFilter(); } 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 6f74303c..75408567 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 @@ -23,6 +23,7 @@ import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsException; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.common.settings.Settings; import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings; +import at.gv.egiz.pdfas.common.utils.PDFUtils; import at.gv.egiz.pdfas.lib.api.Configuration; import at.gv.egiz.pdfas.lib.api.IConfigurationConstants; import at.gv.egiz.pdfas.lib.api.PdfAs; @@ -47,6 +48,7 @@ import at.gv.egiz.pdfas.lib.impl.status.OperationStatus; import at.gv.egiz.pdfas.lib.impl.status.RequestedSignature; import at.gv.egiz.pdfas.lib.impl.verify.IVerifyFilter; import at.gv.egiz.pdfas.lib.impl.verify.VerifierDispatcher; +import at.knowcenter.wag.egov.egiz.pdf.PDFUtilities; import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction; import at.knowcenter.wag.egov.egiz.pdf.TablePos; import at.knowcenter.wag.egov.egiz.table.Table; @@ -83,6 +85,8 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { RequestedSignature requestedSignature = new RequestedSignature( status); + status.setRequestedSignature(requestedSignature); + requestedSignature.setCertificate(status.getSignParamter() .getPlainSigner().getCertificate()); @@ -334,13 +338,25 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { SignatureDataExtractor signatureDataExtractor = new SignatureDataExtractor( request.getCertificate(), pdfFilter, pdfSubFilter, status.getSigningDate()); - + IPdfSigner signer = PdfSignerFactory.createPdfSigner(); signer.signPDF(status.getPdfObject(), status.getRequestedSignature(), signatureDataExtractor); + + StringBuilder sb = new StringBuilder(); + + int[] byteRange = PDFUtils.extractSignatureByteRange(signatureDataExtractor + .getSignatureData()); + + for(int i = 0; i < byteRange.length; i++) { + sb.append(" " + byteRange[i]); + } + + logger.info("ByteRange: " + sb.toString()); + request.setSignatureData(signatureDataExtractor .getSignatureData()); - request.setByteRange(signatureDataExtractor.getByteRange()); + request.setByteRange(byteRange); request.setNeedSignature(true); } catch (Throwable e) { @@ -349,12 +365,14 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { } } else if (request.needSignature()) { request.setNeedSignature(false); - // TODO: Inject signature byte[] into signedDocument - int offset = request.getSignatureData().length; - - for (int i = 0; i < request.getSignature().length; i++) { - status.getPdfObject().getSignedDocument()[offset + i] = request - .getSignature()[i]; + // Inject signature byte[] into signedDocument + int offset = request.getSignatureDataByteRange()[1] + 1; + + String signature = new COSString(request.getSignature()).getHexString(); + byte[] pdfSignature = signature.getBytes(); + + for (int i = 0; i < pdfSignature.length; i++) { + status.getPdfObject().getSignedDocument()[offset + i] = pdfSignature[i]; } /* * diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index 3f566f06..2e5dc62b 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java @@ -22,7 +22,6 @@ import at.gv.egiz.pdfas.common.messages.MessageResolver; import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings; import at.gv.egiz.pdfas.common.utils.StreamUtils; import at.gv.egiz.pdfas.common.utils.TempFileHelper; -import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; import at.gv.egiz.pdfas.lib.impl.signing.IPdfSigner; import at.gv.egiz.pdfas.lib.impl.signing.sig_interface.PDFASSignatureInterface; import at.gv.egiz.pdfas.lib.impl.stamping.TableFactory; @@ -109,7 +108,7 @@ public class PADESPDFBOXSigner implements IPdfSigner { } } - +/* public void signPDF(String src, String dst, SignatureInterface signer) throws Exception { //ByteArrayOutputStream os = new ByteArrayOutputStream(); FileInputStream fis = new FileInputStream(new File(src)); @@ -144,5 +143,5 @@ public class PADESPDFBOXSigner implements IPdfSigner { fos.close(); // FileUtils.writeByteArrayToFile(new File(dst), os.toByteArray()); } - +*/ } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PdfboxSignerWrapper.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PdfboxSignerWrapper.java index 91734312..6ded2d8a 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PdfboxSignerWrapper.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PdfboxSignerWrapper.java @@ -10,6 +10,9 @@ import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.gv.egiz.pdfas.common.exceptions.PdfAsException; +import at.gv.egiz.pdfas.common.exceptions.PdfAsWrappedIOException; +import at.gv.egiz.pdfas.common.utils.PDFUtils; import at.gv.egiz.pdfas.common.utils.StreamUtils; import at.gv.egiz.pdfas.common.utils.StringUtils; import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; @@ -17,27 +20,35 @@ import at.gv.egiz.pdfas.lib.impl.signing.sig_interface.PDFASSignatureInterface; public class PdfboxSignerWrapper implements PDFASSignatureInterface { - private static final Logger logger = LoggerFactory.getLogger(PdfboxSignerWrapper.class); - - private IPlainSigner signer; - private PDSignature signature; - private int[] byteRange; - private Calendar date; - - public PdfboxSignerWrapper(IPlainSigner signer) { - this.signer = signer; - this.date = Calendar.getInstance(); - } - - public byte[] sign(InputStream inputStream) throws SignatureException, IOException { - byteRange = this.signature.getByteRange(); - byte[] signature = signer.sign(StreamUtils.inputStreamToByteArray(inputStream)); - logger.debug("Signature Data: " + StringUtils.bytesToHexString(signature)); - FileOutputStream fos = new FileOutputStream("/tmp/fos.bin"); - fos.write(signature); - fos.close(); - return signature; - } + private static final Logger logger = LoggerFactory + .getLogger(PdfboxSignerWrapper.class); + + private IPlainSigner signer; + private PDSignature signature; + private int[] byteRange; + private Calendar date; + + public PdfboxSignerWrapper(IPlainSigner signer) { + this.signer = signer; + this.date = Calendar.getInstance(); + } + + public byte[] sign(InputStream inputStream) throws SignatureException, + IOException { + byte[] data = StreamUtils.inputStreamToByteArray(inputStream); + byteRange = PDFUtils.extractSignatureByteRange(data); + try { + byte[] signature = signer.sign(data, byteRange); + logger.debug("Signature Data: " + + StringUtils.bytesToHexString(signature)); + FileOutputStream fos = new FileOutputStream("/tmp/fos.bin"); + fos.write(signature); + fos.close(); + return signature; + } catch (PdfAsException e) { + throw new PdfAsWrappedIOException(e); + } + } public int[] getByteRange() { return byteRange; @@ -52,7 +63,7 @@ public class PdfboxSignerWrapper implements PDFASSignatureInterface { } public void setPDSignature(PDSignature signature) { - this.signature = signature; + this.signature = signature; } public Calendar getSigningDate() { |