#include "evo/network/handoff_ticket.hpp" #include "evo/network/crypto_lock.hpp" #include #include #include namespace evo::network { namespace { constexpr std::array 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(static_cast(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(v >> (8 * i)); } std::uint64_t get(std::span 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(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 import_key(const psa_key_attributes_t& a, std::span 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 bytes) noexcept { return bytes.size() == handoff_ticket_bytes && std::equal(marker.begin(), marker.end(), bytes.begin()); } std::expected 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::import(DeploymentEnvironment environment, std::uint32_t key_id, std::span 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 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 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::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 HandoffVerifier::add_key(std::uint64_t source_region, std::uint32_t key_id, std::span 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 HandoffVerifier::verify(std::span 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(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(get(ticket, 88, 4)); c.session_expires_at = static_cast(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