Search in sources :

Example 1 with XPath2FilterContainer

use of org.apache.xml.security.transforms.params.XPath2FilterContainer in project xades4j by luisgoncalves.

the class XPath2FilterTransformParamsMarshaller method marshalParameters.

@Override
public List<Node> marshalParameters(XPath2FilterTransform alg, Document doc) {
    List<XPath2Filter> filters = alg.getFilters();
    List<Node> params = new ArrayList<Node>(filters.size());
    Set<Map.Entry<String, String>> namespaces = alg.getNamespaces().entrySet();
    for (XPath2Filter filter : filters) {
        XPath2FilterContainer c = null;
        String filterType = filter.getFilterType();
        if (XPath2FilterContainer.INTERSECT.equals(filterType)) {
            c = XPath2FilterContainer.newInstanceIntersect(doc, filter.getXPath());
        } else if (XPath2FilterContainer.SUBTRACT.equals(filterType)) {
            c = XPath2FilterContainer.newInstanceSubtract(doc, filter.getXPath());
        } else if (XPath2FilterContainer.UNION.equals(filterType)) {
            c = XPath2FilterContainer.newInstanceUnion(doc, filter.getXPath());
        } else {
            throw new IllegalArgumentException(filterType);
        }
        // different filters.
        for (Map.Entry<String, String> ns : namespaces) {
            try {
                c.setXPathNamespaceContext(ns.getKey(), ns.getValue());
            } catch (XMLSecurityException ex) {
                throw new IllegalArgumentException("Invalid namespaces for XPath query", ex);
            }
        }
        params.add(c.getElement());
    }
    return params;
}
Also used : Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) XPath2Filter(xades4j.algorithms.XPath2FilterTransform.XPath2Filter) XPath2FilterContainer(org.apache.xml.security.transforms.params.XPath2FilterContainer) Map(java.util.Map)

Example 2 with XPath2FilterContainer

use of org.apache.xml.security.transforms.params.XPath2FilterContainer in project santuario-java by apache.

the class CreateSignatureTest method testXFilter2Signature.

@org.junit.Test
public void testXFilter2Signature() throws Exception {
    Document doc = db.newDocument();
    doc.appendChild(doc.createComment(" Comment before "));
    Element root = doc.createElementNS("", "RootElement");
    doc.appendChild(root);
    root.appendChild(doc.createTextNode("Some simple text\n"));
    // Sign
    XMLSignature sig = new XMLSignature(doc, null, XMLSignature.ALGO_ID_SIGNATURE_DSA);
    root.appendChild(sig.getElement());
    Transforms transforms = new Transforms(doc);
    String filter = "here()/ancestor::ds.Signature/parent::node()/descendant-or-self::*";
    XPath2FilterContainer xpathC = XPath2FilterContainer.newInstanceIntersect(doc, filter);
    xpathC.setXPathNamespaceContext("dsig-xpath", Transforms.TRANSFORM_XPATH2FILTER);
    Element node = xpathC.getElement();
    transforms.addTransform(Transforms.TRANSFORM_XPATH2FILTER, node);
    sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
    KeyStore ks = KeyStore.getInstance("JKS");
    FileInputStream fis = null;
    if (BASEDIR != null && !"".equals(BASEDIR)) {
        fis = new FileInputStream(BASEDIR + SEP + "src/test/resources/org/apache/xml/security/samples/input/keystore.jks");
    } else {
        fis = new FileInputStream("src/test/resources/org/apache/xml/security/samples/input/keystore.jks");
    }
    ks.load(fis, "xmlsecurity".toCharArray());
    PrivateKey privateKey = (PrivateKey) ks.getKey("test", "xmlsecurity".toCharArray());
    sig.sign(privateKey);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLUtils.outputDOMc14nWithComments(doc, bos);
    String signedDoc = new String(bos.toByteArray());
    // Now Verify
    try (InputStream is = new ByteArrayInputStream(signedDoc.getBytes())) {
        doc = db.parse(is);
    }
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    xpath.setNamespaceContext(new DSNamespaceContext());
    String expression = "//ds:Signature[1]";
    Element sigElement = (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
    XMLSignature signature = new XMLSignature(sigElement, "");
    assertTrue(signature.checkSignatureValue(ks.getCertificate("test").getPublicKey()));
}
Also used : XPath(javax.xml.xpath.XPath) PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Transforms(org.apache.xml.security.transforms.Transforms) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) XPathFactory(javax.xml.xpath.XPathFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLSignature(org.apache.xml.security.signature.XMLSignature) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) XPath2FilterContainer(org.apache.xml.security.transforms.params.XPath2FilterContainer)

