Search in sources :

Example 11 with CanonicalizationException

use of org.apache.xml.security.c14n.CanonicalizationException in project santuario-java by apache.

the class Reference method getContentsAfterTransformation.

private XMLSignatureInput getContentsAfterTransformation(XMLSignatureInput input, OutputStream os) throws XMLSignatureException {
    try {
        Transforms transforms = this.getTransforms();
        XMLSignatureInput output = null;
        if (transforms != null) {
            output = transforms.performTransforms(input, os);
            // new XMLSignatureInput(output.getBytes());
            this.transformsOutput = output;
        // this.transformsOutput.setSourceURI(output.getSourceURI());
        } else {
            output = input;
        }
        return output;
    } catch (ResourceResolverException ex) {
        throw new XMLSignatureException(ex);
    } catch (CanonicalizationException ex) {
        throw new XMLSignatureException(ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new XMLSignatureException(ex);
    } catch (TransformationException ex) {
        throw new XMLSignatureException(ex);
    } catch (XMLSecurityException ex) {
        throw new XMLSignatureException(ex);
    }
}
Also used : TransformationException(org.apache.xml.security.transforms.TransformationException) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) InvalidCanonicalizerException(org.apache.xml.security.c14n.InvalidCanonicalizerException) Transforms(org.apache.xml.security.transforms.Transforms) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 12 with CanonicalizationException

use of org.apache.xml.security.c14n.CanonicalizationException in project santuario-java by apache.

the class Canonicalizer20010315Test method testRelativeNSbehaviour.

