XmlDsigC14NTransform.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. //
  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.Collections;
  34. using System.IO;
  35. using System.Text;
  36. using System.Xml;
  37. using Mono.Xml;
  38. namespace System.Security.Cryptography.Xml {
  39. public class XmlDsigC14NTransform : Transform {
  40. private Type[] input;
  41. private Type[] output;
  42. private XmlCanonicalizer canonicalizer;
  43. private Stream s;
  44. public XmlDsigC14NTransform ()
  45. {
  46. Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  47. canonicalizer = new XmlCanonicalizer (false, false);
  48. }
  49. public XmlDsigC14NTransform (bool includeComments)
  50. {
  51. canonicalizer = new XmlCanonicalizer (includeComments, false);
  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 null; // THIS IS DOCUMENTED AS SUCH
  82. }
  83. public override object GetOutput ()
  84. {
  85. return (object) s;
  86. }
  87. public override object GetOutput (Type type)
  88. {
  89. if (type == Type.GetType ("Stream"))
  90. return GetOutput ();
  91. throw new ArgumentException ("type");
  92. }
  93. public override void LoadInnerXml (XmlNodeList nodeList)
  94. {
  95. // documented as not changing the state of the transform
  96. }
  97. public override void LoadInput (object obj)
  98. {
  99. if (obj is Stream) {
  100. s = (obj as Stream);
  101. XmlDocument doc = new XmlDocument ();
  102. doc.PreserveWhitespace = true; // REALLY IMPORTANT
  103. doc.Load (obj as Stream);
  104. s = canonicalizer.Canonicalize (doc);
  105. } else if (obj is XmlDocument)
  106. s = canonicalizer.Canonicalize ((obj as XmlDocument));
  107. else if (obj is XmlNodeList)
  108. s = canonicalizer.Canonicalize ((obj as XmlNodeList));
  109. // note: there is no default are other types won't throw an exception
  110. }
  111. }
  112. }