Search in sources :

Example 1 with Transform

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);
    }
}
Also used : Set(java.util.Set) XMLSignatureInput(org.apache.xml.security.signature.XMLSignatureInput) Transform(org.apache.xml.security.transforms.Transform) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException)

Example 2 with Transform

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));
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) Element(org.w3c.dom.Element) XMLSignatureInput(org.apache.xml.security.signature.XMLSignatureInput) Document(org.w3c.dom.Document) Transform(org.apache.xml.security.transforms.Transform) File(java.io.File)

Example 3 with Transform

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);
    }
}
Also used : InvalidTransformException(org.apache.xml.security.transforms.InvalidTransformException) TransformationException(org.apache.xml.security.transforms.TransformationException) Transforms(org.apache.xml.security.transforms.Transforms) InclusiveNamespaces(org.apache.xml.security.transforms.params.InclusiveNamespaces) Transform(org.apache.xml.security.transforms.Transform) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) HashSet(java.util.HashSet)

Example 4 with Transform

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;
}
Also used : Element(org.w3c.dom.Element) Transforms(org.apache.xml.security.transforms.Transforms) Transform(org.apache.xml.security.transforms.Transform) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) PatternSyntaxException(java.util.regex.PatternSyntaxException) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 5 with Transform

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);
    }
}
Also used : Transform(org.apache.xml.security.transforms.Transform) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException)

Aggregations

Transform (org.apache.xml.security.transforms.Transform)7 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)3 XMLSignatureInput (org.apache.xml.security.signature.XMLSignatureInput)3 Transforms (org.apache.xml.security.transforms.Transforms)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 Set (java.util.Set)2 InvalidCanonicalizerException (org.apache.xml.security.c14n.InvalidCanonicalizerException)2 TransformationException (org.apache.xml.security.transforms.TransformationException)2 Element (org.w3c.dom.Element)2 File (java.io.File)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 TransformException (javax.xml.crypto.dsig.TransformException)1 XPath (javax.xml.xpath.XPath)1 XPathFactory (javax.xml.xpath.XPathFactory)1 CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)1 XMLSignatureException (org.apache.xml.security.signature.XMLSignatureException)1 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)1 InvalidTransformException (org.apache.xml.security.transforms.InvalidTransformException)1