Add M10.3a account-bound session grants and region session control
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m47s
Foundation / checks (client) (push) Failing after 9m42s
Foundation / checks (network) (push) Successful in 8m49s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 8m13s
Foundation / checks (tsan) (push) Canceled after 7m13s
Foundation / checks (checks) (push) Canceled after 17m58s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m47s
Foundation / checks (client) (push) Failing after 9m42s
Foundation / checks (network) (push) Successful in 8m49s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 8m13s
Foundation / checks (tsan) (push) Canceled after 7m13s
Foundation / checks (checks) (push) Canceled after 17m58s
Provider-independent 128-bit AccountIds; ECDSA P-256 session grants v1 signed by issuer keys and verified by regions with public keys only, the environment byte signed so dev grants never verify in stage or World0. Per-account serials make the newest grant win: replay and older stolen grants are refused, a newer grant replaces the account's session, and refresh renews in place. Attempt rates and capacities are bounded. The evo_identity library provides the provider, account directory and grant issuer interfaces with a dev-only test provider. ADR-0091. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
#include "evo/identity/identity.hpp"
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <string_view>
|
||||
|
||||
namespace evo::identity {
|
||||
namespace {
|
||||
constexpr std::string_view dev_test_prefix = "dev-test:";
|
||||
constexpr std::size_t max_dev_name = 32;
|
||||
constexpr std::size_t max_subject = 128;
|
||||
} // namespace
|
||||
|
||||
std::expected<DevTestProvider, IdentityError>
|
||||
DevTestProvider::create(DeploymentEnvironment environment) {
|
||||
// Test identities prove nothing; they must not exist outside dev.
|
||||
if (environment != DeploymentEnvironment::dev)
|
||||
return std::unexpected(IdentityError::not_dev_environment);
|
||||
return DevTestProvider{};
|
||||
}
|
||||
|
||||
std::expected<ProviderSubject, IdentityError>
|
||||
DevTestProvider::authenticate(std::span<const std::uint8_t> credential) {
|
||||
const std::string_view text{reinterpret_cast<const char*>(credential.data()),
|
||||
credential.size()};
|
||||
if (!text.starts_with(dev_test_prefix))
|
||||
return std::unexpected(IdentityError::unauthenticated);
|
||||
const auto name = text.substr(dev_test_prefix.size());
|
||||
if (name.empty() || name.size() > max_dev_name || !std::ranges::all_of(name, [](char c) {
|
||||
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-';
|
||||
}))
|
||||
return std::unexpected(IdentityError::unauthenticated);
|
||||
return ProviderSubject{ProviderKind::dev_test, std::string{name}};
|
||||
}
|
||||
|
||||
std::expected<AccountId, IdentityError>
|
||||
MemoryAccountDirectory::resolve(const ProviderSubject& subject) {
|
||||
if (subject.subject.empty() || subject.subject.size() > max_subject)
|
||||
return std::unexpected(IdentityError::invalid_request);
|
||||
if (const auto found = subjects_.find(subject); found != subjects_.end())
|
||||
return found->second;
|
||||
if (subjects_.size() >= max_accounts_)
|
||||
return std::unexpected(IdentityError::directory_full);
|
||||
AccountId account;
|
||||
do { // Collisions of 128 random bits are not expected; never reuse an id regardless.
|
||||
auto generated = network::generate_account_id();
|
||||
if (!generated)
|
||||
return std::unexpected(IdentityError::signing);
|
||||
account = *generated;
|
||||
} while (sequences_.contains(account));
|
||||
subjects_.emplace(subject, account);
|
||||
sequences_.emplace(account, GrantSequence{});
|
||||
return account;
|
||||
}
|
||||
|
||||
std::expected<GrantSequence, IdentityError>
|
||||
MemoryAccountDirectory::next_grant(const AccountId& account, std::uint64_t now_seconds) {
|
||||
const auto found = sequences_.find(account);
|
||||
if (found == sequences_.end())
|
||||
return std::unexpected(IdentityError::invalid_request);
|
||||
auto& s = found->second;
|
||||
if (s.serial == std::numeric_limits<std::uint64_t>::max())
|
||||
return std::unexpected(IdentityError::serial_exhausted);
|
||||
++s.serial;
|
||||
s.issued_at = std::max(s.issued_at, now_seconds); // Never earlier than a prior grant.
|
||||
return s;
|
||||
}
|
||||
|
||||
std::expected<GrantIssuer, IdentityError> GrantIssuer::create(network::GrantSigningKey key,
|
||||
IdentityProvider& provider,
|
||||
AccountDirectory& directory,
|
||||
std::uint64_t lifetime_seconds) {
|
||||
if (provider.environment() != key.environment())
|
||||
return std::unexpected(IdentityError::wrong_environment);
|
||||
if (lifetime_seconds == 0 || lifetime_seconds > 3600)
|
||||
return std::unexpected(IdentityError::invalid_request);
|
||||
return GrantIssuer{std::move(key), provider, directory, lifetime_seconds};
|
||||
}
|
||||
|
||||
std::expected<network::SessionGrant, IdentityError>
|
||||
GrantIssuer::issue(std::span<const std::uint8_t> credential, world::WorldIdentity world,
|
||||
std::uint64_t region, std::uint64_t now_seconds) {
|
||||
if (world.world == 0 || world.frame == 0 || region == 0 ||
|
||||
now_seconds > std::numeric_limits<std::uint64_t>::max() - lifetime_)
|
||||
return std::unexpected(IdentityError::invalid_request);
|
||||
auto subject = provider_->authenticate(credential);
|
||||
if (!subject)
|
||||
return std::unexpected(subject.error());
|
||||
auto account = directory_->resolve(*subject);
|
||||
if (!account)
|
||||
return std::unexpected(account.error());
|
||||
auto sequence = directory_->next_grant(*account, now_seconds);
|
||||
if (!sequence)
|
||||
return std::unexpected(sequence.error());
|
||||
network::GrantClaims claims;
|
||||
do {
|
||||
std::array<std::uint8_t, 8> bytes{};
|
||||
if (!network::random_bytes(bytes))
|
||||
return std::unexpected(IdentityError::signing);
|
||||
for (std::size_t i = 0; i < bytes.size(); ++i)
|
||||
claims.grant_id |= static_cast<std::uint64_t>(bytes.at(i)) << (8U * i);
|
||||
} while (claims.grant_id == 0);
|
||||
claims.account = *account;
|
||||
claims.serial = sequence->serial;
|
||||
claims.world = world;
|
||||
claims.region = region;
|
||||
claims.issued_at = sequence->issued_at;
|
||||
if (sequence->issued_at > std::numeric_limits<std::uint64_t>::max() - lifetime_)
|
||||
return std::unexpected(IdentityError::invalid_request);
|
||||
claims.expires_at = sequence->issued_at + lifetime_;
|
||||
auto grant = key_.sign(claims);
|
||||
if (!grant)
|
||||
return std::unexpected(IdentityError::signing);
|
||||
return *grant;
|
||||
}
|
||||
} // namespace evo::identity
|
||||
@@ -0,0 +1,192 @@
|
||||
#include "evo/network/session_control.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
// Fixed attempt windows: restart when `now` left the window (or the clock went back).
|
||||
bool window_over(std::uint64_t start, std::uint64_t now, std::uint64_t length) {
|
||||
return now < start || now - start >= length;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool AccountId::valid() const noexcept {
|
||||
return std::ranges::any_of(bytes, [](std::uint8_t b) { return b != 0; });
|
||||
}
|
||||
|
||||
std::string log_label(const AccountId& account) {
|
||||
constexpr char digits[] = "0123456789abcdef";
|
||||
std::string label{"acct-"};
|
||||
for (std::size_t i = 0; i < 4; ++i) {
|
||||
label.push_back(digits[account.bytes.at(i) >> 4U]);
|
||||
label.push_back(digits[account.bytes.at(i) & 0xFU]);
|
||||
}
|
||||
return label;
|
||||
}
|
||||
|
||||
std::expected<SessionControl, ControlError> SessionControl::create(ControlConfig config) {
|
||||
if (config.world.world == 0 || config.world.frame == 0 || config.region == 0 ||
|
||||
config.epoch == 0 || config.max_sessions == 0 || config.max_sessions > 1024 ||
|
||||
config.max_accounts < config.max_sessions || config.max_accounts > (std::size_t{1} << 20) ||
|
||||
config.max_grant_lifetime == 0 || config.max_grant_lifetime > 3600 ||
|
||||
config.clock_skew > 300 || config.max_account_attempts == 0 ||
|
||||
config.max_region_attempts == 0 || config.attempt_window == 0 ||
|
||||
config.attempt_window > 3600)
|
||||
return std::unexpected(ControlError::invalid_config);
|
||||
SessionControl control{config};
|
||||
control.accounts_.reserve(config.max_accounts);
|
||||
return control;
|
||||
}
|
||||
|
||||
std::expected<void, ControlError> SessionControl::attempt(std::uint64_t now_seconds) {
|
||||
if (window_over(region_window_start_, now_seconds, config_.attempt_window)) {
|
||||
region_window_start_ = now_seconds;
|
||||
region_attempts_ = 0;
|
||||
}
|
||||
if (region_attempts_ >= config_.max_region_attempts)
|
||||
return std::unexpected(ControlError::rate_limited);
|
||||
++region_attempts_;
|
||||
return {};
|
||||
}
|
||||
|
||||
void SessionControl::forget_idle(std::uint64_t now_seconds) {
|
||||
std::erase_if(accounts_, [&](const Account& a) {
|
||||
return a.session == 0 && a.remember_until <= now_seconds &&
|
||||
window_over(a.window_start, now_seconds, config_.attempt_window);
|
||||
});
|
||||
}
|
||||
|
||||
std::expected<SessionControl::Account*, ControlError>
|
||||
SessionControl::admit(const VerifiedSessionGrant& grant, std::uint64_t now_seconds) {
|
||||
const auto& c = grant.claims();
|
||||
if (grant.environment() != config_.environment)
|
||||
return std::unexpected(ControlError::wrong_environment);
|
||||
if (c.world != config_.world)
|
||||
return std::unexpected(ControlError::wrong_world);
|
||||
if (c.region != config_.region)
|
||||
return std::unexpected(ControlError::wrong_region);
|
||||
if (c.expires_at <= c.issued_at || c.expires_at - c.issued_at > config_.max_grant_lifetime)
|
||||
return std::unexpected(ControlError::invalid_lifetime);
|
||||
if (c.issued_at > now_seconds && c.issued_at - now_seconds > config_.clock_skew)
|
||||
return std::unexpected(ControlError::not_yet_valid);
|
||||
if (now_seconds >= c.expires_at)
|
||||
return std::unexpected(ControlError::expired);
|
||||
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;
|
||||
// Every verified attempt by the account counts, accepted or not.
|
||||
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 (c.serial <= a.serial)
|
||||
return std::unexpected(ControlError::stale_grant);
|
||||
return &a;
|
||||
}
|
||||
|
||||
void SessionControl::accept(Account& a, const GrantClaims& c) {
|
||||
a.serial = c.serial;
|
||||
// Issuers keep serial and issued_at non-decreasing per account, so every older grant
|
||||
// expires by the newest issued_at plus the maximum lifetime: remember until then.
|
||||
a.remember_until = std::max(a.remember_until, c.issued_at + config_.max_grant_lifetime);
|
||||
a.session_expires_at = c.expires_at;
|
||||
}
|
||||
|
||||
std::expected<OpenedSession, ControlError> SessionControl::open(const VerifiedSessionGrant& grant,
|
||||
std::uint64_t now_seconds) {
|
||||
auto admitted = admit(grant, now_seconds);
|
||||
if (!admitted)
|
||||
return std::unexpected(admitted.error());
|
||||
auto& a = **admitted;
|
||||
// A refused open leaves the grant unconsumed: its serial is not recorded.
|
||||
if (a.session == 0 && sessions() >= config_.max_sessions)
|
||||
return std::unexpected(ControlError::sessions_full);
|
||||
OpenedSession opened;
|
||||
opened.superseded = a.session; // Closed before the new session exists.
|
||||
accept(a, grant.claims());
|
||||
a.session = next_session_++;
|
||||
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) {
|
||||
const auto holder_of =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.session == session; });
|
||||
if (session == 0 || holder_of == accounts_.end())
|
||||
return std::unexpected(ControlError::unknown_session);
|
||||
if (holder_of->id != grant.claims().account)
|
||||
return std::unexpected(ControlError::wrong_account);
|
||||
auto admitted = admit(grant, now_seconds);
|
||||
if (!admitted)
|
||||
return std::unexpected(admitted.error());
|
||||
accept(**admitted, grant.claims());
|
||||
return (*admitted)->session_expires_at;
|
||||
}
|
||||
|
||||
std::expected<void, ControlError> SessionControl::close(std::uint64_t session) {
|
||||
const auto found =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.session == session; });
|
||||
if (session == 0 || found == accounts_.end())
|
||||
return std::unexpected(ControlError::unknown_session);
|
||||
found->session = 0;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<std::uint64_t> SessionControl::expire(std::uint64_t now_seconds) {
|
||||
std::vector<std::uint64_t> closed;
|
||||
for (auto& a : accounts_)
|
||||
if (a.session != 0 && now_seconds >= a.session_expires_at) {
|
||||
closed.push_back(a.session);
|
||||
a.session = 0;
|
||||
}
|
||||
return closed;
|
||||
}
|
||||
|
||||
std::expected<std::vector<std::uint64_t>, ControlError>
|
||||
SessionControl::advance_epoch(std::uint64_t epoch) {
|
||||
if (epoch <= config_.epoch)
|
||||
return std::unexpected(ControlError::invalid_config);
|
||||
config_.epoch = epoch;
|
||||
std::vector<std::uint64_t> closed;
|
||||
for (auto& a : accounts_)
|
||||
if (a.session != 0) {
|
||||
closed.push_back(a.session);
|
||||
a.session = 0;
|
||||
}
|
||||
return closed;
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> SessionControl::holder(const AccountId& account) const {
|
||||
const auto found =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.id == account; });
|
||||
if (found == accounts_.end() || found->session == 0)
|
||||
return std::nullopt;
|
||||
return found->session;
|
||||
}
|
||||
|
||||
std::optional<AccountId> SessionControl::account_of(std::uint64_t session) const {
|
||||
const auto found =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.session == session; });
|
||||
if (session == 0 || found == accounts_.end())
|
||||
return std::nullopt;
|
||||
return found->id;
|
||||
}
|
||||
|
||||
std::size_t SessionControl::sessions() const noexcept {
|
||||
return static_cast<std::size_t>(
|
||||
std::ranges::count_if(accounts_, [](const Account& a) { return a.session != 0; }));
|
||||
}
|
||||
} // namespace evo::network
|
||||
@@ -0,0 +1,241 @@
|
||||
#include "evo/network/session_grant.hpp"
|
||||
#include <algorithm>
|
||||
#include <psa/crypto.h>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
static_assert(std::is_same_v<mbedtls_svc_key_id_t, std::uint32_t>,
|
||||
"key ids are stored as plain PSA identifiers");
|
||||
constexpr std::array<std::uint8_t, 16> marker{'E', 'V', 'O', '-', 'G', 'R', 'A', 'N',
|
||||
'T', 0, 0, 0, 0, 0, 0, 0};
|
||||
constexpr std::uint16_t grant_version = 1;
|
||||
constexpr std::size_t signed_end = 96; // The signature covers bytes [0, 96).
|
||||
constexpr std::size_t signature_bytes = session_grant_bytes - signed_end;
|
||||
constexpr psa_algorithm_t algorithm = PSA_ALG_ECDSA(PSA_ALG_SHA_256);
|
||||
static_assert(signature_bytes == PSA_ECDSA_SIGNATURE_SIZE(256));
|
||||
|
||||
std::uint8_t environment_byte(DeploymentEnvironment e) {
|
||||
return static_cast<std::uint8_t>(static_cast<std::uint8_t>(e) + 1);
|
||||
}
|
||||
void put(SessionGrant& 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 GrantClaims& c) {
|
||||
return c.grant_id != 0 && c.account.valid() && c.serial != 0 && c.world.world != 0 &&
|
||||
c.world.frame != 0 && c.region != 0 && c.expires_at > c.issued_at;
|
||||
}
|
||||
std::expected<std::uint32_t, SessionGrantError> import_key(const psa_key_attributes_t& attributes,
|
||||
std::span<const std::uint8_t> bytes) {
|
||||
mbedtls_svc_key_id_t key = PSA_KEY_ID_NULL;
|
||||
if (psa_import_key(&attributes, bytes.data(), bytes.size(), &key) != PSA_SUCCESS)
|
||||
return std::unexpected(SessionGrantError::invalid_key);
|
||||
return key;
|
||||
}
|
||||
psa_key_attributes_t pair_attributes() {
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_bits(&attributes, 256);
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
|
||||
psa_set_key_algorithm(&attributes, algorithm);
|
||||
return attributes;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<void, SessionGrantError> random_bytes(std::span<std::uint8_t> out) {
|
||||
if (psa_crypto_init() != PSA_SUCCESS ||
|
||||
psa_generate_random(out.data(), out.size()) != PSA_SUCCESS)
|
||||
return std::unexpected(SessionGrantError::crypto);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<AccountId, SessionGrantError> generate_account_id() {
|
||||
AccountId id;
|
||||
do {
|
||||
if (auto r = random_bytes(id.bytes); !r)
|
||||
return std::unexpected(r.error());
|
||||
} while (!id.valid());
|
||||
return id;
|
||||
}
|
||||
|
||||
std::expected<GrantSigningKey, SessionGrantError>
|
||||
GrantSigningKey::generate(DeploymentEnvironment environment, std::uint32_t key_id) {
|
||||
if (key_id == 0)
|
||||
return std::unexpected(SessionGrantError::invalid_key);
|
||||
if (psa_crypto_init() != PSA_SUCCESS)
|
||||
return std::unexpected(SessionGrantError::crypto);
|
||||
auto attributes = pair_attributes();
|
||||
mbedtls_svc_key_id_t key = PSA_KEY_ID_NULL;
|
||||
const auto status = psa_generate_key(&attributes, &key);
|
||||
psa_reset_key_attributes(&attributes);
|
||||
if (status != PSA_SUCCESS)
|
||||
return std::unexpected(SessionGrantError::crypto);
|
||||
return GrantSigningKey{environment, key_id, key};
|
||||
}
|
||||
|
||||
std::expected<GrantSigningKey, SessionGrantError>
|
||||
GrantSigningKey::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(SessionGrantError::invalid_key);
|
||||
if (psa_crypto_init() != PSA_SUCCESS)
|
||||
return std::unexpected(SessionGrantError::crypto);
|
||||
auto attributes = pair_attributes();
|
||||
auto key = import_key(attributes, private_key);
|
||||
psa_reset_key_attributes(&attributes);
|
||||
if (!key)
|
||||
return std::unexpected(key.error());
|
||||
return GrantSigningKey{environment, key_id, *key};
|
||||
}
|
||||
|
||||
GrantSigningKey::GrantSigningKey(GrantSigningKey&& other) noexcept
|
||||
: environment_{other.environment_}, key_id_{other.key_id_},
|
||||
key_{std::exchange(other.key_, PSA_KEY_ID_NULL)} {}
|
||||
|
||||
GrantSigningKey& GrantSigningKey::operator=(GrantSigningKey&& 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;
|
||||
}
|
||||
|
||||
GrantSigningKey::~GrantSigningKey() {
|
||||
if (key_ != PSA_KEY_ID_NULL)
|
||||
(void)psa_destroy_key(key_);
|
||||
}
|
||||
|
||||
std::expected<SessionGrant, SessionGrantError>
|
||||
GrantSigningKey::sign(const GrantClaims& claims) const {
|
||||
if (key_ == PSA_KEY_ID_NULL)
|
||||
return std::unexpected(SessionGrantError::invalid_key);
|
||||
if (!structurally_valid(claims))
|
||||
return std::unexpected(SessionGrantError::invalid_claims);
|
||||
SessionGrant out{};
|
||||
std::ranges::copy(marker, out.begin());
|
||||
put(out, 16, grant_version, 2);
|
||||
out.at(18) = environment_byte(environment_);
|
||||
out.at(19) = 0;
|
||||
put(out, 20, key_id_, 4);
|
||||
put(out, 24, claims.grant_id, 8);
|
||||
std::ranges::copy(claims.account.bytes, out.begin() + 32);
|
||||
put(out, 48, claims.serial, 8);
|
||||
put(out, 56, claims.world.world, 8);
|
||||
put(out, 64, claims.world.frame, 8);
|
||||
put(out, 72, claims.region, 8);
|
||||
put(out, 80, claims.issued_at, 8);
|
||||
put(out, 88, claims.expires_at, 8);
|
||||
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(SessionGrantError::crypto);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<GrantPublicKey, SessionGrantError> GrantSigningKey::public_key() const {
|
||||
if (key_ == PSA_KEY_ID_NULL)
|
||||
return std::unexpected(SessionGrantError::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(SessionGrantError::crypto);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<GrantVerifier, SessionGrantError>
|
||||
GrantVerifier::create(DeploymentEnvironment environment) {
|
||||
if (psa_crypto_init() != PSA_SUCCESS)
|
||||
return std::unexpected(SessionGrantError::crypto);
|
||||
return GrantVerifier{environment};
|
||||
}
|
||||
|
||||
GrantVerifier::GrantVerifier(GrantVerifier&& other) noexcept
|
||||
: environment_{other.environment_}, keys_{std::exchange(other.keys_, {})} {}
|
||||
|
||||
GrantVerifier& GrantVerifier::operator=(GrantVerifier&& other) noexcept {
|
||||
if (this != &other) {
|
||||
destroy();
|
||||
environment_ = other.environment_;
|
||||
keys_ = std::exchange(other.keys_, {});
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
GrantVerifier::~GrantVerifier() {
|
||||
destroy();
|
||||
}
|
||||
|
||||
void GrantVerifier::destroy() noexcept {
|
||||
for (const auto& k : keys_)
|
||||
(void)psa_destroy_key(k.key);
|
||||
keys_.clear();
|
||||
}
|
||||
|
||||
std::expected<void, SessionGrantError>
|
||||
GrantVerifier::add_key(std::uint32_t key_id, std::span<const std::uint8_t> public_key) {
|
||||
if (key_id == 0 || public_key.size() != grant_public_key_bytes)
|
||||
return std::unexpected(SessionGrantError::invalid_key);
|
||||
if (std::ranges::any_of(keys_, [&](const Key& k) { return k.key_id == key_id; }))
|
||||
return std::unexpected(SessionGrantError::duplicate_key);
|
||||
if (keys_.size() >= max_keys)
|
||||
return std::unexpected(SessionGrantError::too_many_keys);
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_bits(&attributes, 256);
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
|
||||
psa_set_key_algorithm(&attributes, algorithm);
|
||||
auto key = import_key(attributes, public_key);
|
||||
psa_reset_key_attributes(&attributes);
|
||||
if (!key)
|
||||
return std::unexpected(key.error());
|
||||
keys_.push_back({key_id, *key});
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<VerifiedSessionGrant, SessionGrantError>
|
||||
GrantVerifier::verify(std::span<const std::uint8_t> grant) const {
|
||||
if (grant.size() != session_grant_bytes)
|
||||
return std::unexpected(SessionGrantError::malformed);
|
||||
// The key id only selects which key must verify; it is itself signed.
|
||||
const auto key_id = static_cast<std::uint32_t>(get(grant, 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(SessionGrantError::unknown_key);
|
||||
const auto status = psa_verify_message(key->key, algorithm, grant.data(), signed_end,
|
||||
grant.data() + signed_end, signature_bytes);
|
||||
if (status == PSA_ERROR_INVALID_SIGNATURE)
|
||||
return std::unexpected(SessionGrantError::bad_signature);
|
||||
if (status != PSA_SUCCESS)
|
||||
return std::unexpected(SessionGrantError::crypto);
|
||||
if (!std::equal(marker.begin(), marker.end(), grant.begin()))
|
||||
return std::unexpected(SessionGrantError::wrong_marker);
|
||||
if (get(grant, 16, 2) != grant_version || grant[19] != 0)
|
||||
return std::unexpected(SessionGrantError::unsupported_version);
|
||||
if (grant[18] != environment_byte(environment_))
|
||||
return std::unexpected(SessionGrantError::wrong_environment);
|
||||
GrantClaims claims;
|
||||
claims.grant_id = get(grant, 24, 8);
|
||||
std::copy_n(grant.begin() + 32, claims.account.bytes.size(), claims.account.bytes.begin());
|
||||
claims.serial = get(grant, 48, 8);
|
||||
claims.world = {get(grant, 56, 8), get(grant, 64, 8)};
|
||||
claims.region = get(grant, 72, 8);
|
||||
claims.issued_at = get(grant, 80, 8);
|
||||
claims.expires_at = get(grant, 88, 8);
|
||||
if (!structurally_valid(claims))
|
||||
return std::unexpected(SessionGrantError::invalid_claims);
|
||||
return VerifiedSessionGrant{environment_, claims};
|
||||
}
|
||||
} // namespace evo::network
|
||||
Reference in New Issue
Block a user