XmlDsigEnvelopedSignatureTransform.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // XmlDsigEnvelopedSignatureTransform.cs -
  3. // Enveloped Signature Transform implementation for XML Signature
  4. //
  5. // Author:
  6. // Sebastien Pouliot ([email protected])
  7. // Atsushi Enomoto ([email protected])
  8. //
  9. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  10. // (C) 2004 Novell Inc.
  11. //
  12. using System.Collections;
  13. using System.IO;
  14. using System.Xml;
  15. namespace System.Security.Cryptography.Xml {
  16. [MonoTODO]
  17. public class XmlDsigEnvelopedSignatureTransform : Transform {
  18. private Type[] input;
  19. private Type[] output;
  20. private bool comments;
  21. private object inputObj;
  22. public XmlDsigEnvelopedSignatureTransform ()
  23. {
  24. Algorithm = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
  25. comments = false;
  26. }
  27. public XmlDsigEnvelopedSignatureTransform (bool includeComments)
  28. {
  29. comments = includeComments;
  30. }
  31. public override Type[] InputTypes {
  32. get {
  33. if (input == null) {
  34. lock (this) {
  35. // this way the result is cached if called multiple time
  36. input = new Type [3];
  37. input[0] = typeof (System.IO.Stream);
  38. input[1] = typeof (System.Xml.XmlDocument);
  39. input[2] = typeof (System.Xml.XmlNodeList);
  40. }
  41. }
  42. return input;
  43. }
  44. }
  45. public override Type[] OutputTypes {
  46. get {
  47. if (output == null) {
  48. lock (this) {
  49. // this way the result is cached if called multiple time
  50. output = new Type [2];
  51. input[0] = typeof (System.Xml.XmlDocument);
  52. input[1] = typeof (System.Xml.XmlNodeList);
  53. }
  54. }
  55. return output;
  56. }
  57. }
  58. protected override XmlNodeList GetInnerXml ()
  59. {
  60. return null; // THIS IS DOCUMENTED AS SUCH
  61. }
  62. [MonoTODO ("Is it really spec-compliant??")]
  63. public override object GetOutput ()
  64. {
  65. XmlDocument doc = null;
  66. // possible input: Stream, XmlDocument, and XmlNodeList
  67. if (inputObj is Stream) {
  68. doc = new XmlDocument ();
  69. doc.PreserveWhitespace = true;
  70. doc.XmlResolver = GetResolver ();
  71. doc.Load (inputObj as Stream);
  72. return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
  73. }
  74. else if (inputObj is XmlDocument) {
  75. doc = inputObj as XmlDocument;
  76. return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
  77. }
  78. else if (inputObj is XmlNodeList) {
  79. ArrayList al = new ArrayList ();
  80. XmlNodeList nl = (XmlNodeList) inputObj;
  81. if (nl.Count > 0) {
  82. XmlNamespaceManager m = GetNamespaceManager (nl.Item (0));
  83. ArrayList tmp = new ArrayList ();
  84. foreach (XmlNode n in nl)
  85. tmp.Add (n);
  86. foreach (XmlNode n in tmp)
  87. if (n.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
  88. al.Add (GetOutputFromNode (n, m, false));
  89. }
  90. return new XmlDsigNodeList (al);
  91. }
  92. // Note that it is unexpected behavior with related to InputTypes (MS.NET accepts XmlElement)
  93. else if (inputObj is XmlElement) {
  94. XmlElement el = inputObj as XmlElement;
  95. XmlNamespaceManager m = GetNamespaceManager (el);
  96. if (el.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
  97. return GetOutputFromNode (el, m, true);
  98. }
  99. throw new NullReferenceException ();
  100. }
  101. private XmlNamespaceManager GetNamespaceManager (XmlNode n)
  102. {
  103. XmlDocument doc = n is XmlDocument ? n as XmlDocument : n.OwnerDocument;
  104. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  105. nsmgr.AddNamespace ("dsig", XmlSignature.NamespaceURI);
  106. return nsmgr;
  107. }
  108. private XmlNode GetOutputFromNode (XmlNode input, XmlNamespaceManager nsmgr, bool remove)
  109. {
  110. XmlDocument doc = input is XmlDocument ? input as XmlDocument : input.OwnerDocument;
  111. if (remove) {
  112. XmlNodeList nl = input.SelectNodes ("descendant-or-self::dsig:Signature", nsmgr);
  113. foreach (XmlNode n in nl)
  114. n.ParentNode.RemoveChild (n);
  115. }
  116. return input;
  117. }
  118. public override object GetOutput (Type type)
  119. {
  120. if (type == Type.GetType ("Stream"))
  121. return GetOutput ();
  122. throw new ArgumentException ("type");
  123. }
  124. public override void LoadInnerXml (XmlNodeList nodeList)
  125. {
  126. // NO CHANGE
  127. }
  128. [MonoTODO ("test")]
  129. public override void LoadInput (object obj)
  130. {
  131. inputObj = obj;
  132. }
  133. }
  134. }