diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index 1389d9c..fe09ca7 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -33,7 +33,7 @@ jobs: strategy: fail-fast: false matrix: - suite: [checks, tsan, ecs, postgres, deploy, client] + suite: [checks, tsan, ecs, postgres, deploy, client, network] steps: - name: Checkout uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5abcd9b..c2cf38f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ option(EVO_BUILD_ECS_EXPERIMENT "Build the isolated EnTT comparison (explicit pr option(EVO_BUILD_POSTGRES "Build the isolated Linux PostgreSQL persistence adapter" OFF) option(EVO_BUILD_CLIENT_PROBE "Build the isolated M9.1 SDL dependency probe" OFF) option(EVO_BUILD_CLIENT "Build the native M9 engineering client" OFF) +option(EVO_BUILD_NETWORK "Build the M9.6 dev session protocol and TLS adapter (explicit Mbed TLS preparation required)" OFF) set(EVO_SANITIZER "none" CACHE STRING "none, address (ASan/UBSan), or thread") set_property(CACHE EVO_SANITIZER PROPERTY STRINGS none address thread) include(cmake/ProjectOptions.cmake) @@ -92,6 +93,19 @@ target_link_libraries(evo_region_checkpoint PUBLIC evo::region_lease evo::proces evo_owned_target(evo_region_checkpoint) include(CTest) +if(EVO_BUILD_CLIENT OR EVO_BUILD_NETWORK) + # SDL-free client value contracts (M9.3-M9.5), shared by the client and the dev + # session codec. They copy read-only world/biology views, never the reverse. + add_library(evo_client_controls STATIC src/client/camera/camera.cpp src/client/input/input.cpp) + target_include_directories(evo_client_controls PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") + evo_owned_target(evo_client_controls) + add_library(evo_client_views STATIC src/client/views/environment_view.cpp src/client/views/organism_view.cpp) + target_link_libraries(evo_client_views PUBLIC evo::world evo::biology) + evo_owned_target(evo_client_views) +endif() +if(EVO_BUILD_NETWORK) + include(cmake/Network.cmake) +endif() if(EVO_BUILD_CLIENT_PROBE OR EVO_BUILD_CLIENT) include(cmake/ClientProbe.cmake) endif() diff --git a/CMakePresets.json b/CMakePresets.json index 32c13e2..2e28e9f 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -9,7 +9,8 @@ {"name": "asan", "inherits": "debug", "cacheVariables": {"EVO_SANITIZER": "address"}}, {"name": "tsan", "inherits": "debug", "cacheVariables": {"EVO_SANITIZER": "thread"}}, {"name": "client", "inherits": "release", "cacheVariables": {"EVO_BUILD_CLIENT": "ON"}}, - {"name": "client-probe", "inherits": "release", "cacheVariables": {"EVO_BUILD_CLIENT_PROBE": "ON"}} + {"name": "client-probe", "inherits": "release", "cacheVariables": {"EVO_BUILD_CLIENT_PROBE": "ON"}}, + {"name": "network", "inherits": "release", "cacheVariables": {"EVO_BUILD_NETWORK": "ON"}} ], "buildPresets": [ {"name": "debug", "configurePreset": "debug"}, @@ -18,7 +19,8 @@ {"name": "asan", "configurePreset": "asan"}, {"name": "tsan", "configurePreset": "tsan"}, {"name": "client", "configurePreset": "client", "targets": ["evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests"]}, - {"name": "client-probe", "configurePreset": "client-probe", "targets": ["evo-client-probe"]} + {"name": "client-probe", "configurePreset": "client-probe", "targets": ["evo-client-probe"]}, + {"name": "network", "configurePreset": "network", "targets": ["evo-network-tests"]} ], "testPresets": [ {"name": "debug", "configurePreset": "debug", "output": {"outputOnFailure": true}}, @@ -27,6 +29,7 @@ {"name": "asan", "configurePreset": "asan", "output": {"outputOnFailure": true}}, {"name": "tsan", "configurePreset": "tsan", "output": {"outputOnFailure": true}}, {"name": "client", "configurePreset": "client", "filter": {"include": {"label": "client"}}, "output": {"outputOnFailure": true}}, - {"name": "client-probe", "configurePreset": "client-probe", "filter": {"include": {"label": "client"}}, "output": {"outputOnFailure": true}} + {"name": "client-probe", "configurePreset": "client-probe", "filter": {"include": {"label": "client"}}, "output": {"outputOnFailure": true}}, + {"name": "network", "configurePreset": "network", "filter": {"include": {"label": "network"}}, "output": {"outputOnFailure": true}} ] } diff --git a/README.md b/README.md index b29a1ee..b25e2e3 100644 --- a/README.md +++ b/README.md @@ -176,3 +176,18 @@ M9.4 renders [revision-tagged environment views](docs/engineering/ENVIRONMENT_RE with explicit missing/stale presentation from a local in-memory fixture authority. M9.5 adds [biology-derived organism rendering](docs/engineering/ORGANISM_RENDERING.md) with no player/NPC labels and an opt-in engineering inspector. + +## Dev session protocol (M9.6) + +Opt-in and separate from headless targets. Prepare the pinned TLS library, then +build and test: + +```sh +cmake -P tools/prepare-mbedtls.cmake +cmake --preset network +cmake --build --preset network +ctest --preset network +``` + +See [DEV_SESSION_PROTOCOL](docs/networking/DEV_SESSION_PROTOCOL.md) and ADR-0082. +Windows: `tools/windows-check.ps1 -Preset network -Action check`. diff --git a/cmake/ClientProbe.cmake b/cmake/ClientProbe.cmake index 075fc94..7257711 100644 --- a/cmake/ClientProbe.cmake +++ b/cmake/ClientProbe.cmake @@ -46,16 +46,9 @@ if(EVO_BUILD_CLIENT_PROBE) endif() if(EVO_BUILD_CLIENT) - add_library(evo_client_controls STATIC src/client/camera/camera.cpp src/client/input/input.cpp) - target_include_directories(evo_client_controls PUBLIC "${PROJECT_SOURCE_DIR}/include") - evo_owned_target(evo_client_controls) add_library(evo_render_frame STATIC src/render/frame.cpp) target_include_directories(evo_render_frame PUBLIC "${PROJECT_SOURCE_DIR}/include") evo_owned_target(evo_render_frame) - # SDL-free environment presentation (M9.4): copies read-only world views, never the reverse. - add_library(evo_client_views STATIC src/client/views/environment_view.cpp src/client/views/organism_view.cpp) - target_link_libraries(evo_client_views PUBLIC evo::world evo::biology) - evo_owned_target(evo_client_views) add_library(evo_render_environment STATIC src/render/environment/environment.cpp) target_link_libraries(evo_render_environment PUBLIC evo_client_views evo_client_controls evo_render_frame) evo_owned_target(evo_render_environment) diff --git a/cmake/Network.cmake b/cmake/Network.cmake new file mode 100644 index 0000000..807877f --- /dev/null +++ b/cmake/Network.cmake @@ -0,0 +1,53 @@ +# M9.6 dev session protocol and TLS adapter (ADR-0082). Opt-in; never part of the +# headless simulation. Mbed TLS comes only from the explicitly prepared, pinned source. +include("${PROJECT_SOURCE_DIR}/tools/mbedtls-pin.cmake") +set(mbedtls_source "${PROJECT_SOURCE_DIR}/build/external/mbedtls/mbedtls-${EVO_MBEDTLS_VERSION}") +if(NOT EXISTS "${mbedtls_source}/evo-verified.sha256") + message(FATAL_ERROR "Prepare Mbed TLS explicitly: cmake -P tools/prepare-mbedtls.cmake") +endif() +file(STRINGS "${mbedtls_source}/evo-verified.sha256" verified) +if(NOT verified STREQUAL EVO_MBEDTLS_SHA256) + message(FATAL_ERROR "Prepared Mbed TLS does not match the reviewed source pin") +endif() +enable_language(C) +set(ENABLE_PROGRAMS OFF CACHE BOOL "" FORCE) +set(ENABLE_TESTING OFF CACHE BOOL "" FORCE) +set(GEN_FILES OFF CACHE BOOL "" FORCE) +set(USE_STATIC_MBEDTLS_LIBRARY ON CACHE BOOL "" FORCE) +set(USE_SHARED_MBEDTLS_LIBRARY OFF CACHE BOOL "" FORCE) +set(USE_STATIC_TF_PSA_CRYPTO_LIBRARY ON CACHE BOOL "" FORCE) +set(USE_SHARED_TF_PSA_CRYPTO_LIBRARY OFF CACHE BOOL "" FORCE) +set(DISABLE_PACKAGE_CONFIG_AND_INSTALL ON CACHE BOOL "" FORCE) +set(MBEDTLS_USER_CONFIG_FILE "${PROJECT_SOURCE_DIR}/third_party/mbedtls/evo_mbedtls_config.h" + CACHE FILEPATH "Owned TLS 1.3-only restriction (ADR-0082)" FORCE) +# Upstream code is not compiled with project warning policy or our sanitizer +# instrumentation guarantees; only owned targets use evo_owned_target. +add_subdirectory("${mbedtls_source}" "${PROJECT_BINARY_DIR}/mbedtls" EXCLUDE_FROM_ALL SYSTEM) + +# 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) +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. +add_library(evo_network_tls STATIC src/network/tls/dev_grant.cpp src/network/tls/channel.cpp) +target_link_libraries(evo_network_tls PUBLIC evo_network_protocol PRIVATE mbedtls) +evo_owned_target(evo_network_tls) +if(BUILD_TESTING) + add_executable(evo-network-tests tests/network_protocol.cpp tests/network_tls.cpp + tests/network_main.cpp) + # The TLS test generates throwaway dev certificates with Mbed TLS directly. + target_link_libraries(evo-network-tests PRIVATE evo_network_tls mbedtls) + evo_owned_target(evo-network-tests) + add_test(NAME network COMMAND evo-network-tests) + set_tests_properties(network PROPERTIES TIMEOUT 120 LABELS "network") +endif() +if(EVO_BUILD_FUZZER) + # Coverage-guided codec fuzzing (Linux Clang; see the EVO_BUILD_FUZZER guard). + add_executable(evo-dev-session-fuzz tests/fuzz/dev_session_fuzz.cpp + src/network/protocol/codec.cpp) + target_link_libraries(evo-dev-session-fuzz PRIVATE evo_client_views evo_client_controls) + evo_owned_target(evo-dev-session-fuzz) + target_compile_options(evo-dev-session-fuzz PRIVATE -fsanitize=fuzzer,address,undefined -fno-omit-frame-pointer) + target_link_options(evo-dev-session-fuzz PRIVATE -fsanitize=fuzzer,address,undefined) +endif() diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile index 7e47b85..7580faa 100644 --- a/deploy/docker/Dockerfile +++ b/deploy/docker/Dockerfile @@ -24,6 +24,7 @@ COPY apps/ apps/ COPY tests/ tests/ COPY experiments/ experiments/ COPY tools/ tools/ +COPY third_party/ third_party/ FROM ci-source AS ci-checks RUN bash tools/ci/linux-check.sh checks @@ -50,6 +51,11 @@ RUN cmake -P tools/prepare-sdl3.cmake \ && ctest --preset client-probe --no-tests=error \ && xvfb-run -a env SDL_VIDEO_DRIVER=x11 ./build/client-probe/evo-client-lifecycle-tests +FROM ci-source AS ci-network +# Explicit pinned Mbed TLS preparation (ADR-0082); codec, admission, dev grants and +# TLS 1.3 loopback with static analysis, then again under ASan/UBSan. +RUN cmake -P tools/prepare-mbedtls.cmake && cmake --preset network -DCMAKE_CXX_COMPILER=clang++-19 -DCMAKE_C_COMPILER=clang-19 -DEVO_ENABLE_TIDY=ON && cmake --build --preset network --parallel 2 && ctest --preset network --no-tests=error && cmake --preset network -DEVO_SANITIZER=address -DEVO_ENABLE_TIDY=OFF && cmake --build --preset network --parallel 2 && ctest --preset network --no-tests=error + FROM ci-source AS ci-ecs # Explicit experiment preparation; normal configure/runtime remains dependency-free. RUN cmake -P tools/prepare-entt.cmake \ diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 207a47d..3b85b75 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -416,7 +416,14 @@ M9.7. Evidence in engineering/VALIDATION.md. controller/ownership fields, identical rendering for identical phenotypes, alive/dead/removed handling, budgeted detail with explicit truncation, and an opt-in rectangle-only engineering inspector under ADR-0081. ImGui is not adopted. -Evidence in engineering/VALIDATION.md. Next gate: M9.6. +Evidence in engineering/VALIDATION.md. + +**M9.6 status (2026-10-01):** dev session protocol v1 (versioned bounded frames, +session/epoch/commit watermark, explicit little-endian codec), session admission +(scope, single-use grants, one session per subject, epoch, replay, tick window, +rate) and dev-only HMAC grants refused outside `dev`, over a TLS 1.3-only channel +with pinned Mbed TLS 4.1.1 LTS under ADR-0082. Sockets and region integration are +M9.7. Evidence in engineering/VALIDATION.md. Next gate: M9.7. **Goal:** a native desktop cellular viewer/controller with input, camera, renderer, readable organism/environment views and a real development-server connection. diff --git a/docs/STATUS.md b/docs/STATUS.md index f347750..e866dbf 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -2,19 +2,23 @@ ## Current gate — 2026-10-01 -M9.5 renders organisms and an engineering debug view -([ADR-0081](adr/0081-organism-presentation.md)). Organism presentations are -derived from M3 phenotypes and contain no controller, owner or level field, so -identical biology renders identically for players and NPCs. Death shows as a -remnant; removal is absence. Rendering is budgeted with explicit truncation; the -opt-in inspector draws biology gauges only. ImGui is not adopted. See -[ORGANISM_RENDERING.md](engineering/ORGANISM_RENDERING.md) and -[VALIDATION.md](engineering/VALIDATION.md). Remote Gitea execution of the -extended client CI stage remains unverified locally. +M9.6 defines the narrow secure dev-session protocol +([ADR-0082](adr/0082-dev-session-transport.md), +[DEV_SESSION_PROTOCOL](networking/DEV_SESSION_PROTOCOL.md)). Versioned bounded +frames carry Hello/Welcome/Intent/IntentResult/views/Close with session, epoch and +commit watermark; decoding is explicit and fuzzed. Session admission enforces +scope, single-use dev grants, one session per subject, epoch, replay, tick window +and rate. Dev grants are HMAC-SHA256 and cannot be created or verified outside +`dev`. The channel is TLS 1.3 only, using pinned Mbed TLS 4.1.1 LTS with TLS 1.2, +PSK and tickets compiled out. Opt-in via `EVO_BUILD_NETWORK`. Remote Gitea +execution of the new `network` CI suite remains unverified locally. -Next: **M9.6**, the narrow secure dev-session protocol. The app still shows a local -in-memory fixture (static organism population, environment water moves); no -ecology runs in the client and no deployment, rule or format changed. +Next: **M9.7**, sockets, server/client session adapters, dev secret provisioning +and a real connection to one authoritative dev region. No deployment, simulation +rule or persistent format changed in M9.6. + +M9.5 added biology-derived organism rendering without controller labels +([ADR-0081](adr/0081-organism-presentation.md)). M9.4 added owned revision-tagged environment presentations and explicit missing/stale rendering ([ADR-0080](adr/0080-environment-presentation.md)). diff --git a/docs/adr/0003-dependencies.md b/docs/adr/0003-dependencies.md index c771ebe..fc7ffdb 100644 --- a/docs/adr/0003-dependencies.md +++ b/docs/adr/0003-dependencies.md @@ -38,4 +38,6 @@ M7.2's [ADR-0059](0059-postgresql-bootstrap.md) accepts a scoped checksum-pinned source build for the paired PostgreSQL server/client. The general cross-platform package-manager comparison remains open. M7.4's [ADR-0061](0061-checkpoint-publication.md) links system OpenSSL 3 libcrypto (already required by libpq TLS) for SHA-256 in the -Linux persistence adapter only. +Linux persistence adapter only. M9.6's [ADR-0082](0082-dev-session-transport.md) +accepts a scoped explicit-source, SHA-256-pinned Mbed TLS 4.1.1 build for the opt-in +dev session protocol (`EVO_BUILD_NETWORK`). diff --git a/docs/adr/0017-network-security.md b/docs/adr/0017-network-security.md index 23737c1..add9d06 100644 --- a/docs/adr/0017-network-security.md +++ b/docs/adr/0017-network-security.md @@ -3,6 +3,8 @@ ## Status Transport proposed; server authority/no custom cryptography locked. +[ADR-0082](0082-dev-session-transport.md) applies this to the M9 dev session only +(TLS 1.3 over a stream with Mbed TLS 4.1.1); the M10 transport remains open. ## Context diff --git a/docs/adr/0082-dev-session-transport.md b/docs/adr/0082-dev-session-transport.md new file mode 100644 index 0000000..e427f2a --- /dev/null +++ b/docs/adr/0082-dev-session-transport.md @@ -0,0 +1,91 @@ +# ADR-0082: Narrow secure dev-session protocol and transport security + +## Status +Accepted for M9.6/M9.7 engineering development sessions only. It is a scoped +application of ADR-0017; the M10 realtime transport (QUIC/DATAGRAM vs DTLS plus +owned reliability) remains open. The dev identity is never valid outside `dev`. + +## Context +M9 must connect a desktop client to one authoritative dev region. NETWORK_ARCHITECTURE +locks server authority, untrusted clients, no custom cryptography, explicit wire +decoding, short-lived scoped grants, replay windows and rate/byte caps, and requires +the maintained implementation, exact version, license, cipher configuration and +threat review to be named. Production identity (M10.3) does not exist yet; a test +identity must not be able to escape into stage or World0. + +## Decision +**Transport security.** TLS 1.3 over a reliable byte stream (TCP in M9.7) with +**Mbed TLS 4.1.1** (LTS branch supported until March 2029; upstream BRANCHES.md and +release notes checked 2026-10-01), used under the Apache-2.0 option of its dual +license. The release tarball (bundling TF-PSA-Crypto) is pinned by SHA-256 +`3359a349…375c98c`, matching upstream's published checksum file (same GitHub release +origin, so this proves integrity of what was reviewed, not independent provenance). +`tools/prepare-mbedtls.cmake` prepares it explicitly; `EVO_BUILD_NETWORK` opts in; +static libraries only; headless/server/client builds never evaluate it otherwise. + +Owned build restriction (`third_party/mbedtls/evo_mbedtls_config.h`, enforced by a +compile-time check): TLS 1.2, DTLS, renegotiation, session tickets and TLS 1.3 PSK +modes are compiled out. Runtime profile: TLS 1.3 only; suites +TLS_AES_128_GCM_SHA256 and TLS_CHACHA20_POLY1305_SHA256; groups X25519 and +secp256r1; ephemeral key exchange only; no early data or resumption. The client +requires a server certificate chaining to a pinned dev CA and matching the +configured server name; the system trust store is not used. Clients present no +certificate: identity is the dev grant inside the channel. + +**Dev identity.** A fixed 108-byte grant: `EVO-DEV-ONLY` marker, version, a dev +environment byte, grant id, subject, world/frame, region, issued/expiry seconds, +and an HMAC-SHA256 tag computed and checked (constant time) by PSA with a 32-byte +non-exportable key. `DevGrantKey::import` refuses any environment but `dev`. +`VerifiedGrant` is constructible only by verification. Admission enforces +world/region scope, maximum lifetime (default one hour), clock skew, single use +(remembered until expiry; fail closed when full) and one session per subject. +Reconnect needs a fresh grant. This is not an account system. + +**Protocol v1** (`schemas/view/dev-session-v1.md`, include/evo/network/protocol.hpp): +12-byte header (magic, version, type, zero flags, length <= 256 KiB) and seven +messages: Hello, Welcome (session, world/frame, region, epoch, server tick, commit +watermark, limits), Intent (session, epoch, sequence, tick, bounded vector), +IntentResult, EnvironmentView and OrganismView (session, epoch, server tick, commit +watermark, baseline plus the M9.4/9.5 presentations), Close. Fields are explicit +little-endian; decoders are bounds-checked and views are rebuilt through validated +presentation constructors. Unknown versions, types, flags, required features and +enum values reject. A streaming FrameReader validates headers before buffering +payloads. Admission orders checks: rate (every attempt counts), epoch, expiry, +vector norm <= 1000, strictly increasing sequence, tick window. "Admitted" means +eligible for scheduling, never simulated or durable. + +## Alternatives considered +| Option | Disposition | +|---|---| +| System OpenSSL 3 (already linked by the Linux persistence adapter) | Strong option for Linux servers; no equivalent on the Windows client without a separate Perl/NASM build or a second TLS stack (Schannel). One stack on both ends is simpler for this scope. | +| Mbed TLS 3.6.7 LTS | Support ends March 2027, within the expected M9-M10 lifetime. | +| QUIC library (e.g. msquic) | Fits M10's datagram questions; large dependency and its own TLS integration before any realtime measurements exist. Deferred to M10.1. | +| libsodium primitives with an owned handshake | Requires designing key exchange/authentication: rejected by ADR-0017. | +| Plain TCP for dev | Unauthenticated protocol could escape into production; rejected. | +| Protobuf/FlatBuffers codec | Codegen dependency for seven small messages; explicit codec is reviewable and fuzzed. | + +No performance comparison was measured; the choice rests on maintenance window, +platform build (CMake, no Perl/Python needed for release tarballs), license and +attack-surface reduction. + +## Benefits +Maintained AEAD/ECDHE implementation with a reduced compiled surface; explicit, +fuzzed, canonical codec; replay/rate/epoch admission testable without sockets; dev +identity unusable outside dev by construction. + +## Costs +Static crypto library build (~2.7 MB of static archives on MSVC), certificate/key +management for the dev region, TCP head-of-line behaviour (acceptable for one dev +engineering client, not measured for M10). + +## Risks +Dev CA/HMAC key leakage allows dev impersonation (dev only; rotate by redeploying +secrets). Session ids identify but do not authenticate; M9.7 must bind a session +to its TLS connection. Rate limits are per message, not per byte; M9.7 adds +connection byte/time limits. Upstream advisories require prompt pin updates (the +July 2026 releases fixed several TLS issues, some in code compiled out here). + +## Consequences +M9.7 adds sockets, server/client session adapters, dev secret provisioning and +reconnect semantics. Pin upgrades change version, hash and this ADR together and +rerun the network suite. M10 reopens transport selection with measurements. diff --git a/docs/adr/DEPENDENCY_EVALUATION.md b/docs/adr/DEPENDENCY_EVALUATION.md index 8ecd99b..aa96bd3 100644 --- a/docs/adr/DEPENDENCY_EVALUATION.md +++ b/docs/adr/DEPENDENCY_EVALUATION.md @@ -16,6 +16,7 @@ Maintenance statements are shortlist evidence, not measured suitability. | NATS JetStream | Upstream streams/consumer docs; C-client boundary | Service RAM/disk/fan-out must justify broker | Audit server/client release licenses; proposed | | prometheus-cpp / Otel C++ | Client vs broad SDK; upstream stability docs | Export overhead/cardinality/size/failure measurement | MIT / Apache-2.0; deferred | | mp-units | Modern dimensional APIs; test compiler diagnostics | Compile time/binary footprint and conversion clarity | MIT; deferred | +| Mbed TLS 4.1 LTS | Supported to March 2029; CMake build without Perl/Python from release tarballs; PSA crypto API | ~2.7 MB static archives (MSVC Release); TLS 1.2/DTLS/PSK/tickets compiled out | Apache-2.0 OR GPL-2.0-or-later (Apache taken); adopted for M9 dev session only (ADR-0082) | | SplitMix64 algorithm | Author's published fixed transition; explicit state | Small constant work, 8-byte state; noncryptographic | Reference attribution in ADR0007; proof only | Every experiment records version/hash, compiler/OS, representative workload, diff --git a/docs/adr/README.md b/docs/adr/README.md index cea5a91..8845393 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -93,3 +93,4 @@ design. No unadopted candidate is a link dependency of the proof. - [ADR-0079: Camera and input intentions](0079-camera-and-input-intentions.md) - local-mm projection, bounded sequence/tick intentions, focus/UI capture and explicit host event ordering. - [ADR-0080: Environment presentation](0080-environment-presentation.md) - owned revision-tagged environment presentations, ordered freshness and bounded explicit missing/stale rendering. - [ADR-0081: Organism presentation](0081-organism-presentation.md) - biology-only organism cells without controller labels, budgeted rendering with explicit truncation and an engineering gauge inspector; ImGui deferred. +- [ADR-0082: Dev session transport](0082-dev-session-transport.md) - TLS 1.3-only Mbed TLS 4.1.1 LTS (pinned, compiled-down), dev-only HMAC grants, versioned bounded protocol v1 and session admission for M9. diff --git a/docs/architecture/NETWORK_ARCHITECTURE.md b/docs/architecture/NETWORK_ARCHITECTURE.md index 1b51954..aaa45e3 100644 --- a/docs/architecture/NETWORK_ARCHITECTURE.md +++ b/docs/architecture/NETWORK_ARCHITECTURE.md @@ -1,7 +1,9 @@ # Authoritative network architecture Status: locked server authority and untrusted clients; transport/reliability are -proposals for M10. M0 has no networking. Steam distribution follows later through +proposals for M10. 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. ```mermaid diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 18816d3..c32ec30 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -3389,3 +3389,59 @@ Limits: Docker/Gitea execution of the extended `ci-client` stage was not observe locally. No headless source changed; no full headless suite or TSan rerun. The fixture population is static presentation data, not ecology. No network, database, deployment, persistent format or simulation rule changed. + +## M9.6 narrow secure dev-session protocol — 2026-10-01 + +Implemented ADR-0082, DEV_SESSION_PROTOCOL.md and schemas/view/dev-session-v1.md: +protocol v1 codec and FrameReader, SessionAdmission, dev-only HMAC grants and a +TLS 1.3-only channel over caller byte transport, using Mbed TLS 4.1.1 LTS. + +Dependency evidence (2026-10-01): upstream BRANCHES.md lists 4.1 as LTS supported +until March 2029 (3.6 until March 2027). `mbedtls-4.1.1.tar.bz2` SHA-256 +`3359a349e23db3d5536fcee032ae7b2ecbfc08972fab643089b5cbf2a375c98c` matched the +release's published sha256sum file (same origin). License: Apache-2.0 OR +GPL-2.0-or-later; Apache-2.0 taken, copied to third_party/mbedtls/LICENSE. Release +tarballs need no Python/Perl with programs/tests off. Windows extraction skipped +one symlink inside an unused upstream PQC example directory. MSVC Release static +archives: mbedtls 647 KB, mbedx509 376 KB, tfpsacrypto 1.65 MB; the network test +executable is 584 KB. A compile-time check proves the owned TLS 1.3-only +configuration (TLS 1.2, DTLS, renegotiation, tickets, PSK modes removed) applies. + +Executed: + +- Windows MSVC 19.44 Release: `tools/windows-check.ps1 -Preset network -Action + check` 1/1; `-Preset client` 5/5 after moving the SDL-free client value targets + to the top-level CMake file. +- Linux GCC 14 Release (`build/network-linux`): network suite passed with no + owned-code warnings; existing client build 7/7. +- Clang 19 Debug ASan/UBSan (`build/network-asan`): network suite passed. +- libFuzzer (`evo-dev-session-fuzz`, ASan/UBSan, empty seed corpus, max_len 8192): + 1,272,671 runs in 61 s (cov 685, 158-entry corpus), then 269,854 runs in 31 s + after the analysis fixes; no crash, leak or canonical re-encoding abort. +- Seeded mutation fuzz inside the suite: 30,000 rounds, 8,208 accepted frames, each + re-encoded byte-identically. +- Tests also cover: explicit little-endian Intent golden; truncated, bad magic, + version 0/2, unknown type, flags, oversized, trailing and wrong-type frames; + unsupported required features; grant length bounds; invalid enum bytes; views + failing presentation rules; 257-cell counts; byte-by-byte reassembly and early + oversized-header rejection with latching; dev keys refused in stage/world0, + short/zero keys, every single-bit grant change rejected, foreign key, moved-from + key; admission world/region/lifetime/skew/expiry/reuse/duplicate/capacity/ + grant-table rules, every IntentStatus, rate windows and epoch advance; TLS 1.3 + loopback (version, suite, frame and 100 kB transfers with 700-byte partial + writes, close_notify), untrusted CA, wrong server name, mismatched private key, + bad PEM, tampered record, transport failure and plaintext rejection. +- clang-tidy 19 (warnings as errors) on the seven network translation units: + passed after restructuring enum decoding to cast only validated bytes, reading + the latched TLS error once, checking a test return value, and annotating + intentional invalid-enum/use-after-move test inputs. Repository clang-format + passed. CI path-selector fixtures passed including the new `network` suite. +- Headless: a fresh GCC 14 Release configure with default options contained no + Mbed TLS or SDL references; `evo-sim` replay of seed 123456 / 8 entities / 1000 + ticks gave unchanged `d61ad5bf0c928c76`, `replay_verified=true`. + +Limits: the `ci-network` Docker stage and Gitea matrix entry were not executed +locally. No sockets, server integration, secret provisioning or deployment exist +yet (M9.7). TLS interoperability was tested only between Mbed TLS endpoints. No +performance or latency measurement is claimed. Certificates in tests are +throwaway; no dev CA or HMAC key was created or committed. diff --git a/docs/networking/DEV_SESSION_PROTOCOL.md b/docs/networking/DEV_SESSION_PROTOCOL.md new file mode 100644 index 0000000..c84cb29 --- /dev/null +++ b/docs/networking/DEV_SESSION_PROTOCOL.md @@ -0,0 +1,93 @@ +# M9 dev session protocol + +Decision: [ADR-0082](../adr/0082-dev-session-transport.md). Wire layout: +[dev-session-v1](../../schemas/view/dev-session-v1.md). Architecture: +[NETWORK_ARCHITECTURE](../architecture/NETWORK_ARCHITECTURE.md). Scope: one +engineering client and one authoritative **dev** region. Not the M10 multiplayer +transport, not production identity, not a gameplay protocol. + +```mermaid +sequenceDiagram + participant C as evo-client + participant T as TLS 1.3 (Mbed TLS) + participant A as SessionAdmission + participant R as Region host (M9.7) + C->>T: handshake, verify pinned dev CA + server name + C->>A: Hello{dev grant} + A->>A: HMAC verify, world/region, lifetime, single use, one subject + A-->>C: Welcome{session, epoch, server tick, commit watermark, limits} + R-->>C: EnvironmentView / OrganismView{session, epoch, tick, watermark, baseline} + C->>A: Intent{session, epoch, sequence, tick, vector} + A-->>C: IntentResult{admitted | rejected reason} + A->>R: admitted intention for scheduling (server decides outcome) +``` + +## Layers and modules + +| Layer | Module | Depends on | +|---|---|---| +| Wire codec, FrameReader | `evo_network_protocol` (`protocol.hpp`) | client presentation values only | +| Session admission | `evo_network_protocol` (`admission.hpp`) | codec, world identity | +| Dev grants (HMAC-SHA256) | `evo_network_tls` (`dev_grant.hpp`) | PSA crypto (Mbed TLS) | +| TLS 1.3 channel | `evo_network_tls` (`tls_channel.hpp`) | Mbed TLS, caller `ByteTransport` | +| Sockets, server/client adapters | M9.7 | this gate | + +Nothing here links into `evo_simulation`, world stores or the region host yet. +`EVO_BUILD_NETWORK` is opt-in and requires `cmake -P tools/prepare-mbedtls.cmake`. + +## Trust and authority + +- The client authors only Hello (an opaque grant) and Intent (sequence, tick and a + signed vector of norm <= 1000). It cannot author position, energy, resources, + genome, reproduction, death or presentation values. +- Views travel server -> client and are rebuilt through the presentation + validators; a client never sends them. +- Every view and Welcome carries epoch and the durable commit watermark. Receipt + of bytes, admission for scheduling, simulation and durable outcome are distinct: + `IntentResult::admitted` is only the second. +- Session ids are server-assigned counters. They identify, they do not + authenticate; M9.7 binds each session to its TLS connection. + +## Admission rules + +`SessionAdmission` is single-owner (the host serializes calls) and bounded: +at most 256 sessions and 65536 remembered grant ids by configuration limits. + +Open: verified grant -> world/frame and region must match; lifetime <= configured +maximum; not issued beyond clock skew in the future; not expired; grant id not +seen before (remembered until its expiry; when the table is full, new grants are +refused rather than forgetting live ones); the subject has no active session; +capacity available. Reconnect = close + new grant. + +Intent (status, first failing rule wins): unknown session; rate (every attempt by +a known session in the current `window_ticks` counts, accepted or not); epoch +(message and session must match the current ownership epoch; `advance_epoch` +makes all existing sessions stale); grant expiry; vector bound; sequence strictly +greater than the last admitted; tick in `[server_tick, server_tick + window]`. +Rejected messages never advance the sequence. + +## Dev-only enforcement + +`DevGrantKey::import` refuses `stage` and `world0`. The grant carries the marker +`EVO-DEV-ONLY` and a dev environment byte under the HMAC. The key is a 32-byte +secret from dev provisioning (M9.7: a dev-namespace Secret), never logged, never +in images. A leaked dev key compromises dev sessions only; rotation is a redeploy. + +## Failure behaviour + +Decoding errors (`ProtocolError`) mean the peer is malformed: close with +`malformed`/`unsupported_version` and drop the connection. TLS errors latch per +channel; `peer_verification` means the server certificate did not chain to the +pinned CA or name. No error path retries automatically or falls back to plaintext. + +## Evidence + +`evo-network-tests` (label `network`): round trips and an explicit little-endian +golden; header/payload rejection cases; byte-by-byte FrameReader reassembly and +early oversized rejection; 30,000-round seeded mutation fuzzing with canonical +re-encoding; dev grant environment refusal, bit-flip tag failures and key misuse; +admission scope/lifetime/reuse/duplicate/capacity rules and every intent status; +TLS 1.3 loopback with suite/version checks, 100 kB transfer, close_notify, +untrusted CA, wrong name, mismatched key, record tampering, transport failure and +plaintext rejection. `evo-dev-session-fuzz` is the libFuzzer target +(`EVO_BUILD_FUZZER`, Linux Clang). Results: engineering/VALIDATION.md. diff --git a/include/evo/network/admission.hpp b/include/evo/network/admission.hpp new file mode 100644 index 0000000..5b38d37 --- /dev/null +++ b/include/evo/network/admission.hpp @@ -0,0 +1,95 @@ +#pragma once +#include "evo/network/protocol.hpp" +#include "evo/world/chunks.hpp" +#include +#include +#include +#include + +// 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 + 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 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 close(std::uint64_t session); + // New ownership epoch: existing sessions become stale and must reconnect. + [[nodiscard]] std::expected 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 sessions_; + std::vector grants_; +}; +} // namespace evo::network diff --git a/include/evo/network/dev_grant.hpp b/include/evo/network/dev_grant.hpp new file mode 100644 index 0000000..42eea0f --- /dev/null +++ b/include/evo/network/dev_grant.hpp @@ -0,0 +1,50 @@ +#pragma once +#include "evo/network/admission.hpp" +#include +#include +#include +#include +#include + +// Dev-only session grants for the M9 engineering client (ADR-0082). A grant is a +// fixed 108-byte record ("EVO-DEV-ONLY" marker, version, environment byte, claims) +// authenticated with HMAC-SHA256 from Mbed TLS PSA. It is not an account system, +// cannot be created or verified outside DeploymentEnvironment::dev, and never +// appears in logs. Production identity remains the M10.3 identity-provider adapter. +namespace evo::network { +inline constexpr std::size_t dev_grant_bytes = 108; +inline constexpr std::size_t dev_key_bytes = 32; +using DevGrant = std::array; +enum class GrantError : std::uint8_t { + not_dev_environment, + invalid_key, + invalid_claims, + malformed, + wrong_marker, + unsupported_version, + wrong_environment, + bad_tag, + crypto +}; +// Owns an imported PSA HMAC key (non-exportable). Move-only; destroys the key. +class DevGrantKey { + public: + [[nodiscard]] static std::expected + import(DeploymentEnvironment environment, std::span secret); + DevGrantKey(DevGrantKey&& other) noexcept; + DevGrantKey& operator=(DevGrantKey&& other) noexcept; + DevGrantKey(const DevGrantKey&) = delete; + DevGrantKey& operator=(const DevGrantKey&) = delete; + ~DevGrantKey(); + // Structural claim checks only (nonzero ids/scope, ordered times); admission + // applies the clock, lifetime, world/region and single-use rules. + [[nodiscard]] std::expected issue(const DevGrantClaims& claims) const; + // Constant-time tag check before any claim is trusted. + [[nodiscard]] std::expected + verify(std::span grant) const; + + private: + explicit DevGrantKey(std::uint32_t key) : key_{key} {} + std::uint32_t key_; // PSA key id; zero when moved-from. +}; +} // namespace evo::network diff --git a/include/evo/network/protocol.hpp b/include/evo/network/protocol.hpp new file mode 100644 index 0000000..522b5ae --- /dev/null +++ b/include/evo/network/protocol.hpp @@ -0,0 +1,149 @@ +#pragma once +#include "evo/client/environment_view.hpp" +#include "evo/client/input.hpp" +#include "evo/client/organism_view.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +// M9.6 dev session protocol v1 (schemas/view/dev-session-v1.md, ADR-0082). +// Explicit little-endian fields only: no native struct is ever copied to or from +// the wire. Transport-neutral; the M9 channel carries frames inside TLS 1.3. +namespace evo::network { +inline constexpr std::uint32_t frame_magic = 0x53445645; // Bytes "EVDS". +inline constexpr std::uint16_t protocol_version = 1; +inline constexpr std::size_t frame_header_bytes = 12; +inline constexpr std::size_t max_frame_payload = std::size_t{256} * 1024; +inline constexpr std::size_t max_grant_bytes = 512; +enum class MessageType : std::uint8_t { + hello = 1, + welcome, + intent, + intent_result, + environment_view, + organism_view, + close +}; +enum class ProtocolError : std::uint8_t { + truncated, + bad_magic, + unsupported_version, + unknown_type, + reserved_flags, + oversized, + malformed, + invalid_value, + trailing_bytes, + unsupported_feature, + wrong_type +}; +// A decoded frame borrows its payload from the input bytes. +struct FrameView { + MessageType type; + std::span payload; +}; +struct OwnedFrame { + MessageType type; + std::vector payload; +}; +// Exactly one complete frame; trailing bytes reject. +[[nodiscard]] std::expected +decode_frame(std::span bytes); +[[nodiscard]] std::vector encode_frame(MessageType type, + std::span payload); + +// Accumulates a byte stream into frames. The header is validated as soon as it is +// buffered, so an oversized or foreign frame is rejected before its payload is read. +// Holds at most one frame (header + max payload). Errors latch. +class FrameReader { + public: + // Consumes bytes up to the end of the current frame; returns how many were taken. + [[nodiscard]] std::expected + feed(std::span bytes); + // The completed frame, if any; reading it resets the reader for the next frame. + [[nodiscard]] std::optional take(); + + private: + std::vector buffer_; + std::size_t expected_{frame_header_bytes}; + std::optional error_; +}; + +// Client -> server. `required_features` must be zero in v1; the grant is opaque here. +struct Hello { + std::uint32_t required_features{}; + std::vector grant; +}; +// Server -> client. Session ids identify, they do not authenticate (the channel does). +struct Welcome { + std::uint64_t session{}, world{}, frame{}, region{}, epoch{}; + std::uint64_t server_tick{}, commit_watermark{}, grant_expires_at{}; + std::uint32_t intent_window{}, window_ticks{}, max_intents_per_window{}; +}; +// Client -> server: one bounded movement intention (client::Intention on the wire). +struct IntentMessage { + std::uint64_t session{}, epoch{}, sequence{}, tick{}; + std::int16_t move_x{}, move_y{}; +}; +enum class IntentStatus : std::uint8_t { + admitted, + unknown_session, + stale_epoch, + expired, + invalid_vector, + replayed, + outside_window, + rate_limited +}; +// "admitted" means accepted for scheduling at `tick`, not simulated or durable. +struct IntentResult { + std::uint64_t sequence{}; + IntentStatus status{}; + std::uint64_t server_tick{}; +}; +// Every view names the session/epoch it belongs to and the durable commit watermark. +struct ViewHeader { + std::uint64_t session{}, epoch{}, server_tick{}, commit_watermark{}, baseline{}; +}; +enum class CloseReason : std::uint8_t { + normal, + unsupported_version, + malformed, + unauthenticated, + expired, + duplicate_session, + stale_epoch, + rate_limited, + server_shutdown +}; + +[[nodiscard]] std::expected, ProtocolError> encode(const Hello& m); +[[nodiscard]] std::vector encode(const Welcome& m); +[[nodiscard]] std::vector encode(const IntentMessage& m); +[[nodiscard]] std::vector encode(const IntentResult& m); +[[nodiscard]] std::vector encode(CloseReason reason); +[[nodiscard]] std::vector encode(const ViewHeader& header, + const client::EnvironmentPresentation& view); +[[nodiscard]] std::vector encode(const ViewHeader& header, + const client::OrganismPresentation& view); + +// Decoders take a complete frame and check its type. Views are rebuilt through the +// validated presentation constructors, never trusted as decoded. +[[nodiscard]] std::expected decode_hello(FrameView frame); +[[nodiscard]] std::expected decode_welcome(FrameView frame); +[[nodiscard]] std::expected decode_intent(FrameView frame); +[[nodiscard]] std::expected decode_intent_result(FrameView frame); +[[nodiscard]] std::expected decode_close(FrameView frame); +[[nodiscard]] std::expected, ProtocolError> +decode_environment_view(FrameView frame); +[[nodiscard]] std::expected, ProtocolError> +decode_organism_view(FrameView frame); + +[[nodiscard]] IntentMessage make_intent(std::uint64_t session, std::uint64_t epoch, + const client::Intention& intention) noexcept; +} // namespace evo::network diff --git a/include/evo/network/tls_channel.hpp b/include/evo/network/tls_channel.hpp new file mode 100644 index 0000000..e0b33ea --- /dev/null +++ b/include/evo/network/tls_channel.hpp @@ -0,0 +1,66 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +// TLS 1.3 channel over caller-owned non-blocking byte transport (ADR-0082). The +// M9.7 socket adapter supplies the transport; tests use in-memory pipes. No Mbed TLS +// type crosses this header. One owner thread per channel. +namespace evo::network { +enum class TransportError : std::uint8_t { closed, failed }; +class ByteTransport { + public: + virtual ~ByteTransport() = default; + // Bytes moved; zero means "would block". Errors end the channel. + [[nodiscard]] virtual std::expected + write(std::span bytes) = 0; + [[nodiscard]] virtual std::expected + read(std::span bytes) = 0; +}; +// PEM inputs are secrets (key) or trust anchors; never logged or retained as text. +struct ServerCredentials { + std::string certificate_chain_pem; + std::string private_key_pem; +}; +struct ClientTrust { + std::string authority_pem; // Pinned dev CA; the system trust store is not used. + std::string server_name; // Checked against the certificate. +}; +enum class TlsError : std::uint8_t { + credentials, + configuration, + handshake, + peer_verification, + transport, + closed, + record +}; +enum class TlsProgress : std::uint8_t { done, want_io }; +class TlsChannel { + public: + [[nodiscard]] static std::expected, TlsError> + server(const ServerCredentials& credentials, ByteTransport& transport); + [[nodiscard]] static std::expected, TlsError> + client(const ClientTrust& trust, ByteTransport& transport); + TlsChannel(const TlsChannel&) = delete; + TlsChannel& operator=(const TlsChannel&) = delete; + ~TlsChannel(); + [[nodiscard]] std::expected handshake(); + // Zero means "would block". Errors latch until destruction. + [[nodiscard]] std::expected send(std::span bytes); + [[nodiscard]] std::expected receive(std::span bytes); + [[nodiscard]] std::expected close(); + // Negotiated "TLSv1.3" and suite name after a completed handshake. + [[nodiscard]] std::string_view version() const noexcept; + [[nodiscard]] std::string_view suite() const noexcept; + + private: + struct Impl; + explicit TlsChannel(std::unique_ptr impl); + std::unique_ptr impl_; +}; +} // namespace evo::network diff --git a/schemas/view/dev-session-v1.md b/schemas/view/dev-session-v1.md new file mode 100644 index 0000000..9602344 --- /dev/null +++ b/schemas/view/dev-session-v1.md @@ -0,0 +1,79 @@ +# Dev session protocol v1 (wire schema) + +Decision: [ADR-0082](../../docs/adr/0082-dev-session-transport.md). Implementation: +`include/evo/network/protocol.hpp`, `src/network/protocol/codec.cpp`. Carried inside +TLS 1.3 only. All integers are little-endian, fixed width, with no padding or +alignment; `i` types are two's complement. Encodings are canonical: a decoded +message re-encodes to identical bytes (fuzz-tested). + +## Frame + +| Offset | Field | Type | Rule | +|---|---|---|---| +| 0 | magic | u32 | `0x53445645` (bytes `E V D S`) | +| 4 | version | u16 | exactly 1, otherwise `unsupported_version` | +| 6 | type | u8 | 1..7, otherwise `unknown_type` | +| 7 | flags | u8 | 0, otherwise `reserved_flags` | +| 8 | length | u32 | payload bytes, <= 262144 (`oversized`) | +| 12 | payload | bytes | exactly `length`; short `truncated`, extra `trailing_bytes` | + +Payload decoders also reject truncation, trailing bytes, out-of-range enum bytes +(`invalid_value`) and values failing the presentation contracts (`invalid_value`). + +## Messages + +| Type | Name | Direction | Payload | +|---|---|---|---| +| 1 | Hello | C->S | required_features u32 (must be 0: `unsupported_feature`), grant_length u16 (1..512), grant bytes | +| 2 | Welcome | S->C | session, world, frame, region, epoch, server_tick, commit_watermark, grant_expires_at (u64 each); intent_window, window_ticks, max_intents_per_window (u32 each; session, window_ticks and max nonzero) | +| 3 | Intent | C->S | session, epoch, sequence, tick (u64); move_x, move_y (i16) — 36 bytes | +| 4 | IntentResult | S->C | sequence u64, status u8 (0..7), server_tick u64 | +| 5 | EnvironmentView | S->C | view header, environment presentation | +| 6 | OrganismView | S->C | view header, organism presentation | +| 7 | Close | both | reason u8 (0..8) | + +IntentStatus: 0 admitted, 1 unknown_session, 2 stale_epoch, 3 expired, +4 invalid_vector, 5 replayed, 6 outside_window, 7 rate_limited. +CloseReason: 0 normal, 1 unsupported_version, 2 malformed, 3 unauthenticated, +4 expired, 5 duplicate_session, 6 stale_epoch, 7 rate_limited, 8 server_shutdown. + +View header (40 bytes): session, epoch, server_tick, commit_watermark, baseline (u64). + +Environment presentation: world, frame, tick, field_revision (u64); mask u32; +slice_z i64; count u16 (<= 256); then per cell: x, y (i64; z = slice_z); +chunk_revision u64; habitat x, y, z, width, height, depth (u32 micrometres); +volume u64; substrate, terrain_material, terrain_fill (u16); for each of the ten +fields a presence u8 (0 not_requested, 1 missing, 2 present) followed by a u64 +quantity only when present; moisture status u8 (0 not_requested, 1 missing_water, +2 zero_capacity, 3 available) followed by numerator, denominator (u64) only when +available. The cells are then validated by `EnvironmentPresentation::create`. + +Organism presentation: world, frame, tick, revision (u64); slice_z i64; count u16 +(<= 1024); then per cell: id u64; x, y (i64); offset x, y (u32 um); structural, +membrane, total mass (u64); extent u32; surface, maintenance (u64); movement +speed u32; ingestion rate u64; conversion, thermal shortfall (u32); realized, +damage (u32 per thousand); state u8 (0 alive, 1 dead). Validated by +`OrganismPresentation::create`. There is no controller, owner or label field. + +Maximum payload sizes: Hello 518 bytes, environment view about 42 KiB at 256 +cells, organism view about 105 KiB at 1024 cells; all within one frame. + +## Dev grant (opaque in Hello; 108 bytes) + +| Offset | Field | Type | +|---|---|---| +| 0 | marker | `EVO-DEV-ONLY` + 4 zero bytes | +| 16 | version | u16 = 1 | +| 18 | environment | u8 = 1 (dev) | +| 19 | reserved | u8 = 0 | +| 20 | grant_id, subject, world, frame, region, issued_at, expires_at | u64 each | +| 76 | tag | HMAC-SHA256 over bytes 0..75 | + +The tag is verified before any other field is interpreted. + +## Compatibility + +v1 has no optional extensions: `required_features` and `flags` must be zero, and a +peer receiving another version closes with `unsupported_version`. Any field change +is a new version with an explicit compatibility decision; nothing is negotiated +silently. These messages are transient and never persisted. diff --git a/src/network/protocol/admission.cpp b/src/network/protocol/admission.cpp new file mode 100644 index 0000000..d630bac --- /dev/null +++ b/src/network/protocol/admission.cpp @@ -0,0 +1,118 @@ +#include "evo/network/admission.hpp" +#include + +namespace evo::network { +namespace { +constexpr std::int32_t max_component = 1000; +bool valid_vector(std::int16_t x, std::int16_t y) { + const std::int32_t ix = x, iy = y; + return ix >= -max_component && ix <= max_component && iy >= -max_component && + iy <= max_component && ix * ix + iy * iy <= max_component * max_component; +} +} // namespace + +std::expected SessionAdmission::create(AdmissionConfig config) { + if (config.world.world == 0 || config.world.frame == 0 || config.region == 0 || + config.epoch == 0 || config.max_sessions == 0 || config.max_sessions > 256 || + config.max_remembered_grants < config.max_sessions || + config.max_remembered_grants > 65536 || config.intent_window == 0 || + config.window_ticks == 0 || config.max_intents_per_window == 0 || + config.max_grant_lifetime == 0 || config.max_grant_lifetime > 86400 || + config.clock_skew > 300) + return std::unexpected(AdmissionError::invalid_config); + SessionAdmission admission{config}; + admission.sessions_.reserve(config.max_sessions); + admission.grants_.reserve(config.max_remembered_grants); + return admission; +} + +std::expected SessionAdmission::open(const VerifiedGrant& grant, + std::uint64_t now_seconds, + std::uint64_t server_tick, + std::uint64_t commit_watermark) { + const auto& c = grant.claims(); + if (c.world != config_.world) + return std::unexpected(AdmissionError::wrong_world); + if (c.region != config_.region) + return std::unexpected(AdmissionError::wrong_region); + if (c.expires_at <= c.issued_at || c.expires_at - c.issued_at > config_.max_grant_lifetime) + return std::unexpected(AdmissionError::invalid_lifetime); + if (c.issued_at > now_seconds && c.issued_at - now_seconds > config_.clock_skew) + return std::unexpected(AdmissionError::not_yet_valid); + if (now_seconds >= c.expires_at) + return std::unexpected(AdmissionError::expired); + // Forget only grants that can no longer verify as unexpired. + std::erase_if(grants_, [&](const Remembered& r) { return r.expires_at <= now_seconds; }); + if (std::ranges::any_of(grants_, [&](const Remembered& r) { return r.grant_id == c.grant_id; })) + return std::unexpected(AdmissionError::grant_reused); + if (std::ranges::any_of(sessions_, [&](const Session& s) { return s.subject == c.subject; })) + return std::unexpected(AdmissionError::duplicate_subject); + if (sessions_.size() >= config_.max_sessions) + return std::unexpected(AdmissionError::sessions_full); + if (grants_.size() >= config_.max_remembered_grants) + return std::unexpected(AdmissionError::grants_full); // Fail closed, never forget early. + grants_.push_back({c.grant_id, c.expires_at}); + const auto id = next_session_++; + sessions_.push_back({id, c.subject, config_.epoch, c.expires_at, 0, server_tick, 0}); + Welcome welcome; + welcome.session = id; + welcome.world = config_.world.world; + welcome.frame = config_.world.frame; + welcome.region = config_.region; + welcome.epoch = config_.epoch; + welcome.server_tick = server_tick; + welcome.commit_watermark = commit_watermark; + welcome.grant_expires_at = c.expires_at; + welcome.intent_window = config_.intent_window; + welcome.window_ticks = config_.window_ticks; + welcome.max_intents_per_window = config_.max_intents_per_window; + return welcome; +} + +IntentResult SessionAdmission::admit(const IntentMessage& intent, std::uint64_t server_tick, + std::uint64_t now_seconds) { + IntentResult result{intent.sequence, IntentStatus::admitted, server_tick}; + const auto found = + std::ranges::find_if(sessions_, [&](const Session& s) { return s.id == intent.session; }); + if (found == sessions_.end()) { + result.status = IntentStatus::unknown_session; + return result; + } + auto& s = *found; + // Every attempt by a known session counts toward its rate, admitted or not. + if (server_tick < s.window_start || server_tick - s.window_start >= config_.window_ticks) { + s.window_start = server_tick; + s.attempts = 0; + } + if (s.attempts >= config_.max_intents_per_window) + result.status = IntentStatus::rate_limited; + else if (intent.epoch != config_.epoch || s.epoch != config_.epoch) + result.status = IntentStatus::stale_epoch; + else if (now_seconds >= s.expires_at) + result.status = IntentStatus::expired; + else if (!valid_vector(intent.move_x, intent.move_y)) + result.status = IntentStatus::invalid_vector; + else if (intent.sequence <= s.last_sequence) + result.status = IntentStatus::replayed; + else if (intent.tick < server_tick || intent.tick - server_tick > config_.intent_window) + result.status = IntentStatus::outside_window; + if (s.attempts < config_.max_intents_per_window) + ++s.attempts; + if (result.status == IntentStatus::admitted) + s.last_sequence = intent.sequence; + return result; +} + +std::expected SessionAdmission::close(std::uint64_t session) { + if (std::erase_if(sessions_, [&](const Session& s) { return s.id == session; }) == 0) + return std::unexpected(AdmissionError::unknown_session); + return {}; +} + +std::expected SessionAdmission::advance_epoch(std::uint64_t epoch) { + if (epoch <= config_.epoch) + return std::unexpected(AdmissionError::invalid_config); + config_.epoch = epoch; + return {}; +} +} // namespace evo::network diff --git a/src/network/protocol/codec.cpp b/src/network/protocol/codec.cpp new file mode 100644 index 0000000..ec6c9fb --- /dev/null +++ b/src/network/protocol/codec.cpp @@ -0,0 +1,528 @@ +#include "evo/network/protocol.hpp" +#include +#include +#include + +namespace evo::network { +namespace { +constexpr std::uint8_t last_type = static_cast(MessageType::close); +constexpr std::uint8_t last_status = static_cast(IntentStatus::rate_limited); +constexpr std::uint8_t last_reason = static_cast(CloseReason::server_shutdown); +constexpr std::uint8_t last_presence = static_cast(client::FieldPresence::present); +constexpr std::uint8_t last_moisture = static_cast(world::MoistureStatus::available); +constexpr std::uint8_t last_state = static_cast(client::OrganismState::dead); + +class Writer { + public: + void u8(std::uint8_t v) { + bytes_.push_back(v); + } + void u16(std::uint16_t v) { + put(v, 2); + } + void u32(std::uint32_t v) { + put(v, 4); + } + void u64(std::uint64_t v) { + put(v, 8); + } + void i16(std::int16_t v) { + u16(std::bit_cast(v)); + } + void i64(std::int64_t v) { + u64(std::bit_cast(v)); + } + void raw(std::span v) { + bytes_.insert(bytes_.end(), v.begin(), v.end()); + } + [[nodiscard]] std::vector take() { + return std::move(bytes_); + } + + private: + void put(std::uint64_t v, int count) { + for (int i = 0; i < count; ++i) + bytes_.push_back(static_cast(v >> (8 * i))); + } + std::vector bytes_; +}; + +// Bounds-checked little-endian reader; the first failure is sticky. +class Reader { + public: + explicit Reader(std::span bytes) : bytes_{bytes} {} + std::uint8_t u8() { + return static_cast(get(1)); + } + std::uint16_t u16() { + return static_cast(get(2)); + } + std::uint32_t u32() { + return static_cast(get(4)); + } + std::uint64_t u64() { + return get(8); + } + std::int16_t i16() { + return std::bit_cast(u16()); + } + std::int64_t i64() { + return std::bit_cast(u64()); + } + std::span raw(std::size_t count) { + if (!ok_ || bytes_.size() - at_ < count) { + ok_ = false; + return {}; + } + const auto out = bytes_.subspan(at_, count); + at_ += count; + return out; + } + // Rejects a value outside [0, last] without consuming further input. + std::uint8_t bounded(std::uint8_t last) { + const auto v = u8(); + if (v > last) + invalid_ = true; + return v; + } + void invalid() { + invalid_ = true; + } + [[nodiscard]] std::expected finish() const { + if (!ok_) + return std::unexpected(ProtocolError::truncated); + if (invalid_) + return std::unexpected(ProtocolError::invalid_value); + if (at_ != bytes_.size()) + return std::unexpected(ProtocolError::trailing_bytes); + return {}; + } + [[nodiscard]] bool ok() const noexcept { + return ok_ && !invalid_; + } + + private: + std::uint64_t get(std::size_t count) { + if (!ok_ || bytes_.size() - at_ < count) { + ok_ = false; + return 0; + } + std::uint64_t v = 0; + for (std::size_t i = 0; i < count; ++i) + v |= static_cast(bytes_[at_ + i]) << (8 * i); + at_ += count; + return v; + } + std::span bytes_; + std::size_t at_{}; + bool ok_{true}, invalid_{}; +}; + +// Casts only on the validated path; an out-of-range byte flags the reader invalid +// and yields the enum's zero value, which the decoder then discards. +template E enumerator(Reader& r, std::uint8_t last) { + const auto v = r.bounded(last); + return v <= last ? static_cast(v) : E{}; +} +struct Header { + MessageType type; + std::uint32_t length; +}; +std::expected parse_header(std::span bytes) { + Reader r{bytes.first(frame_header_bytes)}; + const auto magic = r.u32(); + const auto version = r.u16(); + const auto type = r.u8(); + const auto flags = r.u8(); + const auto length = r.u32(); + if (magic != frame_magic) + return std::unexpected(ProtocolError::bad_magic); + if (version != protocol_version) + return std::unexpected(ProtocolError::unsupported_version); + if (type == 0 || type > last_type) + return std::unexpected(ProtocolError::unknown_type); + if (flags != 0) + return std::unexpected(ProtocolError::reserved_flags); + if (length > max_frame_payload) + return std::unexpected(ProtocolError::oversized); + return Header{static_cast(type), length}; +} +std::vector framed(MessageType type, Writer& payload) { + const auto bytes = payload.take(); + return encode_frame(type, bytes); +} +std::expected open(FrameView frame, MessageType type) { + if (frame.type != type) + return std::unexpected(ProtocolError::wrong_type); + return Reader{frame.payload}; +} +void write(Writer& w, const ViewHeader& h) { + w.u64(h.session); + w.u64(h.epoch); + w.u64(h.server_tick); + w.u64(h.commit_watermark); + w.u64(h.baseline); +} +ViewHeader read_view_header(Reader& r) { + ViewHeader h; + h.session = r.u64(); + h.epoch = r.u64(); + h.server_tick = r.u64(); + h.commit_watermark = r.u64(); + h.baseline = r.u64(); + return h; +} +} // namespace + +std::vector encode_frame(MessageType type, std::span payload) { + Writer w; + w.u32(frame_magic); + w.u16(protocol_version); + w.u8(static_cast(type)); + w.u8(0); + w.u32(static_cast(payload.size())); + w.raw(payload); + return w.take(); +} + +std::expected decode_frame(std::span bytes) { + if (bytes.size() < frame_header_bytes) + return std::unexpected(ProtocolError::truncated); + const auto header = parse_header(bytes); + if (!header) + return std::unexpected(header.error()); + const auto total = frame_header_bytes + header->length; + if (bytes.size() < total) + return std::unexpected(ProtocolError::truncated); + if (bytes.size() > total) + return std::unexpected(ProtocolError::trailing_bytes); + return FrameView{header->type, bytes.subspan(frame_header_bytes)}; +} + +std::expected FrameReader::feed(std::span bytes) { + if (error_) + return std::unexpected(*error_); + std::size_t taken = 0; + while (taken < bytes.size() && buffer_.size() < expected_) { + const auto count = std::min(expected_ - buffer_.size(), bytes.size() - taken); + buffer_.insert(buffer_.end(), bytes.begin() + static_cast(taken), + bytes.begin() + static_cast(taken + count)); + taken += count; + if (buffer_.size() == frame_header_bytes && expected_ == frame_header_bytes) { + const auto header = parse_header(buffer_); + if (!header) { + error_ = header.error(); + buffer_.clear(); + return std::unexpected(*error_); + } + expected_ = frame_header_bytes + header->length; + if (header->length == 0) + break; + } + } + return taken; +} + +std::optional FrameReader::take() { + if (error_ || buffer_.size() < frame_header_bytes || buffer_.size() != expected_) + return std::nullopt; + OwnedFrame frame{static_cast(buffer_[6]), + {buffer_.begin() + frame_header_bytes, buffer_.end()}}; + buffer_.clear(); + expected_ = frame_header_bytes; + return frame; +} + +std::expected, ProtocolError> encode(const Hello& m) { + if (m.grant.empty() || m.grant.size() > max_grant_bytes) + return std::unexpected(ProtocolError::invalid_value); + Writer w; + w.u32(m.required_features); + w.u16(static_cast(m.grant.size())); + w.raw(m.grant); + return framed(MessageType::hello, w); +} + +std::vector encode(const Welcome& m) { + Writer w; + for (const auto v : {m.session, m.world, m.frame, m.region, m.epoch, m.server_tick, + m.commit_watermark, m.grant_expires_at}) + w.u64(v); + w.u32(m.intent_window); + w.u32(m.window_ticks); + w.u32(m.max_intents_per_window); + return framed(MessageType::welcome, w); +} + +std::vector encode(const IntentMessage& m) { + Writer w; + w.u64(m.session); + w.u64(m.epoch); + w.u64(m.sequence); + w.u64(m.tick); + w.i16(m.move_x); + w.i16(m.move_y); + return framed(MessageType::intent, w); +} + +std::vector encode(const IntentResult& m) { + Writer w; + w.u64(m.sequence); + w.u8(static_cast(m.status)); + w.u64(m.server_tick); + return framed(MessageType::intent_result, w); +} + +std::vector encode(CloseReason reason) { + Writer w; + w.u8(static_cast(reason)); + return framed(MessageType::close, w); +} + +std::vector encode(const ViewHeader& header, + const client::EnvironmentPresentation& view) { + Writer w; + write(w, header); + const auto revision = view.revision(); + w.u64(revision.identity.world); + w.u64(revision.identity.frame); + w.u64(revision.tick); + w.u64(revision.field_revision); + w.u32(view.requested_mask()); + w.i64(view.slice_z()); + w.u16(static_cast(view.cells().size())); + for (const auto& c : view.cells()) { + w.i64(c.voxel.x); + w.i64(c.voxel.y); + w.u64(c.chunk_revision); + for (const auto v : {c.habitat.x, c.habitat.y, c.habitat.z, c.habitat.width, + c.habitat.height, c.habitat.depth}) + w.u32(static_cast(v.value)); // Validated <= 1e6 um. + w.u64(c.volume.value); + w.u16(c.substrate.value); + w.u16(c.terrain_material.value); + w.u16(c.terrain_fill); + for (std::size_t k = 0; k < world::field_count; ++k) { + w.u8(static_cast(c.presence.at(k))); + if (c.presence.at(k) == client::FieldPresence::present) + w.u64(c.quantity.at(k)); + } + w.u8(static_cast(c.moisture_status)); + if (c.moisture_status == world::MoistureStatus::available) { + w.u64(c.moisture.numerator); + w.u64(c.moisture.denominator); + } + } + return framed(MessageType::environment_view, w); +} + +std::vector encode(const ViewHeader& header, + const client::OrganismPresentation& view) { + Writer w; + write(w, header); + const auto revision = view.revision(); + w.u64(revision.identity.world); + w.u64(revision.identity.frame); + w.u64(revision.tick); + w.u64(revision.revision); + w.i64(view.slice_z()); + w.u16(static_cast(view.cells().size())); + for (const auto& c : view.cells()) { + const auto& p = c.phenotype; + w.u64(c.id); + w.i64(c.voxel.x); + w.i64(c.voxel.y); + w.u32(static_cast(c.x.value)); // Validated < 1e6 um. + w.u32(static_cast(c.y.value)); + w.u64(p.structural_mass); + w.u64(p.membrane_mass); + w.u64(p.total_mass); + w.u32(p.extent); + w.u64(p.surface); + w.u64(p.maintenance); + w.u32(p.movement_speed); + w.u64(p.ingestion_rate); + w.u32(p.conversion_per_thousand); + w.u32(p.thermal_shortfall); + w.u32(c.growth.realized_per_thousand); + w.u32(c.growth.damage_per_thousand); + w.u8(static_cast(c.state)); + } + return framed(MessageType::organism_view, w); +} + +std::expected decode_hello(FrameView frame) { + auto r = open(frame, MessageType::hello); + if (!r) + return std::unexpected(r.error()); + Hello m; + m.required_features = r->u32(); + const auto length = r->u16(); + if (length == 0 || length > max_grant_bytes) + r->invalid(); + const auto grant = r->raw(r->ok() ? length : 0); + if (auto done = r->finish(); !done) + return std::unexpected(done.error()); + if (m.required_features != 0) + return std::unexpected(ProtocolError::unsupported_feature); + m.grant.assign(grant.begin(), grant.end()); + return m; +} + +std::expected decode_welcome(FrameView frame) { + auto r = open(frame, MessageType::welcome); + if (!r) + return std::unexpected(r.error()); + Welcome m; + m.session = r->u64(); + m.world = r->u64(); + m.frame = r->u64(); + m.region = r->u64(); + m.epoch = r->u64(); + m.server_tick = r->u64(); + m.commit_watermark = r->u64(); + m.grant_expires_at = r->u64(); + m.intent_window = r->u32(); + m.window_ticks = r->u32(); + m.max_intents_per_window = r->u32(); + if (auto done = r->finish(); !done) + return std::unexpected(done.error()); + if (m.session == 0 || m.window_ticks == 0 || m.max_intents_per_window == 0) + return std::unexpected(ProtocolError::invalid_value); + return m; +} + +std::expected decode_intent(FrameView frame) { + auto r = open(frame, MessageType::intent); + if (!r) + return std::unexpected(r.error()); + IntentMessage m; + m.session = r->u64(); + m.epoch = r->u64(); + m.sequence = r->u64(); + m.tick = r->u64(); + m.move_x = r->i16(); + m.move_y = r->i16(); + if (auto done = r->finish(); !done) + return std::unexpected(done.error()); + return m; // Range/semantics belong to admission, which reports a status. +} + +std::expected decode_intent_result(FrameView frame) { + auto r = open(frame, MessageType::intent_result); + if (!r) + return std::unexpected(r.error()); + IntentResult m; + m.sequence = r->u64(); + m.status = enumerator(*r, last_status); + m.server_tick = r->u64(); + if (auto done = r->finish(); !done) + return std::unexpected(done.error()); + return m; +} + +std::expected decode_close(FrameView frame) { + auto r = open(frame, MessageType::close); + if (!r) + return std::unexpected(r.error()); + const auto reason = enumerator(*r, last_reason); + if (auto done = r->finish(); !done) + return std::unexpected(done.error()); + return reason; +} + +std::expected, ProtocolError> +decode_environment_view(FrameView frame) { + auto r = open(frame, MessageType::environment_view); + if (!r) + return std::unexpected(r.error()); + const auto header = read_view_header(*r); + client::EnvironmentRevision revision; + revision.identity.world = r->u64(); + revision.identity.frame = r->u64(); + revision.tick = r->u64(); + revision.field_revision = r->u64(); + const auto mask = r->u32(); + const auto slice = r->i64(); + const auto count = r->u16(); + if (count > client::max_environment_cells) + r->invalid(); + std::vector cells; + for (std::uint16_t i = 0; r->ok() && i < count; ++i) { + client::EnvironmentCell c; + c.voxel = {r->i64(), r->i64(), slice}; + c.chunk_revision = r->u64(); + c.habitat = {{r->u32()}, {r->u32()}, {r->u32()}, {r->u32()}, {r->u32()}, {r->u32()}}; + c.volume = {r->u64()}; + c.substrate = {r->u16()}; + c.terrain_material = {r->u16()}; + c.terrain_fill = r->u16(); + for (std::size_t k = 0; k < world::field_count; ++k) { + c.presence.at(k) = enumerator(*r, last_presence); + if (c.presence.at(k) == client::FieldPresence::present) + c.quantity.at(k) = r->u64(); + } + c.moisture_status = enumerator(*r, last_moisture); + if (c.moisture_status == world::MoistureStatus::available) + c.moisture = {r->u64(), r->u64()}; + cells.push_back(c); + } + if (auto done = r->finish(); !done) + return std::unexpected(done.error()); + auto view = client::EnvironmentPresentation::create(revision, mask, slice, cells); + if (!view) + return std::unexpected(ProtocolError::invalid_value); + return std::pair{header, std::move(*view)}; +} + +std::expected, ProtocolError> +decode_organism_view(FrameView frame) { + auto r = open(frame, MessageType::organism_view); + if (!r) + return std::unexpected(r.error()); + const auto header = read_view_header(*r); + client::OrganismRevision revision; + revision.identity.world = r->u64(); + revision.identity.frame = r->u64(); + revision.tick = r->u64(); + revision.revision = r->u64(); + const auto slice = r->i64(); + const auto count = r->u16(); + if (count > client::max_organism_cells) + r->invalid(); + std::vector cells; + for (std::uint16_t i = 0; r->ok() && i < count; ++i) { + client::OrganismCell c; + c.id = r->u64(); + c.voxel = {r->i64(), r->i64(), slice}; + c.x = {r->u32()}; + c.y = {r->u32()}; + auto& p = c.phenotype; + p.structural_mass = r->u64(); + p.membrane_mass = r->u64(); + p.total_mass = r->u64(); + p.extent = r->u32(); + p.surface = r->u64(); + p.maintenance = r->u64(); + p.movement_speed = r->u32(); + p.ingestion_rate = r->u64(); + p.conversion_per_thousand = r->u32(); + p.thermal_shortfall = r->u32(); + c.growth.realized_per_thousand = r->u32(); + c.growth.damage_per_thousand = r->u32(); + c.state = enumerator(*r, last_state); + cells.push_back(c); + } + if (auto done = r->finish(); !done) + return std::unexpected(done.error()); + auto view = client::OrganismPresentation::create(revision, slice, cells); + if (!view) + return std::unexpected(ProtocolError::invalid_value); + return std::pair{header, std::move(*view)}; +} + +IntentMessage make_intent(std::uint64_t session, std::uint64_t epoch, + const client::Intention& intention) noexcept { + return {session, epoch, intention.sequence, intention.tick, intention.move_x, intention.move_y}; +} +} // namespace evo::network diff --git a/src/network/tls/channel.cpp b/src/network/tls/channel.cpp new file mode 100644 index 0000000..76325a3 --- /dev/null +++ b/src/network/tls/channel.cpp @@ -0,0 +1,204 @@ +#include "evo/network/tls_channel.hpp" +#include +#include +#include +#include +#include +#include + +// The owned user config (third_party/mbedtls/evo_mbedtls_config.h) must be in effect. +#if defined(MBEDTLS_SSL_PROTO_TLS1_2) || defined(MBEDTLS_SSL_PROTO_DTLS) || \ + defined(MBEDTLS_SSL_SESSION_TICKETS) || defined(MBEDTLS_SSL_RENEGOTIATION) || \ + defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED) || \ + defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED) || \ + !defined(MBEDTLS_SSL_PROTO_TLS1_3) +#error "ADR-0082 requires the TLS 1.3-only Mbed TLS configuration" +#endif + +namespace evo::network { +namespace { +// ADR-0082 profile: TLS 1.3 only, AEAD suites, ECDHE groups, ephemeral key exchange. +constexpr int suites[]{MBEDTLS_TLS1_3_AES_128_GCM_SHA256, MBEDTLS_TLS1_3_CHACHA20_POLY1305_SHA256, + 0}; +constexpr std::uint16_t groups[]{MBEDTLS_SSL_IANA_TLS_GROUP_X25519, + MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, 0}; +const unsigned char* bytes_of(const std::string& text) { + return reinterpret_cast(text.c_str()); +} +} // namespace + +struct TlsChannel::Impl { + explicit Impl(ByteTransport& t) : transport{t} { + mbedtls_ssl_config_init(&config); + mbedtls_ssl_init(&ssl); + mbedtls_x509_crt_init(&chain); + mbedtls_x509_crt_init(&authority); + mbedtls_pk_init(&key); + } + ~Impl() { + mbedtls_ssl_free(&ssl); + mbedtls_ssl_config_free(&config); + mbedtls_x509_crt_free(&chain); + mbedtls_x509_crt_free(&authority); + mbedtls_pk_free(&key); // Zeroizes key material. + } + Impl(const Impl&) = delete; + Impl& operator=(const Impl&) = delete; + static int send(void* context, const unsigned char* buffer, std::size_t length) { + auto& self = *static_cast(context); + const auto result = self.transport.write( + {buffer, std::min(length, static_cast(INT_MAX))}); + if (!result) { + self.transport_failed = true; + return MBEDTLS_ERR_SSL_INTERNAL_ERROR; + } + return *result == 0 ? MBEDTLS_ERR_SSL_WANT_WRITE : static_cast(*result); + } + static int receive(void* context, unsigned char* buffer, std::size_t length) { + auto& self = *static_cast(context); + const auto result = self.transport.read( + {buffer, std::min(length, static_cast(INT_MAX))}); + if (!result) { + if (result.error() == TransportError::closed) + return 0; // End of stream. + self.transport_failed = true; + return MBEDTLS_ERR_SSL_INTERNAL_ERROR; + } + return *result == 0 ? MBEDTLS_ERR_SSL_WANT_READ : static_cast(*result); + } + bool common(int endpoint) { + if (psa_crypto_init() != PSA_SUCCESS || + mbedtls_ssl_config_defaults(&config, endpoint, MBEDTLS_SSL_TRANSPORT_STREAM, + MBEDTLS_SSL_PRESET_DEFAULT) != 0) + return false; + mbedtls_ssl_conf_min_tls_version(&config, MBEDTLS_SSL_VERSION_TLS1_3); + mbedtls_ssl_conf_max_tls_version(&config, MBEDTLS_SSL_VERSION_TLS1_3); + mbedtls_ssl_conf_ciphersuites(&config, suites); + mbedtls_ssl_conf_groups(&config, groups); + mbedtls_ssl_conf_tls13_key_exchange_modes(&config, + MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL); + return true; + } + bool attach() { + if (mbedtls_ssl_setup(&ssl, &config) != 0) + return false; + mbedtls_ssl_set_bio(&ssl, this, send, receive, nullptr); + return true; + } + std::unexpected fail(TlsError e) { + error = e; + return std::unexpected(e); + } + std::unexpected fail_from(int code, TlsError otherwise) { + if (transport_failed) + return fail(TlsError::transport); + // Primary chain/name rejection or secondary certificate checks (ssl_tls.c). + if (code == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || code == MBEDTLS_ERR_SSL_BAD_CERTIFICATE) + return fail(TlsError::peer_verification); + return fail(otherwise); + } + ByteTransport& transport; + mbedtls_ssl_config config; + mbedtls_ssl_context ssl; + mbedtls_x509_crt chain, authority; + mbedtls_pk_context key; + bool transport_failed{}; + std::optional error; +}; + +TlsChannel::TlsChannel(std::unique_ptr impl) : impl_{std::move(impl)} {} +TlsChannel::~TlsChannel() = default; + +std::expected, TlsError> +TlsChannel::server(const ServerCredentials& credentials, ByteTransport& transport) { + auto impl = std::make_unique(transport); + if (!impl->common(MBEDTLS_SSL_IS_SERVER)) + return std::unexpected(TlsError::configuration); + // PEM parsing requires the terminating NUL in the length. + if (mbedtls_x509_crt_parse(&impl->chain, bytes_of(credentials.certificate_chain_pem), + credentials.certificate_chain_pem.size() + 1) != 0 || + mbedtls_pk_parse_key(&impl->key, bytes_of(credentials.private_key_pem), + credentials.private_key_pem.size() + 1, nullptr, 0) != 0 || + mbedtls_pk_check_pair(&impl->chain.pk, &impl->key) != 0 || + mbedtls_ssl_conf_own_cert(&impl->config, &impl->chain, &impl->key) != 0) + return std::unexpected(TlsError::credentials); + // Client identity is the in-channel grant, not a client certificate. + mbedtls_ssl_conf_authmode(&impl->config, MBEDTLS_SSL_VERIFY_NONE); + if (!impl->attach()) + return std::unexpected(TlsError::configuration); + return std::unique_ptr(new TlsChannel(std::move(impl))); +} + +std::expected, TlsError> TlsChannel::client(const ClientTrust& trust, + ByteTransport& transport) { + if (trust.server_name.empty()) + return std::unexpected(TlsError::configuration); + auto impl = std::make_unique(transport); + if (!impl->common(MBEDTLS_SSL_IS_CLIENT)) + return std::unexpected(TlsError::configuration); + if (mbedtls_x509_crt_parse(&impl->authority, bytes_of(trust.authority_pem), + trust.authority_pem.size() + 1) != 0) + return std::unexpected(TlsError::credentials); + mbedtls_ssl_conf_ca_chain(&impl->config, &impl->authority, nullptr); + mbedtls_ssl_conf_authmode(&impl->config, MBEDTLS_SSL_VERIFY_REQUIRED); + if (!impl->attach() || mbedtls_ssl_set_hostname(&impl->ssl, trust.server_name.c_str()) != 0) + return std::unexpected(TlsError::configuration); + return std::unique_ptr(new TlsChannel(std::move(impl))); +} + +std::expected TlsChannel::handshake() { + if (const auto latched = impl_->error) + return std::unexpected(*latched); + const int result = mbedtls_ssl_handshake(&impl_->ssl); + if (result == 0) + return TlsProgress::done; + if (result == MBEDTLS_ERR_SSL_WANT_READ || result == MBEDTLS_ERR_SSL_WANT_WRITE) + return TlsProgress::want_io; + return impl_->fail_from(result, TlsError::handshake); +} + +std::expected TlsChannel::send(std::span bytes) { + if (const auto latched = impl_->error) + return std::unexpected(*latched); + const int result = mbedtls_ssl_write(&impl_->ssl, bytes.data(), bytes.size()); + if (result >= 0) + return static_cast(result); + if (result == MBEDTLS_ERR_SSL_WANT_READ || result == MBEDTLS_ERR_SSL_WANT_WRITE) + return 0; + return impl_->fail_from(result, TlsError::record); +} + +std::expected TlsChannel::receive(std::span bytes) { + if (const auto latched = impl_->error) + return std::unexpected(*latched); + const int result = mbedtls_ssl_read(&impl_->ssl, bytes.data(), bytes.size()); + if (result > 0) + return static_cast(result); + if (result == MBEDTLS_ERR_SSL_WANT_READ || result == MBEDTLS_ERR_SSL_WANT_WRITE) + return 0; + if (result == 0 || result == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) + return impl_->fail(TlsError::closed); + return impl_->fail_from(result, TlsError::record); +} + +std::expected TlsChannel::close() { + if (const auto latched = impl_->error) + return std::unexpected(*latched); + const int result = mbedtls_ssl_close_notify(&impl_->ssl); + if (result == 0) + return TlsProgress::done; + if (result == MBEDTLS_ERR_SSL_WANT_READ || result == MBEDTLS_ERR_SSL_WANT_WRITE) + return TlsProgress::want_io; + return impl_->fail_from(result, TlsError::record); +} + +std::string_view TlsChannel::version() const noexcept { + const char* name = mbedtls_ssl_get_version(&impl_->ssl); + return name == nullptr ? std::string_view{} : std::string_view{name}; +} + +std::string_view TlsChannel::suite() const noexcept { + const char* name = mbedtls_ssl_get_ciphersuite(&impl_->ssl); + return name == nullptr ? std::string_view{} : std::string_view{name}; +} +} // namespace evo::network diff --git a/src/network/tls/dev_grant.cpp b/src/network/tls/dev_grant.cpp new file mode 100644 index 0000000..df203e0 --- /dev/null +++ b/src/network/tls/dev_grant.cpp @@ -0,0 +1,129 @@ +#include "evo/network/dev_grant.hpp" +#include +#include +#include + +namespace evo::network { +namespace { +static_assert(std::is_same_v, + "key ids are stored as plain PSA identifiers"); +constexpr std::array marker{'E', 'V', 'O', '-', 'D', 'E', 'V', '-', + 'O', 'N', 'L', 'Y', 0, 0, 0, 0}; +constexpr std::uint16_t grant_version = 1; +constexpr std::uint8_t dev_environment_byte = 1; +constexpr std::size_t claims_end = 76; // Tag covers bytes [0, 76). +constexpr psa_algorithm_t algorithm = PSA_ALG_HMAC(PSA_ALG_SHA_256); +void put(DevGrant& out, std::size_t at, std::uint64_t v, std::size_t count) { + for (std::size_t i = 0; i < count; ++i) + out.at(at + i) = 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; +} +bool structurally_valid(const DevGrantClaims& c) { + return c.grant_id != 0 && c.subject != 0 && c.world.world != 0 && c.world.frame != 0 && + c.region != 0 && c.expires_at > c.issued_at; +} +} // namespace + +std::expected DevGrantKey::import(DeploymentEnvironment environment, + std::span secret) { + // The dev identity must not exist anywhere but the dev environment. + if (environment != DeploymentEnvironment::dev) + return std::unexpected(GrantError::not_dev_environment); + if (secret.size() != dev_key_bytes || + std::ranges::all_of(secret, [](std::uint8_t b) { return b == 0; })) + return std::unexpected(GrantError::invalid_key); + if (psa_crypto_init() != PSA_SUCCESS) + return std::unexpected(GrantError::crypto); + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; + psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); + psa_set_key_bits(&attributes, dev_key_bytes * 8); + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE); + psa_set_key_algorithm(&attributes, algorithm); + mbedtls_svc_key_id_t key = PSA_KEY_ID_NULL; + const auto status = psa_import_key(&attributes, secret.data(), secret.size(), &key); + psa_reset_key_attributes(&attributes); + if (status != PSA_SUCCESS) + return std::unexpected(GrantError::crypto); + return DevGrantKey{key}; +} + +DevGrantKey::DevGrantKey(DevGrantKey&& other) noexcept : key_{other.key_} { + other.key_ = PSA_KEY_ID_NULL; +} + +DevGrantKey& DevGrantKey::operator=(DevGrantKey&& other) noexcept { + if (this != &other) { + if (key_ != PSA_KEY_ID_NULL) + (void)psa_destroy_key(key_); + key_ = other.key_; + other.key_ = PSA_KEY_ID_NULL; + } + return *this; +} + +DevGrantKey::~DevGrantKey() { + if (key_ != PSA_KEY_ID_NULL) + (void)psa_destroy_key(key_); +} + +std::expected DevGrantKey::issue(const DevGrantClaims& claims) const { + if (key_ == PSA_KEY_ID_NULL) + return std::unexpected(GrantError::invalid_key); + if (!structurally_valid(claims)) + return std::unexpected(GrantError::invalid_claims); + DevGrant out{}; + std::ranges::copy(marker, out.begin()); + put(out, 16, grant_version, 2); + out.at(18) = dev_environment_byte; + out.at(19) = 0; + put(out, 20, claims.grant_id, 8); + put(out, 28, claims.subject, 8); + put(out, 36, claims.world.world, 8); + put(out, 44, claims.world.frame, 8); + put(out, 52, claims.region, 8); + put(out, 60, claims.issued_at, 8); + put(out, 68, claims.expires_at, 8); + std::size_t length = 0; + if (psa_mac_compute(key_, algorithm, out.data(), claims_end, out.data() + claims_end, + dev_grant_bytes - claims_end, &length) != PSA_SUCCESS || + length != dev_grant_bytes - claims_end) + return std::unexpected(GrantError::crypto); + return out; +} + +std::expected +DevGrantKey::verify(std::span grant) const { + if (key_ == PSA_KEY_ID_NULL) + return std::unexpected(GrantError::invalid_key); + if (grant.size() != dev_grant_bytes) + return std::unexpected(GrantError::malformed); + // Authenticate first: no claim byte influences behaviour until the tag matches. + const auto status = psa_mac_verify(key_, algorithm, grant.data(), claims_end, + grant.data() + claims_end, dev_grant_bytes - claims_end); + if (status == PSA_ERROR_INVALID_SIGNATURE) + return std::unexpected(GrantError::bad_tag); + if (status != PSA_SUCCESS) + return std::unexpected(GrantError::crypto); + if (!std::equal(marker.begin(), marker.end(), grant.begin())) + return std::unexpected(GrantError::wrong_marker); + if (get(grant, 16, 2) != grant_version || grant[19] != 0) + return std::unexpected(GrantError::unsupported_version); + if (grant[18] != dev_environment_byte) + return std::unexpected(GrantError::wrong_environment); + DevGrantClaims claims; + claims.grant_id = get(grant, 20, 8); + claims.subject = get(grant, 28, 8); + claims.world = {get(grant, 36, 8), get(grant, 44, 8)}; + claims.region = get(grant, 52, 8); + claims.issued_at = get(grant, 60, 8); + claims.expires_at = get(grant, 68, 8); + if (!structurally_valid(claims)) + return std::unexpected(GrantError::invalid_claims); + return VerifiedGrant{claims}; +} +} // namespace evo::network diff --git a/tests/fuzz/dev_session_fuzz.cpp b/tests/fuzz/dev_session_fuzz.cpp new file mode 100644 index 0000000..0bd7eb8 --- /dev/null +++ b/tests/fuzz/dev_session_fuzz.cpp @@ -0,0 +1,61 @@ +#include "evo/network/protocol.hpp" +#include +#include +#include +#include +#include +#include + +// Any input: decoding never crashes, and an accepted message re-encodes to the same +// bytes (no field is trusted or silently dropped). Also streams through FrameReader. +extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) { + using namespace evo::network; + const std::span bytes{data, size}; + FrameReader reader; + for (std::size_t at = 0; at < size;) { + const auto taken = reader.feed(bytes.subspan(at, std::min(7, size - at))); + if (!taken) + break; + at += *taken; + if (*taken == 0 && !reader.take()) + std::abort(); + (void)reader.take(); + } + const auto frame = decode_frame(bytes); + if (!frame) + return 0; + std::vector again; + switch (frame->type) { + case MessageType::hello: + if (const auto m = decode_hello(*frame)) + again = *encode(*m); + break; + case MessageType::welcome: + if (const auto m = decode_welcome(*frame)) + again = encode(*m); + break; + case MessageType::intent: + if (const auto m = decode_intent(*frame)) + again = encode(*m); + break; + case MessageType::intent_result: + if (const auto m = decode_intent_result(*frame)) + again = encode(*m); + break; + case MessageType::close: + if (const auto m = decode_close(*frame)) + again = encode(*m); + break; + case MessageType::environment_view: + if (const auto m = decode_environment_view(*frame)) + again = encode(m->first, m->second); + break; + case MessageType::organism_view: + if (const auto m = decode_organism_view(*frame)) + again = encode(m->first, m->second); + break; + } + if (!again.empty() && !std::equal(again.begin(), again.end(), bytes.begin(), bytes.end())) + std::abort(); + return 0; +} diff --git a/tests/network_main.cpp b/tests/network_main.cpp new file mode 100644 index 0000000..683b2c2 --- /dev/null +++ b/tests/network_main.cpp @@ -0,0 +1,13 @@ +#include "network_test.hpp" +#include +#include + +int main() try { + evo::network_test::protocol_tests(); + evo::network_test::tls_tests(); + std::puts("network tests passed"); + return 0; +} catch (const std::exception& e) { + std::fprintf(stderr, "%s\n", e.what()); + return 1; +} diff --git a/tests/network_protocol.cpp b/tests/network_protocol.cpp new file mode 100644 index 0000000..01cec59 --- /dev/null +++ b/tests/network_protocol.cpp @@ -0,0 +1,457 @@ +#include "evo/network/admission.hpp" +#include "evo/network/dev_grant.hpp" +#include "evo/network/protocol.hpp" +#include "network_test.hpp" +#include +#include +#include +#include +#include + +namespace evo::network_test { +namespace { +using namespace evo::network; +constexpr world::WorldIdentity identity{11, 12}; +constexpr std::uint32_t env_mask = (1U << 0) | (1U << 1) | (1U << 7); +using Bytes = std::vector; + +client::EnvironmentPresentation environment(std::size_t count = 3) { + std::vector cells; + for (std::size_t i = 0; i < count; ++i) { + client::EnvironmentCell c; + c.voxel = {static_cast(i) - 1, 2, -7}; + c.chunk_revision = 40 + i; + c.habitat = {{100}, {200}, {0}, {1000}, {500}, {10}}; + c.volume = {1000ULL * 500 * 10}; + c.substrate = {1}; + c.terrain_material = {2}; + c.terrain_fill = static_cast(1000 * i); + c.presence[0] = client::FieldPresence::present; + c.quantity[0] = 30 + i; + c.presence[1] = client::FieldPresence::missing; + c.presence[7] = client::FieldPresence::present; + c.quantity[7] = i == 1 ? 0 : 0xFFFF'FFFF'FFFF'0000ULL; + c.moisture_status = world::MoistureStatus::available; + c.moisture = {3, 50}; + cells.push_back(c); + } + auto view = client::EnvironmentPresentation::create({identity, 9, 4}, env_mask, -7, cells); + CHECK(view); + return std::move(*view); +} +client::OrganismPresentation organisms(std::size_t count = 2) { + using biology::Capability; + const auto genome = biology::Genome::create({7, + 1, + 1, + {}, + {{Capability::energy_conversion, 400, 0}, + {Capability::membrane, 10, 0}, + {Capability::movement, 50, 0}, + {Capability::target_size, 8'000'000, 0}}}); + CHECK(genome); + const biology::EnvironmentState temperate{ + 293'150, {{1'000'000'000}, {accounting::Femtograms{1'000'000'000}, {}, {}, {}}}}; + std::vector cells; + for (std::size_t i = 0; i < count; ++i) { + auto cell = client::present_organism( + i + 1, {-3, 4, -7}, {static_cast(100 * i)}, {999'999}, *genome, + {static_cast(500 + i % 500), 0}, temperate, + i % 3 == 2 ? client::OrganismState::dead : client::OrganismState::alive); + CHECK(cell); + cells.push_back(*cell); + } + auto view = client::OrganismPresentation::create({identity, 9, 5}, -7, cells); + CHECK(view); + return std::move(*view); +} +constexpr ViewHeader view_header{3, 4, 9, 8, 77}; +bool same(const ViewHeader& a, const ViewHeader& b) { + return a.session == b.session && a.epoch == b.epoch && a.server_tick == b.server_tick && + a.commit_watermark == b.commit_watermark && a.baseline == b.baseline; +} +FrameView frame(const Bytes& bytes) { + auto f = decode_frame(bytes); + CHECK(f); + return *f; +} +Bytes with_header(MessageType type, std::uint16_t version, std::uint8_t flags, std::uint32_t length, + std::size_t payload) { + Bytes b{0x45, 0x56, 0x44, 0x53}; + b.push_back(static_cast(version)); + b.push_back(static_cast(version >> 8)); + b.push_back(static_cast(type)); + b.push_back(flags); + for (int i = 0; i < 4; ++i) + b.push_back(static_cast(length >> (8 * i))); + b.resize(b.size() + payload); + return b; +} + +void round_trips() { + const Hello hello{0, Bytes(dev_grant_bytes, 0xA5)}; + const auto hello_bytes = encode(hello); + CHECK(hello_bytes); + const auto h = decode_hello(frame(*hello_bytes)); + CHECK(h && h->grant == hello.grant && h->required_features == 0); + + const Welcome welcome{1, 11, 12, 13, 14, 15, 16, 17, 8, 8, 16}; + const auto w = decode_welcome(frame(encode(welcome))); + CHECK(w && w->session == 1 && w->world == 11 && w->frame == 12 && w->region == 13 && + w->epoch == 14 && w->server_tick == 15 && w->commit_watermark == 16 && + w->grant_expires_at == 17 && w->intent_window == 8 && w->window_ticks == 8 && + w->max_intents_per_window == 16); + + // Explicit little-endian golden: no native layout, padding or byte order leaks. + const IntentMessage intent{1, 2, 3, 4, -1, 1000}; + const auto intent_bytes = encode(intent); + const Bytes golden{ + 0x45, 0x56, 0x44, 0x53, 1, 0, 3, 0, 36, 0, 0, 0, // header + 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, // session, epoch + 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, // sequence, tick + 0xFF, 0xFF, 0xE8, 0x03}; // -1, 1000 + CHECK(intent_bytes == golden); + const auto i = decode_intent(frame(intent_bytes)); + CHECK(i && i->session == 1 && i->epoch == 2 && i->sequence == 3 && i->tick == 4 && + i->move_x == -1 && i->move_y == 1000); + const auto from_client = make_intent(5, 6, client::Intention{7, 8, -707, 707}); + CHECK(from_client.session == 5 && from_client.epoch == 6 && from_client.sequence == 7 && + from_client.tick == 8 && from_client.move_x == -707 && from_client.move_y == 707); + + const auto r = decode_intent_result(frame(encode(IntentResult{9, IntentStatus::replayed, 10}))); + CHECK(r && r->sequence == 9 && r->status == IntentStatus::replayed && r->server_tick == 10); + const auto c = decode_close(frame(encode(CloseReason::stale_epoch))); + CHECK(c && *c == CloseReason::stale_epoch); + + const auto env = environment(); + const auto env_bytes = encode(view_header, env); + const auto e = decode_environment_view(frame(env_bytes)); + CHECK(e && same(e->first, view_header) && e->second.revision() == env.revision() && + e->second.requested_mask() == env_mask && e->second.slice_z() == -7); + CHECK(e->second.cells().size() == 3 && + e->second.cells()[2].quantity[7] == 0xFFFF'FFFF'FFFF'0000ULL); + CHECK(encode(e->first, e->second) == env_bytes); // Canonical. + const auto orgs = organisms(); + const auto org_bytes = encode(view_header, orgs); + const auto o = decode_organism_view(frame(org_bytes)); + CHECK(o && o->second.revision() == orgs.revision() && o->second.cells().size() == 2 && + o->second.cells()[1].phenotype == orgs.cells()[1].phenotype && + o->second.cells()[1].y.value == 999'999); + CHECK(encode(o->first, o->second) == org_bytes); + // Full-size views fit the frame bound. + CHECK(encode(view_header, environment(client::max_environment_cells)).size() <= + frame_header_bytes + max_frame_payload); + CHECK(encode(view_header, organisms(client::max_organism_cells)).size() <= + frame_header_bytes + max_frame_payload); +} + +void frame_rejection() { + const auto intent = encode(IntentMessage{1, 1, 1, 1, 0, 0}); + CHECK(refused(decode_frame(Bytes(11, 0)), ProtocolError::truncated)); + auto bad = intent; + bad[0] ^= 1; + CHECK(refused(decode_frame(bad), ProtocolError::bad_magic)); + CHECK(refused(decode_frame(with_header(MessageType::intent, 2, 0, 36, 36)), + ProtocolError::unsupported_version)); + CHECK(refused(decode_frame(with_header(MessageType::intent, 0, 0, 36, 36)), + ProtocolError::unsupported_version)); + // Out-of-range enumerators model untrusted wire bytes; rejecting them is the point. + // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) + CHECK(refused(decode_frame(with_header(MessageType{0}, 1, 0, 0, 0)), + ProtocolError::unknown_type)); + // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) + CHECK(refused(decode_frame(with_header(MessageType{8}, 1, 0, 0, 0)), + ProtocolError::unknown_type)); + CHECK(refused(decode_frame(with_header(MessageType::intent, 1, 1, 36, 36)), + ProtocolError::reserved_flags)); + CHECK(refused( + decode_frame(with_header(MessageType::environment_view, 1, 0, max_frame_payload + 1, 0)), + ProtocolError::oversized)); + auto shorter = intent; + shorter.pop_back(); + CHECK(refused(decode_frame(shorter), ProtocolError::truncated)); + auto longer = intent; + longer.push_back(0); + CHECK(refused(decode_frame(longer), ProtocolError::trailing_bytes)); + // Payload-level checks. + CHECK(refused(decode_welcome(frame(intent)), ProtocolError::wrong_type)); + CHECK(refused(decode_intent(frame(with_header(MessageType::intent, 1, 0, 35, 35))), + ProtocolError::truncated)); + CHECK(refused(decode_intent(frame(with_header(MessageType::intent, 1, 0, 37, 37))), + ProtocolError::trailing_bytes)); + auto feature = *encode(Hello{0, Bytes(4, 1)}); + feature[frame_header_bytes] = 1; + CHECK(refused(decode_hello(frame(feature)), ProtocolError::unsupported_feature)); + CHECK(refused(encode(Hello{0, {}}), ProtocolError::invalid_value)); + CHECK(refused(encode(Hello{0, Bytes(max_grant_bytes + 1, 1)}), ProtocolError::invalid_value)); + auto empty_grant = with_header(MessageType::hello, 1, 0, 6, 6); + CHECK(refused(decode_hello(frame(empty_grant)), ProtocolError::invalid_value)); + auto status = encode(IntentResult{1, IntentStatus::admitted, 1}); + status[frame_header_bytes + 8] = 8; + CHECK(refused(decode_intent_result(frame(status)), ProtocolError::invalid_value)); + auto reason = encode(CloseReason::normal); + reason[frame_header_bytes] = 9; + CHECK(refused(decode_close(frame(reason)), ProtocolError::invalid_value)); + CHECK(refused(decode_welcome(frame(encode(Welcome{}))), ProtocolError::invalid_value)); + // A well-formed encoding of an invalid view is rejected by the presentation rules. + auto env = encode(view_header, environment(1)); + const std::size_t habitat_x = frame_header_bytes + 40 + 32 + 4 + 8 + 2 + 16 + 8; + env[habitat_x + 2] = 0x10; // x = 100 + 0x100000 um: outside the voxel. + CHECK(refused(decode_environment_view(frame(env)), ProtocolError::invalid_value)); + auto count = encode(view_header, environment(1)); + count[frame_header_bytes + 40 + 32 + 4 + 8] = 1; // 257 cells. + count[frame_header_bytes + 40 + 32 + 4 + 8 + 1] = 1; + CHECK(refused(decode_environment_view(frame(count)), ProtocolError::invalid_value)); + auto state = encode(view_header, organisms(1)); + state.back() = 2; + CHECK(refused(decode_organism_view(frame(state)), ProtocolError::invalid_value)); +} + +void reader() { + Bytes stream = encode(IntentMessage{1, 1, 1, 1, 0, 0}); + const auto second = encode(view_header, environment()); + stream.insert(stream.end(), second.begin(), second.end()); + FrameReader r; + std::vector frames; + for (const auto byte : stream) { + const auto fed = r.feed({&byte, 1}); + CHECK(fed && *fed == 1); // Completed frames are taken immediately below. + if (auto f = r.take()) + frames.push_back(std::move(*f)); + } + CHECK(frames.size() == 2 && frames[0].type == MessageType::intent && + frames[1].type == MessageType::environment_view); + CHECK(decode_environment_view({frames[1].type, frames[1].payload})); + // Whole-stream feed stops at the frame boundary. + FrameReader bulk; + const auto first = bulk.feed(stream); + CHECK(first && *first == frame_header_bytes + 36 && bulk.take() && !bulk.take()); + // An oversized header is rejected after 12 bytes, before any payload is buffered. + FrameReader guard; + const auto huge = with_header(MessageType::environment_view, 1, 0, max_frame_payload + 1, 64); + CHECK(refused(guard.feed(huge), ProtocolError::oversized)); + CHECK(refused(guard.feed(stream), ProtocolError::oversized) && !guard.take()); // Latched. +} + +// Mutation fuzzing: decoding never crashes, and anything accepted re-encodes to the +// identical bytes (canonical, no hidden fields trusted from the wire). +void fuzz() { + std::mt19937_64 random{0x9E3779B97F4A7C15ULL}; + const std::vector seeds{*encode(Hello{0, Bytes(dev_grant_bytes, 7)}), + encode(Welcome{1, 11, 12, 13, 14, 15, 16, 17, 8, 8, 16}), + encode(IntentMessage{1, 2, 3, 4, 5, 6}), + encode(IntentResult{1, IntentStatus::outside_window, 2}), + encode(CloseReason::rate_limited), + encode(view_header, environment()), + encode(view_header, organisms(3))}; + std::size_t accepted = 0; + for (int round = 0; round < 30000; ++round) { + auto bytes = seeds[random() % seeds.size()]; + switch (random() % 4) { + case 0: + for (int flips = 1 + static_cast(random() % 4); flips > 0; --flips) + bytes[random() % bytes.size()] ^= static_cast(1U << (random() % 8)); + break; + case 1: + bytes.resize(random() % bytes.size()); + break; + case 2: // Mutate the payload only, keeping a valid header. + if (bytes.size() > frame_header_bytes) + bytes[frame_header_bytes + random() % (bytes.size() - frame_header_bytes)] = + static_cast(random()); + break; + default: + bytes.resize(random() % 96); + for (auto& b : bytes) + b = static_cast(random()); + } + const auto f = decode_frame(bytes); + if (!f) + continue; + Bytes again; + switch (f->type) { + case MessageType::hello: + if (const auto m = decode_hello(*f)) + again = *encode(*m); + break; + case MessageType::welcome: + if (const auto m = decode_welcome(*f)) + again = encode(*m); + break; + case MessageType::intent: + if (const auto m = decode_intent(*f)) + again = encode(*m); + break; + case MessageType::intent_result: + if (const auto m = decode_intent_result(*f)) + again = encode(*m); + break; + case MessageType::close: + if (const auto m = decode_close(*f)) + again = encode(*m); + break; + case MessageType::environment_view: + if (const auto m = decode_environment_view(*f)) + again = encode(m->first, m->second); + break; + case MessageType::organism_view: + if (const auto m = decode_organism_view(*f)) + again = encode(m->first, m->second); + break; + } + if (!again.empty()) { + ++accepted; + CHECK(again == bytes); + } + FrameReader r; + if (const auto fed = r.feed(bytes)) + CHECK(*fed <= bytes.size()); + } + CHECK(accepted > 1000); // The mutations exercised acceptance paths as well. + std::printf("network_fuzz rounds=30000 accepted=%zu\n", accepted); +} + +DevGrantKey key(std::uint8_t fill = 0x5A) { + std::array secret{}; + secret.fill(fill); + secret[0] = 1; + auto k = DevGrantKey::import(DeploymentEnvironment::dev, secret); + CHECK(k); + return std::move(*k); +} +DevGrantClaims claims(std::uint64_t grant, std::uint64_t subject, std::uint64_t issued = 1000, + std::uint64_t expires = 1600) { + return {grant, subject, identity, 21, issued, expires}; +} + +void dev_grants() { + std::array secret{}; + secret.fill(3); + CHECK(refused(DevGrantKey::import(DeploymentEnvironment::stage, secret), + GrantError::not_dev_environment)); + CHECK(refused(DevGrantKey::import(DeploymentEnvironment::world0, secret), + GrantError::not_dev_environment)); + CHECK(refused(DevGrantKey::import(DeploymentEnvironment::dev, std::span{secret}.first(31)), + GrantError::invalid_key)); + secret.fill(0); + CHECK( + refused(DevGrantKey::import(DeploymentEnvironment::dev, secret), GrantError::invalid_key)); + const auto k = key(); + CHECK(refused(k.issue(claims(0, 1)), GrantError::invalid_claims)); + CHECK(refused(k.issue(claims(1, 1, 5, 5)), GrantError::invalid_claims)); + const auto grant = k.issue(claims(1, 2)); + CHECK(grant); + const Bytes marker(grant->begin(), grant->begin() + 12); + CHECK(marker == Bytes({'E', 'V', 'O', '-', 'D', 'E', 'V', '-', 'O', 'N', 'L', 'Y'})); + const auto verified = k.verify(*grant); + CHECK(verified && verified->claims().grant_id == 1 && verified->claims().subject == 2 && + verified->claims().world == identity && verified->claims().region == 21); + // Every single-bit change anywhere in the record fails authentication. + for (std::size_t i = 0; i < dev_grant_bytes; ++i) { + auto tampered = *grant; + tampered.at(i) ^= 0x01; + CHECK(refused(k.verify(tampered), GrantError::bad_tag)); + } + CHECK(refused(key(0x77).verify(*grant), GrantError::bad_tag)); + CHECK(refused(k.verify(std::span{*grant}.first(dev_grant_bytes - 1)), GrantError::malformed)); + auto moved = key(); + auto target = std::move(moved); + CHECK(target.verify(*target.issue(claims(5, 5)))); + // Moved-from keys refuse use; checking that is the point. + // NOLINTNEXTLINE(bugprone-use-after-move) + CHECK(refused(moved.issue(claims(5, 5)), GrantError::invalid_key)); +} + +SessionAdmission admission(std::size_t max_sessions = 4, std::size_t remembered = 8) { + AdmissionConfig config; + config.world = identity; + config.region = 21; + config.epoch = 3; + config.max_sessions = max_sessions; + config.max_remembered_grants = remembered; + config.intent_window = 4; + config.window_ticks = 10; + config.max_intents_per_window = 5; + config.max_grant_lifetime = 900; + config.clock_skew = 30; + auto a = SessionAdmission::create(config); + CHECK(a); + return std::move(*a); +} + +void admission_rules() { + auto bad = AdmissionConfig{}; + CHECK(refused(SessionAdmission::create(bad), AdmissionError::invalid_config)); + const auto k = key(); + const auto verify = [&](DevGrantClaims c) { + auto g = k.verify(*k.issue(c)); + CHECK(g); + return *g; + }; + auto a = admission(); + auto foreign = claims(1, 1); + foreign.world = {11, 99}; + CHECK(refused(a.open(verify(foreign), 1100, 50, 40), AdmissionError::wrong_world)); + foreign = claims(1, 1); + foreign.region = 22; + CHECK(refused(a.open(verify(foreign), 1100, 50, 40), AdmissionError::wrong_region)); + CHECK(refused(a.open(verify(claims(1, 1, 1000, 2000)), 1100, 50, 40), + AdmissionError::invalid_lifetime)); + CHECK(refused(a.open(verify(claims(1, 1)), 900, 50, 40), AdmissionError::not_yet_valid)); + CHECK(refused(a.open(verify(claims(1, 1)), 1600, 50, 40), AdmissionError::expired)); + const auto welcome = a.open(verify(claims(1, 1)), 975, 50, 40); // Within clock skew. + CHECK(welcome && welcome->session == 1 && welcome->epoch == 3 && welcome->region == 21 && + welcome->world == 11 && welcome->frame == 12 && welcome->server_tick == 50 && + welcome->commit_watermark == 40 && welcome->grant_expires_at == 1600); + // Grants are single use; a subject never holds two sessions. + CHECK(refused(a.open(verify(claims(1, 9)), 1100, 50, 40), AdmissionError::grant_reused)); + CHECK(refused(a.open(verify(claims(2, 1)), 1100, 50, 40), AdmissionError::duplicate_subject)); + CHECK(a.close(1) && refused(a.close(1), AdmissionError::unknown_session)); + CHECK(refused(a.open(verify(claims(1, 1)), 1100, 50, 40), AdmissionError::grant_reused)); + const auto again = a.open(verify(claims(2, 1)), 1100, 50, 40); // Reconnect: fresh grant. + CHECK(again && again->session == 2); + for (std::uint64_t s = 2; s <= 4; ++s) + CHECK(a.open(verify(claims(10 + s, 100 + s)), 1100, 50, 40)); + CHECK(refused(a.open(verify(claims(20, 200)), 1100, 50, 40), AdmissionError::sessions_full)); + auto small = admission(1, 1); + CHECK(small.open(verify(claims(1, 1)), 1100, 50, 40) && small.close(1)); + CHECK(refused(small.open(verify(claims(2, 2)), 1100, 50, 40), AdmissionError::grants_full)); + CHECK(small.open(verify(claims(3, 3, 1500, 2000)), 1600, 50, 40)); // Expired id forgotten. + + // Intent admission for session 2. + const auto status = [&](IntentMessage m, std::uint64_t tick = 100, std::uint64_t now = 1100) { + return a.admit(m, tick, now).status; + }; + CHECK(status({99, 3, 1, 100, 0, 0}) == IntentStatus::unknown_session); + CHECK(status({2, 2, 1, 100, 0, 0}) == IntentStatus::stale_epoch); + CHECK(status({2, 3, 1, 100, 0, 0}, 100, 1600) == IntentStatus::expired); + CHECK(status({2, 3, 1, 100, 1000, 1}) == IntentStatus::invalid_vector); + CHECK(status({2, 3, 1, 100, 0, -1001}) == IntentStatus::invalid_vector); + CHECK(status({2, 3, 0, 100, 0, 0}, 111) == IntentStatus::replayed); + CHECK(status({2, 3, 1, 110, 707, -707}, 111) == IntentStatus::outside_window); + CHECK(status({2, 3, 1, 116, 0, 0}, 111) == IntentStatus::outside_window); + const auto ok = a.admit({2, 3, 1, 115, 707, -707}, 111, 1100); + CHECK(ok.status == IntentStatus::admitted && ok.sequence == 1 && ok.server_tick == 111); + CHECK(status({2, 3, 1, 112, 0, 0}, 111) == IntentStatus::replayed); + // The window opened at tick 111 has had 5 attempts (rejections count too). + CHECK(status({2, 3, 2, 112, 0, 0}, 112) == IntentStatus::rate_limited); + CHECK(status({2, 3, 2, 120, 0, 0}, 120) == IntentStatus::rate_limited); + CHECK(status({2, 3, 2, 121, 0, 0}, 121) == IntentStatus::admitted); // New window. + CHECK(status({2, 3, 3, 122, 0, 0}, 122) == IntentStatus::admitted); + // A new ownership epoch makes every existing session stale. + CHECK(refused(a.advance_epoch(3), AdmissionError::invalid_config) && a.advance_epoch(4)); + CHECK(status({2, 4, 4, 122, 0, 0}, 122) == IntentStatus::stale_epoch); + CHECK(status({2, 3, 4, 122, 0, 0}, 122) == IntentStatus::stale_epoch); + CHECK(a.close(2) && a.open(verify(claims(30, 1)), 1100, 121, 40)->epoch == 4); +} +} // namespace + +void protocol_tests() { + round_trips(); + frame_rejection(); + reader(); + fuzz(); + dev_grants(); + admission_rules(); +} +} // namespace evo::network_test diff --git a/tests/network_test.hpp b/tests/network_test.hpp new file mode 100644 index 0000000..980c0e9 --- /dev/null +++ b/tests/network_test.hpp @@ -0,0 +1,19 @@ +#pragma once +#include +#include +#include +#include + +namespace evo::network_test { +inline void check(bool ok, std::source_location at = std::source_location::current()) { + if (!ok) + throw std::runtime_error(std::string{"network "} + at.file_name() + ":" + + std::to_string(at.line())); +} +template bool refused(const std::expected& r, E e) { + return !r && r.error() == e; +} +void protocol_tests(); +void tls_tests(); +} // namespace evo::network_test +#define CHECK(...) ::evo::network_test::check(static_cast(__VA_ARGS__)) diff --git a/tests/network_tls.cpp b/tests/network_tls.cpp new file mode 100644 index 0000000..a43874e --- /dev/null +++ b/tests/network_tls.cpp @@ -0,0 +1,253 @@ +#include "evo/network/protocol.hpp" +#include "evo/network/tls_channel.hpp" +#include "network_test.hpp" +#include +#include +#include +#include +#include +#include + +namespace evo::network_test { +namespace { +using namespace evo::network; +constexpr const char* server_name = "dev-region.evolution.test"; + +// Throwaway test credentials; production/dev deployment keys are never generated here. +struct Key { + Key() { + mbedtls_pk_init(&pk); + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; + psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); + psa_set_key_bits(&attributes, 256); + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT); + psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256)); + mbedtls_svc_key_id_t id = PSA_KEY_ID_NULL; + CHECK(psa_crypto_init() == PSA_SUCCESS && + psa_generate_key(&attributes, &id) == PSA_SUCCESS); + CHECK(mbedtls_pk_copy_from_psa(id, &pk) == 0); + (void)psa_destroy_key(id); + } + ~Key() { + mbedtls_pk_free(&pk); + } + Key(const Key&) = delete; + Key& operator=(const Key&) = delete; + [[nodiscard]] std::string pem() { + std::array buffer{}; + CHECK(mbedtls_pk_write_key_pem(&pk, buffer.data(), buffer.size()) == 0); + return reinterpret_cast(buffer.data()); + } + mbedtls_pk_context pk; +}; +std::string certificate(Key& subject, Key& issuer, const char* subject_name, + const char* issuer_name, bool authority, unsigned char serial) { + mbedtls_x509write_cert crt; + mbedtls_x509write_crt_init(&crt); + mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3); + mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256); + mbedtls_x509write_crt_set_subject_key(&crt, &subject.pk); + mbedtls_x509write_crt_set_issuer_key(&crt, &issuer.pk); + CHECK(mbedtls_x509write_crt_set_serial_raw(&crt, &serial, 1) == 0 && + mbedtls_x509write_crt_set_validity(&crt, "20250101000000", "20991231235959") == 0 && + mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0 && + mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0 && + mbedtls_x509write_crt_set_basic_constraints(&crt, authority ? 1 : 0, -1) == 0 && + mbedtls_x509write_crt_set_key_usage(&crt, authority + ? MBEDTLS_X509_KU_KEY_CERT_SIGN + : MBEDTLS_X509_KU_DIGITAL_SIGNATURE) == 0); + std::array buffer{}; + const int result = mbedtls_x509write_crt_pem(&crt, buffer.data(), buffer.size()); + mbedtls_x509write_crt_free(&crt); + CHECK(result == 0); + return reinterpret_cast(buffer.data()); +} +struct Pki { + Pki(const char* name = server_name) { + authority = + certificate(ca_key, ca_key, "CN=AEvolution dev CA", "CN=AEvolution dev CA", true, 1); + const std::string subject = std::string{"CN="} + name; + server.certificate_chain_pem = + certificate(server_key, ca_key, subject.c_str(), "CN=AEvolution dev CA", false, 2); + server.private_key_pem = server_key.pem(); + } + Key ca_key, server_key; + std::string authority; + ServerCredentials server; +}; + +// Two in-memory byte queues with optional faults. +struct Wire { + std::deque to_server, to_client; + bool fail{}; +}; +class Endpoint final : public ByteTransport { + public: + Endpoint(Wire& wire, bool is_server) : wire_{wire}, server_{is_server} {} + std::expected write(std::span bytes) override { + if (wire_.fail) + return std::unexpected(TransportError::failed); + auto& out = server_ ? wire_.to_client : wire_.to_server; + const auto count = std::min(bytes.size(), 700); // Force partial writes. + out.insert(out.end(), bytes.begin(), bytes.begin() + static_cast(count)); + return count; + } + std::expected read(std::span bytes) override { + if (wire_.fail) + return std::unexpected(TransportError::failed); + auto& in = server_ ? wire_.to_server : wire_.to_client; + const auto count = std::min(bytes.size(), in.size()); + for (std::size_t i = 0; i < count; ++i) { + bytes[i] = in.front(); + in.pop_front(); + } + return count; + } + + private: + Wire& wire_; + bool server_; +}; +struct Pair { + explicit Pair(const ServerCredentials& credentials, const ClientTrust& trust) + : server_end{wire, true}, client_end{wire, false} { + auto s = TlsChannel::server(credentials, server_end); + auto c = TlsChannel::client(trust, client_end); + CHECK(s && c); + server = std::move(*s); + client = std::move(*c); + } + // Pumps both handshakes; returns the first error, if any. + std::expected> handshake() { + bool server_done = false, client_done = false; + for (int i = 0; i < 200 && !(server_done && client_done); ++i) { + if (!client_done) { + const auto p = client->handshake(); + if (!p) + return std::unexpected(std::pair{false, p.error()}); + client_done = *p == TlsProgress::done; + } + if (!server_done) { + const auto p = server->handshake(); + if (!p) + return std::unexpected(std::pair{true, p.error()}); + server_done = *p == TlsProgress::done; + } + } + CHECK(server_done && client_done); + return {}; + } + std::vector transfer(TlsChannel& from, TlsChannel& to, + std::span bytes) { + std::size_t sent = 0; + std::vector received; + std::array buffer{}; + for (int i = 0; i < 1000 && received.size() < bytes.size(); ++i) { + if (sent < bytes.size()) { + const auto n = from.send(bytes.subspan(sent)); + CHECK(n); + sent += *n; + } + const auto n = to.receive(buffer); + CHECK(n); + received.insert(received.end(), buffer.begin(), + buffer.begin() + static_cast(*n)); + } + return received; + } + Wire wire; + Endpoint server_end, client_end; + std::unique_ptr server, client; +}; + +void handshake_and_frames() { + Pki pki; + Pair pair{pki.server, {pki.authority, server_name}}; + CHECK(pair.handshake()); + CHECK(pair.client->version() == "TLSv1.3" && pair.server->version() == "TLSv1.3"); + const auto suite = pair.client->suite(); + CHECK(suite == "TLS1-3-AES-128-GCM-SHA256" || suite == "TLS1-3-CHACHA20-POLY1305-SHA256"); + // A protocol frame crosses the channel and is reassembled by a FrameReader. + const auto hello = encode(Hello{0, std::vector(108, 0x42)}); + CHECK(hello); + const auto at_server = pair.transfer(*pair.client, *pair.server, *hello); + CHECK(at_server == *hello); + FrameReader reader; + CHECK(reader.feed(at_server)); + const auto frame = reader.take(); + CHECK(frame && frame->type == MessageType::hello); + const auto large = std::vector(100'000, 0x17); // Multiple TLS records. + CHECK(pair.transfer(*pair.server, *pair.client, large) == large); + // Orderly close: the peer observes close_notify as `closed`. + CHECK(pair.client->close()); + std::array buffer{}; + std::expected r = 0; + for (int i = 0; i < 10 && r && *r == 0; ++i) + r = pair.server->receive(buffer); + CHECK(refused(r, TlsError::closed)); + CHECK(refused(pair.server->send(buffer), TlsError::closed)); // Latched. +} + +void rejections() { + Pki pki; + Pki other; + // Untrusted authority and wrong host name fail peer verification on the client. + { + Pair pair{pki.server, {other.authority, server_name}}; + const auto r = pair.handshake(); + CHECK(!r && !r.error().first && r.error().second == TlsError::peer_verification); + } + { + Pair pair{pki.server, {pki.authority, "other.evolution.test"}}; + const auto r = pair.handshake(); + CHECK(!r && !r.error().first && r.error().second == TlsError::peer_verification); + } + // Configuration and credential errors. + Wire wire; + Endpoint end{wire, false}; + CHECK(refused(TlsChannel::client({pki.authority, ""}, end), TlsError::configuration)); + CHECK(refused(TlsChannel::client({"not a certificate", server_name}, end), + TlsError::credentials)); + CHECK(refused(TlsChannel::server({pki.server.certificate_chain_pem, "garbage"}, end), + TlsError::credentials)); + CHECK(refused( + TlsChannel::server({pki.server.certificate_chain_pem, other.server.private_key_pem}, end), + TlsError::credentials)); // Key does not match the certificate. + // A modified application record is rejected (AEAD integrity). + { + Pair pair{pki.server, {pki.authority, server_name}}; + CHECK(pair.handshake()); + const std::array data{}; + const auto sent = pair.client->send(data); + CHECK(sent && *sent == data.size()); + CHECK(pair.wire.to_server.size() > 20); + pair.wire.to_server[pair.wire.to_server.size() - 5] ^= 0x01; + std::array buffer{}; + auto r = pair.server->receive(buffer); + while (r && *r > 0) + r = pair.server->receive(buffer); + CHECK(refused(r, TlsError::record)); + } + // Transport failure surfaces as `transport` and latches. + { + Pair pair{pki.server, {pki.authority, server_name}}; + pair.wire.fail = true; + CHECK(refused(pair.client->handshake(), TlsError::transport)); + CHECK(refused(pair.client->handshake(), TlsError::transport)); + } + // Plaintext protocol bytes are not TLS: the server rejects them. + { + Pair pair{pki.server, {pki.authority, server_name}}; + const auto plain = encode(IntentMessage{1, 1, 1, 1, 0, 0}); + pair.wire.to_server.insert(pair.wire.to_server.end(), plain.begin(), plain.end()); + const auto r = pair.server->handshake(); + CHECK(!r && r.error() == TlsError::handshake); + } +} +} // namespace + +void tls_tests() { + handshake_and_frames(); + rejections(); +} +} // namespace evo::network_test diff --git a/third_party/mbedtls/LICENSE b/third_party/mbedtls/LICENSE new file mode 100644 index 0000000..776ac77 --- /dev/null +++ b/third_party/mbedtls/LICENSE @@ -0,0 +1,553 @@ +Mbed TLS files are provided under a dual [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) +OR [GPL-2.0-or-later](https://spdx.org/licenses/GPL-2.0-or-later.html) license. +This means that users may choose which of these licenses they take the code +under. + +The full text of each of these licenses is given below. + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +=============================================================================== + + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/third_party/mbedtls/README.md b/third_party/mbedtls/README.md new file mode 100644 index 0000000..8095839 --- /dev/null +++ b/third_party/mbedtls/README.md @@ -0,0 +1,8 @@ +# Mbed TLS (explicitly prepared, not vendored) + +Version 4.1.1 LTS (supported until March 2029), pinned by SHA-256 in +`tools/mbedtls-pin.cmake` and prepared with `cmake -P tools/prepare-mbedtls.cmake`. +Used under the Apache-2.0 option of its Apache-2.0 OR GPL-2.0-or-later dual license +(copied here as LICENSE). `evo_mbedtls_config.h` is the owned build restriction. +Decision and scope: docs/adr/0082-dev-session-transport.md. Opt-in via +`EVO_BUILD_NETWORK`; headless and server builds never evaluate it otherwise. diff --git a/third_party/mbedtls/evo_mbedtls_config.h b/third_party/mbedtls/evo_mbedtls_config.h new file mode 100644 index 0000000..674cb33 --- /dev/null +++ b/third_party/mbedtls/evo_mbedtls_config.h @@ -0,0 +1,14 @@ +/* Owned restriction of the Mbed TLS 4.1.1 default configuration (ADR-0082). + * Appended through MBEDTLS_USER_CONFIG_FILE. The M9 dev session uses TLS 1.3 over a + * reliable stream with certificate-authenticated ephemeral (EC)DHE only. Everything + * removed here is unreachable code for that profile and recent advisory surface. */ +#undef MBEDTLS_SSL_PROTO_TLS1_2 +#undef MBEDTLS_SSL_PROTO_DTLS +#undef MBEDTLS_SSL_DTLS_ANTI_REPLAY +#undef MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE +#undef MBEDTLS_SSL_DTLS_CONNECTION_ID +#undef MBEDTLS_SSL_DTLS_HELLO_VERIFY +#undef MBEDTLS_SSL_RENEGOTIATION +#undef MBEDTLS_SSL_SESSION_TICKETS +#undef MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED +#undef MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED diff --git a/tools/ci/ci-paths-check.sh b/tools/ci/ci-paths-check.sh index c767300..4b49efd 100644 --- a/tools/ci/ci-paths-check.sh +++ b/tools/ci/ci-paths-check.sh @@ -47,4 +47,9 @@ mkdir -p tools client_base="$(git rev-parse HEAD)" echo pin > tools/sdl3-pin.cmake; git add .; git commit -qm client-pin expect "push: SDL pin runs client" 0 CI_EVENT=push "CI_BEFORE=$client_base" -- client +# A TLS dependency update must select the isolated network gate, not only the image. +network_base="$(git rev-parse HEAD)" +echo pin > tools/mbedtls-pin.cmake; git add .; git commit -qm network-pin +expect "push: Mbed TLS pin runs network" 0 CI_EVENT=push "CI_BEFORE=$network_base" -- network +expect "push: Mbed TLS pin skips deploy" 1 CI_EVENT=push "CI_BEFORE=$network_base" -- deploy [ "$failures" = 0 ] && echo "ci path selection fixtures passed" || { echo "$failures fixture(s) failed"; exit 1; } diff --git a/tools/ci/ci-paths.sh b/tools/ci/ci-paths.sh index aa084af..1ef4165 100755 --- a/tools/ci/ci-paths.sh +++ b/tools/ci/ci-paths.sh @@ -36,6 +36,9 @@ case "$suite" in client) pattern="$common|$native|experiments/client/|tools/prepare-sdl3\\.cmake|tools/sdl3-pin\\.cmake|deploy/docker/Dockerfile" ;; + network) + pattern="$common|$native|tests/network_|tests/fuzz/dev_session|third_party/mbedtls/|tools/prepare-mbedtls\.cmake|tools/mbedtls-pin\.cmake|deploy/docker/Dockerfile" + ;; checks|tsan|ecs) pattern="$common|$native|tests/|experiments/|tools/ci/linux-check\.sh|tools/ci/tsan-profile\.mjs|deploy/docker/Dockerfile" ;; diff --git a/tools/mbedtls-pin.cmake b/tools/mbedtls-pin.cmake new file mode 100644 index 0000000..839aa53 --- /dev/null +++ b/tools/mbedtls-pin.cmake @@ -0,0 +1,3 @@ +set(EVO_MBEDTLS_VERSION "4.1.1") +set(EVO_MBEDTLS_SHA256 "3359a349e23db3d5536fcee032ae7b2ecbfc08972fab643089b5cbf2a375c98c") +set(EVO_MBEDTLS_URL "https://github.com/Mbed-TLS/mbedtls/releases/download/mbedtls-${EVO_MBEDTLS_VERSION}/mbedtls-${EVO_MBEDTLS_VERSION}.tar.bz2") diff --git a/tools/prepare-mbedtls.cmake b/tools/prepare-mbedtls.cmake new file mode 100644 index 0000000..c7e7809 --- /dev/null +++ b/tools/prepare-mbedtls.cmake @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.28) +include("${CMAKE_CURRENT_LIST_DIR}/mbedtls-pin.cmake") +set(root "${CMAKE_CURRENT_LIST_DIR}/../build/external/mbedtls") +file(MAKE_DIRECTORY "${root}") +if(NOT DEFINED MBEDTLS_ARCHIVE) + set(MBEDTLS_ARCHIVE "${root}/mbedtls-${EVO_MBEDTLS_VERSION}.tar.bz2") + if(NOT EXISTS "${MBEDTLS_ARCHIVE}") + file(DOWNLOAD "${EVO_MBEDTLS_URL}" "${MBEDTLS_ARCHIVE}" TLS_VERIFY ON + EXPECTED_HASH "SHA256=${EVO_MBEDTLS_SHA256}" STATUS result) + list(GET result 0 code) + if(NOT code EQUAL 0) + message(FATAL_ERROR "Mbed TLS download failed: ${result}") + endif() + endif() +endif() +file(SHA256 "${MBEDTLS_ARCHIVE}" actual) +if(NOT actual STREQUAL EVO_MBEDTLS_SHA256) + message(FATAL_ERROR "Mbed TLS archive checksum mismatch; refusing extraction") +endif() +file(REMOVE_RECURSE "${root}/mbedtls-${EVO_MBEDTLS_VERSION}") +file(ARCHIVE_EXTRACT INPUT "${MBEDTLS_ARCHIVE}" DESTINATION "${root}") +file(WRITE "${root}/mbedtls-${EVO_MBEDTLS_VERSION}/evo-verified.sha256" "${actual}\n") +message(STATUS "Prepared Mbed TLS ${EVO_MBEDTLS_VERSION}; license: ${root}/mbedtls-${EVO_MBEDTLS_VERSION}/LICENSE (Apache-2.0 option)") diff --git a/tools/windows-check.ps1 b/tools/windows-check.ps1 index 5ca075e..75fd0d9 100644 --- a/tools/windows-check.ps1 +++ b/tools/windows-check.ps1 @@ -1,5 +1,5 @@ param( - [ValidateSet('debug', 'release', 'analysis', 'asan', 'tsan', 'client', 'client-probe')] + [ValidateSet('debug', 'release', 'analysis', 'asan', 'tsan', 'client', 'client-probe', 'network')] [string]$Preset = 'debug', [ValidateSet('configure', 'build', 'test', 'format', 'check')] [string]$Action = 'check'