Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / system / security / permissions / permissionattributes.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>[....]</OWNER>
7 namespace System.Security.Permissions
8 {
9
10     using System.Security.Util;
11     using System.IO;
12     using System.Security.Policy;
13 #if FEATURE_MACL
14     using System.Security.AccessControl;
15 #endif
16     using System.Text;
17     using System.Runtime.Serialization.Formatters;
18     using System.Threading;
19     using System.Runtime.InteropServices;
20     using System.Runtime.Remoting;
21     using System.Runtime.Serialization;
22 #if FEATURE_X509
23     using System.Security.Cryptography.X509Certificates;
24 #endif
25     using System.Runtime.Versioning;
26     using System.Diagnostics.Contracts;
27     
28     [Serializable]
29 [System.Runtime.InteropServices.ComVisible(true)]
30 #if !FEATURE_CAS_POLICY
31     // The csharp compiler requires these types to be public, but they are not used elsewhere.
32     [Obsolete("SecurityAction is no longer accessible to application code.")]
33 #endif
34     public enum SecurityAction
35     {
36         // Demand permission of all caller
37         Demand = 2,
38
39         // Assert permission so callers don't need
40         Assert = 3,
41
42         // Deny permissions so checks will fail
43         [Obsolete("Deny is obsolete and will be removed in a future release of the .NET Framework. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
44         Deny = 4,
45
46         // Reduce permissions so check will fail
47         PermitOnly = 5,
48
49         // Demand permission of caller
50         LinkDemand = 6,
51     
52         // Demand permission of a subclass
53         InheritanceDemand = 7,
54
55         // Request minimum permissions to run
56         [Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
57         RequestMinimum = 8,
58
59         // Request optional additional permissions
60         [Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
61         RequestOptional = 9,
62
63         // Refuse to be granted these permissions
64         [Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
65         RequestRefuse = 10,
66     }
67
68
69 [Serializable]
70 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
71 [System.Runtime.InteropServices.ComVisible(true)]
72 #if !FEATURE_CAS_POLICY
73     // The csharp compiler requires these types to be public, but they are not used elsewhere.
74     [Obsolete("SecurityAttribute is no longer accessible to application code.")]
75 #endif
76     public abstract class SecurityAttribute : System.Attribute
77     {
78         /// <internalonly/>
79         internal SecurityAction m_action;
80         /// <internalonly/>
81         internal bool m_unrestricted;
82 #if FEATURE_LEGACYNETCF
83         public
84 #else
85         protected
86 #endif
87             SecurityAttribute( SecurityAction action ) 
88         {
89             m_action = action;
90         }
91
92         public SecurityAction Action
93         {
94             get { return m_action; }
95             set { m_action = value; }
96         }
97
98         public bool Unrestricted
99         {
100             get { return m_unrestricted; }
101             set { m_unrestricted = value; }
102         }
103
104         abstract public IPermission CreatePermission();
105
106         [System.Security.SecurityCritical]  // auto-generated
107         internal static unsafe IntPtr FindSecurityAttributeTypeHandle(String typeName)
108         {
109             PermissionSet.s_fullTrust.Assert();
110             Type t = Type.GetType(typeName, false, false);
111             if(t == null)
112                 return IntPtr.Zero;
113             IntPtr typeHandle = t.TypeHandle.Value;
114             return typeHandle;
115         }
116     }
117
118 [Serializable]
119 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
120 [System.Runtime.InteropServices.ComVisible(true)]
121 #if !FEATURE_CAS_POLICY
122     // The csharp compiler requires these types to be public, but they are not used elsewhere.
123     [Obsolete("CodeAccessSecurityAttribute is no longer accessible to application code.")]
124 #endif
125     public abstract class CodeAccessSecurityAttribute : SecurityAttribute
126     {
127 #if FEATURE_LEGACYNETCF
128         public
129 #else
130         protected
131 #endif
132             CodeAccessSecurityAttribute( SecurityAction action )
133             : base( action )
134         {
135         }
136     }
137
138     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
139 [System.Runtime.InteropServices.ComVisible(true)]
140     [Serializable]
141 #pragma warning disable 618
142     sealed public class EnvironmentPermissionAttribute : CodeAccessSecurityAttribute
143 #pragma warning restore 618
144     {
145         private String m_read = null;
146         private String m_write = null;
147     
148 #pragma warning disable 618
149         public EnvironmentPermissionAttribute( SecurityAction action )
150 #pragma warning restore 618
151             : base( action )
152         {
153         }
154
155         public String Read {
156             get { return m_read; }
157             set { m_read = value; }
158         }
159     
160         public String Write {
161             get { return m_write; }
162             set { m_write = value; }
163         }
164
165         public String All {
166             get { throw new NotSupportedException( Environment.GetResourceString( "NotSupported_GetMethod" ) ); }
167             set { m_write = value; m_read = value; }
168         }
169
170         public override IPermission CreatePermission()
171         {
172             if (m_unrestricted)
173             {
174                 return new EnvironmentPermission(PermissionState.Unrestricted);
175             }
176             else
177             {
178                 EnvironmentPermission perm = new EnvironmentPermission(PermissionState.None);
179                 if (m_read != null)
180                     perm.SetPathList( EnvironmentPermissionAccess.Read, m_read );
181                 if (m_write != null)
182                     perm.SetPathList( EnvironmentPermissionAccess.Write, m_write );
183                 return perm;
184             }
185         }
186     }
187
188     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
189 [System.Runtime.InteropServices.ComVisible(true)]
190     [Serializable]
191 #pragma warning disable 618
192     sealed public class FileDialogPermissionAttribute : CodeAccessSecurityAttribute
193 #pragma warning restore 618
194     {
195         private FileDialogPermissionAccess m_access;
196
197 #pragma warning disable 618
198         public FileDialogPermissionAttribute( SecurityAction action )
199 #pragma warning restore 618
200             : base( action )
201         {
202         }
203
204         public bool Open
205         {
206             get { return (m_access & FileDialogPermissionAccess.Open) != 0; }
207             set { m_access = value ? m_access | FileDialogPermissionAccess.Open : m_access & ~FileDialogPermissionAccess.Open; }
208         }
209             
210         public bool Save
211         {
212             get { return (m_access & FileDialogPermissionAccess.Save) != 0; }
213             set { m_access = value ? m_access | FileDialogPermissionAccess.Save : m_access & ~FileDialogPermissionAccess.Save; }
214         }
215
216         public override IPermission CreatePermission()
217         {
218             if (m_unrestricted)
219             {
220                 return new FileDialogPermission( PermissionState.Unrestricted );
221             }
222             else
223             {
224                 return new FileDialogPermission( m_access );
225             }
226         }
227     }
228
229
230     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
231 [System.Runtime.InteropServices.ComVisible(true)]
232     [Serializable]
233 #pragma warning disable 618
234     sealed public class FileIOPermissionAttribute : CodeAccessSecurityAttribute
235 #pragma warning restore 618
236     {
237         private String m_read = null;
238         private String m_write = null;
239         private String m_append = null;
240         private String m_pathDiscovery = null;
241         private String m_viewAccess = null;
242         private String m_changeAccess = null;
243         [OptionalField(VersionAdded = 2)] private FileIOPermissionAccess m_allLocalFiles = FileIOPermissionAccess.NoAccess;
244         [OptionalField(VersionAdded = 2)] private FileIOPermissionAccess m_allFiles = FileIOPermissionAccess.NoAccess;
245     
246 #pragma warning disable 618
247         public FileIOPermissionAttribute( SecurityAction action )
248 #pragma warning restore 618
249             : base( action )
250         {
251         }
252
253         public String Read {
254             get { return m_read; }
255             set { m_read = value; }
256         }
257     
258         public String Write {
259             get { return m_write; }
260             set { m_write = value; }
261         }
262
263         public String Append {
264             get { return m_append; }
265             set { m_append = value; }
266         }
267
268         public String PathDiscovery {
269             get { return m_pathDiscovery; }
270             set { m_pathDiscovery = value; }
271         }
272
273         public String ViewAccessControl {
274             get { return m_viewAccess; }
275             set { m_viewAccess = value; }
276         }
277
278         public String ChangeAccessControl {
279             get { return m_changeAccess; }
280             set { m_changeAccess = value; }
281         }
282
283         [Obsolete("Please use the ViewAndModify property instead.")]
284         public String All {
285             set { m_read = value; m_write = value; m_append = value; m_pathDiscovery = value; }
286             get { throw new NotSupportedException( Environment.GetResourceString( "NotSupported_GetMethod" ) ); }
287         }
288
289         // Read, Write, Append, PathDiscovery, but no ACL-related permissions
290         public String ViewAndModify {
291             get { throw new NotSupportedException( Environment.GetResourceString( "NotSupported_GetMethod" ) ); }
292             set { m_read = value; m_write = value; m_append = value; m_pathDiscovery = value; }
293         }
294
295         public FileIOPermissionAccess AllFiles {
296             get { return m_allFiles; }
297             set { m_allFiles = value; }
298         }
299
300         public FileIOPermissionAccess AllLocalFiles {
301             get { return m_allLocalFiles; }
302             set { m_allLocalFiles = value; }
303         }
304
305         public override IPermission CreatePermission()
306         {
307             if (m_unrestricted)
308             {
309                 return new FileIOPermission(PermissionState.Unrestricted);
310             }
311             else
312             {
313                 FileIOPermission perm = new FileIOPermission(PermissionState.None);
314                 if (m_read != null)
315                     perm.SetPathList( FileIOPermissionAccess.Read, m_read );
316                 if (m_write != null)
317                     perm.SetPathList( FileIOPermissionAccess.Write, m_write );
318                 if (m_append != null)
319                     perm.SetPathList( FileIOPermissionAccess.Append, m_append );
320                 if (m_pathDiscovery != null)
321                     perm.SetPathList( FileIOPermissionAccess.PathDiscovery, m_pathDiscovery );
322 #if FEATURE_MACL
323                 if (m_viewAccess != null)
324                     perm.SetPathList( FileIOPermissionAccess.NoAccess, AccessControlActions.View, new String[] { m_viewAccess }, false );
325                 if (m_changeAccess != null)
326                     perm.SetPathList( FileIOPermissionAccess.NoAccess, AccessControlActions.Change, new String[] { m_changeAccess }, false );
327 #endif
328
329                 perm.AllFiles = m_allFiles;
330                 perm.AllLocalFiles = m_allLocalFiles;
331                 return perm;
332             }
333         }
334     }
335
336 #if !FEATURE_PAL
337     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
338     [Serializable]
339 [System.Runtime.InteropServices.ComVisible(true)]
340 #pragma warning disable 618
341     public sealed class KeyContainerPermissionAttribute : CodeAccessSecurityAttribute {
342 #pragma warning restore 618
343         KeyContainerPermissionFlags m_flags = KeyContainerPermissionFlags.NoFlags;
344         private string m_keyStore;
345         private string m_providerName;
346         private int m_providerType = -1;
347         private string m_keyContainerName;
348         private int m_keySpec = -1;
349
350 #pragma warning disable 618
351         public KeyContainerPermissionAttribute(SecurityAction action) : base(action) {}
352 #pragma warning restore 618
353
354         public string KeyStore {
355             get { return m_keyStore; }
356             set { m_keyStore = value; }
357         }
358
359         public string ProviderName {
360             get { return m_providerName; }
361             set { m_providerName = value; }
362         }
363
364         public int ProviderType {
365             get { return m_providerType; }
366             set { m_providerType = value; }
367         }
368
369         public string KeyContainerName {
370             get { return m_keyContainerName; }
371             set { m_keyContainerName = value; }
372         }
373
374         public int KeySpec {
375             get { return m_keySpec; }
376             set { m_keySpec = value; }
377         }
378
379         public KeyContainerPermissionFlags Flags {
380             get { return m_flags; }
381             set { m_flags = value; }
382         }
383
384         public override IPermission CreatePermission() {
385             if (m_unrestricted) {
386                 return new KeyContainerPermission(PermissionState.Unrestricted);
387             } else {
388                 if (KeyContainerPermissionAccessEntry.IsUnrestrictedEntry(m_keyStore, m_providerName, m_providerType, m_keyContainerName, m_keySpec))
389                     return new KeyContainerPermission(m_flags);
390
391                 // create a KeyContainerPermission with a single access entry.
392                 KeyContainerPermission cp = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
393                 KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(m_keyStore, m_providerName, m_providerType, m_keyContainerName, m_keySpec, m_flags);
394                 cp.AccessEntries.Add(accessEntry);
395                 return cp;
396             }
397         }
398     }
399 #endif // !FEATURE_PAL
400
401 #if !FEATURE_CORECLR
402     // PrincipalPermissionAttribute currently derives from
403     // CodeAccessSecurityAttribute, even though it's not related to code access
404     // security. This is because compilers are currently looking for
405     // CodeAccessSecurityAttribute as a direct parent class rather than
406     // SecurityAttribute as the root class.
407     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = false )] 
408 [System.Runtime.InteropServices.ComVisible(true)]
409     [Serializable]
410     sealed public class PrincipalPermissionAttribute : CodeAccessSecurityAttribute
411     {
412         private String m_name = null;
413         private String m_role = null;
414         private bool m_authenticated = true;
415     
416         public PrincipalPermissionAttribute( SecurityAction action )
417             : base( action )
418         {
419         }
420         
421         public String Name
422         {
423             get { return m_name; }
424             set { m_name = value; }
425         }
426         
427         public String Role
428         {
429             get { return m_role; }
430             set { m_role = value; }
431         }
432         
433         public bool Authenticated
434         {
435             get { return m_authenticated; }
436             set { m_authenticated = value; }
437         }
438         
439         
440         public override IPermission CreatePermission()
441         {
442             if (m_unrestricted)
443             {
444                 return new PrincipalPermission( PermissionState.Unrestricted );
445             }
446             else
447             {
448                 return new PrincipalPermission( m_name, m_role, m_authenticated );
449             }
450         }
451     }
452 #endif // !FEATURE_CORECLR
453
454     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
455 [System.Runtime.InteropServices.ComVisible(true)]
456     [Serializable]
457 #pragma warning disable 618
458     sealed public class ReflectionPermissionAttribute : CodeAccessSecurityAttribute
459 #pragma warning restore 618
460     {
461         private ReflectionPermissionFlag m_flag = ReflectionPermissionFlag.NoFlags;
462
463 #pragma warning disable 618
464         public ReflectionPermissionAttribute( SecurityAction action )
465 #pragma warning restore 618
466             : base( action )
467         {
468         }
469
470         public ReflectionPermissionFlag Flags {
471             get { return m_flag; }
472             set { m_flag = value; }
473         }
474
475         [Obsolete("This API has been deprecated. http://go.microsoft.com/fwlink/?linkid=14202")]
476         public bool TypeInformation {
477 #pragma warning disable 618
478             get { return (m_flag & ReflectionPermissionFlag.TypeInformation) != 0; }
479             set { m_flag = value ? m_flag | ReflectionPermissionFlag.TypeInformation : m_flag & ~ReflectionPermissionFlag.TypeInformation; }
480 #pragma warning restore 618
481         }
482
483         public bool MemberAccess {
484             get { return (m_flag & ReflectionPermissionFlag.MemberAccess) != 0; }
485             set { m_flag = value ? m_flag | ReflectionPermissionFlag.MemberAccess : m_flag & ~ReflectionPermissionFlag.MemberAccess; }
486         }
487
488         [Obsolete("This permission is no longer used by the CLR.")]
489         public bool ReflectionEmit {
490 #pragma warning disable 618
491             get { return (m_flag & ReflectionPermissionFlag.ReflectionEmit) != 0; }
492             set { m_flag = value ? m_flag | ReflectionPermissionFlag.ReflectionEmit : m_flag & ~ReflectionPermissionFlag.ReflectionEmit; }
493 #pragma warning restore 618
494         }
495
496         public bool RestrictedMemberAccess
497         {
498             get { return (m_flag & ReflectionPermissionFlag.RestrictedMemberAccess) != 0; }
499             set { m_flag = value ? m_flag | ReflectionPermissionFlag.RestrictedMemberAccess : m_flag & ~ReflectionPermissionFlag.RestrictedMemberAccess; }
500         }
501
502         public override IPermission CreatePermission()
503         {
504             if (m_unrestricted)
505             {
506                 return new ReflectionPermission( PermissionState.Unrestricted );
507             }
508             else
509             {
510                 return new ReflectionPermission( m_flag );
511             }
512         }
513     }
514
515 #if !FEATURE_PAL
516     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
517 [System.Runtime.InteropServices.ComVisible(true)]
518     [Serializable]
519 #pragma warning disable 618
520     sealed public class RegistryPermissionAttribute : CodeAccessSecurityAttribute
521 #pragma warning restore 618
522     {
523         private String m_read = null;
524         private String m_write = null;
525         private String m_create = null;
526         private String m_viewAcl = null;
527         private String m_changeAcl = null;
528
529 #pragma warning disable 618
530         public RegistryPermissionAttribute( SecurityAction action )
531 #pragma warning restore 618
532             : base( action )
533         {
534         }
535
536         public String Read {
537             get { return m_read; }
538             set { m_read = value; }
539         }
540     
541         public String Write {
542             get { return m_write; }
543             set { m_write = value; }
544         }
545
546         public String Create {
547             get { return m_create; }
548             set { m_create = value; }
549         }
550
551         public String ViewAccessControl {
552             get { return m_viewAcl; }
553             set { m_viewAcl = value; }
554         }
555
556         public String ChangeAccessControl {
557             get { return m_changeAcl; }
558             set { m_changeAcl = value; }
559         }
560
561         // Read, Write, & Create, but no ACL's
562         public String ViewAndModify {
563             get { throw new NotSupportedException( Environment.GetResourceString( "NotSupported_GetMethod" ) ); }
564             set { m_read = value; m_write = value; m_create = value; }
565         }
566
567         [Obsolete("Please use the ViewAndModify property instead.")]
568         public String All {
569             get { throw new NotSupportedException( Environment.GetResourceString( "NotSupported_GetMethod" ) ); }
570             set { m_read = value; m_write = value; m_create = value; }
571         }
572
573         public override IPermission CreatePermission()
574         {
575             if (m_unrestricted)
576             {
577                 return new RegistryPermission( PermissionState.Unrestricted );
578             }
579             else
580             {
581                 RegistryPermission perm = new RegistryPermission(PermissionState.None);
582                 if (m_read != null)
583                     perm.SetPathList( RegistryPermissionAccess.Read, m_read );
584                 if (m_write != null)
585                     perm.SetPathList( RegistryPermissionAccess.Write, m_write );
586                 if (m_create != null)
587                     perm.SetPathList( RegistryPermissionAccess.Create, m_create );
588 #if FEATURE_MACL
589                 if (m_viewAcl != null)
590                     perm.SetPathList( AccessControlActions.View, m_viewAcl );
591                 if (m_changeAcl != null)
592                     perm.SetPathList( AccessControlActions.Change, m_changeAcl );
593 #endif
594                 return perm;
595             }
596         }
597     }
598 #endif // !FEATURE_PAL
599     
600     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
601 [System.Runtime.InteropServices.ComVisible(true)]
602     [Serializable]
603 #if !FEATURE_CAS_POLICY
604     // The csharp compiler requires these types to be public, but they are not used elsewhere.
605     [Obsolete("SecurityPermissionAttribute is no longer accessible to application code.")]
606 #endif
607     sealed public class SecurityPermissionAttribute : CodeAccessSecurityAttribute
608     {
609         private SecurityPermissionFlag m_flag = SecurityPermissionFlag.NoFlags;
610     
611         public SecurityPermissionAttribute( SecurityAction action )
612             : base( action )
613         {
614         }
615
616         public SecurityPermissionFlag Flags {
617             get { return m_flag; }
618             set { m_flag = value; }
619         }
620
621         public bool Assertion {
622             get { return (m_flag & SecurityPermissionFlag.Assertion) != 0; }
623             set { m_flag = value ? m_flag | SecurityPermissionFlag.Assertion : m_flag & ~SecurityPermissionFlag.Assertion; }
624         }
625
626         public bool UnmanagedCode {
627             get { return (m_flag & SecurityPermissionFlag.UnmanagedCode) != 0; }
628             set { m_flag = value ? m_flag | SecurityPermissionFlag.UnmanagedCode : m_flag & ~SecurityPermissionFlag.UnmanagedCode; }
629         }
630
631         public bool SkipVerification {
632             get { return (m_flag & SecurityPermissionFlag.SkipVerification) != 0; }
633             set { m_flag = value ? m_flag | SecurityPermissionFlag.SkipVerification : m_flag & ~SecurityPermissionFlag.SkipVerification; }
634         }
635
636         public bool Execution {
637             get { return (m_flag & SecurityPermissionFlag.Execution) != 0; }
638             set { m_flag = value ? m_flag | SecurityPermissionFlag.Execution : m_flag & ~SecurityPermissionFlag.Execution; }
639         }
640
641         public bool ControlThread {
642             get { return (m_flag & SecurityPermissionFlag.ControlThread) != 0; }
643             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlThread : m_flag & ~SecurityPermissionFlag.ControlThread; }
644         }
645     
646         public bool ControlEvidence {
647             get { return (m_flag & SecurityPermissionFlag.ControlEvidence) != 0; }
648             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlEvidence : m_flag & ~SecurityPermissionFlag.ControlEvidence; }
649         }
650     
651         public bool ControlPolicy {
652             get { return (m_flag & SecurityPermissionFlag.ControlPolicy) != 0; }
653             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlPolicy : m_flag & ~SecurityPermissionFlag.ControlPolicy; }
654         }
655     
656         public bool SerializationFormatter {
657             get { return (m_flag & SecurityPermissionFlag.SerializationFormatter) != 0; }
658             set { m_flag = value ? m_flag | SecurityPermissionFlag.SerializationFormatter : m_flag & ~SecurityPermissionFlag.SerializationFormatter; }
659         }
660
661         public bool ControlDomainPolicy {
662             get { return (m_flag & SecurityPermissionFlag.ControlDomainPolicy) != 0; }
663             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlDomainPolicy : m_flag & ~SecurityPermissionFlag.ControlDomainPolicy; }
664         }
665     
666         public bool ControlPrincipal {
667             get { return (m_flag & SecurityPermissionFlag.ControlPrincipal) != 0; }
668             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlPrincipal : m_flag & ~SecurityPermissionFlag.ControlPrincipal; }
669         }
670
671         public bool ControlAppDomain {
672             get { return (m_flag & SecurityPermissionFlag.ControlAppDomain) != 0; }
673             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlAppDomain : m_flag & ~SecurityPermissionFlag.ControlAppDomain; }
674         } 
675
676         public bool RemotingConfiguration {
677             get { return (m_flag & SecurityPermissionFlag.RemotingConfiguration) != 0; }
678             set { m_flag = value ? m_flag | SecurityPermissionFlag.RemotingConfiguration : m_flag & ~SecurityPermissionFlag.RemotingConfiguration; }
679         }
680
681 [System.Runtime.InteropServices.ComVisible(true)]
682         public bool Infrastructure {
683             get { return (m_flag & SecurityPermissionFlag.Infrastructure) != 0; }
684             set { m_flag = value ? m_flag | SecurityPermissionFlag.Infrastructure : m_flag & ~SecurityPermissionFlag.Infrastructure; }
685         }
686     
687         public bool BindingRedirects {
688             get { return (m_flag & SecurityPermissionFlag.BindingRedirects) != 0; }
689             set { m_flag = value ? m_flag | SecurityPermissionFlag.BindingRedirects : m_flag & ~SecurityPermissionFlag.BindingRedirects; }
690         }
691
692         public override IPermission CreatePermission()
693         {
694             if (m_unrestricted)
695             {
696                 return new SecurityPermission( PermissionState.Unrestricted );
697             }
698             else
699             {
700                 return new SecurityPermission( m_flag );
701             }
702         }
703     }
704
705     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
706 [System.Runtime.InteropServices.ComVisible(true)]
707     [Serializable]
708 #pragma warning disable 618
709     sealed public class UIPermissionAttribute : CodeAccessSecurityAttribute
710 #pragma warning restore 618
711     {
712         private UIPermissionWindow m_windowFlag = UIPermissionWindow.NoWindows;
713         private UIPermissionClipboard m_clipboardFlag = UIPermissionClipboard.NoClipboard;
714     
715 #pragma warning disable 618
716         public UIPermissionAttribute( SecurityAction action )
717 #pragma warning restore 618
718             : base( action )
719         {
720         }
721
722         public UIPermissionWindow Window {
723             get { return m_windowFlag; }
724             set { m_windowFlag = value; }
725         }
726
727         public UIPermissionClipboard Clipboard {
728             get { return m_clipboardFlag; }
729             set { m_clipboardFlag = value; }
730         }
731     
732         public override IPermission CreatePermission()
733         {
734             if (m_unrestricted)
735             {
736                 return new UIPermission( PermissionState.Unrestricted );
737             }
738             else
739             {
740                 return new UIPermission( m_windowFlag, m_clipboardFlag );
741             }
742         }
743     }
744
745     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
746 [System.Runtime.InteropServices.ComVisible(true)]
747     [Serializable]
748 #pragma warning disable 618
749     sealed public class ZoneIdentityPermissionAttribute : CodeAccessSecurityAttribute
750 #pragma warning restore 618
751     {
752         private SecurityZone m_flag = SecurityZone.NoZone;
753     
754 #pragma warning disable 618
755         public ZoneIdentityPermissionAttribute( SecurityAction action )
756 #pragma warning restore 618
757             : base( action )
758         {
759         }
760
761         public SecurityZone Zone {
762             get { return m_flag; }
763             set { m_flag = value; }
764         }
765     
766         public override IPermission CreatePermission()
767         {
768             if (m_unrestricted)
769             {
770                 return new ZoneIdentityPermission(PermissionState.Unrestricted);
771             }
772             else
773             {
774                 return new ZoneIdentityPermission( m_flag );
775             }
776         }
777     }
778
779     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
780 [System.Runtime.InteropServices.ComVisible(true)]
781     [Serializable]
782 #pragma warning disable 618
783     sealed public class StrongNameIdentityPermissionAttribute : CodeAccessSecurityAttribute
784 #pragma warning restore 618
785     {
786         private String m_name = null;
787         private String m_version = null;
788         private String m_blob = null;
789
790 #pragma warning disable 618
791         public StrongNameIdentityPermissionAttribute( SecurityAction action )
792 #pragma warning restore 618
793             : base( action )
794         {
795         }
796
797         public String Name
798         {
799             get { return m_name; }
800             set { m_name = value; }
801         }
802         
803         public String Version
804         {
805             get { return m_version; }
806             set { m_version = value; }
807         }
808         
809         public String PublicKey
810         {
811             get { return m_blob; }
812             set { m_blob = value; }
813         }
814
815         public override IPermission CreatePermission()
816         {
817             if (m_unrestricted)
818             {
819                 return new StrongNameIdentityPermission( PermissionState.Unrestricted );
820             }
821             else
822             {
823                 if (m_blob == null && m_name == null && m_version == null)
824                     return new StrongNameIdentityPermission( PermissionState.None );
825             
826                 if (m_blob == null)
827                     throw new ArgumentException( Environment.GetResourceString("ArgumentNull_Key"));
828                     
829                 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob( m_blob );
830                 
831                 if (m_version == null || m_version.Equals(String.Empty))
832                     return new StrongNameIdentityPermission( blob, m_name, null );
833                 else    
834                     return new StrongNameIdentityPermission( blob, m_name, new Version( m_version ) );
835             }
836         }
837     }
838
839
840     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
841 [System.Runtime.InteropServices.ComVisible(true)]
842     [Serializable]
843 #pragma warning disable 618
844     sealed public class SiteIdentityPermissionAttribute : CodeAccessSecurityAttribute
845 #pragma warning restore 618
846     {
847         private String m_site = null;
848     
849 #pragma warning disable 618
850         public SiteIdentityPermissionAttribute( SecurityAction action )
851 #pragma warning restore 618
852             : base( action )
853         {
854         }
855
856         public String Site {
857             get { return m_site; }
858             set { m_site = value; }
859         }
860     
861         public override IPermission CreatePermission()
862         {
863             if (m_unrestricted)
864             {
865                 return new SiteIdentityPermission( PermissionState.Unrestricted );
866             }
867             else
868             {
869                 if (m_site == null)
870                     return new SiteIdentityPermission( PermissionState.None );
871             
872                 return new SiteIdentityPermission( m_site );
873             }
874         }
875     }
876
877     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
878 [System.Runtime.InteropServices.ComVisible(true)]
879 #pragma warning disable 618
880     [Serializable] sealed public class UrlIdentityPermissionAttribute : CodeAccessSecurityAttribute
881 #pragma warning restore 618
882     {
883         private String m_url = null;
884     
885 #pragma warning disable 618
886         public UrlIdentityPermissionAttribute( SecurityAction action )
887 #pragma warning restore 618
888             : base( action )
889         {
890         }
891
892         public String Url {
893             get { return m_url; }
894             set { m_url = value; }
895         }
896     
897         public override IPermission CreatePermission()
898         {
899             if (m_unrestricted)
900             {
901                 return new UrlIdentityPermission( PermissionState.Unrestricted );
902             }
903             else
904             {
905                 if (m_url == null)
906                     return new UrlIdentityPermission( PermissionState.None );
907                     
908                 return new UrlIdentityPermission( m_url );
909             }
910         }
911     }
912     
913 #if FEATURE_X509 && FEATURE_CAS_POLICY
914     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
915 [System.Runtime.InteropServices.ComVisible(true)]
916     [Serializable]
917     sealed public class PublisherIdentityPermissionAttribute : CodeAccessSecurityAttribute
918     {
919         private String m_x509cert = null;
920         private String m_certFile = null;
921         private String m_signedFile = null;
922     
923         public PublisherIdentityPermissionAttribute( SecurityAction action )
924             : base( action )
925         {
926             m_x509cert = null;
927             m_certFile = null;
928             m_signedFile = null;
929         }
930
931         public String X509Certificate {
932             get { return m_x509cert; }
933             set { m_x509cert = value; }
934         }
935         
936         public String CertFile {
937             get { return m_certFile; }
938             [ResourceExposure(ResourceScope.Machine)]
939             [ResourceConsumption(ResourceScope.Machine)]
940             set { m_certFile = value; }
941         }
942         
943         public String SignedFile {
944             get { return m_signedFile; }
945             [ResourceExposure(ResourceScope.Machine)]
946             [ResourceConsumption(ResourceScope.Machine)]
947             set { m_signedFile = value; }
948         }
949
950         [ResourceExposure(ResourceScope.Machine)]
951         [ResourceConsumption(ResourceScope.Machine)]    
952         public override IPermission CreatePermission()
953         {
954             if (m_unrestricted)
955             {
956                 return new PublisherIdentityPermission( PermissionState.Unrestricted );
957             }
958             else
959             {
960                 if (m_x509cert != null)
961                 {
962                     return new PublisherIdentityPermission( new X509Certificate( System.Security.Util.Hex.DecodeHexString( m_x509cert ) ) );
963                 }
964                 else if (m_certFile != null)
965                 {
966                     return new PublisherIdentityPermission( System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile( m_certFile ) );
967                 }
968                 else if (m_signedFile != null)
969                 {
970                     return new PublisherIdentityPermission( System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromSignedFile( m_signedFile ) );
971                 }
972                 else
973                 {
974                     return new PublisherIdentityPermission( PermissionState.None );
975                 }
976             }
977         }
978     }
979 #endif // #if FEATURE_X509 && FEATURE_CAS_POLICY
980
981 #if !FEATURE_CORECLR                              
982 [Serializable]
983 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor
984      | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly,
985     AllowMultiple=true, Inherited=false)]
986 [System.Runtime.InteropServices.ComVisible(true)]
987     public abstract class IsolatedStoragePermissionAttribute : CodeAccessSecurityAttribute
988     {
989         /// <internalonly/>
990         internal long m_userQuota;
991 #if false
992         /// <internalonly/>
993         internal long m_machineQuota;
994         /// <internalonly/>
995         internal long m_expirationDays;
996         /// <internalonly/>
997         internal bool m_permanentData;
998 #endif
999         /// <internalonly/>
1000         internal IsolatedStorageContainment m_allowed;
1001         protected IsolatedStoragePermissionAttribute(SecurityAction action) : base(action)
1002         {
1003         }
1004
1005         // properties
1006         public long UserQuota {
1007             set{
1008                 m_userQuota = value;
1009             }
1010             get{
1011                 return m_userQuota;
1012             }
1013         }
1014 #if false
1015         internal long MachineQuota {
1016             set{
1017                 m_machineQuota = value;
1018             }
1019             get{
1020                 return m_machineQuota;
1021             }
1022         }
1023         internal long ExpirationDays {
1024             set{
1025                 m_expirationDays = value;
1026             }
1027             get{
1028                 return m_expirationDays;
1029             }
1030         }
1031         internal bool PermanentData {
1032             set{
1033                 m_permanentData = value;
1034             }
1035             get{
1036                 return m_permanentData;
1037             }
1038         }
1039 #endif
1040         public IsolatedStorageContainment UsageAllowed {
1041             set{
1042                 m_allowed = value;
1043             }
1044             get{
1045                 return m_allowed;
1046             }
1047         }
1048
1049     }
1050
1051     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor
1052      | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly,
1053     AllowMultiple=true, Inherited=false)]
1054 [System.Runtime.InteropServices.ComVisible(true)]
1055     [Serializable]
1056     sealed public class IsolatedStorageFilePermissionAttribute : IsolatedStoragePermissionAttribute
1057     {
1058         public IsolatedStorageFilePermissionAttribute(SecurityAction action) : base(action)
1059         {
1060
1061         }
1062         public override IPermission CreatePermission()
1063         {
1064             IsolatedStorageFilePermission p;
1065             if (m_unrestricted) {
1066                 p = new IsolatedStorageFilePermission
1067                         (PermissionState.Unrestricted);
1068             } else {
1069                 p = new IsolatedStorageFilePermission(PermissionState.None);
1070                 p.UserQuota      = m_userQuota;
1071                 p.UsageAllowed   = m_allowed;
1072 #if false
1073                 p.PermanentData  = m_permanentData;
1074                 p.MachineQuota   = m_machineQuota;
1075                 p.ExpirationDays = m_expirationDays;
1076 #endif
1077             }
1078             return p;
1079         }
1080     }
1081 #endif // FEATURE_CORECLR
1082
1083     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
1084 [System.Runtime.InteropServices.ComVisible(true)]
1085     [Serializable]
1086 #pragma warning disable 618
1087     sealed public class PermissionSetAttribute : CodeAccessSecurityAttribute
1088 #pragma warning restore 618
1089     {
1090         private String m_file;
1091         private String m_name;
1092         private bool m_unicode;
1093         private String m_xml;
1094         private String m_hex;
1095
1096 #pragma warning disable 618
1097         public PermissionSetAttribute( SecurityAction action )
1098 #pragma warning restore 618
1099             : base( action )
1100         {
1101             m_unicode = false;
1102         }
1103
1104         public String File {
1105             get { return m_file; }
1106             [ResourceExposure(ResourceScope.Machine)]
1107             [ResourceConsumption(ResourceScope.Machine)]
1108             set { m_file = value; }
1109         }
1110     
1111         public bool UnicodeEncoded {
1112             get { return m_unicode; }
1113             set { m_unicode = value; }
1114         }
1115         
1116         public String Name {
1117             get { return m_name; }
1118             set { m_name = value; }
1119         }
1120         
1121         public String XML {
1122             get { return m_xml; }
1123             set { m_xml = value; }
1124         }       
1125
1126         public String Hex {
1127             get { return m_hex; }
1128             set { m_hex = value; }
1129         }
1130
1131         public override IPermission CreatePermission()
1132         {
1133             return null;
1134         }
1135
1136 #if FEATURE_CAS_POLICY
1137         private PermissionSet BruteForceParseStream(Stream stream)
1138         {
1139             Encoding[] encodings = new Encoding[] { Encoding.UTF8, 
1140 #if FEATURE_ASCII
1141                                                     Encoding.ASCII, 
1142 #endif                                              
1143                                                     Encoding.Unicode };
1144
1145             StreamReader reader = null;
1146             Exception exception = null;
1147
1148             for (int i = 0; reader == null && i < encodings.Length; ++i)
1149             {
1150                 try
1151                 {
1152                     stream.Position = 0;
1153                     reader = new StreamReader( stream, encodings[i] );
1154
1155                     return ParsePermissionSet( new Parser(reader) );
1156                 }
1157                 catch (Exception e1)
1158                 {
1159                     if (exception == null)
1160                         exception = e1;
1161                 }
1162             }
1163
1164             throw exception;
1165         }
1166
1167         private PermissionSet ParsePermissionSet(Parser parser)
1168         {
1169             SecurityElement e = parser.GetTopElement();
1170             PermissionSet permSet = new PermissionSet( PermissionState.None );
1171             permSet.FromXml( e );
1172
1173             return permSet;
1174         }
1175 #endif // FEATURE_CAS_POLICY
1176
1177 #if FEATURE_CAS_POLICY
1178         [System.Security.SecuritySafeCritical]  // auto-generated
1179 #endif
1180         [ResourceExposure(ResourceScope.Machine)]
1181         [ResourceConsumption(ResourceScope.Machine)]
1182         public PermissionSet CreatePermissionSet()
1183         {
1184             if (m_unrestricted)
1185                 return new PermissionSet( PermissionState.Unrestricted );
1186             else if (m_name != null)
1187 #if FEATURE_CAS_POLICY
1188                 return PolicyLevel.GetBuiltInSet( m_name );
1189 #else
1190                 return NamedPermissionSet.GetBuiltInSet( m_name );
1191 #endif // FEATURE_CAS_POLICY
1192 #if FEATURE_CAS_POLICY
1193             else if (m_xml != null)
1194                 return ParsePermissionSet( new Parser(m_xml.ToCharArray()) );
1195             else if (m_hex != null)
1196                 return BruteForceParseStream( new MemoryStream(Util.Hex.DecodeHexString(m_hex)) );
1197             else if (m_file != null)
1198                 return BruteForceParseStream( new FileStream( m_file, FileMode.Open, FileAccess.Read) );
1199 #endif // FEATURE_CAS_POLICY
1200             else
1201                 return new PermissionSet( PermissionState.None );
1202         }
1203     }
1204 }