Merge pull request #3081 from lambdageek/coop-no-prof
[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                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
56                 public void Assert ()
57                 {
58                         new PermissionSet (this).Assert ();
59                 }
60
61                 public abstract IPermission Copy ();
62
63 #if MOBILE && DISABLE_CAS_USE
64                 [Obsolete ("CAS support is removed by linker", true)]
65 #endif
66                 public void Demand ()
67                 {
68                         // note: here we're sure it's a CAS demand
69                         if (!SecurityManager.SecurityEnabled)
70                                 return;
71
72                         // skip frames until we get the caller (of our caller)
73                         new PermissionSet (this).CasOnlyDemand (3);
74                 }
75
76                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
77                 public void Deny ()
78                 {
79                         new PermissionSet (this).Deny ();
80                 }
81
82                 [ComVisible (false)]
83                 public override bool Equals (object obj)
84                 {
85                         if (obj == null)
86                                 return false;
87                         if (obj.GetType () != this.GetType ())
88                                 return false;
89                         CodeAccessPermission cap = (obj as CodeAccessPermission);
90                         return (IsSubsetOf (cap) && cap.IsSubsetOf (this));
91                 }
92
93                 public abstract void FromXml (SecurityElement elem);
94
95                 [ComVisible (false)]
96                 public override int GetHashCode ()
97                 {
98                         return base.GetHashCode ();
99                 }
100
101                 public abstract IPermission Intersect (IPermission target);
102
103                 public abstract bool IsSubsetOf (IPermission target);
104
105                 public override string ToString ()
106                 {
107                         SecurityElement elem = ToXml ();
108                         return elem.ToString ();
109                 }
110
111                 public abstract SecurityElement ToXml ();
112
113                 public virtual IPermission Union (IPermission other)
114                 {
115                         if (null != other)
116                                 throw new System.NotSupportedException (); // other is not null.
117                         return null;
118                 }
119
120                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
121                 public void PermitOnly ()
122                 {
123                         new PermissionSet (this).PermitOnly ();
124                 }
125
126                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
127                 public static void RevertAll ()
128                 {
129                         if (!SecurityManager.SecurityEnabled)
130                                 return;
131                         throw new NotImplementedException ();
132                 }
133
134                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
135                 public static void RevertAssert ()
136                 {
137                         if (!SecurityManager.SecurityEnabled)
138                                 return;
139                         throw new NotImplementedException ();
140                 }
141
142                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
143                 public static void RevertDeny ()
144                 {
145                         if (!SecurityManager.SecurityEnabled)
146                                 return;
147                         throw new NotImplementedException ();
148                 }
149
150                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
151                 public static void RevertPermitOnly ()
152                 {
153                         if (!SecurityManager.SecurityEnabled)
154                                 return;
155                         throw new NotImplementedException ();
156                 }
157
158                 // Internal helpers methods
159
160                 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
161                 internal SecurityElement Element (int version) 
162                 {
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 ());
167                         return se;
168                 }
169
170                 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
171                 {
172                         string msg;
173                         switch (state) {
174                         case PermissionState.None:
175                                 break;
176                         case PermissionState.Unrestricted:
177                                 // unrestricted permissions are possible for identiy permissions
178                                 break;
179                         default:
180                                 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
181                                 throw new ArgumentException (msg, "state");
182                         }
183                         return state;
184                 }
185
186                 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion) 
187                 {
188                         if (se == null)
189                                 throw new ArgumentNullException (parameterName);
190
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
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
201
202                         // we assume minimum version if no version number is supplied
203                         int version = minimumVersion;
204                         string v = se.Attribute ("version");
205                         if (v != null) {
206                                 try {
207                                         version = Int32.Parse (v);
208                                 }
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);
213                                 }
214                         }
215
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);
220                         }
221                         return version;
222                 }
223
224                 // must be called after CheckSecurityElement (i.e. se != null)
225                 internal static bool IsUnrestricted (SecurityElement se) 
226                 {
227                         string value = se.Attribute ("Unrestricted");
228                         if (value == null)
229                                 return false;
230                         return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
231                 }
232
233                 internal static void ThrowInvalidPermission (IPermission target, Type expected) 
234                 {
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");
238                 }
239         }
240 }