DataProtection - CreateEncryptor has System.NotSupportedException: Method is not supported
From @kevinlo on Friday, 19 October 2018 16:32:00
I have 4 apps that shared the data protection with certificate for cookie authentication using these codes:
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(imyDataProtectionPath))
.ProtectKeysWithCertificate(certificate)
.SetApplicationName(MyAppName);
App1 and App2 has target netcoreapp2.1
<TargetFramework>netcoreapp2.1</TargetFramework>
App3 and App4 has target net461 as they need to use some legacy .Net SDK components
<TargetFramework>net461</TargetFramework>
App1 will create a protected cookie, App2, App3 and App4 will read that cookie and unprotect it.
Before I add the .ProtectKeysWithCertificate(certificate), they all works fine. But after I add that ProtectKeysWithCertificate, in some of the servers, I get this exception in App2 and App3
20181018 20:47:16,761 WARN [DefaultKeyResolver] Key {4076b197-22b0-4b75-b43c-83b92caa08b5} is ineligible to be the default key because its CreateEncryptor method failed. [(null) (null):19 ]
System.NotSupportedException: Method is not supported.
at System.Security.Cryptography.RSA.DecryptValue(Byte[] rgb)
at System.Security.Cryptography.RSAPKCS1KeyExchangeDeformatter.DecryptKeyExchange(Byte[] rgbIn)
at Microsoft.AspNetCore.DataProtection.XmlEncryption.EncryptedXmlDecryptor.EncryptedXmlWithCertificateKeys.GetKeyFromCert(EncryptedKey encryptedKey, KeyInfoX509Data keyInfo)
at Microsoft.AspNetCore.DataProtection.XmlEncryption.EncryptedXmlDecryptor.EncryptedXmlWithCertificateKeys.DecryptEncryptedKey(EncryptedKey encryptedKey)
at System.Security.Cryptography.Xml.EncryptedXml.GetDecryptionKey(EncryptedData encryptedData, String symmetricAlgorithmUri)
at System.Security.Cryptography.Xml.EncryptedXml.DecryptDocument()
at Microsoft.AspNetCore.DataProtection.XmlEncryption.EncryptedXmlDecryptor.Decrypt(XElement encryptedElement)
at Microsoft.AspNetCore.DataProtection.XmlEncryption.XmlEncryptionExtensions.DecryptElement(XElement element, IActivator activator)
at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.IInternalXmlKeyManager.DeserializeDescriptorFromKeyElement(XElement keyElement)
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngGcmAuthenticatedEncryptorFactory.CreateEncryptorInstance(IKey key)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyBase.CreateEncryptor()
at Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver.CanCreateAuthenticatedEncryptor(IKey key)
App2 targeting .Net Core works fine, only App3 and App4 targeting .Net SDK has that exception.
However, it happens only a few machines, most machines are working fine. Is it related to the .Net SDK installed? I have .Net SDK 4.7.03062 and 4.6.01055 on those machines.
Copied from original issue: aspnet/AspNetCore#3676
From @natemcmaster on Friday, 19 October 2018 16:58:57
But after I add that ProtectKeysWithCertificate, in some of the servers, I get this exception in App2 and App3 App2 targeting .Net Core works fine, only App3 and App4 targeting .Net SDK has that exception.
Can you clarify which apps have the error? Is it only the apps which have <TargetFramework>net461</TargetFramework>? If so, this could be a bug in .NET Framework.
From @blowdart on Friday, 19 October 2018 17:17:14
And what operating system are the failed servers running?
From @kevinlo on Friday, 19 October 2018 17:46:14
Yes, only those apps target net461 has the error and the server is Win 2012 R2 Standard. It is running on a VM. Another VM with similar settings is working fine. At first, I think that is the .Net framework version as that machine is 4.6.01055. But then find out another machine with 4.6.01055 is working fine.
From @blowdart on Friday, 19 October 2018 17:58:17
Tagging @morganbr @bartonjs
These problems should have been fixed with .NET Framework 4.6.2. (RSAPKCS1KeyExchangeDeformatter.DecryptKeyExchange got fixed to accept CNG keys). Installing net462+ on the machines that still have only net461 will fix the problem.
We just install .Net SDK 4.72 at https://support.microsoft.com/en-us/help/4054530/microsoft-net-framework-4-7-2-offline-installer-for-windows and the problem is gone.
@bartonjs Do you have a Issue number so I can reference and make 4.6.2 a requirement?
See .NET 4.6.2 release notes: https://github.com/Microsoft/dotnet/blob/master/releases/net462/dotnet462-changes.md#bcl It is most likely this one (based on @bartonjs's reply above):
- System.Security.Cryptography RSAOAEPKeyExchangeDeformatter, RSAOAEPKeyExchangeFormatter, RSAPKCS1KeyExchangeDeformatters and RSAPKCS1KeyExchangeFormatter no longer fail when used with a CNG key. [190217]
Closing as answered.