- 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>
278 lines
12 KiB
C++
278 lines
12 KiB
C++
#include "evo/network/protocol.hpp"
|
|
#include "evo/network/tls_channel.hpp"
|
|
#include "network_pki.hpp"
|
|
#include "network_test.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <deque>
|
|
#include <mbedtls/x509_crt.h>
|
|
#include <psa/crypto.h>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace evo::network_test {
|
|
namespace {
|
|
using namespace evo::network;
|
|
|
|
// Two in-memory byte queues with optional faults.
|
|
struct Wire {
|
|
std::deque<std::uint8_t> to_server, to_client;
|
|
bool fail{};
|
|
// Bytes each side may still write; 0 makes writes "would block" (zero progress).
|
|
std::size_t server_budget{SIZE_MAX}, client_budget{SIZE_MAX};
|
|
};
|
|
class Endpoint final : public ByteTransport {
|
|
public:
|
|
Endpoint(Wire& wire, bool is_server) : wire_{wire}, server_{is_server} {}
|
|
std::expected<std::size_t, TransportError> write(std::span<const std::uint8_t> bytes) override {
|
|
if (wire_.fail)
|
|
return std::unexpected(TransportError::failed);
|
|
auto& out = server_ ? wire_.to_client : wire_.to_server;
|
|
auto& budget = server_ ? wire_.server_budget : wire_.client_budget;
|
|
// Force partial writes, and stop entirely when the injected budget runs out.
|
|
const auto count = std::min({bytes.size(), std::size_t{700}, budget});
|
|
if (budget != SIZE_MAX)
|
|
budget -= count;
|
|
out.insert(out.end(), bytes.begin(), bytes.begin() + static_cast<std::ptrdiff_t>(count));
|
|
return count;
|
|
}
|
|
std::expected<std::size_t, TransportError> read(std::span<std::uint8_t> 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<void, std::pair<bool, TlsError>> 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<std::uint8_t> transfer(TlsChannel& from, TlsChannel& to,
|
|
std::span<const std::uint8_t> bytes) {
|
|
CHECK(from.queue(bytes));
|
|
return drain(from, to, bytes.size());
|
|
}
|
|
// Flushes `from` and reads `to` until `expected` plaintext bytes arrive.
|
|
std::vector<std::uint8_t> drain(TlsChannel& from, TlsChannel& to, std::size_t expected) {
|
|
std::vector<std::uint8_t> received;
|
|
std::array<std::uint8_t, 4096> buffer{};
|
|
for (int i = 0; i < 1000 && received.size() < expected; ++i) {
|
|
CHECK(from.flush());
|
|
const auto n = to.receive(buffer);
|
|
CHECK(n);
|
|
received.insert(received.end(), buffer.begin(),
|
|
buffer.begin() + static_cast<std::ptrdiff_t>(*n));
|
|
}
|
|
return received;
|
|
}
|
|
Wire wire;
|
|
Endpoint server_end, client_end;
|
|
std::unique_ptr<TlsChannel> 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<std::uint8_t>(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<std::uint8_t>(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<std::uint8_t, 16> buffer{};
|
|
std::expected<std::size_t, TlsError> 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->queue(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<std::uint8_t, 64> data{};
|
|
CHECK(pair.client->queue(data) && pair.client->flush());
|
|
CHECK(pair.client->queued() == 0);
|
|
CHECK(pair.wire.to_server.size() > 20);
|
|
pair.wire.to_server[pair.wire.to_server.size() - 5] ^= 0x01;
|
|
std::array<std::uint8_t, 256> 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);
|
|
}
|
|
}
|
|
// Review M9.4-M9.7 #1: frames queued while a TLS record is stalled (zero progress,
|
|
// or partial progress then zero) must neither be lost nor reordered.
|
|
std::vector<std::uint8_t> intents(std::uint64_t first, std::uint64_t count) {
|
|
std::vector<std::uint8_t> bytes;
|
|
for (std::uint64_t i = 0; i < count; ++i) {
|
|
const auto f = encode(IntentMessage{1, 1, first + i, 10, 0, 0});
|
|
bytes.insert(bytes.end(), f.begin(), f.end());
|
|
}
|
|
return bytes;
|
|
}
|
|
std::vector<std::uint64_t> sequences(const std::vector<std::uint8_t>& bytes) {
|
|
FrameReader reader;
|
|
std::vector<std::uint64_t> out;
|
|
std::span<const std::uint8_t> rest{bytes};
|
|
while (!rest.empty()) {
|
|
const auto taken = reader.feed(rest);
|
|
CHECK(taken);
|
|
rest = rest.subspan(*taken);
|
|
while (auto f = reader.take()) {
|
|
const auto m = decode_intent({f->type, f->payload});
|
|
CHECK(m);
|
|
out.push_back(m ? m->sequence : 0);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
void interrupted_writes() {
|
|
Pki pki;
|
|
for (const bool from_client : {true, false})
|
|
for (const std::size_t before_stall : {std::size_t{0}, std::size_t{10}}) {
|
|
Pair pair{pki.server, {pki.authority, server_name}};
|
|
CHECK(pair.handshake());
|
|
auto& from = from_client ? *pair.client : *pair.server;
|
|
auto& to = from_client ? *pair.server : *pair.client;
|
|
auto& budget = from_client ? pair.wire.client_budget : pair.wire.server_budget;
|
|
budget = before_stall; // Zero progress, or partial progress then zero.
|
|
const auto first = intents(1, 1);
|
|
CHECK(from.queue(first));
|
|
const auto stalled = from.flush();
|
|
CHECK(stalled && *stalled == TlsProgress::want_io);
|
|
const auto second = intents(2, 3); // Queued while the first record is stuck.
|
|
CHECK(from.queue(second) && from.queued() == first.size() + second.size());
|
|
budget = SIZE_MAX;
|
|
const auto received = pair.drain(from, to, first.size() + second.size());
|
|
CHECK(received.size() == first.size() + second.size() && from.queued() == 0);
|
|
CHECK(sequences(received) == std::vector<std::uint64_t>({1, 2, 3, 4}));
|
|
}
|
|
// Close while stalled: queued frames are flushed before close_notify.
|
|
Pair pair{pki.server, {pki.authority, server_name}};
|
|
CHECK(pair.handshake());
|
|
pair.wire.client_budget = 0;
|
|
const auto last = intents(9, 2);
|
|
CHECK(pair.client->queue(last));
|
|
const auto pending = pair.client->close();
|
|
CHECK(pending && *pending == TlsProgress::want_io);
|
|
pair.wire.client_budget = SIZE_MAX;
|
|
std::expected<TlsProgress, TlsError> closed = TlsProgress::want_io;
|
|
for (int i = 0; i < 50 && closed && *closed == TlsProgress::want_io; ++i)
|
|
closed = pair.client->close();
|
|
CHECK(closed && *closed == TlsProgress::done);
|
|
std::vector<std::uint8_t> received;
|
|
std::array<std::uint8_t, 256> buffer{};
|
|
std::expected<std::size_t, TlsError> r = 0;
|
|
for (int i = 0; i < 50 && r; ++i) {
|
|
r = pair.server->receive(buffer);
|
|
if (r)
|
|
received.insert(received.end(), buffer.begin(),
|
|
buffer.begin() + static_cast<std::ptrdiff_t>(*r));
|
|
}
|
|
CHECK(refused(r, TlsError::closed) &&
|
|
sequences(received) == std::vector<std::uint64_t>({9, 10}));
|
|
// A full queue refuses without queuing or latching.
|
|
Pair full{pki.server, {pki.authority, server_name}};
|
|
CHECK(full.handshake());
|
|
const std::vector<std::uint8_t> huge(TlsChannel::max_queued_bytes + 1, 0);
|
|
CHECK(refused(full.client->queue(huge), TlsError::backpressure) && full.client->queued() == 0);
|
|
CHECK(full.client->queue(intents(1, 1)) && full.client->queued() == intents(1, 1).size());
|
|
}
|
|
} // namespace
|
|
|
|
void tls_tests() {
|
|
handshake_and_frames();
|
|
rejections();
|
|
interrupted_writes();
|
|
}
|
|
} // namespace evo::network_test
|