Coverage Report

Created: 2025-10-08 19:34

/work/toxcore/onion.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2025 The TokTok team.
3
 * Copyright © 2013 Tox project.
4
 */
5
6
/**
7
 * Implementation of the onion part of docs/Prevent_Tracking.txt
8
 */
9
#include "onion.h"
10
11
#include <assert.h>
12
#include <string.h>
13
14
#include "DHT.h"
15
#include "attributes.h"
16
#include "ccompat.h"
17
#include "crypto_core.h"
18
#include "logger.h"
19
#include "mem.h"
20
#include "mono_time.h"
21
#include "network.h"
22
#include "shared_key_cache.h"
23
#include "util.h"
24
25
767k
#define RETURN_1 ONION_RETURN_1
26
890k
#define RETURN_2 ONION_RETURN_2
27
630k
#define RETURN_3 ONION_RETURN_3
28
29
360k
#define SEND_BASE ONION_SEND_BASE
30
65.4k
#define SEND_3 ONION_SEND_3
31
67.3k
#define SEND_2 ONION_SEND_2
32
141k
#define SEND_1 ONION_SEND_1
33
34
389k
#define KEY_REFRESH_INTERVAL (2 * 60 * 60)
35
36
// Settings for the shared key cache
37
8.64k
#define MAX_KEYS_PER_SLOT 4
38
8.64k
#define KEYS_TIMEOUT 600
39
40
/** Change symmetric keys every 2 hours to make paths expire eventually. */
41
static void change_symmetric_key(Onion *_Nonnull onion)
42
389k
{
43
389k
    if (mono_time_is_timeout(onion->mono_time, onion->timestamp, KEY_REFRESH_INTERVAL)) {
44
0
        new_symmetric_key(onion->rng, onion->secret_symmetric_key);
45
0
        onion->timestamp = mono_time_get(onion->mono_time);
46
0
    }
47
389k
}
48
49
/** packing and unpacking functions */
50
static void ip_pack_to_bytes(uint8_t *_Nonnull data, const IP *_Nonnull source)
51
420k
{
52
420k
    data[0] = source->family.value;
53
54
420k
    if (net_family_is_ipv4(source->family) || net_family_is_tox_tcp_ipv4(source->family)) {
55
419k
        memzero(data + 1, SIZE_IP6);
56
419k
        memcpy(data + 1, source->ip.v4.uint8, SIZE_IP4);
57
419k
    } else {
58
990
        memcpy(data + 1, source->ip.v6.uint8, SIZE_IP6);
59
990
    }
60
420k
}
61
62
/** return 0 on success, -1 on failure. */
63
static int ip_unpack_from_bytes(IP *_Nonnull target, const uint8_t *_Nonnull data, unsigned int data_size, bool disable_family_check)
64
388k
{
65
388k
    if (data_size < (1 + SIZE_IP6)) {
66
0
        return -1;
67
0
    }
68
69
    // TODO(iphydf): Validate input.
70
388k
    target->family.value = data[0];
71
72
388k
    if (net_family_is_ipv4(target->family) || net_family_is_tox_tcp_ipv4(target->family)) {
73
387k
        memcpy(target->ip.v4.uint8, data + 1, SIZE_IP4);
74
387k
    } else {
75
803
        memcpy(target->ip.v6.uint8, data + 1, SIZE_IP6);
76
803
    }
77
78
388k
    const bool valid = disable_family_check ||
79
388k
                       net_family_is_ipv4(target->family) ||
80
388k
                       net_family_is_ipv6(target->family);
81
82
388k
    return valid ? 0 : -1;
83
388k
}
84
85
static void ipport_pack(uint8_t *_Nonnull data, const IP_Port *_Nonnull source)
86
420k
{
87
420k
    ip_pack_to_bytes(data, &source->ip);
88
420k
    memcpy(data + SIZE_IP, &source->port, SIZE_PORT);
89
420k
}
90
91
/** return 0 on success, -1 on failure. */
92
static int ipport_unpack(IP_Port *_Nonnull target, const uint8_t *_Nonnull data, unsigned int data_size, bool disable_family_check)
93
388k
{
94
388k
    if (data_size < (SIZE_IP + SIZE_PORT)) {
95
0
        return -1;
96
0
    }
97
98
388k
    if (ip_unpack_from_bytes(&target->ip, data, data_size, disable_family_check) == -1) {
99
0
        return -1;
100
0
    }
101
102
388k
    memcpy(&target->port, data + SIZE_IP, SIZE_PORT);
103
388k
    return 0;
104
388k
}
105
106
/** @brief Create a new onion path.
107
 *
108
 * Create a new onion path out of nodes (nodes is a list of ONION_PATH_LENGTH nodes)
109
 *
110
 * new_path must be an empty memory location of at least Onion_Path size.
111
 *
112
 * return -1 on failure.
113
 * return 0 on success.
114
 */