Example 3 with XPath2FilterContainer

use of org.apache.xml.security.transforms.params.XPath2FilterContainer in project santuario-java by apache.

the class XPath2NodeFilter method enginePerformTransform.

/**
 * Method enginePerformTransform
 * {@inheritDoc}
 * @param input
 *
 * @throws TransformationException
 */
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws TransformationException {
    try {
        List<NodeList> unionNodes = new ArrayList<>();
        List<NodeList> subtractNodes = new ArrayList<>();
        List<NodeList> intersectNodes = new ArrayList<>();
        Element[] xpathElements = XMLUtils.selectNodes(transformObject.getElement().getFirstChild(), XPath2FilterContainer.XPathFilter2NS, XPath2FilterContainer._TAG_XPATH2);
        if (xpathElements.length == 0) {
            Object[] exArgs = { Transforms.TRANSFORM_XPATH2FILTER, "XPath" };
            throw new TransformationException("xml.WrongContent", exArgs);
        }
        Document inputDoc = null;
        if (input.getSubNode() != null) {
            inputDoc = XMLUtils.getOwnerDocument(input.getSubNode());
        } else {
            inputDoc = XMLUtils.getOwnerDocument(input.getNodeSet());
        }
        for (int i = 0; i < xpathElements.length; i++) {
            Element xpathElement = xpathElements[i];
            XPath2FilterContainer xpathContainer = XPath2FilterContainer.newInstance(xpathElement, input.getSourceURI());
            String str = XMLUtils.getStrFromNode(xpathContainer.getXPathFilterTextNode());
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPathAPI xpathAPIInstance = xpathFactory.newXPathAPI();
            NodeList subtreeRoots = xpathAPIInstance.selectNodeList(inputDoc, xpathContainer.getXPathFilterTextNode(), str, xpathContainer.getElement());
            if (xpathContainer.isIntersect()) {
                intersectNodes.add(subtreeRoots);
            } else if (xpathContainer.isSubtract()) {
                subtractNodes.add(subtreeRoots);
            } else if (xpathContainer.isUnion()) {
                unionNodes.add(subtreeRoots);
            }
        }
        input.addNodeFilter(new XPath2NodeFilter(unionNodes, subtractNodes, intersectNodes));
        input.setNodeSet(true);
        return input;
    } catch (TransformerException ex) {
        throw new TransformationException(ex);
    } catch (DOMException ex) {
        throw new TransformationException(ex);
    } catch (CanonicalizationException ex) {
        throw new TransformationException(ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new TransformationException(ex);
    } catch (XMLSecurityException ex) {
        throw new TransformationException(ex);
    } catch (SAXException ex) {
        throw new TransformationException(ex);
    } catch (IOException ex) {
        throw new TransformationException(ex);
    } catch (ParserConfigurationException ex) {
        throw new TransformationException(ex);
    }
}
Also used : TransformationException(org.apache.xml.security.transforms.TransformationException) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) XPathAPI(org.apache.xml.security.utils.XPathAPI) SAXException(org.xml.sax.SAXException) XPathFactory(org.apache.xml.security.utils.XPathFactory) DOMException(org.w3c.dom.DOMException) InvalidCanonicalizerException(org.apache.xml.security.c14n.InvalidCanonicalizerException) XPath2FilterContainer(org.apache.xml.security.transforms.params.XPath2FilterContainer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

XPath2FilterContainer (org.apache.xml.security.transforms.params.XPath2FilterContainer)3 ArrayList (java.util.ArrayList)2 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 XPath (javax.xml.xpath.XPath)1 XPathFactory (javax.xml.xpath.XPathFactory)1 CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)1 InvalidCanonicalizerException (org.apache.xml.security.c14n.InvalidCanonicalizerException)1 XMLSignature (org.apache.xml.security.signature.XMLSignature)1