Search in sources :

Example 1 with CanonicalizationException

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

the class Canonicalizer20010315 method outputAttributes.

/**
 * Output the Attr[]s for the given element.
 * <br>
 * IMPORTANT: This method expects to work on a modified DOM tree, i.e. a DOM which has
 * been prepared using {@link org.apache.xml.security.utils.XMLUtils#circumventBug2650(
 * org.w3c.dom.Document)}.
 *
 * @param element
 * @param ns
 * @param cache
 * @throws CanonicalizationException, DOMException, IOException
 */
@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
    xmlattrStack.push(ns.getLevel());
    boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1;
    SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
    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 NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();
            if (!XMLNS_URI.equals(NUri)) {
                // A non namespace definition node.
                if (XML_LANG_URI.equals(NUri)) {
                    if (c14n11 && "id".equals(NName)) {
                        if (isRealVisible) {
                            // treat xml:id like any other attribute
                            // (emit it, but don't inherit it)
                            result.add(attribute);
                        }
                    } else {
                        xmlattrStack.addXmlnsAttr(attribute);
                    }
                } else if (isRealVisible) {
                    // The node is visible add the attribute to the list of output attributes.
                    result.add(attribute);
                }
            } else if (!XML.equals(NName) || !XML_LANG_URI.equals(NValue)) {
                // add the prefix binding to the ns symb table.
                if (isVisible(attribute)) {
                    if (isRealVisible || !ns.removeMappingIfRender(NName)) {
                        // The xpath select this node output it if needed.
                        Node n = ns.addMappingAndRender(NName, NValue, 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);
                            }
                        }
                    }
                } else {
                    if (isRealVisible && !XMLNS.equals(NName)) {
                        ns.removeMapping(NName);
                    } else {
                        ns.addMapping(NName, NValue, attribute);
                    }
                }
            }
        }
    }
    if (isRealVisible) {
        // The element is visible, handle the xmlns definition
        Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS);
        Node n = null;
        if (xmlns == null) {
            // No xmlns def just get the already defined.
            n = ns.getMapping(XMLNS);
        } else if (!isVisible(xmlns)) {
            // There is a definition but the xmlns is not selected by the xpath.
            // then xmlns=""
            n = ns.addMappingAndRender(XMLNS, "", getNullNode(xmlns.getOwnerDocument()));
        }
        // output the xmlns def if needed.
        if (n != null) {
            result.add((Attr) n);
        }
        // Float all xml:* attributes of the unselected parent elements to this one.
        xmlattrStack.getXmlnsAttr(result);
        ns.getUnrenderedNodes(result);
    }
    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)

Example 2 with CanonicalizationException

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

the class Canonicalizer20010315 method outputAttributesSubtree.

/**
 * Output the Attr[]s for the given element.
 * <br>
 * The code of this method is a copy of {@link #outputAttributes(Element,
 * NameSpaceSymbTable, Map<String, byte[]>)},
 * whereas it takes into account that subtree-c14n is -- well -- subtree-based.
 * So if the element in question isRoot of c14n, it's parent is not in the
 * node set, as well as all other ancestors.
 *
 * @param element
 * @param ns
 * @param cache
 * @throws CanonicalizationException, DOMException, IOException
 */
@Override
protected void outputAttributesSubtree(Element element, NameSpaceSymbTable ns, Map<String, byte[]> cache) throws CanonicalizationException, DOMException, IOException {
    if (!element.hasAttributes() && !firstCall) {
        return;
    }
    // result will contain the attrs which have to be output
    SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
    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 NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();
            if (!XMLNS_URI.equals(NUri)) {
                // It's not a namespace attr node. Add to the result and continue.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
                // The default mapping for xml must not be output.
                Node n = ns.addMappingAndRender(NName, NValue, attribute);
                if (n != null) {
                    // Render the ns definition
                    result.add((Attr) n);
                    if (C14nHelper.namespaceIsRelative(attribute)) {
                        Object[] exArgs = { element.getTagName(), NName, attribute.getNodeValue() };
                        throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs);
                    }
                }
            }
        }
    }
    if (firstCall) {
        // It is the first node of the subtree
        // Obtain all the namespaces defined in the parents, and added to the output.
        ns.getUnrenderedNodes(result);
        // output the attributes in the xml namespace.
        xmlattrStack.getXmlnsAttr(result);
        firstCall = false;
    }
    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)

