2 * security-manager.c: Security Manager (Unmanaged side)
5 * Sebastien Pouliot <sebastien@ximian.com>
7 * Copyright 2005-2009 Novell, Inc (http://www.novell.com)
10 #include "security-manager.h"
12 static MonoSecurityMode mono_security_mode = MONO_SECURITY_MODE_NONE;
13 static MonoBoolean mono_security_manager_activated = FALSE;
14 static MonoBoolean mono_security_manager_enabled = TRUE;
15 static MonoBoolean mono_security_manager_execution = TRUE;
18 mono_security_set_mode (MonoSecurityMode mode)
20 mono_security_mode = mode;
24 mono_security_get_mode (void)
26 return mono_security_mode;
30 * Note: The security manager is activate once when executing the Mono. This
31 * is not meant to be a turn on/off runtime switch.
34 mono_activate_security_manager (void)
36 mono_security_manager_activated = TRUE;
40 mono_is_security_manager_active (void)
42 return mono_security_manager_activated;
45 #ifndef DISABLE_SECURITY
47 static MonoSecurityManager secman;
50 mono_security_manager_get_methods (void)
52 /* Already initialized ? */
53 if (secman.securitymanager)
57 secman.securitymanager = mono_class_from_name (mono_defaults.corlib,
58 "System.Security", "SecurityManager");
59 g_assert (secman.securitymanager);
60 if (!secman.securitymanager->inited)
61 mono_class_init (secman.securitymanager);
63 secman.demand = mono_class_get_method_from_name (secman.securitymanager,
65 g_assert (secman.demand);
67 secman.demandchoice = mono_class_get_method_from_name (secman.securitymanager,
68 "InternalDemandChoice", 2);
69 g_assert (secman.demandchoice);
71 secman.demandunmanaged = mono_class_get_method_from_name (secman.securitymanager,
72 "DemandUnmanaged", 0);
73 g_assert (secman.demandunmanaged);
75 secman.inheritancedemand = mono_class_get_method_from_name (secman.securitymanager,
76 "InheritanceDemand", 3);
77 g_assert (secman.inheritancedemand);
79 secman.inheritsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
80 "InheritanceDemandSecurityException", 4);
81 g_assert (secman.inheritsecurityexception);
83 secman.linkdemand = mono_class_get_method_from_name (secman.securitymanager,
85 g_assert (secman.linkdemand);
87 secman.linkdemandunmanaged = mono_class_get_method_from_name (secman.securitymanager,
88 "LinkDemandUnmanaged", 1);
89 g_assert (secman.linkdemandunmanaged);
91 secman.linkdemandfulltrust = mono_class_get_method_from_name (secman.securitymanager,
92 "LinkDemandFullTrust", 1);
93 g_assert (secman.linkdemandfulltrust);
95 secman.linkdemandsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
96 "LinkDemandSecurityException", 2);
97 g_assert (secman.linkdemandsecurityexception);
99 secman.allowpartiallytrustedcallers = mono_class_from_name (mono_defaults.corlib, "System.Security",
100 "AllowPartiallyTrustedCallersAttribute");
101 g_assert (secman.allowpartiallytrustedcallers);
103 secman.suppressunmanagedcodesecurity = mono_class_from_name (mono_defaults.corlib, "System.Security",
104 "SuppressUnmanagedCodeSecurityAttribute");
105 g_assert (secman.suppressunmanagedcodesecurity);
111 mono_secman_inheritance_check (MonoClass *klass, MonoDeclSecurityActions *demands)
113 MonoSecurityManager* secman = mono_security_manager_get_methods ();
114 MonoDomain *domain = mono_domain_get ();
115 MonoAssembly *assembly = mono_image_get_assembly (klass->image);
116 MonoReflectionAssembly *refass = mono_assembly_get_object (domain, assembly);
120 args [0] = domain->domain;
124 res = mono_runtime_invoke (secman->inheritancedemand, NULL, args, NULL);
125 return (*(MonoBoolean *) mono_object_unbox (res));
129 mono_secman_inheritancedemand_class (MonoClass *klass, MonoClass *parent)
131 MonoDeclSecurityActions demands;
133 /* don't hide previous results -and- don't calc everything for nothing */
134 if (klass->exception_type != 0)
137 /* short-circuit corlib as it is fully trusted (within itself)
138 * and because this cause major recursion headaches */
139 if ((klass->image == mono_defaults.corlib) && (parent->image == mono_defaults.corlib))
142 /* Check if there are an InheritanceDemand on the parent class */
143 if (mono_declsec_get_inheritdemands_class (parent, &demands)) {
144 /* If so check the demands on the klass (inheritor) */
145 if (!mono_secman_inheritance_check (klass, &demands)) {
146 /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
147 mono_class_set_failure (klass, MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND, NULL);
153 mono_secman_inheritancedemand_method (MonoMethod *override, MonoMethod *base)
155 MonoDeclSecurityActions demands;
157 /* don't hide previous results -and- don't calc everything for nothing */
158 if (override->klass->exception_type != 0)
161 /* short-circuit corlib as it is fully trusted (within itself)
162 * and because this cause major recursion headaches */
163 if ((override->klass->image == mono_defaults.corlib) && (base->klass->image == mono_defaults.corlib))
166 /* Check if there are an InheritanceDemand on the base (virtual) method */
167 if (mono_declsec_get_inheritdemands_method (base, &demands)) {
168 /* If so check the demands on the overriding method */
169 if (!mono_secman_inheritance_check (override->klass, &demands)) {
170 /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
171 mono_class_set_failure (override->klass, MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND, base);
179 mono_security_manager_get_methods (void)
185 mono_secman_inheritancedemand_class (MonoClass *klass, MonoClass *parent)
190 mono_secman_inheritancedemand_method (MonoMethod *override, MonoMethod *base)
194 #endif /* DISABLE_SECURITY */
197 * @publickey An encoded (with header) public key
198 * @size The length of the public key
200 * returns TRUE if the public key is the ECMA "key", FALSE otherwise
202 * ECMA key isn't a real public key - it's simply an empty (but valid) header
203 * so it's length (16) and value (00000000000000000400000000000000) are
207 mono_is_ecma_key (const char *publickey, int size)
210 if ((publickey == NULL) || (size != MONO_ECMA_KEY_LENGTH) || (publickey [8] != 0x04))
213 for (i=0; i < size; i++) {
214 if ((publickey [i] != 0x00) && (i != 8))
221 * Context propagation is required when:
222 * (a) the security manager is active (1.x and later)
223 * (b) other contexts needs to be propagated (2.x and later)
225 * returns NULL if no context propagation is required, else the returns the
226 * MonoMethod to call to Capture the ExecutionContext.
229 mono_get_context_capture_method (void)
231 static MonoMethod *method = NULL;
233 if (!mono_security_manager_activated) {
234 if (mono_image_get_assembly (mono_defaults.corlib)->aname.major < 2)
238 /* older corlib revisions won't have the class (nor the method) */
239 if (mono_defaults.executioncontext_class && !method) {
240 mono_class_init (mono_defaults.executioncontext_class);
241 method = mono_class_get_method_from_name (mono_defaults.executioncontext_class, "Capture", 0);
248 /* System.Security icalls */
251 ves_icall_System_Security_SecurityManager_get_SecurityEnabled (void)
253 if (!mono_security_manager_activated) {
254 /* SecurityManager is internal for Moonlight and SecurityEnabled is used to know if CoreCLR is active
255 * (e.g. plugin executing in the browser) or not (e.g. smcs compiling source code with corlib 2.1)
257 return (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR);
259 return mono_security_manager_enabled;
263 ves_icall_System_Security_SecurityManager_set_SecurityEnabled (MonoBoolean value)
265 /* value can be changed only if the security manager is activated */
266 if (mono_security_manager_activated) {
267 mono_security_manager_enabled = value;
272 ves_icall_System_Security_SecurityManager_get_CheckExecutionRights (void)
274 if (!mono_security_manager_activated)
276 return mono_security_manager_execution;
280 ves_icall_System_Security_SecurityManager_set_CheckExecutionRights (MonoBoolean value)
282 /* value can be changed only id the security manager is activated */
283 if (mono_security_manager_activated) {
284 mono_security_manager_execution = value;
289 ves_icall_System_Security_SecurityManager_GetLinkDemandSecurity (MonoReflectionMethod *m, MonoDeclSecurityActions *kactions, MonoDeclSecurityActions *mactions)
291 MonoMethod *method = m->method;
292 /* we want the original as the wrapper is "free" of the security informations */
293 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
294 method = mono_marshal_method_from_wrapper (method);
297 mono_class_init (method->klass);
299 /* if either the method or it's class has security (any type) */
300 if ((method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) || (method->klass->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
301 memset (kactions, 0, sizeof (MonoDeclSecurityActions));
302 memset (mactions, 0, sizeof (MonoDeclSecurityActions));
304 /* get any linkdemand (either on the method or it's class) */
305 return mono_declsec_get_linkdemands (method, kactions, mactions);