Author: coheigea
Date: Fri Jun 17 10:59:35 2011
New Revision: 1136840

URL: http://svn.apache.org/viewvc?rev=1136840&view=rev
Log:
Added the ability to embed an EncryptedKey in an EncryptedData structure

Modified:
    
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/WSSecEncrypt.java
    
webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/EncryptionTest.java

Modified: 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/WSSecEncrypt.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/WSSecEncrypt.java?rev=1136840&r1=1136839&r2=1136840&view=diff
==============================================================================
--- 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/WSSecEncrypt.java
 (original)
+++ 
webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/WSSecEncrypt.java
 Fri Jun 17 10:59:35 2011
@@ -90,6 +90,8 @@ public class WSSecEncrypt extends WSSecE
      * True if the encKeyId is a direct reference to a key identifier instead 
of a URI to a key
      */
     private boolean encKeyIdDirectId;
+    
+    private boolean embedEncryptedKey;
  
     public WSSecEncrypt() {
         super();
@@ -541,7 +543,9 @@ public class WSSecEncrypt extends WSSecE
     private KeyInfo createKeyInfo() throws WSSecurityException {
 
         KeyInfo keyInfo = new KeyInfo(document);
-        if (keyIdentifierType == WSConstants.ENCRYPTED_KEY_SHA1_IDENTIFIER) {
+        if (embedEncryptedKey) {
+            keyInfo.addUnknownElement(getEncryptedKeyElement());
+        } else if (keyIdentifierType == 
WSConstants.ENCRYPTED_KEY_SHA1_IDENTIFIER) {
             SecurityTokenReference secToken = new 
SecurityTokenReference(document);
             secToken.addWSSENamespace();
             if (customReferenceValue != null) {
@@ -686,4 +690,12 @@ public class WSSecEncrypt extends WSSecE
     public void setEncKeyIdDirectId(boolean b) {
         encKeyIdDirectId = b;
     }
+    
+    public void setEmbedEncryptedKey(boolean embedEncryptedKey) {
+        this.embedEncryptedKey = embedEncryptedKey;
+    }
+    
+    public boolean isEmbedEncryptedKey() {
+        return embedEncryptedKey;
+    }
 }

Modified: 
webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/EncryptionTest.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/EncryptionTest.java?rev=1136840&r1=1136839&r2=1136840&view=diff
==============================================================================
--- 
webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/EncryptionTest.java
 (original)
+++ 
webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/EncryptionTest.java
 Fri Jun 17 10:59:35 2011
@@ -565,6 +565,41 @@ public class EncryptionTest extends org.
             assert ex.getMessage().contains("bad attribute");
         }
     }
+    
+    /**
+     * In this test an EncryptedKey structure is embedded in the EncryptedData 
structure.
+     * The EncryptedKey structure refers to a certificate via the 
SKI_KEY_IDENTIFIER.
+     */
+    @org.junit.Test
+    public void testEmbeddedEncryptedKey() throws Exception {
+        Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
+        
+        WSSecEncrypt builder = new WSSecEncrypt();
+        builder.setUserInfo("wss40");
+        builder.setKeyIdentifierType(WSConstants.SKI_KEY_IDENTIFIER);
+        builder.setSymmetricEncAlgorithm(WSConstants.AES_128);
+        builder.prepare(doc, crypto);
+        builder.setEmbedEncryptedKey(true);
+
+        SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(doc
+                .getDocumentElement());
+        java.util.List<WSEncryptionPart> parts = new 
ArrayList<WSEncryptionPart>();
+        WSEncryptionPart encP = new WSEncryptionPart(soapConstants
+                .getBodyQName().getLocalPart(), soapConstants.getEnvelopeURI(),
+                "Content");
+        parts.add(encP);
+
+        builder.encryptForRef(null, parts);
+
+        String outputString = 
+            org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
+        System.out.println(outputString);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug(outputString);
+        }
+        
+        verify(doc, crypto, keystoreCallbackHandler);
+    }
 
     
     /**


Reply via email to