Merge pull request #2663 from esdrubal/islocal
[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 2005-2009 Novell, Inc (http://www.novell.com)
8  */
9
10 #include "security-manager.h"
11
12 /* Class lazy loading functions */
13 static GENERATE_GET_CLASS_WITH_CACHE (security_manager, System.Security, SecurityManager)
14 static GENERATE_TRY_GET_CLASS_WITH_CACHE (execution_context, System.Threading, ExecutionContext)
15
16 static MonoSecurityMode mono_security_mode = MONO_SECURITY_MODE_NONE;
17
18 void
19 mono_security_set_mode (MonoSecurityMode mode)
20 {
21         mono_security_mode = mode;
22 }
23
24 MonoSecurityMode
25 mono_security_get_mode (void)
26 {
27         return mono_security_mode;
28 }
29
30 #ifndef DISABLE_SECURITY
31
32 static MonoSecurityManager secman;
33
34 MonoSecurityManager*
35 mono_security_manager_get_methods (void)
36 {
37         /* Already initialized ? */
38         if (secman.securitymanager)
39                 return &secman;
40
41         /* Initialize */
42         secman.securitymanager = mono_class_get_security_manager_class ();
43         if (!secman.securitymanager->inited)
44                 mono_class_init (secman.securitymanager);
45
46         return &secman;
47 }
48
49 #else
50
51 MonoSecurityManager*
52 mono_security_manager_get_methods (void)
53 {
54         return NULL;
55 }
56
57 #endif /* DISABLE_SECURITY */
58
59 /*
60  * @publickey   An encoded (with header) public key
61  * @size        The length of the public key
62  *
63  * returns TRUE if the public key is the ECMA "key", FALSE otherwise
64  *
65  * ECMA key isn't a real public key - it's simply an empty (but valid) header
66  * so it's length (16) and value (00000000000000000400000000000000) are 
67  * constants.
68  */
69 gboolean 
70 mono_is_ecma_key (const char *publickey, int size)
71 {
72         int i;
73         if ((publickey == NULL) || (size != MONO_ECMA_KEY_LENGTH) || (publickey [8] != 0x04))
74                 return FALSE;
75
76         for (i=0; i < size; i++) {
77                 if ((publickey [i] != 0x00) && (i != 8))
78                         return FALSE;
79         }
80         return TRUE;
81 }
82
83 /*
84  * Context propagation is required when:
85  * (a) the security manager is active (1.x and later)
86  * (b) other contexts needs to be propagated (2.x and later)
87  *
88  * returns NULL if no context propagation is required, else the returns the
89  * MonoMethod to call to Capture the ExecutionContext.
90  */
91 MonoMethod*
92 mono_get_context_capture_method (void)
93 {
94         static MonoMethod *method = NULL;
95
96         if (mono_image_get_assembly (mono_defaults.corlib)->aname.major < 2)
97                 return NULL;
98
99         /* older corlib revisions won't have the class (nor the method) */
100         MonoClass *execution_context = mono_class_try_get_execution_context_class ();
101         if (execution_context && !method) {
102                 mono_class_init (execution_context);
103                 method = mono_class_get_method_from_name (execution_context, "Capture", 0);
104         }
105
106         return method;
107 }
108
109
110 /* System.Security icalls */
111
112 MonoBoolean
113 ves_icall_System_Security_SecurityManager_get_SecurityEnabled (void)
114 {
115         /* SecurityManager is internal for Moonlight and SecurityEnabled is used to know if CoreCLR is active
116          * (e.g. plugin executing in the browser) or not (e.g. smcs compiling source code with corlib 2.1)
117          */
118         return (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR);
119 }
120
121 void
122 ves_icall_System_Security_SecurityManager_set_SecurityEnabled (MonoBoolean value)
123 {
124 }