Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit f62e311

Browse files
committed
Adding some enveloped + C14n11 tests
git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1877666 13f79535-47bb-0310-9956-ffa450edef68
1 parent b73e4f2 commit f62e311

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

src/test/java/org/apache/xml/security/test/stax/signature/SignatureCreationTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,70 @@ public void testEnvelopedSignatureCreation() throws Exception {
289289
verifyUsingDOM(document, cert, properties.getSignatureSecureParts());
290290
}
291291

292+
@Test
293+
public void testEnvelopedSignatureCreationC14n11() throws Exception {
294+
// Set up the Configuration
295+
XMLSecurityProperties properties = new XMLSecurityProperties();
296+
List<XMLSecurityConstants.Action> actions = new ArrayList<>();
297+
actions.add(XMLSecurityConstants.SIGNATURE);
298+
properties.setActions(actions);
299+
300+
// Set the key up
301+
KeyStore keyStore = KeyStore.getInstance("jks");
302+
keyStore.load(
303+
this.getClass().getClassLoader().getResource("transmitter.jks").openStream(),
304+
"default".toCharArray()
305+
);
306+
Key key = keyStore.getKey("transmitter", "default".toCharArray());
307+
properties.setSignatureKey(key);
308+
X509Certificate cert = (X509Certificate)keyStore.getCertificate("transmitter");
309+
properties.setSignatureCerts(new X509Certificate[]{cert});
310+
311+
SecurePart securePart =
312+
new SecurePart(
313+
new QName("urn:example:po", "PurchaseOrder"),
314+
SecurePart.Modifier.Content,
315+
new String[]{
316+
"http://www.w3.org/2000/09/xmldsig#enveloped-signature",
317+
"http://www.w3.org/2006/12/xml-c14n11"
318+
},
319+
"http://www.w3.org/2000/09/xmldsig#sha1"
320+
);
321+
properties.addSignaturePart(securePart);
322+
323+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
324+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
325+
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, StandardCharsets.UTF_8.name());
326+
327+
InputStream sourceDocument =
328+
this.getClass().getClassLoader().getResourceAsStream(
329+
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
330+
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
331+
332+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
333+
xmlStreamWriter.close();
334+
335+
// System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8.name()));
336+
Document document = null;
337+
try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) {
338+
document = XMLUtils.read(is, false);
339+
}
340+
341+
//first child element must be the dsig:Signature @see SANTUARIO-324:
342+
Node childNode = document.getDocumentElement().getFirstChild();
343+
while (childNode != null) {
344+
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
345+
Element element = (Element)childNode;
346+
assertEquals(element.getLocalName(), "Signature");
347+
break;
348+
}
349+
childNode = childNode.getNextSibling();
350+
}
351+
352+
// Verify using DOM
353+
verifyUsingDOM(document, cert, properties.getSignatureSecureParts());
354+
}
355+
292356
@Test
293357
public void testSignRootElementInRequest() throws Exception {
294358
// Set up the Configuration

src/test/java/org/apache/xml/security/test/stax/signature/SignatureVerificationTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,67 @@ public void testEnvelopedSignatureVerification() throws Exception {
368368
StAX2DOM.readDoc(securityStreamReader);
369369
}
370370

371+
@Test
372+
public void testEnvelopedSignatureVerificationC14n11() throws Exception {
373+
// Read in plaintext document
374+
InputStream sourceDocument =
375+
this.getClass().getClassLoader().getResourceAsStream(
376+
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
377+
Document document = XMLUtils.read(sourceDocument, false);
378+
379+
// Set up the Key
380+
KeyStore keyStore = KeyStore.getInstance("jks");
381+
keyStore.load(
382+
this.getClass().getClassLoader().getResource("transmitter.jks").openStream(),
383+
"default".toCharArray()
384+
);
385+
Key key = keyStore.getKey("transmitter", "default".toCharArray());
386+
X509Certificate cert = (X509Certificate)keyStore.getCertificate("transmitter");
387+
388+
ReferenceInfo referenceInfo = new ReferenceInfo(
389+
"",
390+
new String[]{
391+
"http://www.w3.org/2000/09/xmldsig#enveloped-signature",
392+
"http://www.w3.org/2006/12/xml-c14n11"
393+
},
394+
"http://www.w3.org/2000/09/xmldsig#sha1",
395+
false
396+
);
397+
398+
List<ReferenceInfo> referenceInfos = new ArrayList<>();
399+
referenceInfos.add(referenceInfo);
400+
401+
// Sign using DOM
402+
List<String> localNames = new ArrayList<>();
403+
localNames.add("PaymentInfo");
404+
XMLSignature sig = signUsingDOM(
405+
"http://www.w3.org/2000/09/xmldsig#rsa-sha1", document, localNames, key, referenceInfos
406+
);
407+
408+
// Add KeyInfo
409+
sig.addKeyInfo(cert);
410+
411+
// XMLUtils.outputDOM(document, System.out);
412+
413+
// Convert Document to a Stream Reader
414+
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
415+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
416+
transformer.transform(new DOMSource(document), new StreamResult(baos));
417+
418+
XMLStreamReader xmlStreamReader = null;
419+
try (InputStream is = new ByteArrayInputStream(baos.toByteArray())) {
420+
xmlStreamReader = xmlInputFactory.createXMLStreamReader(is);
421+
}
422+
423+
// Verify signature
424+
XMLSecurityProperties properties = new XMLSecurityProperties();
425+
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
426+
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
427+
XMLStreamReader securityStreamReader =
428+
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
429+
430+
StAX2DOM.readDoc(securityStreamReader);
431+
}
371432

372433
@Test
373434
public void testHMACSignatureVerification() throws Exception {

0 commit comments

Comments
 (0)