You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by ra...@apache.org on 2006/08/03 18:05:20 UTC

svn commit: r428439 - in /xml/security/trunk/src/org/apache/xml/security/c14n/implementations: Canonicalizer20010315.java Canonicalizer20010315Excl.java CanonicalizerBase.java NameSpaceSymbTable.java

Author: raul
Date: Thu Aug  3 09:05:19 2006
New Revision: 428439

URL: http://svn.apache.org/viewvc?rev=428439&view=rev
Log:
Make the c14n works without having the tree circumbented

Modified:
    xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java
    xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java
    xml/security/trunk/src/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
    xml/security/trunk/src/org/apache/xml/security/c14n/implementations/NameSpaceSymbTable.java

Modified: xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java
URL: http://svn.apache.org/viewvc/xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java?rev=428439&r1=428438&r2=428439&view=diff
==============================================================================
--- xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java (original)
+++ xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java Thu Aug  3 09:05:19 2006
@@ -19,6 +19,7 @@
 
 
 
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
@@ -26,13 +27,19 @@
 import java.util.SortedSet;
 import java.util.TreeSet;
 
+import javax.xml.parsers.ParserConfigurationException;
+
 import org.apache.xml.security.c14n.CanonicalizationException;
 import org.apache.xml.security.c14n.helper.C14nHelper;
+import org.apache.xml.security.signature.XMLSignatureInput;
 import org.apache.xml.security.utils.Constants;
+import org.apache.xml.security.utils.XMLUtils;
 import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
 
 
 /**
@@ -189,7 +196,6 @@
     
     SortedSet result = this.result;       
     result.clear();
-    
             
     for (int i = 0; i < attrsLength; i++) {
        Attr N = (Attr) attrs.item(i);
@@ -217,16 +223,26 @@
        //add the prefix binding to the ns symb table.
        //ns.addInclusiveMapping(NName,NValue,N,isRealVisible);          
 	    if  (isVisible(N))  {
-			    //The xpath select this node output it if needed.
-	    		Node n=ns.addMappingAndRenderXNodeSet(NName,NValue,N,isRealVisible); 	    		
-		 	 	if (n!=null) {
+	    	if (!isRealVisible && ns.removeMappingIfRender(NName)) {
+	    		continue;
+	    	} 
+			//The xpath select this node output it if needed.
+	    	//Node n=ns.addMappingAndRenderXNodeSet(NName,NValue,N,isRealVisible);
+	    	Node n=ns.addMappingAndRender(NName,NValue,N);
+		 	if (n!=null) {
 		 	 		result.add(n);
                     if (C14nHelper.namespaceIsRelative(N)) {
                        Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() };
                        throw new CanonicalizationException(
                           "c14n.Canonicalizer.RelativeNamespace", exArgs);
-                    }
-		 	 	}
+                   }
+		 	 }
+    	} else {
+    		if (isRealVisible && NName!=XMLNS) {
+    			ns.removeMapping(NName);	
+    		} else {
+    			ns.addMapping(NName,NValue,N);
+    		}
     	}
     }
     if (isRealVisible) {    	           
@@ -239,7 +255,7 @@
         } else if ( !isVisible(xmlns)) {
         	//There is a definition but the xmlns is not selected by the xpath.
         	//then xmlns=""
-        	n=ns.addMappingAndRenderXNodeSet(XMLNS,"",nullNode,true);        	    		      	
+        	n=ns.addMappingAndRender(XMLNS,"",nullNode);        	    		      	
         }
         //output the xmlns def if needed.
         if (n!=null) {
@@ -247,6 +263,8 @@
     	}
         //Float all xml:* attributes of the unselected parent elements to this one. 
     	addXmlAttributes(E,result);
+    	ns.getUnrenderedNodes(result);
+        
     }
     
     return result.iterator();
@@ -336,5 +354,17 @@
       /** $todo$ well, should we throw UnsupportedOperationException ? */
       throw new CanonicalizationException(
          "c14n.Canonicalizer.UnsupportedOperation");
