#include "evo/identity/identity.hpp" #include "evo/network/dev_grant.hpp" #include "evo/network/session_control.hpp" #include "evo/network/session_grant.hpp" #include "evo/network/session_protocol.hpp" #include "network_test.hpp" #include #include #include #include #include namespace evo::network_test { namespace { using namespace evo::network; using evo::identity::IdentityError; constexpr world::WorldIdentity world_id{7, 3}; constexpr std::uint64_t region = 11; constexpr std::uint64_t now = 1'800'000'000; AccountId account(std::uint8_t tag) { AccountId a; a.bytes.fill(tag); return a; } GrantClaims claims(const AccountId& who, std::uint64_t serial, std::uint64_t issued = now, std::uint64_t lifetime = 60) { GrantClaims c; c.grant_id = 1000 + serial; c.account = who; c.serial = serial; c.world = world_id; c.region = region; c.issued_at = issued; c.expires_at = issued + lifetime; return c; } std::span bytes(std::string_view text) { return {reinterpret_cast(text.data()), text.size()}; } // One environment's issuer key and region verifier. struct Keys { explicit Keys(DeploymentEnvironment e, std::uint32_t key_id = 1) : signer{*GrantSigningKey::generate(e, key_id)}, verifier{*GrantVerifier::create(e)} { CHECK(verifier.add_key(key_id, *signer.public_key())); } VerifiedSessionGrant verified(const GrantClaims& c) const { auto grant = signer.sign(c); CHECK(grant); auto v = verifier.verify(*grant); CHECK(v); return *v; } GrantSigningKey signer; GrantVerifier verifier; }; ControlConfig config() { ControlConfig c; c.environment = DeploymentEnvironment::dev; c.world = world_id; c.region = region; c.epoch = 5; c.max_sessions = 4; c.max_accounts = 8; return c; } void grants() { Keys dev{DeploymentEnvironment::dev}; const auto who = account(0x42); const auto grant = *dev.signer.sign(claims(who, 1)); const auto v = dev.verifier.verify(grant); CHECK(v && v->claims().account == who && v->claims().serial == 1 && v->claims().world == world_id && v->claims().region == region && v->environment() == DeploymentEnvironment::dev); // Every signed byte matters, including the key id and environment. for (std::size_t i = 0; i < session_grant_bytes; ++i) { auto tampered = grant; tampered.at(i) ^= 0x01; const auto r = dev.verifier.verify(tampered); CHECK(!r); CHECK(r.error() == (i >= 20 && i < 24 ? SessionGrantError::unknown_key : SessionGrantError::bad_signature)); } CHECK(refused(dev.verifier.verify(std::span{grant}.first(159)), SessionGrantError::malformed)); // A dev-signed grant never verifies in stage or World0, even if a dev public key were // misconfigured there: the environment byte is signed. for (const auto e : {DeploymentEnvironment::stage, DeploymentEnvironment::world0}) { auto other = *GrantVerifier::create(e); CHECK(refused(other.verify(grant), SessionGrantError::unknown_key)); CHECK(other.add_key(1, *dev.signer.public_key())); CHECK(refused(other.verify(grant), SessionGrantError::wrong_environment)); } // M9 dev grants are not session grants. std::array secret{}; secret.fill(9); const auto dev_key = *DevGrantKey::import(DeploymentEnvironment::dev, secret); DevGrantClaims dc; dc.grant_id = 1; dc.subject = 2; dc.world = world_id; dc.region = region; dc.issued_at = now; dc.expires_at = now + 60; CHECK(refused(dev.verifier.verify(*dev_key.issue(dc)), SessionGrantError::malformed)); // Rotation: a second key verifies alongside; ids are unique; at most 8 keys. Keys rotated{DeploymentEnvironment::dev, 2}; CHECK(dev.verifier.add_key(2, *rotated.signer.public_key())); CHECK(dev.verifier.verify(*rotated.signer.sign(claims(who, 2)))); CHECK(refused(dev.verifier.add_key(2, *rotated.signer.public_key()), SessionGrantError::duplicate_key)); for (std::uint32_t id = 3; id <= GrantVerifier::max_keys; ++id) CHECK(dev.verifier.add_key(id, *rotated.signer.public_key())); CHECK(refused(dev.verifier.add_key(99, *rotated.signer.public_key()), SessionGrantError::too_many_keys)); // A grant signed by key 2 but labelled key 1 fails (the label is signed too). auto relabelled = *rotated.signer.sign(claims(who, 3)); relabelled.at(20) = 1; CHECK(refused(dev.verifier.verify(relabelled), SessionGrantError::bad_signature)); GrantPublicKey not_a_point{}; not_a_point.fill(0x04); auto fresh = *GrantVerifier::create(DeploymentEnvironment::dev); CHECK(refused(fresh.add_key(1, not_a_point), SessionGrantError::invalid_key)); CHECK(refused(fresh.add_key(0, *dev.signer.public_key()), SessionGrantError::invalid_key)); // Structural claims and key import. auto bad = claims(who, 1); bad.serial = 0; CHECK(refused(dev.signer.sign(bad), SessionGrantError::invalid_claims)); bad = claims(who, 1); bad.account = {}; CHECK(refused(dev.signer.sign(bad), SessionGrantError::invalid_claims)); std::array scalar{}; scalar.back() = 7; auto imported = GrantSigningKey::import(DeploymentEnvironment::stage, 4, scalar); CHECK(imported); auto stage = *GrantVerifier::create(DeploymentEnvironment::stage); CHECK(stage.add_key(4, *imported->public_key())); CHECK(stage.verify(*imported->sign(claims(who, 1)))); CHECK(refused( GrantSigningKey::import(DeploymentEnvironment::stage, 4, std::span{scalar}.first(31)), SessionGrantError::invalid_key)); scalar.fill(0); CHECK(refused(GrantSigningKey::import(DeploymentEnvironment::stage, 4, scalar), SessionGrantError::invalid_key)); // Moved-from keys are inert. auto moved = std::move(imported).value(); CHECK(refused(imported->sign(claims(who, 1)), // NOLINT(bugprone-use-after-move) SessionGrantError::invalid_key)); CHECK(moved.sign(claims(who, 1))); } void identity_adapter() { using namespace evo::identity; for (const auto e : {DeploymentEnvironment::stage, DeploymentEnvironment::world0}) CHECK(refused(DevTestProvider::create(e), IdentityError::not_dev_environment)); auto provider = *DevTestProvider::create(DeploymentEnvironment::dev); CHECK(provider.authenticate(bytes("dev-test:alice"))->subject == "alice"); for (const auto bad : {"alice", "dev-test:", "dev-test:Alice", "dev-test:a b", "dev-test:0123456789abcdef0123456789abcdefX", "steam:alice"}) CHECK(refused(provider.authenticate(bytes(bad)), IdentityError::unauthenticated)); MemoryAccountDirectory directory{2}; const ProviderSubject alice{ProviderKind::dev_test, "alice"}; const auto a1 = *directory.resolve(alice); CHECK(a1.valid() && *directory.resolve(alice) == a1); const auto b = *directory.resolve({ProviderKind::dev_test, "bob"}); CHECK(b != a1); CHECK(refused(directory.resolve({ProviderKind::dev_test, "carol"}), IdentityError::directory_full)); const auto s1 = *directory.next_grant(a1, now); const auto s2 = *directory.next_grant(a1, now - 100); // Clock went back. CHECK(s1.serial == 1 && s2.serial == 2 && s2.issued_at == now); // End to end: credential -> account -> signed grant -> region control. Keys dev{DeploymentEnvironment::dev}; MemoryAccountDirectory accounts{16}; auto issuer = GrantIssuer::create(std::move(dev.signer), provider, accounts, 60); CHECK(issuer); auto control = *SessionControl::create(config()); const auto g1 = issuer->issue(bytes("dev-test:alice"), world_id, region, now); const auto opened = control.open(*dev.verifier.verify(*g1), now); CHECK(opened && opened->superseded == 0); const auto g2 = issuer->issue(bytes("dev-test:alice"), world_id, region, now + 1); const auto again = control.open(*dev.verifier.verify(*g2), now + 1); CHECK(again && again->superseded == opened->session); CHECK(refused(issuer->issue(bytes("nobody"), world_id, region, now), IdentityError::unauthenticated)); CHECK(refused(issuer->issue(bytes("dev-test:alice"), {0, 0}, region, now), IdentityError::invalid_request)); // A dev provider cannot drive a stage or World0 key. for (const auto e : {DeploymentEnvironment::stage, DeploymentEnvironment::world0}) CHECK(refused(GrantIssuer::create(*GrantSigningKey::generate(e, 1), provider, accounts, 60), IdentityError::wrong_environment)); CHECK(refused(GrantIssuer::create(*GrantSigningKey::generate(DeploymentEnvironment::dev, 1), provider, accounts, 0), IdentityError::invalid_request)); } void control_rules() { Keys dev{DeploymentEnvironment::dev}; const auto alice = account(1), bob = account(2); { auto bad = config(); bad.max_accounts = 2; // Fewer than sessions. CHECK(refused(SessionControl::create(bad), ControlError::invalid_config)); bad = config(); bad.max_grant_lifetime = 3601; CHECK(refused(SessionControl::create(bad), ControlError::invalid_config)); } // Scope and time. { auto control = *SessionControl::create(config()); auto c = claims(alice, 1); c.world = {7, 4}; CHECK(refused(control.open(dev.verified(c), now), ControlError::wrong_world)); c = claims(alice, 1); c.region = 12; CHECK(refused(control.open(dev.verified(c), now), ControlError::wrong_region)); CHECK(refused(control.open(dev.verified(claims(alice, 1, now, 301)), now), ControlError::invalid_lifetime)); CHECK(refused(control.open(dev.verified(claims(alice, 1, now + 31)), now), ControlError::not_yet_valid)); CHECK(control.open(dev.verified(claims(alice, 1, now + 30)), now)); CHECK(refused(control.open(dev.verified(claims(bob, 1)), now + 60), ControlError::expired)); // Grants verified for another environment are refused by this region. Keys stage{DeploymentEnvironment::stage}; CHECK(refused(control.open(stage.verified(claims(bob, 1)), now), ControlError::wrong_environment)); } // Replay, duplicate login (newest wins) and stolen stale grants. { auto c = config(); c.max_account_attempts = 16; // Rate limits are tested separately. auto control = *SessionControl::create(c); const auto first = dev.verified(claims(alice, 5)); const auto s1 = control.open(first, now); CHECK(s1 && s1->superseded == 0 && s1->epoch == 5 && s1->expires_at == now + 60); CHECK(refused(control.open(first, now + 1), ControlError::stale_grant)); // Replay. // An older, unexpired grant (e.g. stolen earlier) cannot take control. CHECK(refused(control.open(dev.verified(claims(alice, 4)), now + 1), ControlError::stale_grant)); CHECK(control.holder(alice) == s1->session); const auto s2 = control.open(dev.verified(claims(alice, 6)), now + 2); CHECK(s2 && s2->superseded == s1->session && s2->session != s1->session); CHECK(control.holder(alice) == s2->session && control.sessions() == 1); CHECK(!control.account_of(s1->session)); CHECK(refused(control.close(s1->session), ControlError::unknown_session)); // After close, the stale serial stays refused until every older grant expired. CHECK(control.close(s2->session)); CHECK(refused(control.open(dev.verified(claims(alice, 6)), now + 3), ControlError::stale_grant)); } // Rate limits: account attempts (every verified attempt counts) and region attempts. { auto c = config(); c.max_account_attempts = 3; c.max_region_attempts = 5; c.attempt_window = 10; auto control = *SessionControl::create(c); const auto replayed = dev.verified(claims(alice, 1)); CHECK(control.open(replayed, now)); CHECK(refused(control.open(replayed, now), ControlError::stale_grant)); CHECK(refused(control.open(replayed, now), ControlError::stale_grant)); CHECK( refused(control.open(dev.verified(claims(alice, 2)), now), ControlError::rate_limited)); CHECK(control.open(dev.verified(claims(alice, 2)), now + 10)); // New window. for (int i = 0; i < 5; ++i) CHECK(control.attempt(now)); CHECK(refused(control.attempt(now), ControlError::rate_limited)); CHECK(refused(control.attempt(now + 9), ControlError::rate_limited)); CHECK(control.attempt(now + 10)); } // Capacity: a refused open does not consume the grant; accounts fail closed. { auto c = config(); c.max_sessions = 1; c.max_accounts = 2; auto control = *SessionControl::create(c); const auto a = control.open(dev.verified(claims(alice, 1)), now); const auto bob_grant = dev.verified(claims(bob, 1)); CHECK(refused(control.open(bob_grant, now), ControlError::sessions_full)); CHECK(control.close(a->session)); CHECK(control.open(bob_grant, now)); // Same grant, now admitted. CHECK(refused(control.open(dev.verified(claims(account(3), 1)), now), ControlError::accounts_full)); // Idle accounts are forgotten only after their grants and windows end. CHECK(refused(control.open(dev.verified(claims(account(3), 1, now + 299)), now + 299), ControlError::accounts_full)); const auto later = dev.verified(claims(account(3), 1, now + 300)); CHECK(refused(control.open(later, now + 300), ControlError::sessions_full)); // Bob holds. } // Refresh, expiry and epochs. { auto control = *SessionControl::create(config()); const auto a = control.open(dev.verified(claims(alice, 1)), now); const auto b = control.open(dev.verified(claims(bob, 1)), now); CHECK(control.refresh(a->session, dev.verified(claims(alice, 2, now + 50)), now + 50) == now + 110); CHECK(refused(control.refresh(a->session, dev.verified(claims(alice, 2)), now + 51), ControlError::stale_grant)); CHECK(refused(control.refresh(a->session, dev.verified(claims(bob, 2)), now + 51), ControlError::wrong_account)); CHECK(refused(control.refresh(999, dev.verified(claims(alice, 3)), now + 51), ControlError::unknown_session)); const auto expired = control.expire(now + 60); CHECK(expired == std::vector{b->session}); CHECK(control.holder(alice) == a->session && !control.holder(bob)); const auto c2 = control.open(dev.verified(claims(bob, 2, now + 60)), now + 60); auto closed = control.advance_epoch(6); CHECK(closed && closed->size() == 2 && control.sessions() == 0 && control.epoch() == 6); CHECK(std::ranges::find(*closed, c2->session) != closed->end()); CHECK(refused(control.advance_epoch(6), ControlError::invalid_config)); const auto reopened = control.open(dev.verified(claims(alice, 3, now + 61)), now + 61); CHECK(reopened && reopened->epoch == 6 && reopened->superseded == 0); } // Redaction: the log label carries only the first four id bytes. AccountId labelled; for (std::size_t i = 0; i < labelled.bytes.size(); ++i) labelled.bytes.at(i) = static_cast(0xA0 + i); CHECK(log_label(labelled) == "acct-a0a1a2a3"); } void session_codec() { WireGrant grant{}; for (std::size_t i = 0; i < grant.size(); ++i) grant.at(i) = static_cast(i * 7); SessionWelcome welcome{1, 2, 3, 4, 5, 6, 7}; const std::vector messages{ SessionHello{0, grant}, welcome, SessionRefresh{grant}, SessionRefreshResult{0, 99}, SessionRefreshResult{static_cast(SessionCloseReason::stale_grant), 0}, SessionClose{SessionCloseReason::superseded}, SessionClose{SessionCloseReason::shutdown}}; for (const auto& m : messages) { const auto bytes = encode_session(m); CHECK(bytes.at(0) == 'E' && bytes.at(1) == 'V' && bytes.at(2) == 'S' && bytes.at(3) == 'N'); const auto back = decode_session(bytes); CHECK(back && back->index() == m.index()); CHECK(encode_session(*back) == bytes); // Every truncation and any extension is refused. for (std::size_t n = 0; n < bytes.size(); ++n) { const auto r = decode_session(std::span{bytes}.first(n)); CHECK(!r && r.error() == (n < session_header_bytes ? SessionCodecError::truncated : SessionCodecError::wrong_length)); } auto longer = bytes; longer.push_back(0); CHECK(refused(decode_session(longer), SessionCodecError::wrong_length)); auto mutated = bytes; mutated.at(0) ^= 1; CHECK(refused(decode_session(mutated), SessionCodecError::bad_magic)); mutated = bytes; mutated.at(4) = 2; CHECK(refused(decode_session(mutated), SessionCodecError::unsupported_version)); mutated = bytes; mutated.at(7) = 1; CHECK(refused(decode_session(mutated), SessionCodecError::reserved_flags)); for (const auto type : std::array{0, 6, 255}) { mutated = bytes; mutated.at(6) = type; CHECK(refused(decode_session(mutated), SessionCodecError::unknown_type)); } } CHECK(std::get(*decode_session(encode_session(welcome))) == welcome); CHECK(std::get(*decode_session(encode_session(SessionHello{0, grant}))).grant == grant); for (const auto reason : std::array{0, 14, 200, 255}) { auto close = encode_session(SessionClose{SessionCloseReason::protocol}); close.back() = reason; CHECK(refused(decode_session(close), SessionCodecError::invalid_value)); if (reason != 0) { auto result = encode_session(SessionRefreshResult{0, 0}); result.at(session_header_bytes) = reason; CHECK(refused(decode_session(result), SessionCodecError::invalid_value)); } } for (const auto zero : {0, 1}) { auto w = welcome; (zero == 0 ? w.session : w.epoch) = 0; CHECK(refused(decode_session(encode_session(w)), SessionCodecError::invalid_value)); } } } // namespace void control_tests() { grants(); identity_adapter(); control_rules(); session_codec(); } } // namespace evo::network_test