diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 739003a4..59a068c8 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -32,6 +32,11 @@ | |||
32 | #define CONN_ESTABLISHED 3 | 32 | #define CONN_ESTABLISHED 3 |
33 | #define CONN_TIMED_OUT 4 | 33 | #define CONN_TIMED_OUT 4 |
34 | 34 | ||
35 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) | ||
36 | { | ||
37 | return (uint32_t)crypt_connection_id >= c->crypto_connections_length; | ||
38 | } | ||
39 | |||
35 | /* Use this instead of memcmp; not vulnerable to timing attacks. */ | 40 | /* Use this instead of memcmp; not vulnerable to timing attacks. */ |
36 | uint8_t crypto_iszero(uint8_t *mem, uint32_t length) | 41 | uint8_t crypto_iszero(uint8_t *mem, uint32_t length) |
37 | { | 42 | { |
@@ -150,7 +155,7 @@ void random_nonce(uint8_t *nonce) | |||
150 | */ | 155 | */ |
151 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | 156 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) |
152 | { | 157 | { |
153 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) | 158 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) |
154 | return 0; | 159 | return 0; |
155 | 160 | ||
156 | if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) | 161 | if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) |
@@ -182,7 +187,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | |||
182 | */ | 187 | */ |
183 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) | 188 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) |
184 | { | 189 | { |
185 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) | 190 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) |
186 | return 0; | 191 | return 0; |
187 | 192 | ||
188 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) | 193 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) |
@@ -305,7 +310,9 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
305 | len); | 310 | len); |
306 | 311 | ||
307 | } else { /* If request is not for us, try routing it. */ | 312 | } else { /* If request is not for us, try routing it. */ |
308 | if (route_packet(dht, packet + 1, packet, length) == length) | 313 | int retval = route_packet(dht, packet + 1, packet, length); |
314 | |||
315 | if ((unsigned int)retval == length) | ||
309 | return 0; | 316 | return 0; |
310 | } | 317 | } |
311 | } | 318 | } |
@@ -512,7 +519,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui | |||
512 | */ | 519 | */ |
513 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) | 520 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) |
514 | { | 521 | { |
515 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) | 522 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) |
516 | return 1; | 523 | return 1; |
517 | 524 | ||
518 | if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { | 525 | if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { |
@@ -604,7 +611,7 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
604 | */ | 611 | */ |
605 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) | 612 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) |
606 | { | 613 | { |
607 | if (crypt_connection_id >= 0 && crypt_connection_id < c->crypto_connections_length) | 614 | if ((unsigned int)crypt_connection_id < c->crypto_connections_length) |
608 | return c->crypto_connections[crypt_connection_id].status; | 615 | return c->crypto_connections[crypt_connection_id].status; |
609 | 616 | ||
610 | return CONN_NO_CONNECTION; | 617 | return CONN_NO_CONNECTION; |