You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2012/07/24 12:28:33 UTC

svn commit: r1364963 - in /santuario/xml-security-java/trunk/src: main/java/org/apache/xml/security/stax/impl/processor/input/ main/java/org/apache/xml/security/stax/securityEvent/ test/java/org/apache/xml/security/test/stax/encryption/

Author: coheigea
Date: Tue Jul 24 10:28:33 2012
New Revision: 1364963

URL: http://svn.apache.org/viewvc?rev=1364963&view=rev
Log:
Added SecurityEvents for Decryption

Added:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/ContentEncryptedElementSecurityEvent.java
Modified:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/XMLDecryptInputProcessor.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/SecurityEventConstants.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/DecryptionTest.java

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/XMLDecryptInputProcessor.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/XMLDecryptInputProcessor.java?rev=1364963&r1=1364962&r2=1364963&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/XMLDecryptInputProcessor.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/input/XMLDecryptInputProcessor.java Tue Jul 24 10:28:33 2012
@@ -18,16 +18,29 @@
  */
 package org.apache.xml.security.stax.impl.processor.input;
 
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
 import org.apache.xml.security.binding.xmldsig.KeyInfoType;
 import org.apache.xml.security.binding.xmlenc.EncryptedDataType;
 import org.apache.xml.security.binding.xmlenc.ReferenceList;
+import org.apache.xml.security.stax.ext.DocumentContext;
 import org.apache.xml.security.stax.ext.InputProcessorChain;
 import org.apache.xml.security.stax.ext.SecurePart;
 import org.apache.xml.security.stax.ext.SecurityContext;
 import org.apache.xml.security.stax.ext.SecurityToken;
+import org.apache.xml.security.stax.ext.XMLSecurityConstants;
 import org.apache.xml.security.stax.ext.XMLSecurityException;
 import org.apache.xml.security.stax.ext.XMLSecurityProperties;
 import org.apache.xml.security.stax.ext.stax.XMLSecStartElement;
