]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/random.c
core: Fix uninit pointer read in protocol-native
[pulseaudio] / src / pulsecore / random.c
index a87d24e31cd77c3aee9d753a81801abefdd2abfd..acfbc3123cf9b6469802a601985eac2baf2324db 100644 (file)
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
-#include <string.h>
 #include <stdlib.h>
 #include <time.h>
 
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#include <wincrypt.h>
+#endif
+
 #include <pulsecore/core-util.h>
 #include <pulsecore/log.h>
 #include <pulsecore/macro.h>
 
 #include "random.h"
 
-static pa_bool_t has_whined = TRUE;
+static bool has_whined = false;
 
 static const char * const devices[] = { "/dev/urandom", "/dev/random", NULL };
 
 static int random_proper(void *ret_data, size_t length) {
 #ifdef OS_IS_WIN32
+    int ret = -1;
+
+    HCRYPTPROV hCryptProv = 0;
+
     pa_assert(ret_data);
     pa_assert(length > 0);
 
-    return -1;
+    if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
+        if (CryptGenRandom(hCryptProv, length, ret_data))
+            ret = 0;
+        CryptReleaseContext(hCryptProv, 0);
+    }
+
+    return ret;
 
 #else /* OS_IS_WIN32 */
 
@@ -88,7 +102,7 @@ void pa_random_seed(void) {
 
         if (!has_whined) {
             pa_log_warn("Failed to get proper entropy. Falling back to seeding with current time.");
-            has_whined = TRUE;
+            has_whined = true;
         }
 
         seed = (unsigned int) time(NULL);
@@ -109,7 +123,7 @@ void pa_random(void *ret_data, size_t length) {
 
     if (!has_whined) {
         pa_log_warn("Failed to get proper entropy. Falling back to unsecure pseudo RNG.");
-        has_whined = TRUE;
+        has_whined = true;
     }
 
     for (p = ret_data, l = length; l > 0; p++, l--)