|
|
|
|
@ -54,7 +54,7 @@ static void etcp_link_send_init(struct ETCP_LINK* link, uint8_t reset) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
dgram->link = link; |
|
|
|
|
dgram->noencrypt_len = SC_PUBKEY_SIZE; |
|
|
|
|
dgram->noencrypt_len = SC_PUBKEY_ENC_SIZE; |
|
|
|
|
size_t offset = 0; |
|
|
|
|
|
|
|
|
|
// reset=1: ETCP_INIT_REQUEST (0x02), reset=0: ETCP_INIT_REQUEST_NOINIT (0x04)
|
|
|
|
|
@ -78,10 +78,18 @@ static void etcp_link_send_init(struct ETCP_LINK* link, uint8_t reset) {
|
|
|
|
|
|
|
|
|
|
dgram->data[offset++] = link->local_link_id; |
|
|
|
|
|
|
|
|
|
memcpy(dgram->data + offset, link->etcp->instance->my_keys.public_key, SC_PUBKEY_SIZE); |
|
|
|
|
// Шифруем public_key inplace используя собственный ключ
|
|
|
|
|
sc_sha_transcode(link->etcp->crypto_ctx.peer_public_key, SC_PUBKEY_SIZE, dgram->data + offset, SC_PUBKEY_SIZE); |
|
|
|
|
dgram->data_len = offset + SC_PUBKEY_SIZE; |
|
|
|
|
uint8_t salt[SC_PUBKEY_ENC_SALT_SIZE]; |
|
|
|
|
random_bytes(salt, sizeof(salt)); |
|
|
|
|
memcpy(dgram->data + offset, salt, SC_PUBKEY_ENC_SALT_SIZE); |
|
|
|
|
offset += SC_PUBKEY_ENC_SALT_SIZE; |
|
|
|
|
|
|
|
|
|
uint8_t obfuscated_pubkey[SC_PUBKEY_SIZE]; |
|
|
|
|
sc_obfuscate_pubkey(salt, link->etcp->crypto_ctx.peer_public_key,
|
|
|
|
|
link->etcp->instance->my_keys.public_key, obfuscated_pubkey); |
|
|
|
|
memcpy(dgram->data + offset, obfuscated_pubkey, SC_PUBKEY_SIZE); |
|
|
|
|
offset += SC_PUBKEY_SIZE; |
|
|
|
|
|
|
|
|
|
dgram->data_len = offset; |
|
|
|
|
|
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Sending INIT request to link, node_id=%llu, retry=%d", (unsigned long long)node_id, link->init_retry_count); |
|
|
|
|
|
|
|
|
|
@ -800,7 +808,7 @@ static void etcp_connections_read_callback_socket(socket_t sock, void* arg) {
|
|
|
|
|
|
|
|
|
|
// Try INIT decryption (for incoming connection requests)
|
|
|
|
|
// This handles: no link found, or link without session, or normal decrypt failed
|
|
|
|
|
if (recv_len<=SC_PUBKEY_SIZE) { |
|
|
|
|
if (recv_len <= SC_PUBKEY_ENC_SIZE) { |
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: packet too small for init, size=%zd", recv_len); |
|
|
|
|
errorcode=1; |
|
|
|
|
goto ec_fr; |
|
|
|
|
@ -809,15 +817,18 @@ static void etcp_connections_read_callback_socket(socket_t sock, void* arg) {
|
|
|
|
|
struct secure_channel sc; |
|
|
|
|
sc_init_ctx(&sc, &e_sock->instance->my_keys); |
|
|
|
|
|
|
|
|
|
// Расшифровываем public_key inplace используя собственный ключ
|
|
|
|
|
sc_sha_transcode(e_sock->instance->my_keys.public_key, SC_PUBKEY_SIZE, data + recv_len - SC_PUBKEY_SIZE, SC_PUBKEY_SIZE); |
|
|
|
|
const uint8_t* salt = data + recv_len - SC_PUBKEY_ENC_SIZE; |
|
|
|
|
const uint8_t* encrypted_pubkey = salt + SC_PUBKEY_ENC_SALT_SIZE; |
|
|
|
|
|
|
|
|
|
uint8_t decrypted_pubkey[SC_PUBKEY_SIZE]; |
|
|
|
|
sc_obfuscate_pubkey(salt, e_sock->instance->my_keys.public_key, encrypted_pubkey, decrypted_pubkey); |
|
|
|
|
|
|
|
|
|
if (sc_set_peer_public_key(&sc, &data[recv_len-SC_PUBKEY_SIZE], SC_PEER_PUBKEY_BIN)!=SC_OK) {
|
|
|
|
|
if (sc_set_peer_public_key(&sc, decrypted_pubkey, SC_PEER_PUBKEY_BIN)!=SC_OK) {
|
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_connections_read_callback: failed to set peer public key during init"); |
|
|
|
|
errorcode=2;
|
|
|
|
|
goto ec_fr;
|
|
|
|
|
} |
|
|
|
|
if (sc_decrypt(&sc, data, recv_len-SC_PUBKEY_SIZE, (uint8_t*)&pkt->timestamp, &pkt_len)) { |
|
|
|
|
if (sc_decrypt(&sc, data, recv_len - SC_PUBKEY_ENC_SIZE, (uint8_t*)&pkt->timestamp, &pkt_len)) { |
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_connections_read_callback: failed to decrypt init packet"); |
|
|
|
|
errorcode=3;
|
|
|
|
|
goto ec_fr;
|
|
|
|
|
|