Search in sources :

Example 1 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class Canonicalizer11 method handleAttributes.

/**
     * Returns the Attr[]s to be output 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 com.sun.org.apache.xml.internal.security.utils.XMLUtils#circumventBug2650(
     * org.w3c.dom.Document)}.
     *
     * @param element
     * @param ns
     * @return the Attr[]s to be output
     * @throws CanonicalizationException
     */
@Override
protected Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns) throws CanonicalizationException {
    // result will contain the attrs which have to be output
    xmlattrStack.push(ns.getLevel());
    boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1;
    final SortedSet<Attr> result = this.result;
    result.clear();
    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 (NName.equals("id")) {
                        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);
    }
    return result.iterator();
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr)

Example 2 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class CanonicalizerBase method canonicalizeXPathNodeSet.

/**
     * Canonicalizes all the nodes included in the currentNode and contained in the
     * xpathNodeSet field.
     *
     * @param currentNode
     * @param endnode
     * @throws CanonicalizationException
     * @throws IOException
     */
protected final void canonicalizeXPathNodeSet(Node currentNode, Node endnode) throws CanonicalizationException, IOException {
    if (isVisibleInt(currentNode) == -1) {
        return;
    }
    boolean currentNodeIsVisible = false;
    NameSpaceSymbTable ns = new NameSpaceSymbTable();
    if (currentNode != null && Node.ELEMENT_NODE == currentNode.getNodeType()) {
        getParentNameSpaces((Element) currentNode, ns);
    }
    if (currentNode == null) {
        return;
    }
    Node sibling = null;
    Node parentNode = null;
    OutputStream writer = this.writer;
    int documentLevel = NODE_BEFORE_DOCUMENT_ELEMENT;
    Map<String, byte[]> cache = new HashMap<String, byte[]>();
    do {
        switch(currentNode.getNodeType()) {
            case Node.ENTITY_NODE:
            case Node.NOTATION_NODE:
            case Node.ATTRIBUTE_NODE:
                // illegal node type during traversal
                throw new CanonicalizationException("empty");
            case Node.DOCUMENT_FRAGMENT_NODE:
            case Node.DOCUMENT_NODE:
                ns.outputNodePush();
                sibling = currentNode.getFirstChild();
                break;
            case Node.COMMENT_NODE:
                if (this.includeComments && (isVisibleDO(currentNode, ns.getLevel()) == 1)) {
                    outputCommentToWriter((Comment) currentNode, writer, documentLevel);
                }
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                if (isVisible(currentNode)) {
                    outputPItoWriter((ProcessingInstruction) currentNode, writer, documentLevel);
                }
                break;
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                if (isVisible(currentNode)) {
                    outputTextToWriter(currentNode.getNodeValue(), writer);
                    for (Node nextSibling = currentNode.getNextSibling(); (nextSibling != null) && ((nextSibling.getNodeType() == Node.TEXT_NODE) || (nextSibling.getNodeType() == Node.CDATA_SECTION_NODE)); nextSibling = nextSibling.getNextSibling()) {
                        outputTextToWriter(nextSibling.getNodeValue(), writer);
                        currentNode = nextSibling;
                        sibling = currentNode.getNextSibling();
                    }
                }
                break;
            case Node.ELEMENT_NODE:
                documentLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
                Element currentElement = (Element) currentNode;
                //Add a level to the nssymbtable. So latter can be pop-back.
                String name = null;
                int i = isVisibleDO(currentNode, ns.getLevel());
                if (i == -1) {
                    sibling = currentNode.getNextSibling();
                    break;
                }
                currentNodeIsVisible = (i == 1);
                if (currentNodeIsVisible) {
                    ns.outputNodePush();
                    writer.write('<');
                    name = currentElement.getTagName();
                    UtfHelpper.writeByte(name, writer, cache);
                } else {
                    ns.push();
                }
                Iterator<Attr> attrs = handleAttributes(currentElement, ns);
                if (attrs != null) {
                    //we output all Attrs which are available
                    while (attrs.hasNext()) {
                        Attr attr = attrs.next();
                        outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);
                    }
                }
                if (currentNodeIsVisible) {
                    writer.write('>');
                }
                sibling = currentNode.getFirstChild();
                if (sibling == null) {
                    if (currentNodeIsVisible) {
                        writer.write(END_TAG.clone());
                        UtfHelpper.writeByte(name, writer, cache);
                        writer.write('>');
                        //We finished with this level, pop to the previous definitions.
                        ns.outputNodePop();
                    } else {
                        ns.pop();
                    }
                    if (parentNode != null) {
                        sibling = currentNode.getNextSibling();
                    }
                } else {
                    parentNode = currentElement;
                }
                break;
            case Node.DOCUMENT_TYPE_NODE:
            default:
                break;
        }
        while (sibling == null && parentNode != null) {
            if (isVisible(parentNode)) {
                writer.write(END_TAG.clone());
                UtfHelpper.writeByte(((Element) parentNode).getTagName(), writer, cache);
                writer.write('>');
                //We finished with this level, pop to the previous definitions.
                ns.outputNodePop();
            } else {
                ns.pop();
            }
            if (parentNode == endnode) {
                return;
            }
            sibling = parentNode.getNextSibling();
            parentNode = parentNode.getParentNode();
            if (parentNode == null || Node.ELEMENT_NODE != parentNode.getNodeType()) {
                parentNode = null;
                documentLevel = NODE_AFTER_DOCUMENT_ELEMENT;
            }
        }
        if (sibling == null) {
            return;
        }
        currentNode = sibling;
        sibling = currentNode.getNextSibling();
    } while (true);
}
Also used : HashMap(java.util.HashMap) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Node(org.w3c.dom.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) UnsyncByteArrayOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Example 3 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class CanonicalizerBase method engineCanonicalizeXPathNodeSetInternal.

