diff --git a/lib/connect.c b/lib/connect.c index f2804002..ee00bf95 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -28,6 +28,7 @@ Contributors: #include "net_mosq.h" #include "send_mosq.h" #include "socks_mosq.h" +#include "util_mosq.h" static int mosquitto__reconnect(struct mosquitto *mosq, bool blocking, const mosquitto_property *properties); static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address); @@ -36,6 +37,7 @@ static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address) { int i; + int rc; if(!mosq) return MOSQ_ERR_INVAL; if(!host || port <= 0) return MOSQ_ERR_INVAL; @@ -51,8 +53,11 @@ static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int mosq->id[3] = 'q'; mosq->id[4] = '/'; + rc = util__random_bytes(&mosq->id[5], 18); + if(rc) return rc; + for(i=5; i<23; i++){ - mosq->id[i] = (random()%73)+48; + mosq->id[i] = (mosq->id[i]%73)+48; } } diff --git a/lib/util_mosq.c b/lib/util_mosq.c index 3ec471da..5ef19b2a 100644 --- a/lib/util_mosq.c +++ b/lib/util_mosq.c @@ -28,8 +28,13 @@ Contributors: # include #endif +#ifdef __linux__ +# include +#endif + #ifdef WITH_TLS # include +# include #endif #ifdef WITH_BROKER @@ -295,3 +300,41 @@ void util__increment_send_quota(struct mosquitto *mosq) mosq->send_quota++; } } + + +int util__random_bytes(void *bytes, int count) +{ + int rc = MOSQ_ERR_UNKNOWN; + +#ifdef WITH_TLS + if(RAND_bytes(bytes, count) == 1){ + rc = MOSQ_ERR_SUCCESS; + } +#else +# ifdef __GLIBC__ + if(getrandom(bytes, count, 0) == 0){ + rc = MOSQ_ERR_SUCCESS; + } +# elif defined(WIN32) + HRYPTPROV provider; + + if(!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)){ + return MOSQ_ERR_UNKNOWN; + } + + if(CryptGenRandom(provider, count, bytes)){ + rc = MOSQ_ERR_SUCCESS; + } + + CryptReleaseContext(provider, 0); +# else + int i; + + for(i=0; i -#endif - -#ifdef __linux__ -# include -#endif - #ifdef WITH_WEBSOCKETS # include #endif -static int random_16_bytes(uint8_t *bytes) -{ - int rc = MOSQ_ERR_UNKNOWN; - -#ifdef WITH_TLS - if(RAND_bytes(bytes, 16) == 1){ - rc = MOSQ_ERR_SUCCESS; - } -#else -# ifdef __GLIBC__ - if(getrandom(bytes, 16, 0) == 0){ - rc = MOSQ_ERR_SUCCESS; - } -# elif defined(WIN32) - HRYPTPROV provider; - - if(!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)){ - return MOSQ_ERR_UNKNOWN; - } - - if(CryptGenRandom(provider, 16, bytes)){ - rc = MOSQ_ERR_SUCCESS; - } - - CryptReleaseContext(provider, 0); -# else - int i; - - for(i=0; i<16; i++){ - bytes[i] = (uint8_t )(random()&0xFF); - } - rc = MOSQ_ERR_SUCCESS; -# endif -#endif - return rc; -} - static char nibble_to_hex(uint8_t value) { if(value < 0x0A){ @@ -96,7 +51,7 @@ static char *client_id_gen(struct mosquitto_db *db, int *idlen, const char *auto int i; int pos; - if(random_16_bytes(rnd)) return NULL; + if(util__random_bytes(rnd, 16)) return NULL; *idlen = 36 + auto_id_prefix_len;