+import org.apache.xml.security.stax.securityEvent.ContentEncryptedElementSecurityEvent;
+import org.apache.xml.security.stax.securityEvent.DefaultTokenSecurityEvent;
+import org.apache.xml.security.stax.securityEvent.EncryptedElementSecurityEvent;
+import org.apache.xml.security.stax.securityEvent.KeyNameTokenSecurityEvent;
+import org.apache.xml.security.stax.securityEvent.KeyValueTokenSecurityEvent;
+import org.apache.xml.security.stax.securityEvent.TokenSecurityEvent;
+import org.apache.xml.security.stax.securityEvent.X509TokenSecurityEvent;
 
 
 /**
@@ -60,37 +73,45 @@ public class XMLDecryptInputProcessor ex
 
     @Override
     protected void handleSecurityToken(
-            SecurityToken securityToken, SecurityContext securityContext, EncryptedDataType encryptedDataType) throws XMLSecurityException {
-        /*
-         *  securityToken.addTokenUsage(SecurityToken.TokenUsage.Encryption);
-        TokenSecurityEvent tokenSecurityEvent = WSSUtils.createTokenSecurityEvent(securityToken);
+            SecurityToken securityToken, SecurityContext securityContext, EncryptedDataType encryptedDataType
+    ) throws XMLSecurityException {
+        securityToken.addTokenUsage(SecurityToken.TokenUsage.Encryption);
+        XMLSecurityConstants.TokenType tokenType = securityToken.getTokenType();
+        
+        TokenSecurityEvent tokenSecurityEvent = null;
+        if (tokenType == XMLSecurityConstants.X509V1Token
+                || tokenType == XMLSecurityConstants.X509V3Token
+                || tokenType == XMLSecurityConstants.X509Pkcs7Token
+                || tokenType == XMLSecurityConstants.X509PkiPathV1Token) {
+            tokenSecurityEvent = new X509TokenSecurityEvent();
+        } else if (tokenType == XMLSecurityConstants.KeyValueToken) {
+            tokenSecurityEvent = new KeyValueTokenSecurityEvent();
+        } else if (tokenType == XMLSecurityConstants.KeyNameToken) {
+            tokenSecurityEvent = new KeyNameTokenSecurityEvent();
+        } else if (tokenType == XMLSecurityConstants.DefaultToken) {
+            tokenSecurityEvent = new DefaultTokenSecurityEvent();
+        } else {
+            throw new XMLSecurityException(XMLSecurityException.ErrorCode.UNSUPPORTED_SECURITY_TOKEN);
+        }
+        
+        tokenSecurityEvent.setSecurityToken(securityToken);
         securityContext.registerSecurityEvent(tokenSecurityEvent);
-         */
     }
     
     @Override
     protected void handleEncryptedContent(InputProcessorChain inputProcessorChain,
                                                    XMLSecStartElement parentXMLSecStartElement,
                                                    SecurityToken securityToken) throws XMLSecurityException {
-        // TODO
-        /*
-         * final DocumentContext documentContext = inputProcessorChain.getDocumentContext();
-        List<QName> elementPath = parentStartXMLEvent.getElementPath();
-        if (elementPath.size() == 2 && WSSUtils.isInSOAPBody(elementPath)) {
-            //soap:body content encryption counts as EncryptedPart
-            EncryptedPartSecurityEvent encryptedPartSecurityEvent =
-                    new EncryptedPartSecurityEvent(securityToken, true, documentContext.getProtectionOrder());
-            encryptedPartSecurityEvent.setElementPath(elementPath);
-            encryptedPartSecurityEvent.setXmlSecEvent(parentStartXMLEvent);
-            ((WSSecurityContext) inputProcessorChain.getSecurityContext()).registerSecurityEvent(encryptedPartSecurityEvent);
-        } else {
-            ContentEncryptedElementSecurityEvent contentEncryptedElementSecurityEvent =
-                    new ContentEncryptedElementSecurityEvent(securityToken, true, documentContext.getProtectionOrder());
-            contentEncryptedElementSecurityEvent.setElementPath(elementPath);
-            contentEncryptedElementSecurityEvent.setXmlSecEvent(parentStartXMLEvent);
-            ((WSSecurityContext) inputProcessorChain.getSecurityContext()).registerSecurityEvent(contentEncryptedElementSecurityEvent);
-        }
-         */
+        final DocumentContext documentContext = inputProcessorChain.getDocumentContext();
+        List<QName> elementPath = parentXMLSecStartElement.getElementPath();
+        
+        ContentEncryptedElementSecurityEvent contentEncryptedElementSecurityEvent =
+                new ContentEncryptedElementSecurityEvent(securityToken, true, documentContext.getProtectionOrder());
+        contentEncryptedElementSecurityEvent.setElementPath(elementPath);
+        contentEncryptedElementSecurityEvent.setXmlSecEvent(parentXMLSecStartElement);
+        
+        contentEncryptedElementSecurityEvent.setSecurityToken(securityToken);
+        inputProcessorChain.getSecurityContext().registerSecurityEvent(contentEncryptedElementSecurityEvent);
     }
 
     /**
@@ -112,13 +133,16 @@ public class XMLDecryptInputProcessor ex
         protected void handleEncryptedElement(InputProcessorChain inputProcessorChain, XMLSecStartElement xmlSecStartElement,
                                               SecurityToken securityToken) throws XMLSecurityException {
             //fire a SecurityEvent:
-            /* TODO
-             *  EncryptedElementSecurityEvent encryptedElementSecurityEvent =
-                        new EncryptedElementSecurityEvent(securityToken, true, documentContext.getProtectionOrder());
-                encryptedElementSecurityEvent.setElementPath(elementPath);
-                encryptedElementSecurityEvent.setXmlSecEvent(xmlSecStartElement);
-                ((WSSecurityContext) inputProcessorChain.getSecurityContext()).registerSecurityEvent(encryptedElementSecurityEvent);
-             */
+            final DocumentContext documentContext = inputProcessorChain.getDocumentContext();
+            List<QName> elementPath = xmlSecStartElement.getElementPath();
+            
+            EncryptedElementSecurityEvent encryptedElementSecurityEvent =
+                    new EncryptedElementSecurityEvent(securityToken, true, documentContext.getProtectionOrder());
+            encryptedElementSecurityEvent.setElementPath(elementPath);
+            encryptedElementSecurityEvent.setXmlSecEvent(xmlSecStartElement);
+            
+            encryptedElementSecurityEvent.setSecurityToken(securityToken);
+            inputProcessorChain.getSecurityContext().registerSecurityEvent(encryptedElementSecurityEvent);
         }
 
     }

