From f3476576c50efd922593c82656efda7aec5ae97f Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Wed, 27 Nov 2013 10:05:17 +0100 Subject: MOA integration sign/verification (not finished yet ...) Lots of PDF-AS Web implementation --- .../at/gv/egiz/pdfas/lib/api/PdfAsFactory.java | 4 +- .../gv/egiz/pdfas/lib/api/verify/VerifyResult.java | 2 + .../java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java | 37 ++++++----- .../pdfas/lib/impl/verify/SignatureCheckImpl.java | 23 +++++++ .../pdfas/lib/impl/verify/VerifierDispatcher.java | 71 +++++++++++++--------- .../pdfas/lib/impl/verify/VerifyResultImpl.java | 10 ++- 6 files changed, 99 insertions(+), 48 deletions(-) create mode 100644 pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/SignatureCheckImpl.java (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas') diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java index e8865d3e..10391ecc 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java @@ -2,6 +2,7 @@ package at.gv.egiz.pdfas.lib.api; import java.io.File; +import org.apache.log4j.BasicConfigurator; import org.apache.log4j.PropertyConfigurator; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; @@ -13,7 +14,8 @@ import at.gv.egiz.pdfas.lib.impl.VerifyParameterImpl; public class PdfAsFactory { static { - PropertyConfigurator.configure(ClassLoader.getSystemResourceAsStream("resources/log4j.properties")); + //PropertyConfigurator.configure(ClassLoader.getSystemResourceAsStream("resources/log4j.properties")); + BasicConfigurator.configure(); } public static PdfAs createPdfAs(File configuration) { diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/verify/VerifyResult.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/verify/VerifyResult.java index b9d286c3..436bfb25 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/verify/VerifyResult.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/verify/VerifyResult.java @@ -51,4 +51,6 @@ public interface VerifyResult { public X509Certificate getSignerCertificate(); + + public byte[] getSignatureData(); } 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 75408567..4a8e41c3 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 @@ -86,7 +86,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { status); status.setRequestedSignature(requestedSignature); - + requestedSignature.setCertificate(status.getSignParamter() .getPlainSigner().getCertificate()); @@ -256,10 +256,13 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { dict.getNameAsString("Filter"), dict.getNameAsString("SubFilter")); - List results = verifyFilter.verify( - contentData.toByteArray(), content.getBytes()); - - result.addAll(results); + if (verifyFilter != null) { + List results = verifyFilter.verify( + contentData.toByteArray(), content.getBytes()); + if(results != null && !results.isEmpty()) { + result.addAll(results); + } + } } } return result; @@ -338,22 +341,23 @@ 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++) { + + 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(byteRange); @@ -368,9 +372,10 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { // Inject signature byte[] into signedDocument int offset = request.getSignatureDataByteRange()[1] + 1; - String signature = new COSString(request.getSignature()).getHexString(); + 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/verify/SignatureCheckImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/SignatureCheckImpl.java new file mode 100644 index 00000000..6daf4735 --- /dev/null +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/SignatureCheckImpl.java @@ -0,0 +1,23 @@ +package at.gv.egiz.pdfas.lib.impl.verify; + +import at.gv.egiz.pdfas.lib.api.verify.SignatureCheck; + +public class SignatureCheckImpl implements SignatureCheck { + + private int code; + private String message; + + public SignatureCheckImpl(int code, String message) { + this.code = code; + this.message = message; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return message; + } + +} diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/VerifierDispatcher.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/VerifierDispatcher.java index 0de3a71e..ee9c27ae 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/VerifierDispatcher.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/VerifierDispatcher.java @@ -13,49 +13,60 @@ import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.settings.ISettings; public class VerifierDispatcher { - - private static final Logger logger = LoggerFactory.getLogger(VerifierDispatcher.class); - - public static final String currentClass = "at.gv.egiz.pdfas.sigs.pkcs7detached.PKCS7DetachedVerifier"; - + + private static final Logger logger = LoggerFactory + .getLogger(VerifierDispatcher.class); + + public static final String[] currentClasses = new String[] { + //"at.gv.egiz.pdfas.sigs.pkcs7detached.PKCS7DetachedVerifier", + "at.gv.egiz.pdfas.sigs.pades.PAdESVerifier" }; + public Map> filterMap = new HashMap>(); - + public VerifierDispatcher(ISettings settings) { // TODO: read config build verify filter try { - Class cls = (Class) Class.forName(currentClass); - IVerifyFilter fitler = cls.newInstance(); - List entries = fitler.getFiters(); - Iterator it = entries.iterator(); - while(it.hasNext()) { - FilterEntry entry = it.next(); - HashMap filters = filterMap.get(entry.getFilter().getName()); - if(filters == null) { - filters = new HashMap(); - filterMap.put(entry.getFilter().getName(), filters); - } - - IVerifyFilter oldFilter = filters.get(entry.getSubFilter().getName()); - - if(oldFilter != null) { - throw new PdfAsException("Filter allready registered"); + for (int i = 0; i < currentClasses.length; i++) { + String clsName = currentClasses[i]; + Class cls = (Class) Class + .forName(clsName); + IVerifyFilter fitler = cls.newInstance(); + List entries = fitler.getFiters(); + Iterator it = entries.iterator(); + while (it.hasNext()) { + FilterEntry entry = it.next(); + HashMap filters = filterMap + .get(entry.getFilter().getName()); + if (filters == null) { + filters = new HashMap(); + filterMap.put(entry.getFilter().getName(), filters); + } + + IVerifyFilter oldFilter = filters.get(entry.getSubFilter() + .getName()); + + if (oldFilter != null) { + throw new PdfAsException("Filter allready registered"); + } + + filters.put(entry.getSubFilter().getName(), fitler); + logger.debug("Registered Filter: " + cls.getName() + + " for " + entry.getFilter().getName() + "/" + + entry.getSubFilter().getName()); } - - filters.put(entry.getSubFilter().getName(), fitler); - logger.debug("Registered Filter: " + cls.getName() + " for " + entry.getFilter().getName() + "/" + entry.getSubFilter().getName()); } - } catch(Throwable e) { + } catch (Throwable e) { e.printStackTrace(); } - + } - + public IVerifyFilter getVerifier(String filter, String subfilter) { HashMap filters = filterMap.get(filter); - if(filters == null) { + if (filters == null) { return null; } - + return filters.get(subfilter); } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/VerifyResultImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/VerifyResultImpl.java index 451c1706..5a67d807 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/VerifyResultImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/verify/VerifyResultImpl.java @@ -13,7 +13,7 @@ public class VerifyResultImpl implements VerifyResult { private SignatureCheck certificateCheck; private SignatureCheck valueCheck; private SignatureCheck manifestCheck; - + private byte[] signatureData; private X509Certificate signerCertificate; public boolean isVerificationDone() { @@ -72,4 +72,12 @@ public class VerifyResultImpl implements VerifyResult { this.signerCertificate = signerCertificate; } + public void setSignatureData(byte[] signaturData) { + this.signatureData = signaturData; + } + + public byte[] getSignatureData() { + return signatureData; + } + } -- cgit v1.2.3