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;
24 mono_security_manager_get_methods (void)
26 /* Already initialized ? */
27 if (secman.securitymanager)
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);
37 secman.demand = mono_class_get_method_from_name (secman.securitymanager,
39 g_assert (secman.demand);
41 secman.demandchoice = mono_class_get_method_from_name (secman.securitymanager,
42 "InternalDemandChoice", 2);
43 g_assert (secman.demandchoice);
45 secman.demandunmanaged = mono_class_get_method_from_name (secman.securitymanager,
46 "DemandUnmanaged", 0);
47 g_assert (secman.demandunmanaged);
49 secman.inheritancedemand = mono_class_get_method_from_name (secman.securitymanager,
50 "InheritanceDemand", 2);
51 g_assert (secman.inheritancedemand);
53 secman.inheritsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
54 "InheritanceDemandSecurityException", 4);
55 g_assert (secman.inheritsecurityexception);
57 secman.linkdemand = mono_class_get_method_from_name (secman.securitymanager,
59 g_assert (secman.linkdemand);
61 secman.linkdemandunmanaged = mono_class_get_method_from_name (secman.securitymanager,
62 "LinkDemandUnmanaged", 1);
63 g_assert (secman.linkdemandunmanaged);
65 secman.linkdemandfulltrust = mono_class_get_method_from_name (secman.securitymanager,
66 "LinkDemandFullTrust", 1);
67 g_assert (secman.linkdemandfulltrust);
69 secman.linkdemandsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
70 "LinkDemandSecurityException", 3);
71 g_assert (secman.linkdemandsecurityexception);
73 secman.allowpartiallytrustedcallers = mono_class_from_name (mono_defaults.corlib, "System.Security",
74 "AllowPartiallyTrustedCallersAttribute");
75 g_assert (secman.allowpartiallytrustedcallers);
77 secman.suppressunmanagedcodesecurity = mono_class_from_name (mono_defaults.corlib, "System.Security",
78 "SuppressUnmanagedCodeSecurityAttribute");
79 g_assert (secman.suppressunmanagedcodesecurity);
85 mono_secman_inheritance_check (MonoClass *klass, MonoDeclSecurityActions *demands)
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);
97 res = mono_runtime_invoke (secman->inheritancedemand, NULL, args, NULL);
98 return (*(MonoBoolean *) mono_object_unbox (res));
102 mono_secman_inheritancedemand_class (MonoClass *klass, MonoClass *parent)
104 MonoDeclSecurityActions demands;
106 /* don't hide previous results -and- don't calc everything for nothing */
107 if (klass->exception_type != 0)
110 /* Check if there are an InheritanceDemand on the parent class */
111 if (mono_declsec_get_inheritdemands_class (parent, &demands)) {
112 /* If so check the demands on the klass (inheritor) */
113 if (!mono_secman_inheritance_check (klass, &demands)) {
114 /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
115 klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
116 klass->exception_data = NULL;
122 mono_secman_inheritancedemand_method (MonoMethod *override, MonoMethod *base)
124 MonoDeclSecurityActions demands;
126 /* don't hide previous results -and- don't calc everything for nothing */
127 if (override->klass->exception_type != 0)
130 /* Check if there are an InheritanceDemand on the base (virtual) method */
131 if (mono_declsec_get_inheritdemands_method (base, &demands)) {
132 /* If so check the demands on the overriding method */
133 if (!mono_secman_inheritance_check (override->klass, &demands)) {
134 /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
135 override->klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
136 override->klass->exception_data = base;
143 * Note: The security manager is activate once when executing the Mono. This
144 * is not meant to be a turn on/off runtime switch.
147 mono_activate_security_manager (void)
149 mono_security_manager_activated = TRUE;
153 mono_is_security_manager_active (void)
155 return mono_security_manager_activated;
159 * @publickey An encoded (with header) public key
160 * @size The length of the public key
162 * returns TRUE if the public key is the ECMA "key", FALSE otherwise
164 * ECMA key isn't a real public key - it's simply an empty (but valid) header
165 * so it's length (16) and value (00000000000000000400000000000000) are
169 mono_is_ecma_key (const char *publickey, int size)
172 if ((publickey == NULL) || (size != MONO_ECMA_KEY_LENGTH) || (publickey [8] != 0x04))
175 for (i=0; i < size; i++) {
176 if ((publickey [i] != 0x00) && (i != 8))
182 /* System.Security icalls */
185 ves_icall_System_Security_SecurityManager_get_SecurityEnabled (void)
187 if (!mono_security_manager_activated)
189 return mono_security_manager_enabled;
193 ves_icall_System_Security_SecurityManager_set_SecurityEnabled (MonoBoolean value)
195 /* value can be changed only if the security manager is activated */
196 if (mono_security_manager_activated) {
197 mono_security_manager_enabled = value;
202 ves_icall_System_Security_SecurityManager_get_CheckExecutionRights (void)
204 if (!mono_security_manager_activated)
206 return mono_security_manager_execution;
210 ves_icall_System_Security_SecurityManager_set_CheckExecutionRights (MonoBoolean value)
212 /* value can be changed only id the security manager is activated */
213 if (mono_security_manager_activated) {
214 mono_security_manager_execution = value;
219 ves_icall_System_Security_SecurityManager_GetLinkDemandSecurity (MonoReflectionMethod *m, MonoDeclSecurityActions *kactions, MonoDeclSecurityActions *mactions)
221 MonoMethod *method = m->method;
222 /* we want the original as the wrapper is "free" of the security informations */
223 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
224 method = mono_marshal_method_from_wrapper (method);
227 mono_class_init (method->klass);
229 /* if either the method or it's class has security (any type) */
230 if ((method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) || (method->klass->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
231 memset (kactions, 0, sizeof (MonoDeclSecurityActions));
232 memset (mactions, 0, sizeof (MonoDeclSecurityActions));
234 /* get any linkdemand (either on the method or it's class) */
235 return mono_declsec_get_linkdemands (method, kactions, mactions);