Search in sources :

Example 11 with Canonicalizer

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

the class XMLCipherTest method toString.

private String toString(Node n) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    byte[] serBytes = c14n.canonicalizeSubtree(n);
    baos.write(serBytes);
    baos.close();
    return baos.toString(StandardCharsets.UTF_8.name());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 12 with Canonicalizer

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

the class SignedInfo method signInOctetStream.

/**
 * Output the C14n stream to the given OutputStream.
 * @param os
 * @throws CanonicalizationException
 * @throws InvalidCanonicalizerException
 * @throws XMLSecurityException
 */
public void signInOctetStream(OutputStream os) throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException {
    if (this.c14nizedBytes == null) {
        Canonicalizer c14nizer = Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
        c14nizer.setSecureValidation(isSecureValidation());
        c14nizer.setWriter(os);
        String inclusiveNamespaces = this.getInclusiveNamespaces();
        if (inclusiveNamespaces == null) {
            c14nizer.canonicalizeSubtree(getElement());
        } else {
            c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces);
        }
    } else {
        try {
            os.write(this.c14nizedBytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : IOException(java.io.IOException) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 13 with Canonicalizer

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

the class SignedInfo method getCanonicalizedOctetStream.

/**
 * Returns getCanonicalizedOctetStream
 *
 * @return the canonicalization result octet stream of <code>SignedInfo</code> element
 * @throws CanonicalizationException
 * @throws InvalidCanonicalizerException
 * @throws XMLSecurityException
 */
public byte[] getCanonicalizedOctetStream() throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException {
    if (this.c14nizedBytes == null) {
        Canonicalizer c14nizer = Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
        c14nizer.setSecureValidation(isSecureValidation());
        String inclusiveNamespaces = this.getInclusiveNamespaces();
        if (inclusiveNamespaces == null) {
            this.c14nizedBytes = c14nizer.canonicalizeSubtree(getElement());
        } else {
            this.c14nizedBytes = c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces);
        }
    }
    // make defensive copy
    return this.c14nizedBytes.clone();
}
Also used : Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 14 with Canonicalizer

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

the class Santuario273Test method testC14n11Base.

@org.junit.Test
public void testC14n11Base() throws Exception {
    DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(true);
    documentBuilder.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
    Document doc = null;
    try (InputStream is = new ByteArrayInputStream(input.getBytes())) {
        doc = documentBuilder.parse(is);
    }
    Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xPath = xpf.newXPath();
    xPath.setNamespaceContext(new DSNamespaceContext());
    Node signedInfo = (Node) xPath.evaluate("//ds:SignedInfo[1]", doc, XPathConstants.NODE);
    byte[] output = c14n.canonicalizeSubtree(signedInfo);
    assertEquals(new String(output, java.nio.charset.StandardCharsets.UTF_8), expectedResult);
}
Also used : XPath(javax.xml.xpath.XPath) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) 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