private byte[] engineCanonicalizeXPathNodeSetInternal(Node doc) throws CanonicalizationException {
    try {
        this.canonicalizeXPathNodeSet(doc, doc);
        this.writer.flush();
        if (this.writer instanceof ByteArrayOutputStream) {
            byte[] sol = ((ByteArrayOutputStream) this.writer).toByteArray();
            if (reset) {
                ((ByteArrayOutputStream) this.writer).reset();
            } else {
                this.writer.close();
            }
            return sol;
        } 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("empty", ex);
    } catch (IOException ex) {
        throw new CanonicalizationException("empty", ex);
    }
}
Also used : UnsyncByteArrayOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsyncByteArrayOutputStream(com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

the class Canonicalizer20010315Excl method handleAttributes.

/**
     * @inheritDoc
     * @param element
     * @throws CanonicalizationException
     */
@Override
protected final Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns) throws CanonicalizationException {
    // result will contain the attrs which have to be output
    final SortedSet<Attr> result = this.result;
    result.clear();
    // 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);
            }
        }
    }
    return result.iterator();
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr)

Example 5 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException in project jdk8u_jdk by JetBrains.

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("empty", ex);
    } catch (CanonicalizationException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (TransformationException ex) {
        throw new XMLSignatureException("empty", ex);
    } catch (XMLSecurityException ex) {
        throw new XMLSignatureException("empty", ex);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException) Transforms(com.sun.org.apache.xml.internal.security.transforms.Transforms) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Aggregations

CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)17 Attr (org.w3c.dom.Attr)9 Node (org.w3c.dom.Node)7 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)6 IOException (java.io.IOException)6 NamedNodeMap (org.w3c.dom.NamedNodeMap)6 Element (org.w3c.dom.Element)5 InvalidCanonicalizerException (com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)4 TransformationException (com.sun.org.apache.xml.internal.security.transforms.TransformationException)4 UnsyncByteArrayOutputStream (com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 XMLSignatureInput (com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)3 ResourceResolverException (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException)3 OutputStream (java.io.OutputStream)3 Transforms (com.sun.org.apache.xml.internal.security.transforms.Transforms)2 InclusiveNamespaces (com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 SignatureAlgorithm (com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm)1 Canonicalizer20010315ExclOmitComments (com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315ExclOmitComments)1