diff --git a/CMakeLists.txt b/CMakeLists.txt index 0080944..03d9878 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -144,6 +144,13 @@ if(EVO_BUILD_QUIC) target_include_directories(evo_network_quic PUBLIC "${PROJECT_SOURCE_DIR}/include") target_link_libraries(evo_network_quic PUBLIC evo_msquic PRIVATE mbedtls) evo_owned_target(evo_network_quic) + # M10.3b session login over QUIC (ADR-0091): region side and client side. + add_library(evo_session_server STATIC src/server/session/region_sessions.cpp) + target_link_libraries(evo_session_server PUBLIC evo_network_quic evo_network_tls) + evo_owned_target(evo_session_server) + add_library(evo_session_client STATIC src/client/session/region_session.cpp) + target_link_libraries(evo_session_client PUBLIC evo_network_quic evo_network_protocol) + evo_owned_target(evo_session_client) if(BUILD_TESTING) # Owned impairment relay (tests and measurements only). add_library(evo_network_impair STATIC tools/network/impair/udp_relay.cpp) @@ -163,6 +170,12 @@ if(EVO_BUILD_QUIC) evo_msquic_runtime(evo-quic-hol-measure) add_test(NAME quic-transport COMMAND evo-quic-transport-tests) set_tests_properties(quic-transport PROPERTIES TIMEOUT 180 LABELS "quic") + add_executable(evo-quic-session-tests tests/quic_sessions.cpp) + target_link_libraries(evo-quic-session-tests PRIVATE evo_session_server evo_session_client) + evo_owned_target(evo-quic-session-tests) + evo_msquic_runtime(evo-quic-session-tests) + add_test(NAME quic-sessions COMMAND evo-quic-session-tests) + set_tests_properties(quic-sessions PROPERTIES TIMEOUT 120 LABELS "quic") add_executable(evo-quic-smoke-tests tests/quic_smoke.cpp) target_link_libraries(evo-quic-smoke-tests PRIVATE evo_msquic evo_network_tls) evo_owned_target(evo-quic-smoke-tests) diff --git a/CMakePresets.json b/CMakePresets.json index 6196970..085684b 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -24,7 +24,7 @@ {"name": "client", "configurePreset": "client", "targets": ["evo-client-state-tests", "evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-client-ui-model-tests", "evo-client-panel-tests", "evo-client-bindings-tests"]}, {"name": "client-probe", "configurePreset": "client-probe", "targets": ["evo-client-probe"]}, {"name": "network", "configurePreset": "network", "targets": ["evo-client-state-tests", "evo-network-tests", "evo-dev-session"]}, - {"name": "quic", "configurePreset": "quic", "targets": ["evo-quic-smoke-tests", "evo-quic-transport-tests", "evo-quic-hol-measure", "evo-network-tests"]}, + {"name": "quic", "configurePreset": "quic", "targets": ["evo-quic-smoke-tests", "evo-quic-transport-tests", "evo-quic-session-tests", "evo-quic-hol-measure", "evo-network-tests"]}, {"name": "client-network", "configurePreset": "client-network", "targets": ["evo-client-state-tests", "evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-client-ui-model-tests", "evo-client-panel-tests", "evo-client-bindings-tests", "evo-network-tests", "evo-dev-session"]}, {"name": "client-package", "configurePreset": "client-package", "targets": ["evo-client-state-tests", "evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-client-ui-model-tests", "evo-client-panel-tests", "evo-client-bindings-tests", "evo-network-tests", "evo-dev-session"]} ], diff --git a/cmake/Network.cmake b/cmake/Network.cmake index 657348c..cf542b2 100644 --- a/cmake/Network.cmake +++ b/cmake/Network.cmake @@ -31,7 +31,8 @@ add_subdirectory("${mbedtls_source}" "${PROJECT_BINARY_DIR}/mbedtls" EXCLUDE_FRO # Dependency-free protocol: framing, envelopes, admission. No Mbed TLS types. add_library(evo_network_protocol STATIC src/network/protocol/codec.cpp - src/network/protocol/admission.cpp src/network/protocol/session_control.cpp) + src/network/protocol/admission.cpp src/network/protocol/session_control.cpp + src/network/protocol/session_protocol.cpp) target_link_libraries(evo_network_protocol PUBLIC evo_client_views evo_client_controls) evo_owned_target(evo_network_protocol) # Mbed TLS adapters: dev grants (HMAC-SHA256 via PSA) and the TLS 1.3 channel. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 24614c3..ecd875a 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -520,8 +520,8 @@ hardware (10.9). 10.2 split: 10.2a selection, pins and build of msquic/OpenSSL d (ADR-0090); 10.2b bounded channels, exact CA pinning and the impairment relay done; 10.2c loss/head-of-line measurements done (networking/M10_TRANSPORT_MEASUREMENTS.md). 10.3 split (ADR-0091): 10.3a account ids, ECDSA grants, identity adapter and region -session control done; 10.3b QUIC session handshake and 10.3c durable control lease -follow. 10.4-10.9 planned. +session control done; 10.3b QUIC session login (schemas/network/session-v1.md) done; +10.3c durable control lease follows. 10.4-10.9 planned. **Entry:** M9 client/session evidence and M7/M8 dev/stage durability. Read NETWORK_ARCHITECTURE, PERSISTENCE_ARCHITECTURE and ADR-0017. The M9 transport is diff --git a/docs/STATUS.md b/docs/STATUS.md index 2b16191..56f7086 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -19,8 +19,10 @@ loss the ordered stream's p95 latency is 3-5.5x the datagram channel's and excee (networking/M10_TRANSPORT_MEASUREMENTS.md). M10.2 is complete. M10.3a (ADR-0091) adds provider-independent account ids, ECDSA P-256 session grants verified with public keys only, the identity adapter and grant issuer library, and per-region session control (newest grant wins and replaces the old session, -replay and stale grants refused, in-session refresh, attempt rate limits). Next: M10.3b -QUIC session handshake, then the durable control lease (10.3c). +replay and stale grants refused, in-session refresh, attempt rate limits). M10.3b adds the +session login over QUIC (session-v1: Hello, Welcome, Refresh, Close with reasons) on both +the region and client side, tested end to end on loopback. Next: the durable control +lease (10.3c). ## Previous gate - 2026-10-02 diff --git a/docs/adr/0091-m10-session-identity-and-control.md b/docs/adr/0091-m10-session-identity-and-control.md index b0fbf6b..54b7736 100644 --- a/docs/adr/0091-m10-session-identity-and-control.md +++ b/docs/adr/0091-m10-session-identity-and-control.md @@ -84,3 +84,22 @@ are later work behind these interfaces. never logged; revocation (bans) and channel binding are not designed yet. - Private-key provisioning: `GrantSigningKey::import` takes a raw 32-byte P-256 scalar; a provisioning tool and secret handling come with the issuer service. + +## M10.3b implementation (2026-10-03) +Session protocol v1 session messages ([session-v1](../../schemas/network/session-v1.md)): +Hello (grant), Welcome, Refresh, RefreshResult and Close with a reason, each a fixed-size +message with an `EVSN` header on the QUIC reliable stream. +- **Region side** (`RegionSessions`, src/server/session): owns the QUIC endpoint, the + verifier and SessionControl on the region host's thread. Hello deadline 5 s; region + attempts counted before signature work; a takeover closes the previous connection with + `superseded` before Welcome; grant expiry and epoch advances close sessions; every + close sends Close and then closes the connection after a flush of at most 1 s. A peer + that closes its own connection is reported as `client_closed` (the client's Close can + be discarded by its own immediate close). Connections are bounded by the transport limit + (at least the session limit; the rest is headroom for logins and closes still flushing). + Events carry account log labels only. +- **Client side** (`RegionSessionClient`, src/client/session): sends Hello when the pinned + connection is up and then zeroes its copy of the grant; Welcome deadline 5 s; refresh in + place; any Close, malformed message, deadline or transport loss ends the session. +- Not yet wired into evo-server or the engineering client: they keep the M9 dev path + until the durable control lease (10.3c) exists, so nothing deploys M10 sessions. diff --git a/docs/architecture/NETWORK_ARCHITECTURE.md b/docs/architecture/NETWORK_ARCHITECTURE.md index 361af1d..cf01c7e 100644 --- a/docs/architecture/NETWORK_ARCHITECTURE.md +++ b/docs/architecture/NETWORK_ARCHITECTURE.md @@ -8,7 +8,8 @@ pinning (`evo_network_quic`, M10.2b); loss/head-of-line measurements (M10.2c, [M10_TRANSPORT_MEASUREMENTS](../networking/M10_TRANSPORT_MEASUREMENTS.md)) confirm superseding state on DATAGRAMs and reliable actions on the ordered stream. Session identity and control (ADR-0091, M10.3): ECDSA-signed account-scoped grants -([session-grant-v1](../../schemas/network/session-grant-v1.md)), newest grant wins. The scoped M9 dev session (TLS 1.3, protocol v1, dev-only grants) +([session-grant-v1](../../schemas/network/session-grant-v1.md)), newest grant wins; login over +QUIC per [session-v1](../../schemas/network/session-v1.md). The scoped M9 dev session (TLS 1.3, protocol v1, dev-only grants) is specified in [DEV_SESSION_PROTOCOL](../networking/DEV_SESSION_PROTOCOL.md) and ADR-0082. M0 has no networking. Steam distribution follows later through an identity-provider adapter; core account IDs are independent of Steam IDs. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 14de434..7e26c9a 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -4121,3 +4121,32 @@ ASan/UBSan (Debug, leak detection) `evo-network-tests`; clang-tidy 19 on the new and tests; format check; Windows MSVC 19.44 preset `network` build and CTest 1/1. Not done: QUIC session handshake (10.3b), durable control lease and serial persistence across restarts and regions (10.3c), revocation, private-key provisioning tooling. + +## 2026-10-03 — M10.3b session login over QUIC (ADR-0091) + +Implemented the session-v1 codec (evo_network_protocol), `RegionSessions` +(evo_session_server) and `RegionSessionClient` (evo_session_client); tests +`evo-quic-session-tests` (label `quic`) and codec tests in `evo-network-tests`. +Codec: round trips of every message; every truncation, an extra byte, bad magic, other +version, flags, unknown types and invalid reasons/statuses/zero ids refused. +End to end on loopback QUIC with an injected wall clock: Welcome fields and holder; +replay (`stale_grant`); newer grant superseding the old connection (Close `superseded`); +older unexpired grant refused; refresh accepted, stale and wrong-account refusals with the +session kept; wrong region and stage-signed grant (`wrong_scope`), unknown issuer and a +modified grant (`unauthenticated`); expired grant at login and session expiry closing +only the expired session; client close releasing control; silent peer (`timeout`), +garbage, wrong first message and unknown required feature (`protocol`), other version +(`unsupported_version`); epoch advance (`stale_epoch`) and Welcome with the new epoch; +shutdown; capacity refusal not consuming the grant; region attempt limit. + +Found and fixed during development: a connection cap of twice the session limit refused +new logins while refused connections were still flushing their Close (now the transport +limit, which must be at least the session limit). + +Executed: Linux GCC 14 preset `quic` build and CTest `-L "quic|network"` 4/4; session +tests 10 consecutive runs 10/10; Clang 19 ASan/UBSan (Debug, leak detection) session and +network tests (msquic itself uninstrumented); clang-tidy 19 on the new sources and tests; +format check; Windows MSVC 19.44 preset `quic` build and CTest 4/4 (binary rebuilt and +timestamp checked). +Not done: wiring into evo-server and the engineering client, the durable control lease +(10.3c), an impairment-relay run of the login path. diff --git a/include/evo/client/region_session.hpp b/include/evo/client/region_session.hpp new file mode 100644 index 0000000..92442ce --- /dev/null +++ b/include/evo/client/region_session.hpp @@ -0,0 +1,48 @@ +#pragma once +#include "evo/network/quic_transport.hpp" +#include "evo/network/session_protocol.hpp" +#include +#include +#include +#include +#include + +// M10.3b client-side session login over QUIC (ADR-0091, schemas/network/session-v1.md). +// Owned by one thread, which calls poll(). Sends Hello with the grant once the pinned +// connection is up (then forgets the grant), waits for Welcome or Close, and can renew +// the session in place with a newer grant. A Close from the server, a malformed message, +// the Welcome deadline or a lost transport ends the session; reconnecting needs a new +// client and a fresh grant (ADR-0089). Intentions and views are later segments. +namespace evo::client { +struct RegionSessionClientConfig { + network::quic::ClientConfig transport; + network::WireGrant grant{}; + std::chrono::milliseconds welcome_deadline{5000}; // Connect to Welcome. +}; +enum class RegionSessionState : std::uint8_t { connecting, awaiting_welcome, established, closed }; + +class RegionSessionClient { + public: + [[nodiscard]] static std::expected, + network::quic::EndpointError> + connect(RegionSessionClientConfig config, std::chrono::steady_clock::time_point now); + RegionSessionClient(const RegionSessionClient&) = delete; + RegionSessionClient& operator=(const RegionSessionClient&) = delete; + ~RegionSessionClient(); + void poll(std::chrono::steady_clock::time_point now); + // Sends a newer grant for the open session; false unless established and sent. + [[nodiscard]] bool refresh(const network::WireGrant& grant); + // The latest refresh answer, once (an accepted one also updates welcome().expires_at). + [[nodiscard]] std::optional take_refresh_result(); + // Sends Close{client_closed} and closes the connection. + void close(); + [[nodiscard]] RegionSessionState state() const noexcept; + [[nodiscard]] const std::optional& welcome() const noexcept; + [[nodiscard]] std::optional close_reason() const noexcept; + + private: + struct Impl; + explicit RegionSessionClient(std::unique_ptr impl); + std::unique_ptr impl_; +}; +} // namespace evo::client diff --git a/include/evo/network/session_protocol.hpp b/include/evo/network/session_protocol.hpp new file mode 100644 index 0000000..bb4397b --- /dev/null +++ b/include/evo/network/session_protocol.hpp @@ -0,0 +1,85 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +// M10 session protocol v1, session part (ADR-0091, schemas/network/session-v1.md). +// Messages travel on the QUIC reliable stream (one message per framed QUIC message). +// Each starts with an 8-byte header: magic "EVSN", version u16, type u8, flags u8 (0); +// every payload has a fixed size and explicit little-endian fields. Grants are opaque +// 160-byte credentials here. Later M10 segments add message types under a new version. +namespace evo::network { +inline constexpr std::uint32_t session_magic = 0x4E535645; // Bytes "EVSN". +inline constexpr std::uint16_t session_protocol_version = 1; +inline constexpr std::size_t session_header_bytes = 8; +inline constexpr std::size_t session_grant_wire_bytes = 160; + +enum class SessionMessageType : std::uint8_t { + hello = 1, // Client -> server: grant. + welcome, // Server -> client. + refresh, // Client -> server: newer grant for the open session. + refresh_result, // Server -> client. + close // Either way, then the sender closes the connection. +}; +// Wire values 1..13. `connection_lost` is local only (the transport ended without a Close). +enum class SessionCloseReason : std::uint8_t { + client_closed = 1, + superseded, // A newer grant for the same account opened another session. + expired, + stale_epoch, + unauthenticated, + wrong_scope, // Environment, world or region. + stale_grant, // Replayed or older than a grant already presented. + rate_limited, + capacity, + protocol, + timeout, // No Hello before the deadline. + unsupported_version, + shutdown, + connection_lost = 255 +}; +enum class SessionCodecError : std::uint8_t { + truncated, + bad_magic, + unsupported_version, + unknown_type, + reserved_flags, + wrong_length, + invalid_value +}; + +using WireGrant = std::array; +struct SessionHello { + std::uint32_t required_features{}; // Must be zero in v1. + WireGrant grant{}; +}; +struct SessionWelcome { + std::uint64_t session{}, epoch{}, world{}, frame{}, region{}, expires_at{}; + std::uint64_t server_seconds{}; // Server wall clock at Welcome, for refresh timing. + friend bool operator==(const SessionWelcome&, const SessionWelcome&) = default; +}; +struct SessionRefresh { + WireGrant grant{}; +}; +// accepted: 0 and the new expiry. Otherwise a refusal reason (stale_grant, +// unauthenticated, wrong_scope, expired, rate_limited); the session keeps its expiry. +struct SessionRefreshResult { + std::uint8_t status{}; + std::uint64_t expires_at{}; + friend bool operator==(const SessionRefreshResult&, const SessionRefreshResult&) = default; +}; +struct SessionClose { + SessionCloseReason reason{}; +}; +using SessionMessage = + std::variant; + +[[nodiscard]] std::vector encode_session(const SessionMessage& message); +// Exactly one message; any other length for its type rejects. +[[nodiscard]] std::expected +decode_session(std::span bytes); +} // namespace evo::network diff --git a/include/evo/server/region_sessions.hpp b/include/evo/server/region_sessions.hpp new file mode 100644 index 0000000..9739722 --- /dev/null +++ b/include/evo/server/region_sessions.hpp @@ -0,0 +1,66 @@ +#pragma once +#include "evo/network/quic_transport.hpp" +#include "evo/network/session_control.hpp" +#include "evo/network/session_grant.hpp" +#include "evo/network/session_protocol.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +// M10.3b region-side session login over QUIC (ADR-0091, schemas/network/session-v1.md). +// Owned by the region host's thread, which calls poll(). A connection must send Hello +// with a grant before the deadline; the grant is verified with the region's issuer +// public keys and admitted by SessionControl. A newer grant for an account closes the +// account's previous connection with `superseded`. Refusals and closes send Close with a +// reason, then the connection is closed after a short flush. Intentions and views are +// later segments (10.4, 10.5). Events carry account log labels only, never grants. +namespace evo::server { +struct IssuerKey { + std::uint32_t key_id{}; + network::GrantPublicKey public_key{}; +}; +struct RegionSessionsConfig { + network::quic::ServerConfig transport; + network::ControlConfig control; // `epoch` is the initial ownership epoch. + std::vector issuer_keys; + std::chrono::milliseconds hello_deadline{5000}; // Connected to Hello (ADR-0089). + std::chrono::milliseconds close_flush{1000}; // Close sent to connection closed. +}; +enum class RegionSessionsError : std::uint8_t { configuration, keys, transport }; +struct RegionSessionEvent { + enum class Kind : std::uint8_t { opened, closed, refused } kind{}; + std::uint64_t session{}; // 0 for refusals. + std::string account; // log_label, empty when unauthenticated. + network::SessionCloseReason reason{}; +}; + +class RegionSessions { + public: + [[nodiscard]] static std::expected, RegionSessionsError> + create(RegionSessionsConfig config); + RegionSessions(const RegionSessions&) = delete; + RegionSessions& operator=(const RegionSessions&) = delete; + ~RegionSessions(); + // `epoch` is the region's current ownership epoch: a newer one closes every session + // (stale_epoch). `now_seconds` is wall time for grants, `now` monotonic for deadlines. + void poll(std::uint64_t epoch, std::uint64_t now_seconds, + std::chrono::steady_clock::time_point now); + // Closes every connection with `shutdown` (flushed by later polls). + void shutdown(std::chrono::steady_clock::time_point now); + [[nodiscard]] std::vector take_events(); + [[nodiscard]] std::optional holder(const network::AccountId& account) const; + [[nodiscard]] std::size_t sessions() const noexcept; + [[nodiscard]] std::size_t connections() const noexcept; + [[nodiscard]] std::uint16_t port() const noexcept; + + private: + struct Impl; + explicit RegionSessions(std::unique_ptr impl); + std::unique_ptr impl_; +}; +} // namespace evo::server diff --git a/schemas/network/session-v1.md b/schemas/network/session-v1.md new file mode 100644 index 0000000..c6865ce --- /dev/null +++ b/schemas/network/session-v1.md @@ -0,0 +1,49 @@ +# M10 session protocol, version 1 (session messages) + +Login and session control over the QUIC reliable stream (ADR-0090 transport, ADR-0091 +identity). Implemented by `encode_session`/`decode_session` +(include/evo/network/session_protocol.hpp), `RegionSessions` (server) and +`RegionSessionClient` (client). Each message is one framed QUIC reliable message. +Integers little-endian. Every message has a fixed length for its type; any other length, +magic, version, nonzero flags, unknown type or out-of-range value is refused, and the +server then sends Close (`unsupported_version` for another version, otherwise +`protocol`) and closes the connection. Later M10 segments add types in a new version. + +## Header (8 bytes) +| Offset | Size | Field | Rule | +|---|---|---|---| +| 0 | 4 | magic | bytes `EVSN` | +| 4 | 2 | version | 1 | +| 6 | 1 | type | 1..5 below | +| 7 | 1 | flags | 0 | + +## Messages +| Type | Direction | Payload | Size | +|---|---|---|---| +| 1 Hello | client -> server | required_features u32 (0), grant (session-grant-v1, 160 bytes) | 164 | +| 2 Welcome | server -> client | session, epoch, world, frame, region, expires_at, server_seconds (u64 each; session and epoch nonzero) | 56 | +| 3 Refresh | client -> server | newer grant for the same account (160 bytes) | 160 | +| 4 RefreshResult | server -> client | status u8 (0 accepted, else a close reason value), expires_at u64 | 9 | +| 5 Close | either | reason u8 | 1 | + +Close reasons (wire 1..13): 1 client_closed, 2 superseded, 3 expired, 4 stale_epoch, +5 unauthenticated, 6 wrong_scope, 7 stale_grant, 8 rate_limited, 9 capacity, +10 protocol, 11 timeout, 12 unsupported_version, 13 shutdown. Value 255 +(`connection_lost`) is local only and never sent. + +## Sequence and rules +- The client sends Hello first, as soon as the pinned QUIC connection is up. The server + closes with `timeout` if no Hello arrives within 5 s of connecting, and refuses any + other first message with `protocol`. +- Region-wide attempts are counted before the signature is checked (`rate_limited`). + The grant must verify with one of the region's issuer keys (`unauthenticated`) for the + region's environment (`wrong_scope`), then pass session control: world/region scope, + lifetime, expiry, serial (`stale_grant`), per-account attempts and capacity. +- Welcome confirms the session; `expires_at` is the grant's expiry. A newer grant for an + account with an open session closes that session's connection with `superseded`. +- Refresh renews the open session with a newer grant; a refusal leaves the session and + its expiry unchanged. The server closes a session with `expired` when its expiry + passes, and every session with `stale_epoch` when the region's ownership epoch advances. +- Close is followed by closing the connection (the server waits up to 1 s for delivery). + A client that closes its connection without Close is treated as `client_closed`. +- Reconnecting needs a new connection and a newer grant (ADR-0089). diff --git a/src/client/session/region_session.cpp b/src/client/session/region_session.cpp new file mode 100644 index 0000000..fca4f42 --- /dev/null +++ b/src/client/session/region_session.cpp @@ -0,0 +1,127 @@ +#include "evo/client/region_session.hpp" +#include + +namespace evo::client { +namespace { +using network::SessionCloseReason; +using Clock = std::chrono::steady_clock; +} // namespace + +struct RegionSessionClient::Impl { + std::unique_ptr endpoint; + network::WireGrant grant{}; + Clock::time_point deadline; + RegionSessionState state{RegionSessionState::connecting}; + network::quic::ConnectionId connection{}; + std::optional welcome; + std::optional reason; + std::optional refresh_result; + + bool send(const network::SessionMessage& m) { + return connection != 0 && + endpoint->send_message(connection, network::encode_session(m)).has_value(); + } + // Ends the session locally; `tell` sends a Close first (best effort). + void end(SessionCloseReason why, bool tell) { + if (state == RegionSessionState::closed) + return; + if (tell) + (void)send(network::SessionClose{why}); + state = RegionSessionState::closed; + reason = why; + grant.fill(0); + if (connection != 0) + endpoint->close(connection); + } + void message(std::span bytes) { + const auto decoded = network::decode_session(bytes); + if (!decoded) + return end(SessionCloseReason::protocol, true); + if (const auto* m = std::get_if(&*decoded)) + return end(m->reason, false); + if (state == RegionSessionState::awaiting_welcome) { + if (const auto* m = std::get_if(&*decoded)) { + welcome = *m; + state = RegionSessionState::established; + return; + } + } else if (state == RegionSessionState::established) { + if (const auto* m = std::get_if(&*decoded)) { + if (m->status == 0) + welcome->expires_at = m->expires_at; + refresh_result = *m; + return; + } + } + end(SessionCloseReason::protocol, true); + } +}; + +RegionSessionClient::RegionSessionClient(std::unique_ptr impl) : impl_{std::move(impl)} {} +RegionSessionClient::~RegionSessionClient() = default; + +std::expected, network::quic::EndpointError> +RegionSessionClient::connect(RegionSessionClientConfig config, Clock::time_point now) { + auto endpoint = network::quic::Endpoint::connect(config.transport); + if (!endpoint) + return std::unexpected(endpoint.error()); + auto impl = std::make_unique(); + impl->endpoint = std::move(*endpoint); + impl->grant = config.grant; + config.grant.fill(0); + impl->deadline = now + config.welcome_deadline; + return std::unique_ptr{new RegionSessionClient{std::move(impl)}}; +} + +void RegionSessionClient::poll(Clock::time_point now) { + auto& m = *impl_; + for (auto& e : m.endpoint->poll()) { + using network::quic::EventKind; + if (m.state == RegionSessionState::closed) + continue; + if (e.kind == EventKind::connected && m.state == RegionSessionState::connecting) { + m.connection = e.connection; + m.state = RegionSessionState::awaiting_welcome; + network::SessionHello hello; + hello.grant = m.grant; + m.grant.fill(0); // The grant is a credential: keep it no longer than needed. + if (!m.send(hello)) + m.end(SessionCloseReason::connection_lost, false); + } else if (e.kind == EventKind::message) { + m.message(e.bytes); + } else if (e.kind == EventKind::closed) { + m.connection = 0; // Already gone; nothing to close or send. + m.end(SessionCloseReason::connection_lost, false); + } + } + if ((m.state == RegionSessionState::connecting || + m.state == RegionSessionState::awaiting_welcome) && + now >= m.deadline) + m.end(SessionCloseReason::timeout, false); +} + +bool RegionSessionClient::refresh(const network::WireGrant& grant) { + return impl_->state == RegionSessionState::established && + impl_->send(network::SessionRefresh{grant}); +} + +std::optional RegionSessionClient::take_refresh_result() { + return std::exchange(impl_->refresh_result, std::nullopt); +} + +void RegionSessionClient::close() { + impl_->end(SessionCloseReason::client_closed, true); +} + +RegionSessionState RegionSessionClient::state() const noexcept { + return impl_->state; +} + +const std::optional& RegionSessionClient::welcome() const noexcept { + return impl_->welcome; +} + +std::optional RegionSessionClient::close_reason() const noexcept { + return impl_->reason; +} +} // namespace evo::client diff --git a/src/network/protocol/session_protocol.cpp b/src/network/protocol/session_protocol.cpp new file mode 100644 index 0000000..3fa28f8 --- /dev/null +++ b/src/network/protocol/session_protocol.cpp @@ -0,0 +1,134 @@ +#include "evo/network/session_protocol.hpp" +#include + +namespace evo::network { +namespace { +constexpr std::size_t hello_bytes = 4 + session_grant_wire_bytes; +constexpr std::size_t welcome_bytes = std::size_t{7} * 8; +constexpr std::size_t refresh_bytes = session_grant_wire_bytes; +constexpr std::size_t refresh_result_bytes = 1 + 8; +constexpr std::size_t close_bytes = 1; + +void put(std::vector& out, std::uint64_t v, std::size_t count) { + for (std::size_t i = 0; i < count; ++i) + out.push_back(static_cast(v >> (8 * i))); +} +std::uint64_t get(std::span in, std::size_t at, std::size_t count) { + std::uint64_t v = 0; + for (std::size_t i = 0; i < count; ++i) + v |= static_cast(in[at + i]) << (8 * i); + return v; +} +std::vector header(SessionMessageType type, std::size_t payload) { + std::vector out; + out.reserve(session_header_bytes + payload); + put(out, session_magic, 4); + put(out, session_protocol_version, 2); + out.push_back(static_cast(type)); + out.push_back(0); + return out; +} +bool wire_reason(std::uint64_t v) { + return v >= static_cast(SessionCloseReason::client_closed) && + v <= static_cast(SessionCloseReason::shutdown); +} +WireGrant grant_at(std::span in, std::size_t at) { + WireGrant g{}; + std::copy_n(in.begin() + static_cast(at), g.size(), g.begin()); + return g; +} +} // namespace + +std::vector encode_session(const SessionMessage& message) { + struct Encoder { + std::vector operator()(const SessionHello& m) const { + auto out = header(SessionMessageType::hello, hello_bytes); + put(out, m.required_features, 4); + out.insert(out.end(), m.grant.begin(), m.grant.end()); + return out; + } + std::vector operator()(const SessionWelcome& m) const { + auto out = header(SessionMessageType::welcome, welcome_bytes); + for (const auto v : + {m.session, m.epoch, m.world, m.frame, m.region, m.expires_at, m.server_seconds}) + put(out, v, 8); + return out; + } + std::vector operator()(const SessionRefresh& m) const { + auto out = header(SessionMessageType::refresh, refresh_bytes); + out.insert(out.end(), m.grant.begin(), m.grant.end()); + return out; + } + std::vector operator()(const SessionRefreshResult& m) const { + auto out = header(SessionMessageType::refresh_result, refresh_result_bytes); + out.push_back(m.status); + put(out, m.expires_at, 8); + return out; + } + std::vector operator()(const SessionClose& m) const { + auto out = header(SessionMessageType::close, close_bytes); + out.push_back(static_cast(m.reason)); + return out; + } + }; + return std::visit(Encoder{}, message); +} + +std::expected +decode_session(std::span bytes) { + if (bytes.size() < session_header_bytes) + return std::unexpected(SessionCodecError::truncated); + if (get(bytes, 0, 4) != session_magic) + return std::unexpected(SessionCodecError::bad_magic); + if (get(bytes, 4, 2) != session_protocol_version) + return std::unexpected(SessionCodecError::unsupported_version); + if (bytes[7] != 0) + return std::unexpected(SessionCodecError::reserved_flags); + const auto payload = bytes.subspan(session_header_bytes); + const auto expect = [&](std::size_t n) { return payload.size() == n; }; + switch (bytes[6]) { + case static_cast(SessionMessageType::hello): { + if (!expect(hello_bytes)) + return std::unexpected(SessionCodecError::wrong_length); + SessionHello m; + m.required_features = static_cast(get(payload, 0, 4)); + m.grant = grant_at(payload, 4); + return m; + } + case static_cast(SessionMessageType::welcome): { + if (!expect(welcome_bytes)) + return std::unexpected(SessionCodecError::wrong_length); + SessionWelcome m; + std::uint64_t* fields[] = {&m.session, &m.epoch, &m.world, &m.frame, + &m.region, &m.expires_at, &m.server_seconds}; + for (std::size_t i = 0; i < std::size(fields); ++i) + *fields[i] = get(payload, 8 * i, 8); + if (m.session == 0 || m.epoch == 0) + return std::unexpected(SessionCodecError::invalid_value); + return m; + } + case static_cast(SessionMessageType::refresh): { + if (!expect(refresh_bytes)) + return std::unexpected(SessionCodecError::wrong_length); + return SessionRefresh{grant_at(payload, 0)}; + } + case static_cast(SessionMessageType::refresh_result): { + if (!expect(refresh_result_bytes)) + return std::unexpected(SessionCodecError::wrong_length); + SessionRefreshResult m{payload[0], get(payload, 1, 8)}; + if (m.status != 0 && !wire_reason(m.status)) + return std::unexpected(SessionCodecError::invalid_value); + return m; + } + case static_cast(SessionMessageType::close): { + if (!expect(close_bytes)) + return std::unexpected(SessionCodecError::wrong_length); + if (!wire_reason(payload[0])) + return std::unexpected(SessionCodecError::invalid_value); + return SessionClose{static_cast(payload[0])}; + } + default: + return std::unexpected(SessionCodecError::unknown_type); + } +} +} // namespace evo::network diff --git a/src/server/session/region_sessions.cpp b/src/server/session/region_sessions.cpp new file mode 100644 index 0000000..c7131f4 --- /dev/null +++ b/src/server/session/region_sessions.cpp @@ -0,0 +1,266 @@ +#include "evo/server/region_sessions.hpp" +#include +#include + +namespace evo::server { +namespace { +using network::ControlError; +using network::SessionCloseReason; +using network::SessionGrantError; +using Clock = std::chrono::steady_clock; + +SessionCloseReason reason_for(ControlError e) { + switch (e) { + case ControlError::wrong_environment: + case ControlError::wrong_world: + case ControlError::wrong_region: + return SessionCloseReason::wrong_scope; + case ControlError::expired: + return SessionCloseReason::expired; + case ControlError::stale_grant: + return SessionCloseReason::stale_grant; + case ControlError::rate_limited: + return SessionCloseReason::rate_limited; + case ControlError::sessions_full: + case ControlError::accounts_full: + return SessionCloseReason::capacity; + default: + return SessionCloseReason::unauthenticated; + } +} +SessionCloseReason reason_for(SessionGrantError e) { + return e == SessionGrantError::wrong_environment ? SessionCloseReason::wrong_scope + : SessionCloseReason::unauthenticated; +} +} // namespace + +struct RegionSessions::Impl { + enum class State : std::uint8_t { hello, established, closing }; + struct Connection { + network::quic::ConnectionId id{}; + State state{State::hello}; + std::uint64_t session{}; + std::string account; + Clock::time_point deadline; + bool close_requested{}; + }; + + Impl(RegionSessionsConfig c, std::unique_ptr e, + network::GrantVerifier v, network::SessionControl s) + : config{std::move(c)}, endpoint{std::move(e)}, verifier{std::move(v)}, + control{std::move(s)} {} + + RegionSessionsConfig config; + std::unique_ptr endpoint; + network::GrantVerifier verifier; + network::SessionControl control; + std::vector connections; + std::vector events; + + Connection* find(network::quic::ConnectionId id) { + const auto it = + std::ranges::find_if(connections, [&](const Connection& c) { return c.id == id; }); + return it == connections.end() ? nullptr : &*it; + } + Connection* by_session(std::uint64_t session) { + const auto it = std::ranges::find_if(connections, [&](const Connection& c) { + return c.session == session && c.state == State::established; + }); + return it == connections.end() ? nullptr : &*it; + } + void send(const Connection& c, const network::SessionMessage& m) { + // A refused send (budget) leaves the peer to observe the transport close instead. + (void)endpoint->send_message(c.id, network::encode_session(m)).has_value(); + } + // Sends Close, releases control, reports, and closes after the flush deadline. + void close(Connection& c, SessionCloseReason reason, Clock::time_point now) { + if (c.state == State::closing) + return; + send(c, network::SessionClose{reason}); + RegionSessionEvent event; + event.kind = c.state == State::established ? RegionSessionEvent::Kind::closed + : RegionSessionEvent::Kind::refused; + event.session = c.session; + event.account = c.account; + event.reason = reason; + events.push_back(std::move(event)); + if (c.session != 0) // May already be released (superseded, expired, epoch). + (void)control.close(c.session).has_value(); + c.session = 0; + c.state = State::closing; + c.deadline = now + config.close_flush; + } + + void hello(Connection& c, const network::SessionHello& m, std::uint64_t now_seconds, + Clock::time_point now) { + if (m.required_features != 0) + return close(c, SessionCloseReason::protocol, now); + if (!control.attempt(now_seconds)) // Region-wide, before signature work. + return close(c, SessionCloseReason::rate_limited, now); + const auto grant = verifier.verify(m.grant); + if (!grant) + return close(c, reason_for(grant.error()), now); + c.account = network::log_label(grant->claims().account); + const auto opened = control.open(*grant, now_seconds); + if (!opened) + return close(c, reason_for(opened.error()), now); + if (opened->superseded != 0) + if (auto* old = by_session(opened->superseded)) + close(*old, SessionCloseReason::superseded, now); + c.state = State::established; + c.session = opened->session; + network::SessionWelcome welcome; + welcome.session = opened->session; + welcome.epoch = opened->epoch; + welcome.world = config.control.world.world; + welcome.frame = config.control.world.frame; + welcome.region = config.control.region; + welcome.expires_at = opened->expires_at; + welcome.server_seconds = now_seconds; + send(c, welcome); + events.push_back({RegionSessionEvent::Kind::opened, c.session, c.account, {}}); + } + + void refresh(Connection& c, const network::SessionRefresh& m, std::uint64_t now_seconds) { + network::SessionRefreshResult result; + if (!control.attempt(now_seconds)) { + result.status = static_cast(SessionCloseReason::rate_limited); + } else if (const auto grant = verifier.verify(m.grant); !grant) { + result.status = static_cast(reason_for(grant.error())); + } else if (const auto renewed = control.refresh(c.session, *grant, now_seconds); !renewed) { + result.status = static_cast(renewed.error() == ControlError::wrong_account + ? SessionCloseReason::unauthenticated + : reason_for(renewed.error())); + } else { + result.expires_at = *renewed; + } + send(c, result); + } + + void message(Connection& c, std::span bytes, std::uint64_t now_seconds, + Clock::time_point now) { + if (c.state == State::closing) + return; + const auto decoded = network::decode_session(bytes); + if (!decoded) + return close(c, + decoded.error() == network::SessionCodecError::unsupported_version + ? SessionCloseReason::unsupported_version + : SessionCloseReason::protocol, + now); + if (c.state == State::hello) { + if (const auto* m = std::get_if(&*decoded)) + return hello(c, *m, now_seconds, now); + return close(c, SessionCloseReason::protocol, now); + } + if (const auto* m = std::get_if(&*decoded)) + return refresh(c, *m, now_seconds); + if (const auto* m = std::get_if(&*decoded); + m && m->reason == SessionCloseReason::client_closed) { + close(c, SessionCloseReason::client_closed, now); + c.deadline = now; // Nothing left to flush to a peer that is leaving. + return; + } + close(c, SessionCloseReason::protocol, now); + } +}; + +RegionSessions::RegionSessions(std::unique_ptr impl) : impl_{std::move(impl)} {} +RegionSessions::~RegionSessions() = default; + +std::expected, RegionSessionsError> +RegionSessions::create(RegionSessionsConfig config) { + // Connections beyond sessions are headroom for logins and closes still flushing. + if (config.issuer_keys.empty() || + config.transport.limits.max_connections < config.control.max_sessions || + config.hello_deadline.count() <= 0 || config.close_flush.count() <= 0) + return std::unexpected(RegionSessionsError::configuration); + auto control = network::SessionControl::create(config.control); + if (!control) + return std::unexpected(RegionSessionsError::configuration); + auto verifier = network::GrantVerifier::create(config.control.environment); + if (!verifier) + return std::unexpected(RegionSessionsError::keys); + for (const auto& k : config.issuer_keys) + if (!verifier->add_key(k.key_id, k.public_key)) + return std::unexpected(RegionSessionsError::keys); + auto endpoint = network::quic::Endpoint::serve(config.transport); + if (!endpoint) + return std::unexpected(RegionSessionsError::transport); + return std::unique_ptr{new RegionSessions{std::make_unique( + std::move(config), std::move(*endpoint), std::move(*verifier), std::move(*control))}}; +} + +void RegionSessions::poll(std::uint64_t epoch, std::uint64_t now_seconds, Clock::time_point now) { + auto& m = *impl_; + if (epoch > m.control.epoch()) { + // Every session ends with the epoch (ADR-0089); control has released them. + const auto closed = m.control.advance_epoch(epoch); + if (closed) + for (const auto session : *closed) + if (auto* c = m.by_session(session)) + m.close(*c, SessionCloseReason::stale_epoch, now); + } + for (auto& e : m.endpoint->poll()) { + using network::quic::EventKind; + if (e.kind == EventKind::connected) { + m.connections.push_back( + {e.connection, Impl::State::hello, 0, {}, now + m.config.hello_deadline, false}); + } else if (e.kind == EventKind::message) { + if (auto* c = m.find(e.connection)) + m.message(*c, e.bytes, now_seconds, now); + } else if (e.kind == EventKind::closed) { + const auto it = std::ranges::find_if( + m.connections, [&](const Impl::Connection& c) { return c.id == e.connection; }); + if (it == m.connections.end()) + continue; + if (it->state == Impl::State::established) { + (void)m.control.close(it->session).has_value(); + // The client closes its connection right after sending Close, which may be + // discarded with it: a peer close is the client leaving, anything else is loss. + m.events.push_back({RegionSessionEvent::Kind::closed, it->session, it->account, + e.cause == network::quic::CloseCause::peer + ? SessionCloseReason::client_closed + : SessionCloseReason::connection_lost}); + } + m.connections.erase(it); + } // Datagrams carry nothing before 10.4. + } + for (const auto session : m.control.expire(now_seconds)) + if (auto* c = m.by_session(session)) + m.close(*c, SessionCloseReason::expired, now); + for (auto& c : m.connections) { + if (c.state == Impl::State::hello && now >= c.deadline) + m.close(c, SessionCloseReason::timeout, now); + if (c.state == Impl::State::closing && now >= c.deadline && !c.close_requested) { + c.close_requested = true; + m.endpoint->close(c.id); // Its closed event removes it. + } + } +} + +void RegionSessions::shutdown(Clock::time_point now) { + for (auto& c : impl_->connections) + impl_->close(c, SessionCloseReason::shutdown, now); +} + +std::vector RegionSessions::take_events() { + return std::exchange(impl_->events, {}); +} + +std::optional RegionSessions::holder(const network::AccountId& account) const { + return impl_->control.holder(account); +} + +std::size_t RegionSessions::sessions() const noexcept { + return impl_->control.sessions(); +} + +std::size_t RegionSessions::connections() const noexcept { + return impl_->connections.size(); +} + +std::uint16_t RegionSessions::port() const noexcept { + return impl_->endpoint->port(); +} +} // namespace evo::server diff --git a/tests/network_control.cpp b/tests/network_control.cpp index a462f44..4b51932 100644 --- a/tests/network_control.cpp +++ b/tests/network_control.cpp @@ -2,6 +2,7 @@ #include "evo/network/dev_grant.hpp" #include "evo/network/session_control.hpp" #include "evo/network/session_grant.hpp" +#include "evo/network/session_protocol.hpp" #include "network_test.hpp" #include #include @@ -321,11 +322,74 @@ void control_rules() { labelled.bytes.at(i) = static_cast(0xA0 + i); CHECK(log_label(labelled) == "acct-a0a1a2a3"); } +void session_codec() { + WireGrant grant{}; + for (std::size_t i = 0; i < grant.size(); ++i) + grant.at(i) = static_cast(i * 7); + SessionWelcome welcome{1, 2, 3, 4, 5, 6, 7}; + const std::vector messages{ + SessionHello{0, grant}, + welcome, + SessionRefresh{grant}, + SessionRefreshResult{0, 99}, + SessionRefreshResult{static_cast(SessionCloseReason::stale_grant), 0}, + SessionClose{SessionCloseReason::superseded}, + SessionClose{SessionCloseReason::shutdown}}; + for (const auto& m : messages) { + const auto bytes = encode_session(m); + CHECK(bytes.at(0) == 'E' && bytes.at(1) == 'V' && bytes.at(2) == 'S' && bytes.at(3) == 'N'); + const auto back = decode_session(bytes); + CHECK(back && back->index() == m.index()); + CHECK(encode_session(*back) == bytes); + // Every truncation and any extension is refused. + for (std::size_t n = 0; n < bytes.size(); ++n) { + const auto r = decode_session(std::span{bytes}.first(n)); + CHECK(!r && r.error() == (n < session_header_bytes ? SessionCodecError::truncated + : SessionCodecError::wrong_length)); + } + auto longer = bytes; + longer.push_back(0); + CHECK(refused(decode_session(longer), SessionCodecError::wrong_length)); + auto mutated = bytes; + mutated.at(0) ^= 1; + CHECK(refused(decode_session(mutated), SessionCodecError::bad_magic)); + mutated = bytes; + mutated.at(4) = 2; + CHECK(refused(decode_session(mutated), SessionCodecError::unsupported_version)); + mutated = bytes; + mutated.at(7) = 1; + CHECK(refused(decode_session(mutated), SessionCodecError::reserved_flags)); + for (const auto type : std::array{0, 6, 255}) { + mutated = bytes; + mutated.at(6) = type; + CHECK(refused(decode_session(mutated), SessionCodecError::unknown_type)); + } + } + CHECK(std::get(*decode_session(encode_session(welcome))) == welcome); + CHECK(std::get(*decode_session(encode_session(SessionHello{0, grant}))).grant == + grant); + for (const auto reason : std::array{0, 14, 200, 255}) { + auto close = encode_session(SessionClose{SessionCloseReason::protocol}); + close.back() = reason; + CHECK(refused(decode_session(close), SessionCodecError::invalid_value)); + if (reason != 0) { + auto result = encode_session(SessionRefreshResult{0, 0}); + result.at(session_header_bytes) = reason; + CHECK(refused(decode_session(result), SessionCodecError::invalid_value)); + } + } + for (const auto zero : {0, 1}) { + auto w = welcome; + (zero == 0 ? w.session : w.epoch) = 0; + CHECK(refused(decode_session(encode_session(w)), SessionCodecError::invalid_value)); + } +} } // namespace void control_tests() { grants(); identity_adapter(); control_rules(); + session_codec(); } } // namespace evo::network_test diff --git a/tests/quic_sessions.cpp b/tests/quic_sessions.cpp new file mode 100644 index 0000000..3653c55 --- /dev/null +++ b/tests/quic_sessions.cpp @@ -0,0 +1,351 @@ +// M10.3b session login over QUIC (ADR-0091): grants verified by the region, newest grant +// wins, replay and stale grants, scope, expiry, refresh, epochs, rate and capacity +// limits, malformed and silent peers, client close and shutdown. The wall clock used for +// grants is injected; deadlines use the real monotonic clock. +#include "evo/client/region_session.hpp" +#include "evo/network/dev_credentials.hpp" +#include "evo/network/session_grant.hpp" +#include "evo/server/region_sessions.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { +using namespace evo; +using namespace evo::network; +using client::RegionSessionClient; +using client::RegionSessionState; +using server::RegionSessionEvent; +using server::RegionSessions; +using Clock = std::chrono::steady_clock; +using Reason = SessionCloseReason; + +void check(bool ok, std::source_location at = std::source_location::current()) { + if (!ok) + throw std::runtime_error("quic sessions line " + std::to_string(at.line())); +} +#define CHECK(...) check(static_cast(__VA_ARGS__)) +template +const T& must(const std::optional& o, + std::source_location at = std::source_location::current()) { + if (!o) + throw std::runtime_error("quic sessions missing value line " + std::to_string(at.line())); + return *o; +} +constexpr std::string_view name = "dev-region.evolution.test"; +constexpr world::WorldIdentity world_id{7, 3}; +constexpr std::uint64_t region = 11; +constexpr std::uint64_t start = 1'800'000'000; + +struct Files { + std::filesystem::path dir; + std::string ca_pem; + std::filesystem::path ca, certificate, key; +}; +Files credentials() { + const auto made = generate_dev_credentials(DeploymentEnvironment::dev, name, 1, + std::chrono::system_clock::now()); + CHECK(made.has_value()); + Files f; + f.dir = std::filesystem::temp_directory_path() / "evo-quic-sessions"; + std::filesystem::remove_all(f.dir); + std::filesystem::create_directories(f.dir); + f.ca_pem = made->authority_pem; + f.ca = f.dir / "ca.pem"; + f.certificate = f.dir / "server.pem"; + f.key = f.dir / "server.key"; + for (const auto& [path, text] : {std::pair{f.ca, made->authority_pem}, + std::pair{f.certificate, made->server_certificate_pem}, + std::pair{f.key, made->server_key_pem}}) { + std::ofstream out{path, std::ios::binary | std::ios::trunc}; + out << text; + CHECK(out.good()); + } + return f; +} + +AccountId account(std::uint8_t tag) { + AccountId a; + a.bytes.fill(tag); + return a; +} +// The issuer's dev key from a fixed scalar, so the same key can also sign as "stage". +std::array scalar() { + std::array s{}; + s.back() = 0x2A; + return s; +} +struct Issuer { + GrantSigningKey key = *GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar()); + WireGrant grant(const AccountId& who, std::uint64_t serial, std::uint64_t issued = start, + std::uint64_t lifetime = 60, std::uint64_t in_region = region) const { + GrantClaims c; + c.grant_id = 500 + serial; + c.account = who; + c.serial = serial; + c.world = world_id; + c.region = in_region; + c.issued_at = issued; + c.expires_at = issued + lifetime; + const auto g = key.sign(c); + CHECK(g.has_value()); + return *g; + } +}; + +struct Harness { + explicit Harness(const Files& f, std::size_t max_sessions = 4, + std::uint32_t region_attempts = 64) + : files{f} { + server::RegionSessionsConfig c; + c.transport.certificate_pem_file = f.certificate.string(); + c.transport.key_pem_file = f.key.string(); + c.control.environment = DeploymentEnvironment::dev; + c.control.world = world_id; + c.control.region = region; + c.control.epoch = 5; + c.control.max_sessions = max_sessions; + c.control.max_account_attempts = 16; + c.control.max_region_attempts = region_attempts; + c.issuer_keys.push_back({1, *issuer.key.public_key()}); + c.hello_deadline = std::chrono::milliseconds{500}; + c.close_flush = std::chrono::milliseconds{300}; + auto made = RegionSessions::create(std::move(c)); + CHECK(made.has_value()); + server = std::move(*made); + } + std::unique_ptr connect(const WireGrant& grant) { + client::RegionSessionClientConfig c; + c.transport.port = server->port(); + c.transport.server_name = std::string{name}; + c.transport.authority_pem = files.ca_pem; + c.transport.authority_pem_file = files.ca.string(); + c.grant = grant; + auto made = RegionSessionClient::connect(std::move(c), Clock::now()); + CHECK(made.has_value()); + clients.push_back(made->get()); + return std::move(*made); + } + std::unique_ptr raw() { + quic::ClientConfig c; + c.port = server->port(); + c.server_name = std::string{name}; + c.authority_pem = files.ca_pem; + c.authority_pem_file = files.ca.string(); + auto made = quic::Endpoint::connect(c); + CHECK(made.has_value()); + return std::move(*made); + } + void pump() { + server->poll(epoch, now_seconds, Clock::now()); + for (auto& e : server->take_events()) + events.push_back(std::move(e)); + for (auto* c : clients) + c->poll(Clock::now()); + } + bool until(const std::function& done, + std::chrono::milliseconds limit = std::chrono::milliseconds{10000}) { + const auto end = Clock::now() + limit; + while (Clock::now() < end) { + pump(); + if (done()) + return true; + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + } + pump(); + return done(); + } + bool settled(RegionSessionClient& c) { + return until([&] { + return c.state() == RegionSessionState::established || + c.state() == RegionSessionState::closed; + }); + } + bool has_event(RegionSessionEvent::Kind kind, Reason reason, std::uint64_t session = 0) const { + return std::ranges::any_of(events, [&](const RegionSessionEvent& e) { + return e.kind == kind && + (kind == RegionSessionEvent::Kind::opened || e.reason == reason) && + (session == 0 || e.session == session); + }); + } + void forget(const RegionSessionClient* c) { + std::erase(clients, c); + } + const Files& files; + Issuer issuer; + std::unique_ptr server; + std::vector clients; + std::vector events; + std::uint64_t epoch{5}, now_seconds{start}; +}; + +bool refused_with(Harness& h, const WireGrant& grant, Reason reason) { + auto c = h.connect(grant); + const bool ok = h.settled(*c) && c->state() == RegionSessionState::closed && + c->close_reason() == reason && !c->welcome(); + h.forget(c.get()); + return ok; +} + +// A raw QUIC peer: sends `bytes` (or nothing) and returns the server's Close reason. +std::optional raw_peer(Harness& h, const std::optional>& bytes) { + auto peer = h.raw(); + std::optional reason; + quic::ConnectionId id = 0; + const bool done = h.until([&] { + for (auto& e : peer->poll()) { + if (e.kind == quic::EventKind::connected) { + id = e.connection; + if (bytes) + CHECK(peer->send_message(id, *bytes).has_value()); + } else if (e.kind == quic::EventKind::message) { + const auto m = decode_session(e.bytes); + if (m && std::holds_alternative(*m)) + reason = std::get(*m).reason; + } + } + return reason.has_value(); + }); + CHECK(done); + return reason; +} + +void login_rules(const Files& f) { + Harness h{f}; + const auto alice = account(1), bob = account(2), carol = account(3); + // Login and Welcome. + auto a1 = h.connect(h.issuer.grant(alice, 1)); + CHECK(h.settled(*a1) && a1->state() == RegionSessionState::established); + const auto& w = must(a1->welcome()); + CHECK(w.epoch == 5 && w.world == world_id.world && w.frame == world_id.frame && + w.region == region && w.expires_at == start + 60 && w.server_seconds == start); + CHECK(h.server->holder(alice) == w.session && h.server->sessions() == 1); + CHECK(h.has_event(RegionSessionEvent::Kind::opened, {}, w.session)); + CHECK(std::ranges::all_of(h.events, [&](const RegionSessionEvent& e) { + return e.account.empty() || e.account == log_label(alice) || e.account == log_label(bob) || + e.account == log_label(carol); + })); + // Replay of the same grant. + CHECK(refused_with(h, h.issuer.grant(alice, 1), Reason::stale_grant)); + CHECK(h.server->holder(alice) == w.session); + // Duplicate login: the newer grant takes over; the old connection is told why. + auto a2 = h.connect(h.issuer.grant(alice, 3)); + CHECK(h.settled(*a2) && a2->state() == RegionSessionState::established); + CHECK(h.until([&] { return a1->state() == RegionSessionState::closed; })); + CHECK(a1->close_reason() == Reason::superseded); + CHECK(h.has_event(RegionSessionEvent::Kind::closed, Reason::superseded, w.session)); + CHECK(h.server->holder(alice) == must(a2->welcome()).session && h.server->sessions() == 1); + // A stolen older grant (serial 2, still unexpired) cannot take control back. + CHECK(refused_with(h, h.issuer.grant(alice, 2), Reason::stale_grant)); + CHECK(a2->state() == RegionSessionState::established); + // Refresh in place: accepted, then stale, then another account's grant. + CHECK(a2->refresh(h.issuer.grant(alice, 4, start + 30))); + std::optional r; + CHECK(h.until([&] { return (r = a2->take_refresh_result()).has_value(); })); + CHECK(must(r).status == 0 && must(r).expires_at == start + 90 && + must(a2->welcome()).expires_at == start + 90); + CHECK(a2->refresh(h.issuer.grant(alice, 4, start + 30))); + CHECK(h.until([&] { return (r = a2->take_refresh_result()).has_value(); })); + CHECK(must(r).status == static_cast(Reason::stale_grant)); + CHECK(a2->refresh(h.issuer.grant(bob, 9))); + CHECK(h.until([&] { return (r = a2->take_refresh_result()).has_value(); })); + CHECK(must(r).status == static_cast(Reason::unauthenticated)); + CHECK(a2->state() == RegionSessionState::established && + must(a2->welcome()).expires_at == start + 90); + // Scope and authenticity. + CHECK(refused_with(h, h.issuer.grant(bob, 1, start, 60, region + 1), Reason::wrong_scope)); + const auto stage = *GrantSigningKey::import(DeploymentEnvironment::stage, 1, scalar()); + GrantClaims staged; + staged.grant_id = 1; + staged.account = bob; + staged.serial = 1; + staged.world = world_id; + staged.region = region; + staged.issued_at = start; + staged.expires_at = start + 60; + CHECK(refused_with(h, *stage.sign(staged), Reason::wrong_scope)); // Stage grant, dev region. + const auto stranger = *GrantSigningKey::generate(DeploymentEnvironment::dev, 1); + CHECK(refused_with(h, *stranger.sign(staged), Reason::unauthenticated)); // Not our issuer. + auto forged = h.issuer.grant(bob, 1); + forged.at(40) ^= 1; + CHECK(refused_with(h, forged, Reason::unauthenticated)); + // Expiry at login and during a session (injected wall clock). + CHECK(refused_with(h, h.issuer.grant(bob, 1, start - 120), Reason::expired)); + auto b = h.connect(h.issuer.grant(bob, 2)); + CHECK(h.settled(*b) && b->state() == RegionSessionState::established); + h.now_seconds = start + 60; // Bob's grant ends; Alice refreshed until start + 90. + CHECK(h.until([&] { return b->state() == RegionSessionState::closed; })); + CHECK(b->close_reason() == Reason::expired && a2->state() == RegionSessionState::established); + // Client close releases control. + auto c = h.connect(h.issuer.grant(carol, 1, start + 60)); + CHECK(h.settled(*c) && c->state() == RegionSessionState::established); + const auto carol_session = must(c->welcome()).session; + c->close(); + CHECK(h.until([&] { return !h.server->holder(carol); })); + CHECK(h.has_event(RegionSessionEvent::Kind::closed, Reason::client_closed, carol_session)); + // Malformed and silent peers. + CHECK(raw_peer(h, std::nullopt) == Reason::timeout); + CHECK(raw_peer(h, std::vector{1, 2, 3}) == Reason::protocol); + auto future = encode_session(SessionHello{0, h.issuer.grant(carol, 2, start + 60)}); + future.at(4) = 2; + CHECK(raw_peer(h, future) == Reason::unsupported_version); + CHECK(raw_peer(h, encode_session(SessionClose{Reason::client_closed})) == Reason::protocol); + CHECK(raw_peer(h, encode_session(SessionHello{1, h.issuer.grant(carol, 3, start + 60)})) == + Reason::protocol); // Unknown required feature. + // A new ownership epoch ends every session. + h.epoch = 6; + CHECK(h.until([&] { return a2->state() == RegionSessionState::closed; })); + CHECK(a2->close_reason() == Reason::stale_epoch && h.server->sessions() == 0); + auto a3 = h.connect(h.issuer.grant(alice, 5, start + 60)); + CHECK(h.settled(*a3) && must(a3->welcome()).epoch == 6); + // Shutdown tells every client. + h.server->shutdown(Clock::now()); + CHECK(h.until([&] { return a3->state() == RegionSessionState::closed; })); + CHECK(a3->close_reason() == Reason::shutdown); + CHECK(h.until([&] { return h.server->connections() == 0; })); +} + +void limits(const Files& f) { + { + Harness h{f, 1}; + auto a = h.connect(h.issuer.grant(account(1), 1)); + CHECK(h.settled(*a) && a->state() == RegionSessionState::established); + CHECK(refused_with(h, h.issuer.grant(account(2), 1), Reason::capacity)); + a->close(); + CHECK(h.until([&] { return h.server->sessions() == 0; })); + // The refused grant was not consumed. + auto b = h.connect(h.issuer.grant(account(2), 1)); + CHECK(h.settled(*b) && b->state() == RegionSessionState::established); + } + { + Harness h{f, 4, 3}; + for (std::uint64_t serial = 1; serial <= 3; ++serial) { + auto c = h.connect(h.issuer.grant(account(1), serial)); + CHECK(h.settled(*c) && c->state() == RegionSessionState::established); + h.forget(c.get()); + } + CHECK(refused_with(h, h.issuer.grant(account(1), 4), Reason::rate_limited)); + } +} +} // namespace + +int main() try { + const auto files = credentials(); + login_rules(files); + limits(files); + std::filesystem::remove_all(files.dir); + std::puts("quic session tests passed"); + return 0; +} catch (const std::exception& e) { + std::fprintf(stderr, "%s\n", e.what()); + return 1; +}