X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Fmetadata%2Fsecurity.c;h=c61eab99f7bf0e1ca51e417c4799a0223d39d807;hb=8e0792472f54311772f942f0188ce06ba7deb2b6;hp=8a734d18eec0fdb3217142181978916179c84202;hpb=ec9269729a98a6b05e78c74aa1d82d6ed643764d;p=mono.git diff --git a/mono/metadata/security.c b/mono/metadata/security.c index 8a734d18eec..c61eab99f7b 100644 --- a/mono/metadata/security.c +++ b/mono/metadata/security.c @@ -4,30 +4,46 @@ * Author: * Sebastien Pouliot * - * (C) 2004 Novell (http://www.novell.com) + * Copyright 2004-2009 Novell, Inc (http://www.novell.com) */ #ifdef HAVE_CONFIG_H #include #endif +#include #include +#include #include +#include +#include #include #include #include -#ifndef PLATFORM_WIN32 +#ifdef HOST_WIN32 + +#include +#include + +#ifndef PROTECTED_DACL_SECURITY_INFORMATION +#define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L +#endif + +#else #include #include #include #include +#include #include #include /* Disclaimers */ +#if defined(__GNUC__) + #ifndef HAVE_GETGRGID_R #warning Non-thread safe getgrgid being used! #endif @@ -41,12 +57,14 @@ #warning Non-thread safe getpwuid being used! #endif -#endif /* not PLATFORM_WIN32 */ +#endif /* defined(__GNUC__) */ + +#endif /* not HOST_WIN32 */ /* internal functions - reuse driven */ -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 /* ask a server to translate a SID into a textual representation */ static gunichar2* @@ -84,7 +102,7 @@ GetSidName (gunichar2 *server, PSID sid, gint32 *size) } } else { - // nothing -> return NULL + /* nothing -> return NULL */ g_free (user); } @@ -95,7 +113,20 @@ GetSidName (gunichar2 *server, PSID sid, gint32 *size) } -#else /* not PLATFORM_WIN32 */ +#else /* not HOST_WIN32 */ + +#define MONO_SYSCONF_DEFAULT_SIZE ((size_t) 1024) + +/* + * Ensure we always get a valid (usable) value from sysconf. + * In case of error, we return the default value. + */ +static size_t mono_sysconf (int name) +{ + size_t size = (size_t) sysconf (name); + /* default value */ + return (size == -1) ? MONO_SYSCONF_DEFAULT_SIZE : size; +} static gchar* @@ -114,9 +145,9 @@ GetTokenName (uid_t uid) #ifdef HAVE_GETPWUID_R #ifdef _SC_GETPW_R_SIZE_MAX - fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX); + fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX); #else - fbufsize = (size_t) 1024; + fbufsize = MONO_SYSCONF_DEFAULT_SIZE; #endif fbuf = g_malloc0 (fbufsize); retval = getpwuid_r (uid, &pwd, fbuf, fbufsize, &p); @@ -180,9 +211,9 @@ IsDefaultGroup (uid_t user, gid_t group) #ifdef HAVE_GETPWUID_R #ifdef _SC_GETPW_R_SIZE_MAX - fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX); + fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX); #else - fbufsize = (size_t) 1024; + fbufsize = MONO_SYSCONF_DEFAULT_SIZE; #endif fbuf = g_malloc0 (fbufsize); @@ -226,19 +257,6 @@ IsMemberOf (gid_t user, struct group *g) /* ICALLS */ -/* System.Environment */ - - -MonoString* -ves_icall_System_Environment_get_UserName (void) -{ - MONO_ARCH_SAVE_REGS; - - /* using glib is more portable */ - return mono_string_new (mono_domain_get (), g_get_user_name ()); -} - - /* System.Security.Principal.WindowsIdentity */ @@ -249,7 +267,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetCurrentToken (void) MONO_ARCH_SAVE_REGS; -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 /* Note: This isn't a copy of the Token - we must not close it!!! * http://www.develop.com/kbrown/book/html/whatis_windowsprincipal.html */ @@ -260,7 +278,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetCurrentToken (void) OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token); } #else - token = (gpointer) geteuid (); + token = GINT_TO_POINTER (geteuid ()); #endif return token; } @@ -273,7 +291,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetTokenName (gpointer token gunichar2 *uniname = NULL; gint32 size = 0; -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 MONO_ARCH_SAVE_REGS; GetTokenInformation (token, TokenUser, NULL, size, (PDWORD)&size); @@ -285,7 +303,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetTokenName (gpointer token g_free (tu); } #else - gchar *uname = GetTokenName ((uid_t) token); + gchar *uname = GetTokenName ((uid_t) GPOINTER_TO_INT (token)); MONO_ARCH_SAVE_REGS; @@ -294,7 +312,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetTokenName (gpointer token uniname = g_utf8_to_utf16 (uname, size, NULL, NULL, NULL); g_free (uname); } -#endif /* PLATFORM_WIN32 */ +#endif /* HOST_WIN32 */ if (size > 0) { result = mono_string_new_utf16 (mono_domain_get (), uniname, size); @@ -312,7 +330,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetTokenName (gpointer token gpointer ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *username) { -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 gpointer token = NULL; MONO_ARCH_SAVE_REGS; @@ -323,7 +341,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *us */ g_warning ("Unsupported on Win32 (anyway requires W2K3 minimum)"); -#else /* PLATFORM_WIN32*/ +#else /* HOST_WIN32*/ #ifdef HAVE_GETPWNAM_R struct passwd pwd; @@ -342,9 +360,9 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *us #ifdef HAVE_GETPWNAM_R #ifdef _SC_GETPW_R_SIZE_MAX - fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX); + fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX); #else - fbufsize = (size_t) 1024; + fbufsize = MONO_SYSCONF_DEFAULT_SIZE; #endif fbuf = g_malloc0 (fbufsize); @@ -357,7 +375,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *us #endif if (result) { - token = (gpointer) p->pw_uid; + token = GINT_TO_POINTER (p->pw_uid); } #ifdef HAVE_GETPWNAM_R @@ -369,14 +387,15 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *us } -// http://www.dotnet247.com/247reference/msgs/39/195403.aspx +/* http://www.dotnet247.com/247reference/msgs/39/195403.aspx // internal static string[] WindowsIdentity._GetRoles (IntPtr token) +*/ MonoArray* ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token) { MonoArray *array = NULL; MonoDomain *domain = mono_domain_get (); -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 gint32 size = 0; MONO_ARCH_SAVE_REGS; @@ -388,7 +407,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token) int i=0; int num = tg->GroupCount; - array = mono_array_new (domain, mono_defaults.string_class, num); + array = mono_array_new (domain, mono_get_string_class (), num); for (i=0; i < num; i++) { gint32 size = 0; @@ -396,7 +415,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token) if (uniname) { MonoString *str = mono_string_new_utf16 (domain, uniname, size); - mono_array_set (array, MonoString *, i, str); + mono_array_setref (array, i, str); g_free (uniname); } } @@ -409,7 +428,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token) #endif if (!array) { /* return empty array of string, i.e. string [0] */ - array = mono_array_new (domain, mono_defaults.string_class, 0); + array = mono_array_new (domain, mono_get_string_class (), 0); } return array; } @@ -425,7 +444,7 @@ ves_icall_System_Security_Principal_WindowsImpersonationContext_CloseToken (gpoi MONO_ARCH_SAVE_REGS; -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 result = (CloseHandle (token) != 0); #endif return result; @@ -439,7 +458,7 @@ ves_icall_System_Security_Principal_WindowsImpersonationContext_DuplicateToken ( MONO_ARCH_SAVE_REGS; -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 if (DuplicateToken (token, SecurityImpersonation, &dupe) == 0) { dupe = NULL; } @@ -477,13 +496,13 @@ ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupId (gpointer { gboolean result = FALSE; -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 MONO_ARCH_SAVE_REGS; /* The convertion from an ID to a string is done in managed code for Windows */ g_warning ("IsMemberOfGroupId should never be called on Win32"); -#else /* PLATFORM_WIN32 */ +#else /* HOST_WIN32 */ #ifdef HAVE_GETGRGID_R struct group grp; @@ -497,28 +516,28 @@ ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupId (gpointer #ifdef HAVE_GETGRGID_R #ifdef _SC_GETGR_R_SIZE_MAX - fbufsize = (size_t) sysconf (_SC_GETGR_R_SIZE_MAX); + fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX); #else - fbufsize = (size_t) 1024; + fbufsize = MONO_SYSCONF_DEFAULT_SIZE; #endif fbuf = g_malloc0 (fbufsize); - retval = getgrgid_r ((gid_t) group, &grp, fbuf, fbufsize, &g); + retval = getgrgid_r ((gid_t) GPOINTER_TO_INT (group), &grp, fbuf, fbufsize, &g); result = ((retval == 0) && (g == &grp)); #else /* default to non thread-safe but posix compliant function */ - g = getgrgid ((gid_t) group); + g = getgrgid ((gid_t) GPOINTER_TO_INT (group)); result = (g != NULL); #endif if (result) { - result = IsMemberOf ((uid_t) user, g); + result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g); } #ifdef HAVE_GETGRGID_R g_free (fbuf); #endif -#endif /* PLATFORM_WIN32 */ +#endif /* HOST_WIN32 */ return result; } @@ -529,14 +548,14 @@ ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpoint { gboolean result = FALSE; -#ifdef PLATFORM_WIN32 +#ifdef HOST_WIN32 MONO_ARCH_SAVE_REGS; /* Windows version use a cache built using WindowsIdentity._GetRoles */ g_warning ("IsMemberOfGroupName should never be called on Win32"); -#else /* PLATFORM_WIN32 */ +#else /* HOST_WIN32 */ gchar *utf8_groupname; MONO_ARCH_SAVE_REGS; @@ -549,9 +568,9 @@ ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpoint gchar *fbuf; gint32 retval; #ifdef _SC_GETGR_R_SIZE_MAX - size_t fbufsize = (size_t) sysconf (_SC_GETGR_R_SIZE_MAX); + size_t fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX); #else - size_t fbufsize = (size_t) 1024; + size_t fbufsize = MONO_SYSCONF_DEFAULT_SIZE; #endif fbuf = g_malloc0 (fbufsize); retval = getgrnam_r (utf8_groupname, &grp, fbuf, fbufsize, &g); @@ -563,7 +582,7 @@ ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpoint #endif if (result) { - result = IsMemberOf ((uid_t) user, g); + result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g); } #ifdef HAVE_GETGRNAM_R @@ -571,7 +590,407 @@ ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpoint #endif g_free (utf8_groupname); } -#endif /* PLATFORM_WIN32 */ +#endif /* HOST_WIN32 */ + + return result; +} + + +/* Mono.Security.Cryptography IO related internal calls */ + +#ifdef HOST_WIN32 + +static PSID +GetAdministratorsSid (void) +{ + SID_IDENTIFIER_AUTHORITY admins = SECURITY_NT_AUTHORITY; + PSID pSid = NULL; + if (!AllocateAndInitializeSid (&admins, 2, SECURITY_BUILTIN_DOMAIN_RID, + DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pSid)) + return NULL; + /* Note: this SID must be freed with FreeSid () */ + return pSid; +} + + +static PSID +GetEveryoneSid (void) +{ + SID_IDENTIFIER_AUTHORITY everyone = SECURITY_WORLD_SID_AUTHORITY; + PSID pSid = NULL; + if (!AllocateAndInitializeSid (&everyone, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pSid)) + return NULL; + /* Note: this SID must be freed with FreeSid () */ + return pSid; +} + + +static PSID +GetCurrentUserSid (void) +{ + PSID sid = NULL; + guint32 size = 0; + gpointer token = ves_icall_System_Security_Principal_WindowsIdentity_GetCurrentToken (); + + GetTokenInformation (token, TokenUser, NULL, size, (PDWORD)&size); + if (size > 0) { + TOKEN_USER *tu = g_malloc0 (size); + if (GetTokenInformation (token, TokenUser, tu, size, (PDWORD)&size)) { + DWORD length = GetLengthSid (tu->User.Sid); + sid = (PSID) g_malloc0 (length); + if (!CopySid (length, sid, tu->User.Sid)) { + g_free (sid); + sid = NULL; + } + } + g_free (tu); + } + /* Note: this SID must be freed with g_free () */ + return sid; +} + + +static ACCESS_MASK +GetRightsFromSid (PSID sid, PACL acl) +{ + ACCESS_MASK rights = 0; + TRUSTEE trustee; + + BuildTrusteeWithSidW (&trustee, sid); + if (GetEffectiveRightsFromAcl (acl, &trustee, &rights) != ERROR_SUCCESS) + return 0; + + return rights; +} + + +static gboolean +IsMachineProtected (gunichar2 *path) +{ + gboolean success = FALSE; + PACL pDACL = NULL; + PSID pEveryoneSid = NULL; + + DWORD dwRes = GetNamedSecurityInfoW (path, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, &pDACL, NULL, NULL); + if (dwRes != ERROR_SUCCESS) + return FALSE; + + /* We check that Everyone is still limited to READ-ONLY - + but not if new entries have been added by an Administrator */ + + pEveryoneSid = GetEveryoneSid (); + if (pEveryoneSid) { + ACCESS_MASK rights = GetRightsFromSid (pEveryoneSid, pDACL); + /* http://msdn.microsoft.com/library/en-us/security/security/generic_access_rights.asp?frame=true */ + success = (rights == (READ_CONTROL | SYNCHRONIZE | FILE_READ_DATA | FILE_READ_EA | FILE_READ_ATTRIBUTES)); + FreeSid (pEveryoneSid); + } + /* Note: we don't need to check our own access - + we'll know soon enough when reading the file */ + + if (pDACL) + LocalFree (pDACL); + + return success; +} + + +static gboolean +IsUserProtected (gunichar2 *path) +{ + gboolean success = FALSE; + PACL pDACL = NULL; + PSID pEveryoneSid = NULL; + + DWORD dwRes = GetNamedSecurityInfoW (path, SE_FILE_OBJECT, + DACL_SECURITY_INFORMATION, NULL, NULL, &pDACL, NULL, NULL); + if (dwRes != ERROR_SUCCESS) + return FALSE; + + /* We check that our original entries in the ACL are in place - + but not if new entries have been added by the user */ + + /* Everyone should be denied */ + pEveryoneSid = GetEveryoneSid (); + if (pEveryoneSid) { + ACCESS_MASK rights = GetRightsFromSid (pEveryoneSid, pDACL); + success = (rights == 0); + FreeSid (pEveryoneSid); + } + /* Note: we don't need to check our own access - + we'll know soon enough when reading the file */ + + if (pDACL) + LocalFree (pDACL); + + return success; +} + + +static gboolean +ProtectMachine (gunichar2 *path) +{ + PSID pEveryoneSid = GetEveryoneSid (); + PSID pAdminsSid = GetAdministratorsSid (); + DWORD retval = -1; + + if (pEveryoneSid && pAdminsSid) { + PACL pDACL = NULL; + EXPLICIT_ACCESS ea [2]; + ZeroMemory (&ea, 2 * sizeof (EXPLICIT_ACCESS)); + + /* grant all access to the BUILTIN\Administrators group */ + BuildTrusteeWithSidW (&ea [0].Trustee, pAdminsSid); + ea [0].grfAccessPermissions = GENERIC_ALL; + ea [0].grfAccessMode = SET_ACCESS; + ea [0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT; + ea [0].Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea [0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; + + /* read-only access everyone */ + BuildTrusteeWithSidW (&ea [1].Trustee, pEveryoneSid); + ea [1].grfAccessPermissions = GENERIC_READ; + ea [1].grfAccessMode = SET_ACCESS; + ea [1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT; + ea [1].Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea [1].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; + + retval = SetEntriesInAcl (2, ea, NULL, &pDACL); + if (retval == ERROR_SUCCESS) { + /* with PROTECTED_DACL_SECURITY_INFORMATION we */ + /* remove any existing ACL (like inherited ones) */ + retval = SetNamedSecurityInfo (path, SE_FILE_OBJECT, + DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, + NULL, NULL, pDACL, NULL); + } + if (pDACL) + LocalFree (pDACL); + } + + if (pEveryoneSid) + FreeSid (pEveryoneSid); + if (pAdminsSid) + FreeSid (pAdminsSid); + return (retval == ERROR_SUCCESS); +} + + +static gboolean +ProtectUser (gunichar2 *path) +{ + DWORD retval = -1; + + PSID pCurrentSid = GetCurrentUserSid (); + if (pCurrentSid) { + PACL pDACL = NULL; + EXPLICIT_ACCESS ea; + ZeroMemory (&ea, sizeof (EXPLICIT_ACCESS)); + + /* grant exclusive access to the current user */ + BuildTrusteeWithSidW (&ea.Trustee, pCurrentSid); + ea.grfAccessPermissions = GENERIC_ALL; + ea.grfAccessMode = SET_ACCESS; + ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT; + ea.Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea.Trustee.TrusteeType = TRUSTEE_IS_USER; + + retval = SetEntriesInAcl (1, &ea, NULL, &pDACL); + if (retval == ERROR_SUCCESS) { + /* with PROTECTED_DACL_SECURITY_INFORMATION we + remove any existing ACL (like inherited ones) */ + retval = SetNamedSecurityInfo (path, SE_FILE_OBJECT, + DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, + NULL, NULL, pDACL, NULL); + } + + if (pDACL) + LocalFree (pDACL); + g_free (pCurrentSid); /* g_malloc0 */ + } + return (retval == ERROR_SUCCESS); +} + +#else + +static gboolean +IsProtected (MonoString *path, gint32 protection) +{ + gboolean result = FALSE; + gchar *utf8_name = mono_unicode_to_external (mono_string_chars (path)); + if (utf8_name) { + struct stat st; + if (stat (utf8_name, &st) == 0) { + result = (((st.st_mode & 0777) & protection) == 0); + } + g_free (utf8_name); + } return result; } + + +static gboolean +Protect (MonoString *path, gint32 file_mode, gint32 add_dir_mode) +{ + gboolean result = FALSE; + gchar *utf8_name = mono_unicode_to_external (mono_string_chars (path)); + if (utf8_name) { + struct stat st; + if (stat (utf8_name, &st) == 0) { + int mode = file_mode; + if (st.st_mode & S_IFDIR) + mode |= add_dir_mode; + result = (chmod (utf8_name, mode) == 0); + } + g_free (utf8_name); + } + return result; +} + +#endif /* not HOST_WIN32 */ + + +MonoBoolean +ves_icall_Mono_Security_Cryptography_KeyPairPersistence_CanSecure (MonoString *root) +{ +#if HOST_WIN32 + gint32 flags; + + MONO_ARCH_SAVE_REGS; + + /* ACL are nice... unless you have FAT or other uncivilized filesystem */ + if (!GetVolumeInformation (mono_string_chars (root), NULL, 0, NULL, NULL, (LPDWORD)&flags, NULL, 0)) + return FALSE; + return ((flags & FS_PERSISTENT_ACLS) == FS_PERSISTENT_ACLS); +#else + MONO_ARCH_SAVE_REGS; + /* we assume some kind of security is applicable outside Windows */ + return TRUE; +#endif +} + + +MonoBoolean +ves_icall_Mono_Security_Cryptography_KeyPairPersistence_IsMachineProtected (MonoString *path) +{ + gboolean ret = FALSE; + + MONO_ARCH_SAVE_REGS; + + /* no one, but the owner, should have write access to the directory */ +#ifdef HOST_WIN32 + ret = IsMachineProtected (mono_string_chars (path)); +#else + ret = IsProtected (path, (S_IWGRP | S_IWOTH)); +#endif + return ret; +} + + +MonoBoolean +ves_icall_Mono_Security_Cryptography_KeyPairPersistence_IsUserProtected (MonoString *path) +{ + gboolean ret = FALSE; + + MONO_ARCH_SAVE_REGS; + + /* no one, but the user, should have access to the directory */ +#ifdef HOST_WIN32 + ret = IsUserProtected (mono_string_chars (path)); +#else + ret = IsProtected (path, (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)); +#endif + return ret; +} + + +MonoBoolean +ves_icall_Mono_Security_Cryptography_KeyPairPersistence_ProtectMachine (MonoString *path) +{ + gboolean ret = FALSE; + + MONO_ARCH_SAVE_REGS; + + /* read/write to owner, read to everyone else */ +#ifdef HOST_WIN32 + ret = ProtectMachine (mono_string_chars (path)); +#else + ret = Protect (path, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), (S_IXUSR | S_IXGRP | S_IXOTH)); +#endif + return ret; +} + + +MonoBoolean +ves_icall_Mono_Security_Cryptography_KeyPairPersistence_ProtectUser (MonoString *path) +{ + gboolean ret = FALSE; + + MONO_ARCH_SAVE_REGS; + + /* read/write to user, no access to everyone else */ +#ifdef HOST_WIN32 + ret = ProtectUser (mono_string_chars (path)); +#else + ret = Protect (path, (S_IRUSR | S_IWUSR), S_IXUSR); +#endif + return ret; +} + + +/* + * Returns TRUE if there is "something" where the Authenticode signature is + * normally located. Returns FALSE is data directory is empty. + * + * Note: Neither the structure nor the signature is verified by this function. + */ +MonoBoolean +ves_icall_System_Security_Policy_Evidence_IsAuthenticodePresent (MonoReflectionAssembly *refass) +{ + if (refass && refass->assembly && refass->assembly->image) { + return mono_image_has_authenticode_entry (refass->assembly->image); + } + return FALSE; +} + + +/* System.Security.SecureString related internal calls */ + +static MonoImage *system_security_assembly = NULL; + +void +ves_icall_System_Security_SecureString_DecryptInternal (MonoArray *data, MonoObject *scope) +{ + invoke_protected_memory_method (data, scope, FALSE); +} +void +ves_icall_System_Security_SecureString_EncryptInternal (MonoArray* data, MonoObject *scope) +{ + invoke_protected_memory_method (data, scope, TRUE); +} + +void invoke_protected_memory_method (MonoArray *data, MonoObject *scope, gboolean encrypt) +{ + MonoClass *klass; + MonoMethod *method; + void *params [2]; + + MONO_ARCH_SAVE_REGS; + + if (system_security_assembly == NULL) { + system_security_assembly = mono_image_loaded ("System.Security"); + if (!system_security_assembly) { + MonoAssembly *sa = mono_assembly_open ("System.Security.dll", NULL); + if (!sa) + g_assert_not_reached (); + system_security_assembly = mono_assembly_get_image (sa); + } + } + + klass = mono_class_from_name (system_security_assembly, + "System.Security.Cryptography", "ProtectedMemory"); + method = mono_class_get_method_from_name (klass, encrypt ? "Protect" : "Unprotect", 2); + params [0] = data; + params [1] = scope; /* MemoryProtectionScope.SameProcess */ + mono_runtime_invoke (method, NULL, params, NULL); +}