115
int create_onion_path(const Random *rng, const DHT *dht, Onion_Path *new_path, const Node_format *nodes)
116
7.37k
{
117
7.37k
    if (new_path == nullptr || nodes == nullptr) {
118
0
        return -1;
119
0
    }
120
121
7.37k
    encrypt_precompute(nodes[0].public_key, dht_get_self_secret_key(dht), new_path->shared_key1);
122
7.37k
    memcpy(new_path->public_key1, dht_get_self_public_key(dht), CRYPTO_PUBLIC_KEY_SIZE);
123
124
7.37k
    uint8_t random_public_key[CRYPTO_PUBLIC_KEY_SIZE];
125
7.37k
    uint8_t random_secret_key[CRYPTO_SECRET_KEY_SIZE];
126
127
7.37k
    crypto_new_keypair(rng, random_public_key, random_secret_key);
128
7.37k
    encrypt_precompute(nodes[1].public_key, random_secret_key, new_path->shared_key2);
129
7.37k
    memcpy(new_path->public_key2, random_public_key, CRYPTO_PUBLIC_KEY_SIZE);
130
131
7.37k
    crypto_new_keypair(rng, random_public_key, random_secret_key);
132
7.37k
    encrypt_precompute(nodes[2].public_key, random_secret_key, new_path->shared_key3);
133
7.37k
    memcpy(new_path->public_key3, random_public_key, CRYPTO_PUBLIC_KEY_SIZE);
134
135
7.37k
    crypto_memzero(random_secret_key, sizeof(random_secret_key));
136
137
7.37k
    new_path->ip_port1 = nodes[0].ip_port;
138
7.37k
    new_path->ip_port2 = nodes[1].ip_port;
139
7.37k
    new_path->ip_port3 = nodes[2].ip_port;
140
141
7.37k
    memcpy(new_path->node_public_key1, nodes[0].public_key, CRYPTO_PUBLIC_KEY_SIZE);
142
7.37k
    memcpy(new_path->node_public_key2, nodes[1].public_key, CRYPTO_PUBLIC_KEY_SIZE);
143
7.37k
    memcpy(new_path->node_public_key3, nodes[2].public_key, CRYPTO_PUBLIC_KEY_SIZE);
144
145
7.37k
    return 0;
146
7.37k
}
147
148
/** @brief Dump nodes in onion path to nodes of length num_nodes.
149
 *
150
 * return -1 on failure.
151
 * return 0 on success.
152
 */
153
int onion_path_to_nodes(Node_format *nodes, unsigned int num_nodes, const Onion_Path *path)
154
55.4k
{
155
55.4k
    if (num_nodes < ONION_PATH_LENGTH) {
156
0
        return -1;
157
0
    }
158
159
55.4k
    nodes[0].ip_port = path->ip_port1;
160
55.4k
    nodes[1].ip_port = path->ip_port2;
161
55.4k
    nodes[2].ip_port = path->ip_port3;
162
163
55.4k
    memcpy(nodes[0].public_key, path->node_public_key1, CRYPTO_PUBLIC_KEY_SIZE);
164
55.4k
    memcpy(nodes[1].public_key, path->node_public_key2, CRYPTO_PUBLIC_KEY_SIZE);
165
55.4k
    memcpy(nodes[2].public_key, path->node_public_key3, CRYPTO_PUBLIC_KEY_SIZE);
166
55.4k
    return 0;
167
55.4k
}
168
169
/** @brief Create a onion packet.
170
 *
171
 * Use Onion_Path path to create packet for data of length to dest.
172
 * Maximum length of data is ONION_MAX_DATA_SIZE.
173
 * packet should be at least ONION_MAX_PACKET_SIZE big.
174
 *
175
 * return -1 on failure.
176
 * return length of created packet on success.
177
 */
178
int create_onion_packet(const Memory *mem, const Random *rng, uint8_t *packet, uint16_t max_packet_length,
179
                        const Onion_Path *path, const IP_Port *dest,
180
                        const uint8_t *data, uint16_t length)
