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