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