diff options
Diffstat (limited to 'moaSig/moa-sig-lib/src/main/java/at/gv/egovernment')
3 files changed, 289 insertions, 302 deletions
diff --git a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/VerifyXMLSignatureResponseBuilder.java b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/VerifyXMLSignatureResponseBuilder.java index 27a42c8..bc949fa 100644 --- a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/VerifyXMLSignatureResponseBuilder.java +++ b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/VerifyXMLSignatureResponseBuilder.java @@ -21,7 +21,6 @@ * 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; @@ -35,7 +34,7 @@ import org.w3c.dom.NodeList; import at.gv.egovernment.moa.util.Base64Utils; import at.gv.egovernment.moa.util.Constants; - +import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.spss.MOAApplicationException; import at.gv.egovernment.moa.spss.MOASystemException; import at.gv.egovernment.moa.spss.api.common.Content; @@ -46,312 +45,285 @@ import at.gv.egovernment.moa.spss.api.xmlverify.AdESFormResults; import at.gv.egovernment.moa.spss.api.xmlverify.ManifestRefsCheckResult; import at.gv.egovernment.moa.spss.api.xmlverify.ReferencesCheckResult; import at.gv.egovernment.moa.spss.api.xmlverify.VerifyXMLSignatureResponse; +import at.gv.egovernment.moa.spss.server.config.ConfigurationException; +import at.gv.egovernment.moa.spss.server.config.ConfigurationProvider; /** - * Convert a <code>VerifyXMLSignatureResponse</code> API object into its - * XML representation, according to the MOA XML schema. + * Convert a <code>VerifyXMLSignatureResponse</code> API object into its XML + * representation, according to the MOA XML schema. * * @author Patrick Peck * @version $Id$ */ public class VerifyXMLSignatureResponseBuilder { - private static final String MOA_NS_URI = Constants.MOA_NS_URI; - - /** The XML document containing the response element. */ - private Document responseDoc; - /** The response <code>VerifyXMLSignatureResponse</code> DOM element. */ - private Element responseElem; - - /** - * Create a new <code>VerifyXMLSignatureResponseBuilder</code>: - * - * @throws MOASystemException An error occurred setting up the resulting - * XML document. - */ - public VerifyXMLSignatureResponseBuilder() throws MOASystemException { - responseDoc = - ResponseBuilderUtils.createResponse("VerifyXMLSignatureResponse"); - responseElem = responseDoc.getDocumentElement(); - } - - /** - * Build a document containing a <code>VerifyXMLSignatureResponse</code> - * DOM element being the XML representation of the given - * <code>VerifyXMLSignatureResponse</code> API object. - * - * @param response The <code>VerifyXMLSignatureResponse</code> to convert - * to XML. - * @return A document containing the <code>VerifyXMLSignatureResponse</code> - * DOM element. - * @throws MOAApplicationException An error occurred building the response. - */ - public Document build(VerifyXMLSignatureResponse response) - throws MOAApplicationException { - - Iterator iter; - List responseData; - - // add the SignerInfo - ResponseBuilderUtils.addSignerInfo( - responseDoc, - responseElem, - response.getSignerInfo().getSignerCertificate(), - response.getSignerInfo().isQualifiedCertificate(), - response.getSignerInfo().getQCSource(), - response.getSignerInfo().isPublicAuthority(), - response.getSignerInfo().getPublicAuhtorityID(), - response.getSignerInfo().isSSCD(), - response.getSignerInfo().getSSCDSource(), - response.getSignerInfo().getIssuerCountryCode()); - - // add HashInputData elements - responseData = response.getHashInputDatas(); - if (responseData != null && !responseData.isEmpty()) { - for (iter = responseData.iterator(); iter.hasNext();) { - InputData inputData = (InputData) iter.next(); - addContent("HashInputData", inputData); - } - } - - // add ReferenceInputData elements - responseData = response.getReferenceInputDatas(); - if (responseData != null && !responseData.isEmpty()) { - for (iter = responseData.iterator(); iter.hasNext();) { - InputData inputData = (InputData) iter.next(); - addContent("ReferenceInputData", inputData); - } - } - - // add the SignatureCheck - addReferencesCheckResult("SignatureCheck", response.getSignatureCheck()); - - // add the SignatureManifestCheck - if (response.getSignatureManifestCheck() != null) { - addReferencesCheckResult( - "SignatureManifestCheck", - response.getSignatureManifestCheck()); - } - - // add the XMLDsigManifestChecks - responseData = response.getXMLDsigManifestChecks(); - if (responseData != null && !responseData.isEmpty()) { - for (iter = responseData.iterator(); iter.hasNext();) { - ManifestRefsCheckResult checkResult = - (ManifestRefsCheckResult) iter.next(); - addManifestRefsCheckResult("XMLDSIGManifestCheck", checkResult); - } - } - - // add the CertificateCheck - ResponseBuilderUtils.addCodeInfoElement( - responseDoc, - responseElem, - "CertificateCheck", - response.getCertificateCheck().getCode(), - response.getCertificateCheck().getInfo()); - - - if(response.getAdESFormResults() != null) { - - Iterator formIterator = response.getAdESFormResults().iterator(); - - while(formIterator.hasNext()) { - AdESFormResults adESFormResult = (AdESFormResults)formIterator.next(); - // add the CertificateCheck - ResponseBuilderUtils.addFormCheckElement( - responseDoc, - responseElem, - "FormCheckResult", - adESFormResult.getCode().intValue(), - adESFormResult.getName()); - - } - } - - return responseDoc; - } - - /** - * Add an element of type <code>ContentBaseType</code> to the response. - * - * @param elementName The name of the element. - * - * @param inputData The <code>InputData</code> to add. Based on the type of - * - * the <code>InputData</code>, either a <code>Base64Content</code> element - * or a <code>XMLContent</code> subelement will be added. An <code> - * InputDataBinaryImpl</code> will be added as a <code>Base64Content</code> - * child element. An<code>InputDataXMLImpl</code> will be added as <code> - * XMLContent</code> child element. - * - * @throws MOAApplicationException An error occurred adding the content. - */ - private void addContent(String elementName, InputData inputData) - throws MOAApplicationException { - - Element contentElem = responseDoc.createElementNS(MOA_NS_URI, elementName); - - contentElem.setAttributeNS(null, "PartOf", inputData.getPartOf()); - if (inputData.getReferringReferenceNumber() != InputData.REFERER_NONE_) - contentElem.setAttributeNS( - null, - "ReferringSigReference", - Integer.toString(inputData.getReferringReferenceNumber())); - - switch (inputData.getContentType()) { - case Content.XML_CONTENT : - ContentXML contentXml = (ContentXML) inputData; - NodeList nodes = contentXml.getXMLContent(); - Element xmlElem; - int i; - - xmlElem = responseDoc.createElementNS(MOA_NS_URI, "XMLContent"); - //xmlElem.setAttributeNS(XML_NS_URI, "xml:space", "preserve"); - xmlElem.setAttribute("xml:space", "preserve"); - - for (i = 0; i < nodes.getLength(); i++) { - xmlElem.appendChild(responseDoc.importNode(nodes.item(i), true)); - } - contentElem.appendChild(xmlElem); - responseElem.appendChild(contentElem); - break; - case Content.BINARY_CONTENT : - Element binaryElem = - responseDoc.createElementNS(MOA_NS_URI, "Base64Content"); - ContentBinary contentBinary = (ContentBinary) inputData; - String base64Str; - - try { - base64Str = Base64Utils.encode(contentBinary.getBinaryContent()); - } catch (IOException e) { - throw new MOAApplicationException("2200", null, e); - } - binaryElem.appendChild(responseDoc.createTextNode(base64Str)); - contentElem.appendChild(binaryElem); - responseElem.appendChild(contentElem); - break; - } - } - - /** - * Add a <code>ReferencesCheckResult</code> to the response. - * - * @param elementName The DOM element name to use. - * @param checkResult The <code>ReferencesCheckResult</code> to add. - */ - private void addReferencesCheckResult( - String elementName, - ReferencesCheckResult checkResult) { - - NodeList info = null; - - if (checkResult.getInfo() != null) { - DocumentFragment fragment = responseDoc.createDocumentFragment(); - NodeList anyOtherInfo = checkResult.getInfo().getAnyOtherInfo(); - int[] failedReferences = checkResult.getInfo().getFailedReferences(); - - if (anyOtherInfo != null) { - addAnyOtherInfo(fragment, checkResult.getInfo().getAnyOtherInfo()); - } - - if (failedReferences != null) { - addFailedReferences(fragment, failedReferences); - } - - info = fragment.getChildNodes(); - } - - ResponseBuilderUtils.addCodeInfoElement( - responseDoc, - responseElem, - elementName, - checkResult.getCode(), - info); - } - - - /** - * Add a <code>ManifestRefsCheckResult</code> to the response. - * - * @param elementName The DOM element name to use. - * @param checkResult The <code>ManifestRefsCheckResult</code> to add. - */ - private void addManifestRefsCheckResult( - String elementName, - ManifestRefsCheckResult checkResult) { - - DocumentFragment fragment = responseDoc.createDocumentFragment(); - NodeList anyOtherInfo = checkResult.getInfo().getAnyOtherInfo(); - int[] failedReferences = checkResult.getInfo().getFailedReferences(); - Element referringSigRefElem; - String referringSigRefStr; - - // add any other elements - if (anyOtherInfo != null) { - addAnyOtherInfo(fragment, checkResult.getInfo().getAnyOtherInfo()); - } - - // add the failed references - if (failedReferences != null) { - addFailedReferences(fragment, failedReferences); - } - - // add the ReferringSigReference - referringSigRefElem = - responseDoc.createElementNS(MOA_NS_URI, "ReferringSigReference"); - referringSigRefStr = - Integer.toString(checkResult.getInfo().getReferringSignatureReference()); - referringSigRefElem.appendChild( - responseDoc.createTextNode(referringSigRefStr)); - fragment.appendChild(referringSigRefElem); - - // add XMLDSIGManifestCheckResult to the response - ResponseBuilderUtils.addCodeInfoElement( - responseDoc, - responseElem, - elementName, - checkResult.getCode(), - fragment.getChildNodes()); - } - - /** - * Add arbitrary XML content to a DOM <code>DocumentFragment</code>. - * - * @param fragment The fragment to add the XML content to. - * @param anyOtherInfo The XML content to add. - */ - private void addAnyOtherInfo( - DocumentFragment fragment, - NodeList anyOtherInfo) { - - int i; - - for (i = 0; i < anyOtherInfo.getLength(); i++) { - fragment.appendChild(responseDoc.importNode(anyOtherInfo.item(i), true)); - } - } - - /** - * Add the failed references as <code>FailedReference</code> DOM elements to - * the fragment. - * - * @param fragment The DOM document fragment to add the - * <code>FailedReference</code> elements to. - * @param failedReferences The indexes of the failed references. - */ - private void addFailedReferences( - DocumentFragment fragment, - int[] failedReferences) { - Element failedReferenceElem; - int i; - - for (i = 0; i < failedReferences.length; i++) { - failedReferenceElem = - responseDoc.createElementNS(MOA_NS_URI, "FailedReference"); - failedReferenceElem.appendChild( - responseDoc.createTextNode(Integer.toString(failedReferences[i]))); - fragment.appendChild(failedReferenceElem); - } - } + private static final String MOA_NS_URI = Constants.MOA_NS_URI; + + /** The XML document containing the response element. */ + private Document responseDoc; + /** The response <code>VerifyXMLSignatureResponse</code> DOM element. */ + private Element responseElem; + + /** + * Create a new <code>VerifyXMLSignatureResponseBuilder</code>: + * + * @throws MOASystemException + * An error occurred setting up the resulting XML document. + */ + public VerifyXMLSignatureResponseBuilder() throws MOASystemException { + responseDoc = ResponseBuilderUtils.createResponse("VerifyXMLSignatureResponse"); + responseElem = responseDoc.getDocumentElement(); + } + + /** + * Build a document containing a <code>VerifyXMLSignatureResponse</code> DOM + * element being the XML representation of the given + * <code>VerifyXMLSignatureResponse</code> API object. + * + * @param response + * The <code>VerifyXMLSignatureResponse</code> to convert to XML. + * @return A document containing the <code>VerifyXMLSignatureResponse</code> + * DOM element. + * @throws MOAApplicationException + * An error occurred building the response. + */ + public Document build(VerifyXMLSignatureResponse response) throws MOAApplicationException { + + Iterator iter; + List responseData; + + // add the SignerInfo + ResponseBuilderUtils.addSignerInfo(responseDoc, responseElem, response.getSignerInfo().getSignerCertificate(), + response.getSignerInfo().isQualifiedCertificate(), response.getSignerInfo().getQCSource(), + response.getSignerInfo().isPublicAuthority(), response.getSignerInfo().getPublicAuhtorityID(), + response.getSignerInfo().isSSCD(), response.getSignerInfo().getSSCDSource(), + response.getSignerInfo().getIssuerCountryCode()); + + // add HashInputData elements + responseData = response.getHashInputDatas(); + if (responseData != null && !responseData.isEmpty()) { + for (iter = responseData.iterator(); iter.hasNext();) { + InputData inputData = (InputData) iter.next(); + addContent("HashInputData", inputData); + } + } + + // add ReferenceInputData elements + responseData = response.getReferenceInputDatas(); + if (responseData != null && !responseData.isEmpty()) { + for (iter = responseData.iterator(); iter.hasNext();) { + InputData inputData = (InputData) iter.next(); + addContent("ReferenceInputData", inputData); + } + } + + // add the SignatureCheck + addReferencesCheckResult("SignatureCheck", response.getSignatureCheck()); + + // add the SignatureManifestCheck + if (response.getSignatureManifestCheck() != null) { + addReferencesCheckResult("SignatureManifestCheck", response.getSignatureManifestCheck()); + } + + // add the XMLDsigManifestChecks + responseData = response.getXMLDsigManifestChecks(); + if (responseData != null && !responseData.isEmpty()) { + for (iter = responseData.iterator(); iter.hasNext();) { + ManifestRefsCheckResult checkResult = (ManifestRefsCheckResult) iter.next(); + addManifestRefsCheckResult("XMLDSIGManifestCheck", checkResult); + } + } + + // add the CertificateCheck + ResponseBuilderUtils.addCodeInfoElement(responseDoc, responseElem, "CertificateCheck", + response.getCertificateCheck().getCode(), response.getCertificateCheck().getInfo()); + + try { + if (ConfigurationProvider.getInstance().getAdesFormResults() && response.getAdESFormResults() != null) { + + Iterator formIterator = response.getAdESFormResults().iterator(); + + while (formIterator.hasNext()) { + AdESFormResults adESFormResult = (AdESFormResults) formIterator.next(); + // add the CertificateCheck + ResponseBuilderUtils.addFormCheckElement(responseDoc, responseElem, "FormCheckResult", + adESFormResult.getCode().intValue(), adESFormResult.getName()); + + } + } + } catch (ConfigurationException e) { + Logger.warn("Failed to access configuration to determine if we should return AdES Form Results"); + } + + return responseDoc; + } + + /** + * Add an element of type <code>ContentBaseType</code> to the response. + * + * @param elementName + * The name of the element. + * + * @param inputData + * The <code>InputData</code> to add. Based on the type of + * + * the <code>InputData</code>, either a + * <code>Base64Content</code> element or a + * <code>XMLContent</code> subelement will be added. An <code> + * InputDataBinaryImpl</code> will be added as a <code>Base64Content</code> + * child element. An<code>InputDataXMLImpl</code> will be added + * as <code> + * XMLContent</code> child element. + * + * @throws MOAApplicationException + * An error occurred adding the content. + */ + private void addContent(String elementName, InputData inputData) throws MOAApplicationException { + + Element contentElem = responseDoc.createElementNS(MOA_NS_URI, elementName); + + contentElem.setAttributeNS(null, "PartOf", inputData.getPartOf()); + if (inputData.getReferringReferenceNumber() != InputData.REFERER_NONE_) + contentElem.setAttributeNS(null, "ReferringSigReference", + Integer.toString(inputData.getReferringReferenceNumber())); + + switch (inputData.getContentType()) { + case Content.XML_CONTENT: + ContentXML contentXml = (ContentXML) inputData; + NodeList nodes = contentXml.getXMLContent(); + Element xmlElem; + int i; + + xmlElem = responseDoc.createElementNS(MOA_NS_URI, "XMLContent"); + // xmlElem.setAttributeNS(XML_NS_URI, "xml:space", "preserve"); + xmlElem.setAttribute("xml:space", "preserve"); + + for (i = 0; i < nodes.getLength(); i++) { + xmlElem.appendChild(responseDoc.importNode(nodes.item(i), true)); + } + contentElem.appendChild(xmlElem); + responseElem.appendChild(contentElem); + break; + case Content.BINARY_CONTENT: + Element binaryElem = responseDoc.createElementNS(MOA_NS_URI, "Base64Content"); + ContentBinary contentBinary = (ContentBinary) inputData; + String base64Str; + + try { + base64Str = Base64Utils.encode(contentBinary.getBinaryContent()); + } catch (IOException e) { + throw new MOAApplicationException("2200", null, e); + } + binaryElem.appendChild(responseDoc.createTextNode(base64Str)); + contentElem.appendChild(binaryElem); + responseElem.appendChild(contentElem); + break; + } + } + + /** + * Add a <code>ReferencesCheckResult</code> to the response. + * + * @param elementName + * The DOM element name to use. + * @param checkResult + * The <code>ReferencesCheckResult</code> to add. + */ + private void addReferencesCheckResult(String elementName, ReferencesCheckResult checkResult) { + + NodeList info = null; + + if (checkResult.getInfo() != null) { + DocumentFragment fragment = responseDoc.createDocumentFragment(); + NodeList anyOtherInfo = checkResult.getInfo().getAnyOtherInfo(); + int[] failedReferences = checkResult.getInfo().getFailedReferences(); + + if (anyOtherInfo != null) { + addAnyOtherInfo(fragment, checkResult.getInfo().getAnyOtherInfo()); + } + + if (failedReferences != null) { + addFailedReferences(fragment, failedReferences); + } + + info = fragment.getChildNodes(); + } + + ResponseBuilderUtils.addCodeInfoElement(responseDoc, responseElem, elementName, checkResult.getCode(), info); + } + + /** + * Add a <code>ManifestRefsCheckResult</code> to the response. + * + * @param elementName + * The DOM element name to use. + * @param checkResult + * The <code>ManifestRefsCheckResult</code> to add. + */ + private void addManifestRefsCheckResult(String elementName, ManifestRefsCheckResult checkResult) { + + DocumentFragment fragment = responseDoc.createDocumentFragment(); + NodeList anyOtherInfo = checkResult.getInfo().getAnyOtherInfo(); + int[] failedReferences = checkResult.getInfo().getFailedReferences(); + Element referringSigRefElem; + String referringSigRefStr; + + // add any other elements + if (anyOtherInfo != null) { + addAnyOtherInfo(fragment, checkResult.getInfo().getAnyOtherInfo()); + } + + // add the failed references + if (failedReferences != null) { + addFailedReferences(fragment, failedReferences); + } + + // add the ReferringSigReference + referringSigRefElem = responseDoc.createElementNS(MOA_NS_URI, "ReferringSigReference"); + referringSigRefStr = Integer.toString(checkResult.getInfo().getReferringSignatureReference()); + referringSigRefElem.appendChild(responseDoc.createTextNode(referringSigRefStr)); + fragment.appendChild(referringSigRefElem); + + // add XMLDSIGManifestCheckResult to the response + ResponseBuilderUtils.addCodeInfoElement(responseDoc, responseElem, elementName, checkResult.getCode(), + fragment.getChildNodes()); + } + + /** + * Add arbitrary XML content to a DOM <code>DocumentFragment</code>. + * + * @param fragment + * The fragment to add the XML content to. + * @param anyOtherInfo + * The XML content to add. + */ + private void addAnyOtherInfo(DocumentFragment fragment, NodeList anyOtherInfo) { + + int i; + + for (i = 0; i < anyOtherInfo.getLength(); i++) { + fragment.appendChild(responseDoc.importNode(anyOtherInfo.item(i), true)); + } + } + + /** + * Add the failed references as <code>FailedReference</code> DOM elements to + * the fragment. + * + * @param fragment + * The DOM document fragment to add the + * <code>FailedReference</code> elements to. + * @param failedReferences + * The indexes of the failed references. + */ + private void addFailedReferences(DocumentFragment fragment, int[] failedReferences) { + Element failedReferenceElem; + int i; + + for (i = 0; i < failedReferences.length; i++) { + failedReferenceElem = responseDoc.createElementNS(MOA_NS_URI, "FailedReference"); + failedReferenceElem.appendChild(responseDoc.createTextNode(Integer.toString(failedReferences[i]))); + fragment.appendChild(failedReferenceElem); + } + } } diff --git a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/config/ConfigurationPartsBuilder.java b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/config/ConfigurationPartsBuilder.java index cb840ae..6bc6f0b 100644 --- a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/config/ConfigurationPartsBuilder.java +++ b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/config/ConfigurationPartsBuilder.java @@ -104,6 +104,9 @@ public class ConfigurationPartsBuilder { private static final String PDFAS_CONFIGURATION_XPATH = ROOT + CONF + "Common/" + CONF + "PDFASConfig"; + private static final String FORMRESULT_CONFIGURATION_XPATH = + ROOT + CONF + "Common/" + CONF + "AdESFormResult"; + private static final String DIGEST_METHOD_XPATH = ROOT + CONF + "SignatureCreation/" + CONF + "XMLDSig/" @@ -405,6 +408,11 @@ public class ConfigurationPartsBuilder { return pdfasConfiguration; } + public boolean getAdesFormResult() + { + String enableArchiving = getElementValue(getConfigElem(), FORMRESULT_CONFIGURATION_XPATH, null); + return Boolean.valueOf(enableArchiving).booleanValue(); + } /** * Returns the canonicalization algorithm name. diff --git a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/config/ConfigurationProvider.java b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/config/ConfigurationProvider.java index 6c1a192..578f2fd 100644 --- a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/config/ConfigurationProvider.java +++ b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/config/ConfigurationProvider.java @@ -227,6 +227,8 @@ public class ConfigurationProvider */ private String[] serviceOrder_; + private boolean adesFormResults; + /** * Indicates whether certificates found during certificate path construction * should be added to the certificate store. @@ -375,6 +377,7 @@ public class ConfigurationProvider builder.buildKeyGroupMappings(keyGroups, ANONYMOUS_ISSUER_SERIAL); pdfAsConfiguration = builder.getPDFASConfiguration(); + adesFormResults = builder.getAdesFormResult(); xadesVersion = builder.getXAdESVersion(); defaultChainingMode = builder.getDefaultChainingMode(); chainingModes = builder.buildChainingModes(); @@ -553,6 +556,10 @@ public class ConfigurationProvider return pdfAsConfiguration; } + public boolean getAdesFormResults() { + return this.adesFormResults; + } + public boolean getAllowExternalUris() { return this.allowExternalUris_; } |