dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.27k stars 4.73k forks source link

Desktop: System.Security.Cryptography.Xml.Tests.SigningVerifyingX509Cert.SignedXmlHasCertificateVerifiableSignature failed with "System.NotSupportedException" #21482

Closed KristinXie1 closed 4 years ago

KristinXie1 commented 7 years ago

Failed test: System.Security.Cryptography.Xml.Tests.SigningVerifyingX509Cert.SignedXmlHasCertificateVerifiableSignature

Detail: https://ci.dot.net/job/dotnet_corefx/job/master/job/outerloop_netfx_windows_nt_debug/39/testReport/System.Security.Cryptography.Xml.Tests/SigningVerifyingX509Cert/SignedXmlHasCertificateVerifiableSignature/

MESSAGE:

System.NotSupportedException : Method is not supported.

STACK TRACE:

at System.Security.Cryptography.RSA.DecryptValue(Byte[] rgb) 
at System.Security.Cryptography.Xml.SignedXml.ComputeSignature() 
at System.Security.Cryptography.Xml.Tests.SigningVerifyingX509Cert.SignXml(XmlDocument doc, AsymmetricAlgorithm key) 
in D:\j\workspace\outerloop_net---903ddde6\src\System.Security.Cryptography.Xml\tests\Samples\SigningVerifyingX509Cert.cs:line 38 
at System.Security.Cryptography.Xml.Tests.SigningVerifyingX509Cert.SignedXmlHasCertificateVerifiableSignature() 
in D:\j\workspace\outerloop_net---903ddde6\src\System.Security.Cryptography.Xml\tests\Samples\SigningVerifyingX509Cert.cs:line 73
karelz commented 7 years ago

Fixed in dotnet/corefx#19376

KristinXie1 commented 7 years ago

This issue is still repro: https://ci.dot.net/job/dotnet_corefx/job/master/job/outerloop_netfx_windows_nt_debug/45/testReport/System.Security.Cryptography.Xml.Tests/SigningVerifyingX509Cert/SignedXmlHasCertificateVerifiableSignature/

bartonjs commented 7 years ago

This is probably a if (key is RSACryptoServiceProvider)-ism which was fixed in net462.

krwq commented 7 years ago

@bartonjs I was trying to run this test on new desktop 4.5.2 project and getting compile errors:

is this expected?

I'm also not sure I understand if (key is RSACryptoServiceProvider) - what's the proper way to write this test then? (or what's the limitation on desktop?) Here is the skeleton of the test:

            using (X509Certificate2 x509cert = TestHelpers.GetSampleX509Certificate())
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.PreserveWhitespace = true;
                xmlDoc.LoadXml(ExampleXml);

                using (RSA key = x509cert.GetRSAPrivateKey())
                {
                    SignXml(xmlDoc, key); // calls key.DecryptValue
                }

                Assert.True(VerifyXml(xmlDoc.OuterXml, x509cert));
            }

all that SignedXml is trying to do is calling key.DecryptValue

bartonjs commented 7 years ago

@krwq Why net452? netstandard20 is net461, we don't support any lower than that.

The problem is https://referencesource.microsoft.com/#mscorlib/system/security/cryptography/rsapkcs1signatureformatter.cs,65. In net461 we didn't have the (middle) else-if. So it went from RSACryptoServiceProvider straight to RSA.DecryptValue (which has never been implemented by a .NET type).

So the whole RSA signing scenario here is busted for 4.6.1; it needs all of the SignedXml-related fixes we did in 4.6.2. While there is workable code that could be written, it's definitely not best-of-breed; it's worst-of-compat; so let's just disable the tests which fail in DecryptValue for net461.

krwq commented 7 years ago

@bartonjs - my bad with 4.5.2 🤦‍♂️ I've sent a PR disabling that test

karelz commented 7 years ago

@krwq which PR fixed it?

krwq commented 7 years ago

@karelz the one which says merged two posts above (#19500)