blautenb 2003/11/17 02:36:45
Modified: src_unitTests/org/apache/xml/security/test/encryption
BaltimoreEncTest.java XMLCipherTester.java
Log:
Add RSA (PKCS1.5 padding) key wrap/unwrap test
Revision Changes Path
1.10 +1 -14
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.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- BaltimoreEncTest.java 17 Nov 2003 09:27:05 -0000 1.9
+++ BaltimoreEncTest.java 17 Nov 2003 10:36:45 -0000 1.10
@@ -560,21 +560,8 @@
*/
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;
-
- }
- */
- if (name.equals("job")) {
+ if (name.equals("job")) {
// Jeb is a AES-128 key
SecretKey key = new SecretKeySpec(jobBytes, "AES");
1.9 +82 -1
xml-security/src_unitTests/org/apache/xml/security/test/encryption/XMLCipherTester.java
Index: XMLCipherTester.java
===================================================================
RCS file:
/home/cvs/xml-security/src_unitTests/org/apache/xml/security/test/encryption/XMLCipherTester.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- XMLCipherTester.java 17 Nov 2003 09:27:05 -0000 1.8
+++ XMLCipherTester.java 17 Nov 2003 10:36:45 -0000 1.9
@@ -65,6 +65,10 @@
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.security.Key;
+import java.security.KeyPairGenerator;
+import java.security.KeyPair;
+import java.security.PrivateKey;
+import java.security.PublicKey;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
@@ -99,6 +103,11 @@
* @author Berin Lautenbach
*/
public class XMLCipherTester extends TestCase {
+
+ /** [EMAIL PROTECTED] org.apache.commons.logging} logging facility */
+ static org.apache.commons.logging.Log log =
+
org.apache.commons.logging.LogFactory.getLog(XMLCipherTester.class.getName());
+
private String documentName;
private String elementName;
private String elementIndex;
@@ -221,6 +230,78 @@
Assert.assertEquals(source, target);
}
+ /**
+ * Test encryption using a generated AES 256 bit key that is
+ * encrypted using an RSA key. Reverse using KEK
+ */
+
+ public void testAES128ElementRSAKWCipherUsingKEK() {
+
+ Document d = document(); // source
+ Document ed = null;
+ Document dd = null;
+ Element e = (Element)
d.getElementsByTagName(element()).item(index());
+ Element ee = null;
+
+ String source = null;
+ String target = null;
+
+ try {
+
+ source = toString(d);;
+
+ // Generate an RSA key
+ KeyPairGenerator rsaKeygen = KeyPairGenerator.getInstance("RSA");
+ KeyPair kp = rsaKeygen.generateKeyPair();
+ PrivateKey priv = kp.getPrivate();
+ PublicKey pub = kp.getPublic();
+
+ // Generate a traffic key
+ KeyGenerator keygen = KeyGenerator.getInstance("AES");
+ keygen.init(256);
+ Key key = keygen.generateKey();
+
+
+ cipher = XMLCipher.getInstance(XMLCipher.RSA_v1dot5);
+ cipher.init(XMLCipher.WRAP_MODE, pub);
+ EncryptedKey encryptedKey = cipher.encryptKey(d, key);
+
+ // encrypt
+ cipher = XMLCipher.getInstance(XMLCipher.AES_256);
+ cipher.init(XMLCipher.ENCRYPT_MODE, key);
+ EncryptedData builder = cipher.getEncryptedData();
+
+ KeyInfo builderKeyInfo = builder.getKeyInfo();
+ if (builderKeyInfo == null) {
+ builderKeyInfo = new KeyInfo(d);
+ builder.setKeyInfo(builderKeyInfo);
+ }
+
+ builderKeyInfo.add(encryptedKey);
+
+ ed = cipher.doFinal(d, e);
+ log.info("Encrypted document");
+ log.info(toString(ed));
+
+
+ //decrypt
+ key = null;
+ ee = (Element)
ed.getElementsByTagName("xenc:EncryptedData").item(0);
+ cipher = XMLCipher.getInstance(XMLCipher.AES_128);
+ cipher.init(XMLCipher.DECRYPT_MODE, null);
+ cipher.setKEK(priv);
+ dd = cipher.doFinal(ed, ee);
+
+ target = toString(dd);
+ log.debug("Output document");
+ log.debug(target);
+
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+
+ Assert.assertEquals(source, target);
+ }
/**
* Test encryption using a generated AES 192 bit key that is
@@ -485,7 +566,7 @@
Assert.assertEquals(source, target);
}
- /*
+ /*
* Test case for when the entire document is encrypted and decrypted
* In this case the EncryptedData becomes the root element of the
document
*/