#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, backpressure }; 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(); // 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 queue(std::span bytes); // Writes queued plaintext until done or the transport would block. [[nodiscard]] std::expected flush(); [[nodiscard]] std::size_t queued() const noexcept; [[nodiscard]] std::expected receive(std::span bytes); // Flushes queued plaintext first, then sends close_notify (want_io until both done). [[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