Why Enabling SHA256 Support for XML Signatures Breaks JWT Signing

For some times there’s been bug reports to Kentor.AuthServices, IdentityServer3 and System.IdentityModel.Tokens.Jwt about enabling SHA256 XML signature support sometimes breaks JWT signing. It fails with an error of System.Security.Cryptography.CryptographicException: Invalid algorithm specified.

This has been one of those annoying bugs where everyone’s solution works perfectly by itself, but combined they fail. I closed this issue in AuthServices with a comment that “works for us, has to be IdentityServer3/System.IdentityModel.Tokens doing something strange.”. I’ve finally had some time to look deeper into this thanks to IRM that asked me to do this as a consultancy service. Without someone paying for the time, it’s hard to spend the hours needed to find the root cause of a problem like this. When I started out on this I looked at all three systems/components involved to try to understand what triggers the problem. I ended up fixing this in Kentor.AuthServices for now. The fix could also have been done in the .NET Framework, IdentityServer3 or System.IdentityModel.Tokens.Jwt. Doing it in Kentor.AuthServices was mostly a matter of convenience because I control it myself.

That means that the TL;DR of all of this is that if you update to Kentor.AuthServices 0.19.0 or later this problem is solved. If you’re interested on how to solve it if you add SHA256 support yourself, please read on.

Global Registration of Crypto Algorithms

In .NET, crypto algorithms are held in a process wide registry in System.Security.Cryptography.CryptoConfig. For SignedXml.CheckSignature there is no way to inject the configuration or do it non-applicationwide. SignedXml.CheckSignedInfo and Reference.CalculateHashValue both call CryptoConfig.CreateFromName to resolve the algorithms specified in the XML. So to enable support for more algorithms in SignedXml, they have to be added to CryptoConfig. It’s promised that additional SHA versions will be included by default in .NET 4.6.2, but the release seems to be lagging behind schedule.

The problem with global registration is that it affects all users of the algorithm. In the case of SignedXml a common recommendation for adding SHA256 support is to register the existing System.Deployment.Internal.CodeSigning.RSAPKCS1SHA256SignatureDescription. This was what I did for Kentor.AuthServices. It works perfectly fine for validating signatures. But it only works for creating signatures if the certificate used has the right CSP (Crypto Service Provider).

Crypto Service Providers

In .NET (and the underlying Win32 CryptoAPI) a private key of a certificate is tied to a Crypto Service Provider when loaded. The most commonly used Crypto Service Provider is of type 1, PROV_RSA_FULL. Even though the .NET default is 24 which is PROV_RSA_AES. The commonly used PROV_RSA_FULL doesn’t support SHA256. So to get SHA256 support two things are needed:

  • The SHA256 algorithm must be registered
  • The private key from the certificate must be associated with a Crypto Service Provider that supports SHA256

Getting both of these right is a bit hard, so the .NET Framework contains some magic to help. The System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler uses X509AsymmetricSecurityKey.GetSignatureFormatter to get a signature formatter that is used to sign the data. First it looks among the registered algorithms. In the SHA256 case (which is the default algorithm for System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler) it is normally not found, so a fallback mechanism is activated. That fallback mechanism contains some serious magic that instantiates a SHA256 implementation and wraps the private key in a new RSACryptoServiceProvider associated with the PROV_RSA_AES Crypto Service Provider.

When the Magic is Lost

Having that magic in the fallback for when there is no registered SHA256 signature means that as soon as a SHA256 implementation is registered the magic is lost. I think that it is very confusing that it is the fallback mechanism that contains the magic. If there is to be any magic at all it should always be applied, both if a registered SHA256 implementation is found and if the fallback is used. Another option would be to clearly and early reject certificates with the wrong Crypto Service Provider.

But getting such a change into the .NET Framework would take more time than my customer is willing to accept. So I added the same magic to a custom SignatureDescription in Kentor.AuthServices.

A Magic SignatureDescription for SHA256

This is the final working signature description that both enables SHA256 support for XML Signatures and contains the magic to change Crypto Service Providers when generating signatures.

