From 7182e751a105610cb3e161fac17ebd247515c8e7 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 29 Mar 2017 12:05:24 -0400 Subject: [PATCH] abandon usage of random() Change-Id: I870fd7963dc9148b496361892a2bfe2cc0f88690 --- daemon/aux.h | 8 +++++++- daemon/ice.c | 2 +- daemon/media_socket.c | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/daemon/aux.h b/daemon/aux.h index 842243a6d..3d45afb5d 100644 --- a/daemon/aux.h +++ b/daemon/aux.h @@ -19,6 +19,7 @@ #include #include "compat.h" #include +#include #if !(GLIB_CHECK_VERSION(2,30,0)) #define g_atomic_int_and(atomic, val) \ @@ -185,7 +186,12 @@ INLINE int strmemcmp(const void *mem, int len, const char *str) { } INLINE void random_string(unsigned char *buf, int len) { - RAND_bytes(buf, len); + assert(RAND_bytes(buf, len) == 1); +} +INLINE long int ssl_random() { + long int ret; + random_string((void *) &ret, sizeof(ret)); + return ret; } INLINE const char *__get_enum_array_text(const char * const *array, unsigned int idx, diff --git a/daemon/ice.c b/daemon/ice.c index bd872a924..7b9171138 100644 --- a/daemon/ice.c +++ b/daemon/ice.c @@ -1372,7 +1372,7 @@ sleep: static void random_ice_string(char *buf, int len) { while (len--) - *buf++ = ice_chars[random() % strlen(ice_chars)]; + *buf++ = ice_chars[ssl_random() % strlen(ice_chars)]; } static void create_random_ice_string(struct call *call, str *s, int len) { diff --git a/daemon/media_socket.c b/daemon/media_socket.c index 4fe46718b..7361afe71 100644 --- a/daemon/media_socket.c +++ b/daemon/media_socket.c @@ -648,7 +648,7 @@ int __get_consecutive_ports(GQueue *out, unsigned int num_ports, unsigned int wa port = g_atomic_int_get(&pp->last_used); __C_DBG("before randomization port=%d", port); #if PORT_RANDOM_MIN && PORT_RANDOM_MAX - port += PORT_RANDOM_MIN + (random() % (PORT_RANDOM_MAX - PORT_RANDOM_MIN)); + port += PORT_RANDOM_MIN + (ssl_random() % (PORT_RANDOM_MAX - PORT_RANDOM_MIN)); #endif __C_DBG("after randomization port=%d", port); }