181
71.9k
{
182
71.9k
    if (1 + length + SEND_1 > max_packet_length || length == 0) {
183
0
        return -1;
184
0
    }
185
186
71.9k
    const uint16_t step1_size = SIZE_IPPORT + length;
187
71.9k
    VLA(uint8_t, step1, step1_size);
188
189
71.9k
    ipport_pack(step1, dest);
190
71.9k
    memcpy(step1 + SIZE_IPPORT, data, length);
191
192
71.9k
    uint8_t nonce[CRYPTO_NONCE_SIZE];
193
71.9k
    random_nonce(rng, nonce);
194
195
71.9k
    const uint16_t step2_size = SIZE_IPPORT + SEND_BASE + length;
196
71.9k
    VLA(uint8_t, step2, step2_size);
197
71.9k
    ipport_pack(step2, &path->ip_port3);
198
71.9k
    memcpy(step2 + SIZE_IPPORT, path->public_key3, CRYPTO_PUBLIC_KEY_SIZE);
199
200
71.9k
    int len = encrypt_data_symmetric(mem, path->shared_key3, nonce, step1, step1_size,
201
71.9k
                                     step2 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
202
203
71.9k
    if (len != SIZE_IPPORT + length + CRYPTO_MAC_SIZE) {
204
39
        return -1;
205
39
    }
206
207
71.9k
    const uint16_t step3_size = SIZE_IPPORT + SEND_BASE * 2 + length;
208
71.9k
    VLA(uint8_t, step3, step3_size);
209
71.9k
    ipport_pack(step3, &path->ip_port2);
210
71.9k
    memcpy(step3 + SIZE_IPPORT, path->public_key2, CRYPTO_PUBLIC_KEY_SIZE);
211
71.9k
    len = encrypt_data_symmetric(mem, path->shared_key2, nonce, step2, step2_size,
212
71.9k
                                 step3 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
213
214
71.9k
    if (len != SIZE_IPPORT + SEND_BASE + length + CRYPTO_MAC_SIZE) {
215
38
        return -1;
216
38
    }
217
218
71.9k
    packet[0] = NET_PACKET_ONION_SEND_INITIAL;
219
71.9k
    memcpy(packet + 1, nonce, CRYPTO_NONCE_SIZE);
220
71.9k
    memcpy(packet + 1 + CRYPTO_NONCE_SIZE, path->public_key1, CRYPTO_PUBLIC_KEY_SIZE);
221
222
71.9k
    len = encrypt_data_symmetric(mem, path->shared_key1, nonce, step3, step3_size,
223
71.9k
                                 packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE);
224
225
71.9k
    if (len != SIZE_IPPORT + SEND_BASE * 2 + length + CRYPTO_MAC_SIZE) {
226
38
        return -1;
227
38
    }
228
229
71.8k
    return 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + len;
230
71.9k
}
231
232
/** @brief Create a onion packet to be sent over tcp.
233
 *
234
 * Use Onion_Path path to create packet for data of length to dest.
235
 * Maximum length of data is ONION_MAX_DATA_SIZE.
236
 * packet should be at least ONION_MAX_PACKET_SIZE big.
237
 *
238
 * return -1 on failure.
239
 * return length of created packet on success.
240
 */
241
int create_onion_packet_tcp(const Memory *mem, const Random *rng, uint8_t *packet, uint16_t max_packet_length,
242
                            const Onion_Path *path, const IP_Port *dest,
243
                            const uint8_t *data, uint16_t length)
244
1.00k
{
245
1.00k
    if (CRYPTO_NONCE_SIZE + SIZE_IPPORT + SEND_BASE * 2 + length > max_packet_length || length == 0) {
246
0
        return -1;
247
0
    }
248
249
1.00k
    const uint16_t step1_size = SIZE_IPPORT + length;
250
1.00k
    VLA(uint8_t, step1, step1_size);
251
252
1.00k
    ipport_pack(step1, dest);
253
1.00k
    memcpy(step1 + SIZE_IPPORT, data, length);
254
255
1.00k
    uint8_t nonce[CRYPTO_NONCE_SIZE];
256
1.00k
    random_nonce(rng, nonce);
257
258
1.00k
    const uint16_t step2_size = SIZE_IPPORT + SEND_BASE + length;
259
1.00k
    VLA(uint8_t, step2, step2_size);
260
1.00k
    ipport_pack(step2, &path->ip_port3);
261
1.00k
    memcpy(step2 + SIZE_IPPORT, path->public_key3, CRYPTO_PUBLIC_KEY_SIZE);
262
263
1.00k
    int len = encrypt_data_symmetric(mem, path->shared_key3, nonce, step1, step1_size,
264
1.00k
                                     step2 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
265
266
1.00k
    if (len != SIZE_IPPORT + length + CRYPTO_MAC_SIZE) {
267
0
        return -1;
268
0
    }
269
270
1.00k
    ipport_pack(packet + CRYPTO_NONCE_SIZE, &path->ip_port2);
271
1.00k
    memcpy(packet + CRYPTO_NONCE_SIZE + SIZE_IPPORT, path->public_key2, CRYPTO_PUBLIC_KEY_SIZE);
272
1.00k
    len = encrypt_data_symmetric(mem, path->shared_key2, nonce, step2, step2_size,
273
1.00k
                                 packet + CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
274
275
1.00k
    if (len != SIZE_IPPORT + SEND_BASE + length + CRYPTO_MAC_SIZE) {
276
0
        return -1;
277
0
    }
278
279
1.00k
    memcpy(packet, nonce, CRYPTO_NONCE_SIZE);
280
281
1.00k
    return CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE + len;
282
1.00k
}
283
284
/** @brief Create and send a onion response sent initially to dest with.
285
 * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
286
 *
287
 * return -1 on failure.
288
 * return 0 on success.
289
 */
290
int send_onion_response(const Logger *log, const Networking_Core *net,
291
                        const IP_Port *dest, const uint8_t *data, uint16_t length,
292
                        const uint8_t *ret)
293
62.7k
{
294
62.7k
    if (length > ONION_RESPONSE_MAX_DATA_SIZE || length == 0) {
295
0
        return -1;
296
0
    }
297
298
62.7k
    const uint16_t packet_size = 1 + RETURN_3 + length;
299
62.7k
    VLA(uint8_t, packet, packet_size);
300
62.7k
    packet[0] = NET_PACKET_ONION_RECV_3;
301
62.7k
    memcpy(packet + 1, ret, RETURN_3);
302
62.7k
    memcpy(packet + 1 + RETURN_3, data, length);
303
304
62.7k
    if ((uint16_t)sendpacket(net, dest, packet, packet_size) != packet_size) {
305
11
        return -1;
306
11
    }
307
308
62.7k
    Ip_Ntoa ip_str;
309
62.7k
    LOGGER_TRACE(log, "forwarded onion RECV_3 to %s:%d (%02x in %02x, %d bytes)",
310
62.7k
                 net_ip_ntoa(&dest->ip, &ip_str), net_ntohs(dest->port), data[0], packet[0], packet_size);
311
62.7k
    return 0;
312
62.7k
}
313
314
static int handle_send_initial(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata)
315
69.2k
{
316
69.2k
    Onion *onion = (Onion *)object;
317
318
69.2k
    if (length > ONION_MAX_PACKET_SIZE) {
319
0
        LOGGER_TRACE(onion->log, "invalid initial onion packet length: %u (max: %u)",
320
0
                     length, (unsigned int)ONION_MAX_PACKET_SIZE);
321
0
        return 1;
322
0
    }
323
324
69.2k
    if (length <= 1 + SEND_1) {
325
0
        LOGGER_TRACE(onion->log, "initial onion packet cannot contain SEND_1 packet: %u <= %u",
326
0
                     length, (unsigned int)(1 + SEND_1));
327
0
        return 1;
328
0
    }
329
330
69.2k
    change_symmetric_key(onion);
331
332
69.2k
    const int nonce_start = 1;
333
69.2k
    const int public_key_start = nonce_start + CRYPTO_NONCE_SIZE;
334
69.2k
    const int ciphertext_start = public_key_start + CRYPTO_PUBLIC_KEY_SIZE;
335
336
69.2k
    const int ciphertext_length = length - ciphertext_start;
337
69.2k
    const int plaintext_length = ciphertext_length - CRYPTO_MAC_SIZE;
338
339
69.2k
    uint8_t plain[ONION_MAX_PACKET_SIZE];
340
69.2k
    const uint8_t *public_key = &packet[public_key_start];
341
69.2k
    const uint8_t *shared_key = shared_key_cache_lookup(onion->shared_keys_1, public_key);
342
343
69.2k
    if (shared_key == nullptr) {
344
        /* Error looking up/deriving the shared key */
345
0
        LOGGER_TRACE(onion->log, "shared onion key lookup failed for pk %02x%02x...",
346
0
                     public_key[0], public_key[1]);
347
0
        return 1;
348
0
    }
349
350
69.2k
    const int len = decrypt_data_symmetric(
351
69.2k
                        onion->mem, shared_key, &packet[nonce_start], &packet[ciphertext_start], ciphertext_length, plain);
352
353
69.2k
    if (len != plaintext_length) {
354
838
        LOGGER_TRACE(onion->log, "decrypt failed: %d != %d", len, plaintext_length);
355
838
        return 1;
356
838
    }
357
358
68.4k
    return onion_send_1(onion, plain, len, source, packet + 1);
359
69.2k
}
360
361
int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, const IP_Port *source, const uint8_t *nonce)
362
69.4k
{
363
69.4k
    const uint16_t max_len = ONION_MAX_PACKET_SIZE + SIZE_IPPORT - (1 + CRYPTO_NONCE_SIZE + ONION_RETURN_1);
364
69.4k
    if (len > max_len) {
365
0
        LOGGER_TRACE(onion->log, "invalid SEND_1 length: %d > %d", len, max_len);
366
0
        return 1;
367
0
    }
368
369
69.4k
    if (len <= SIZE_IPPORT + SEND_BASE * 2) {
370
0
        return 1;
371
0
    }
372
373
69.4k
    IP_Port send_to;
374
375
69.4k
    if (ipport_unpack(&send_to, plain, len, false) == -1) {
376
0
        return 1;
377
0
    }
378
379
69.4k
    uint8_t ip_port[SIZE_IPPORT];
380
69.4k
    ipport_pack(ip_port, source);
381
382
69.4k
    uint8_t data[ONION_MAX_PACKET_SIZE] = {0};
383
69.4k
    data[0] = NET_PACKET_ONION_SEND_1;
384
69.4k
    memcpy(data + 1, nonce, CRYPTO_NONCE_SIZE);
385
69.4k
    memcpy(data + 1 + CRYPTO_NONCE_SIZE, plain + SIZE_IPPORT, len - SIZE_IPPORT);
386
69.4k
    uint16_t data_len = 1 + CRYPTO_NONCE_SIZE + (len - SIZE_IPPORT);
387
69.4k
    uint8_t *ret_part = data + data_len;
388
69.4k
    random_nonce(onion->rng, ret_part);
389
69.4k
    len = encrypt_data_symmetric(onion->mem, onion->secret_symmetric_key, ret_part, ip_port, SIZE_IPPORT,
390
69.4k
                                 ret_part + CRYPTO_NONCE_SIZE);
391
392
69.4k
    if (len != SIZE_IPPORT + CRYPTO_MAC_SIZE) {
393
18
        return 1;
394
18
    }
395
396
69.3k
    data_len += CRYPTO_NONCE_SIZE + len;
397
398
69.3k
    if ((uint32_t)sendpacket(onion->net, &send_to, data, data_len) != data_len) {
399
18
        return 1;
400
18
    }
401
402
69.3k
    Ip_Ntoa ip_str;
403
69.3k
    LOGGER_TRACE(onion->log, "forwarded onion packet to %s:%d, level 1 (%02x in %02x, %d bytes)",
404
69.3k
                 net_ip_ntoa(&send_to.ip, &ip_str), net_ntohs(send_to.port), plain[0], data[0], data_len);
405
69.3k
    return 0;
406
69.3k
}
407
408
static int handle_send_1(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata)
409
67.3k
{
410
67.3k
    Onion *onion = (Onion *)object;
411
412
67.3k
    if (length > ONION_MAX_PACKET_SIZE) {
413
0
        return 1;
414
0
    }
415
416
67.3k
    if (length <= 1 + SEND_2) {
417
0
        return 1;
418
0
    }
419
420
67.3k
    change_symmetric_key(onion);
421
422
67.3k
    uint8_t plain[ONION_MAX_PACKET_SIZE];
423
67.3k
    const uint8_t *public_key = packet + 1 + CRYPTO_NONCE_SIZE;
424
67.3k
    const uint8_t *shared_key = shared_key_cache_lookup(onion->shared_keys_2, public_key);
425
426
67.3k
    if (shared_key == nullptr) {
427
        /* Error looking up/deriving the shared key */
428
0
        return 1;
429
0
    }
430
431
67.3k
    int len = decrypt_data_symmetric(onion->mem, shared_key, packet + 1, packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE,
432
67.3k
                                     length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + RETURN_1), plain);
433
434
67.3k
    if (len != length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + RETURN_1 + CRYPTO_MAC_SIZE)) {
435
502
        return 1;
436
502
    }
437
438
66.8k
    IP_Port send_to;
439
440
66.8k
    if (ipport_unpack(&send_to, plain, len, false) == -1) {
441
0
        return 1;
442
0
    }
443
444
66.8k
    uint8_t data[ONION_MAX_PACKET_SIZE] = {0};
445
66.8k
    data[0] = NET_PACKET_ONION_SEND_2;
446
66.8k
    memcpy(data + 1, packet + 1, CRYPTO_NONCE_SIZE);
447
66.8k
    memcpy(data + 1 + CRYPTO_NONCE_SIZE, plain + SIZE_IPPORT, len - SIZE_IPPORT);
448
66.8k
    uint16_t data_len = 1 + CRYPTO_NONCE_SIZE + (len - SIZE_IPPORT);
449
66.8k
    uint8_t *ret_part = data + data_len;
450
66.8k
    random_nonce(onion->rng, ret_part);
451
66.8k
    uint8_t ret_data[RETURN_1 + SIZE_IPPORT];
452
66.8k
    ipport_pack(ret_data, source);
453
66.8k
    memcpy(ret_data + SIZE_IPPORT, packet + (length - RETURN_1), RETURN_1);
454
66.8k
    len = encrypt_data_symmetric(onion->mem, onion->secret_symmetric_key, ret_part, ret_data, sizeof(ret_data),
455
66.8k
                                 ret_part + CRYPTO_NONCE_SIZE);
456
457
66.8k
    if (len != RETURN_2 - CRYPTO_NONCE_SIZE) {
458
17
        return 1;
459
17
    }
460
461
66.7k
    data_len += CRYPTO_NONCE_SIZE + len;
462
463
66.7k
    if ((uint32_t)sendpacket(onion->net, &send_to, data, data_len) != data_len) {
464
16
        return 1;
465
16
    }
466
467
66.7k
    Ip_Ntoa ip_str;
468
66.7k
    LOGGER_TRACE(onion->log, "forwarded onion packet to %s:%d, level 2 (%02x in %02x, %d bytes)",
469
66.7k
                 net_ip_ntoa(&send_to.ip, &ip_str), net_ntohs(send_to.port), packet[0], data[0], data_len);
470
66.7k
    return 0;
471
66.7k
}
472
473
static int handle_send_2(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata)
474
65.4k
{
475
65.4k
    Onion *onion = (Onion *)object;
476
477
65.4k
    if (length > ONION_MAX_PACKET_SIZE) {
478
0
        return 1;
479
0
    }
480
481
65.4k
    if (length <= 1 + SEND_3) {
482
0
        return 1;
483
0
    }
484
485
65.4k
    change_symmetric_key(onion);
486
487
65.4k
    uint8_t plain[ONION_MAX_PACKET_SIZE];
488
65.4k
    const uint8_t *public_key = packet + 1 + CRYPTO_NONCE_SIZE;
489
65.4k
    const uint8_t *shared_key = shared_key_cache_lookup(onion->shared_keys_3, public_key);
490
491
65.4k
    if (shared_key == nullptr) {
492
        /* Error looking up/deriving the shared key */
493
0
        return 1;
494
0
    }
495
496
65.4k
    int len = decrypt_data_symmetric(onion->mem, shared_key, packet + 1, packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE,
497
65.4k
                                     length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + RETURN_2), plain);
498
499
65.4k
    if (len != length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + RETURN_2 + CRYPTO_MAC_SIZE)) {
500
538
        return 1;
501
538
    }
