updating to the latest module.
[mono.git] / mono / metadata / security-manager.c
1 /*
2  * security-manager.c:  Security Manager (Unmanaged side)
3  *
4  * Author:
5  *      Sebastien Pouliot  <sebastien@ximian.com>
6  *
7  * Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8  */
9
10 #include "security-manager.h"
11
12
13 /* Internal stuff */
14
15 static MonoSecurityManager secman;
16 static MonoBoolean mono_security_manager_activated = FALSE;
17 static MonoBoolean mono_security_manager_enabled = TRUE;
18 static MonoBoolean mono_security_manager_execution = TRUE;
19
20
21 /* Public stuff */
22
23 MonoSecurityManager*
24 mono_security_manager_get_methods (void)
25 {
26         /* Already initialized ? */
27         if (secman.securitymanager)
28                 return &secman;
29
30         /* Initialize */
31         secman.securitymanager = mono_class_from_name (mono_defaults.corlib, 
32                 "System.Security", "SecurityManager");
33         g_assert (secman.securitymanager);
34         if (!secman.securitymanager->inited)
35                 mono_class_init (secman.securitymanager);
36                 
37         secman.demand = mono_class_get_method_from_name (secman.securitymanager,
38                 "InternalDemand", 2);   
39         g_assert (secman.demand);
40
41         secman.demandchoice = mono_class_get_method_from_name (secman.securitymanager,
42                 "InternalDemandChoice", 2);     
43         g_assert (secman.demandchoice);
44
45         secman.demandunmanaged = mono_class_get_method_from_name (secman.securitymanager,
46                 "DemandUnmanaged", 0);
47         g_assert (secman.demandunmanaged);
48
49         secman.inheritancedemand = mono_class_get_method_from_name (secman.securitymanager,
50                 "InheritanceDemand", 2);        
51         g_assert (secman.inheritancedemand);
52
53         secman.inheritsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
54                 "InheritanceDemandSecurityException", 4);       
55         g_assert (secman.inheritsecurityexception);
56
57         secman.linkdemand = mono_class_get_method_from_name (secman.securitymanager,
58                 "LinkDemand", 3);
59         g_assert (secman.linkdemand);
60
61         secman.linkdemandunmanaged = mono_class_get_method_from_name (secman.securitymanager,
62                 "LinkDemandUnmanaged", 1);
63         g_assert (secman.linkdemandunmanaged);
64
65         secman.linkdemandfulltrust = mono_class_get_method_from_name (secman.securitymanager,
66                 "LinkDemandFullTrust", 1);
67         g_assert (secman.linkdemandfulltrust);
68
69         secman.linkdemandsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
70                 "LinkDemandSecurityException", 3);
71         g_assert (secman.linkdemandsecurityexception);
72
73         secman.allowpartiallytrustedcallers = mono_class_from_name (mono_defaults.corlib, "System.Security", 
74                 "AllowPartiallyTrustedCallersAttribute");
75         g_assert (secman.allowpartiallytrustedcallers);
76
77         secman.suppressunmanagedcodesecurity = mono_class_from_name (mono_defaults.corlib, "System.Security", 
78                 "SuppressUnmanagedCodeSecurityAttribute");
79         g_assert (secman.suppressunmanagedcodesecurity);
80
81         return &secman;
82 }
83
84 static gboolean
85 mono_secman_inheritance_check (MonoClass *klass, MonoDeclSecurityActions *demands)
86 {
87         MonoSecurityManager* secman = mono_security_manager_get_methods ();
88         MonoDomain *domain = mono_domain_get ();
89         MonoAssembly *assembly = mono_image_get_assembly (klass->image);
90         MonoReflectionAssembly *refass = mono_assembly_get_object (domain, assembly);
91         MonoObject *res;
92         gpointer args [2];
93
94         args [0] = refass;
95         args [1] = demands;
96
97         res = mono_runtime_invoke (secman->inheritancedemand, NULL, args, NULL);
98         return (*(MonoBoolean *) mono_object_unbox (res));
99 }
100
101 void
102 mono_secman_inheritancedemand_class (MonoClass *klass, MonoClass *parent)
103 {
104         MonoDeclSecurityActions demands;
105
106         /* don't hide previous results -and- don't calc everything for nothing */
107         if (klass->exception_type != 0)
108                 return;
109
110         /* short-circuit corlib as it is fully trusted (within itself)
111          * and because this cause major recursion headaches */
112         if ((klass->image == mono_defaults.corlib) && (parent->image == mono_defaults.corlib))
113                 return;
114
115         /* Check if there are an InheritanceDemand on the parent class */
116         if (mono_declsec_get_inheritdemands_class (parent, &demands)) {
117                 /* If so check the demands on the klass (inheritor) */
118                 if (!mono_secman_inheritance_check (klass, &demands)) {
119                         /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
120                         klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
121                         klass->exception_data = NULL;
122                 }
123         }
124 }
125
126 void
127 mono_secman_inheritancedemand_method (MonoMethod *override, MonoMethod *base)
128 {
129         MonoDeclSecurityActions demands;
130
131         /* don't hide previous results -and- don't calc everything for nothing */
132         if (override->klass->exception_type != 0)
133                 return;
134
135         /* short-circuit corlib as it is fully trusted (within itself)
136          * and because this cause major recursion headaches */
137         if ((override->klass->image == mono_defaults.corlib) && (base->klass->image == mono_defaults.corlib))
138                 return;
139
140         /* Check if there are an InheritanceDemand on the base (virtual) method */
141         if (mono_declsec_get_inheritdemands_method (base, &demands)) {
142                 /* If so check the demands on the overriding method */
143                 if (!mono_secman_inheritance_check (override->klass, &demands)) {
144                         /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
145                         override->klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
146                         override->klass->exception_data = base;
147                 }
148         }
149 }
150
151
152 /*
153  * Note: The security manager is activate once when executing the Mono. This 
154  * is not meant to be a turn on/off runtime switch.
155  */
156 void
157 mono_activate_security_manager (void)
158 {
159         mono_security_manager_activated = TRUE;
160 }
161
162 gboolean
163 mono_is_security_manager_active (void)
164 {
165         return mono_security_manager_activated;
166 }
167
168 /*
169  * @publickey   An encoded (with header) public key
170  * @size        The length of the public key
171  *
172  * returns TRUE if the public key is the ECMA "key", FALSE otherwise
173  *
174  * ECMA key isn't a real public key - it's simply an empty (but valid) header
175  * so it's length (16) and value (00000000000000000400000000000000) are 
176  * constants.
177  */
178 gboolean 
179 mono_is_ecma_key (const char *publickey, int size)
180 {
181         int i;
182         if ((publickey == NULL) || (size != MONO_ECMA_KEY_LENGTH) || (publickey [8] != 0x04))
183                 return FALSE;
184
185         for (i=0; i < size; i++) {
186                 if ((publickey [i] != 0x00) && (i != 8))
187                         return FALSE;
188         }
189         return TRUE;
190 }
191
192 /* System.Security icalls */
193
194 MonoBoolean
195 ves_icall_System_Security_SecurityManager_get_SecurityEnabled (void)
196 {
197         if (!mono_security_manager_activated)
198                 return FALSE;
199         return mono_security_manager_enabled;
200 }
201
202 void
203 ves_icall_System_Security_SecurityManager_set_SecurityEnabled (MonoBoolean value)
204 {
205         /* value can be changed only if the security manager is activated */
206         if (mono_security_manager_activated) {
207                 mono_security_manager_enabled = value;
208         }
209 }
210
211 MonoBoolean
212 ves_icall_System_Security_SecurityManager_get_CheckExecutionRights (void)
213 {
214         if (!mono_security_manager_activated)
215                 return FALSE;
216         return mono_security_manager_execution;
217 }
218
219 void
220 ves_icall_System_Security_SecurityManager_set_CheckExecutionRights (MonoBoolean value)
221 {
222         /* value can be changed only id the security manager is activated */
223         if (mono_security_manager_activated) {
224                 mono_security_manager_execution = value;
225         }
226 }
227
228 MonoBoolean
229 ves_icall_System_Security_SecurityManager_GetLinkDemandSecurity (MonoReflectionMethod *m, MonoDeclSecurityActions *kactions, MonoDeclSecurityActions *mactions)
230 {
231         MonoMethod *method = m->method;
232         /* we want the original as the wrapper is "free" of the security informations */
233         if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
234                 method = mono_marshal_method_from_wrapper (method);
235         }
236
237         mono_class_init (method->klass);
238
239         /* if either the method or it's class has security (any type) */
240         if ((method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) || (method->klass->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
241                 memset (kactions, 0, sizeof (MonoDeclSecurityActions));
242                 memset (mactions, 0, sizeof (MonoDeclSecurityActions));
243
244                 /* get any linkdemand (either on the method or it's class) */
245                 return mono_declsec_get_linkdemands (method, kactions, mactions);
246         }
247         return FALSE;
248 }