summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-08-01 23:02:13 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-08-16 21:01:43 +0000
commitd6d305feeb76735ee4b4e14c6bca737a5482bc19 (patch)
tree99005c635a452245006b3b5de44f1dd80da9f77f /toxcore/net_crypto.c
parent54066f338f185f2fbd6694d9a4877f42cbfa21c8 (diff)
Use per-instance `Mono_Time` for Messenger and onion.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c42
1 files changed, 23 insertions, 19 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index bd214b91..a577b6c7 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -134,6 +134,7 @@ typedef struct Crypto_Connection {
134 134
135struct Net_Crypto { 135struct Net_Crypto {
136 const Logger *log; 136 const Logger *log;
137 Mono_Time *mono_time;
137 138
138 DHT *dht; 139 DHT *dht;
139 TCP_Connections *tcp_c; 140 TCP_Connections *tcp_c;
@@ -248,10 +249,11 @@ static int create_cookie_request(const Net_Crypto *c, uint8_t *packet, uint8_t *
248 * return -1 on failure. 249 * return -1 on failure.
249 * return 0 on success. 250 * return 0 on success.
250 */ 251 */
251static int create_cookie(const Logger *log, uint8_t *cookie, const uint8_t *bytes, const uint8_t *encryption_key) 252static int create_cookie(const Logger *log, const Mono_Time *mono_time, uint8_t *cookie, const uint8_t *bytes,
253 const uint8_t *encryption_key)
252{ 254{
253 uint8_t contents[COOKIE_CONTENTS_LENGTH]; 255 uint8_t contents[COOKIE_CONTENTS_LENGTH];
254 const uint64_t temp_time = unix_time(); 256 const uint64_t temp_time = mono_time_get(mono_time);
255 memcpy(contents, &temp_time, sizeof(temp_time)); 257 memcpy(contents, &temp_time, sizeof(temp_time));
256 memcpy(contents + sizeof(temp_time), bytes, COOKIE_DATA_LENGTH); 258 memcpy(contents + sizeof(temp_time), bytes, COOKIE_DATA_LENGTH);
257 random_nonce(cookie); 259 random_nonce(cookie);
@@ -269,7 +271,8 @@ static int create_cookie(const Logger *log, uint8_t *cookie, const uint8_t *byte
269 * return -1 on failure. 271 * return -1 on failure.
270 * return 0 on success. 272 * return 0 on success.
271 */ 273 */
272static int open_cookie(const Logger *log, uint8_t *bytes, const uint8_t *cookie, const uint8_t *encryption_key) 274static int open_cookie(const Logger *log, const Mono_Time *mono_time, uint8_t *bytes, const uint8_t *cookie,
275 const uint8_t *encryption_key)
273{ 276{
274 uint8_t contents[COOKIE_CONTENTS_LENGTH]; 277 uint8_t contents[COOKIE_CONTENTS_LENGTH];
275 const int len = decrypt_data_symmetric(encryption_key, cookie, cookie + CRYPTO_NONCE_SIZE, 278 const int len = decrypt_data_symmetric(encryption_key, cookie, cookie + CRYPTO_NONCE_SIZE,
@@ -281,7 +284,7 @@ static int open_cookie(const Logger *log, uint8_t *bytes, const uint8_t *cookie,
281 284
282 uint64_t cookie_time; 285 uint64_t cookie_time;
283 memcpy(&cookie_time, contents, sizeof(cookie_time)); 286 memcpy(&cookie_time, contents, sizeof(cookie_time));
284 const uint64_t temp_time = unix_time(); 287 const uint64_t temp_time = mono_time_get(mono_time);
285 288
286 if (cookie_time + COOKIE_TIMEOUT < temp_time || temp_time < cookie_time) { 289 if (cookie_time + COOKIE_TIMEOUT < temp_time || temp_time < cookie_time) {
287 return -1; 290 return -1;
@@ -307,7 +310,7 @@ static int create_cookie_response(const Net_Crypto *c, uint8_t *packet, const ui
307 memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE); 310 memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE);
308 uint8_t plain[COOKIE_LENGTH + sizeof(uint64_t)]; 311 uint8_t plain[COOKIE_LENGTH + sizeof(uint64_t)];
309 312
310 if (create_cookie(c->log, plain, cookie_plain, c->secret_symmetric_key) != 0) { 313 if (create_cookie(c->log, c->mono_time, plain, cookie_plain, c->secret_symmetric_key) != 0) {
311 return -1; 314 return -1;
312 } 315 }
313 316
@@ -475,8 +478,8 @@ static int create_crypto_handshake(const Net_Crypto *c, uint8_t *packet, const u
475 memcpy(cookie_plain, peer_real_pk, CRYPTO_PUBLIC_KEY_SIZE); 478 memcpy(cookie_plain, peer_real_pk, CRYPTO_PUBLIC_KEY_SIZE);
476 memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, peer_dht_pubkey, CRYPTO_PUBLIC_KEY_SIZE); 479 memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, peer_dht_pubkey, CRYPTO_PUBLIC_KEY_SIZE);
477 480
478 if (create_cookie(c->log, plain + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SHA512_SIZE, cookie_plain, 481 if (create_cookie(c->log, c->mono_time, plain + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SHA512_SIZE,
479 c->secret_symmetric_key) != 0) { 482 cookie_plain, c->secret_symmetric_key) != 0) {
480 return -1; 483 return -1;
481 } 484 }
482 485
@@ -521,7 +524,7 @@ static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t
521 524
522 uint8_t cookie_plain[COOKIE_DATA_LENGTH]; 525 uint8_t cookie_plain[COOKIE_DATA_LENGTH];
523 526
524 if (open_cookie(c->log, cookie_plain, packet + 1, c->secret_symmetric_key) != 0) { 527 if (open_cookie(c->log, c->mono_time, cookie_plain, packet + 1, c->secret_symmetric_key) != 0) {
525 return -1; 528 return -1;
526 } 529 }
527 530
@@ -620,7 +623,7 @@ static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id)
620 return empty; 623 return empty;
621 } 624 }
622 625
623 uint64_t current_time = unix_time(); 626 const uint64_t current_time = mono_time_get(c->mono_time);
624 bool v6 = 0, v4 = 0; 627 bool v6 = 0, v4 = 0;
625 628
626 if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) { 629 if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) {
@@ -681,13 +684,13 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t
681 } 684 }
682 685
683 // TODO(irungentoo): a better way of sending packets directly to confirm the others ip. 686 // TODO(irungentoo): a better way of sending packets directly to confirm the others ip.
684 uint64_t current_time = unix_time(); 687 const uint64_t current_time = mono_time_get(c->mono_time);
685 688
686 if ((((UDP_DIRECT_TIMEOUT / 2) + conn->direct_send_attempt_time) > current_time && length < 96) 689 if ((((UDP_DIRECT_TIMEOUT / 2) + conn->direct_send_attempt_time) > current_time && length < 96)
687 || data[0] == NET_PACKET_COOKIE_REQUEST || data[0] == NET_PACKET_CRYPTO_HS) { 690 || data[0] == NET_PACKET_COOKIE_REQUEST || data[0] == NET_PACKET_CRYPTO_HS) {
688 if ((uint32_t)sendpacket(dht_get_net(c->dht), ip_port, data, length) == length) { 691 if ((uint32_t)sendpacket(dht_get_net(c->dht), ip_port, data, length) == length) {
689 direct_send_attempt = 1; 692 direct_send_attempt = 1;
690 conn->direct_send_attempt_time = unix_time(); 693 conn->direct_send_attempt_time = mono_time_get(c->mono_time);
691 } 694 }
692 } 695 }
693 } 696 }
@@ -1841,9 +1844,9 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id,
1841 } 1844 }
1842 1845
1843 if (net_family_is_ipv4(source.ip.family)) { 1846 if (net_family_is_ipv4(source.ip.family)) {
1844 conn->direct_lastrecv_timev4 = unix_time(); 1847 conn->direct_lastrecv_timev4 = mono_time_get(c->mono_time);
1845 } else { 1848 } else {
1846 conn->direct_lastrecv_timev6 = unix_time(); 1849 conn->direct_lastrecv_timev6 = mono_time_get(c->mono_time);
1847 } 1850 }
1848 1851
1849 return 0; 1852 return 0;
@@ -2069,7 +2072,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port,
2069 return -1; 2072 return -1;
2070 } 2073 }
2071 2074
2072 const uint64_t direct_lastrecv_time = connected ? unix_time() : 0; 2075 const uint64_t direct_lastrecv_time = connected ? mono_time_get(c->mono_time) : 0;
2073 2076
2074 if (net_family_is_ipv4(ip_port.ip.family)) { 2077 if (net_family_is_ipv4(ip_port.ip.family)) {
2075 conn->direct_lastrecv_timev4 = direct_lastrecv_time; 2078 conn->direct_lastrecv_timev4 = direct_lastrecv_time;
@@ -2409,9 +2412,9 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
2409 pthread_mutex_lock(&conn->mutex); 2412 pthread_mutex_lock(&conn->mutex);
2410 2413
2411 if (net_family_is_ipv4(source.ip.family)) { 2414 if (net_family_is_ipv4(source.ip.family)) {
2412 conn->direct_lastrecv_timev4 = unix_time(); 2415 conn->direct_lastrecv_timev4 = mono_time_get(c->mono_time);
2413 } else { 2416 } else {
2414 conn->direct_lastrecv_timev6 = unix_time(); 2417 conn->direct_lastrecv_timev6 = mono_time_get(c->mono_time);
2415 } 2418 }
2416 2419
2417 pthread_mutex_unlock(&conn->mutex); 2420 pthread_mutex_unlock(&conn->mutex);
@@ -2877,7 +2880,7 @@ Crypto_Conn_State crypto_connection_status(const Net_Crypto *c, int crypt_connec
2877 if (direct_connected) { 2880 if (direct_connected) {
2878 *direct_connected = 0; 2881 *direct_connected = 0;
2879 2882
2880 uint64_t current_time = unix_time(); 2883 const uint64_t current_time = mono_time_get(c->mono_time);
2881 2884
2882 if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) { 2885 if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) {
2883 *direct_connected = 1; 2886 *direct_connected = 1;
@@ -2923,7 +2926,7 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk)
2923/* Run this to (re)initialize net_crypto. 2926/* Run this to (re)initialize net_crypto.
2924 * Sets all the global connection variables to their default values. 2927 * Sets all the global connection variables to their default values.
2925 */ 2928 */
2926Net_Crypto *new_net_crypto(const Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info) 2929Net_Crypto *new_net_crypto(const Logger *log, Mono_Time *mono_time, DHT *dht, TCP_Proxy_Info *proxy_info)
2927{ 2930{
2928 if (dht == nullptr) { 2931 if (dht == nullptr) {
2929 return nullptr; 2932 return nullptr;
@@ -2936,8 +2939,9 @@ Net_Crypto *new_net_crypto(const Logger *log, DHT *dht, TCP_Proxy_Info *proxy_in
2936 } 2939 }
2937 2940
2938 temp->log = log; 2941 temp->log = log;
2942 temp->mono_time = mono_time;
2939 2943
2940 temp->tcp_c = new_tcp_connections(dht_get_self_secret_key(dht), proxy_info); 2944 temp->tcp_c = new_tcp_connections(mono_time, dht_get_self_secret_key(dht), proxy_info);
2941 2945
2942 if (temp->tcp_c == nullptr) { 2946 if (temp->tcp_c == nullptr) {
2943 free(temp); 2947 free(temp);