-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[iOS] Implement DSA, RSA, EC key import/export (#51926)
- Loading branch information
1 parent
1a18528
commit 6a5bc53
Showing
38 changed files
with
1,535 additions
and
784 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/libraries/Common/src/Internal/Cryptography/AsymmetricAlgorithmHelpers.Ansi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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.Security.Cryptography; | ||
|
||
namespace Internal.Cryptography | ||
{ | ||
internal static partial class AsymmetricAlgorithmHelpers | ||
{ | ||
// Encodes a EC key as an uncompressed set of concatenated scalars, | ||
// optionally including the private key. To omit the private parameter, | ||
// "d" must have a length of zero. | ||
public static void EncodeToUncompressedAnsiX963Key( | ||
ReadOnlySpan<byte> x, | ||
ReadOnlySpan<byte> y, | ||
ReadOnlySpan<byte> d, | ||
Span<byte> destination) | ||
{ | ||
const byte UncompressedKeyPrefix = 0x04; | ||
if (x.Length != y.Length || (d.Length > 0 && d.Length != y.Length)) | ||
throw new CryptographicException(SR.Cryptography_NotValidPublicOrPrivateKey); | ||
|
||
int size = 1 + x.Length + y.Length + d.Length; // 0x04 || X || Y { || D } | ||
|
||
if (destination.Length < size) | ||
{ | ||
Debug.Fail("destination.Length < size"); | ||
throw new CryptographicException(); | ||
} | ||
|
||
destination[0] = UncompressedKeyPrefix; | ||
x.CopyTo(destination.Slice(1)); | ||
y.CopyTo(destination.Slice(1 + x.Length)); | ||
d.CopyTo(destination.Slice(1 + x.Length + y.Length)); | ||
} | ||
|
||
public static void DecodeFromUncompressedAnsiX963Key( | ||
ReadOnlySpan<byte> ansiKey, | ||
bool hasPrivateKey, | ||
out ECParameters ret) | ||
{ | ||
ret = default; | ||
|
||
const byte UncompressedKeyPrefix = 0x04; | ||
if (ansiKey.Length < 1 || ansiKey[0] != UncompressedKeyPrefix) | ||
throw new CryptographicException(SR.Cryptography_NotValidPublicOrPrivateKey); | ||
|
||
int fieldCount = hasPrivateKey ? 3 : 2; | ||
int fieldSize = (ansiKey.Length - 1) / fieldCount; | ||
|
||
if (ansiKey.Length != 1 + fieldSize * fieldCount) | ||
throw new CryptographicException(SR.Cryptography_NotValidPublicOrPrivateKey); | ||
|
||
ret.Q = new ECPoint { | ||
X = ansiKey.Slice(1, fieldSize).ToArray(), | ||
Y = ansiKey.Slice(1 + fieldSize, fieldSize).ToArray() | ||
}; | ||
|
||
if (hasPrivateKey) | ||
{ | ||
ret.D = ansiKey.Slice(1 + fieldSize + fieldSize, fieldSize).ToArray(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecKeyRef.iOS.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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.Security.Cryptography; | ||
using System.Security.Cryptography.Apple; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class AppleCrypto | ||
{ | ||
internal enum PAL_KeyAlgorithm : uint | ||
{ | ||
Unknown = 0, | ||
EC = 1, | ||
RSA = 2, | ||
} | ||
|
||
internal static unsafe SafeSecKeyRefHandle CreateDataKey( | ||
ReadOnlySpan<byte> keyData, | ||
PAL_KeyAlgorithm keyAlgorithm, | ||
bool isPublic) | ||
{ | ||
fixed (byte* pKey = keyData) | ||
{ | ||
int result = AppleCryptoNative_SecKeyCreateWithData( | ||
pKey, | ||
keyData.Length, | ||
keyAlgorithm, | ||
isPublic ? 1 : 0, | ||
out SafeSecKeyRefHandle dataKey, | ||
out SafeCFErrorHandle errorHandle); | ||
|
||
using (errorHandle) | ||
{ | ||
switch (result) | ||
{ | ||
case kSuccess: | ||
return dataKey; | ||
case kErrorSeeError: | ||
throw CreateExceptionForCFError(errorHandle); | ||
default: | ||
Debug.Fail($"SecKeyCreateWithData returned {result}"); | ||
throw new CryptographicException(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal static byte[] SecKeyCopyExternalRepresentation( | ||
SafeSecKeyRefHandle key) | ||
{ | ||
int result = AppleCryptoNative_SecKeyCopyExternalRepresentation( | ||
key, | ||
out SafeCFDataHandle data, | ||
out SafeCFErrorHandle errorHandle); | ||
|
||
using (errorHandle) | ||
using (data) | ||
{ | ||
switch (result) | ||
{ | ||
case kSuccess: | ||
return CoreFoundation.CFGetData(data); | ||
case kErrorSeeError: | ||
throw CreateExceptionForCFError(errorHandle); | ||
default: | ||
Debug.Fail($"SecKeyCopyExternalRepresentation returned {result}"); | ||
throw new CryptographicException(); | ||
} | ||
} | ||
} | ||
|
||
[DllImport(Libraries.AppleCryptoNative)] | ||
private static unsafe extern int AppleCryptoNative_SecKeyCreateWithData( | ||
byte* pKey, | ||
int cbKey, | ||
PAL_KeyAlgorithm keyAlgorithm, | ||
int isPublic, | ||
out SafeSecKeyRefHandle pDataKey, | ||
out SafeCFErrorHandle pErrorOut); | ||
|
||
[DllImport(Libraries.AppleCryptoNative)] | ||
private static unsafe extern int AppleCryptoNative_SecKeyCopyExternalRepresentation( | ||
SafeSecKeyRefHandle key, | ||
out SafeCFDataHandle pDataOut, | ||
out SafeCFErrorHandle pErrorOut); | ||
|
||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_SecKeyCopyPublicKey")] | ||
internal static unsafe extern SafeSecKeyRefHandle CopyPublicKey(SafeSecKeyRefHandle privateKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.