diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-10-30 17:15:54 +0100 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-10-30 17:15:54 +0100 |
commit | fb3344c1e4d4ac8e8a6aa5605d052cee8cd4c3b1 (patch) | |
tree | 01e04f2f9e9286c02bbc55699d027150180bbe01 /pdf-as-lib/src/main/java/at/gv/egiz | |
parent | e123b037e65b336c677c0a66125ae85f5827417a (diff) | |
download | pdf-as-4-fb3344c1e4d4ac8e8a6aa5605d052cee8cd4c3b1.tar.gz pdf-as-4-fb3344c1e4d4ac8e8a6aa5605d052cee8cd4c3b1.tar.bz2 pdf-as-4-fb3344c1e4d4ac8e8a6aa5605d052cee8cd4c3b1.zip |
Introduced Process Information Map in PDFASError and SignResult
Diffstat (limited to 'pdf-as-lib/src/main/java/at/gv/egiz')
11 files changed, 158 insertions, 46 deletions
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignResult.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignResult.java index 8084f8f1..d2dc896e 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignResult.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/sign/SignResult.java @@ -24,6 +24,7 @@ package at.gv.egiz.pdfas.lib.api.sign; import java.security.cert.X509Certificate; +import java.util.Map; import at.gv.egiz.pdfas.lib.api.SignaturePosition; @@ -52,4 +53,6 @@ public interface SignResult { * return null if no position information is available. */ public SignaturePosition getSignaturePosition(); + + public Map<String, String> getProcessInformations(); } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/ErrorExtractor.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/ErrorExtractor.java index 6708fc5b..9b2a8d79 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/ErrorExtractor.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/ErrorExtractor.java @@ -7,6 +7,7 @@ import at.gv.egiz.pdfas.common.exceptions.ErrorConstants; import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.exceptions.PdfAsMOAException; import at.gv.egiz.pdfas.common.exceptions.SLPdfAsException; +import at.gv.egiz.pdfas.lib.impl.status.OperationStatus; public class ErrorExtractor implements ErrorConstants { @@ -48,7 +49,7 @@ public class ErrorExtractor implements ErrorConstants { return null; } - public static PDFASError searchPdfAsError(Throwable e) { + public static PDFASError searchPdfAsError(Throwable e, OperationStatus status) { Throwable cur = e; PDFASError err = null; @@ -68,6 +69,11 @@ public class ErrorExtractor implements ErrorConstants { } if(err != null) { + + if(status != null) { + err.getProcessInformations().putAll(status.getMetaInformations()); + } + return err; } @@ -89,11 +95,22 @@ public class ErrorExtractor implements ErrorConstants { } if (err != null) { + + if(status != null) { + err.getProcessInformations().putAll(status.getMetaInformations()); + } + return err; } logger.info("Cannot extract correct failure code from: ", e); - return new PDFASError(ERROR_GENERIC, e); + err = new PDFASError(ERROR_GENERIC, e); + + if(status != null) { + err.getProcessInformations().putAll(status.getMetaInformations()); + } + + return err; } } 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 4828bae3..aa524cc9 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 @@ -190,7 +190,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, } catch (Throwable e) { logger.error("Failed to create signature [" + e.getMessage() + "]", e); - throw ErrorExtractor.searchPdfAsError(e); + throw ErrorExtractor.searchPdfAsError(e, status); } finally { if (status != null) { status.clear(); @@ -217,7 +217,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, try { return backend.getVerifier().verify(parameter); } catch (Throwable e) { - throw ErrorExtractor.searchPdfAsError(e); + throw ErrorExtractor.searchPdfAsError(e, null); } } @@ -230,7 +230,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, verifySignParameter(parameter); StatusRequestImpl request = new StatusRequestImpl(); - + OperationStatus status = null; try { // Status initialization if (!(parameter.getConfiguration() instanceof ISettings)) { @@ -248,7 +248,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, } ISettings settings = (ISettings) parameter.getConfiguration(); - OperationStatus status = new OperationStatus(settings, parameter, + status = new OperationStatus(settings, parameter, backend); IPdfSigner signer = backend.getPdfSigner(); @@ -267,7 +267,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, return request; } catch (Throwable e) { logger.error("startSign", e); - throw ErrorExtractor.searchPdfAsError(e); + throw ErrorExtractor.searchPdfAsError(e, status); } } @@ -329,7 +329,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, } catch (Throwable e) { logger.error("process", e); - throw ErrorExtractor.searchPdfAsError(e); + throw ErrorExtractor.searchPdfAsError(e, status); } } else if (request.needSignature()) { request.setNeedSignature(false); @@ -379,7 +379,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, return createSignResult(status); } catch (IOException e) { // new PdfAsException("error.pdf.sig.06", e); - throw ErrorExtractor.searchPdfAsError(e); + throw ErrorExtractor.searchPdfAsError(e, status); } finally { if (status != null) { status.clear(); @@ -456,7 +456,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, .getCertificate()); result.setSignaturePosition(status.getRequestedSignature() .getSignaturePosition()); - + result.getProcessInformations().putAll(status.getMetaInformations()); return result; } @@ -499,10 +499,10 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants, resolution, status, requestedSignature); } catch (PdfAsException e) { logger.error("PDF-AS Exception", e); - throw ErrorExtractor.searchPdfAsError(e); + throw ErrorExtractor.searchPdfAsError(e, status); } catch (Throwable e) { logger.error("Throwable Exception", e); - throw ErrorExtractor.searchPdfAsError(e); + throw ErrorExtractor.searchPdfAsError(e, status); } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignResultImpl.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignResultImpl.java index f713eb6a..44992c2b 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignResultImpl.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/SignResultImpl.java @@ -24,6 +24,8 @@ package at.gv.egiz.pdfas.lib.impl; import java.security.cert.X509Certificate; +import java.util.HashMap; +import java.util.Map; import at.gv.egiz.pdfas.lib.api.SignaturePosition; import at.gv.egiz.pdfas.lib.api.sign.SignResult; @@ -32,6 +34,7 @@ public class SignResultImpl implements SignResult { protected X509Certificate certificate; protected SignaturePosition position; + protected Map<String, String> processInfo = new HashMap<String, String>(); public SignResultImpl() { } @@ -52,4 +55,9 @@ public class SignResultImpl implements SignResult { this.position = position; } + @Override + public Map<String, String> getProcessInformations() { + return processInfo; + } + } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/OperationStatus.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/OperationStatus.java index 7b2ce69c..ecdddfa1 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/OperationStatus.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/OperationStatus.java @@ -54,6 +54,7 @@ public class OperationStatus implements Serializable { private RequestedSignature requestedSignature; private Calendar signingDate; private PDFASBackend backend; + private Map<String, String> metaInformations = new HashMap<String, String>(); public OperationStatus(ISettings configuration, SignParameter signParameter, PDFASBackend backend) { this.configuration = configuration; @@ -169,4 +170,8 @@ public class OperationStatus implements Serializable { } return null; } + + public Map<String, String> getMetaInformations() { + return metaInformations; + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/RequestedSignature.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/RequestedSignature.java index c9cab906..8226d7e9 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/RequestedSignature.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/status/RequestedSignature.java @@ -92,7 +92,8 @@ public class RequestedSignature implements ICertificateProvider { public void setSignaturePosition(SignaturePosition signaturePosition) { this.signaturePosition = signaturePosition; } - - - + + public OperationStatus getStatus() { + return status; + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/util/SignatureUtils.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/util/SignatureUtils.java index cdc7f11e..14dbdd16 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/util/SignatureUtils.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/util/SignatureUtils.java @@ -46,14 +46,14 @@ public class SignatureUtils implements ErrorConstants { VerifyResultImpl verifyResult = new VerifyResultImpl(); //results.add(verifyResult); try { - logger.info("Signature Algo: {}, Digest {}", signedData + logger.debug("Signature Algo: {}, Digest {}", signedData .getSignerInfos()[0].getSignatureAlgorithm(), signedData.getSignerInfos()[0].getDigestAlgorithm()); // verify the signature for SignerInfo at index i X509Certificate signer_cert = signedData.verify(0); // if the signature is OK the certificate of the // signer is returned - logger.info("Signature OK from signer: " + logger.debug("Signature OK from signer: " + signer_cert.getSubjectDN()); verifyResult.setSignerCertificate(signer_cert); diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUHeader.java b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUHeader.java new file mode 100644 index 00000000..7733e78a --- /dev/null +++ b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUHeader.java @@ -0,0 +1,32 @@ +package at.gv.egiz.sl.util; + +public class BKUHeader { + private String name; + private String value; + + public BKUHeader(String name, String value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return getName() + " = " + getValue(); + } +} diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUSLConnector.java b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUSLConnector.java index 8907711a..e1c7bf40 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUSLConnector.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUSLConnector.java @@ -31,6 +31,7 @@ import java.nio.charset.Charset; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; +import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; @@ -69,7 +70,8 @@ public class BKUSLConnector extends BaseSLConnector { return builder.build(); } - private String performHttpRequestToBKU(String xmlRequest, RequestPackage pack, SignParameter parameter) + private String performHttpRequestToBKU(String xmlRequest, + RequestPackage pack, SignParameter parameter) throws ClientProtocolException, IOException, IllegalStateException { CloseableHttpClient client = null; try { @@ -82,16 +84,17 @@ public class BKUSLConnector extends BaseSLConnector { entityBuilder.addTextBody(XMLREQUEST, xmlRequest, ContentType.TEXT_XML); - if(parameter != null) { + if (parameter != null) { String transactionId = parameter.getTransactionId(); - if(transactionId != null) { + if (transactionId != null) { entityBuilder.addTextBody("TransactionId_", transactionId); } } - - if(pack != null && pack.getSignatureData() != null) { - entityBuilder.addBinaryBody("fileupload", - PDFUtils.blackOutSignature(pack.getSignatureData(), pack.getByteRange())); + + if (pack != null && pack.getSignatureData() != null) { + entityBuilder.addBinaryBody("fileupload", PDFUtils + .blackOutSignature(pack.getSignatureData(), + pack.getByteRange())); } post.setEntity(entityBuilder.build()); @@ -99,6 +102,19 @@ public class BKUSLConnector extends BaseSLConnector { logger.debug("Response Code : " + response.getStatusLine().getStatusCode()); + if(pack != null) { + Header[] headers = response.getAllHeaders(); + + if (headers != null) { + for (int i = 0; i < headers.length; i++) { + BKUHeader hdr = new BKUHeader(headers[i].getName(), headers[i].getValue()); + logger.debug("Response Header : {}", + hdr.toString()); + pack.getHeaders().add(hdr); + } + } + } + BufferedReader rd = new BufferedReader(new InputStreamReader( response.getEntity().getContent())); @@ -110,20 +126,21 @@ public class BKUSLConnector extends BaseSLConnector { rd.close(); response = null; rd = null; - + logger.trace(result.toString()); return result.toString(); } catch (PDFIOException e) { throw new PdfAsWrappedIOException(e); } finally { - if(client != null) { + if (client != null) { client.close(); } } } public InfoboxReadResponseType sendInfoboxReadRequest( - InfoboxReadRequestType request, SignParameter parameter) throws PdfAsException { + InfoboxReadRequestType request, SignParameter parameter) + throws PdfAsException { JAXBElement<?> element = null; String slRequest; try { @@ -131,7 +148,8 @@ public class BKUSLConnector extends BaseSLConnector { .createInfoboxReadRequest(request)); logger.trace(slRequest); - String slResponse = performHttpRequestToBKU(slRequest, null, parameter); + String slResponse = performHttpRequestToBKU(slRequest, null, + parameter); element = (JAXBElement<?>) SLMarschaller .unmarshalFromString(slResponse); @@ -161,8 +179,8 @@ public class BKUSLConnector extends BaseSLConnector { throw new PdfAsException("error.pdf.io.03"); } - public CreateCMSSignatureResponseType sendCMSRequest( - RequestPackage pack, SignParameter parameter) throws PdfAsException { + public CreateCMSSignatureResponseType sendCMSRequest(RequestPackage pack, + SignParameter parameter) throws PdfAsException { JAXBElement<?> element = null; String slRequest; try { @@ -170,7 +188,8 @@ public class BKUSLConnector extends BaseSLConnector { .createCreateCMSSignatureRequest(pack.getRequestType())); logger.debug(slRequest); - String slResponse = performHttpRequestToBKU(slRequest, pack, parameter); + String slResponse = performHttpRequestToBKU(slRequest, pack, + parameter); element = (JAXBElement<?>) SLMarschaller .unmarshalFromString(slResponse); diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/ISignatureConnectorSLWrapper.java b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/ISignatureConnectorSLWrapper.java index 19dc3d76..49a450af 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/ISignatureConnectorSLWrapper.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/ISignatureConnectorSLWrapper.java @@ -31,6 +31,7 @@ import java.util.Iterator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.gv.egiz.pdfas.common.exceptions.ErrorConstants; import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.exceptions.PdfAsErrorCarrier; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; @@ -48,7 +49,9 @@ import at.gv.egiz.sl.schema.InfoboxReadResponseType; public class ISignatureConnectorSLWrapper implements ISignatureConnector { public static final String SL_USE_BASE64 = ""; - + + public static final String SIGNATURE_DEVICE = "BKU"; + private static final Logger logger = LoggerFactory .getLogger(ISignatureConnectorSLWrapper.class); @@ -58,7 +61,8 @@ public class ISignatureConnectorSLWrapper implements ISignatureConnector { this.connector = connector; } - public X509Certificate getCertificate(SignParameter parameter) throws PdfAsException { + public X509Certificate getCertificate(SignParameter parameter) + throws PdfAsException { X509Certificate certificate = null; try { InfoboxReadRequestType request = connector @@ -83,25 +87,43 @@ public class ISignatureConnectorSLWrapper implements ISignatureConnector { return certificate; } - public byte[] sign(byte[] input, int[] byteRange, - SignParameter parameter, RequestedSignature requestedSignature) throws PdfAsException { - RequestPackage pack = connector.createCMSRequest( - input, byteRange, parameter); - CreateCMSSignatureResponseType response = connector - .sendCMSRequest(pack, parameter); - + public byte[] sign(byte[] input, int[] byteRange, SignParameter parameter, + RequestedSignature requestedSignature) throws PdfAsException { + RequestPackage pack = connector.createCMSRequest(input, byteRange, + parameter); + CreateCMSSignatureResponseType response = connector.sendCMSRequest( + pack, parameter); + + Iterator<BKUHeader> bkuHeaderIt = pack.getHeaders().iterator(); + + requestedSignature.getStatus().getMetaInformations() + .put(ErrorConstants.STATUS_INFO_SIGDEVICE, SIGNATURE_DEVICE); + while (bkuHeaderIt.hasNext()) { + BKUHeader header = bkuHeaderIt.next(); + if ("Server".equalsIgnoreCase(header.getName())) { + requestedSignature + .getStatus() + .getMetaInformations() + .put(ErrorConstants.STATUS_INFO_SIGDEVICEVERSION, + header.getValue()); + break; + } + } + VerifyResult verifyResult; try { - verifyResult = SignatureUtils.verifySignature(response.getCMSSignature(), input); + verifyResult = SignatureUtils.verifySignature( + response.getCMSSignature(), input); } catch (PDFASError e) { throw new PdfAsErrorCarrier(e); } - if(!StreamUtils.dataCompare(requestedSignature.getCertificate().getFingerprintSHA(), - ((X509Certificate)verifyResult.getSignerCertificate()).getFingerprintSHA())) { + if (!StreamUtils.dataCompare(requestedSignature.getCertificate() + .getFingerprintSHA(), ((X509Certificate) verifyResult + .getSignerCertificate()).getFingerprintSHA())) { throw new PdfAsSignatureException("Certificates missmatch!"); } - + return response.getCMSSignature(); } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/RequestPackage.java b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/RequestPackage.java index fccd800d..f08184d2 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/RequestPackage.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/RequestPackage.java @@ -23,12 +23,16 @@ ******************************************************************************/ package at.gv.egiz.sl.util; +import java.util.ArrayList; +import java.util.List; + import at.gv.egiz.sl.schema.CreateCMSSignatureRequestType; public class RequestPackage { private CreateCMSSignatureRequestType requestType; private byte[] signatureData; private int[] byteRange; + private List<BKUHeader> headers = new ArrayList<BKUHeader>(); public CreateCMSSignatureRequestType getRequestType() { return requestType; @@ -48,6 +52,7 @@ public class RequestPackage { public void setByteRange(int[] byteRange) { this.byteRange = byteRange; } - - + public List<BKUHeader> getHeaders() { + return headers; + } } |