diff options
Diffstat (limited to 'core/net_crypto.c')
-rw-r--r-- | core/net_crypto.c | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/core/net_crypto.c b/core/net_crypto.c index 2dbaa87c..31fb24be 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -48,6 +48,12 @@ typedef struct { | |||
48 | 48 | ||
49 | static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS]; | 49 | static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS]; |
50 | 50 | ||
51 | #define CONN_NO_CONNECTION 0 | ||
52 | #define CONN_HANDSHAKE_SENT 1 | ||
53 | #define CONN_NOT_CONFIRMED 2 | ||
54 | #define CONN_ESTABLISHED 3 | ||
55 | #define CONN_TIMED_OUT 4 | ||
56 | |||
51 | #define MAX_INCOMING 64 | 57 | #define MAX_INCOMING 64 |
52 | 58 | ||
53 | /* keeps track of the connection numbers for friends request so we can check later if they were sent */ | 59 | /* keeps track of the connection numbers for friends request so we can check later if they were sent */ |
@@ -147,7 +153,7 @@ int read_cryptpacket(int crypt_connection_id, uint8_t *data) | |||
147 | { | 153 | { |
148 | if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 154 | if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
149 | return 0; | 155 | return 0; |
150 | if (crypto_connections[crypt_connection_id].status != 3) | 156 | if (crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) |
151 | return 0; | 157 | return 0; |
152 | uint8_t temp_data[MAX_DATA_SIZE]; | 158 | uint8_t temp_data[MAX_DATA_SIZE]; |
153 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); | 159 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); |
@@ -173,7 +179,7 @@ int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length) | |||
173 | return 0; | 179 | return 0; |
174 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) | 180 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) |
175 | return 0; | 181 | return 0; |
176 | if (crypto_connections[crypt_connection_id].status != 3) | 182 | if (crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) |
177 | return 0; | 183 | return 0; |
178 | uint8_t temp_data[MAX_DATA_SIZE]; | 184 | uint8_t temp_data[MAX_DATA_SIZE]; |
179 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, | 185 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
@@ -293,7 +299,7 @@ int getcryptconnection_id(uint8_t *public_key) | |||
293 | { | 299 | { |
294 | uint32_t i; | 300 | uint32_t i; |
295 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { | 301 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
296 | if (crypto_connections[i].status > 0) | 302 | if (crypto_connections[i].status != CONN_NO_CONNECTION) |
297 | if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | 303 | if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) |
298 | return i; | 304 | return i; |
299 | } | 305 | } |
@@ -313,12 +319,12 @@ int crypto_connect(uint8_t *public_key, IP_Port ip_port) | |||
313 | return -1; | 319 | return -1; |
314 | } | 320 | } |
315 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { | 321 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
316 | if (crypto_connections[i].status == 0) { | 322 | if (crypto_connections[i].status == CONN_NO_CONNECTION) { |
317 | int id = new_connection(ip_port); | 323 | int id = new_connection(ip_port); |
318 | if (id == -1) | 324 | if (id == -1) |
319 | return -1; | 325 | return -1; |
320 | crypto_connections[i].number = id; | 326 | crypto_connections[i].number = id; |
321 | crypto_connections[i].status = 1; | 327 | crypto_connections[i].status = CONN_HANDSHAKE_SENT; |
322 | random_nonce(crypto_connections[i].recv_nonce); | 328 | random_nonce(crypto_connections[i].recv_nonce); |
323 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 329 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); |
324 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); | 330 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); |
@@ -372,8 +378,8 @@ int crypto_kill(int crypt_connection_id) | |||
372 | { | 378 | { |
373 | if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 379 | if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
374 | return 1; | 380 | return 1; |
375 | if (crypto_connections[crypt_connection_id].status != 0) { | 381 | if (crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { |
376 | crypto_connections[crypt_connection_id].status = 0; | 382 | crypto_connections[crypt_connection_id].status = CONN_NO_CONNECTION; |
377 | kill_connection(crypto_connections[crypt_connection_id].number); | 383 | kill_connection(crypto_connections[crypt_connection_id].number); |
378 | memset(&crypto_connections[crypt_connection_id], 0 ,sizeof(Crypto_Connection)); | 384 | memset(&crypto_connections[crypt_connection_id], 0 ,sizeof(Crypto_Connection)); |
379 | crypto_connections[crypt_connection_id].number = ~0; | 385 | crypto_connections[crypt_connection_id].number = ~0; |
@@ -396,9 +402,9 @@ int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secre | |||
396 | return -1; | 402 | return -1; |
397 | }*/ | 403 | }*/ |
398 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { | 404 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
399 | if(crypto_connections[i].status == 0) { | 405 | if(crypto_connections[i].status == CONN_NO_CONNECTION) { |
400 | crypto_connections[i].number = connection_id; | 406 | crypto_connections[i].number = connection_id; |
401 | crypto_connections[i].status = 2; | 407 | crypto_connections[i].status = CONN_NOT_CONFIRMED; |
402 | random_nonce(crypto_connections[i].recv_nonce); | 408 | random_nonce(crypto_connections[i].recv_nonce); |
403 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 409 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
404 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 410 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); |
@@ -411,9 +417,9 @@ int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secre | |||
411 | crypto_connections[i].sessionpublic_key) == 1) { | 417 | crypto_connections[i].sessionpublic_key) == 1) { |
412 | increment_nonce(crypto_connections[i].recv_nonce); | 418 | increment_nonce(crypto_connections[i].recv_nonce); |
413 | uint32_t zero = 0; | 419 | uint32_t zero = 0; |
414 | crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */ | 420 | crypto_connections[i].status = CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */ |
415 | write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); | 421 | write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); |
416 | crypto_connections[i].status = 2; /* set it to its proper value right after. */ | 422 | crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */ |
417 | return i; | 423 | return i; |
418 | } | 424 | } |
419 | return -1; /* this should never happen. */ | 425 | return -1; /* this should never happen. */ |
@@ -429,7 +435,7 @@ int is_cryptoconnected(int crypt_connection_id) | |||
429 | { | 435 | { |
430 | if (crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) | 436 | if (crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) |
431 | return crypto_connections[crypt_connection_id].status; | 437 | return crypto_connections[crypt_connection_id].status; |
432 | return 0; | 438 | return CONN_NO_CONNECTION; |
433 | } | 439 | } |
434 | 440 | ||
435 | /* Generate our public and private keys | 441 | /* Generate our public and private keys |
@@ -488,7 +494,7 @@ static void receive_crypto() | |||
488 | { | 494 | { |
489 | uint32_t i; | 495 | uint32_t i; |
490 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { | 496 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
491 | if (crypto_connections[i].status == 1) { | 497 | if (crypto_connections[i].status == CONN_HANDSHAKE_SENT) { |
492 | uint8_t temp_data[MAX_DATA_SIZE]; | 498 | uint8_t temp_data[MAX_DATA_SIZE]; |
493 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 499 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
494 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 500 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
@@ -505,17 +511,17 @@ static void receive_crypto() | |||
505 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 511 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); |
506 | increment_nonce(crypto_connections[i].sent_nonce); | 512 | increment_nonce(crypto_connections[i].sent_nonce); |
507 | uint32_t zero = 0; | 513 | uint32_t zero = 0; |
508 | crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */ | 514 | crypto_connections[i].status = CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */ |
509 | write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); | 515 | write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); |
510 | crypto_connections[i].status = 2; /* set it to its proper value right after. */ | 516 | crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */ |
511 | } | 517 | } |
512 | } | 518 | } |
513 | } else if (id_packet(crypto_connections[i].number) != -1) // This should not happen kill the connection if it does | 519 | } else if (id_packet(crypto_connections[i].number) != -1) // This should not happen kill the connection if it does |
514 | crypto_kill(crypto_connections[i].number); | 520 | crypto_kill(crypto_connections[i].number); |
515 | 521 | ||
516 | } | 522 | } |
517 | if (crypto_connections[i].status == 2) { | 523 | if (crypto_connections[i].status == CONN_NOT_CONFIRMED) { |
518 | if (id_packet(crypto_connections[i].number) == 3) { | 524 | if (id_packet(crypto_connections[i].number) == CONN_ESTABLISHED) { |
519 | uint8_t temp_data[MAX_DATA_SIZE]; | 525 | uint8_t temp_data[MAX_DATA_SIZE]; |
520 | uint8_t data[MAX_DATA_SIZE]; | 526 | uint8_t data[MAX_DATA_SIZE]; |
521 | int length = read_packet(crypto_connections[i].number, temp_data); | 527 | int length = read_packet(crypto_connections[i].number, temp_data); |
@@ -525,7 +531,7 @@ static void receive_crypto() | |||
525 | uint32_t zero = 0; | 531 | uint32_t zero = 0; |
526 | if (len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) { | 532 | if (len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) { |
527 | increment_nonce(crypto_connections[i].recv_nonce); | 533 | increment_nonce(crypto_connections[i].recv_nonce); |
528 | crypto_connections[i].status = 3; | 534 | crypto_connections[i].status = CONN_ESTABLISHED; |
529 | 535 | ||
530 | /* connection is accepted so we disable the auto kill by setting it to about 1 month from now. */ | 536 | /* connection is accepted so we disable the auto kill by setting it to about 1 month from now. */ |
531 | kill_connection_in(crypto_connections[i].number, 3000000); | 537 | kill_connection_in(crypto_connections[i].number, 3000000); |
@@ -554,8 +560,8 @@ static void killTimedout() | |||
554 | { | 560 | { |
555 | uint32_t i; | 561 | uint32_t i; |
556 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { | 562 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
557 | if (crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) | 563 | if (crypto_connections[i].status != CONN_NO_CONNECTION && is_connected(crypto_connections[i].number) == 4) |
558 | crypto_connections[i].status = 4; | 564 | crypto_connections[i].status = CONN_TIMED_OUT; |
559 | else if (is_connected(crypto_connections[i].number) == 4) { | 565 | else if (is_connected(crypto_connections[i].number) == 4) { |
560 | kill_connection(crypto_connections[i].number); | 566 | kill_connection(crypto_connections[i].number); |
561 | crypto_connections[i].number = ~0; | 567 | crypto_connections[i].number = ~0; |