Skip to content

Commit 865d4e6

Browse files
Cleanup for cert accessors (#118116)
1 parent 1aa2fd8 commit 865d4e6

File tree

11 files changed

+155
-88
lines changed

11 files changed

+155
-88
lines changed

src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Diagnostics;
65
using System.Runtime.InteropServices;
76
using System.Runtime.Versioning;
87
using System.Security.Cryptography;
9-
8+
using Internal.Cryptography;
109
using Microsoft.Win32.SafeHandles;
1110

1211
internal static partial class Interop
@@ -56,9 +55,7 @@ internal static unsafe ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject,
5655
{
5756
fixed (int* pResult = &result)
5857
{
59-
#if NETSTANDARD || NET
60-
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
61-
#endif
58+
Debug.Assert(Helpers.IsOSPlatformWindows);
6259

6360
ErrorCode errorCode = Interop.NCrypt.NCryptGetProperty(
6461
hObject,

src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsaImplementation.Windows.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Runtime.InteropServices;
4+
using Internal.Cryptography;
55

66
namespace System.Security.Cryptography
77
{
@@ -15,60 +15,50 @@ private CompositeMLDsaImplementation(CompositeMLDsaAlgorithm algorithm)
1515

1616
internal static partial bool SupportsAny()
1717
{
18-
#if !NETFRAMEWORK
19-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
18+
if (!Helpers.IsOSPlatformWindows)
2019
{
2120
return false;
2221
}
23-
#endif
2422

2523
return CompositeMLDsaManaged.SupportsAny();
2624
}
2725

2826
internal static partial bool IsAlgorithmSupportedImpl(CompositeMLDsaAlgorithm algorithm)
2927
{
30-
#if !NETFRAMEWORK
31-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
28+
if (!Helpers.IsOSPlatformWindows)
3229
{
3330
return false;
3431
}
35-
#endif
3632

3733
return CompositeMLDsaManaged.IsAlgorithmSupportedImpl(algorithm);
3834
}
3935

4036
internal static partial CompositeMLDsa GenerateKeyImpl(CompositeMLDsaAlgorithm algorithm)
4137
{
42-
#if !NETFRAMEWORK
43-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
38+
if (!Helpers.IsOSPlatformWindows)
4439
{
4540
throw new PlatformNotSupportedException();
4641
}
47-
#endif
4842

4943
return CompositeMLDsaManaged.GenerateKeyImpl(algorithm);
5044
}
5145

5246
internal static partial CompositeMLDsa ImportCompositeMLDsaPublicKeyImpl(CompositeMLDsaAlgorithm algorithm, ReadOnlySpan<byte> source)
5347
{
54-
#if !NETFRAMEWORK
55-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
48+
if (!Helpers.IsOSPlatformWindows)
5649
{
5750
throw new PlatformNotSupportedException();
5851
}
59-
#endif
6052

6153
return CompositeMLDsaManaged.ImportCompositeMLDsaPublicKeyImpl(algorithm, source);
6254
}
6355

6456
internal static partial CompositeMLDsa ImportCompositeMLDsaPrivateKeyImpl(CompositeMLDsaAlgorithm algorithm, ReadOnlySpan<byte> source)
6557
{
66-
#if !NETFRAMEWORK
67-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
58+
if (!Helpers.IsOSPlatformWindows)
6859
{
6960
throw new PlatformNotSupportedException();
7061
}
71-
#endif
7262

7363
return CompositeMLDsaManaged.ImportCompositeMLDsaPrivateKeyImpl(algorithm, source);
7464
}

src/libraries/Common/src/System/Security/Cryptography/Helpers.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ internal static partial class Helpers
5353
true;
5454
#endif
5555

56+
[SupportedOSPlatformGuard("windows")]
57+
internal static bool IsOSPlatformWindows =>
58+
#if NETFRAMEWORK
59+
true;
60+
#else
61+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
62+
#endif
63+
5664
[return: NotNullIfNotNull(nameof(src))]
5765
public static byte[]? CloneByteArray(this byte[]? src)
5866
{

src/libraries/Common/src/System/Security/Cryptography/MLDsaCng.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics.CodeAnalysis;
5-
using System.Runtime.InteropServices;
65
using System.Runtime.Versioning;
6+
using Internal.Cryptography;
77

88
namespace System.Security.Cryptography
99
{
@@ -50,12 +50,10 @@ public MLDsaCng(CngKey key)
5050

5151
private static MLDsaAlgorithm AlgorithmFromHandleWithPlatformCheck(CngKey key, out CngKey duplicateKey)
5252
{
53-
#if !NETFRAMEWORK
54-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
53+
if (!Helpers.IsOSPlatformWindows)
5554
{
5655
throw new PlatformNotSupportedException();
5756
}
58-
#endif
5957

6058
return AlgorithmFromHandle(key, out duplicateKey);
6159
}

src/libraries/Common/src/System/Security/Cryptography/MLDsaImplementation.Windows.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
6-
using System.Runtime.InteropServices;
6+
using Internal.Cryptography;
77
using Internal.NativeCrypto;
88
using Microsoft.Win32.SafeHandles;
99

@@ -248,12 +248,10 @@ private void ExportKey(string keyBlobType, int expectedKeySize, Span<byte> desti
248248

249249
private static SafeBCryptAlgorithmHandle? OpenAlgorithmHandle()
250250
{
251-
#if !NETFRAMEWORK
252-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
251+
if (!Helpers.IsOSPlatformWindows)
253252
{
254253
return null;
255254
}
256-
#endif
257255

258256
NTSTATUS status = Interop.BCrypt.BCryptOpenAlgorithmProvider(
259257
out SafeBCryptAlgorithmHandle hAlgorithm,

src/libraries/Common/src/System/Security/Cryptography/MLKemCng.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Diagnostics.CodeAnalysis;
5-
using System.Runtime.InteropServices;
64
using System.Runtime.Versioning;
5+
using Internal.Cryptography;
76

87
namespace System.Security.Cryptography
98
{
@@ -50,12 +49,10 @@ public MLKemCng(CngKey key) : base(AlgorithmFromHandleWithPlatformCheck(key, out
5049

5150
private static MLKemAlgorithm AlgorithmFromHandleWithPlatformCheck(CngKey key, out CngKey duplicateKey)
5251
{
53-
#if !NETFRAMEWORK
54-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
52+
if (!Helpers.IsOSPlatformWindows)
5553
{
5654
throw new PlatformNotSupportedException();
5755
}
58-
#endif
5956

6057
return AlgorithmFromHandle(key, out duplicateKey);
6158
}

src/libraries/Common/src/System/Security/Cryptography/MLKemImplementation.Windows.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
6-
using System.Runtime.InteropServices;
7-
using System.Text;
86
using Internal.NativeCrypto;
97
using Microsoft.Win32.SafeHandles;
108

119
using NTSTATUS = Interop.BCrypt.NTSTATUS;
1210
using KeyBlobMagicNumber = Interop.BCrypt.KeyBlobMagicNumber;
1311
using KeyBlobType = Interop.BCrypt.KeyBlobType;
1412
using BCRYPT_MLKEM_KEY_BLOB = Interop.BCrypt.BCRYPT_MLKEM_KEY_BLOB;
13+
using Internal.Cryptography;
1514

1615
namespace System.Security.Cryptography
1716
{
@@ -139,12 +138,10 @@ protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out
139138

140139
private static SafeBCryptAlgorithmHandle? OpenAlgorithmHandle()
141140
{
142-
#if !NETFRAMEWORK
143-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
141+
if (!Helpers.IsOSPlatformWindows)
144142
{
145143
return null;
146144
}
147-
#endif
148145

149146
NTSTATUS status = Interop.BCrypt.BCryptOpenAlgorithmProvider(
150147
out SafeBCryptAlgorithmHandle hAlgorithm,

0 commit comments

Comments
 (0)