Path: redb-route/redb.Route.As4/Security/AttachmentCanonicalizer.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/Security/AttachmentCanonicalizer.cs
----
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text.RegularExpressions;
using System.Xml;
using redb.Route.As4.Mime;
using redb.Route.Core;
namespace redb.Route.As4.Security;
///
/// The digest input of the Attachment-Content-Signature-Transform: the part's content in its MIME canonical form
/// (OASIS WSS SwA Profile 1.1 §5.4.2). The rules and the media type patterns are those of Apache WSS4J, which
/// Holodeck B2B and Domibus sign and verify with:
///
/// - XML (text/xml, application/xml, application/*+xml, image/*+xml) — Exclusive XML
/// Canonicalization without comments, with no InclusiveNamespaces prefix list;
/// - any other text/* — line endings normalized to CRLF;
/// - anything else — the content as it is.
///
/// The media type is the part's own: with AS4 compression it is application/gzip, so the rules bite only on
/// payloads sent uncompressed.
///
internal static class AttachmentCanonicalizer
{
private static readonly Regex Xml = new(@"^(text/xml|application/xml|(application|image)/[^;]*\+xml)",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
private static readonly Regex Text = new(@"^text/", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
/// SHA-256 of the canonical form of .
public static byte[] Sha256(SwaPart part)
{
ArgumentNullException.ThrowIfNull(part);
if (Xml.IsMatch(part.ContentType))
return SHA256.HashData(ExclusiveC14N(part));
if (Text.IsMatch(part.ContentType))
return CrlfSha256(part.OpenRead());
return part.Sha256();
}
private static Stream ExclusiveC14N(SwaPart part)
{
// Foreign XML: the core SafeXml reader (no DTD, the default structure limits), whitespace kept, as the transform
// reads the content. The payload is the partner's document, not an envelope, so it gets the generous default
// limits; its size is bounded by the request.
var document = new XmlDocument { PreserveWhitespace = true, XmlResolver = null };
try
{
using var reader = SafeXml.CreateReader(part.OpenRead(), XmlLimits.Default);
document.Load(reader);
}
catch (XmlException e)
{
// Its digest cannot be computed, so the signature over it cannot hold.
throw new CryptographicException($"Attachment '{part.ContentId}' is declared {part.ContentType} but cannot be read as XML: {e.Message}", e);
}
var transform = new XmlDsigExcC14NTransform(includeComments: false);
transform.LoadInput(document);
return (Stream)transform.GetOutput(typeof(Stream));
}
///
/// SHA-256 of with every CR, LF and CRLF written as CRLF — the rule of WSS4J's
/// CRLFOutputStream.
///
private static byte[] CrlfSha256(Stream input)
{
using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
ReadOnlySpan crlf = "\r\n"u8;
var buffer = new byte[81920];
var afterCr = false;
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
var start = 0;
for (var i = 0; i < read; i++)
{
var b = buffer[i];
if (b == (byte)'\r')
{
hash.AppendData(buffer, start, i - start);
hash.AppendData(crlf);
start = i + 1;
afterCr = true;
}
else if (b == (byte)'\n')
{
hash.AppendData(buffer, start, i - start);
if (!afterCr) hash.AppendData(crlf);
start = i + 1;
afterCr = false;
}
else
{
afterCr = false;
}
}
hash.AppendData(buffer, start, read - start);
}
return hash.GetHashAndReset();
}
}