Metadata collection prevention part 1 of ???.

Crypto requests packets are packets that can be routed by nodes in the
DHT. In this pull request I have merged both Natping requests and friend
requests into one common packet (Crypto request packets). Both these
packets should now only be distinguishable by the size of the data in
them to an outside observer for an example on how to send and recieve
data with these packets see: friend_requests.c

Note that these packets are prefixed with id 32 (friend request packets)
which means this change is compatible with the currently running DHT
bootstrap servers.

Also changed small thing in DHT_test.c
This commit is contained in:
irungentoo 2013-08-14 18:26:00 -04:00
parent 95664357d4
commit 4330bfbf87
5 changed files with 83 additions and 79 deletions

View File

@ -932,49 +932,30 @@ static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
} }
/* Handle a received ping request for */ /* Handle a received ping request for */
static int handle_NATping(IP_Port source, uint8_t * packet, uint32_t length) static int handle_NATping(IP_Port source, uint8_t * source_pubkey, uint8_t * packet, uint32_t length)
{ {
if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING uint64_t ping_id;
|| length > MAX_DATA_SIZE + ENCRYPTION_PADDING) memcpy(&ping_id, packet + 1, sizeof(uint64_t));
int friendnumber = friend_number(source_pubkey);
if (friendnumber == -1)
return 1; return 1;
/* check if request is for us. */ Friend * friend = &friends_list[friendnumber];
if (id_equal(packet + 1, self_public_key)) {
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t data[MAX_DATA_SIZE];
int len = handle_request(public_key, data, packet, length); if (packet[0] == 0) {
if (len != sizeof(uint64_t) + 1) /* 1 is reply */
return 1; send_NATping(source_pubkey, ping_id, 1);
friend->recvNATping_timestamp = unix_time();
uint64_t ping_id; return 0;
memcpy(&ping_id, data + 1, sizeof(uint64_t)); } else if (packet[0] == 1) {
if (friend->NATping_id == ping_id) {
int friendnumber = friend_number(public_key); friend->NATping_id = ((uint64_t)random_int() << 32) + random_int();
if (friendnumber == -1) friend->hole_punching = 1;
return 1;
Friend * friend = &friends_list[friendnumber];
if (data[0] == 0) {
/* 1 is reply */
send_NATping(public_key, ping_id, 1);
friend->recvNATping_timestamp = unix_time();
return 0; return 0;
} else if (data[0] == 1) {
if (friend->NATping_id == ping_id) {
friend->NATping_id = ((uint64_t)random_int() << 32) + random_int();
friend->hole_punching = 1;
return 0;
}
} }
return 1;
} }
return 1;
/* if request is not for us, try routing it. */
route_packet(packet + 1, packet, length);
return 0;
} }
/* Get the most common ip in the ip_portlist /* Get the most common ip in the ip_portlist
@ -1082,7 +1063,7 @@ void DHT_init(void)
networking_registerhandler(1, &handle_ping_response); networking_registerhandler(1, &handle_ping_response);
networking_registerhandler(2, &handle_getnodes); networking_registerhandler(2, &handle_getnodes);
networking_registerhandler(3, &handle_sendnodes); networking_registerhandler(3, &handle_sendnodes);
networking_registerhandler(254, &handle_NATping); cryptopacket_registerhandler(254, &handle_NATping);
} }
void doDHT(void) void doDHT(void)

View File

@ -123,39 +123,23 @@ static int request_received(uint8_t * client_id)
} }
static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t length) static int friendreq_handlepacket(IP_Port source, uint8_t * source_pubkey, uint8_t * packet, uint32_t length)
{ {
if (packet[0] == 32) { if (handle_friendrequest_isset == 0)
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || return 1;
length > MAX_DATA_SIZE + ENCRYPTION_PADDING) if (length <= sizeof(nospam))
return 1; return 1;
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us. if (request_received(source_pubkey))
if (handle_friendrequest_isset == 0) return 1;
return 1; if (memcmp(packet, &nospam, sizeof(nospam)) != 0)
return 1;
uint8_t public_key[crypto_box_PUBLICKEYBYTES]; addto_receivedlist(source_pubkey);
uint8_t data[MAX_DATA_SIZE]; (*handle_friendrequest)(source_pubkey, packet + 4, length - 4, handle_friendrequest_userdata);
int len = handle_request(public_key, data, packet, length); return 0;
if (len == -1)
return 1;
if (len <= sizeof(nospam))
return 1;
if (request_received(public_key))
return 1;
if (memcmp(data, &nospam, sizeof(nospam)) != 0)
return 1;
addto_receivedlist(public_key);
(*handle_friendrequest)(public_key, data + 4, len - 4, handle_friendrequest_userdata);
} else { /* if request is not for us, try routing it. */
if(route_packet(packet + 1, packet, length) == length)
return 0;
}
}
return 1;
} }
void friendreq_init(void) void friendreq_init(void)
{ {
networking_registerhandler(32, &friendreq_handlepacket); cryptopacket_registerhandler(32, &friendreq_handlepacket);
} }

View File

