2005-03-10 Sebastien Pouliot <sebastien@ximian.com>
[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.inheritancedemand = mono_class_get_method_from_name (secman.securitymanager,
46                 "InheritanceDemand", 2);        
47         g_assert (secman.inheritancedemand);
48
49         secman.inheritsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
50                 "InheritanceDemandSecurityException", 4);       
51         g_assert (secman.inheritsecurityexception);
52
53         secman.linkdemand = mono_class_get_method_from_name (secman.securitymanager,
54                 "LinkDemand", 3);
55         g_assert (secman.linkdemand);
56
57         secman.linkdemandunmanaged = mono_class_get_method_from_name (secman.securitymanager,
58                 "LinkDemandUnmanaged", 1);
59         g_assert (secman.linkdemandunmanaged);
60
61         secman.linkdemandfulltrust = mono_class_get_method_from_name (secman.securitymanager,
62                 "LinkDemandFullTrust", 1);
63         g_assert (secman.linkdemandfulltrust);
64
65         secman.linkdemandsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
66                 "LinkDemandSecurityException", 3);
67         g_assert (secman.linkdemandsecurityexception);
68
69         secman.aptc = mono_class_from_name (mono_defaults.corlib, "System.Security", 
70                 "AllowPartiallyTrustedCallersAttribute");
71         g_assert (secman.aptc);
72
73         return &secman;
74 }
75
76 static gboolean
77 mono_secman_inheritance_check (MonoClass *klass, MonoDeclSecurityActions *demands)
78 {
79         MonoSecurityManager* secman = mono_security_manager_get_methods ();
80         MonoDomain *domain = mono_domain_get ();
81         MonoAssembly *assembly = mono_image_get_assembly (klass->image);
82         MonoReflectionAssembly *refass = mono_assembly_get_object (domain, assembly);
83         MonoObject *res;
84         gpointer args [2];
85
86         args [0] = refass;
87         args [1] = demands;
88
89         res = mono_runtime_invoke (secman->inheritancedemand, NULL, args, NULL);
90         return (*(MonoBoolean *) mono_object_unbox (res));
91 }
92
93 void
94 mono_secman_inheritancedemand_class (MonoClass *klass, MonoClass *parent)
95 {
96         MonoDeclSecurityActions demands;
97
98         /* don't hide previous results -and- don't calc everything for nothing */
99         if (klass->exception_type != 0)
100                 return;
101
102         /* Check if there are an InheritanceDemand on the parent class */
103         if (mono_declsec_get_inheritdemands_class (parent, &demands)) {
104                 /* If so check the demands on the klass (inheritor) */
105                 if (!mono_secman_inheritance_check (klass, &demands)) {
106                         /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
107                         klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
108                         klass->exception_data = NULL;
109                 }
110         }
111 }
112
113 void
114 mono_secman_inheritancedemand_method (MonoMethod *override, MonoMethod *base)
115 {
116         MonoDeclSecurityActions demands;
117
118         /* don't hide previous results -and- don't calc everything for nothing */
119         if (override->klass->exception_type != 0)
120                 return;
121
122         /* Check if there are an InheritanceDemand on the base (virtual) method */
123         if (mono_declsec_get_inheritdemands_method (base, &demands)) {
124                 /* If so check the demands on the overriding method */
125                 if (!mono_secman_inheritance_check (override->klass, &demands)) {
126                         /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
127                         override->klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
128                         override->klass->exception_data = base;
129                 }
130         }
131 }
132
133
134 /*
135  * Note: The security manager is activate once when executing the Mono. This 
136  * is not meant to be a turn on/off runtime switch.
137  */
138 void
139 mono_activate_security_manager (void)
140 {
141         mono_security_manager_activated = TRUE;
142 }
143
144 gboolean
145 mono_is_security_manager_active (void)
146 {
147         return mono_security_manager_activated;
148 }
149
150 /*
151  * @publickey   An encoded (with header) public key
152  * @size        The length of the public key
153  *
154  * returns TRUE if the public key is the ECMA "key", FALSE otherwise
155  *
156  * ECMA key isn't a real public key - it's simply an empty (but valid) header
157  * so it's length (16) and value (00000000000000000400000000000000) are 
158  * constants.
159  */
160 gboolean 
161 mono_is_ecma_key (const char *publickey, int size)
162 {
163         int i;
164         if ((publickey == NULL) || (size != MONO_ECMA_KEY_LENGTH) || (publickey [8] != 0x04))
165                 return FALSE;
166
167         for (i=0; i < size; i++) {
168                 if ((publickey [i] != 0x00) && (i != 8))
169                         return FALSE;
170         }
171         return TRUE;
172 }
173
174 /* System.Security icalls */
175
176 MonoBoolean
177 ves_icall_System_Security_SecurityManager_get_SecurityEnabled (void)
178 {
179         if (!mono_security_manager_activated)
180                 return FALSE;
181         return mono_security_manager_enabled;
182 }
183
184 void
185 ves_icall_System_Security_SecurityManager_set_SecurityEnabled (MonoBoolean value)
186 {
187         /* value can be changed only if the security manager is activated */
188         if (mono_security_manager_activated) {
189                 mono_security_manager_enabled = value;
190         }
191 }
192
193 MonoBoolean
194 ves_icall_System_Security_SecurityManager_get_CheckExecutionRights (void)
195 {
196         if (!mono_security_manager_activated)
197                 return FALSE;
198         return mono_security_manager_execution;
199 }
200
201 void
202 ves_icall_System_Security_SecurityManager_set_CheckExecutionRights (MonoBoolean value)
203 {
204         /* value can be changed only id the security manager is activated */
205         if (mono_security_manager_activated) {
206                 mono_security_manager_execution = value;
207         }
208 }
209
210 MonoBoolean
211 ves_icall_System_Security_SecurityManager_GetLinkDemandSecurity (MonoReflectionMethod *m, MonoDeclSecurityActions *kactions, MonoDeclSecurityActions *mactions)
212 {
213         MonoMethod *method = m->method;
214         /* we want the original as the wrapper is "free" of the security informations */
215         if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
216                 method = mono_marshal_method_from_wrapper (method);
217         }
218
219         mono_class_init (method->klass);
220
221         /* if either the method or it's class has security (any type) */
222         if ((method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) || (method->klass->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
223                 memset (kactions, 0, sizeof (MonoDeclSecurityActions));
224                 memset (mactions, 0, sizeof (MonoDeclSecurityActions));
225
226                 /* get any linkdemand (either on the method or it's class) */
227                 return mono_declsec_get_linkdemands (method, kactions, mactions);
228         }
229         return FALSE;
230 }