blautenb    2004/03/06 03:35:20

  Modified:    src_unitTests/org/apache/xml/security/test/encryption
                        BaltimoreEncTest.java XMLCipherTester.java
  Log:
  Test for padding and key wrap algorithms and disable relevant tests if they 
are not available
  
  Revision  Changes    Path
  1.15      +105 -34   
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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- BaltimoreEncTest.java     8 Feb 2004 06:08:22 -0000       1.14
  +++ BaltimoreEncTest.java     6 Mar 2004 11:35:19 -0000       1.15
  @@ -21,10 +21,13 @@
   import java.io.IOException;
   import java.security.Key;
   import java.security.KeyFactory;
  +import java.security.NoSuchAlgorithmException;
   import java.security.PrivateKey;
   import java.security.cert.X509Certificate;
   import java.security.spec.PKCS8EncodedKeySpec;
   
  +import javax.crypto.Cipher;
  +import javax.crypto.NoSuchPaddingException;
   import javax.crypto.SecretKey;
   import javax.crypto.spec.SecretKeySpec;
   import javax.xml.parsers.DocumentBuilder;
  @@ -35,6 +38,7 @@
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  +import org.apache.xml.security.algorithms.JCEMapper;
   import org.apache.xml.security.encryption.EncryptedData;
   import org.apache.xml.security.encryption.EncryptedKey;
   import org.apache.xml.security.encryption.XMLCipher;
  @@ -92,6 +96,8 @@
        private static byte[] jobBytes;
        private static byte[] jedBytes;
        private static PrivateKey rsaKey;
  +     private boolean haveISOPadding;
  +     private boolean haveKeyWraps;
   
        /** [EMAIL PROTECTED] org.apache.commons.logging} logging facility */
       static org.apache.commons.logging.Log log = 
  @@ -185,6 +191,23 @@
        
                // Register our key resolver
                
KeyResolver.register("org.apache.xml.security.test.encryption.BobKeyResolver");
  +
  +             // Check what algorithms are available
  +
  +             haveISOPadding = false;
  +             JCEMapper.ProviderIdClass provId = 
  +                     
JCEMapper.translateURItoJCEID(org.apache.xml.security.utils.EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128);
  +
  +             if (provId != null) {
  +                     try {
  +                             if (Cipher.getInstance(provId.getAlgorithmID()) 
!= null)
  +                                     haveISOPadding = true;
  +                     } catch (NoSuchAlgorithmException nsae) {
  +                     } catch (NoSuchPaddingException nspe) {
  +                     }
  +             }
  +
  +             haveKeyWraps = 
