XmlDsigXPathTransform.cs 6.6 KB

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