Added: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/ContentEncryptedElementSecurityEvent.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/ContentEncryptedElementSecurityEvent.java?rev=1364963&view=auto
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/ContentEncryptedElementSecurityEvent.java (added)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/ContentEncryptedElementSecurityEvent.java Tue Jul 24 10:28:33 2012
@@ -0,0 +1,35 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.xml.security.stax.securityEvent;
+
+import org.apache.xml.security.stax.ext.SecurityToken;
+import org.apache.xml.security.stax.ext.XMLSecurityConstants;
+
+import java.util.List;
+
+/**
+ * @author $Author: coheigea $
+ * @version $Revision: 1359731 $ $Date: 2012-07-10 16:39:40 +0100 (Tue, 10 Jul 2012) $
+ */
+public class ContentEncryptedElementSecurityEvent extends AbstractSecuredElementSecurityEvent {
+
+    public ContentEncryptedElementSecurityEvent(SecurityToken securityToken, boolean encrypted, List<XMLSecurityConstants.ContentType> protectionOrder) {
+        super(SecurityEventConstants.ContentEncrypted, securityToken, protectionOrder, false, encrypted);
+    }
+}

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/SecurityEventConstants.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/SecurityEventConstants.java?rev=1364963&r1=1364962&r2=1364963&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/SecurityEventConstants.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/securityEvent/SecurityEventConstants.java Tue Jul 24 10:28:33 2012
@@ -30,6 +30,8 @@ public class SecurityEventConstants {
     public static final Event X509Token = new Event("X509Token");
     public static final Event AlgorithmSuite = new Event("AlgorithmSuite");
     public static final Event DefaultToken = new Event("DefaultToken");
+    public static final Event ContentEncrypted = new Event("ContentEncrypted");
+    public static final Event EncryptedElement = new Event("EncryptedElement");
 
     public static class Event implements Comparable<Event> {
         private final String name;

Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/DecryptionTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/DecryptionTest.java?rev=1364963&r1=1364962&r2=1364963&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/DecryptionTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/DecryptionTest.java Tue Jul 24 10:28:33 2012
@@ -21,6 +21,8 @@ package org.apache.xml.security.test.sta
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
+import java.security.Key;
+import java.security.cert.X509Certificate;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
@@ -48,7 +50,13 @@ import org.apache.xml.security.keys.KeyI
 import org.apache.xml.security.stax.ext.InboundXMLSec;
 import org.apache.xml.security.stax.ext.XMLSec;
 import org.apache.xml.security.stax.ext.XMLSecurityConstants;
+import org.apache.xml.security.stax.ext.XMLSecurityException;
 import org.apache.xml.security.stax.ext.XMLSecurityProperties;
+import org.apache.xml.security.stax.securityEvent.ContentEncryptedElementSecurityEvent;
+import org.apache.xml.security.stax.securityEvent.DefaultTokenSecurityEvent;
+import org.apache.xml.security.stax.securityEvent.EncryptedElementSecurityEvent;
+import org.apache.xml.security.stax.securityEvent.SecurityEvent;
+import org.apache.xml.security.stax.securityEvent.SecurityEventConstants;
 import org.apache.xml.security.test.dom.DSNamespaceContext;
 import org.apache.xml.security.test.stax.signature.TestSecurityEventListener;
 import org.apache.xml.security.test.stax.utils.StAX2DOM;
@@ -134,7 +142,10 @@ public class DecryptionTest extends org.
         nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
         Assert.assertEquals(nodeList.getLength(), 1);
         
-        // TODO Check the SecurityEvents
+        // Check the SecurityEvents
+        checkEncryptedElementSecurityEvents(securityEventListener);
+        checkEncryptionToken(securityEventListener, null, secretKey, 
+                XMLSecurityConstants.XMLKeyIdentifierType.NO_KEY_INFO);
     }
     
 
@@ -185,7 +196,10 @@ public class DecryptionTest extends org.
         nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
         Assert.assertEquals(nodeList.getLength(), 1);
         
-        // TODO Check the SecurityEvents
+        // Check the SecurityEvents
+        checkEncryptedContentSecurityEvents(securityEventListener);
+        checkEncryptionToken(securityEventListener, null, secretKey, 
+                XMLSecurityConstants.XMLKeyIdentifierType.NO_KEY_INFO);
     }
     
     @Test
@@ -237,7 +251,10 @@ public class DecryptionTest extends org.
         nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
         Assert.assertEquals(nodeList.getLength(), 1);
         
-        // TODO Check the SecurityEvents
+        // Check the SecurityEvents
+        checkEncryptedElementSecurityEvents(securityEventListener);
+        checkEncryptionToken(securityEventListener, null, secretKey, 
+                XMLSecurityConstants.XMLKeyIdentifierType.NO_KEY_INFO);
     }
     
     @Test
