2 * security-manager.c: Security Manager (Unmanaged side)
5 * Sebastien Pouliot <sebastien@ximian.com>
7 * Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 #include "security-manager.h"
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 static MonoSecurityMode mono_security_mode = MONO_SECURITY_MODE_NONE;
25 mono_security_set_mode (MonoSecurityMode mode)
27 mono_security_mode = mode;
31 mono_security_get_mode (void)
33 return mono_security_mode;
37 mono_security_manager_get_methods (void)
39 /* Already initialized ? */
40 if (secman.securitymanager)
44 secman.securitymanager = mono_class_from_name (mono_defaults.corlib,
45 "System.Security", "SecurityManager");
46 g_assert (secman.securitymanager);
47 if (!secman.securitymanager->inited)
48 mono_class_init (secman.securitymanager);
50 secman.demand = mono_class_get_method_from_name (secman.securitymanager,
52 g_assert (secman.demand);
54 secman.demandchoice = mono_class_get_method_from_name (secman.securitymanager,
55 "InternalDemandChoice", 2);
56 g_assert (secman.demandchoice);
58 secman.demandunmanaged = mono_class_get_method_from_name (secman.securitymanager,
59 "DemandUnmanaged", 0);
60 g_assert (secman.demandunmanaged);
62 secman.inheritancedemand = mono_class_get_method_from_name (secman.securitymanager,
63 "InheritanceDemand", 3);
64 g_assert (secman.inheritancedemand);
66 secman.inheritsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
67 "InheritanceDemandSecurityException", 4);
68 g_assert (secman.inheritsecurityexception);
70 secman.linkdemand = mono_class_get_method_from_name (secman.securitymanager,
72 g_assert (secman.linkdemand);
74 secman.linkdemandunmanaged = mono_class_get_method_from_name (secman.securitymanager,
75 "LinkDemandUnmanaged", 1);
76 g_assert (secman.linkdemandunmanaged);
78 secman.linkdemandfulltrust = mono_class_get_method_from_name (secman.securitymanager,
79 "LinkDemandFullTrust", 1);
80 g_assert (secman.linkdemandfulltrust);
82 secman.linkdemandsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
83 "LinkDemandSecurityException", 2);
84 g_assert (secman.linkdemandsecurityexception);
86 secman.allowpartiallytrustedcallers = mono_class_from_name (mono_defaults.corlib, "System.Security",
87 "AllowPartiallyTrustedCallersAttribute");
88 g_assert (secman.allowpartiallytrustedcallers);
90 secman.suppressunmanagedcodesecurity = mono_class_from_name (mono_defaults.corlib, "System.Security",
91 "SuppressUnmanagedCodeSecurityAttribute");
92 g_assert (secman.suppressunmanagedcodesecurity);
98 mono_secman_inheritance_check (MonoClass *klass, MonoDeclSecurityActions *demands)
100 MonoSecurityManager* secman = mono_security_manager_get_methods ();
101 MonoDomain *domain = mono_domain_get ();
102 MonoAssembly *assembly = mono_image_get_assembly (klass->image);
103 MonoReflectionAssembly *refass = mono_assembly_get_object (domain, assembly);
107 args [0] = domain->domain;
111 res = mono_runtime_invoke (secman->inheritancedemand, NULL, args, NULL);
112 return (*(MonoBoolean *) mono_object_unbox (res));
116 mono_secman_inheritancedemand_class (MonoClass *klass, MonoClass *parent)
118 MonoDeclSecurityActions demands;
120 /* don't hide previous results -and- don't calc everything for nothing */
121 if (klass->exception_type != 0)
124 /* short-circuit corlib as it is fully trusted (within itself)
125 * and because this cause major recursion headaches */
126 if ((klass->image == mono_defaults.corlib) && (parent->image == mono_defaults.corlib))
129 /* Check if there are an InheritanceDemand on the parent class */
130 if (mono_declsec_get_inheritdemands_class (parent, &demands)) {
131 /* If so check the demands on the klass (inheritor) */
132 if (!mono_secman_inheritance_check (klass, &demands)) {
133 /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
134 klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
135 klass->exception_data = NULL;
141 mono_secman_inheritancedemand_method (MonoMethod *override, MonoMethod *base)
143 MonoDeclSecurityActions demands;
145 /* don't hide previous results -and- don't calc everything for nothing */
146 if (override->klass->exception_type != 0)
149 /* short-circuit corlib as it is fully trusted (within itself)
150 * and because this cause major recursion headaches */
151 if ((override->klass->image == mono_defaults.corlib) && (base->klass->image == mono_defaults.corlib))
154 /* Check if there are an InheritanceDemand on the base (virtual) method */
155 if (mono_declsec_get_inheritdemands_method (base, &demands)) {
156 /* If so check the demands on the overriding method */
157 if (!mono_secman_inheritance_check (override->klass, &demands)) {
158 /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
159 override->klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
160 override->klass->exception_data = base;
167 * Note: The security manager is activate once when executing the Mono. This
168 * is not meant to be a turn on/off runtime switch.
171 mono_activate_security_manager (void)
173 mono_security_manager_activated = TRUE;
177 mono_is_security_manager_active (void)
179 return mono_security_manager_activated;
183 * @publickey An encoded (with header) public key
184 * @size The length of the public key
186 * returns TRUE if the public key is the ECMA "key", FALSE otherwise
188 * ECMA key isn't a real public key - it's simply an empty (but valid) header
189 * so it's length (16) and value (00000000000000000400000000000000) are
193 mono_is_ecma_key (const char *publickey, int size)
196 if ((publickey == NULL) || (size != MONO_ECMA_KEY_LENGTH) || (publickey [8] != 0x04))
199 for (i=0; i < size; i++) {
200 if ((publickey [i] != 0x00) && (i != 8))
207 * Context propagation is required when:
208 * (a) the security manager is active (1.x and later)
209 * (b) other contexts needs to be propagated (2.x and later)
211 * returns NULL if no context propagation is required, else the returns the
212 * MonoMethod to call to Capture the ExecutionContext.
215 mono_get_context_capture_method (void)
217 static MonoMethod *method = NULL;
219 if (!mono_security_manager_activated) {
220 if (mono_image_get_assembly (mono_defaults.corlib)->aname.major < 2)
224 /* older corlib revisions won't have the class (nor the method) */
225 if (mono_defaults.executioncontext_class && !method) {
226 mono_class_init (mono_defaults.executioncontext_class);
227 method = mono_class_get_method_from_name (mono_defaults.executioncontext_class, "Capture", 0);
234 /* System.Security icalls */
237 ves_icall_System_Security_SecurityManager_get_SecurityEnabled (void)
239 if (!mono_security_manager_activated)
241 return mono_security_manager_enabled;
245 ves_icall_System_Security_SecurityManager_set_SecurityEnabled (MonoBoolean value)
247 /* value can be changed only if the security manager is activated */
248 if (mono_security_manager_activated) {
249 mono_security_manager_enabled = value;
254 ves_icall_System_Security_SecurityManager_get_CheckExecutionRights (void)
256 if (!mono_security_manager_activated)
258 return mono_security_manager_execution;
262 ves_icall_System_Security_SecurityManager_set_CheckExecutionRights (MonoBoolean value)
264 /* value can be changed only id the security manager is activated */
265 if (mono_security_manager_activated) {
266 mono_security_manager_execution = value;
271 ves_icall_System_Security_SecurityManager_GetLinkDemandSecurity (MonoReflectionMethod *m, MonoDeclSecurityActions *kactions, MonoDeclSecurityActions *mactions)
273 MonoMethod *method = m->method;
274 /* we want the original as the wrapper is "free" of the security informations */
275 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
276 method = mono_marshal_method_from_wrapper (method);
279 mono_class_init (method->klass);
281 /* if either the method or it's class has security (any type) */
282 if ((method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) || (method->klass->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
283 memset (kactions, 0, sizeof (MonoDeclSecurityActions));
284 memset (mactions, 0, sizeof (MonoDeclSecurityActions));
286 /* get any linkdemand (either on the method or it's class) */
287 return mono_declsec_get_linkdemands (method, kactions, mactions);