Foundation / checks (deploy) (push) Successful in 3m24s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (client) (push) Successful in 12m18s
Foundation / checks (network) (push) Successful in 13m30s
Foundation / checks (tsan) (push) Successful in 8m0s
Foundation / checks (checks) (push) Successful in 32m39s
Foundation / checks (postgres) (push) Successful in 45m47s
Foundation / postgres-tools (push) Successful in 17s
Foundation / image (push) Successful in 2m30s
Foundation / server-image (push) Successful in 4m8s
ADR-0097. tools/ops/load-drill.sh (runbook docs/networking/M10_LOAD_DRILL.md) runs the evolution-dev reference qualification: synthetic inputs on for the run (the ADR-0073 drill-time override, restored on exit), bundle-mode evo-load bots through the public route, and each step's hold matched with the server's reports. evo-server logs "m10 load-report" every 10 s, since the agent cannot reach the ops port. evo-load gains --grants FILE --grants-per-bot C, exhausted=, hold_utc= and close_reasons=; evo-session-probe grants gains --accounts N. Finding: Mbed TLS/PSA are built without MBEDTLS_THREADING_C, and bots on different threads verifying their first connections at once were refused as untrusted (two per smoke run). One process-wide recursive crypto lock now guards every network-library entry point that touches them; the smoke went from 2/2 failing to 3/3 clean. quic-transport adds a concurrent-connect test. Refreshes the dev session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
257 lines
11 KiB
C++
257 lines
11 KiB
C++
#include "evo/network/handoff_ticket.hpp"
|
|
#include "evo/network/crypto_lock.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) {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
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) {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
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 {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
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() {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
if (key_ != PSA_KEY_ID_NULL)
|
|
(void)psa_destroy_key(key_);
|
|
}
|
|
|
|
std::expected<HandoffTicket, HandoffTicketError>
|
|
HandoffSigningKey::sign(const TicketClaims& claims) const {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
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 {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
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) {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
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 {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
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) {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
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 {
|
|
const CryptoLock crypto{crypto_mutex()};
|
|
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
|