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