@ -221,15 +221,18 @@ int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length)
returns the length of the created packet on success */ returns the length of the created packet on success */
int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t length, uint8_t request_id) int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t length, uint8_t request_id)
{ {
if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING)
return -1; return -1;
uint8_t nonce[crypto_box_NONCEBYTES]; uint8_t nonce[crypto_box_NONCEBYTES];
uint8_t temp[MAX_DATA_SIZE];
memcpy(temp + 1, data, length);
temp[0] = request_id;
random_nonce(nonce); random_nonce(nonce);
int len = encrypt_data(public_key, self_secret_key, nonce, data, length, int len = encrypt_data(public_key, self_secret_key, nonce, temp, length,
1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
if (len == -1) if (len == -1)
return -1; return -1;
packet[0] = request_id; packet[0] = 32;
memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES); memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES);
memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES); memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES);
@ -241,7 +244,7 @@ int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t
in data if a friend or ping request was sent to us and returns the length of the data. in data if a friend or ping request was sent to us and returns the length of the data.
packet is the request packet and length is its length packet is the request packet and length is its length
return -1 if not valid request. */ return -1 if not valid request. */
int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length) static int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, uint16_t length)
{ {
if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
@ -249,16 +252,51 @@ int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t
memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
uint8_t nonce[crypto_box_NONCEBYTES]; uint8_t nonce[crypto_box_NONCEBYTES];
uint8_t temp[MAX_DATA_SIZE];
memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES); memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES, int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data); length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp);
if(len1 == -1) if(len1 == -1 || len1 == 0)
return -1; return -1;
request_id[0] = temp[0];
--len1;
memcpy(data, temp + 1, len1);
return len1; return len1;
} else } else
return -1; return -1;
} }
static cryptopacket_handler_callback cryptopackethandlers[256] = {0};
void cryptopacket_registerhandler(uint8_t byte, cryptopacket_handler_callback cb)
{
cryptopackethandlers[byte] = cb;
}
static int cryptopacket_handle(IP_Port source, uint8_t * packet, uint32_t length)
{
if (packet[0] == 32) {
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING ||
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
return 1;
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t data[MAX_DATA_SIZE];
uint8_t number;
int len = handle_request(public_key, data, &number, packet, length);
if (len == -1 || len == 0)
return 1;
if (!cryptopackethandlers[number]) return 1;
cryptopackethandlers[number](source, public_key, data, len - 1);
} else { /* if request is not for us, try routing it. */
if(route_packet(packet + 1, packet, length) == length)
return 0;
}
}
return 1;
}
/* Send a crypto handshake packet containing an encrypted secret nonce and session public key /* Send a crypto handshake packet containing an encrypted secret nonce and session public key
to peer with connection_id and public_key to peer with connection_id and public_key
the packet is encrypted with a random nonce which is sent in plain text with the packet */ the packet is encrypted with a random nonce which is sent in plain text with the packet */
@ -579,6 +617,7 @@ void initNetCrypto(void)
{ {
memset(crypto_connections, 0 ,sizeof(crypto_connections)); memset(crypto_connections, 0 ,sizeof(crypto_connections));
memset(incoming_connections, -1 ,sizeof(incoming_connections)); memset(incoming_connections, -1 ,sizeof(incoming_connections));
networking_registerhandler(32, &cryptopacket_handle);
uint32_t i; uint32_t i;
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
crypto_connections[i].number = ~0; crypto_connections[i].number = ~0;

View File

@ -25,6 +25,7 @@
#define NET_CRYPTO_H #define NET_CRYPTO_H
#include "Lossless_UDP.h" #include "Lossless_UDP.h"
#include "DHT.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -88,11 +89,10 @@ int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length);
returns the length of the created packet on success */ returns the length of the created packet on success */
int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id); int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id);
/* puts the senders public key in the request in public_key, the data from the request
in data if a friend or ping request was sent to us and returns the length of the data. typedef int (*cryptopacket_handler_callback)(IP_Port ip_port, uint8_t * source_pubkey, uint8_t *data, uint32_t len);
packet is the request packet and length is its length /* Function to call when request beginning with byte is received */
return -1 if not valid request. */ void cryptopacket_registerhandler(uint8_t byte, cryptopacket_handler_callback cb);
int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length);
/* Start a secure connection with other peer who has public_key and ip_port /* Start a secure connection with other peer who has public_key and ip_port
returns -1 if failure returns -1 if failure

View File

@ -53,10 +53,10 @@ void print_clientlist()
uint32_t i, j; uint32_t i, j;
IP_Port p_ip; IP_Port p_ip;
printf("___________________CLOSE________________________________\n"); printf("___________________CLOSE________________________________\n");
for(i = 0; i < 4; i++) { for(i = 0; i < 32; i++) {
printf("ClientID: "); printf("ClientID: ");
for(j = 0; j < 32; j++) { for(j = 0; j < 32; j++) {
printf("%hhX", close_clientlist[i].client_id[j]); printf("%02hhX", close_clientlist[i].client_id[j]);
} }
p_ip = close_clientlist[i].ip_port; p_ip = close_clientlist[i].ip_port;
printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));