XmlSecureResolver.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. #if NET_1_0
  22. #endif
  23. #if NET_1_1
  24. //
  25. // System.Xml.XmlSecureResolver.cs
  26. //
  27. // Author: Atsushi Enomoto ([email protected])
  28. //
  29. // (C) 2003 Atsushi Enomoto
  30. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  31. //
  32. using System.Net;
  33. using System.Security;
  34. using System.Security.Policy;
  35. using System.Security.Permissions;
  36. namespace System.Xml
  37. {
  38. public class XmlSecureResolver : XmlResolver
  39. {
  40. #region Static Members
  41. public static Evidence CreateEvidenceForUrl (string securityUrl)
  42. {
  43. Evidence e = new Evidence ();
  44. if ((securityUrl != null) && (securityUrl.Length > 0)) {
  45. try {
  46. Url url = new Url (securityUrl);
  47. e.AddHost (url);
  48. } catch (ArgumentException) {
  49. }
  50. try {
  51. Zone zone = Zone.CreateFromUrl (securityUrl);
  52. e.AddHost (zone);
  53. } catch (ArgumentException) {
  54. }
  55. try {
  56. Site site = Site.CreateFromUrl (securityUrl);
  57. e.AddHost (site);
  58. } catch (ArgumentException) {
  59. }
  60. }
  61. return e;
  62. }
  63. #endregion
  64. XmlResolver resolver;
  65. PermissionSet permissionSet;
  66. #region .ctor and Finalizer
  67. public XmlSecureResolver (
  68. XmlResolver resolver, Evidence evidence)
  69. {
  70. this.resolver = resolver;
  71. if (SecurityManager.SecurityEnabled) {
  72. this.permissionSet = SecurityManager.ResolvePolicy (evidence);
  73. }
  74. }
  75. public XmlSecureResolver (
  76. XmlResolver resolver, PermissionSet permissionSet)
  77. {
  78. this.resolver = resolver;
  79. this.permissionSet = permissionSet;
  80. }
  81. public XmlSecureResolver (
  82. XmlResolver resolver, string securityUrl)
  83. {
  84. this.resolver = resolver;
  85. if (SecurityManager.SecurityEnabled) {
  86. this.permissionSet = SecurityManager.ResolvePolicy (CreateEvidenceForUrl (securityUrl));
  87. }
  88. }
  89. #endregion
  90. #region Property
  91. public override ICredentials Credentials {
  92. set { resolver.Credentials = value; }
  93. }
  94. #endregion
  95. #region Methods
  96. [MonoTODO ("CAS: imperative PermitOnly isn't supported")]
  97. public override object GetEntity (
  98. Uri absoluteUri, string role, Type ofObjectToReturn)
  99. {
  100. if (SecurityManager.SecurityEnabled) {
  101. // in case the security manager was switched after the constructor was called
  102. if (permissionSet == null) {
  103. throw new SecurityException (Locale.GetText (
  104. "Security Manager wasn't active when instance was created."));
  105. }
  106. permissionSet.PermitOnly ();
  107. }
  108. return resolver.GetEntity (absoluteUri, role, ofObjectToReturn);
  109. }
  110. public override Uri ResolveUri (Uri baseUri, string relativeUri)
  111. {
  112. return resolver.ResolveUri (baseUri, relativeUri);
  113. }
  114. #endregion
  115. }
  116. }
  117. #endif