Another packet type added to onion.c

This commit is contained in:
irungentoo 2013-12-27 15:21:16 -05:00
parent 977aab7f2f
commit 86aff82a9f
3 changed files with 46 additions and 3 deletions

View File

@ -2105,6 +2105,7 @@ DHT *new_DHT(Net_Crypto *c)
cryptopacket_registerhandler(c, CRYPTO_PACKET_HARDENING, &handle_hardening, dht);
new_symmetric_key(dht->secret_symmetric_key);
crypto_box_keypair(dht->self_public_key, dht->self_secret_key);
dht->assoc = new_Assoc_default(dht->c->self_public_key);
return dht;

View File

@ -142,6 +142,9 @@ typedef struct {
/* Note: this key should not be/is not used to transmit any sensitive materials */
uint8_t secret_symmetric_key[crypto_secretbox_KEYBYTES];
/* DHT keypair */
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
DHT_Friend *friends_list;
uint16_t num_friends;

View File

@ -22,16 +22,55 @@
#include "onion.h"
#define MAX_ONION_SIZE 1400
#define MAX_ONION_SIZE MAX_DATA_SIZE
#define RETURN_1 (crypto_secretbox_NONCEBYTES + sizeof(IP_Port) + crypto_secretbox_MACBYTES)
#define RETURN_2 (crypto_secretbox_NONCEBYTES + sizeof(IP_Port) + crypto_secretbox_MACBYTES + RETURN_1)
#define RETURN_3 (crypto_secretbox_NONCEBYTES + sizeof(IP_Port) + crypto_secretbox_MACBYTES + RETURN_2)
#define SEND_BASE (crypto_box_PUBLICKEYBYTES + sizeof(IP_Port) + crypto_box_MACBYTES)
#define SEND_3 (crypto_box_NONCEBYTES + SEND_BASE + RETURN_2)
#define SEND_2 (crypto_box_NONCEBYTES + SEND_BASE*2 + RETURN_1)
#define SEND_1 (crypto_box_NONCEBYTES + SEND_BASE*3)
static int handle_send_initial(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
Onion *onion = object;
if (length > MAX_ONION_SIZE)
return 1;
if (length <= 1 + SEND_1)
return 1;
uint8_t plain[MAX_ONION_SIZE];
int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion->dht->self_secret_key, packet + 1,
packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES), plain);
if ((uint32_t)len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES))
return 1;
IP_Port send_to;
memcpy(&send_to, plain, sizeof(IP_Port));
uint8_t data[MAX_ONION_SIZE];
data[0] = NET_PACKET_ONION_SEND_1;
memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES);
memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port));
uint8_t *ret_part = data + 1 + crypto_box_NONCEBYTES + (len - sizeof(IP_Port));
new_nonce(ret_part);
len = encrypt_data_symmetric(onion->secret_symmetric_key, ret_part, (uint8_t *)&source, sizeof(IP_Port),
ret_part + crypto_secretbox_NONCEBYTES);
if (len != sizeof(IP_Port) + crypto_secretbox_MACBYTES)
return 1;
uint32_t data_len = 1 + crypto_box_NONCEBYTES + (len - sizeof(IP_Port)) + len;
if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len)
return 1;
return 0;
}
@ -157,7 +196,7 @@ Onion *new_onion(DHT *dht)
networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_INITIAL, &handle_send_initial, onion);
networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, &handle_send_1, onion);
networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, &handle_send_2, onion);
networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_2, &handle_send_2, onion);
networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_3, &handle_recv_3, onion);
networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_2, &handle_recv_2, onion);
@ -170,7 +209,7 @@ void kill_onion(Onion *onion)
{
networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_INITIAL, NULL, NULL);
networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, NULL, NULL);
networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, NULL, NULL);
networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_2, NULL, NULL);
networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_3, NULL, NULL);
networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_2, NULL, NULL);