Skip to content

Commit 5c80887

Browse files
authored
Make DSA.Create, AesCcm, AesGcm, ChaCha20Poly1305 throw PNSE on iOS (#52978)
1 parent d8fec85 commit 5c80887

File tree

41 files changed

+291
-78
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+291
-78
lines changed

src/libraries/Common/src/Internal/Cryptography/Helpers.cs

+10
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44
using System;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Security.Cryptography;
7+
using System.Runtime.Versioning;
78

89
namespace Internal.Cryptography
910
{
1011
internal static partial class Helpers
1112
{
13+
#if NET5_0_OR_GREATER
14+
[UnsupportedOSPlatformGuard("ios")]
15+
[UnsupportedOSPlatformGuard("tvos")]
16+
[UnsupportedOSPlatformGuard("maccatalyst")]
17+
public static bool IsDSASupported => !OperatingSystem.IsIOS() && !OperatingSystem.IsTvOS() && !OperatingSystem.IsMacCatalyst();
18+
#else
19+
public static bool IsDSASupported => true;
20+
#endif
21+
1222
[return: NotNullIfNotNull("src")]
1323
public static byte[]? CloneByteArray(this byte[]? src)
1424
{

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace System.Security.Cryptography
1111
#if INTERNAL_ASYMMETRIC_IMPLEMENTATIONS
1212
public partial class DSA : AsymmetricAlgorithm
1313
{
14-
public static new DSA Create()
14+
private static DSA CreateCore()
1515
{
1616
return new DSAImplementation.DSAOpenSsl();
1717
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
namespace System.Security.Cryptography
1313
{
1414
#if INTERNAL_ASYMMETRIC_IMPLEMENTATIONS
15-
1615
public partial class DSA : AsymmetricAlgorithm
1716
{
18-
public static new DSA Create()
17+
private static DSA CreateCore()
1918
{
2019
return new DSAImplementation.DSASecurityTransforms();
2120
}

src/libraries/Common/src/System/Security/Cryptography/DSASecurityTransforms.iOS.cs

-25
This file was deleted.

src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static bool OpenSslPresentOnSystem
105105
{
106106
get
107107
{
108-
if (IsAndroid || IsiOS || IstvOS || IsMacCatalyst || IsBrowser)
108+
if (IsAndroid || UsesMobileAppleCrypto || IsBrowser)
109109
{
110110
return false;
111111
}

src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public static bool IsNonZeroLowerBoundArraySupported
138138
public static bool IsOpenSslSupported => IsLinux || IsFreeBSD || Isillumos || IsSolaris;
139139

140140
public static bool UsesAppleCrypto => IsOSX || IsMacCatalyst || IsiOS || IstvOS;
141+
public static bool UsesMobileAppleCrypto => IsMacCatalyst || IsiOS || IstvOS;
141142

142143
// Changed to `true` when linking
143144
public static bool IsBuiltWithAggressiveTrimming => false;

src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ protected Aes() { }
1515
public static new System.Security.Cryptography.Aes? Create(string algorithmName) { throw null; }
1616
}
1717
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
18+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
19+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
20+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
1821
public sealed partial class AesCcm : System.IDisposable
1922
{
2023
public AesCcm(byte[] key) { }
@@ -29,6 +32,9 @@ public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] ta
2932
public void Encrypt(System.ReadOnlySpan<byte> nonce, System.ReadOnlySpan<byte> plaintext, System.Span<byte> ciphertext, System.Span<byte> tag, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { }
3033
}
3134
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
35+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
36+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
37+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
3238
public sealed partial class AesGcm : System.IDisposable
3339
{
3440
public AesGcm(byte[] key) { }
@@ -101,6 +107,9 @@ protected AsymmetricSignatureFormatter() { }
101107
public abstract void SetKey(System.Security.Cryptography.AsymmetricAlgorithm key);
102108
}
103109
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
110+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
111+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
112+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
104113
public sealed partial class ChaCha20Poly1305 : System.IDisposable
105114
{
106115
public ChaCha20Poly1305(byte[] key) { }
@@ -154,8 +163,17 @@ protected DES() { }
154163
public abstract partial class DSA : System.Security.Cryptography.AsymmetricAlgorithm
155164
{
156165
protected DSA() { }
166+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
167+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
168+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
157169
public static new System.Security.Cryptography.DSA Create() { throw null; }
170+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
171+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
172+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
158173
public static System.Security.Cryptography.DSA Create(int keySizeInBits) { throw null; }
174+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
175+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
176+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
159177
public static System.Security.Cryptography.DSA Create(System.Security.Cryptography.DSAParameters parameters) { throw null; }
160178
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
161179
public static new System.Security.Cryptography.DSA? Create(string algName) { throw null; }

src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj

+12-8
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,6 @@
520520
Link="Common\Microsoft\Win32\SafeHandles\SafeCreateHandle.OSX.cs" />
521521
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeHandleCache.cs"
522522
Link="Common\Microsoft\Win32\SafeHandles\SafeHandleCache.cs" />
523-
<Compile Include="$(CommonPath)System\Security\Cryptography\DSASecurityTransforms.cs"
524-
Link="Common\System\Security\Cryptography\DSASecurityTransforms.cs" />
525523
<Compile Include="$(CommonPath)System\Security\Cryptography\EccSecurityTransforms.cs"
526524
Link="Common\System\Security\Cryptography\EccSecurityTransforms.cs" />
527525
<Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanSecurityTransforms.cs"
@@ -546,6 +544,8 @@
546544
Link="Common\Interop\OSX\System.Security.Cryptography.Native.Apple\Interop.Pbkdf2.cs" />
547545
<Compile Include="$(CommonPath)Interop\OSX\System.Security.Cryptography.Native.Apple\Interop.SecKeyRef.macOS.cs"
548546
Link="Common\Interop\OSX\System.Security.Cryptography.Native.Apple\Interop.SecKeyRef.macOS.cs" />
547+
<Compile Include="$(CommonPath)System\Security\Cryptography\DSASecurityTransforms.cs"
548+
Link="Common\System\Security\Cryptography\DSASecurityTransforms.cs" />
549549
<Compile Include="$(CommonPath)System\Security\Cryptography\DSASecurityTransforms.macOS.cs"
550550
Link="Common\System\Security\Cryptography\DSASecurityTransforms.macOS.cs" />
551551
<Compile Include="$(CommonPath)System\Security\Cryptography\EccSecurityTransforms.macOS.cs"
@@ -555,25 +555,27 @@
555555
<Compile Include="Internal\Cryptography\Pbkdf2Implementation.OSX.cs" />
556556
</ItemGroup>
557557
<ItemGroup Condition="'$(UseAppleCrypto)' == 'true' AND '$(TargetsOSX)' != 'true'">
558-
<Compile Include="$(CommonPath)System\Security\Cryptography\DSASecurityTransforms.iOS.cs"
559-
Link="Common\System\Security\Cryptography\DSASecurityTransforms.iOS.cs" />
560558
<Compile Include="$(CommonPath)System\Security\Cryptography\EccSecurityTransforms.iOS.cs"
561559
Link="Common\System\Security\Cryptography\EccSecurityTransforms.iOS.cs" />
562560
<Compile Include="$(CommonPath)System\Security\Cryptography\RSASecurityTransforms.iOS.cs"
563561
Link="Common\System\Security\Cryptography\RSASecurityTransforms.iOS.cs" />
564562
<Compile Include="Internal\Cryptography\Pbkdf2Implementation.Managed.cs" />
563+
<Compile Include="System\Security\Cryptography\AesCcm.NotSupported.cs" />
564+
<Compile Include="System\Security\Cryptography\AesGcm.NotSupported.cs" />
565+
<Compile Include="System\Security\Cryptography\ChaCha20Poly1305.NotSupported.cs" />
566+
<Compile Include="System\Security\Cryptography\DSA.Create.NotSupported.cs" />
565567
</ItemGroup>
566568
<ItemGroup Condition=" '$(TargetsUnix)' == 'true'">
567569
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
568570
Link="Common\Interop\Unix\Interop.Libraries.cs" />
569571
<Compile Include="$(CommonPath)Internal\Cryptography\AsymmetricAlgorithmHelpers.Hash.cs"
570572
Link="Common\Internal\Cryptography\AsymmetricAlgorithmHelpers.Hash.cs" />
571-
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs"
572-
Link="Common\Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs" />
573573
<Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanDerivation.cs"
574574
Link="Common\System\Security\Cryptography\ECDiffieHellmanDerivation.cs" />
575575
</ItemGroup>
576-
<ItemGroup Condition="'$(TargetsUnix)' == 'true' and '$(UseAndroidCrypto)' != 'true'">
576+
<ItemGroup Condition="'$(TargetsUnix)' == 'true' and '$(UseAndroidCrypto)' != 'true' and '$(TargetsiOS)' != 'true' and '$(TargetstvOS)' != 'true'">
577+
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs"
578+
Link="Common\Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs" />
577579
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslAvailable.cs"
578580
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslAvailable.cs" />
579581
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.EVP.Cipher.cs"
@@ -582,9 +584,9 @@
582584
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ERR.cs" />
583585
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.Initialization.cs"
584586
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.Initialization.cs" />
587+
<Compile Include="System\Security\Cryptography\ChaCha20Poly1305.Unix.cs" />
585588
<Compile Include="System\Security\Cryptography\AesCcm.Unix.cs" />
586589
<Compile Include="System\Security\Cryptography\AesGcm.Unix.cs" />
587-
<Compile Include="System\Security\Cryptography\ChaCha20Poly1305.Unix.cs" />
588590
</ItemGroup>
589591
<ItemGroup Condition="'$(TargetsUnix)' == 'true' and '$(UseAndroidCrypto)' != 'true' and '$(UseAppleCrypto)' != 'true'">
590592
<Compile Include="Internal\Cryptography\DesImplementation.Unix.cs" />
@@ -676,6 +678,8 @@
676678
Link="Common\Interop\Android\System.Security.Cryptography.Native.Android\Interop.Rsa.cs" />
677679
<Compile Include="$(CommonPath)Interop\Android\System.Security.Cryptography.Native.Android\SafeKeyHandle.cs"
678680
Link="Common\Interop\Android\System.Security.Cryptography.Native.Android\SafeKeyHandle.cs" />
681+
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs"
682+
Link="Common\Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs" />
679683
<Compile Include="$(CommonPath)System\Security\Cryptography\DSAAndroid.cs"
680684
Link="Common\System\Security\Cryptography\DSAAndroid.cs" />
681685
<Compile Include="System\Security\Cryptography\DSA.Create.Android.cs" />
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
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;
5+
46
namespace System.Security.Cryptography
57
{
68
public partial class AesCcm
79
{
810
public static bool IsSupported => false;
11+
12+
#if !BROWSER // allow GenFacades to handle browser target
13+
private void ImportKey(ReadOnlySpan<byte> key)
14+
{
15+
Debug.Fail("Instance ctor should fail before we reach this point.");
16+
throw new NotImplementedException();
17+
}
18+
19+
private void EncryptCore(
20+
ReadOnlySpan<byte> nonce,
21+
ReadOnlySpan<byte> plaintext,
22+
Span<byte> ciphertext,
23+
Span<byte> tag,
24+
ReadOnlySpan<byte> associatedData = default)
25+
{
26+
Debug.Fail("Instance ctor should fail before we reach this point.");
27+
throw new NotImplementedException();
28+
}
29+
30+
private void DecryptCore(
31+
ReadOnlySpan<byte> nonce,
32+
ReadOnlySpan<byte> ciphertext,
33+
ReadOnlySpan<byte> tag,
34+
Span<byte> plaintext,
35+
ReadOnlySpan<byte> associatedData = default)
36+
{
37+
Debug.Fail("Instance ctor should fail before we reach this point.");
38+
throw new NotImplementedException();
39+
}
40+
41+
public void Dispose()
42+
{
43+
Debug.Fail("Instance ctor should fail before we reach this point.");
44+
// no-op
45+
}
46+
#endif
947
}
1048
}

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.cs

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
namespace System.Security.Cryptography
88
{
99
[UnsupportedOSPlatform("browser")]
10+
[UnsupportedOSPlatform("ios")]
11+
[UnsupportedOSPlatform("tvos")]
12+
[UnsupportedOSPlatform("maccatalyst")]
1013
public sealed partial class AesCcm : IDisposable
1114
{
1215
public static KeySizes NonceByteSizes { get; } = new KeySizes(7, 13, 1);
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
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;
5+
46
namespace System.Security.Cryptography
57
{
68
public partial class AesGcm
79
{
810
public static bool IsSupported => false;
11+
12+
#if !BROWSER // allow GenFacades to handle browser target
13+
private void ImportKey(ReadOnlySpan<byte> key)
14+
{
15+
Debug.Fail("Instance ctor should fail before we reach this point.");
16+
throw new NotImplementedException();
17+
}
18+
19+
private void EncryptCore(
20+
ReadOnlySpan<byte> nonce,
21+
ReadOnlySpan<byte> plaintext,
22+
Span<byte> ciphertext,
23+
Span<byte> tag,
24+
ReadOnlySpan<byte> associatedData = default)
25+
{
26+
Debug.Fail("Instance ctor should fail before we reach this point.");
27+
throw new NotImplementedException();
28+
}
29+
30+
private void DecryptCore(
31+
ReadOnlySpan<byte> nonce,
32+
ReadOnlySpan<byte> ciphertext,
33+
ReadOnlySpan<byte> tag,
34+
Span<byte> plaintext,
35+
ReadOnlySpan<byte> associatedData = default)
36+
{
37+
Debug.Fail("Instance ctor should fail before we reach this point.");
38+
throw new NotImplementedException();
39+
}
40+
41+
public void Dispose()
42+
{
43+
Debug.Fail("Instance ctor should fail before we reach this point.");
44+
// no-op
45+
}
46+
#endif
947
}
1048
}

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.cs

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
namespace System.Security.Cryptography
88
{
99
[UnsupportedOSPlatform("browser")]
10+
[UnsupportedOSPlatform("ios")]
11+
[UnsupportedOSPlatform("tvos")]
12+
[UnsupportedOSPlatform("maccatalyst")]
1013
public sealed partial class AesGcm : IDisposable
1114
{
1215
private const int NonceSize = 12;

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ChaCha20Poly1305.cs

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
namespace System.Security.Cryptography
88
{
99
[UnsupportedOSPlatform("browser")]
10+
[UnsupportedOSPlatform("ios")]
11+
[UnsupportedOSPlatform("tvos")]
12+
[UnsupportedOSPlatform("maccatalyst")]
1013
public sealed partial class ChaCha20Poly1305 : IDisposable
1114
{
1215
// Per https://tools.ietf.org/html/rfc7539, ChaCha20Poly1305 AEAD requires a 256-bit key and 96-bit nonce,

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,13 @@ private static Dictionary<string, object> DefaultNameHT
185185
ht.Add("System.Security.Cryptography.RSA", RSACryptoServiceProviderType);
186186
ht.Add("System.Security.Cryptography.AsymmetricAlgorithm", RSACryptoServiceProviderType);
187187

188-
ht.Add("DSA", DSACryptoServiceProviderType);
189-
ht.Add("System.Security.Cryptography.DSA", DSACryptoServiceProviderType);
188+
if (!OperatingSystem.IsIOS() &&
189+
!OperatingSystem.IsTvOS() &&
190+
!OperatingSystem.IsMacCatalyst())
191+
{
192+
ht.Add("DSA", DSACryptoServiceProviderType);
193+
ht.Add("System.Security.Cryptography.DSA", DSACryptoServiceProviderType);
194+
}
190195

191196
// Windows will register the public ECDsaCng type. Non-Windows gets a special handler.
192197
if (OperatingSystem.IsWindows())

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.Create.Android.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace System.Security.Cryptography
55
{
66
public partial class DSA : AsymmetricAlgorithm
77
{
8-
public static new DSA Create()
8+
private static DSA CreateCore()
99
{
1010
return new DSAImplementation.DSAAndroid();
1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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.Runtime.Versioning;
5+
6+
namespace System.Security.Cryptography
7+
{
8+
public partial class DSA : AsymmetricAlgorithm
9+
{
10+
private static DSA CreateCore()
11+
{
12+
throw new PlatformNotSupportedException();
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)