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