Search in sources :

Example 1 with Canonicalizer

use of org.apache.xml.security.c14n.Canonicalizer in project OpenAM by OpenRock.

the class SAMLUtils method getCanonicalElement.

/**
       * Gets input Node Canonicalized
       *
       * @param node Node
       * @return Canonical element if the operation succeeded.
       *     Otherwise, return null.
       */
public static Element getCanonicalElement(Node node) {
    try {
        Canonicalizer c14n = Canonicalizer.getInstance("http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
        byte[] outputBytes = c14n.canonicalizeSubtree(node);
        DocumentBuilder documentBuilder = XMLUtils.getSafeDocumentBuilder(false);
        Document doc = documentBuilder.parse(new ByteArrayInputStream(outputBytes));
        Element result = doc.getDocumentElement();
        return result;
    } catch (Exception e) {
        SAMLUtils.debug.error("Response:getCanonicalElement: " + "Error while performing canonicalization on " + "the input Node.");
        return null;
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer) ServletException(javax.servlet.ServletException) SystemConfigurationException(com.sun.identity.common.SystemConfigurationException) SessionException(com.sun.identity.plugin.session.SessionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 2 with Canonicalizer

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

the class Canonicalizer20010315Test method test37byNodeList.

/**
 * 3.7 Document Subsets
 *
 * @throws CanonicalizationException
 * @throws FileNotFoundException
 * @throws IOException
 * @throws InvalidCanonicalizerException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-DocSubsets">the example from the spec</A>
 * @throws TransformerException
 * @throws XPathExpressionException
 */
@org.junit.Test
public void test37byNodeList() throws IOException, FileNotFoundException, SAXException, ParserConfigurationException, CanonicalizationException, InvalidCanonicalizerException, TransformerException, XPathExpressionException {
    // String descri = "3.7 Document Subsets. (uncommented), c14n by NodeList";
    String fileIn = prefix + "in/37_input.xml";
    String fileRef = prefix + "in/37_c14n.xml";
    // String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
    // boolean validating = true;
    DocumentBuilder db = XMLUtils.createDocumentBuilder(false, false);
    org.xml.sax.EntityResolver resolver = new TestVectorResolver();
    db.setEntityResolver(resolver);
    Document doc = db.parse(resolver.resolveEntity(null, fileIn));
    String xpath = "(//. | //@* | //namespace::*)" + "[ " + "self::ietf:e1 or " + "(parent::ietf:e1 and not(self::text() or self::e2)) or " + "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node()) " + "]";
    Map<String, String> namespace = new HashMap<>();
    namespace.put("ietf", "http://www.ietf.org");
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xPath = xpf.newXPath();
    DSNamespaceContext namespaceContext = new DSNamespaceContext(namespace);
    xPath.setNamespaceContext(namespaceContext);
    NodeList nodes = (NodeList) xPath.evaluate(xpath, doc, XPathConstants.NODESET);
    Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    byte[] c14nBytes = c14n.canonicalizeXPathNodeSet(nodes);
    InputStream refStream = resolver.resolveEntity(null, fileRef).getByteStream();
    byte[] refBytes = JavaUtils.getBytesFromStream(refStream);
    assertEquals(new String(refBytes), new String(c14nBytes));
}
Also used : XPath(javax.xml.xpath.XPath) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) TestVectorResolver(org.apache.xml.security.test.dom.resource.TestVectorResolver) Document(org.w3c.dom.Document) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 3 with Canonicalizer

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

the class Canonicalizer20010315Test method doTestXMLAttributes.

/**
 * Method doTestXMLAttributes
 *
 * @param input
 * @param definedOutput
 * @param writeResultsToFile
 *
 * @throws CanonicalizationException
 * @throws FileNotFoundException
 * @throws IOException
 * @throws InvalidCanonicalizerException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 * @throws XPathExpressionException
 */
private boolean doTestXMLAttributes(String input, String definedOutput) throws IOException, FileNotFoundException, SAXException, ParserConfigurationException, CanonicalizationException, InvalidCanonicalizerException, TransformerException, XPathExpressionException {
    DocumentBuilder db = XMLUtils.createDocumentBuilder(true);
    db.setErrorHandler(new IgnoreAllErrorHandler());
    Document doc = null;
    try (InputStream is = new ByteArrayInputStream(input.getBytes())) {
        doc = db.parse(is);
    }
    Canonicalizer c14nizer = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    // XMLUtils.circumventBug2650(doc);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xPath = xpf.newXPath();
    xPath.setNamespaceContext(new DSNamespaceContext());
    String xpath = "(//*[local-name()='included'] | //@*[parent::node()[local-name()='included']])";
    NodeList nodes = (NodeList) xPath.evaluate(xpath, doc, XPathConstants.NODESET);
    byte[] result = c14nizer.canonicalizeXPathNodeSet(nodes);
    byte[] defined = definedOutput.getBytes();
    assertEquals(definedOutput, new String(result));
    return java.security.MessageDigest.isEqual(defined, result);
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) IgnoreAllErrorHandler(org.apache.xml.security.utils.IgnoreAllErrorHandler) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 4 with Canonicalizer

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

the class Canonicalizer20010315Test method c14nAndCompare.

private boolean c14nAndCompare(String fileIn, String fileRef, String fileOut, String c14nURI, boolean validating, String xpath, Map<String, String> namespaces) throws IOException, FileNotFoundException, SAXException, ParserConfigurationException, CanonicalizationException, InvalidCanonicalizerException, TransformerException, XPathExpressionException {
    DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(validating, false);
    // throw away all warnings and errors
    documentBuilder.setErrorHandler(new IgnoreAllErrorHandler());
    // org.xml.sax.EntityResolver resolver = new TestVectorResolver();
    // documentBuilder.setEntityResolver(resolver);
    // Document doc = documentBuilder.parse(resolver.resolveEntity(null, fileIn));
    Document doc = documentBuilder.parse(fileIn);
    Canonicalizer c14n = Canonicalizer.getInstance(c14nURI);
    byte[] c14nBytes = null;
    if (xpath == null) {
        c14nBytes = c14n.canonicalizeSubtree(doc);
    } else {
        NodeList nl = null;
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xPath = xpf.newXPath();
        DSNamespaceContext namespaceContext = new DSNamespaceContext(namespaces);
        xPath.setNamespaceContext(namespaceContext);
        nl = (NodeList) xPath.evaluate(xpath, doc, XPathConstants.NODESET);
        c14nBytes = c14n.canonicalizeXPathNodeSet(nl);
    }
    // org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);
    // byte refBytes[] = JavaUtils.getBytesFromStream(refIs.getByteStream());
    byte[] refBytes = JavaUtils.getBytesFromFile(fileRef);
    // if everything is OK, result is true; we do a binary compare, byte by byte
    boolean result = java.security.MessageDigest.isEqual(refBytes, c14nBytes);
    if (!result) {
        File f = new File(fileOut);
        if (!f.exists()) {
            File parent = new File(f.getParent());
            parent.mkdirs();
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(c14nBytes);
        LOG.debug("Wrote erroneous result to file " + f.toURI().toURL().toString());
        assertEquals(new String(refBytes), new String(c14nBytes));
        fos.close();
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) IgnoreAllErrorHandler(org.apache.xml.security.utils.IgnoreAllErrorHandler) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 5 with Canonicalizer

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

the class Canonicalizer20010315Test method testTranslationFromUTF16toUTF8.

/**
 * The XPath data model represents data using UCS characters.
 * Implementations MUST use XML processors that support UTF-8 and UTF-16
 * and translate to the UCS character domain. For UTF-16, the leading byte
 * order mark is treated as an artifact of encoding and stripped from the
 * UCS character data (subsequent zero width non-breaking spaces appearing
 * within the UTF-16 data are not removed) [UTF-16, Section 3.2]. Support
 * for ISO-8859-1 encoding is RECOMMENDED, and all other character encodings
 * are OPTIONAL.
 *
 * @throws CanonicalizationException
 * @throws FileNotFoundException
 * @throws IOException
 * @throws InvalidCanonicalizerException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
@org.junit.Test
public void testTranslationFromUTF16toUTF8() throws IOException, FileNotFoundException, SAXException, ParserConfigurationException, CanonicalizationException, InvalidCanonicalizerException, TransformerException {
    String val = "<UTF16>The german &amp;auml (which is Unicode &amp;#xE4;):  &quot;&#xE4;&quot;</UTF16>";
    byte[] utf16 = convertToUTF16(val.getBytes());
    Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    byte[] c14nBytes = c14n.canonicalize(utf16);
    org.xml.sax.EntityResolver resolver = new TestVectorResolver();
    InputStream refStream = resolver.resolveEntity(null, prefix + "/in/testTranslationFromUTF16toUTF8.xml").getByteStream();
    byte[] refBytes = JavaUtils.getBytesFromStream(refStream);
    boolean equal = java.security.MessageDigest.isEqual(refBytes, c14nBytes);
    assertTrue("Parser does not translate to UCS character domain", equal);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TestVectorResolver(org.apache.xml.security.test.dom.resource.TestVectorResolver) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Aggregations

Canonicalizer (org.apache.xml.security.c14n.Canonicalizer)14 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Document (org.w3c.dom.Document)8 InputStream (java.io.InputStream)7 DocumentBuilder (javax.xml.parsers.DocumentBuilder)7 XPath (javax.xml.xpath.XPath)5 XPathFactory (javax.xml.xpath.XPathFactory)5 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)5 IOException (java.io.IOException)4 NodeList (org.w3c.dom.NodeList)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IgnoreAllErrorHandler (org.apache.xml.security.utils.IgnoreAllErrorHandler)3 Node (org.w3c.dom.Node)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 TestVectorResolver (org.apache.xml.security.test.dom.resource.TestVectorResolver)2 Element (org.w3c.dom.Element)2 SystemConfigurationException (com.sun.identity.common.SystemConfigurationException)1 SessionException (com.sun.identity.plugin.session.SessionException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1