Search in sources :

Example 16 with CanonicalizationException

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

the class CanonicalizerBase method canonicalizeSubTree.

/**
 * Method canonicalizeSubTree, this function is a recursive one.
 *
 * @param currentNode
 * @param ns
 * @param endnode
 * @throws CanonicalizationException
 * @throws IOException
 */
protected final void canonicalizeSubTree(Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel) throws CanonicalizationException, IOException {
    if (currentNode == null || isVisibleInt(currentNode) == -1) {
        return;
    }
    Node sibling = null;
    Node parentNode = null;
    final OutputStream writer = this.writer;
    final Node excludeNode = this.excludeNode;
    final boolean includeComments = this.includeComments;
    Map<String, byte[]> cache = new HashMap<>();
    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", new Object[] { "illegal node type during traversal" });
            case Node.DOCUMENT_FRAGMENT_NODE:
            case Node.DOCUMENT_NODE:
                ns.outputNodePush();
                sibling = currentNode.getFirstChild();
                break;
            case Node.COMMENT_NODE:
                if (includeComments) {
                    outputCommentToWriter((Comment) currentNode, writer, documentLevel);
                }
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                outputPItoWriter((ProcessingInstruction) currentNode, writer, documentLevel);
                break;
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                outputTextToWriter(currentNode.getNodeValue(), writer);
                break;
            case Node.ELEMENT_NODE:
                documentLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
                if (currentNode == excludeNode) {
                    break;
                }
                Element currentElement = (Element) currentNode;
                // Add a level to the nssymbtable. So latter can be pop-back.
                ns.outputNodePush();
                writer.write('<');
                String name = currentElement.getTagName();
                UtfHelpper.writeByte(name, writer, cache);
                outputAttributesSubtree(currentElement, ns, cache);
                writer.write('>');
                sibling = currentNode.getFirstChild();
                if (sibling == null) {
                    writer.write(END_TAG.clone());
                    UtfHelpper.writeStringToUtf8(name, writer);
                    writer.write('>');
                    // We finished with this level, pop to the previous definitions.
                    ns.outputNodePop();
                    if (parentNode != null) {
                        sibling = currentNode.getNextSibling();
                    }
                } else {
                    parentNode = currentElement;
                }
                break;
            case Node.DOCUMENT_TYPE_NODE:
            default:
                break;
        }
        while (sibling == null && parentNode != null) {
            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();
            if (parentNode == endnode) {
                return;
            }
            sibling = parentNode.getNextSibling();
            parentNode = parentNode.getParentNode();
            if (parentNode == null || Node.ELEMENT_NODE != parentNode.getNodeType()) {
                documentLevel = NODE_AFTER_DOCUMENT_ELEMENT;
                parentNode = null;
            }
        }
        if (sibling == null) {
            return;
        }
        currentNode = sibling;
        sibling = currentNode.getNextSibling();
    } while (true);
}
Also used : HashMap(java.util.HashMap) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) Node(org.w3c.dom.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) OutputStream(java.io.OutputStream) Element(org.w3c.dom.Element)

Example 17 with CanonicalizationException

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

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(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)

Example 18 with CanonicalizationException

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

the class Reference method getNodesetBeforeFirstCanonicalization.

/**
 * This method returns the XMLSignatureInput which represents the node set before
 * some kind of canonicalization is applied for the first time.
 * @return Gets a the node doing everything till the first c14n is needed
 *
 * @throws XMLSignatureException
 */
public XMLSignatureInput getNodesetBeforeFirstCanonicalization() throws XMLSignatureException {
    try {
        XMLSignatureInput input = this.getContentsBeforeTransformation();
        cacheDereferencedElement(input);
        XMLSignatureInput output = input;
        Transforms transforms = this.getTransforms();
        if (transforms != null) {
            for (int i = 0; i < transforms.getLength(); i++) {
                Transform t = transforms.item(i);
                String uri = t.getURI();
                if (uri.equals(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_WITH_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N11_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N11_WITH_COMMENTS)) {
                    break;
                }
                output = t.performTransform(output, null);
            }
            output.setSourceURI(input.getSourceURI());
        }
        return output;
    } catch (IOException ex) {
        throw new XMLSignatureException(ex);
    } 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) IOException(java.io.IOException) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) Transform(org.apache.xml.security.transforms.Transform) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

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