Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DSA.Create, AesCcm, AesGcm, ChaCha20Poly1305 throw PNSE on iOS #52978

Merged
merged 16 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.Security.Cryptography
#if INTERNAL_ASYMMETRIC_IMPLEMENTATIONS
public partial class DSA : AsymmetricAlgorithm
{
public static new DSA Create()
private static DSA CreateCore()
{
return new DSAImplementation.DSAOpenSsl();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
namespace System.Security.Cryptography
{
#if INTERNAL_ASYMMETRIC_IMPLEMENTATIONS

public partial class DSA : AsymmetricAlgorithm
{
public static new DSA Create()
private static DSA CreateCore()
{
return new DSAImplementation.DSASecurityTransforms();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static bool OpenSslPresentOnSystem
{
get
{
if (IsAndroid || IsiOS || IstvOS || IsMacCatalyst || IsBrowser)
if (IsAndroid || UsesMobileAppleCrypto || IsBrowser)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public static bool IsNonZeroLowerBoundArraySupported
public static bool IsOpenSslSupported => IsLinux || IsFreeBSD || Isillumos || IsSolaris;

public static bool UsesAppleCrypto => IsOSX || IsMacCatalyst || IsiOS || IstvOS;
public static bool UsesMobileAppleCrypto => IsMacCatalyst || IsiOS || IstvOS;

// Changed to `true` when linking
public static bool IsBuiltWithAggressiveTrimming => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public abstract partial class Aes : System.Security.Cryptography.SymmetricAlgori
public static new System.Security.Cryptography.Aes? Create(string algorithmName) { throw null; }
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
filipnavara marked this conversation as resolved.
Show resolved Hide resolved
public sealed partial class AesCcm : System.IDisposable
{
public AesCcm(byte[] key) { }
Expand All @@ -29,6 +32,9 @@ public sealed partial class AesCcm : System.IDisposable
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>)) { }
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
public sealed partial class AesGcm : System.IDisposable
{
public AesGcm(byte[] key) { }
Expand Down Expand Up @@ -101,6 +107,9 @@ public abstract partial class AsymmetricSignatureFormatter
public abstract void SetKey(System.Security.Cryptography.AsymmetricAlgorithm key);
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
public sealed partial class ChaCha20Poly1305 : System.IDisposable
{
public ChaCha20Poly1305(byte[] key) { }
Expand Down Expand Up @@ -154,8 +163,17 @@ public abstract partial class DES : System.Security.Cryptography.SymmetricAlgori
public abstract partial class DSA : System.Security.Cryptography.AsymmetricAlgorithm
{
protected DSA() { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
public static new System.Security.Cryptography.DSA Create() { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
public static System.Security.Cryptography.DSA Create(int keySizeInBits) { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
public static System.Security.Cryptography.DSA Create(System.Security.Cryptography.DSAParameters parameters) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
public static new System.Security.Cryptography.DSA? Create(string algName) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,6 @@
Link="Common\Microsoft\Win32\SafeHandles\SafeCreateHandle.OSX.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeHandleCache.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeHandleCache.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\DSASecurityTransforms.cs"
Link="Common\System\Security\Cryptography\DSASecurityTransforms.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\EccSecurityTransforms.cs"
Link="Common\System\Security\Cryptography\EccSecurityTransforms.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanSecurityTransforms.cs"
Expand All @@ -546,6 +544,8 @@
Link="Common\Interop\OSX\System.Security.Cryptography.Native.Apple\Interop.Pbkdf2.cs" />
<Compile Include="$(CommonPath)Interop\OSX\System.Security.Cryptography.Native.Apple\Interop.SecKeyRef.macOS.cs"
Link="Common\Interop\OSX\System.Security.Cryptography.Native.Apple\Interop.SecKeyRef.macOS.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\DSASecurityTransforms.cs"
Link="Common\System\Security\Cryptography\DSASecurityTransforms.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\DSASecurityTransforms.macOS.cs"
Link="Common\System\Security\Cryptography\DSASecurityTransforms.macOS.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\EccSecurityTransforms.macOS.cs"
Expand All @@ -555,25 +555,27 @@
<Compile Include="Internal\Cryptography\Pbkdf2Implementation.OSX.cs" />
</ItemGroup>
<ItemGroup Condition="'$(UseAppleCrypto)' == 'true' AND '$(TargetsOSX)' != 'true'">
<Compile Include="$(CommonPath)System\Security\Cryptography\DSASecurityTransforms.iOS.cs"
Link="Common\System\Security\Cryptography\DSASecurityTransforms.iOS.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\EccSecurityTransforms.iOS.cs"
Link="Common\System\Security\Cryptography\EccSecurityTransforms.iOS.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\RSASecurityTransforms.iOS.cs"
Link="Common\System\Security\Cryptography\RSASecurityTransforms.iOS.cs" />
<Compile Include="Internal\Cryptography\Pbkdf2Implementation.Managed.cs" />
<Compile Include="System\Security\Cryptography\AesCcm.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\AesGcm.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\ChaCha20Poly1305.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\DSA.Create.NotSupported.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsUnix)' == 'true'">
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\AsymmetricAlgorithmHelpers.Hash.cs"
Link="Common\Internal\Cryptography\AsymmetricAlgorithmHelpers.Hash.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\ECDiffieHellmanDerivation.cs"
Link="Common\System\Security\Cryptography\ECDiffieHellmanDerivation.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true' and '$(UseAndroidCrypto)' != 'true'">
<ItemGroup Condition="'$(TargetsUnix)' == 'true' and '$(UseAndroidCrypto)' != 'true' and '$(TargetsiOS)' != 'true' and '$(TargetstvOS)' != 'true'">
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslAvailable.cs"
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslAvailable.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.EVP.Cipher.cs"
Expand All @@ -582,9 +584,9 @@
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ERR.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.Initialization.cs"
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.Initialization.cs" />
<Compile Include="System\Security\Cryptography\ChaCha20Poly1305.Unix.cs" />
<Compile Include="System\Security\Cryptography\AesCcm.Unix.cs" />
<Compile Include="System\Security\Cryptography\AesGcm.Unix.cs" />
<Compile Include="System\Security\Cryptography\ChaCha20Poly1305.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true' and '$(UseAndroidCrypto)' != 'true' and '$(UseAppleCrypto)' != 'true'">
<Compile Include="Internal\Cryptography\DesImplementation.Unix.cs" />
Expand Down Expand Up @@ -676,6 +678,8 @@
Link="Common\Interop\Android\System.Security.Cryptography.Native.Android\Interop.Rsa.cs" />
<Compile Include="$(CommonPath)Interop\Android\System.Security.Cryptography.Native.Android\SafeKeyHandle.cs"
Link="Common\Interop\Android\System.Security.Cryptography.Native.Android\SafeKeyHandle.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeEvpCipherCtxHandle.Unix.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\DSAAndroid.cs"
Link="Common\System\Security\Cryptography\DSAAndroid.cs" />
<Compile Include="System\Security\Cryptography\DSA.Create.Android.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace System.Security.Cryptography
{
public partial class AesCcm
{
public static bool IsSupported => false;

#if !BROWSER // allow GenFacades to handle browser target
private void ImportKey(ReadOnlySpan<byte> key)
{
Debug.Fail("Instance ctor should fail before we reach this point.");
throw new NotImplementedException();
}

private void EncryptCore(
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> plaintext,
Span<byte> ciphertext,
Span<byte> tag,
ReadOnlySpan<byte> associatedData = default)
{
Debug.Fail("Instance ctor should fail before we reach this point.");
throw new NotImplementedException();
}

private void DecryptCore(
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> ciphertext,
ReadOnlySpan<byte> tag,
Span<byte> plaintext,
ReadOnlySpan<byte> associatedData = default)
{
Debug.Fail("Instance ctor should fail before we reach this point.");
throw new NotImplementedException();
}

public void Dispose()
{
Debug.Fail("Instance ctor should fail before we reach this point.");
// no-op
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
namespace System.Security.Cryptography
{
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[UnsupportedOSPlatform("maccatalyst")]
public sealed partial class AesCcm : IDisposable
{
public static KeySizes NonceByteSizes { get; } = new KeySizes(7, 13, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace System.Security.Cryptography
{
public partial class AesGcm
{
public static bool IsSupported => false;

#if !BROWSER // allow GenFacades to handle browser target
private void ImportKey(ReadOnlySpan<byte> key)
{
Debug.Fail("Instance ctor should fail before we reach this point.");
throw new NotImplementedException();
}

private void EncryptCore(
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> plaintext,
Span<byte> ciphertext,
Span<byte> tag,
ReadOnlySpan<byte> associatedData = default)
{
Debug.Fail("Instance ctor should fail before we reach this point.");
throw new NotImplementedException();
}

private void DecryptCore(
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> ciphertext,
ReadOnlySpan<byte> tag,
Span<byte> plaintext,
ReadOnlySpan<byte> associatedData = default)
{
Debug.Fail("Instance ctor should fail before we reach this point.");
throw new NotImplementedException();
}

public void Dispose()
{
Debug.Fail("Instance ctor should fail before we reach this point.");
// no-op
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
namespace System.Security.Cryptography
{
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[UnsupportedOSPlatform("maccatalyst")]
public sealed partial class AesGcm : IDisposable
{
private const int NonceSize = 12;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
namespace System.Security.Cryptography
{
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[UnsupportedOSPlatform("maccatalyst")]
public sealed partial class ChaCha20Poly1305 : IDisposable
{
// Per https://tools.ietf.org/html/rfc7539, ChaCha20Poly1305 AEAD requires a 256-bit key and 96-bit nonce,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,13 @@ public partial class CryptoConfig
ht.Add("System.Security.Cryptography.RSA", RSACryptoServiceProviderType);
ht.Add("System.Security.Cryptography.AsymmetricAlgorithm", RSACryptoServiceProviderType);

ht.Add("DSA", DSACryptoServiceProviderType);
ht.Add("System.Security.Cryptography.DSA", DSACryptoServiceProviderType);
if (!OperatingSystem.IsIOS() &&
!OperatingSystem.IsTvOS() &&
!OperatingSystem.IsMacCatalyst())
{
ht.Add("DSA", DSACryptoServiceProviderType);
ht.Add("System.Security.Cryptography.DSA", DSACryptoServiceProviderType);
}

// Windows will register the public ECDsaCng type. Non-Windows gets a special handler.
if (OperatingSystem.IsWindows())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace System.Security.Cryptography
{
public partial class DSA : AsymmetricAlgorithm
{
public static new DSA Create()
private static DSA CreateCore()
{
return new DSAImplementation.DSAAndroid();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.Versioning;

namespace System.Security.Cryptography
{
public partial class DSA : AsymmetricAlgorithm
{
private static DSA CreateCore()
{
throw new PlatformNotSupportedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,20 @@ public abstract partial class DSA : AsymmetricAlgorithm
return (DSA?)CryptoConfig.CreateFromName(algName);
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[UnsupportedOSPlatform("maccatalyst")]
public static new DSA Create()
{
return CreateCore();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indirection is necessary because of the UnsupportedOSPlatform attributes which would otherwise have to be in 5 different places.


[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[UnsupportedOSPlatform("maccatalyst")]
public static DSA Create(int keySizeInBits)
{
DSA dsa = Create();
DSA dsa = CreateCore();

try
{
Expand All @@ -51,9 +62,12 @@ public static DSA Create(int keySizeInBits)
}
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[UnsupportedOSPlatform("maccatalyst")]
public static DSA Create(DSAParameters parameters)
{
DSA dsa = Create();
DSA dsa = CreateCore();

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Security.Cryptography
{
public partial class DSA : AsymmetricAlgorithm
{
public static new DSA Create()
private static DSA CreateCore()
{
return new DSAImplementation.DSACng();
}
Expand Down