From 0b663afa4d0167df1e838e1f37bb5862e8951037 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Wed, 15 Oct 2014 18:07:20 +0200 Subject: PDF-AS Errorhandling redesign only PDFASError Exceptions are returned ! --- .../src/main/java/at/gv/egiz/pdfas/cli/Main.java | 9 ++- .../pdfas/common/exceptions/ErrorConstants.java | 20 +++++ .../egiz/pdfas/common/exceptions/PDFASError.java | 90 ++++++++++++++++++++++ .../pdfas/common/exceptions/PdfAsErrorCarrier.java | 14 ++++ .../pdfas/common/exceptions/SLPdfAsException.java | 26 ++++--- .../pdfas/common/messages/ErrorCodeResolver.java | 67 ++++++++++++++++ .../resources/resources/messages/error.properties | 24 ++++++ .../java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java | 12 ++- .../main/java/at/gv/egiz/pdfas/lib/api/PdfAs.java | 15 ++-- .../at/gv/egiz/pdfas/lib/impl/ErrorExtractor.java | 65 ++++++++++++++++ .../java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java | 88 +++++++++++---------- .../at/gv/egiz/pdfas/lib/util/SignatureUtils.java | 19 +++-- .../egiz/sl/util/ISignatureConnectorSLWrapper.java | 11 ++- .../java/at/gv/egiz/pdfas/moa/MOAConnector.java | 11 ++- 14 files changed, 396 insertions(+), 75 deletions(-) create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/ErrorConstants.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PDFASError.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsErrorCarrier.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/ErrorCodeResolver.java create mode 100644 pdf-as-common/src/main/resources/resources/messages/error.properties create mode 100644 pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/ErrorExtractor.java diff --git a/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/Main.java b/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/Main.java index 222fd33a..3ce215c8 100644 --- a/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/Main.java +++ b/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/Main.java @@ -41,6 +41,7 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.io.IOUtils; +import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.utils.StreamUtils; import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; import at.gv.egiz.pdfas.lib.api.Configuration; @@ -241,8 +242,12 @@ public class Main { System.err.println("Invalid arguments: " + e.getMessage()); usage(); System.exit(-1); - } catch (Exception e) { - System.err.println("PDF-AS Error: " + e.getMessage()); + } catch (PDFASError e) { + System.err.println("PDF-AS Error: [" + e.getCode() + "]" + e.getMessage()); + e.printStackTrace(System.err); + System.exit(-1); + } catch (Throwable e) { + System.err.println("Unhandled PDF-AS Error: " + e.getMessage()); e.printStackTrace(System.err); System.exit(-1); } diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/ErrorConstants.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/ErrorConstants.java new file mode 100644 index 00000000..04314c6c --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/ErrorConstants.java @@ -0,0 +1,20 @@ +package at.gv.egiz.pdfas.common.exceptions; + +public interface ErrorConstants { + // Code below 10000 are reserved for SL Error Codes + + public static final long ERROR_GENERIC = 10000; + public static final long ERROR_NO_INPUT = 10001; + + // Signature Errors + public static final long ERROR_SIG_INVALID_STATUS = 11004; + public static final long ERROR_SIG_INVALID_BKU_SIG = 11008; + public static final long ERROR_SIG_INVALID_PROFILE = 11009; + + public static final long ERROR_SIG_CERTIFICATE_MISSMATCH = 11019; + + // Verification Errors + + // Configuration Errors: + public static final long ERROR_SET_INVALID_SETTINGS_OBJ = 13001; +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PDFASError.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PDFASError.java new file mode 100644 index 00000000..8a6d7379 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PDFASError.java @@ -0,0 +1,90 @@ +package at.gv.egiz.pdfas.common.exceptions; + +import at.gv.egiz.pdfas.common.messages.ErrorCodeResolver; + +/** + * The Class PDFASError. + */ +public class PDFASError extends Exception { + + /** The Constant serialVersionUID. */ + private static final long serialVersionUID = 1233586898708485346L; + + /** The code. */ + private long code; + + /** + * Instantiates a new PDFAS error. + * + * @param code the code + */ + public PDFASError(long code) { + super(ErrorCodeResolver.resolveMessage(code)); + this.code = code; + } + + /** + * Instantiates a new PDFAS error. + * + * @param code the code + * @param e the e + */ + public PDFASError(long code, Throwable e) { + super(ErrorCodeResolver.resolveMessage(code), e); + this.code = code; + } + + /** + * Instantiates a new PDFAS error. + * + * @param code the code + * @param info the info + * @param e the e + */ + public PDFASError(long code, String info, Throwable e) { + super(info, e); + this.code = code; + } + + /** + * Instantiates a new PDFAS error. + * + * @param code the code + * @param info the info + */ + public PDFASError(long code, String info) { + super(info); + this.code = code; + } + + /** + * Gets the code. + * + * @return the code + */ + public long getCode() { + return code; + } + + /** + * Gets the info. + * + * @return the info + */ + public String getInfo() { + return this.getMessage(); + } + + /** + * Gets the code info. + * + * @return the code info + */ + public String getCodeInfo() { + return ErrorCodeResolver.resolveMessage(code); + } + + public static String buildInfoString(long code, Object ... args) { + return String.format(ErrorCodeResolver.resolveMessage(code), args); + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsErrorCarrier.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsErrorCarrier.java new file mode 100644 index 00000000..f5c2fa9d --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsErrorCarrier.java @@ -0,0 +1,14 @@ +package at.gv.egiz.pdfas.common.exceptions; + +public class PdfAsErrorCarrier extends PdfAsException { + + /** + * + */ + private static final long serialVersionUID = 8823547416257994310L; + + public PdfAsErrorCarrier(PDFASError error) { + super("Carrier", error); + } + +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/SLPdfAsException.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/SLPdfAsException.java index 64536ea4..a0ee44d9 100644 --- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/SLPdfAsException.java +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/SLPdfAsException.java @@ -23,7 +23,6 @@ ******************************************************************************/ package at.gv.egiz.pdfas.common.exceptions; - public class SLPdfAsException extends PdfAsException { /** @@ -33,15 +32,22 @@ public class SLPdfAsException extends PdfAsException { private int code; private String info; - + public SLPdfAsException(int code, String info) { - super(); - this.code = code; - this.info = info; - } - - + super(); + this.code = code; + this.info = info; + } + + public int getCode() { + return code; + } + + public String getInfo() { + return info; + } + protected String localizeMessage(String msgId) { - return String.format("%d : %s", code, info); - } + return String.format("%d : %s", code, info); + } } diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/ErrorCodeResolver.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/ErrorCodeResolver.java new file mode 100644 index 00000000..2ae6838e --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/messages/ErrorCodeResolver.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright 2014 by E-Government Innovation Center EGIZ, Graz, Austria + * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a + * joint initiative of the Federal Chancellery Austria and Graz University of + * Technology. + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://www.osor.eu/eupl/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + * + * This product combines work with different licenses. See the "NOTICE" text + * file for details on the various modules and licenses. + * The "NOTICE" text file is part of the distribution. Any derivative works + * that you distribute must include a readable copy of the "NOTICE" text file. + ******************************************************************************/ +package at.gv.egiz.pdfas.common.messages; + +import java.util.Locale; +import java.util.ResourceBundle; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ErrorCodeResolver { + private static final String messageResource = "resources.messages.error"; + private static final String missingMsg = "No Information for code: "; + + private static final Logger logger = LoggerFactory.getLogger(ErrorCodeResolver.class); + + private static ResourceBundle bundle; + + static { + bundle = ResourceBundle.getBundle(messageResource); + if(bundle == null) { + logger.error("Failed to load resource bundle!!"); + System.err.println("Failed to load resource bundle!!"); + } + } + + public static void forceLocale(Locale locale) { + bundle = ResourceBundle.getBundle(messageResource, locale); + } + + public static String resolveMessage(long code) { + String msgId = String.valueOf(code); + if(bundle == null) { + return missingMsg + msgId; + } + + if(bundle.containsKey(msgId)) { + String value = bundle.getString(msgId); + if(value == null) { + return missingMsg + msgId; + } + return value; + } + return missingMsg + msgId; + } +} diff --git a/pdf-as-common/src/main/resources/resources/messages/error.properties b/pdf-as-common/src/main/resources/resources/messages/error.properties new file mode 100644 index 00000000..2bacde27 --- /dev/null +++ b/pdf-as-common/src/main/resources/resources/messages/error.properties @@ -0,0 +1,24 @@ +10000=Generic Error +10001=No Input provided + +11001=Failed to create signature +11002=Failed to open keystore +11003=Failed to start signature process +11004=Invalid PDF-AS status handed over +11005=Failed to continue signature process +11006=Failed to finish signature process +11007=Failed to determine Signature Profile +11008=Signature created by the BKU is not valid +11009=Signature profile %s not available +11010=No signature data available +11011=No data sink available +11012=Document is protected +11013=Invalid Alias for Keystore Entry +11014=Invalid Keystore Type +11015=Keystore password is null +11016=Keyentry password is null +11017=Failed to retrieve certificate +11018=Given Alias contains no private key +11019=Signature was created for wrong certificate + +13001=Invalid Configuration Object \ No newline at end of file diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java index ac76a56b..2412b8cd 100644 --- a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java @@ -115,6 +115,11 @@ public class PdfAsObject implements PdfAs { throw new PdfAsException( ErrorCode.SIGNATURE_COULDNT_BE_CREATED, e.getMessage()); + } catch (at.gv.egiz.pdfas.common.exceptions.PDFASError e) { + e.printStackTrace(); + throw new PdfAsException( + ErrorCode.SIGNATURE_COULDNT_BE_CREATED, e.getMessage()); + } } else { throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, @@ -142,7 +147,7 @@ public class PdfAsObject implements PdfAs { } return new VerifyResultsImpl(resultList); - } catch (at.gv.egiz.pdfas.common.exceptions.PdfAsException e) { + } catch (at.gv.egiz.pdfas.common.exceptions.PDFASError e) { throw new PdfAsException(0, e.getMessage()); } } @@ -264,6 +269,11 @@ public class PdfAsObject implements PdfAs { } catch (CertificateException e) { throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, e.getMessage()); + } catch (at.gv.egiz.pdfas.common.exceptions.PDFASError e) { + e.printStackTrace(); + throw new PdfAsException( + ErrorCode.SIGNATURE_COULDNT_BE_CREATED, e.getMessage()); + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAs.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAs.java index 2ac02a18..1d23c070 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAs.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAs.java @@ -23,11 +23,12 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.api; -import iaik.x509.X509Certificate; +import java.security.cert.X509Certificate; import java.awt.Image; import java.util.List; +import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; import at.gv.egiz.pdfas.lib.api.sign.SignResult; @@ -41,7 +42,7 @@ public interface PdfAs { * @param parameter * @return */ - public SignResult sign(SignParameter parameter) throws PdfAsException; + public SignResult sign(SignParameter parameter) throws PDFASError; /** * Verifies a document with (potentially multiple) PDF-AS signatures. @@ -49,7 +50,7 @@ public interface PdfAs { * @param parameter The verification parameter * @return A list of verification Results */ - public List verify(VerifyParameter parameter) throws PdfAsException; + public List verify(VerifyParameter parameter) throws PDFASError; /** * Gets a copy of the PDF-AS configuration, to allow the application to @@ -68,7 +69,7 @@ public interface PdfAs { * @return A status request * @throws PdfAsException */ - public StatusRequest startSign(SignParameter parameter) throws PdfAsException; + public StatusRequest startSign(SignParameter parameter) throws PDFASError; /** * Continues an ongoing signature process @@ -77,7 +78,7 @@ public interface PdfAs { * @return A status request * @throws PdfAsException */ - public StatusRequest process(StatusRequest statusRequest) throws PdfAsException; + public StatusRequest process(StatusRequest statusRequest) throws PDFASError; /** * Finishes a signature process @@ -86,7 +87,7 @@ public interface PdfAs { * @return A signature result * @throws PdfAsException */ - public SignResult finishSign(StatusRequest statusRequest) throws PdfAsException; + public SignResult finishSign(StatusRequest statusRequest) throws PDFASError; /** * Generates a Image of the visual signatur block as Preview @@ -97,5 +98,5 @@ public interface PdfAs { * @return * @throws PdfAsException */ - public Image generateVisibleSignaturePreview(SignParameter parameter, X509Certificate cert, int resolution) throws PdfAsException; + public Image generateVisibleSignaturePreview(SignParameter parameter, X509Certificate cert, int resolution) throws PDFASError; } 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 new file mode 100644 index 00000000..90a4e9e8 --- /dev/null +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/ErrorExtractor.java @@ -0,0 +1,65 @@ +package at.gv.egiz.pdfas.lib.impl; + +import at.gv.egiz.pdfas.common.exceptions.ErrorConstants; +import at.gv.egiz.pdfas.common.exceptions.PDFASError; +import at.gv.egiz.pdfas.common.exceptions.SLPdfAsException; + +public class ErrorExtractor implements ErrorConstants { + + private static final int MAX_CAUSE_DEPTH = 30; + + private static PDFASError convertPdfAsError(Throwable e) { + if (e instanceof SLPdfAsException) { + SLPdfAsException ex = (SLPdfAsException) e; + if (ex.getInfo() != null) { + return new PDFASError(ex.getCode(), ex.getInfo(), e); + } else { + return new PDFASError(ex.getCode(), e); + } + } // TODO: Handle more exceptions + + return null; + } + + public static PDFASError searchPdfAsError(Throwable e) { + Throwable cur = e; + PDFASError err = null; + + // Search PDFASError + for (int i = 0; i < MAX_CAUSE_DEPTH; i++) { + if (cur instanceof PDFASError) { + err = (PDFASError) cur; + } + if (err != null) { + break; + } + + cur = cur.getCause(); + if (cur == null) { + break; + } + } + cur = e; + // Search other reasons + for (int i = 0; i < MAX_CAUSE_DEPTH; i++) { + + if (cur == null) { + break; + } + + err = convertPdfAsError(cur); + + if (err != null) { + break; + } + + cur = cur.getCause(); + } + + if (err != null) { + return err; + } + + return new PDFASError(ERROR_GENERIC, e); + } +} 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 c853f7eb..a94f63ad 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 @@ -32,7 +32,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.Calendar; import java.util.List; @@ -48,11 +47,10 @@ import org.apache.pdfbox.pdmodel.PDPage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.gv.egiz.pdfas.common.exceptions.PDFIOException; +import at.gv.egiz.pdfas.common.exceptions.ErrorConstants; +import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsException; -import at.gv.egiz.pdfas.common.exceptions.PdfAsSignatureException; -import at.gv.egiz.pdfas.common.exceptions.PdfAsValidationException; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.common.settings.Settings; import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings; @@ -91,7 +89,8 @@ import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction; import at.knowcenter.wag.egov.egiz.pdf.TablePos; import at.knowcenter.wag.egov.egiz.table.Table; -public class PdfAsImpl implements PdfAs, IConfigurationConstants { +public class PdfAsImpl implements PdfAs, IConfigurationConstants, + ErrorConstants { private static final Logger logger = LoggerFactory .getLogger(PdfAsImpl.class); @@ -109,11 +108,10 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { this.settings = cfgObject; } - private void verifySignParameter(SignParameter parameter) - throws PdfAsException { + private void verifySignParameter(SignParameter parameter) throws PDFASError { // Status initialization if (!(parameter.getConfiguration() instanceof ISettings)) { - throw new PdfAsSettingsException("Invalid settings object!"); + throw new PDFASError(ERROR_SET_INVALID_SETTINGS_OBJ); } ISettings settings = (ISettings) parameter.getConfiguration(); @@ -121,30 +119,31 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { String signatureProfile = parameter.getSignatureProfileId(); if (signatureProfile != null) { if (!settings.hasPrefix("sig_obj." + signatureProfile)) { - throw new PdfAsValidationException("error.pdf.sig.09", - signatureProfile); + throw new PDFASError(ERROR_SIG_INVALID_PROFILE, + PDFASError.buildInfoString(ERROR_SIG_INVALID_PROFILE, + signatureProfile)); } } if (parameter.getDataSource() == null) { - throw new PdfAsValidationException("error.pdf.sig.10", null); + throw new PDFASError(ERROR_NO_INPUT); } } private void verifyVerifyParameter(VerifyParameter parameter) - throws PdfAsException { + throws PDFASError { // Status initialization if (!(parameter.getConfiguration() instanceof ISettings)) { - throw new PdfAsSettingsException("Invalid settings object!"); + throw new PDFASError(ERROR_SET_INVALID_SETTINGS_OBJ); } if (parameter.getDataSource() == null) { - throw new PdfAsValidationException("error.pdf.verify.01", null); + throw new PDFASError(ERROR_NO_INPUT); } } - public SignResult sign(SignParameter parameter) throws PdfAsException { + public SignResult sign(SignParameter parameter) throws PDFASError { logger.trace("sign started"); @@ -204,7 +203,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { } catch (Throwable e) { logger.error("Failed to create signature [" + e.getMessage() + "]", e); - throw new PdfAsException("error.pdf.sig.01", e); + throw ErrorExtractor.searchPdfAsError(e); } finally { if (status != null) { status.clear(); @@ -214,7 +213,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { } public List verify(VerifyParameter parameter) - throws PdfAsException { + throws PDFASError { verifyVerifyParameter(parameter); @@ -262,7 +261,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { byte[] inputData = IOUtils.toByteArray(parameter.getDataSource() .getInputStream()); - + for (int i = 0; i < fields.size(); i++) { COSDictionary field = (COSDictionary) fields.getObject(i); String type = field.getNameAsString("FT"); @@ -303,7 +302,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { COSString content = (COSString) dict .getDictionaryObject("Contents"); - + ByteArrayOutputStream contentData = new ByteArrayOutputStream(); for (int j = 0; j < bytes.length; j = j + 2) { int offset = bytes[j]; @@ -339,10 +338,10 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { return result; } catch (IOException e) { logger.error("Failed to verify document", e); - throw new PDFIOException("error.pdf.verify.02", e); + throw ErrorExtractor.searchPdfAsError(e); } catch (PdfAsException e) { logger.error("Failed to verify document", e); - throw new PdfAsException("error.pdf.verify.02", e); + throw ErrorExtractor.searchPdfAsError(e); } finally { if (doc != null) { try { @@ -358,8 +357,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { return new ConfigurationImpl(this.settings); } - public StatusRequest startSign(SignParameter parameter) - throws PdfAsException { + public StatusRequest startSign(SignParameter parameter) throws PDFASError { verifySignParameter(parameter); @@ -386,14 +384,13 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { return request; } catch (Throwable e) { logger.error("startSign", e); - throw new PdfAsException("error.pdf.sig.03", e); + throw ErrorExtractor.searchPdfAsError(e); } } - public StatusRequest process(StatusRequest statusRequest) - throws PdfAsException { + public StatusRequest process(StatusRequest statusRequest) throws PDFASError { if (!(statusRequest instanceof StatusRequestImpl)) { - throw new PdfAsException("error.pdf.sig.04"); + throw new PDFASError(ERROR_SIG_INVALID_STATUS); } StatusRequestImpl request = (StatusRequestImpl) statusRequest; @@ -446,7 +443,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { } catch (Throwable e) { logger.error("process", e); - throw new PdfAsException("error.pdf.sig.05", e); + throw ErrorExtractor.searchPdfAsError(e); } } else if (request.needSignature()) { request.setNeedSignature(false); @@ -467,7 +464,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { if (!StreamUtils.dataCompare(requestedSignature.getCertificate() .getFingerprintSHA(), ((X509Certificate) verifyResult .getSignerCertificate()).getFingerprintSHA())) { - throw new PdfAsSignatureException("Certificates missmatch!"); + throw new PDFASError(ERROR_SIG_CERTIFICATE_MISSMATCH); } for (int i = 0; i < pdfSignature.length; i++) { @@ -475,29 +472,29 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { } request.setIsReady(true); } else { - throw new PdfAsException("error.pdf.sig.04"); + throw new PDFASError(ERROR_SIG_INVALID_STATUS); } return request; } - public SignResult finishSign(StatusRequest statusRequest) - throws PdfAsException { + public SignResult finishSign(StatusRequest statusRequest) throws PDFASError { if (!(statusRequest instanceof StatusRequestImpl)) { - throw new PdfAsException("error.pdf.sig.04"); + throw new PDFASError(ERROR_SIG_INVALID_STATUS); } StatusRequestImpl request = (StatusRequestImpl) statusRequest; OperationStatus status = request.getStatus(); if (!request.isReady()) { - throw new PdfAsException("error.pdf.sig.04"); + throw new PDFASError(ERROR_SIG_INVALID_STATUS); } try { return createSignResult(status); } catch (IOException e) { - throw new PdfAsException("error.pdf.sig.06", e); + // new PdfAsException("error.pdf.sig.06", e); + throw ErrorExtractor.searchPdfAsError(e); } finally { if (status != null) { status.clear(); @@ -521,21 +518,27 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { } public Image generateVisibleSignaturePreview(SignParameter parameter, - X509Certificate cert, int resolution) throws PdfAsException { + java.security.cert.X509Certificate cert, int resolution) throws PDFASError { OperationStatus status = null; try { // Status initialization if (!(parameter.getConfiguration() instanceof ISettings)) { - throw new PdfAsSettingsException("Invalid settings object!"); + throw new PDFASError(ERROR_SET_INVALID_SETTINGS_OBJ); } - + X509Certificate iaikCert; + if(!(cert instanceof X509Certificate)) { + iaikCert = new X509Certificate(cert.getEncoded()); + } else { + iaikCert = (X509Certificate)cert; + } + ISettings settings = (ISettings) parameter.getConfiguration(); status = new OperationStatus(settings, parameter); RequestedSignature requestedSignature = new RequestedSignature( status); - requestedSignature.setCertificate(cert); + requestedSignature.setCertificate(iaikCert); if (!requestedSignature.isVisual()) { logger.warn("Profile is invisible so not block image is generated"); @@ -550,7 +553,8 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { origDoc.save(baos); baos.close(); - pdfObject.setOriginalDocument(new ByteArrayDataSource(baos.toByteArray())); + pdfObject.setOriginalDocument(new ByteArrayDataSource(baos + .toByteArray())); SignatureProfileSettings signatureProfileSettings = TableFactory .createProfile(requestedSignature.getSignatureProfileID(), @@ -630,10 +634,10 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants { return cutOut; } catch (PdfAsException e) { logger.error("PDF-AS Exception", e); - throw e; + throw ErrorExtractor.searchPdfAsError(e); } catch (Throwable e) { logger.error("Throwable Exception", e); - throw new PdfAsException("", e); + throw ErrorExtractor.searchPdfAsError(e); } } 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 608818f9..3b992e46 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 @@ -16,11 +16,12 @@ import org.apache.pdfbox.pdmodel.PDDocument; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.gv.egiz.pdfas.common.exceptions.PdfAsSignatureException; +import at.gv.egiz.pdfas.common.exceptions.ErrorConstants; +import at.gv.egiz.pdfas.common.exceptions.PDFASError; import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; import at.gv.egiz.pdfas.lib.impl.verify.VerifyResultImpl; -public class SignatureUtils { +public class SignatureUtils implements ErrorConstants { private static final Logger logger = LoggerFactory .getLogger(SignatureUtils.class); @@ -68,7 +69,7 @@ public class SignatureUtils { } - public static VerifyResult verifySignature(byte[] signature, byte[] input) throws PdfAsSignatureException { + public static VerifyResult verifySignature(byte[] signature, byte[] input) throws PDFASError { //List results = new ArrayList(); try { SignedData signedData = new SignedData(new ByteArrayInputStream( @@ -79,11 +80,13 @@ public class SignatureUtils { // get the signer infos SignerInfo[] signerInfos = signedData.getSignerInfos(); if (signerInfos.length == 0) { - throw new PdfAsSignatureException("Invalid Signature (no signer info created!)", null); + logger.error("Invalid signature (no signer information)"); + throw new PDFASError(ERROR_SIG_INVALID_BKU_SIG); } if (signerInfos.length != 1) { - throw new PdfAsSignatureException("Invalid Signature (multiple signer infos found!)", null); + logger.error("Invalid signature (multiple signer information)"); + throw new PDFASError(ERROR_SIG_INVALID_BKU_SIG); } // verify the signatures //for (int i = 0; i < signerInfos.length; i++) { @@ -114,15 +117,15 @@ public class SignatureUtils { verifyResult.setSignerCertificate(signedData .getCertificate(signerInfos[0] .getSignerIdentifier())); - throw new PdfAsSignatureException("error.pdf.sig.08", ex); + throw new PDFASError(ERROR_SIG_INVALID_BKU_SIG, ex); } return verifyResult; //} } catch (CMSException e) { - throw new PdfAsSignatureException("error.pdf.sig.08", e); + throw new PDFASError(ERROR_SIG_INVALID_BKU_SIG, e); } catch (IOException e) { - throw new PdfAsSignatureException("error.pdf.sig.08", e); + throw new PDFASError(ERROR_SIG_INVALID_BKU_SIG, e); } 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 82dc0602..19dc3d76 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 @@ -27,17 +27,17 @@ import iaik.x509.X509Certificate; import java.security.cert.CertificateException; import java.util.Iterator; -import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.gv.egiz.pdfas.common.exceptions.PDFASError; +import at.gv.egiz.pdfas.common.exceptions.PdfAsErrorCarrier; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.exceptions.PdfAsSignatureException; import at.gv.egiz.pdfas.common.utils.StreamUtils; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; import at.gv.egiz.pdfas.lib.api.verify.VerifyResult; -import at.gv.egiz.pdfas.lib.impl.SignResultImpl; import at.gv.egiz.pdfas.lib.impl.status.RequestedSignature; import at.gv.egiz.pdfas.lib.util.SignatureUtils; import at.gv.egiz.sl.schema.CreateCMSSignatureResponseType; @@ -90,7 +90,12 @@ public class ISignatureConnectorSLWrapper implements ISignatureConnector { CreateCMSSignatureResponseType response = connector .sendCMSRequest(pack, parameter); - VerifyResult verifyResult = SignatureUtils.verifySignature(response.getCMSSignature(), input); + VerifyResult verifyResult; + try { + verifyResult = SignatureUtils.verifySignature(response.getCMSSignature(), input); + } catch (PDFASError e) { + throw new PdfAsErrorCarrier(e); + } if(!StreamUtils.dataCompare(requestedSignature.getCertificate().getFingerprintSHA(), ((X509Certificate)verifyResult.getSignerCertificate()).getFingerprintSHA())) { diff --git a/pdf-as-moa/src/main/java/at/gv/egiz/pdfas/moa/MOAConnector.java b/pdf-as-moa/src/main/java/at/gv/egiz/pdfas/moa/MOAConnector.java index 405c02a6..d79320e3 100644 --- a/pdf-as-moa/src/main/java/at/gv/egiz/pdfas/moa/MOAConnector.java +++ b/pdf-as-moa/src/main/java/at/gv/egiz/pdfas/moa/MOAConnector.java @@ -49,6 +49,8 @@ import at.gv.e_government.reference.namespace.moa._20020822.MetaInfoType; import at.gv.e_government.reference.namespace.moa._20020822_.MOAFault; import at.gv.e_government.reference.namespace.moa._20020822_.SignatureCreationPortType; import at.gv.e_government.reference.namespace.moa._20020822_.SignatureCreationService; +import at.gv.egiz.pdfas.common.exceptions.PDFASError; +import at.gv.egiz.pdfas.common.exceptions.PdfAsErrorCarrier; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.exceptions.PdfAsMOAException; import at.gv.egiz.pdfas.common.exceptions.PdfAsSignatureException; @@ -194,8 +196,13 @@ public class MOAConnector implements ISignatureConnector, // done the signature! byte[] cmsSignatureData = (byte[])resp; - VerifyResult verifyResult = SignatureUtils - .verifySignature(cmsSignatureData, input); + VerifyResult verifyResult; + try { + verifyResult = SignatureUtils + .verifySignature(cmsSignatureData, input); + } catch (PDFASError e) { + throw new PdfAsErrorCarrier(e); + } if (!StreamUtils.dataCompare(requestedSignature .getCertificate().getFingerprintSHA(), -- cgit v1.2.3