summaryrefslogtreecommitdiff
path: root/toxcore/onion.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/onion.c')
-rw-r--r--toxcore/onion.c40
1 files changed, 22 insertions, 18 deletions
diff --git a/toxcore/onion.c b/toxcore/onion.c
index c2ca21f8..8cc851c2 100644
--- a/toxcore/onion.c
+++ b/toxcore/onion.c
@@ -26,8 +26,6 @@
26#include "onion.h" 26#include "onion.h"
27#include "util.h" 27#include "util.h"
28 28
29#define MAX_ONION_SIZE MAX_DATA_SIZE
30
31#define RETURN_1 ONION_RETURN_1 29#define RETURN_1 ONION_RETURN_1
32#define RETURN_2 ONION_RETURN_2 30#define RETURN_2 ONION_RETURN_2
33#define RETURN_3 ONION_RETURN_3 31#define RETURN_3 ONION_RETURN_3
@@ -89,13 +87,14 @@ int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes)
89/* Create and send a onion packet. 87/* Create and send a onion packet.
90 * 88 *
91 * Use Onion_Path path to send data of length to dest. 89 * Use Onion_Path path to send data of length to dest.
90 * Maximum length of data is ONION_MAX_DATA_SIZE.
92 * 91 *
93 * return -1 on failure. 92 * return -1 on failure.
94 * return 0 on success. 93 * return 0 on success.
95 */ 94 */
96int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length) 95int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length)
97{ 96{
98 if (1 + length + SEND_1 > MAX_ONION_SIZE || length == 0) 97 if (1 + length + SEND_1 > ONION_MAX_PACKET_SIZE || length == 0)
99 return -1; 98 return -1;
100 99
101 to_net_family(&dest.ip); 100 to_net_family(&dest.ip);
@@ -142,13 +141,18 @@ int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint
142 141
143 return 0; 142 return 0;
144} 143}
144
145/* Create and send a onion response sent initially to dest with. 145/* Create and send a onion response sent initially to dest with.
146 * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
146 * 147 *
147 * return -1 on failure. 148 * return -1 on failure.
148 * return 0 on success. 149 * return 0 on success.
149 */ 150 */
150int send_onion_response(Networking_Core *net, IP_Port dest, uint8_t *data, uint32_t length, uint8_t *ret) 151int send_onion_response(Networking_Core *net, IP_Port dest, uint8_t *data, uint32_t length, uint8_t *ret)
151{ 152{
153 if (length > ONION_RESPONSE_MAX_DATA_SIZE || length == 0)
154 return -1;
155
152 uint8_t packet[1 + RETURN_3 + length]; 156 uint8_t packet[1 + RETURN_3 + length];
153 packet[0] = NET_PACKET_ONION_RECV_3; 157 packet[0] = NET_PACKET_ONION_RECV_3;
154 memcpy(packet + 1, ret, RETURN_3); 158 memcpy(packet + 1, ret, RETURN_3);
@@ -164,7 +168,7 @@ static int handle_send_initial(void *object, IP_Port source, uint8_t *packet, ui
164{ 168{
165 Onion *onion = object; 169 Onion *onion = object;
166 170
167 if (length > MAX_ONION_SIZE) 171 if (length > ONION_MAX_PACKET_SIZE)
168 return 1; 172 return 1;
169 173
170 if (length <= 1 + SEND_1) 174 if (length <= 1 + SEND_1)
@@ -172,7 +176,7 @@ static int handle_send_initial(void *object, IP_Port source, uint8_t *packet, ui
172 176
173 change_symmetric_key(onion); 177 change_symmetric_key(onion);
174 178
175 uint8_t plain[MAX_ONION_SIZE]; 179 uint8_t plain[ONION_MAX_PACKET_SIZE];
176 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 180 uint8_t shared_key[crypto_box_BEFORENMBYTES];
177 get_shared_key(&onion->shared_keys_1, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES); 181 get_shared_key(&onion->shared_keys_1, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES);
178 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 182 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
@@ -190,7 +194,7 @@ int onion_send_1(Onion *onion, uint8_t *plain, uint32_t len, IP_Port source, uin
190 memcpy(&send_to, plain, sizeof(IP_Port)); 194 memcpy(&send_to, plain, sizeof(IP_Port));
191 to_host_family(&send_to.ip); 195 to_host_family(&send_to.ip);
192 196
193 uint8_t data[MAX_ONION_SIZE]; 197 uint8_t data[ONION_MAX_PACKET_SIZE];
194 data[0] = NET_PACKET_ONION_SEND_1; 198 data[0] = NET_PACKET_ONION_SEND_1;
195 memcpy(data + 1, nonce, crypto_box_NONCEBYTES); 199 memcpy(data + 1, nonce, crypto_box_NONCEBYTES);
196 memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port)); 200 memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port));
@@ -215,7 +219,7 @@ static int handle_send_1(void *object, IP_Port source, uint8_t *packet, uint32_t
215{ 219{
216 Onion *onion = object; 220 Onion *onion = object;
217 221
218 if (length > MAX_ONION_SIZE) 222 if (length > ONION_MAX_PACKET_SIZE)
219 return 1; 223 return 1;
220 224
221 if (length <= 1 + SEND_2) 225 if (length <= 1 + SEND_2)
@@ -223,7 +227,7 @@ static int handle_send_1(void *object, IP_Port source, uint8_t *packet, uint32_t
223 227
224 change_symmetric_key(onion); 228 change_symmetric_key(onion);
225 229
226 uint8_t plain[MAX_ONION_SIZE]; 230 uint8_t plain[ONION_MAX_PACKET_SIZE];
227 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 231 uint8_t shared_key[crypto_box_BEFORENMBYTES];
228 get_shared_key(&onion->shared_keys_2, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES); 232 get_shared_key(&onion->shared_keys_2, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES);
229 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 233 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
@@ -236,7 +240,7 @@ static int handle_send_1(void *object, IP_Port source, uint8_t *packet, uint32_t
236 memcpy(&send_to, plain, sizeof(IP_Port)); 240 memcpy(&send_to, plain, sizeof(IP_Port));
237 to_host_family(&send_to.ip); 241 to_host_family(&send_to.ip);
238 242
239 uint8_t data[MAX_ONION_SIZE]; 243 uint8_t data[ONION_MAX_PACKET_SIZE];
240 data[0] = NET_PACKET_ONION_SEND_2; 244 data[0] = NET_PACKET_ONION_SEND_2;
241 memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES); 245 memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES);
242 memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port)); 246 memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port));
@@ -264,7 +268,7 @@ static int handle_send_2(void *object, IP_Port source, uint8_t *packet, uint32_t
264{ 268{
265 Onion *onion = object; 269 Onion *onion = object;
266 270
267 if (length > MAX_ONION_SIZE) 271 if (length > ONION_MAX_PACKET_SIZE)
268 return 1; 272 return 1;
269 273
270 if (length <= 1 + SEND_3) 274 if (length <= 1 + SEND_3)
@@ -272,7 +276,7 @@ static int handle_send_2(void *object, IP_Port source, uint8_t *packet, uint32_t
272 276
273 change_symmetric_key(onion); 277 change_symmetric_key(onion);
274 278
275 uint8_t plain[MAX_ONION_SIZE]; 279 uint8_t plain[ONION_MAX_PACKET_SIZE];
276 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 280 uint8_t shared_key[crypto_box_BEFORENMBYTES];
277 get_shared_key(&onion->shared_keys_3, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES); 281 get_shared_key(&onion->shared_keys_3, shared_key, onion->dht->self_secret_key, packet + 1 + crypto_box_NONCEBYTES);
278 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 282 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
@@ -285,7 +289,7 @@ static int handle_send_2(void *object, IP_Port source, uint8_t *packet, uint32_t
285 memcpy(&send_to, plain, sizeof(IP_Port)); 289 memcpy(&send_to, plain, sizeof(IP_Port));
286 to_host_family(&send_to.ip); 290 to_host_family(&send_to.ip);
287 291
288 uint8_t data[MAX_ONION_SIZE]; 292 uint8_t data[ONION_MAX_PACKET_SIZE];
289 memcpy(data, plain + sizeof(IP_Port), len - sizeof(IP_Port)); 293 memcpy(data, plain + sizeof(IP_Port), len - sizeof(IP_Port));
290 uint32_t data_len = (len - sizeof(IP_Port)); 294 uint32_t data_len = (len - sizeof(IP_Port));
291 uint8_t *ret_part = data + (len - sizeof(IP_Port)); 295 uint8_t *ret_part = data + (len - sizeof(IP_Port));
@@ -312,7 +316,7 @@ static int handle_recv_3(void *object, IP_Port source, uint8_t *packet, uint32_t
312{ 316{
313 Onion *onion = object; 317 Onion *onion = object;
314 318
315 if (length > MAX_ONION_SIZE) 319 if (length > ONION_MAX_PACKET_SIZE)
316 return 1; 320 return 1;
317 321
318 if (length <= 1 + RETURN_3) 322 if (length <= 1 + RETURN_3)
@@ -330,7 +334,7 @@ static int handle_recv_3(void *object, IP_Port source, uint8_t *packet, uint32_t
330 IP_Port send_to; 334 IP_Port send_to;
331 memcpy(&send_to, plain, sizeof(IP_Port)); 335 memcpy(&send_to, plain, sizeof(IP_Port));
332 336
333 uint8_t data[MAX_ONION_SIZE]; 337 uint8_t data[ONION_MAX_PACKET_SIZE];
334 data[0] = NET_PACKET_ONION_RECV_2; 338 data[0] = NET_PACKET_ONION_RECV_2;
335 memcpy(data + 1, plain + sizeof(IP_Port), RETURN_2); 339 memcpy(data + 1, plain + sizeof(IP_Port), RETURN_2);
336 memcpy(data + 1 + RETURN_2, packet + 1 + RETURN_3, length - (1 + RETURN_3)); 340 memcpy(data + 1 + RETURN_2, packet + 1 + RETURN_3, length - (1 + RETURN_3));
@@ -346,7 +350,7 @@ static int handle_recv_2(void *object, IP_Port source, uint8_t *packet, uint32_t
346{ 350{
347 Onion *onion = object; 351 Onion *onion = object;
348 352
349 if (length > MAX_ONION_SIZE) 353 if (length > ONION_MAX_PACKET_SIZE)
350 return 1; 354 return 1;
351 355
352 if (length <= 1 + RETURN_2) 356 if (length <= 1 + RETURN_2)
@@ -364,7 +368,7 @@ static int handle_recv_2(void *object, IP_Port source, uint8_t *packet, uint32_t
364 IP_Port send_to; 368 IP_Port send_to;
365 memcpy(&send_to, plain, sizeof(IP_Port)); 369 memcpy(&send_to, plain, sizeof(IP_Port));
366 370
367 uint8_t data[MAX_ONION_SIZE]; 371 uint8_t data[ONION_MAX_PACKET_SIZE];
368 data[0] = NET_PACKET_ONION_RECV_1; 372 data[0] = NET_PACKET_ONION_RECV_1;
369 memcpy(data + 1, plain + sizeof(IP_Port), RETURN_1); 373 memcpy(data + 1, plain + sizeof(IP_Port), RETURN_1);
370 memcpy(data + 1 + RETURN_1, packet + 1 + RETURN_2, length - (1 + RETURN_2)); 374 memcpy(data + 1 + RETURN_1, packet + 1 + RETURN_2, length - (1 + RETURN_2));
@@ -380,7 +384,7 @@ static int handle_recv_1(void *object, IP_Port source, uint8_t *packet, uint32_t
380{ 384{
381 Onion *onion = object; 385 Onion *onion = object;
382 386
383 if (length > MAX_ONION_SIZE) 387 if (length > ONION_MAX_PACKET_SIZE)
384 return 1; 388 return 1;
385 389
386 if (length <= 1 + RETURN_1) 390 if (length <= 1 + RETURN_1)
@@ -424,7 +428,7 @@ Onion *new_onion(DHT *dht)
424 return NULL; 428 return NULL;
425 429
426 onion->dht = dht; 430 onion->dht = dht;
427 onion->net = dht->c->lossless_udp->net; 431 onion->net = dht->net;
428 new_symmetric_key(onion->secret_symmetric_key); 432 new_symmetric_key(onion->secret_symmetric_key);
429 onion->timestamp = unix_time(); 433 onion->timestamp = unix_time();
430 434