Skip to content

Commit 7690b0a

Browse files
authored
Use platform runtime check for ProtectedData (#80158)
* Use platform runtime check for ProtectedData * Remove duplicate TargetFramework
1 parent abc04f2 commit 7690b0a

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

Diff for: src/libraries/System.Security.Cryptography.ProtectedData/src/System.Security.Cryptography.ProtectedData.csproj

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppPrevious)-windows;$(NetCoreAppPrevious);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
3+
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
44
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
55
<IsPackable>true</IsPackable>
66
<AddXamarinPlaceholderFilesToPackage>true</AddXamarinPlaceholderFilesToPackage>
@@ -13,13 +13,11 @@ System.Security.Cryptography.ProtectedData</PackageDescription>
1313

1414
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
1515
<PropertyGroup>
16-
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
1716
<IsPartialFacadeAssembly Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETFramework'">true</IsPartialFacadeAssembly>
1817
<OmitResources Condition="'$(IsPartialFacadeAssembly)' == 'true'">true</OmitResources>
19-
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(IsPartialFacadeAssembly)' != 'true' and '$(TargetPlatformIdentifier)' != 'windows'">SR.PlatformNotSupported_CryptographyProtectedData</GeneratePlatformNotSupportedAssemblyMessage>
2018
</PropertyGroup>
2119

22-
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
20+
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true'">
2321
<Compile Include="System\Security\Cryptography\DataProtectionScope.cs" />
2422
<Compile Include="System\Security\Cryptography\ProtectedData.cs" />
2523
<Compile Include="$(CommonPath)Interop\Windows\Crypt32\Interop.CryptProtectData.cs"
@@ -42,12 +40,16 @@ System.Security.Cryptography.ProtectedData</PackageDescription>
4240
Link="Common\System\Security\Cryptography\CryptoThrowHelper.Windows.cs" />
4341
</ItemGroup>
4442

45-
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0-windows'))">
43+
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
4644
<Compile Include="$(CommonPath)DisableRuntimeMarshalling.cs"
4745
Link="Common\DisableRuntimeMarshalling.cs" />
4846
</ItemGroup>
4947

5048
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
5149
<Reference Include="System.Security" />
5250
</ItemGroup>
51+
52+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETStandard'">
53+
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
54+
</ItemGroup>
5355
</Project>

Diff for: src/libraries/System.Security.Cryptography.ProtectedData/src/System/Security/Cryptography/ProtectedData.cs

+20-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ public static partial class ProtectedData
1616

1717
public static byte[] Protect(byte[] userData, byte[]? optionalEntropy, DataProtectionScope scope)
1818
{
19-
ArgumentNullException.ThrowIfNull(userData);
19+
CheckPlatformSupport();
20+
21+
if (userData is null)
22+
throw new ArgumentNullException(nameof(userData));
2023

2124
return ProtectOrUnprotect(userData, optionalEntropy, scope, protect: true);
2225
}
2326

2427
public static byte[] Unprotect(byte[] encryptedData, byte[]? optionalEntropy, DataProtectionScope scope)
2528
{
26-
ArgumentNullException.ThrowIfNull(encryptedData);
29+
CheckPlatformSupport();
30+
31+
if (encryptedData is null)
32+
throw new ArgumentNullException(nameof(encryptedData));
2733

2834
return ProtectOrUnprotect(encryptedData, optionalEntropy, scope, protect: false);
2935
}
@@ -61,7 +67,11 @@ private static byte[] ProtectOrUnprotect(byte[] inputData, byte[]? optionalEntro
6167
Interop.Crypt32.CryptUnprotectData(in userDataBlob, IntPtr.Zero, ref optionalEntropyBlob, IntPtr.Zero, IntPtr.Zero, flags, out outputBlob);
6268
if (!success)
6369
{
70+
#if NET
6471
int lastWin32Error = Marshal.GetLastPInvokeError();
72+
#else
73+
int lastWin32Error = Marshal.GetLastWin32Error();
74+
#endif
6575
if (protect && ErrorMayBeCausedByUnloadedProfile(lastWin32Error))
6676
throw new CryptographicException(SR.Cryptography_DpApi_ProfileMayNotBeLoaded);
6777
else
@@ -102,5 +112,13 @@ private static bool ErrorMayBeCausedByUnloadedProfile(int errorCode)
102112
return errorCode == HResults.E_FILENOTFOUND ||
103113
errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND;
104114
}
115+
116+
private static void CheckPlatformSupport()
117+
{
118+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
119+
{
120+
throw new PlatformNotSupportedException();
121+
}
122+
}
105123
}
106124
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Security.Cryptography;
5+
6+
using Xunit;
7+
8+
namespace System.Security.Cryptography.ProtectedDataTests
9+
{
10+
[PlatformSpecific(~TestPlatforms.Windows)]
11+
public static class ProtectedUnsupportedDataTests
12+
{
13+
[Theory]
14+
[InlineData(DataProtectionScope.LocalMachine)]
15+
[InlineData(DataProtectionScope.CurrentUser)]
16+
public static void Protect_PlatformNotSupported(DataProtectionScope scope)
17+
{
18+
Assert.Throws<PlatformNotSupportedException>(() => ProtectedData.Protect(null, null, scope));
19+
}
20+
21+
[Theory]
22+
[InlineData(DataProtectionScope.LocalMachine)]
23+
[InlineData(DataProtectionScope.CurrentUser)]
24+
public static void Unprotect_PlatformNotSupported(DataProtectionScope scope)
25+
{
26+
Assert.Throws<PlatformNotSupportedException>(() => ProtectedData.Unprotect(null, null, scope));
27+
}
28+
}
29+
}

Diff for: src/libraries/System.Security.Cryptography.ProtectedData/tests/System.Security.Cryptography.ProtectedData.Tests.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4-
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetFrameworkMinimum)</TargetFrameworks>
4+
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkMinimum)</TargetFrameworks>
55
</PropertyGroup>
66
<ItemGroup>
77
<Compile Include="ProtectedDataTests.cs" />
8+
<Compile Include="ProtectedDataUnsupportedTests.cs" />
89
<Compile Include="$(CommonTestPath)System\Security\Cryptography\ByteUtils.cs"
910
Link="CommonTest\System\Security\Cryptography\ByteUtils.cs" />
1011
</ItemGroup>

0 commit comments

Comments
 (0)