Search in sources :

Example 21 with Transforms

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

the class Bug45961Test method getTransforms.

private Transforms getTransforms(Document document) throws Exception {
    Transforms transforms = new Transforms(document);
    transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
    return transforms;
}
Also used : Transforms(org.apache.xml.security.transforms.Transforms)

Example 22 with Transforms

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

the class PKSignatureAlgorithmTest method sign.

private XMLSignature sign(String algorithm, Document document, List<String> localNames, Key signingKey, AlgorithmParameterSpec parameterSpec) throws Exception {
    String c14nMethod = "http://www.w3.org/2001/10/xml-exc-c14n#";
    XMLSignature sig = new XMLSignature(document, "", algorithm, 0, c14nMethod, null, parameterSpec);
    Element root = document.getDocumentElement();
    root.appendChild(sig.getElement());
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    xpath.setNamespaceContext(new DSNamespaceContext());
    for (String localName : localNames) {
        String expression = "//*[local-name()='" + localName + "']";
        NodeList elementsToSign = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
        for (int i = 0; i < elementsToSign.getLength(); i++) {
            Element elementToSign = (Element) elementsToSign.item(i);
            assertNotNull(elementToSign);
            String id = UUID.randomUUID().toString();
            elementToSign.setAttributeNS(null, "Id", id);
            elementToSign.setIdAttributeNS(null, "Id", true);
            Transforms transforms = new Transforms(document);
            transforms.addTransform(c14nMethod);
            String digestMethod = "http://www.w3.org/2000/09/xmldsig#sha1";
            sig.addDocument("#" + id, transforms, digestMethod);
        }
    }
    sig.sign(signingKey);
    String expression = "//ds:Signature[1]";
    Element sigElement = (Element) xpath.evaluate(expression, document, XPathConstants.NODE);
    assertNotNull(sigElement);
    return sig;
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) XMLSignature(org.apache.xml.security.signature.XMLSignature) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Transforms(org.apache.xml.security.transforms.Transforms)

Example 23 with Transforms

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

the class AbstractPerformanceTest method doDOMSignatureOutbound.

protected void doDOMSignatureOutbound(File file, int tagCount) throws Exception {
    Document document = XMLUtils.read(new FileInputStream(file), false);
    XMLSignature sig = new XMLSignature(document, "", "http://www.w3.org/2000/09/xmldsig#rsa-sha1");
    Element root = document.getDocumentElement();
    root.insertBefore(sig.getElement(), root.getFirstChild());
    Transforms transforms = new Transforms(document);
    transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
    transforms.addTransform(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);
    sig.addDocument("", transforms, "http://www.w3.org/2000/09/xmldsig#sha1");
    sig.sign(key);
    sig.addKeyInfo(cert);
    XMLUtils.outputDOM(document, new BufferedOutputStream(new FileOutputStream(new File(getTmpFilePath(), "signature-dom-" + tagCount + ".xml"))));
}
Also used : XMLSignature(org.apache.xml.security.signature.XMLSignature) Element(org.w3c.dom.Element) Transforms(org.apache.xml.security.transforms.Transforms) FileOutputStream(java.io.FileOutputStream) Document(org.w3c.dom.Document) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 24 with Transforms

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

the class RetrievalMethodResolver method resolveInput.

/**
 * Resolves the input from the given retrieval method
 * @return the input from the given retrieval method
 * @throws XMLSecurityException
 */
private static XMLSignatureInput resolveInput(RetrievalMethod rm, String baseURI, boolean secureValidation) throws XMLSecurityException {
    Attr uri = rm.getURIAttr();
    // Apply the transforms
    Transforms transforms = rm.getTransforms();
    ResourceResolverContext resContext = new ResourceResolverContext(uri, baseURI, secureValidation);
    XMLSignatureInput resource = ResourceResolver.resolve(resContext);
    if (transforms != null) {
        LOG.debug("We have Transforms");
        resource = transforms.performTransforms(resource);
    }
    return resource;
}
Also used : Transforms(org.apache.xml.security.transforms.Transforms) ResourceResolverContext(org.apache.xml.security.utils.resolver.ResourceResolverContext) XMLSignatureInput(org.apache.xml.security.signature.XMLSignatureInput) Attr(org.w3c.dom.Attr)

Example 25 with Transforms

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

the class Reference method getContentsAfterTransformation.

private XMLSignatureInput getContentsAfterTransformation(XMLSignatureInput input, OutputStream os) throws XMLSignatureException {
    try {
        Transforms transforms = this.getTransforms();
        XMLSignatureInput output = null;
        if (transforms != null) {
            output = transforms.performTransforms(input, os);
            // new XMLSignatureInput(output.getBytes());
            this.transformsOutput = output;
        // this.transformsOutput.setSourceURI(output.getSourceURI());
        } else {
            output = input;
        }
        return output;
    } catch (XMLSecurityException ex) {
        throw new XMLSignatureException(ex);
    }
}
Also used : Transforms(org.apache.xml.security.transforms.Transforms) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Aggregations

Transforms (org.apache.xml.security.transforms.Transforms)90 XMLSignature (org.apache.xml.security.signature.XMLSignature)64 Element (org.w3c.dom.Element)57 Document (org.w3c.dom.Document)43 XPath (javax.xml.xpath.XPath)24 XPathFactory (javax.xml.xpath.XPathFactory)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)22 PrivateKey (java.security.PrivateKey)20 InputStream (java.io.InputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)14 NodeList (org.w3c.dom.NodeList)14 SignatureAlgorithm (org.apache.xml.security.algorithms.SignatureAlgorithm)13 FileInputStream (java.io.FileInputStream)12 ObjectContainer (org.apache.xml.security.signature.ObjectContainer)12 XMLSignatureException (org.apache.xml.security.signature.XMLSignatureException)10 XPathContainer (org.apache.xml.security.transforms.params.XPathContainer)10 KeyStore (java.security.KeyStore)9 X509Certificate (java.security.cert.X509Certificate)8