diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 87c98089..f55f40a1 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -32,6 +32,8 @@ | |||
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_id_valid(int crypt_connection_id, Net_Crypto *c) { return crypt_connection_id < 0 || (uint32_t)crypt_connection_id >= c->crypto_connections_length; } | ||
36 | |||
35 | /* Use this instead of memcmp; not vulnerable to timing attacks. */ | 37 | /* Use this instead of memcmp; not vulnerable to timing attacks. */ |
36 | uint8_t crypto_iszero(uint8_t *mem, uint32_t length) | 38 | uint8_t crypto_iszero(uint8_t *mem, uint32_t length) |
37 | { | 39 | { |
@@ -150,7 +152,7 @@ void random_nonce(uint8_t *nonce) | |||
150 | */ | 152 | */ |
151 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | 153 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) |
152 | { | 154 | { |
153 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) | 155 | if (crypt_id_valid(crypt_connection_id,c)) |
154 | return 0; | 156 | return 0; |
155 | 157 | ||
156 | if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) | 158 | if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) |
@@ -182,7 +184,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | |||
182 | */ | 184 | */ |
183 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) | 185 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) |
184 | { | 186 | { |
185 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) | 187 | if (crypt_id_valid(crypt_connection_id,c)) |
186 | return 0; | 188 | return 0; |
187 | 189 | ||
188 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) | 190 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) |
@@ -304,7 +306,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
304 | len); | 306 | len); |
305 | 307 | ||
306 | } else { /* If request is not for us, try routing it. */ | 308 | } else { /* If request is not for us, try routing it. */ |
307 | if (route_packet(dht, packet + 1, packet, length) == length) | 309 | int retval = route_packet(dht, packet + 1, packet, length); |
310 | if (retval < 0 || (uint32_t)retval == length) | ||
308 | return 0; | 311 | return 0; |
309 | } | 312 | } |
310 | } | 313 | } |
@@ -505,7 +508,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui | |||
505 | */ | 508 | */ |
506 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) | 509 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) |
507 | { | 510 | { |
508 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) | 511 | if (crypt_id_valid(crypt_connection_id,c)) |
509 | return 1; | 512 | return 1; |
510 | 513 | ||
511 | if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { | 514 | if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { |
@@ -596,7 +599,7 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
596 | */ | 599 | */ |
597 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) | 600 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) |
598 | { | 601 | { |
599 | if (crypt_connection_id >= 0 && crypt_connection_id < c->crypto_connections_length) | 602 | if (crypt_connection_id >= 0 && (uint32_t)crypt_connection_id < c->crypto_connections_length) |
600 | return c->crypto_connections[crypt_connection_id].status; | 603 | return c->crypto_connections[crypt_connection_id].status; |
601 | 604 | ||
602 | return CONN_NO_CONNECTION; | 605 | return CONN_NO_CONNECTION; |