Example 3 with CanonicalizationException

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

the class C14nHelper method checkForRelativeNamespace.

/**
 * This method throws a CanonicalizationException if the supplied Element
 * contains any relative namespaces.
 *
 * @param ctxNode
 * @throws CanonicalizationException
 * @see C14nHelper#assertNotRelativeNS(Attr)
 */
public static void checkForRelativeNamespace(Element ctxNode) throws CanonicalizationException {
    if (ctxNode != null) {
        NamedNodeMap attributes = ctxNode.getAttributes();
        int length = attributes.getLength();
        for (int i = 0; i < length; i++) {
            C14nHelper.assertNotRelativeNS((Attr) attributes.item(i));
        }
    } else {
        throw new CanonicalizationException("Called checkForRelativeNamespace() on null");
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException)

Example 4 with CanonicalizationException

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

the class Canonicalizer20010315Excl method outputAttributesSubtree.

@Override
protected void outputAttributesSubtree(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
    SortedSet<String> 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())) {
                // Not a namespace definition.
                // The Element is output element, add the prefix (if used) to
                // visiblyUtilized
                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 (!(XML.equals(NName) && XML_LANG_URI.equals(NNodeValue)) && ns.addMapping(NName, NNodeValue, attribute) && C14nHelper.namespaceIsRelative(NNodeValue)) {
                // The default mapping for xml must not be output.
                // New definition check if it is relative.
                Object[] exArgs = { element.getTagName(), NName, attribute.getNodeValue() };
                throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs);
            }
        }
    }
    if (propagateDefaultNamespace && ns.getLevel() == 1 && inclusiveNSSet.contains(XMLNS) && ns.getMappingWithoutRendered(XMLNS) == null) {
        ns.removeMapping(XMLNS);
        ns.addMapping(XMLNS, "", getNullNode(element.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) OutputStream(java.io.OutputStream) Attr(org.w3c.dom.Attr)

Example 5 with CanonicalizationException

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

the class CanonicalizerBase method engineCanonicalizeSubTree.

/**
 * Canonicalizes a Subtree node.
 *
 * @param rootNode
 *            the root of the subtree to canonicalize
 * @param excludeNode
 *            a node to be excluded from the canonicalize operation
 * @return The canonicalize stream.
 * @throws CanonicalizationException
 */
protected byte[] engineCanonicalizeSubTree(Node rootNode, Node excludeNode) throws CanonicalizationException {
    this.excludeNode = excludeNode;
    try {
        NameSpaceSymbTable ns = new NameSpaceSymbTable();
        int nodeLevel = NODE_BEFORE_DOCUMENT_ELEMENT;
        if (rootNode != null && Node.ELEMENT_NODE == rootNode.getNodeType()) {
            // Fills the nssymbtable with the definitions of the parent of the root subnode
            getParentNameSpaces((Element) rootNode, ns);
            nodeLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
        }
        this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel);
        this.writer.flush();
        if (this.writer instanceof ByteArrayOutputStream) {
            byte[] result = ((ByteArrayOutputStream) this.writer).toByteArray();
            if (reset) {
                ((ByteArrayOutputStream) this.writer).reset();
            } else {
                this.writer.close();
            }
            return result;
        } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
            byte[] result = ((UnsyncByteArrayOutputStream) this.writer).toByteArray();
            if (reset) {
                ((UnsyncByteArrayOutputStream) this.writer).reset();
            } else {
                this.writer.close();
            }
            return result;
        } else {
            this.writer.close();
        }
        return null;
    } catch (UnsupportedEncodingException ex) {
        throw new CanonicalizationException(ex);
    } catch (IOException ex) {
        throw new CanonicalizationException(ex);
    }
}
Also used : UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) IOException(java.io.IOException)

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