XmlDsigBase64Transform.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // XmlDsigBase64Transform.cs - Base64 Transform implementation for XML Signature
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // (C) 2004 Novell (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.IO;
  31. using System.Security.Cryptography;
  32. using System.Text;
  33. using System.Xml;
  34. namespace System.Security.Cryptography.Xml {
  35. // http://www.w3.org/2000/09/xmldsig#base64
  36. public class XmlDsigBase64Transform : Transform {
  37. private CryptoStream cs;
  38. public XmlDsigBase64Transform ()
  39. {
  40. Algorithm = "http://www.w3.org/2000/09/xmldsig#base64";
  41. }
  42. public override Type[] InputTypes {
  43. get {
  44. Type[] input = new Type [3];
  45. input[0] = typeof (System.IO.Stream);
  46. input[1] = typeof (System.Xml.XmlDocument);
  47. input[2] = typeof (System.Xml.XmlNodeList);
  48. return input;
  49. }
  50. }
  51. public override Type[] OutputTypes {
  52. get {
  53. Type[] output = new Type [1];
  54. output[0] = typeof (System.IO.Stream);
  55. return output;
  56. }
  57. }
  58. protected override XmlNodeList GetInnerXml ()
  59. {
  60. return null; // THIS IS DOCUMENTED AS SUCH
  61. }
  62. public override object GetOutput ()
  63. {
  64. return (object) cs;
  65. }
  66. public override object GetOutput (Type type)
  67. {
  68. if (type != typeof (System.IO.Stream))
  69. throw new ArgumentException ("type");
  70. return GetOutput ();
  71. }
  72. public override void LoadInnerXml (XmlNodeList nodeList)
  73. {
  74. // documented as not changing the state of the transform
  75. }
  76. public override void LoadInput (object obj)
  77. {
  78. XmlNodeList xnl = null;
  79. Stream stream = null;
  80. if (obj is Stream)
  81. stream = (obj as Stream);
  82. else if (obj is XmlDocument)
  83. xnl = (obj as XmlDocument).SelectNodes ("//.");
  84. else if (obj is XmlNodeList)
  85. xnl = (XmlNodeList) obj;
  86. if (xnl != null) {
  87. stream = new MemoryStream ();
  88. StreamWriter sw = new StreamWriter (stream);
  89. foreach (XmlNode xn in xnl) {
  90. switch (xn.NodeType) {
  91. case XmlNodeType.Attribute:
  92. case XmlNodeType.Text:
  93. case XmlNodeType.CDATA:
  94. case XmlNodeType.SignificantWhitespace:
  95. case XmlNodeType.Whitespace:
  96. sw.Write (xn.Value);
  97. break;
  98. }
  99. }
  100. sw.Flush ();
  101. // ready to be re-used
  102. stream.Position = 0;
  103. }
  104. if (stream != null)
  105. cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read);
  106. // note: there is no default are other types won't throw an exception
  107. }
  108. }
  109. }