blautenb 2003/11/14 13:05:41
Modified: src/org/apache/xml/security/encryption XMLCipher.java
src/org/apache/xml/security/keys KeyInfo.java
src_unitTests/org/apache/xml/security/test/encryption
BaltimoreEncTest.java
Log:
Add ability to get instances of XMLCipher without specifying transform for
decryption operations
Revision Changes Path
1.12 +81 -1
xml-security/src/org/apache/xml/security/encryption/XMLCipher.java
Index: XMLCipher.java
===================================================================
RCS file:
/home/cvs/xml-security/src/org/apache/xml/security/encryption/XMLCipher.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- XMLCipher.java 14 Nov 2003 09:23:19 -0000 1.11
+++ XMLCipher.java 14 Nov 2003 21:05:41 -0000 1.12
@@ -324,7 +324,7 @@
* @param provider the JCE provider that supplies the transformation
* @throws <code>XMLEncryptionException</code>.
*/
- public static XMLCipher getInstance(String transformation, String
provider)
+ public static XMLCipher getProviderInstance(String transformation,
String provider)
throws XMLEncryptionException {
// sanity checks
logger.debug("Getting XMLCipher...");
@@ -365,6 +365,66 @@
}
/**
+ * Returns an <code>XMLCipher</code> that implements no specific
+ * transformation, and can therefore only be used for decrypt or
+ * unwrap operations where the encryption method is defined in the
+ * <code>EncryptionMethod</code> element.
+ *
+ * @throws <code>XMLEncryptionException</code>.
+ */
+
+ public static XMLCipher getInstance()
+ throws XMLEncryptionException {
+ // sanity checks
+ logger.debug("Getting XMLCipher for no transformation...");
+
+ XMLCipher instance = new XMLCipher();
+
+ instance._algorithm = null;
+ instance._requestedJCEProvider = null;
+ instance._key = null;
+ instance._kek = null;
+ instance._contextCipher = null;
+
+ return (instance);
+ }
+
+ /**
+ * Returns an <code>XMLCipher</code> that implements no specific
+ * transformation, and can therefore only be used for decrypt or
+ * unwrap operations where the encryption method is defined in the
+ * <code>EncryptionMethod</code> element.
+ *
+ * Allows the caller to specify a provider that will be used for
+ * cryptographic operations.
+ *
+ * @param provider the JCE provider that supplies the cryptographic
+ * needs.
+ * @throws <code>XMLEncryptionException</code>.
+ */
+
+ public static XMLCipher getProviderInstance(String provider)
+ throws XMLEncryptionException {
+ // sanity checks
+
+ logger.debug("Getting XMLCipher, provider but no transformation");
+ if(null == provider)
+ logger.error("Provider unexpectedly null..");
+ if("" == provider)
+ logger.error("Provider's value unexpectedly not specified...");
+
+ XMLCipher instance = new XMLCipher();
+
+ instance._algorithm = null;
+ instance._requestedJCEProvider = provider;
+ instance._key = null;
+ instance._kek = null;
+ instance._contextCipher = null;
+
+ return (instance);
+ }
+
+ /**
* Initializes this cipher with a key.
* <p>
* The cipher is initialized for one of the following four operations:
@@ -521,6 +581,11 @@
if(_cipherMode != ENCRYPT_MODE)
logger.error("XMLCipher unexpectedly not in ENCRYPT_MODE...");
+ if (_algorithm == null) {
+
+ throw new XMLEncryptionException("XMLCipher instance
without transformation specified");
+ }
+
String serializedOctets = _serializer.serialize(element);
logger.debug("Serialized octets:\n" + serializedOctets);
@@ -640,6 +705,11 @@
if(_cipherMode != ENCRYPT_MODE)
logger.error("XMLCipher unexpectedly not in ENCRYPT_MODE...");
+ if (_algorithm == null) {
+
+ throw new XMLEncryptionException("XMLCipher instance
without transformation specified");
+ }
+
NodeList children = element.getChildNodes();
String serializedOctets = null;
if ((null != children)) {
@@ -893,6 +963,11 @@
_contextDocument = context;
+ if (_algorithm == null) {
+
+ throw new XMLEncryptionException("XMLCipher instance
without transformation specified");
+ }
+
String serializedOctets = _serializer.serialize(element);
logger.debug("Serialized octets:\n" + serializedOctets);
@@ -1023,6 +1098,11 @@
logger.error("Key unexpectedly null...");
if(_cipherMode != WRAP_MODE)
logger.error("XMLCipher unexpectedly not in WRAP_MODE...");
+
+ if (_algorithm == null) {
+
+ throw new XMLEncryptionException("XMLCipher instance
without transformation specified");
+ }
_contextDocument = doc;
1.16 +2 -2
xml-security/src/org/apache/xml/security/keys/KeyInfo.java
Index: KeyInfo.java
===================================================================
RCS file:
/home/cvs/xml-security/src/org/apache/xml/security/keys/KeyInfo.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- KeyInfo.java 14 Nov 2003 09:23:19 -0000 1.15
+++ KeyInfo.java 14 Nov 2003 21:05:41 -0000 1.16
@@ -366,7 +366,7 @@
throws XMLEncryptionException {
if (this._state == MODE_SIGN) {
- XMLCipher cipher =
XMLCipher.getInstance(XMLCipher.TRIPLEDES);
+ XMLCipher cipher = XMLCipher.getInstance();
this._constructionElement.appendChild(cipher.martial(encryptedKey));
}
@@ -626,7 +626,7 @@
EncryptionConstants._TAG_ENCRYPTEDKEY);
if (e != null) {
- XMLCipher cipher =
XMLCipher.getInstance(XMLCipher.TRIPLEDES);
+ XMLCipher cipher = XMLCipher.getInstance();
cipher.init(XMLCipher.UNWRAP_MODE, null);
return cipher.loadEncryptedKey(e);
}
1.6 +62 -17
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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- BaltimoreEncTest.java 14 Nov 2003 04:55:02 -0000 1.5
+++ BaltimoreEncTest.java 14 Nov 2003 21:05:41 -0000 1.6
@@ -94,6 +94,42 @@
*
* @author Berin Lautenbach
*/
+
+/*
+
+Tests for merlin-xmlenc-five not currently done
+
+bad-encrypt-content-aes128-cbc-kw-aes192.xml
+decryption-transform-except.xml
+decryption-transform.xml
+dh0.p8
+dh1.p8
+dsa.p8
+encrypt-content-aes128-cbc-kw-aes192.xml
+encrypt-content-aes192-cbc-dh-sha512.xml
+encrypt-content-aes256-cbc-prop.xml
+encrypt-content-tripledes-cbc.xml
+encrypt-data-aes128-cbc.xml
+encrypt-data-aes192-cbc-kw-aes256.xml
+encrypt-data-aes256-cbc-kw-tripledes.xml
+encrypt-data-tripledes-cbc-rsa-oaep-mgf1p-sha256.xml
+encrypt-data-tripledes-cbc-rsa-oaep-mgf1p.xml
+encrypt-element-aes128-cbc-rsa-1_5.xml
+encrypt-element-aes192-cbc-ref.xml
+encrypt-element-aes256-cbc-carried-kw-aes256.xml
+encrypt-element-aes256-cbc-kw-aes256-dh-ripemd160.xml
+encrypt-element-aes256-cbc-retrieved-kw-aes256.xml
+encrypt-element-tripledes-cbc-kw-aes128.xml
+encsig-hmac-sha256-dh.xml
+encsig-hmac-sha256-kw-tripledes-dh.xml
+encsig-hmac-sha256-rsa-1_5.xml
+encsig-hmac-sha256-rsa-oaep-mgf1p.xml
+encsig-ripemd160-hmac-ripemd160-kw-tripledes.xml
+encsig-sha256-hmac-sha256-kw-aes128.xml
+encsig-sha384-hmac-sha384-kw-aes192.xml
+encsig-sha512-hmac-sha512-kw-aes256.xml
+
+*/
public class BaltimoreEncTest extends TestCase {
private static String cardNumber;
@@ -185,6 +221,26 @@
}
+ /*
+ * Check we have retrieved a Credit Card number and that it is OK
+ * Check that the document has the correct number of nodes
+ */
+
+ private void checkDecryptedDoc(Document d) throws Exception {
+
+ String cc = retrieveCCNumber(d);
+ log.debug("Retrieved Credit Card : " + cc);
+ assertTrue(cc, ((cc!= null) && (cc.equals(cardNumber))));
+
+ // Test cc numbers
+
+ int myNodeCount = countNodes(d);
+
+ assertTrue("Node count mismatches",
+ ((myNodeCount > 0) && myNodeCount ==
nodeCount));
+
+ }
+
/**
* Method test_five_content_3des_cbc
*
@@ -192,24 +248,13 @@
*
*/
+
public void test_five_content_3des_cbc() throws Exception {
String filename =
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-content-tripledes-cbc.xml";
- 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))));
-
- // Test my numbers
-
- int myNodeCount = countNodes(dd);
-
- assertTrue("Node count mismatches",
- ((myNodeCount > 0) && myNodeCount ==
nodeCount));
+ Document dd = decryptElement(filename);
+ checkDecryptedDoc(dd);
}
/**
@@ -223,7 +268,7 @@
String filename =
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-content-aes128-cbc-kw-aes192.xml";
- Document dd = decryptElement(filename, XMLCipher.TRIPLEDES);
+ Document dd = decryptElement(filename);
String cc = retrieveCCNumber(dd);
@@ -250,7 +295,7 @@
* @param key Key to use for decryption
*/
- public Document decryptElement (String filename, String encType)
+ public Document decryptElement (String filename)
throws Exception {
XMLCipher cipher;
@@ -273,7 +318,7 @@
// Create the XMLCipher element
- cipher = XMLCipher.getInstance(encType);
+ cipher = XMLCipher.getInstance();
// Need to pre-load the Encrypted Data so we can get the key
info