diff --git a/src/libraries/System.Security.Cryptography.ProtectedData/src/System.Security.Cryptography.ProtectedData.csproj b/src/libraries/System.Security.Cryptography.ProtectedData/src/System.Security.Cryptography.ProtectedData.csproj index 34ed55a968938f..4274dbc0d8be7b 100644 --- a/src/libraries/System.Security.Cryptography.ProtectedData/src/System.Security.Cryptography.ProtectedData.csproj +++ b/src/libraries/System.Security.Cryptography.ProtectedData/src/System.Security.Cryptography.ProtectedData.csproj @@ -1,9 +1,11 @@ - $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) + $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) true true true + true + 1 Provides access to Windows Data Protection Api. Commonly Used Types: @@ -13,13 +15,11 @@ System.Security.Cryptography.ProtectedData - $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) true true - SR.PlatformNotSupported_CryptographyProtectedData - + Link="Common\System\Security\Cryptography\CryptoThrowHelper.Windows.cs" /> - + @@ -50,4 +50,8 @@ System.Security.Cryptography.ProtectedData + + + + diff --git a/src/libraries/System.Security.Cryptography.ProtectedData/src/System/Security/Cryptography/ProtectedData.cs b/src/libraries/System.Security.Cryptography.ProtectedData/src/System/Security/Cryptography/ProtectedData.cs index 61bb88a6cbe53f..0779fc90fd668a 100644 --- a/src/libraries/System.Security.Cryptography.ProtectedData/src/System/Security/Cryptography/ProtectedData.cs +++ b/src/libraries/System.Security.Cryptography.ProtectedData/src/System/Security/Cryptography/ProtectedData.cs @@ -16,14 +16,20 @@ public static partial class ProtectedData public static byte[] Protect(byte[] userData, byte[]? optionalEntropy, DataProtectionScope scope) { - ArgumentNullException.ThrowIfNull(userData); + CheckPlatformSupport(); + + if (userData is null) + throw new ArgumentNullException(nameof(userData)); return ProtectOrUnprotect(userData, optionalEntropy, scope, protect: true); } public static byte[] Unprotect(byte[] encryptedData, byte[]? optionalEntropy, DataProtectionScope scope) { - ArgumentNullException.ThrowIfNull(encryptedData); + CheckPlatformSupport(); + + if (encryptedData is null) + throw new ArgumentNullException(nameof(encryptedData)); return ProtectOrUnprotect(encryptedData, optionalEntropy, scope, protect: false); } @@ -102,5 +108,13 @@ private static bool ErrorMayBeCausedByUnloadedProfile(int errorCode) return errorCode == HResults.E_FILENOTFOUND || errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND; } + + private static void CheckPlatformSupport() + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + throw new PlatformNotSupportedException(); + } + } } } diff --git a/src/libraries/System.Security.Cryptography.ProtectedData/tests/ProtectedDataUnsupportedTests.cs b/src/libraries/System.Security.Cryptography.ProtectedData/tests/ProtectedDataUnsupportedTests.cs new file mode 100644 index 00000000000000..641a6df2d38c80 --- /dev/null +++ b/src/libraries/System.Security.Cryptography.ProtectedData/tests/ProtectedDataUnsupportedTests.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Security.Cryptography; + +using Xunit; + +namespace System.Security.Cryptography.ProtectedDataTests +{ + [PlatformSpecific(~TestPlatforms.Windows)] + public static class ProtectedUnsupportedDataTests + { + [Theory] + [InlineData(DataProtectionScope.LocalMachine)] + [InlineData(DataProtectionScope.CurrentUser)] + public static void Protect_PlatformNotSupported(DataProtectionScope scope) + { + Assert.Throws(() => ProtectedData.Protect(null, null, scope)); + } + + [Theory] + [InlineData(DataProtectionScope.LocalMachine)] + [InlineData(DataProtectionScope.CurrentUser)] + public static void Unprotect_PlatformNotSupported(DataProtectionScope scope) + { + Assert.Throws(() => ProtectedData.Unprotect(null, null, scope)); + } + } +} diff --git a/src/libraries/System.Security.Cryptography.ProtectedData/tests/System.Security.Cryptography.ProtectedData.Tests.csproj b/src/libraries/System.Security.Cryptography.ProtectedData/tests/System.Security.Cryptography.ProtectedData.Tests.csproj index 61408ea83bcdb9..da449bd55a8456 100644 --- a/src/libraries/System.Security.Cryptography.ProtectedData/tests/System.Security.Cryptography.ProtectedData.Tests.csproj +++ b/src/libraries/System.Security.Cryptography.ProtectedData/tests/System.Security.Cryptography.ProtectedData.Tests.csproj @@ -1,10 +1,11 @@ true - $(NetCoreAppCurrent)-windows;$(NetFrameworkMinimum) + $(NetCoreAppCurrent);$(NetFrameworkMinimum) +