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());
}
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);
}
}
}
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();
}
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);
}
Aggregations