e2e: AES-128-CCM encryption via BGP router (ROUTER_FLAG_ENCRYPTED), ECDH key per peer-pair from NODEINFO pubkeys, cache 8 entries
- etcp_router.h/c: new flag ROUTER_FLAG_ENCRYPTED (0x04), etcp_route_send_encrypted(), inflight/retrans/send_q support - utun_instance.h: e2e_ctx_cache[8] — per-peer sc_context_t cache with derived session key - topo_node.c: topo_node_sign_self() — single point for Ed25519 self-signing of NODEINFO - topo_node.c: fix stale x25519_self_sig after address update (topo_node_sign_self call) - nat_detection.c: fix stale x25519_self_sig after in-place socket meta modification - test_etcp_router_unit: 5 E2E tests (basic round-trip, no NODEINFO drop, tampered, cache reuse, ENCRYPTED+SIGNED combined) - builds clean, 70/70 tests pass
This commit is contained in:
1 parent
691ec8046f
commit
eaeb763412
44 files changed
+2586
-145
No files matched your search
@@ -151,6 +151,54 @@ void topo_node_registry_unref(struct TOPO_GROUPS* groups, uint64_t node_id) {
|
||||
|
||||
// ===== serialization =====
|
||||
|
||||
int topo_node_build_sig_msg(struct TOPO_NODE* ni, uint8_t* buf, size_t buf_size) {
|
||||
if (!ni || !buf || buf_size < 64) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "build_sig_msg: invalid args"); return -1; }
|
||||
size_t off = 0;
|
||||
|
||||
memcpy(buf + off, ni->public_key, SC_PUBKEY_SIZE); off += SC_PUBKEY_SIZE;
|
||||
|
||||
size_t name_len = ni->node_name ? strlen(ni->node_name) : 0;
|
||||
if (name_len > 63) name_len = 63;
|
||||
if (off + name_len + 1 > buf_size) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "build_sig_msg: buf too small for name"); return -1; }
|
||||
if (name_len) { memcpy(buf + off, ni->node_name, name_len); off += name_len; }
|
||||
buf[off++] = '\0';
|
||||
|
||||
if (off + 2 > buf_size) return -1;
|
||||
buf[off++] = ni->client_type;
|
||||
buf[off++] = ni->client_activity;
|
||||
|
||||
{ struct TOPO_SOCKMETA4* e = ni->v4_sock_meta; while (e) {
|
||||
if (off + 3 > buf_size) return -1;
|
||||
buf[off++] = e->id; buf[off++] = e->config_type; buf[off++] = e->nat_type; e = e->next;
|
||||
}}
|
||||
{ struct TOPO_ADDR4* e = ni->v4_addrs; while (e) {
|
||||
if (off + 9 > buf_size) return -1;
|
||||
memcpy(buf + off, e->addr, 4); off += 4;
|
||||
buf[off++] = (uint8_t)(e->port & 0xFF); buf[off++] = (uint8_t)((e->port >> 8) & 0xFF);
|
||||
buf[off++] = e->type; buf[off++] = e->socket_id; buf[off++] = e->protocol; e = e->next;
|
||||
}}
|
||||
{ struct TOPO_SOCKMETA6* e = ni->v6_sock_meta; while (e) {
|
||||
if (off + 3 > buf_size) return -1;
|
||||
buf[off++] = e->id; buf[off++] = e->config_type; buf[off++] = e->nat_type; e = e->next;
|
||||
}}
|
||||
{ struct TOPO_ADDR6* e = ni->v6_addrs; while (e) {
|
||||
if (off + 21 > buf_size) return -1;
|
||||
memcpy(buf + off, e->addr, 16); off += 16;
|
||||
buf[off++] = (uint8_t)(e->port & 0xFF); buf[off++] = (uint8_t)((e->port >> 8) & 0xFF);
|
||||
buf[off++] = e->type; buf[off++] = e->socket_id; buf[off++] = e->protocol; e = e->next;
|
||||
}}
|
||||
|
||||
return (int)off;
|
||||
}
|
||||
|
||||
void topo_node_sign_self(struct UTUN_INSTANCE* instance, struct TOPO_NODE* ni) {
|
||||
if (!instance || !ni) return;
|
||||
uint8_t sig_msg[TOPO_SIG_MSG_MAX_SIZE];
|
||||
int sig_len = topo_node_build_sig_msg(ni, sig_msg, sizeof(sig_msg));
|
||||
if (sig_len > 0 && sc_ed25519_sign(instance->my_ed25519_privkey, sig_msg, (size_t)sig_len, ni->x25519_self_sig) != SC_OK)
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Ed25519 self-sign FAILED for node=%016llx", (unsigned long long)ni->node_id);
|
||||
}
|
||||
|
||||
int topo_node_dyn_size(const struct TOPOMSG_NODE* msg) {
|
||||
return msg->node_name_len
|
||||
+ msg->local_v4_sockets * sizeof(struct TOPOMSG_SOCKMETA4)
|
||||
@@ -184,6 +232,8 @@ int topo_node_serialize(struct TOPO_NODE* ni, struct TOPO_GROUP_NODE* nq, uint8_
|
||||
msg.local_v6_subnets = nq->subnets ? topo_list_count((struct _topo_head*)nq->subnets->v6_subnets) : 0;
|
||||
{ uint8_t hc; uint64_t* hl = topo_node_best_hop_list(nq, &hc, NULL);
|
||||
msg.hop_count = hc; }
|
||||
msg.client_type = ni->client_type;
|
||||
msg.client_activity = ni->client_activity;
|
||||
msg.cumulative_rtt = cumulative_rtt;
|
||||
|
||||
size_t total = TOPOMSG_NODE_HDR_SIZE + (size_t)topo_node_dyn_size(&msg);
|
||||
@@ -248,6 +298,8 @@ int topo_node_deserialize(struct TOPO_GROUP* group, const uint8_t* data, size_t
|
||||
memcpy(ni->public_key, msg->public_key, SC_PUBKEY_SIZE);
|
||||
memcpy(ni->ed25519_public_key, msg->ed25519_public_key, SC_PUBKEY_SIZE);
|
||||
memcpy(ni->x25519_self_sig, msg->x25519_self_sig, 64);
|
||||
ni->client_type = msg->client_type;
|
||||
ni->client_activity = msg->client_activity;
|
||||
|
||||
const uint8_t* dp = data + TOPOMSG_NODE_HDR_SIZE;
|
||||
|
||||
@@ -257,34 +309,42 @@ int topo_node_deserialize(struct TOPO_GROUP* group, const uint8_t* data, size_t
|
||||
dp += msg->node_name_len;
|
||||
}
|
||||
|
||||
for (int i = 0; i < msg->local_v4_sockets; i++) {
|
||||
const struct TOPOMSG_SOCKMETA4* w = (const struct TOPOMSG_SOCKMETA4*)dp; dp += sizeof(*w);
|
||||
struct TOPO_SOCKMETA4* e = memory_pool_alloc(group->instance->topo_groups->v4_sock_meta_pool);
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
e->id = w->id; e->config_type = w->config_type; e->nat_type = w->nat_type;
|
||||
e->next = ni->v4_sock_meta; ni->v4_sock_meta = e;
|
||||
}
|
||||
for (int i = 0; i < msg->local_v4_addrs; i++) {
|
||||
const struct TOPOMSG_ADDR4* w = (const struct TOPOMSG_ADDR4*)dp; dp += sizeof(*w);
|
||||
struct TOPO_ADDR4* e = memory_pool_alloc(group->instance->topo_groups->v4_addr_pool);
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(e->addr, w->addr, 4); e->port = w->port; e->type = w->type; e->socket_id = w->socket_id; e->protocol = w->protocol;
|
||||
e->next = ni->v4_addrs; ni->v4_addrs = e;
|
||||
}
|
||||
for (int i = 0; i < msg->local_v6_sockets; i++) {
|
||||
const struct TOPOMSG_SOCKMETA6* w = (const struct TOPOMSG_SOCKMETA6*)dp; dp += sizeof(*w);
|
||||
struct TOPO_SOCKMETA6* e = memory_pool_alloc(group->instance->topo_groups->v6_sock_meta_pool);
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
e->id = w->id; e->config_type = w->config_type; e->nat_type = w->nat_type;
|
||||
e->next = ni->v6_sock_meta; ni->v6_sock_meta = e;
|
||||
}
|
||||
for (int i = 0; i < msg->local_v6_addrs; i++) {
|
||||
const struct TOPOMSG_ADDR6* w = (const struct TOPOMSG_ADDR6*)dp; dp += sizeof(*w);
|
||||
struct TOPO_ADDR6* e = memory_pool_alloc(group->instance->topo_groups->v6_addr_pool);
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(e->addr, w->addr, 16); e->port = w->port; e->type = w->type; e->socket_id = w->socket_id; e->protocol = w->protocol;
|
||||
e->next = ni->v6_addrs; ni->v6_addrs = e;
|
||||
}
|
||||
{ struct TOPO_SOCKMETA4* head = NULL; struct TOPO_SOCKMETA4* tail = NULL;
|
||||
for (int i = 0; i < msg->local_v4_sockets; i++) {
|
||||
const struct TOPOMSG_SOCKMETA4* w = (const struct TOPOMSG_SOCKMETA4*)dp; dp += sizeof(*w);
|
||||
struct TOPO_SOCKMETA4* e = memory_pool_alloc(group->instance->topo_groups->v4_sock_meta_pool);
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
e->id = w->id; e->config_type = w->config_type; e->nat_type = w->nat_type; e->next = NULL;
|
||||
if (!head) head = tail = e; else { tail->next = e; tail = e; }
|
||||
}
|
||||
ni->v4_sock_meta = head; }
|
||||
{ struct TOPO_ADDR4* head = NULL; struct TOPO_ADDR4* tail = NULL;
|
||||
for (int i = 0; i < msg->local_v4_addrs; i++) {
|
||||
const struct TOPOMSG_ADDR4* w = (const struct TOPOMSG_ADDR4*)dp; dp += sizeof(*w);
|
||||
struct TOPO_ADDR4* e = memory_pool_alloc(group->instance->topo_groups->v4_addr_pool);
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(e->addr, w->addr, 4); e->port = w->port; e->type = w->type; e->socket_id = w->socket_id; e->protocol = w->protocol; e->next = NULL;
|
||||
if (!head) head = tail = e; else { tail->next = e; tail = e; }
|
||||
}
|
||||
ni->v4_addrs = head; }
|
||||
{ struct TOPO_SOCKMETA6* head = NULL; struct TOPO_SOCKMETA6* tail = NULL;
|
||||
for (int i = 0; i < msg->local_v6_sockets; i++) {
|
||||
const struct TOPOMSG_SOCKMETA6* w = (const struct TOPOMSG_SOCKMETA6*)dp; dp += sizeof(*w);
|
||||
struct TOPO_SOCKMETA6* e = memory_pool_alloc(group->instance->topo_groups->v6_sock_meta_pool);
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
e->id = w->id; e->config_type = w->config_type; e->nat_type = w->nat_type; e->next = NULL;
|
||||
if (!head) head = tail = e; else { tail->next = e; tail = e; }
|
||||
}
|
||||
ni->v6_sock_meta = head; }
|
||||
{ struct TOPO_ADDR6* head = NULL; struct TOPO_ADDR6* tail = NULL;
|
||||
for (int i = 0; i < msg->local_v6_addrs; i++) {
|
||||
const struct TOPOMSG_ADDR6* w = (const struct TOPOMSG_ADDR6*)dp; dp += sizeof(*w);
|
||||
struct TOPO_ADDR6* e = memory_pool_alloc(group->instance->topo_groups->v6_addr_pool);
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(e->addr, w->addr, 16); e->port = w->port; e->type = w->type; e->socket_id = w->socket_id; e->protocol = w->protocol; e->next = NULL;
|
||||
if (!head) head = tail = e; else { tail->next = e; tail = e; }
|
||||
}
|
||||
ni->v6_addrs = head; }
|
||||
|
||||
if (out_subnets && (msg->flags & TOPO_FLAG_SEND_SUBNETS) && (msg->local_v4_subnets || msg->local_v6_subnets)) {
|
||||
struct TOPO_NODESUBNETS* r = u_calloc(1, sizeof(struct TOPO_NODESUBNETS));
|
||||
@@ -634,7 +694,9 @@ int topo_group_update_my_nodeinfo(struct UTUN_INSTANCE* instance, struct TOPO_GR
|
||||
|| (memcmp(oni->public_key, instance->my_keys.public_key, SC_PUBKEY_SIZE) != 0)
|
||||
|| (vc6 != (group->local_node->subnets ? topo_list_count((struct _topo_head*)group->local_node->subnets->v6_subnets) : 0))
|
||||
|| (sock6_count != topo_list_count((struct _topo_head*)oni->v6_sock_meta))
|
||||
|| (addr6_count != topo_list_count((struct _topo_head*)oni->v6_addrs));
|
||||
|| (addr6_count != topo_list_count((struct _topo_head*)oni->v6_addrs))
|
||||
|| (oni->client_type != instance->client_type)
|
||||
|| (oni->client_activity != instance->client_activity);
|
||||
if (!changed && oni->v4_sock_meta) {
|
||||
struct ETCP_SOCKET* es = instance->etcp_sockets;
|
||||
struct TOPO_SOCKMETA4* sm = oni->v4_sock_meta;
|
||||
@@ -676,10 +738,10 @@ int topo_group_update_my_nodeinfo(struct UTUN_INSTANCE* instance, struct TOPO_GR
|
||||
ni->group_id = group->group_id;
|
||||
ni->node_id = instance->node_id;
|
||||
ni->ver = (old_ver % 255) + 1;
|
||||
ni->client_type = instance->client_type;
|
||||
ni->client_activity = instance->client_activity;
|
||||
memcpy(ni->public_key, instance->my_keys.public_key, SC_PUBKEY_SIZE);
|
||||
memcpy(ni->ed25519_public_key, group->ed25519_public_key, SC_PUBKEY_SIZE);
|
||||
if (sc_ed25519_sign(instance->my_ed25519_privkey, instance->my_keys.public_key, SC_PUBKEY_SIZE, ni->x25519_self_sig) != SC_OK)
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Ed25519 self-sign FAILED for my node=%016llx", (unsigned long long)ni->node_id);
|
||||
if (name_len) { ni->node_name = u_malloc(name_len + 1); if (ni->node_name) { memcpy(ni->node_name, instance->name, name_len); ni->node_name[name_len] = 0; } }
|
||||
|
||||
{ uint64_t saved_node_id = ni->node_id; uint32_t saved_ver = ni->ver;
|
||||
@@ -743,6 +805,8 @@ int topo_group_update_my_nodeinfo(struct UTUN_INSTANCE* instance, struct TOPO_GR
|
||||
ts = ts->next; }
|
||||
}
|
||||
|
||||
topo_node_sign_self(instance, ni);
|
||||
|
||||
if (group->group_type != TOPO_GROUP_TYPE_CHAT && (vc || vc6)) {
|
||||
struct TOPO_NODESUBNETS* r = u_calloc(1, sizeof(struct TOPO_NODESUBNETS));
|
||||
s = instance->config->my_subnets;
|
||||
@@ -848,6 +912,8 @@ void topo_node_update_my_addresses(struct UTUN_INSTANCE* instance) {
|
||||
ni->ver = (ni->ver % 255) + 1;
|
||||
DEBUG_INFO(DEBUG_CATEGORY_BGP, "my addresses updated, new ver=%d", ni->ver);
|
||||
|
||||
topo_node_sign_self(instance, ni);
|
||||
|
||||
struct ll_entry* ge = instance->topo_groups->group_list->head;
|
||||
while (ge) {
|
||||
struct TOPO_GROUP* g = (struct TOPO_GROUP*)ge;
|
||||
|
||||
Reference in new issue
Block a user