Transform.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Transform.cs - Transform implementation for XML Signature
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. // Tim Coleman <[email protected]>
  8. //
  9. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  10. // Copyright (C) Tim Coleman, 2004
  11. // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Collections;
  33. using System.IO;
  34. using System.Runtime.InteropServices;
  35. using System.Security.Policy;
  36. using System.Xml;
  37. namespace System.Security.Cryptography.Xml {
  38. public abstract class Transform {
  39. private string algo;
  40. private XmlResolver xmlResolver;
  41. public Transform ()
  42. {
  43. // FIXME: enable it after CAS implementation
  44. #if false // NET_1_1
  45. xmlResolver = new XmlSecureResolver (new XmlUrlResolver (), (Evidence) new Evidence ());
  46. #else
  47. xmlResolver = new XmlUrlResolver ();
  48. #endif
  49. }
  50. #region Properties
  51. public string Algorithm {
  52. get { return algo; }
  53. set { algo = value; }
  54. }
  55. public abstract Type[] InputTypes {
  56. get;
  57. }
  58. public abstract Type[] OutputTypes {
  59. get;
  60. }
  61. #if NET_1_1
  62. [ComVisible(false)]
  63. public XmlResolver Resolver {
  64. set { xmlResolver = value; }
  65. }
  66. #endif
  67. #if NET_2_0
  68. [MonoTODO]
  69. [ComVisible (false)]
  70. public XmlElement Context {
  71. get { throw new NotImplementedException (); }
  72. set { throw new NotImplementedException (); }
  73. }
  74. [MonoTODO]
  75. [ComVisible (false)]
  76. public Hashtable PropagatedNamespaces {
  77. get { throw new NotImplementedException (); }
  78. }
  79. #endif
  80. #endregion // Properties
  81. #region Methods
  82. #if NET_2_0
  83. [ComVisible (false)]
  84. public virtual byte[] GetDigestedOutput (HashAlgorithm hash)
  85. {
  86. return hash.ComputeHash ((Stream) GetOutput (typeof (Stream)));
  87. }
  88. #endif
  89. protected abstract XmlNodeList GetInnerXml ();
  90. public abstract object GetOutput ();
  91. public abstract object GetOutput (Type type);
  92. public XmlElement GetXml ()
  93. {
  94. XmlDocument document = new XmlDocument ();
  95. document.XmlResolver = GetResolver ();
  96. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI);
  97. xel.SetAttribute (XmlSignature.AttributeNames.Algorithm, algo);
  98. XmlNodeList xnl = this.GetInnerXml ();
  99. if (xnl != null) {
  100. foreach (XmlNode xn in xnl) {
  101. XmlNode importedNode = document.ImportNode (xn, true);
  102. xel.AppendChild (importedNode);
  103. }
  104. }
  105. return xel;
  106. }
  107. public abstract void LoadInnerXml (XmlNodeList nodeList);
  108. public abstract void LoadInput (object obj);
  109. internal XmlResolver GetResolver ()
  110. {
  111. return xmlResolver;
  112. }
  113. #endregion // Methods
  114. }
  115. }