blautenb 2003/07/21 04:30:05
Modified: src_unitTests/org/apache/xml/security/test/encryption
BaltimoreEncTest.java
Added: src_unitTests/org/apache/xml/security/test
EncryptionTest.java
src_unitTests/org/apache/xml/security/test/encryption
XMLCipherTester.java
Log:
Moved XMLCipherTester into unit tests
Revision Changes Path
1.1
xml-security/src_unitTests/org/apache/xml/security/test/EncryptionTest.java
Index: EncryptionTest.java
===================================================================
package org.apache.xml.security.test;
import org.apache.xml.security.test.encryption.XMLCipherTester;
import org.apache.xml.security.test.encryption.BaltimoreEncTest;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
public class EncryptionTest extends TestCase {
public EncryptionTest(String test) {
super(test);
}
public static void main(String[] args) {
org.apache.xml.security.Init.init();
processCmdLineArgs(args);
TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite("DOM XML Encryption Tests");
suite.addTest(new TestSuite(XMLCipherTester.class));
suite.addTest(new TestSuite(BaltimoreEncTest.class));
return (suite);
}
private static void processCmdLineArgs(String[] args) {
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-d")) {
String doc = args[i].substring(2).trim();
System.setProperty("org.apache.xml.enc.test.doc", doc);
} else if (args[i].startsWith("-e")) {
String elem = args[i].substring(2).trim();
System.setProperty("org.apache.xml.enc.test.elem", elem);
} else if (args[i].startsWith("-i")) {
String idx = args[i].substring(2).trim();
System.setProperty("org.apache.xml.enc.test.idx", idx);
}
}
}
}
1.2 +55 -16
xml-security/src_unitTests/org/apache/xml/security/test/encryption/BaltimoreEncTest.java
Index: BaltimoreEncTest.java
===================================================================
RCS file:
/home/cvs/xml-security/src_unitTests/org/apache/xml/security/test/encryption/BaltimoreEncTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BaltimoreEncTest.java 15 Jul 2003 12:30:58 -0000 1.1
+++ BaltimoreEncTest.java 21 Jul 2003 11:30:04 -0000 1.2
@@ -76,6 +76,8 @@
import org.apache.xml.serialize.Method;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
+import org.apache.xml.security.keys.KeyInfo;
+import org.apache.xml.security.keys.content.KeyName;
import org.apache.xml.security.test.interop.InteropTest;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
@@ -85,6 +87,7 @@
import org.w3c.dom.NodeList;
import junit.framework.Test;
+import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
@@ -92,10 +95,13 @@
*
* @author Berin Lautenbach
*/
-public class BaltimoreEncTest extends InteropTest {
+public class BaltimoreEncTest extends TestCase {
private static String cardNumber;
private static int nodeCount = 0;
+ private static final byte[] bobBytes =
+ "abcdefghijklmnopqrstuvwx".getBytes();
+
/** [EMAIL PROTECTED] org.apache.commons.logging} logging facility */
static org.apache.commons.logging.Log log =
@@ -106,7 +112,7 @@
*
*
*/
- public static Test suite() {
+ public static Test suite() throws Exception {
return new TestSuite(BaltimoreEncTest.class);
}
@@ -125,7 +131,7 @@
* @param args
*/
- public static void main(String[] args) throws Exception {
+ protected void setUp() throws Exception {
String[] testCaseName = { "-noloading",
BaltimoreEncTest.class.getName() };
@@ -152,7 +158,6 @@
// Initialise the library and get out of here
org.apache.xml.security.Init.init();
- junit.textui.TestRunner.main(testCaseName);
}
/**
@@ -191,15 +196,10 @@
String filename =
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-content-tripledes-cbc.xml";
- byte[] passPhrase = "abcdefghijklmnopqrstuvwx".getBytes();
- DESedeKeySpec keySpec = new DESedeKeySpec(passPhrase);
- SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("DESede");
- SecretKey key = keyFactory.generateSecret(keySpec);
-
- Document dd = decryptElement(filename, XMLCipher.TRIPLEDES,
key);
+ Document dd = decryptElement(filename, XMLCipher.TRIPLEDES);
String cc = retrieveCCNumber(dd);
-
+
// Compare the retrieved number to the stored number
assertTrue(cc, ((cc != null) && (cc.equals(cardNumber))));
@@ -223,9 +223,7 @@
* @param key Key to use for decryption
*/
- static Document decryptElement (String filename,
- String encType,
- SecretKey key)
+ public Document decryptElement (String filename, String encType)
throws Exception {
XMLCipher cipher;
@@ -249,13 +247,54 @@
// Create the XMLCipher element
cipher = XMLCipher.getInstance(encType);
- cipher.init(XMLCipher.DECRYPT_MODE, key);
+
+ // Need to pre-load the Encrypted Data so we can get the key
info
+
ee = (Element)
doc.getElementsByTagName("EncryptedData").item(0);
+ EncryptedData encryptedData = cipher.loadEncryptedData(doc, ee);
+ KeyInfo ki = encryptedData.getKeyInfo();
+
+ SecretKey key = null;
+
+ if (ki != null) {
+ KeyName keyName = ki.itemKeyName(0);
+ if (keyName != null) {
+ key = mapKeyName(keyName.getKeyName());
+ }
+ }
+ cipher.init(XMLCipher.DECRYPT_MODE, key);
Document dd = cipher.doFinal(doc, ee);
-
+
return dd;
}
+
+ /**
+ * Method mapKeyName
+ *
+ * Create a secret key from a key name for merlin-five
+ *
+ * @param name Name to map a key from
+ */
+
+ public SecretKey mapKeyName(String name) throws Exception {
+
+ if (name.equals("bob")) {
+
+ // Bob is a DESEDE key
+
+ DESedeKeySpec keySpec = new DESedeKeySpec(bobBytes);
+ SecretKeyFactory keyFactory =
+ SecretKeyFactory.getInstance("DESede");
+ SecretKey key = keyFactory.generateSecret(keySpec);
+
+ return key;
+
+ }
+
+ return null;
+
+ }
/**
* Method countNodes
1.1
xml-security/src_unitTests/org/apache/xml/security/test/encryption/XMLCipherTester.java
Index: XMLCipherTester.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "<WebSig>" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Institute for
* Data Communications Systems, <http://www.nue.et-inf.uni-siegen.de/>.
* The development of this software was partly funded by the European
* Commission in the <WebSig> project in the ISIS Programme.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.xml.security.test.encryption;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.xml.security.encryption.EncryptedData;
import org.apache.xml.security.encryption.XMLCipher;
import org.apache.xml.serialize.DOMSerializer;
import org.apache.xml.serialize.Method;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author Axl Mattheus
*/
public class XMLCipherTester extends TestCase {
private String documentName;
private String elementName;
private String elementIndex;
private XMLCipher cipher;
public XMLCipherTester(String test) {
super(test);
}
protected void setUp() {
documentName = System.getProperty("org.apache.xml.enc.test.doc",
"./build.xml");
elementName = System.getProperty("org.apache.xml.enc.test.elem",
"path");
elementIndex = System.getProperty("org.apache.xml.enc.test.idx",
"0");
}
protected void tearDown() {
}
private Document document() {
Document d = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File f = new File(documentName);
d = db.parse(f);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
return (d);
}
private String element() {
return (elementName);
}
private int index() {
int result = -1;
try {
result = Integer.parseInt(elementIndex);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
System.exit(-1);
}
return (result);
}
public void testTrippleDesElementCipher() {
Document d = document(); // source
Document ed = null; // target
Document dd = null; // target
Element e = (Element) d.getElementsByTagName(element()).item(index());
Element ee = null;
String source = toString(d);
String target = null;
try {
// prepare for encryption
byte[] passPhrase = "24 Bytes per DESede key!".getBytes();
DESedeKeySpec keySpec = new DESedeKeySpec(passPhrase);
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(keySpec);
// encrypt
cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
cipher.init(XMLCipher.ENCRYPT_MODE, key);
ed = cipher.doFinal(d, e);
//decrypt
cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
cipher.init(XMLCipher.DECRYPT_MODE, key);
ee = (Element)
ed.getElementsByTagName("xenc:EncryptedData").item(0);
dd = cipher.doFinal(ed, ee);
target = toString(dd);
} catch (Exception ex) {
ex.printStackTrace();
}
Assert.assertEquals(source, target);
}
public void testAes128ElementCipher() {
byte[] bits128 = {
(byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
(byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17,
(byte) 0x18, (byte) 0x19, (byte) 0x1A, (byte) 0x1B,
(byte) 0x1C, (byte) 0x1D, (byte) 0x1E, (byte) 0x1F};
Key key = new SecretKeySpec(bits128, "AES");
Document d = document(); // source
Document ed = null; // target
Document dd = null; // target
Element e = (Element) d.getElementsByTagName(element()).item(index());
Element ee = null;
String source = toString(d);
String target = null;
try {
// encrypt
cipher = XMLCipher.getInstance(XMLCipher.AES_128);
cipher.init(XMLCipher.ENCRYPT_MODE, key);
ed = cipher.doFinal(d, e);
//decrypt
cipher = XMLCipher.getInstance(XMLCipher.AES_128);
cipher.init(XMLCipher.DECRYPT_MODE, key);
ee = (Element)
ed.getElementsByTagName("xenc:EncryptedData").item(0);
dd = cipher.doFinal(ed, ee);
target = toString(dd);
} catch (Exception ex) {
ex.printStackTrace();
}
Assert.assertEquals(source, target);
}
public void testAes192ElementCipher() {
byte[] bits192 = {
(byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
(byte) 0x0C, (byte) 0x0D, (byte) 0x0E, (byte) 0x0F,
(byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
(byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17,
(byte) 0x18, (byte) 0x19, (byte) 0x1A, (byte) 0x1B,
(byte) 0x1C, (byte) 0x1D, (byte) 0x1E, (byte) 0x1F};
Key key = new SecretKeySpec(bits192, "AES");
Document d = document(); // source
Document ed = null; // target
Document dd = null; // target
Element e = (Element) d.getElementsByTagName(element()).item(index());
Element ee = null;
String source = toString(d);
String target = null;
try {
// encrypt
cipher = XMLCipher.getInstance(XMLCipher.AES_192);
cipher.init(XMLCipher.ENCRYPT_MODE, key);
ed = cipher.doFinal(d, e);
//decrypt
cipher = XMLCipher.getInstance(XMLCipher.AES_192);
cipher.init(XMLCipher.DECRYPT_MODE, key);
ee = (Element)
ed.getElementsByTagName("xenc:EncryptedData").item(0);
dd = cipher.doFinal(ed, ee);
target = toString(dd);
} catch (Exception ex) {
ex.printStackTrace();
}
Assert.assertEquals(source, target);
}
public void testAes265ElementCipher() {
byte[] bits256 = {
(byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
(byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
(byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
(byte) 0x0C, (byte) 0x0D, (byte) 0x0E, (byte) 0x0F,
(byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
(byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17,
(byte) 0x18, (byte) 0x19, (byte) 0x1A, (byte) 0x1B,
(byte) 0x1C, (byte) 0x1D, (byte) 0x1E, (byte) 0x1F};
Key key = new SecretKeySpec(bits256, "AES");
Document d = document(); // source
Document ed = null; // target
Document dd = null; // target
Element e = (Element) d.getElementsByTagName(element()).item(index());
Element ee = null;
String source = toString(d);
String target = null;
try {
// encrypt
cipher = XMLCipher.getInstance(XMLCipher.AES_256);
cipher.init(XMLCipher.ENCRYPT_MODE, key);
ed = cipher.doFinal(d, e);
//decrypt
cipher = XMLCipher.getInstance(XMLCipher.AES_256);
cipher.init(XMLCipher.DECRYPT_MODE, key);
ee = (Element)
ed.getElementsByTagName("xenc:EncryptedData").item(0);
dd = cipher.doFinal(ed, ee);
target = toString(dd);
} catch (Exception ex) {
ex.printStackTrace();
}
Assert.assertEquals(source, target);
}
private void dump(Element element) {
OutputFormat of = new OutputFormat();
of.setIndenting(true);
of.setMethod(Method.XML);
of.setOmitDocumentType(true);
of.setOmitXMLDeclaration(true);
DOMSerializer serializer = new XMLSerializer(System.out, of);
try {
serializer.serialize(element);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private void dump(Document document) {
OutputFormat of = new OutputFormat();
of.setIndenting(true);
of.setMethod(Method.XML);
of.setOmitDocumentType(true);
of.setOmitXMLDeclaration(true);
DOMSerializer serializer = new XMLSerializer(System.out, of);
try {
serializer.serialize(document);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private String toString(Element element) {
OutputFormat of = new OutputFormat();
of.setIndenting(true);
of.setMethod(Method.XML);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DOMSerializer serializer = new XMLSerializer(baos, of);
try {
serializer.serialize(element);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return (baos.toString());
}
private String toString(Document document) {
OutputFormat of = new OutputFormat();
of.setIndenting(true);
of.setMethod(Method.XML);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DOMSerializer serializer = new XMLSerializer(baos, of);
try {
serializer.serialize(document);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return (baos.toString());
}
static {
org.apache.xml.security.Init.init();
}
}