use of org.apache.xml.security.transforms.Transform in project santuario-java by apache.
the class ApacheTransform method transformIt.
private Data transformIt(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException {
if (ownerDoc == null) {
throw new TransformException("transform must be marshalled");
}
if (apacheTransform == null) {
try {
apacheTransform = new Transform(ownerDoc, getAlgorithm(), transformElem.getChildNodes());
apacheTransform.setElement(transformElem, xc.getBaseURI());
boolean secVal = Utils.secureValidation(xc);
apacheTransform.setSecureValidation(secVal);
LOG.debug("Created transform for algorithm: {}", getAlgorithm());
} catch (Exception ex) {
throw new TransformException("Couldn't find Transform for: " + getAlgorithm(), ex);
}
}
if (Utils.secureValidation(xc)) {
String algorithm = getAlgorithm();
if (Transforms.TRANSFORM_XSLT.equals(algorithm)) {
throw new TransformException("Transform " + algorithm + " is forbidden when secure validation is enabled");
}
}
XMLSignatureInput in;
if (data instanceof ApacheData) {
LOG.debug("ApacheData = true");
in = ((ApacheData) data).getXMLSignatureInput();
} else if (data instanceof NodeSetData) {
LOG.debug("isNodeSet() = true");
if (data instanceof DOMSubTreeData) {
LOG.debug("DOMSubTreeData = true");
DOMSubTreeData subTree = (DOMSubTreeData) data;
in = new XMLSignatureInput(subTree.getRoot());
in.setExcludeComments(subTree.excludeComments());
} else {
@SuppressWarnings("unchecked") Set<Node> nodeSet = Utils.toNodeSet(((NodeSetData) data).iterator());
in = new XMLSignatureInput(nodeSet);
}
} else {
LOG.debug("isNodeSet() = false");
try {
in = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
} catch (Exception ex) {
throw new TransformException(ex);
}
}
boolean secVal = Utils.secureValidation(xc);
in.setSecureValidation(secVal);
try {
if (os != null) {
in = apacheTransform.performTransform(in, os);
if (!in.isNodeSet() && !in.isElement()) {
return null;
}
} else {
in = apacheTransform.performTransform(in);
}
if (in.isOctetStream()) {
return new ApacheOctetStreamData(in);
} else {
return new ApacheNodeSetData(in);
}
} catch (Exception ex) {
throw new TransformException(ex);
}
}
use of org.apache.xml.security.transforms.Transform in project santuario-java by apache.
the class TransformXSLTTest method test1.
/**
* Make sure Transform.performTransform does not throw NullPointerException.
* See bug 41927 for more info.
*/
@org.junit.Test
public void test1() throws Exception {
File file1 = null;
File file2 = null;
if (BASEDIR != null && !"".equals(BASEDIR)) {
file1 = new File(BASEDIR + SEP + SOURCE_PATH, SIGNATURE_FILE);
file2 = new File(BASEDIR + SEP + SOURCE_PATH, STYLESHEET_FILE);
} else {
file1 = new File(SOURCE_PATH, SIGNATURE_FILE);
file1 = new File(SOURCE_PATH, STYLESHEET_FILE);
}
Document doc1 = getDocument(file1);
Document doc2 = getDocument(file2);
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DSNamespaceContext());
String expression = "//ds:Transform[1]";
Element transformEl = (Element) xpath.evaluate(expression, doc1, XPathConstants.NODE);
Transform transform = new Transform(doc1, Transforms.TRANSFORM_XSLT, transformEl.getChildNodes());
transform.performTransform(new XMLSignatureInput(doc2));
}
use of org.apache.xml.security.transforms.Transform in project santuario-java by apache.
the class Reference method getHTMLRepresentation.
/**
* Method getHTMLRepresentation
* @return The HTML of the transformation
* @throws XMLSignatureException
*/
public String getHTMLRepresentation() throws XMLSignatureException {
try {
XMLSignatureInput nodes = this.getNodesetBeforeFirstCanonicalization();
Transforms transforms = this.getTransforms();
Transform c14nTransform = null;
if (transforms != null) {
for (int i = 0; i < transforms.getLength(); i++) {
Transform t = transforms.item(i);
String uri = t.getURI();
if (uri.equals(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS)) {
c14nTransform = t;
break;
}
}
}
Set<String> inclusiveNamespaces = new HashSet<>();
if (c14nTransform != null && c14nTransform.length(InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES) == 1) {
// there is one InclusiveNamespaces element
InclusiveNamespaces in = new InclusiveNamespaces(XMLUtils.selectNode(c14nTransform.getElement().getFirstChild(), InclusiveNamespaces.ExclusiveCanonicalizationNamespace, InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES, 0), this.getBaseURI());
inclusiveNamespaces = InclusiveNamespaces.prefixStr2Set(in.getInclusiveNamespaces());
}
return nodes.getHTMLRepresentation(inclusiveNamespaces);
} catch (TransformationException ex) {
throw new XMLSignatureException(ex);
} catch (InvalidTransformException ex) {
throw new XMLSignatureException(ex);
} catch (XMLSecurityException ex) {
throw new XMLSignatureException(ex);
}
}
use of org.apache.xml.security.transforms.Transform in project cxf by apache.
the class AbstractXmlSigInHandler method validateReference.
protected Element validateReference(Element root, Reference ref) {
boolean enveloped = false;
String refId = ref.getURI();
if (!refId.startsWith("#") || refId.length() <= 1) {
throwFault("Only local Signature References are supported", null);
}
Element signedEl = getSignedElement(root, ref);
if (signedEl != null) {
enveloped = signedEl == root;
} else {
throwFault("Signature Reference ID is invalid", null);
}
Transforms transforms = null;
try {
transforms = ref.getTransforms();
} catch (XMLSecurityException ex) {
throwFault("Signature transforms can not be obtained", ex);
}
boolean c14TransformConfirmed = false;
String c14TransformExpected = sigProps != null ? sigProps.getSignatureC14nTransform() : null;
boolean envelopedConfirmed = false;
for (int i = 0; i < transforms.getLength(); i++) {
try {
Transform tr = transforms.item(i);
if (Transforms.TRANSFORM_ENVELOPED_SIGNATURE.equals(tr.getURI())) {
envelopedConfirmed = true;
} else if (c14TransformExpected != null && c14TransformExpected.equals(tr.getURI())) {
c14TransformConfirmed = true;
}
} catch (Exception ex) {
throwFault("Problem accessing Transform instance", ex);
}
}
if (enveloped && !envelopedConfirmed) {
throwFault("Only enveloped signatures are currently supported", null);
}
if (c14TransformExpected != null && !c14TransformConfirmed) {
throwFault("Transform Canonicalization is not supported", null);
}
if (sigProps != null && sigProps.getSignatureDigestAlgo() != null) {
Element dm = DOMUtils.getFirstChildWithName(ref.getElement(), Constants.SignatureSpecNS, "DigestMethod");
if (dm != null && !dm.getAttribute("Algorithm").equals(sigProps.getSignatureDigestAlgo())) {
throwFault("Signature Digest Algorithm is not supported", null);
}
}
return signedEl;
}
use of org.apache.xml.security.transforms.Transform in project xades4j by luisgoncalves.
the class TimeStampDigestInputImpl method addToDigestInput.
private void addToDigestInput(XMLSignatureInput refData, Document doc) throws CannotAddDataToDigestInputException {
try {
if (refData.isNodeSet() || refData.isElement()) {
Transform c14nTransform = TransformUtils.createTransform(this.c14n, this.parametersMarshallingProvider, doc);
refData = c14nTransform.performTransform(refData);
// Fall through to add the bytes resulting from the canonicalization.
}
if (refData.isByteArray()) {
digestInput.write(refData.getBytes());
} else if (refData.isOctetStream()) {
StreamUtils.readWrite(refData.getOctetStream(), digestInput);
}
} catch (Exception ex) {
throw new CannotAddDataToDigestInputException(ex);
}
}
Aggregations