|
| 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; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Security.Cryptography; |
| 8 | +using System.Security.Cryptography.Apple; |
| 9 | +using Microsoft.Win32.SafeHandles; |
| 10 | + |
| 11 | +internal static partial class Interop |
| 12 | +{ |
| 13 | + internal static partial class AppleCrypto |
| 14 | + { |
| 15 | + internal enum PAL_KeyAlgorithm : uint |
| 16 | + { |
| 17 | + Unknown = 0, |
| 18 | + EC = 1, |
| 19 | + RSA = 2, |
| 20 | + } |
| 21 | + |
| 22 | + internal static unsafe SafeSecKeyRefHandle CreateDataKey( |
| 23 | + ReadOnlySpan<byte> keyData, |
| 24 | + PAL_KeyAlgorithm keyAlgorithm, |
| 25 | + bool isPublic) |
| 26 | + { |
| 27 | + fixed (byte* pKey = keyData) |
| 28 | + { |
| 29 | + int result = AppleCryptoNative_SecKeyCreateWithData( |
| 30 | + pKey, |
| 31 | + keyData.Length, |
| 32 | + keyAlgorithm, |
| 33 | + isPublic ? 1 : 0, |
| 34 | + out SafeSecKeyRefHandle dataKey, |
| 35 | + out SafeCFErrorHandle errorHandle); |
| 36 | + |
| 37 | + using (errorHandle) |
| 38 | + { |
| 39 | + return result switch |
| 40 | + { |
| 41 | + kSuccess => dataKey, |
| 42 | + kErrorSeeError => throw CreateExceptionForCFError(errorHandle), |
| 43 | + _ => throw new CryptographicException { HResult = result } |
| 44 | + }; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + internal static byte[] SecKeyCopyExternalRepresentation( |
| 50 | + SafeSecKeyRefHandle key) |
| 51 | + { |
| 52 | + int result = AppleCryptoNative_SecKeyCopyExternalRepresentation( |
| 53 | + key, |
| 54 | + out SafeCFDataHandle data, |
| 55 | + out SafeCFErrorHandle errorHandle); |
| 56 | + |
| 57 | + using (errorHandle) |
| 58 | + using (data) |
| 59 | + { |
| 60 | + return result switch |
| 61 | + { |
| 62 | + kSuccess => CoreFoundation.CFGetData(data), |
| 63 | + kErrorSeeError => throw CreateExceptionForCFError(errorHandle), |
| 64 | + _ => throw new CryptographicException { HResult = result } |
| 65 | + }; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + [DllImport(Libraries.AppleCryptoNative)] |
| 70 | + private static unsafe extern int AppleCryptoNative_SecKeyCreateWithData( |
| 71 | + byte* pKey, |
| 72 | + int cbKey, |
| 73 | + PAL_KeyAlgorithm keyAlgorithm, |
| 74 | + int isPublic, |
| 75 | + out SafeSecKeyRefHandle pDataKey, |
| 76 | + out SafeCFErrorHandle pErrorOut); |
| 77 | + |
| 78 | + [DllImport(Libraries.AppleCryptoNative)] |
| 79 | + private static unsafe extern int AppleCryptoNative_SecKeyCopyExternalRepresentation( |
| 80 | + SafeSecKeyRefHandle key, |
| 81 | + out SafeCFDataHandle pDataOut, |
| 82 | + out SafeCFErrorHandle pErrorOut); |
| 83 | + |
| 84 | + [DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_SecKeyCopyPublicKey")] |
| 85 | + internal static unsafe extern SafeSecKeyRefHandle CopyPublicKey(SafeSecKeyRefHandle privateKey); |
| 86 | + } |
| 87 | +} |
0 commit comments