+   }
+   void circumventBugIfNeeded(XMLSignatureInput input) throws CanonicalizationException, ParserConfigurationException, IOException, SAXException {
+	   if (!input.isNeedsToBeExpanded()) 
+		   return;
+	   Document doc = null;
+       if (input.getSubNode() != null) {
+           doc=XMLUtils.getOwnerDocument(input.getSubNode());
+       } else {
+           doc=XMLUtils.getOwnerDocument(input.getNodeSet());
+       }
+	   XMLUtils.circumventBug2650(doc);
+		
    }
 }

Modified: xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java
URL: http://svn.apache.org/viewvc/xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java?rev=428439&r1=428438&r2=428439&view=diff
==============================================================================
--- xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java (original)
+++ xml/security/trunk/src/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java Thu Aug  3 09:05:19 2006
@@ -17,20 +17,26 @@
  */
 package org.apache.xml.security.c14n.implementations;
 
+import java.io.IOException;
 import java.util.Iterator;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
+import javax.xml.parsers.ParserConfigurationException;
+
 import org.apache.xml.security.c14n.CanonicalizationException;
 import org.apache.xml.security.c14n.helper.C14nHelper;
 import org.apache.xml.security.signature.XMLSignatureInput;
 import org.apache.xml.security.transforms.params.InclusiveNamespaces;
 import org.apache.xml.security.utils.Constants;
+import org.apache.xml.security.utils.XMLUtils;
 import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
 /**
  * Implements &quot; <A
  * HREF="http://www.w3.org/TR/2002/REC-xml-exc-c14n-20020718/">Exclusive XML
@@ -165,17 +171,17 @@
                 }
             }
 		}		
-							
+		String prefix;
 		if (E.getNamespaceURI() != null) {
-			String prefix = E.getPrefix();
+			prefix = E.getPrefix();
 			if ((prefix == null) || (prefix.length() == 0)) {
-				visiblyUtilized.add(XMLNS);
-			} else {
-				visiblyUtilized.add(prefix);
+				prefix=XMLNS;
 			}
+						
 		} else {
-			visiblyUtilized.add(XMLNS);
+			prefix=XMLNS;
 		}
+		visiblyUtilized.add(prefix);
 									
 		//This can be optimezed by I don't have time
 		Iterator it=visiblyUtilized.iterator();
@@ -234,12 +240,13 @@
 		
 		for (int i = 0; i < attrsLength; i++) {
 			Attr N = (Attr) attrs.item(i);
-			if ( !isVisible(N) )  {
-				//The node is not in the nodeset(if there is a nodeset)
-				continue;
-			}			
+						
 						
 			if (XMLNS_URI!=N.getNamespaceURI()) {
+				if ( !isVisible(N) )  {
+					//The node is not in the nodeset(if there is a nodeset)
+					continue;
+				}
 				//Not a namespace definition.
 				if (isOutputElement) {
 					//The Element is output element, add his prefix(if used) to visibyUtilized
@@ -252,10 +259,26 @@
 				}
 				continue;
 			}
-
 			String NName=N.getLocalName();
+			if (isOutputElement && !isVisible(N) && NName!=XMLNS) {
+    			ns.removeMappingIfNotRender(NName);
+    			continue;
+    		}
 			String NNodeValue=N.getNodeValue();
-
+			
+			if (!isOutputElement && isVisible(N) && _inclusiveNSSet.contains(NName) && !ns.removeMappingIfRender(NName)) {
+				Node n=ns.addMappingAndRender(NName,NNodeValue,N);
+			 	if (n!=null) {
+			 	 		result.add(n);
+	                    if (C14nHelper.namespaceIsRelative(N)) {
+	                       Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() };
+	                       throw new CanonicalizationException(
+	                          "c14n.Canonicalizer.RelativeNamespace", exArgs);
+	                   }
+			 	 }
+			}
+						
+			
 			
 			if (ns.addMapping(NName, NNodeValue,N)) {
                 //New definiton check if it is relative
@@ -298,18 +321,20 @@
 				}
 				result.add(key);
 			}
-		} else /*if (_circunvented)*/ {			
-			Iterator it=this._inclusiveNSSet.iterator();
-			while (it.hasNext()) {
-				String s=(String)it.next();				
-				Attr key=ns.getMappingWithoutRendered(s);
-				if (key==null) {
-					continue;
-				}
-				result.add(key);								
-			}
-		}
+		} 
 
 		return result.iterator(); 
 	}
