gpointer ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpointer handle, MonoArray *arry) { guint32 len = mono_array_length (arry); guchar *buf = mono_array_addr (arry, guchar, 0); /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */ gint count = 0; gint err; do { if (len - count >= sizeof (long)) { *(long*)buf = rand(); count += sizeof (long); } else if (len - count >= sizeof (short)) { *(short*)buf = rand(); count += sizeof (short); } else if (len - count >= sizeof (char)) { *buf = rand(); count += sizeof (char); } } while (count < len); return (gpointer)-1L; }
gpointer ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpointer handle, MonoArray *arry) { gint file = GPOINTER_TO_INT (handle); guint32 len = mono_array_length (arry); guchar *buf = mono_array_addr (arry, guchar, 0); if (egd) { const char *socket_path = g_getenv ("MONO_EGD_SOCKET"); /* exception will be thrown in managed code */ if (socket_path == NULL) return NULL; get_entropy_from_server (socket_path, mono_array_addr (arry, guchar, 0), mono_array_length (arry)); return (gpointer) -1; } else { /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */ gint count = 0; gint err; do { err = read (file, buf + count, len - count); if (err < 0) { if (errno == EINTR) continue; break; } count += err; } while (count < len); if (err < 0) { g_warning("Entropy error! Error in read (%s).", strerror (errno)); /* exception will be thrown in managed code */ return NULL; } } /* We do not support PRNG seeding right now but the class library is this */ return handle; }
gpointer ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpointer handle, MonoArray *arry) { HCRYPTPROV provider = (HCRYPTPROV) handle; guint32 len = mono_array_length (arry); guchar *buf = mono_array_addr (arry, guchar, 0); if (!CryptGenRandom (provider, len, buf)) { CryptReleaseContext (provider, 0); /* we may have lost our context with CryptoAPI, but all hope isn't lost yet! */ provider = (HCRYPTPROV) ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (NULL); if (!CryptGenRandom (provider, len, buf)) { CryptReleaseContext (provider, 0); provider = 0; /* exception will be thrown in managed code */ } } return (gpointer) provider; }
MonoArray * ves_icall_System_Diagnostics_Process_GetProcesses_internal (void) { MonoError error; MonoArray *procs; gboolean ret; DWORD needed; int count; DWORD *pids; count = 512; do { pids = g_new0 (DWORD, count); ret = mono_process_win_enum_processes (pids, count * sizeof (guint32), &needed); if (ret == FALSE) { MonoException *exc; g_free (pids); pids = NULL; exc = mono_get_exception_not_supported ("This system does not support EnumProcesses"); mono_set_pending_exception (exc); return NULL; } if (needed < (count * sizeof (guint32))) break; g_free (pids); pids = NULL; count = (count * 3) / 2; } while (TRUE); count = needed / sizeof (guint32); procs = mono_array_new_checked (mono_domain_get (), mono_get_int32_class (), count, &error); if (mono_error_set_pending_exception (&error)) { g_free (pids); return NULL; } memcpy (mono_array_addr (procs, guint32, 0), pids, needed); g_free (pids); pids = NULL; return procs; }
std::vector<guid_t> MonoGetImplementedClasses(const guid_t& iid) { MonoEnsureThreadAttached(); void* args[1]; args[0] = (char*)&iid; MonoObject* exc = nullptr; MonoArray* retval = (MonoArray*)mono_runtime_invoke(g_getImplementsMethod, nullptr, args, &exc); if (exc) { return std::vector<guid_t>(); } guid_t* retvalStart = mono_array_addr(retval, guid_t, 0); uintptr_t retvalLength = mono_array_length(retval); return std::vector<guid_t>(retvalStart, retvalStart + retvalLength); }
char * mono_exception_get_native_backtrace (MonoException *exc) { #ifdef HAVE_BACKTRACE_SYMBOLS MonoDomain *domain; MonoArray *arr = exc->native_trace_ips; int i, len; GString *text; char **messages; if (!arr) return g_strdup (""); domain = mono_domain_get (); len = mono_array_length (arr); text = g_string_new_len (NULL, len * 20); uint32_t gchandle = mono_gchandle_new (&arr->obj, TRUE); /* pinned */ void* addr = mono_array_addr (arr, gpointer, 0); MONO_ENTER_GC_SAFE; messages = backtrace_symbols (addr, len); MONO_EXIT_GC_SAFE; mono_gchandle_free (gchandle); for (i = 0; i < len; ++i) { gpointer ip = mono_array_get (arr, gpointer, i); MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip); if (ji) { char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain); g_string_append_printf (text, "%s\n", msg); g_free (msg); } else { g_string_append_printf (text, "%s\n", messages [i]); } } g_free (messages); return g_string_free (text, FALSE); #else return g_strdup (""); #endif }
gpointer ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (MonoArray *seed) { HCRYPTPROV provider = 0; /* There is no need to create a container for just random data, so we can use CRYPT_VERIFY_CONTEXT (one call) see: http://blogs.msdn.com/dangriff/archive/2003/11/19/51709.aspx */ /* We first try to use the Intel PIII RNG if drivers are present */ if (!CryptAcquireContext (&provider, NULL, NULL, PROV_INTEL_SEC, CRYPT_VERIFY_CONTEXT)) { /* not a PIII or no drivers available, use default RSA CSP */ if (!CryptAcquireContext (&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFY_CONTEXT)) { provider = 0; /* exception will be thrown in managed code */ } } /* seed the CSP with the supplied buffer (if present) */ if ((provider != 0) && (seed)) { guint32 len = mono_array_length (seed); guchar *buf = mono_array_addr (seed, guchar, 0); /* the call we replace the seed with random - this isn't what is expected from the class library user */ guchar *data = g_malloc (len); if (data) { memcpy (data, buf, len); /* add seeding material to the RNG */ CryptGenRandom (provider, len, data); /* zeroize and free */ memset (data, 0, len); g_free (data); } } return (gpointer) provider; }