Search in sources :

Example 21 with XMLSignatureInput

use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.

the class ApacheCanonicalizer method canonicalize.

public Data canonicalize(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException {
    if (apacheCanonicalizer == null) {
        try {
            apacheCanonicalizer = Canonicalizer.getInstance(getAlgorithm());
            if (log.isLoggable(java.util.logging.Level.FINE)) {
                log.log(java.util.logging.Level.FINE, "Created canonicalizer for algorithm: " + getAlgorithm());
            }
        } catch (InvalidCanonicalizerException ice) {
            throw new TransformException("Couldn't find Canonicalizer for: " + getAlgorithm() + ": " + ice.getMessage(), ice);
        }
    }
    if (os != null) {
        apacheCanonicalizer.setWriter(os);
    } else {
        apacheCanonicalizer.setWriter(new ByteArrayOutputStream());
    }
    try {
        Set<Node> nodeSet = null;
        if (data instanceof ApacheData) {
            XMLSignatureInput in = ((ApacheData) data).getXMLSignatureInput();
            if (in.isElement()) {
                if (inclusiveNamespaces != null) {
                    return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeSubtree(in.getSubNode(), inclusiveNamespaces)));
                } else {
                    return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeSubtree(in.getSubNode())));
                }
            } else if (in.isNodeSet()) {
                nodeSet = in.getNodeSet();
            } else {
                return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalize(Utils.readBytesFromStream(in.getOctetStream()))));
            }
        } else if (data instanceof DOMSubTreeData) {
            DOMSubTreeData subTree = (DOMSubTreeData) data;
            if (inclusiveNamespaces != null) {
                return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeSubtree(subTree.getRoot(), inclusiveNamespaces)));
            } else {
                return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeSubtree(subTree.getRoot())));
            }
        } else if (data instanceof NodeSetData) {
            NodeSetData nsd = (NodeSetData) data;
            // convert Iterator to Set
            @SuppressWarnings("unchecked") Set<Node> ns = Utils.toNodeSet(nsd.iterator());
            nodeSet = ns;
            if (log.isLoggable(java.util.logging.Level.FINE)) {
                log.log(java.util.logging.Level.FINE, "Canonicalizing " + nodeSet.size() + " nodes");
            }
        } else {
            return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalize(Utils.readBytesFromStream(((OctetStreamData) data).getOctetStream()))));
        }
        if (inclusiveNamespaces != null) {
            return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeXPathNodeSet(nodeSet, inclusiveNamespaces)));
        } else {
            return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeXPathNodeSet(nodeSet)));
        }
    } catch (Exception e) {
        throw new TransformException(e);
    }
}
Also used : Node(org.w3c.dom.Node) TransformException(javax.xml.crypto.dsig.TransformException) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) TransformException(javax.xml.crypto.dsig.TransformException) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)

Example 22 with XMLSignatureInput

use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.

the class TransformC14NExclusive 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();
        }
        Canonicalizer20010315ExclOmitComments c14n = new Canonicalizer20010315ExclOmitComments();
        if (os != null) {
            c14n.setWriter(os);
        }
        byte[] result = c14n.engineCanonicalize(input, inclusiveNamespaces);
        XMLSignatureInput output = new XMLSignatureInput(result);
        if (os != null) {
            output.setOutputStream(os);
        }
        return output;
    } catch (XMLSecurityException ex) {
        throw new CanonicalizationException("empty", ex);
    }
}
Also used : CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Element(org.w3c.dom.Element) InclusiveNamespaces(com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) Canonicalizer20010315ExclOmitComments(com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315ExclOmitComments) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Example 23 with XMLSignatureInput

use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.

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();
        if (os != null) {
            c14n.setWriter(os);
        }
        byte[] result = c14n.engineCanonicalize(input, inclusiveNamespaces);
        XMLSignatureInput output = new XMLSignatureInput(result);
        return output;
    } catch (XMLSecurityException ex) {
        throw new CanonicalizationException("empty", ex);
    }
}
Also used : Canonicalizer20010315ExclWithComments(com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315ExclWithComments) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) Element(org.w3c.dom.Element) InclusiveNamespaces(com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Example 24 with XMLSignatureInput

use of com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput in project jdk8u_jdk by JetBrains.

the class TransformBase64Decode method enginePerformTransform.

protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws IOException, CanonicalizationException, TransformationException {
    try {
        if (input.isElement()) {
            Node el = input.getSubNode();
            if (input.getSubNode().getNodeType() == Node.TEXT_NODE) {
                el = el.getParentNode();
            }
            StringBuilder sb = new StringBuilder();
            traverseElement((Element) el, sb);
            if (os == null) {
                byte[] decodedBytes = Base64.decode(sb.toString());
                return new XMLSignatureInput(decodedBytes);
            }
            Base64.decode(sb.toString(), os);
            XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
            output.setOutputStream(os);
            return output;
        }
        if (input.isOctetStream() || input.isNodeSet()) {
            if (os == null) {
                byte[] base64Bytes = input.getBytes();
                byte[] decodedBytes = Base64.decode(base64Bytes);
                return new XMLSignatureInput(decodedBytes);
            }
            if (input.isByteArray() || input.isNodeSet()) {
                Base64.decode(input.getBytes(), os);
            } else {
                Base64.decode(new BufferedInputStream(input.getOctetStreamReal()), os);
            }
            XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
            output.setOutputStream(os);
            return output;
        }
        try {
            //Exceptional case there is current not text case testing this(Before it was a
            //a common case).
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
            Document doc = dbf.newDocumentBuilder().parse(input.getOctetStream());
            Element rootNode = doc.getDocumentElement();
            StringBuilder sb = new StringBuilder();
            traverseElement(rootNode, sb);
            byte[] decodedBytes = Base64.decode(sb.toString());
            return new XMLSignatureInput(decodedBytes);
        } catch (ParserConfigurationException e) {
            throw new TransformationException("c14n.Canonicalizer.Exception", e);
        } catch (SAXException e) {
            throw new TransformationException("SAX exception", e);
        }
    } catch (Base64DecodingException e) {
        throw new TransformationException("Base64Decoding", e);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Base64DecodingException(com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException) BufferedInputStream(java.io.BufferedInputStream) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Aggregations

XMLSignatureInput (com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)24 Element (org.w3c.dom.Element)11 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)6 IOException (java.io.IOException)6 Node (org.w3c.dom.Node)6 ResourceResolverException (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException)5 CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)4 ResourceResolver (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 Attr (org.w3c.dom.Attr)4 SAXException (org.xml.sax.SAXException)4 Base64DecodingException (com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException)3 TransformationException (com.sun.org.apache.xml.internal.security.transforms.TransformationException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 URISyntaxException (java.net.URISyntaxException)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 InvalidCanonicalizerException (com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)2 RetrievalMethod (com.sun.org.apache.xml.internal.security.keys.content.RetrievalMethod)2 Transform (com.sun.org.apache.xml.internal.security.transforms.Transform)2 InclusiveNamespaces (com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces)2