Search in sources :

Example 16 with CanonicalizationException

use of com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException 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 17 with CanonicalizationException

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

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