Przeglądaj źródła

2004-03-04 Atsushi Enomoto <[email protected]>

	* KeyInfo.cs :
	  Fixed LoadXml() to skip text nodes as MS.NET does.
	  Uncommented out RSAKeyValue.
	* Signature.cs : Fixed LoadXml(). Don't use GetElementsByTagName()
	  that incorrectly acquires descendants. Throw CryptographicException
	  if required elements were not found.
	* Transform.cs : added internal GetResolver().
	* XmlDsigXPathTransform.cs,
	  XmlDsigXsltTransform.cs : use XmlResolver.

svn path=/trunk/mcs/; revision=23711
Atsushi Eno 22 lat temu
rodzic
commit
a026462b96

+ 12 - 0
mcs/class/System.Security/System.Security.Cryptography.Xml/ChangeLog

@@ -1,3 +1,15 @@
+2004-03-04  Atsushi Enomoto <[email protected]>
+
+	* KeyInfo.cs :
+	  Fixed LoadXml() to skip text nodes as MS.NET does.
+	  Uncommented out RSAKeyValue.
+	* Signature.cs : Fixed LoadXml(). Don't use GetElementsByTagName()
+	  that incorrectly acquires descendants. Throw CryptographicException
+	  if required elements were not found.
+	* Transform.cs : added internal GetResolver().
+	* XmlDsigXPathTransform.cs,
+	  XmlDsigXsltTransform.cs : use XmlResolver.
+
 2004-02-19  Tim Coleman <[email protected]>
 	* SymmetricKeyWrap.cs:
 		Add AES Key Wrap

+ 6 - 4
mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfo.cs

@@ -78,10 +78,11 @@ namespace System.Security.Cryptography.Xml {
 
 			if ((value.LocalName == XmlSignature.ElementNames.KeyInfo) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
 				foreach (XmlNode n in value.ChildNodes) {
-					KeyInfoClause kic = null;
-					if (n is XmlWhitespace)
+					if (n.NodeType != XmlNodeType.Element)
 						continue;
 
+					KeyInfoClause kic = null;
+
 					switch (n.LocalName) {
 					case XmlSignature.ElementNames.KeyValue:
 						XmlNodeList xnl = n.ChildNodes;
@@ -108,9 +109,9 @@ namespace System.Security.Cryptography.Xml {
 					case XmlSignature.ElementNames.X509Data:
 						kic = (KeyInfoClause) new KeyInfoX509Data ();
 						break;
-/*					case XmlSignature.ElementNames.RSAKeyValue:
+					case XmlSignature.ElementNames.RSAKeyValue:
 						kic = (KeyInfoClause) new RSAKeyValue ();
-						break;*/
+						break;
 					default:
 						kic = (KeyInfoClause) new KeyInfoNode ();
 						break;
@@ -122,6 +123,7 @@ namespace System.Security.Cryptography.Xml {
 					}
 				}
 			}
+			// No check is performed on MS.NET...
 		}
 	}
 }

+ 46 - 19
mcs/class/System.Security/System.Security.Cryptography.Xml/Signature.cs

@@ -14,6 +14,13 @@ using System.Xml;
 namespace System.Security.Cryptography.Xml {
 
 	public class Signature {
+		static XmlNamespaceManager dsigNsmgr;
+		
+		static Signature ()
+		{
+			dsigNsmgr = new XmlNamespaceManager (new NameTable ());
+			dsigNsmgr.AddNamespace ("xd", XmlSignature.NamespaceURI);
+		}
 
 		private ArrayList list;
 		private SignedInfo info;
@@ -109,32 +116,32 @@ namespace System.Security.Cryptography.Xml {
 			if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
 				id = GetAttribute (value, XmlSignature.AttributeNames.Id);
 
-				XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignedInfo);
-				if ((xnl != null) && (xnl.Count == 1)) {
-					info = new SignedInfo ();
-					info.LoadXml ((XmlElement) xnl[0]);
-				}
+				// LAMESPEC: This library is totally useless against eXtensibly Marked-up document.
+				int i = NextElementPos (value.ChildNodes, 0, XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI, true);
+				XmlElement sinfo = (XmlElement) value.ChildNodes [i];
+				info = new SignedInfo ();
+				info.LoadXml (sinfo);
 
-				xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignatureValue);
-				if ((xnl != null) && (xnl.Count == 1)) {
-					signature = Convert.FromBase64String (xnl[0].InnerText);
-				}
+				i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI, true);
+				XmlElement sigValue = (XmlElement) value.ChildNodes [i];
+				signature = Convert.FromBase64String (sigValue.InnerText);
 
