Search in sources :

Example 6 with TransformationException

use of com.sun.org.apache.xml.internal.security.transforms.TransformationException 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)

Example 7 with TransformationException

use of com.sun.org.apache.xml.internal.security.transforms.TransformationException in project jdk8u_jdk by JetBrains.

the class XMLCipherInput method getDecryptBytes.

/**
     * Internal method to get bytes in decryption mode
     * @return the decrypted bytes
     * @throws XMLEncryptionException
     */
private byte[] getDecryptBytes() throws XMLEncryptionException {
    String base64EncodedEncryptedOctets = null;
    if (cipherData.getDataType() == CipherData.REFERENCE_TYPE) {
        // Fun time!
        if (logger.isLoggable(java.util.logging.Level.FINE)) {
            logger.log(java.util.logging.Level.FINE, "Found a reference type CipherData");
        }
        CipherReference cr = cipherData.getCipherReference();
        // Need to wrap the uri in an Attribute node so that we can
        // Pass to the resource resolvers
        Attr uriAttr = cr.getURIAsAttr();
        XMLSignatureInput input = null;
        try {
            ResourceResolver resolver = ResourceResolver.getInstance(uriAttr, null, secureValidation);
            input = resolver.resolve(uriAttr, null, secureValidation);
        } catch (ResourceResolverException ex) {
            throw new XMLEncryptionException("empty", ex);
        }
        if (input != null) {
            if (logger.isLoggable(java.util.logging.Level.FINE)) {
                logger.log(java.util.logging.Level.FINE, "Managed to resolve URI \"" + cr.getURI() + "\"");
            }
        } else {
            if (logger.isLoggable(java.util.logging.Level.FINE)) {
                logger.log(java.util.logging.Level.FINE, "Failed to resolve URI \"" + cr.getURI() + "\"");
            }
        }
        // Lets see if there are any transforms
        Transforms transforms = cr.getTransforms();
        if (transforms != null) {
            if (logger.isLoggable(java.util.logging.Level.FINE)) {
                logger.log(java.util.logging.Level.FINE, "Have transforms in cipher reference");
            }
            try {
                com.sun.org.apache.xml.internal.security.transforms.Transforms dsTransforms = transforms.getDSTransforms();
                dsTransforms.setSecureValidation(secureValidation);
                input = dsTransforms.performTransforms(input);
            } catch (TransformationException ex) {
                throw new XMLEncryptionException("empty", ex);
            }
        }
        try {
            return input.getBytes();
        } catch (IOException ex) {
            throw new XMLEncryptionException("empty", ex);
        } catch (CanonicalizationException ex) {
            throw new XMLEncryptionException("empty", ex);
        }
    // retrieve the cipher text
    } else if (cipherData.getDataType() == CipherData.VALUE_TYPE) {
        base64EncodedEncryptedOctets = cipherData.getCipherValue().getValue();
    } else {
        throw new XMLEncryptionException("CipherData.getDataType() returned unexpected value");
    }
    if (logger.isLoggable(java.util.logging.Level.FINE)) {
        logger.log(java.util.logging.Level.FINE, "Encrypted octets:\n" + base64EncodedEncryptedOctets);
    }
    try {
        return Base64.decode(base64EncodedEncryptedOctets);
    } catch (Base64DecodingException bde) {
        throw new XMLEncryptionException("empty", bde);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException) IOException(java.io.IOException) Attr(org.w3c.dom.Attr) Base64DecodingException(com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException) ResourceResolver(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver)

Example 8 with TransformationException

use of com.sun.org.apache.xml.internal.security.transforms.TransformationException in project jdk8u_jdk by JetBrains.

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<NodeList>();
        List<NodeList> subtractNodes = new ArrayList<NodeList>();
        List<NodeList> intersectNodes = new ArrayList<NodeList>();
        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("empty", ex);
    } catch (DOMException ex) {
        throw new TransformationException("empty", ex);
    } catch (CanonicalizationException ex) {
        throw new TransformationException("empty", ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new TransformationException("empty", ex);
    } catch (XMLSecurityException ex) {
        throw new TransformationException("empty", ex);
    } catch (SAXException ex) {
        throw new TransformationException("empty", ex);
    } catch (IOException ex) {
        throw new TransformationException("empty", ex);
    } catch (ParserConfigurationException ex) {
        throw new TransformationException("empty", ex);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) CanonicalizationException(com.sun.org.apache.xml.internal.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(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException) XPathAPI(com.sun.org.apache.xml.internal.security.utils.XPathAPI) SAXException(org.xml.sax.SAXException) XPathFactory(com.sun.org.apache.xml.internal.security.utils.XPathFactory) DOMException(org.w3c.dom.DOMException) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException) XPath2FilterContainer(com.sun.org.apache.xml.internal.security.transforms.params.XPath2FilterContainer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 9 with TransformationException

use of com.sun.org.apache.xml.internal.security.transforms.TransformationException in project jdk8u_jdk by JetBrains.

the class TransformXSLT method enginePerformTransform.

protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream baos, Transform transformObject) throws IOException, TransformationException {
    try {
        Element transformElement = transformObject.getElement();
        Element xsltElement = XMLUtils.selectNode(transformElement.getFirstChild(), XSLTSpecNS, "stylesheet", 0);
        if (xsltElement == null) {
            Object[] exArgs = { "xslt:stylesheet", "Transform" };
            throw new TransformationException("xml.WrongContent", exArgs);
        }
        TransformerFactory tFactory = TransformerFactory.newInstance();
        // Process XSLT stylesheets in a secure manner
        tFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
        /*
             * This transform requires an octet stream as input. If the actual
             * input is an XPath node-set, then the signature application should
             * attempt to convert it to octets (apply Canonical XML]) as described
             * in the Reference Processing Model (section 4.3.3.2).
             */
        Source xmlSource = new StreamSource(new ByteArrayInputStream(input.getBytes()));
        Source stylesheet;
        /*
             * This complicated transformation of the stylesheet itself is necessary
             * because of the need to get the pure style sheet. If we simply say
             * Source stylesheet = new DOMSource(this.xsltElement);
             * whereby this.xsltElement is not the rootElement of the Document,
             * this causes problems;
             * so we convert the stylesheet to byte[] and use this as input stream
             */
        {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            Transformer transformer = tFactory.newTransformer();
            DOMSource source = new DOMSource(xsltElement);
            StreamResult result = new StreamResult(os);
            transformer.transform(source, result);
            stylesheet = new StreamSource(new ByteArrayInputStream(os.toByteArray()));
        }
        Transformer transformer = tFactory.newTransformer(stylesheet);
        // implementations.
        try {
            transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n");
        } catch (Exception e) {
            log.log(java.util.logging.Level.WARNING, "Unable to set Xalan line-separator property: " + e.getMessage());
        }
        if (baos == null) {
            ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
            StreamResult outputTarget = new StreamResult(baos1);
            transformer.transform(xmlSource, outputTarget);
            return new XMLSignatureInput(baos1.toByteArray());
        }
        StreamResult outputTarget = new StreamResult(baos);
        transformer.transform(xmlSource, outputTarget);
        XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
        output.setOutputStream(baos);
        return output;
    } catch (XMLSecurityException ex) {
        Object[] exArgs = { ex.getMessage() };
        throw new TransformationException("generic.EmptyMessage", exArgs, ex);
    } catch (TransformerConfigurationException ex) {
        Object[] exArgs = { ex.getMessage() };
        throw new TransformationException("generic.EmptyMessage", exArgs, ex);
    } catch (TransformerException ex) {
        Object[] exArgs = { ex.getMessage() };
        throw new TransformationException("generic.EmptyMessage", exArgs, ex);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformationException (com.sun.org.apache.xml.internal.security.transforms.TransformationException)9 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)5 Element (org.w3c.dom.Element)5 CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)4 IOException (java.io.IOException)4 InvalidCanonicalizerException (com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)3 XMLSignatureInput (com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)3 Transforms (com.sun.org.apache.xml.internal.security.transforms.Transforms)3 ResourceResolverException (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException)3 Base64DecodingException (com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException)2 Transform (com.sun.org.apache.xml.internal.security.transforms.Transform)2 XPathAPI (com.sun.org.apache.xml.internal.security.utils.XPathAPI)2 XPathFactory (com.sun.org.apache.xml.internal.security.utils.XPathFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 DOMException (org.w3c.dom.DOMException)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 SAXException (org.xml.sax.SAXException)2 InvalidTransformException (com.sun.org.apache.xml.internal.security.transforms.InvalidTransformException)1