XmlDsigC14NTransform.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // XmlDsigC14NTransform.cs - C14N Transform implementation for XML Signature
  3. // http://www.w3.org/TR/xml-c14n
  4. //
  5. // Authors:
  6. // Sebastien Pouliot <[email protected]>
  7. // Aleksey Sanin ([email protected])
  8. //
  9. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  10. // (C) 2003 Aleksey Sanin ([email protected])
  11. // (C) 2004 Novell (http://www.novell.com)
  12. //
  13. using System.Collections;
  14. using System.IO;
  15. using System.Text;
  16. using System.Xml;
  17. using Mono.Xml;
  18. namespace System.Security.Cryptography.Xml {
  19. public class XmlDsigC14NTransform : Transform {
  20. private Type[] input;
  21. private Type[] output;
  22. private XmlCanonicalizer canonicalizer;
  23. private Stream s;
  24. public XmlDsigC14NTransform ()
  25. {
  26. Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  27. canonicalizer = new XmlCanonicalizer (false, false);
  28. }
  29. public XmlDsigC14NTransform (bool includeComments)
  30. {
  31. canonicalizer = new XmlCanonicalizer (includeComments, false);
  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 null; // THIS IS DOCUMENTED AS SUCH
  62. }
  63. public override object GetOutput ()
  64. {
  65. return (object) s;
  66. }
  67. public override object GetOutput (Type type)
  68. {
  69. if (type == Type.GetType ("Stream"))
  70. return GetOutput ();
  71. throw new ArgumentException ("type");
  72. }
  73. public override void LoadInnerXml (XmlNodeList nodeList)
  74. {
  75. // documented as not changing the state of the transform
  76. }
  77. public override void LoadInput (object obj)
  78. {
  79. if (obj is Stream) {
  80. s = (obj as Stream);
  81. XmlDocument doc = new XmlDocument ();
  82. doc.PreserveWhitespace = true; // REALLY IMPORTANT
  83. doc.Load (obj as Stream);
  84. s = canonicalizer.Canonicalize (doc);
  85. } else if (obj is XmlDocument)
  86. s = canonicalizer.Canonicalize ((obj as XmlDocument));
  87. else if (obj is XmlNodeList)
  88. s = canonicalizer.Canonicalize ((obj as XmlNodeList));
  89. // note: there is no default are other types won't throw an exception
  90. }
  91. }
  92. }