2 // System.Security.CodeAccessPermission.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Nick Drochak, ndrochak@gol.com
7 // Sebastien Pouliot <sebastien@ximian.com>
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)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
34 using System.Diagnostics;
35 using System.Globalization;
36 using System.Reflection;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39 using System.Security.Permissions;
40 using System.Threading;
42 namespace System.Security {
45 [SecurityPermission (SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true)]
47 [MonoTODO ("CAS support is experimental (and unsupported).")]
48 public abstract class CodeAccessPermission : IPermission, ISecurityEncodable, IStackWalk {
51 protected CodeAccessPermission ()
55 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
58 new PermissionSet (this).Assert ();
61 public abstract IPermission Copy ();
63 #if MOBILE && DISABLE_CAS_USE
64 [Obsolete ("CAS support is removed by linker", true)]
68 // note: here we're sure it's a CAS demand
69 if (!SecurityManager.SecurityEnabled)
72 // skip frames until we get the caller (of our caller)
73 new PermissionSet (this).CasOnlyDemand (3);
76 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
79 new PermissionSet (this).Deny ();
83 public override bool Equals (object obj)
87 if (obj.GetType () != this.GetType ())
89 CodeAccessPermission cap = (obj as CodeAccessPermission);
90 return (IsSubsetOf (cap) && cap.IsSubsetOf (this));
93 public abstract void FromXml (SecurityElement elem);
96 public override int GetHashCode ()
98 return base.GetHashCode ();
101 public abstract IPermission Intersect (IPermission target);
103 public abstract bool IsSubsetOf (IPermission target);
105 public override string ToString ()
107 SecurityElement elem = ToXml ();
108 return elem.ToString ();
111 public abstract SecurityElement ToXml ();
113 public virtual IPermission Union (IPermission other)
116 throw new System.NotSupportedException (); // other is not null.
120 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
121 public void PermitOnly ()
123 new PermissionSet (this).PermitOnly ();
126 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
127 public static void RevertAll ()
129 if (!SecurityManager.SecurityEnabled)
131 throw new NotImplementedException ();
134 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
135 public static void RevertAssert ()
137 if (!SecurityManager.SecurityEnabled)
139 throw new NotImplementedException ();
142 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
143 public static void RevertDeny ()
145 if (!SecurityManager.SecurityEnabled)
147 throw new NotImplementedException ();
150 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
151 public static void RevertPermitOnly ()
153 if (!SecurityManager.SecurityEnabled)
155 throw new NotImplementedException ();
158 // Internal helpers methods
160 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
161 internal SecurityElement Element (int version)
163 SecurityElement se = new SecurityElement ("IPermission");
164 Type type = this.GetType ();
165 se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
166 se.AddAttribute ("version", version.ToString ());
170 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
174 case PermissionState.None:
176 case PermissionState.Unrestricted:
177 // unrestricted permissions are possible for identiy permissions
180 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
181 throw new ArgumentException (msg, "state");
186 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
189 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);
197 // Note: we do not care about the class attribute at
198 // this stage (in fact we don't even if the class
199 // attribute is present or not). Anyway the object has
200 // already be created, with success, if we're loading it
202 // we assume minimum version if no version number is supplied
203 int version = minimumVersion;
204 string v = se.Attribute ("version");
207 version = Int32.Parse (v);
209 catch (Exception e) {
210 string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
211 msg = String.Format (msg, v);
212 throw new ArgumentException (msg, parameterName, e);
216 if ((version < minimumVersion) || (version > maximumVersion)) {
217 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
218 msg = String.Format (msg, version, minimumVersion, maximumVersion);
219 throw new ArgumentException (msg, parameterName);
224 // must be called after CheckSecurityElement (i.e. se != null)
225 internal static bool IsUnrestricted (SecurityElement se)
227 string value = se.Attribute ("Unrestricted");
230 return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
233 internal static void ThrowInvalidPermission (IPermission target, Type expected)
235 string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
236 msg = String.Format (msg, target.GetType (), expected);
237 throw new ArgumentException (msg, "target");