Path: redb-route/tests/redb.Route.Tests.As4/As4TestMessages.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/tests/redb.Route.Tests.As4/As4TestMessages.cs ---- using System.IO.Compression; using System.Text; using System.Xml; using redb.Route.As4; using redb.Route.As4.Security; using redb.Route.Core; namespace redb.Route.Tests.As4; /// /// Hand-built AS4 user messages for the security tests — written before the ebMS model existed. One /// attachment, compressed, with the eDelivery four-corner properties. /// internal static class As4TestMessages { /// A SOAP 1.2 envelope with one eb:UserMessage whose payload is the part . public static XmlDocument UserMessage(string messageId, string contentId, string from = "sender", string to = "receiver", string? agreementRef = null, string service = "urn:example", string? serviceType = null, string action = "Submit", bool fourCorner = true, string? uncompressedMimeType = null, params string[] moreContentIds) { var properties = fourCorner ? "urn:oasis:names:tc:ebcore:partyid-type:unregistered:C1urn:oasis:names:tc:ebcore:partyid-type:unregistered:C4" : ""; var partInfos = string.Concat(new[] { contentId }.Concat(moreContentIds).Select(id => uncompressedMimeType is null ? $"text/xmlapplication/gzip" : $"{uncompressedMimeType}")); var agreement = agreementRef is null ? "" : $"{agreementRef}"; var serviceTypeAttribute = serviceType is null ? "" : $" type=\"{serviceType}\""; var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'", System.Globalization.CultureInfo.InvariantCulture); return SafeXml.LoadDocument(Encoding.UTF8.GetBytes($""" {timestamp}{messageId} {from}{As4Partner.InitiatorRole} {to}{As4Partner.ResponderRole} {agreement}{service}{action}conversation-1 {properties} {partInfos} """)); } /// A self-signed RSA key pair with a Subject Key Identifier, usable for decryption on every platform. public static System.Security.Cryptography.X509Certificates.X509Certificate2 KeyPair(string subject) => KeyPair(subject, DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddYears(1)); /// A key pair valid from to (an expired or future one, for validity tests). public static System.Security.Cryptography.X509Certificates.X509Certificate2 KeyPair(string subject, DateTimeOffset notBefore, DateTimeOffset notAfter) { using var rsa = System.Security.Cryptography.RSA.Create(2048); var request = new System.Security.Cryptography.X509Certificates.CertificateRequest(subject, rsa, System.Security.Cryptography.HashAlgorithmName.SHA256, System.Security.Cryptography.RSASignaturePadding.Pkcs1); request.CertificateExtensions.Add(new System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension(request.PublicKey, critical: false)); using var created = request.CreateSelfSigned(notBefore, notAfter); return System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12( created.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Pfx), null, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable); } /// The certificate of without its private key. public static System.Security.Cryptography.X509Certificates.X509Certificate2 PublicOnly(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate) => System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadCertificate( certificate.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Cert)); public static byte[] Gzip(byte[] data) { using var output = new MemoryStream(); using (var gzip = new GZipStream(output, CompressionLevel.Optimal)) gzip.Write(data); return output.ToArray(); } /// Flips one bit of a part's content, as a tampering attacker would. public static void Flip(redb.Route.As4.Mime.SwaPart part, int index) { var bytes = part.ToArray(); bytes[index] ^= 0x01; part.Replace(new MemoryStream(bytes, writable: false)); } /// /// The same base64 value with the lowest unused bit of its last character set (YR== for YQ==): .NET 10 /// decodes it to the same bytes, .NET 11 and XML Schema base64Binary refuse it. The value must end in padding. /// public static string WithUnusedBitsSet(string base64) { const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var value = base64.Trim(); var last = value.TrimEnd('=').Length - 1; if (last == value.Length - 1) throw new ArgumentException("The value has no padding, so no unused bits.", nameof(base64)); var chars = value.ToCharArray(); chars[last] = alphabet[alphabet.IndexOf(chars[last], StringComparison.Ordinal) | 1]; return new string(chars); } public static byte[] Gunzip(byte[] data) { using var input = new GZipStream(new MemoryStream(data), CompressionMode.Decompress); using var output = new MemoryStream(); input.CopyTo(output); return output.ToArray(); } }