XmlDsigXsltTransform.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // XmlDsigEnvelopedSignatureTransform.cs -
  3. // Enveloped Signature Transform implementation for XML Signature
  4. // http://www.w3.org/TR/1999/REC-xslt-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 Inc.
  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.IO;
  34. using System.Xml;
  35. using System.Xml.Xsl;
  36. namespace System.Security.Cryptography.Xml
  37. {
  38. public class XmlDsigXsltTransform : Transform
  39. {
  40. private Type [] input;
  41. private Type [] output;
  42. private bool comments;
  43. private XmlNodeList xnl;
  44. private XmlDocument inputDoc;
  45. public XmlDsigXsltTransform () : this (false)
  46. {
  47. }
  48. public XmlDsigXsltTransform (bool includeComments)
  49. {
  50. comments = includeComments;
  51. Algorithm = "http://www.w3.org/TR/1999/REC-xslt-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.IO.Stream);
  74. }
  75. }
  76. return output;
  77. }
  78. }
  79. protected override XmlNodeList GetInnerXml ()
  80. {
  81. return xnl;
  82. }
  83. public override object GetOutput ()
  84. {
  85. #if NET_2_0
  86. if (xnl == null)
  87. throw new ArgumentNullException ("LoadInnerXml before transformation.");
  88. #endif
  89. XmlResolver resolver = GetResolver ();
  90. XslTransform xsl = new XslTransform ();
  91. XmlDocument doc = new XmlDocument ();
  92. #if NET_1_1
  93. doc.XmlResolver = resolver;
  94. #endif
  95. foreach (XmlNode n in xnl)
  96. doc.AppendChild (doc.ImportNode (n, true));
  97. #if NET_1_1
  98. xsl.Load (doc, resolver);
  99. #else
  100. xsl.Load (doc);
  101. #endif
  102. if (inputDoc == null)
  103. #if NET_2_0
  104. throw new ArgumentNullException ("LoadInput before transformation.");
  105. #else
  106. throw new NullReferenceException ("LoadInput before transformation.");
  107. #endif
  108. MemoryStream stream = new MemoryStream ();
  109. // only possible output: Stream
  110. #if NET_1_1
  111. xsl.XmlResolver = resolver;
  112. #endif
  113. xsl.Transform (inputDoc, null, stream);
  114. stream.Seek (0, SeekOrigin.Begin);
  115. return stream;
  116. }
  117. public override object GetOutput (Type type)
  118. {
  119. if (type != Type.GetType ("System.IO.Stream"))
  120. throw new ArgumentException ("type");
  121. return GetOutput ();
  122. }
  123. public override void LoadInnerXml (XmlNodeList nodeList)
  124. {
  125. if (nodeList == null)
  126. throw new CryptographicException ("nodeList");
  127. xnl = nodeList;
  128. }
  129. public override void LoadInput (object obj)
  130. {
  131. // possible input: Stream, XmlDocument, and XmlNodeList
  132. if (obj is Stream) {
  133. inputDoc = new XmlDocument ();
  134. #if NET_1_1
  135. inputDoc.XmlResolver = GetResolver ();
  136. #endif
  137. // inputDoc.Load (obj as Stream);
  138. inputDoc.Load (new XmlSignatureStreamReader (
  139. new StreamReader (obj as Stream)));
  140. }
  141. else if (obj is XmlDocument) {
  142. inputDoc= obj as XmlDocument;
  143. }
  144. else if (obj is XmlNodeList) {
  145. inputDoc = new XmlDocument ();
  146. #if NET_1_1
  147. inputDoc.XmlResolver = GetResolver ();
  148. #endif
  149. XmlNodeList nl = (XmlNodeList) obj;
  150. for (int i = 0; i < nl.Count; i++)
  151. inputDoc.AppendChild (inputDoc.ImportNode (nl [i], true));
  152. }
  153. }
  154. }
  155. }