aboutsummaryrefslogtreecommitdiff
path: root/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/ResponseBuilderUtils.java
diff options
context:
space:
mode:
authorAndreas Fitzek <andreas.fitzek@iaik.tugraz.at>2015-11-03 14:38:34 +0100
committerAndreas Fitzek <andreas.fitzek@iaik.tugraz.at>2015-11-03 14:38:34 +0100
commit0872d2d8a64fd701776b272f49222428d8def07f (patch)
tree0954a523ad2cc7ad615dbbae5282dd56497e4c6e /moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/ResponseBuilderUtils.java
parente635718b8d6a12e4e80207c8bdf30b02eed3f2ab (diff)
downloadmoa-sig-0872d2d8a64fd701776b272f49222428d8def07f.tar.gz
moa-sig-0872d2d8a64fd701776b272f49222428d8def07f.tar.bz2
moa-sig-0872d2d8a64fd701776b272f49222428d8def07f.zip
initial commit
Diffstat (limited to 'moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/ResponseBuilderUtils.java')
-rw-r--r--moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/ResponseBuilderUtils.java289
1 files changed, 289 insertions, 0 deletions
diff --git a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/ResponseBuilderUtils.java b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/ResponseBuilderUtils.java
new file mode 100644
index 0000000..b5ec20f
--- /dev/null
+++ b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/ResponseBuilderUtils.java
@@ -0,0 +1,289 @@
+/*
+ * Copyright 2003 Federal Chancellery Austria
+ * MOA-SPSS has been developed in a cooperation between BRZ, the Federal
+ * Chancellery Austria - ICT staff unit, 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.egovernment.moa.spss.api.xmlbind;
+
+import java.io.IOException;
+import java.security.cert.CertificateEncodingException;
+import java.security.cert.X509Certificate;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import iaik.utils.RFC2253NameParser;
+import iaik.utils.RFC2253NameParserException;
+
+import at.gv.egovernment.moa.util.Base64Utils;
+import at.gv.egovernment.moa.util.Constants;
+
+import at.gv.egovernment.moa.spss.MOAApplicationException;
+import at.gv.egovernment.moa.spss.MOASystemException;
+
+/**
+ * Utility methods used by the verious <code>ResponseBuilder</code> classes.
+ *
+ * @author Patrick Peck
+ * @version $Id$
+ */
+public class ResponseBuilderUtils {
+ //
+ // shortcuts to various XML namespace constants
+ //
+ private static final String MOA_NS_URI = Constants.MOA_NS_URI;
+ private static final String DSIG = Constants.DSIG_PREFIX + ":";
+ private static final String DSIG_NS_URI = Constants.DSIG_NS_URI;
+ private static final String XMLNS_NS_URI = Constants.XMLNS_NS_URI;
+
+ /**
+ * Create a response element with all the namespaces set.
+ *
+ * @param responseName The name of the response root element.
+ * @return A DOM document containing the response root element and predefined
+ * MOA, DSIG and XML namespace declarations.
+ * @throws MOASystemException An error building the response document.
+ */
+ public static Document createResponse(String responseName)
+ throws MOASystemException {
+
+ try {
+ DocumentBuilder docBuilder =
+ DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ DOMImplementation impl = docBuilder.getDOMImplementation();
+ Document response;
+ Element root;
+ String attrValue;
+
+ response = impl.createDocument(MOA_NS_URI, responseName, null);
+ root = response.getDocumentElement();
+
+ // add namespace prefix declarations
+ root.setAttributeNS(XMLNS_NS_URI, "xmlns", MOA_NS_URI);
+ attrValue = "xmlns:" + Constants.DSIG_PREFIX;
+ root.setAttributeNS(XMLNS_NS_URI, attrValue, DSIG_NS_URI);
+
+ return response;
+ } catch (ParserConfigurationException e) {
+ throw new MOASystemException("2200", null, e);
+ }
+ }
+
+ /**
+ * Add a <code>SignerInfo</code> element to the response.
+ *
+ * @param response The response document, in order to create new elements in
+ * it.
+ * @param root The root element into which the <code>SignerInfo</code> element
+ * will be inserted.
+ * @param cert The signer certificate to add.
+ * @param isQualified Indicates, whether <code>cert</code> is a qualified
+ * certificate.
+ * @param isPublicAuthority Indicates, whether <code>cert</code> is
+ * certificate owned by a public authority.
+ * @param publicAuthorityID Information about the public authority owning
+ * <code>cert</code>. Must not be <code>null</code>, if
+ * <code>isPublicAuthority ! = null</code>.
+ * @throws MOAApplicationException An error occurred reading data from the
+ * certificate.
+ */
+ public static void addSignerInfo(
+ Document response,
+ Element root,
+ X509Certificate cert,
+ boolean isQualified,
+ String qcSource,
+ boolean isPublicAuthority,
+ String publicAuthorityID,
+ boolean isSSCD,
+ String sscdSource,
+ String issuerCountryCode)
+ throws MOAApplicationException {
+
+ Element signerInfoElem = response.createElementNS(MOA_NS_URI, "SignerInfo");
+ Element x509DataElem =
+ response.createElementNS(DSIG_NS_URI, DSIG + "X509Data");
+ Element x509IssuerSerialElem =
+ response.createElementNS(DSIG_NS_URI, DSIG + "X509IssuerSerial");
+ Element x509IssuerElem =
+ response.createElementNS(DSIG_NS_URI, DSIG + "X509IssuerName");
+ String issuer = cert.getIssuerDN().getName();
+ Element x509SerialNumberElem =
+ response.createElementNS(DSIG_NS_URI, DSIG + "X509SerialNumber");
+ String serialNumber = cert.getSerialNumber().toString();
+ Element x509SubjectNameElem =
+ response.createElementNS(DSIG_NS_URI, DSIG + "X509SubjectName");
+ Element x509CertificateElem =
+ response.createElementNS(DSIG_NS_URI, DSIG + "X509Certificate");
+ Element qualifiedCertificateElem =
+ isQualified
+ ? response.createElementNS(MOA_NS_URI, "QualifiedCertificate")
+ : null;
+ Element sscdElem =
+ isSSCD
+ ? response.createElementNS(MOA_NS_URI, "SecureSignatureCreationDevice")
+ : null;
+ Element issuerCountryCodeElem = null;
+ if (issuerCountryCode != null) {
+ issuerCountryCodeElem = response.createElementNS(MOA_NS_URI, "IssuerCountryCode");
+ issuerCountryCodeElem.setTextContent(issuerCountryCode);
+ }
+
+ Element publicAuthorityElem =
+ isPublicAuthority
+ ? response.createElementNS(MOA_NS_URI, "PublicAuthority")
+ : null;
+ Element codeElem =
+ publicAuthorityID != null
+ ? response.createElementNS(MOA_NS_URI, "Code")
+ : null;
+
+ // fill in text
+ x509IssuerElem.appendChild(response.createTextNode(issuer));
+ x509SerialNumberElem.appendChild(response.createTextNode(serialNumber));
+ try {
+ RFC2253NameParser parser =
+ new RFC2253NameParser(cert.getSubjectDN().getName());
+ String subjectRfc2253 = parser.parse().getRFC2253String();
+ x509SubjectNameElem.appendChild(response.createTextNode(subjectRfc2253));
+ } catch (RFC2253NameParserException e) {
+ x509SubjectNameElem.appendChild(
+ response.createTextNode(cert.getSubjectDN().getName()));
+ }
+ try {
+ x509CertificateElem.appendChild(
+ response.createTextNode(Base64Utils.encode(cert.getEncoded())));
+ } catch (CertificateEncodingException e) {
+ throw new MOAApplicationException("2245", null, e);
+ } catch (IOException e) {
+ throw new MOAApplicationException("2245", null, e);
+ }
+
+ // build structure
+ x509DataElem.appendChild(x509SubjectNameElem);
+ x509IssuerSerialElem.appendChild(x509IssuerElem);
+ x509IssuerSerialElem.appendChild(x509SerialNumberElem);
+ x509DataElem.appendChild(x509IssuerSerialElem);
+ x509DataElem.appendChild(x509CertificateElem);
+ if (isQualified) {
+ if (qcSource.compareToIgnoreCase("TSL") == 0)
+ qualifiedCertificateElem.setAttributeNS(MOA_NS_URI, "Source", qcSource);
+
+ x509DataElem.appendChild(qualifiedCertificateElem);
+ }
+ if (isPublicAuthority) {
+ x509DataElem.appendChild(publicAuthorityElem);
+ if (publicAuthorityID != null) {
+ codeElem.appendChild(response.createTextNode(publicAuthorityID));
+ publicAuthorityElem.appendChild(codeElem);
+ }
+ }
+ if (isSSCD) {
+ sscdElem.setAttributeNS(MOA_NS_URI, "Source", sscdSource);
+ x509DataElem.appendChild(sscdElem);
+ }
+ if (issuerCountryCodeElem != null)
+ x509DataElem.appendChild(issuerCountryCodeElem);
+
+ signerInfoElem.appendChild(x509DataElem);
+ root.appendChild(signerInfoElem);
+ }
+
+ /**
+ * Add an element containing <code>Code</code> and <code>Info</code>
+ * subelements.
+ *
+ * @param response The response document, in order to create new elements in
+ * it.
+ * @param root The root element into which to insert the newly created
+ * element.
+ * @param elementName The name of the newly created element.
+ * @param code The content of the <code>Code</code> subelement.
+ * @param info The content of the <code>Info</code> subelement.
+ */
+ public static void addCodeInfoElement(
+ Document response,
+ Element root,
+ String elementName,
+ int code,
+ NodeList info) {
+
+ Element codeInfoElem = response.createElementNS(MOA_NS_URI, elementName);
+ Element codeElem = response.createElementNS(MOA_NS_URI, "Code");
+ Element infoElem;
+ int i;
+
+ codeElem.appendChild(response.createTextNode(Integer.toString(code)));
+ codeInfoElem.appendChild(codeElem);
+ if (info != null) {
+ infoElem = response.createElementNS(MOA_NS_URI, "Info");
+
+ for (i = 0; i < info.getLength(); i++) {
+ infoElem.appendChild(info.item(i).cloneNode(true));
+ }
+ codeInfoElem.appendChild(infoElem);
+ }
+ root.appendChild(codeInfoElem);
+ }
+
+ /**
+ * Add an element containing <code>Code</code> and <code>Info</code>
+ * subelements.
+ *
+ * @param response The response document, in order to create new elements in
+ * it.
+ * @param root The root element into which to insert the newly created
+ * element.
+ * @param elementName The name of the newly created element.
+ * @param code The content of the <code>Code</code> subelement.
+ * @param info The content of the <code>Info</code> subelement.
+ */
+ public static void addCodeInfoElement(
+ Document response,
+ Element root,
+ String elementName,
+ int code,
+ String info) {
+
+ Element codeInfoElem = response.createElementNS(MOA_NS_URI, elementName);
+ Element codeElem = response.createElementNS(MOA_NS_URI, "Code");
+ Element infoElem;
+ int i;
+
+ codeElem.appendChild(response.createTextNode(Integer.toString(code)));
+ codeInfoElem.appendChild(codeElem);
+
+ if (info != null) {
+ infoElem = response.createElementNS(MOA_NS_URI, "Info");
+ infoElem.appendChild(response.createTextNode(info));
+ codeInfoElem.appendChild(infoElem);
+ }
+ root.appendChild(codeInfoElem);
+ }
+
+}