summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c6
-rw-r--r--toxcore/net_crypto.c45
-rw-r--r--toxcore/net_crypto.h6
3 files changed, 32 insertions, 25 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 749eb78c..4807c369 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -483,12 +483,12 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli
483{ 483{
484 /* Check if packet is going to be sent to ourself. */ 484 /* Check if packet is going to be sent to ourself. */
485 if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0)) 485 if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0))
486 return 1; 486 return -1;
487 487
488 uint64_t ping_id = add_gettingnodes(dht, ip_port); 488 uint64_t ping_id = add_gettingnodes(dht, ip_port);
489 489
490 if (ping_id == 0) 490 if (ping_id == 0)
491 return 1; 491 return -1;
492 492
493 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; 493 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
494 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; 494 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
@@ -522,7 +522,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
522{ 522{
523 /* Check if packet is going to be sent to ourself. */ 523 /* Check if packet is going to be sent to ourself. */
524 if (id_equal(public_key, dht->c->self_public_key)) 524 if (id_equal(public_key, dht->c->self_public_key))
525 return 1; 525 return -1;
526 526
527 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) 527 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
528 + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; 528 + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING];
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 92727a8c..cfa1bc34 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -254,30 +254,31 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *
254 * 254 *
255 * return -1 if not valid request. 255 * return -1 if not valid request.
256 */ 256 */
257static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, 257int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
258 uint16_t length) 258 uint8_t *request_id, uint8_t *packet, uint16_t length)
259{ 259{
260
261 if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && 260 if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
262 length <= MAX_DATA_SIZE + ENCRYPTION_PADDING && 261 length <= MAX_DATA_SIZE) {
263 memcmp(packet + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { 262 if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
264 memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); 263 memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
265 uint8_t nonce[crypto_box_NONCEBYTES]; 264 uint8_t nonce[crypto_box_NONCEBYTES];
266 uint8_t temp[MAX_DATA_SIZE]; 265 uint8_t temp[MAX_DATA_SIZE];
267 memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES); 266 memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
268 int len1 = decrypt_data(public_key, c->self_secret_key, nonce, 267 int len1 = decrypt_data(public_key, self_secret_key, nonce,
269 packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES, 268 packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
270 length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp); 269 length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp);
271 270
272 if (len1 == -1 || len1 == 0) 271 if (len1 == -1 || len1 == 0)
273 return -1; 272 return -1;
274 273
275 request_id[0] = temp[0]; 274 request_id[0] = temp[0];
276 --len1; 275 --len1;
277 memcpy(data, temp + 1, len1); 276 memcpy(data, temp + 1, len1);
278 return len1; 277 return len1;
279 } else 278 }
280 return -1; 279 }
280
281 return -1;
281} 282}
282 283
283void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object) 284void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object)
@@ -299,7 +300,7 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui
299 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 300 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
300 uint8_t data[MAX_DATA_SIZE]; 301 uint8_t data[MAX_DATA_SIZE];
301 uint8_t number; 302 uint8_t number;
302 int len = handle_request(dht->c, public_key, data, &number, packet, length); 303 int len = handle_request(dht->c->self_public_key, dht->c->self_secret_key, public_key, data, &number, packet, length);
303 304
304 if (len == -1 || len == 0) 305 if (len == -1 || len == 0)
305 return 1; 306 return 1;
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 030cc678..e5dfcae0 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -139,6 +139,12 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
139int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, 139int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
140 uint8_t *data, uint32_t length, uint8_t request_id); 140 uint8_t *data, uint32_t length, uint8_t request_id);
141 141
142/* puts the senders public key in the request in public_key, the data from the request
143 in data if a friend or ping request was sent to us and returns the length of the data.
144 packet is the request packet and length is its length
145 return -1 if not valid request. */
146int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
147 uint8_t *request_id, uint8_t *packet, uint16_t length);
142 148
143/* Function to call when request beginning with byte is received. */ 149/* Function to call when request beginning with byte is received. */
144void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object); 150void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object);