Files
aevolution/include/evo/network/tls_channel.hpp
T
dingoandClaude Opus 5.5 c588be237c Fix M9.4-M9.7 review findings in the dev session stack
- TLS: replace send() with a bounded queue/flush; an interrupted record is
  retried with identical arguments and later frames wait behind it, so no
  queued frame is lost or reordered; close() flushes first.
- Carriers: server-issued 'I','N' | 48-bit epoch | 64-bit ordinal ids
  replace the aliasing low-32-bit packing; client sequences are kept
  separately for replies; out-of-range epochs are refused, never truncated.
- Grants: a durable, fsynced, fail-closed consumed-grant ledger
  (--session-grant-ledger, /objects/dev-session-grants.v1 in dev) keeps
  single-use grants consumed across process and pod restarts.
- Liveness: protocol v1 (undeployed) gains Heartbeat and the timeout close
  reason; both sides heartbeat when idle and treat 5 s of silence as dead.
- Client sends are bounded and unresolved tracking starts at send time.
- ci-client configures with an explicit C compiler.

Each finding has a regression test; ADR-0083, the schema, operations doc
and VALIDATION record the corrections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 05:44:44 -04:00

76 lines
3.1 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <span>
#include <string>
#include <string_view>
// 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<std::size_t, TransportError>
write(std::span<const std::uint8_t> bytes) = 0;
[[nodiscard]] virtual std::expected<std::size_t, TransportError>
read(std::span<std::uint8_t> 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,
backpressure
};
enum class TlsProgress : std::uint8_t { done, want_io };
class TlsChannel {
public:
[[nodiscard]] static std::expected<std::unique_ptr<TlsChannel>, TlsError>
server(const ServerCredentials& credentials, ByteTransport& transport);
[[nodiscard]] static std::expected<std::unique_ptr<TlsChannel>, TlsError>
client(const ClientTrust& trust, ByteTransport& transport);
TlsChannel(const TlsChannel&) = delete;
TlsChannel& operator=(const TlsChannel&) = delete;
~TlsChannel();
[[nodiscard]] std::expected<TlsProgress, TlsError> handshake();
// Outbound plaintext is queued, never handed to the caller to retry: a record that
// TLS could not finish writing is retried with identical arguments (Mbed TLS
// requires this), and newly queued bytes wait behind it. Bounded; a full queue
// returns `backpressure` and queues nothing. Errors latch until destruction.
static constexpr std::size_t max_queued_bytes = std::size_t{1} << 20;
[[nodiscard]] std::expected<void, TlsError> queue(std::span<const std::uint8_t> bytes);
// Writes queued plaintext until done or the transport would block.
[[nodiscard]] std::expected<TlsProgress, TlsError> flush();
[[nodiscard]] std::size_t queued() const noexcept;
[[nodiscard]] std::expected<std::size_t, TlsError> receive(std::span<std::uint8_t> bytes);
// Flushes queued plaintext first, then sends close_notify (want_io until both done).
[[nodiscard]] std::expected<TlsProgress, TlsError> 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> impl);
std::unique_ptr<Impl> impl_;
};
} // namespace evo::network