502
503
64.9k
    assert(len > SIZE_IPPORT);
504
505
64.9k
    const uint8_t packet_id = plain[SIZE_IPPORT];
506
507
64.9k
    if (packet_id != NET_PACKET_ANNOUNCE_REQUEST && packet_id != NET_PACKET_ANNOUNCE_REQUEST_OLD &&
508
64.9k
            packet_id != NET_PACKET_ONION_DATA_REQUEST) {
509
0
        return 1;
510
0
    }
511
512
64.9k
    IP_Port send_to;
513
514
64.9k
    if (ipport_unpack(&send_to, plain, len, false) == -1) {
515
0
        return 1;
516
0
    }
517
518
64.9k
    uint8_t data[ONION_MAX_PACKET_SIZE] = {0};
519
64.9k
    memcpy(data, plain + SIZE_IPPORT, len - SIZE_IPPORT);
520
64.9k
    uint16_t data_len = len - SIZE_IPPORT;
521
64.9k
    uint8_t *ret_part = data + (len - SIZE_IPPORT);
522
64.9k
    random_nonce(onion->rng, ret_part);
523
64.9k
    uint8_t ret_data[RETURN_2 + SIZE_IPPORT];
524
64.9k
    ipport_pack(ret_data, source);
525
64.9k
    memcpy(ret_data + SIZE_IPPORT, packet + (length - RETURN_2), RETURN_2);
