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