Path: redb-route/tests/redb.Route.Tests.As4/SecurityTokenResolverIssuerSerialTests.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/SecurityTokenResolverIssuerSerialTests.cs
----
using System.Numerics;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using redb.Route.As4.Security;
namespace redb.Route.Tests.As4;
///
/// A ds:X509IssuerSerial reference finds our certificate whatever spacing the sender puts between the parts
/// of the issuer name: WSS4J and Domibus write RFC 2253 ("CN=a,OU=b,C=BE"), .NET writes "CN=a, OU=b, C=BE".
///
public class SecurityTokenResolverIssuerSerialTests
{
private const string Subject = "CN=Conformance Testing CA, OU=eDelivery, O=Building Blocks Office, C=BE";
private static readonly X509Certificate2 Ours = SelfSigned(Subject);
[Theory]
[InlineData("CN=Conformance Testing CA,OU=eDelivery,O=Building Blocks Office,C=BE")]
[InlineData("CN=Conformance Testing CA, OU=eDelivery, O=Building Blocks Office, C=BE")]
[InlineData("cn=Conformance Testing CA,ou=eDelivery,o=Building Blocks Office,c=BE")]
public void IssuerSerial_FindsOurCertificate_WhateverTheSpacingOfTheIssuerName(string issuerName)
{
var (keyInfo, envelope) = KeyInfo(issuerName, Serial(Ours));
var resolved = SecurityTokenResolver.Resolve(keyInfo, envelope, [Ours]);
resolved.Should().NotBeNull();
resolved!.Thumbprint.Should().Be(Ours.Thumbprint);
}
[Fact]
public void IssuerSerial_OfAnotherSerial_FindsNothing()
{
var (keyInfo, envelope) = KeyInfo("CN=Conformance Testing CA,OU=eDelivery,O=Building Blocks Office,C=BE",
Serial(Ours) + 1);
SecurityTokenResolver.Resolve(keyInfo, envelope, [Ours]).Should().BeNull();
}
[Fact]
public void IssuerSerial_OfAnotherIssuer_FindsNothing()
{
var (keyInfo, envelope) = KeyInfo("CN=Conformance Testing CA,OU=eDelivery,O=Building Blocks Office,C=DE",
Serial(Ours));
SecurityTokenResolver.Resolve(keyInfo, envelope, [Ours]).Should().BeNull();
}
private static (XmlElement KeyInfo, XmlDocument Envelope) KeyInfo(string issuerName, BigInteger serial)
{
var doc = new XmlDocument();
doc.LoadXml(
$"""
{issuerName}
{serial}
""");
return (doc.DocumentElement!, doc);
}
private static BigInteger Serial(X509Certificate2 certificate) =>
new(certificate.GetSerialNumber(), isUnsigned: true, isBigEndian: false);
private static X509Certificate2 SelfSigned(string subject)
{
using var rsa = RSA.Create(2048);
var request = new CertificateRequest(subject, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
using var created = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddYears(1));
return X509CertificateLoader.LoadPkcs12(created.Export(X509ContentType.Pfx), null, X509KeyStorageFlags.Exportable);
}
}