/**
 * Note: This specification supports the recent XML plenary decision to
 * deprecate relative namespace URIs as follows: implementations of XML
 * canonicalization MUST report an operation failure on documents containing
 * relative namespace URIs. XML canonicalization MUST NOT be implemented
 * with an XML parser that converts relative URIs to absolute URIs.
 *
 * Implementations MUST report an operation failure on documents containing
 * relative namespace URIs.
 *
 * @throws CanonicalizationException
 * @throws FileNotFoundException
 * @throws IOException
 * @throws InvalidCanonicalizerException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
@org.junit.Test
public void testRelativeNSbehaviour() throws IOException, FileNotFoundException, SAXException, ParserConfigurationException, CanonicalizationException, InvalidCanonicalizerException, TransformerException {
    // J-
    String inputStr = "" + "<absolute:correct xmlns:absolute='http://www.absolute.org/#likeVodka'>" + "<relative:incorrect xmlns:relative='../cheating#away'>" + "</relative:incorrect>" + "</absolute:correct>" + "\n" + "";
    // J+
    DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
    Document doc = null;
    try (InputStream is = new ByteArrayInputStream(inputStr.getBytes())) {
        doc = db.parse(is);
    }
    boolean weCatchedTheRelativeNS = false;
    try {
        Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
        c14n.canonicalizeSubtree(doc);
    } catch (CanonicalizationException cex) {
        // if we reach this point - good.
        LOG.debug("We catched the C14nEx, that's good: " + cex.getMessage());
        weCatchedTheRelativeNS = true;
    }
    assertTrue("We did not catch the relative namespace", weCatchedTheRelativeNS);
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) Document(org.w3c.dom.Document) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 13 with CanonicalizationException

use of org.apache.xml.security.c14n.CanonicalizationException in project santuario-java by apache.

the class XPath2NodeFilter method enginePerformTransform.

/**
 * Method enginePerformTransform
 * {@inheritDoc}
 * @param input
 *
 * @throws TransformationException
 */
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws TransformationException {
    try {
        List<NodeList> unionNodes = new ArrayList<>();
        List<NodeList> subtractNodes = new ArrayList<>();
        List<NodeList> intersectNodes = new ArrayList<>();
        Element[] xpathElements = XMLUtils.selectNodes(transformObject.getElement().getFirstChild(), XPath2FilterContainer.XPathFilter2NS, XPath2FilterContainer._TAG_XPATH2);
        if (xpathElements.length == 0) {
            Object[] exArgs = { Transforms.TRANSFORM_XPATH2FILTER, "XPath" };
            throw new TransformationException("xml.WrongContent", exArgs);
        }
        Document inputDoc = null;
        if (input.getSubNode() != null) {
            inputDoc = XMLUtils.getOwnerDocument(input.getSubNode());
        } else {
            inputDoc = XMLUtils.getOwnerDocument(input.getNodeSet());
        }
        for (int i = 0; i < xpathElements.length; i++) {
            Element xpathElement = xpathElements[i];
            XPath2FilterContainer xpathContainer = XPath2FilterContainer.newInstance(xpathElement, input.getSourceURI());
            String str = XMLUtils.getStrFromNode(xpathContainer.getXPathFilterTextNode());
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPathAPI xpathAPIInstance = xpathFactory.newXPathAPI();
            NodeList subtreeRoots = xpathAPIInstance.selectNodeList(inputDoc, xpathContainer.getXPathFilterTextNode(), str, xpathContainer.getElement());
            if (xpathContainer.isIntersect()) {
                intersectNodes.add(subtreeRoots);
            } else if (xpathContainer.isSubtract()) {
                subtractNodes.add(subtreeRoots);
            } else if (xpathContainer.isUnion()) {
                unionNodes.add(subtreeRoots);
            }
        }
        input.addNodeFilter(new XPath2NodeFilter(unionNodes, subtractNodes, intersectNodes));
        input.setNodeSet(true);
        return input;
    } catch (TransformerException ex) {
        throw new TransformationException(ex);
    } catch (DOMException ex) {
        throw new TransformationException(ex);
    } catch (CanonicalizationException ex) {
        throw new TransformationException(ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new TransformationException(ex);
    } catch (XMLSecurityException ex) {
        throw new TransformationException(ex);
    } catch (SAXException ex) {
        throw new TransformationException(ex);
    } catch (IOException ex) {
        throw new TransformationException(ex);
    } catch (ParserConfigurationException ex) {
        throw new TransformationException(ex);
    }
}
Also used : TransformationException(org.apache.xml.security.transforms.TransformationException) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) XPathAPI(org.apache.xml.security.utils.XPathAPI) SAXException(org.xml.sax.SAXException) XPathFactory(org.apache.xml.security.utils.XPathFactory) DOMException(org.w3c.dom.DOMException) InvalidCanonicalizerException(org.apache.xml.security.c14n.InvalidCanonicalizerException) XPath2FilterContainer(org.apache.xml.security.transforms.params.XPath2FilterContainer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 14 with CanonicalizationException

use of org.apache.xml.security.c14n.CanonicalizationException in project santuario-java by apache.

the class TransformC14NExclusiveWithComments method enginePerformTransform.

protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws CanonicalizationException {
    try {
        String inclusiveNamespaces = null;
        if (transformObject.length(InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES) == 1) {
            Element inclusiveElement = XMLUtils.selectNode(transformObject.getElement().getFirstChild(), InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES, 0);
            inclusiveNamespaces = new InclusiveNamespaces(inclusiveElement, transformObject.getBaseURI()).getInclusiveNamespaces();
        }
        Canonicalizer20010315ExclWithComments c14n = new Canonicalizer20010315ExclWithComments();
        c14n.setSecureValidation(secureValidation);
        if (os != null) {
            c14n.setWriter(os);
        }
        byte[] result = c14n.engineCanonicalize(input, inclusiveNamespaces);
        XMLSignatureInput output = new XMLSignatureInput(result);
        output.setSecureValidation(secureValidation);
        return output;
    } catch (XMLSecurityException ex) {
        throw new CanonicalizationException(ex);
    }
}
Also used : Canonicalizer20010315ExclWithComments(org.apache.xml.security.c14n.implementations.Canonicalizer20010315ExclWithComments) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) Element(org.w3c.dom.Element) InclusiveNamespaces(org.apache.xml.security.transforms.params.InclusiveNamespaces) XMLSignatureInput(org.apache.xml.security.signature.XMLSignatureInput) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 15 with CanonicalizationException

use of org.apache.xml.security.c14n.CanonicalizationException in project santuario-java by apache.

the class Canonicalizer20010315Excl method outputAttributes.

/**
 * {@inheritDoc}
 */