/// <summary>
/// Crypto description for a Managed implementation of SHA256 signatures.
/// </summary>
public class ManagedSHA256SignatureDescription : SignatureDescription
{
    /// <summary>
    /// Ctor
    /// </summary>
    public ManagedSHA256SignatureDescription()
    {
        KeyAlgorithm = typeof(RSACryptoServiceProvider).FullName;
        DigestAlgorithm = typeof(SHA256Managed).FullName;
    }
 
    /// <summary>
    /// Create a deformatter
    /// </summary>
    /// <param name="key">Key</param>
    /// <returns>Deformatter</returns>
    public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
    {
        if (key == null)
        {
            throw new ArgumentNullException(nameof(key));
        }
 
        var df = new RSAPKCS1SignatureDeformatter(key);
        df.SetHashAlgorithm(typeof(SHA256Managed).FullName);
        return df;
    }
 
    /// <summary>
    /// Create a formatter
    /// </summary>
    /// <param name="key">Key</param>
    /// <returns>Formatter</returns>
    public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
    {
        if (key == null)
        {
            throw new ArgumentNullException(nameof(key));
        }
 
        var provider = (RSACryptoServiceProvider)key;
 
        // The provider is probably using the default ProviderType. That's
        // a problem, because it doesn't support SHA256. Let's do some
        // black magic and create a new provider of a type that supports
        // SHA256 without the user ever knowing we fix this. This is what 
        // is done in X509AsymmetricKey.GetSignatureFormatter if 
        // http://www.w3.org/2001/04/xmldsig-more#rsa-sha256 isn't
        // a known algorithm, so users kind of expect this to be handled
        // for them magically.
 
        var cspParams = new CspParameters();
        cspParams.ProviderType = 24; //PROV_RSA_AES
        cspParams.KeyContainerName = provider.CspKeyContainerInfo.KeyContainerName;
        cspParams.KeyNumber = (int)provider.CspKeyContainerInfo.KeyNumber;
        if (provider.CspKeyContainerInfo.MachineKeyStore)
        {
            cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
        }
        cspParams.Flags |= CspProviderFlags.UseExistingKey;
 
        provider = new RSACryptoServiceProvider(cspParams);
 
        var f = new RSAPKCS1SignatureFormatter(provider);
        f.SetHashAlgorithm(typeof(SHA256Managed).FullName);
        return f;
    }
}
  • Pingback: Working with Box OpenAPI Clients – <CharlieDigital/> on 2017-09-15
  • Craig Stutzman on 2016-10-12

    Thanks for this! I’ve been banging my head against the wall as to why my app worked on some target platforms but not on others.

    I had already tried setting the first three of CspParameters you showed above, but not the CspProviderFlags (because I thought the KeyContainerName sufficed). I had also tried using the RSAPKCS1SignatureFormatter SetHashAlgorithm method, however, I used “SHA256” instead of the typeof construct you used above. Somewhere in the last three steps (starting with the CspProviderFlags) my problem was fixed.

    Thanks again!

  • Oscar on 2017-04-28

    Just wanted to say thanks, this was exactly the solution I was looking for! :)

  • Gretel Garcia on 2017-06-27

    Hi, thanks a lot for this solution it’s work great!.
    But now i have a problem with the CheckSignature() of the class SignedXML. I have an exception :
    “Signature description could not be created for the signature algorithm supplied” I suppose that it is because the SHA256.
    Any ideas of how can I fix this?
    Thanks in advance.

    • Anders Abel on 2017-08-31

      Looks like you didn’t properly register the new SignatureDescription with `CryptoConfig.AddAlgorithm()`

  • Leave a Reply

    Your name as it will be displayed on the posted comment.
    Your e-mail address will not be published. It is only used if I want to get in touch during comment moderation.
    Your name will be a link to this address.
Software Development is a Job – Coding is a Passion

I'm Anders Abel, an independent systems architect and developer in Stockholm, Sweden.

profile for Anders Abel at Stack Overflow, Q&A for professional and enthusiast programmers

Code for most posts is available on my GitHub account.

Popular Posts

Archives

Series

Powered by WordPress with the Passion for Coding theme.