question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AsymmetricSignatureProvider abusively disposes RSACryptoServiceProvider instances it doesn't own on .NET Desktop

See original GitHub issue

On .NET Desktop, signing or verifying a token more than once when the security key is a RsaSecurityKey results in an exception if the underlying algorithm instance is a RSACryptoServiceProvider:

System.ObjectDisposedException: Le handle sécurisé a été fermé
   à System.Security.Cryptography.Utils._GetKeyParameter(SafeKeyHandle hKey, UInt32 paramID)
   à System.Security.Cryptography.RSACryptoServiceProvider.get_KeySize()
   à Microsoft.IdentityModel.Tokens.RsaSecurityKey.get_KeySize()
   à Microsoft.IdentityModel.Tokens.AsymmetricSignatureProvider.ValidateAsymmetricSecurityKeySize(SecurityKey key, String algorithm, Boolean willCreateSignatures)
   à Microsoft.IdentityModel.Tokens.AsymmetricSignatureProvider..ctor(SecurityKey key, String algorithm, Boolean willCreateSignatures)
   à Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateProvider(SecurityKey key, String algorithm, Boolean willCreateSignatures)
   à Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateForSigning(SecurityKey key, String algorithm)
   à System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.CreateEncodedSignature(String input, SigningCredentials signingCredentials)
   à System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.WriteToken(SecurityToken token)
 `AsymmetricSignatureProvider.Dispose()`

This bug is likely caused by Dispose(), that disposes the underlying RSACryptoServiceProvider instance without ensuring it owns it: https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/master/src/Microsoft.IdentityModel.Tokens/AsymmetricSignatureProvider.cs#L691

(it’s likely the same as the one an ASOS user experienced last month when trying to use a CSP HSM)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
brentschmaltzcommented, Sep 22, 2016

@mbliesath missed your comment. The library is disposing an rsa object it didn’t create. A possible workaround it we get this fixed is based on JwtSecurityTokenHandler calling CryptoProviderFactory.ReleaseSignatureProvider after creating the signature. You can set your own CPF and override ReleaseSignatureProvider. RSP is in the call graph that will dispose the rsa. SigningCredentials. CryptoProviderFactory is a convenient place to set your CustomProviderFactory.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AsymmetricSignatureProvider Class
Provides signature and verification operations for Asymmetric Algorithms using a SecurityKey.
Read more >
RSACryptoServiceProvider Class (System.Security. ...
Performs asymmetric encryption and decryption using the implementation of the RSA algorithm provided by the cryptographic service provider (CSP).
Read more >
Using Asymmetric Key on .Net Core - cryptography
macOS can store keys in a Keychain, but .NET Core doesn't provide API to read them out. Linux (OpenSSL) does not have any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found