526
64.9k
    len = encrypt_data_symmetric(onion->mem, onion->secret_symmetric_key, ret_part, ret_data, sizeof(ret_data),
527
64.9k
                                 ret_part + CRYPTO_NONCE_SIZE);
528
529
64.9k
    if (len != RETURN_3 - CRYPTO_NONCE_SIZE) {
530
16
        return 1;
531
16
    }
532
533
64.8k
    data_len += RETURN_3;
534
535
64.8k
    if ((uint32_t)sendpacket(onion->net, &send_to, data, data_len) != data_len) {
536
16
        return 1;
537
16
    }
538
539
64.8k
    Ip_Ntoa ip_str;
540
64.8k
    LOGGER_TRACE(onion->log, "forwarded onion packet to %s:%d, level 3 (%02x in %02x, %d bytes)",
541
64.8k
                 net_ip_ntoa(&send_to.ip, &ip_str), net_ntohs(send_to.port), packet[0], data[0], data_len);
542
64.8k
    return 0;
543
64.8k
}
544
545
static int handle_recv_3(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata)
546
62.6k
{
547
62.6k
    Onion *onion = (Onion *)object;
548
549
62.6k
    if (length > ONION_MAX_PACKET_SIZE) {
550
0
        return 1;
551
0
    }
552
553
62.6k
    if (length <= 1 + RETURN_3) {
554
0
        return 1;
555
0
    }
556
557
62.6k
    const uint8_t packet_id = packet[1 + RETURN_3];
558
559
62.6k
    if (packet_id != NET_PACKET_ANNOUNCE_RESPONSE && packet_id != NET_PACKET_ANNOUNCE_RESPONSE_OLD &&
560
62.6k
            packet_id != NET_PACKET_ONION_DATA_RESPONSE) {
561
0
        return 1;
562
0
    }
563
564
62.6k
    change_symmetric_key(onion);
565
566
62.6k
    uint8_t plain[SIZE_IPPORT + RETURN_2];
567
62.6k
    const int len = decrypt_data_symmetric(onion->mem, onion->secret_symmetric_key, packet + 1, packet + 1 + CRYPTO_NONCE_SIZE,
568
62.6k
                                           SIZE_IPPORT + RETURN_2 + CRYPTO_MAC_SIZE, plain);
569
570
62.6k
    if ((uint32_t)len != sizeof(plain)) {
571
16
        return 1;
572
16
    }
573
574
62.5k
    IP_Port send_to;
575
576
62.5k
    if (ipport_unpack(&send_to, plain, len, false) == -1) {
577
0
        LOGGER_DEBUG(onion->log, "failed to unpack IP/Port");
578
0
        return 1;
579
0
    }
580
581
62.5k
    uint8_t data[ONION_MAX_PACKET_SIZE] = {0};
582
62.5k
    data[0] = NET_PACKET_ONION_RECV_2;
583
62.5k
    memcpy(data + 1, plain + SIZE_IPPORT, RETURN_2);
584
62.5k
    memcpy(data + 1 + RETURN_2, packet + 1 + RETURN_3, length - (1 + RETURN_3));
585
62.5k
    const uint16_t data_len = 1 + RETURN_2 + (length - (1 + RETURN_3));
586
587
62.5k
    if ((uint32_t)sendpacket(onion->net, &send_to, data, data_len) != data_len) {
588
11
        return 1;
589
11
    }
590
591
62.5k
    Ip_Ntoa ip_str;
592
62.5k
    LOGGER_TRACE(onion->log, "forwarded onion RECV_2 to %s:%d (%02x in %02x, %d bytes)",
593
62.5k
                 net_ip_ntoa(&send_to.ip, &ip_str), net_ntohs(send_to.port), packet[0], data[0], data_len);
594
62.5k
    return 0;
595
62.5k
}
596
597
static int handle_recv_2(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata)
598
62.5k
{
599
62.5k
    Onion *onion = (Onion *)object;
600
601
62.5k
    if (length > ONION_MAX_PACKET_SIZE) {
602
0
        return 1;
603
0
    }
604
605
62.5k
    if (length <= 1 + RETURN_2) {
606
0
        return 1;
607
0
    }
608
609
62.5k
    const uint8_t packet_id = packet[1 + RETURN_2];
610
611
62.5k
    if (packet_id != NET_PACKET_ANNOUNCE_RESPONSE && packet_id != NET_PACKET_ANNOUNCE_RESPONSE_OLD &&
612
62.5k
            packet_id != NET_PACKET_ONION_DATA_RESPONSE) {
613
0
        return 1;
614
0
    }
615
616
62.5k
    change_symmetric_key(onion);
617
618
62.5k
    uint8_t plain[SIZE_IPPORT + RETURN_1];
619
62.5k
    const int len = decrypt_data_symmetric(onion->mem, onion->secret_symmetric_key, packet + 1, packet + 1 + CRYPTO_NONCE_SIZE,
620
62.5k
                                           SIZE_IPPORT + RETURN_1 + CRYPTO_MAC_SIZE, plain);
621
622
62.5k
    if ((uint32_t)len != sizeof(plain)) {
623
12
        return 1;
624
12
    }
625
626
62.5k
    IP_Port send_to;
627
628
62.5k
    if (ipport_unpack(&send_to, plain, len, false) == -1) {
629
0
        return 1;
630
0
    }
631
632
62.5k
    uint8_t data[ONION_MAX_PACKET_SIZE] = {0};
633
62.5k
    data[0] = NET_PACKET_ONION_RECV_1;
634
62.5k
    memcpy(data + 1, plain + SIZE_IPPORT, RETURN_1);
635
62.5k
    memcpy(data + 1 + RETURN_1, packet + 1 + RETURN_2, length - (1 + RETURN_2));
636
62.5k
    const uint16_t data_len = 1 + RETURN_1 + (length - (1 + RETURN_2));
637
638
62.5k
    if ((uint32_t)sendpacket(onion->net, &send_to, data, data_len) != data_len) {
639
10
        return 1;
640
10
    }
641
642
62.4k
    Ip_Ntoa ip_str;
643
62.4k
    LOGGER_TRACE(onion->log, "forwarded onion RECV_1 to %s:%d (%02x in %02x, %d bytes)",
644
62.4k
                 net_ip_ntoa(&send_to.ip, &ip_str), net_ntohs(send_to.port), packet[0], data[0], data_len);
645
62.4k
    return 0;
646
62.5k
}
647
648
static int handle_recv_1(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata)
649
62.4k
{
650
62.4k
    Onion *onion = (Onion *)object;
651
652
62.4k
    if (length > ONION_MAX_PACKET_SIZE) {
653
0
        return 1;
654
0
    }
655
656
62.4k
    if (length <= 1 + RETURN_1) {
657
0
        return 1;
658
0
    }
659
660
62.4k
    const uint8_t packet_id = packet[1 + RETURN_1];
661
662
62.4k
    if (packet_id != NET_PACKET_ANNOUNCE_RESPONSE && packet_id != NET_PACKET_ANNOUNCE_RESPONSE_OLD &&
663
62.4k
            packet_id != NET_PACKET_ONION_DATA_RESPONSE) {
664
0
        return 1;
665
0
    }
666
667
62.4k
    change_symmetric_key(onion);
668
669
62.4k
    uint8_t plain[SIZE_IPPORT];
670
62.4k
    const int len = decrypt_data_symmetric(onion->mem, onion->secret_symmetric_key, packet + 1, packet + 1 + CRYPTO_NONCE_SIZE,
671
62.4k
                                           SIZE_IPPORT + CRYPTO_MAC_SIZE, plain);
672
673
62.4k
    if ((uint32_t)len != SIZE_IPPORT) {
674
14
        return 1;
675
14
    }
676
677
62.4k
    IP_Port send_to;
678
679
62.4k
    if (ipport_unpack(&send_to, plain, len, true) == -1) {
680
0
        LOGGER_DEBUG(onion->log, "failed to unpack IP/Port");
681
0
        return 1;
682
0
    }
683
684
62.4k
    const uint16_t data_len = length - (1 + RETURN_1);
685
686
62.4k
    if (onion->recv_1_function != nullptr &&
687
62.4k
            !net_family_is_ipv4(send_to.ip.family) &&
688
62.4k
            !net_family_is_ipv6(send_to.ip.family)) {
689
803
        return onion->recv_1_function(onion->callback_object, &send_to, packet + (1 + RETURN_1), data_len);
690
803
    }
691
692
61.5k
    if ((uint32_t)sendpacket(onion->net, &send_to, packet + (1 + RETURN_1), data_len) != data_len) {
693
10
        return 1;
694
10
    }
695
696
61.5k
    return 0;
697
61.5k
}
698
699
void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *object)
700
8
{
701
8
    onion->recv_1_function = function;
702
8
    onion->callback_object = object;
703
8
}
704
705
Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht)
706
2.89k
{
707
2.89k
    if (dht == nullptr) {
708
0
        return nullptr;
709
0
    }
710
711
2.89k
    Onion *onion = (Onion *)mem_alloc(mem, sizeof(Onion));
712
713
2.89k
    if (onion == nullptr) {
714
17
        return nullptr;
715
17
    }
716
717
2.88k
    onion->log = log;
718
2.88k
    onion->dht = dht;
719
2.88k
    onion->net = dht_get_net(dht);
720
2.88k
    onion->mono_time = mono_time;
721
2.88k
    onion->rng = rng;
722
2.88k
    onion->mem = mem;
723
2.88k
    new_symmetric_key(rng, onion->secret_symmetric_key);
724
2.88k
    onion->timestamp = mono_time_get(onion->mono_time);
725
726
2.88k
    const uint8_t *secret_key = dht_get_self_secret_key(dht);
727
2.88k
    onion->shared_keys_1 = shared_key_cache_new(log, mono_time, mem, secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
728
2.88k
    onion->shared_keys_2 = shared_key_cache_new(log, mono_time, mem, secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
729
2.88k
    onion->shared_keys_3 = shared_key_cache_new(log, mono_time, mem, secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
730
731
2.88k
    if (onion->shared_keys_1 == nullptr ||
732
2.88k
            onion->shared_keys_2 == nullptr ||
733
2.88k
            onion->shared_keys_3 == nullptr) {
734
        // cppcheck-suppress mismatchAllocDealloc
735
34
        kill_onion(onion);
736
34
        return nullptr;
737
34
    }
738
739
2.84k
    networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_INITIAL, &handle_send_initial, onion);
740
2.84k
    networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, &handle_send_1, onion);
741
2.84k
    networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_2, &handle_send_2, onion);
742
743
2.84k
    networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_3, &handle_recv_3, onion);
744
2.84k
    networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_2, &handle_recv_2, onion);
745
2.84k
    networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_1, &handle_recv_1, onion);
746
747
2.84k
    return onion;
748
2.88k
}
749
750
void kill_onion(Onion *onion)
751
2.05k
{
752
2.05k
    if (onion == nullptr) {
753
51
        return;
754
51
    }
755
756
2.00k
    networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_INITIAL, nullptr, nullptr);
757
2.00k
    networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, nullptr, nullptr);
758
2.00k
    networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_2, nullptr, nullptr);
759
760
2.00k
    networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_3, nullptr, nullptr);
761
2.00k
    networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_2, nullptr, nullptr);
762
2.00k
    networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_1, nullptr, nullptr);
763
764
2.00k
    crypto_memzero(onion->secret_symmetric_key, sizeof(onion->secret_symmetric_key));
765
766
2.00k
    shared_key_cache_free(onion->shared_keys_1);
767
2.00k
    shared_key_cache_free(onion->shared_keys_2);
768
2.00k
    shared_key_cache_free(onion->shared_keys_3);
769
770
2.00k
    mem_delete(onion->mem, onion);
771
2.00k
}