Search in sources :

Example 1 with AlgorithmMethod

use of javax.xml.crypto.AlgorithmMethod in project camel by apache.

the class XmlSignatureTest method getTransformsXsltXpath.

private List<AlgorithmMethod> getTransformsXsltXpath() {
    try {
        AlgorithmMethod transformXslt = XmlSignatureHelper.getXslTransform("/org/apache/camel/component/xmlsecurity/xslt_test.xsl");
        Map<String, String> namespaceMap = new HashMap<String, String>(1);
        namespaceMap.put("n0", "https://org.apache/camel/xmlsecurity/test");
        AlgorithmMethod transformXpath = XmlSignatureHelper.getXPathTransform("//n0:XMLSecurity/n0:Content", namespaceMap);
        // I removed base 64 transform because the JDK implementation does
        // not correctly support this transformation
        // AlgorithmMethod transformBase64 = helper.getBase64Transform();
        List<AlgorithmMethod> result = new ArrayList<AlgorithmMethod>(3);
        result.add(XmlSignatureHelper.getCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE));
        result.add(transformXslt);
        result.add(transformXpath);
        // result.add(transformBase64);
        return result;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AlgorithmMethod(javax.xml.crypto.AlgorithmMethod) XPathExpressionException(javax.xml.xpath.XPathExpressionException) XmlSignatureInvalidValueException(org.apache.camel.component.xmlsecurity.api.XmlSignatureInvalidValueException) KeyException(java.security.KeyException) XmlSignatureInvalidContentHashException(org.apache.camel.component.xmlsecurity.api.XmlSignatureInvalidContentHashException) XmlSignatureInvalidException(org.apache.camel.component.xmlsecurity.api.XmlSignatureInvalidException) XmlSignatureFormatException(org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException) KeySelectorException(javax.xml.crypto.KeySelectorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SchemaValidationException(org.apache.camel.processor.validation.SchemaValidationException) XmlSignatureException(org.apache.camel.component.xmlsecurity.api.XmlSignatureException) XmlSignatureInvalidKeyException(org.apache.camel.component.xmlsecurity.api.XmlSignatureInvalidKeyException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with AlgorithmMethod

use of javax.xml.crypto.AlgorithmMethod in project camel by apache.

the class XmlSignerProcessor method getTransforms.

private List<Transform> getTransforms(XMLSignatureFactory fac, SignatureType sigType, Message message) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    String transformMethodsHeaderValue = message.getHeader(XmlSignatureConstants.HEADER_TRANSFORM_METHODS, String.class);
    if (transformMethodsHeaderValue == null) {
        List<AlgorithmMethod> configuredTrafos = getConfiguration().getTransformMethods();
        if (SignatureType.enveloped == sigType) {
            // add enveloped transform if necessary
            if (configuredTrafos.size() > 0) {
                if (!containsEnvelopedTransform(configuredTrafos)) {
                    configuredTrafos = new ArrayList<AlgorithmMethod>(configuredTrafos.size() + 1);
                    configuredTrafos.add(XmlSignatureHelper.getEnvelopedTransform());
                    configuredTrafos.addAll(getConfiguration().getTransformMethods());
                }
            } else {
                // add enveloped and C14N trafo
                configuredTrafos = new ArrayList<AlgorithmMethod>(2);
                configuredTrafos.add(XmlSignatureHelper.getEnvelopedTransform());
                configuredTrafos.add(XmlSignatureHelper.getCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE));
            }
        }
        List<Transform> transforms = new ArrayList<Transform>(configuredTrafos.size());
        for (AlgorithmMethod trafo : configuredTrafos) {
            Transform transform = fac.newTransform(trafo.getAlgorithm(), (TransformParameterSpec) trafo.getParameterSpec());
            transforms.add(transform);
            LOG.debug("Transform method: {}", trafo.getAlgorithm());
        }
        return transforms;
    } else {
        LOG.debug("Header {} with value '{}' found", XmlSignatureConstants.HEADER_TRANSFORM_METHODS, transformMethodsHeaderValue);
        String[] transformAlgorithms = transformMethodsHeaderValue.split(",");
        List<Transform> transforms = new ArrayList<Transform>(transformAlgorithms.length);
        for (String transformAlgorithm : transformAlgorithms) {
            transformAlgorithm = transformAlgorithm.trim();
            Transform transform = fac.newTransform(transformAlgorithm, (TransformParameterSpec) null);
            transforms.add(transform);
            LOG.debug("Transform method: {}", transformAlgorithm);
        }
        return transforms;
    }
}
Also used : ArrayList(java.util.ArrayList) AlgorithmMethod(javax.xml.crypto.AlgorithmMethod) Transform(javax.xml.crypto.dsig.Transform)

Example 3 with AlgorithmMethod

use of javax.xml.crypto.AlgorithmMethod in project camel by apache.

the class XmlSignatureTest method getTransformsXPath2.

private List<AlgorithmMethod> getTransformsXPath2() {
    List<XPathAndFilter> list = new ArrayList<XPathAndFilter>(3);
    XPathAndFilter xpath1 = new XPathAndFilter("//n0:ToBeSigned", XPathType.Filter.INTERSECT.toString());
    list.add(xpath1);
    XPathAndFilter xpath2 = new XPathAndFilter("//n0:NotToBeSigned", XPathType.Filter.SUBTRACT.toString());
    list.add(xpath2);
    XPathAndFilter xpath3 = new XPathAndFilter("//n0:ReallyToBeSigned", XPathType.Filter.UNION.toString());
    list.add(xpath3);
    List<AlgorithmMethod> result = new ArrayList<AlgorithmMethod>(2);
    result.add(XmlSignatureHelper.getCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE));
    result.add(XmlSignatureHelper.getXPath2Transform(list, getNamespaceMap()));
    return result;
}
Also used : XPathAndFilter(org.apache.camel.component.xmlsecurity.api.XmlSignatureHelper.XPathAndFilter) ArrayList(java.util.ArrayList) AlgorithmMethod(javax.xml.crypto.AlgorithmMethod)

