Merge pull request #4222 from alexanderkyte/fix_mismatch_com_disabled
[mono.git] / mcs / class / corlib / System.Security.Permissions / PermissionSetAttribute.cs
1 //
2 // System.Security.Permissions.PermissionSetAttribute.cs
3 //
4 // Authors
5 //      Duncan Mak <duncan@ximian.com>
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
32 using System.Runtime.InteropServices;
33 using System.Security.Policy;
34 using System.Text;
35
36 using Mono.Security.Cryptography;
37 using Mono.Xml;
38
39 namespace System.Security.Permissions {
40
41         [ComVisible (true)]
42         [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
43                          AttributeTargets.Struct | AttributeTargets.Constructor |
44                          AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
45         [Serializable]
46         public sealed class PermissionSetAttribute : CodeAccessSecurityAttribute {
47
48                 // Fields
49                 private string file;
50                 private string name;
51                 private bool isUnicodeEncoded;
52                 private string xml;
53                 private string hex;
54                 
55                 // Constructor
56                 public PermissionSetAttribute (SecurityAction action)
57                         : base (action)
58                 {
59                 }
60                 
61                 // Properties
62                 public string File {
63                         get { return file; }
64                         set { file = value; }
65                 }
66                 public string Hex {
67                         get { return hex; }
68                         set { hex = value; }
69                 }
70                 public string Name {
71                         get { return name; }
72                         set { name = value; }
73                 }
74
75                 public bool UnicodeEncoded {
76                         get { return isUnicodeEncoded; }
77                         set { isUnicodeEncoded = value; }
78                 }
79
80                 public string XML {
81                         get { return xml; }
82                         set { xml = value; }
83                 }
84                 
85                 // Methods
86                 public override IPermission CreatePermission ()
87                 {
88                         return null;      // Not used, used for inheritance from SecurityAttribute
89                 }
90
91                 private PermissionSet CreateFromXml (string xml) 
92                 {
93 #if !MOBILE
94                         SecurityParser sp = new SecurityParser ();
95                         try {
96                                 sp.LoadXml (xml);
97                         }
98                         catch (Mono.Xml.SmallXmlParserException xe) {
99                                 throw new XmlSyntaxException (xe.Line, xe.ToString ());
100                         }
101                         SecurityElement se = sp.ToXml ();
102
103                         string className = se.Attribute ("class");
104                         if (className == null)
105                                 return null;
106
107                         PermissionState state = PermissionState.None;
108                         if (CodeAccessPermission.IsUnrestricted (se))
109                                 state = PermissionState.Unrestricted;
110
111                         if (className.EndsWith ("NamedPermissionSet")) {
112                                 NamedPermissionSet nps = new NamedPermissionSet (se.Attribute ("Name"), state);
113                                 nps.FromXml (se);
114                                 return (PermissionSet) nps;
115                         }
116                         else if (className.EndsWith ("PermissionSet")) {
117                                 PermissionSet ps = new PermissionSet (state);
118                                 ps.FromXml (se);
119                                 return ps;
120                         }
121 #endif
122                         return null;
123                 }
124
125                 public PermissionSet CreatePermissionSet ()
126                 {
127                         PermissionSet pset = null;
128 #if !MOBILE
129                         if (this.Unrestricted)
130                                 pset = new PermissionSet (PermissionState.Unrestricted);
131                         else {
132                                 pset = new PermissionSet (PermissionState.None);
133                                 if (name != null) {
134                                         return PolicyLevel.CreateAppDomainLevel ().GetNamedPermissionSet (name);
135                                 }
136                                 else if (file != null) {
137                                         Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
138                                         using (StreamReader sr = new StreamReader (file, e)) {
139                                                 pset = CreateFromXml (sr.ReadToEnd ());
140                                         }
141                                 }
142                                 else if (xml != null) {
143                                         pset = CreateFromXml (xml);
144                                 }
145                                 else if (hex != null) {
146                                         // Unicode isn't supported
147                                         //Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
148                                         Encoding e = System.Text.Encoding.ASCII;
149                                         byte[] bin = CryptoConvert.FromHex (hex);
150                                         pset = CreateFromXml (e.GetString (bin, 0, bin.Length));
151                                 }
152                         }
153 #endif
154                         return pset;
155                 }
156         }
157 }