XmlDsigC14NTransform.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // Tim Coleman ([email protected])
  9. //
  10. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  11. // (C) 2003 Aleksey Sanin ([email protected])
  12. // Copyright (C) Tim Coleman, 2004
  13. // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System.Collections;
  35. using System.IO;
  36. using System.Runtime.InteropServices;
  37. using System.Text;
  38. using System.Xml;
  39. using Mono.Xml;
  40. namespace System.Security.Cryptography.Xml {
  41. public class XmlDsigC14NTransform : Transform {
  42. private Type[] input;
  43. private Type[] output;
  44. private XmlCanonicalizer canonicalizer;
  45. private Stream s;
  46. public XmlDsigC14NTransform ()
  47. {
  48. Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  49. canonicalizer = new XmlCanonicalizer (false, false);
  50. }
  51. public XmlDsigC14NTransform (bool includeComments)
  52. {
  53. canonicalizer = new XmlCanonicalizer (includeComments, false);
  54. }
  55. public override Type[] InputTypes {
  56. get {
  57. if (input == null) {
  58. lock (this) {
  59. // this way the result is cached if called multiple time
  60. input = new Type [3];
  61. input[0] = typeof (System.IO.Stream);
  62. input[1] = typeof (System.Xml.XmlDocument);
  63. input[2] = typeof (System.Xml.XmlNodeList);
  64. }
  65. }
  66. return input;
  67. }
  68. }
  69. public override Type[] OutputTypes {
  70. get {
  71. if (output == null) {
  72. lock (this) {
  73. // this way the result is cached if called multiple time
  74. output = new Type [1];
  75. output[0] = typeof (System.IO.Stream);
  76. }
  77. }
  78. return output;
  79. }
  80. }
  81. protected override XmlNodeList GetInnerXml ()
  82. {
  83. return null; // THIS IS DOCUMENTED AS SUCH
  84. }
  85. #if NET_2_0
  86. [ComVisible (false)]
  87. public override byte[] GetDigestedOutput (HashAlgorithm hash)
  88. {
  89. return hash.ComputeHash ((Stream) GetOutput ());
  90. }
  91. #endif
  92. public override object GetOutput ()
  93. {
  94. return (object) s;
  95. }
  96. public override object GetOutput (Type type)
  97. {
  98. if (type == Type.GetType ("Stream"))
  99. return GetOutput ();
  100. throw new ArgumentException ("type");
  101. }
  102. public override void LoadInnerXml (XmlNodeList nodeList)
  103. {
  104. // documented as not changing the state of the transform
  105. }
  106. public override void LoadInput (object obj)
  107. {
  108. if (obj is Stream) {
  109. s = (obj as Stream);
  110. XmlDocument doc = new XmlDocument ();
  111. doc.PreserveWhitespace = true; // REALLY IMPORTANT
  112. #if NET_1_1
  113. doc.XmlResolver = GetResolver ();
  114. #endif
  115. doc.Load (new XmlSignatureStreamReader (
  116. new StreamReader ((Stream) obj)));
  117. // doc.Load ((Stream) obj);
  118. s = canonicalizer.Canonicalize (doc);
  119. } else if (obj is XmlDocument)
  120. s = canonicalizer.Canonicalize ((obj as XmlDocument));
  121. else if (obj is XmlNodeList)
  122. s = canonicalizer.Canonicalize ((obj as XmlNodeList));
  123. // note: there is no default are other types won't throw an exception
  124. }
  125. }
  126. }