Authenticate advertised QUIC provider routes
This commit is contained in:
parent
36a822d914
commit
1742473845
4 changed files with 60 additions and 3 deletions
|
|
@ -4,6 +4,7 @@ using System.Net;
|
|||
using System.Net.Quic;
|
||||
using System.Net.Security;
|
||||
using System.Security.Authentication;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Channels;
|
||||
|
|
@ -81,8 +82,13 @@ public sealed class CultMeshQuicRealtimeTransportConnector : ICultMeshRealtimeTr
|
|||
ApplicationProtocols = new List<SslApplicationProtocol> { _options.ApplicationProtocol },
|
||||
EnabledSslProtocols = SslProtocols.Tls13,
|
||||
RemoteCertificateValidationCallback = (_, certificate, chain, errors) =>
|
||||
validator?.Invoke(endpointId, certificate as X509Certificate2, chain, errors) ??
|
||||
errors == SslPolicyErrors.None
|
||||
{
|
||||
var providerCertificate = certificate as X509Certificate2 ??
|
||||
(certificate == null ? null : new X509Certificate2(certificate));
|
||||
return validator?.Invoke(endpointId, providerCertificate, chain, errors) ??
|
||||
errors == SslPolicyErrors.None ||
|
||||
MatchesAdvertisedCertificatePin(candidate.Endpoint, providerCertificate);
|
||||
}
|
||||
}
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
|
|
@ -101,6 +107,29 @@ public sealed class CultMeshQuicRealtimeTransportConnector : ICultMeshRealtimeTr
|
|||
port = uri.Port;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool MatchesAdvertisedCertificatePin(string endpoint, X509Certificate2? certificate)
|
||||
{
|
||||
if (certificate == null || !Uri.TryCreate(endpoint, UriKind.Absolute, out var uri))
|
||||
return false;
|
||||
var advertised = ParseQueryValue(uri.Query, "cert-sha256");
|
||||
if (string.IsNullOrWhiteSpace(advertised)) return false;
|
||||
var actual = Convert.ToHexString(SHA256.HashData(certificate.RawData));
|
||||
return string.Equals(advertised, actual, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private static string? ParseQueryValue(string query, string key)
|
||||
{
|
||||
foreach (var component in query.TrimStart('?').Split('&', StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
var separator = component.IndexOf('=');
|
||||
var candidateKey = separator < 0 ? component : component[..separator];
|
||||
if (!string.Equals(Uri.UnescapeDataString(candidateKey), key, StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
return separator < 0 ? string.Empty : Uri.UnescapeDataString(component[(separator + 1)..]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Configures an authenticated CultMesh QUIC realtime listener.</summary>
|
||||
|
|
|
|||
|
|
@ -37,6 +37,10 @@ await server.BroadcastAsync(new CultMeshRealtimeFrame
|
|||
Advertise the listener as `cultmesh-state+quic://host:port`. Certificate
|
||||
identity and provider identity must be bound by the application trust policy;
|
||||
accepting every certificate is appropriate only in isolated tests.
|
||||
For directly trusted discovery records, a provider may append its uppercase
|
||||
SHA-256 certificate fingerprint as `?cert-sha256=...`. The default connector
|
||||
accepts a self-signed certificate only when its raw certificate bytes match that
|
||||
advertised pin. A caller-supplied validator remains authoritative when present.
|
||||
|
||||
## Consumer
|
||||
|
||||
|
|
|
|||
|
|
@ -73,7 +73,9 @@ adapter.
|
|||
The QUIC adapter always uses TLS 1.3. Its certificate validation callback is
|
||||
given the advertised endpoint identity so the consumer can bind transport
|
||||
authentication to provider authorization. Test-only trust-all callbacks are not
|
||||
a release configuration.
|
||||
a release configuration. A trusted discovery record may carry
|
||||
`cert-sha256=<fingerprint>` on its QUIC route; absent a caller validator, the
|
||||
connector accepts a self-signed provider certificate only when that pin matches.
|
||||
|
||||
## Local parity evidence
|
||||
|
||||
|
|
|
|||
|
|
@ -46,6 +46,28 @@ public sealed class CultMeshQuicRealtimeTransportTests
|
|||
client.TransportId.Should().Be("msquic-realtime");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AdvertisedCertificatePinAuthenticatesASelfSignedProvider()
|
||||
{
|
||||
if (!QuicListener.IsSupported) Assert.Ignore("MsQuic is unavailable on this test host.");
|
||||
using var certificate = CreateCertificate();
|
||||
await using var server = await CultMeshQuicRealtimeServer.ListenAsync(new CultMeshQuicRealtimeServerOptions
|
||||
{
|
||||
ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0),
|
||||
ServerCertificate = certificate
|
||||
});
|
||||
var pin = Convert.ToHexString(SHA256.HashData(certificate.RawData));
|
||||
var endpoint = $"{CultMeshQuicRealtimeTransportConnector.Scheme}://127.0.0.1:{server.LocalEndPoint.Port}?cert-sha256={pin}";
|
||||
var connector = new CultMeshQuicRealtimeTransportConnector();
|
||||
|
||||
using var client = await connector.ConnectAsync(
|
||||
new CultMeshTransportCandidate(endpoint),
|
||||
CultMeshEndpointId.Parse("service:aetheria.daemon"));
|
||||
|
||||
client.TransportId.Should().Be("msquic-realtime");
|
||||
await WaitUntilAsync(() => server.ConnectionCount == 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReliableOrderedFramesShareAnOrderedStreamInBothDirections()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue