Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use crypto safe random numbers #2738

Merged
merged 21 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,12 @@ mjr2pcap_SOURCES = \
mjr2pcap_CFLAGS = \
$(AM_CFLAGS) \
$(POST_PROCESSING_CFLAGS) \
$(BORINGSSL_CFLAGS) \
$(NULL)

mjr2pcap_LDADD = \
$(POST_PROCESSING_LIBS) \
$(BORINGSSL_LIBS) \
$(POST_PROCESSING_MANUAL_LIBS) \
$(NULL)

Expand All @@ -658,11 +660,13 @@ pcap2mjr_CFLAGS = \
$(AM_CFLAGS) \
-I$(top_builddir)/postprocessing \
$(POST_PROCESSING_CFLAGS) \
$(BORINGSSL_CFLAGS) \
$(PCAP_CFLAGS) \
$(NULL)

pcap2mjr_LDADD = \
$(POST_PROCESSING_LIBS) \
$(BORINGSSL_LIBS) \
$(POST_PROCESSING_MANUAL_LIBS) \
$(PCAP_LIBS) \
$(NULL)
Expand Down
2 changes: 1 addition & 1 deletion ice.c
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ janus_ice_handle *janus_ice_handle_create(void *core_session, const char *opaque
janus_ice_handle *handle = NULL;
guint64 handle_id = 0;
while(handle_id == 0) {
handle_id = janus_random_uint64();
handle_id = janus_random_uint64_javacript_safe();
handle = janus_session_handles_find(session, handle_id);
if(handle != NULL) {
/* Handle ID already taken, try another one */
Expand Down
7 changes: 6 additions & 1 deletion janus.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ janus_session *janus_session_create(guint64 session_id) {
janus_session *session = NULL;
if(session_id == 0) {
while(session_id == 0) {
session_id = janus_random_uint64();
session_id = janus_random_uint64_javacript_safe();
session = janus_session_find(session_id);
if(session != NULL) {
/* Session ID already taken, try another one */
Expand Down Expand Up @@ -4996,6 +4996,11 @@ gint main(int argc, char *argv[])
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
/* check if random pool looks ok (this does not give any guarantees for later, though) */
if(RAND_status() != 1) {
JANUS_LOG(LOG_FATAL, "\tOpenSSL PRNG is not properly seeded, cannot generate random numbers\n");
exit(1);
}
/* ... and DTLS-SRTP in particular */
const char *dtls_ciphers = NULL;
item = janus_config_get(config, config_certs, janus_config_type_item, "dtls_ciphers");
Expand Down
6 changes: 3 additions & 3 deletions plugins/janus_audiobridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -2821,7 +2821,7 @@ static json_t *janus_audiobridge_process_synchronous_request(janus_audiobridge_s
gboolean room_id_allocated = FALSE;
if(!string_ids && room_id == 0) {
while(room_id == 0) {
room_id = janus_random_uint64();
room_id = janus_random_uint64_javacript_safe();
if(g_hash_table_lookup(rooms, &room_id) != NULL) {
/* Room ID already taken, try another one */
room_id = 0;
Expand Down Expand Up @@ -5673,7 +5673,7 @@ static void *janus_audiobridge_handler(void *data) {
if(user_id == 0) {
/* Generate a random ID */
while(user_id == 0) {
user_id = janus_random_uint64();
user_id = janus_random_uint64_javacript_safe();
if(g_hash_table_lookup(audiobridge->participants, &user_id) != NULL) {
/* User ID already taken, try another one */
user_id = 0;
Expand Down Expand Up @@ -6394,7 +6394,7 @@ static void *janus_audiobridge_handler(void *data) {
if(user_id == 0) {
/* Generate a random ID */
while(user_id == 0) {
user_id = janus_random_uint64();
user_id = janus_random_uint64_javacript_safe();
if(g_hash_table_lookup(audiobridge->participants, &user_id) != NULL) {
/* User ID already taken, try another one */
user_id = 0;
Expand Down
5 changes: 0 additions & 5 deletions plugins/janus_nosip.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,11 +786,6 @@ int janus_nosip_init(janus_callbacks *callback, const char *config_path) {
}
JANUS_LOG(LOG_VERB, "Local IP set to %s\n", local_ip);

#ifdef HAVE_SRTP_2
/* Init randomizer (for randum numbers in SRTP) */
RAND_poll();
#endif

sessions = g_hash_table_new_full(NULL, NULL, NULL, (GDestroyNotify)janus_nosip_session_destroy);
messages = g_async_queue_new_full((GDestroyNotify) janus_nosip_message_free);
/* This is the callback we'll need to invoke to contact the Janus core */
Expand Down
2 changes: 1 addition & 1 deletion plugins/janus_recordplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,7 @@ static void *janus_recordplay_handler(void *data) {
}
if(id == 0) {
while(id == 0) {
id = janus_random_uint64();
id = janus_random_uint64_javacript_safe();
if(g_hash_table_lookup(recordings, &id) != NULL) {
/* Recording ID already taken, try another one */
id = 0;
Expand Down
5 changes: 0 additions & 5 deletions plugins/janus_sip.c
Original file line number Diff line number Diff line change
Expand Up @@ -1924,11 +1924,6 @@ int janus_sip_init(janus_callbacks *callback, const char *config_path) {
}
JANUS_LOG(LOG_VERB, "Local IP set to %s\n", local_ip);

#ifdef HAVE_SRTP_2
/* Init randomizer (for randum numbers in SRTP) */
RAND_poll();
#endif

/* Setup sofia */
su_init();
if(notify_events && callback->events_is_enabled()) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/janus_streaming.c
Original file line number Diff line number Diff line change
Expand Up @@ -2728,7 +2728,7 @@ static json_t *janus_streaming_process_synchronous_request(janus_streaming_sessi
/* Generate a unique numeric ID */
JANUS_LOG(LOG_VERB, "Missing numeric id, will generate a random one...\n");
while(mpid == 0) {
mpid = janus_random_uint64();
mpid = janus_random_uint64_javacript_safe();
if(g_hash_table_lookup(mountpoints, &mpid) != NULL ||
g_hash_table_lookup(mountpoints_temp, &mpid) != NULL) {
/* ID already in use, try another one */
Expand Down
2 changes: 1 addition & 1 deletion plugins/janus_textroom.c
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,7 @@ janus_plugin_result *janus_textroom_handle_incoming_request(janus_plugin_session
gboolean room_id_allocated = FALSE;
if(!string_ids && room_id == 0) {
while(room_id == 0) {
room_id = janus_random_uint64();
room_id = janus_random_uint64_javacript_safe();
if(g_hash_table_lookup(rooms, &room_id) != NULL) {
/* Room ID already taken, try another one */
room_id = 0;
Expand Down
4 changes: 2 additions & 2 deletions plugins/janus_videoroom.c
Original file line number Diff line number Diff line change
Expand Up @@ -3152,7 +3152,7 @@ static json_t *janus_videoroom_process_synchronous_request(janus_videoroom_sessi
gboolean room_id_allocated = FALSE;
if(!string_ids && room_id == 0) {
while(room_id == 0) {
room_id = janus_random_uint64();
room_id = janus_random_uint64_javacript_safe();
if(g_hash_table_lookup(rooms, &room_id) != NULL) {
/* Room ID already taken, try another one */
room_id = 0;
Expand Down Expand Up @@ -6093,7 +6093,7 @@ static void *janus_videoroom_handler(void *data) {
if(user_id == 0) {
/* Generate a random ID */
while(user_id == 0) {
user_id = janus_random_uint64();
user_id = janus_random_uint64_javacript_safe();
if(g_hash_table_lookup(videoroom->participants, &user_id) != NULL) {
/* User ID already taken, try another one */
user_id = 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/janus_voicemail.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ void janus_voicemail_create_session(janus_plugin_session *handle, int *error) {
}
janus_voicemail_session *session = g_malloc0(sizeof(janus_voicemail_session));
session->handle = handle;
session->recording_id = janus_random_uint64();
session->recording_id = janus_random_uint64_javacript_safe();
session->start_time = 0;
session->stream = NULL;
char f[255];
Expand Down
31 changes: 17 additions & 14 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <inttypes.h>

#include <zlib.h>
#include <openssl/rand.h>

#include "utils.h"
#include "debug.h"
Expand Down Expand Up @@ -71,23 +72,25 @@ gboolean janus_strcmp_const_time(const void *str1, const void *str2) {
}

guint32 janus_random_uint32(void) {
return g_random_int();
guint32 ret = 0;
if (RAND_bytes((void *)&ret, sizeof(ret)) != 1) {
JANUS_LOG(LOG_FATAL, "\tOpenSSL RAND_bytes() failed\n");
exit(1);
}
return ret;
}

guint64 janus_random_uint64(void) {
/*
* FIXME This needs to be improved, and use something that generates
* more strongly random stuff... using /dev/urandom is probably not
* a good idea, as we don't want to make it harder to cross compile Janus
*
* TODO Look into what libssl and/or libcrypto provide in that respect
*
* PS: JavaScript only supports integer up to 2^53, so we need to
* make sure the number is below 9007199254740992 for safety
*/
guint64 num = g_random_int() & 0x1FFFFF;
num = (num << 32) | g_random_int();
return num;
guint64 ret = 0;
if (RAND_bytes((void *)&ret, sizeof(ret)) != 1) {
JANUS_LOG(LOG_FATAL, "\tOpenSSL RAND_bytes() failed\n");
exit(1);
}
return ret;
}

guint64 janus_random_uint64_javacript_safe(void) {
return janus_random_uint64() & 0x1FFFFFFFFFFFFF;
}

char *janus_random_uuid(void) {
Expand Down
9 changes: 9 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ guint32 janus_random_uint32(void);
* @returns A random 64-bit unsigned integer */
guint64 janus_random_uint64(void);

/*! \brief Helper to generate random 64-bit unsigned integers which are safe to use in Javascript
* @note Javascript does not have real integers, its builtin "number" type is a float64.
* Thus, only integer values up to Number.MAX_SAFE_INTEGER == 2^53 - 1 == 9007199254740991
* can be safely represented in Javascript. This method returns such numbers.
* Use this method instead of janus_random_uint64() whenever you generate numbers which
* might end up in Javascript (via JSON API).
* @returns A random 64-bit unsigned integer */
guint64 janus_random_uint64_javacript_safe(void);

/*! \brief Helper to generate random UUIDs (needed by some plugins)
* @returns A random UUID string, which must be deallocated with \c g_free */
char *janus_random_uuid(void);
Expand Down