XmlDsigXPathTransform.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // XmlDsigXPathTransform.cs -
  3. // XmlDsigXPathTransform implementation for XML Signature
  4. // http://www.w3.org/TR/1999/REC-xpath-19991116
  5. //
  6. // Author:
  7. // Sebastien Pouliot <[email protected]>
  8. // Atsushi Enomoto <[email protected]>
  9. //
  10. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  11. // (C) 2004 Novell (http://www.novell.com)
  12. //
  13. using System.Collections;
  14. using System.IO;
  15. using System.Text;
  16. using System.Xml;
  17. using System.Xml.XPath;
  18. using System.Xml.Xsl;
  19. namespace System.Security.Cryptography.Xml
  20. {
  21. // www.w3.org/TR/xmldsig-core/
  22. // see Section 6.6.3 of the XMLDSIG specification
  23. public class XmlDsigXPathTransform : Transform
  24. {
  25. private Type [] input;
  26. private Type [] output;
  27. private XmlNodeList xpath;
  28. private XmlDocument doc;
  29. private XsltContext ctx;
  30. public XmlDsigXPathTransform ()
  31. {
  32. Algorithm = "http://www.w3.org/TR/1999/REC-xpath-19991116";
  33. }
  34. public override Type [] InputTypes {
  35. get {
  36. if (input == null) {
  37. lock (this) {
  38. // this way the result is cached if called multiple time
  39. input = new Type [3];
  40. input [0] = typeof (System.IO.Stream);
  41. input [1] = typeof (System.Xml.XmlDocument);
  42. input [2] = typeof (System.Xml.XmlNodeList);
  43. }
  44. }
  45. return input;
  46. }
  47. }
  48. public override Type[] OutputTypes {
  49. get {
  50. if (output == null) {
  51. lock (this) {
  52. // this way the result is cached if called multiple time
  53. output = new Type [1];
  54. output [0] = typeof (System.Xml.XmlNodeList);
  55. }
  56. }
  57. return output;
  58. }
  59. }
  60. protected override XmlNodeList GetInnerXml ()
  61. {
  62. if (xpath == null) {
  63. // default value
  64. XmlDocument xpdoc = new XmlDocument ();
  65. xpdoc.LoadXml ("<XPath xmlns=\"" + XmlSignature.NamespaceURI + "\"></XPath>");
  66. xpath = xpdoc.ChildNodes;
  67. }
  68. return xpath;
  69. }
  70. [MonoTODO ("Evaluation of extension function here() results in different from MS.NET (is MS.NET really correct??).")]
  71. public override object GetOutput ()
  72. {
  73. if (xpath == null)
  74. return new XmlDsigNodeList (new ArrayList ());
  75. // evaluate every time since input or xpath might have changed.
  76. string x = null;
  77. for (int i = 0; i < xpath.Count; i++) {
  78. switch (xpath [i].NodeType) {
  79. case XmlNodeType.Text:
  80. case XmlNodeType.CDATA:
  81. case XmlNodeType.Element:
  82. x += xpath [i].InnerText;
  83. break;
  84. }
  85. }
  86. ctx = new XmlDsigXPathContext (doc);
  87. foreach (XmlNode n in xpath) {
  88. XPathNavigator nav = n.CreateNavigator ();
  89. XPathNodeIterator iter = nav.Select ("namespace::*");
  90. while (iter.MoveNext ())
  91. if (iter.Current.LocalName != "xml")
  92. ctx.AddNamespace (iter.Current.LocalName, iter.Current.Value);
  93. }
  94. return EvaluateMatch (doc, x);
  95. }
  96. public override object GetOutput (Type type)
  97. {
  98. if (type != typeof (XmlNodeList))
  99. throw new ArgumentException ("type");
  100. return GetOutput ();
  101. }
  102. private XmlDsigNodeList EvaluateMatch (XmlNode n, string xpath)
  103. {
  104. ArrayList al = new ArrayList ();
  105. // Strictly to say, document node is explicitly
  106. // excluded by W3C spec (context node is initialized
  107. // to the document root and XPath expression is
  108. // "//. | //@* | //namespace::*)
  109. XPathNavigator nav = n.CreateNavigator ();
  110. XPathExpression exp = nav.Compile (xpath);
  111. exp.SetContext (ctx);
  112. EvaluateMatch (n, exp, al);
  113. return new XmlDsigNodeList (al);
  114. }
  115. private void EvaluateMatch (XmlNode n, XPathExpression exp, ArrayList al)
  116. {
  117. if (NodeMatches (n, exp))
  118. al.Add (n);
  119. if (n.Attributes != null)
  120. for (int i = 0; i < n.Attributes.Count; i++)
  121. if (NodeMatches (n.Attributes [i], exp))
  122. al.Add (n.Attributes [i]);
  123. for (int i = 0; i < n.ChildNodes.Count; i++)
  124. EvaluateMatch (n.ChildNodes [i], exp, al);
  125. }
  126. private bool NodeMatches (XmlNode n, XPathExpression exp)
  127. {
  128. // This looks waste of memory since it creates
  129. // XPathNavigator every time, but even if we use
  130. // XPathNodeIterator.Current, it also clones every time.
  131. object ret = n.CreateNavigator ().Evaluate (exp);
  132. if (ret is bool)
  133. return (bool) ret;
  134. if (ret is double) {
  135. double d = (double) ret;
  136. return !(d == 0.0 || d == double.NaN);
  137. }
  138. if (ret is string)
  139. return ((string) ret).Length > 0;
  140. if (ret is XPathNodeIterator) {
  141. XPathNodeIterator retiter = (XPathNodeIterator) ret;
  142. return retiter.Count > 0;
  143. }
  144. return false;
  145. }
  146. public override void LoadInnerXml (XmlNodeList nodeList)
  147. {
  148. if (nodeList == null)
  149. throw new CryptographicException ("nodeList");
  150. xpath = nodeList;
  151. }
  152. public override void LoadInput (object obj)
  153. {
  154. // possible input: Stream, XmlDocument, and XmlNodeList
  155. if (obj is Stream) {
  156. doc = new XmlDocument ();
  157. #if ! NET_1_0
  158. doc.XmlResolver = GetResolver ();
  159. #endif
  160. doc.Load (obj as Stream);
  161. }
  162. else if (obj is XmlDocument) {
  163. doc = (obj as XmlDocument);
  164. }
  165. else if (obj is XmlNodeList) {
  166. doc = new XmlDocument ();
  167. #if ! NET_1_0
  168. doc.XmlResolver = GetResolver ();
  169. #endif
  170. foreach (XmlNode xn in (obj as XmlNodeList)) {
  171. XmlNode importedNode = doc.ImportNode (xn, true);
  172. doc.AppendChild (importedNode);
  173. }
  174. }
  175. }
  176. // Internal classes to support XPath extension function here()
  177. internal class XmlDsigXPathContext : XsltContext
  178. {
  179. XmlDsigXPathFunctionHere here;
  180. public XmlDsigXPathContext (XmlNode node)
  181. {
  182. here = new XmlDsigXPathFunctionHere (node);
  183. }
  184. public override IXsltContextFunction ResolveFunction (
  185. string prefix, string name, XPathResultType [] argType)
  186. {
  187. // Here MS.NET incorrectly allows arbitrary
  188. // name e.g. "heretic()".
  189. if (name == "here" &&
  190. prefix == String.Empty &&
  191. argType.Length == 0)
  192. return here;
  193. else
  194. return null; // ????
  195. }
  196. public override bool Whitespace {
  197. get { return true; }
  198. }
  199. public override bool PreserveWhitespace (XPathNavigator node)
  200. {
  201. return true;
  202. }
  203. public override int CompareDocument (string s1, string s2)
  204. {
  205. return String.Compare (s1, s2);
  206. }
  207. public override IXsltContextVariable ResolveVariable (string prefix, string name)
  208. {
  209. throw new InvalidOperationException ();
  210. }
  211. }
  212. internal class XmlDsigXPathFunctionHere : IXsltContextFunction
  213. {
  214. // Static
  215. static XPathResultType [] types;
  216. static XmlDsigXPathFunctionHere ()
  217. {
  218. types = new XPathResultType [0];
  219. }
  220. // Instance
  221. XPathNodeIterator xpathNode;
  222. public XmlDsigXPathFunctionHere (XmlNode node)
  223. {
  224. xpathNode = node.CreateNavigator ().Select (".");
  225. }
  226. public XPathResultType [] ArgTypes {
  227. get { return types; }
  228. }
  229. public int Maxargs { get { return 0; } }
  230. public int Minargs { get { return 0; } }
  231. public XPathResultType ReturnType {
  232. get { return XPathResultType.NodeSet; }
  233. }
  234. public object Invoke (XsltContext ctx, object [] args, XPathNavigator docContext)
  235. {
  236. if (args.Length != 0)
  237. throw new ArgumentException ("Not allowed arguments for function here().", "args");
  238. return xpathNode.Clone ();
  239. }
  240. }
  241. }
  242. }