Path: redb-route/tests/redb.Route.Tests.As4/SecurityTokenResolverBase64Tests.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/SecurityTokenResolverBase64Tests.cs
----
using System.Security.Cryptography;
using System.Xml;
using redb.Route.As4.Security;
namespace redb.Route.Tests.As4;
///
/// Every base64 value of a received security header (DigestValue, SignatureValue, CipherValue, BinarySecurityToken,
/// KeyIdentifier) is read through . A value whose last character has non-zero
/// unused bits is refused on every runtime: .NET 10 decodes it, .NET 11 refuses it, XML Schema base64Binary forbids it.
///
public class SecurityTokenResolverBase64Tests
{
[Theory]
[InlineData("YQ==", new byte[] { 0x61 })]
[InlineData("YWI=", new byte[] { 0x61, 0x62 })]
[InlineData("YWJj", new byte[] { 0x61, 0x62, 0x63 })]
[InlineData("\n YWJj\nYQ==\n", new byte[] { 0x61, 0x62, 0x63, 0x61 })] // line breaks, as WSS4J wraps long values
public void CanonicalValue_IsDecoded(string text, byte[] expected)
{
SecurityTokenResolver.Base64(Element("DigestValue", text)).Should().Equal(expected);
}
[Theory]
[InlineData("YR==")] // two '=': the low four bits of 'R' are unused, and one is set
[InlineData("YWJ=")] // one '=': the low two bits of 'J' are unused, and one is set
[InlineData("YWJjYR==\n")] // after a whole group, with trailing whitespace
[InlineData("YWJj\nYR = =")] // whitespace inside the padding
public void ValueWithUnusedBitsSet_IsRefusedAsASecurityFailure(string text)
{
var act = () => SecurityTokenResolver.Base64(Element("DigestValue", text));
act.Should().Throw().WithMessage("ds:DigestValue is not valid base64.");
}
[Fact]
public void BinarySecurityTokenWithUnusedBitsSet_IsRefused()
{
var der = As4TestMessages.KeyPair("CN=bst.as4.test").RawData;
if (der.Length % 3 == 0)
der = [.. der, 0x00]; // a value with padding; the base64 check comes before the certificate is parsed
var doc = new XmlDocument();
var token = doc.CreateElement("wsse", "BinarySecurityToken", WsSecurityNames.Wsse);
token.SetAttribute("ValueType", WsSecurityNames.X509V3);
token.InnerText = As4TestMessages.WithUnusedBitsSet(Convert.ToBase64String(der));
var act = () => SecurityTokenResolver.FromBinarySecurityToken(token);
act.Should().Throw().WithMessage("wsse:BinarySecurityToken is not valid base64.");
}
private static XmlElement Element(string localName, string text)
{
var doc = new XmlDocument();
var element = doc.CreateElement("ds", localName, WsSecurityNames.Ds);
element.InnerText = text;
return element;
}
}