use of org.apache.xml.security.signature.XMLSignature in project xades4j by luisgoncalves.
the class SignedDataObjectsProcessorTest method testProcess.
@Test
public void testProcess() throws Exception {
System.out.println("process");
Document doc = getNewDocument();
SignedDataObjects dataObjsDescs = new SignedDataObjects().withSignedDataObject(new DataObjectReference("uri").withTransform(new EnvelopedSignatureTransform())).withSignedDataObject(new EnvelopedXmlObject(doc.createElement("test1"))).withSignedDataObject(new EnvelopedXmlObject(doc.createElement("test2"), "text/xml", null));
XMLSignature xmlSignature = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256);
xmlSignature.setId("sigId");
AllwaysNullAlgsParamsMarshaller algsParamsMarshaller = new AllwaysNullAlgsParamsMarshaller();
SignedDataObjectsProcessor processor = new SignedDataObjectsProcessor(new TestAlgorithmsProvider(), algsParamsMarshaller);
Map<DataObjectDesc, Reference> result = processor.process(dataObjsDescs, xmlSignature);
assertEquals(dataObjsDescs.getDataObjectsDescs().size(), result.size());
assertEquals(2, xmlSignature.getObjectLength());
assertEquals(xmlSignature.getSignedInfo().getLength(), dataObjsDescs.getDataObjectsDescs().size());
assertEquals(1, algsParamsMarshaller.getInvokeCount());
Reference ref = xmlSignature.getSignedInfo().item(0);
assertEquals(1, ref.getTransforms().getLength());
ObjectContainer obj = xmlSignature.getObjectItem(1);
assertEquals("text/xml", obj.getMimeType());
assertTrue(StringUtils.isNullOrEmptyString(obj.getEncoding()));
}
use of org.apache.xml.security.signature.XMLSignature in project xades4j by luisgoncalves.
the class SignedDataObjectsProcessorTest method testAddNullReference.
@Test
public void testAddNullReference() throws Exception {
System.out.println("addNullReference");
Document doc = SignatureServicesTestBase.getNewDocument();
SignedDataObjects dataObjsDescs = new SignedDataObjects().withSignedDataObject(new AnonymousDataObjectReference("data".getBytes()));
XMLSignature xmlSignature = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256);
xmlSignature.setId("sigId");
SignedDataObjectsProcessor processor = new SignedDataObjectsProcessor(new TestAlgorithmsProvider(), new AllwaysNullAlgsParamsMarshaller());
Map<DataObjectDesc, Reference> result = processor.process(dataObjsDescs, xmlSignature);
assertEquals(1, result.size());
assertEquals(0, xmlSignature.getObjectLength());
assertEquals(1, xmlSignature.getSignedInfo().getLength());
Reference r = xmlSignature.getSignedInfo().item(0);
assertNull(r.getElement().getAttributeNodeNS(Constants.SignatureSpecNS, "URI"));
}
use of org.apache.xml.security.signature.XMLSignature in project testcases by coheigea.
the class SignatureDOMEnvelopedTest method testSignatureUsingDOMAPI.
// Sign + Verify an XML Document using the DOM API
@org.junit.Test
public void testSignatureUsingDOMAPI() throws Exception {
// Read in plaintext document
InputStream sourceDocument = this.getClass().getClassLoader().getResourceAsStream("plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
// Set up the Key
KeyStore keyStore = KeyStore.getInstance("jks");
keyStore.load(this.getClass().getClassLoader().getResource("clientstore.jks").openStream(), "cspass".toCharArray());
Key key = keyStore.getKey("myclientkey", "ckpass".toCharArray());
X509Certificate cert = (X509Certificate) keyStore.getCertificate("myclientkey");
// Sign using DOM
XMLSignature sig = new XMLSignature(document, "", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", "http://www.w3.org/2001/10/xml-exc-c14n#");
Element root = document.getDocumentElement();
root.appendChild(sig.getElement());
Transforms transforms = new Transforms(document);
transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
transforms.addTransform("http://www.w3.org/2001/10/xml-exc-c14n#");
sig.addDocument("", transforms, "http://www.w3.org/2000/09/xmldsig#sha1");
sig.sign(key);
if (cert != null) {
sig.addKeyInfo(cert);
}
XMLUtils.outputDOM(document, System.out);
// Verify using DOM
List<QName> namesToSign = new ArrayList<QName>();
namesToSign.add(new QName("urn:example:po", "PurchaseOrder"));
SignatureUtils.verifyUsingDOM(document, namesToSign, cert);
}
use of org.apache.xml.security.signature.XMLSignature in project testcases by coheigea.
the class SignatureUtils method verifyUsingDOM.
/**
* Verify the document using the DOM API of Apache Santuario - XML Security for Java.
* It finds a list of QNames via XPath and uses the DOM API to mark them as having an
* "Id".
*/
public static void verifyUsingDOM(Document document, List<QName> namesToSign, X509Certificate cert) throws Exception {
// Find the Signature Element
Element sigElement = getSignatureElement(document);
Assert.assertNotNull(sigElement);
findElementsToVerify(document, namesToSign);
XMLSignature signature = new XMLSignature(sigElement, "");
// Check we have a KeyInfo
KeyInfo ki = signature.getKeyInfo();
Assert.assertNotNull(ki);
// Check the Signature value
Assert.assertTrue(signature.checkSignatureValue(cert));
}
use of org.apache.xml.security.signature.XMLSignature in project santuario-java by apache.
the class AbstractSignatureCreationTest method verifyUsingDOMWihtoutIdAndDefaultTransform.
protected void verifyUsingDOMWihtoutIdAndDefaultTransform(Document document, Key key, List<SecurePart> secureParts) throws Exception {
XPath xpath = getxPath();
String expression = "//dsig:Signature[1]";
Element sigElement = (Element) xpath.evaluate(expression, document, XPathConstants.NODE);
Assert.assertNotNull(sigElement);
Assert.assertEquals("", sigElement.getAttribute("Id"));
assertEquals("Without Id there can only be one secure part", 1, secureParts.size());
// assertNull(secureParts.get(0).getName());
Element signedElement = document.getDocumentElement();
XMLSignature signature = new XMLSignature(sigElement, "");
// We need a special resolver for the empty URI
signature.addResourceResolver(new EmptyURIResourceResolverSpi(signedElement));
Assert.assertTrue(signature.checkSignatureValue(key));
}
Aggregations