Example 4 with AlgorithmMethod

use of javax.xml.crypto.AlgorithmMethod in project wildfly by wildfly.

the class TestServlet method validateSignature.

private static boolean validateSignature(final Document document, final PublicKey publicKey) throws Exception {
    final KeySelector ks = new KeySelector() {

        @Override
        public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException {
            return new KeySelectorResult() {

                public Key getKey() {
                    return publicKey;
                }
            };
        }
    };
    final DOMValidateContext context = new DOMValidateContext(ks, document.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature").item(0));
    return XMLSignatureFactory.getInstance("DOM").unmarshalXMLSignature(context).validate(context);
}
Also used : KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo) DOMValidateContext(javax.xml.crypto.dsig.dom.DOMValidateContext) XMLCryptoContext(javax.xml.crypto.XMLCryptoContext) AlgorithmMethod(javax.xml.crypto.AlgorithmMethod) KeySelectorResult(javax.xml.crypto.KeySelectorResult) KeySelector(javax.xml.crypto.KeySelector)

Aggregations

AlgorithmMethod (javax.xml.crypto.AlgorithmMethod)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 KeyException (java.security.KeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 KeySelector (javax.xml.crypto.KeySelector)1 KeySelectorException (javax.xml.crypto.KeySelectorException)1 KeySelectorResult (javax.xml.crypto.KeySelectorResult)1 XMLCryptoContext (javax.xml.crypto.XMLCryptoContext)1 Transform (javax.xml.crypto.dsig.Transform)1 DOMValidateContext (javax.xml.crypto.dsig.dom.DOMValidateContext)1 KeyInfo (javax.xml.crypto.dsig.keyinfo.KeyInfo)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 XmlSignatureException (org.apache.camel.component.xmlsecurity.api.XmlSignatureException)1 XmlSignatureFormatException (org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException)1 XPathAndFilter (org.apache.camel.component.xmlsecurity.api.XmlSignatureHelper.XPathAndFilter)1 XmlSignatureInvalidContentHashException (org.apache.camel.component.xmlsecurity.api.XmlSignatureInvalidContentHashException)1