Distinguish member author admission, pending credentials and verification errors
This commit is contained in:
1 parent
93cd72c8b5
commit
8decbb2577
2 files changed
+82
-22
No files matched your search
+67
-22
@@ -1316,45 +1316,90 @@ int member_sync_count(struct UTUN_INSTANCE* inst, const char* ch_id) {
|
||||
return c;
|
||||
}
|
||||
|
||||
/* Полный мембер, доверенный ключ узла, join_sig и подпись дерева приглашений обязательны. */
|
||||
int member_sync_verified_ed25519(struct UTUN_INSTANCE* inst, const char* channel, uint64_t author, uint8_t key[32]) {
|
||||
/* Проверяет удостоверение целиком; ожидание зависимости сохраняет возможность позднего догоняния. */
|
||||
enum ms_author_status member_sync_author_key(struct UTUN_INSTANCE* inst, const char* channel,
|
||||
uint64_t author, uint8_t key[32]) {
|
||||
if (!inst || !inst->topo_sqlite_db || !key || !author || !channel || !channel[0]
|
||||
|| strlen(channel) > 20 || strspn(channel, "0123456789") != strlen(channel)
|
||||
|| (channel[0] == '0' && channel[1])) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMBER_SYNC, "author verification: invalid arguments"); return MS_AUTHOR_ERROR;
|
||||
}
|
||||
char sql[384];
|
||||
snprintf(sql, sizeof(sql), "SELECT x25519_pubkey,ed25519_pubkey,signed_by,signature,join_sig,join_ts"
|
||||
" FROM peers_%s WHERE node_id=? AND source=0 AND deleted=0", channel);
|
||||
sqlite3_stmt* st = NULL; sqlite3* db = inst->topo_sqlite_db;
|
||||
if (sqlite3_prepare_v2(db, sql, -1, &st, NULL) != SQLITE_OK) { DEBUG_ERROR(DEBUG_CATEGORY_MEMBER_SYNC, "verified member lookup: %s", sqlite3_errmsg(db)); return -1; }
|
||||
sqlite3_bind_int64(st, 1, (sqlite3_int64)author);
|
||||
int rc = sqlite3_step(st), ok = 0;
|
||||
int x_bytes = rc == SQLITE_ROW ? sqlite3_column_bytes(st, 0) : 0, ed_bytes = rc == SQLITE_ROW ? sqlite3_column_bytes(st, 1) : 0;
|
||||
int tree_bytes = rc == SQLITE_ROW ? sqlite3_column_bytes(st, 3) : 0, join_bytes = rc == SQLITE_ROW ? sqlite3_column_bytes(st, 4) : 0;
|
||||
const char* reason = "member absent";
|
||||
enum ms_author_status status = MS_AUTHOR_ERROR;
|
||||
const char* reason = "member lookup";
|
||||
uint8_t verified_key[32];
|
||||
int rc = sqlite3_prepare_v2(db, sql, -1, &st, NULL);
|
||||
if (rc == SQLITE_OK) rc = sqlite3_bind_int64(st, 1, (sqlite3_int64)author);
|
||||
if (rc == SQLITE_OK) rc = sqlite3_step(st);
|
||||
if (rc == SQLITE_DONE) { status = MS_AUTHOR_INACTIVE; reason = "member inactive"; goto done; }
|
||||
if (rc != SQLITE_ROW) goto done;
|
||||
int x_bytes = sqlite3_column_bytes(st, 0), ed_bytes = sqlite3_column_bytes(st, 1);
|
||||
int tree_bytes = sqlite3_column_bytes(st, 3), join_bytes = sqlite3_column_bytes(st, 4);
|
||||
if (sqlite3_errcode(db) == SQLITE_NOMEM) goto done;
|
||||
status = MS_AUTHOR_INVALID;
|
||||
reason = "invalid member fields";
|
||||
if (x_bytes != 32 || ed_bytes != 32 || tree_bytes != 64
|
||||
|| join_bytes != 64 || sqlite3_column_int64(st, 5) <= 0) goto done;
|
||||
if (x_bytes != 32 || ed_bytes != 32 || (tree_bytes != 0 && tree_bytes != 64)
|
||||
|| (join_bytes != 0 && join_bytes != 64)) goto done;
|
||||
if (!tree_bytes || !join_bytes || sqlite3_column_int64(st, 5) <= 0
|
||||
|| _sig_is_zero64(sqlite3_column_blob(st, 3)) || _sig_is_zero64(sqlite3_column_blob(st, 4))) {
|
||||
status = MS_AUTHOR_PENDING; reason = "join or invitation pending"; goto done;
|
||||
}
|
||||
struct ms_member_rec member = { .node_id = author, .x25519 = sqlite3_column_blob(st, 0),
|
||||
.ed25519 = sqlite3_column_blob(st, 1), .signed_by = (uint64_t)sqlite3_column_int64(st, 2),
|
||||
.signature = sqlite3_column_blob(st, 3), .join_sig = sqlite3_column_blob(st, 4),
|
||||
.join_ts = (uint64_t)sqlite3_column_int64(st, 5) };
|
||||
reason = "node_id/X25519 mismatch";
|
||||
if (sc_derive_node_id_from_pubkey(member.x25519) != author) goto done;
|
||||
uint64_t derived = sc_derive_node_id_from_pubkey(member.x25519);
|
||||
if (!derived) { status = MS_AUTHOR_ERROR; reason = "identity digest"; goto done; }
|
||||
if (derived != author) goto done;
|
||||
uint8_t trusted[32], x[32], ed[32], body[112];
|
||||
reason = "trusted Ed25519 key unavailable";
|
||||
if (topo_node_sqlite_get_ed25519_pubkey(db, author, trusted) != 0) goto done;
|
||||
int lookup = topo_node_sqlite_get_ed25519_pubkey(db, author, trusted);
|
||||
if (lookup != 0) {
|
||||
status = lookup == TOPO_SQLITE_LOOKUP_MISSING ? MS_AUTHOR_PENDING : MS_AUTHOR_ERROR; goto done;
|
||||
}
|
||||
reason = "trusted Ed25519 key mismatch";
|
||||
if (memcmp(trusted, member.ed25519, 32)) goto done;
|
||||
reason = "channel keys unavailable";
|
||||
if (topo_node_sqlite_channel_get(db, channel, NULL, 0, NULL, x, ed, NULL) != 0) goto done;
|
||||
lookup = topo_node_sqlite_channel_get(db, channel, NULL, 0, NULL, x, ed, NULL);
|
||||
if (lookup != 0) {
|
||||
status = lookup == TOPO_SQLITE_LOOKUP_MISSING ? MS_AUTHOR_PENDING : MS_AUTHOR_ERROR; goto done;
|
||||
}
|
||||
reason = "invalid join signature";
|
||||
int bytes = member_sync_build_join_msg(x, ed, author, member.x25519, member.join_ts, body, sizeof(body));
|
||||
if (bytes != 112 || sc_ed25519_verify(member.ed25519, body, bytes, member.join_sig) != SC_OK) goto done;
|
||||
reason = "invalid or unresolved invitation signature";
|
||||
if (member_sync_verify_signature(db, channel, &member) != 1) goto done;
|
||||
memcpy(key, member.ed25519, 32); ok = 1;
|
||||
if (bytes != 112) { status = MS_AUTHOR_ERROR; reason = "join message build"; goto done; }
|
||||
int signature = sc_ed25519_verify(member.ed25519, body, bytes, member.join_sig);
|
||||
if (signature != SC_OK) {
|
||||
status = signature == SC_ERR_AUTH_FAILED ? MS_AUTHOR_INVALID : MS_AUTHOR_ERROR; goto done;
|
||||
}
|
||||
reason = "invitation verification";
|
||||
signature = member_sync_verify_signature(db, channel, &member);
|
||||
if (signature != 1) {
|
||||
status = signature == MS_VERIFY_DEFERRED ? MS_AUTHOR_PENDING
|
||||
: signature == MS_VERIFY_ERROR ? MS_AUTHOR_ERROR : MS_AUTHOR_INVALID;
|
||||
goto done;
|
||||
}
|
||||
memcpy(verified_key, member.ed25519, 32); status = MS_AUTHOR_ACTIVE; reason = "verified";
|
||||
done:
|
||||
sqlite3_finalize(st);
|
||||
if (ok) return 0;
|
||||
DEBUG_WARN(DEBUG_CATEGORY_MEMBER_SYNC,
|
||||
"member verification failed ch=%s node=%016llx reason=%s rc=%d x_bytes=%d ed_bytes=%d tree_bytes=%d join_bytes=%d",
|
||||
channel, (unsigned long long)author, reason, rc, x_bytes, ed_bytes, tree_bytes, join_bytes); return -1;
|
||||
if (sqlite3_finalize(st) != SQLITE_OK) { status = MS_AUTHOR_ERROR; reason = "member finalize"; }
|
||||
if (status == MS_AUTHOR_ACTIVE) memcpy(key, verified_key, 32);
|
||||
if (status == MS_AUTHOR_ERROR) DEBUG_ERROR(DEBUG_CATEGORY_MEMBER_SYNC,
|
||||
"author verification failed ch=%s node=%016llx reason=%s rc=%d: %s",
|
||||
channel, (unsigned long long)author, reason, rc, sqlite3_errmsg(db));
|
||||
else if (status == MS_AUTHOR_INVALID) DEBUG_WARN(DEBUG_CATEGORY_MEMBER_SYNC,
|
||||
"author invalid ch=%s node=%016llx reason=%s", channel, (unsigned long long)author, reason);
|
||||
else if (status != MS_AUTHOR_ACTIVE) DEBUG_DEBUG(DEBUG_CATEGORY_MEMBER_SYNC,
|
||||
"author unavailable ch=%s node=%016llx status=%d reason=%s", channel, (unsigned long long)author, status, reason);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Строгий допуск radio/attachments: ни ожидание, ни исключение не разрешают действие. */
|
||||
int member_sync_verified_ed25519(struct UTUN_INSTANCE* inst, const char* channel, uint64_t author, uint8_t key[32]) {
|
||||
enum ms_author_status status = member_sync_author_key(inst, channel, author, key);
|
||||
if (status == MS_AUTHOR_ACTIVE) return 0;
|
||||
DEBUG_WARN(DEBUG_CATEGORY_MEMBER_SYNC, "member verification denied ch=%s node=%016llx status=%d",
|
||||
channel ? channel : "-", (unsigned long long)author, status); return -1;
|
||||
}
|
||||
@@ -25,6 +25,21 @@ struct UTUN_INSTANCE;
|
||||
struct TOPO_GROUP;
|
||||
struct sqlite3;
|
||||
|
||||
/* Допуск автора отделён от ожидания ключей и отказа самой проверки. */
|
||||
enum ms_author_status {
|
||||
MS_AUTHOR_ERROR = -1, /* SQL/OpenSSL/аргументы: проверка не выполнена */
|
||||
MS_AUTHOR_INACTIVE = 0, /* участник отсутствует, source!=0 или deleted=1 */
|
||||
MS_AUTHOR_ACTIVE = 1, /* удостоверение полностью проверено */
|
||||
MS_AUTHOR_PENDING = 2, /* ещё нет ключа или подтверждения приглашения */
|
||||
MS_AUTHOR_INVALID = 3 /* неверная подпись, идентичность или формат удостоверения */
|
||||
};
|
||||
|
||||
/* Проверить текущий допуск; возвращает MS_AUTHOR_*, key[32] меняется только при ACTIVE.
|
||||
* Неактивность и ожидание зависимости не являются ошибками БД или доказательством порчи.
|
||||
* inst/channel/key обязательны; channel — канонический десятичный ID канала. */
|
||||
enum ms_author_status member_sync_author_key(struct UTUN_INSTANCE* inst, const char* channel,
|
||||
uint64_t author, uint8_t key[32]);
|
||||
|
||||
/* Проверить source=0/deleted=0, привязку node_id/X25519 и доверенный Ed25519,
|
||||
* join_sig и подпись непосредственного пригласившего (либо ключом канала для signed_by=0).
|
||||
* Не обходит цепочку предков. inst/channel/key обязательны, channel — канонический uint64 decimal.
|
||||
|
||||
Reference in new issue
Block a user