XmlDsigXPathTransform.cs 8.2 KB

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