1 /* SPDX-License-Identifier: GPL-3.0-or-later 2 * Copyright © 2016-2018 The TokTok team. 3 * Copyright © 2013 Tox project. 4 */ 5 6 /* 7 * Implementation of the onion part of docs/Prevent_Tracking.txt 8 */ 9 #ifndef C_TOXCORE_TOXCORE_ONION_H 10 #define C_TOXCORE_TOXCORE_ONION_H 11 12 #include "DHT.h" 13 #include "mono_time.h" 14 15 typedef int onion_recv_1_cb(void *object, IP_Port dest, const uint8_t *data, uint16_t length); 16 17 typedef struct Onion { 18 Mono_Time *mono_time; 19 DHT *dht; 20 Networking_Core *net; 21 uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; 22 uint64_t timestamp; 23 24 Shared_Keys shared_keys_1; 25 Shared_Keys shared_keys_2; 26 Shared_Keys shared_keys_3; 27 28 onion_recv_1_cb *recv_1_function; 29 void *callback_object; 30 } Onion; 31 32 #define ONION_MAX_PACKET_SIZE 1400 33 34 #define ONION_RETURN_1 (CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_MAC_SIZE) 35 #define ONION_RETURN_2 (CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_MAC_SIZE + ONION_RETURN_1) 36 #define ONION_RETURN_3 (CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_MAC_SIZE + ONION_RETURN_2) 37 38 #define ONION_SEND_BASE (CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT + CRYPTO_MAC_SIZE) 39 #define ONION_SEND_3 (CRYPTO_NONCE_SIZE + ONION_SEND_BASE + ONION_RETURN_2) 40 #define ONION_SEND_2 (CRYPTO_NONCE_SIZE + ONION_SEND_BASE*2 + ONION_RETURN_1) 41 #define ONION_SEND_1 (CRYPTO_NONCE_SIZE + ONION_SEND_BASE*3) 42 43 #define ONION_MAX_DATA_SIZE (ONION_MAX_PACKET_SIZE - (ONION_SEND_1 + 1)) 44 #define ONION_RESPONSE_MAX_DATA_SIZE (ONION_MAX_PACKET_SIZE - (1 + ONION_RETURN_3)) 45 46 #define ONION_PATH_LENGTH 3 47 48 typedef struct Onion_Path { 49 uint8_t shared_key1[CRYPTO_SHARED_KEY_SIZE]; 50 uint8_t shared_key2[CRYPTO_SHARED_KEY_SIZE]; 51 uint8_t shared_key3[CRYPTO_SHARED_KEY_SIZE]; 52 53 uint8_t public_key1[CRYPTO_PUBLIC_KEY_SIZE]; 54 uint8_t public_key2[CRYPTO_PUBLIC_KEY_SIZE]; 55 uint8_t public_key3[CRYPTO_PUBLIC_KEY_SIZE]; 56 57 IP_Port ip_port1; 58 uint8_t node_public_key1[CRYPTO_PUBLIC_KEY_SIZE]; 59 60 IP_Port ip_port2; 61 uint8_t node_public_key2[CRYPTO_PUBLIC_KEY_SIZE]; 62 63 IP_Port ip_port3; 64 uint8_t node_public_key3[CRYPTO_PUBLIC_KEY_SIZE]; 65 66 uint32_t path_num; 67 } Onion_Path; 68 69 /* Create a new onion path. 70 * 71 * Create a new onion path out of nodes (nodes is a list of ONION_PATH_LENGTH nodes) 72 * 73 * new_path must be an empty memory location of at least Onion_Path size. 74 * 75 * return -1 on failure. 76 * return 0 on success. 77 */ 78 int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *nodes); 79 80 /* Dump nodes in onion path to nodes of length num_nodes; 81 * 82 * return -1 on failure. 83 * return 0 on success. 84 */ 85 int onion_path_to_nodes(Node_format *nodes, unsigned int num_nodes, const Onion_Path *path); 86 87 /* Create a onion packet. 88 * 89 * Use Onion_Path path to create packet for data of length to dest. 90 * Maximum length of data is ONION_MAX_DATA_SIZE. 91 * packet should be at least ONION_MAX_PACKET_SIZE big. 92 * 93 * return -1 on failure. 94 * return length of created packet on success. 95 */ 96 int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest, 97 const uint8_t *data, uint16_t length); 98 99 100 /* Create a onion packet to be sent over tcp. 101 * 102 * Use Onion_Path path to create packet for data of length to dest. 103 * Maximum length of data is ONION_MAX_DATA_SIZE. 104 * packet should be at least ONION_MAX_PACKET_SIZE big. 105 * 106 * return -1 on failure. 107 * return length of created packet on success. 108 */ 109 int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest, 110 const uint8_t *data, uint16_t length); 111 112 /* Create and send a onion packet. 113 * 114 * Use Onion_Path path to send data of length to dest. 115 * Maximum length of data is ONION_MAX_DATA_SIZE. 116 * 117 * return -1 on failure. 118 * return 0 on success. 119 */ 120 int send_onion_packet(Networking_Core *net, const Onion_Path *path, IP_Port dest, const uint8_t *data, uint16_t length); 121 122 /* Create and send a onion response sent initially to dest with. 123 * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE. 124 * 125 * return -1 on failure. 126 * return 0 on success. 127 */ 128 int send_onion_response(Networking_Core *net, IP_Port dest, const uint8_t *data, uint16_t length, const uint8_t *ret); 129 130 /* Function to handle/send received decrypted versions of the packet sent with send_onion_packet. 131 * 132 * return 0 on success. 133 * return 1 on failure. 134 * 135 * Used to handle these packets that are received in a non traditional way (by TCP for example). 136 * 137 * Source family must be set to something else than TOX_AF_INET6 or TOX_AF_INET so that the callback gets called 138 * when the response is received. 139 */ 140 int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port source, const uint8_t *nonce); 141 142 /* Set the callback to be called when the dest ip_port doesn't have TOX_AF_INET6 or TOX_AF_INET as the family. 143 */ 144 void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *object); 145 146 Onion *new_onion(Mono_Time *mono_time, DHT *dht); 147 148 void kill_onion(Onion *onion); 149 150 151 #endif 152