XmlDsigXPathTransform.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections;
  34. using System.IO;
  35. using System.Text;
  36. using System.Xml;
  37. using System.Xml.XPath;
  38. using System.Xml.Xsl;
  39. namespace System.Security.Cryptography.Xml
  40. {
  41. // www.w3.org/TR/xmldsig-core/
  42. // see Section 6.6.3 of the XMLDSIG specification
  43. public class XmlDsigXPathTransform : Transform
  44. {
  45. private Type [] input;
  46. private Type [] output;
  47. private XmlNodeList xpath;
  48. private XmlDocument doc;
  49. private XsltContext ctx;
  50. public XmlDsigXPathTransform ()
  51. {
  52. Algorithm = "http://www.w3.org/TR/1999/REC-xpath-19991116";
  53. }
  54. public override Type [] InputTypes {
  55. get {
  56. if (input == null) {
  57. lock (this) {
  58. // this way the result is cached if called multiple time
  59. input = new Type [3];
  60. input [0] = typeof (System.IO.Stream);
  61. input [1] = typeof (System.Xml.XmlDocument);
  62. input [2] = typeof (System.Xml.XmlNodeList);
  63. }
  64. }
  65. return input;
  66. }
  67. }
  68. public override Type[] OutputTypes {
  69. get {
  70. if (output == null) {
  71. lock (this) {
  72. // this way the result is cached if called multiple time
  73. output = new Type [1];
  74. output [0] = typeof (System.Xml.XmlNodeList);
  75. }
  76. }
  77. return output;
  78. }
  79. }
  80. protected override XmlNodeList GetInnerXml ()
  81. {
  82. if (xpath == null) {
  83. // default value
  84. XmlDocument xpdoc = new XmlDocument ();
  85. xpdoc.LoadXml ("<XPath xmlns=\"" + XmlSignature.NamespaceURI + "\"></XPath>");
  86. xpath = xpdoc.ChildNodes;
  87. }
  88. return xpath;
  89. }
  90. [MonoTODO ("Evaluation of extension function here() results in different from MS.NET (is MS.NET really correct??).")]
  91. public override object GetOutput ()
  92. {
  93. if (xpath == null)
  94. return new XmlDsigNodeList (new ArrayList ());
  95. // evaluate every time since input or xpath might have changed.
  96. string x = null;
  97. for (int i = 0; i < xpath.Count; i++) {
  98. switch (xpath [i].NodeType) {
  99. case XmlNodeType.Text:
  100. case XmlNodeType.CDATA:
  101. case XmlNodeType.Element:
  102. x += xpath [i].InnerText;
  103. break;
  104. }
  105. }
  106. ctx = new XmlDsigXPathContext (doc);
  107. foreach (XmlNode n in xpath) {
  108. XPathNavigator nav = n.CreateNavigator ();
  109. XPathNodeIterator iter = nav.Select ("namespace::*");
  110. while (iter.MoveNext ())
  111. if (iter.Current.LocalName != "xml")
  112. ctx.AddNamespace (iter.Current.LocalName, iter.Current.Value);
  113. }
  114. return EvaluateMatch (doc, x);
  115. }
  116. public override object GetOutput (Type type)
  117. {
  118. if (type != typeof (XmlNodeList))
  119. throw new ArgumentException ("type");
  120. return GetOutput ();
  121. }
  122. private XmlDsigNodeList EvaluateMatch (XmlNode n, string xpath)
  123. {
  124. ArrayList al = new ArrayList ();
  125. // Strictly to say, document node is explicitly
  126. // excluded by W3C spec (context node is initialized
  127. // to the document root and XPath expression is
  128. // "//. | //@* | //namespace::*)
  129. XPathNavigator nav = n.CreateNavigator ();
  130. XPathExpression exp = nav.Compile (xpath);
  131. exp.SetContext (ctx);
  132. EvaluateMatch (n, exp, al);
  133. return new XmlDsigNodeList (al);
  134. }
  135. private void EvaluateMatch (XmlNode n, XPathExpression exp, ArrayList al)
  136. {
  137. if (NodeMatches (n, exp))
  138. al.Add (n);
  139. if (n.Attributes != null)
  140. for (int i = 0; i < n.Attributes.Count; i++)
  141. if (NodeMatches (n.Attributes [i], exp))
  142. al.Add (n.Attributes [i]);
  143. for (int i = 0; i < n.ChildNodes.Count; i++)
  144. EvaluateMatch (n.ChildNodes [i], exp, al);
  145. }
  146. private bool NodeMatches (XmlNode n, XPathExpression exp)
  147. {
  148. // This looks waste of memory since it creates
  149. // XPathNavigator every time, but even if we use
  150. // XPathNodeIterator.Current, it also clones every time.
  151. object ret = n.CreateNavigator ().Evaluate (exp);
  152. if (ret is bool)
  153. return (bool) ret;
  154. if (ret is double) {
  155. double d = (double) ret;
  156. return !(d == 0.0 || d == double.NaN);
  157. }
  158. if (ret is string)
  159. return ((string) ret).Length > 0;
  160. if (ret is XPathNodeIterator) {
  161. XPathNodeIterator retiter = (XPathNodeIterator) ret;
  162. return retiter.Count > 0;
  163. }
  164. return false;
  165. }
  166. public override void LoadInnerXml (XmlNodeList nodeList)
  167. {
  168. if (nodeList == null)
  169. throw new CryptographicException ("nodeList");
  170. xpath = nodeList;
  171. }
  172. public override void LoadInput (object obj)
  173. {
  174. // possible input: Stream, XmlDocument, and XmlNodeList
  175. if (obj is Stream) {
  176. doc = new XmlDocument ();
  177. #if ! NET_1_0
  178. doc.XmlResolver = GetResolver ();
  179. #endif
  180. doc.Load (obj as Stream);
  181. }
  182. else if (obj is XmlDocument) {
  183. doc = (obj as XmlDocument);
  184. }
  185. else if (obj is XmlNodeList) {
  186. doc = new XmlDocument ();
  187. #if ! NET_1_0
  188. doc.XmlResolver = GetResolver ();
  189. #endif
  190. foreach (XmlNode xn in (obj as XmlNodeList)) {
  191. XmlNode importedNode = doc.ImportNode (xn, true);
  192. doc.AppendChild (importedNode);
  193. }
  194. }
  195. }
  196. // Internal classes to support XPath extension function here()
  197. internal class XmlDsigXPathContext : XsltContext
  198. {
  199. XmlDsigXPathFunctionHere here;
  200. public XmlDsigXPathContext (XmlNode node)
  201. {
  202. here = new XmlDsigXPathFunctionHere (node);
  203. }
  204. public override IXsltContextFunction ResolveFunction (
  205. string prefix, string name, XPathResultType [] argType)
  206. {
  207. // Here MS.NET incorrectly allows arbitrary
  208. // name e.g. "heretic()".
  209. if (name == "here" &&
  210. prefix == String.Empty &&
  211. argType.Length == 0)
  212. return here;
  213. else
  214. return null; // ????
  215. }
  216. public override bool Whitespace {
  217. get { return true; }
  218. }
  219. public override bool PreserveWhitespace (XPathNavigator node)
  220. {
  221. return true;
  222. }
  223. public override int CompareDocument (string s1, string s2)
  224. {
  225. return String.Compare (s1, s2);
  226. }
  227. public override IXsltContextVariable ResolveVariable (string prefix, string name)
  228. {
  229. throw new InvalidOperationException ();
  230. }
  231. }
  232. internal class XmlDsigXPathFunctionHere : IXsltContextFunction
  233. {
  234. // Static
  235. static XPathResultType [] types;
  236. static XmlDsigXPathFunctionHere ()
  237. {
  238. types = new XPathResultType [0];
  239. }
  240. // Instance
  241. XPathNodeIterator xpathNode;
  242. public XmlDsigXPathFunctionHere (XmlNode node)
  243. {
  244. xpathNode = node.CreateNavigator ().Select (".");
  245. }
  246. public XPathResultType [] ArgTypes {
  247. get { return types; }
  248. }
  249. public int Maxargs { get { return 0; } }
  250. public int Minargs { get { return 0; } }
  251. public XPathResultType ReturnType {
  252. get { return XPathResultType.NodeSet; }
  253. }
  254. public object Invoke (XsltContext ctx, object [] args, XPathNavigator docContext)
  255. {
  256. if (args.Length != 0)
  257. throw new ArgumentException ("Not allowed arguments for function here().", "args");
  258. return xpathNode.Clone ();
  259. }
  260. }
  261. }
  262. }