You've already forked linux-packaging-mono
Imported Upstream version 5.10.0.47
Former-commit-id: d0813289fa2d35e1f8ed77530acb4fb1df441bc0
This commit is contained in:
parent
88ff76fe28
commit
e46a49ecf1
8
external/corefx/src/Common/Common.Tests.sln
vendored
8
external/corefx/src/Common/Common.Tests.sln
vendored
@@ -11,10 +11,10 @@ Global
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{C72FD34C-539A-4447-9796-62A229571199}.Debug|Any CPU.ActiveCfg = netstandard-Windows_NT-Debug|Any CPU
|
||||
{C72FD34C-539A-4447-9796-62A229571199}.Debug|Any CPU.Build.0 = netstandard-Windows_NT-Debug|Any CPU
|
||||
{C72FD34C-539A-4447-9796-62A229571199}.Release|Any CPU.ActiveCfg = netstandard-Windows_NT-Release|Any CPU
|
||||
{C72FD34C-539A-4447-9796-62A229571199}.Release|Any CPU.Build.0 = netstandard-Windows_NT-Release|Any CPU
|
||||
{C72FD34C-539A-4447-9796-62A229571199}.Debug|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Debug|Any CPU
|
||||
{C72FD34C-539A-4447-9796-62A229571199}.Debug|Any CPU.Build.0 = netcoreapp-Windows_NT-Debug|Any CPU
|
||||
{C72FD34C-539A-4447-9796-62A229571199}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU
|
||||
{C72FD34C-539A-4447-9796-62A229571199}.Release|Any CPU.Build.0 = netcoreapp-Windows_NT-Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -17,9 +17,8 @@ namespace Internal.Cryptography
|
||||
/// <summary>
|
||||
/// Convert Ieee1363 format of (r, s) to Der format
|
||||
/// </summary>
|
||||
public static byte[] ConvertIeee1363ToDer(byte[] input)
|
||||
public static byte[] ConvertIeee1363ToDer(ReadOnlySpan<byte> input)
|
||||
{
|
||||
Debug.Assert(input != null);
|
||||
Debug.Assert(input.Length % 2 == 0);
|
||||
Debug.Assert(input.Length > 1);
|
||||
|
||||
@@ -27,8 +26,8 @@ namespace Internal.Cryptography
|
||||
// Output is the DER encoded value of CONSTRUCTEDSEQUENCE(INTEGER(r), INTEGER(s)).
|
||||
int halfLength = input.Length / 2;
|
||||
|
||||
byte[][] rEncoded = DerEncoder.SegmentedEncodeUnsignedInteger(input, 0, halfLength);
|
||||
byte[][] sEncoded = DerEncoder.SegmentedEncodeUnsignedInteger(input, halfLength, halfLength);
|
||||
byte[][] rEncoded = DerEncoder.SegmentedEncodeUnsignedInteger(input.Slice(0, halfLength));
|
||||
byte[][] sEncoded = DerEncoder.SegmentedEncodeUnsignedInteger(input.Slice(halfLength, halfLength));
|
||||
|
||||
return DerEncoder.ConstructSequence(rEncoded, sEncoded);
|
||||
}
|
||||
|
||||
@@ -40,38 +40,25 @@ namespace Internal.Cryptography
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
|
||||
{
|
||||
// The classes that call us are sealed and their base class has checked this already.
|
||||
Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));
|
||||
|
||||
using (HashAlgorithm hasher = GetHashAlgorithm(hashAlgorithm))
|
||||
{
|
||||
return hasher.TryComputeHash(source, destination, out bytesWritten);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5351", Justification = "MD5 is used when the user asks for it.")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5350", Justification = "SHA1 is used when the user asks for it.")]
|
||||
private static HashAlgorithm GetHashAlgorithm(HashAlgorithmName hashAlgorithmName)
|
||||
{
|
||||
HashAlgorithm hasher;
|
||||
|
||||
if (hashAlgorithmName == HashAlgorithmName.MD5)
|
||||
{
|
||||
hasher = MD5.Create();
|
||||
}
|
||||
else if (hashAlgorithmName == HashAlgorithmName.SHA1)
|
||||
{
|
||||
hasher = SHA1.Create();
|
||||
}
|
||||
else if (hashAlgorithmName == HashAlgorithmName.SHA256)
|
||||
{
|
||||
hasher = SHA256.Create();
|
||||
}
|
||||
else if (hashAlgorithmName == HashAlgorithmName.SHA384)
|
||||
{
|
||||
hasher = SHA384.Create();
|
||||
}
|
||||
else if (hashAlgorithmName == HashAlgorithmName.SHA512)
|
||||
{
|
||||
hasher = SHA512.Create();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmName.Name);
|
||||
}
|
||||
|
||||
return hasher;
|
||||
}
|
||||
private static HashAlgorithm GetHashAlgorithm(HashAlgorithmName hashAlgorithmName) =>
|
||||
hashAlgorithmName == HashAlgorithmName.MD5 ? MD5.Create() :
|
||||
hashAlgorithmName == HashAlgorithmName.SHA1 ? SHA1.Create() :
|
||||
hashAlgorithmName == HashAlgorithmName.SHA256 ? SHA256.Create() :
|
||||
hashAlgorithmName == HashAlgorithmName.SHA384 ? SHA384.Create() :
|
||||
hashAlgorithmName == HashAlgorithmName.SHA512 ? (HashAlgorithm)SHA512.Create() :
|
||||
throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmName.Name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,11 +19,27 @@ namespace Internal.Cryptography
|
||||
Debug.Assert(count >= 0 && count <= data.Length);
|
||||
Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));
|
||||
|
||||
using (HashProviderCng hashProvider = new HashProviderCng(hashAlgorithm.Name, null))
|
||||
using (var hashProvider = new HashProviderCng(hashAlgorithm.Name, null))
|
||||
{
|
||||
hashProvider.AppendHashData(data, offset, count);
|
||||
byte[] hash = hashProvider.FinalizeHashAndReset();
|
||||
return hash;
|
||||
return hashProvider.FinalizeHashAndReset();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
|
||||
{
|
||||
Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));
|
||||
|
||||
using (var hashProvider = new HashProviderCng(hashAlgorithm.Name, null))
|
||||
{
|
||||
if (destination.Length < hashProvider.HashSizeInBytes)
|
||||
{
|
||||
bytesWritten = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
hashProvider.AppendHashData(source);
|
||||
return hashProvider.TryFinalizeHashAndReset(destination, out bytesWritten);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +49,7 @@ namespace Internal.Cryptography
|
||||
Debug.Assert(data != null);
|
||||
Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));
|
||||
|
||||
using (HashProviderCng hashProvider = new HashProviderCng(hashAlgorithm.Name, null))
|
||||
using (var hashProvider = new HashProviderCng(hashAlgorithm.Name, null))
|
||||
{
|
||||
// Default the buffer size to 4K.
|
||||
byte[] buffer = new byte[4096];
|
||||
|
||||
@@ -3,11 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
using ErrorCode = Interop.NCrypt.ErrorCode;
|
||||
using AsymmetricPaddingMode = Interop.NCrypt.AsymmetricPaddingMode;
|
||||
|
||||
@@ -15,7 +11,7 @@ namespace Internal.Cryptography
|
||||
{
|
||||
internal static partial class CngCommon
|
||||
{
|
||||
public static unsafe byte[] SignHash(this SafeNCryptKeyHandle keyHandle, byte[] hash, AsymmetricPaddingMode paddingMode, void* pPaddingInfo, int estimatedSize)
|
||||
public static unsafe byte[] SignHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan<byte> hash, AsymmetricPaddingMode paddingMode, void* pPaddingInfo, int estimatedSize)
|
||||
{
|
||||
#if DEBUG
|
||||
estimatedSize = 2; // Make sure the NTE_BUFFER_TOO_SMALL scenario gets exercised.
|
||||
@@ -35,11 +31,28 @@ namespace Internal.Cryptography
|
||||
return signature;
|
||||
}
|
||||
|
||||
public static unsafe bool VerifyHash(this SafeNCryptKeyHandle keyHandle, byte[] hash, byte[] signature, AsymmetricPaddingMode paddingMode, void* pPaddingInfo)
|
||||
public static unsafe bool TrySignHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan<byte> hash, Span<byte> signature, AsymmetricPaddingMode paddingMode, void* pPaddingInfo, out int bytesWritten)
|
||||
{
|
||||
ErrorCode error = Interop.NCrypt.NCryptSignHash(keyHandle, pPaddingInfo, hash, hash.Length, signature, signature.Length, out int numBytesNeeded, paddingMode);
|
||||
switch (error)
|
||||
{
|
||||
case ErrorCode.ERROR_SUCCESS:
|
||||
bytesWritten = numBytesNeeded;
|
||||
return true;
|
||||
|
||||
case ErrorCode.NTE_BUFFER_TOO_SMALL:
|
||||
bytesWritten = 0;
|
||||
return false;
|
||||
|
||||
default:
|
||||
throw error.ToCryptographicException();
|
||||
}
|
||||
}
|
||||
|
||||
public static unsafe bool VerifyHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature, AsymmetricPaddingMode paddingMode, void* pPaddingInfo)
|
||||
{
|
||||
ErrorCode errorCode = Interop.NCrypt.NCryptVerifySignature(keyHandle, pPaddingInfo, hash, hash.Length, signature, signature.Length, paddingMode);
|
||||
bool verified = (errorCode == ErrorCode.ERROR_SUCCESS); // For consistency with other AsymmetricAlgorithm-derived classes, return "false" for any error code rather than making the caller catch an exception.
|
||||
return verified;
|
||||
return errorCode == ErrorCode.ERROR_SUCCESS; // For consistency with other AsymmetricAlgorithm-derived classes, return "false" for any error code rather than making the caller catch an exception.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Internal.Cryptography
|
||||
{
|
||||
@@ -29,16 +28,16 @@ namespace Internal.Cryptography
|
||||
if (data.Length - offset < count)
|
||||
throw new ArgumentException(SR.Argument_InvalidOffLen);
|
||||
|
||||
AppendHashDataCore(data, offset, count);
|
||||
AppendHashData(new ReadOnlySpan<byte>(data, offset, count));
|
||||
}
|
||||
|
||||
// Adds new data to be hashed. This can be called repeatedly in order to hash data from noncontiguous sources.
|
||||
// Argument validation is handled by AppendHashData.
|
||||
public abstract void AppendHashDataCore(byte[] data, int offset, int count);
|
||||
|
||||
public abstract void AppendHashData(ReadOnlySpan<byte> data);
|
||||
|
||||
// Compute the hash based on the appended data and resets the HashProvider for more hashing.
|
||||
public abstract byte[] FinalizeHashAndReset();
|
||||
|
||||
public abstract bool TryFinalizeHashAndReset(Span<byte> destination, out int bytesWritten);
|
||||
|
||||
// Returns the length of the byte array returned by FinalizeHashAndReset.
|
||||
public abstract int HashSizeInBytes { get; }
|
||||
|
||||
@@ -53,4 +52,3 @@ namespace Internal.Cryptography
|
||||
public abstract void Dispose(bool disposing);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
using NTSTATUS = Interop.BCrypt.NTSTATUS;
|
||||
using BCryptOpenAlgorithmProviderFlags = Interop.BCrypt.BCryptOpenAlgorithmProviderFlags;
|
||||
@@ -68,30 +66,43 @@ namespace Internal.Cryptography
|
||||
return;
|
||||
}
|
||||
|
||||
public sealed override void AppendHashDataCore(byte[] data, int offset, int count)
|
||||
public sealed override unsafe void AppendHashData(ReadOnlySpan<byte> source)
|
||||
{
|
||||
unsafe
|
||||
NTSTATUS ntStatus = Interop.BCrypt.BCryptHashData(_hHash, source, source.Length, 0);
|
||||
if (ntStatus != NTSTATUS.STATUS_SUCCESS)
|
||||
{
|
||||
fixed (byte* pRgb = data)
|
||||
{
|
||||
NTSTATUS ntStatus = Interop.BCrypt.BCryptHashData(_hHash, pRgb + offset, count, 0);
|
||||
if (ntStatus != NTSTATUS.STATUS_SUCCESS)
|
||||
throw Interop.BCrypt.CreateCryptographicException(ntStatus);
|
||||
}
|
||||
throw Interop.BCrypt.CreateCryptographicException(ntStatus);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed override byte[] FinalizeHashAndReset()
|
||||
{
|
||||
byte[] hash = new byte[_hashSize];
|
||||
NTSTATUS ntStatus = Interop.BCrypt.BCryptFinishHash(_hHash, hash, hash.Length, 0);
|
||||
if (ntStatus != NTSTATUS.STATUS_SUCCESS)
|
||||
throw Interop.BCrypt.CreateCryptographicException(ntStatus);
|
||||
|
||||
ResetHashObject();
|
||||
var hash = new byte[_hashSize];
|
||||
bool success = TryFinalizeHashAndReset(hash, out int bytesWritten);
|
||||
Debug.Assert(success);
|
||||
Debug.Assert(hash.Length == bytesWritten);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public override bool TryFinalizeHashAndReset(Span<byte> destination, out int bytesWritten)
|
||||
{
|
||||
if (destination.Length < _hashSize)
|
||||
{
|
||||
bytesWritten = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
NTSTATUS ntStatus = Interop.BCrypt.BCryptFinishHash(_hHash, destination, _hashSize, 0);
|
||||
if (ntStatus != NTSTATUS.STATUS_SUCCESS)
|
||||
{
|
||||
throw Interop.BCrypt.CreateCryptographicException(ntStatus);
|
||||
}
|
||||
|
||||
bytesWritten = _hashSize;
|
||||
ResetHashObject();
|
||||
return true;
|
||||
}
|
||||
|
||||
public sealed override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
@@ -106,13 +117,7 @@ namespace Internal.Cryptography
|
||||
}
|
||||
}
|
||||
|
||||
public sealed override int HashSizeInBytes
|
||||
{
|
||||
get
|
||||
{
|
||||
return _hashSize;
|
||||
}
|
||||
}
|
||||
public sealed override int HashSizeInBytes => _hashSize;
|
||||
|
||||
private void ResetHashObject()
|
||||
{
|
||||
|
||||
@@ -14,13 +14,15 @@ internal static partial class Interop
|
||||
internal static partial class procfs
|
||||
{
|
||||
internal const string RootPath = "/proc/";
|
||||
internal const string SelfExeFilePath = RootPath + "self/exe";
|
||||
internal const string ProcUptimeFilePath = RootPath + "uptime";
|
||||
private const string ExeFileName = "/exe";
|
||||
private const string StatFileName = "/stat";
|
||||
private const string MapsFileName = "/maps";
|
||||
private const string FileDescriptorDirectoryName = "/fd/";
|
||||
private const string TaskDirectoryName = "/task/";
|
||||
|
||||
internal const string SelfExeFilePath = RootPath + "self" + ExeFileName;
|
||||
internal const string ProcUptimeFilePath = RootPath + "uptime";
|
||||
|
||||
internal struct ParsedStat
|
||||
{
|
||||
// Commented out fields are available in the stat data file but
|
||||
@@ -80,6 +82,11 @@ internal static partial class Interop
|
||||
internal KeyValuePair<long, long> AddressRange;
|
||||
}
|
||||
|
||||
internal static string GetExeFilePathForProcess(int pid)
|
||||
{
|
||||
return RootPath + pid.ToString(CultureInfo.InvariantCulture) + ExeFileName;
|
||||
}
|
||||
|
||||
internal static string GetStatFilePathForProcess(int pid)
|
||||
{
|
||||
return RootPath + pid.ToString(CultureInfo.InvariantCulture) + StatFileName;
|
||||
|
||||
@@ -46,6 +46,38 @@ internal static partial class Interop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static unsafe bool TryCFWriteData(SafeCFDataHandle cfData, Span<byte> destination, out int bytesWritten)
|
||||
{
|
||||
bool addedRef = false;
|
||||
try
|
||||
{
|
||||
cfData.DangerousAddRef(ref addedRef);
|
||||
|
||||
long length = CFDataGetLength(cfData).ToInt64();
|
||||
if (destination.Length < length)
|
||||
{
|
||||
bytesWritten = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
byte* dataBytes = CFDataGetBytePtr(cfData);
|
||||
fixed (byte* destinationPtr = &destination.DangerousGetPinnableReference())
|
||||
{
|
||||
Buffer.MemoryCopy(dataBytes, destinationPtr, destination.Length, length);
|
||||
}
|
||||
|
||||
bytesWritten = (int)length;
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (addedRef)
|
||||
{
|
||||
cfData.DangerousRelease();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,17 @@ internal static partial class Interop
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_DigestCreate")]
|
||||
internal static extern SafeDigestCtxHandle DigestCreate(PAL_HashAlgorithm algorithm, out int cbDigest);
|
||||
|
||||
internal static int DigestUpdate(SafeDigestCtxHandle ctx, ReadOnlySpan<byte> pbData, int cbData) =>
|
||||
DigestUpdate(ctx, ref pbData.DangerousGetPinnableReference(), cbData);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_DigestUpdate")]
|
||||
internal static extern unsafe int DigestUpdate(SafeDigestCtxHandle ctx, byte* pbData, int cbData);
|
||||
private static extern int DigestUpdate(SafeDigestCtxHandle ctx, ref byte pbData, int cbData);
|
||||
|
||||
internal static int DigestFinal(SafeDigestCtxHandle ctx, Span<byte> pbOutput, int cbOutput) =>
|
||||
DigestFinal(ctx, ref pbOutput.DangerousGetPinnableReference(), cbOutput);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_DigestFinal")]
|
||||
internal static extern unsafe int DigestFinal(SafeDigestCtxHandle ctx, byte* pbOutput, int cbOutput);
|
||||
private static extern int DigestFinal(SafeDigestCtxHandle ctx, ref byte pbOutput, int cbOutput);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,13 +17,19 @@ internal static partial class Interop
|
||||
internal static extern SafeHmacHandle HmacCreate(PAL_HashAlgorithm algorithm, ref int cbDigest);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_HmacInit")]
|
||||
internal static extern unsafe int HmacInit(SafeHmacHandle ctx, byte* pbKey, int cbKey);
|
||||
internal static extern unsafe int HmacInit(SafeHmacHandle ctx, [In] byte[] pbKey, int cbKey);
|
||||
|
||||
internal static int HmacUpdate(SafeHmacHandle ctx, ReadOnlySpan<byte> pbData, int cbData) =>
|
||||
HmacUpdate(ctx, ref pbData.DangerousGetPinnableReference(), cbData);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_HmacUpdate")]
|
||||
internal static extern unsafe int HmacUpdate(SafeHmacHandle ctx, byte* pbData, int cbData);
|
||||
private static extern int HmacUpdate(SafeHmacHandle ctx, ref byte pbData, int cbData);
|
||||
|
||||
internal static int HmacFinal(SafeHmacHandle ctx, ReadOnlySpan<byte> pbOutput, int cbOutput) =>
|
||||
HmacFinal(ctx, ref pbOutput.DangerousGetPinnableReference(), cbOutput);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_HmacFinal")]
|
||||
internal static extern unsafe int HmacFinal(SafeHmacHandle ctx, byte* pbOutput, int cbOutput);
|
||||
private static extern unsafe int HmacFinal(SafeHmacHandle ctx, ref byte pbOutput, int cbOutput);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,36 +21,70 @@ internal static partial class Interop
|
||||
out SafeSecKeyRefHandle pPrivateKey,
|
||||
out int pOSStatus);
|
||||
|
||||
private static int RsaEncryptOaep(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
ReadOnlySpan<byte> pbData,
|
||||
int cbData,
|
||||
PAL_HashAlgorithm mgfAlgorithm,
|
||||
out SafeCFDataHandle pEncryptedOut,
|
||||
out SafeCFErrorHandle pErrorOut) =>
|
||||
RsaEncryptOaep(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaEncryptOaep")]
|
||||
private static extern int RsaEncryptOaep(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
byte[] pbData,
|
||||
ref byte pbData,
|
||||
int cbData,
|
||||
PAL_HashAlgorithm mgfAlgorithm,
|
||||
out SafeCFDataHandle pEncryptedOut,
|
||||
out SafeCFErrorHandle pErrorOut);
|
||||
|
||||
private static int RsaEncryptPkcs(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
ReadOnlySpan<byte> pbData,
|
||||
int cbData,
|
||||
out SafeCFDataHandle pEncryptedOut,
|
||||
out SafeCFErrorHandle pErrorOut) =>
|
||||
RsaEncryptPkcs(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, out pEncryptedOut, out pErrorOut);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaEncryptPkcs")]
|
||||
private static extern int RsaEncryptPkcs(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
byte[] pbData,
|
||||
ref byte pbData,
|
||||
int cbData,
|
||||
out SafeCFDataHandle pEncryptedOut,
|
||||
out SafeCFErrorHandle pErrorOut);
|
||||
|
||||
private static int RsaDecryptOaep(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
ReadOnlySpan<byte> pbData,
|
||||
int cbData,
|
||||
PAL_HashAlgorithm mgfAlgorithm,
|
||||
out SafeCFDataHandle pEncryptedOut,
|
||||
out SafeCFErrorHandle pErrorOut) =>
|
||||
RsaDecryptOaep(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaDecryptOaep")]
|
||||
private static extern int RsaDecryptOaep(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
byte[] pbData,
|
||||
ref byte pbData,
|
||||
int cbData,
|
||||
PAL_HashAlgorithm mgfAlgorithm,
|
||||
out SafeCFDataHandle pEncryptedOut,
|
||||
out SafeCFErrorHandle pErrorOut);
|
||||
|
||||
private static int RsaDecryptPkcs(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
ReadOnlySpan<byte> pbData,
|
||||
int cbData,
|
||||
out SafeCFDataHandle pEncryptedOut,
|
||||
out SafeCFErrorHandle pErrorOut) =>
|
||||
RsaDecryptPkcs(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, out pEncryptedOut, out pErrorOut);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaDecryptPkcs")]
|
||||
private static extern int RsaDecryptPkcs(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
byte[] pbData,
|
||||
ref byte pbData,
|
||||
int cbData,
|
||||
out SafeCFDataHandle pEncryptedOut,
|
||||
out SafeCFErrorHandle pErrorOut);
|
||||
@@ -100,24 +134,44 @@ internal static partial class Interop
|
||||
RSAEncryptionPadding padding)
|
||||
{
|
||||
return ExecuteTransform(
|
||||
(out SafeCFDataHandle encrypted, out SafeCFErrorHandle error) =>
|
||||
data,
|
||||
(ReadOnlySpan<byte> source, out SafeCFDataHandle encrypted, out SafeCFErrorHandle error) =>
|
||||
{
|
||||
if (padding == RSAEncryptionPadding.Pkcs1)
|
||||
{
|
||||
return RsaEncryptPkcs(publicKey, data, data.Length, out encrypted, out error);
|
||||
return RsaEncryptPkcs(publicKey, source, source.Length, out encrypted, out error);
|
||||
}
|
||||
|
||||
Debug.Assert(padding.Mode == RSAEncryptionPaddingMode.Oaep);
|
||||
|
||||
return RsaEncryptOaep(
|
||||
publicKey,
|
||||
data,
|
||||
data.Length,
|
||||
source,
|
||||
source.Length,
|
||||
PalAlgorithmFromAlgorithmName(padding.OaepHashAlgorithm),
|
||||
out encrypted,
|
||||
out error);
|
||||
});
|
||||
}
|
||||
|
||||
internal static bool TryRsaEncrypt(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
ReadOnlySpan<byte> source,
|
||||
Span<byte> destination,
|
||||
RSAEncryptionPadding padding,
|
||||
out int bytesWritten)
|
||||
{
|
||||
Debug.Assert(padding.Mode == RSAEncryptionPaddingMode.Pkcs1 || padding.Mode == RSAEncryptionPaddingMode.Oaep);
|
||||
return TryExecuteTransform(
|
||||
source,
|
||||
destination,
|
||||
out bytesWritten,
|
||||
delegate (ReadOnlySpan<byte> innerSource, out SafeCFDataHandle outputHandle, out SafeCFErrorHandle errorHandle)
|
||||
{
|
||||
return padding.Mode == RSAEncryptionPaddingMode.Pkcs1 ?
|
||||
RsaEncryptPkcs(publicKey, innerSource, innerSource.Length, out outputHandle, out errorHandle) :
|
||||
RsaEncryptOaep(publicKey, innerSource, innerSource.Length, PalAlgorithmFromAlgorithmName(padding.OaepHashAlgorithm), out outputHandle, out errorHandle);
|
||||
});
|
||||
}
|
||||
|
||||
internal static byte[] RsaDecrypt(
|
||||
@@ -126,50 +180,52 @@ internal static partial class Interop
|
||||
RSAEncryptionPadding padding)
|
||||
{
|
||||
return ExecuteTransform(
|
||||
(out SafeCFDataHandle decrypted, out SafeCFErrorHandle error) =>
|
||||
data,
|
||||
(ReadOnlySpan<byte> source, out SafeCFDataHandle decrypted, out SafeCFErrorHandle error) =>
|
||||
{
|
||||
if (padding == RSAEncryptionPadding.Pkcs1)
|
||||
{
|
||||
return RsaDecryptPkcs(privateKey, data, data.Length, out decrypted, out error);
|
||||
return RsaDecryptPkcs(privateKey, source, source.Length, out decrypted, out error);
|
||||
}
|
||||
|
||||
Debug.Assert(padding.Mode == RSAEncryptionPaddingMode.Oaep);
|
||||
|
||||
return RsaDecryptOaep(
|
||||
privateKey,
|
||||
data,
|
||||
data.Length,
|
||||
source,
|
||||
source.Length,
|
||||
PalAlgorithmFromAlgorithmName(padding.OaepHashAlgorithm),
|
||||
out decrypted,
|
||||
out error);
|
||||
});
|
||||
}
|
||||
|
||||
private static Interop.AppleCrypto.PAL_HashAlgorithm PalAlgorithmFromAlgorithmName(
|
||||
HashAlgorithmName hashAlgorithmName)
|
||||
internal static bool TryRsaDecrypt(
|
||||
SafeSecKeyRefHandle privateKey,
|
||||
ReadOnlySpan<byte> source,
|
||||
Span<byte> destination,
|
||||
RSAEncryptionPadding padding,
|
||||
out int bytesWritten)
|
||||
{
|
||||
if (hashAlgorithmName == HashAlgorithmName.MD5)
|
||||
{
|
||||
return Interop.AppleCrypto.PAL_HashAlgorithm.Md5;
|
||||
}
|
||||
else if (hashAlgorithmName == HashAlgorithmName.SHA1)
|
||||
{
|
||||
return Interop.AppleCrypto.PAL_HashAlgorithm.Sha1;
|
||||
}
|
||||
else if (hashAlgorithmName == HashAlgorithmName.SHA256)
|
||||
{
|
||||
return Interop.AppleCrypto.PAL_HashAlgorithm.Sha256;
|
||||
}
|
||||
else if (hashAlgorithmName == HashAlgorithmName.SHA384)
|
||||
{
|
||||
return Interop.AppleCrypto.PAL_HashAlgorithm.Sha384;
|
||||
}
|
||||
else if (hashAlgorithmName == HashAlgorithmName.SHA512)
|
||||
{
|
||||
return Interop.AppleCrypto.PAL_HashAlgorithm.Sha512;
|
||||
}
|
||||
|
||||
throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmName.Name);
|
||||
Debug.Assert(padding.Mode == RSAEncryptionPaddingMode.Pkcs1 || padding.Mode == RSAEncryptionPaddingMode.Oaep);
|
||||
return TryExecuteTransform(
|
||||
source,
|
||||
destination,
|
||||
out bytesWritten,
|
||||
delegate (ReadOnlySpan<byte> innerSource, out SafeCFDataHandle outputHandle, out SafeCFErrorHandle errorHandle)
|
||||
{
|
||||
return padding.Mode == RSAEncryptionPaddingMode.Pkcs1 ?
|
||||
RsaDecryptPkcs(privateKey, innerSource, innerSource.Length, out outputHandle, out errorHandle) :
|
||||
RsaDecryptOaep(privateKey, innerSource, innerSource.Length, PalAlgorithmFromAlgorithmName(padding.OaepHashAlgorithm), out outputHandle, out errorHandle);
|
||||
});
|
||||
}
|
||||
|
||||
private static PAL_HashAlgorithm PalAlgorithmFromAlgorithmName(HashAlgorithmName hashAlgorithmName) =>
|
||||
hashAlgorithmName == HashAlgorithmName.MD5 ? PAL_HashAlgorithm.Md5 :
|
||||
hashAlgorithmName == HashAlgorithmName.SHA1 ? PAL_HashAlgorithm.Sha1 :
|
||||
hashAlgorithmName == HashAlgorithmName.SHA256 ? PAL_HashAlgorithm.Sha256 :
|
||||
hashAlgorithmName == HashAlgorithmName.SHA384 ? PAL_HashAlgorithm.Sha384 :
|
||||
hashAlgorithmName == HashAlgorithmName.SHA512 ? PAL_HashAlgorithm.Sha512 :
|
||||
throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmName.Name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,13 +10,12 @@ internal static partial class Interop
|
||||
{
|
||||
internal static partial class AppleCrypto
|
||||
{
|
||||
internal static unsafe void GetRandomBytes(byte* pbBuffer, int count)
|
||||
internal static void GetRandomBytes(ref byte pbBuffer, int count)
|
||||
{
|
||||
Debug.Assert(pbBuffer != null);
|
||||
Debug.Assert(count >= 0);
|
||||
|
||||
int errorCode;
|
||||
int ret = AppleCryptoNative_GetRandomBytes(pbBuffer, count, out errorCode);
|
||||
int ret = AppleCryptoNative_GetRandomBytes(ref pbBuffer, count, out errorCode);
|
||||
|
||||
if (ret == 0)
|
||||
{
|
||||
@@ -30,6 +29,6 @@ internal static partial class Interop
|
||||
}
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative)]
|
||||
private static extern unsafe int AppleCryptoNative_GetRandomBytes(byte* buf, int num, out int errorCode);
|
||||
private static extern int AppleCryptoNative_GetRandomBytes(ref byte buf, int num, out int errorCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
@@ -20,38 +21,89 @@ internal static partial class Interop
|
||||
out SafeSecKeyRefHandle ppKeyOut,
|
||||
out int pOSStatus);
|
||||
|
||||
private static int AppleCryptoNative_GenerateSignature(
|
||||
SafeSecKeyRefHandle privateKey,
|
||||
ReadOnlySpan<byte> pbDataHash,
|
||||
int cbDataHash,
|
||||
out SafeCFDataHandle pSignatureOut,
|
||||
out SafeCFErrorHandle pErrorOut) =>
|
||||
AppleCryptoNative_GenerateSignature(
|
||||
privateKey, ref pbDataHash.DangerousGetPinnableReference(), cbDataHash, out pSignatureOut, out pErrorOut);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative)]
|
||||
private static extern int AppleCryptoNative_GenerateSignature(
|
||||
SafeSecKeyRefHandle privateKey,
|
||||
byte[] pbDataHash,
|
||||
ref byte pbDataHash,
|
||||
int cbDataHash,
|
||||
out SafeCFDataHandle pSignatureOut,
|
||||
out SafeCFErrorHandle pErrorOut);
|
||||
|
||||
private static int AppleCryptoNative_GenerateSignatureWithHashAlgorithm(
|
||||
SafeSecKeyRefHandle privateKey,
|
||||
ReadOnlySpan<byte> pbDataHash,
|
||||
int cbDataHash,
|
||||
PAL_HashAlgorithm hashAlgorithm,
|
||||
out SafeCFDataHandle pSignatureOut,
|
||||
out SafeCFErrorHandle pErrorOut) =>
|
||||
AppleCryptoNative_GenerateSignatureWithHashAlgorithm(
|
||||
privateKey, ref pbDataHash.DangerousGetPinnableReference(), cbDataHash, hashAlgorithm, out pSignatureOut, out pErrorOut);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative)]
|
||||
private static extern int AppleCryptoNative_GenerateSignatureWithHashAlgorithm(
|
||||
SafeSecKeyRefHandle privateKey,
|
||||
byte[] pbDataHash,
|
||||
ref byte pbDataHash,
|
||||
int cbDataHash,
|
||||
PAL_HashAlgorithm hashAlgorithm,
|
||||
out SafeCFDataHandle pSignatureOut,
|
||||
out SafeCFErrorHandle pErrorOut);
|
||||
|
||||
private static int AppleCryptoNative_VerifySignature(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
ReadOnlySpan<byte> pbDataHash,
|
||||
int cbDataHash,
|
||||
ReadOnlySpan<byte> pbSignature,
|
||||
int cbSignature,
|
||||
out SafeCFErrorHandle pErrorOut) =>
|
||||
AppleCryptoNative_VerifySignature(
|
||||
publicKey,
|
||||
ref pbDataHash.DangerousGetPinnableReference(),
|
||||
cbDataHash,
|
||||
ref pbSignature.DangerousGetPinnableReference(),
|
||||
cbSignature,
|
||||
out pErrorOut);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative)]
|
||||
private static extern int AppleCryptoNative_VerifySignature(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
byte[] pbDataHash,
|
||||
ref byte pbDataHash,
|
||||
int cbDataHash,
|
||||
byte[] pbSignature,
|
||||
ref byte pbSignature,
|
||||
int cbSignature,
|
||||
out SafeCFErrorHandle pErrorOut);
|
||||
|
||||
private static int AppleCryptoNative_VerifySignatureWithHashAlgorithm(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
ReadOnlySpan<byte> pbDataHash,
|
||||
int cbDataHash,
|
||||
ReadOnlySpan<byte> pbSignature,
|
||||
int cbSignature,
|
||||
PAL_HashAlgorithm hashAlgorithm,
|
||||
out SafeCFErrorHandle pErrorOut) =>
|
||||
AppleCryptoNative_VerifySignatureWithHashAlgorithm(
|
||||
publicKey,
|
||||
ref pbDataHash.DangerousGetPinnableReference(),
|
||||
cbDataHash,
|
||||
ref pbSignature.DangerousGetPinnableReference(),
|
||||
cbSignature,
|
||||
hashAlgorithm,
|
||||
out pErrorOut);
|
||||
|
||||
[DllImport(Libraries.AppleCryptoNative)]
|
||||
private static extern int AppleCryptoNative_VerifySignatureWithHashAlgorithm(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
byte[] pbDataHash,
|
||||
ref byte pbDataHash,
|
||||
int cbDataHash,
|
||||
byte[] pbSignature,
|
||||
ref byte pbSignature,
|
||||
int cbSignature,
|
||||
PAL_HashAlgorithm hashAlgorithm,
|
||||
out SafeCFErrorHandle pErrorOut);
|
||||
@@ -59,9 +111,9 @@ internal static partial class Interop
|
||||
[DllImport(Libraries.AppleCryptoNative)]
|
||||
private static extern ulong AppleCryptoNative_SecKeyGetSimpleKeySizeInBytes(SafeSecKeyRefHandle publicKey);
|
||||
|
||||
private delegate int SecKeyTransform(out SafeCFDataHandle data, out SafeCFErrorHandle error);
|
||||
private delegate int SecKeyTransform(ReadOnlySpan<byte> source, out SafeCFDataHandle outputHandle, out SafeCFErrorHandle errorHandle);
|
||||
|
||||
private static byte[] ExecuteTransform(SecKeyTransform transform)
|
||||
private static byte[] ExecuteTransform(ReadOnlySpan<byte> source, SecKeyTransform transform)
|
||||
{
|
||||
const int Success = 1;
|
||||
const int kErrorSeeError = -2;
|
||||
@@ -69,7 +121,7 @@ internal static partial class Interop
|
||||
SafeCFDataHandle data;
|
||||
SafeCFErrorHandle error;
|
||||
|
||||
int ret = transform(out data, out error);
|
||||
int ret = transform(source, out data, out error);
|
||||
|
||||
using (error)
|
||||
using (data)
|
||||
@@ -89,6 +141,35 @@ internal static partial class Interop
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryExecuteTransform(
|
||||
ReadOnlySpan<byte> source,
|
||||
Span<byte> destination,
|
||||
out int bytesWritten,
|
||||
SecKeyTransform transform)
|
||||
{
|
||||
SafeCFDataHandle outputHandle;
|
||||
SafeCFErrorHandle errorHandle;
|
||||
|
||||
int ret = transform(source, out outputHandle, out errorHandle);
|
||||
|
||||
using (errorHandle)
|
||||
using (outputHandle)
|
||||
{
|
||||
const int Success = 1;
|
||||
const int kErrorSeeError = -2;
|
||||
switch (ret)
|
||||
{
|
||||
case Success:
|
||||
return CoreFoundation.TryCFWriteData(outputHandle, destination, out bytesWritten);
|
||||
case kErrorSeeError:
|
||||
throw CreateExceptionForCFError(errorHandle);
|
||||
default:
|
||||
Debug.Fail($"transform returned {ret}");
|
||||
throw new CryptographicException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static int GetSimpleKeySizeInBits(SafeSecKeyRefHandle publicKey)
|
||||
{
|
||||
ulong keySizeInBytes = AppleCryptoNative_SecKeyGetSimpleKeySizeInBytes(publicKey);
|
||||
@@ -127,49 +208,68 @@ internal static partial class Interop
|
||||
throw new CryptographicException();
|
||||
}
|
||||
|
||||
internal static byte[] GenerateSignature(SafeSecKeyRefHandle privateKey, byte[] dataHash)
|
||||
internal static byte[] GenerateSignature(SafeSecKeyRefHandle privateKey, ReadOnlySpan<byte> dataHash)
|
||||
{
|
||||
Debug.Assert(privateKey != null, "privateKey != null");
|
||||
Debug.Assert(dataHash != null, "dataHash != null");
|
||||
|
||||
return ExecuteTransform(
|
||||
(out SafeCFDataHandle signature, out SafeCFErrorHandle error) =>
|
||||
dataHash,
|
||||
(ReadOnlySpan<byte> source, out SafeCFDataHandle signature, out SafeCFErrorHandle error) =>
|
||||
AppleCryptoNative_GenerateSignature(
|
||||
privateKey,
|
||||
dataHash,
|
||||
dataHash.Length,
|
||||
source,
|
||||
source.Length,
|
||||
out signature,
|
||||
out error));
|
||||
}
|
||||
|
||||
internal static byte[] GenerateSignature(
|
||||
SafeSecKeyRefHandle privateKey,
|
||||
byte[] dataHash,
|
||||
ReadOnlySpan<byte> dataHash,
|
||||
PAL_HashAlgorithm hashAlgorithm)
|
||||
{
|
||||
Debug.Assert(privateKey != null, "privateKey != null");
|
||||
Debug.Assert(dataHash != null, "dataHash != null");
|
||||
Debug.Assert(hashAlgorithm != PAL_HashAlgorithm.Unknown, "hashAlgorithm != PAL_HashAlgorithm.Unknown");
|
||||
|
||||
return ExecuteTransform(
|
||||
(out SafeCFDataHandle signature, out SafeCFErrorHandle error) =>
|
||||
dataHash,
|
||||
(ReadOnlySpan<byte> source, out SafeCFDataHandle signature, out SafeCFErrorHandle error) =>
|
||||
AppleCryptoNative_GenerateSignatureWithHashAlgorithm(
|
||||
privateKey,
|
||||
dataHash,
|
||||
dataHash.Length,
|
||||
source,
|
||||
source.Length,
|
||||
hashAlgorithm,
|
||||
out signature,
|
||||
out error));
|
||||
}
|
||||
|
||||
internal static bool TryGenerateSignature(
|
||||
SafeSecKeyRefHandle privateKey,
|
||||
ReadOnlySpan<byte> source,
|
||||
Span<byte> destination,
|
||||
PAL_HashAlgorithm hashAlgorithm,
|
||||
out int bytesWritten)
|
||||
{
|
||||
Debug.Assert(privateKey != null, "privateKey != null");
|
||||
Debug.Assert(hashAlgorithm != PAL_HashAlgorithm.Unknown, "hashAlgorithm != PAL_HashAlgorithm.Unknown");
|
||||
|
||||
return TryExecuteTransform(
|
||||
source,
|
||||
destination,
|
||||
out bytesWritten,
|
||||
delegate (ReadOnlySpan<byte> innerSource, out SafeCFDataHandle outputHandle, out SafeCFErrorHandle errorHandle)
|
||||
{
|
||||
return AppleCryptoNative_GenerateSignatureWithHashAlgorithm(
|
||||
privateKey, innerSource, innerSource.Length, hashAlgorithm, out outputHandle, out errorHandle);
|
||||
});
|
||||
}
|
||||
|
||||
internal static bool VerifySignature(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
byte[] dataHash,
|
||||
byte[] signature)
|
||||
ReadOnlySpan<byte> dataHash,
|
||||
ReadOnlySpan<byte> signature)
|
||||
{
|
||||
Debug.Assert(publicKey != null, "publicKey != null");
|
||||
Debug.Assert(dataHash != null, "dataHash != null");
|
||||
Debug.Assert(signature != null, "signature != null");
|
||||
|
||||
SafeCFErrorHandle error;
|
||||
|
||||
@@ -204,13 +304,11 @@ internal static partial class Interop
|
||||
|
||||
internal static bool VerifySignature(
|
||||
SafeSecKeyRefHandle publicKey,
|
||||
byte[] dataHash,
|
||||
byte[] signature,
|
||||
ReadOnlySpan<byte> dataHash,
|
||||
ReadOnlySpan<byte> signature,
|
||||
PAL_HashAlgorithm hashAlgorithm)
|
||||
{
|
||||
Debug.Assert(publicKey != null, "publicKey != null");
|
||||
Debug.Assert(dataHash != null, "dataHash != null");
|
||||
Debug.Assert(signature != null, "signature != null");
|
||||
Debug.Assert(hashAlgorithm != PAL_HashAlgorithm.Unknown);
|
||||
|
||||
SafeCFErrorHandle error;
|
||||
|
||||
@@ -13,5 +13,6 @@ internal static partial class Interop
|
||||
internal const string CryptoNative = "System.Security.Cryptography.Native.OpenSsl";
|
||||
internal const string GlobalizationNative = "System.Globalization.Native";
|
||||
internal const string CompressionNative = "System.IO.Compression.Native";
|
||||
internal const string Libdl = "libdl";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,14 +15,15 @@ internal static partial class Interop
|
||||
internal static unsafe void ForkAndExecProcess(
|
||||
string filename, string[] argv, string[] envp, string cwd,
|
||||
bool redirectStdin, bool redirectStdout, bool redirectStderr,
|
||||
out int lpChildPid, out int stdinFd, out int stdoutFd, out int stderrFd)
|
||||
out int lpChildPid, out int stdinFd, out int stdoutFd, out int stderrFd, bool shouldThrow = true)
|
||||
{
|
||||
byte** argvPtr = null, envpPtr = null;
|
||||
int result = -1;
|
||||
try
|
||||
{
|
||||
AllocNullTerminatedArray(argv, ref argvPtr);
|
||||
AllocNullTerminatedArray(envp, ref envpPtr);
|
||||
int result = ForkAndExecProcess(
|
||||
result = ForkAndExecProcess(
|
||||
filename, argvPtr, envpPtr, cwd,
|
||||
redirectStdin ? 1 : 0, redirectStdout ? 1 : 0, redirectStderr ? 1 :0,
|
||||
out lpChildPid, out stdinFd, out stdoutFd, out stderrFd);
|
||||
|
||||
@@ -21,53 +21,101 @@ internal static partial class Interop
|
||||
/// </remarks>
|
||||
internal enum UnixFileSystemTypes : long
|
||||
{
|
||||
adfs = 0xadf5,
|
||||
adfs = 0xADF5,
|
||||
affs = 0xADFF,
|
||||
afs = 0x5346414F,
|
||||
anoninode = 0x09041934,
|
||||
aufs = 0x61756673,
|
||||
autofs = 0x0187,
|
||||
befs = 0x42465331,
|
||||
bdevfs = 0x62646576,
|
||||
bfs = 0x1BADFACE,
|
||||
binfmt_misc = 0x42494E4D,
|
||||
btrfs = 0x9123683E,
|
||||
ceph = 0x00C36400,
|
||||
cgroupfs = 0x0027E0EB,
|
||||
cifs = 0xFF534D42,
|
||||
coda = 0x73757245,
|
||||
coherent = 0x012FF7B7,
|
||||
cramfs = 0x28cd3d45,
|
||||
cramfs = 0x28CD3D45,
|
||||
debugfs = 0x64626720,
|
||||
devfs = 0x1373,
|
||||
devpts = 0x1CD1,
|
||||
ecryptfs = 0xF15F,
|
||||
efs = 0x00414A53,
|
||||
ext = 0x137D,
|
||||
ext2_old = 0xEF51,
|
||||
ext2 = 0xEF53,
|
||||
ext3 = 0xEF53,
|
||||
ext4 = 0xEF53,
|
||||
fat = 0x4006,
|
||||
fhgfs = 0x19830326,
|
||||
fuseblk = 0x65735546,
|
||||
fusectl = 0x65735543,
|
||||
futexfs = 0x0BAD1DEA,
|
||||
gfsgfs2 = 0x1161970,
|
||||
gpfs = 0x47504653,
|
||||
hfs = 0x4244,
|
||||
hpfs = 0xF995E849,
|
||||
hugetlbfs = 0x958458f6,
|
||||
hugetlbfs = 0x958458F6,
|
||||
inodefs = 0x11307854,
|
||||
inotifyfs = 0x2BAD1DEA,
|
||||
isofs = 0x9660,
|
||||
jffs2 = 0x72b6,
|
||||
jfs = 0x3153464a,
|
||||
// isofs = 0x4004, // R_WIN
|
||||
// isofs = 0x4000, // WIN
|
||||
jffs = 0x07C0,
|
||||
jffs2 = 0x72B6,
|
||||
jfs = 0x3153464A,
|
||||
kafs = 0x6B414653,
|
||||
lustre = 0x0BD00BD0,
|
||||
minix_old = 0x137F, /* orig. minix */
|
||||
minix = 0x138F, /* 30 char minix */
|
||||
minix2 = 0x2468, /* minix V2 */
|
||||
minix2v2 = 0x2478, /* minix V2, 30 char names */
|
||||
msdos = 0x4d44,
|
||||
ncpfs = 0x564c,
|
||||
minix2v2 = 0x2478, /* MINIX V2, 30 char names */
|
||||
minix3 = 0x4D5A,
|
||||
mqueue = 0x19800202,
|
||||
msdos = 0x4D44,
|
||||
nfs = 0x6969,
|
||||
ntfs = 0x5346544e,
|
||||
openprom = 0x9fa1,
|
||||
overlay = 0x794c7630,
|
||||
overlayfs = 0x794c764f,
|
||||
proc = 0x9fa0,
|
||||
qnx4 = 0x002f,
|
||||
nfsd = 0x6E667364,
|
||||
nilfs = 0x3434,
|
||||
novell = 0x564C,
|
||||
ntfs = 0x5346544E,
|
||||
openprom = 0x9FA1,
|
||||
ocfs2 = 0x7461636F,
|
||||
overlay = 0x794C7630,
|
||||
overlayfs = 0x794C764F,
|
||||
panfs = 0xAAD7AAEA,
|
||||
pipefs = 0x50495045,
|
||||
proc = 0x9FA0,
|
||||
pstorefs = 0x6165676C,
|
||||
qnx4 = 0x002F,
|
||||
qnx6 = 0x68191122,
|
||||
ramfs = 0x858458F6,
|
||||
reiserfs = 0x52654973,
|
||||
romfs = 0x7275,
|
||||
rpc_pipefs = 0x67596969,
|
||||
securityfs = 0x73636673,
|
||||
selinux = 0xF97CFF8C,
|
||||
smb = 0x517B,
|
||||
sockfs = 0x534F434B,
|
||||
squashfs = 0x73717368,
|
||||
sysfs = 0x62656572,
|
||||
sysv2 = 0x012FF7B6,
|
||||
sysv4 = 0x012FF7B5,
|
||||
tmpfs = 0x01021994,
|
||||
udf = 0x15013346,
|
||||
ufs = 0x00011954,
|
||||
usbdevice = 0x9fa2,
|
||||
vxfs = 0xa501FCF5,
|
||||
// ufs = 0x54190100, // byteswapped
|
||||
usbdevice = 0x9FA2,
|
||||
v9fs = 0x01021997,
|
||||
vmhgfs = 0xBACBACBC,
|
||||
vxfs = 0xA501FCF5,
|
||||
vzfs = 0x565A4653,
|
||||
xenfs = 0xABBA1974,
|
||||
xenix = 0x012FF7B4,
|
||||
xfs = 0x58465342,
|
||||
xiafs = 0x012FD16D,
|
||||
xia = 0x012FD16D,
|
||||
zfs = 0x2FC12FC1,
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
@@ -130,7 +178,7 @@ internal static partial class Interop
|
||||
/// <returns>The recognized drive type.</returns>
|
||||
private static DriveType GetDriveType(string fileSystemName)
|
||||
{
|
||||
// This list is based primarily on "man fs", "man mount", "mntent.h", "/proc/filesystems",
|
||||
// This list is based primarily on "man fs", "man mount", "mntent.h", "/proc/filesystems", coreutils "stat.c",
|
||||
// and "wiki.debian.org/FileSystem". It can be extended over time as we
|
||||
// find additional file systems that should be recognized as a particular drive type.
|
||||
switch (fileSystemName)
|
||||
@@ -143,91 +191,154 @@ internal static partial class Interop
|
||||
case "umview-mod-umfuseiso9660":
|
||||
return DriveType.CDRom;
|
||||
|
||||
case "aafs":
|
||||
case "adfs":
|
||||
case "affs":
|
||||
case "anoninode":
|
||||
case "anon-inode FS":
|
||||
case "apfs":
|
||||
case "autofs":
|
||||
case "balloon-kvm-fs":
|
||||
case "bdevfs":
|
||||
case "befs":
|
||||
case "bfs":
|
||||
case "bpf_fs":
|
||||
case "btrfs":
|
||||
case "btrfs_test":
|
||||
case "cgroup2fs":
|
||||
case "cgroupfs":
|
||||
case "coh":
|
||||
case "cramfs":
|
||||
case "cramfs-wend":
|
||||
case "daxfs":
|
||||
case "drvfs":
|
||||
case "ecryptfs":
|
||||
case "efivarfs":
|
||||
case "efs":
|
||||
case "exofs":
|
||||
case "ext":
|
||||
case "ext2":
|
||||
case "ext2_old":
|
||||
case "ext3":
|
||||
case "ext2/ext3":
|
||||
case "ext4":
|
||||
case "ext4dev":
|
||||
case "f2fs":
|
||||
case "fat":
|
||||
case "fuseblk":
|
||||
case "fuseext2":
|
||||
case "fusefat":
|
||||
case "futexfs":
|
||||
case "hfs":
|
||||
case "hfs+":
|
||||
case "hfsplus":
|
||||
case "hfsx":
|
||||
case "hostfs":
|
||||
case "hpfs":
|
||||
case "inodefs":
|
||||
case "inotifyfs":
|
||||
case "jbd":
|
||||
case "jbd2":
|
||||
case "jfs":
|
||||
case "jffs":
|
||||
case "jffs2":
|
||||
case "jfs":
|
||||
case "logfs":
|
||||
case "lxfs":
|
||||
case "minix (30 char.)":
|
||||
case "minix v2 (30 char.)":
|
||||
case "minix v2":
|
||||
case "minix":
|
||||
case "minix_old":
|
||||
case "minix2":
|
||||
case "minix2v2":
|
||||
case "minix3":
|
||||
case "mlfs":
|
||||
case "msdos":
|
||||
case "nilfs":
|
||||
case "nsfs":
|
||||
case "ntfs":
|
||||
case "ocfs2":
|
||||
case "omfs":
|
||||
case "openprom":
|
||||
case "overlay":
|
||||
case "overlayfs":
|
||||
case "ntfs":
|
||||
case "pstorefs":
|
||||
case "qnx4":
|
||||
case "qnx6":
|
||||
case "reiserfs":
|
||||
case "rpc_pipefs":
|
||||
case "selinux":
|
||||
case "smackfs":
|
||||
case "squashfs":
|
||||
case "swap":
|
||||
case "sysfs":
|
||||
case "sysv":
|
||||
case "sysv2":
|
||||
case "sysv4":
|
||||
case "tracefs":
|
||||
case "ubifs":
|
||||
case "udf":
|
||||
case "ufs":
|
||||
case "umsdos":
|
||||
case "umview-mod-umfuseext2":
|
||||
case "usbdevfs":
|
||||
case "v9fs":
|
||||
case "vzfs":
|
||||
case "wslfs":
|
||||
case "xenfs":
|
||||
case "xenix":
|
||||
case "xfs":
|
||||
case "xia":
|
||||
case "xiafs":
|
||||
case "xmount":
|
||||
case "zfs":
|
||||
case "zfs-fuse":
|
||||
case "zsmallocfs":
|
||||
return DriveType.Fixed;
|
||||
|
||||
case "9p":
|
||||
case "autofs":
|
||||
case "acfs":
|
||||
case "afs":
|
||||
case "aufs":
|
||||
case "autofs4":
|
||||
case "beaglefs":
|
||||
case "ceph":
|
||||
case "cifs":
|
||||
case "coda":
|
||||
case "coherent":
|
||||
case "curlftpfs":
|
||||
case "davfs2":
|
||||
case "dlm":
|
||||
case "fhgfs":
|
||||
case "flickrfs":
|
||||
case "fuseblk":
|
||||
case "fusedav":
|
||||
case "fusesmb":
|
||||
case "gfsgfs2":
|
||||
case "gfs/gfs2":
|
||||
case "gfs2":
|
||||
case "glusterfs-client":
|
||||
case "gmailfs":
|
||||
case "gpfs":
|
||||
case "ibrix":
|
||||
case "k-afs":
|
||||
case "kafs":
|
||||
case "ltspfs":
|
||||
case "lustre":
|
||||
case "ncpfs":
|
||||
case "nfs":
|
||||
case "nfs4":
|
||||
case "nfsd":
|
||||
case "novell":
|
||||
case "obexfs":
|
||||
case "panfs":
|
||||
case "prl_fs":
|
||||
case "s3ql":
|
||||
case "smb":
|
||||
case "smb2":
|
||||
case "smbfs":
|
||||
case "snfs":
|
||||
case "sshfs":
|
||||
case "sysfs":
|
||||
case "sysv2":
|
||||
case "sysv4":
|
||||
case "vmhgfs":
|
||||
case "vxfs":
|
||||
case "wikipediafs":
|
||||
return DriveType.Network;
|
||||
@@ -239,7 +350,6 @@ internal static partial class Interop
|
||||
case "binfmt_misc":
|
||||
case "cgroup":
|
||||
case "configfs":
|
||||
case "cramfs":
|
||||
case "cryptkeeper":
|
||||
case "cpuset":
|
||||
case "debugfs":
|
||||
|
||||
@@ -16,7 +16,7 @@ internal static partial class Interop
|
||||
POSIX_FADV_SEQUENTIAL = 2, /* sequential I/O access */
|
||||
POSIX_FADV_WILLNEED = 3, /* will need specified pages */
|
||||
POSIX_FADV_DONTNEED = 4, /* don't need the specified pages */
|
||||
POSIX_FADV_NOREUSE = 5, /* data will only be acessed once */
|
||||
POSIX_FADV_NOREUSE = 5, /* data will only be accessed once */
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -66,13 +66,19 @@ internal static partial class Interop
|
||||
return keySize;
|
||||
}
|
||||
|
||||
internal static bool DsaSign(SafeDsaHandle dsa, ReadOnlySpan<byte> hash, int hashLength, ReadOnlySpan<byte> refSignature, out int outSignatureLength) =>
|
||||
DsaSign(dsa, ref hash.DangerousGetPinnableReference(), hashLength, ref refSignature.DangerousGetPinnableReference(), out outSignatureLength);
|
||||
|
||||
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DsaSign")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool DsaSign(SafeDsaHandle dsa, byte[] hash, int hashLength, byte[] refSignature, out int outSignatureLength);
|
||||
private static extern bool DsaSign(SafeDsaHandle dsa, ref byte hash, int hashLength, ref byte refSignature, out int outSignatureLength);
|
||||
|
||||
internal static bool DsaVerify(SafeDsaHandle dsa, ReadOnlySpan<byte> hash, int hashLength, ReadOnlySpan<byte> signature, int signatureLength) =>
|
||||
DsaVerify(dsa, ref hash.DangerousGetPinnableReference(), hashLength, ref signature.DangerousGetPinnableReference(), signatureLength);
|
||||
|
||||
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DsaVerify")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool DsaVerify(SafeDsaHandle dsa, byte[] hash, int hashLength, byte[] signature, int signatureLength);
|
||||
private static extern bool DsaVerify(SafeDsaHandle dsa, ref byte hash, int hashLength, ref byte signature, int signatureLength);
|
||||
|
||||
internal static DSAParameters ExportDsaParameters(SafeDsaHandle key, bool includePrivateParameters)
|
||||
{
|
||||
|
||||
@@ -19,11 +19,14 @@ internal static partial class Interop
|
||||
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpDigestReset")]
|
||||
internal extern static int EvpDigestReset(SafeEvpMdCtxHandle ctx, IntPtr type);
|
||||
|
||||
internal static int EvpDigestUpdate(SafeEvpMdCtxHandle ctx, ReadOnlySpan<byte> d, int cnt) =>
|
||||
EvpDigestUpdate(ctx, ref d.DangerousGetPinnableReference(), cnt);
|
||||
|
||||
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpDigestUpdate")]
|
||||
internal extern static unsafe int EvpDigestUpdate(SafeEvpMdCtxHandle ctx, byte* d, int cnt);
|
||||
private extern static int EvpDigestUpdate(SafeEvpMdCtxHandle ctx, ref byte d, int cnt);
|
||||
|
||||
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpDigestFinalEx")]
|
||||
internal extern static unsafe int EvpDigestFinalEx(SafeEvpMdCtxHandle ctx, byte* md, ref uint s);
|
||||
internal extern static int EvpDigestFinalEx(SafeEvpMdCtxHandle ctx, ref byte md, ref uint s);
|
||||
|
||||
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpMdSize")]
|
||||
internal extern static int EvpMdSize(IntPtr md);
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user