minor fix for bug 9520:
[mono.git] / mcs / class / corlib / System.Security / SecurityException.cs
1 //
2 // System.Security.SecurityException.cs
3 //
4 // Authors:
5 //      Nick Drochak(ndrochak@gol.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) Nick Drochak
9 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Globalization;
33 using System.Reflection;
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Security.Permissions;
37 using System.Security.Policy;
38 using System.Text;
39
40 namespace System.Security {
41
42         [Serializable]
43         [ComVisible (true)]
44         public class SecurityException : SystemException {
45                 // Fields
46                 string permissionState;
47                 Type permissionType;
48                 private string _granted;
49                 private string _refused;
50                 private object _demanded;
51                 private IPermission _firstperm;
52 //              private IPermission _permfailed;
53                 private MethodInfo _method;
54 #if !MOBILE
55                 private Evidence _evidence;
56 #endif
57                 private SecurityAction _action;
58                 private object _denyset;
59                 private object _permitset;
60                 private AssemblyName _assembly;
61                 private string _url;
62                 private SecurityZone _zone;
63                 
64                 // Properties
65
66                 [ComVisible (false)]
67                 public SecurityAction Action {
68                         get { return _action; }
69                         set { _action = value; }
70                 }
71
72                 [ComVisible (false)]
73                 public object DenySetInstance {
74                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
75                         get { return _denyset; }
76                         set { _denyset = value; }
77                 }
78
79                 [ComVisible (false)]
80                 public AssemblyName FailedAssemblyInfo {
81                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
82                         get { return _assembly; }
83                         set { _assembly = value; }
84                 }
85
86                 [ComVisible (false)]
87                 public MethodInfo Method {
88                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
89                         get { return _method; }
90                         set { _method = value; }
91                 }
92
93                 [ComVisible (false)]
94                 public object PermitOnlySetInstance {
95                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
96                         get { return _permitset; }
97                         set { _permitset = value; }
98                 }
99
100                 public string Url {
101                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
102                         get { return _url; }
103                         set { _url = value; }
104                 }
105
106                 public SecurityZone Zone {
107                         get { return _zone; }
108                         set { _zone = value; }
109                 }
110
111                 [ComVisible (false)]
112                 public object Demanded {
113                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
114                         get { return _demanded; }
115                         set { _demanded = value; }
116                 }
117
118                 public IPermission FirstPermissionThatFailed {
119                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
120                         get { return _firstperm; }
121                         set { _firstperm = value; }
122                 }
123
124                 public string PermissionState {
125                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
126                         get { return permissionState; }
127                         set { permissionState = value; }
128                 }
129
130                 public Type PermissionType {
131                         get { return permissionType; }
132                         set { permissionType = value; }
133                 }
134
135                 public string GrantedSet {
136                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
137                         get { return _granted; }
138                         set { _granted = value; }
139                 }
140
141                 public string RefusedSet {
142                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
143                         get { return _refused; }
144                         set { _refused = value; }
145                 }
146
147                 // Constructors
148
149                 public SecurityException ()
150                         : base (Locale.GetText ("A security error has been detected."))
151                 {
152                         base.HResult = unchecked ((int)0x8013150A);
153                 }
154
155                 public SecurityException (string message) 
156                         : base (message)
157                 {
158                         base.HResult = unchecked ((int)0x8013150A);
159                 }
160                 
161                 protected SecurityException (SerializationInfo info, StreamingContext context) 
162                         : base (info, context)
163                 {
164                         base.HResult = unchecked ((int)0x8013150A);
165                         // depending on the security policy the info about PermissionState may
166                         // not be available (but the serializable must work)
167                         SerializationInfoEnumerator e = info.GetEnumerator ();
168                         while (e.MoveNext ()) {
169                                 if (e.Name == "PermissionState") {
170                                         permissionState = (string) e.Value;
171                                         break;
172                                 }
173                         }
174                 }
175                 
176                 public SecurityException (string message, Exception inner) 
177                         : base (message, inner)
178                 {
179                         base.HResult = unchecked ((int)0x8013150A);
180                 }
181                 
182                 public SecurityException (string message, Type type) 
183                         :  base (message) 
184                 {
185                         base.HResult = unchecked ((int)0x8013150A);
186                         permissionType = type;
187                 }
188                 
189                 public SecurityException (string message, Type type, string state) 
190                         : base (message) 
191                 {
192                         base.HResult = unchecked ((int)0x8013150A);
193                         permissionType = type;
194                         permissionState = state;
195                 }
196
197                 internal SecurityException (string message, PermissionSet granted, PermissionSet refused) 
198                         : base (message)
199                 {
200                         base.HResult = unchecked ((int)0x8013150A);
201                         _granted = granted.ToString ();
202                         _refused = refused.ToString ();
203                 }
204
205                 public SecurityException (string message, object deny, object permitOnly, MethodInfo method, 
206                         object demanded, IPermission permThatFailed)
207                         : base (message)
208                 {
209                         base.HResult = unchecked ((int)0x8013150A);
210                         _denyset = deny;
211                         _permitset = permitOnly;
212                         _method = method;
213                         _demanded = demanded;
214                         _firstperm = permThatFailed;
215                 }
216 #if !MOBILE
217                 public SecurityException (string message, AssemblyName assemblyName, PermissionSet grant, 
218                         PermissionSet refused, MethodInfo method, SecurityAction action, object demanded, 
219                         IPermission permThatFailed, Evidence evidence)
220                         : base (message)
221                 {
222                         base.HResult = unchecked ((int)0x8013150A);
223                         _assembly = assemblyName;
224                         _granted = (grant == null) ? String.Empty : grant.ToString ();
225                         _refused = (refused == null) ? String.Empty : refused.ToString ();
226                         _method = method;
227                         _action = action;
228                         _demanded = demanded;
229                         _firstperm = permThatFailed;
230                         if (_firstperm != null)
231                                 permissionType = _firstperm.GetType ();
232                         _evidence = evidence;
233                 }
234 #endif
235                 // Methods
236                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
237                 {
238                         base.GetObjectData (info, context);
239                         try {
240                                 info.AddValue ("PermissionState", PermissionState);
241                         }
242                         catch (SecurityException) {
243                                 // serialize only if permitted to do so
244                         }
245                 }
246
247                 public override string ToString ()
248                 {
249 #if MOBILE
250                         return base.ToString ();
251 #else
252                         StringBuilder sb = new StringBuilder (base.ToString ());
253                         try {
254                                 if (permissionType != null) {
255                                         sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, PermissionType);
256                                 }
257                                 if (_method != null) {
258                                         // method string representation doesn't include the type
259                                         string m = _method.ToString ();
260                                         int ret = m.IndexOf (" ") + 1;
261                                         sb.AppendFormat ("{0}Method: {1} {2}.{3}", Environment.NewLine, 
262                                                 _method.ReturnType.Name, _method.ReflectedType, m.Substring (ret));
263                                 }
264                                 if (permissionState != null) {
265                                         sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
266                                 }
267                                 if ((_granted != null) && (_granted.Length > 0)) {
268                                         sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, GrantedSet);
269                                 }
270                                 if ((_refused != null) && (_refused.Length > 0)) {
271                                         sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, RefusedSet);
272                                 }
273                                 if (_demanded != null) {
274                                         sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, Demanded);
275                                 }
276                                 if (_firstperm != null) {
277                                         sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, FirstPermissionThatFailed);
278                                 }
279                                 if (_evidence != null) {
280                                         sb.AppendFormat ("{0}Evidences:", Environment.NewLine);
281                                         foreach (object o in _evidence) {
282                                                 // Hash evidence is way much too verbose to be useful to anyone
283                                                 if (!(o is Hash))
284                                                         sb.AppendFormat ("{0}\t{1}", Environment.NewLine, o);
285                                         }
286                                 }
287                         }
288                         catch (SecurityException) {
289                                 // some informations can't be displayed
290                         }
291                         return sb.ToString ();
292 #endif
293                 }
294         }
295 }