use of org.apache.xml.security.signature.XMLSignatureInput in project santuario-java by apache.
the class TransformBase64Decode method enginePerformTransform.
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws IOException, CanonicalizationException, TransformationException {
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.getMimeDecoder().decode(sb.toString());
XMLSignatureInput output = new XMLSignatureInput(decodedBytes);
output.setSecureValidation(secureValidation);
return output;
}
byte[] bytes = Base64.getMimeDecoder().decode(sb.toString());
os.write(bytes);
XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
output.setSecureValidation(secureValidation);
output.setOutputStream(os);
return output;
}
if (input.isOctetStream() || input.isNodeSet()) {
if (os == null) {
byte[] base64Bytes = input.getBytes();
byte[] decodedBytes = Base64.getMimeDecoder().decode(base64Bytes);
XMLSignatureInput output = new XMLSignatureInput(decodedBytes);
output.setSecureValidation(secureValidation);
return output;
}
if (input.isByteArray() || input.isNodeSet()) {
byte[] bytes = Base64.getMimeDecoder().decode(input.getBytes());
os.write(bytes);
} else {
byte[] inputBytes = JavaUtils.getBytesFromStream(input.getOctetStreamReal());
byte[] bytes = Base64.getMimeDecoder().decode(inputBytes);
os.write(bytes);
}
XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
output.setSecureValidation(secureValidation);
output.setOutputStream(os);
return output;
}
try {
// Exceptional case there is current not text case testing this(Before it was a
// a common case).
Document doc = XMLUtils.createDocumentBuilder(false, secureValidation).parse(input.getOctetStream());
Element rootNode = doc.getDocumentElement();
StringBuilder sb = new StringBuilder();
traverseElement(rootNode, sb);
byte[] decodedBytes = Base64.getMimeDecoder().decode(sb.toString());
XMLSignatureInput output = new XMLSignatureInput(decodedBytes);
output.setSecureValidation(secureValidation);
return output;
} catch (ParserConfigurationException e) {
throw new TransformationException(e, "c14n.Canonicalizer.Exception");
} catch (SAXException e) {
throw new TransformationException(e, "SAX exception");
}
}
use of org.apache.xml.security.signature.XMLSignatureInput in project santuario-java by apache.
the class TransformC14N11 method enginePerformTransform.
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transform) throws CanonicalizationException {
Canonicalizer11_OmitComments c14n = new Canonicalizer11_OmitComments();
c14n.setSecureValidation(secureValidation);
if (os != null) {
c14n.setWriter(os);
}
byte[] result = null;
result = c14n.engineCanonicalize(input);
XMLSignatureInput output = new XMLSignatureInput(result);
output.setSecureValidation(secureValidation);
if (os != null) {
output.setOutputStream(os);
}
return output;
}
use of org.apache.xml.security.signature.XMLSignatureInput in project santuario-java by apache.
the class TransformC14NExclusive method enginePerformTransform.
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws CanonicalizationException {
try {
String inclusiveNamespaces = null;
if (transformObject.length(InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES) == 1) {
Element inclusiveElement = XMLUtils.selectNode(transformObject.getElement().getFirstChild(), InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES, 0);
inclusiveNamespaces = new InclusiveNamespaces(inclusiveElement, transformObject.getBaseURI()).getInclusiveNamespaces();
}
Canonicalizer20010315ExclOmitComments c14n = new Canonicalizer20010315ExclOmitComments();
c14n.setSecureValidation(secureValidation);
if (os != null) {
c14n.setWriter(os);
}
byte[] result = c14n.engineCanonicalize(input, inclusiveNamespaces);
XMLSignatureInput output = new XMLSignatureInput(result);
output.setSecureValidation(secureValidation);
if (os != null) {
output.setOutputStream(os);
}
return output;
} catch (XMLSecurityException ex) {
throw new CanonicalizationException(ex);
}
}
use of org.apache.xml.security.signature.XMLSignatureInput in project santuario-java by apache.
the class TransformC14NWithComments method enginePerformTransform.
/**
* {@inheritDoc}
*/
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws CanonicalizationException {
Canonicalizer20010315WithComments c14n = new Canonicalizer20010315WithComments();
c14n.setSecureValidation(secureValidation);
if (os != null) {
c14n.setWriter(os);
}
byte[] result = null;
result = c14n.engineCanonicalize(input);
XMLSignatureInput output = new XMLSignatureInput(result);
output.setSecureValidation(secureValidation);
if (os != null) {
output.setOutputStream(os);
}
return output;
}
use of org.apache.xml.security.signature.XMLSignatureInput in project santuario-java by apache.
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 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
*/
{
try (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.warn("Unable to set Xalan line-separator property: " + e.getMessage());
}
try (InputStream is = new ByteArrayInputStream(input.getBytes())) {
Source xmlSource = new StreamSource(is);
if (baos == null) {
try (ByteArrayOutputStream baos1 = new ByteArrayOutputStream()) {
StreamResult outputTarget = new StreamResult(baos1);
transformer.transform(xmlSource, outputTarget);
XMLSignatureInput output = new XMLSignatureInput(baos1.toByteArray());
output.setSecureValidation(secureValidation);
return output;
}
}
StreamResult outputTarget = new StreamResult(baos);
transformer.transform(xmlSource, outputTarget);
}
XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
output.setSecureValidation(secureValidation);
output.setOutputStream(baos);
return output;
} catch (XMLSecurityException ex) {
throw new TransformationException(ex);
} catch (TransformerConfigurationException ex) {
throw new TransformationException(ex);
} catch (TransformerException ex) {
throw new TransformationException(ex);
}
}
Aggregations