CodeAccessPermission.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // System.Security.CodeAccessPermission.cs
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. // Nick Drochak, [email protected]
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // (C) Ximian, Inc. http://www.ximian.com
  10. // Copyright (C) 2001 Nick Drochak, All Rights Reserved
  11. // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
  12. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  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.Diagnostics;
  34. using System.Globalization;
  35. using System.Reflection;
  36. using System.Runtime.CompilerServices;
  37. using System.Runtime.InteropServices;
  38. using System.Security.Permissions;
  39. using System.Threading;
  40. namespace System.Security {
  41. [Serializable]
  42. [SecurityPermission (SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true)]
  43. [ComVisible (true)]
  44. [MonoTODO ("CAS support is experimental (and unsupported).")]
  45. public abstract class CodeAccessPermission : IPermission, ISecurityEncodable, IStackWalk {
  46. protected CodeAccessPermission ()
  47. {
  48. }
  49. #if MOBILE
  50. [Conditional ("FEATURE_MONO_CAS")]
  51. #else
  52. [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
  53. #endif
  54. public void Assert ()
  55. {
  56. new PermissionSet (this).Assert ();
  57. }
  58. public abstract IPermission Copy ();
  59. #if MOBILE
  60. [Conditional ("FEATURE_MONO_CAS")]
  61. #endif
  62. public void Demand ()
  63. {
  64. // note: here we're sure it's a CAS demand
  65. if (!SecurityManager.SecurityEnabled)
  66. return;
  67. // skip frames until we get the caller (of our caller)
  68. new PermissionSet (this).CasOnlyDemand (3);
  69. }
  70. #if MOBILE
  71. [Conditional ("FEATURE_MONO_CAS")]
  72. #else
  73. [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
  74. #endif
  75. public void Deny ()
  76. {
  77. new PermissionSet (this).Deny ();
  78. }
  79. [ComVisible (false)]
  80. public override bool Equals (object obj)
  81. {
  82. if (obj == null)
  83. return false;
  84. if (obj.GetType () != this.GetType ())
  85. return false;
  86. CodeAccessPermission cap = (obj as CodeAccessPermission);
  87. return (IsSubsetOf (cap) && cap.IsSubsetOf (this));
  88. }
  89. public abstract void FromXml (SecurityElement elem);
  90. [ComVisible (false)]
  91. public override int GetHashCode ()
  92. {
  93. return base.GetHashCode ();
  94. }
  95. public abstract IPermission Intersect (IPermission target);
  96. public abstract bool IsSubsetOf (IPermission target);
  97. public override string ToString ()
  98. {
  99. SecurityElement elem = ToXml ();
  100. return elem.ToString ();
  101. }
  102. public abstract SecurityElement ToXml ();
  103. public virtual IPermission Union (IPermission other)
  104. {
  105. if (null != other)
  106. throw new System.NotSupportedException (); // other is not null.
  107. return null;
  108. }
  109. #if MOBILE
  110. [Conditional ("FEATURE_MONO_CAS")]
  111. #else
  112. [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
  113. #endif
  114. public void PermitOnly ()
  115. {
  116. new PermissionSet (this).PermitOnly ();
  117. }
  118. #if MOBILE
  119. [Conditional ("FEATURE_MONO_CAS")]
  120. #else
  121. [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
  122. #endif
  123. public static void RevertAll ()
  124. {
  125. if (!SecurityManager.SecurityEnabled)
  126. return;
  127. throw new NotImplementedException ();
  128. }
  129. #if MOBILE
  130. [Conditional ("FEATURE_MONO_CAS")]
  131. #else
  132. [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
  133. #endif
  134. public static void RevertAssert ()
  135. {
  136. if (!SecurityManager.SecurityEnabled)
  137. return;
  138. throw new NotImplementedException ();
  139. }
  140. #if MOBILE
  141. [Conditional ("FEATURE_MONO_CAS")]
  142. #else
  143. [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
  144. #endif
  145. public static void RevertDeny ()
  146. {
  147. if (!SecurityManager.SecurityEnabled)
  148. return;
  149. throw new NotImplementedException ();
  150. }
  151. #if MOBILE
  152. [Conditional ("FEATURE_MONO_CAS")]
  153. #else
  154. [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
  155. #endif
  156. public static void RevertPermitOnly ()
  157. {
  158. if (!SecurityManager.SecurityEnabled)
  159. return;
  160. throw new NotImplementedException ();
  161. }
  162. // Internal helpers methods
  163. // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
  164. internal SecurityElement Element (int version)
  165. {
  166. SecurityElement se = new SecurityElement ("IPermission");
  167. Type type = this.GetType ();
  168. se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
  169. se.AddAttribute ("version", version.ToString ());
  170. return se;
  171. }
  172. internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
  173. {
  174. string msg;
  175. switch (state) {
  176. case PermissionState.None:
  177. break;
  178. case PermissionState.Unrestricted:
  179. // unrestricted permissions are possible for identiy permissions
  180. break;
  181. default:
  182. msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
  183. throw new ArgumentException (msg, "state");
  184. }
  185. return state;
  186. }
  187. internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
  188. {
  189. if (se == null)
  190. throw new ArgumentNullException (parameterName);
  191. // Tag is case-sensitive
  192. if (se.Tag != "IPermission") {
  193. string msg = String.Format (Locale.GetText ("Invalid tag {0}"), se.Tag);
  194. throw new ArgumentException (msg, parameterName);
  195. }
  196. // Note: we do not care about the class attribute at
  197. // this stage (in fact we don't even if the class
  198. // attribute is present or not). Anyway the object has
  199. // already be created, with success, if we're loading it
  200. // we assume minimum version if no version number is supplied
  201. int version = minimumVersion;
  202. string v = se.Attribute ("version");
  203. if (v != null) {
  204. try {
  205. version = Int32.Parse (v);
  206. }
  207. catch (Exception e) {
  208. string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
  209. msg = String.Format (msg, v);
  210. throw new ArgumentException (msg, parameterName, e);
  211. }
  212. }
  213. if ((version < minimumVersion) || (version > maximumVersion)) {
  214. string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
  215. msg = String.Format (msg, version, minimumVersion, maximumVersion);
  216. throw new ArgumentException (msg, parameterName);
  217. }
  218. return version;
  219. }
  220. // must be called after CheckSecurityElement (i.e. se != null)
  221. internal static bool IsUnrestricted (SecurityElement se)
  222. {
  223. string value = se.Attribute ("Unrestricted");
  224. if (value == null)
  225. return false;
  226. return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
  227. }
  228. internal static void ThrowInvalidPermission (IPermission target, Type expected)
  229. {
  230. string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
  231. msg = String.Format (msg, target.GetType (), expected);
  232. throw new ArgumentException (msg, "target");
  233. }
  234. #if MOBILE
  235. // Workaround for CS0629
  236. void IStackWalk.Assert ()
  237. {
  238. }
  239. void IStackWalk.Deny ()
  240. {
  241. }
  242. void IStackWalk.PermitOnly ()
  243. {
  244. }
  245. void IStackWalk.Demand ()
  246. {
  247. }
  248. void IPermission.Demand ()
  249. {
  250. }
  251. #endif
  252. }
  253. }