#include "evo/network/dev_grant.hpp" #include #include #include namespace evo::network { namespace { static_assert(std::is_same_v, "key ids are stored as plain PSA identifiers"); constexpr std::array marker{'E', 'V', 'O', '-', 'D', 'E', 'V', '-', 'O', 'N', 'L', 'Y', 0, 0, 0, 0}; constexpr std::uint16_t grant_version = 1; constexpr std::uint8_t dev_environment_byte = 1; constexpr std::size_t claims_end = 76; // Tag covers bytes [0, 76). constexpr psa_algorithm_t algorithm = PSA_ALG_HMAC(PSA_ALG_SHA_256); void put(DevGrant& 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 DevGrantClaims& c) { return c.grant_id != 0 && c.subject != 0 && c.world.world != 0 && c.world.frame != 0 && c.region != 0 && c.expires_at > c.issued_at; } } // namespace std::expected DevGrantKey::import(DeploymentEnvironment environment, std::span secret) { // The dev identity must not exist anywhere but the dev environment. if (environment != DeploymentEnvironment::dev) return std::unexpected(GrantError::not_dev_environment); if (secret.size() != dev_key_bytes || std::ranges::all_of(secret, [](std::uint8_t b) { return b == 0; })) return std::unexpected(GrantError::invalid_key); if (psa_crypto_init() != PSA_SUCCESS) return std::unexpected(GrantError::crypto); psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); psa_set_key_bits(&attributes, dev_key_bytes * 8); psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE); psa_set_key_algorithm(&attributes, algorithm); mbedtls_svc_key_id_t key = PSA_KEY_ID_NULL; const auto status = psa_import_key(&attributes, secret.data(), secret.size(), &key); psa_reset_key_attributes(&attributes); if (status != PSA_SUCCESS) return std::unexpected(GrantError::crypto); return DevGrantKey{key}; } DevGrantKey::DevGrantKey(DevGrantKey&& other) noexcept : key_{other.key_} { other.key_ = PSA_KEY_ID_NULL; } DevGrantKey& DevGrantKey::operator=(DevGrantKey&& other) noexcept { if (this != &other) { if (key_ != PSA_KEY_ID_NULL) (void)psa_destroy_key(key_); key_ = other.key_; other.key_ = PSA_KEY_ID_NULL; } return *this; } DevGrantKey::~DevGrantKey() { if (key_ != PSA_KEY_ID_NULL) (void)psa_destroy_key(key_); } std::expected DevGrantKey::issue(const DevGrantClaims& claims) const { if (key_ == PSA_KEY_ID_NULL) return std::unexpected(GrantError::invalid_key); if (!structurally_valid(claims)) return std::unexpected(GrantError::invalid_claims); DevGrant out{}; std::ranges::copy(marker, out.begin()); put(out, 16, grant_version, 2); out.at(18) = dev_environment_byte; out.at(19) = 0; put(out, 20, claims.grant_id, 8); put(out, 28, claims.subject, 8); put(out, 36, claims.world.world, 8); put(out, 44, claims.world.frame, 8); put(out, 52, claims.region, 8); put(out, 60, claims.issued_at, 8); put(out, 68, claims.expires_at, 8); std::size_t length = 0; if (psa_mac_compute(key_, algorithm, out.data(), claims_end, out.data() + claims_end, dev_grant_bytes - claims_end, &length) != PSA_SUCCESS || length != dev_grant_bytes - claims_end) return std::unexpected(GrantError::crypto); return out; } std::expected DevGrantKey::verify(std::span grant) const { if (key_ == PSA_KEY_ID_NULL) return std::unexpected(GrantError::invalid_key); if (grant.size() != dev_grant_bytes) return std::unexpected(GrantError::malformed); // Authenticate first: no claim byte influences behaviour until the tag matches. const auto status = psa_mac_verify(key_, algorithm, grant.data(), claims_end, grant.data() + claims_end, dev_grant_bytes - claims_end); if (status == PSA_ERROR_INVALID_SIGNATURE) return std::unexpected(GrantError::bad_tag); if (status != PSA_SUCCESS) return std::unexpected(GrantError::crypto); if (!std::equal(marker.begin(), marker.end(), grant.begin())) return std::unexpected(GrantError::wrong_marker); if (get(grant, 16, 2) != grant_version || grant[19] != 0) return std::unexpected(GrantError::unsupported_version); if (grant[18] != dev_environment_byte) return std::unexpected(GrantError::wrong_environment); DevGrantClaims claims; claims.grant_id = get(grant, 20, 8); claims.subject = get(grant, 28, 8); claims.world = {get(grant, 36, 8), get(grant, 44, 8)}; claims.region = get(grant, 52, 8); claims.issued_at = get(grant, 60, 8); claims.expires_at = get(grant, 68, 8); if (!structurally_valid(claims)) return std::unexpected(GrantError::invalid_claims); return VerifiedGrant{claims}; } } // namespace evo::network