Преглед изворни кода

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

	* Reference.cs, Signature.cs : They should keep input element and hence
	  keep namespace context nodes on LoadXml() as same as SignedInfo.
	* SignedXml.cs : Fixed SignedInfoTransformed() to fill context
	  namespace nodes into the input document (unlike other classes, there
	  seems no other way to keep ns-nodes here).
	  Now some of W3C phaos xmldsig test passes.

svn path=/trunk/mcs/; revision=24532
Atsushi Eno пре 22 година
родитељ
комит
f4e851c96f

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

@@ -1,3 +1,12 @@
+2004-03-24  Atsushi Enomoto <[email protected]>
+
+	* Reference.cs, Signature.cs : They should keep input element and hence
+	  keep namespace context nodes on LoadXml() as same as SignedInfo.
+	* SignedXml.cs : Fixed SignedInfoTransformed() to fill context 
+	  namespace nodes into the input document (unlike other classes, there
+	  seems no other way to keep ns-nodes here).
+	  Now some of W3C phaos xmldsig test passes.
+
 2004-03-23  Atsushi Enomoto <[email protected]>
 
 	* SignedInfo.cs : It holds input element and directly returns it

+ 28 - 6
mcs/class/System.Security/System.Security.Cryptography.Xml/Reference.cs

@@ -22,6 +22,8 @@ namespace System.Security.Cryptography.Xml {
 		private string id;
 		private string uri;
 		private string type;
+		private Stream stream;
+		private XmlElement element;
 
 		public Reference () 
 		{
@@ -29,9 +31,10 @@ namespace System.Security.Cryptography.Xml {
 			digestMethod = XmlSignature.NamespaceURI + "sha1";
 		}
 
-		[MonoTODO()]
+		[MonoTODO ("There is no description about how it is used.")]
 		public Reference (Stream stream) : this () 
 		{
+			this.stream = stream;
 		}
 
 		public Reference (string uri) : this ()
@@ -42,17 +45,26 @@ namespace System.Security.Cryptography.Xml {
 		// default to SHA1
 		public string DigestMethod {
 			get { return digestMethod; }
-			set { digestMethod = value; }
+			set {
+				element = null;
+				digestMethod = value;
+			}
 		}
 
 		public byte[] DigestValue {
 			get { return digestValue; }
-			set { digestValue = value; }
+			set {
+				element = null;
+				digestValue = value;
+			}
 		}
 
 		public string Id {
 			get { return id; }
-			set { id = value; }
+			set {
+				element = null;
+				id = value;
+			}
 		}
 
 		public TransformChain TransformChain {
@@ -61,12 +73,18 @@ namespace System.Security.Cryptography.Xml {
 
 		public string Type {
 			get { return type; }
-			set { type = value; }
+			set {
+				element = null;
+				type = value;
+			}
 		}
 
 		public string Uri {
 			get { return uri; }
-			set { uri = value; }
+			set {
+				element = null;
+				uri = value;
+			}
 		}
 
 		public void AddTransform (Transform transform) 
@@ -76,6 +94,9 @@ namespace System.Security.Cryptography.Xml {
 
 		public XmlElement GetXml () 
 		{
+			if (element != null)
+				return element;
+
 			if (digestMethod == null)
 				throw new CryptographicException ("DigestMethod");
 			if (digestValue == null)
@@ -169,6 +190,7 @@ namespace System.Security.Cryptography.Xml {
 			XmlElement dig = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.DigestValue, XmlSignature.NamespaceURI);
 			if (dig != null)
 				DigestValue = Convert.FromBase64String (dig.InnerText);
+			element = value;
 		}
 	}
 }

+ 21 - 5
mcs/class/System.Security/System.Security.Cryptography.Xml/Signature.cs

@@ -27,20 +27,27 @@ namespace System.Security.Cryptography.Xml {
 		private KeyInfo key;
 		private string id;
 		private byte[] signature;
+		private XmlElement element;
 
-		public Signature() 
+		public Signature () 
 		{
 			list = new ArrayList ();
 		}
 
 		public string Id {
 			get { return id; }
-			set { id = value; }
+			set {
+				element = null;
+				id = value;
+			}
 		}
 
 		public KeyInfo KeyInfo {
 			get { return key; }
-			set { key = value; }
+			set {
+				element = null;
+				key = value;
+			}
 		}
 
 		public IList ObjectList {
@@ -50,12 +57,18 @@ namespace System.Security.Cryptography.Xml {
 
 		public byte[] SignatureValue {
 			get { return signature; }
-			set { signature = value; }
+			set {
+				element = null;
+				signature = value;
+			}
 		}
 
 		public SignedInfo SignedInfo {
 			get { return info; }
-			set { info = value; }
+			set {
+				element = null;
+				info = value;
+			}
 		}
 
 		public void AddObject (DataObject dataObject) 
@@ -65,6 +78,9 @@ namespace System.Security.Cryptography.Xml {
 
 		public XmlElement GetXml () 
 		{
+			if (element != null)
+				return element;
+
 			if (info == null)
 				throw new CryptographicException ("SignedInfo");
 			if (signature == null)

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

@@ -218,8 +218,29 @@ namespace System.Security.Cryptography.Xml {
 			else {
 				// when verifying signatures
 				// TODO - check signature.SignedInfo.Id
-				XmlNodeList xnl = signatureElement.GetElementsByTagName (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
-				byte[] si = Encoding.UTF8.GetBytes (xnl [0].OuterXml);
+				XmlElement el = signatureElement.GetElementsByTagName (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI) [0] as XmlElement;
+				StringWriter sw = new StringWriter ();
+				XmlTextWriter xtw = new XmlTextWriter (sw);
+				xtw.WriteStartElement (el.Prefix, el.LocalName, el.NamespaceURI);
+
+				// context namespace nodes (except for "xmlns:xml")
+				XmlNodeList nl = el.SelectNodes ("namespace::*");
+				foreach (XmlAttribute attr in nl) {
+					if (attr.ParentNode == el)
+						continue;
+					if (attr.LocalName == "xml")
+						continue;
+					attr.WriteTo (xtw);
+				}
+
+				foreach (XmlNode attr in el.Attributes)
+					attr.WriteTo (xtw);
+				foreach (XmlNode n in el.ChildNodes)
+					n.WriteTo (xtw);
+
+				xtw.WriteEndElement ();
+
+				byte [] si = Encoding.UTF8.GetBytes (sw.ToString ());
 
 				MemoryStream ms = new MemoryStream ();
 				ms.Write (si, 0, si.Length);
@@ -231,6 +252,25 @@ namespace System.Security.Cryptography.Xml {
 			return (Stream) t.GetOutput ();
 		}
 
+/*
+		private void CollectDescendants (XmlNode n, ArrayList al)
+		{
+			switch (n.NodeType) {
+			case XmlNodeType.EntityReference:
+				break;
+			default:
+				al.Add (n);
+				break;
+			}
+
+			if (n.Attributes != null)
+				foreach (XmlAttribute a in n.Attributes)
+					al.Add (a);
+			foreach (XmlNode c in n.ChildNodes)
+				CollectDescendants (c, al);
+		}
+*/
+
 		// reuse hash - most document will always use the same hash
 		private HashAlgorithm GetHash (string algorithm) 
 		{