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 6a397247f0fed9..2f4ff90b361306 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,6 +1,6 @@
- $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppPrevious)-windows;$(NetCoreAppPrevious);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)
+ $(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)
true
true
true
@@ -13,13 +13,11 @@ System.Security.Cryptography.ProtectedData
- $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))
true
true
- SR.PlatformNotSupported_CryptographyProtectedData
-
+
Link="Common\System\Security\Cryptography\CryptoThrowHelper.Windows.cs" />
-
+
@@ -50,4 +48,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 5ed0e5d4128453..b17f544a5f861e 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);
}
@@ -61,7 +67,11 @@ private static byte[] ProtectOrUnprotect(byte[] inputData, byte[]? optionalEntro
Interop.Crypt32.CryptUnprotectData(in userDataBlob, IntPtr.Zero, ref optionalEntropyBlob, IntPtr.Zero, IntPtr.Zero, flags, out outputBlob);
if (!success)
{
+#if NET
int lastWin32Error = Marshal.GetLastPInvokeError();
+#else
+ int lastWin32Error = Marshal.GetLastWin32Error();
+#endif
if (protect && ErrorMayBeCausedByUnloadedProfile(lastWin32Error))
throw new CryptographicException(SR.Cryptography_DpApi_ProfileMayNotBeLoaded);
else
@@ -102,5 +112,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)
+