You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2015/12/15 18:13:22 UTC
svn commit: r1720201 [20/24] - in /santuario/xml-security-java/trunk:
samples/javax/xml/crypto/dsig/samples/
samples/org/apache/xml/security/samples/
samples/org/apache/xml/security/samples/algorithms/
samples/org/apache/xml/security/samples/canonicali...
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/KeyWrapEncryptionCreationTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/KeyWrapEncryptionCreationTest.java?rev=1720201&r1=1720200&r2=1720201&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/KeyWrapEncryptionCreationTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/KeyWrapEncryptionCreationTest.java Tue Dec 15 17:13:17 2015
@@ -71,9 +71,9 @@ public class KeyWrapEncryptionCreationTe
xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setEventAllocator(new XMLSecEventAllocator());
-
+
//
- // If the BouncyCastle provider is not installed, then try to load it
+ // If the BouncyCastle provider is not installed, then try to load it
// via reflection.
//
if (Security.getProvider("BC") == null) {
@@ -90,7 +90,7 @@ public class KeyWrapEncryptionCreationTe
bcInstalled = true;
}
}
-
+
rsaKeyPair = KeyPairGenerator.getInstance("RSA").genKeyPair();
}
@@ -106,7 +106,7 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
@@ -114,7 +114,7 @@ public class KeyWrapEncryptionCreationTe
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmlenc#aes128-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
@@ -122,49 +122,49 @@ public class KeyWrapEncryptionCreationTe
String wrappingAlgorithm = "http://www.w3.org/2001/04/xmlenc#kw-aes128";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, keyWrappingKey);
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testAES192KW() throws Exception {
// Set up the Configuration
@@ -172,7 +172,7 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(192);
@@ -180,7 +180,7 @@ public class KeyWrapEncryptionCreationTe
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmlenc#aes192-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
keygen = KeyGenerator.getInstance("AES");
keygen.init(192);
@@ -188,49 +188,49 @@ public class KeyWrapEncryptionCreationTe
String wrappingAlgorithm = "http://www.w3.org/2001/04/xmlenc#kw-aes192";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, keyWrappingKey);
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testAES256KW() throws Exception {
// Set up the Configuration
@@ -238,7 +238,7 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(256);
@@ -246,7 +246,7 @@ public class KeyWrapEncryptionCreationTe
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmlenc#aes256-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
keygen = KeyGenerator.getInstance("AES");
keygen.init(256);
@@ -254,49 +254,49 @@ public class KeyWrapEncryptionCreationTe
String wrappingAlgorithm = "http://www.w3.org/2001/04/xmlenc#kw-aes256";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, keyWrappingKey);
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testTripleDESKW() throws Exception {
// Set up the Configuration
@@ -304,63 +304,63 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
SecretKey key = keygen.generateKey();
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
keygen = KeyGenerator.getInstance("DESede");
SecretKey keyWrappingKey = keygen.generateKey();
String wrappingAlgorithm = "http://www.w3.org/2001/04/xmlenc#kw-tripledes";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, keyWrappingKey);
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testRSAv15KW() throws Exception {
// Set up the Configuration
@@ -368,62 +368,62 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
SecretKey key = keygen.generateKey();
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
Key keyWrappingKey = rsaKeyPair.getPublic();
String wrappingAlgorithm = "http://www.w3.org/2001/04/xmlenc#rsa-1_5";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, rsaKeyPair.getPrivate());
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testRSAOAEPKW() throws Exception {
// Set up the Configuration
@@ -431,62 +431,62 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
SecretKey key = keygen.generateKey();
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
Key keyWrappingKey = rsaKeyPair.getPublic();
String wrappingAlgorithm = "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, rsaKeyPair.getPrivate());
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testRSAOAEP11KW() throws Exception {
// Set up the Configuration
@@ -494,62 +494,62 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
SecretKey key = keygen.generateKey();
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
Key keyWrappingKey = rsaKeyPair.getPublic();
String wrappingAlgorithm = "http://www.w3.org/2009/xmlenc11#rsa-oaep";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, rsaKeyPair.getPrivate());
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testCamellia128KW() throws Exception {
if (!bcInstalled) {
@@ -560,7 +560,7 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("Camellia");
keygen.init(128);
@@ -568,7 +568,7 @@ public class KeyWrapEncryptionCreationTe
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmldsig-more#camellia128-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
keygen = KeyGenerator.getInstance("Camellia");
keygen.init(128);
@@ -576,49 +576,49 @@ public class KeyWrapEncryptionCreationTe
String wrappingAlgorithm = "http://www.w3.org/2001/04/xmldsig-more#kw-camellia128";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, keyWrappingKey);
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testCamellia192KW() throws Exception {
if (!bcInstalled) {
@@ -629,7 +629,7 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("Camellia");
keygen.init(192);
@@ -637,7 +637,7 @@ public class KeyWrapEncryptionCreationTe
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmldsig-more#camellia192-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
keygen = KeyGenerator.getInstance("Camellia");
keygen.init(192);
@@ -645,49 +645,49 @@ public class KeyWrapEncryptionCreationTe
String wrappingAlgorithm = "http://www.w3.org/2001/04/xmldsig-more#kw-camellia192";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, keyWrappingKey);
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testCamellia256KW() throws Exception {
if (!bcInstalled) {
@@ -698,7 +698,7 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("Camellia");
keygen.init(256);
@@ -706,7 +706,7 @@ public class KeyWrapEncryptionCreationTe
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2001/04/xmldsig-more#camellia256-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
keygen = KeyGenerator.getInstance("Camellia");
keygen.init(256);
@@ -714,49 +714,49 @@ public class KeyWrapEncryptionCreationTe
String wrappingAlgorithm = "http://www.w3.org/2001/04/xmldsig-more#kw-camellia256";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, keyWrappingKey);
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testSEED128KW() throws Exception {
if (!bcInstalled) {
@@ -767,7 +767,7 @@ public class KeyWrapEncryptionCreationTe
List<XMLSecurityConstants.Action> actions = new ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("SEED");
keygen.init(128);
@@ -775,7 +775,7 @@ public class KeyWrapEncryptionCreationTe
properties.setEncryptionKey(key);
String algorithm = "http://www.w3.org/2007/05/xmldsig-more#seed128-cbc";
properties.setEncryptionSymAlgorithm(algorithm);
-
+
// Set up the Key Wrapping Key
keygen = KeyGenerator.getInstance("SEED");
keygen.init(128);
@@ -783,54 +783,54 @@ public class KeyWrapEncryptionCreationTe
String wrappingAlgorithm = "http://www.w3.org/2007/05/xmldsig-more#kw-seed128";
properties.setEncryptionKeyTransportAlgorithm(wrappingAlgorithm);
properties.setEncryptionTransportKey(keyWrappingKey);
-
- SecurePart securePart =
+
+ SecurePart securePart =
new SecurePart(new QName("urn:example:po", "PaymentInfo"), SecurePart.Modifier.Element);
properties.addEncryptionPart(securePart);
-
+
OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = outboundXMLSec.processOutMessage(baos, "UTF-8");
-
- InputStream sourceDocument =
+
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(sourceDocument);
-
+
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
-
+
// System.out.println("Got:\n" + new String(baos.toByteArray(), "UTF-8"));
-
- Document document =
+
+ Document document =
XMLUtils.createDocumentBuilder(false).parse(new ByteArrayInputStream(baos.toByteArray()));
-
+
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "PaymentInfo");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// Check the CreditCard encrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertEquals(nodeList.getLength(), 1);
-
+
// Decrypt using DOM API
Document doc = decryptUsingDOM(document, keyWrappingKey);
-
+
// Check the CreditCard decrypted ok
nodeList = doc.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
private Document decryptUsingDOM(
Document document,
Key keyWrappingKey
) throws Exception {
- NodeList nodeList =
+ NodeList nodeList =
document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
@@ -846,7 +846,7 @@ public class KeyWrapEncryptionCreationTe
kwCipher.init(XMLCipher.UNWRAP_MODE, keyWrappingKey);
KeyInfo ki = encryptedData.getKeyInfo();
EncryptedKey encryptedKey = ki.itemEncryptedKey(0);
- Key symmetricKey =
+ Key symmetricKey =
kwCipher.decryptKey(
encryptedKey, encryptedData.getEncryptionMethod().getAlgorithm()
);
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/KeyWrapEncryptionVerificationTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/KeyWrapEncryptionVerificationTest.java?rev=1720201&r1=1720200&r2=1720201&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/KeyWrapEncryptionVerificationTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/KeyWrapEncryptionVerificationTest.java Tue Dec 15 17:13:17 2015
@@ -71,16 +71,16 @@ public class KeyWrapEncryptionVerificati
private XMLInputFactory xmlInputFactory;
private TransformerFactory transformerFactory = TransformerFactory.newInstance();
private KeyPair rsaKeyPair;
-
+
@Before
public void setUp() throws Exception {
org.apache.xml.security.Init.init();
xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setEventAllocator(new XMLSecEventAllocator());
-
+
//
- // If the BouncyCastle provider is not installed, then try to load it
+ // If the BouncyCastle provider is not installed, then try to load it
// via reflection.
//
if (Security.getProvider("BC") == null) {
@@ -97,7 +97,7 @@ public class KeyWrapEncryptionVerificati
bcInstalled = true;
}
}
-
+
rsaKeyPair = KeyPairGenerator.getInstance("RSA").genKeyPair();
}
@@ -109,17 +109,17 @@ public class KeyWrapEncryptionVerificati
@Test
public void testAES128KW() throws Exception {
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_128_KeyWrap);
keygen = KeyGenerator.getInstance("AES");
@@ -127,56 +127,56 @@ public class KeyWrapEncryptionVerificati
SecretKey keyWrappingKey = keygen.generateKey();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.AES_128;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(keyWrappingKey);
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testAES192KW() throws Exception {
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(192);
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_192_KeyWrap);
keygen = KeyGenerator.getInstance("AES");
@@ -184,56 +184,56 @@ public class KeyWrapEncryptionVerificati
SecretKey keyWrappingKey = keygen.generateKey();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.AES_192;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(keyWrappingKey);
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testAES256KW() throws Exception {
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(256);
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_256_KeyWrap);
keygen = KeyGenerator.getInstance("AES");
@@ -241,276 +241,276 @@ public class KeyWrapEncryptionVerificati
SecretKey keyWrappingKey = keygen.generateKey();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.AES_256;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(keyWrappingKey);
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testTripleDESKW() throws Exception {
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES_KeyWrap);
keygen = KeyGenerator.getInstance("DESede");
SecretKey keyWrappingKey = keygen.generateKey();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.TRIPLEDES;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(keyWrappingKey);
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testRSAv15KW() throws Exception {
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.RSA_v1dot5);
Key keyWrappingKey = rsaKeyPair.getPublic();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.TRIPLEDES;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(rsaKeyPair.getPrivate());
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testRSAOAEPKW() throws Exception {
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.RSA_OAEP);
Key keyWrappingKey = rsaKeyPair.getPublic();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.TRIPLEDES;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(rsaKeyPair.getPrivate());
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testRSAOAEP11KW() throws Exception {
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.RSA_OAEP_11);
Key keyWrappingKey = rsaKeyPair.getPublic();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.TRIPLEDES;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(rsaKeyPair.getPrivate());
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testCamellia128KW() throws Exception {
if (!bcInstalled) {
return;
}
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("Camellia");
keygen.init(128);
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.CAMELLIA_128_KeyWrap);
keygen = KeyGenerator.getInstance("Camellia");
@@ -518,59 +518,59 @@ public class KeyWrapEncryptionVerificati
SecretKey keyWrappingKey = keygen.generateKey();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.CAMELLIA_128;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(keyWrappingKey);
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testCamellia192KW() throws Exception {
if (!bcInstalled) {
return;
}
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("Camellia");
keygen.init(192);
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.CAMELLIA_192_KeyWrap);
keygen = KeyGenerator.getInstance("Camellia");
@@ -578,59 +578,59 @@ public class KeyWrapEncryptionVerificati
SecretKey keyWrappingKey = keygen.generateKey();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.CAMELLIA_192;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(keyWrappingKey);
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testCamellia256KW() throws Exception {
if (!bcInstalled) {
return;
}
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("Camellia");
keygen.init(256);
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.CAMELLIA_256_KeyWrap);
keygen = KeyGenerator.getInstance("Camellia");
@@ -638,59 +638,59 @@ public class KeyWrapEncryptionVerificati
SecretKey keyWrappingKey = keygen.generateKey();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.CAMELLIA_256;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(keyWrappingKey);
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
@Test
public void testSEED128KW() throws Exception {
if (!bcInstalled) {
return;
}
// Read in plaintext document
- InputStream sourceDocument =
+ InputStream sourceDocument =
this.getClass().getClassLoader().getResourceAsStream(
"ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(sourceDocument);
-
+
// Set up the Key
KeyGenerator keygen = KeyGenerator.getInstance("SEED");
keygen.init(128);
SecretKey key = keygen.generateKey();
-
+
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.SEED_128_KeyWrap);
keygen = KeyGenerator.getInstance("SEED");
@@ -698,42 +698,42 @@ public class KeyWrapEncryptionVerificati
SecretKey keyWrappingKey = keygen.generateKey();
cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
EncryptedKey encryptedKey = cipher.encryptKey(document, key);
-
+
// Encrypt using DOM
List<String> localNames = new ArrayList<String>();
localNames.add("PaymentInfo");
-
+
String encryptionAlgorithm = XMLCipher.SEED_128;
encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
-
+
// Check the CreditCard encrypted ok
NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 0);
-
+
// XMLUtils.outputDOM(document, System.out);
-
+
// Convert Document to a Stream Reader
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(baos));
- final XMLStreamReader xmlStreamReader =
+ final XMLStreamReader xmlStreamReader =
xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
-
+
// Decrypt
XMLSecurityProperties properties = new XMLSecurityProperties();
properties.setDecryptionKey(keyWrappingKey);
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
- XMLStreamReader securityStreamReader =
+ XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);
-
+
document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
-
+
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
-
+
private void encrypt(
EncryptedKey encryptedKey,
String algorithm,