diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2013-11-12 12:26:10 +0100 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2013-11-12 12:26:10 +0100 |
commit | 7b2e2b640b0f392183f7927f692936950d3fabfc (patch) | |
tree | ffd4acccb62f2679ff613bf0cd8cd0850186b465 /pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl | |
parent | dd43264f702c5e0351cc63835862c55f3e847634 (diff) | |
download | pdf-as-4-7b2e2b640b0f392183f7927f692936950d3fabfc.tar.gz pdf-as-4-7b2e2b640b0f392183f7927f692936950d3fabfc.tar.bz2 pdf-as-4-7b2e2b640b0f392183f7927f692936950d3fabfc.zip |
SecurityLayer commands included
Diffstat (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl')
3 files changed, 61 insertions, 33 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 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() { |