Merge pull request #1857 from slluis/fix-assembly-resolver
[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                 public void Demand ()
64                 {
65                         // note: here we're sure it's a CAS demand
66                         if (!SecurityManager.SecurityEnabled)
67                                 return;
68
69                         // skip frames until we get the caller (of our caller)
70                         new PermissionSet (this).CasOnlyDemand (3);
71                 }
72
73                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
74                 public void Deny ()
75                 {
76                         new PermissionSet (this).Deny ();
77                 }
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
90                 public abstract void FromXml (SecurityElement elem);
91
92                 [ComVisible (false)]
93                 public override int GetHashCode ()
94                 {
95                         return base.GetHashCode ();
96                 }
97
98                 public abstract IPermission Intersect (IPermission target);
99
100                 public abstract bool IsSubsetOf (IPermission target);
101
102                 public override string ToString ()
103                 {
104                         SecurityElement elem = ToXml ();
105                         return elem.ToString ();
106                 }
107
108                 public abstract SecurityElement ToXml ();
109
110                 public virtual IPermission Union (IPermission other)
111                 {
112                         if (null != other)
113                                 throw new System.NotSupportedException (); // other is not null.
114                         return null;
115                 }
116
117                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
118                 public void PermitOnly ()
119                 {
120                         new PermissionSet (this).PermitOnly ();
121                 }
122
123                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
124                 public static void RevertAll ()
125                 {
126                         if (!SecurityManager.SecurityEnabled)
127                                 return;
128                         throw new NotImplementedException ();
129                 }
130
131                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
132                 public static void RevertAssert ()
133                 {
134                         if (!SecurityManager.SecurityEnabled)
135                                 return;
136                         throw new NotImplementedException ();
137                 }
138
139                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
140                 public static void RevertDeny ()
141                 {
142                         if (!SecurityManager.SecurityEnabled)
143                                 return;
144                         throw new NotImplementedException ();
145                 }
146
147                 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
148                 public static void RevertPermitOnly ()
149                 {
150                         if (!SecurityManager.SecurityEnabled)
151                                 return;
152                         throw new NotImplementedException ();
153                 }
154
155                 // Internal helpers methods
156
157                 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
158                 internal SecurityElement Element (int version) 
159                 {
160                         SecurityElement se = new SecurityElement ("IPermission");
161                         Type type = this.GetType ();
162                         se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
163                         se.AddAttribute ("version", version.ToString ());
164                         return se;
165                 }
166
167                 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
168                 {
169                         string msg;
170                         switch (state) {
171                         case PermissionState.None:
172                                 break;
173                         case PermissionState.Unrestricted:
174                                 // unrestricted permissions are possible for identiy permissions
175                                 break;
176                         default:
177                                 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
178                                 throw new ArgumentException (msg, "state");
179                         }
180                         return state;
181                 }
182
183                 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion) 
184                 {
185                         if (se == null)
186                                 throw new ArgumentNullException (parameterName);
187
188                         // Tag is case-sensitive
189                         if (se.Tag != "IPermission") {
190                                 string msg = String.Format (Locale.GetText ("Invalid tag {0}"), se.Tag);
191                                 throw new ArgumentException (msg, parameterName);
192                         }
193
194                         // Note: we do not care about the class attribute at 
195                         // this stage (in fact we don't even if the class 
196                         // attribute is present or not). Anyway the object has
197                         // already be created, with success, if we're loading it
198
199                         // we assume minimum version if no version number is supplied
200                         int version = minimumVersion;
201                         string v = se.Attribute ("version");
202                         if (v != null) {
203                                 try {
204                                         version = Int32.Parse (v);
205                                 }
206                                 catch (Exception e) {
207                                         string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
208                                         msg = String.Format (msg, v);
209                                         throw new ArgumentException (msg, parameterName, e);
210                                 }
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
221                 // must be called after CheckSecurityElement (i.e. se != null)
222                 internal static bool IsUnrestricted (SecurityElement se) 
223                 {
224                         string value = se.Attribute ("Unrestricted");
225                         if (value == null)
226                                 return false;
227                         return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
228                 }
229
230                 internal static void ThrowInvalidPermission (IPermission target, Type expected) 
231                 {
232                         string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
233                         msg = String.Format (msg, target.GetType (), expected);
234                         throw new ArgumentException (msg, "target");
235                 }
236         }
237 }