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