Skip to content

Commit 66c2604

Browse files
authored
Work around the RSACryptoServiceProviderProxy crash on mono (#1287)
Mono identifies as sha1 provider but is capable of all operations (i.e. does not require a workaround). The workaround must be skipped on mono because it crashes there.
1 parent b9c40c0 commit 66c2604

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/Microsoft.IdentityModel.Tokens/RsaCryptoServiceProviderProxy.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ public RSACryptoServiceProviderProxy(RSACryptoServiceProvider rsa)
8383
// Level up the provider type only if:
8484
// 1. it is PROV_RSA_FULL or PROV_RSA_SCHANNEL which denote CSPs that only understand Sha1 algorithms
8585
// 2. it is not associated with a hardware key
86-
if ((rsa.CspKeyContainerInfo.ProviderType == PROV_RSA_FULL || rsa.CspKeyContainerInfo.ProviderType == PROV_RSA_SCHANNEL) && !rsa.CspKeyContainerInfo.HardwareDevice)
86+
// 3. we are not running on mono (which reports PROV_RSA_FULL but doesn't need a workaround)
87+
var isSha1Provider = rsa.CspKeyContainerInfo.ProviderType == PROV_RSA_FULL || rsa.CspKeyContainerInfo.ProviderType == PROV_RSA_SCHANNEL;
88+
var isMono = Type.GetType("Mono.Runtime") != null;
89+
if (isSha1Provider && !rsa.CspKeyContainerInfo.HardwareDevice)
8790
{
8891
var csp = new CspParameters();
8992
csp.ProviderType = PROV_RSA_AES;
@@ -96,10 +99,18 @@ public RSACryptoServiceProviderProxy(RSACryptoServiceProvider rsa)
9699
// With this flag, a CryptographicException is thrown instead.
97100
csp.Flags |= CspProviderFlags.UseExistingKey;
98101

99-
_rsa = new RSACryptoServiceProvider(csp);
100-
101-
// since we created a new RsaCryptoServiceProvider we need to dispose it
102-
_disposeRsa = true;
102+
try
103+
{
104+
_rsa = new RSACryptoServiceProvider(csp);
105+
// since we created a new RsaCryptoServiceProvider we need to dispose it
106+
_disposeRsa = true;
107+
}
108+
catch (CryptographicException) when (isMono)
109+
{
110+
// On mono, this exception is expected behavior.
111+
// The solution is to simply not level up the provider as this workaround is not needed on mono.
112+
_rsa = rsa;
113+
}
103114
}
104115
else
105116
{

0 commit comments

Comments
 (0)