XmlDsigXsltTransform.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. using System.IO;
  14. using System.Xml;
  15. using System.Xml.Xsl;
  16. namespace System.Security.Cryptography.Xml
  17. {
  18. public class XmlDsigXsltTransform : Transform
  19. {
  20. private Type [] input;
  21. private Type [] output;
  22. private bool comments;
  23. private XmlNodeList xnl;
  24. private XmlDocument inputDoc;
  25. public XmlDsigXsltTransform () : this (false)
  26. {
  27. }
  28. public XmlDsigXsltTransform (bool includeComments)
  29. {
  30. comments = includeComments;
  31. Algorithm = "http://www.w3.org/TR/1999/REC-xslt-19991116";
  32. }
  33. public override Type [] InputTypes {
  34. get {
  35. if (input == null) {
  36. lock (this) {
  37. // this way the result is cached if called multiple time
  38. input = new Type [3];
  39. input [0] = typeof (System.IO.Stream);
  40. input [1] = typeof (System.Xml.XmlDocument);
  41. input [2] = typeof (System.Xml.XmlNodeList);
  42. }
  43. }
  44. return input;
  45. }
  46. }
  47. public override Type [] OutputTypes {
  48. get {
  49. if (output == null) {
  50. lock (this) {
  51. // this way the result is cached if called multiple time
  52. output = new Type [1];
  53. output [0] = typeof (System.IO.Stream);
  54. }
  55. }
  56. return output;
  57. }
  58. }
  59. protected override XmlNodeList GetInnerXml ()
  60. {
  61. return xnl;
  62. }
  63. public override object GetOutput ()
  64. {
  65. XslTransform xsl = new XslTransform ();
  66. XmlDocument doc = new XmlDocument ();
  67. doc.XmlResolver = GetResolver ();
  68. foreach (XmlNode n in xnl)
  69. doc.AppendChild (doc.ImportNode (n, true));
  70. xsl.Load (doc);
  71. Stream stream = null;
  72. stream = new MemoryStream ();
  73. // only possible output: Stream
  74. xsl.Transform (inputDoc, null, stream);
  75. CryptoStream cs = null;
  76. if (stream != null)
  77. cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read);
  78. // note: there is no default are other types won't throw an exception
  79. return cs;
  80. }
  81. public override object GetOutput (Type type)
  82. {
  83. if (type != Type.GetType ("System.IO.Stream"))
  84. throw new ArgumentException ("type");
  85. return GetOutput ();
  86. }
  87. public override void LoadInnerXml (XmlNodeList nodeList)
  88. {
  89. if (nodeList == null)
  90. throw new CryptographicException ("nodeList");
  91. xnl = nodeList;
  92. }
  93. public override void LoadInput (object obj)
  94. {
  95. // possible input: Stream, XmlDocument, and XmlNodeList
  96. if (obj is Stream) {
  97. inputDoc = new XmlDocument ();
  98. inputDoc.Load (obj as Stream);
  99. }
  100. else if (obj is XmlDocument) {
  101. inputDoc= obj as XmlDocument;
  102. }
  103. else if (obj is XmlNodeList) {
  104. inputDoc = new XmlDocument ();
  105. XmlNodeList nl = (XmlNodeList) obj;
  106. for (int i = 0; i < nl.Count; i++)
  107. inputDoc.AppendChild (inputDoc.ImportNode (nl [i], true));
  108. }
  109. }
  110. }
  111. }