Path: redb-route/redb.Route.As4/As4Consumer.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/redb.Route.As4/As4Consumer.cs ---- using System.Diagnostics; using System.Security.Cryptography; using System.Xml; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Components; using redb.Route.Configuration; using redb.Route.As4.Compression; using redb.Route.As4.Messaging; using redb.Route.As4.Mime; using redb.Route.As4.Security; using redb.Route.As4.Signals; using redb.Route.Core; using redb.Route.Http; using redb.Route.Telemetry; namespace redb.Route.As4; /// /// AS4 receive server (One-Way / Push, eDelivery AS4 1.16) on the shared Kestrel host. For every request: /// read the envelope (no DTD, bounded), match the agreement, decrypt, verify the signature, check the payload /// references, decompress, hand one exchange to the route, and answer in the HTTP response — a signed /// non-repudiation receipt when the unit of work succeeded (EndedInFailure() is false), an ebMS error /// otherwise. The receipt therefore goes out after the route's transaction has committed. /// /// What the partner reads never carries the text of one of our exceptions (BR-4): each ebMS code has a fixed /// text and the answer quotes a reference; the detail goes to the log. Decryption and signature failures all /// get the same answer of their code, so varying the input teaches the sender nothing. /// /// internal sealed class As4Consumer : IConsumer { private readonly As4Endpoint _endpoint; private readonly IProcessor _processor; private readonly As4EndpointOptions _options; private readonly InflightDrainGuard _drain = new(); private RouteRegistration? _registration; private As4Node? _node; private IIdempotentRepository? _duplicates; private StreamCacheOptions _spool = new(); private ILogger? _logger; public As4Consumer(As4Endpoint endpoint, IProcessor processor, As4EndpointOptions options) { _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); _processor = processor ?? throw new ArgumentNullException(nameof(processor)); _options = options ?? throw new ArgumentNullException(nameof(options)); } /// public IEndpoint Endpoint => _endpoint; /// The engine pipeline received messages are handed to. internal IProcessor Processor => _processor; private SharedHttpServerManager Server => ((As4Component)_endpoint.Component).Server; private string Path => _endpoint.Uri.Path; /// public async Task Start(CancellationToken ct = default) { if (_endpoint.PartnerUrl is not null || Path.Length == 0) throw new InvalidOperationException( $"AS4 receive endpoint {_endpoint.Uri} has no receive path; a receive endpoint is as4:/path?host=...&port=...."); _logger ??= _endpoint.Logger; _node = As4Node.Resolve(_endpoint.Context, _options.ConnectionFactory!); // eDelivery AS4 1.16 mandates duplicate detection; the store is named, as the S3 consumer names it. if (string.IsNullOrWhiteSpace(_options.IdempotentRepository)) throw new InvalidOperationException( $"AS4 receive endpoint {_endpoint.Uri} requires 'idempotentRepository': the name of an IIdempotentRepository registered with " + "context.AddIdempotentRepository(name, repository). It remembers received message ids so a resend is not delivered twice."); var context = _endpoint.Context ?? throw new InvalidOperationException($"AS4 receive endpoint {_endpoint.Uri} has no route context to resolve its idempotent repository from."); _duplicates = context.GetIdempotentRepositoryProvider().Get(_options.IdempotentRepository); _spool = As4Spool.Options(context); // TLS material, as the SOAP receiver resolves it: the URI wins where it says something, the node fills the rest // (a password in a URI travels into logs and the dashboard). var certPath = _options.SslCertPath ?? _node.Factory.SslCertPath; var certPassword = _options.SslCertPassword ?? _node.Factory.SslCertPassword; var certMode = _options.ClientCertificateMode != As4ClientCertificateMode.NoCertificate ? _options.ClientCertificateMode : _node.Factory.ClientCertificateMode; var thumbprints = _options.AllowedClientThumbprints ?? _node.Factory.AllowedClientThumbprints; // Fail here rather than inside Kestrel, as the SOAP receiver does: without a certificate the listener would fall // back to a host default (present on a developer's machine, absent in production), and a partner would meet a // certificate nobody agreed on. if (_options.UseTls && string.IsNullOrWhiteSpace(certPath)) throw new InvalidOperationException( $"AS4 receive endpoint on {_options.Host}:{_options.Port}{Path} is set to serve TLS but has no certificate. " + "Set sslCertPath on the endpoint URI, or SslCertPath on the connection factory it names."); if (certMode != As4ClientCertificateMode.NoCertificate && !_options.UseTls) throw new InvalidOperationException( $"AS4 receive endpoint on {_options.Host}:{_options.Port}{Path} asks for client certificates without TLS: a client " + "certificate is presented in the TLS handshake. Use the as4s scheme, or drop clientCertificateMode."); _drain.Start(ct); _registration = Server.RegisterRoute(_options.Host, _options.Port, Path, "POST", HandleRequest, _options.UseTls, certPath, certPassword, corsOptions: null, maxRequestBodySize: _options.MaxRequestBodySize, clientCertificateMode: MapClientCertificateMode(certMode), clientCertificateValidation: ThumbprintValidator(thumbprints, _node.Factory.Revocation), concurrencyLimit: ConcurrencyLimitOptions.FromEndpoint( _options.MaxConcurrentRequests, _options.RequestQueueLimit, _options.RejectStatusCode, _options.RetryAfterSeconds, onRejected: _endpoint.RecordRejected)); try { await Server.EnsureStarted(_options.Host, _options.Port, ct).ConfigureAwait(false); } catch { // The listener did not come up (a port still taken): the route leaves it, so a later Start registers // once and a stopped receiver does not keep the port. await Stop(CancellationToken.None).ConfigureAwait(false); throw; } _logger?.LogInformation("AS4 receiver started: {Host}:{Port}{Path}, node {Node}", _options.Host, _options.Port, Path, _node.Name); } /// public async Task Stop(CancellationToken ct = default) { if (_registration is null) return; // Unregister, drain what is already inside, then release the listener — the order of the HTTP family. Server.UnregisterRoute(_registration); _registration = null; await _drain.DrainAsync(ct, _logger, $"as4://{_options.Host}:{_options.Port}{Path}").ConfigureAwait(false); await Server.StopIfEmpty(_options.Host, _options.Port, ct).ConfigureAwait(false); _logger?.LogInformation("AS4 receiver stopped: {Host}:{Port}{Path}", _options.Host, _options.Port, Path); } private async Task HandleRequest(HttpContext http) { // Counted for the drain: a message already inside the pipeline must be answered before Stop returns. _drain.Increment(); try { await HandleRequestCore(http).ConfigureAwait(false); } catch (Exception e) when (e is not OperationCanceledException) { // What the receiver did not foresee is still an ebMS answer (BR-4): logged with its reference and counted, the // partner gets EBMS:0004 and the reference, never a bare 500 it cannot tell from a network failure. _logger?.LogError(e, "AS4 receiver {Path}: unexpected failure (ref {Reference}).", Path, http.TraceIdentifier); _endpoint.RecordError(e); if (!http.Response.HasStarted) await WriteSignal(http, Responses.Error(As4ErrorCode.Other, null, _node!.Factory.ExternalHostName, http.TraceIdentifier)).ConfigureAwait(false); } finally { _drain.Decrement(); } } private async Task HandleRequestCore(HttpContext http) { var node = _node!; var host = node.Factory.ExternalHostName; var reference = http.TraceIdentifier; // The Consumer span on the core contract: the host's request span when the application traces ASP.NET Core, else the // sender's traceparent, else a root; the sender's baggage is put back; EnableTelemetry=false opens nothing. using var span = RouteTelemetryExtensions.StartConsumerSpan( _endpoint.Context, $"{Path} receive", ActivityKind.Consumer, "messaging.system", "as4", _endpoint.Uri.NormalizedKey, http.Request.Headers, static (headers, name) => headers.TryGetValue(name, out var value) ? value.ToString() : null, InboundParent.HostRequest, destination: Path, operation: "receive"); var activity = span.Activity; // The request is spooled, not buffered: past the threshold it goes to a temporary file (Kestrel bounds it by // maxRequestBodySize), and MimeKit reads the parts from there without another copy. var body = new StreamCache(_spool); // ── Not ebMS at all: a SOAP fault ── SwaMessage message; try { await body.CacheFromSourceAsync(http.Request.Body, http.RequestAborted).ConfigureAwait(false); body.CompleteWriting(); message = SwaMessage.Read(http.Request.ContentType ?? string.Empty, body, _options.MaxEnvelopeCharacters, _spool); } catch (XmlException e) { await body.DisposeAsync().ConfigureAwait(false); // A DTD (forbidden in SOAP, SOAP 1.2 Part 1 §5), broken XML or an envelope over its limits: the text describes // the caller's own bytes. A refusal, not an error of this endpoint. _endpoint.RecordRefused($"Malformed SOAP envelope: {e.Message}"); activity.RecordFailure(e); await Write(http, Responses.SoapFault(mustUnderstand: false, $"Malformed SOAP envelope: {e.Message}")).ConfigureAwait(false); return; } catch (Exception e) when (e is FormatException or MimeKit.ParseException or ArgumentException) { await body.DisposeAsync().ConfigureAwait(false); _endpoint.RecordRefused($"{As4ErrorCode.MimeInconsistency.Code}: {e.Message}"); activity.RecordFailure(e); await WriteSignal(http, Responses.Error(As4ErrorCode.MimeInconsistency, null, host, reference)).ConfigureAwait(false); return; } catch { await body.DisposeAsync().ConfigureAwait(false); throw; } using var owned = message; // parts the route did not take, and the spooled request if (EnvelopeProblem(message.Envelope) is { } notSoap) { _endpoint.RecordRefused(notSoap); activity?.SetStatus(ActivityStatusCode.Error); await Write(http, Responses.SoapFault(mustUnderstand: false, notSoap)).ConfigureAwait(false); return; } if (UnderstoodHeaders(message.Envelope) is { } unknownBlock) { _endpoint.RecordRefused($"Header block {unknownBlock} is not understood."); activity?.SetStatus(ActivityStatusCode.Error); await Write(http, Responses.SoapFault(mustUnderstand: true, $"Header block {unknownBlock} is not understood.")).ConfigureAwait(false); return; } // ── ebMS processing up to the route: every failure is an ebMS error ── string? messageId = null; As4Partner? partner = null; Exchange exchange; SignatureVerification verification; try { var messaging = MessagingReader.Read(message.Envelope); if (messaging.SignalMessages.FirstOrDefault(s => s.PullRequestMpc is not null) is { } pull) { // No pull process here (pull is out of scope): EBMS:0010, as Domibus answers a pull request for an mpc // without one (PullRequestLegConfigurationExtractor). messageId = pull.MessageInfo.MessageId; throw new As4ProcessingException(As4ErrorCode.ProcessingModeMismatch, $"a pull request for mpc '{pull.PullRequestMpc}': this receiver supports push only."); } if (messaging.UserMessages.Count == 0) { // A signal on the message endpoint: eDelivery 1.16 forbids asynchronous signals, so nothing waits for it. _logger?.LogWarning("AS4 receiver {Path}: a signal message arrived and no exchange waits for it; ignored.", Path); http.Response.StatusCode = StatusCodes.Status202Accepted; return; } var user = messaging.UserMessages[0]; messageId = user.MessageInfo.MessageId; (partner, _) = node.MatchInbound(user); verification = Secure(message, node, partner); if (partner.RequireFourCornerProperties) RequireFourCornerProperties(user); exchange = CreateExchange(http, user, message, partner, verification); } catch (As4ProcessingException e) { _logger?.LogWarning("AS4 receiver {Path}: message {MessageId} refused with {Code} (ref {Reference}): {Detail}", Path, messageId, e.Code.Code, reference, e.Detail); // The partner's message does not hold (security, agreement, profile): a refusal answered by protocol. EBMS:0004 // stays an error: it names a failure on this side, except the impersonation refused with it. if (e.Code == As4ErrorCode.Other && e.InnerException is not SignerNotAuthorizedException) _endpoint.RecordError(e); else _endpoint.RecordRefused($"{e.Code.Code} for {messageId}: {e.Detail}"); activity.RecordFailure(e); await WriteSignal(http, Responses.Error(e.Code, messageId, host, reference, signer: partner is null ? null : node.Factory.SigningCertificate, keyReference: partner?.KeyReference ?? As4KeyReference.BinarySecurityToken, signingPath: node.SigningPath)).ConfigureAwait(false); return; } try { // ── Duplicate detection: the two-phase claim of the S3 consumer (Lease, then Confirm or Release of that claim) ── // Claimed after the security checks, so a forged message cannot burn the id of a real one. The claim is a short // state write taken uncancelled, as Confirm and Release are: cut by the sender's disconnect it could be written // and reported failed, leaving the id marked for a message the route never saw. A disconnect after it reaches // the route, whose cancellation releases the claim. IdempotentClaim claim; string? claimToken; try { var lease = await _duplicates!.Lease(messageId!, TimeSpan.FromMilliseconds(_options.InFlightClaimTimeout), CancellationToken.None) .ConfigureAwait(false); claim = lease.Outcome; claimToken = lease.Token; } catch (Exception e) { _logger?.LogError(e, "AS4 receiver {Path}: duplicate check for message {MessageId} failed (ref {Reference}).", Path, messageId, reference); _endpoint.RecordError(e); activity.RecordFailure(e); await WriteSignal(http, Responses.Error(As4ErrorCode.Other, messageId, host, reference, signer: node.Factory.SigningCertificate, keyReference: partner!.KeyReference, signingPath: node.SigningPath)).ConfigureAwait(false); return; } if (claim == IdempotentClaim.InFlight) { // The first copy is still in the route: not a duplicate yet. It may fail and release its claim, and a receipt // now would have told the sender the message arrived. Refused as busy with no ebMS signal, as the admission // limit refuses: the sender resends on its reception-awareness schedule (Domibus does, at its P-Mode interval). _endpoint.RecordRejected(); _logger?.LogWarning("AS4 receiver {Path}: message {MessageId} arrived again while its first copy is in the route; " + "refused with {Status} (ref {Reference}).", Path, messageId, _options.RejectStatusCode, reference); http.Response.StatusCode = _options.RejectStatusCode; if (_options.RetryAfterSeconds > 0) http.Response.Headers.RetryAfter = _options.RetryAfterSeconds.ToString(System.Globalization.CultureInfo.InvariantCulture); return; } if (claim == IdempotentClaim.TakenOver) _logger?.LogWarning("AS4 receiver {Path}: message {MessageId} had a claim older than inFlightClaimTimeout {Timeout} ms " + "that was never confirmed; taken over and processed.", Path, messageId, _options.InFlightClaimTimeout); if (claim == IdempotentClaim.Confirmed) { // Received before: answer with the receipt of the original, the very same signal (AS4_CP_TA63), and do // not deliver it twice. The sender stops resending once it has a receipt. A receipt no longer kept is // built again from the same bytes, so with the same non-repudiation information. var kept = await KeptReceiptAsync(node, partner!, messageId!).ConfigureAwait(false); _logger?.LogInformation("AS4 receiver {Path}: message {MessageId} is a duplicate; {Receipt}, not delivered.", Path, messageId, kept is null ? "a new receipt sent (the original is no longer kept)" : "its receipt sent again"); await WriteSignal(http, kept ?? Responses.Receipt(messageId!, verification.References, host, node.Factory.SigningCertificate!, partner!.KeyReference, partner.TimestampTtl, node.SigningPath)).ConfigureAwait(false); return; } // ── The route; the answer follows its unit of work ── try { await _processor.Process(exchange, http.RequestAborted).ConfigureAwait(false); } catch (OperationCanceledException) { // Aborted mid-route (the sender went away, or the host stopped after the drain timeout): the message // was not processed, so its resend must be delivered — released on any failure, as the S3 consumer does. await ReleaseClaimAsync(messageId!, claimToken).ConfigureAwait(false); throw; } catch (Exception e) { exchange.Exception ??= e; } if (exchange.EndedInFailure()) { // No RecordError: the core StatisticsProcessor saw the same exchange.Exception. The span shows it red. if (exchange.Exception is { } routeFailure) activity.RecordFailure(routeFailure); await ReleaseClaimAsync(messageId!, claimToken).ConfigureAwait(false); // the sender resends; that is not a duplicate var callerText = (exchange.Exception as MalformedRequestException)?.Message; var code = callerText is null ? As4ErrorCode.Other : As4ErrorCode.ValueInconsistent; _logger?.LogWarning(exchange.Exception, "AS4 receiver {Path}: route failed for message {MessageId}; answered {Code} (ref {Reference}).", Path, messageId, code.Code, exchange.ExchangeId); await WriteSignal(http, Responses.Error(code, messageId, host, exchange.ExchangeId, callerText, node.Factory.SigningCertificate, partner!.KeyReference, node.SigningPath)).ConfigureAwait(false); return; } await ConfirmClaimAsync(messageId!, claimToken).ConfigureAwait(false); var receipt = Responses.Receipt(messageId!, verification.References, host, node.Factory.SigningCertificate!, partner!.KeyReference, partner.TimestampTtl, node.SigningPath); await KeepReceiptAsync(node, partner, messageId!, receipt).ConfigureAwait(false); await WriteSignal(http, receipt).ConfigureAwait(false); } finally { await exchange.DisposeAsync().ConfigureAwait(false); } } /// /// Confirms the claim of a processed message. The work is done by now, so a failure to confirm is logged and /// the receipt still goes out; the claim itself (from Lease) already keeps a resend from being delivered. Only this /// request's claim is confirmed: a claim another node took over meanwhile is left to its holder. /// private async Task ConfirmClaimAsync(string messageId, string? token) { try { if (!await _duplicates!.Confirm(messageId, token, CancellationToken.None).ConfigureAwait(false)) LogTakenOver(messageId); } catch (Exception e) when (e is not OperationCanceledException) { _logger?.LogWarning(e, "AS4 receiver {Path}: confirming duplicate claim for message {MessageId} failed.", Path, messageId); } } /// /// Keeps the receipt sent for a message, for its duplicates. Under the sending party and the message id, so a /// receipt goes back only to the party it was signed for. The receipt goes out whether or not it was kept. /// private async Task KeepReceiptAsync(As4Node node, As4Partner partner, string messageId, (string ContentType, byte[] Body) receipt) { try { var contentType = System.Text.Encoding.UTF8.GetBytes(receipt.ContentType); var data = new byte[contentType.Length + 1 + receipt.Body.Length]; contentType.CopyTo(data, 0); data[contentType.Length] = (byte)'\n'; receipt.Body.CopyTo(data, contentType.Length + 1); await node.Factory.Receipts.Store(ReceiptKey(partner, messageId), data, node.Factory.ReceiptRetention, CancellationToken.None) .ConfigureAwait(false); } catch (Exception e) when (e is not OperationCanceledException) { _logger?.LogWarning(e, "AS4 receiver {Path}: keeping the receipt of message {MessageId} failed; a duplicate gets a new receipt.", Path, messageId); } } /// The receipt kept for a message of this party, or null when none is kept (or the store fails). private async Task<(string ContentType, byte[] Body)?> KeptReceiptAsync(As4Node node, As4Partner partner, string messageId) { try { var data = await node.Factory.Receipts.Retrieve(ReceiptKey(partner, messageId), CancellationToken.None).ConfigureAwait(false); var split = data is null ? -1 : Array.IndexOf(data, (byte)'\n'); if (split < 0) return null; return (System.Text.Encoding.UTF8.GetString(data!, 0, split), data![(split + 1)..]); } catch (Exception e) when (e is not OperationCanceledException) { _logger?.LogWarning(e, "AS4 receiver {Path}: reading the kept receipt of message {MessageId} failed.", Path, messageId); return null; } } /// /// A key every store takes (a file store makes a file name of it): a hash of the sending party and the message id. /// The party, not the agreement: a resend may match another agreement with the same party (the conformance test /// driver resends AS4_CP_TA63 under another action), and it is still the same message of the same sender. /// private void LogTakenOver(string messageId) => _logger?.LogWarning("AS4 receiver {Path}: the claim of message {MessageId} was taken over while it was processed " + "(longer than inFlightClaimTimeout {Timeout} ms); the new holder's claim is left as it is.", Path, messageId, _options.InFlightClaimTimeout); private static string ReceiptKey(As4Partner partner, string messageId) => "as4-receipt-" + Convert.ToHexString(System.Security.Cryptography.SHA256.HashData( System.Text.Encoding.UTF8.GetBytes(partner.PartyIdType + "\n" + partner.PartyId + "\n" + messageId))).ToLowerInvariant(); /// /// Releases the claim of a message the route did not process, so its resend is delivered (as the S3 consumer does). /// Only this request's claim is released: a claim another node took over meanwhile stays. /// private async Task ReleaseClaimAsync(string messageId, string? token) { try { if (!await _duplicates!.Release(messageId, token, CancellationToken.None).ConfigureAwait(false)) LogTakenOver(messageId); } catch (Exception e) when (e is not OperationCanceledException) { _logger?.LogWarning(e, "AS4 receiver {Path}: releasing duplicate claim for message {MessageId} failed — it stays marked until manual cleanup.", Path, messageId); } } /// /// Decrypts, verifies and checks the policy of the agreement: eDelivery mandates a signed message with /// encrypted payloads, so a message without either is refused before any other check. Throws the ebMS code /// for each failure; decryption and signature failures carry the detail only for our log. /// private SignatureVerification Secure(SwaMessage message, As4Node node, As4Partner partner) { SoapParts parts; try { parts = SoapParts.Of(message.Envelope); } catch (CryptographicException e) { // Two wsse:Security headers for one role (WS-Security 1.1 §6.1): a policy failure, as Domibus reports WSS4J's. throw new As4ProcessingException(As4ErrorCode.PolicyNoncompliance, e.Message, e); } var security = parts.Security; var signed = security is not null && security.ChildNodes.OfType().Any(e => e.LocalName == "Signature" && e.NamespaceURI == WsSecurityNames.Ds); var encrypted = security is not null && security.ChildNodes.OfType().Any(e => e.LocalName == "EncryptedKey" && e.NamespaceURI == WsSecurityNames.Xenc); if (!signed) // Every agreement asks for a signed message, so an unsigned one does not match the processing mode: EBMS:0010, // as the eDelivery conformance test driver expects (AS4_CP_TA80). throw new As4ProcessingException(As4ErrorCode.ProcessingModeMismatch, "the message is not signed."); if (message.Parts.Count > 0 && !encrypted) throw new As4ProcessingException(As4ErrorCode.PolicyNoncompliance, "the payloads are not encrypted."); try { As4SecurityEngine.Decrypt(message, node.Factory.DecryptionCertificates); } catch (CryptographicException e) { throw new As4ProcessingException(As4ErrorCode.FailedDecryption, e.Message, e); } try { return As4SecurityEngine.Verify(message, partner.PartnerSigningCertificates.ToList(), partner.TimestampTolerance, DateTimeOffset.UtcNow, node.Factory.Revocation, partner.TimestampTimeToLive, partner.PkiPathTrust, TrustedElsewhere(node, partner)); } catch (SignatureCoverageException e) { // Signed, and what is signed is intact, but not everything the profile requires: the policy is not met. throw new As4ProcessingException(As4ErrorCode.PolicyNoncompliance, e.Message, e); } catch (Exception e) when (e is UnsupportedAttachmentTransformException or MalformedSignatureValueException) { throw new As4ProcessingException(As4ErrorCode.FailedDecryption, e.Message, e); } catch (SignerNotAuthorizedException e) { throw new As4ProcessingException(As4ErrorCode.Other, e.Message, e); } catch (CryptographicException e) { throw new As4ProcessingException(As4ErrorCode.FailedAuthentication, e.Message, e); } } /// /// The certificates this node trusts apart from the sender's agreement: the signing certificates of its other /// agreements and the trust anchors of all of them. A message signed with one of these, or with a certificate one of /// them issued, is an impersonation of the sender, not a stranger's message. /// private static List TrustedElsewhere(As4Node node, As4Partner partner) { var trusted = new List(); foreach (var other in node.Factory.Partners) { if (!ReferenceEquals(other, partner)) trusted.AddRange(other.PartnerSigningCertificates); trusted.AddRange(other.PartnerTrustAnchors); } return trusted; } private Exchange CreateExchange(HttpContext http, UserMessage user, SwaMessage message, As4Partner partner, SignatureVerification verification) { // Every payload is decompressed before any is handed on: a failure leaves them all with the message. var matched = new List<(PartInfo Info, SwaPart Part)>(); PayloadConsistency.CheckCharacterSets(user); foreach (var (info, part) in PayloadConsistency.Match(user, message)) { if (part is null) throw new As4ProcessingException(As4ErrorCode.FeatureNotSupported, "a payload in the SOAP body is not supported; AS4 carries payloads as attachments."); As4Compression.Decompress(part, info, MaxDecompressedBytes, message.Spool); matched.Add((info, part)); } // One payload is the body itself — byte[], or with streamBody the spooled stream, which the exchange closes // (Exchange.DisposeAsync), as the File and S3 consumers hand theirs. Several are As4Payload streams the // exchange releases (ExchangeResources.ReleaseWithExchange). With payloadList the body is always that list, in // the order of eb:PartInfo, whatever the number of payloads. object? body; List? payloads = null; var single = matched.Count == 1 && !_options.PayloadList; if (matched.Count == 0 && !_options.PayloadList) { body = null; } else if (single) { body = _options.StreamBody ? matched[0].Part.Detach() : matched[0].Part.ToArray(); } else { payloads = matched.Select(m => new As4Payload(m.Part.ContentId, m.Part.ContentType, m.Part.Detach(), m.Info.Properties.ToDictionary(p => p.Name, p => p.Value, StringComparer.Ordinal))).ToList(); body = payloads; } var messageOut = new Message(body) { ContentType = single ? matched[0].Part.ContentType : null }; Headers(messageOut, user, partner, verification); if (http.Connection.RemoteIpAddress is { } remote) messageOut.Headers[As4Headers.RemoteAddress] = remote.ToString(); // mTLS: what the handshake proved about the caller, as the SOAP receiver puts it on the exchange. if (http.Connection.ClientCertificate is { } clientCert) { messageOut.Headers[As4Headers.ClientCertThumbprint] = clientCert.Thumbprint; messageOut.Headers[As4Headers.ClientCertSubject] = clientCert.Subject; messageOut.Headers[As4Headers.ClientCertNotAfter] = clientCert.NotAfter.ToUniversalTime().ToString("O"); } var exchange = Exchange.Create(messageOut, _endpoint.ScopeFactory); foreach (var payload in payloads ?? []) ExchangeResources.ReleaseWithExchange(exchange, payload.Content); exchange.Pattern = ExchangePattern.InOut; ExchangePrincipal.Set(exchange, Principal(http, partner, verification)); return exchange; } /// /// Who sent the message (as camel-cxf with WSS4J takes the principal from the signature): the /// envelope signer first — its subject, thumbprint and the agreement it authenticated — then the TLS client /// certificate when the partner presented one, then whatever the host's principal resolver adds. /// private static System.Security.Claims.ClaimsPrincipal Principal(HttpContext http, As4Partner partner, SignatureVerification verification) { var signer = verification.Signer; var identities = new List { new([ new(System.Security.Claims.ClaimTypes.NameIdentifier, partner.Name!), new(System.Security.Claims.ClaimTypes.Name, signer.Subject), new(System.Security.Claims.ClaimTypes.Thumbprint, signer.Thumbprint), ], As4AuthenticationTypes.Signature), }; if (http.Connection.ClientCertificate is { } clientCert) identities.Add(new([ new(System.Security.Claims.ClaimTypes.Name, clientCert.Subject), new(System.Security.Claims.ClaimTypes.Thumbprint, clientCert.Thumbprint), ], As4AuthenticationTypes.ClientCertificate)); if (SharedHttpServerManager.GetResolvedPrincipal(http) is { } resolved) identities.AddRange(resolved.Identities); return new System.Security.Claims.ClaimsPrincipal(identities); } /// The eb:Messaging plane onto the exchange, under names only. private static void Headers(IMessage message, UserMessage user, As4Partner partner, SignatureVerification verification) { var h = message.Headers; h[As4Headers.MessageId] = user.MessageInfo.MessageId; if (user.MessageInfo.RefToMessageId is { } refTo) h[As4Headers.RefToMessageId] = refTo; h[As4Headers.Timestamp] = user.MessageInfo.Timestamp; h[As4Headers.ConversationId] = user.CollaborationInfo.ConversationId; var from = user.From.PartyIds[0]; h[As4Headers.FromPartyId] = from.Value; if (from.Type is not null) h[As4Headers.FromPartyIdType] = from.Type; h[As4Headers.FromRole] = user.From.Role; var to = user.To.PartyIds[0]; h[As4Headers.ToPartyId] = to.Value; if (to.Type is not null) h[As4Headers.ToPartyIdType] = to.Type; h[As4Headers.ToRole] = user.To.Role; h[As4Headers.Service] = user.CollaborationInfo.Service.Value; if (user.CollaborationInfo.Service.Type is not null) h[As4Headers.ServiceType] = user.CollaborationInfo.Service.Type; h[As4Headers.Action] = user.CollaborationInfo.Action; if (user.CollaborationInfo.AgreementRef is { } agreement) { h[As4Headers.AgreementRef] = agreement.Value; if (agreement.PMode is not null) h[As4Headers.PModeId] = agreement.PMode; } foreach (var property in user.MessageProperties) { h[As4Headers.PropertyPrefix + property.Name] = property.Value; if (property.Type is not null) h[As4Headers.PropertyTypePrefix + property.Name] = property.Type; } h[As4Headers.Partner] = partner.Name; h[As4Headers.SignatureValid] = true; h[As4Headers.SignerThumbprint] = verification.Signer.Thumbprint; } /// /// The first header block with mustUnderstand="true" that is neither eb:Messaging nor /// wsse:Security (SOAP 1.2 Part 1 §5.2.3), or null. /// private static string? EnvelopeProblem(XmlDocument envelope) { // SOAP 1.2 Part 1 §5: an Envelope, at most one Header, exactly one Body — and an AS4 message has its ebMS // header, so both are required. Anything else is not a message this receiver can answer in ebMS terms. var root = envelope.DocumentElement; if (root is null || root.LocalName != "Envelope" || root.NamespaceURI != EbmsNamespaces.Soap12) return "Not a SOAP 1.2 envelope."; foreach (var name in new[] { "Header", "Body" }) { var count = root.ChildNodes.OfType().Count(e => e.LocalName == name && e.NamespaceURI == EbmsNamespaces.Soap12); if (count != 1) return $"The SOAP 1.2 envelope carries {count} {name} elements; an AS4 message has exactly one."; } return null; } private const string SoapNextRole = "http://www.w3.org/2003/05/soap-envelope/role/next"; private const string SoapUltimateReceiverRole = "http://www.w3.org/2003/05/soap-envelope/role/ultimateReceiver"; private static string? UnderstoodHeaders(XmlDocument envelope) { var header = envelope.DocumentElement?.ChildNodes.OfType() .FirstOrDefault(e => e.LocalName == "Header" && e.NamespaceURI == EbmsNamespaces.Soap12); if (header is null) return null; foreach (var block in header.ChildNodes.OfType()) { // SOAP 1.2 Part 1 §2.2: every node plays "next", the ultimate receiver also "ultimateReceiver" (the role of a // block without one); a block for "none" or another role is not addressed to us. var role = block.GetAttribute("role", EbmsNamespaces.Soap12); if (role.Length > 0 && role is not (SoapNextRole or SoapUltimateReceiverRole)) continue; var understood = (block.LocalName == "Messaging" && block.NamespaceURI == EbmsNamespaces.Eb) || (block.LocalName == "Security" && block.NamespaceURI == WsSecurityNames.Wsse); var mustUnderstand = block.GetAttribute("mustUnderstand", EbmsNamespaces.Soap12); if (!understood && mustUnderstand is "true" or "1") return $"{{{block.NamespaceURI}}}{block.LocalName}"; } return null; } /// /// eDelivery AS4 1.16 §4.1.2: originalSender and finalRecipient MUST be on every message of a /// four-corner exchange; asked of an agreement with (the default). /// Missing or empty is , as Domibus answers it (PropertyProfileValidator); /// checked after the security checks, as there. /// private static void RequireFourCornerProperties(UserMessage user) { foreach (var required in new[] { As4Headers.OriginalSender, As4Headers.FinalRecipient }) { var name = required[As4Headers.PropertyPrefix.Length..]; if (string.IsNullOrEmpty(user.PropertyValue(name))) throw new As4ProcessingException(As4ErrorCode.ProcessingModeMismatch, $"the message has no '{name}' property (eDelivery four-corner model)."); } } /// Upper bound on one decompressed payload: ten times the request limit, at most 1 GB, so a compression bomb cannot exhaust memory. private long MaxDecompressedBytes => Math.Min(_options.MaxRequestBodySize * 10, 1L << 30); private static Task WriteSignal(HttpContext http, (string ContentType, byte[] Body) signal) => Write(http, (StatusCodes.Status200OK, signal.ContentType, signal.Body)); private static async Task Write(HttpContext http, (int Status, string ContentType, byte[] Body) response) { http.Response.StatusCode = response.Status; http.Response.ContentType = response.ContentType; await http.Response.Body.WriteAsync(response.Body, http.RequestAborted).ConfigureAwait(false); } private static Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode? MapClientCertificateMode(As4ClientCertificateMode mode) => mode switch { As4ClientCertificateMode.AllowCertificate => Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode.AllowCertificate, As4ClientCertificateMode.RequireCertificate => Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode.RequireCertificate, _ => null, }; /// /// The allow-list check for client certificates, as the SOAP receiver builds it. It replaces Kestrel's own check, and a /// pin is a constraint on top of validation, not instead of it: the certificate must be within its validity period and /// pass the node's revocation policy, as Holodeck ("directly trusted, check validity") and Domibus check a pinned /// client certificate. Chain building is skipped for the directly pinned certificate, as Holodeck skips it. /// private Func? ThumbprintValidator(string? allowedThumbprints, RevocationPolicy revocation) { if (string.IsNullOrWhiteSpace(allowedThumbprints)) return null; var allowed = allowedThumbprints .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .Select(t => t.Replace(" ", string.Empty)) .ToHashSet(StringComparer.OrdinalIgnoreCase); return (certificate, _, _) => { if (!allowed.Contains(certificate.Thumbprint)) return false; if (CertificateValidity.Problem(certificate, DateTimeOffset.UtcNow, "TLS client certificate", revocation) is not { } problem) return true; _logger?.LogWarning("AS4 receiver {Path}: refused an allow-listed client certificate in the TLS handshake: {Problem}", Path, problem); return false; }; } }