+	void circumventBugIfNeeded(XMLSignatureInput input) throws CanonicalizationException, ParserConfigurationException, IOException, SAXException {
+		if (!input.isNeedsToBeExpanded() || _inclusiveNSSet.isEmpty()) 
+			return;
+		Document doc = null;
+	       if (input.getSubNode() != null) {
+	           doc=XMLUtils.getOwnerDocument(input.getSubNode());
+	       } else {
+	           doc=XMLUtils.getOwnerDocument(input.getNodeSet());
+	       }
+		   
+		XMLUtils.circumventBug2650(doc);
+	   }
 }

Modified: xml/security/trunk/src/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
URL: http://svn.apache.org/viewvc/xml/security/trunk/src/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java?rev=428439&r1=428438&r2=428439&view=diff
==============================================================================
--- xml/security/trunk/src/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java (original)
+++ xml/security/trunk/src/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java Thu Aug  3 09:05:19 2006
@@ -43,7 +43,6 @@
 import org.apache.xml.security.utils.XMLUtils;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
@@ -142,15 +141,9 @@
 				return bytes;
 			} else if (input.isNodeSet()) {
 				nodeFilter=input.getNodeFilters();
-                                Document doc = null;
-                                if (input.getSubNode() != null) {
-                                    doc=XMLUtils.getOwnerDocument(input.getSubNode());
-                                } else {
-                                    doc=XMLUtils.getOwnerDocument(input.getNodeSet());
-                                }
-				if (input.isNeedsToBeExpanded()) {
-					XMLUtils.circumventBug2650(doc);
-				}
+                                
+                circumventBugIfNeeded(input);
+				
 
 				if (input.getSubNode() != null) {
 				    bytes = engineCanonicalizeXPathNodeSetInternal(input.getSubNode());
@@ -386,6 +379,8 @@
 		return;
 	boolean currentNodeIsVisible = false;	  
 	NameSpaceSymbTable ns=new  NameSpaceSymbTable();
+	if (currentNode instanceof Element)
+		getParentNameSpaces((Element)currentNode,ns);
   	Node sibling=null;
 	Node parentNode=null;	
 	OutputStream writer=this._writer;
@@ -565,13 +560,13 @@
    	 * @param ns
    	 */
    	final static void getParentNameSpaces(Element el,NameSpaceSymbTable ns)  {
-   		List parents=new ArrayList();
+   		List parents=new ArrayList(10);
    		Node n1=el.getParentNode();
    		if (!(n1 instanceof Element)) {
    			return;
    		}
    		//Obtain all the parents of the elemnt
-   		Element parent=(Element) el.getParentNode();
+   		Element parent=(Element) n1;
    		while (parent!=null) {
    			parents.add(parent);
    			Node n=parent.getParentNode();
@@ -583,27 +578,27 @@
    		//Visit them in reverse order.
    		ListIterator it=parents.listIterator(parents.size());
    		while (it.hasPrevious()) {
-   		Element ele=(Element)it.previous();
-        if (!ele.hasAttributes()) {
-        	continue;
-        }
-		NamedNodeMap attrs = ele.getAttributes();
-   		int attrsLength = attrs.getLength();
-   		 for (int i = 0; i < attrsLength; i++) {
-            Attr N = (Attr) attrs.item(i);
-            if (Constants.NamespaceSpecNS!=N.getNamespaceURI()) {
-               //Not a namespace definition, ignore.
-               continue;
-            }
-
-            String NName=N.getLocalName();
-            String NValue=N.getNodeValue();
-            if (XML.equals(NName)
+   			Element ele=(Element)it.previous();
+   			if (!ele.hasAttributes()) {
+   				continue;
+   			}
+   			NamedNodeMap attrs = ele.getAttributes();
+   			int attrsLength = attrs.getLength();
+   			for (int i = 0; i < attrsLength; i++) {
+   				Attr N = (Attr) attrs.item(i);
+   				if (Constants.NamespaceSpecNS!=N.getNamespaceURI()) {
+   					//Not a namespace definition, ignore.
+   					continue;
+   				}
+
+   				String NName=N.getLocalName();
+   				String NValue=N.getNodeValue();
+   				if (XML.equals(NName)
                     && Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
-               continue;
-            }            
-            ns.addMapping(NName,NValue,N);             
-   		 }   			
+   					continue;
+   				}            
+   				ns.addMapping(NName,NValue,N);             
+   			}   			
    		}
         Attr nsprefix;
         if (((nsprefix=ns.getMappingWithoutRendered("xmlns"))!=null) 
@@ -947,6 +942,7 @@
    abstract Iterator handleAttributesSubtree(Element E, NameSpaceSymbTable ns)
    throws CanonicalizationException;
 
+   abstract void circumventBugIfNeeded(XMLSignatureInput input) throws CanonicalizationException, ParserConfigurationException, IOException, SAXException;
 
     
     /**

Modified: xml/security/trunk/src/org/apache/xml/security/c14n/implementations/NameSpaceSymbTable.java
URL: http://svn.apache.org/viewvc/xml/security/trunk/src/org/apache/xml/security/c14n/implementations/NameSpaceSymbTable.java?rev=428439&r1=428438&r2=428439&view=diff
==============================================================================
--- xml/security/trunk/src/org/apache/xml/security/c14n/implementations/NameSpaceSymbTable.java (original)
+++ xml/security/trunk/src/org/apache/xml/security/c14n/implementations/NameSpaceSymbTable.java Thu Aug  3 09:05:19 2006
@@ -45,7 +45,7 @@
 	static final String XMLNS="xmlns";
 	final static SymbMap initialMap=new SymbMap();
 	static {
-		NameSpaceSymbEntry ne=new NameSpaceSymbEntry("",null,true);
+		NameSpaceSymbEntry ne=new NameSpaceSymbEntry("",null,true,XMLNS);
 		ne.lastrendered="";		
 		initialMap.put(XMLNS,ne);
 	}
@@ -70,9 +70,14 @@
 	   		NameSpaceSymbEntry n=(NameSpaceSymbEntry)(it.next());
 	   		//put them rendered?
 	   		if ((!n.rendered) && (n.n!=null)) {
+	   			n=(NameSpaceSymbEntry) n.clone();
+                needsClone();
+                symb.put(n.prefix,n);         
+                n.lastrendered=n.uri;
+                n.rendered=true;
+                
 	   			result.add(n.n);
-	   			n.rendered=true;
-	   			n.lastrendered=n.uri;
+	   			
 	   		}
 	   }	   
 	}
@@ -191,7 +196,7 @@
 			return false;
 		}			
 		//Creates and entry in the table for this new definition.
-		NameSpaceSymbEntry ne=new NameSpaceSymbEntry(uri,n,false);		
+		NameSpaceSymbEntry ne=new NameSpaceSymbEntry(uri,n,false,prefix);		
         needsClone();
 		symb.put(prefix, ne);
 		if (ob != null) {
@@ -229,7 +234,7 @@
             return null;
         }   
         
-        NameSpaceSymbEntry ne=new NameSpaceSymbEntry(uri,n,true);
+        NameSpaceSymbEntry ne=new NameSpaceSymbEntry(uri,n,true,prefix);
         ne.lastrendered=uri;
         needsClone();
         symb.put(prefix, ne);
@@ -242,69 +247,50 @@
         }       
         return ne.n;
     }
-	/** 
-     * Adds & gets(if needed) the attribute node that defines the binding for the prefix. 
-     * Take on account if the rules of rendering in the inclusive c14n.
-     * For inclusive c14n.
-     * @param prefix the prefix to obtain the attribute.
-     * @param outputNode the container element is an output element.
-     * @param uri the Uri of the definition
-     * @param n the attribute that have the definition
-     * @return null if there is no need to render the prefix. Otherwise the node of
-     * definition.     
-     **/
-	public Node addMappingAndRenderXNodeSet(String prefix, String uri,Attr n,boolean outputNode) {						
-		NameSpaceSymbEntry ob = symb.get(prefix);
-		int visibleNameSpaces=nameSpaces;		
-		if ((ob!=null) && uri.equals(ob.uri)) {
-			if (!ob.rendered) {					
-				ob=(NameSpaceSymbEntry)ob.clone();
-                needsClone();
-                symb.put(prefix,ob);				
-				ob.rendered=true;
-				ob.level=visibleNameSpaces;
-				return ob.n;
-			}						
-            ob=(NameSpaceSymbEntry)ob.clone();
-            needsClone();
-            symb.put(prefix,ob);
-			if (outputNode && (((visibleNameSpaces-ob.level)<2) || XMLNS.equals(prefix)) ) {
-				ob.level=visibleNameSpaces;
-				return null; //Already rendered, just return nulll
-			}
-			ob.level=visibleNameSpaces;
-			return ob.n;
-		}	
-		
-		NameSpaceSymbEntry ne=new NameSpaceSymbEntry(uri,n,true);
-		ne.level=nameSpaces;
-		ne.rendered=true;
-        needsClone();
-		symb.put(prefix, ne);
-		if (ob != null) {			
-			ne.lastrendered=ob.lastrendered;
-			
-			if ((ob.lastrendered!=null)&& (ob.lastrendered.equals(uri))) {
-				ne.rendered=true;
-			}
-		}	
-		return ne.n;
-	}
 
 	public int getLevel() {
 		// TODO Auto-generated method stub
 		return level.size();
 	}
+
+	public void removeMapping(String prefix) {
+		NameSpaceSymbEntry ob = symb.get(prefix);
+        
+        if (ob!=null) {
+            needsClone();
+            symb.put(prefix,null);         
+        }
+	}
+
+	public void removeMappingIfNotRender(String prefix) {
+		NameSpaceSymbEntry ob = symb.get(prefix);
+        
+        if (ob!=null && !ob.rendered) {
+            needsClone();
+            symb.put(prefix,null);         
+        }
+	}
+
+	public boolean removeMappingIfRender(String prefix) {
+		NameSpaceSymbEntry ob = symb.get(prefix);
+        
+        if (ob!=null && ob.rendered) {
+            needsClone();
+            symb.put(prefix,null);         
+        }
+        return false;
+	}
 }
 
 /**
  * The internal structure of NameSpaceSymbTable.
  **/
 class NameSpaceSymbEntry implements Cloneable {
-    NameSpaceSymbEntry(String name,Attr n,boolean rendered) {
+    NameSpaceSymbEntry(String name,Attr n,boolean rendered,String prefix) {
         this.uri=name;          
         this.rendered=rendered;
         this.n=n;            
+        this.prefix=prefix;
     }
     /** @inheritDoc */
     public Object clone() {         
@@ -316,6 +302,7 @@
     }
     /** The level where the definition was rendered(Only for inclusive) */
     int level=0;
+    String prefix;
     /**The URI that the prefix defines */
     String uri;
     /**The last output in the URI for this prefix (This for speed reason).*/