XmlDsigXsltTransform.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. XmlResolver resolver = GetResolver ();
  86. XslTransform xsl = new XslTransform ();
  87. XmlDocument doc = new XmlDocument ();
  88. #if NET_1_1
  89. doc.XmlResolver = resolver;
  90. #endif
  91. foreach (XmlNode n in xnl)
  92. doc.AppendChild (doc.ImportNode (n, true));
  93. #if NET_1_1
  94. xsl.Load (doc, resolver);
  95. #else
  96. xsl.Load (doc);
  97. #endif
  98. if (inputDoc == null)
  99. throw new NullReferenceException ("Load input document before transformation.");
  100. MemoryStream stream = new MemoryStream ();
  101. // only possible output: Stream
  102. #if NET_1_1
  103. xsl.XmlResolver = resolver;
  104. #endif
  105. xsl.Transform (inputDoc, null, stream);
  106. stream.Seek (0, SeekOrigin.Begin);
  107. return stream;
  108. }
  109. public override object GetOutput (Type type)
  110. {
  111. if (type != Type.GetType ("System.IO.Stream"))
  112. throw new ArgumentException ("type");
  113. return GetOutput ();
  114. }
  115. public override void LoadInnerXml (XmlNodeList nodeList)
  116. {
  117. if (nodeList == null)
  118. throw new CryptographicException ("nodeList");
  119. xnl = nodeList;
  120. }
  121. public override void LoadInput (object obj)
  122. {
  123. // possible input: Stream, XmlDocument, and XmlNodeList
  124. if (obj is Stream) {
  125. inputDoc = new XmlDocument ();
  126. #if NET_1_1
  127. inputDoc.XmlResolver = GetResolver ();
  128. #endif
  129. inputDoc.Load (obj as Stream);
  130. }
  131. else if (obj is XmlDocument) {
  132. inputDoc= obj as XmlDocument;
  133. }
  134. else if (obj is XmlNodeList) {
  135. inputDoc = new XmlDocument ();
  136. #if NET_1_1
  137. inputDoc.XmlResolver = GetResolver ();
  138. #endif
  139. XmlNodeList nl = (XmlNodeList) obj;
  140. for (int i = 0; i < nl.Count; i++)
  141. inputDoc.AppendChild (inputDoc.ImportNode (nl [i], true));
  142. }
  143. }
  144. }
  145. }