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

Mark some S.S.C.Algorithms APIs as unsupported on ios/tvos #49762

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0126f6f
Not use AppleCryptoNative_EccGenerateKey on iOS/tvOS
MaximLipnin Mar 17, 2021
6e3e61f
Not use AppleCryptoNative_SecKeychainItemCopyKeychain, AppleCryptoNat…
MaximLipnin Mar 17, 2021
1a5d5ff
Not use AppleCryptoNative_RsaGenerateKey, AppleCryptoNative_RsaEncryp…
MaximLipnin Mar 18, 2021
1763124
Not use AppleCryptoNative_SecKeyImportEphemeral, AppleCryptoNative_Ge…
MaximLipnin Mar 19, 2021
b8653b4
Rename the property and use item groups
MaximLipnin Mar 22, 2021
53121a2
Remove redundant annotation
MaximLipnin Mar 23, 2021
aa655e5
Redundant line
MaximLipnin Mar 23, 2021
d802656
Remove redundant annotations
MaximLipnin Mar 24, 2021
d1a75b8
Address the review comments
MaximLipnin Mar 25, 2021
dba0695
Suppress the analyzer warning
MaximLipnin Mar 25, 2021
09df97a
Remove redundant annotations
MaximLipnin Mar 26, 2021
87fdecd
Remove usings
MaximLipnin Mar 26, 2021
c2ed4cc
Remove using
MaximLipnin Mar 26, 2021
fbf70d4
Remove using
MaximLipnin Mar 26, 2021
c8f77ae
Remove redundant annotations
MaximLipnin Mar 29, 2021
375047f
Use warning suppression instead of attributes for PNSE throwing metho…
MaximLipnin Mar 29, 2021
293d159
Add a link to the GH issue for enabling the native operations
MaximLipnin Apr 12, 2021
0deb6be
Extract some RSA-related methods to a shared part
MaximLipnin Apr 19, 2021
b34331b
Annotate the public base class (RSA type) and derived class (RSASecur…
MaximLipnin Apr 20, 2021
17bf76c
Address a couple more warnings
MaximLipnin Apr 20, 2021
2777fe2
Exclude the RSAXmlTEest trimming test on iOS/tvOS due to the unsuppor…
MaximLipnin Apr 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Security.Cryptography.Apple;

internal static partial class Interop
{
internal static partial class AppleCrypto
{
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_EccGetKeySizeInBits")]
internal static extern long EccGetKeySizeInBits(SafeSecKeyRefHandle publicKey);

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static void EccGenerateKey(
int keySizeInBits,
out SafeSecKeyRefHandle pPublicKey,
out SafeSecKeyRefHandle pPrivateKey)
{
throw new PlatformNotSupportedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Security.Cryptography.Apple;

using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class AppleCrypto
{
[DllImport(Libraries.AppleCryptoNative)]
private static extern int AppleCryptoNative_SecKeychainCopyDefault(out SafeKeychainHandle keychain);

[DllImport(Libraries.AppleCryptoNative)]
private static extern int AppleCryptoNative_SecKeychainOpen(
string keychainPath,
out SafeKeychainHandle keychain);

[DllImport(Libraries.AppleCryptoNative)]
private static extern int AppleCryptoNative_SecKeychainUnlock(
SafeKeychainHandle keychain,
int utf8PassphraseLength,
byte[] utf8Passphrase);

[DllImport(Libraries.AppleCryptoNative)]
private static extern int AppleCryptoNative_SecKeychainEnumerateCerts(
SafeKeychainHandle keychain,
out SafeCFArrayHandle matches,
out int pOSStatus);

[DllImport(Libraries.AppleCryptoNative)]
private static extern int AppleCryptoNative_SecKeychainEnumerateIdentities(
SafeKeychainHandle keychain,
out SafeCFArrayHandle matches,
out int pOSStatus);

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static SafeKeychainHandle SecKeychainItemCopyKeychain(SafeKeychainItemHandle item)
{
throw new PlatformNotSupportedException();
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static SafeKeychainHandle SecKeychainItemCopyKeychain(IntPtr item)
{
throw new PlatformNotSupportedException();
}

internal static SafeKeychainHandle SecKeychainCopyDefault()
{
SafeKeychainHandle keychain;
int osStatus = AppleCryptoNative_SecKeychainCopyDefault(out keychain);

if (osStatus == 0)
{
return keychain;
}

keychain.Dispose();
throw CreateExceptionForOSStatus(osStatus);
}

internal static SafeKeychainHandle SecKeychainOpen(string keychainPath)
{
SafeKeychainHandle keychain;
int osStatus = AppleCryptoNative_SecKeychainOpen(keychainPath, out keychain);

if (osStatus == 0)
{
return keychain;
}

keychain.Dispose();
throw CreateExceptionForOSStatus(osStatus);
}

internal static SafeCFArrayHandle KeychainEnumerateCerts(SafeKeychainHandle keychainHandle)
{
SafeCFArrayHandle matches;
int osStatus;
int result = AppleCryptoNative_SecKeychainEnumerateCerts(keychainHandle, out matches, out osStatus);

if (result == 1)
{
return matches;
}

matches.Dispose();

if (result == 0)
throw CreateExceptionForOSStatus(osStatus);

Debug.Fail($"Unexpected result from AppleCryptoNative_SecKeychainEnumerateCerts: {result}");
throw new CryptographicException();
}

internal static SafeCFArrayHandle KeychainEnumerateIdentities(SafeKeychainHandle keychainHandle)
{
SafeCFArrayHandle matches;
int osStatus;
int result = AppleCryptoNative_SecKeychainEnumerateIdentities(keychainHandle, out matches, out osStatus);

if (result == 1)
{
return matches;
}

matches.Dispose();

if (result == 0)
throw CreateExceptionForOSStatus(osStatus);

Debug.Fail($"Unexpected result from AppleCryptoNative_SecKeychainEnumerateCerts: {result}");
throw new CryptographicException();
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static SafeKeychainHandle CreateOrOpenKeychain(string keychainPath, bool createAllowed)
{
throw new PlatformNotSupportedException();
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static unsafe SafeTemporaryKeychainHandle CreateTemporaryKeychain()
{
throw new PlatformNotSupportedException();
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static void SecKeychainDelete(IntPtr handle, bool throwOnError=true)
{
throw new PlatformNotSupportedException();
}
}
}

namespace System.Security.Cryptography.Apple
{
internal class SafeKeychainItemHandle : SafeHandle
{
public SafeKeychainItemHandle()
: base(IntPtr.Zero, ownsHandle: true)
{
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
protected override bool ReleaseHandle()
{
SafeTemporaryKeychainHandle.UntrackItem(handle);
Interop.CoreFoundation.CFRelease(handle);
SetHandle(IntPtr.Zero);
return true;
}

public override bool IsInvalid => handle == IntPtr.Zero;
}

internal class SafeKeychainHandle : SafeHandle
{
public SafeKeychainHandle()
: base(IntPtr.Zero, ownsHandle: true)
{
}

internal SafeKeychainHandle(IntPtr handle)
: base(handle, ownsHandle: true)
{
}

protected override bool ReleaseHandle()
{
Interop.CoreFoundation.CFRelease(handle);
SetHandle(IntPtr.Zero);
return true;
}

public override bool IsInvalid => handle == IntPtr.Zero;
}

internal sealed class SafeTemporaryKeychainHandle : SafeKeychainHandle
{
private static readonly Dictionary<IntPtr, SafeTemporaryKeychainHandle> s_lookup =
new Dictionary<IntPtr, SafeTemporaryKeychainHandle>();

internal SafeTemporaryKeychainHandle()
{
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
protected override bool ReleaseHandle()
{
throw new PlatformNotSupportedException();
}

protected override void Dispose(bool disposing)
{
if (disposing && SafeHandleCache<SafeTemporaryKeychainHandle>.IsCachedInvalidHandle(this))
{
return;
}

base.Dispose(disposing);
}

public static SafeTemporaryKeychainHandle InvalidHandle =>
SafeHandleCache<SafeTemporaryKeychainHandle>.GetInvalidHandle(() => new SafeTemporaryKeychainHandle());

internal static void TrackKeychain(SafeTemporaryKeychainHandle toTrack)
{
if (toTrack.IsInvalid)
{
return;
}

lock (s_lookup)
{
Debug.Assert(!s_lookup.ContainsKey(toTrack.handle));

s_lookup[toTrack.handle] = toTrack;
}
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static void TrackItem(SafeKeychainItemHandle keychainItem)
{
throw new PlatformNotSupportedException();
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static void UntrackItem(IntPtr keychainItem)
{
throw new PlatformNotSupportedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Security.Cryptography.Apple;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class AppleCrypto
{
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static void RsaGenerateKey(
int keySizeInBits,
out SafeSecKeyRefHandle pPublicKey,
out SafeSecKeyRefHandle pPrivateKey)
{
throw new PlatformNotSupportedException();
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static byte[] RsaEncrypt(
SafeSecKeyRefHandle publicKey,
byte[] data,
RSAEncryptionPadding padding)
{
throw new PlatformNotSupportedException();
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static bool TryRsaEncrypt(
SafeSecKeyRefHandle publicKey,
ReadOnlySpan<byte> source,
Span<byte> destination,
RSAEncryptionPadding padding,
out int bytesWritten)
{
throw new PlatformNotSupportedException();
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static byte[] RsaDecrypt(
SafeSecKeyRefHandle privateKey,
byte[] data,
RSAEncryptionPadding padding)
{
throw new PlatformNotSupportedException();
}

[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
internal static bool TryRsaDecrypt(
SafeSecKeyRefHandle privateKey,
ReadOnlySpan<byte> source,
Span<byte> destination,
RSAEncryptionPadding padding,
out int bytesWritten)
{
throw new PlatformNotSupportedException();
}
}
}
Loading