diff options
author | Jeremy Barton <jbarton@microsoft.com> | 2017-05-09 21:41:17 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-09 21:41:17 +0300 |
commit | 21ca4d7954955878103ff33881f970c5d6820522 (patch) | |
tree | 3ee59e982d673457ed8bc62d1634727439b6df8a /src/System.Security.Cryptography.Xml | |
parent | 639ab08526437d108d8a41cf527d5bc9b56d2f4d (diff) |
Handle the PNSE when opening LocalMachine\My store on Linux (#19533)
When an X509Data is being used as a source of hunting for a store certificate
with a (matching) private key it fails on Linux with a
PlatformNotSupportedException when trying to open the LocalMachine\My
store.
Since the code was already resilient to CryptographicException from the store
open, add PlatformNotSupportedException to things it expects as ignorable.
Diffstat (limited to 'src/System.Security.Cryptography.Xml')
3 files changed, 44 insertions, 1 deletions
diff --git a/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs b/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs index 84dd7fde8e..0eda167c95 100644 --- a/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs +++ b/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs @@ -680,7 +680,10 @@ namespace System.Security.Cryptography.Xml } } } + // Store doesn't exist, no read permissions, other system error catch (CryptographicException) { } + // Opening LocalMachine stores (other than Root or CertificateAuthority) on Linux + catch (PlatformNotSupportedException) { } if (filters != null) collection.AddRange(filters); diff --git a/src/System.Security.Cryptography.Xml/tests/EncryptedXmlTests.cs b/src/System.Security.Cryptography.Xml/tests/EncryptedXmlTests.cs new file mode 100644 index 0000000000..1aafb813c4 --- /dev/null +++ b/src/System.Security.Cryptography.Xml/tests/EncryptedXmlTests.cs @@ -0,0 +1,39 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Security.Cryptography.X509Certificates; +using System.Xml; +using Xunit; + +namespace System.Security.Cryptography.Xml.Tests +{ + public static class EncryptedXmlTests + { + [Fact] + public static void DecryptWithCertificate_NotInStore() + { + const string SecretMessage = "Grilled cheese is tasty"; + + XmlDocument document = new XmlDocument(); + document.LoadXml($"<data><secret>{SecretMessage}</secret></data>"); + XmlElement toEncrypt = (XmlElement)document.DocumentElement.FirstChild; + + using (X509Certificate2 cert = TestHelpers.GetSampleX509Certificate()) + { + EncryptedXml encryptor = new EncryptedXml(document); + EncryptedData encryptedElement = encryptor.Encrypt(toEncrypt, cert); + EncryptedXml.ReplaceElement(toEncrypt, encryptedElement, false); + + XmlDocument document2 = new XmlDocument(); + document2.LoadXml(document.OuterXml); + + EncryptedXml decryptor = new EncryptedXml(document2); + + Assert.Throws<CryptographicException>(() => decryptor.DecryptDocument()); + Assert.DoesNotContain(SecretMessage, document2.OuterXml); + } + } + } +} diff --git a/src/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj b/src/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj index 70436465c1..bc23483698 100644 --- a/src/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj +++ b/src/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj @@ -18,6 +18,7 @@ <Compile Include="EncryptedXmlEqualityComparer.cs" /> <Compile Include="EncryptionMethodTests.cs" /> <Compile Include="EncryptedXmlTest.cs" /> + <Compile Include="EncryptedXmlTests.cs" /> <Compile Include="EncryptionPropertyCollectionTest.cs" /> <Compile Include="EncryptionPropertyTest.cs" /> <Compile Include="KeyInfoNameTest.cs" /> @@ -69,4 +70,4 @@ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Debug|AnyCPU'" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Release|AnyCPU'" /> <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> -</Project>
\ No newline at end of file +</Project> |