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>
100 lines
4.1 KiB
C++
100 lines
4.1 KiB
C++
#pragma once
|
|
#include "evo/network/session_control.hpp"
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
// Production session grants v1 (ADR-0091, schemas/network/session-grant-v1.md): a fixed
|
|
// 160-byte record ("EVO-GRANT" marker, version, environment, issuer key id, claims)
|
|
// signed with ECDSA P-256/SHA-256 through Mbed TLS PSA. Regions hold only public keys,
|
|
// so a compromised region cannot mint grants. The signed environment byte must match
|
|
// the verifier's environment: a dev-signed grant never verifies in stage or World0, and
|
|
// M9 dev grants (a different marker and length) never verify here. Grant bytes and keys
|
|
// have no log form.
|
|
namespace evo::network {
|
|
inline constexpr std::size_t session_grant_bytes = 160;
|
|
inline constexpr std::size_t grant_private_key_bytes = 32; // P-256 scalar.
|
|
inline constexpr std::size_t grant_public_key_bytes = 65; // Uncompressed point.
|
|
using SessionGrant = std::array<std::uint8_t, session_grant_bytes>;
|
|
using GrantPublicKey = std::array<std::uint8_t, grant_public_key_bytes>;
|
|
enum class SessionGrantError : std::uint8_t {
|
|
invalid_key,
|
|
duplicate_key,
|
|
too_many_keys,
|
|
invalid_claims,
|
|
malformed,
|
|
wrong_marker,
|
|
unsupported_version,
|
|
wrong_environment,
|
|
unknown_key,
|
|
bad_signature,
|
|
crypto
|
|
};
|
|
|
|
// Issuer-side signing key (PSA, non-exportable private part). Move-only.
|
|
class GrantSigningKey {
|
|
public:
|
|
[[nodiscard]] static std::expected<GrantSigningKey, SessionGrantError>
|
|
generate(DeploymentEnvironment environment, std::uint32_t key_id);
|
|
[[nodiscard]] static std::expected<GrantSigningKey, SessionGrantError>
|
|
import(DeploymentEnvironment environment, std::uint32_t key_id,
|
|
std::span<const std::uint8_t> private_key);
|
|
GrantSigningKey(GrantSigningKey&& other) noexcept;
|
|
GrantSigningKey& operator=(GrantSigningKey&& other) noexcept;
|
|
GrantSigningKey(const GrantSigningKey&) = delete;
|
|
GrantSigningKey& operator=(const GrantSigningKey&) = delete;
|
|
~GrantSigningKey();
|
|
// Structural checks only (nonzero ids, serial and scope, ordered times).
|
|
[[nodiscard]] std::expected<SessionGrant, SessionGrantError>
|
|
sign(const GrantClaims& claims) const;
|
|
[[nodiscard]] std::expected<GrantPublicKey, SessionGrantError> public_key() const;
|
|
[[nodiscard]] DeploymentEnvironment environment() const noexcept {
|
|
return environment_;
|
|
}
|
|
[[nodiscard]] std::uint32_t key_id() const noexcept {
|
|
return key_id_;
|
|
}
|
|
|
|
private:
|
|
GrantSigningKey(DeploymentEnvironment environment, std::uint32_t key_id, std::uint32_t key)
|
|
: environment_{environment}, key_id_{key_id}, key_{key} {}
|
|
DeploymentEnvironment environment_;
|
|
std::uint32_t key_id_;
|
|
std::uint32_t key_; // PSA key id; zero when moved-from.
|
|
};
|
|
|
|
// Region-side verifier for one environment with up to 8 issuer public keys (rotation).
|
|
class GrantVerifier {
|
|
public:
|
|
static constexpr std::size_t max_keys = 8;
|
|
[[nodiscard]] static std::expected<GrantVerifier, SessionGrantError>
|
|
create(DeploymentEnvironment environment);
|
|
GrantVerifier(GrantVerifier&& other) noexcept;
|
|
GrantVerifier& operator=(GrantVerifier&& other) noexcept;
|
|
GrantVerifier(const GrantVerifier&) = delete;
|
|
GrantVerifier& operator=(const GrantVerifier&) = delete;
|
|
~GrantVerifier();
|
|
[[nodiscard]] std::expected<void, SessionGrantError>
|
|
add_key(std::uint32_t key_id, std::span<const std::uint8_t> public_key);
|
|
// Signature first: no claim byte is trusted before it verifies.
|
|
[[nodiscard]] std::expected<VerifiedSessionGrant, SessionGrantError>
|
|
verify(std::span<const std::uint8_t> grant) const;
|
|
|
|
private:
|
|
struct Key {
|
|
std::uint32_t key_id{}, key{};
|
|
};
|
|
explicit GrantVerifier(DeploymentEnvironment environment) : environment_{environment} {}
|
|
void destroy() noexcept;
|
|
DeploymentEnvironment environment_;
|
|
std::vector<Key> keys_;
|
|
};
|
|
|
|
// Cryptographically random bytes (PSA); used for account and grant ids.
|
|
[[nodiscard]] std::expected<void, SessionGrantError> random_bytes(std::span<std::uint8_t> out);
|
|
[[nodiscard]] std::expected<AccountId, SessionGrantError> generate_account_id();
|
|
} // namespace evo::network
|