.net Framework 4.6.2 adds support to sign XML Documents using RSA-SHA256
One of the hidden useful gems in the .net Framework is the System.Security.Cryptography.Xml.SignedXml class, which allows to sign XML documents, and validate the signature of signed XML documents.
In the process of implementing both a SAML 2.0 Service Provider library and an Identity Provider, I found that RSA-SHA256 signatures are common, but not straight forward. Validating them is relatively easy, add a reference to System.Deployment
and run this on app startup:
CryptoConfig.AddAlgorithm( typeof(RSAPKCS1SHA256SignatureDescription), "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
However, signing documents with a RSA-SHA256 private key yields a NotSupportedException
when calling SignedXml.ComputeSignature()
. Turns out that only .net Framework 4.6.2 will add support for the SHA2-family:
X509 Certificates Now Support FIPS 186-3 DSA
The .NET Framework 4.6.2 adds support for DSA (Digital Signature Algorithm) X509 certificates whose keys exceed the FIPS 186-2 limit of 1024-bit.
In addition to supporting the larger key sizes of FIPS 186-3, the .NET Framework 4.6.2 allows computing signatures with the SHA-2 family of hash algorithms (SHA256, SHA384, and SHA512). The FIPS 186-3 support is provided by the new DSACng class.
Keeping in line with recent changes to RSA (.NET Framework 4.6) and ECDsa (.NET Framework 4.6.1), the DSA abstract base class has additional methods to allow callers to make use of this functionality without casting.
After updating my system to the 4.6.2 preview, signing XML documents works flawlessly:
// exported is a byte[] that contains an exported cert incl. private key var myCert = new X509Certificate2(exported); var certPrivateKey = myCert.GetRSAPrivateKey(); var doc = new XmlDocument(); doc.LoadXml("<root><test1>Foo</test1><test2><bar baz=\"boom\">Real?</bar></test2></root>"); var signedXml = new SignedXml(doc); signedXml.SigningKey = certPrivateKey; Reference reference = new Reference(); reference.Uri = ""; XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform(); reference.AddTransform(env); signedXml.AddReference(reference); signedXml.ComputeSignature(); XmlElement xmlDigitalSignature = signedXml.GetXml(); doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true)); // doc is now a Signed XML document