1e501c5997e527e9c35f199ed13b47dbda15029d
[mono.git] / mcs / class / referencesource / mscorlib / system / security / permissions / securitypermission.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // SecurityPermission.cs
7 // 
8 // <OWNER>Microsoft</OWNER>
9 //
10
11 namespace System.Security.Permissions
12 {
13     using System;
14     using System.IO;
15     using System.Security.Util;
16     using System.Text;
17     using System.Threading;
18     using System.Runtime.Remoting;
19     using System.Security;
20     using System.Runtime.Serialization;
21     using System.Reflection;
22     using System.Globalization;
23     using System.Diagnostics.Contracts;
24
25 [Serializable]
26     [Flags]
27 [System.Runtime.InteropServices.ComVisible(true)]
28 #if !FEATURE_CAS_POLICY
29     // The csharp compiler requires these types to be public, but they are not used elsewhere.
30     [Obsolete("SecurityPermissionFlag is no longer accessible to application code.")]
31 #endif
32     public enum SecurityPermissionFlag
33     {
34         NoFlags = 0x00,
35         /* The following enum value is used in the EE (ASSERT_PERMISSION in security.cpp)
36          * Should this value change, make corresponding changes there
37          */ 
38         Assertion = 0x01,
39         UnmanagedCode = 0x02,       // Update vm\Security.h if you change this !
40         SkipVerification = 0x04,    // Update vm\Security.h if you change this !
41         Execution = 0x08,
42         ControlThread = 0x10,
43         ControlEvidence = 0x20,
44         ControlPolicy = 0x40,
45         SerializationFormatter = 0x80,
46         ControlDomainPolicy = 0x100,
47         ControlPrincipal = 0x200,
48         ControlAppDomain = 0x400,
49         RemotingConfiguration = 0x800,
50         Infrastructure = 0x1000,
51         BindingRedirects = 0x2000,
52         AllFlags = 0x3fff,
53     }
54
55 [System.Runtime.InteropServices.ComVisible(true)]
56     [Serializable]
57     sealed public class SecurityPermission 
58            : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
59     {
60 #pragma warning disable 618
61         private SecurityPermissionFlag m_flags;
62 #pragma warning restore 618
63         
64         //
65         // Public Constructors
66         //
67     
68         public SecurityPermission(PermissionState state)
69         {
70             if (state == PermissionState.Unrestricted)
71             {
72                 SetUnrestricted( true );
73             }
74             else if (state == PermissionState.None)
75             {
76                 SetUnrestricted( false );
77                 Reset();
78             }
79             else
80             {
81                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
82             }
83         }
84         
85         
86         // SecurityPermission
87         //
88 #pragma warning disable 618
89         public SecurityPermission(SecurityPermissionFlag flag)
90 #pragma warning restore 618
91         {
92             VerifyAccess(flag);
93             
94             SetUnrestricted(false);
95             m_flags = flag;
96         }
97     
98     
99         //------------------------------------------------------
100         //
101         // PRIVATE AND PROTECTED MODIFIERS 
102         //
103         //------------------------------------------------------
104         
105         
106         private void SetUnrestricted(bool unrestricted)
107         {
108             if (unrestricted)
109             {
110 #pragma warning disable 618
111                 m_flags = SecurityPermissionFlag.AllFlags;
112 #pragma warning restore 618
113             }
114         }
115     
116         private void Reset()
117         {
118 #pragma warning disable 618
119             m_flags = SecurityPermissionFlag.NoFlags;
120 #pragma warning restore 618
121         }
122         
123         
124 #pragma warning disable 618
125         public SecurityPermissionFlag Flags
126 #pragma warning restore 618
127         {
128             set
129             {
130                 VerifyAccess(value);
131             
132                 m_flags = value;
133             }
134             
135             get
136             {
137                 return m_flags;
138             }
139         }
140         
141         //
142         // CodeAccessPermission methods
143         // 
144         
145        /*
146          * IPermission interface implementation
147          */
148          
149         public override bool IsSubsetOf(IPermission target)
150         {
151             if (target == null)
152             {
153                 return m_flags == 0;
154             }
155         
156             SecurityPermission operand = target as SecurityPermission;
157             if (operand != null)
158             {
159                 return (((int)this.m_flags) & ~((int)operand.m_flags)) == 0;
160             }
161             else
162             {
163                 throw new 
164                     ArgumentException(
165                                     Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
166                                      );
167             }
168
169         }
170         
171         public override IPermission Union(IPermission target) {
172             if (target == null) return(this.Copy());
173             if (!VerifyType(target)) {
174                 throw new 
175                     ArgumentException(
176                                     Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
177                                      );
178             }
179             SecurityPermission sp_target = (SecurityPermission) target;
180             if (sp_target.IsUnrestricted() || IsUnrestricted()) {
181                 return(new SecurityPermission(PermissionState.Unrestricted));
182             }
183 #pragma warning disable 618
184             SecurityPermissionFlag flag_union = (SecurityPermissionFlag)(m_flags | sp_target.m_flags);
185 #pragma warning restore 618
186             return(new SecurityPermission(flag_union));
187         }
188     
189         public override IPermission Intersect(IPermission target)
190         {
191             if (target == null)
192                 return null;
193             else if (!VerifyType(target))
194             {
195                 throw new 
196                     ArgumentException(
197                                     Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
198                                      );
199             }
200             
201             SecurityPermission operand = (SecurityPermission)target;
202 #pragma warning disable 618
203             SecurityPermissionFlag isectFlags = SecurityPermissionFlag.NoFlags;
204 #pragma warning restore 618
205            
206             if (operand.IsUnrestricted())
207             {
208                 if (this.IsUnrestricted())
209                     return new SecurityPermission(PermissionState.Unrestricted);
210                 else
211 #pragma warning disable 618
212                     isectFlags = (SecurityPermissionFlag)this.m_flags;
213 #pragma warning restore 618
214             }
215             else if (this.IsUnrestricted())
216             {
217 #pragma warning disable 618
218                 isectFlags = (SecurityPermissionFlag)operand.m_flags;
219 #pragma warning restore 618
220             }
221             else
222             {
223 #pragma warning disable 618
224                 isectFlags = (SecurityPermissionFlag)m_flags & (SecurityPermissionFlag)operand.m_flags;
225 #pragma warning restore 618
226             }
227             
228             if (isectFlags == 0)
229                 return null;
230             else
231                 return new SecurityPermission(isectFlags);
232         }
233     
234         public override IPermission Copy()
235         {
236             if (IsUnrestricted())
237                 return new SecurityPermission(PermissionState.Unrestricted);
238             else
239 #pragma warning disable 618
240                 return new SecurityPermission((SecurityPermissionFlag)m_flags);
241 #pragma warning restore 618
242         }
243     
244         public bool IsUnrestricted()
245         {
246 #pragma warning disable 618
247             return m_flags == SecurityPermissionFlag.AllFlags;
248 #pragma warning restore 618
249         }
250         
251         private
252 #pragma warning disable 618
253         void VerifyAccess(SecurityPermissionFlag type)
254 #pragma warning restore 618
255         {
256 #pragma warning disable 618
257             if ((type & ~SecurityPermissionFlag.AllFlags) != 0)
258 #pragma warning restore 618
259                 throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)type));
260             Contract.EndContractBlock();
261         }
262
263 #if FEATURE_CAS_POLICY
264         //------------------------------------------------------
265         //
266         // PUBLIC ENCODING METHODS
267         //
268         //------------------------------------------------------
269         
270         private const String _strHeaderAssertion  = "Assertion";
271         private const String _strHeaderUnmanagedCode = "UnmanagedCode";
272         private const String _strHeaderExecution = "Execution";
273         private const String _strHeaderSkipVerification = "SkipVerification";
274         private const String _strHeaderControlThread = "ControlThread";
275         private const String _strHeaderControlEvidence = "ControlEvidence";
276         private const String _strHeaderControlPolicy = "ControlPolicy";
277         private const String _strHeaderSerializationFormatter = "SerializationFormatter";
278         private const String _strHeaderControlDomainPolicy = "ControlDomainPolicy";
279         private const String _strHeaderControlPrincipal = "ControlPrincipal";
280         private const String _strHeaderControlAppDomain = "ControlAppDomain";
281     
282         public override SecurityElement ToXml()
283         {
284             SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, "System.Security.Permissions.SecurityPermission" );
285             if (!IsUnrestricted())
286             {
287                 esd.AddAttribute( "Flags", XMLUtil.BitFieldEnumToString( typeof( SecurityPermissionFlag ), m_flags ) );
288             }
289             else
290             {
291                 esd.AddAttribute( "Unrestricted", "true" );
292             }
293             return esd;
294         }
295     
296         public override void FromXml(SecurityElement esd)
297         {
298             CodeAccessPermission.ValidateElement( esd, this );
299             if (XMLUtil.IsUnrestricted( esd ))
300             {
301                 m_flags = SecurityPermissionFlag.AllFlags;
302                 return;
303             }
304            
305             Reset () ;
306             SetUnrestricted (false) ;
307     
308             String flags = esd.Attribute( "Flags" );
309     
310             if (flags != null)
311                 m_flags = (SecurityPermissionFlag)Enum.Parse( typeof( SecurityPermissionFlag ), flags );
312         }
313 #endif // FEATURE_CAS_POLICY
314
315         //
316         // Object Overrides
317         //
318         
319     #if ZERO   // Do not remove this code, usefull for debugging
320         public override String ToString()
321         {
322             StringBuilder sb = new StringBuilder();
323             sb.Append("SecurityPermission(");
324             if (IsUnrestricted())
325             {
326                 sb.Append("Unrestricted");
327             }
328             else
329             {
330                 if (GetFlag(SecurityPermissionFlag.Assertion))
331                     sb.Append("Assertion; ");
332                 if (GetFlag(SecurityPermissionFlag.UnmanagedCode))
333                     sb.Append("UnmangedCode; ");
334                 if (GetFlag(SecurityPermissionFlag.SkipVerification))
335                     sb.Append("SkipVerification; ");
336                 if (GetFlag(SecurityPermissionFlag.Execution))
337                     sb.Append("Execution; ");
338                 if (GetFlag(SecurityPermissionFlag.ControlThread))
339                     sb.Append("ControlThread; ");
340                 if (GetFlag(SecurityPermissionFlag.ControlEvidence))
341                     sb.Append("ControlEvidence; ");
342                 if (GetFlag(SecurityPermissionFlag.ControlPolicy))
343                     sb.Append("ControlPolicy; ");
344                 if (GetFlag(SecurityPermissionFlag.SerializationFormatter))
345                     sb.Append("SerializationFormatter; ");
346                 if (GetFlag(SecurityPermissionFlag.ControlDomainPolicy))
347                     sb.Append("ControlDomainPolicy; ");
348                 if (GetFlag(SecurityPermissionFlag.ControlPrincipal))
349                     sb.Append("ControlPrincipal; ");
350             }
351             
352             sb.Append(")");
353             return sb.ToString();
354         }
355     #endif
356
357         /// <internalonly/>
358         int IBuiltInPermission.GetTokenIndex()
359         {
360             return SecurityPermission.GetTokenIndex();
361         }
362
363         internal static int GetTokenIndex()
364         {
365             return BuiltInPermissionIndex.SecurityPermissionIndex;
366         }
367     }
368 }