XmlDsigEnvelopedSignatureTransform.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. output [0] = typeof (System.Xml.XmlDocument);
  52. output [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. // NOTE: This method never supports the requirements written
  63. // in xmldsig spec that says its input is canonicalized before
  64. // transforming. This method just removes Signature element.
  65. // Canonicalization is done in SignedXml.
  66. public override object GetOutput ()
  67. {
  68. XmlDocument doc = null;
  69. // possible input: Stream, XmlDocument, and XmlNodeList
  70. if (inputObj is Stream) {
  71. doc = new XmlDocument ();
  72. doc.PreserveWhitespace = true;
  73. doc.XmlResolver = GetResolver ();
  74. doc.Load (inputObj as Stream);
  75. return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
  76. }
  77. else if (inputObj is XmlDocument) {
  78. doc = inputObj as XmlDocument;
  79. return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
  80. }
  81. else if (inputObj is XmlNodeList) {
  82. ArrayList al = new ArrayList ();
  83. XmlNodeList nl = (XmlNodeList) inputObj;
  84. if (nl.Count > 0) {
  85. XmlNamespaceManager m = GetNamespaceManager (nl.Item (0));
  86. ArrayList tmp = new ArrayList ();
  87. foreach (XmlNode n in nl)
  88. tmp.Add (n);
  89. foreach (XmlNode n in tmp)
  90. if (n.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
  91. al.Add (GetOutputFromNode (n, m, false));
  92. }
  93. return new XmlDsigNodeList (al);
  94. }
  95. // Note that it is unexpected behavior with related to InputTypes (MS.NET accepts XmlElement)
  96. else if (inputObj is XmlElement) {
  97. XmlElement el = inputObj as XmlElement;
  98. XmlNamespaceManager m = GetNamespaceManager (el);
  99. if (el.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
  100. return GetOutputFromNode (el, m, true);
  101. }
  102. throw new NullReferenceException ();
  103. }
  104. private XmlNamespaceManager GetNamespaceManager (XmlNode n)
  105. {
  106. XmlDocument doc = n is XmlDocument ? n as XmlDocument : n.OwnerDocument;
  107. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  108. nsmgr.AddNamespace ("dsig", XmlSignature.NamespaceURI);
  109. return nsmgr;
  110. }
  111. private XmlNode GetOutputFromNode (XmlNode input, XmlNamespaceManager nsmgr, bool remove)
  112. {
  113. XmlDocument doc = input is XmlDocument ? input as XmlDocument : input.OwnerDocument;
  114. if (remove) {
  115. XmlNodeList nl = input.SelectNodes ("descendant-or-self::dsig:Signature", nsmgr);
  116. foreach (XmlNode n in nl)
  117. n.ParentNode.RemoveChild (n);
  118. }
  119. return input;
  120. }
  121. public override object GetOutput (Type type)
  122. {
  123. if (type == Type.GetType ("Stream"))
  124. return GetOutput ();
  125. throw new ArgumentException ("type");
  126. }
  127. public override void LoadInnerXml (XmlNodeList nodeList)
  128. {
  129. // NO CHANGE
  130. }
  131. [MonoTODO ("test")]
  132. public override void LoadInput (object obj)
  133. {
  134. inputObj = obj;
  135. }
  136. }
  137. }