@@ -292,7 +309,10 @@ public class DecryptionTest extends org.
         nodeList = document.getElementsByTagNameNS("urn:example:po", "ShippingAddress");
         Assert.assertEquals(nodeList.getLength(), 1);
         
-        // TODO Check the SecurityEvents
+        // Check the SecurityEvents
+        checkMultipleEncryptedElementSecurityEvents(securityEventListener);
+        checkEncryptionToken(securityEventListener, null, secretKey, 
+                XMLSecurityConstants.XMLKeyIdentifierType.NO_KEY_INFO);
     }
     
     // TODO
@@ -490,4 +510,60 @@ public class DecryptionTest extends org.
         Assert.assertTrue(nodeList.getLength() > 0);
     }
     
+    protected void checkEncryptedElementSecurityEvents(TestSecurityEventListener securityEventListener) {
+        EncryptedElementSecurityEvent encryptedElementEvent =
+                (EncryptedElementSecurityEvent) securityEventListener.getTokenEvent(SecurityEventConstants.EncryptedElement);
+        assertNotNull(encryptedElementEvent);
+        assertEquals(encryptedElementEvent.getElementPath().size(), 2);
+        assertEquals("{urn:example:po}PurchaseOrder", encryptedElementEvent.getElementPath().get(0).toString());
+        assertEquals("{urn:example:po}PaymentInfo", encryptedElementEvent.getElementPath().get(1).toString());
+        assertTrue(encryptedElementEvent.isEncrypted());
+    }
+    
+    protected void checkMultipleEncryptedElementSecurityEvents(TestSecurityEventListener securityEventListener) {
+        List<SecurityEvent> encryptedElements =
+                securityEventListener.getTokenEvents(SecurityEventConstants.EncryptedElement);
+        assertTrue(encryptedElements.size() == 2);
+        
+        EncryptedElementSecurityEvent encryptedElementEvent =
+                (EncryptedElementSecurityEvent)encryptedElements.get(0);
+        assertNotNull(encryptedElementEvent);
+        assertEquals(encryptedElementEvent.getElementPath().size(), 2);
+        assertEquals("{urn:example:po}PurchaseOrder", encryptedElementEvent.getElementPath().get(0).toString());
+        assertEquals("{urn:example:po}ShippingAddress", encryptedElementEvent.getElementPath().get(1).toString());
+        assertTrue(encryptedElementEvent.isEncrypted());
+        
+        encryptedElementEvent =
+                (EncryptedElementSecurityEvent)encryptedElements.get(1);
+        assertNotNull(encryptedElementEvent);
+        assertEquals(encryptedElementEvent.getElementPath().size(), 2);
+        assertEquals("{urn:example:po}PurchaseOrder", encryptedElementEvent.getElementPath().get(0).toString());
+        assertEquals("{urn:example:po}PaymentInfo", encryptedElementEvent.getElementPath().get(1).toString());
+        assertTrue(encryptedElementEvent.isEncrypted());
+    }
+    
+    protected void checkEncryptedContentSecurityEvents(TestSecurityEventListener securityEventListener) {
+        ContentEncryptedElementSecurityEvent encryptedElementEvent =
+                (ContentEncryptedElementSecurityEvent) securityEventListener.getTokenEvent(SecurityEventConstants.ContentEncrypted);
+        assertNotNull(encryptedElementEvent);
+        assertEquals(encryptedElementEvent.getElementPath().size(), 2);
+        assertEquals("{urn:example:po}PurchaseOrder", encryptedElementEvent.getElementPath().get(0).toString());
+        assertEquals("{urn:example:po}PaymentInfo", encryptedElementEvent.getElementPath().get(1).toString());
+        assertTrue(encryptedElementEvent.isEncrypted());
+    }
+    
+    protected void checkEncryptionToken(
+            TestSecurityEventListener securityEventListener,
+            X509Certificate cert,
+            Key key,
+            XMLSecurityConstants.XMLKeyIdentifierType keyIdentifierType
+    ) throws XMLSecurityException {
+        if (keyIdentifierType == XMLSecurityConstants.XMLKeyIdentifierType.NO_KEY_INFO) {
+            DefaultTokenSecurityEvent tokenEvent =
+                    (DefaultTokenSecurityEvent) securityEventListener.getTokenEvent(SecurityEventConstants.DefaultToken);
+            assertNotNull(tokenEvent);
+            Key processedKey = tokenEvent.getSecurityToken().getSecretKey("", null);
+            assertEquals(processedKey, key);
+        } 
+    }
 }