2
0

XmlDsigXPathTransform.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. return EvaluateMatch (doc, x);
  88. }
  89. public override object GetOutput (Type type)
  90. {
  91. if (type != typeof (XmlNodeList))
  92. throw new ArgumentException ("type");
  93. return GetOutput ();
  94. }
  95. private XmlDsigNodeList EvaluateMatch (XmlNode n, string xpath)
  96. {
  97. ArrayList al = new ArrayList ();
  98. // Strictly to say, document node is explicitly
  99. // excluded by W3C spec (context node is initialized
  100. // to the document root and XPath expression is
  101. // "//. | //@* | //namespace::*)
  102. XPathNavigator nav = n.CreateNavigator ();
  103. XPathExpression exp = nav.Compile (xpath);
  104. exp.SetContext (ctx);
  105. EvaluateMatch (n, exp, al);
  106. return new XmlDsigNodeList (al);
  107. }
  108. private void EvaluateMatch (XmlNode n, XPathExpression exp, ArrayList al)
  109. {
  110. if (NodeMatches (n, exp))
  111. al.Add (n);
  112. if (n.Attributes != null)
  113. for (int i = 0; i < n.Attributes.Count; i++)
  114. EvaluateMatch (n.Attributes [i], exp, al);
  115. for (int i = 0; i < n.ChildNodes.Count; i++)
  116. EvaluateMatch (n.ChildNodes [i], exp, al);
  117. }
  118. private bool NodeMatches (XmlNode n, XPathExpression exp)
  119. {
  120. // This looks waste of memory since it creates
  121. // XPathNavigator every time, but even if we use
  122. // XPathNodeIterator.Current, it also clones every time.
  123. object ret = n.CreateNavigator ().Evaluate (exp);
  124. if (ret is bool)
  125. return (bool) ret;
  126. if (ret is double) {
  127. double d = (double) ret;
  128. return !(d == 0.0 || d == double.NaN);
  129. }
  130. if (ret is string)
  131. return ((string) ret).Length > 0;
  132. if (ret is XPathNodeIterator) {
  133. XPathNodeIterator retiter = (XPathNodeIterator) ret;
  134. return retiter.Count > 0;
  135. }
  136. return false;
  137. }
  138. public override void LoadInnerXml (XmlNodeList nodeList)
  139. {
  140. if (nodeList == null)
  141. throw new CryptographicException ("nodeList");
  142. xpath = nodeList;
  143. }
  144. public override void LoadInput (object obj)
  145. {
  146. // possible input: Stream, XmlDocument, and XmlNodeList
  147. if (obj is Stream) {
  148. doc = new XmlDocument ();
  149. doc.XmlResolver = GetResolver ();
  150. doc.Load (obj as Stream);
  151. }
  152. else if (obj is XmlDocument) {
  153. doc = (obj as XmlDocument);
  154. }
  155. else if (obj is XmlNodeList) {
  156. doc = new XmlDocument ();
  157. doc.XmlResolver = GetResolver ();
  158. foreach (XmlNode xn in (obj as XmlNodeList)) {
  159. XmlNode importedNode = doc.ImportNode (xn, true);
  160. doc.AppendChild (importedNode);
  161. }
  162. }
  163. }
  164. // Internal classes to support XPath extension function here()
  165. internal class XmlDsigXPathContext : XsltContext
  166. {
  167. XmlDsigXPathFunctionHere here;
  168. public XmlDsigXPathContext (XmlNode node)
  169. {
  170. here = new XmlDsigXPathFunctionHere (node);
  171. }
  172. public override IXsltContextFunction ResolveFunction (
  173. string prefix, string name, XPathResultType [] argType)
  174. {
  175. // Here MS.NET incorrectly allows arbitrary
  176. // name e.g. "heretic()".
  177. if (name == "here" &&
  178. prefix == String.Empty &&
  179. argType.Length == 0)
  180. return here;
  181. else
  182. return null; // ????
  183. }
  184. public override bool Whitespace {
  185. get { return true; }
  186. }
  187. public override bool PreserveWhitespace (XPathNavigator node)
  188. {
  189. return true;
  190. }
  191. public override int CompareDocument (string s1, string s2)
  192. {
  193. return String.Compare (s1, s2);
  194. }
  195. public override IXsltContextVariable ResolveVariable (string prefix, string name)
  196. {
  197. throw new InvalidOperationException ();
  198. }
  199. }
  200. internal class XmlDsigXPathFunctionHere : IXsltContextFunction
  201. {
  202. // Static
  203. static XPathResultType [] types;
  204. static XmlDsigXPathFunctionHere ()
  205. {
  206. types = new XPathResultType [0];
  207. }
  208. // Instance
  209. XPathNodeIterator xpathNode;
  210. public XmlDsigXPathFunctionHere (XmlNode node)
  211. {
  212. xpathNode = node.CreateNavigator ().Select (".");
  213. }
  214. public XPathResultType [] ArgTypes {
  215. get { return types; }
  216. }
  217. public int Maxargs { get { return 0; } }
  218. public int Minargs { get { return 0; } }
  219. public XPathResultType ReturnType {
  220. get { return XPathResultType.NodeSet; }
  221. }
  222. public object Invoke (XsltContext ctx, object [] args, XPathNavigator docContext)
  223. {
  224. if (args.Length != 0)
  225. throw new ArgumentException ("Not allowed arguments for function here().", "args");
  226. return xpathNode.Clone ();
  227. }
  228. }
  229. }
  230. }