aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/DeveloperMain.java4
-rw-r--r--pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/Main.java5
-rw-r--r--pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsMOAException.java29
-rw-r--r--pdf-as-common/src/main/resources/resources/messages/common.properties3
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/PdfAsImpl.java13
-rw-r--r--pdf-as-lib/src/main/java/at/gv/egiz/sl/util/MOAConnector.java102
-rw-r--r--pdf-as-lib/src/main/resources/config/config.zipbin1040620 -> 1039811 bytes
7 files changed, 132 insertions, 24 deletions
diff --git a/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/DeveloperMain.java b/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/DeveloperMain.java
index 808d0b80..2c8114a8 100644
--- a/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/DeveloperMain.java
+++ b/pdf-as-cli/src/main/java/at/gv/egiz/pdfas/cli/DeveloperMain.java
@@ -47,8 +47,8 @@ public class DeveloperMain {
parameter.setSignatureProfileId("AMTSSIGNATURBLOCK_DE");
parameter.setOutput(bads);
//parameter.setPlainSigner(new PAdESSigner(new BKUSLConnector(config)));
- parameter.setPlainSigner(signer);
- //parameter.setPlainSigner(new PAdESSigner(new MOAConnector(config)));
+ //parameter.setPlainSigner(signer);
+ parameter.setPlainSigner(new PAdESSigner(new MOAConnector(config)));
/*
StatusRequest request = pdfas.startSign(parameter);
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 578175a8..db655268 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
@@ -186,8 +186,7 @@ public class Main {
}
if (mode == ModeOfOperation.INVALID && !cli.hasOption(CLI_ARG_DEPLOY_SHORT)) {
- throw new ParseException("Missing required option: "
- + CLI_ARG_MODE_SHORT);
+ usage();
} else if (mode == ModeOfOperation.SIGN) {
perform_sign(cli);
} else if (mode == ModeOfOperation.VERIFY) {
@@ -284,7 +283,7 @@ public class Main {
ByteArrayDataSink dataSink = new ByteArrayDataSink();
PdfAs pdfAs = null;
-
+
pdfAs = PdfAsFactory.createPdfAs(new File(configurationFile));
Configuration configuration = pdfAs.getConfiguration();
diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsMOAException.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsMOAException.java
new file mode 100644
index 00000000..7350680d
--- /dev/null
+++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/exceptions/PdfAsMOAException.java
@@ -0,0 +1,29 @@
+package at.gv.egiz.pdfas.common.exceptions;
+
+import at.gv.egiz.pdfas.common.messages.MessageResolver;
+
+public class PdfAsMOAException extends PdfAsException {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -217112433494784615L;
+
+ private String faultCode;
+ private String faultString;
+ private String errorResponse;
+ private String errorCode;
+
+ public PdfAsMOAException(String faultCode, String faultString, String errorResponse, String errorCode) {
+ super("error.pdf.io.06");
+ this.faultCode = faultCode;
+ this.faultString = faultString;
+ this.errorResponse = errorResponse;
+ this.errorCode = errorCode;
+ }
+
+ @Override
+ protected String localizeMessage(String msgId) {
+ return String.format(MessageResolver.resolveMessage(msgId), errorResponse, errorCode, faultCode, faultString);
+ }
+}
diff --git a/pdf-as-common/src/main/resources/resources/messages/common.properties b/pdf-as-common/src/main/resources/resources/messages/common.properties
index d4c58888..dadef742 100644
--- a/pdf-as-common/src/main/resources/resources/messages/common.properties
+++ b/pdf-as-common/src/main/resources/resources/messages/common.properties
@@ -7,6 +7,9 @@ error.pdf.io.02=Failed to write PDF Document
error.pdf.io.03=Security Layer communication error
error.pdf.io.04=Failed to write PDF Document
error.pdf.io.05=Security Layer element not available
+error.pdf.io.06=MOA-SS Error %s [%s] [%s [%s]]
+error.pdf.io.07=No CMS Signature in MOA SS Response
+error.pdf.io.08=MOA-SS Error communication error
#PDF Stamper Errors
error.pdf.stamp.01=Failed to load true type font
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 5f9d2b03..48d15b4d 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
@@ -139,11 +139,12 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants {
public List<VerifyResult> verify(VerifyParameter parameter)
throws PdfAsException {
+ PDDocument doc = null;
try {
List<VerifyResult> result = new ArrayList<VerifyResult>();
ISettings settings = (ISettings) parameter.getConfiguration();
VerifierDispatcher verifier = new VerifierDispatcher(settings);
- PDDocument doc = PDDocument.load(new ByteArrayInputStream(parameter
+ doc = PDDocument.load(new ByteArrayInputStream(parameter
.getDataSource().getByteData()));
COSDictionary trailer = doc.getDocument().getTrailer();
@@ -210,6 +211,14 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants {
} catch (PdfAsException e) {
logger.error("Failed to verify document", e);
throw new PdfAsException("error.pdf.verify.02", e);
+ } finally {
+ if(doc != null) {
+ try {
+ doc.close();
+ } catch (IOException e) {
+ logger.info("Failed to close doc");
+ }
+ }
}
}
@@ -236,7 +245,7 @@ public class PdfAsImpl implements PdfAs, IConfigurationConstants {
status);
status.setRequestedSignature(requestedSignature);
-
+
request.setStatus(status);
request.setNeedCertificate(true);
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/MOAConnector.java b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/MOAConnector.java
index d46f34a3..61b11d29 100644
--- a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/MOAConnector.java
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/MOAConnector.java
@@ -9,9 +9,13 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.StringReader;
import java.security.cert.CertificateException;
import javax.activation.DataHandler;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
import org.apache.axis2.databinding.types.Token;
import org.apache.commons.codec.binary.Base64;
@@ -24,6 +28,11 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
import at.gv.egiz.moa.ByteArrayDataSource;
import at.gv.egiz.moa.SignatureCreationServiceStub;
@@ -39,6 +48,7 @@ import at.gv.egiz.moa.SignatureCreationServiceStub.MimeTypeType;
import at.gv.egiz.moa.SignatureCreationServiceStub.SingleSignatureInfo_type1;
import at.gv.egiz.moa.SignatureCreationServiceStub.Structure_type1;
import at.gv.egiz.pdfas.common.exceptions.PdfAsException;
+import at.gv.egiz.pdfas.common.exceptions.PdfAsMOAException;
import at.gv.egiz.pdfas.common.utils.StreamUtils;
import at.gv.egiz.pdfas.lib.api.Configuration;
@@ -46,6 +56,8 @@ public class MOAConnector implements ISignatureConnector {
private static final Logger logger = LoggerFactory
.getLogger(MOAConnector.class);
+
+ private static final Logger moalogger = LoggerFactory.getLogger("at.knowcenter.wag.egov.egiz.sig.connectors.MOASSRepsonseLogger");
public static final String MOA_SIGN_URL = "moa.sign.url";
public static final String MOA_SIGN_KEY_ID = "moa.sign.KeyIdentifier";
@@ -54,6 +66,12 @@ public class MOAConnector implements ISignatureConnector {
public static final String KEY_ID_PATTERN = "##KEYID##";
public static final String CONTENT_PATTERN = "##CONTENT##";
+ public static final String FAULTCODE = "faultcode";
+ public static final String FAULTSTRING = "faultstring";
+ public static final String ERRORRESPONSE = "ErrorResponse";
+ public static final String ERRORCODE = "ErrorCode";
+ public static final String CMSSIGNATURE = "CMSSignature";
+
public static final String CMS_REQUEST = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://reference.e-government.gv.at/namespace/moa/20020822#\">"
+ "<soapenv:Header/><soapenv:Body><ns:CreateCMSSignatureRequest><ns:KeyIdentifier>"
+ KEY_ID_PATTERN
@@ -72,6 +90,7 @@ public class MOAConnector implements ISignatureConnector {
public MOAConnector(Configuration config) throws CertificateException,
FileNotFoundException, IOException {
+ logger.info("Loading certificate: " + config.getValue(MOA_SIGN_CERTIFICATE));
this.certificate = new X509Certificate(new FileInputStream(new File(
config.getValue(MOA_SIGN_CERTIFICATE))));
this.moaEndpoint = config.getValue(MOA_SIGN_URL);
@@ -104,20 +123,18 @@ public class MOAConnector implements ISignatureConnector {
request = request
.replace(KEY_ID_PATTERN, this.keyIdentifier.trim());
- //SOAPAction: "urn:CreateCMSSignatureAction"
+ // SOAPAction: "urn:CreateCMSSignatureAction"
post.setHeader("SOAPAction", "urn:CreateCMSSignatureAction");
-
+
EntityBuilder entityBuilder = EntityBuilder.create();
-
+
entityBuilder.setContentType(ContentType.TEXT_XML);
entityBuilder.setContentEncoding("UTF-8");
entityBuilder.setText(request);
-
- post.setEntity(entityBuilder.build());
+ post.setEntity(entityBuilder.build());
+ moalogger.debug(">>> " + request);
HttpResponse response = client.execute(post);
- logger.debug("Response Code : "
- + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
@@ -128,27 +145,78 @@ public class MOAConnector implements ISignatureConnector {
result.append(line);
}
- logger.trace(result.toString());
- return new byte[] {};
+ moalogger.debug("<<< " + result.toString());
+
+ DocumentBuilderFactory dbFactory = DocumentBuilderFactory
+ .newInstance();
+ DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
+ Document doc = dBuilder.parse(new InputSource(new StringReader(
+ result.toString())));
+ doc.getDocumentElement().normalize();
+
+ if (response.getStatusLine().getStatusCode() != 200) {
+ String faultCode = "";
+ String faultString = "";
+ String errorResponse = "";
+ String errorCode = "";
+ NodeList nodeList = doc.getElementsByTagName("*");
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Node node = nodeList.item(i);
+ if (node.getNodeType() == Node.ELEMENT_NODE) {
+ if (node.getNodeName().equals(FAULTCODE)) {
+ faultCode = node.getTextContent();
+ } else if (node.getNodeName().equals(FAULTSTRING)) {
+ faultString = node.getTextContent();
+ } else if (node.getNodeName().equals(ERRORCODE)) {
+ errorCode = node.getTextContent();
+ } else if (node.getNodeName().equals(ERRORRESPONSE)) {
+ errorResponse = node.getTextContent();
+ }
+ }
+ }
+ throw new PdfAsMOAException(faultCode, faultString,
+ errorResponse, errorCode);
+ } else {
+ String cmsSignature = null;
+ NodeList nodeList = doc.getElementsByTagName("*");
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Node node = nodeList.item(i);
+ if (node.getNodeType() == Node.ELEMENT_NODE) {
+ if (node.getNodeName().equals(CMSSIGNATURE)) {
+ cmsSignature = node.getTextContent();
+ break;
+ }
+ }
+ }
+
+ if (cmsSignature != null) {
+ try {
+ return base64.decode(cmsSignature);
+ } catch(Exception e) {
+ throw new PdfAsException("error.pdf.io.07", e);
+ }
+ } else {
+ throw new PdfAsException("error.pdf.io.07");
+ }
+ }
} catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ throw new PdfAsException("error.pdf.io.08", e);
} catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ throw new PdfAsException("error.pdf.io.08", e);
+ } catch (SAXException e) {
+ throw new PdfAsException("error.pdf.io.08", e);
+ } catch (ParserConfigurationException e) {
+ throw new PdfAsException("error.pdf.io.08", e);
} finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ logger.warn("Failed to close client", e);
}
}
}
- return new byte[] {};
}
-
/*
* public byte[] sign(byte[] input, int[] byteRange) throws PdfAsException {
* try {
diff --git a/pdf-as-lib/src/main/resources/config/config.zip b/pdf-as-lib/src/main/resources/config/config.zip
index 03b1b4f9..dd8892c2 100644
--- a/pdf-as-lib/src/main/resources/config/config.zip
+++ b/pdf-as-lib/src/main/resources/config/config.zip
Binary files differ