XmlDsigEnvelopedSignatureTransform.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. //
  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.Xml;
  35. namespace System.Security.Cryptography.Xml {
  36. public class XmlDsigEnvelopedSignatureTransform : Transform {
  37. private Type[] input;
  38. private Type[] output;
  39. private bool comments;
  40. private object inputObj;
  41. public XmlDsigEnvelopedSignatureTransform ()
  42. {
  43. Algorithm = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
  44. comments = false;
  45. }
  46. public XmlDsigEnvelopedSignatureTransform (bool includeComments)
  47. {
  48. comments = includeComments;
  49. }
  50. public override Type[] InputTypes {
  51. get {
  52. if (input == null) {
  53. lock (this) {
  54. // this way the result is cached if called multiple time
  55. input = new Type [3];
  56. input[0] = typeof (System.IO.Stream);
  57. input[1] = typeof (System.Xml.XmlDocument);
  58. input[2] = typeof (System.Xml.XmlNodeList);
  59. }
  60. }
  61. return input;
  62. }
  63. }
  64. public override Type[] OutputTypes {
  65. get {
  66. if (output == null) {
  67. lock (this) {
  68. // this way the result is cached if called multiple time
  69. output = new Type [2];
  70. output [0] = typeof (System.Xml.XmlDocument);
  71. output [1] = typeof (System.Xml.XmlNodeList);
  72. }
  73. }
  74. return output;
  75. }
  76. }
  77. protected override XmlNodeList GetInnerXml ()
  78. {
  79. return null; // THIS IS DOCUMENTED AS SUCH
  80. }
  81. // NOTE: This method never supports the requirements written
  82. // in xmldsig spec that says its input is canonicalized before
  83. // transforming. This method just removes Signature element.
  84. // Canonicalization is done in SignedXml.
  85. public override object GetOutput ()
  86. {
  87. XmlDocument doc = null;
  88. // possible input: Stream, XmlDocument, and XmlNodeList
  89. if (inputObj is Stream) {
  90. doc = new XmlDocument ();
  91. doc.PreserveWhitespace = true;
  92. #if NET_1_1
  93. doc.XmlResolver = GetResolver ();
  94. #endif
  95. doc.Load (inputObj as Stream);
  96. return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
  97. }
  98. else if (inputObj is XmlDocument) {
  99. doc = inputObj as XmlDocument;
  100. return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
  101. }
  102. else if (inputObj is XmlNodeList) {
  103. ArrayList al = new ArrayList ();
  104. XmlNodeList nl = (XmlNodeList) inputObj;
  105. if (nl.Count > 0) {
  106. XmlNamespaceManager m = GetNamespaceManager (nl.Item (0));
  107. ArrayList tmp = new ArrayList ();
  108. foreach (XmlNode n in nl)
  109. tmp.Add (n);
  110. foreach (XmlNode n in tmp)
  111. if (n.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
  112. al.Add (GetOutputFromNode (n, m, false));
  113. }
  114. return new XmlDsigNodeList (al);
  115. }
  116. // Note that it is unexpected behavior with related to InputTypes (MS.NET accepts XmlElement)
  117. else if (inputObj is XmlElement) {
  118. XmlElement el = inputObj as XmlElement;
  119. XmlNamespaceManager m = GetNamespaceManager (el);
  120. if (el.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
  121. return GetOutputFromNode (el, m, true);
  122. }
  123. throw new NullReferenceException ();
  124. }
  125. private XmlNamespaceManager GetNamespaceManager (XmlNode n)
  126. {
  127. XmlDocument doc = n is XmlDocument ? n as XmlDocument : n.OwnerDocument;
  128. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  129. nsmgr.AddNamespace ("dsig", XmlSignature.NamespaceURI);
  130. return nsmgr;
  131. }
  132. private XmlNode GetOutputFromNode (XmlNode input, XmlNamespaceManager nsmgr, bool remove)
  133. {
  134. XmlDocument doc = input is XmlDocument ? input as XmlDocument : input.OwnerDocument;
  135. if (remove) {
  136. XmlNodeList nl = input.SelectNodes ("descendant-or-self::dsig:Signature", nsmgr);
  137. foreach (XmlNode n in nl)
  138. n.ParentNode.RemoveChild (n);
  139. }
  140. return input;
  141. }
  142. public override object GetOutput (Type type)
  143. {
  144. if (type == Type.GetType ("Stream"))
  145. return GetOutput ();
  146. throw new ArgumentException ("type");
  147. }
  148. public override void LoadInnerXml (XmlNodeList nodeList)
  149. {
  150. // NO CHANGE
  151. }
  152. public override void LoadInput (object obj)
  153. {
  154. inputObj = obj;
  155. }
  156. }
  157. }