Search in sources :

Example 16 with XMLSignature

use of org.apache.xml.security.signature.XMLSignature in project santuario-java by apache.

the class PreCalculatedDigestSignatureTest method validateSignatureWithCorrectDigestShouldBeValid.

@Test
public void validateSignatureWithCorrectDigestShouldBeValid() throws Exception {
    XMLSignature signature = openSignature(signatureFilePath);
    // Add resource resolver for the external document (test.txt) with the pre-calculated digest (valid for this test)
    ExternalResourceResolver resolver = new ExternalResourceResolver(EXTERNAL_DOCUMENT_URI, PRE_CALCULATED_DIGEST);
    signature.addResourceResolver(resolver);
    boolean validSignature = validateSignature(signature);
    assertTrue(validSignature);
}
Also used : XMLSignature(org.apache.xml.security.signature.XMLSignature) Test(org.junit.Test)

Example 17 with XMLSignature

use of org.apache.xml.security.signature.XMLSignature in project santuario-java by apache.

the class SignatureReferenceTest method testSigningVerifyingReference.

@org.junit.Test
public void testSigningVerifyingReference() throws Throwable {
    Document doc = getOriginalDocument();
    XMLSignature signature = signDocument(doc);
    PublicKey pubKey = getPublicKey();
    assertTrue(signature.checkSignatureValue(pubKey));
    // Check the reference(s)
    SignedInfo signedInfo = signature.getSignedInfo();
    assertTrue(signedInfo.getLength() == 1);
    Reference reference = signedInfo.item(0);
    ReferenceData referenceData = reference.getReferenceData();
    assertNotNull(referenceData);
    assertTrue(referenceData instanceof ReferenceNodeSetData);
    // Test the cached Element
    Element referenceElement = (Element) ((ReferenceNodeSetData) referenceData).iterator().next();
    assertNotNull(referenceElement);
    assertTrue("root".equals(referenceElement.getLocalName()));
    Element originalElement = (Element) doc.getElementsByTagNameNS("http://ns.example.org/", "root").item(0);
    assertNotNull(originalElement);
    assertEquals(referenceElement, originalElement);
}
Also used : ReferenceData(org.apache.xml.security.signature.reference.ReferenceData) XMLSignature(org.apache.xml.security.signature.XMLSignature) PublicKey(java.security.PublicKey) Reference(org.apache.xml.security.signature.Reference) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) ReferenceNodeSetData(org.apache.xml.security.signature.reference.ReferenceNodeSetData) SignedInfo(org.apache.xml.security.signature.SignedInfo)

Example 18 with XMLSignature

use of org.apache.xml.security.signature.XMLSignature in project santuario-java by apache.

the class UnknownAlgoSignatureTest method unmarshalXMLSignature.

private XMLSignature unmarshalXMLSignature(String fileName) throws ParserConfigurationException, SAXException, IOException, TransformerException, XMLSecurityException, XPathExpressionException {
    File file = null;
    if (BASEDIR != null && !"".equals(BASEDIR)) {
        file = new File(BASEDIR + SEP + SIGNATURE_SOURCE_PATH, fileName);
    } else {
        file = new File(SIGNATURE_SOURCE_PATH, fileName);
    }
    Document doc = getDocument(file);
    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);
    return new XMLSignature(sigElement, file.toURI().toURL().toString());
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) XMLSignature(org.apache.xml.security.signature.XMLSignature) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) File(java.io.File)

Example 19 with XMLSignature

use of org.apache.xml.security.signature.XMLSignature in project santuario-java by apache.

the class WrappingAttackTest method readAndVerifySignatureAndSetManifest.

private boolean readAndVerifySignatureAndSetManifest(String directory, String file) throws Exception {
    String basedir = System.getProperty("basedir");
    if (basedir != null && !"".equals(basedir)) {
        directory = basedir + "/" + directory;
    }
    File f = new File(directory + "/" + file);
    javax.xml.parsers.DocumentBuilder db = XMLUtils.createDocumentBuilder(false, false);
    org.w3c.dom.Document doc = db.parse(f);
    Element sigElement = (Element) doc.getElementsByTagNameNS(Constants.SignatureSpecNS, Constants._TAG_SIGNATURE).item(0);
    Element manifestElement = (Element) doc.getElementsByTagName("Manifest").item(0);
    manifestElement.setIdAttribute("Id", true);
    XMLSignature signature = new XMLSignature(sigElement, f.toURI().toURL().toString(), true);
    return signature.checkSignatureValue(signature.getKeyInfo().getPublicKey());
}
Also used : XMLSignature(org.apache.xml.security.signature.XMLSignature) Element(org.w3c.dom.Element) File(java.io.File)

Example 20 with XMLSignature

use of org.apache.xml.security.signature.XMLSignature in project santuario-java by apache.

the class WrappingAttackTest method readAndVerifySignature.

private boolean readAndVerifySignature(String directory, String file) throws Exception {
    String basedir = System.getProperty("basedir");
    if (basedir != null && !"".equals(basedir)) {
        directory = basedir + "/" + directory;
    }
    File f = new File(directory + "/" + file);
    javax.xml.parsers.DocumentBuilder db = XMLUtils.createDocumentBuilder(false, false);
    org.w3c.dom.Document doc = db.parse(f);
    Element sigElement = (Element) doc.getElementsByTagNameNS(Constants.SignatureSpecNS, Constants._TAG_SIGNATURE).item(0);
    XMLSignature signature = new XMLSignature(sigElement, f.toURI().toURL().toString());
    return signature.checkSignatureValue(signature.getKeyInfo().getPublicKey());
}
Also used : XMLSignature(org.apache.xml.security.signature.XMLSignature) Element(org.w3c.dom.Element) File(java.io.File)

Aggregations

XMLSignature (org.apache.xml.security.signature.XMLSignature)137 Document (org.w3c.dom.Document)92 Element (org.w3c.dom.Element)69 X509Certificate (java.security.cert.X509Certificate)60 Test (org.junit.Test)60 DocumentBuilder (javax.xml.parsers.DocumentBuilder)52 InputStream (java.io.InputStream)51 ByteArrayInputStream (java.io.ByteArrayInputStream)50 ByteArrayOutputStream (java.io.ByteArrayOutputStream)49 KeyStore (java.security.KeyStore)48 ArrayList (java.util.ArrayList)48 XMLStreamReader (javax.xml.stream.XMLStreamReader)43 Key (java.security.Key)42 DOMSource (javax.xml.transform.dom.DOMSource)42 StreamResult (javax.xml.transform.stream.StreamResult)42 Transforms (org.apache.xml.security.transforms.Transforms)29 SecretKey (javax.crypto.SecretKey)28 XPath (javax.xml.xpath.XPath)23 KeyInfo (org.apache.xml.security.keys.KeyInfo)22 XPathFactory (javax.xml.xpath.XPathFactory)19