(JCEMapper.translateURItoJCEID(org.apache.xml.security.utils.EncryptionConstants.ALGO_ID_KEYWRAP_AES128)
 != null);
        }
   
        /**
  @@ -253,10 +276,15 @@
   
        public void test_five_content_3des_cbc() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-content-tripledes-cbc.xml";
  +             if (haveISOPadding) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-content-tripledes-cbc.xml";
   
  -             Document dd = decryptElement(filename);
  -             checkDecryptedDoc(dd, true);
  +                     Document dd = decryptElement(filename);
  +                     checkDecryptedDoc(dd, true);
  +             }
  +             else {
  +                     log.warn("Skipping test test_five_content_3des_cbs as 
necessary crypto algorithms are not available");
  +             }
       }
   
        /**
  @@ -268,10 +296,15 @@
   
        public void test_five_content_aes256_cbc() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-content-aes256-cbc-prop.xml";
  +             if (haveISOPadding) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-content-aes256-cbc-prop.xml";
   
  -             Document dd = decryptElement(filename);
  -             checkDecryptedDoc(dd, true);
  +                     Document dd = decryptElement(filename);
  +                     checkDecryptedDoc(dd, true);
  +             }
  +             else {
  +                     log.warn("Skipping test test_five_content_aes256_cbc as 
necessary crypto algorithms are not available");
  +             }
       }
   
        /**
  @@ -284,11 +317,15 @@
   
        public void test_five_content_aes128_cbc_kw_aes192() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-content-aes128-cbc-kw-aes192.xml";
  -
  -             Document dd = decryptElement(filename);
  -             checkDecryptedDoc(dd, true);
  +             if (haveISOPadding && haveKeyWraps) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-content-aes128-cbc-kw-aes192.xml";
   
  +                     Document dd = decryptElement(filename);
  +                     checkDecryptedDoc(dd, true);
  +             }
  +             else {
  +                     log.warn("Skipping test 
test_five_content_aes128_cbc_kw_aes192 as necessary crypto algorithms are not 
available");
  +             }
       }
   
        /**
  @@ -301,10 +338,15 @@
   
        public void test_five_content_3des_cbc_kw_aes128() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-element-tripledes-cbc-kw-aes128.xml";
  +             if (haveISOPadding && haveKeyWraps) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-element-tripledes-cbc-kw-aes128.xml";
   
  -             Document dd = decryptElement(filename);
  -             checkDecryptedDoc(dd, true);
  +                     Document dd = decryptElement(filename);
  +                     checkDecryptedDoc(dd, true);
  +             }
  +             else {
  +                     log.warn("Skipping test 
test_five_content_3des_cbc_kw_aes128 as necessary crypto algorithms are not 
available");
  +             }
   
       }
   
  @@ -318,11 +360,15 @@
   
        public void test_five_content_aes128_cbc_rsa_15() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-element-aes128-cbc-rsa-1_5.xml";
  -
  -             Document dd = decryptElement(filename);
  -             checkDecryptedDoc(dd, true);
  +             if (haveISOPadding) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-element-aes128-cbc-rsa-1_5.xml";
   
  +                     Document dd = decryptElement(filename);
  +                     checkDecryptedDoc(dd, true);
  +             }
  +             else {
  +                     log.warn("Skipping test 
test_five_content_aes128_cbc_rsa_15 as necessary crypto algorithms are not 
available");
  +             }
       }
   
        /**
  @@ -335,12 +381,17 @@
   
        public void test_five_element_aes192_cbc_ref() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-element-aes192-cbc-ref.xml";
  +             if (haveISOPadding) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-element-aes192-cbc-ref.xml";
   
  -             Document dd = decryptElement(filename);
  -             // Note - we don't check the node count, as it will be different
  -             // due to the encrypted text remainin in the reference nodes
  -             checkDecryptedDoc(dd, false);
  +                     Document dd = decryptElement(filename);
  +                     // Note - we don't check the node count, as it will be 
different
  +                     // due to the encrypted text remainin in the reference 
nodes
  +                     checkDecryptedDoc(dd, false);
  +             }
  +             else {
  +                     log.warn("Skipping test 
test_five_element_aes192_cbc_ref as necessary crypto algorithms are not 
available");
  +             }
       }
   
        /**
  @@ -353,10 +404,15 @@
   
        public void test_five_data_aes128_cbc() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-data-aes128-cbc.xml";
  +             if (haveISOPadding) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-data-aes128-cbc.xml";
   
  -             byte[] decrypt = decryptData(filename);
  -             checkDecryptedData(decrypt);
  +                     byte[] decrypt = decryptData(filename);
  +                     checkDecryptedData(decrypt);
  +             }
  +             else {
  +                     log.warn("Skipping test test_five_data_aes128_cbc as 
necessary crypto algorithms are not available");
  +             }
       }
   
        /**
  @@ -369,10 +425,15 @@
   
        public void test_five_data_aes256_cbc_3des() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-data-aes256-cbc-kw-tripledes.xml";
  +             if (haveISOPadding && haveKeyWraps) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-data-aes256-cbc-kw-tripledes.xml";
   
  -             byte[] decrypt = decryptData(filename);
  -             checkDecryptedData(decrypt);
  +                     byte[] decrypt = decryptData(filename);
  +                     checkDecryptedData(decrypt);
  +             }
  +             else {
  +                     log.warn("Skipping test test_five_data_aes256_cbc_3des 
as necessary crypto algorithms are not available");
  +             }
       }
   
        /**
  @@ -385,10 +446,15 @@
   
        public void test_five_data_aes192_cbc_aes256() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-data-aes192-cbc-kw-aes256.xml";
  +             if (haveISOPadding && haveKeyWraps) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-data-aes192-cbc-kw-aes256.xml";
   
  -             byte[] decrypt = decryptData(filename);
  -             checkDecryptedData(decrypt);
  +                     byte[] decrypt = decryptData(filename);
  +                     checkDecryptedData(decrypt);
  +             }
  +             else {
  +                     log.warn("Skipping test 
test_five_data_aes192_cbc_aes256 as necessary crypto algorithms are not 
available");
  +             }
       }
   
        /**
  @@ -401,10 +467,15 @@
   
        public void test_five_data_3des_cbc_rsa_oaep() throws Exception {
   
  -             String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-data-tripledes-cbc-rsa-oaep-mgf1p.xml";
  +             if (haveISOPadding) {
  +                     String filename = 
"data/ie/baltimore/merlin-examples/merlin-xmlenc-five/encrypt-data-tripledes-cbc-rsa-oaep-mgf1p.xml";
   
  -             byte[] decrypt = decryptData(filename);
  -             checkDecryptedData(decrypt);
  +                     byte[] decrypt = decryptData(filename);
  +                     checkDecryptedData(decrypt);
  +             }
  +             else {
  +                     log.warn("Skipping test 
test_five_data_3des_cbc_rsa_oaep as necessary crypto algorithms are not 
available");
  +             }
       }
   
        /**
  
  
  
  1.14      +138 -105  
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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- XMLCipherTester.java      8 Feb 2004 06:08:22 -0000       1.13
  +++ XMLCipherTester.java      6 Mar 2004 11:35:19 -0000       1.14
  @@ -25,10 +25,13 @@
   import java.security.Key;
   import java.security.KeyPairGenerator;
   import java.security.KeyPair;
  +import java.security.NoSuchAlgorithmException;
   import java.security.PrivateKey;
   import java.security.PublicKey;
   
  +import javax.crypto.Cipher;
   import javax.crypto.KeyGenerator;
  +import javax.crypto.NoSuchPaddingException;
   import javax.crypto.SecretKey;
   import javax.crypto.SecretKeyFactory;
   import javax.crypto.spec.DESedeKeySpec;
  @@ -39,6 +42,7 @@
   import junit.framework.Assert;
   import junit.framework.TestCase;
   
  +import org.apache.xml.security.algorithms.JCEMapper;
   import org.apache.xml.security.c14n.Canonicalizer;
   import org.apache.xml.security.encryption.XMLCipher;
   import org.apache.xml.security.encryption.EncryptedData;
  @@ -74,6 +78,9 @@
       private String elementIndex;
       private XMLCipher cipher;
   
  +     private boolean haveISOPadding;
  +     private boolean haveKeyWraps;
  +
        private String tstBase64EncodedString;
   
       public XMLCipherTester(String test) {
  @@ -89,6 +96,25 @@
               "0");
   
                tstBase64EncodedString = new 
String("YmNkZWZnaGlqa2xtbm9wcRrPXjQ1hvhDFT+EdesMAPE4F6vlT+y0HPXe0+nAGLQ8");
  +
  +             // Determine if we have ISO 10126 Padding - needed for Bulk AES 
or
  +             // 3DES encryption
  +
  +             haveISOPadding = false;
  +             JCEMapper.ProviderIdClass provId = 
  +                     
JCEMapper.translateURItoJCEID(org.apache.xml.security.utils.EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128);
  +
  +             if (provId != null) {
  +                     try {
  +                             if (Cipher.getInstance(provId.getAlgorithmID()) 
!= null)
  +                                     haveISOPadding = true;
  +                     } catch (NoSuchAlgorithmException nsae) {
  +                     } catch (NoSuchPaddingException nspe) {
  +                     }
  +             }
  +
  +             haveKeyWraps = 
(JCEMapper.translateURItoJCEID(org.apache.xml.security.utils.EncryptionConstants.ALGO_ID_KEYWRAP_AES128)
 != null);
  +
       }
   
       protected void tearDown() {
  @@ -131,7 +157,7 @@
         * encrypted using a AES 192 bit key.  Then reverse using the KEK
         */
   
  -     public void testAES128ElementAES192KWCipherUsingKEK() {
  +     public void testAES128ElementAES192KWCipherUsingKEK() throws Exception {
   
                Document d = document(); // source
                Document ed = null;
  @@ -142,7 +168,7 @@
                String source = null;
                String target = null;
   
  -        try {
  +        if (haveISOPadding && haveKeyWraps) {
   
                        source = toString(d);
   
  @@ -184,11 +210,11 @@
   
               target = toString(dd);
   
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -        }
  -
  -        Assert.assertEquals(source, target);
  +                     Assert.assertEquals(source, target);
  +             }
  +             else {
  +                     log.warn("Test testAES128ElementAES192KWCipherUsingKEK 
skipped as necessary algorithms not available");
  +             }
       }
     
        /**
  @@ -196,7 +222,7 @@
         * encrypted using an RSA key.  Reverse using KEK
         */
   
  -     public void testAES128ElementRSAKWCipherUsingKEK() {
  +     public void testAES128ElementRSAKWCipherUsingKEK() throws Exception {
   
                Document d = document(); // source
                Document ed = null;
  @@ -207,7 +233,7 @@
                String source = null;
                String target = null;
   
  -        try {
  +        if (haveISOPadding) {
   
                        source = toString(d);
   
  @@ -257,11 +283,11 @@
               log.debug("Output document");
               log.debug(target);
   
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -        }
  -
  -        Assert.assertEquals(source, target);
  +                     Assert.assertEquals(source, target);
  +             }
  +             else {
  +                     log.warn("Test testAES128ElementRSAKWCipherUsingKEK 
skipped as necessary algorithms not available");
  +             }
       }
   
        /**
  @@ -270,7 +296,7 @@
         * EncryptedKey by hand
         */
   
  -     public void testAES192ElementAES256KWCipher() {
  +     public void testAES192ElementAES256KWCipher() throws Exception {
   
                Document d = document(); // source
                Document ed = null;
  @@ -281,7 +307,7 @@
                String source = null;
                String target = null;
   
  -        try {
  +        if (haveISOPadding && haveKeyWraps) {
   
                        source = toString(d);
   
  @@ -343,14 +369,15 @@
               dd = cipher3.doFinal(ed, ee);
   
               target = toString(dd);
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -        }
   
  -        Assert.assertEquals(source, target);
  +                     Assert.assertEquals(source, target);
  +             }
  +             else {
  +                     log.warn("Test testAES192ElementAES256KWCipher skipped 
as necessary algorithms not available");
  +             }
       }
   
  -    public void testTrippleDesElementCipher() {
  +    public void testTrippleDesElementCipher() throws Exception {
           Document d = document(); // source
           Document ed = null;      // target
           Document dd = null;      // target
  @@ -360,7 +387,7 @@
           String source = null;
           String target = null;
   
  -        try {
  +        if (haveISOPadding) {
   
                        source = toString(d);
   
  @@ -385,14 +412,14 @@
               dd = cipher.doFinal(ed, ee);
   
               target = toString(dd);
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -        }
  -
  -        Assert.assertEquals(source, target);
  +                     Assert.assertEquals(source, target);
  +             }
  +             else {
  +                     log.warn("Test testTrippleDesElementCipher skipped as 
necessary algorithms not available");
  +             }
       }
   
  -    public void testAes128ElementCipher() {
  +    public void testAes128ElementCipher() throws Exception {
           byte[] bits128 = {
               (byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
               (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17,
  @@ -409,7 +436,7 @@
           String source = null;
           String target = null;
   
  -        try {
  +        if (haveISOPadding) {
   
                        source = toString(d);
   
  @@ -428,14 +455,14 @@
               dd = cipher.doFinal(ed, ee);
   
               target = toString(dd);
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -        }
  -
  -        Assert.assertEquals(source, target);
  +                     Assert.assertEquals(source, target);
  +             }
  +             else {
  +                     log.warn("Test testAes128ElementCipher skipped as 
necessary algorithms not available");
  +             }
       }
   
  -    public void testAes192ElementCipher() {
  +    public void testAes192ElementCipher() throws Exception {
           byte[] bits192 = {
               (byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
               (byte) 0x0C, (byte) 0x0D, (byte) 0x0E, (byte) 0x0F,
  @@ -454,7 +481,7 @@
           String source = null;
           String target = null;
   
  -        try {
  +        if (haveISOPadding) {
   
                        source = toString(d);
   
  @@ -473,14 +500,14 @@
               dd = cipher.doFinal(ed, ee);
   
               target = toString(dd);
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -        }
  -
  -        Assert.assertEquals(source, target);
  +                     Assert.assertEquals(source, target);
  +             }
  +             else {
  +                     log.warn("Test testAes192ElementCipher skipped as 
necessary algorithms not available");
  +             }
       }
   
  -    public void testAes265ElementCipher() {
  +    public void testAes265ElementCipher() throws Exception {
           byte[] bits256 = {
               (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
               (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
  @@ -501,7 +528,7 @@
           String source = null;
           String target = null;
   
  -        try {
  +        if (haveISOPadding) {
   
                        source = toString(d);
   
  @@ -520,11 +547,11 @@
               dd = cipher.doFinal(ed, ee);
   
               target = toString(dd);
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -        }
  -
  -        Assert.assertEquals(source, target);
  +                     Assert.assertEquals(source, target);
  +             }
  +             else {
  +                     log.warn("Test testAes265ElementCipher skipped as 
necessary algorithms not available");
  +             }
       }
   
       /*
  @@ -532,7 +559,7 @@
         * In this case the EncryptedData becomes the root element of the 
document
         */
   
  -    public void testTrippleDesDocumentCipher() {
  +    public void testTrippleDesDocumentCipher() throws Exception {
           Document d = document(); // source
           Document ed = null;      // target
           Document dd = null;      // target
  @@ -542,7 +569,8 @@
           String source = null;
           String target = null;
   
  -        try {
  +        if (haveISOPadding) {
  +
                        source = toString(d);
   
               // prepare for encryption
  @@ -564,11 +592,11 @@
   
               target = toString(dd);
   
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -        }
  -
  -        Assert.assertEquals(source, target);
  +                     Assert.assertEquals(source, target);
  +             }
  +             else {
  +                     log.warn("Test testTrippleDesDocumentCipher skipped as 
necessary algorithms not available");
  +             }
       }
   
        /*
  @@ -577,63 +605,68 @@
   
        public void testSameDocumentCipherReference() throws Exception {
   
  -             DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
  -             DocumentBuilder db = dbf.newDocumentBuilder();
  +        if (haveISOPadding) {
  +                     DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
  +                     DocumentBuilder db = dbf.newDocumentBuilder();
   
  -             Document d = db.newDocument();
  +                     Document d = db.newDocument();
   
  -             Element docElement = d.createElement("EncryptedDoc");
  -             d.appendChild(docElement);
  +                     Element docElement = d.createElement("EncryptedDoc");
  +                     d.appendChild(docElement);
   
  -             // Create the XMLCipher object
  -             cipher = XMLCipher.getInstance();
  +                     // Create the XMLCipher object
  +                     cipher = XMLCipher.getInstance();
   
  -             EncryptedData ed = 
  -                     cipher.createEncryptedData(CipherData.REFERENCE_TYPE,
  +                     EncryptedData ed = 
  +                             
cipher.createEncryptedData(CipherData.REFERENCE_TYPE,
                                                                           
"#CipherTextId");
  -             EncryptionMethod em =
  -                     cipher.createEncryptionMethod(XMLCipher.AES_128);
  -
  -             ed.setEncryptionMethod(em);
  -
  -             org.apache.xml.security.encryption.Transforms xencTransforms =
  -                     cipher.createTransforms(d);
  -             
ed.getCipherData().getCipherReference().setTransforms(xencTransforms);
  -             org.apache.xml.security.transforms.Transforms dsTransforms =
  -                     xencTransforms.getDSTransforms();
  -
  -             // An XPath transform
  -             XPathContainer xpc = new XPathContainer(d);
  -             xpc.setXPath("self::text()[parent::[EMAIL 
PROTECTED]"CipherTextId\"]]");
  -             
dsTransforms.addTransform(org.apache.xml.security.transforms.Transforms.TRANSFORM_XPATH,
 
  -                                                               
xpc.getElementPlusReturns());
  -
  -             // Add a Base64 Transforms
  -             dsTransforms.addTransform(
  -                                                               
org.apache.xml.security.transforms.Transforms.TRANSFORM_BASE64_DECODE);
  -
  -             Element ee = cipher.martial(d, ed);
  -
  -             docElement.appendChild(ee);
  -
  -             // Add the cipher text
  -             Element encryptedElement = d.createElement("CipherText");
  -             encryptedElement.setAttributeNS(null, "Id", "CipherTextId");
  -             IdResolver.registerElementById(encryptedElement, 
"CipherTextId");
  -             
encryptedElement.appendChild(d.createTextNode(tstBase64EncodedString));
  -             docElement.appendChild(encryptedElement);
  -             // dump(d);
  -
  -             // Now the decrypt, with a brand new cipher
  -             XMLCipher cipherDecrypt = XMLCipher.getInstance();
  -        Key key = 
  -                     new SecretKeySpec("abcdefghijklmnop".getBytes("ASCII"), 
"AES");
  +                     EncryptionMethod em =
  +                             
cipher.createEncryptionMethod(XMLCipher.AES_128);
   
  -             cipherDecrypt.init(XMLCipher.DECRYPT_MODE, key);
  -             byte[] decryptBytes = cipherDecrypt.decryptToByteArray(ee);
  +                     ed.setEncryptionMethod(em);
   
  -        Assert.assertEquals(new String(decryptBytes, "ASCII"), 
  -                                                     new String("A test 
encrypted secret"));
  +                     org.apache.xml.security.encryption.Transforms 
xencTransforms =
  +                             cipher.createTransforms(d);
  +                     
ed.getCipherData().getCipherReference().setTransforms(xencTransforms);
  +                     org.apache.xml.security.transforms.Transforms 
dsTransforms =
  +                             xencTransforms.getDSTransforms();
  +
  +                     // An XPath transform
  +                     XPathContainer xpc = new XPathContainer(d);
  +                     xpc.setXPath("self::text()[parent::[EMAIL 
PROTECTED]"CipherTextId\"]]");
  +                     
dsTransforms.addTransform(org.apache.xml.security.transforms.Transforms.TRANSFORM_XPATH,
 
  +                                                                       
xpc.getElementPlusReturns());
  +
  +                     // Add a Base64 Transforms
  +                     dsTransforms.addTransform(
  +                                                                       
org.apache.xml.security.transforms.Transforms.TRANSFORM_BASE64_DECODE);
  +
  +                     Element ee = cipher.martial(d, ed);
  +
  +                     docElement.appendChild(ee);
  +
  +                     // Add the cipher text
  +                     Element encryptedElement = 
d.createElement("CipherText");
  +                     encryptedElement.setAttributeNS(null, "Id", 
"CipherTextId");
  +                     IdResolver.registerElementById(encryptedElement, 
"CipherTextId");
  +                     
encryptedElement.appendChild(d.createTextNode(tstBase64EncodedString));
  +                     docElement.appendChild(encryptedElement);
  +                     // dump(d);
  +
  +                     // Now the decrypt, with a brand new cipher
  +                     XMLCipher cipherDecrypt = XMLCipher.getInstance();
  +                     Key key = 
  +                             new 
SecretKeySpec("abcdefghijklmnop".getBytes("ASCII"), "AES");
  +
  +                     cipherDecrypt.init(XMLCipher.DECRYPT_MODE, key);
  +                     byte[] decryptBytes = 
cipherDecrypt.decryptToByteArray(ee);
  +
  +                     Assert.assertEquals(new String(decryptBytes, "ASCII"), 
  +                                                             new String("A 
test encrypted secret"));
  +             }
  +             else {
  +                     log.warn("Test testSameDocumentCipherReference skipped 
as necessary algorithms not available");
  +             }
   
        }
   
  
  
  

Reply via email to