Add M10.8b signed handoff tickets and ticket admission
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (client) (push) Successful in 12m40s
Foundation / checks (postgres) (push) Canceled after 4s
Foundation / checks (network) (push) Successful in 12m53s
Foundation / checks (tsan) (push) Successful in 7m55s
Foundation / checks (checks) (push) Successful in 31m47s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

ADR-0096. A source region's handoff key signs a 160-byte ticket for one probe
transfer, account and destination; it rides the Hello grant slot and is
verified against neighbour keys bound to their regions. SessionControl
admits a ticket once per region, superseding like a grant, with the session
ending at the client's original grant expiry. RegionSessions makes the
destination's durable step first: PostgresHandoffLedger stages and commits
the transfer and moves the account's control, idempotently.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-06 18:32:14 -04:00
co-authored by Claude Opus 5.5
parent 6a645ef7e4
commit 0ce51077b1
18 changed files with 1039 additions and 19 deletions
+77
View File
@@ -1,4 +1,5 @@
#include "evo/network/session_control.hpp"
#include "evo/network/handoff_ticket.hpp"
#include <algorithm>
namespace evo::network {
@@ -154,6 +155,82 @@ std::expected<OpenedSession, ControlError> SessionControl::open(const VerifiedSe
return opened;
}
std::optional<ControlError> SessionControl::ticket_error(const VerifiedHandoffTicket& ticket,
std::uint64_t now_seconds) const {
const auto& c = ticket.claims();
if (c.world != config_.world)
return ControlError::wrong_world;
if (c.destination != config_.region)
return ControlError::wrong_region;
if (now_seconds >= c.expires_at || now_seconds >= c.session_expires_at)
return ControlError::expired;
if (std::ranges::any_of(used_tickets_, [&](const UsedTicket& u) {
return u.transfer == c.transfer && u.until > now_seconds;
}))
return ControlError::stale_grant; // A replayed ticket.
return std::nullopt;
}
std::expected<void, ControlError> SessionControl::check_handoff(const VerifiedHandoffTicket& ticket,
std::uint64_t now_seconds) const {
if (const auto e = ticket_error(ticket, now_seconds))
return std::unexpected(*e);
const auto& c = ticket.claims();
const auto found =
std::ranges::find_if(accounts_, [&](const Account& a) { return a.id == c.account; });
if (found == accounts_.end()) {
const auto kept = std::ranges::count_if(
accounts_, [&](const Account& a) { return !forgettable(a, now_seconds); });
if (static_cast<std::size_t>(kept) >= config_.max_accounts)
return std::unexpected(ControlError::accounts_full);
} else if (!window_over(found->window_start, now_seconds, config_.attempt_window) &&
found->attempts >= config_.max_account_attempts) {
return std::unexpected(ControlError::rate_limited);
}
if ((found == accounts_.end() || found->session == 0) && sessions() >= config_.max_sessions)
return std::unexpected(ControlError::sessions_full);
return {};
}
std::expected<OpenedSession, ControlError>
SessionControl::open_handoff(const VerifiedHandoffTicket& ticket, std::uint64_t now_seconds) {
std::erase_if(used_tickets_, [&](const UsedTicket& u) { return u.until <= now_seconds; });
if (const auto e = ticket_error(ticket, now_seconds))
return std::unexpected(*e);
const auto& c = ticket.claims();
if (used_tickets_.size() >= config_.max_accounts)
return std::unexpected(ControlError::accounts_full); // Fail closed: never forget early.
forget_idle(now_seconds);
auto found =
std::ranges::find_if(accounts_, [&](const Account& a) { return a.id == c.account; });
if (found == accounts_.end()) {
if (accounts_.size() >= config_.max_accounts)
return std::unexpected(ControlError::accounts_full);
accounts_.push_back({c.account, 0, 0, 0, 0, now_seconds, 0});
found = std::prev(accounts_.end());
}
auto& a = *found;
if (window_over(a.window_start, now_seconds, config_.attempt_window)) {
a.window_start = now_seconds;
a.attempts = 0;
}
if (a.attempts >= config_.max_account_attempts)
return std::unexpected(ControlError::rate_limited);
++a.attempts;
if (a.session == 0 && sessions() >= config_.max_sessions)
return std::unexpected(ControlError::sessions_full);
used_tickets_.push_back({c.transfer, c.expires_at});
OpenedSession opened;
opened.superseded = a.session;
a.session = next_session_++;
a.session_expires_at =
std::min<std::uint64_t>(c.session_expires_at, now_seconds + config_.max_grant_lifetime);
opened.session = a.session;
opened.epoch = config_.epoch;
opened.expires_at = a.session_expires_at;
return opened;
}
std::expected<std::uint64_t, ControlError>
SessionControl::refresh(std::uint64_t session, const VerifiedSessionGrant& grant,
std::uint64_t now_seconds) {
+245
View File
@@ -0,0 +1,245 @@
#include "evo/network/handoff_ticket.hpp"
#include <algorithm>
#include <psa/crypto.h>
#include <utility>
namespace evo::network {
namespace {
constexpr std::array<std::uint8_t, 16> marker{'E', 'V', 'O', '-', 'H', 'A', 'N', 'D',
'O', 'F', 'F', 0, 0, 0, 0, 0};
constexpr std::uint16_t ticket_version = 1;
constexpr std::size_t signed_end = 96; // The signature covers bytes [0, 96).
constexpr std::size_t signature_bytes = handoff_ticket_bytes - signed_end;
constexpr psa_algorithm_t algorithm = PSA_ALG_ECDSA(PSA_ALG_SHA_256);
static_assert(signature_bytes == PSA_ECDSA_SIGNATURE_SIZE(256));
static_assert(handoff_ticket_bytes == session_grant_bytes, "tickets use the Hello grant slot");
std::uint8_t environment_byte(DeploymentEnvironment e) {
return static_cast<std::uint8_t>(static_cast<std::uint8_t>(e) + 1);
}
void put(HandoffTicket& 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<std::uint8_t>(v >> (8 * i));
}
std::uint64_t get(std::span<const std::uint8_t> 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<std::uint64_t>(in[at + i]) << (8 * i);
return v;
}
bool structurally_valid(const TicketClaims& c) {
return std::ranges::any_of(c.transfer, [](std::uint8_t b) { return b != 0; }) &&
c.account.valid() && c.world.world != 0 && c.world.frame != 0 && c.source != 0 &&
c.destination != 0 && c.source != c.destination && c.expires_at != 0 &&
c.session_expires_at != 0;
}
std::expected<std::uint32_t, HandoffTicketError> import_key(const psa_key_attributes_t& a,
std::span<const std::uint8_t> bytes) {
mbedtls_svc_key_id_t key = PSA_KEY_ID_NULL;
if (psa_import_key(&a, bytes.data(), bytes.size(), &key) != PSA_SUCCESS)
return std::unexpected(HandoffTicketError::invalid_key);
return key;
}
psa_key_attributes_t pair_attributes() {
psa_key_attributes_t a = PSA_KEY_ATTRIBUTES_INIT;
psa_set_key_type(&a, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
psa_set_key_bits(&a, 256);
psa_set_key_usage_flags(&a, PSA_KEY_USAGE_SIGN_MESSAGE);
psa_set_key_algorithm(&a, algorithm);
return a;
}
} // namespace
bool looks_like_handoff_ticket(std::span<const std::uint8_t> bytes) noexcept {
return bytes.size() == handoff_ticket_bytes &&
std::equal(marker.begin(), marker.end(), bytes.begin());
}
std::expected<HandoffSigningKey, HandoffTicketError>
HandoffSigningKey::generate(DeploymentEnvironment environment, std::uint32_t key_id) {
if (key_id == 0)
return std::unexpected(HandoffTicketError::invalid_key);
if (psa_crypto_init() != PSA_SUCCESS)
return std::unexpected(HandoffTicketError::crypto);
auto a = pair_attributes();
mbedtls_svc_key_id_t key = PSA_KEY_ID_NULL;
const auto status = psa_generate_key(&a, &key);
psa_reset_key_attributes(&a);
if (status != PSA_SUCCESS)
return std::unexpected(HandoffTicketError::crypto);
return HandoffSigningKey{environment, key_id, key};
}
std::expected<HandoffSigningKey, HandoffTicketError>
HandoffSigningKey::import(DeploymentEnvironment environment, std::uint32_t key_id,
std::span<const std::uint8_t> private_key) {
if (key_id == 0 || private_key.size() != grant_private_key_bytes)
return std::unexpected(HandoffTicketError::invalid_key);
if (psa_crypto_init() != PSA_SUCCESS)
return std::unexpected(HandoffTicketError::crypto);
auto a = pair_attributes();
auto key = import_key(a, private_key);
psa_reset_key_attributes(&a);
if (!key)
return std::unexpected(key.error());
return HandoffSigningKey{environment, key_id, *key};
}
HandoffSigningKey::HandoffSigningKey(HandoffSigningKey&& other) noexcept
: environment_{other.environment_}, key_id_{other.key_id_},
key_{std::exchange(other.key_, PSA_KEY_ID_NULL)} {}
HandoffSigningKey& HandoffSigningKey::operator=(HandoffSigningKey&& other) noexcept {
if (this != &other) {
if (key_ != PSA_KEY_ID_NULL)
(void)psa_destroy_key(key_);
environment_ = other.environment_;
key_id_ = other.key_id_;
key_ = std::exchange(other.key_, PSA_KEY_ID_NULL);
}
return *this;
}
HandoffSigningKey::~HandoffSigningKey() {
if (key_ != PSA_KEY_ID_NULL)
(void)psa_destroy_key(key_);
}
std::expected<HandoffTicket, HandoffTicketError>
HandoffSigningKey::sign(const TicketClaims& claims) const {
if (key_ == PSA_KEY_ID_NULL)
return std::unexpected(HandoffTicketError::invalid_key);
if (!structurally_valid(claims))
return std::unexpected(HandoffTicketError::invalid_claims);
HandoffTicket out{};
std::ranges::copy(marker, out.begin());
put(out, 16, ticket_version, 2);
out.at(18) = environment_byte(environment_);
out.at(19) = 0;
put(out, 20, key_id_, 4);
std::ranges::copy(claims.transfer, out.begin() + 24);
std::ranges::copy(claims.account.bytes, out.begin() + 40);
put(out, 56, claims.world.world, 8);
put(out, 64, claims.world.frame, 8);
put(out, 72, claims.source, 8);
put(out, 80, claims.destination, 8);
put(out, 88, claims.expires_at, 4);
put(out, 92, claims.session_expires_at, 4);
std::size_t length = 0;
if (psa_sign_message(key_, algorithm, out.data(), signed_end, out.data() + signed_end,
signature_bytes, &length) != PSA_SUCCESS ||
length != signature_bytes)
return std::unexpected(HandoffTicketError::crypto);
return out;
}
std::expected<GrantPublicKey, HandoffTicketError> HandoffSigningKey::public_key() const {
if (key_ == PSA_KEY_ID_NULL)
return std::unexpected(HandoffTicketError::invalid_key);
GrantPublicKey out{};
std::size_t length = 0;
if (psa_export_public_key(key_, out.data(), out.size(), &length) != PSA_SUCCESS ||
length != out.size())
return std::unexpected(HandoffTicketError::crypto);
return out;
}
std::expected<HandoffVerifier, HandoffTicketError>
HandoffVerifier::create(DeploymentEnvironment environment, world::WorldIdentity world,
std::uint64_t region) {
if (world.world == 0 || world.frame == 0 || region == 0)
return std::unexpected(HandoffTicketError::invalid_claims);
if (psa_crypto_init() != PSA_SUCCESS)
return std::unexpected(HandoffTicketError::crypto);
return HandoffVerifier{environment, world, region};
}
HandoffVerifier::HandoffVerifier(HandoffVerifier&& other) noexcept
: environment_{other.environment_}, world_{other.world_}, region_{other.region_},
keys_{std::exchange(other.keys_, {})} {}
HandoffVerifier& HandoffVerifier::operator=(HandoffVerifier&& other) noexcept {
if (this != &other) {
destroy();
environment_ = other.environment_;
world_ = other.world_;
region_ = other.region_;
keys_ = std::exchange(other.keys_, {});
}
return *this;
}
HandoffVerifier::~HandoffVerifier() {
destroy();
}
void HandoffVerifier::destroy() noexcept {
for (const auto& k : keys_)
(void)psa_destroy_key(k.key);
keys_.clear();
}
std::expected<void, HandoffTicketError>
HandoffVerifier::add_key(std::uint64_t source_region, std::uint32_t key_id,
std::span<const std::uint8_t> public_key) {
if (source_region == 0 || source_region == region_ || key_id == 0 ||
public_key.size() != grant_public_key_bytes)
return std::unexpected(HandoffTicketError::invalid_key);
if (std::ranges::any_of(keys_, [&](const Key& k) { return k.key_id == key_id; }))
return std::unexpected(HandoffTicketError::duplicate_key);
if (keys_.size() >= max_keys)
return std::unexpected(HandoffTicketError::too_many_keys);
psa_key_attributes_t a = PSA_KEY_ATTRIBUTES_INIT;
psa_set_key_type(&a, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1));
psa_set_key_bits(&a, 256);
psa_set_key_usage_flags(&a, PSA_KEY_USAGE_VERIFY_MESSAGE);
psa_set_key_algorithm(&a, algorithm);
auto key = import_key(a, public_key);
psa_reset_key_attributes(&a);
if (!key)
return std::unexpected(key.error());
keys_.push_back({source_region, key_id, *key});
return {};
}
std::expected<VerifiedHandoffTicket, HandoffTicketError>
HandoffVerifier::verify(std::span<const std::uint8_t> ticket, std::uint64_t now_seconds) const {
if (ticket.size() != handoff_ticket_bytes)
return std::unexpected(HandoffTicketError::malformed);
// The key id only selects which key must verify; it is itself signed.
const auto key_id = static_cast<std::uint32_t>(get(ticket, 20, 4));
const auto key = std::ranges::find_if(keys_, [&](const Key& k) { return k.key_id == key_id; });
if (key == keys_.end())
return std::unexpected(HandoffTicketError::unknown_key);
const auto status = psa_verify_message(key->key, algorithm, ticket.data(), signed_end,
ticket.data() + signed_end, signature_bytes);
if (status == PSA_ERROR_INVALID_SIGNATURE)
return std::unexpected(HandoffTicketError::bad_signature);
if (status != PSA_SUCCESS)
return std::unexpected(HandoffTicketError::crypto);
if (!std::equal(marker.begin(), marker.end(), ticket.begin()))
return std::unexpected(HandoffTicketError::wrong_marker);
if (get(ticket, 16, 2) != ticket_version || ticket[19] != 0)
return std::unexpected(HandoffTicketError::unsupported_version);
if (ticket[18] != environment_byte(environment_))
return std::unexpected(HandoffTicketError::wrong_environment);
TicketClaims c;
std::copy_n(ticket.begin() + 24, c.transfer.size(), c.transfer.begin());
std::copy_n(ticket.begin() + 40, c.account.bytes.size(), c.account.bytes.begin());
c.world = {get(ticket, 56, 8), get(ticket, 64, 8)};
c.source = get(ticket, 72, 8);
c.destination = get(ticket, 80, 8);
c.expires_at = static_cast<std::uint32_t>(get(ticket, 88, 4));
c.session_expires_at = static_cast<std::uint32_t>(get(ticket, 92, 4));
if (!structurally_valid(c))
return std::unexpected(HandoffTicketError::invalid_claims);
if (c.source != key->region)
return std::unexpected(HandoffTicketError::wrong_source);
if (c.world.world != world_.world || c.world.frame != world_.frame || c.destination != region_)
return std::unexpected(HandoffTicketError::wrong_scope);
if (now_seconds >= c.expires_at || now_seconds >= c.session_expires_at ||
c.expires_at - now_seconds > max_ticket_lifetime)
return std::unexpected(HandoffTicketError::expired);
return VerifiedHandoffTicket{c};
}
} // namespace evo::network