XmlSecureResolver.cs 3.5 KB

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