Authenticate CultMesh authority routes and replay operations

This commit is contained in:
Meta Vens 2026-08-18 19:02:46 +02:00
commit f622ee1b24
30 changed files with 1432 additions and 142 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using GameCult.Networking;
@ -68,6 +69,7 @@ namespace GameCult.Mesh
private readonly HashSet<string> _verseIds;
private readonly HashSet<string> _protocolIds;
private readonly HashSet<string> _routeGenerations;
private readonly Dictionary<string, CultMeshSessionProofSigner> _proofSigners;
private readonly Func<CultMeshSessionOpenMessage, ICultNetSchemaServerPeer, Task> _handler;
private bool _disposed;
@ -76,13 +78,16 @@ namespace GameCult.Mesh
string authorityRuntimeId,
IEnumerable<string> verseIds,
IEnumerable<string> protocolIds,
IEnumerable<string> routeGenerations)
IEnumerable<string> routeGenerations,
IEnumerable<CultMeshSessionProofSigner>? proofSigners = null)
{
_server = server ?? throw new ArgumentNullException(nameof(server));
_authorityRuntimeId = Require(authorityRuntimeId, nameof(authorityRuntimeId));
_verseIds = Clean(verseIds, nameof(verseIds));
_protocolIds = Clean(protocolIds, nameof(protocolIds));
_routeGenerations = Clean(routeGenerations, nameof(routeGenerations));
_proofSigners = (proofSigners ?? Array.Empty<CultMeshSessionProofSigner>())
.ToDictionary(signer => signer.Route.Generation, StringComparer.Ordinal);
_handler = HandleAsync;
_server.OnCultNet(_handler);
}
@ -97,6 +102,7 @@ namespace GameCult.Mesh
private Task HandleAsync(CultMeshSessionOpenMessage request, ICultNetSchemaServerPeer peer)
{
var error = Validate(request);
_proofSigners.TryGetValue(request.RouteGeneration ?? string.Empty, out var signer);
peer.SendCultNet(new CultMeshSessionAcceptedMessage
{
MessageId = request.MessageId ?? string.Empty,
@ -105,6 +111,9 @@ namespace GameCult.Mesh
AuthorityRuntimeId = _authorityRuntimeId,
ProtocolId = request.ProtocolId ?? string.Empty,
RouteGeneration = request.RouteGeneration ?? string.Empty,
ClientNonce = request.ClientNonce ?? string.Empty,
ProviderKeyId = error == null ? signer?.ProviderKeyId ?? string.Empty : string.Empty,
ProviderSignature = error == null && signer != null ? signer.Sign(request) : string.Empty,
Error = error
});
return Task.CompletedTask;
@ -114,6 +123,7 @@ namespace GameCult.Mesh
{
if (string.IsNullOrWhiteSpace(request.MessageId)) return "session-message-id-required";
if (string.IsNullOrWhiteSpace(request.SourceRuntimeId)) return "source-runtime-id-required";
if (!TryNonce(request.ClientNonce)) return "client-nonce-invalid";
if (!string.Equals(request.AuthorityRuntimeId, _authorityRuntimeId, StringComparison.Ordinal))
return "target-runtime-mismatch";
if (!_verseIds.Contains(request.VerseId ?? string.Empty)) return "verse-not-served";
@ -122,6 +132,13 @@ namespace GameCult.Mesh
return null;
}
private static bool TryNonce(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return false;
try { return Convert.FromBase64String(value).Length == 32; }
catch (FormatException) { return false; }
}
private static HashSet<string> Clean(IEnumerable<string> values, string name)
{
if (values == null) throw new ArgumentNullException(name);
@ -145,26 +162,45 @@ namespace GameCult.Mesh
string sourceRuntimeId,
CultMeshSessionTarget target,
CultMeshProtocolId protocol,
string routeGeneration,
CultMeshTransportCandidate candidate,
CultMeshAuthorityTrustPolicy? trust,
DateTimeOffset now,
TimeSpan timeout,
CancellationToken cancellationToken = default)
{
if (client == null) throw new ArgumentNullException(nameof(client));
if (candidate == null) throw new ArgumentNullException(nameof(candidate));
var route = candidate.AuthorityRoute ?? new CultMeshAuthorityRoute(
target.AuthorityRuntimeId,
candidate.Endpoint,
new[] { protocol.Value },
candidate.Priority,
candidate.Generation);
var verifiedRoute = candidate.VerifiedAuthority ??
(trust ?? throw new CultMeshSessionException(new CultMeshSessionFailure(
CultMeshSessionFailureReason.Authentication,
"No consumer trust policy verified this CultMesh route.",
candidate.Endpoint))).Verify(target.VerseId, route, now);
if (client is ICultMeshVerifiedSchemaClient verified)
{
if (!verified.IsVerifiedFor(
target.VerseId,
target.AuthorityRuntimeId,
protocol.Value,
routeGeneration ?? string.Empty))
candidate.Generation))
{
throw new CultMeshSessionException(new CultMeshSessionFailure(
CultMeshSessionFailureReason.Authority,
$"Native transport identity did not match CultMesh target '{target}' for '{protocol}'."));
}
return;
// Native identity is sufficient only for an explicitly selected
// loopback development route. Remote schema sessions still prove
// possession of the provider key certified by Odin.
if (verifiedRoute.IsLocalDevelopment) return;
}
var messageId = Guid.NewGuid().ToString("N");
var nonce = new byte[32];
using (var random = RandomNumberGenerator.Create()) random.GetBytes(nonce);
var completion = new TaskCompletionSource<CultMeshSessionAcceptedMessage>(
TaskCreationOptions.RunContinuationsAsynchronously);
client.OnCultNet<CultMeshSessionAcceptedMessage>(response =>
@ -172,15 +208,17 @@ namespace GameCult.Mesh
if (string.Equals(response.MessageId, messageId, StringComparison.Ordinal))
completion.TrySetResult(response);
});
client.SendCultNet(new CultMeshSessionOpenMessage
var request = new CultMeshSessionOpenMessage
{
MessageId = messageId,
SourceRuntimeId = sourceRuntimeId,
VerseId = target.VerseId,
AuthorityRuntimeId = target.AuthorityRuntimeId,
ProtocolId = protocol.Value,
RouteGeneration = routeGeneration ?? string.Empty
});
RouteGeneration = candidate.Generation,
ClientNonce = Convert.ToBase64String(nonce)
};
client.SendCultNet(request);
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
deadline.CancelAfter(timeout);
@ -201,7 +239,8 @@ namespace GameCult.Mesh
!string.Equals(accepted.VerseId, target.VerseId, StringComparison.Ordinal) ||
!string.Equals(accepted.AuthorityRuntimeId, target.AuthorityRuntimeId, StringComparison.Ordinal) ||
!string.Equals(accepted.ProtocolId, protocol.Value, StringComparison.Ordinal) ||
!string.Equals(accepted.RouteGeneration, routeGeneration ?? string.Empty, StringComparison.Ordinal))
!string.Equals(accepted.RouteGeneration, candidate.Generation, StringComparison.Ordinal) ||
!CultMeshAuthorityProof.VerifySession(request, accepted, verifiedRoute))
{
throw new CultMeshSessionException(new CultMeshSessionFailure(
CultMeshSessionFailureReason.Authority,