-				xnl = value.GetElementsByTagName (XmlSignature.ElementNames.KeyInfo);
-				if ((xnl != null) && (xnl.Count == 1)) {
+				i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI, true);
+				if (i > 0) {
+					XmlElement kinfo = (XmlElement) value.ChildNodes [i];
 					key = new KeyInfo ();
-					key.LoadXml ((XmlElement) xnl[0]);
+					key.LoadXml (kinfo);
 				}
 
-				xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Object);
-				if ((xnl != null) && (xnl.Count > 0)) {
-					foreach (XmlNode xn in xnl) {
-						DataObject obj = new DataObject ();
-						obj.LoadXml ((XmlElement) xn);
-						AddObject (obj);
-					}
+				XmlNodeList xnl = value.SelectNodes ("xd:Object", dsigNsmgr);
+				foreach (XmlElement xn in xnl) {
+					DataObject obj = new DataObject ();
+					obj.LoadXml (xn);
+					AddObject (obj);
 				}
 			}
+			else
+				throw new CryptographicException ("Malformed element: Signature.");
 
 			// if invalid
 			if (info == null)
@@ -142,5 +149,25 @@ namespace System.Security.Cryptography.Xml {
 			if (signature == null)
 				throw new CryptographicException ("SignatureValue");
 		}
+
+		private int NextElementPos (XmlNodeList nl, int pos, string name, string ns, bool required)
+		{
+			while (pos < nl.Count) {
+				if (nl [pos].NodeType == XmlNodeType.Element) {
+					if (nl [pos].LocalName != name && nl [pos].NamespaceURI != ns) {
+						if (required)
+							throw new CryptographicException ("Malformed element " + name);
+						else
+							return -2;
+					}
+					return pos;
+				}
+				else
+					pos++;
+			}
+			if (required)
+				throw new CryptographicException ("Malformed element " + name);
+			return -1;
+		}
 	}
 }

+ 5 - 1
mcs/class/System.Security/System.Security.Cryptography.Xml/Transform.cs

@@ -60,11 +60,15 @@ namespace System.Security.Cryptography.Xml {
 #if ! NET_1_0
 		private XmlResolver xmlResolver;
 
-		[MonoTODO("property not (yet) used in derived classes")]
 		[ComVisible(false)]
 		public XmlResolver Resolver {
 			set { xmlResolver = value; }
 		}
+		
+		internal XmlResolver GetResolver ()
+		{
+			return xmlResolver;
+		}
 #endif
 	}
 }

+ 2 - 0
mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigXPathTransform.cs

@@ -104,6 +104,7 @@ namespace System.Security.Cryptography.Xml {
 			// possible input: Stream, XmlDocument, and XmlNodeList
 			if (obj is Stream) {
 				doc = new XmlDocument ();
+				doc.XmlResolver = GetResolver ();
 				doc.Load (obj as Stream);
 			}
 			else if (obj is XmlDocument) {
@@ -111,6 +112,7 @@ namespace System.Security.Cryptography.Xml {
 			}
 			else if (obj is XmlNodeList) {
 				doc = new XmlDocument ();
+				doc.XmlResolver = GetResolver ();
 				foreach (XmlNode xn in (obj as XmlNodeList))  {
 					XmlNode importedNode = doc.ImportNode (xn, true);
 					doc.AppendChild (importedNode);

+ 2 - 2
mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigXsltTransform.cs

@@ -90,6 +90,7 @@ namespace System.Security.Cryptography.Xml {
 		{
 			XslTransform xsl = new XslTransform ();
 			XmlDocument doc = new XmlDocument ();
+			doc.XmlResolver = GetResolver ();
 			Stream stream = null;
 
 			// possible input: Stream, XmlDocument, and XmlNodeList
@@ -101,8 +102,7 @@ namespace System.Security.Cryptography.Xml {
 				xsl.Load (obj as XmlDocument);
 			}
 			else if (obj is XmlNodeList) {
-//				xnl = (XmlNodeList) obj;
-//				xsl.Load (obj a);
+				// Is it valid operation?
 			}
 
 			if (xnl != null) {