Path: redb-route/demo/As4ConformanceDemo/ConformanceModule/ConformanceSettings.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/demo/As4ConformanceDemo/ConformanceModule/ConformanceSettings.cs
----
using System.Globalization;
using System.Security.Cryptography.X509Certificates;
using redb.Route.Abstractions; // IRouteContext
using redb.Route.As4;
namespace ConformanceModule;
///
/// What the eDelivery support team hands over at registration, and where this module listens. The values come from
/// the route context (), so nothing of one test account is compiled into the module.
///
public sealed class ConformanceSettings
{
/// eb:PartyId/@type of every party of the conformance tests.
public const string UnregisteredPartyIdType = "urn:oasis:names:tc:ebcore:partyid-type:unregistered";
/// Party id of the access point under test, as assigned by the support team.
public string SutPartyId { get; init; } = "system_under_test";
/// Party id of the test driver in the message exchanges under test.
public string TestDriverPartyId { get; init; } = "test_driver";
/// Our party id on the command channel (Submit, Deliver, Notify).
public string ControllerPartyId { get; init; } = "sut_controller_domain";
/// Party id of the test driver on the command channel.
public string TestDriverControllerPartyId { get; init; } = "test_driver_sut_controller_domain";
/// MSH address of the test driver for the messages under test.
public required string TestDriverUrl { get; init; }
/// MSH address of the test driver for Deliver and Notify commands.
public required string TestDriverControllerUrl { get; init; }
/// Port both receive endpoints listen on (one shared host).
public int Port { get; init; } = 4090;
/// Path of the access point under test.
public string SutPath { get; init; } = "/as4/sut";
/// Path that receives Submit commands.
public string ControllerPath { get; init; } = "/as4/controller";
/// Host part of the message ids we generate.
public string ExternalHostName { get; init; } = "redb.route";
/// The SUT key: signs our messages and receipts, decrypts what the test driver encrypts for us.
public required X509Certificate2 SutKey { get; init; }
/// The test driver's certificate: verifies its signatures, and payloads are encrypted for it.
public required X509Certificate2 TestDriverCertificate { get; init; }
///
/// Certificate of the test driver on the command channel. The conformance testing service signs Submit, Deliver and
/// Notify with a certificate of its own (test_driver_sut_controller); null: ,
/// as a driver with one key has it.
///
public X509Certificate2? TestDriverControllerCertificate { get; init; }
///
/// Redeliveries of the ONEWAY_RETRY profile. The test driver takes 5 transmissions, the message and 4
/// redeliveries, and then expects the Notify (AS4_CP_TA15); a sixth transmission fails the test.
///
public int RetryCount { get; init; } = 4;
/// Delay between those redeliveries; the user guide asks for less than 3 minutes.
public TimeSpan RetryDelay { get; init; } = TimeSpan.FromSeconds(20);
///
/// Where the ids of received messages are remembered: memory, lost with the process, or redb, the
/// worker's redb, so that a resend is still recognised after a restart and by every node of a cluster.
///
public string DuplicateStore { get; init; } = "memory";
/// How long a received message id is remembered.
public TimeSpan DuplicateWindow { get; init; } = TimeSpan.FromDays(7);
/// Milliseconds to wait for the test driver's response with the receipt.
public int SendTimeout { get; init; } = 60000;
///
/// The certification authorities above : sent with it as its certification path on the PKI path
/// legs. Empty when the SUT certificate is its own authority.
///
public IReadOnlyList SutChain { get; init; } = [];
///
/// The authorities the test driver's signing certificate must chain to on the PKI path legs. Empty: the test
/// driver's certificate itself, as its own authority.
///
public IReadOnlyList TestDriverTrustAnchors { get; init; } = [];
/// Certificate policy OID required on the leg bstPkiPathSutC3PolicyFail; that leg is left out when null.
public string? PkiPathPolicy { get; init; }
/// Service and action pairs added to : service|action;service|action.
public string? ExtraLegs { get; init; }
///
/// Reads the settings from the route context, the way the Tsak worker provides them: the module's
/// ConformanceModule.config.json gives the defaults, and what differs per test account arrives through the
/// Override layer, environment variables on the worker such as
/// Tsak__Contexts__as4-conformance__Override__TestDriver__Url. Every root key is a context property; a
/// section is a dictionary.
///
public static ConformanceSettings FromContext(IRouteContext context)
{
ArgumentNullException.ThrowIfNull(context);
IDictionary Section(string name) =>
context.GetProperty>(name) ?? new Dictionary();
static string? Value(IDictionary section, string key) =>
section.TryGetValue(key, out var value) && Convert.ToString(value, CultureInfo.InvariantCulture) is { Length: > 0 } text ? text : null;
string Need(IDictionary section, string sectionName, string key) => Value(section, key)
?? throw new InvalidOperationException(
$"{sectionName}:{key} is not configured for context '{context.ContextId}': set it in ConformanceModule.config.json, " +
$"or, for a value of one test account, in Tsak:Contexts:{context.ContextId}:Override (see demos/As4ConformanceDemo/README.md).");
// Certificate files separated by ";".
static IReadOnlyList Certificates(string? files) =>
(files ?? "").Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(X509CertificateLoader.LoadCertificateFromFile).ToList();
static int Int(string text) => int.Parse(text, CultureInfo.InvariantCulture);
var driver = Section("TestDriver");
var sut = Section("Sut");
var receive = Section("Receive");
var retry = Section("Retry");
var pkiPath = Section("PkiPath");
var url = Need(driver, "TestDriver", "Url");
return new ConformanceSettings
{
TestDriverUrl = url,
TestDriverControllerUrl = Value(driver, "ControllerUrl") ?? url,
TestDriverPartyId = Need(driver, "TestDriver", "PartyId"),
TestDriverControllerPartyId = Need(driver, "TestDriver", "ControllerPartyId"),
TestDriverCertificate = X509CertificateLoader.LoadCertificateFromFile(Need(driver, "TestDriver", "Certificate")),
TestDriverControllerCertificate = Value(driver, "ControllerCertificate") is { } controllerCertificate
? X509CertificateLoader.LoadCertificateFromFile(controllerCertificate)
: null,
TestDriverTrustAnchors = Certificates(Value(driver, "TrustAnchors")),
SutPartyId = Need(sut, "Sut", "PartyId"),
ControllerPartyId = Need(sut, "Sut", "ControllerPartyId"),
SutKey = X509CertificateLoader.LoadPkcs12FromFile(Need(sut, "Sut", "Pfx"), Value(sut, "PfxPassword")),
SutChain = Certificates(Value(sut, "Chain")),
ExternalHostName = Need(sut, "Sut", "HostName"),
Port = Int(Need(receive, "Receive", "Port")),
SutPath = Need(receive, "Receive", "SutPath"),
ControllerPath = Need(receive, "Receive", "ControllerPath"),
DuplicateStore = Need(receive, "Receive", "DuplicateStore"),
DuplicateWindow = TimeSpan.FromDays(Int(Need(receive, "Receive", "DuplicateWindowDays"))),
RetryCount = Int(Need(retry, "Retry", "Count")),
RetryDelay = TimeSpan.FromSeconds(Int(Need(retry, "Retry", "DelaySeconds"))),
SendTimeout = Int(Need(retry, "Retry", "SendTimeoutMs")),
PkiPathPolicy = Value(pkiPath, "Policy"),
ExtraLegs = context.GetProperty("ExtraLegs") is { Length: > 0 } extra ? extra : null,
};
}
}
///
/// One service and action pair of the conformance tests, with how our signature references our certificate:
/// X509IssuerSerial unless the test asks for another form, as the test suite does.
///
public sealed record ConformanceLeg(string Service, string Action, As4KeyReference KeyReference = As4KeyReference.IssuerSerial,
bool PkiPath = false, bool WithPolicy = false)
{
/// Whether a missing receipt is redelivered: the ONEWAY_RETRY profile of the user guide.
public bool Retry => Action == "retry";
}
///
/// The legs between the test driver and the access point under test: user guide v2.90, annex A (PMode parameters 11 and
/// 12) for the common profile and the Four Corner enhancement. The agreements of the node are built from this list, one
/// per pair, since a received message is matched on sender, service and action.
///
public static class ConformanceLegs
{
public const string OneWay = "http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/ns/core/200704/oneWay";
public const string TwoWay = "http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/ns/core/200704/twoWay";
/// ebMS 3.0 test service (Core ยง5.2.2): its messages get a receipt and reach no business application.
public const string TestService = "http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/ns/core/200704/service";
public const string TestAction = "http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/ns/core/200704/test";
/// Service of the command channel; its actions are Submit, Deliver and Notify.
public const string CommandService = "http://www.edelivery.eu/as4/conformancetest";
public static IReadOnlyList Default { get; } =
[
new(OneWay, "default"),
new(OneWay, "retry"),
new(OneWay, "fourCorner"),
new(OneWay, "duplicateDetection"),
new(OneWay, "emptyBodyNotReferencedInSignature"),
new(OneWay, "bstDefault", As4KeyReference.BinarySecurityToken),
new(OneWay, "ski", As4KeyReference.KeyIdentifier),
new(OneWay, "issuerSerial", As4KeyReference.IssuerSerial),
// PKI path: we sign with our certification path, and the test driver's signer must present one that leads to
// a trust anchor (AS4_CP_TA47 to TA51). The last leg also requires a certificate policy.
new(OneWay, "bstPkiPath", As4KeyReference.PkiPath, PkiPath: true),
new(OneWay, "bstPkiPathSutC3Fail", As4KeyReference.PkiPath, PkiPath: true),
new(OneWay, "bstPkiPathSutC3PolicyFail", As4KeyReference.PkiPath, PkiPath: true, WithPolicy: true),
new(TwoWay, "default"),
new(TestService, TestAction),
];
/// plus the pairs of service|action;service|action.
public static IReadOnlyList With(string? extra)
{
var legs = Default.ToList();
foreach (var pair in (extra ?? "").Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
var parts = pair.Split('|');
if (parts.Length != 2 || parts[0].Length == 0 || parts[1].Length == 0)
throw new InvalidOperationException($"ExtraLegs: '{pair}' is not service|action.");
if (!legs.Any(l => l.Service == parts[0] && l.Action == parts[1]))
legs.Add(new ConformanceLeg(parts[0], parts[1]));
}
return legs;
}
}