Path: redb-route/redb.Route.Soap/SoapEncryption.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.Soap/SoapEncryption.cs
----
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Xml;
namespace redb.Route.Soap;
///
/// WS-Security XML-Encryption of the SOAP Body payload (in-box ), in the layout real
/// WSS stacks (WCF, Apache CXF/WSS4J) produce and expect: the payload is encrypted with a per-message AES-256
/// key, the <xenc:EncryptedKey> (that key RSA-wrapped to the recipient) lives in the
/// <wsse:Security> header and points at the <xenc:EncryptedData> in the Body through a
/// <xenc:ReferenceList>/<xenc:DataReference>. Decryption also accepts the older inline
/// layout (EncryptedKey nested in EncryptedData) for backward compatibility. Runs on the patched
/// System.Security.Cryptography.Xml 9.0.18 (CVE-2026-50648 fixed).
///
internal static class SoapEncryption
{
public const string XmlEncNs = "http://www.w3.org/2001/04/xmlenc#";
/// Encrypts the Body's payload element to the recipient's certificate (standard WSS layout).
public static void EncryptBody(XmlDocument doc, X509Certificate2 recipientCert, SoapVersion version)
{
var soapNs = SoapEnvelope.Ns(version).NamespaceName;
var body = (XmlElement)(doc.GetElementsByTagName("Body", soapNs)[0]
?? throw new InvalidOperationException("No to encrypt."));
// ALL child elements, not just the first: a document/literal Body can carry several, and encrypting
// only the first would leak the rest in cleartext. They share one session key (standard WSS).
var payloads = body.ChildNodes.OfType().ToList();
if (payloads.Count == 0)
throw new InvalidOperationException("Empty — nothing to encrypt.");
// 1. Fresh AES-256 session key, RSA-wrapped to the recipient (OAEP). The EncryptedKey's ReferenceList
// points at every EncryptedData below.
using var sessionKey = Aes.Create();
sessionKey.KeySize = 256;
using var rsa = recipientCert.GetRSAPublicKey()
?? throw new InvalidOperationException("Recipient certificate has no RSA public key.");
var wrappedKey = EncryptedXml.EncryptKey(sessionKey.Key, rsa, useOAEP: true);
var encryptedKey = new EncryptedKey
{
EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSAOAEPUrl),
CipherData = new CipherData(wrappedKey),
};
encryptedKey.KeyInfo.AddClause(new KeyInfoX509Data(recipientCert)); // recipient hint
// 2. Encrypt each Body child in place; reference each EncryptedData from the EncryptedKey.
var encXml = new EncryptedXml();
foreach (var payload in payloads)
{
var cipher = encXml.EncryptData(payload, sessionKey, content: false);
var edId = "ED-" + Guid.NewGuid().ToString("N");
var encryptedData = new EncryptedData
{
Type = EncryptedXml.XmlEncElementUrl, // whole element replaced
Id = edId,
EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url),
CipherData = new CipherData(cipher),
};
encryptedKey.AddReference(new DataReference("#" + edId));
EncryptedXml.ReplaceElement(payload, encryptedData, content: false);
}
// 3. Put the EncryptedKey in the Security header.
var security = SoapSignature.EnsureSecurityHeader(doc, version);
security.AppendChild(doc.ImportNode(encryptedKey.GetXml(), true));
}
/// Whether the document carries an EncryptedData element.
public static bool HasEncryptedData(XmlDocument doc) =>
doc.GetElementsByTagName("EncryptedData", XmlEncNs).Count > 0;
///
/// Decrypts the Body payload in place using our private key. Accepts both the standard WSS layout
/// (EncryptedKey in the Security header with a ReferenceList) and the legacy inline layout.
///
public static void DecryptBody(XmlDocument doc, X509Certificate2 ourCertWithKey)
{
var encKeyEl = (XmlElement)(doc.GetElementsByTagName("EncryptedKey", XmlEncNs)[0]
?? throw new InvalidOperationException("No EncryptedKey to decrypt."));
RefuseNonCanonical(encKeyEl);
var encKey = new EncryptedKey();
encKey.LoadXml(encKeyEl);
using var rsa = ourCertWithKey.GetRSAPrivateKey()
?? throw new InvalidOperationException("Decryption certificate has no RSA private key.");
var useOaep = encKey.EncryptionMethod?.KeyAlgorithm == EncryptedXml.XmlEncRSAOAEPUrl;
var sessionKey = EncryptedXml.DecryptKey(encKey.CipherData.CipherValue, rsa, useOaep);
// Resolve the EncryptedData(s) the key references; fall back to every EncryptedData in the document.
var targets = new List();
foreach (EncryptedReference reference in encKey.ReferenceList)
if (reference.Uri is { Length: > 1 } uri && uri[0] == '#' && FindById(doc, uri[1..]) is { } el)
targets.Add(el);
if (targets.Count == 0)
targets.AddRange(doc.GetElementsByTagName("EncryptedData", XmlEncNs).OfType());
foreach (var edEl in targets)
RefuseNonCanonical(edEl);
foreach (var edEl in targets)
{
var encData = new EncryptedData();
encData.LoadXml(edEl);
using var alg = CreateSymmetric(encData.EncryptionMethod?.KeyAlgorithm);
alg.Key = sessionKey;
var eXml = new EncryptedXml(doc);
var plain = eXml.DecryptData(encData, alg);
eXml.ReplaceData(edEl, plain);
}
}
/// base64Binary has one spelling: a value with non-zero unused bits is refused on every runtime before EncryptedXml decodes it.
private static void RefuseNonCanonical(XmlElement element)
{
if (SoapBase64.FirstNonCanonical(element) is { } bad)
throw new CryptographicException($"{bad.Name} is not canonical base64 (non-zero unused bits in its last character).");
}
private static XmlElement? FindById(XmlDocument doc, string id) =>
doc.GetElementsByTagName("EncryptedData", XmlEncNs)
.OfType()
.FirstOrDefault(e => e.GetAttribute("Id") == id);
private static SymmetricAlgorithm CreateSymmetric(string? algorithm)
{
var aes = Aes.Create();
aes.KeySize = algorithm switch
{
EncryptedXml.XmlEncAES128Url => 128,
EncryptedXml.XmlEncAES192Url => 192,
_ => 256, // AES-256 default (also our own EncryptBody)
};
return aes;
}
}