Implement M9.6 narrow secure dev-session protocol
Add protocol v1: versioned, bounded frames (Hello, Welcome, Intent, IntentResult, environment/organism views, Close) carrying session, epoch and commit watermark, with an explicit little-endian canonical codec, a header-first FrameReader, seeded mutation tests and a libFuzzer target. SessionAdmission enforces world/region scope, grant lifetime and skew, single-use grants, one session per subject, epoch, replay, tick window and per-session rate. Dev identity is a 108-byte HMAC-SHA256 grant (PSA) that cannot be created or verified outside the dev environment. Transport security is TLS 1.3 only via pinned Mbed TLS 4.1.1 LTS with TLS 1.2, DTLS, tickets and PSK compiled out, AEAD suites, ECDHE groups and a pinned dev CA. Opt-in EVO_BUILD_NETWORK, new network CI suite. Sockets and region integration follow in M9.7. ADR-0082 records the decision. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
#include "evo/network/protocol.hpp"
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <vector>
|
||||
|
||||
// Server-side session admission for the M9.6 dev session (ADR-0082). Transport-
|
||||
// neutral and single-owner: the host serializes calls. It admits intentions for
|
||||
// scheduling only; the simulation still validates and decides every outcome.
|
||||
namespace evo::network {
|
||||
enum class DeploymentEnvironment : std::uint8_t { dev, stage, world0 };
|
||||
// Claims a dev grant carries; meaningful only once verified.
|
||||
struct DevGrantClaims {
|
||||
std::uint64_t grant_id{}, subject{};
|
||||
world::WorldIdentity world;
|
||||
std::uint64_t region{}, issued_at{}, expires_at{}; // Server clock, seconds.
|
||||
};
|
||||
class DevGrantKey;
|
||||
// Only DevGrantKey::verify constructs this, so admission cannot be handed
|
||||
// unauthenticated claims by accident.
|
||||
class VerifiedGrant {
|
||||
public:
|
||||
[[nodiscard]] const DevGrantClaims& claims() const noexcept {
|
||||
return claims_;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class DevGrantKey;
|
||||
explicit VerifiedGrant(DevGrantClaims claims) : claims_{claims} {}
|
||||
DevGrantClaims claims_;
|
||||
};
|
||||
struct AdmissionConfig {
|
||||
world::WorldIdentity world;
|
||||
std::uint64_t region{}, epoch{};
|
||||
std::size_t max_sessions{16};
|
||||
std::size_t max_remembered_grants{4096};
|
||||
std::uint32_t intent_window{8}; // Ticks ahead of the server tick.
|
||||
std::uint32_t window_ticks{8}; // Rate window length.
|
||||
std::uint32_t max_intents_per_window{16}; // Counts every attempt by a session.
|
||||
std::uint64_t max_grant_lifetime{3600}; // Seconds.
|
||||
std::uint64_t clock_skew{30}; // Seconds tolerated before issued_at.
|
||||
};
|
||||
enum class AdmissionError : std::uint8_t {
|
||||
invalid_config,
|
||||
wrong_world,
|
||||
wrong_region,
|
||||
invalid_lifetime,
|
||||
not_yet_valid,
|
||||
expired,
|
||||
grant_reused,
|
||||
duplicate_subject,
|
||||
sessions_full,
|
||||
grants_full,
|
||||
unknown_session
|
||||
};
|
||||
class SessionAdmission {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<SessionAdmission, AdmissionError>
|
||||
create(AdmissionConfig config);
|
||||
// Single-use grants: a grant id is remembered until it expires. A subject may hold
|
||||
// one session; a second open is refused (control is never duplicated).
|
||||
[[nodiscard]] std::expected<Welcome, AdmissionError> open(const VerifiedGrant& grant,
|
||||
std::uint64_t now_seconds,
|
||||
std::uint64_t server_tick,
|
||||
std::uint64_t commit_watermark);
|
||||
[[nodiscard]] IntentResult admit(const IntentMessage& intent, std::uint64_t server_tick,
|
||||
std::uint64_t now_seconds);
|
||||
[[nodiscard]] std::expected<void, AdmissionError> close(std::uint64_t session);
|
||||
// New ownership epoch: existing sessions become stale and must reconnect.
|
||||
[[nodiscard]] std::expected<void, AdmissionError> advance_epoch(std::uint64_t epoch);
|
||||
[[nodiscard]] std::size_t sessions() const noexcept {
|
||||
return sessions_.size();
|
||||
}
|
||||
[[nodiscard]] std::uint64_t epoch() const noexcept {
|
||||
return config_.epoch;
|
||||
}
|
||||
|
||||
private:
|
||||
struct Session {
|
||||
std::uint64_t id{}, subject{}, epoch{}, expires_at{}, last_sequence{};
|
||||
std::uint64_t window_start{};
|
||||
std::uint32_t attempts{};
|
||||
};
|
||||
struct Remembered {
|
||||
std::uint64_t grant_id{}, expires_at{};
|
||||
};
|
||||
explicit SessionAdmission(AdmissionConfig config) : config_{config} {}
|
||||
AdmissionConfig config_;
|
||||
std::uint64_t next_session_{1};
|
||||
std::vector<Session> sessions_;
|
||||
std::vector<Remembered> grants_;
|
||||
};
|
||||
} // namespace evo::network
|
||||
Reference in New Issue
Block a user