@Override
protected void outputAttributes(Element element, NameSpaceSymbTable ns, Map<String, byte[]> cache) throws CanonicalizationException, DOMException, IOException {
    // result will contain the attrs which have to be output
    SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
    // The prefix visibly utilized (in the attribute or in the name) in
    // the element
    Set<String> visiblyUtilized = null;
    // It's the output selected.
    boolean isOutputElement = isVisibleDO(element, ns.getLevel()) == 1;
    if (isOutputElement) {
        visiblyUtilized = new TreeSet<String>();
        if (inclusiveNSSet != null && !inclusiveNSSet.isEmpty()) {
            visiblyUtilized.addAll(inclusiveNSSet);
        }
    }
    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();
        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NName = attribute.getLocalName();
            String NNodeValue = attribute.getNodeValue();
            if (!XMLNS_URI.equals(attribute.getNamespaceURI())) {
                if (isVisible(attribute) && isOutputElement) {
                    // The Element is output element, add the prefix (if used)
                    // to visibyUtilized
                    String prefix = attribute.getPrefix();
                    if (prefix != null && !(prefix.equals(XML) || prefix.equals(XMLNS))) {
                        visiblyUtilized.add(prefix);
                    }
                    // Add to the result.
                    result.add(attribute);
                }
            } else if (isOutputElement && !isVisible(attribute) && !XMLNS.equals(NName)) {
                ns.removeMappingIfNotRender(NName);
            } else {
                if (!isOutputElement && isVisible(attribute) && inclusiveNSSet.contains(NName) && !ns.removeMappingIfRender(NName)) {
                    Node n = ns.addMappingAndRender(NName, NNodeValue, attribute);
                    if (n != null) {
                        result.add((Attr) n);
                        if (C14nHelper.namespaceIsRelative(attribute)) {
                            Object[] exArgs = { element.getTagName(), NName, attribute.getNodeValue() };
                            throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs);
                        }
                    }
                }
                if (ns.addMapping(NName, NNodeValue, attribute) && C14nHelper.namespaceIsRelative(NNodeValue)) {
                    // New definition check if it is relative
                    Object[] exArgs = { element.getTagName(), NName, attribute.getNodeValue() };
                    throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs);
                }
            }
        }
    }
    if (isOutputElement) {
        // The element is visible, handle the xmlns definition
        Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS);
        if (xmlns != null && !isVisible(xmlns)) {
            // There is a definition but the xmlns is not selected by the
            // xpath. then xmlns=""
            ns.addMapping(XMLNS, "", getNullNode(xmlns.getOwnerDocument()));
        }
        String prefix = null;
        if (element.getNamespaceURI() != null && !(element.getPrefix() == null || element.getPrefix().length() == 0)) {
            prefix = element.getPrefix();
        } else {
            prefix = XMLNS;
        }
        visiblyUtilized.add(prefix);
        for (String s : visiblyUtilized) {
            Attr key = ns.getMapping(s);
            if (key != null) {
                result.add(key);
            }
        }
    }
    OutputStream writer = getWriter();
    // we output all Attrs which are available
    for (Attr attr : result) {
        outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeSet(java.util.TreeSet) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) Node(org.w3c.dom.Node) OutputStream(java.io.OutputStream) Attr(org.w3c.dom.Attr)

Aggregations

CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)18 IOException (java.io.IOException)6 OutputStream (java.io.OutputStream)6 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)6 Attr (org.w3c.dom.Attr)5 Element (org.w3c.dom.Element)5 NamedNodeMap (org.w3c.dom.NamedNodeMap)5 Node (org.w3c.dom.Node)5 TreeSet (java.util.TreeSet)4 InvalidCanonicalizerException (org.apache.xml.security.c14n.InvalidCanonicalizerException)4 XMLSignatureInput (org.apache.xml.security.signature.XMLSignatureInput)4 TransformationException (org.apache.xml.security.transforms.TransformationException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 UnsyncByteArrayOutputStream (org.apache.xml.security.utils.UnsyncByteArrayOutputStream)3 ResourceResolverException (org.apache.xml.security.utils.resolver.ResourceResolverException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Transforms (org.apache.xml.security.transforms.Transforms)2 InclusiveNamespaces (org.apache.xml.security.transforms.params.InclusiveNamespaces)2